Home > Techniques > Mystery? SOLVED!

Mystery? SOLVED!

Lets say you want to set a break point, but only break into the debugger when you havn’t gotten to this point via a given code path. You essentially want to only break when a given function is NOT in the stack trace. How do you do this? In VisualStudio (2003 at least), you don’t. The magic of windbg!

I like to use scripts cause I can be very verbose, but you could do this on one line as well. First, just set a breakpoint:

bu `mymodule!myfile.cpp:92` "$$><C:\\windbgscripts\\myscript.txt"

That command sets a breakpoint that will save in a workspace, on the file/line specified. Further, it executes the script specified in the quotes whenever you hit this breakpoint.

Now comes the fun part. Whats in the script?

.if ( @@c++(this->;;m_cRef) != 0){   .foreach (line {k100})   {   .if ($SPAT("line","*MyClass::MyFunc*")) {gc}   };k   

}  .else {gc}

Look at that madness!!!!! Basically, all I am doing here is checking if the refcount of ‘this’ is zero. If it is, I just continue with the ‘gc’ command (gc is ‘go from conditional breakpoint’). If it isn’t zero, I am interested in it. Then I use the .foreach command. .foreach tokenizes anything in windbg. It can take command or external files as input. In this case, I give it the ‘k100′ command. This dumps a stacktrace with a depth of 100. Each token is assigned to the variable ‘line’ and the code is executed. The .if statements use the $SPAT command which check if 2 strings are equal. The cool thing is that you can put wildcards in there. So I just put in some strings from code paths which I don’t want to break, and then do a ‘gc’. This essentially causes me to only break in the debugger when those strings are NOT in my stacktrace!

WOW!

Categories: Techniques Tags: , ,
  1. No comments yet.
  1. No trackbacks yet.