CISC2000: Computer Science II Intro to Debug

1. Getting example codes

Run the following command to copy two programs to your directory:

cp ~zhang/public_html/cs2000/Labs/fib.cpp .
cp ~zhang/public_html/cs2000/Labs/samplecode.cpp .

2. Use gdb to help to view the execution of a program

Sometimes it's very difficult to debug memory related problems. For cases like this, you can use Gnu debugger gdb to trace the execution of your program. Essentially, running your program in the debugger allows you to view the values and addresses of the variables, contents of stack, and so on. So it's also a good tool for teaching purposes.

Please watch the insturctor demo the usage of debugger gdb, and then carry out the following exercises:

  1. Compile code with -g option,

    In order to allow gdb to understand your program, the program needs to be compiled with debugging information in the exectuable (which gives a larger executable file).

    g++ -g fib.cpp -o fib.out
    g++ -g samplecode.cpp -o prog
    
  2. Trace the execution of the given sample program

    Run the following command to load the program using gdb

    gdb fib.out
    GNU gdb (GDB) Fedora 7.9.1-19.fc22
    Copyright (C) 2015 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".
    Type "show configuration" for configuration details.
    For bug reporting instructions, please see:
    .
    Find the GDB manual and other documentation resources online at:
    .
    For help, type "help".
    Type "apropos word" to search for commands related to "word"...
    Reading symbols from fib...done.
    (gdb) break fib.cpp:32
    Breakpoint 1 at 0x401250: file fib.cpp, line 32.
    (gdb) run
    Starting program: /home/staff/zhang/TEST/fib.out 
    Missing separate debuginfos, use: dnf debuginfo-install glibc-2.32-4.fc33.x86_64
    Fibonacci sequence calculator:
    Enter a non-negative number:3
    
    Breakpoint 1, main () at fib.cpp:32
    32		cout <=0);
    (gdb) step
    14	    if (i==0)
    (gdb) step
    16	    else if (i==1)
    (gdb) cont
    Continuing.
    
    Breakpoint 2, fib (i=2) at fib.cpp:12
    12	    assert (i>=0);
    (gdb) cont
    Continuing.
    
    Breakpoint 2, fib (i=1) at fib.cpp:12
    12	    assert (i>=0);
    (gdb) where
    #0  fib (i=1) at fib.cpp:12
    #1  0x0000000000400908 in fib (i=2) at fib.cpp:19
    #2  0x0000000000400908 in fib (i=3) at fib.cpp:19
    #3  0x000000000040098a in main () at fib.cpp:32
    
  3. A list of gdb commands