Getting started with gdb

Some hints on using the command-based gdb debugger in Linux.

  • Always compile with the -ggdb option. Otherwise you won't be able to use the debugger on your program.
  • To run gdb from the shell type gdb executable.  If you are an emacs user, you can run it from inside emacs, and it will show you what line number you are on in a different code buffer. To get into gdb  from emacs, type M-x gdb.  It will ask the name of the executable you wish to debug.  You should see a (gdb) prompt. All of the other points here are assuming you have already started gdb.
  • To run a program type run (and give any command line arguments for your program on the same line as the run command).
  • What to do if your program crashes.  Type where and it will show you the runtime stack (see next point for details) at the time of the crash.  Often the current function is some system function to deal with the error, so you may have to repeatedly do up to get to one of your functions.  There you can see the exact line you were on when the program crashed, and you can look at the values of variables.  From inside emacs, when you are stopped inside your program, gdb will show you what line you are on by showing the source code in another emacs window, and putting an arrow next to the line you are on.
  • What is the run-time stack.  If you do where in a stopped program gdb shows you where you are currently in the program in the form of what functions called what, such that the last function called is at the top of the stack, and deepest in the stack is was the first one called that hasn't yet returned (i.e., main) You can move around on this stack by typing "up" or "down" (changes where you are by one level).  This is useful for seeing exactly what was going on at the time of a crash.
  • To see the value of variables.  The general command is print varname, although, varname can actually be a C++ expression. Before you can do this the program first has to be stopped in the scope of the variable.  So for example, if you are in function foo() that was called by function main, to see vars local to main, you would first have to do the command up (see info about run-time stack above for details).
  • To abort your program  you can type ctrl-c ctrl-c.  From there you can see what line it was executing, look at the values of variables, etc.
  • To single step though a program.  The step command executes one source code line, but will also enter functions called from that line. To skip over function calls, do next  instead.
  • To set a breakpoint.  From the gdb prompt, type break linenumber. Alternatively, if you are running emacs you can position your cursor on the source code line, then type ctrl-x SPC (i.e., SPC = spacebar key).
  • To restart a program from a stopped point.  Type cont (as in continue) to keep running it from where you were.
  • How to get more info about gdb.  There's much more you can do with gdb than is shown here. 
    • One way to find out about other commands is to type help at the gdb prompt. 
    • Or you can type M-info to emacs and then move your cursor to the gdb option, and then hit return.  Once you have chosen gdb, you can choose Emacs to get more info on using gdb from inside emacs.
    • Or you can find an html version of the manual at the GNU site.

  • Last Updated by Claire Bono , Apr. 2021