Skip to main content
Redhat Developers  Logo
  • Products

    Platforms

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat AI
      Red Hat AI
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • View All Red Hat Products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat Developer Hub
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat OpenShift Local
    • Red Hat Developer Sandbox

      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Secure Development & Architectures

      • Security
      • Secure coding
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • Product Documentation
    • API Catalog
    • Legacy Documentation
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

Printf-style debugging using GDB, Part 2

October 13, 2021
Kevin Buettner
Related topics:
C, C#, C++LinuxOpen source
Related products:
Red Hat Enterprise Linux

Share:

    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

    Related Posts

    • Printf-style debugging using GDB, Part 1

    • The GDB developer's GNU Debugger tutorial, Part 1: Getting started with the debugger

    • Debugging Python C extensions with GDB

    • Debugging GraalVM-native images using gdb

    Recent Posts

    • DeepSeek-V3.2-Exp on vLLM, Day 0: Sparse Attention for long-context inference, ready for experimentation today with Red Hat AI

    • How to deploy the Offline Knowledge Portal on OpenShift

    • Autoscaling vLLM with OpenShift AI

    • Filtering packets from anywhere in the networking stack

    • PostGIS: A powerful geospatial extension for PostgreSQL

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Platforms

    • Red Hat AI
    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Build

    • Developer Sandbox
    • Developer Tools
    • Interactive Tutorials
    • API Catalog

    Quicklinks

    • Learning Resources
    • E-books
    • Cheat Sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site Status Dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2025 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Report a website issue