Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Shell 调用gdb自动将参数传递给正在调试的程序_Shell_Gdb - Fatal编程技术网

Shell 调用gdb自动将参数传递给正在调试的程序

Shell 调用gdb自动将参数传递给正在调试的程序,shell,gdb,Shell,Gdb,我想写一个脚本(在某些条件下)将执行gdb并自动运行一些程序X和一些参数集Y。一旦程序完成执行,用户应该保持在gdb的提示下,直到他/她明确退出它 一种方法是让脚本将run命令和参数Y输出到某个文件F,然后让脚本像这样调用gdb: gdb X < F gdb -ex "target remote localhost:1234" gdbx

我想写一个脚本(在某些条件下)将执行gdb并自动运行一些程序X和一些参数集Y。一旦程序完成执行,用户应该保持在gdb的提示下,直到他/她明确退出它

一种方法是让脚本将run命令和参数Y输出到某个文件F,然后让脚本像这样调用gdb:

gdb X < F
gdb -ex "target remote localhost:1234"
gdbx
但是有没有一种方法可以在不引入临时文件的情况下做到这一点


谢谢。

cat F | gdb X
应该是相同的。因此,您可以使用生成输出的任何东西,并将其导入gdb,而不是这里的cat命令


我假设您是正确的,gdb从stdin读取。

如果您想通过gdb运行一些命令,然后让它退出或运行到完成,只需这样做

echo commands | gdb X
如果要在运行这些命令后将其保留在命令提示下,可以执行以下操作

(echo commands; cat) | gdb X

这导致将命令回显到GDB,然后您输入
cat
进程,该进程将其stdin复制到stdout,stdout通过管道传输到GDB。

在给定程序
X
和参数列表的情况下,最简单的方法是:

X a b c
要使用
gdb
--args
选项,如下所示:

gdb --args X a b c

gdb--help
是关于
--args
的:

--将可执行文件传递给下级后的args参数

这意味着
--args
之后的第一个参数是要调试的可执行文件,之后的所有参数都按原样传递给该可执行文件

gdb target -e "my-automation-commands"
我的自动化命令包含您通常希望运行的任何内容

break 0x123
set args "foo" bar 2
r
如果您有一些标准的init脚本,则严格来说不是临时文件;)

有选项-x,例如

其中gdb_命令可以是例如(在android emulator的情况下):


好吧,这只是一个注释,不是一个真正的答案-只是想包括一些代码片段。我在
bash
/Ubuntu-Lucid上——对我来说,我遇到的问题与在:“.”中几乎相同

基本上,我希望实现与以下代码片段相同的功能:

$ gdb
GNU gdb (GDB) 7.1-ubuntu
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "i486-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
(gdb) pwd
Working directory /media/work/dir.
(gdb) 
$ echo -e "***\n" <(echo "pwd") "\n***\n`cat <(ls -C /dev/fd ; echo; for ix in /dev/fd/*; do irl=$(readlink -f $ix); echo $ix -\> $irl; ls -la $ix 2>&1; ls -la $irl 2>&1; echo '______'; done ; ls -C /dev/fd )`"

***
 /dev/fd/63 
***
0  1  2  3  63

/dev/fd/0 -> /dev/pts/0
lrwx------ 1 user user 64 2010-11-07 21:18 /dev/fd/0 -> /dev/pts/0
crw--w---- 1 user tty 136, 0 2010-11-07 21:18 /dev/pts/0
______
/dev/fd/1 -> /proc/10713/fd/pipe:[236191]
l-wx------ 1 user user 64 2010-11-07 21:18 /dev/fd/1 -> pipe:[236151]
ls: cannot access /proc/10713/fd/pipe:[236191]: No such file or directory
______
/dev/fd/2 -> /dev/pts/0
l-wx------ 1 user user 64 2010-11-07 21:18 /dev/fd/2 -> pipe:[236151]
crw--w---- 1 user tty 136, 0 2010-11-07 21:18 /dev/pts/0
______
/dev/fd/255 -> /proc/10721/fd/255
ls: cannot access /dev/fd/255: No such file or directory
ls: cannot access /proc/10721/fd/255: No such file or directory
______
/dev/fd/3 -> /proc/10725/fd/3
ls: cannot access /dev/fd/3: No such file or directory
ls: cannot access /proc/10725/fd/3: No such file or directory
______
0  1  2  3
好吧,希望这对某人有所帮助,
干杯


编辑:现在至少我知道了为什么进程替换不起作用了——它将使用一个临时文件描述符,它不能被
ls
识别为文件(因此
gdb
肯定不能读取它;此外,引用几乎立即消失,除非进程以某种方式被阻止,如
cat
)-请参阅终端日志片段:

$ gdb
GNU gdb (GDB) 7.1-ubuntu
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "i486-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
(gdb) pwd
Working directory /media/work/dir.
(gdb) 
$ echo -e "***\n" <(echo "pwd") "\n***\n`cat <(ls -C /dev/fd ; echo; for ix in /dev/fd/*; do irl=$(readlink -f $ix); echo $ix -\> $irl; ls -la $ix 2>&1; ls -la $irl 2>&1; echo '______'; done ; ls -C /dev/fd )`"

***
 /dev/fd/63 
***
0  1  2  3  63

/dev/fd/0 -> /dev/pts/0
lrwx------ 1 user user 64 2010-11-07 21:18 /dev/fd/0 -> /dev/pts/0
crw--w---- 1 user tty 136, 0 2010-11-07 21:18 /dev/pts/0
______
/dev/fd/1 -> /proc/10713/fd/pipe:[236191]
l-wx------ 1 user user 64 2010-11-07 21:18 /dev/fd/1 -> pipe:[236151]
ls: cannot access /proc/10713/fd/pipe:[236191]: No such file or directory
______
/dev/fd/2 -> /dev/pts/0
l-wx------ 1 user user 64 2010-11-07 21:18 /dev/fd/2 -> pipe:[236151]
crw--w---- 1 user tty 136, 0 2010-11-07 21:18 /dev/pts/0
______
/dev/fd/255 -> /proc/10721/fd/255
ls: cannot access /dev/fd/255: No such file or directory
ls: cannot access /proc/10721/fd/255: No such file or directory
______
/dev/fd/3 -> /proc/10725/fd/3
ls: cannot access /dev/fd/3: No such file or directory
ls: cannot access /proc/10725/fd/3: No such file or directory
______
0  1  2  3

您可以尝试使用
stty-cooked
打开原始字符模式(或关闭缓冲/煮熟模式),然后
cat
将以
^[[A
,并移动光标-不幸的是,在此模式下,Ctrl-C不再工作,因此您将无法以这种方式关闭
cat

使用bash,您可以创建一个脚本,为正在执行的任何可执行文件提供类似用户的输入:

#!/bin/sh
gdb X <<GDB_INPUT
pwd
run X a b c
quit
GDB_INPUT
!/bin/sh

gdb X在尝试了这里的所有答案之后

  • echo/cat黑客虽然聪明,但破坏了gdb的许多重要功能。最值得注意的是,所有用户提示都会自动响应(因此您没有机会确认潜在的危险操作),而Ctrl+C(停止正在调试的进程)最终会杀死cat,因此在那之后您实际上无法与gdb通话
  • x选项应该可以工作,但我无法让它与我的gdb版本一起工作,它需要一个临时文件
  • 但是,事实证明,您可以使用-ex,如下所示:

    gdb X < F
    
    gdb -ex "target remote localhost:1234"
    

    您还可以多次指定-ex来运行多个命令!

    (echo“run params”cat)124bx;#非常感谢!请注意,如果这样做,您将失去shell的交互性(所有读线编辑功能、历史记录等)。只是注意到up/down不起作用。这不是一个可接受的解决方案:(@ijw,@Nils:你是对的。有关更好的解决方案,请参阅mike v的答案()。我有一个用例,其中某个函数foo(a,b,c)(任何数据类型),它在say main()中有一个调用,现在我想在开始执行程序之前输入这个参数。我们可以实现吗?@Kedar:不是这样。这个方法确定传递给
    main()的参数
    。这取决于您是否将它们正确地传输到
    foo
    。好吧,这让我很清楚;我们无法将参数提供给main中的任何内部方法(除非通过传输完成)!谢谢@Nathan Fellman您甚至可以
    gdb-ex-run--args prog-arg…
    让程序自动启动。请注意,如果您不想使用临时文件,可以使用进程替换:
    gdb-x只需使用
    gdb-ex'foo'-ex'bar'
    用命令
    foo
    然后
    bar
    运行gdb。您怎么可以我让gdb在那之后继续运行,而不是退出?。因此:gdb-ex-run--args prog arg…似乎是更好的方法