When running a program on GDB, usually, the arguments for the program are given at the run
command. Is there a way to run the program using GDB and as well as give arguments within a shell script?
I saw an answer in a related question, mentioning that we can attach GDB to the program after the script starts executing. But then I will have to 'wait' the program.
Is there another way to do this?
You can run gdb with --args
parameter:
gdb --args executablename arg1 arg2 arg3
If you are doing this often (e.g. when running GDB from a script), you might want to consider the following arguments to automate things further. First, you can place your GDB commands (such as 'run') in a text file and provide the filename to the -x
argument. Second, you can have GDB exit after running your commands by providing the --batch
argument. A full example:
gdb -x commands.txt --batch --args executablename arg1 arg2 arg3