views:

37

answers:

1

I would like Visual Studio debugger to break within a function only when the call is from a specific sequence of callers. Is there a way to set such a breakpoint? Or perhaps some alternative hack?

I ask this in the context of native (C++) as well as managed (C#) code.

+3  A: 

I think you could set a conditional breakpoint that utilizes the System.Diagnostics.StackTrace class.

EDIT: GrayWizardx has pointed out in a comment that this may not be possible. In that case you could cause your code to break programmatically:

#if DEBUG
    // Use StackTrace class in this conditional to determine whether or not to break:
    if (yourConditionIsTrue)
    {
        System.Diagnostics.Debugger.Break();
    }
#endif
Andy West
As ar as I know CBP need to evaluate to true inline without instantiation (i.e. essentially static), not to mention it would be a heck of a CBP to write.
GrayWizardx
@GrayWizardx: That's true - it would be pretty kludgey. Hopefully my new suggestion is better.
Andy West