Visual Studio debugging mode macros

The situation – you are in a debugging session, and have some local variables. Local variables and warlking throught the stack is not good enough for you. You would like some way how to display some list of items in a nice form.

Fortunatelly, the Visual Studio macros can be used in the running debugging session. Lets say I have my sesssion like this in my Factorial calculating method:

I know it is possible to display local variables and using Call Stack go to the n-th recursion and find arguments in there, but I can display all that info using single VS VBA macro.

To open Macro IDE, I can hit Alt+F11, or Go to the Tools > Macros > Macro IDE.

Open Module1 and insert following code in there:


    Sub DumpExecPath()
        Dim currentStackFrame As EnvDTE.StackFrame
        currentStackFrame = DTE.Debugger.CurrentStackFrame

        For Each frame As EnvDTE.StackFrame In DTE.Debugger.CurrentThread.StackFrames
            If (frame.Module = "") Then Exit For
            DumpExecPathStackFrame(frame)
        Next

    End Sub

    Sub DumpExecPathStackFrame(frame As EnvDTE.StackFrame)
        Dim outputWindow As EnvDTE.OutputWindow
        outputWindow = DTE.Windows.Item(EnvDTE.Constants.vsWindowKindOutput).Object

        Dim a As String
        For Each arg As EnvDTE.Expression In frame.Arguments
            If (Len(a) > 0) Then a = a + ", "
            a = a + arg.Name + " = " + arg.Value
        Next

        outputWindow.ActivePane.OutputString(frame.FunctionName + "(" + a + ")" + Environment.NewLine)
    End Sub

After you save your macro you can return to Visual Studio. Now, when you open a debugging session, you can take advantage of written macro, when you go to the Command Window, write down full name of macro to execute it:

>Macros.MyMacros.Module1.DumpExecPath

Because we have used Output Window to write our Macro results, we need to switch to it to see results:

You can take advantage of macros to display also local variables using this code:

 


    Sub DumpLocals()
        Dim outputWindow As EnvDTE.OutputWindow
        outputWindow = DTE.Windows.Item(EnvDTE.Constants.vsWindowKindOutput).Object

        Dim currentStackFrame As EnvDTE.StackFrame
        currentStackFrame = DTE.Debugger.CurrentStackFrame

        outputWindow.ActivePane.OutputString("*Dumping Local Variables*" + vbCrLf)
        For Each exp As EnvDTE.Expression In currentStackFrame.Locals
            outputWindow.ActivePane.OutputString(exp.Name + " = " + exp.Value.ToString() + vbCrLf)
        Next
    End Sub

Enjoy ..
 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s