gdb takes only the first parameter

I'm trying to debug a program which takes on the command line a couple of arguments. Inside the main I print out the arguments as follows:

int main (int argc, char **argv)
{
 for (int i = 0; i < argc; i++) {
  printf("param%d=%s\n", i, argv[i]);
}

when I run my program without gdb, like this

"program img.jpg 1 2"

I get as output:

param0: program
param1: img.jpg
param2: 1
param3: 2

When I run it with gdb like this:

"program img.jpg 1 2"

I only get

param0: img.jpg

on one hand img.jpg should be param1, also param2 and 3 are missing. Is there a special way to specify command line params to gdb that I'm missing?

Answers


You should specify parameters when you run the program.

First you should invoke the debugger with

$ gdb <binary_file_name (executable)>

Than you start the program also passing command line arguments:

(gdb) r p1 -p2 --p3 p4=p5

Side note: I think this solution is more flexible than using the --args flag, because you can launch your program multiple times with different parameters without quitting the current gdb instance (e.g., you keep your breakpoints).


You are now sending parameters to gdb instead of program. In order to pass parameters to program you can use the --args parameter for gdb.

gdb --args program img.jpg 1 2

You can use the --args option to tell gdb to pass the arguments to the program being debugged:

> gdb --args program img.jpg 1 2

Alternatively, you can launch gdb without the arguments, and then supply them to the run command:

> gdb program
(gdb) run img.jpg 1 2

Need Your Help

Read Write file to android error with cocos2dx (v3.6)

android c++ cocos2d-x cocos2d-x-3.0

I'm trying to store game stage status into a file with Cocos2d-x (v3.6). But it works in iOS but not in Android. Here is the code i'm using. could anyone please tell me what's wrong i'm doing?

Publish to facebook timeline from app with a button

php facebook publish publishing timeline

i feel so frustrated, since i have been looking for this all day, from hundreds of sites and with my accumulated frustration i´m not making my life easier.