Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I run a program with commandline arguments using GDB within a Bash script?

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?

like image 640
drox Avatar asked May 25 '11 07:05

drox


People also ask

How do you pass command-line arguments to a program when using GDB?

You can optionally have gdb pass any arguments after the executable file to the inferior using --args . This option stops option processing. This will cause gdb to debug gcc , and to set gcc 's command-line arguments (see Arguments) to ` -O2 -c foo. c '.

How do I run a program under GDB?

Starting your program. Use the run command to start your program under GDB. You must first specify the program name (except on VxWorks) with an argument to GDB (see section Getting In and Out of GDB), or by using the file or exec-file command (see section Commands to specify files).

Can you pass arguments to a bash script?

Passing arguments before runningWe can pass parameters just after the name of the script while running the bash interpreter command. You can pass parameters or arguments to the file.


2 Answers

You can run gdb with --args parameter,

gdb --args executablename arg1 arg2 arg3 

If you want it to run automatically, place some commands in a file (e.g. 'run') and give it as argument: -x /tmp/cmds. Optionally you can run with -batch mode.

gdb -batch -x /tmp/cmds --args executablename arg1 arg2 arg3 
like image 175
Hugo Ideler Avatar answered Sep 20 '22 01:09

Hugo Ideler


gdb -ex=r --args myprogram arg1 arg2 

-ex=r is short for -ex=run and tells gdb to run your program immediately, rather than wait for you to type "run" at the prompt. Then --args says that everything that follows is the command and arguments, just as you'd normally type them at the commandline prompt.

like image 30
Hugo Avatar answered Sep 24 '22 01:09

Hugo