views:

5724

answers:

7

Hi,

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?

Thanks.

A: 

cat F | gdb X should be identical. So you can use anything that produces output and pipe that into gdb instead of the cat command here.

I'm assuming you're correct and gdb reads from stdin.

A: 
echo command arguments | gdb X
InsDel
+8  A: 

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.

Adam Rosenfield
(echo "run params"; cat) | gdb X; # worked a treat, many thanks!
IanVaughan
+8  A: 

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.

Nathan Fellman
I had one use case wherein say some function foo(a,b,c) (any data type), this has a call in say main(), now I want to input this arguments even before I start executing the program. Can we achieve this?
Kedar
@Kedar: Not this way. This method determines the arguments passed to `main()`. It's up to you to transfer them properly to `foo`.
Nathan Fellman
Ok, that make my doubt clear; that there is no way we can feed arguments to any internal method inside main (unless done some by transfer)! Thanks @Nathan Fellman
Kedar
+1  A: 

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 ;)

RandomNickName42
+3  A: 

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
mike v