Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

"Segmentation fault" text not captured by perl script

by Special_K (Monk)
on Apr 01, 2020 at 17:21 UTC ( [id://11114914]=perlquestion: print w/replies, xml ) Need Help??

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

I am debugging an issue with a compiled program written in c++ that is being called from a perl script. If I create a shell script that calls the compiled program directly:

#!/bin/tcsh myexecutable exit

And then run it, the last 2 lines written to the console are:

right before out-of-bounds array access Segmentation fault

If I instead write the following standalone perl script:

#!/tool/bin/perl use strict; my $cmd = "myexecutable"; system($cmd); my $exit_value = $? >> 8; my $signal_num = $? & 127; my $dumped_core = $? & 128; printf("exit_value = $exit_value, signal_num = $signal_num, dumped_cor +e = $dumped_core\n");

The last line printed from the executable is:

right before out-of-bounds array access

The values of the variables are as follows: exit_value = 0, signal_num = 11, dumped_core = 0 Why in this case is the "Segmentation fault" text not captured by the perl script? The behavior is the same if I replace this line:

system($cmd);
with this:
my $backtick_return = `$cmd`;

and then print $backtick_return at the end of the program. When I first started debugging this issue I was unaware of how to properly check system() return codes. Now that I've added that check as shown above debugging future issues should be easier, but I would still like to know why the "Segmentation fault" text isn't being captured by the system command, as seeing that when I first started debugging this issue would have been helpful.

Replies are listed 'Best First'.
Re: "Segmentation fault" text not captured by perl script
by dave_the_m (Monsignor) on Apr 01, 2020 at 18:39 UTC
    The "segmentation fault" text is emitted by the shell which invoked the bad executable, not the executable itself. In your second case, you're not using a shell and invoking the executable directly from perl, so it's up to your perl program to check the return status and display any suitable error message.

    Note that if you include metacharacters in your call to system, e.g. system("$cmd > foo") then perl will invoke a shell to invoke the executable, and you'll see the error message generated by the shell.

    Dave.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11114914]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-04-19 17:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found