How to debug a program that takes user input from stdin with GDB?

后端 未结 3 589
没有蜡笔的小新
没有蜡笔的小新 2020-12-13 13:09

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 whe

相关标签:
3条回答
  • 2020-12-13 13:18

    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.

    0 讨论(0)
  • 2020-12-13 13:21
    $ 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) 
    
    0 讨论(0)
  • 2020-12-13 13:22

    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.

    0 讨论(0)
提交回复
热议问题