Visual Studio断点以堆栈状态为条件

Vas*_*aka 11 c++ debugging conditional-breakpoint visual-studio

Visual Studio可以在断点命中时打印调用堆栈,并且可以在满足条件时停止,是否有任何方法可以组合它并在从另一个选定的函数调用函数时停止,并忽略所有其他调用?

Ome*_*viv 4

我相信做到这一点的唯一方法是使用宏。右键单击断点,选择“When Hit..”,选择“运行宏”,然后将其指向一个类似于以下内容的宏:

 Sub ContinueUnlessCalledFromRightContext()
    For Each frame As EnvDTE.StackFrame In DTE.Debugger.CurrentThread.StackFrames
        If (frame.FunctionName.Contains("SomeOtherMethodsName") Then Exit Function
    Next

    DTE.Debugger.Go() ` we weren't called from the right context so continue execution.
End Sub
Run Code Online (Sandbox Code Playgroud)

以上是半伪代码;我实际上没有测试它,但应该进行一些小的编辑。

请注意,如果多次命中断点,这将非常慢,因为从断点运行宏本身就非常慢。

顺便说一句,如果你问的是 .NET / C#,那就简单多了,你可以在上面设置一个条件断点

new System.Diagnostics.StackTrace().ToString().Contains("SomeOtherMethodsName")
Run Code Online (Sandbox Code Playgroud)

...然后就完成了。

  • 未来注意事项:[当前版本的 Visual Studio 中不存在宏](https://social.msdn.microsoft.com/Forums/vstudio/en-US/d8410838-085b-4647-8c42-e31b669c9f11/macros-在-visual-studio-11-developer-preview?forum=vsx)。我愿意接受任何有关执行 OP 在 VS2013 及更高版本中要求的操作的建议。 (4认同)