How to pass arguments and redirect stdin from a file to program run in gdb?

I usually run a program as :

./a.out arg1 arg2 <file

I would like to debug it using gdb.

I am aware of the set args functionality, but that only works from the gdb prompt.


将参数从gdb中传递给run命令。

$ gdb ./a.out
(gdb) r < t
Starting program: /dir/a.out < t

You can do this:

gdb --args path/to/executable -every -arg you can=think < of

The magic bit being --args .

Just type run in the gdb command console to start debugging.


If you want to have bare run command in gdb to execute your program with redirections and arguments, you can use set args :

% gdb ./a.out
(gdb) set args arg1 arg2 <file
(gdb) run

I was unable to achieve the same behaviour with --args parameter, gdb fiercely escapes the redirections, ie

% gdb --args echo 1 2 "<file"
(gdb) show args
Argument list to give program being debugged when it is started is "1 2 <file".
(gdb) run
...
1 2 <file
...

This one actually redirects the input of gdb itself, not what we really want here

% gdb --args echo 1 2 <file
zsh: no such file or directory: file
链接地址: http://www.djcxy.com/p/86032.html

上一篇: 如何在GDB中打印寄存器值?

下一篇: 如何传递参数并将stdin从文件重定向到程序在gdb中运行?