How do I run a program with commandline args using gdb within a bash script?
When running a program on gdb, usually, the arguments for the program are given at 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 the gdb to the program after script starts executing. But then I will have to 'wait' the program.
I'm curious whether there is any other way to do this.
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 (eg '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
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.
另一种做法是,我个人发现稍微更方便和直观(不必记住--args参数),就是正常编译,并直接从gdb中使用r arg1 arg2 arg3
,如下所示:
gcc -g *.c *.h
gdb ./a.out
(gdb) r arg1 arh2 arg3
链接地址: http://www.djcxy.com/p/86036.html
上一篇: dup2()不执行