views:

38

answers:

3

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.

A: 

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
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
@Alex: A good answer, however the OP is asking for help on writing/recording a Visual Studio Macro.
Dennis Roche
as others have said, I want a Visual Studio macro (nothing to do with Excel or #define)
Tim
A: 

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
yes, I tried recording a macro to do "Ctrl-Alt-Q", but it stopped recording when the window appeared.
Tim
@Tim: Would it be possible to just add a line or two to do the assignment in the code instead?
Jerry Coffin
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
A: 

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