Log GDB Output To A File

Sometimes, especially when you’re dealing with a long stack trace, or a multi-threaded stack trace, trying to view the debug output from GDB in a terminal window can be a little awkward.

Did you know that you can log specific output (or even the entire session) from within GDB to a text file?

All you have to do is type:

set logging on

at the GDB prompt.

GDB saves all output from this point in a text file called gdb.txt that resides in the directory in which you are running GDB (usually the same directory as your executable).

When you’re done you can type:

set logging off

And GDB will stop recording output.

Note that you can turn logging on and off several times and GDB will concatenate output to the gdb.txt file, so you don’t have to worry about overwriting what you’ve already got there.

If you want to record multiple files in a session you can change the filename with:

set logging file myfile.txt

Here’s a little screenshot:

gdb-file-logging

Access GDB Directly In Eclipse Luna

This isn’t a new feature, but it’s really useful when you’re debugging in eclipse, especially if you’re using the more advance or obscure features in GDB.

Basically, when you are debugging any program, there is a way to talk directly to GDB without using the point-and-click interface of Eclipse.

It’s called the GDB console.

It’s also really easy to find. When you are next running a debug session, just click on the console icon that is displayed in the console tab. The console usually shows your program output, but there is more than just one console!

Read moreAccess GDB Directly In Eclipse Luna

Debug ncurses with GDB + GDBserver and Full Input Output

Actually, you can use this technique to debug any kind of console app where debugging on the command line interferes with the program’s output. It’s quick and easy, and unlike attaching to a running process in another shell, it allows you to debug as soon as the program starts, meaning you can catch even those fiddly bugs that only appear during set up.

If you’ve ever tried to debug an ncurses application, you’ll know how problematic it can be.

Using printf statements makes a mess of your visual output…

Debuggers get the keyboard input all mixed up in the shell…

Eugh. HEADACHE!

There is a way to seamlessly debug your ncurses application and it is probably easier than you realised.

Enter gdbserver.

Read moreDebug ncurses with GDB + GDBserver and Full Input Output

GDB | Auto-Load Safe-Path Declined

More GDB shenanigans today, as kindly pointed out by one of my readers – thank you Laurent!

Did you know that as of GDB version 7.5 (Aug 2012), there is a new security feature in place that prevents GDB from looking in “non-trusted” directories for the super-useful .gdbinit file?

[For more on the usefulness of .gdbinit files, see here.]

Read moreGDB | Auto-Load Safe-Path Declined