Setting watchpoint for watching writes on a variable in C++ - GDB
Watchpoint basics:
Watchpoints on non-global variables:
- Watchpoints are set on the variables, not on the functions or on the lines of code.
- When the watched variables are read or written, the watchpoint gets triggered and the program's execution stops.
Watchpoints on non-global variables:
- To set a watchpoint on a non-global variable, we must first set a breakpoint that will stop the program in the scope of the variable to be watched.
- The watchpoint can be set after the program stops at the above set breakpoint.
- To get notified about the 'writes' on a variable, we need to use the GDB's command 'watch'.
watchpoint.cpp
Code:#include <iostream> using namespace std; class dummyA { int x; public: dummyA () { x = 0; } void test () { x++; } }; int main () { cout << "\nG'Morning"; dummyA obj; obj.test (); return 0; }
- In the above shown code, we intend to watch the non-static class variable 'x'.
- The non-static class variable x can be accessed by an object of its respective class, which in the current case is declared in the function main().
- Setting the breakpoint on the function main() where the class object obj is declared.
Code:(gdb) b main Breakpoint 1 at 0x40074c: file watchpoint.cpp, line 22.
- Since, GDB hasn't yet reached the variable declaration point of the class object 'obj' through which we intend to call the class variable 'x', it is not able to find the class object 'obj' in the file 'watchpoint.cpp'.
Code:(gdb) watch obj.x No symbol "obj" in current context.
- We need to run the program till it reaches the above set breakpoint.
Code:(gdb) r Breakpoint 1, main () at watchpoint.cpp:22 22 cout << "\nG'Morning";
- Now since GDB has reached the scope of the variable we intend to watch, we can safely set the watchpoint.
Code:(gdb) watch obj.x Hardware watchpoint 2: obj.x
- Instead of traversing line by line, we can simply issue the continue command 'c'. The continue command continues the code execution silently till the watchpoint gets triggered by the constructor call (through the statement dummyA obj;) which changes the value of the class variable x.
Code:(gdb) c Continuing. Hardware watchpoint 2: obj.x Old value = -8912 New value = 0 dummyA::dummyA (this=0x7fffffffdc40) at watchpoint.cpp:12 12 }
- The continue command again continues the code execution silently till the watchpoint gets triggered by the test function call (through the statement obj.test ();) which increments the value of the class variable x.
Code:(gdb) c Continuing. Hardware watchpoint 2: obj.x Old value = 0 New value = 1 dummyA::test (this=0x7fffffffdc40) at watchpoint.cpp:17 17 }
- The continue command again continues the code execution silently, reaches the end of the main function and finds that the watchpoint variable is out of scope and hence it deletes the watchpoint.
Code:(gdb) c Continuing. Watchpoint 2 deleted because the program has left the block in which its expression is valid. 0x00007ffff7325b7d in __libc_start_main () from /lib64/libc.so.6
Total Comments 0