Jichao
Jichao

Reputation: 41845

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

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

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.

Upvotes: 21

Views: 28968

Answers (4)

hackhowtofaq
hackhowtofaq

Reputation: 476

  • 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!

Upvotes: 3

Mark Wood-Patrick
Mark Wood-Patrick

Reputation: 71

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?

Upvotes: 2

Hasturkun
Hasturkun

Reputation: 36422

If you want to log GDB's output, you can use the GDB logging output commands, eg.

set logging file mylog.txt
set logging on

If you want to redirect your program's output to a file, you can use a redirect, eg.

run myprog > mylog.txt

see the chapter on program IO in the GDB manual for more information

Upvotes: 35

Aaron Digulla
Aaron Digulla

Reputation: 328830

Have a look at the GDB documentation. Search for "Canned Sequences of Commands". There is a way to save GDB commands in a file and run them with the source command and you can use some GDB commands in these scripts to print information available to GDB (like echo, output and printf).

If you want that output in a file, use set logging file FILE.

Upvotes: 0

Related Questions