LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Blogs > TheIndependentAquarius
User Name
Password

Notices



Rate this Entry

Setting watchpoint for watching writes on a variable in C++ - GDB

Posted 01-16-2012 at 07:14 AM by TheIndependentAquarius
Updated 01-18-2012 at 10:48 AM by TheIndependentAquarius

Watchpoint basics:
  • 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
Posted in GNU GDB
Views 3460 Comments 0
« Prev     Main     Next »
Total Comments 0

Comments

 

  



All times are GMT -5. The time now is 12:54 PM.

Main Menu
Advertisement

Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration