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?
            Asked
            
        
        
            Active
            
        
            Viewed 3.8k times
        
    47
            
            
        - 
                    For the simpler case of direct input, the question is the same as this: http://stackoverflow.com/questions/455544 There are however cases which this does not cover: 1) you need to see stdout to decide stdin 2) actual timing and keypresses are needed like in ncurses. In those cases, maybe expect might do it: http://stackoverflow.com/questions/5579901/automated-test-tools-for-linux-ncurses – Ciro Santilli OurBigBook.com Jun 10 '15 at 17:48
 
3 Answers
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) 
        matt
        
- 5,364
 - 1
 - 25
 - 25
 
8
            
            
        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.
        Hedede
        
- 1,133
 - 13
 - 27
 
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.
        BWONG
        
- 153
 - 1
 - 2
 - 9