在书中看到给出的C代码,翻译成c++(bcb)后如下:
但在调试过程中发现根本无法捕捉设置DR0后的单步中断异常,结果
iTotal始终为1!!
请看雪老师帮忙检查下面这段翻译后的代码是否有误.
do
{
::WaitForDebugEvent(&DBEvent,INFINITE);
dwState=DBG_EXCEPTION_NOT_HANDLED;
switch(DBEvent.dwDebugEventCode)
{
case EXCEPTION_DEBUG_EVENT:
{
switch(DBEvent.u.Exception.ExceptionRecord.ExceptionCode)
{
case EXCEPTION_BREAKPOINT:
{
++dwBpCnt;
if(dwBpCnt==1)
{
::GetThreadContext(pi.hThread,&Regs);
Regs.Dr0=(DWORD)(::GetProcAddress(::GetModuleHandle("ntdll.dll"),"NtContinue"));
Regs.Dr7=0x101;
::SetThreadContext(pi.hThread,&Regs);
dwState=DBG_CONTINUE;
}
break;
}
case EXCEPTION_SINGLE_STEP:
{
++dwSSCnt;
if(dwSSCnt==1)
{
::GetThreadContext(pi.hThread,&Regs);
Regs.Dr0=Regs.Dr7=0;
::SetThreadContext(pi.hThread,&Regs);
::ReadProcessMemory(pi.hProcess,(LPCVOID)(Regs.Esp+4),&dwAddrProc,sizeof(DWORD),NULL);
::ReadProcessMemory(pi.hProcess,(LPCVOID)dwAddrProc,&Regs,sizeof(CONTEXT),NULL);
Regs.Dr0=dwBreakAddr;
Regs.Dr7=0x101;
::WriteProcessMemory(pi.hProcess,(LPVOID)dwAddrProc,&Regs,sizeof(CONTEXT),NULL);
dwState=DBG_CONTINUE;
}
else if(dwSSCnt==2)
{
::GetThreadContext(pi.hThread,&Regs);
Regs.Dr0=Regs.Dr7=0;
Regs.EFlags|=0x100;
::SetThreadContext(pi.hThread,&Regs);
++iTotal;
dwState=DBG_CONTINUE;
}
else
{
::GetThreadContext(pi.hThread,&Regs);
Regs.EFlags|=0x100;
::SetThreadContext(pi.hThread,&Regs);
++iTotal;
dwState=DBG_CONTINUE;
}
break;
}
}
break;
}
case EXIT_PROCESS_DEBUG_EVENT:
{
++iTotal;
STOP=true;
Edit1->Text=iTotal;
ShowMessage("over!");
//::ExitProcess(-1);
break;
}
}
if(!STOP) {::ContinueDebugEvent(pi.dwProcessId,pi.dwThreadId,dwState);}
}while(!STOP);
[注意]传递专业知识、拓宽行业人脉——看雪讲师团队等你加入!