77

In the Terminal, I have:

myapp < myfileinput

But if I want to use gdb,

gdb myapp < myfileinput

It didn't run correctly.

How to use gdb here?

galoget
  • 722
  • 9
  • 15
  • Possible duplicate of [How to load program reading stdin and taking parameters in gdb?](http://stackoverflow.com/questions/455544/how-to-load-program-reading-stdin-and-taking-parameters-in-gdb) – Ciro Santilli OurBigBook.com Apr 23 '17 at 06:33

4 Answers4

127
~$ gdb <executable>

GNU gdb (Ubuntu/Linaro 7.3-0ubuntu2) 7.3-2011.08
Copyright (C) 2011 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 "i686-linux-gnu".
For bug reporting instructions, please see:
<http://bugs.launchpad.net/gdb-linaro/>...
Reading symbols from /home/abhishek/maxtest...done.

(gdb) run < input.txt

This is doing the trick for me. Wondering if this was what you were looking for.

Abhishek
  • 2,543
  • 4
  • 34
  • 46
13

Try running your application from within gdb?

(gdb) file /usr/bin/head
Reading symbols from /usr/bin/head...(no debugging symbols found)...done.
(gdb) run -2 < /etc/passwd
Starting program: /usr/bin/head -2 < /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/bin/sh

Program exited normally.
(gdb)

EDIT: Alternatively:

gdb -q -ex 'set args -2 < /etc/passwd' /usr/bin/head
Reading symbols from /usr/bin/head...done.

(gdb) run
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/bin/sh

Program exited normally.
(gdb) quit
Employed Russian
  • 199,314
  • 34
  • 295
  • 362
sarnold
  • 102,305
  • 22
  • 181
  • 238
2

You can try the following:

(gdb) run < input.txt
galoget
  • 722
  • 9
  • 15
Sachin
  • 29
  • 1
1

Another alternative seems to be:

$ gdb /usr/bin/head
(gdb) set args `cat /etc/passwd`
(gdb) run
Walfits
  • 446
  • 5
  • 9