7

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.

Tim
  • 8,912
  • 3
  • 39
  • 57

5 Answers5

13

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.

Tim
  • 8,912
  • 3
  • 39
  • 57
4

Looking for similar today and found that you can also use the 'Print a message:' option instead of a macro. Values from code can be printed by placing them inside {}. The key is that VS will also evaluate the content as an expression - so {variable_name=0} should achieve the same as the macro example.

0

Select "Condition..." and write an assignment for the variable in question in the "Condition:" textbox. This will naturally resolve to "true" with it not being an actual conditional test. Therefore, the breakpoint is never hit, and your variable has been set accordingly.

Jakub Keller
  • 465
  • 3
  • 13
  • I tried it and received a gift: The breakpoint can not be set. This expression has side effects and will not be evaluated. – sergiol Jan 05 '17 at 18:11
0

If you are think of a macro in the same way as Microsoft excel, then you're out of luck. It doesn't quite work that way.

In C++, a macro refers to a small inline function created with #define. It is a preprocessor, so a macro is like using a replace on all its references with its body.

For example:

#define add(a,b) ((a)+(b))

int main() {
  int a=3, b=4, c=5, d=6, e, f;
  d = add(a,b);
  e = add(c,d);
}

Would like to the c++ compiler as:

int main() {
  int a=3, b=4, c=5, ...;
  d = ((a)+(b));
  e = ((c)+(d));
}

Now, back to your question. If the variable is within scope at this breakpoint, just set it from within your code:

myVar = myValue;

If it is not, but it is guaranteed to exist, you may need a little hack. Say that this variable is an int, make a global int pointer. If this variable is static, make sure to set it to its address, and back to NULL inside it's scope. If it is dynamic, you may need some extra work. Here is an example:

int* globalIntPointer;

void func() {
  *globalIntPointer = 3;
  //...
}

int main() {
  int a = 5;
  globalIntPointer = &a;
  func();
  //...
  globalIntPointer = NULL; // for safety sake
  return 0;
}
Alexander Rafferty
  • 6,134
  • 4
  • 33
  • 55
  • no. no. he wants to know how to do it using a Visual Studio macro that can be run when a breakpoint is hit, without modifying code. – Chubsdad Oct 06 '10 at 01:14
  • A good answer, however the OP is asking for help on writing/recording a Visual Studio Macro. – Dennis Oct 06 '10 at 01:14
  • as others have said, I want a Visual Studio macro (nothing to do with Excel or #define) – Tim Oct 06 '10 at 01:36
0

You can execute a VS macro when a breakpoint is hit (open the breakpoints window, right click on the breakpoint in question, and select "When Hit..." off the popup menu). I'm less certain about writing a macro that modifies a variable of the program under debug though -- I've never done that, and a quick try with attempting to record a macro to do it doesn't seem to work (all it records is activating the right window, not changing the value).

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • yes, I tried recording a macro to do "Ctrl-Alt-Q", but it stopped recording when the window appeared. – Tim Oct 06 '10 at 02:08
  • @Tim: Would it be possible to just add a line or two to do the assignment in the code instead? – Jerry Coffin Oct 06 '10 at 03:11
  • I'm trying to find a way that doesn't force a full recompile. I did find a way to do it though, so I'll share that. – Tim Oct 06 '10 at 17:41