51

I have a program and I am trying to debug it using gdb. Inside the program I have methods that require the user to enter an input using stdin. How can I enter this input when I am in gdb? So that I can trace how my methods work?

1

3 Answers 3

33
$ cat >foo <<EOF
something
EOF
$ gdb -quiet /bin/cat
Reading symbols from /bin/cat...(no debugging symbols found)...done.
Missing separate debuginfos, use: debuginfo-install coreutils-8.12-7.fc16.x86_64
(gdb) run <foo
Starting program: /bin/cat <foo
something
[Inferior 1 (process 22436) exited normally]
(gdb) 
Sign up to request clarification or add additional context in comments.

Comments

10

You can also run your program first, then attach GDB to it:

gdb --pid $(pgrep your_program)

This way you will be able to run your program interactively in a separate terminal.

Note: attaching GDB to another process might require using sudo or changing permissions.

4 Comments

how to run program first? I mean the executable compiled with -g flag is a.out. Should I run it using a.out some parameters. Then gdb --pid <id>? Or the `some parameters' will be entered in gdb?
I executed my file ./a.out 0 1 map1.txt. I connect gdb just like I mentioned in previour comment. However, I was unable to debug the run executable.
Ok it require admin acces. I was able to connect it by using sudo gdb ..... Is there another way to make it possible without using sudo?
@ÖmerGÜZEL You can find the answer here: stackoverflow.com/questions/45171339/…. But be wary that setting ptrace_scope to 0 has security implications as any process will be able to attach itself to another process.
3

I just went through something like this yesterday and recursed through a bunch of "help" commands in gdb because I couldn't find exactly what I needed on the Internet.

I used set variable *your_variable* = *your desired input* after I had started gdb and began running my code. Worked like a charm.

I know this is late, but maybe it'll help someone else.

1 Comment

How would you use this for stdin, though?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.