Click here to Skip to main content
15,861,168 members
Articles / Desktop Programming / Win32
Article

Toggle hardware data/read/execute breakpoints programmatically

Rate me:
Please Sign up or sign in to vote.
4.97/5 (41 votes)
23 Jul 2008CPOL2 min read 171K   4.7K   84   44
Simple code to introduce a hardware breakpoint mechanism.

Introduction

I decided to write this article about hardware breakpoints for the following reasons:

  • Visual C++ only supports write-only data breakpoints. You might want to trigger a break when data is read as well.
  • You might not be using Visual C++, so chances are that your debugger uses some slow software-based mechanism.
  • You might want to set/remove a breakpoint programmatically.
  • You may be interested in low level CPU stuff!

Features

  • Works for x86 and x64.
  • Supports upto 4 hardware breakpoints per thread.

Debug Registers

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:

BitsMeaning
0-7Flags 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:

  • 00b - Triggers when code is executed
  • 01b - Triggers when data is written
  • 10b - Reserved
  • 11b - Triggers when data is read or written
24-31

2 bits for each register, defining the size of the breakpoint:

  • 00b - 1 byte
  • 01b - 2 bytes
  • 10b - 8 bytes
  • 11b - 4 bytes

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.

Setting the Breakpoint

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_CODE
    • HWBRK_TYPE_READWRITE
    • HWBRK_TYPE_WRITE
  • Size - Size of the breakpoint:
    • HWBRK_SIZE_1
    • HWBRK_SIZE_2
    • HWBRK_SIZE_4
    • HWBRK_SIZE_8
  • addr - The address of the breakpoint.

The function returns a handle to the breakpoint, to be used later in RemoveHardwareBreakpoint. It can return 0 if:

  • You do not have access to the thread.
  • You have set the maximum number of breakpoints for that thread (4).

Removing the Breakpoint

bool RemoveHardwareBreakpoint(HANDLE hBrk);

Removes the breakpoint, returning true on success.

Sample

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!

History

  • July 24, 2008 - First post.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Greece Greece
I'm working in C++, PHP , Java, Windows, iOS, Android and Web (HTML/Javascript/CSS).

I 've a PhD in Digital Signal Processing and Artificial Intelligence and I specialize in Pro Audio and AI applications.

My home page: https://www.turbo-play.com

Comments and Discussions

 
Questionwho can help me im stuck on it Pin
Mohammed Alkaabi18-Jul-21 5:25
Mohammed Alkaabi18-Jul-21 5:25 
QuestionDll Injection Pin
Member 1295252116-Jan-17 4:17
Member 1295252116-Jan-17 4:17 
QuestionThe hardwarebreakpoint doesn't fire for multithreaded app Pin
lightweave11-Oct-15 22:26
lightweave11-Oct-15 22:26 
AnswerRe: The hardwarebreakpoint doesn't fire for multithreaded app Pin
lightweave12-Oct-15 2:11
lightweave12-Oct-15 2:11 
GeneralRe: The hardwarebreakpoint doesn't fire for multithreaded app Pin
Michael Chourdakis13-Oct-15 8:41
mvaMichael Chourdakis13-Oct-15 8:41 
GeneralRe: The hardwarebreakpoint doesn't fire for multithreaded app Pin
lightweave4-Jan-16 1:01
lightweave4-Jan-16 1:01 
QuestionOnce the breakpoint is reached I don't stop receiving EXCEPTION_SINGLE_STEP event Pin
Al3487624-Mar-15 13:10
Al3487624-Mar-15 13:10 
AnswerRe: Once the breakpoint is reached I don't stop receiving EXCEPTION_SINGLE_STEP event Pin
Michael Chourdakis24-Mar-15 23:21
mvaMichael Chourdakis24-Mar-15 23:21 
GeneralRe: Once the breakpoint is reached I don't stop receiving EXCEPTION_SINGLE_STEP event Pin
Al3487625-Mar-15 6:24
Al3487625-Mar-15 6:24 
GeneralRe: Once the breakpoint is reached I don't stop receiving EXCEPTION_SINGLE_STEP event Pin
Al3487626-Mar-15 8:22
Al3487626-Mar-15 8:22 
GeneralMy vote of 3 Pin
mobtadi23-Feb-15 4:06
mobtadi23-Feb-15 4:06 
SuggestionThere is no need in creating an event Pin
DiesIrae7-Aug-14 15:26
DiesIrae7-Aug-14 15:26 
GeneralRe: There is no need in creating an event Pin
Michael Chourdakis7-Aug-14 16:06
mvaMichael Chourdakis7-Aug-14 16:06 
QuestionBreakpoints don't work under Olly 1 or 2 Pin
ynot6729-Jun-14 23:46
ynot6729-Jun-14 23:46 
AnswerRe: Breakpoints don't work under Olly 1 or 2 Pin
Michael Chourdakis1-Jul-14 1:16
mvaMichael Chourdakis1-Jul-14 1:16 
QuestionThis is awesome! Pin
Stewart_Lynch27-Mar-14 4:31
Stewart_Lynch27-Mar-14 4:31 
Question"th" function question Pin
rabbit_slayer14-Nov-12 3:33
rabbit_slayer14-Nov-12 3:33 
AnswerRe: "th" function question Pin
Michael Chourdakis14-Nov-12 5:38
mvaMichael Chourdakis14-Nov-12 5:38 
AnswerRe: "th" function question Pin
_FKS_7-May-13 13:16
_FKS_7-May-13 13:16 
QuestionBreakpoints don't trigger in the debugger? Pin
Member 86976873-Apr-12 15:02
Member 86976873-Apr-12 15:02 
AnswerRe: Breakpoints don't trigger in the debugger? Pin
Michael Chourdakis3-Apr-12 20:02
mvaMichael Chourdakis3-Apr-12 20:02 
GeneralRe: Breakpoints don't trigger in the debugger? Pin
Member 86976873-Apr-12 21:09
Member 86976873-Apr-12 21:09 
GeneralRe: Breakpoints don't trigger in the debugger? Pin
Member 86976879-Apr-12 15:42
Member 86976879-Apr-12 15:42 
GeneralRe: Breakpoints don't trigger in the debugger? Pin
Member 869768710-Apr-12 9:47
Member 869768710-Apr-12 9:47 
QuestionHWBRK_TYPE_CODE not working as expected in VS 2008 Pin
Brad Heide30-Mar-12 12:27
Brad Heide30-Mar-12 12:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.