Featured image for C-language topics.

The first article in this series introduced the GNU debugger, GDB, and in particular its dprintf command, which displays variables from programs in a fashion similar to C-language printf statements. This article expands on the rich capabilities of printf-style debugging by showing how to save commands for reuse and how to save the output from the program and GDB for later examination.

Listing currently defined breakpoints

The dprintf command creates a special type of breakpoint. The info breakpoints command displays all breakpoints; however, at the moment, we have only dprintf breakpoints defined:

(gdb) info breakpoints
Num     Type           Disp Enb Address            What
1       dprintf        keep y   0x0000000000401281 in insert at tree.c:41
	breakpoint already hit 7 times
        printf "Allocating node for data=%s\n", data
2       dprintf        keep y   0x00000000004012b9 in insert at tree.c:47
	breakpoint already hit 6 times
        printf "Recursing left for %s at node %s\n", data, tree->data
3       dprintf        keep y   0x00000000004012de in insert at tree.c:49
	breakpoint already hit 6 times
        printf "Recursing right for %s at node %s\n", data, tree->data
(gdb) 

Saving dprintf commands for a later session

In traditional printf-style debugging, print statements added to the program persist until they are removed. This is not the case when using the dprintf command with GDB; both dprintf breakpoints and ordinary breakpoints will persist throughout a GDB session, but they won't persist between sessions. However, breakpoints may be saved to a file for later reuse.

The save breakpoints command saves breakpoints to a file. The following example shows how to save breakpoints to a file named my-dprintf-breakpoints:

(gdb) save breakpoints my-dprintf-breakpoints
Saved to file 'my-dprintf-breakpoints'.

The resulting file consists of GDB breakpoint commands saved from the session. Thus, the file my-dprintf-breakpoints contains three lines:

dprintf /home/kev/ctests/tree.c:41,"Allocating node for data=%s\n", data
dprintf /home/kev/ctests/tree.c:47,"Recursing left for %s at node %s\n", data, tree->data
dprintf /home/kev/ctests/tree.c:49,"Recursing right for %s at node %s\n", data, tree->data

If changes are made to the program in between GDB sessions, the line numbers specified by these commands may no longer be correct. If that happens, the most straightforward fix is to use a text editor to adjust them.

The my-dprintf-breakpoints file can be loaded into some future GDB session—by the programmer who saved them, or by another programmer debugging the same program—via the source command:

(gdb) quit
$ gdb -q ./tree
Reading symbols from ./tree...
(gdb) source my-dprintf-breakpoints
Dprintf 1 at 0x401281: file tree.c, line 41.
Dprintf 2 at 0x4012b9: file tree.c, line 47.
Dprintf 3 at 0x4012de: file tree.c, line 49.

Redirecting output

Printf-style debugging can generate a lot of output. It is often useful to send debugging output to a file for later analysis.

By default, output from a dynamic printf is sent to GDB's console. Also, by default, the output from a program run under GDB is sent to the console, but via a different file descriptor. Therefore, output from GDB and the program are usually intermixed. But since different file descriptors are used, it's possible to redirect either GDB's output or program output to a file, or even both outputs to separate files.

Logging GDB's output to a file

GDB provides a number of commands for saving output from GDB to a file. I'll discuss a few of them here; see the GDB manual for more information.

Let's suppose that you wish to save a log of GDB output to a log file named my-gdb-log. This is done by first issuing the command set logging file my-gdb-log, followed by the command set logging on. Later on, you can issue the set logging off command to stop sending GDB output to the log file. Using the dprintf commands established earlier, this is what the sequence of commands looks like:

(gdb) set logging file my-gdb-log
(gdb) set logging on
Copying output to my-gdb-log.
Copying debug output to my-gdb-log.
(gdb) run
Starting program: /home/kev/ctests/tree 
Allocating node for data=dog
...
      scorpion
  wolf
[Inferior 1 (process 321429) exited normally]
(gdb) set logging off
Done logging to my-gdb-log.

As shown in the example, both program output and GDB's output are still sent to the console. (The set logging debugredirect on command can be used to send GDB's output only to the log file.) However, only GDB's output is placed in my-gdb-log, as you can see by viewing that file:

Starting program: /home/kev/ctests/tree 
Allocating node for data=dog
Recursing left for cat at node dog
...
Recursing right for scorpion at node javelina
Allocating node for data=scorpion
[Inferior 1 (process 321429) exited normally]

Note, too, that no prompts or user-typed commands appear in the log output.

Redirecting program output to a file

The mechanism for redirecting program output to a file is simple; the > redirection operator is used with the run command in much the same way that output is redirected by most shells. The example below shows how to run the program while redirecting program output to the file my-program-output:

(gdb) run >my-program-output
Starting program: /home/kev/ctests/tree >my-program-output
Allocating node for data=dog
...
Allocating node for data=scorpion
[Inferior 1 (process 321813) exited normally]
(gdb) 

The my-program-output file now looks like this:

cat coyote dog gecko javelina scorpion wolf 

  cat
    coyote
dog
      gecko
    javelina
      scorpion
  wolf

Sending dprintf output to the same file as program output

When saving program output to a file, you might want to place dprintf-related output in the same file, intermixed with the rest of the program output. This can be done by making GDB invoke the program's printf() function from the standard C library linked with the program. GDB's dprintf-style setting is used to control where dprintf related output is sent. The default dprintf-style setting is gdb; it causes GDB's internal printf command to be used, sending output to the GDB console. When the dprintf-style setting is call, GDB will perform what is known as an inferior function call; i.e., it will call a function in the program being debugged, in this case printf(). Therefore, the set dprintf-style call command causes the output that is printed when hitting a dprintf breakpoint to be performed by calling printf() from within the program:

(gdb) set dprintf-style call
(gdb) run >my-program-output
Starting program: /home/kev/ctests/tree >my-program-output
[Inferior 1 (process 322195) exited normally]
(gdb) 

The my-program-output file now contains both dprintf output and program output together:

Allocating node for data=dog
Recursing left for cat at node dog
...
      scorpion
  wolf

GDB provides other commands that send dprintf output to a different file descriptor, much like using fprintf() instead of printf(). These same facilities can also be used to invoke printf-style logging functions defined in the program. Refer to the GDB manual for an explanation of these commands.

Conclusion

Look for the third and final article in this series, which shows powerful ways to interact with functions in your program from GDB, and how to automate the execution of GDB commands.

Last updated: June 9, 2023