1

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?

4

3 回答 3

4

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

于 2012-07-24T12:15:07.160 回答
1

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
于 2012-07-24T12:17:24.183 回答
1

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
于 2012-07-24T12:17:57.960 回答