set args command
Sets the default arguments for the debugged program.
Syntax
show args
Parameters
- Arguments
- Specifies the default command-line arguments that will be passed to the program.
Remarks
The arguments specified with set args will be used if you start the program with the run command.
If you provide arguments with the run command, the arguments set with set args will not be used.
Restrictions
If you set the arguments using the set args command after the program has been already started, they will not take effect until you restart the program.
The set args command has no effect when attaching to processes with attach.
Examples
In this example we will run the following C++ program:
int main(int argc, char *argv[])
{
for(int i = 1; i < argc; i++)
printf("Argument %d: %s\n", i, argv[i]);
return 0;
}
We will set the default arguments, start the program and observe its output:
Breakpoint 1 at 0x401395: file 0.cpp, line 4.
(gdb) set args "Hello, World" Test
(gdb) run
Starting program: c:"Hello, World" Test
[New Thread 704.0x418]
Breakpoint 1, main (argc=3, argv=0x3e3f68) at 0.cpp:4
4 {
(gdb) show args
Argument list to give program being debugged when it is started is ""Hello, World" Test".
(gdb) continue
Continuing.
Argument 1: Hello, World
Argument 2: Test
[Inferior 1 (process 704) exited normally]
(gdb)
Common errors
Setting the arguments after you have started your program will not take effect until you restart it:
(gdb) b main
Breakpoint 1 at 0x401395: file 0.cpp, line 4.
(gdb) run
Starting program: c:Test1
[New Thread 1992.0x9c0]
Breakpoint 1, main (argc=2, argv=0x3e3f38) at 0.cpp:4
4 {
(gdb) print *argv@argc
$1 = {0x3e3ec5 "c:\mingw\bin\0.exe", 0x3e3ed9 "Test1"}
(gdb) set args Test2
(gdb) print *argv@argc
$2 = {0x3e3ec5 "c:\mingw\bin\0.exe", 0x3e3ed9 "Test1"}
(gdb) run
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: c:Test2
[New Thread 828.0x62c]
Breakpoint 1, main (argc=2, argv=0x3e3f38) at 0.cpp:4
4 {
(gdb) print *argv@argc
$3 = {0x3e3ec5 "c:\mingw\bin\0.exe", 0x3e3ed9 "Test2"}
(gdb)
Compatibility with VisualGDB
Do not execute the set args command manually under Visual Studio. Use the VisualGDB Project Properties dialog or the VisualGDB toolbar in Visual Studio to set the arguments of the debugged program: