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

Using run, break, next, print, cont, & display

Typing run at this point will execute the program to the end, which wouldn't be all that enlightening. What we want to do is stop the execution in mid-stream, and look around the executing program a little. Pick a line of code to stop before, and execute a break command:

(gdb) break 8 Breakpoint 1 at 0x80483d6: file example1a.c, line 8. Now there is a breakpoint at line 8, and if we run the program, the execution will stop there: (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) The debugger tells you where it stopped, and prints the line, execution is paused, and we can go forward one line at a time: (gdb) next 10 switch (i % 5) Execution is now stopped just before the switch statement. The print command can be used to examine variables or expressions (gdb) print i $1 = 0 (gdb) print i%5 $2 = 0 (gdb) So we can look at the source code now, and figure out where, with i = 0, the program execution is going to go: (gdb) next 21 printf(" Beginning:\n "); That's exactly where we expected it to go, right?
Using next again executes the printf() statement, and ends the switch statement and the first time through the while loop. (gdb) next Beginning: 8 for (i = 0; i < 5; ++i) (gdb) Let's work our way through the loop again. (gdb) next 10 switch (i % 5) (gdb) print i $3 = 1 (gdb) print i%5 $4 = 1 (gdb) next 18 printf(" *******\n "); Using next again executes the printf() statement, and ends the switch statement and the second time through the while loop. (gdb) next ******* 19 break; So if we use next again we should end up back at the for() statement, right? (gdb) next 8 for (i = 0; i < 5; ++i) Instead of stepping through the entire program one statement at at time, the cont command will run the program from where it is, stopping only at breakpoints that have been defined. Let's set a breakpoint at the switch statement now so that it will stop everytime at line 10. (gdb) break 10 Breakpoint 2 at 0x80483e8: file example1a.c, line 10. (gdb) cont Continuing. Breakpoint 2, main () at example1a.c:10 10 switch (i % 5) Instead of using the print command to look at the value of the variable i, let's use the display command. (gdb) display i 1: i = 2 (gdb) cont Continuing. * * Breakpoint 2, main () at example1a.c:10 10 switch (i % 5) 1: i = 3 (gdb) Okay, we have things set up now to do the tracing automatically. Each time we use cont, the program will run to the break point and then display the current value of i. (gdb) cont Continuing. * * Breakpoint 2, main () at example1a.c:10 10 switch (i % 5) 1: i = 4 (gdb) cont Continuing. ******* Program exited normally. (gdb)quit linux3[107] %


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