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?


In reply to Help checking program return values by Special_K

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.