Using run, break, next, print, cont, & displayTyping 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: Now there is a breakpoint at line 8, and if we run the program, the execution will stop there:(gdb) break 8 Breakpoint 1 at 0x80483d6: file example1a.c, line 8. 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) 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) Execution is now stopped just before the switch statement. The print command can be used to examine variables or expressions(gdb) next 10 switch (i % 5) So we can look at the source code now, and figure out where, with i = 0, the program execution is going to go:(gdb) print i $1 = 0 (gdb) print i%5 $2 = 0 (gdb)
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. Let's work our way through the loop again.(gdb) next Beginning: 8 for (i = 0; i < 5; ++i) (gdb)
Using next again executes the printf() statement, and ends the switch
statement and the second time through the while loop.
So if we use next again we should end up back at the for() statement, right?(gdb) next ******* 19 break; 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) next 8 for (i = 0; i < 5; ++i) Instead of using the print command to look at the value of the variable i, let's use the display command.(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) 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) display i 1: i = 2 (gdb) cont Continuing. * * Breakpoint 2, main () at example1a.c:10 10 switch (i % 5) 1: i = 3 (gdb) (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 S'05 | lectures | news | resources | help |
|