I have the following code that calls an executable and checks the return value (OS is Linux):
system($path_to_my_executable);
my $exit_value = $? >> 8;
my $signal_num = $? & 127;
my $dumped_core = $? & 128;
# check for segfaults
if ($signal_num == 11)
{
if (!$dumped_core)
{
printf("ERROR: executable exited with segmentation fault.\n");
exit(-1);
}
else
{
printf("ERROR: executable exited with segmentation fault (core
+ dumped).\n");
}
}
elsif ($signal_num != 0)
{
printf("ERROR: executable exited abnormally with signal $signal_nu
+m\n");
exit(-1);
}
elsif ($exit_value == 255)
{
printf("ERROR: executable exited abnormally with value -1, exiting
+...\n");
exit(-1);
}
The original intention of the above code was to catch all segmentation faults from myexecutable and exit the script if one was found. The problem is that my program seg faulted but there was no signal, i.e. $? contained the value 0b1000101100000000. The upper 8 bits had an exit code of 139, which indicates a seg fault, but the signal for seg fault was not set. Consequently my code above did not catch the seg fault. I have two questions:
1. Do segmentation faults not require that signal 11 be sent? Under what circumstances will signal 11 be sent in the event of a segmentation fault?
2. Is there a module or library I can use to automatically catch all of the "bad" return values so I don't have to code all of the enumerations of signals and return values myself?