GDB print to file instead of stdout

I am running GDB and want to examine one of those unfortunate god objects. It takes many pages (and I have a 24" monitor turned sideways!) to see the whole thing.

For ease of use, I'd like GDB to print the object to a file instead of the screen so that I can open it in vi and move around with ease.

With all of GDB's versatility, there must be a way to do this, right?


Solution 1:

You need to enable logging:

(gdb) set logging on

Now GDB will log to ./gdb.txt. You can tell it which file to use:

(gdb) set logging file my_god_object.log

And you can examine the current logging configuration:

(gdb) show logging

Solution 2:

I've found that you can redirect the output from gdb to a file via the run command:

(gdb) run > outfile

Solution 3:

Extending on @qubodup's answer

gdb core.3599 -ex bt -ex quit |& tee backtrace.log

the -ex switch runs a gdb command. So the above loads the core file, runs bt command, then quit command. Output is written to backtrace.log and also on the screen.

Another useful gdb invocation (giving stacktrace with local variables from all threads) is

gdb core.3599 -ex 'thread apply all bt full' -ex quit

Solution 4:

From https://sourceware.org/gdb/onlinedocs/gdb/Logging-Output.html:

You may want to save the output of gdb commands to a file. There are several commands to control gdb's logging.

set logging on

Enable logging.

set logging off

Disable logging.

set logging file file

Change the name of the current logfile. The default logfile is gdb.txt.

set logging overwrite [on|off]

By default, gdb will append to the logfile. Set overwrite if you want set logging on to overwrite the logfile instead.

set logging redirect [on|off]

By default, gdb output will go to both the terminal and the logfile. Set redirect if you want output to go only to the log file.

show logging

Show the current values of the logging settings.