How to pass arguments and redirect stdin from a file to program run in gdb?
Pass the arguments to the run
command from within gdb.
$ gdb ./a.out
(gdb) r < t
Starting program: /dir/a.out < t
How to debug C program with argument and stdin as input
For easier debugging with GDB, you should convert the "here string" lines between the <<end
and end
into a text file (say "input.txt"). Then, in gdb
you can use the set args
command to set up the command-line arguments and redirection of standard input from the file.
For example: suppose the file "input.txt" contains:
Monos(1,2)
Monos(6)
Run gdb
from the shell as follows:
$ gdb ./test
Within GDB, set the command-line arguments and redirection of standard input:
(gdb) set args 1 2 3 < input.txt
Set any breakpoints, e.g.:
(gdb) b main
And start running the code:
(gdb) r
How to load program reading stdin and taking parameters in gdb?
If you were doing it from a shell you'd do it like this:
% gdb myprogram
gdb> run params ... < input.txt
This seems to work within emacs too.
Pass File Input and Stdin to gdb
You can run the program in one console and attach to it with gdb from another one when it is waiting for input. Therefore you will be able to enter program input in the 1st console and debug it in the 2nd.
How do I pass the arguments from a text file to run a program under gdb?
Thanks and I got an simple solution.
(gdb) run $( cat arg.txt )
It is also possible to pass the output of a command to be the arguments.
(gdb) run $( ruby -e 'print( "text as arguments" )' )
How do I run a program with commandline arguments using GDB within a Bash script?
You can run gdb with --args
parameter:
gdb --args executablename arg1 arg2 arg3
If you are doing this often (e.g. when running GDB from a script), you might want to consider the following arguments to automate things further. First, you can place your GDB commands (such as 'run') in a text file and provide the filename to the -x
argument. Second, you can have GDB exit after running your commands by providing the --batch
argument. A full example:
gdb -x commands.txt --batch --args executablename arg1 arg2 arg3
Passing stdin arguments to executable being debugged with gdb
SOLVED
definitely an issue with bash calls inside gdb on macOS, managed to workaround like this:
$ gdb --args ./a.out $(perl -e 'print "\x41"')
UPDATE - NOW REALLY SOLVED
@EmployedRussian was right, the problem was in the stdin passing through bash to gdb:
This is a problem with macOS Sierra:
gdb can't start with a shell (bash) if SIP is enabled, so there are two ways of using gdb on Sierra:
Using gdb 8.0.1 on Sierra:
OPTION 1: Disable shell startup on gdb:
echo "set startup-with-shell off" >> ~/.gdbinit
when I installed and codesigned gdb I did this while following the official wiki (https://sourceware.org/gdb/wiki/BuildingOnDarwin) and since forgot. That was the reason I couldn't pass stdin to gdb.
OPTION 2: Disable SIP and leave shell startup enable
echo "set startup-with-shell on" >> ~/.gdbinit
reboot into Recovery and:
csrutil disable
This is obviously not recommended but it serves to illustrate where the problem is regarding gdb 8.0.1 usage on Sierra. Hopefully a gdb update will fix this.
How to use gdb with input redirection?
~$ 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.
Related Topics
How to Show Enter Password in the Form of Asterisks(*) on Terminal
Why Doesn't Reinterpret_Cast Force Copy_N for Casts Between Same-Sized Types
Better Shading on Bw Display While Rendering Filled Surfaces
Comparing Character Arrays and String Literals in C++
C++ Error, Undefined Reference Class
What Is Linux's Native Gui API
In Cmake, How to Test If the Compiler Is Clang
Easiest Way of Using Min Priority Queue with Key Update in C++
Why Would Anyone Use Set Instead of Unordered_Set
How to Avoid Including Class Implementation Files
Why Is Locking a Std::Mutex Twice 'Undefined Behaviour'
Why Can't I Do Polymorphism with Normal Variables
How to Use a String as a Variable Name in C++
Address-Of Operator (&) VS Reference Operator(&)
Get Function Names from Call Stack
What Is the Most Efficient Way to Display Decoded Video Frames in Qt
C++ Project Compiled with Modern Compiler, But Linked Against Outdated Libstdc++