Click here to Skip to main content
12,887,625 members (33,737 online)
Click here to Skip to main content
Add your own
alternative version


81 bookmarked
Posted 23 Jul 2008

Toggle hardware data/read/execute breakpoints programmatically

, 23 Jul 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
Simple code to introduce a hardware breakpoint mechanism.


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!


  • 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:

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).

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

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:
  • 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.


int __stdcall WinMain(HINSTANCE,HINSTANCE,LPSTR,int)
 char c1[100] = {0};
 lstrcpyA(c1,"Hello 1");
 HANDLE hX1 = 0;

 hX1 = SetHardwareBreakpoint(GetCurrentThread(), 
   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);
 return 0; 

I wait for your comments and questions!


  • July 24, 2008 - First post.


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


About the Author

Michael Chourdakis
Greece Greece
I'm working in C++, PHP , Java, Windows, iOS and Android.

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

My home page:

You may also be interested in...


Comments and Discussions

Questionmeaning of hThread? Pin
rrdillon13-Oct-11 8:44
memberrrdillon13-Oct-11 8:44 
AnswerRe: meaning of hThread? Pin
Michael Chourdakis13-Oct-11 8:50
memberMichael Chourdakis13-Oct-11 8:50 
GeneralRe: meaning of hThread? Pin
rrdillon15-Oct-11 14:45
memberrrdillon15-Oct-11 14:45 
Thx for the reply. Yes, that is unfortunate.

So, do you know how Visual Studio does it then when you use it to set a data breakpoint? This works on multiple threads. Does it set the debug bits on every thread (including new ones that get created after you set the data breakpoint) ?

GeneralRe: meaning of hThread? Pin
Michael Chourdakis15-Oct-11 21:12
memberMichael Chourdakis15-Oct-11 21:12 
GeneralGood Job Pin
imbadyc8-Jun-11 22:05
memberimbadyc8-Jun-11 22:05 
GeneralMixed-mode applications Pin
Brendan Wilson13-Aug-10 12:35
memberBrendan Wilson13-Aug-10 12:35 
GeneralRe: Mixed-mode applications Pin
Michael Chourdakis15-Aug-10 3:43
memberMichael Chourdakis15-Aug-10 3:43 
GeneralRe: Mixed-mode applications Pin
Brendan Wilson15-Aug-10 9:43
memberBrendan Wilson15-Aug-10 9:43 
GeneralRe: Mixed-mode applications Pin
Ivan Kolev22-Feb-12 23:02
memberIvan Kolev22-Feb-12 23:02 
GeneralSounds good Pin
svsundar (Vairavan)12-Aug-08 6:19
membersvsundar (Vairavan)12-Aug-08 6:19 
GeneralRe: Sounds good Pin
Michael Chourdakis12-Aug-08 8:05
memberMichael Chourdakis12-Aug-08 8:05 
QuestionCould you add VS2005 solution file also? Pin
nalla11-Aug-08 23:24
membernalla11-Aug-08 23:24 
AnswerRe: Could you add VS2005 solution file also? Pin
Michael Chourdakis12-Aug-08 0:50
memberMichael Chourdakis12-Aug-08 0:50 
QuestionWould it have to disrupt the thread? Pin
supercat926-Jul-08 16:59
membersupercat926-Jul-08 16:59 
AnswerRe: Would it have to disrupt the thread? Pin
Michael Chourdakis26-Jul-08 21:15
memberMichael Chourdakis26-Jul-08 21:15 
GeneralInteresting Pin
juggler24-Jul-08 3:11
memberjuggler24-Jul-08 3:11 
GeneralRe: Interesting Pin
Michael Chourdakis24-Jul-08 3:20
memberMichael Chourdakis24-Jul-08 3:20 
GeneralRe: Interesting Pin
juggler24-Jul-08 3:33
memberjuggler24-Jul-08 3:33 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170424.1 | Last Updated 24 Jul 2008
Article Copyright 2008 by Michael Chourdakis
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid