CS202 Lecture notes -- Recursion Debugging Example

  • James S Plank
  • Directory: /home/jplank/cs202/Recursion/debug.html
  • Lecture notes: http://web.eecs.utk.edu/~jplank/plank/classes/cs202/Notes/Recursion/debug.html
  • Last Modified: Mon Oct 28 16:41:49 EDT 2019
    This is a simple example of using gdb to print the stack. Below we debug rec2.cpp using gdb and we set breakpoints at lines 13 and 22. First, let's take a look at those lines:

    /* Line  1 */    #include <cstdio>
    /* Line  2 */    using namespace std;
    /* Line  3 */    
    /* Line  4 */    /* This is a slightly more complex recursion example, where the
    /* Line  5 */       procedure a() calls itself recursively twice. */
    /* Line  6 */    
    /* Line  7 */    void a(int i)
    /* Line  8 */    {
    /* Line  9 */      int j;
    /* Line 10 */    
    /* Line 11 */      j = i*5;
    /* Line 12 */      printf("In procedure a: i = %d, j = %d\n", i, j);
    /* Line 13 */      if (i > 0) a(i-1);
    /* Line 14 */      printf("Later In procedure a: i = %d, j = %d\n", i, j);
    /* Line 15 */    }
    /* Line 16 */    
    /* Line 17 */    int main()
    /* Line 18 */    {
    /* Line 19 */      int i;
    /* Line 20 */      
    /* Line 21 */      i = 16;
    /* Line 22 */      a(3);
    /* Line 23 */      printf("main: %d\n", i);
    /* Line 24 */      return 0;
    /* Line 25 */    }
    

    To 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.

    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> g++ -g src/rec2.cpp
    UNIX> gdb a.out
    GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-115.el7
    Copyright (C) 2013 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later 
    This is free software: you are free to change and redistribute it.
    There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
    and "show warranty" for details.
    This GDB was configured as "x86_64-redhat-linux-gnu".
    For bug reporting instructions, please see:
    ...
    Reading symbols from /home/jplank/cs202/Notes/Recursion/a.out...done.
    (gdb) break 13
    Breakpoint 1 at 0x40058c: file src/rec2.cpp, line 13.
    (gdb) break 22
    Breakpoint 2 at 0x4005c7: file src/rec2.cpp, line 22.
    (gdb) run
    Starting program: /home/jplank/cs202/Notes/Recursion/a.out 
    
    Breakpoint 2, main () at src/rec2.cpp:22
    22	  a(3);
    (gdb) where
    #0  main () at src/rec2.cpp:22
    (gdb) cont
    Continuing.
    In procedure a: i = 3, j = 15
    
    Breakpoint 1, a (i=3) at src/rec2.cpp:13
    13	  if (i > 0) a(i-1);
    (gdb) where
    #0  a (i=3) at src/rec2.cpp:13
    #1  0x00000000004005d1 in main () at src/rec2.cpp:22
    (gdb) print i
    $1 = 3
    (gdb) print j
    $2 = 15
    (gdb) cont
    Continuing.
    In procedure a: i = 2, j = 10
    
    Breakpoint 1, a (i=2) at src/rec2.cpp:13
    13	  if (i > 0) a(i-1);
    (gdb) where
    #0  a (i=2) at src/rec2.cpp:13
    #1  0x000000000040059f in a (i=3) at src/rec2.cpp:13
    #2  0x00000000004005d1 in main () at src/rec2.cpp:22
    (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 src/rec2.cpp:13
    13	  if (i > 0) a(i-1);
    (gdb) where
    #0  a (i=1) at src/rec2.cpp:13
    #1  0x000000000040059f in a (i=2) at src/rec2.cpp:13
    #2  0x000000000040059f in a (i=3) at src/rec2.cpp:13
    #3  0x00000000004005d1 in main () at src/rec2.cpp:22
    (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 src/rec2.cpp:13
    13	  if (i > 0) a(i-1);
    (gdb) where
    #0  a (i=0) at src/rec2.cpp:13
    #1  0x000000000040059f in a (i=1) at src/rec2.cpp:13
    #2  0x000000000040059f in a (i=2) at src/rec2.cpp:13
    #3  0x000000000040059f in a (i=3) at src/rec2.cpp:13
    #4  0x00000000004005d1 in main () at src/rec2.cpp:22
    (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
    [Inferior 1 (process 16002) exited normally]
    (gdb) quit
    UNIX>