如何以编程方式检查 Visual Studio 扩展中的堆栈? [英] How can I programmatically examine the stack in my Visual Studio extension?

查看:22
本文介绍了如何以编程方式检查 Visual Studio 扩展中的堆栈?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 VS 扩展中,假设代码刚刚遇到断点,并且处于中断模式.如何以编程方式检查堆栈?另外,有没有办法找出最后执行的语句是什么?

In a VS extension, assume that the code has just hit a breakpoint, and is in break mode. How can I programmatically examine the stack? Also, is there a way to figure out what the last executed statement was?

我找不到高级示例.有 hello-world 类型示例 但他们主要专注于在 Visual Studio IDE 中添加/修改 UI 元素.

I haven't been able to find an advanced sample. There are hello-world type samples but they are mostly focused on adding/modifying UI elements in Visual Studio IDE.

推荐答案

您需要挂钩 EnvDTE.Events.DebuggerEvents.OnEnterBreakMode 事件或等效事件,以了解进程何时停止(和因此有一个调用堆栈).小心保留对 EnvDTE.Events.DebuggerEvents 的引用,否则它可能会被垃圾收集并且与事件处理程序的连接丢失(通常这不会在 C# 中发生,但由于 EnvDTE 事件的方式COM 包装器已实现,这是一个已知问题).

You'll need to hook into the EnvDTE.Events.DebuggerEvents.OnEnterBreakMode event or equivalent to know when the process stops (and thus has a callstack). Be careful to keep a reference to EnvDTE.Events.DebuggerEvents otherwise it can be garbage collected and the connection to your event handler lost (normally this can't happen in C# but because of the way the EnvDTE event COM wrappers are implemented this is a known issue).

一旦调试器处于中断模式,您可以像这样迭代EnvDte.Debugger.CurrentThread.StackFrames:

Once the debugger is in break mode, you can iterate EnvDte.Debugger.CurrentThread.StackFrames like so:

foreach (var frame in dte.Debugger.CurrentThread.StackFrames.Cast<EnvDTE.StackFrame>())
    ...

如果您希望修改当前线程/堆栈或获取比 EnvDTE 公开的更多详细信息,这也是可能的,但并非微不足道.有一个名为 IDebuggerInternal 的 COM 接口直接公开这些东西,但它不是从公共 MS DLL 导出的.但是,由于它是一个 COM 接口,您可以在 C# 中重新声明它并将 SVsShellDebugger 实例转换为它.如果你想走这条路,我建议反汇编 Microsoft.VisualStudio.Debugger.Interop.Internal, Version=11.0.0.0 (e.g. with dotPeek) 来获取接口定义和 GUID.

If you wish to modify the current thread/stack or get more details than EnvDTE exposes, this is also possible but non-trivial. There is a COM interface called IDebuggerInternal that exposes these things directly, but it's not exported from the public MS DLLs. However, since it's a COM interface, you can re-declare it in C# and cast the SVsShellDebugger instance to it. If you want to go down this route I suggest disassembling Microsoft.VisualStudio.Debugger.Interop.Internal, Version=11.0.0.0 (e.g. with dotPeek) to get the interface definition and GUID.

这篇关于如何以编程方式检查 Visual Studio 扩展中的堆栈?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆