Visual Studio Breakpoint Macro to modify a value?

后端 未结 5 1169
一个人的身影
一个人的身影 2021-01-05 12:25

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, whereu

相关标签:
5条回答
  • 2021-01-05 13:04

    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).

    0 讨论(0)
  • 2021-01-05 13:08

    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.

    0 讨论(0)
  • 2021-01-05 13:09

    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.

    0 讨论(0)
  • 2021-01-05 13:09

    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;
    }
    
    0 讨论(0)
  • 2021-01-05 13:13

    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 讨论(0)
提交回复
热议问题