## Setting Dynamic Breakpoints in WinDbg

Q100642

The information in this article applies to:

- Microsoft Win32 Software Development Kit (SDK), versions 3.1, 3.5, 3.51, 4.0

## SUMMARY

The WinDbg breakpoint command contains a metacircular interpreter; that is, you can execute commands dynamically once a breakpoint is hit. This allows you to perform complex operations, including breaking when an automatic variable has changed, as described below.
The command interpreter of WinDbg allows any valid C expression to serve as a break condition. For example, to break whenever a static variable has changed, use the following expression in the Expression field of the breakpoint dialog box:

> &<variablename>

In addition, the length should be specified as 4 (the size of a DWORD) in the length field.
This technique does not work for automatic variables because the address of an automatic variable may change depending on the value that the stack pointer has upon entering the function that defines the automatic variable. This is one case where the breakpoint needs to be redefined dynamically.
For this purpose, a breakpoint can be enabled at function start and disabled at function exit, so that the address of the variable is recomputed.

## MORE INFORMATION

Suppose that the name of the function is "subroutine" and the local variable name is "i". The following steps will be used:

1. Start the program and step into the function that defines the automatic variable with the commands:

             g subroutine
             p
             bp500 ={subroutine}&i /r4 /C"?i"

   The breakpoint number is chosen to be large so that the breakpoint will be well out of range of other breakpoints. Note that /r4 indicates a length of 4 because i is an integer. Make this number larger for other data types. The command "?i" prints out the value of i.
2. Next, disable this first breakpoint with the command

             bd500

   because the address of i may change. The breakpoint will be enabled when in the scope of function subroutine.
3. The second breakpoint definition is set at the entry point of the function:

             bp .<FirstLine> /C"be 500;g"

   This is where thebreakpoint is enabled. Note that <FirstLine> is the line number of the first statement in the function subroutine.
4. The last breakpoint is set at the end of the function

             bp .<LastLine> /C"bd 500;g"

   and will disable the breakpoint again. Note that <LastLine> is the line number of the last statement in the function subroutine.
   Note that if the function has more than one exit point, multiple breakpoints may have to be defined.

Program execution stops when breakpoint #500 is hit (for example, the value of i changes), but execution will continue after the other two breakpoints because they contain go ("g") commands.

Additional query words: 3.10 3.50 4.00 95

Keywords :
Issue type :
Technology :

Last Reviewed: January 18, 2000 © 2001 Microsoft Corporation. All rights reserved. Terms of Use.