c++ was a system call executed properly - c++

I have written a small c++ program that takes some input files and runs some ffmpeg processes on them (via the 'system()' function). I would like to add to that program some code to delete the original files but I need to be sure that the ffmpeg commands executed properly and with no errors. How can I get my c++ program to check if the system() function it used executed properly?

According to the documentation for system
If command is not a null pointer, the value returned depends on the
system and library implementations, but it is generally expected to be
the status code returned by the called command, if supported.
In other words:
if(system("mycommand") != 0)
{
cout << "mycommand failed..." << endl;
}
or something like that. [Obviously assuming that "mycommand" is defined to give a result code of 0 if successfull - most things do, but there are exceptions].

Related

strange behavior with system command in c++

I have written a code in that at some point calls an external executable using the system command.
Here is how I do this:
First remove the output files of the external code.
boost::filesystem::remove("Results/CVground.bin");
boost::filesystem::remove("Results/CVground.BUD");
then write input files for the external code. The code is too long to paste here. It just writes few ascii files.
Then run the model. To make things even worse as you can see I'm calling a windows executable under linux using wine.
std::string sim_command = "/opt/wine-stable/bin/wine ";
sim_command.append(cvd.simulationExe()).append(" CVsimAqua.in >/dev/null");
int sys = system(sim_command.c_str());
Essentially the command I'm calling under system is the following
int sys = system("/opt/wine-stable/bin/wine Simulation3.02.exe CVsimAqua.in >/dev/null");
UPDATE:
Based on the suggestion I modified the above command so that it prints to a file as such
int sys = system("/opt/wine-stable/bin/wine Simulation3.02.exe CVsimAqua.in > log.dat");
This is usually takes a couple of minutes to complete.
Then I'm doing a check that the output files actually exists as follows:
if (!boost::filesystem::exists("Results/CVground.bin")) {
std::cout << "\t\tSys output from rank " << rank << " is " << sys << std::endl;
fun.clear();
fun.push_back(10000000);
fun.push_back(10000000);
boost::filesystem::current_path(main_dir);
return;
}
I have found that in some cases I do get the following print in my log file. The rank number is different in each run.
Sys output from rank 78 is 32512
What I don't understand is that the output of system is positive, which I believe it means that the system command was successful. Is there any way to capture more information from the system command?
UPDATE
After I changed the from /dev/null to log.dat I realized that when the system fails it doesn't even create the log.dat file, however I always check with system(NULL) that system is available before calling the system.
I have found extremely difficult to debug this because these errors occur only when I run the code on the cluster.
Is it possible that the system command returns before the execution code finishes? I have seen a definite NO answer to that question but I was wondering if things are getting trickier since I'm calling via wine.
Thank you
The system function returns the exit code from the command you run. By convention, processes return 0 on success and non-zero on failure. What a non-zero exit code means, depends on the application, wine in this case. If may be easier to debug if you don't redirect its output to /dev/null
There are some specific return values for when the system call itself fails: -1 is a process could not be created, and 127 if a shell could not be executed. See man 3 system for the details on those cases.

Printing program and function name of each instruction with Pin tool

I'm new to writing a pin tool to instrument the program.
Currently, I'm kind of stuck with printing out the program name (image? I would say) and the function that the instruction belongs to.
For example, I I have a program foo.cpp and function name func() that simple addition and cout.
Then, when I use a pin tool, I want to print like below
0xAddress foo (or lib64/ld-linux... etc) func disassembled_instruction (ex. move etc)
I can get the address and disassembled instructions, but not the program and function name.
Can anyone suggest me whether this is possible and how?
Thank you!
Program Name
To get the full path to the main binary (hence the program name) you must set an instrumentation routine for IMG (image) in your main() using IMG_AddInstrumentFunction.
In the analysis callback (passed to IMG_AddInstrumentFunction) use the IMG_IsMainExecutable function which simply returns a boolean indicating if the currently loaded image is the main binary (true) or not.
If the former function (IMG_IsMainExecutable) returns true you can call IMG_Name to get its full path.
For a full example see the Detecting the Loading and Unloading of Images (Image Instrumentation) example in the manual.
Function Name
Use PIN_InitSymbols in your main, before calling PIN_StartProgram.
You can instrument at the routine level using RTN_AddInstrumentFunction (or get the routine from the instruction, BBL or TRACE).
Once you have the RTN (routine), you can get its name with the RTN_Name function.
Check the manual for the example Procedure Instruction Count (Routine Instrumentation) which should give you a good start on how to use these functions.
Note: as obvious as its sounds, the target executable must have symbolic information (symbols): No symbols == no routine names.
You can use standard predefined macros for printing out the program and function name.
cout << __FILE__ << " " << __FUNCTION__ << endl;

Check for error in command passed to popen API in cpp

There is a cpp application where I want to read following type of compressed file-
file_name.gz
file_name.Z
file_name.tar.gz
For this purpose, I check the file extension and choose decompression technique accordingly. E.g. file_name.gz will be decompressed using "gunzip -C file_name.gz".
I want to get the FILE handle for decompressed file. I use popen() API for it. Now, there might be a case where gunzip/uncompress/tar fails while decompressing the file due to memory issues. How do I capture the failure in my CPP application. There is way to check if popen failed or not. What about command passed to popen().
Please help. I tried to find it at various places but could not get satisfactory solution.
When a process terminates normally, it is expected to return the exit code of 0 (legally, EXIT_SUCCESS) to the parent. Otherwise, in the case of a crash or any other abnormal termination, a non-zero value is expected to be returned. You can obtain the exit code by calling pclose(). If the code is 0, the child process most probably terminated successfully.

Is there a way to let IAR CSPY return an error code defined by executed user program?

I am using IAR EWARM's cspybat to run some unit tests for my embedded code using Unity. I would like an easy way for my build server to determine if the unit tests passed or failed. Is there a way for CSPY to return a nonzero error code if my unit tests fail? I have tried changing the return value in main() with no change. Is there a function I can call to force an error to be returned?
My cspybat batch file looks like this:
"C:\Program Files (x86)\IAR Systems\Embedded Workbench 7.4\common\bin\cspybat" -f "C:\Work\Sandbox\ST\stmicroeval\_iar_ewarm_project\settings\Project.UnitTest.general.xcl" --backend -f "C:\Work\Sandbox\ST\stmicroeval\_iar_ewarm_project\settings\Project.UnitTest.driver.xcl"
Unfortunately, no.
I've solved this by replacing "exit" with a function that prints a specific pattern, plus the exit code. I then wrapped the call to cspybat into a script that 1) strips the output of the extra output and 2) exits with the desired exit code.
It's late 2020 and they still don't offer a mechanism to do this.
We solved it by including a macro file with the contents:
execUserExit()
{
__message "program exited with __exit_value = ", __exit_value:%d ;
}
And having our own exit variable in the code:
extern "C" int __exit_value=0xff;
That we set prior to calling exit() (though you could just write your own version of exit())
This makes the debugger always print SOMETHING, even if the program crashes on startup.
Then we parse with a python wrapper:
pattern = "__exit_value =\s([\-|0-9|a-f|A-F|x]*)"
retvalue = int(re.findall(pattern,process.stdout)[0])

Invoke another exe and get value

How to invoke another .exe and then get the returned value?
Here's the code that I tried and failed:
int main() {
int ret = (int) system("Test.exe");
}
In this code ret holds Zero value but it's should be able to container Test.exe's value.
system returns OS return code, not the console output. There is no portable way to get the output of the program you run (#Rapptz correction, system calls are implementation-defined).
Much easier (at least for some basic usage) would be to redirect output of said .exe to a file, and then read that file.