Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Help checking program return values

by Special_K (Scribe)
on Jul 29, 2020 at 16:58 UTC ( #11120004=perlquestion: print w/replies, xml ) Need Help??

Special_K has asked for the wisdom of the Perl Monks concerning the following question:

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?

Replies are listed 'Best First'.
Re: Help checking program return values
by hippo (Chancellor) on Jul 29, 2020 at 18:03 UTC

    You haven't said which O/S you are running on and that might well have an impact here.

    elseif ($signal_num != 0)

    That isn't Perl, so either the code you are showing isn't the code you are running or your code isn't running at all.

    The upper 8 bits had an exit code of 139, which indicates a seg fault, but the signal for seg fault was not set.

    An exit code of 139 does not necessarily indicate a seg fault. Trivially:

    #!/usr/bin/env perl use strict; use warnings; system ('perl -e "exit 139"'); my $exit_value = $? >> 8; my $signal_num = $? & 127; my $dumped_core = $? & 128; print <<EOT Exit value: $exit_value Signal: $signal_num Core: $dumped_core EOT
    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?

    Unlikely because your idea of "bad" may not be someone else's idea of "bad". However, you could simply write your own module for your own use and then use that in all your other code.


    🦛

      > You haven't said which O/S you are running on and that might well have an impact here.

      Linux (I've updated my post to say this).

      > That isn't Perl, so either the code you are showing isn't the code you are running or your code isn't running at all.

      Correct, it should have said "elsif" as I manually typed that out into the web submission form. This has also been corrected.

      > An exit code of 139 does not necessarily indicate a seg fault. Trivially:

      Correct, I could make an executable return any code I want it to under any circumstances, but assuming I'm only using the return codes 0 and 255 inside the executable to indicate correct and incorrect return values respectively, what is the most reliable way to detect a segmentation fault (or other undesirable return values) that were initiated by the OS?
Re: Help checking program return values
by haukex (Bishop) on Jul 29, 2020 at 20:49 UTC
    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?

    IPC::System::Simple has some pretty nice error handling. For other modules, see my post here.

Re: Help checking program return values
by perlfan (Priest) on Jul 29, 2020 at 17:20 UTC
    Generally, you catch signals by defining a handler. See perlipc (maybe lookat at $SIG{CHLD}). This at least will provide you a place to start. I've never attempted to do what you are with a child process. This has surely been solved somewhere in CPAN.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://11120004]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (4)
As of 2020-09-25 11:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If at first I donít succeed, I Ö










    Results (137 votes). Check out past polls.

    Notices?