Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

gdb

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?

like image 836
FranXh Avatar asked Oct 27 '12 21:10

FranXh


People also ask

How do you specify command line arguments to program being debugged in GDB?

Passing arguments to the program being debugged. The --args option must be immediately followed by the command invoking the program you wish to debug. That command should consist of the program name and then its arguments, just as they would appear if you were starting that program without GDB.

What is a command in GDB that will bring your debugging into the function?

To debug your program in gdb, you have to run it by typing "run". However, if you just type "run" gdb will run your program to completion without debugging it at all. If your program crashes, gdb will stop it and allow you to debug it.

How do I run a program in debugger?

To run a program in a debuggerClick the Image File tab. In the Image box, type the name of an executable file or DLL, including the file name extension,and then press the TAB key. This activates the check boxes on the Image File tab. Click the Debugger check box to select it.


2 Answers

$ 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)  
like image 99
matt Avatar answered Sep 22 '22 23:09

matt


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.

like image 33
Hedede Avatar answered Sep 18 '22 23:09

Hedede