5

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.

johnnyRose
  • 7,310
  • 17
  • 40
  • 61
Jack
  • 61
  • 1
  • 4

2 Answers2

4

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!

Chris Schmich
  • 29,128
  • 5
  • 77
  • 94
  • Thanks a lot for the answer! I will definitely give this a try. – Jack Jun 03 '10 at 00:39
  • Any chance you know how to get the line numbers and/or function signatures? – Earlz Jan 10 '13 at 05:26
  • Nevermind, figured it out(but it's not pretty) see http://stackoverflow.com/a/14251675/69742 – Earlz Jan 10 '13 at 05:59
  • See http://go4answers.webhost4life.com/Example/get-exception-information-call-stack-7287.aspx. Basically, you can call `DTE.Debugger.GetExpression` to evaluate expressions through the debugger. This might help you get the exception information. – Chris Schmich Dec 06 '13 at 22:07
-1

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.

logicnp
  • 5,796
  • 1
  • 28
  • 32
  • Yes I am having the add-in run in Visual Studio while I am debugging a process in the same Visual Studio window and I need access to that process' stack trace inside of my add-in. – Jack Jun 02 '10 at 05:10