5,446,542 members and growing! (18,735 online)
Email Password   helpLost your password?
Development Lifecycle » Debug Tips » General     Intermediate License: The Code Project Open License (CPOL)

Toggle hardware data/read/execute breakpoints programmatically

By Michael Chourdakis

Simple code to introduce a hardware breakpoint mechanism.
C++ (VC6, VC7, VC7.1, VC8.0, C++), C++/CLI, C, Windows, Win32, Win64, Architect, Dev, SysAdmin

Posted: 23 Jul 2008
Updated: 23 Jul 2008
Views: 6,075
Bookmarked: 35 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
17 votes for this Article.
Popularity: 5.78 Rating: 4.70 out of 5
1 vote, 5.9%
1
0 votes, 0.0%
2
1 vote, 5.9%
3
1 vote, 5.9%
4
14 votes, 82.4%
5

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:

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:

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

About the Author

Michael Chourdakis


I am a professional in C++/PHP/DSP Developement.
http://www.turboirc.com
Occupation: Software Developer (Senior)
Location: Greece Greece

Other popular Debug Tips articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 9 of 9 (Total in Forum: 9) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralSounds goodmembersvsundar (Vairavan)7:19 12 Aug '08  
GeneralRe: Sounds goodmemberMichael Chourdakis9:05 12 Aug '08  
GeneralCould you add VS2005 solution file also?membernalla0:24 12 Aug '08  
GeneralRe: Could you add VS2005 solution file also?memberMichael Chourdakis1:50 12 Aug '08  
GeneralWould it have to disrupt the thread?membersupercat917:59 26 Jul '08  
GeneralRe: Would it have to disrupt the thread?memberMichael Chourdakis22:15 26 Jul '08  
GeneralInterestingmemberjuggler4:11 24 Jul '08  
GeneralRe: InterestingmemberMichael Chourdakis4:20 24 Jul '08  
GeneralRe: Interestingmemberjuggler4:33 24 Jul '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 23 Jul 2008
Editor: Smitha Vijayan
Copyright 2008 by Michael Chourdakis
Everything else Copyright © CodeProject, 1999-2008
Web07 | Advertise on the Code Project