How to dump the entire GDB session to a file, including commands I type and their output?

Viewed 25679

In bash, I can use the script command, which dumps everything that shows on the shell to a file, including:

  • commands typed
  • PS1 line
  • stdout and stderr of commands

What is the equivalent in gdb?

I tried to run shell script from inside GDB, but after I hit return, I was in the shell and lost the shell prompt and could not run command any more. Moreover I could not use ctrl+c or ctrl+\ to exit. I needed to force kill the /bin/login tty2 to exit.

4 Answers
  • Create a text file, i.e. gdbCommands.txt, with the following commands

set logging on my_log_file\nbt 10\nq

bt 10, indicates the number of lines (function calls) we need from the backtrace, in our example is 10 lines.

  • Execute gdb using the following command, assuming a core dump file core.2345

gdb -x gdbCommands.txt myApp core.2345

  • Open my_log_file and inspect backtrace!

howto-redirect-gdb-backtrace-output-to-a-text-file

I have logging enabled using:

set trace-commands on
set pagination off
set logging file $log

and show logging reports (to both to terminal and file):

  +show logging
     Currently logging to mylog.
     Logs will be appended to the log file.
     Output will be logged and displayed

If I print the value of a variable that also gets logged (to both to terminal and file):

+p myvar
$2 = 0

But if I do command like where or “info b” all I get logged to the file is:

+where
+info b

Anyone know why or how to fix it?

Related