![]() |
Development Lifecycle »
Debug Tips »
General
Intermediate
License: The Code Project Open License (CPOL)
Toggle hardware data/read/execute breakpoints programmaticallyBy Michael ChourdakisSimple code to introduce a hardware breakpoint mechanism. |
C++ (VC6, VC7, VC7.1, VC8.0), C++/CLI, C, Windows, Win32, Win64, Architect, Dev, SysAdmin
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
I decided to write this article about hardware breakpoints for the following reasons:
x86/x64 contains a set of debug registers, named DR0, DR1, DR2, DR3, DR6, and DR7. These registers are 32-bit when in 32-bit mode, and 64-bit when in long mode. DR0, DR1, DR2, and DR3 contain the linear addresses of the breakpoint, and DR7 contains the bits explained here:
| Bits | Meaning |
| 0-7 | Flags for each of the 4 debug registers (2 for each). The first flag is set to specify a local breakpoint (so the CPU resets the flag when switching tasks), and the second flag is set to specify a global breakpoint. In Windows, obviously, you can only use the first flag (although I haven't tried the second). |
| 16-23 |
2 bits for each register, defining when the breakpoint will be triggered:
|
| 24-31 |
2 bits for each register, defining the size of the breakpoint:
|
We use SetThreadContext to set the necessary flags for the thread. After that, when the breakpoint is triggered, an exception of the value EXCEPTION_SINGLE_STEP is raised.
HANDLE SetHardwareBreakpoint(HANDLE hThread,HWBRK_TYPE Type,HWBRK_SIZE Size,void* s);
hThread - Handle to the thread for which the breakpoint is to be set.Type - Type of the breakpoint:HWBRK_TYPE_CODEHWBRK_TYPE_READWRITEHWBRK_TYPE_WRITESize - Size of the breakpoint:HWBRK_SIZE_1HWBRK_SIZE_2HWBRK_SIZE_4HWBRK_SIZE_8addr - The address of the breakpoint.The function returns a handle to the breakpoint, to be used later in RemoveHardwareBreakpoint. It can return 0 if:
bool RemoveHardwareBreakpoint(HANDLE hBrk);
Removes the breakpoint, returning true on success.
int __stdcall WinMain(HINSTANCE,HINSTANCE,LPSTR,int) { char c1[100] = {0}; lstrcpyA(c1,"Hello 1"); HANDLE hX1 = 0; hX1 = SetHardwareBreakpoint(GetCurrentThread(), HWBRK_TYPE_READWRITE,HWBRK_SIZE_4,c1); __try { volatile char a1 = c1[2]; // To ensure that it won't be optimized out. } __except(GetExceptionCode() == STATUS_SINGLE_STEP) { MessageBoxA(0,"Breakpoint hit!",0,MB_OK); } RemoveHardwareBreakpoint(hX1); return 0; }
I wait for your comments and questions!
| You must Sign In to use this message board. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 23 Jul 2008 Editor: Smitha Vijayan |
Copyright 2008 by Michael Chourdakis Everything else Copyright © CodeProject, 1999-2009 Web19 | Advertise on the Code Project |