Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invoke gdb to automatically pass arguments to the program being debugged

Tags:

shell

gdb

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?

like image 874
user41162 Avatar asked Nov 26 '08 20:11

user41162


People also ask

How do you specify command-line arguments to program being debugged in GDB?

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.

How do you invoke 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.

How do you set arguments in GDB?

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".


2 Answers

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.

like image 70
Nathan Fellman Avatar answered Nov 01 '22 01:11

Nathan Fellman


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.

like image 20
Adam Rosenfield Avatar answered Nov 01 '22 03:11

Adam Rosenfield