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

    • 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.

    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

Improve your code: Tales from confinement without a debugger

July 7, 2020
Aldy Hernandez
Related topics:
Linux

    I have always been impressed by developers who make do without a debugger, and have often wished I could be more like them. I vaguely recall a colleague saying he never used a debugger, favoring printf over gdb. Also, in my rookie years, I vividly recall chasing a kernel bug with a friend who was using objdump and the source of a much older kernel:

    "Richard, shouldn't you at least use the correct source?"
    "Meh ... they're close enough."

    I'm still impressed.

    Now that I have coded for some years, I've noticed that I have picked up some bad habits along the way. Over-dependence on the debugger is one of them. I often use it as a high-powered crutch, which frequently leads me waist-deep into stack traces, rarely stopping to think things through. I get lost inside 20 levels of recursion and wonder why an irrelevant variable is being tickled.

    Granted, there are many good uses for a debugger, but I'm at 40% on the good use scale. My uses usually start benign but then degrade into cancerous abstractions. So, for my 20th GNU Compiler Collection (GCC) hacking anniversary, I decided to give myself the challenge of one month without a debugger. Here is the tale.

    How I got here

    When I first started hacking GCC, newbies at Red Hat were put on old toolchain support duty. Nine times out of 10, those bugs had already been fixed upstream. I got quite adept at running two parallel gdbs, single-stepping until I found a difference in the codes, and eventually finding the patch that fixed the bug. My technique was effective, but taught me very little about the underlying problem that I was "fixing."

    Now, I'm a gray-bearded old fogey, and I can't count the number of times I have put a breakpoint on the garbage collector to find out who created a chunk of memory, just to save time analyzing the where of a given optimization. I even have an embarrassing gdb macro for doing it:

    define who_created_me
    break ggc-page.c:2683 if result == $arg0
    

    (Yes, I have different macros for different GCC versions. That source line inevitably changes.)

    I also have an assortment of strcmp variants in my local tree that I use for comparing GNU Compiler Collection internals (GIMPLE), assembly output, and other GCC internal states. I am frequently seen setting breakpoints for particular patterns because I'm trying awfully hard to avoid deep work. Here's an example:

    (gdb) break some_function if !strcmp_gimple(stmt, "var_3 = foo ();")
    

    Unfortunately, these cool party tricks seduce me into believing they're time savers. In reality, they quickly degrade into spelunking stack trace expeditions to nowhere. Frequently, the underlying reason for my expeditions is that I don't understand the code.

    Life without a debugger

    Initially, there was a lot of "Crap, what if I can't solve this?" What if my entire programming-fu is dependent on the debugger? What if this is the one bug that needs a debugger, and I have nothing but frustration to report in my weekly status report? After the initial panic, however, I got into a rhythm. Once that happened, I saw noticeable improvements in my thought process and my coding practice:

    • I noticed that a lot of my code was optimized for the debugging experience, not for readability or maintainability. I shied away from putting more than one statement in a line and broke things up to make it easier to set breakpoints. Doing this can sometimes, but not always, yield cleaner code.
    • I think it was Kernighan who said, "Debugging is twice as hard as writing a program in the first place." I was confronted by this cold, hard truth from the onset and realized what a timesaver gdb is for stepping through spaghetti code. I was also quick to notice, however, that I was spending so much time on specific problems because the code was horrible (which was not entirely my fault in a shared project). As a consequence, in my effort to debug less and fix more, my functions became smaller, less interdependent, and easier to understand. I also got quite good at refactoring other people's code in the compiler, to make things easier to understand. Overall, a win-win!
    • I judiciously added more asserts throughout the code to catch things earlier.
    • My dumps became more readable. It's surprising how many dumps in GCC are confusing at best, or useless at worst. Yes, I've contributed my share of unreadable dumps.
    • The keystrokes I saved navigating stack frames and setting breakpoints, went instead to writing carefully crafted printfs, which I made sure would persist into a dump file. Doing this saved me considerable time on subsequent "debugging" sessions.
    • Of the two times that I broke my promise and pulled out the debugger, both of them were because I was too tired to think the problem through. I hoped that randomly entering up, down, and print would magically solve my problem. Both times, what I needed was a break (no pun intended) and a fresh start the following day.

    Lessons learned

    In the interest of self-disclosure, I should say that I am not about to give up using debuggers. They are great tools in my arsenal. I might be more hesitant to pull out a debugger instinctively, at the first sign of trouble, but there are excellent reasons to use one. During my period of gdb-less confinement, I especially missed these uses for a debugger:

    • Debuggers are a boon for post-mortem analysis of why a process died. Going up and down the stack trace and examining variables is invaluable when inspecting a crash.
    • There is no real substitute for gdb-ing a hung process to see where it's stuck. I had to debug a few of those in the last month, and I don't wish that pain on anyone.
    • I draw the line at adding a bunch of printfs that are just emulating a gdb session. If you're reinventing a debugger through printfs in your code, for the love of Cthulhu, use the existing tools.
    • When you don't have the luxury of refactoring other people's spaghetti, a debugger can be a fast way of coming up to speed—just don't overuse it. At some point, you should sit down and understand the big picture.

    Conclusion

    I learned a lot during my month without a debugger. My code got smaller and easier to understand at a glance, and I got much better at sitting down and analyzing from a big-picture perspective. The fear of being stuck without a debugger led me to introduce fewer bugs in the first place. I thought more about what I wrote and depended less on gdb single-stepping sessions to analyze an algorithm. Overall, it was a good experience.

    If you spend more time noticing the forest rather than the tiny ant on the tree, you're more likely to fix the problem for good. Debuggers, by design, help you look at the ant on the tree. Don't get me wrong; that microfocus is useful. But it shouldn't be your primary way of analyzing a problem. The last month was scary, but it was time well spent.

    Last updated: July 1, 2020

    Recent Posts

    • Red Hat UBI 8 builders have been promoted to the Paketo Buildpacks organization

    • Using eBPF in Red Hat products

    • How we made one data layer serve the UI, the mocks, and the E2E tests

    • Build trusted Python containers with Project Hummingbird and Calunga

    • Simplify distributed tracing: ObservabilityInstaller installation

    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