开发者

How to get stack trace of a running process from within a Visual Studio add-in?

开发者 https://www.devze.com 2023-01-02 02:46 出处:网络
I am writing a Visual开发者_Python百科 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 withi

I am writing a Visual开发者_Python百科 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.


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!


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消