watching a variable change using GDB

Posted by Naka0naka on Tue 23 Dec 2008 12:03 PM — 5 posts, 30,534 views.

#0
I need to monitor a global variable as it changes. I intend to use hook-stop but how can I tell if the stop was due to the watch I set? Please advise.
USA #1
As you can see in Nick's gdb guide [1], you get output like this when using watchpoints:

(gdb) cont
Continuing.
Hardware watchpoint 7: obj

Old value = (OBJ_DATA *) 0x0
New value = (OBJ_DATA *) 0x855f758
0x08119f0a in do_eat (ch=0x855e9a8, argument=0xbfffda24 "pie") at misc.c:686
686 if ( (obj = find_obj(ch, argument, TRUE)) == NULL )
(gdb)



[1] http://www.gammon.com.au/forum/?id=3653
#2
Thanks for the reply. I guess I did not explain the problem clear enough.

I want to watch a global variable passively, without having to type c or continue at the gdb prompt everytime the global variable was modified and stops at the gdb prompt.

I tried to use the following:
define hook-stop
continue
end

The hook-stop appears to work only for one stop and it didn't print out the watched info.

Please advise.

Australia Forum Administrator #3
I got it to work like this ...

I chose a global variable (num_descriptors), and typed:


watch num_descriptors


I saw this:


(gdb) watch num_descriptors
Hardware watchpoint 1: num_descriptors


Then I typed "info watch" :


(gdb) info watch
Num Type           Disp Enb Address    What
1   hw watchpoint  keep y              num_descriptors


This shows it is watchpoint 1. Now I added commands to be executed:


(gdb) commands 1 
Type commands for when breakpoint 1 is hit, one per line.
End with a line saying just "end".
> echo watchpoint reached!\n
> print num_descriptors
> cont
> end


Now after connecting with a new character (I already had one on) I see this:


Wed Dec 24 13:41:42 2008 :: Nick (10.0.0.102) has connected.
Hardware watchpoint 1: num_descriptors

Old value = 1
New value = 2
0x0810f5fe in new_descriptor(int) (new_desc=5) at comm.c:1098
1098	   if( ++num_descriptors > sysdata.maxplayers )
watchpoint reached!
$4 = 2


The lines in italic are my command output, and the MUD continues without stopping at the watchpoint. Actually you can see that the output is pretty explanatory anyway, so all you really need is:


commands 1
cont
end

Amended on Wed 24 Dec 2008 03:37 AM by Nick Gammon
#4
Thanks Nick, that is the solution I'm looking for.