Why does the program exit code change during debugging?

I have a C ++ program that I use to verify that another program is not crashing. Parent code (we will call it parentProg) looks something like this:

int run(const char * command) { ... int retCode = system(command); printf("Code is %d\n",retCode); if(retCode == 134) //128 + SIGABORT { // Record error conditions ... } } 

The command variable contains the program under test (we will call it "childProg"). With the previous Linux distribution, this code worked as expected. If a.out crashes or hits a statement, it will return 134, and the error handling code will work. However, after I switched to the new Linux distribution, this is no longer the case. Instead, I see a return code of 6 when I run as the created program using GDB or nemiver. Oddly enough, it returns to 134 if I run the child program myself or using DDD.

For the following tests, I modified childProg only as the following code:

 #include <assert.h> int main(int argc, char * argv[]) { assert(0); return 0; } 

childProg in itself

 [ user@localhost multi]$ ./childProg childProg: temp.cpp:5: int main(int, char **): Assertion `0' failed. Abort [ user@localhost multi]$ echo $? 134 

parentProg that spawns childProg

 [ user@localhost multi]$ ./parentProg 1 o Running 1 times childProg: temp.cpp:5: int main(int, char **): Assertion `0' failed. Code is 6 Done 0 [ user@localhost multi]$ 

With gdb

 (gdb) run Starting program: parentProg 1 o Running 1 times Detaching after fork from child process 3311. childProg: temp.cpp:5: int main(int, char **): Assertion `0' failed. Code is 6 Done 0 [Inferior 1 (process 3295) exited normally] (gdb) 

With DDD

 (gdb) run 1 o Starting program: parentProg 1 o Running 1 times Detaching after fork from child process 3336. childProg: temp.cpp:5: int main(int, char **): Assertion `0' failed. Code is 134 Done 0 [Inferior 1 (process 3319) exited normally] (gdb) 

It works as expected.

 [ me@localhost multi]$ /bin/sh -c ./childProg childProg: temp.cpp:5: int main(int, char **): Assertion `0' failed. Abort [ me@localhost multi]$ echo $? 134 

What could be here? Is there a better way to check for failures / segfaults / assertions besides checking the exit code?

+4
source share
2 answers

The <sys / wait.h> header file contains macros for parsing return codes.

To check the reason for the exit, you can do something like this:

 #include <stdio.h> #include <sys/wait.h> ... void run(const char *command) { int retcode = system(command); if (WIFEXITED(retcode)) { int status = WEXITSTATUS(retcode); if (status) { printf("Exited normally with FAILURE status of %d!\n", status); } else { printf("Exited normally with SUCCESS(0) status!\n"); } } else if (WIFSIGNALED(retcode)) { int signal = WTERMSIG(retcode); printf("Abnormal termination - program crashed or was aborted with signal %d\n", signal); } } 

See "man waitpid" for a description of these macros.

+6
source

(134 = 6 | __WCOREFLAG)

See man 2 wait

+1
source

Source: https://habr.com/ru/post/1489026/


All Articles