How do I pass a command line argument while starting up GDB in Linux?

CLinuxDebuggingGdbCommand Line-Arguments

C Problem Overview


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.

C Solutions


Solution 1 - C

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

Solution 2 - C

Try

gdb --args InsertionSortWithErrors arg1toinsort arg2toinsort

Solution 3 - C

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.

Solution 4 - C

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionShadyBearsView Question on Stackoverflow
Solution 1 - Cldav1sView Answer on Stackoverflow
Solution 2 - CBasile StarynkevitchView Answer on Stackoverflow
Solution 3 - CAlexView Answer on Stackoverflow
Solution 4 - CFernando Gonzalez SanchezView Answer on Stackoverflow