Subscribe to RSS

Conditional breakpoints in WinDBG

Back to MainPage.  Back to ProgrammingStuff.

I use conditional breakpoints in WinDBG just often enough that I forget the syntax.  Here's how I set a conditional breakpoint:

bp 0x12345678 ".if @@c++( pPasture->countSheep > 16 ) {} .else { gc } "

What does this mean?  It means that the debugger will evaluate my condition each time we reach address 0x12345678.  If my condition is true ( if there are more than 16 sheep in the pasture ), we'll break.  Otherwise, WinDBG will 'gc' which means "continue (go) from a conditional break".

One word of caution, though.  Having to evaluate the condition every time an address is hit can be expensive for the debugger / debugee.  This might slow down your application significantly, if you're watch a very busy piece of code.

Simple and really useful.  It's just tricky to remember the syntax.

Variations on breakpoint location

There are four main ways to specify the address to break at:

  • By raw address
    bp 0x12345678
  • By function+offset
    bp main+9f
  • By C++ class and member function
    bp CClass::CFun
  • By source line  (Note that both quotes must be `graves`, under the tilde (~) key in US 101 layout)
    bp `main.cpp:512`

If you're working on a large project, you might find that resolving symbols takes awhile for the last three methods above.  To narrow the search, you can specify which module to search, like:

bp Class.dll!CClass::CFun

You can add a condition after any of the above address syntaxes.

Workspacing

One last note on breakpoints, pertaining to their persistence in your workspace.  Breakpoints set with "bu" are saved in your workspace, those set as "bp" are not.  So if you want to persist your breakpoint, follow the above examples for the syntax, and replace bp with bu.  bu will delay resolution of the breakpoint, whereas bp will resolve immediately.  I guess the reason for this is that the address of interest may change between application runs, but if we delay resolution, we re-evaluate the address and can handle dynamically loaded libraries etc.

Back to MainPage.  Back to ProgrammingStuff.

Edited on Tue, Dec 29, 2009 at 3:20 a.m.