简单的硬件断点使用
/*---------------------------------------------------------------------// DataBP.cpp : Demonstrate setting data access breakpoint manually.
Software Debugging by Raymond Zhang, All rights reserved.
---------------------------------------------------------------------*/
#include "stdafx.h"
#include <windows.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
CONTEXT cxt;
HANDLE hThread=GetCurrentThread();
DWORD dwTestVar=0;
if(!IsDebuggerPresent())
{
printf("This sample can only run within a debugger.\n");
return E_FAIL;
}
cxt.ContextFlags=CONTEXT_DEBUG_REGISTERS|CONTEXT_FULL;
if(!GetThreadContext(hThread,&cxt))
{
printf("Failed to get thread context.\n");
return E_FAIL;
}
cxt.Dr0=(DWORD) &dwTestVar;
cxt.Dr7=0xF0001;//4 bytes length read& write breakponits
if(!SetThreadContext(hThread,&cxt))
{
printf("Failed to set thread context.\n");
return E_FAIL;
}
dwTestVar=1;
GetThreadContext(hThread,&cxt);
printf("Break into debuger with DR6=%X.\n",cxt.Dr6);
printf("Break into debuger with DR0=%X.\n",cxt.Dr0);
return S_OK;
}
typedef struct DECLSPEC_ALIGN(16) _CONTEXT {
//
// Register parameter home addresses.
//
DWORD64 P1Home;
DWORD64 P2Home;
DWORD64 P3Home;
DWORD64 P4Home;
DWORD64 P5Home;
DWORD64 P6Home;
//
// Control flags.
//
DWORD ContextFlags;
DWORD MxCsr;
//
// Segment Registers and processor flags.
//
WORD SegCs;
WORD SegDs;
WORD SegEs;
WORD SegFs;
WORD SegGs;
WORD SegSs;
DWORD EFlags;
//
// Debug registers
//
DWORD64 Dr0;
DWORD64 Dr1;
DWORD64 Dr2;
DWORD64 Dr3;
DWORD64 Dr6;
DWORD64 Dr7;
//
// Integer registers.
//
DWORD64 Rax;
DWORD64 Rcx;
DWORD64 Rdx;
DWORD64 Rbx;
DWORD64 Rsp;
DWORD64 Rbp;
DWORD64 Rsi;
DWORD64 Rdi;
DWORD64 R8;
DWORD64 R9;
DWORD64 R10;
DWORD64 R11;
DWORD64 R12;
DWORD64 R13;
DWORD64 R14;
DWORD64 R15;
//
// Program counter.
//
DWORD64 Rip;
//
// MMX/floating point state.
//
M128 Xmm0;
M128 Xmm1;
M128 Xmm2;
M128 Xmm3;
M128 Xmm4;
M128 Xmm5;
M128 Xmm6;
M128 Xmm7;
M128 Xmm8;
M128 Xmm9;
M128 Xmm10;
M128 Xmm11;
M128 Xmm12;
M128 Xmm13;
M128 Xmm14;
M128 Xmm15;
//
// Legacy floating point state.
//
LEGACY_SAVE_AREA FltSave;
DWORD Fill;
//
// Special debug control registers.
//
DWORD64 DebugControl;
DWORD64 LastBranchToRip;
DWORD64 LastBranchFromRip;
DWORD64 LastExceptionToRip;
DWORD64 LastExceptionFromRip;
DWORD64 Fill1;
} CONTEXT, *PCONTEXT;
页:
[1]