UNIX> cat -n rec2.cpp 1 #includeTo use gdb, we compile the program with the -g flag. That makes the program run slower, but it also stores information that helps you debug.2 using namespace std; 3 4 5 void a(int i) 6 { 7 int j; 8 9 j = i*5; 10 printf("In procedure a: i = %d, j = %d\n", i, j); 11 if (i > 0) a(i-1); 12 printf("Later In procedure a: i = %d, j = %d\n", i, j); 13 } 14 15 main() 16 { 17 int i; 18 19 i = 16; 20 a(3); 21 printf("main: %d\n", i); 22 } UNIX>
We will run the program under the control of gdb. Each time we hit a breakpoint, we will run the where command and the print command so that you can see the values that are currently on the stack:
Try it out. You should see how the output of gdb matches the lecture note example of running rec2.cpp:
UNIX> gdb rec2 GNU gdb 6.3.50-20050815 (Apple version gdb-1461.2) (Fri Mar 5 04:43:10 UTC 2010) Copyright 2004 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 "x86_64-apple-darwin"...Reading symbols for shared libraries ... done (gdb) break 11 Breakpoint 1 at 0x100000c9a: file rec2.cpp, line 11. (gdb) break 20 Breakpoint 2 at 0x100000cd2: file rec2.cpp, line 20. (gdb) run Starting program: /Users/plank/tmp-class/Recursion/rec2 Reading symbols for shared libraries ++. done Breakpoint 2, main () at rec2.cpp:20 20 a(3); (gdb) where #0 main () at rec2.cpp:20 (gdb) cont Continuing. In procedure a: i = 3, j = 15 Breakpoint 1, a (i=3) at rec2.cpp:11 11 if (i > 0) a(i-1); (gdb) where #0 a (i=3) at rec2.cpp:11 #1 0x0000000100000cdc in main () at rec2.cpp:20 (gdb) print i $1 = 3 (gdb) print j $2 = 15 (gdb) info locals // info locals will print the values of all local variables j = 5 (gdb) cont Continuing. In procedure a: i = 2, j = 10 Breakpoint 1, a (i=2) at rec2.cpp:11 11 if (i > 0) a(i-1); (gdb) where #0 a (i=2) at rec2.cpp:11 #1 0x0000000100000caa in a (i=3) at rec2.cpp:11 #2 0x0000000100000cdc in main () at rec2.cpp:20 (gdb) print i $3 = 2 (gdb) print j $4 = 10 (gdb) cont Continuing. In procedure a: i = 1, j = 5 Breakpoint 1, a (i=1) at rec2.cpp:11 11 if (i > 0) a(i-1); (gdb) where #0 a (i=1) at rec2.cpp:11 #1 0x0000000100000caa in a (i=2) at rec2.cpp:11 #2 0x0000000100000caa in a (i=3) at rec2.cpp:11 #3 0x0000000100000cdc in main () at rec2.cpp:20 (gdb) print i $5 = 1 (gdb) print j $6 = 5 (gdb) cont Continuing. In procedure a: i = 0, j = 0 Breakpoint 1, a (i=0) at rec2.cpp:11 11 if (i > 0) a(i-1); (gdb) where #0 a (i=0) at rec2.cpp:11 #1 0x0000000100000caa in a (i=1) at rec2.cpp:11 #2 0x0000000100000caa in a (i=2) at rec2.cpp:11 #3 0x0000000100000caa in a (i=3) at rec2.cpp:11 #4 0x0000000100000cdc in main () at rec2.cpp:20 (gdb) print i $7 = 0 (gdb) print j $8 = 0 (gdb) cont Continuing. Later In procedure a: i = 0, j = 0 Later In procedure a: i = 1, j = 5 Later In procedure a: i = 2, j = 10 Later In procedure a: i = 3, j = 15 main: 16 Program exited normally. (gdb) quit UNIX>