Skip to main content
Redhat Developers  Logo
  • AI

    Get started with AI

    • Red Hat AI
      Accelerate the development and deployment of enterprise AI solutions.
    • AI learning hub
      Explore learning materials and tools, organized by task.
    • AI interactive demos
      Click through scenarios with Red Hat AI, including training LLMs and more.
    • AI/ML learning paths
      Expand your OpenShift AI knowledge using these learning resources.
    • AI quickstarts
      Focused AI use cases designed for fast deployment on Red Hat AI platforms.
    • No-cost AI training
      Foundational Red Hat AI training.

    Featured resources

    • OpenShift AI learning
    • Open source AI for developers
    • AI product application development
    • Open source-powered AI/ML for hybrid cloud
    • AI and Node.js cheat sheet

    Red Hat AI Factory with NVIDIA

    • Red Hat AI Factory with NVIDIA is a co-engineered, enterprise-grade AI solution for building, deploying, and managing AI at scale across hybrid cloud environments.
    • Explore the solution
  • Learn

    Self-guided

    • Documentation
      Find answers, get step-by-step guidance, and learn how to use Red Hat products.
    • Learning paths
      Explore curated walkthroughs for common development tasks.
    • Guided learning
      Receive custom learning paths powered by our AI assistant.
    • See all learning

    Hands-on

    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.
    • Interactive labs
      Learn by doing in these hands-on, browser-based experiences.
    • Interactive demos
      Click through product features in these guided tours.

    Browse by topic

    • AI/ML
    • Automation
    • Java
    • Kubernetes
    • Linux
    • See all topics

    Training & certifications

    • Courses and exams
    • Certifications
    • Skills assessments
    • Red Hat Academy
    • Learning subscription
    • Explore training
  • Build

    Get started

    • Red Hat build of Podman Desktop
      A downloadable, local development hub to experiment with our products and builds.
    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.

    Download products

    • Access product downloads to start building and testing right away.
    • Red Hat Enterprise Linux
    • Red Hat AI
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat Developer Toolset

    References

    • E-books
    • Documentation
    • Cheat sheets
    • Architecture center
  • Community

    Get involved

    • Events
    • Live AI events
    • Red Hat Summit
    • Red Hat Accelerators
    • Community discussions

    Follow along

    • Articles & blogs
    • Developer newsletter
    • Videos
    • Github

    Get help

    • Customer service
    • Customer support
    • Regional contacts
    • Find a partner

    Join the Red Hat Developer program

    • Download Red Hat products and project builds, access support documentation, learning content, and more.
    • Explore the benefits

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

    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

    • Every layer counts: Defense in depth for AI agents with Red Hat AI

    • Fun in the RUN instruction: Why container builds with distroless images can surprise you

    • Trusted software factory: Building trust in the agentic AI era

    • Build a zero trust AI pipeline with OpenShift and RHEL CVMs

    • Red Hat Hardened Images: Top 5 benefits for software developers

    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
    © 2026 Red Hat

    Red Hat legal and privacy links

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

    Chat Support

    Please log in with your Red Hat account to access chat support.