Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set a C/C++ memory watchpoint in vscode?

In gdb I can type watch &variable, then continue and gdb will break whenever something writes to that address.

I am using vscode to debug and want to do the same thing (This is different from the watch window, which will only show variable values after a breakpoint has been hit). Is it possible?

I can manually add a breakpoint by clicking the '+' and enter &variable but it never becomes active and says the module has yet to be loaded. I've tried manually entering -exec watch &variable in the debugger console window, but after continuing execution with the play button it hangs (vscode thinks the program is running again but it's not).

There are some github issues for this but they're closed without reason:

  • https://github.com/microsoft/vscode/issues/55931
  • https://github.com/microsoft/vscode/issues/47117
  • https://github.com/microsoft/vscode-debugadapter-node/issues/38
like image 317
jozxyqk Avatar asked Aug 26 '20 21:08

jozxyqk


People also ask

How do I set a memory breakpoint in Visual Studio?

A data breakpoint can be created in any of the listed windows by right-clicking (or Shift + F10) the desired variable and selecting Break When Value Changes while in break mode.

How is run configuration set in VS Code?

Alternatively, you can run your configuration through the Command Palette (Ctrl+Shift+P) by filtering on Debug: Select and Start Debugging or typing 'debug ' and selecting the configuration you want to debug. In addition, the debug status appears in the Status Bar showing the active debug configuration.

How do I set a watchpoint in GDB?

You can force GDB to use only software watchpoints with the set can-use-hw-watchpoints 0 command. With this variable set to zero, GDB will never try to use hardware watchpoints, even if the underlying system supports them.


Video Answer


1 Answers

I had luck watching on an address. For example, if my target variable was at address 0xb79b90, I would execute -exec watch *0xb79b90 in the gdb terminal. Then I'd double check it was added as a hardware watchpoint with -exec info watch.

After continuing execution, the debugger would halt with an exception once the watchpoint is hit. vscode would display the line after the value was changed. I could then continue from there if necessary.

enter image description here

like image 94
Tails86 Avatar answered Nov 15 '22 08:11

Tails86