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 finishe
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.
After trying all of the answers here,
However, it turns out you can just use -ex, like this:
gdb -ex "target remote localhost:1234"
You can also specify -ex multiple times to run multiple commands!
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.
there is option -x, e.g.
gdb -x gdb_commands exe_file
where gdb_commands can be for example (in the case of android emulator):
target remote :5039
gdb target -e "my-automation-commands"
my-automation-commands containing anything you would normally want to run,
break 0x123
set args "foo" bar 2
r
Not strictly a temp file, if you have a few standard init scripts ;)
With bash you can create a script that give user like input to any executable you're executing:
#!/bin/sh
gdb X <<GDB_INPUT
pwd
run X a b c
quit
GDB_INPUT