ACM Intermediate UNIX Tutorial - Debugging

Basic debugging concepts

Ok, instead of going right into the heart of GDB, it might be a good idea to talk a bit about debugging in general. Not that everyone isn't already an expert at it, but hey, there might be someone who isn't.

Ok, that was a quick list of debugging ideas. Now on to using GDB

GDB

GDB is the standard GNU command line debugger. It's full-featured, but just using it raw is a little bit painful. You can't really see your code, and the output it interspersed with your program's output, so it's basically a big mess. It's fine for getting a backtrace and some variable spew after a core dump, but for interactive tracing it's nice to use a frontend to it.

Starting GDB

Starting gdb is easy. Just type: gdb program where program is, you know, your program. You can then start running your program in gdb by typing: run. If you need to give your program some arguments, give the arguments to run. As an example:

$ gdb helloworld
GNU gdb 4.17.0.4 with Linux/x86 hardware watchpoint and FPU support
Copyright 1998 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "i386-redhat-linux"...(no debugging symbols found)...
(gdb) run -q
Starting program: /u15/husted/uns-stuff/tutorials/helloworld -q
Hello, world!

Program exited normally.
(gdb) 
      

There, we ran gdb on the "helloworld" program, and gave helloworld the argument, "-q" (which it studiously ignored, it being hello world after all

To trace into a core file, start gdb like this: gdb program core. If your core file is named something else (for instance, if you renamed it) you'd change "core". Thus, an example:

$ ./helloworld 
Segmentation fault (core dumped)
$ gdb helloworld core
GNU gdb 4.17.0.4 with Linux/x86 hardware watchpoint and FPU support
Copyright 1998 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "i386-redhat-linux"...
Core was generated by `./helloworld'.
Program terminated with signal 11, Segmentation fault.
Reading symbols from /lib/libc.so.6...bdone.
Reading symbols from /lib/ld-linux.so.2...done.
#0  0x8048480 in main () at helloworld.c:4
4               int *a = 0; *a = 0;
(gdb) bt
#0  0x8048480 in main () at helloworld.c:4
(gdb) info locals
a = (int *) 0x0
(gdb) info args
No arguments.
(gdb) 
      

There, we ran gdb on the core file, and determined that for some reason, the 4th line of the program had some sort of null pointer bug in it.

Basic gdb commands:

Front ends to GDB

As I said before, using GDB is kind of a pain for anything beyond backtrace. This is why most everyone uses a front-end to it.

One of the most common front ends to gdb is, of course, emacs. If you enter GDB mode in emacs (M-x gdb) it prompts you for a program to run under the debugger (you can enter core here as well) and then basically puts GDB running in one window (with some little navigation buttons on top if you're using xemacs) and shows your source code in the other. As you walk through your program in gdb, the source code window follows along, highlighting exactly where you are. Very convenient - especially since you can fix the bug right there, without leaving emacs at all.

There are also a number of GUI front ends to GDB. One feature most of them have, which is very nice, is the ability to put your program's interaction in one window, and gdb's command console in another. This keeps them from getting all jumbled up, which can be essential with many programs. I'll list a few of the front ends here:

The best advice here is probably to just try out a few of them! Some people like code-medic, some ddd, lots of people like emacs gdb-mode, some people like kdevelop because it looks like MSVC, etc. Some even like gdb raw on the console.


Justin Husted
Last modified: Tue May 16 20:50:17 PDT 2000