How do I run a program with commandline args using gdb within a bash script?


Answers

gdb -ex=r --args myprogram arg1 arg2

-ex=r is short for -ex=run and tells gdb to run your program immediately, rather than wait for you to type "run" at the prompt. Then --args says that everything that follows is the command and arguments, just as you'd normally type them at the commandline prompt.

Question

When running a program on gdb, usually, the arguments for the program are given at run command. Is there a way to run the program using gdb and as well as give arguments within a shell script?

I saw an answer in a related question, mentioning that we can attach the gdb to the program after script starts executing. But then I will have to 'wait' the program.

I'm curious whether there is any other way to do this.




An addition to the answer of Hugo Ideler. When using arguments having themself prefix like -- or -, I was not sure to conflict with gdb one.

It seems gdb takes all after args option as arguments for the program.

At first I wanted to be sure, I ran gdb with quotes around your args, it is removed at launch.

This works too, but optional:

gdb --args executablename "--arg1" "--arg2" "--arg3"

This doesn't work :

gdb --args executablename "--arg1" "--arg2" "--arg3" -tui

In that case, -tui is used as my program parameter not as gdb one.




You could create a file with context:

run arg1 arg2 arg3 etc

program input

And call gdb like

gdb prog < file



Links