0

It's similar with Automatically enter input in command line. But this not work in my situation.

What I want is after run gdb, I want it automatic input target remote:1337. Which I script like this:

printf 'target remote:1337\n' | gdb

But the gdb exit automatic after run the script. The log is:

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]

If I do this manually. It can work normally.

How to fix? Thank you!

1 Answers1

3

You don't need this for gdb. You can specify an initial command using -ex or commands from a file using -x (or use .gdbinit files instead). From the manpage:

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

-ex command Execute given GDB command.

Example with -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) 

Or with -x and a file:

% 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) 
muru
  • 197,895
  • 55
  • 485
  • 740