646

When running a program on GDB, usually, the arguments for the program are given at the 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 GDB to the program after the script starts executing. But then I will have to 'wait' the program.

Is there another way to do this?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
drox
  • 7,523
  • 4
  • 23
  • 34
  • 3
    possible duplicate of [How to pass arguments and redirect stdin from a file to program run in gdb?](http://stackoverflow.com/questions/4521015/how-to-pass-arguments-and-redirect-stdin-from-a-file-to-program-run-in-gdb) – Ciro Santilli OurBigBook.com May 15 '15 at 10:44

9 Answers9

926

You can run gdb with --args parameter:

gdb --args executablename arg1 arg2 arg3

If you are doing this often (e.g. when running GDB from a script), you might want to consider the following arguments to automate things further. First, you can place your GDB commands (such as 'run') in a text file and provide the filename to the -x argument. Second, you can have GDB exit after running your commands by providing the --batch argument. A full example:

gdb -x commands.txt --batch --args executablename arg1 arg2 arg3
Hugo Ideler
  • 10,094
  • 2
  • 20
  • 22
  • 85
    Argg, `man gdb` does not contain `--args`, that's why I did not find it. `gdb --help` does. – Ciro Santilli OurBigBook.com May 15 '15 at 10:31
  • 9
    @CiroSantilli新疆改造中心法轮功六四事件 GDB is GNU. To view the full documentation of GNU tools, remember to use `info gdb` next time :-). – Apteryx Jan 15 '20 at 18:33
  • 7
    @CiroSantilli新疆改造中心法轮功六四事件 I got hooked to info after discovering the 'i' key (`index-search`). Try: `info gdb`, then 'i', then '--args'. It'll bring you right to it. – Apteryx Jan 15 '20 at 23:04
  • @Apteryx When I press 'i' it shows "No indices found." at the bottom of the screen. – Wawrzyniec Pruski Mar 18 '22 at 17:54
  • @WawrzyniecPruski on which GNU/Linux distribution is that? I suspect something's wrong with the info manual shipped with their GDB package; using GNU Guix, you can `guix shell gdb info-reader -- info gdb`, and the `i RET --args` sequence works from there. – Apteryx Mar 21 '22 at 01:15
  • @Apteryx Centos 7.9.2009 – Wawrzyniec Pruski Mar 22 '22 at 03:16
224
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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Hugo
  • 2,402
  • 1
  • 13
  • 4
91

Another way to do this, which I personally find slightly more convenient and intuitive (without having to remember the --args parameter), is to compile normally, and use r arg1 arg2 arg3 directly from within gdb, like so:

$ gcc -g *.c *.h
$ gdb ./a.out
(gdb) r arg1 arg2 arg3
Peter Varo
  • 11,726
  • 7
  • 55
  • 77
aspiring_sarge
  • 2,355
  • 1
  • 25
  • 32
  • 34
    This is exactly what the OP is hoping to avoid having to do. – KarateSnowMachine Mar 09 '16 at 15:52
  • 12
    Hmmm, yes, thank you, fair point. I'm not sure how that's escaped my notice for so long. I think I'll leave the answer here, however, because iirc, I stumbled upon this question when I'd googled something like "How to pass command line arguments using gdb", and finding the answer missing, I went on to add it, without realizing (for almost a year!) that my answer didn't address the question at hand. – aspiring_sarge Mar 10 '16 at 02:27
  • 11
    @KarateSnowMachine As someone arriving from Google as well, this answer is *more* along the lines of what I was looking for. Perhaps we should revise the question to be more general since it has such a general title, or maybe we should narrow the title. Making it less restrictive would probably be more useful to the greater number of readers. – jpmc26 Nov 10 '19 at 01:40
21

You could create a file with context:

run arg1 arg2 arg3 etc

program input

And call gdb like

gdb prog < file
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
George Kastrinis
  • 4,924
  • 4
  • 29
  • 46
7

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

Sandburg
  • 757
  • 15
  • 28
7

Much too late, but here is a method that works during gdb session.

gdb <executable>

then

(gdb) apropos argument

This will return lots of matches, the useful one is set args.

set args -- Set argument list to give program being debugged when it is started.

set args arg1 arg2 ...

then

r

This will run the program, passing to main(argc, argv) the arguments and the argument count.

alinsoar
  • 15,386
  • 4
  • 57
  • 74
6

If the --args parameter is not working on your machine (i.e. on Solaris 8), you may start gdb like

gdb -ex "set args <arg 1> <arg 2> ... <arg n>"

And you can combine this with inputting a file to stdin and "running immediatelly":

gdb -ex "set args <arg 1> <arg 2> ... <arg n> < <input file>" -ex "r"
Johannes
  • 2,901
  • 5
  • 30
  • 50
4

gdb has --init-command <somefile> where somefile has a list of gdb commands to run, I use this to have //GDB comments in my code, then `

echo "file ./a.out" > run
grep -nrIH "//GDB"|
    sed "s/\(^[^:]\+:[^:]\+\):.*$/\1/g" |
    awk '{print "b" " " $1}'|
    grep -v $(echo $0|sed "s/.*\///g") >> run
gdb --init-command ./run -ex=r

as a script, which puts the command to load the debug symbols, and then generates a list of break commands to put a break point for each //GDB comment, and starts it running

Austin_Anderson
  • 900
  • 6
  • 16
1

If you want to pass arguments from file,
e.g. scanf from a file as input

try this

(gdb) run < the_file_contains_data
galian
  • 832
  • 6
  • 12