Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windbg Set Conditional Breakpoints that depends on Call Stack

The problem: I need to make a script or an expression that that doesn't break if somewhere on callstack is a function with a specific name.

Specific question: How can I get functions on callstack to a list of strings ?

Example:

Module!MyFunctionWithConditionalBreakpoint    
Module!Function1   
Module!Function2    
Module!Function3  


Module!MyFunctionWithConditionalBreakpoint    
Module!Function1   
Module!ClassA:MemberFunction    
Module!Function3

I want Module!MyFunctionWithConditionalBreakpoint to break only if the call cames from Module!ClassA:MemberFunction

I need this in unmanaged code. Managed solution is something like

System.Diagnostics.StackTrace().ToString().Contains("YourMethodName")

like image 429
cprogrammer Avatar asked Oct 17 '11 09:10

cprogrammer


2 Answers

Why not set a breakpoint on entering Module!ClassA:MemberFunction to enable a breakpoint for Module!MyFunctionWithConditionalBreakpoint and upon leaving Module!ClassA:MemberFunction disabling it?

like image 44
deemok Avatar answered Oct 09 '22 21:10

deemok


In WinDbg you may set a conditional breakpoint using special $spat function:

bp Module!MyFunctionWithConditionalBreakpoint "r $t0 = 0;.foreach (v { k }) { .if ($spat(\"v\", \"*Module!ClassA:MemberFunction*\")) { r $t0 = 1;.break } }; .if($t0 = 0) { gc }"

In pseudo-code it will be something like:

t0 = 0
foreach (token in k-command result) {
  if (token.contains("Module!ClassA:MemberFunction")) {
    t0 = 1
    break
  }
}
if (t0 == 0) {
  // continue execution
} else {
  // break into the debugger
}
like image 103
Sebastian Avatar answered Oct 09 '22 21:10

Sebastian