我通常这样运行一个程序:

./a.out arg1 arg2 <file

我想用gdb调试它。

我知道设置参数的功能,但这只从gdb提示工作。


当前回答

如果你想在gdb中使用裸运行命令来执行带有重定向和参数的程序,你可以使用set args:

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

我无法实现相同的行为与——args参数,gdb激烈逃避重定向,即。

% 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
...

这个函数实际上重定向了gdb本身的输入,这不是我们在这里真正想要的

% gdb --args echo 1 2 <file
zsh: no such file or directory: file

其他回答

在任何命令前面键入debug,以便在shell级别上使用gdb调试它,这不是很好吗?

下面是这个函数。它甚至与以下工作:

"$program" "$@" < <(in) 1> >(out) 2> >(two) 3> >(three)

这是一个你不能控制任何东西的调用,所有东西都是可变的,可以包含空格,换行符和shell元字符。在本例中,In、out、two和three是其他任意命令,它们使用或产生的数据必须不受损害。

以下bash函数在这样的环境中几乎干净地调用了gdb:

debug()
{
  1000<&0 1001>&1 1002>&2 \
  0</dev/tty 1>/dev/tty 2>&0 \
  /usr/bin/gdb -q -nx -nw \
  -ex 'set exec-wrapper /bin/bash -c "exec 0<&1000 1>&1001 2>&1002 \"\$@\"" exec' \
  -ex r \
  --args "$@";
}

在前面输入debug:

之前:

p=($'\n' $'I\'am\'evil' "  yay  ")
"b u g" "${p[@]}" < <(in) 1> >(out) 2> >(two) 3> >(three)

后:

p=($'\n' $'I\'am\'evil' "  yay  ")
debug "b u g" "${p[@]}" < <(in) 1> >(out) 2> >(two) 3> >(three)

就是这样。现在用gdb进行调试是绝对不需要动脑筋的事情。除了一些细节或更多:

gdb does not quit automatically and hence keeps the IO redirection open until you exit gdb. But I call this a feature. You cannot easily pass argv0 to the program like with exec -a arg0 command args. Following should do this trick: After exec-wrapper change "exec to "exec -a \"\${DEBUG_ARG0:-\$1}\". There are FDs above 1000 open, which are normally closed. If this is a problem, change 0<&1000 1>&1001 2>&1002 to read 0<&1000 1>&1001 2>&1002 1000<&- 1001>&- 1002>&- You cannot run two debuggers in parallel. There also might be issues, if some other command consumes /dev/tty (or STDIN). To fix that, replace /dev/tty with "${DEBUGTTY:-/dev/tty}". In some other TTY type tty; sleep inf and then use the printed TTY (i. E. /dev/pts/60) for debugging, as in DEBUGTTY=/dev/pts/60 debug command arg... That's the Power of Shell, get used to it!

功能解释:

1000<&0 1001>&1 1002>&2 moves away the first 3 FDs This assumes, that FDs 1000, 1001 and 1002 are free 0</dev/tty 1>/dev/tty 2>&0 restores the first 3 FDs to point to your current TTY. So you can control gdb. /usr/bin/gdb -q -nx -nw runs gdb invokes gdb on shell -ex 'set exec-wrapper /bin/bash -c "exec 0<&1000 1>&1001 2>&1002 \"\$@\"" creates a startup wrapper, which restores the first 3 FDs which were saved to 1000 and above -ex r starts the program using the exec-wrapper --args "$@" passes the arguments as given

这不是很容易吗?

如果你想在gdb中使用裸运行命令来执行带有重定向和参数的程序,你可以使用set args:

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

我无法实现相同的行为与——args参数,gdb激烈逃避重定向,即。

% 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
...

这个函数实际上重定向了gdb本身的输入,这不是我们在这里真正想要的

% gdb --args echo 1 2 <file
zsh: no such file or directory: file

在项目上启动GDB。

转到项目目录,在那里您已经编译了项目可执行文件。发出命令gdb和可执行文件的名称,如下所示: gdb projectExecutablename

这将启动gdb,打印以下内容: GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.04) 7.11.1 版权所有2016自由软件基金会 ................................................. 输入“apropos word”搜索与“word”相关的命令… 从projectExecutablename读取符号…完成。 (gdb)

Before you start your program running, you want to set up your breakpoints. The break command allows you to do so. To set a breakpoint at the beginning of the function named main: (gdb) b main Once you've have the (gdb) prompt, the run command starts the executable running. If the program you are debugging requires any command-line arguments, you specify them to the run command. If you wanted to run my program on the "xfiles" file (which is in a folder "mulder" in the project directory), you'd do the following: (gdb) r mulder/xfiles

希望这能有所帮助。

免责声明:此解决方案不是我的,它改编自https://web.stanford.edu/class/cs107/guide_gdb.html 这个简短的gdb指南很可能是由斯坦福大学开发的。

你可以这样做:

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

神奇的一点是——args。

只需在gdb命令控制台中输入run来启动调试。

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

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