命令行中自动输入输入不起作用

命令行中自动输入输入不起作用

它类似于自动在命令行中输入。但这在我的处境下不起作用。

我想要的是运行后gdb自动输入target remote:1337。我的脚本如下:

printf 'target remote:1337\n' | gdb

但是运行完脚本后gdb自动退出,日志如下:

printf 'target remote:1337\n' | gdb
GNU gdb (GDB) 8.3
Copyright (C) 2019 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 "x86_64-apple-darwin14.5.0".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word".
(gdb) Remote debugging using :1337
Reading /data/local/tmp/helloworld.out from remote target...
warning: File transfers from remote targets can be slow. Use "set sysroot" to access files locally instead.
Reading /data/local/tmp/helloworld.out from remote target...
Reading symbols from target:/data/local/tmp/helloworld.out...
_start () at helloworld.s:5
5       blx _thumb
(gdb) quit
A debugging session is active.

    Inferior 1 [process 11826] will be killed.

Quit anyway? (y or n) [answered Y; input not from terminal]

如果我手动执行此操作。它可以正常工作。

如何解决?谢谢!

答案1

对于 ,您不需要这个gdb。您可以使用 指定初始命令-ex,或使用文件中的命令-x(或使用.gdbinit文件而不是)。从手册页

-command=file
-x file
   Execute GDB commands from file file.

-ex command
   Execute given GDB command.

例如-ex

% gdb -ex 'target remote localhost:1337'
GNU gdb (GDB) 10.1
Copyright (C) 2020 Free Software Foundation, Inc.
...
Type "apropos word" to search for commands related to "word".
Remote debugging using localhost:1337
(gdb) 

或者使用-x和一个文件:

% echo 'target remote localhost:1337' > foo                       
% gdb -x foo                            
GNU gdb (GDB) 10.1
Copyright (C) 2020 Free Software Foundation, Inc.
...
0x00007ffff7fd2090 in _start () from target:/lib64/ld-linux-x86-64.so.2
(gdb) 

相关内容