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: