Home Using GDB as a Poor Man’s Profiler

Using GDB as a Poor Man’s Profiler

Most people are aware that you can debug your own programs with gdb, but did you know it’s also great for understanding performance problems in programs that you don’t even have the source for or that you can attach to running processes at any time?

When I notice one of my machines is running slowly, the first thing I do is run the top unix command from the terminal to see where the time is going.

top

This shows me that Firefox is taking up about half of my CPU, but doesn’t tell me much about why. To get that information, make a note of the process number, press q to quit top, fire up gdb with the -p switch and that identifier, and it will hook into the running process:

gdb -p 69881

This pauses the program, so now run the command thread apply all bt to see what it was working on at the instant it was paused:

thread apply all bt

Even though we don’t have the source code, you can often get a decent idea of what the code is related to from the function names. There’s a lot of threads here, but the one we’re interested in happens to be the main thread, listed last. You can see that the stack trace has functions that look Flash-related, and the lowest level ones (highest up the screen) appear to be image-drawing code. This is a clue that it’s spending time in Flash image rendering, but to be more certain we need to gather more samples. To do this, enter the c command to allow the program to continue running, and after a second or two press control-c to pause it again and do another thread listing:

Here it’s no longer in Flash, but it’s still doing image rendering, so the odds are that Firefox is bogged down by something graphics-related. You can repeat this process multiple times to gather more and more evidence, and you’ve got a poor man’s version of a profiler.

The place I probably use this approach most often is when a program appears to be completely hung. By following exactly the same steps you can often spot if it’s truly in an infinite loop, or if it’s just making really slow progress or swapping memory like crazy. If it is in an infinite loop, the clues from the backtrace function names are often enough to figure out what provoked it, and how to avoid running into the same problem in the future.

About ReadWrite’s Editorial Process

The ReadWrite Editorial policy involves closely monitoring the tech industry for major developments, new product launches, AI breakthroughs, video game releases and other newsworthy events. Editors assign relevant stories to staff writers or freelance contributors with expertise in each particular topic area. Before publication, articles go through a rigorous round of editing for accuracy, clarity, and to ensure adherence to ReadWrite's style guidelines.

Get the biggest tech headlines of the day delivered to your inbox

    By signing up, you agree to our Terms and Privacy Policy. Unsubscribe anytime.

    Tech News

    Explore the latest in tech with our Tech News. We cut through the noise for concise, relevant updates, keeping you informed about the rapidly evolving tech landscape with curated content that separates signal from noise.

    In-Depth Tech Stories

    Explore tech impact in In-Depth Stories. Narrative data journalism offers comprehensive analyses, revealing stories behind data. Understand industry trends for a deeper perspective on tech's intricate relationships with society.

    Expert Reviews

    Empower decisions with Expert Reviews, merging industry expertise and insightful analysis. Delve into tech intricacies, get the best deals, and stay ahead with our trustworthy guide to navigating the ever-changing tech market.