150

I have to debug a program that has errors in it as part of my assignment. However, I must first pass command line arguments in order to solve this problem.

I do:

gdb -tui InsertionSortWithErrors

which works, but after that I don't know how to pass arguments. I used gdb -help and it says something about --args which I also tried and it didn't work.

I want to be able to get the debugger+the GUIand pass command line arguments.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ShadyBears
  • 3,955
  • 13
  • 44
  • 66

4 Answers4

249

Once gdb starts, you can run the program using "r args".

So if you are running your code by:

$ executablefile arg1 arg2 arg3 

Debug it on gdb by:

$ gdb executablefile  
(gdb) r arg1 arg2 arg3
ldav1s
  • 15,885
  • 2
  • 53
  • 56
  • 11
    Just to expand on this....I am running mine normally like: `program --option1 --option2=argvalue` so in gdb I typed: `r --option1 --option2=argvalue` This was not obvious to me at first. – harperville Oct 01 '15 at 19:00
  • 4
    This is rightly the top-voted comment. I'd just like to add that `r` stands for the gdb command `run`, and you can see a bit of help for it by typing `help run` while in gdb. – Carl Smotricz Nov 03 '16 at 13:24
  • 5
    i don't want to have to type the arguments every time i start gdb, i want it in my command line history – Michael Dec 08 '18 at 23:06
123

Try

gdb --args InsertionSortWithErrors arg1toinsort arg2toinsort
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • 2
    like mentioned in the GNU GDB Manual section [Invoking GDB](https://sourceware.org/gdb/current/onlinedocs/gdb/Invoking-GDB.html#Invoking-GDB) – RubenLaguna Jun 22 '15 at 09:32
  • 1
    It is also mentioned if you run `gdb -h`, which in turn is mentioned in the man page. – Pietro Saccardi Apr 06 '16 at 14:13
  • 2
    @PietroSaccardi there seems to be some discrepancy between the man page and the `-h` output, at least on some Ubuntu versions. This can happen, especially if the man file file for a command is not updated as often as the source code that parses the `-h` option. – tresf Nov 14 '17 at 04:05
31

Another option, once inside the GDB shell, before running the program, you can do

(gdb) set args file1 file2

and inspect it with:

(gdb) show args
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
30

I'm using GDB7.1.1, as --help shows:

gdb [options] --args executable-file [inferior-arguments ...]

IMHO, the order is a bit unintuitive at first.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Alex
  • 3,264
  • 1
  • 25
  • 40
  • 2
    I agree, very unintuitive, so the real commandline would read: `$ gdb --args `. I mistakenly added quotes around everything after `--args` thing which lead caused gdb to parse the whole thing as the executable. – zpon Jul 25 '16 at 13:51
  • I think it's so the arguments come right after the executable, as if you were running without gdb. – qwr Sep 21 '17 at 21:08
  • 3
    What I think is unintutive is `--args`. If it was spelt `--run` it would be very intuitive – Basile Starynkevitch Aug 08 '18 at 13:54