intel-pin: How to pass command line arguments to binary - c++

I have a simple c code. I am running the binary of that with my pin tool:
pin -t tool.so -- test/test.o a
Here, test/test.o is a binary, and a is some random command line argument to the pin tool (say tool.so), and not the binary (so, there is a distinction between passing command line argument to the pin tool and to the binary).
I would like to know how can I pass command line input (say arg1) to the binary which I am running with the pin tool.
(like we would pass with - ./test/test.o arg1)
Note: I think knowing my pin tool and the c code is irrelevant here.

What you describe here will pass command line arguments to the program you're running. Command line arguments to the tool are all the arguments that come after the -t argument and before the -- (double dash) which indicates the binary and its arguments

Related

Writing tests for cli input

I have a small Go app with a cli using flags and I've been asked to make it more testable.
My app gets called on the command line like
deploy.exe <task> <command> -tenant tenant_name -validate -package "c:\some dir\\"
Based on which task and command a different execution path is called and ultimately a func residing in another package is called like:
if command == "db" {
dbhelper.RunDBCmds(*tenant, *validate, *package)
}
I need to write unit tests for just the flag parsing, without calling the actual functions at the end.
I'm fairly new to Go and I'm struggling figuring out how to accomplish this. I've thought about adding my Os.Args() and Flag parsing to a function that takes input and outputs a pointer of sorts to the RunDBCmds(*tenant, ...) func. However, I'm just not sure I can accomplish returning a pointer to a function.
I'd appreciate any advice on how I can make my code more testable without actually invoking the functions.
If all of your tasks/commands have different sets of flags I would end up with introducing some kind of Command abstraction. The best example can be found in the Go source code itself:
base.Commands = []*base.Command{
work.CmdBuild,
clean.CmdClean,
doc.CmdDoc,
envcmd.CmdEnv,
bug.CmdBug,
fix.CmdFix,
//...
}
Each command can have its own flag.FlagSet to parse command-specific flags:
// A Command is an implementation of a go command
// like go build or go fix.
type Command struct {
// Run runs the command.
// The args are the arguments after the command name.
Run func(cmd *Command, args []string)
// UsageLine is the one-line usage message.
// The first word in the line is taken to be the command name.
UsageLine string
// Short is the short description shown in the 'go help' output.
Short string
// Long is the long message shown in the 'go help <this-command>' output.
Long string
// Flag is a set of flags specific to this command.
Flag flag.FlagSet
// CustomFlags indicates that the command will do its own
// flag parsing.
CustomFlags bool
}
Using this approach you can separate commands' flags parsing from the execution.

Prettify clang -v output

Is there any way to get clang output to insert carriage returns? When compiling with the verbose option, I just get these huge unreadable dumps of compiler flags and paths.
Use popen to start your clang session. Create a new command line as clang -v (including the space) and concatenate the arguments that you usually feed to clang itself. After the final argument, add 2>&1 to convert Clang's stderr output to regular stdout so popen can pick it up. Then loop over popen's input and parse each separate line, adding extra information where you see fit.
As an example, I grabbed the entire set of flags for my local Clang using
clang -cc1 --help
and incorporated this as a single long string in my C program. Then I looped over the return results from popen, scanning for flags starting with -, and when one was found, I scanned the long flags string for this. If it found something, I write it out on a separate line in green (using ANSI escape sequences). Then I test the flags string if an argument should follow – these usually have a leading <...> indicator. If so, I write it out in blue. Finally, I write out the entire flag explanation line until I encounter an end-of-line.
With my very rough program called colorclang – 123 lines of actual code – I get output like this:
As it is, it tests every input line for possible flags so there is some mis-coloring. More exact parsing is possible; I'd have to add separate routines for the single line starting with "/usr/bin/clang" (for the common Clang flags), the single line starting with "/usr/bin/ld" (and parse the loader flags), and possibly the lines after each #include .. statement.
Pieced together with the help of Complete list of clang flags?, Steve Kemp's answer to C: Run a System Command and Get Output?, and after deducing clang -v writes to stderr, larsman's answer to c popen won't catch stderr.

How to handle multiple syntax command line arguments using boost.program_options

Consider a utility which can be used in different modes, like:
utility.exe --mode x (one list of arguments expected)
utility.exe --mode y (different list of arguments expected)
How can i write code for parsing such arguments in Boost.Program_Options???
You could parse this mode out yourself (or with boost program options use a way that ignores unknown options), and then depending on that outcome, you could feed the cmd line arguments to different boost program option objects.

C++ - system command

I have a C++ program which is mainly used for video processing. Inside the program, I am launching the system command in order to obtain pass the processed videos to some other binaries to postprocess them.
I have a while loop towards infinite and I am launching the system command inside the loop every time. The thing is that at a certain point I am receiving the -1 return code from the system command. What could be the reason for that?
Inside the system command I am just calling a binary file with the adequate parameters from the main project.
The system command which I want to execute is actually a shell file.
In this file I am extracting the main feature from the video and passing them through a SVM model from a 3D party library in order to obtain the the desired classification.
./LiveGestureKernel ./Video ./SvmVideo
./mat4libsvm31 -l SvmVideoLabels < SvmVideo > temp_test_file
./svm-predict temp_test_file svm_model temp_output_file
cat < temp_output_file
rm -f temp_*
After a certain number of iterations through the while loop, it just won't execute the script file and I cannot figure out the reason for this. Thanks!
If you get -1 from the call to system(), you should first examine the contents of errno - that will most likely tell you what your specific problem is.
The one thing to watch out for is that the return value from system is an implementation-defined one in the case where you pass it a non-NULL command, so it's possible that -1 may be coming from your actual executable.
Your best bet in that case is to print out (or otherwise log) the command being executed on failure (and possibly all the time), so that you can check what happens with the same arguments when you execute it directly from a command line or shell.

Control a shell program through command line, giving it multiple instructions/data

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