Thursday, February 14, 2019

Debugging recursive program with gdb Listing 5.10 in Sams Teach Yourself C in days

Debugging Listing 5.10 in Sam's Teach Yourself C++ in 21 days by Jesse Liberty with GDB. Referencing: https://www.thegeekstuff.com/2010/03/debug-c-program-using-gdb/
https://cets.seas.upenn.edu/answers/gcc.html

Output: g++ -d listing5.1.c
Output: $ gdb a.out

(gdb) break 21
Breakpoint 1 at 0x40096e: file listing5.10.c, line 21.
(gdb) run
Starting program: /home/brent/Downloads/Cpp/Debugger/a.out
Enter number to find: 5



Breakpoint 1, fib (n=5) at listing5.10.c:24
24       std::cout << "Processing fib(" << n << ")... ";
(gdb) print n
$1 = 5
(gdb) c
Continuing.
Processing fib(5)... Call fib(3) and fib(4).

Breakpoint 1, fib (n=3) at listing5.10.c:24
24       std::cout << "Processing fib(" << n << ")... ";
(gdb) print n
$2 = 3
(gdb) c
Continuing.
Processing fib(3)... Call fib(1) and fib(2).

Breakpoint 1, fib (n=1) at listing5.10.c:24
24       std::cout << "Processing fib(" << n << ")... ";
(gdb) print n
$3 = 1
(gdb) c
Continuing.
Processing fib(1)... Return 1!

Breakpoint 1, fib (n=2) at listing5.10.c:24
24       std::cout << "Processing fib(" << n << ")... ";
(gdb) c
Continuing.
Processing fib(2)... Return 1!

Breakpoint 1, fib (n=4) at listing5.10.c:24
24       std::cout << "Processing fib(" << n << ")... ";
(gdb) print n
$4 = 4
(gdb) c
Continuing.
Processing fib(4)... Call fib(2) and fib(3).

Breakpoint 1, fib (n=2) at listing5.10.c:24
24       std::cout << "Processing fib(" << n << ")... ";
(gdb) print n
$5 = 2
(gdb) c
Continuing.
Processing fib(2)... Return 1!

Breakpoint 1, fib (n=3) at listing5.10.c:24
24       std::cout << "Processing fib(" << n << ")... ";
(gdb) print n
$6 = 3
(gdb) c
Continuing.
Processing fib(3)... Call fib(1) and fib(2).

Breakpoint 1, fib (n=1) at listing5.10.c:24
24       std::cout << "Processing fib(" << n << ")... ";
(gdb) print n
$7 = 1
(gdb) c
Continuing.
Processing fib(1)... Return 1!

Breakpoint 1, fib (n=2) at listing5.10.c:24
24       std::cout << "Processing fib(" << n << ")... ";
(gdb) print n
$8 = 2
(gdb) c
Continuing.
Processing fib(2)... Return 1!
5 is the 5th Fibonacci number
[Inferior 1 (process 19913) exited normally]

No comments:

Post a Comment