I'm using GDB to do some reverse engineering of a basic C program. The program does not accept command line input. It does accept input from standard in during run time. I'm trying to script the standard in by doing
run < temp
Where temp is a file containing something like
1
0
AAAAAAAAA
AAAAA
0
Unfortunately, when I do this, GDB appears to loop infinitely over this programs input.
I tried to make this file using python
python -c 'print "AAAAA"' > temp
And with the solution presented here: How to debug a program that takes user input from stdin with GDB?
But no luck. When I just step through the program with GDB, I can pass input via standard in and that work's just fine.
I'm a bit stumped...
Related
Ok so I created my own shell, and I've tested it plenty on my own, but I need shell scripts that will run it and test it.
I've create a script which consist of this:
#!/bin/bash
echo "ls && echo dog" | ./a.out
However, all it does is print the command prompt "$" infinitely, and I have to force quit the program. therefore I am pretty sure my program does not like my script lol. My program works by using getline to capture the user input until they push <enter> and the boost library to tokenize the string and look for connector e.g "||" "&&" ";" and and so on, then run the commands. All of this is done in a while loop that loops until the user types exit and I close my program. Being as I am new to writing scripts I am sure I probably am not writing my script in the best of manners. I created a simple program to ask for your age and then output it and this script method works for that, but being as my shell isn't as simple I am not surprised this scrip doesn't seem to work.
string user_input;
bool good = true;
typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
while(good){
//command prompt
cout << "$ ";
//read in user input
getline(cin, user_input);
//tokenize user input
tokenizer tok(user_input);
//parse and execute commands inputed by user in string
//only exit while loop if user command is <exit> good = false
}
my shell works if I execute the program normally and I enter inputs into the program what I need is a shell script that I can use to run and test the program for me. Ex. if I type ./script.sh in the standard linux shell it will run my script which will then execute my a.out and then test my own shell with a variety of commands Examples being ls echo ...
You should exit the shell when you reach EOF (End Of File). Getline while return -1 in that case.
I can't think of any other advice as you didn't provide any code, but this might resolve the infinite loop issue.
It seems that input redirection in gdb does not work in Cygwin e.g
(gdb) run < input.txt
Is there other way to redirect input in gdb of Cygwin??
Unfortunately this is not possible when running gdb in cygwin. The bug exists for a quote long time, but apparently it's a hard one to fix - and probably the gdb devs prefer spending time on features/issues relevant to more common environments (such as Linux).
There are various possible workarounds; I'd prefer the first one since it's the cleanest and also useful while not debugging / running on cygwin:
Add a command line argument, e.g. -f whatever with whatever being the filename to read from. If the argument is not present or set to -, read from stdin. The -f - option is optional of course but for arguments accepting filenames it's a common standard (as long as it makes sense) to handle - as "use stdin/out".
Use the gdb hack mentioned here to remap stdin to a manually opened file inside the application:
> gdb yourexecutable
(gdb) break main
(gdb) run
(gdb) call dup2(open("input.txt", 0), 0)
(gdb) continue
This sets a breakpoint on the main function, then executes the program which will break right after entering main. Then dup2 is used to replace the stdin fd (0) with a file descriptor of the input file.
Actually I have trouble naming the title of this post. Because I don't know how to summarize my meaning in a professional way. But I'll explain my question as below:
I'm running a program written by C++, command is:
./VariationHunter_SC
Then it'll let you type in many parameters:
Please enter the minimum paired-end insert size:
Please enter the maximum paired-end insert size:
Please enter the pre-processing mapping prune probability:
Please enter the name of the input file:
Please enter the minimum support for a cluster:
Obviously I need to type in such parameters one by one to run the program; But I have thousands of such jobs, and need to pre-assign such parameters in script, and submit script to computer.
So how can I do that?
thx
Edit
so how can I make parameter-list?
Just like below?:
140
160
0
mrfast.vh
1
Seems the program cannot recognize these numbers, and distribute numbers..
This depends on how the program actually reads the data that you type in - it's likely that its reading stdin, so you could use separate files with the parameters and pass them in via redirection: ./VariationHunter_SC < parameter-file
It's also possible that the program will accept parameters on the command line, but there's no way of really knowing that (or how) except by whatever documentation the program might come with (or by reading the source code, if it's available and there is no other accurate docs).
Simply use the piping character to pipe the contents of a file to your program
example, in a windows command shell:
echo "asdf" | pause
This will pass "asdf" to the pause program. As a result, pause will print a "Press any key to continue" message, then imediately continue because it will receive the "asdf" string as a response.
So, overall, write or use a program that outputs the contents of your file. Call it, then pipe its output to the program that needs the input.
The unix cat command is such a command that writes the contents of a file to output, or to the input of another executable if you are piping the output.
I have a simple C++ program that takes in inputs and outputs some string. Like this:
$ ./game
$ what kind of game? type r for regular, s for special.
$ r
$ choose a number from 1 - 10
$ 1
$ no try again
$ 2
$ no try again
$ 5
$ yes you WIN!
Now I want to write a c++ program can runs this c++ program and plays the game automatically without user input and then outputs it to a file or standard output.
Running it would look like this:
./program game r > outputfile
game is the game program, r for playing regular style.
How should I do this? The main reason I need this program is that I want to do automatic testing for a much bigger program.
You could use std::system from <cstdlib>:
std::system("game r > outputfile");
The return value is ./program's, the sole argument must be of type char const *.
There is no standard way to run a program and feed it standard input, though. Judging by your command line, you're on some Unix variant where popen from <stdio.h> should work:
FILE *sub = popen("game r > outputfile", "w");
then write to sub with the stdio functions and read outputfile afterwards.
(But for simple testing, I'd recommend implementing the core logic of your program as a set of functions/classes that can be run by a custom main function in a loop; or pick your favorite scripting language to handle this kind of thing.)
I'd be more efficient to add a caller function to your main source and have it control looping, logging, and feeding input. It would also not require system calls or other magic to pull off. Being a game programmer, we have our games play themselves as much as possible to help with debugging, and almost always this is done via internal code, not through external scripting or system calls. It makes it easier to feed viable input as well.
This scenario cries out for a "script", IMHO.
Bash, Perl, Python - you name it.
SIMPLEST CASE:
Just write a bash script to call ./program game r > outputfile.
Or ./program game r < test_input.txt > test_output.txt
For more advanced scenarios, you might want to look at "expect".
You might also want to look at "STAF", which might be a great way to "automate your automated tests":
http://staf.sourceforge.net/current/STAFFAQ.htm
I need to control a program in c++ (windows), I need to call it, then pass data to it as I collect it, finally after a certain command that program will use that data.
I need to open the prog.exe and then line by line or value by value supply it information, it works manually through cmd.
I have tried system() but this will stop after I open the program.
I need something like this.
//call it
prog.exe
//add data
DataStart
Data 1 [2 34 454 5]//etc
DataEnd //the program will take it from here.
all being passed though command line
There are different ways you could do this - if your program needs to execute part of the way through your code before getting the data as input, you can just use standard input, and prompt the user to type the data. If you want to use variable values for the input, but you will know them before execution, you can pass the information as command line arguments, where you will execute like so
prog.exe 1 2 3
and your program will access the data via argv[i] where i corresponds to each command line argument.
have your program read from standard input, and from the command line 'pipe' the result of the other program to yours
eg.
datagenerator.exe | prog.exe
assuming that datagenerator.exe writes to standard output, the | character will redirect the output to prog.exe's standard input