gdb in one command

You can pass commands to gdb on the command line with option -ex. You need to repeat this for each command. This can be useful when your program needs to read stdin so you don't want to redirect it. Eg, for od -c

echo abc |
gdb -ex 'break main' -ex 'run -c' -ex bt -ex cont -ex quit  od

So in particular for your question, you can use:

gdb -ex 'run arg1 arg2' -ex bt -ex quit ./program

The commands could be fed in on standard input:

#!/bin/sh
exec gdb -q <<EOF
file ./program
run arg1 arg2
bt
quit
EOF

Or the commands can be placed in afile and gdb run with gdb -batch -x afile, or if you hate newlines (and the maintenance coder) with a fancy shell you can do it all on a single line (a different way to express the heredoc version):

gdb -q <<< "file ./program"$'\n'run$'\n'...

Tags:

Gdb