Does GDB have a built in scripting mechanism, should I code up an expect script, or is there an even better solution out there?
I'll be sending the same sequence of commands every time and I'll be saving the output of each command to a file (most likely using GDB's built-in logging mechanism, unless someone has a better idea).
If a -x with a file is too much for you, just use multiple -ex's. This is an example to track a running program showing (and saving) the backtrace on crashes
gdb
executes file.gdbinit
after running. So you can add your commands to this file and see if it is OK for you. This is an example of.gdbinit
in order to print backtrace for allf()
calls:I was just going through something similar, and came up with a basic example - and knowing I'll forget about it soon, I thought I'd better post it
:)
So I'll post it here, as it looks related to the question.Basically, in this example I wanted to get some variable values in particular places of the code; and have them output until the program crashes. So here is first a little program which is guaranteed to crash in a few steps,
test.c
:The only reason the program accepts command-line arguments is to be able to choose the number of steps before crashing - and to show that
gdb
ignores--args
in batch mode. This I compile with:Then, I prepare the following script - the main trick here is to assign a
command
to eachbreakpoint
, which will eventuallycontinue
(see also Automate gdb: show backtrace at every call to function puts). This script I calltest.gdb
:Note that, if you intend to use it in batch mode, you have to "start up" the script at the end, with
run
orstart
or something similar.With this script in place, I can call
gdb
in batch mode - which will generate the following output in the terminal:Note that while we specify command line argument 5, the loop still spins only two times (as is the specification of
run
in thegdb
script); ifrun
didn't have any arguments, it spins only once (the default value of the program) confirming that--args ./test.exe 5
is ignored.However, since now this is output in a single call, and without any user interaction, the command line output can easily be captured in a text file using
bash
redirection, say:There is also an example of using python for automating gdb in c - GDB auto stepping - automatic printout of lines, while free running?
Hope this helps,
Cheers!