Debugging in UNIX

There are several debuggers on UNIX. The two discussed here are gdb and ddd.

Some hints on using the gdb debugger

  • Always compile with the -g option. Otherwise you won't be able to use the debugger on your program.
  • 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. To run gdb from the shell type gdb executable.  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 will have to 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 list, and the highest level one (main) is at the bottom.  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 the 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 emacs you can position your cursor on the source code line, then type ctrl-x SPC (i.e., SPC = spacebar key). Or you can do break linenumber from the gdb prompt.
  • 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 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.
  • DDD: A GUI front end for gdb



    Last Updated by Claire Bono , Sep 3, 1998
    The University of Southern California does not screen or control the content on this website and thus does not guarantee the accuracy, integrity, or quality of such content. All content on this website is provided by and is the sole responsibility of the person from which such content originated, and such content does not necessarily reflect the opinions of the University administration or the Board of Trustees