Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio Breakpoint Macro to modify a value?

I'm debugging an application (C++), and I've found a point in the code where I want to change a value (via the debugger). So right now, I've got a breakpoint set, whereupon I do:

  • Debugger reaches breakpoint
  • I modify the variable I want to change
  • I hit F5 to continue running
  • lather, rinse, repeat

It's hitting this breakpoint a lot, so I would like to automate this. I would like to set the Breakpoint to run a macro, and continue execution.

However, I have no experience writing VisualStudio macros, so I don't know the commands for modifying a variable of the executing program. I've looked around, but haven't found anything helpful online so far.

like image 636
Tim Avatar asked Oct 06 '10 00:10

Tim


People also ask

How do you break a variable change in Visual Studio?

In fact, you can now right-click a variable name in the Local window and then select Break When Value Changes (see Figure 58). Visual Studio will automatically track your variable, and it will break the application execution when the variable value changes.

Is it possible to change the value of a variable while debugging in C#?

Yes, we can!

How do I Debug a macro in Visual Studio?

Open up a source file from your manage project in Visual Studio and set a breakpoint on a line. Start debugging in Visual Studio by pressing F5. In Excel, open up your worksheet and start debugging your VBA code using Excel's debugger.


1 Answers

I found how to do this with a macro. Initially, I tried using Ctrl-Shift-R to record a macro of keystrokes, but it stopped recording when I did Ctrl-Alt-Q. But I was able to edit the macro to get it to work. So here's what I did, in case anyone else wants to do something similar.

  • Tools -> Macros -> Macro Explorer
  • Right Click -> New macro

    Public Module RecordingModule
        Sub setvalue()
            DTE.Debugger.ExecuteStatement("variable_name=0")
        End Sub
    End Module
    

This macro will execute the assignment statement, setting my variable (in this case, making it a NULL pointer).

  • Right Click on a BreakPoint -> When Hit...
  • Check "Run a macro"
  • Select Macros.MyMacros.RecordingModule.setvalue
  • Check "Continue execution"
  • Click OK

Then, I was able to run my program, automatically adjusting a pointer to NULL as it went. This was very useful for testing, and did not require recompiling.

like image 200
Tim Avatar answered Nov 15 '22 20:11

Tim