如何从 Visual Studio 加载项中获取正在运行的进程的堆栈跟踪?

发布于 2024-09-04 00:41:36 字数 335 浏览 4 评论 0原文

我正在用 C# 编写一个 Visual Studio 外接程序,该外接程序将在我在同一 Visual Studio 窗口中调试进程时运行,并且我需要从外接程序中访问该进程的堆栈跟踪。我尝试将此代码放入我的加载项中,但它返回加载项的堆栈跟踪,而不是我正在调试的进程。

System.Diagnostics.StackTrace stacktrace = new System.Diagnostics.StackTrace(true);
System.Diagnostics.StackFrame stackframe = stacktrace.GetFrame(0);

任何帮助将不胜感激。

I am writing a Visual Studio add-in in C# which will run while I am debugging a process in the same Visual Studio window and I need access to that the process' stack trace from within my add-in. I tried putting this code into my add-in but it returns the add-in's stack trace, not the process I am debugging.

System.Diagnostics.StackTrace stacktrace = new System.Diagnostics.StackTrace(true);
System.Diagnostics.StackFrame stackframe = stacktrace.GetFrame(0);

Any help would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独夜无伴 2024-09-11 00:41:36

最简单的方法是通过 DTE 自动化对象向调试器询问堆栈帧。您应该可以通过加载项使用 DTE 对象。您想要的属性是 Debugger.CurrentThread.StackFrames< /代码>。如果您使用的是 .NET 4,您可以这样做:

    static string GetCurrentStackTrace(DTE dte)
    {
        bool canGetStackTrace =
            (dte != null) &&
            (dte.Debugger != null) &&
            (dte.Debugger.CurrentThread != null) &&
            (dte.Debugger.CurrentThread.StackFrames != null);

        if (!canGetStackTrace)
            return string.Empty;

        return string.Join(
            "\n",
            dte.Debugger.CurrentThread.StackFrames.Cast<StackFrame>().Select(f => f.FunctionName)
        );
    }

否则,您可以这样做:

    static string GetCurrentStackTrace(DTE dte)
    {
        bool canGetStackTrace =
            (dte != null) &&
            (dte.Debugger != null) &&
            (dte.Debugger.CurrentThread != null) &&
            (dte.Debugger.CurrentThread.StackFrames != null);

        if (!canGetStackTrace)
            return string.Empty;

        StringBuilder stackTrace = new StringBuilder();

        foreach (StackFrame frame in dte.Debugger.CurrentThread.StackFrames)
        {
            stackTrace.AppendFormat("{0}\n", frame.FunctionName);
        }

        return stackTrace.ToString();
    }

痛苦且复杂的方法是使用 ICorDebugStackWalk64 分别获取托管堆栈和本机堆栈,然后手动将它们缝合在一起。既然您是 VS 插件,那么您不妨让调试器为您完成繁重的工作!

The easiest way would be to ask the debugger for the stack frames through the DTE automation object. The DTE object should be available to you through your add-in. The property you want is Debugger.CurrentThread.StackFrames. If you're using .NET 4, you can do:

    static string GetCurrentStackTrace(DTE dte)
    {
        bool canGetStackTrace =
            (dte != null) &&
            (dte.Debugger != null) &&
            (dte.Debugger.CurrentThread != null) &&
            (dte.Debugger.CurrentThread.StackFrames != null);

        if (!canGetStackTrace)
            return string.Empty;

        return string.Join(
            "\n",
            dte.Debugger.CurrentThread.StackFrames.Cast<StackFrame>().Select(f => f.FunctionName)
        );
    }

Otherwise, you can do:

    static string GetCurrentStackTrace(DTE dte)
    {
        bool canGetStackTrace =
            (dte != null) &&
            (dte.Debugger != null) &&
            (dte.Debugger.CurrentThread != null) &&
            (dte.Debugger.CurrentThread.StackFrames != null);

        if (!canGetStackTrace)
            return string.Empty;

        StringBuilder stackTrace = new StringBuilder();

        foreach (StackFrame frame in dte.Debugger.CurrentThread.StackFrames)
        {
            stackTrace.AppendFormat("{0}\n", frame.FunctionName);
        }

        return stackTrace.ToString();
    }

The painful and involved way would be to use ICorDebug and StackWalk64 to get managed and native stacks separately and then stitch them together by hand. Since you're a VS add-in, you might as well let the debugger do the heavy lifting for you!

就此别过 2024-09-11 00:41:36

该代码按预期工作,因为当您调用该代码时,您的加载项(在 VS 下)是“当前进程”。

我不确定“当前正在运行的进程”是什么意思(您的意思是在 VS 下运行/调试的进程吗?),但我认为不可能获得另一个进程的堆栈跟踪。

The code is working as expected since when you call the code, your add-in (under VS) is the "current process".

I am not sure what you mean by "currently running process" (do you mean the process being run/debugged under VS?), but I don't think its possible to get a stack-trace of another process.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文