Wrong child process exit status if core dump / seg fault

Hi all,

when a child process exits with core dump or seg fault (Windows/Linux),
I cannot obtain the correct exit code.

I tried the following

···

----------------------
   system("test.exe");
   # or
   `test.exe`
   # or
   sp = spawn("test.exe");
   Process.wait(sp)

   puts $?
----------------------

The exit code is always 0. In command shell, I can test it with e.g.
----------------------
   test.exe && echo "Success"
----------------------

"Success" is not printed due to exit code of test.exe.

test.exe could be a simple c++ source file like this:
----------------------
   class A {};
   int main() {
     throw A();
     return 1;
   }
----------------------

--> How can I find out the exit code in ruby for child processes which
do not terminate gracefully?

Thank you,
alll

--
Posted via http://www.ruby-forum.com/.

Update 1:
I am using ruby 1.9.3p194 (2012-04-20) [i386-mingw32].

Update 2:
With
    result = system('...')
I can see if the exit code is zero or not EVEN if the process terminates
with seg fault / core dump.

···

--
Posted via http://www.ruby-forum.com/.

Thank you for your answer. Right, I have Windows 7 x64.

By the way: I have installed the latest ruby installation package. The
same problem happens with "ruby 2.0.0p195 (2013-05-14) [x64-mingw32]"...

···

--
Posted via http://www.ruby-forum.com/.

Same problem (at least with seg fault) on ubuntu 12.04 LTS 64bit....

···

--
Posted via http://www.ruby-forum.com/.

I think I almost understand the problem. :slight_smile:

$?.to_i shows me the raw 16 bit status. In theory. In my case the status
has 24 bit!

To be more precise, in my case the value is 0x860000.

--> The low byte are states like "signaled", "exited", etc... which are
all 0.
--> The high byte is the exit code. I think ruby reads ONLY the second
byte, which is 0. But my exit code is 0x8600 (core dump).

Yeah, to get the real exit code, I can use

   $?.to_i >> 8

Is this a bug?

Thank you,
alll

···

--
Posted via http://www.ruby-forum.com/.

Hi Robert,

yes, I have tried all the methods. They return false or 0.

I assume that ($?.to_i >> 8) must be equal to ($?.exitstatus), but it
doesn't.

As I can see in your tests, your exit codes are <= 255. In Windows exit
codes are 32 bit, not only 8 bit (I am not sure about other platforms).
I think this is the problem. In most cases the exit codes are < 255, so
it fails only in rare cases...

Thank you for your help!
alll

···

--
Posted via http://www.ruby-forum.com/.

Additional note: The docu says about "to_i": Returns the bits in stat as
a Fixnum. Poking around in these bits is platform dependent.

--> I hope my workaround will work on all platforms I need...

···

--
Posted via http://www.ruby-forum.com/.

Hmm, works for me in case of core:

$ gcc -Wall -ansi -o core x.c
$ ./core
sizeof( a ) = 32
sizeof( pa ) = 4
sizeof( ppa ) = 4
sizeof( b ) = 11
sizeof( c ) = 32
sizeof( d ) = 24
Segmentation fault
$ echo $?
139
$ irb
Ruby version 1.9.3
irb(main):001:0> system("./core")
sizeof( a ) = 32
sizeof( pa ) = 4
sizeof( ppa ) = 4
sizeof( b ) = 11
sizeof( c ) = 32
sizeof( d ) = 24
=> false
irb(main):002:0> $?
=> #<Process::Status: pid 3824 SIGSEGV (signal 11)>
irb(main):003:0> puts $?
pid 3824 SIGSEGV (signal 11)
=> nil
irb(main):004:0> $?.exitstatus
=> nil
irb(main):006:0> RUBY_PATCHLEVEL
=> 392
$ ruby --version
ruby 1.9.3p392 (2013-02-22) [i386-cygwin]

And also for non core:

irb(main):001:0> system "test.exe"
=> false
irb(main):002:0> $?
=> #<Process::Status: pid 7996 exit 1>
irb(main):003:0> $?.exitstatus
=> 1

This is most likely an issue of your Windows version of Ruby. On Linux it
works the same as shown above.

Cheers

robert

···

On Mon, May 27, 2013 at 2:21 PM, Alexander Schaal <lists@ruby-forum.com>wrote:

Update 1:
I am using ruby 1.9.3p194 (2012-04-20) [i386-mingw32].

Update 2:
With
    result = system('...')
I can see if the exit code is zero or not EVEN if the process terminates
with seg fault / core dump.

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

I think I almost understand the problem. :slight_smile:

$?.to_i shows me the raw 16 bit status. In theory. In my case the status
has 24 bit!

To be more precise, in my case the value is 0x860000.

--> The low byte are states like "signaled", "exited", etc... which are
all 0.
--> The high byte is the exit code. I think ruby reads ONLY the second
byte, which is 0. But my exit code is 0x8600 (core dump).

Yeah, to get the real exit code, I can use

   $?.to_i >> 8

I would not normally use #to_i here since it's too unspecific. Did you
look at the documentation of Process::Status? Did you use methods
#exitstatus and #termsig?

Is this a bug?

Good question. Could well be that it's a bug in the platform build you
have. As I said, with other platforms it works.

Kind regards

robert

···

On Mon, May 27, 2013 at 4:58 PM, Alexander Schaal <lists@ruby-forum.com>wrote:

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/