21

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.

3
  • 1
    Can you post an example what you tried? A screenshot with a short explanation? Also, what does "In the shell,I could use script to extract text" mean? Do you run a script on a file to extract text or do you use shell redirection (>) to extract text? Commented Nov 10, 2009 at 11:19
  • script is the command to log the shell output to file typescript.
    – Jichao
    Commented Nov 10, 2009 at 11:41
  • 1
    @AaronDigulla I didn't know about script, it is a (horribly generically named) executable from linux-util. I've explained what it does a bit. Commented Jul 31, 2015 at 15:03

4 Answers 4

35

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

1
3
  • 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!
2
  • 1
    Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference. Otherwise, folks may down vote or flag this answer.
    – joran
    Commented Mar 19, 2012 at 22:23
  • 3
    Thanks for posting your answer! Please be sure to read the FAQ on Self-Promotion carefully. Also note that it is required that you post a disclaimer every time you link to your own site/product. Commented Nov 29, 2012 at 10:06
2

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?

1
  • Did you ever figure this out? I too have backtraces printing on stdout but not getting captured in gdb's logfile
    – Ben Voigt
    Commented Oct 26, 2022 at 15:36
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.