UMBC CMSC 201
Fall '06

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

Breakpoints At Events

We've seen breakpoints set at specific lines in the first example (break 8). We can also set breakpoints based on events, like a variable changing to a specific value, or entering a specific function. Some examples of typical commands are:

watch x > 3 break MyFantasticSortFunction watch value < 0.001 In this example, we'll originally set a breakpoint when main() is entered, then two breakpoints based on the value of i, when i == 1 and when i == 4. Let's see what happens. linux1[104] % 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 main Breakpoint 1 at 0x80483d6: file example1a.c, line 8. (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 == 1 Hardware watchpoint 2: i == 1 (gdb) watch i == 4 Hardware watchpoint 3: i == 4 (gdb) cont Continuing. #0 main () at example1a.c:8 8 for (i = 0; i < 5; ++i) #0 main () at example1a.c:8 8 for (i = 0; i < 5; ++i) Beginning: Hardware watchpoint 2: i == 1 Old value = 0 New value = 1 0x8048460 in main () at example1a.c:8 8 for (i = 0; i < 5; ++i) (gdb) print i $1 = 1 (gdb) Notice that it stopped when the value of i became 1. The Old value printed (0 or FALSE) is NOT the value of i, but the value of the expression i == 1 and the New value shown is the value of the expression i == 1 (1 or TRUE). Although this is not obvious at the moment, you'll see it's so later. I decided to print the value of i while the program was stopped. (gdb) cont Continuing. #0 0x8048460 in main () at example1a.c:8 8 for (i = 0; i < 5; ++i) #0 0x8048460 in main () at example1a.c:8 8 for (i = 0; i < 5; ++i) ******* Hardware watchpoint 2: i == 1 Old value = 1 New value = 0 0x8048460 in main () at example1a.c:8 8 for (i = 0; i < 5; ++i) (gdb) print i $2 = 2 Notice that the watch stops the program again when the value of i is changed from 1 to 2. Now you'll see that Old value is 1 or TRUE and New value is 0 or FALSE. Again I printed the value of i, which just became 2. (gdb) cont Continuing. #0 0x8048460 in main () at example1a.c:8 8 for (i = 0; i < 5; ++i) #0 0x8048460 in main () at example1a.c:8 8 for (i = 0; i < 5; ++i) * * * * Hardware watchpoint 3: i == 4 Old value = 0 New value = 1 0x8048460 in main () at example1a.c:8 8 for (i = 0; i < 5; ++i) (gdb) print i $3 = 4 (gdb) cont Continuing. #0 0x8048460 in main () at example1a.c:8 8 for (i = 0; i < 5; ++i) #0 0x8048460 in main () at example1a.c:8 8 for (i = 0; i < 5; ++i) ******* Hardware watchpoint 3: i == 4 Old value = 1 New value = 0 0x8048460 in main () at example1a.c:8 8 for (i = 0; i < 5; ++i) (gdb) print i $4 = 5 (gdb) cont Continuing. #0 0x8048460 in main () at example1a.c:8 8 for (i = 0; i < 5; ++i) #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. Watchpoint 3 deleted because the program has left the block in which its expression is valid. 0x2aade207 in ?? () from /lib/libc.so.6 Notice that the watchpoints were deleted when main ended. This is because i is no longer a variable and the expressions could not be evaluated. (gdb) cont Continuing. Program exited normally. (gdb) quit linux1[105] %


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