Gdb in one command

gdb

When I debug an executable program with arguments arg1 arg2 with gdb I perform the following sequence

gdb
file ./program
run arg1 arg2
bt
quit

How can I do the same from one command line in shell script?

Best Answer

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
Related Question