When I run execute normally (without GDB), the args[0] is what I entered on the command prompt. But when I run using GDB, it includes full path. Is there any way to GDB to set args[0] same as command prompt?
My code is as follows:
#include <stdio.h>
// defining main with arguments
int main(int argc, char* argv[])
{
printf("You have entered %d arguments:\n", argc);
for (int i = 0; i < argc; i++) {
printf("%s\n", argv[i]);
}
return 0;
}
Stand alone:
./a.out
You have entered 1 arguments:
./a.out
With GDB:
gdb ./a.out
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-120.el7
(gdb) run
Starting program: /home/prasads/./a.out
You have entered 1 arguments:
/home/prasads/./a.out
Is there a way without having to change the code that I can get GDB to same output as command prompt?
Is there a way without having to change the code that I can get GDB to same output as command prompt?
Yes: you can use exec-wrapper to set argv[0]
to anything you want.
Update:
exec-wrapper seems to be used for setting env variables; Not sure how I can use exec-wrapper to change argv[0]
Here is an example:
#include <stdio.h>
int main(int argc, char *argv[])
{
for (int j = 0; j < argc; ++j) printf("%d: %s\n", j, argv[j]);
return 0;
}
gcc echo-argv.c -o echo-argv
gdb --batch -ex run --args ./echo-argv foo bar baz
0: /tmp/echo-argv
1: foo
2: bar
3: baz
[Inferior 1 (process 3580209) exited normally]
cat wrapper.sh
#!/bin/bash
exec -a "xxx" "$@"
gdb --batch -ex 'set exec-wrapper ./wrapper.sh' -ex run --args ./echo-argv foo bar baz
0: xxx
1: foo
2: bar
3: baz
[Inferior 1 (process 3580521) exited normally]
Replace xxx
with ./a.out
or whatever else you want.
P.S. This GDB bug may also be relevant here.