Lone Learner
Lone Learner

Reputation: 20698

How to include GDB commands in logging file?

I have some gdb commands in a file that I want to execute. I also want the commands run and the output to be logged to a file.

I wrote the following gdb script in a file named gdb.in.

set logging file gdb.out
set logging on
echo hi\n
printf "bye\n"
quit

However, if I execute this gdb script, I can only see the output logged to gdb.out. I don't see the commands executed.

$ gdb -x gdb.in
GNU gdb (GDB) Red Hat Enterprise Linux (7.2-60.el6)
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
hi
bye
$ cat gdb.out
hi
bye

As far as I can remember, a few years ago, I found a way to log the commands. Each command used to appear in the log file with a +-sign as a prefix. For example, I was able to get an output like this a few years ago.

+ echo hi\n
hi
+ printf "bye\n"
bye
+ quit

But I am unable to recall what I did to get such an output. Searching the web also didn't help me. I came across this question at How to dump the entire GDB session to a file, including commands I type and their output? but the answers there don't capture the commands executed in the log file.

Upvotes: 14

Views: 3266

Answers (3)

Gabriel Staples
Gabriel Staples

Reputation: 53205

I don't know when it was introduced, but @prasannatsm presents the solution here): set trace-commands on.

I'd just like to add upon that and show the full process. Here's how I like to start logging now:

set logging file ~/temp/gdb.txt
set logging on
set trace-commands on
show logging
flush

show logging at the end is just to verify everything is logging and working as-expected.

Notice that set trace-commands on causes each command you type to be echoed back out to you now (and also logged if logging is on) with a + preceding it to show it was an echoed command input, not the response.

This can be seen in the output here, for instance:

+show logging
Currently logging to "/home/gabriel.staples/temp/gdb.txt".
Logs will be appended to the log file.
Output will be logged and displayed.
+flush
Register cache flushed.

I've written down my gdb logging notes above in my git & Linux cmds, help, tips & tricks - Gabriel.txt file in my eRCaGuy_dotfiles repo. Search that document for "GDB LOGGING".

Upvotes: 5

prasannatsm
prasannatsm

Reputation: 960

set trace-commands on: echos the command in screen before executing it. Use this along with logging to get the command written to file.

Upvotes: 14

Employed Russian
Employed Russian

Reputation: 213955

This is the subject of GDB feature request, which is at least 12 years old :-(

There is currently no way to achieve this. You may use script to record the entire GDB session, but that has a distinct disadvantage of also recording various screen control "garbage" characters.

Upvotes: 4

Related Questions