82

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?

1

4 Answers 4

137
~$ 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.

Sign up to request clarification or add additional context in comments.

3 Comments

Also works with start < inputfile if you want to start debugging at the beginning of main().
For multiple inputs, U can separate the inputs with new line in the input.txt.
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

2 Comments

Sorry, but why the -2? Why not just gdb -ex 'set args < file' ./binary ?
Oh! Ok! You mean head -2, the two first lines of a file. Just another arg. My bad. ;)
4

You can try the following:

(gdb) run < input.txt

1 Comment

1

Another alternative seems to be:

$ gdb /usr/bin/head
(gdb) set args `cat /etc/passwd`
(gdb) run

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.