gcc4: throwing exception from signal handler

2010-07-08 Thread Don Ward
I would like to be able to catch certain signals (SIGSEGV and SIGSYS) and 
throw a C++ exception (to be caught in a try/catch construct).  As a simple 
example:


#include 
#include 
#include 
#include 
void throw_signal( int signum )
{
 // fprintf(stderr,"throw_signal: before throw; 
signum=%d\n",signum);fflush(stderr);

 throw std::runtime_error ("throw_signal");
}
int main() {
 struct sigaction new_action, d_old_action;
 memset (&new_action, 0, sizeof (new_action));
 new_action.sa_handler = throw_signal;
 sigemptyset (&new_action.sa_mask);
 new_action.sa_flags = 0;
 if (sigaction (SIGSEGV, &new_action, &d_old_action) < 0){
   perror ("sigaction (install new)");
   throw std::runtime_error ("sigaction");
 }
 try {
   int j = *((int *)NULL);
 }
 catch (...){
   printf("caught exception\n");
 }
}

g++ 3.3.4 gives the result I expect:

$ sh /usr/bin/set-gcc-default-3.sh
$ gcc --version
gcc (GCC) 3.4.4 (cygming special, gdc 0.12, using dmd 0.125)
Copyright (C) 2004 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ g++ -fnon-call-exceptions throw_segv.cc
$ ./a.exe
caught exception

but g++ 4.3.4 aborts after the exception is thrown and before it is caught:

$ sh /usr/bin/set-gcc-default-4.sh
$ gcc --version
gcc (GCC) 4.3.4 20090804 (release) 1
Copyright (C) 2008 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ g++ -fnon-call-exceptions throw_segv.cc
$ ./a.exe
terminate called after throwing an instance of 'std::runtime_error'
 what():  throw_signal
Aborted (core dumped)

Am I misunderstanding how this should work or doing something wrong?  Or is 
this a problem with Cygwin or gcc?


Thanks,

-- Don W.


cygcheck.out
Description: Binary data


throw_segv.cc
Description: Binary data
--
Problem reports:   http://cygwin.com/problems.html
FAQ:   http://cygwin.com/faq/
Documentation: http://cygwin.com/docs.html
Unsubscribe info:  http://cygwin.com/ml/#unsubscribe-simple

Re: gcc4: throwing exception from signal handler

2010-07-08 Thread Václav Haisman

On Thu, 8 Jul 2010 09:01:41 -0400, "Don Ward" wrote:
> I would like to be able to catch certain signals (SIGSEGV and SIGSYS)
and 
> throw a C++ exception (to be caught in a try/catch construct).  As a
> simple 
> example:
>[...] 
> Am I misunderstanding how this should work or doing something wrong?  Or
> is 
> this a problem with Cygwin or gcc?
I do not think that handling SIGSEGV with an exception is a good idea.
Unless you get SIGSEGV as a result of some well thought through memory
management games, at the point you get SIGSEGV your state is probably so
foobar'd that trying to execute more code can only make it worse. It is
pretty much unrecoverable condition. From diagnostic POV, getting a core
dump with the error state seems better than anything you could do with the
exception.

I am not sure but I do not think that throwing exceptions from signal
handlers is generally supported. Somebody will certainly correct me if I am
wrong.

--
VH


--
Problem reports:   http://cygwin.com/problems.html
FAQ:   http://cygwin.com/faq/
Documentation: http://cygwin.com/docs.html
Unsubscribe info:  http://cygwin.com/ml/#unsubscribe-simple



Re: gcc4: throwing exception from signal handler

2010-07-08 Thread Don Ward

Václav Haismam wrote:


On Thu, 8 Jul 2010 09:01:41 -0400, "Don Ward" wrote:

I would like to be able to catch certain signals (SIGSEGV and SIGSYS)

and

throw a C++ exception (to be caught in a try/catch construct).  As a
simple
example:
[...]
Am I misunderstanding how this should work or doing something wrong?  Or
is
this a problem with Cygwin or gcc?

I do not think that handling SIGSEGV with an exception is a good idea.


I agree, in general.


Unless you get SIGSEGV as a result of some well thought through memory
management games


But that is what I am doing.  I also want to catch SIGSYS to determine 
whether shmat() is available when we don't know in advance whether cygserver 
is running.  In either case, if the signal is generated I want to catch it 
and try something else.  In these contexts I want to treat  SIGSEGV and 
SIGSYS as non-fatal error returns.


. . .

I am not sure but I do not think that throwing exceptions from signal
handlers is generally supported.


It appears that is what the -fnon-call-exceptions is for.  From the 
gcc-4.3.5 manual:


"-fnon-call-exceptions:  Generate code that allows trapping instructions to 
throw exceptions. Note that this requires platform-specific runtime support 
that does not exist everywhere."


I guess one question is whether the runtime support exists in Cygwin 1.7 
with gcc 4.3.4.


-- Don W.


--
Problem reports:   http://cygwin.com/problems.html
FAQ:   http://cygwin.com/faq/
Documentation: http://cygwin.com/docs.html
Unsubscribe info:  http://cygwin.com/ml/#unsubscribe-simple



Re: gcc4: throwing exception from signal handler

2010-07-08 Thread Dave Korn
On 08/07/2010 14:01, Don Ward wrote:
> I would like to be able to catch certain signals (SIGSEGV and SIGSYS)
> and throw a C++ exception (to be caught in a try/catch construct).  As a
> simple example:

> Am I misunderstanding how this should work or doing something wrong?  Or
> is this a problem with Cygwin or gcc?

  Throwing exceptions from a signal handler is a can of worms that requires
support from both the compiler and the C runtime.  I was working on adding
this support for the distro package of gcc-4.5 before I had to go AWOL a
couple of weeks ago; it requires building the Cygwin DLL with EH tables, and
adding support in the last-chance stack unwinder in libgcc that is able to
unwind past our sigfe/sigbe stuff.

  I'll be back in the Cygwin/GCC world starting next week.

cheers,
  DaveK


--
Problem reports:   http://cygwin.com/problems.html
FAQ:   http://cygwin.com/faq/
Documentation: http://cygwin.com/docs.html
Unsubscribe info:  http://cygwin.com/ml/#unsubscribe-simple



Re: gcc4: throwing exception from signal handler

2010-07-08 Thread Don Ward

Dave Korn wrote:


On 08/07/2010 14:01, Don Ward wrote:

I would like to be able to catch certain signals (SIGSEGV and SIGSYS)
and throw a C++ exception (to be caught in a try/catch construct).  As a
simple example:



Am I misunderstanding how this should work or doing something wrong?  Or
is this a problem with Cygwin or gcc?


 Throwing exceptions from a signal handler is a can of worms that requires
support from both the compiler and the C runtime.


Understood!


 I was working on adding
this support for the distro package of gcc-4.5 before I had to go AWOL a
couple of weeks ago; it requires building the Cygwin DLL with EH tables, 
and

adding support in the last-chance stack unwinder in libgcc that is able to
unwind past our sigfe/sigbe stuff.


I'm glad someone is (or will be) working on it.  In the meanwhile, I can 
make do with gcc 3.4.4.


Thanks,

-- Don W.


--
Problem reports:   http://cygwin.com/problems.html
FAQ:   http://cygwin.com/faq/
Documentation: http://cygwin.com/docs.html
Unsubscribe info:  http://cygwin.com/ml/#unsubscribe-simple



Re: gcc4: throwing exception from signal handler

2010-07-08 Thread Dave Korn
On 08/07/2010 18:02, Don Ward wrote:

> I'm glad someone is (or will be) working on it.  In the meanwhile, I can
> make do with gcc 3.4.4.

  Yep, 3.4.4 uses old-style SJLJ exception handling, which can jump past
anything without needing to understand it; you'll be good there, although it's
not as fast as dwarf2 EH, but it ought to be reliable until we've got the
newer one working.

cheers,
  DaveK


--
Problem reports:   http://cygwin.com/problems.html
FAQ:   http://cygwin.com/faq/
Documentation: http://cygwin.com/docs.html
Unsubscribe info:  http://cygwin.com/ml/#unsubscribe-simple



Re: gcc4: throwing exception from signal handler

2010-07-08 Thread Charles Wilson

On 7/8/2010 11:48 AM, Dave Korn wrote:


   I'll be back in the Cygwin/GCC world starting next week.


YAY! We missed you, Dave. Welcome back!

--
Chuck

--
Problem reports:   http://cygwin.com/problems.html
FAQ:   http://cygwin.com/faq/
Documentation: http://cygwin.com/docs.html
Unsubscribe info:  http://cygwin.com/ml/#unsubscribe-simple



Re: gcc4: throwing exception from signal handler

2010-07-09 Thread Christopher Faylor
On Thu, Jul 08, 2010 at 01:02:28PM -0400, Don Ward wrote:
>Dave Korn wrote:
>
>> On 08/07/2010 14:01, Don Ward wrote:
>>> I would like to be able to catch certain signals (SIGSEGV and SIGSYS)
>>> and throw a C++ exception (to be caught in a try/catch construct).  As a
>>> simple example:
>>
>>> Am I misunderstanding how this should work or doing something wrong?  Or
>>> is this a problem with Cygwin or gcc?
>>
>>  Throwing exceptions from a signal handler is a can of worms that requires
>> support from both the compiler and the C runtime.
>
>Understood!
>
>>  I was working on adding
>> this support for the distro package of gcc-4.5 before I had to go AWOL a
>> couple of weeks ago; it requires building the Cygwin DLL with EH tables, 
>> and
>> adding support in the last-chance stack unwinder in libgcc that is able to
>> unwind past our sigfe/sigbe stuff.
>
>I'm glad someone is (or will be) working on it.  In the meanwhile, I can 
>make do with gcc 3.4.4.

Someone may be looking into this but if this is adding complication to the 
signal
handler it is going to take some real convincing that it should go into the DLL.

Dave, this is something that you should be talking about early rather than 
springing
it as a patch in cygwin-patches.

cgf

--
Problem reports:   http://cygwin.com/problems.html
FAQ:   http://cygwin.com/faq/
Documentation: http://cygwin.com/docs.html
Unsubscribe info:  http://cygwin.com/ml/#unsubscribe-simple



Re: gcc4: throwing exception from signal handler

2010-07-15 Thread Dave Korn
On 09/07/2010 17:51, Christopher Faylor wrote:

> Someone may be looking into this but if this is adding complication to the
> signal handler it is going to take some real convincing that it should go
> into the DLL.

  :) Fortunately, it doesn't have to do that.  All the magic takes place in
libgcc's last-chance unwind fallback hook(*).  The only impact on the DLL, in
the design I've been experimenting with, is that we need to build it with EH
unwind tables, add a cygwin_internal method to return a pointer to the EH
data, and export a couple of entrypoints that libgcc needs to be able to look
up.  There are no changes in any of the signal/exception code paths, or at any
rate I didn't yet find any need to change anything (I didn't finish getting it
working before I put it aside though, so it's conceivable-but-low-probability
that something might turn up).

> Dave, this is something that you should be talking about early rather than
> springing it as a patch in cygwin-patches.

  Sure, I was going to get a working PoC first because I wanted to convince
myself it was possible, but no reason not to discuss it right now.  Luckily I
think there's very little to discuss.  (I'll report what the EH data does to
the DLL size when I've got the code up and running again, but since everyone's
been waiting long enough, I guess the first thing I should do is get out a
more vanilla-flavoured 4.5.0 release.)

cheers,
  DaveK
-- 
(*) - MD_FALLBACK_FRAME_STATE_FOR

--
Problem reports:   http://cygwin.com/problems.html
FAQ:   http://cygwin.com/faq/
Documentation: http://cygwin.com/docs.html
Unsubscribe info:  http://cygwin.com/ml/#unsubscribe-simple



Re: gcc4: throwing exception from signal handler

2010-07-15 Thread Charles Wilson

On 7/15/2010 11:42 AM, Dave Korn wrote:

 I guess the first thing I should do is get out a
more vanilla-flavoured 4.5.0 release.)


Err... 4.5.1?  Does the ABI breakage between 4.5.0 and 4.5.1/4.6.0 
mentioned wrt x86_64-mingw affect i386, or cygwin?


--
Chuck

--
Problem reports:   http://cygwin.com/problems.html
FAQ:   http://cygwin.com/faq/
Documentation: http://cygwin.com/docs.html
Unsubscribe info:  http://cygwin.com/ml/#unsubscribe-simple