I'd like to write a script that (under certain conditions) will execute gdb and automatically run some program X with some set of arguments Y. Once the program has finished executing the user should remain at gdb's prompt until s/he explicitly exits it.
One way to do this would be to have the script output the run command plus arguments Y to some file F and then have the script invoke gdb like this:
gdb X < F
But is there a way to do this without introducing a temporary file?
Passing arguments to the program being debugged. The --args option must be immediately followed by the command invoking the program you wish to debug. That command should consist of the program name and then its arguments, just as they would appear if you were starting that program without GDB.
Invoking GDB. Invoke GDB by running the program gdb . Once started, GDB reads commands from the terminal until you tell it to exit. You can also run gdb with a variety of arguments and options, to specify more of your debugging environment at the outset.
Type "gdb [filename]" where [filename] is the name of the compiled file you wish to debug (the name you type to run your program). Set the arguments. If your program runs with any command line arguments, you should input them with "set args".
The easiest way to do this given a program X
and list of parameters a b c
:
X a b c
Is to use gdb
's --args
option, as follows:
gdb --args X a b c
gdb --help
has this to say about --args
:
--args Arguments after executable-file are passed to inferior
Which means that the first argument after --args
is the executable to debug, and all the arguments after that are passed as is to that executable.
If you want to run some commands through GDB and then have it exit or run to completion, just do
echo commands | gdb X
If you want to leave it at the command prompt after running those commands, you can do
(echo commands; cat) | gdb X
This results in echoing the commands to GDB, and then you type into the cat
process, which copies its stdin to stdout, which is piped into GDB.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With