GDB print to file instead of stdout

105,248

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

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 3

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

(gdb) run > outfile

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.

Solution 5

A simple method to log gdb to a file while still seeing the output (which eases writing commands) is to use tee:

gdb command |& tee gdb.log
Share:
105,248
pythonic metaphor
Author by

pythonic metaphor

Updated on December 15, 2021

Comments

  • pythonic metaphor
    pythonic metaphor over 2 years

    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?

  • the paul
    the paul about 9 years
    That would be the output from the program being debugged, not the output from gdb itself. OP wanted to log gdb's own output.
  • Ciro Santilli OurBigBook.com
    Ciro Santilli OurBigBook.com over 8 years
  • user7610
    user7610 about 8 years
    On some systems, you need to type gdb -c core.3599 ...
  • rostamn739
    rostamn739 about 8 years
    @thepaul but this is in fact very useful as I'm debugging a QT program that just trashes gdb's stdin with it's QDebug garbage
  • rostamn739
    rostamn739 about 8 years
    @rostamn739 oops, didn't help at all
  • Ben C
    Ben C almost 8 years
    And if you want the output to go only to the log file, use set logging redirect on.
  • Herpes Free Engineer
    Herpes Free Engineer over 5 years
    Shouldn't we set set logging file my_god_object.log before set logging on?
  • Herpes Free Engineer
    Herpes Free Engineer over 5 years
  • remcycles
    remcycles about 5 years
    This plus tail -f and awk was really helpful today. Thanks!
  • David Skelly
    David Skelly over 4 years
    > gdb core.3599 -ex 'thread apply all bt full' -ex quit Add --batch to run without a prompt -eg sudo gdb --batch core.3599 -ex 'thread apply all bt full' -ex quit > output.log
  • Ambareesh
    Ambareesh over 2 years
    run &> outfile is sometimes needed to capture the complete output. Noticed while using GoogleTest that all lines weren't getting added (unless the stderr was also redirected with &>)
  • codeman48
    codeman48 over 2 years
    Note that one cannot use tui enable or layout commands in this mode. gdb says Cannot enable the TUI when output is not a terminal