Nicola Lepetit
Nicola Lepetit

Reputation: 793

Create a conditional breakpoint base on the call stack

I have a common function that is used from many different objects in many different classes. I want to set a breakpoint on that function that triggers only if is the call stack is present a class.

For example, I have these [meta] call stacks

myFunc()
Intermediate.class.intermediateFunction()
Interesting.class.interestingFunction()

myFunc()
Intermediate.class.intermediateFunction()
Boring.class.boringFunction()

I want to set a breakpoint in myFunc() that activates only if the it is indirectly called from the interestingFunction() function.

Upvotes: 0

Views: 340

Answers (1)

Pepelui360
Pepelui360

Reputation: 457

You can query the stack trace programatically, using System.Diagnostics Namespace. Yoy may do something like this:

System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace();
var f = st.GetFrames();
var names = f.Select(f => f.GetMethod().Name).ToList();
if (names.Contains("DoSomething4"))
{
    var a = 0; // Set breakpoint in this line or use Debugger.Launch()
}

You can use #if DEBUG and #endif so this code doesn't go into release

Also, you can create a condition for a breakpoint using this class

Upvotes: 1

Related Questions