UMBC CMSC 201 Fall '02 CSEE | 201 | 201 F'02 | lectures | news | help

Tracing Variables with GDB

Sometimes it's nice to watch how a variable changes during execution. The watch command lets you set watchpoints, and the debugger will notify you every time a watched variable changes and break at that point.

Warning: The watchpoints are only valid in the scope of that variable.

To set a watchpoint on a variable, your program has to be executing within the scope of that variable. I want to watch how the variable i changes, so I'll originally set a breakpoint at line 7. This is after i has been declared so it is within the scope of i.

linux1[92] % gdb a.out GNU gdb 19991004 Copyright 1998 Free Software Foundation, Inc. GDB is free software, covered by the GNU General Public License, and you are welcome to change it and/or distribute copies of it under certain conditions. Type "show copying" to see the conditions. There is absolutely no warranty for GDB. Type "show warranty" for details. This GDB was configured as "i386-redhat-linux"... (gdb) list 1 2 #include <stdio.h> 3 4 int main() 5 { 6 int i; 7 8 for (i = 0; i < 5; ++i) 9 { 10 switch (i % 5) (gdb) break 7 Breakpoint 1 at 0x80483d6: file example1a.c, line 7. Next I'll run the program and set a watch on i. Since I want you to see that the watchpoint on i is stopping the program rather than the first breakpoint, I'll delete breakpoint 1 now. (gdb) run Starting program: /afs/umbc.edu/users/s/b/sbogar1/home/debug/a.out Breakpoint 1, main () at example1a.c:8 8 for (i = 0; i < 5; ++i) (gdb) watch i Hardware watchpoint 2: i (gdb) delete 1 Now let's continue and see what happens to i (gdb) cont Continuing. #0 main () at example1a.c:8 8 for (i = 0; i < 5; ++i) Hardware watchpoint 2: i Old value = 134518036 New value = 0 0x80483de in main () at example1a.c:8 8 for (i = 0; i < 5; ++i) So, it stopped the program when the value of i was changed. It reports both the old and the new values of i and shows the line of code where the change occured, complete with file name and line number. This could be pretty useful. To continue again let's just use the shorthand for cont, c. (gdb) c Continuing. #0 0x80483de in main () at example1a.c:8 8 for (i = 0; i < 5; ++i) Beginning: Hardware watchpoint 2: i Old value = 0 New value = 1 0x8048460 in main () at example1a.c:8 8 for (i = 0; i < 5; ++i) Let's just keep continuing. (gdb) c Continuing. #0 0x8048460 in main () at example1a.c:8 8 for (i = 0; i < 5; ++i) ******* Hardware watchpoint 2: i Old value = 1 New value = 2 0x8048460 in main () at example1a.c:8 8 for (i = 0; i < 5; ++i) (gdb) c Continuing. #0 0x8048460 in main () at example1a.c:8 8 for (i = 0; i < 5; ++i) * * Hardware watchpoint 2: i Old value = 2 New value = 3 0x8048460 in main () at example1a.c:8 8 for (i = 0; i < 5; ++i) (gdb) c Continuing. #0 0x8048460 in main () at example1a.c:8 8 for (i = 0; i < 5; ++i) * * Hardware watchpoint 2: i Old value = 3 New value = 4 0x8048460 in main () at example1a.c:8 8 for (i = 0; i < 5; ++i) (gdb) c Continuing. #0 0x8048460 in main () at example1a.c:8 8 for (i = 0; i < 5; ++i) ******* Hardware watchpoint 2: i Old value = 4 New value = 5 0x8048460 in main () at example1a.c:8 8 for (i = 0; i < 5; ++i) (gdb) c Continuing. #0 0x8048460 in main () at example1a.c:8 8 for (i = 0; i < 5; ++i) Watchpoint 2 deleted because the program has left the block in which its expression is valid. 0x2aade207 in ?? () from /lib/libc.so.6 (gdb) c Continuing. Program exited normally. (gdb) c The program is not being run. (gdb) quit linux1[93] %


CSEE | 201 | 201 F'02 | lectures | news | help

Monday, 11-Nov-2002 17:40:24 EST