Quick Guide to Gdb

The dynamic debugger utility, gdb, has a large number of capabilities. This quick guide lists a small but useful subset of the gdb commands.

Index 

 

Preparation

  1. Compile with the -g option.

    Example. Compile the program printch.cpp:

    hawk% g++ -g printch.cpp -o printch
    
  2. Start gdb and set the number of source lines to list.

    Example. Run gdb on the printch program and set the number of source lines to list at a time to 28.

    hawk% gdb -xdb -tui printch
    GNU gdb 4.17.1
    Copyright 1998 Free Software Foundation, Inc.
    GDB is free software, covered by the GNU General Public License
    (gdb) set listsize 28
    
 

Setting Breakpoints

Breakpoints are points in your code at which gdb will stop and allow executing other gdb commands.

  1. Set a breakpoint at the beginning of a function.

    Example. Set a breakpoint at the beginning of main.

    (gdb) b main
    
  2. Set a breakpoint at a line of the current file during debugging.

    Example. Set a breakpoint at line 35 while in file printch.cpp.

    (gdb) b 35
    
  3. Set a breakpoint at the beginning of a class member function.

    Example. Set a breakpoint at the beginning of member function erase of the class list.

    (gdb) b list::erase
    
  4. Listing breakpoints.

    Example. List all breakpoints which have been set so far in a debugging session.

    (gdb) info b
    Num Type           Disp Enb Address    What
    1   breakpoint     keep y   0x0040104f in main at printch.cpp:27
    2   breakpoint     keep y   0x004010a7 in main at printch.cpp:35
    
  5. Deleting a breakpoint.

    Example. Delete the breakpoint at line 35.

    (gdb) delete 2
    
 

Running the Program being Debugged

  1. Start the program being debugged.

    Example 1. The program is printch, which can take an optional command line argument. Start it running with no command line argument.

    (gdb) r
    

    Example 2. Start printch running with command line argument A.

    (gdb) r A
    
  2. Execute a single statement. If the statement is a function call, just single step into the function.
    (gdb) s
    
  3. Execute a single statement. If the statement is a function call, execute the entire function and return to the statement just after the call; that is, step over the function.
    (gdb) n
    
  4. Execute from the current point up to the next breakpoint if there is one, otherwise execute until the program terminates.
    (gdb) c
    
  5. Execute the rest of the current function; that is, step out of the function.
    (gdb) finish
    
 

Examining Variables

  1. Print the value of a variable or expression.

    Example 1. Print the value of a variable count

    (gdb) p count
    

    Example 2. Print the value of the expression fname[i+1]

    (gdb) p fname[i+1]
    
 

List Source Code and the Next Statement

  1. List lines of source code.

    Example. List the next listsize number of lines of code. Note that listsize's value can be change with the set command.

    (gdb) l
    
  2. List lines of source code centered around a particular line.

    Example. List the lines centered around line 41.

    (gdb) l 41
    
  3. Show the next statement that will be executed.
    (gdb) where
    #0  mystrcpy (copyto=0x259fc6c "*", copyfrom=0x259fddc "ABC") at printch.cpp:27
    #1  0x4010c8 in main (argc=3, argv=0x25b0cb8) at printch.cpp:40
    

    The statement at line 27 of the function mystrcpy is the next statement and the function mystrcpy was called by main.

 

Help and Quitting Gdb

There is a help command, h and the command to quit gdb is q.

 

Summary of Commands

Gdb CommandDescription
set listsize n Set the number of lines listed by the list command to n [set listsize]
b function Set a breakpoint at the beginning of function [break]
b line number Set a breakpoint at line number of the current file. [break]
info b List all breakpoints [info]
delete n Delete breakpoint number n [delete]
r args Start the program being debugged, possibly with command line arguments args. [run]
s count Single step the next count statments (default is 1). Step into functions. [step]
n count Single step the next count statments (default is 1). Step over functions. [next]
finish Execute the rest of the current function. Step out of the current function. [finish]
c Continue execution up to the next breakpoint or until termination if no breakpoints are encountered. [continue]
p expression print the value of expression [print]
l optional_line List next listsize lines. If optional_line is given, list the lines centered around optional_line. [list]
where Display the current line and function and the stack of calls that got you there. [where]
h optional_topichelp or help on optional_topic [help]
qquit gdb [quit]