Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gdb print to file instead of stdout

People also ask

How do I redirect output to a file in GDB?

Logging GDB's output to a file This is done by first issuing the command set logging file my-gdb-log , followed by the command set logging on . Later on, you can issue the set logging off command to stop sending GDB output to the log file.

How do you step out in GDB?

Bookmark this question. Show activity on this post. Those who use Visual Studio will be familiar with the Shift + F11 hotkey, which steps out of a function, meaning it continues execution of the current function until it returns to its caller, at which point it stops.


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

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

(gdb) run > outfile

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

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.


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

Although there are many good answers here, I still have to post the only thing that worked for me:

[niko@my-laptop]# gdb MyBinary 2>&1 log.txt

This was the only way to get gdb and binary output into the same log.txt file, while also seeing it on the console.

EDIT:

Caution: Output seems to be partially not synced among the gdb output and the binary output. Can someone confirm? You might want to check whether your telnet/ssh client has a function to log the output that you see in your console.