Getting to Know gdb

It's worth making friends with a good C debugger.

gdb normally traps most signals sent to it. By trapping signals, gdb gets to decide what to do with the process you are running. For example, pressing CTRL-C sends the interrupt signal to gdb, which would normally terminate it. But you probably don't want to interrupt gdb; you really want to interrupt the program that gdb is running. Therefore, gdb catches the signal and stops the program it is running; this lets you do some debugging.

The command handle controls signal handling. It takes two arguments: a signal name, and what should be done when the signal arrives. For example, let's say that you want to intercept the signal SIGPIPE, preventing the program you're debugging from seeing it. Whenever it arrives, though, you want the program to stop, and you want some notification. To accomplish this, give the command:

(gdb) handle SIGPIPE stop print

Note that signal names are always capital letters! You may use signal numbers instead of signal names.

C++ Programs

If you write in C++ and compile with g++, you'll find gdb to be a wonderful environment. It completely understands the syntax of the language and how classes extend the concept of C structures. Let's look at a trivial program to see how gdb treats classes and constructors. Listing 1 contains a listing produced in gdb.

In order to see the program in action, we'll set a breakpoint at the entry statement on line 24. This declaration invokes a function, of course—the entry constructor.

(gdb) b 24
Breakpoint 1 at 0x23e4: file ref.C, line 24.
(gdb) run
Starting program: /home/los/mikel/crossref/ref
Breakpoint 1, main (argc=1, argv=0xeffffd8c) at
24           entry entry_1(text_1, strlen(text_1),

Now we'll enter the function. We do this through the step command, just as when entering a function in C.

(gdb) step
entry::entry (this=0xeffffcb8, text=0x2390
"Finding errors in C++ programs",
    length=30, ref=0x23b0 "errc++") at ref.C:14
14                e_text = new char(length+1);

gdb has moved to the first line of the entry constructor, showing us the arguments with which the function was invoked. When we return to the main program, we can print the variable entry_1 just like any other data structure.

(gdb) print entry_1
$1 = {e_text = 0x6128 "Finding errors in C++
  e_reference = "errc++",
  '\e000' <repeats 73 times>}

So C++ debugging is just as straightforward as C debugging.

Command Editing

Another useful feature is the ability to edit your commands in order to correct errors in typing. gdb provides a subset of the editing commands available in Emacs, letting you move back and forth along the line you're typing. For example, consider the command below:

(gdb) stop in gen_and_sort

If this doesn't look familiar to you, it shouldn't; it's a dbx command. We really meant to type break gen_and_sort. To fix this, we can type ESC b three times, to move back over the three words in gen_and_sort (spaces, underscores, and other punctuation define what's meant by a “word”). Then we type ESC DEL twice, to delete the erroneous command stop in. Finally, we type the correct command, break, followed by RETURN to execute it:

(gdb) break gen_and_sort
Breakpoint 1 at 0x2544: file qsort2.c, line 79.

Emacs has a special mode that makes it particularly easy to use gdb. To start it, give the command ESC x gdb. Emacs prompts you for a filename in the minibuffer:

Run gdb (like this): gdb

Add the executable's name and press RETURN; Emacs then starts a special window for running gdb, where you can give all regular gdb commands. When you stop at a breakpoint, gdb automatically creates a window displaying your source code, and marking the point where you have stopped, like this:

        struct tms end_time, *e;
        int begin, end;
=>      s = &start_time;
        e = &end_time;

The mark => shows the next line to be executed. The position is updated whenever gdb stops execution—that is, after every single-step, after every continue, etc. You may never need to use the built-in list command again!

This article was adapted from the book “Programming with GNU Software”, published by O'Reilly & Associates.


White Paper
Linux Management with Red Hat Satellite: Measuring Business Impact and ROI

Linux has become a key foundation for supporting today's rapidly growing IT environments. Linux is being used to deploy business applications and databases, trading on its reputation as a low-cost operating environment. For many IT organizations, Linux is a mainstay for deploying Web servers and has evolved from handling basic file, print, and utility workloads to running mission-critical applications and databases, physically, virtually, and in the cloud. As Linux grows in importance in terms of value to the business, managing Linux environments to high standards of service quality — availability, security, and performance — becomes an essential requirement for business success.

Learn More

Sponsored by Red Hat

White Paper
Private PaaS for the Agile Enterprise

If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.

Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.

Learn More

Sponsored by ActiveState