CISC2200: Data Structure, Intro. to gdb

Please use the following command to copy two sample codes:

cp  ~zhang/public_html/cs2200/LabClass/gdbLabCodes/*.cpp . 
1. 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.

  1. Compile a program with degbug information by using -g option:
    g++ -g fibonacci.cpp -o fib
    
  2. Trace the execution of the given sample program

    Run the following command to load the program using gdb

    [zhang@storm labclass2]$ gdb fib 
    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) 
    (gdb) break main
    Breakpoint 1 at 0x4008a5: file fibonacci.cpp, line 28.
    (gdb) run
    Starting program: /u/erdos/xzhang/OSFall15/labclass2/fib 
    Missing separate debuginfos, use: dnf debuginfo-install glibc-2.21-8.fc22.x86_64
    
    Breakpoint 1, main () at fibonacci.cpp:28
    28         int localVarMain=10;
    Missing separate debuginfos, use: dnf debuginfo-install libgcc-5.1.1-4.fc22.x86_64 libstdc++-5.1.1-4.fc22.x86_64
    (gdb) where
    #0  main () at fibonacci.cpp:28
    (gdb) print &main
    $1 = (int (*)(void)) 0x40089d 
    (gdb) print &localVarMain
    $2 = (int *) 0x7fffffffe2bc
    (gdb) break Fibo
    Breakpoint 2 at 0x400852: file fibonacci.cpp, line 12.
    (gdb) cont
    Continuing.
    
    Breakpoint 2, Fibo (n=10) at fibonacci.cpp:12
    12         int localVar=n;
    (gdb) where
    #0  Fibo (n=10) at fibonacci.cpp:12
    #1  0x00000000004008b6 in main () at fibonacci.cpp:30
    (gdb) 
    
    
  3. A list of gdb commands
2. Use gdb to see where memory related problem occurs 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. Below is a step-by-step guide for using gdb for this purpose
  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 samplecode.cpp -o prog
    
  2. Start gdb command, passing your program as argument:
    gdb ./prog 
    
  3. After gdb finish starting, type run command to run your program in gdb.
  4. When your program crashes due to memory related issue, you can then use gdb commands such as where,up,down to see where in the code the program crashes.
  5. You can also use command print to view the value of variables.