Re: Converting SIGSEGV signals to C++ exception in gcc/g++

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 08/22/2011 09:33 AM, Andrew Haley wrote:
> On 08/21/2011 11:02 PM, sudhakar govindavajhala wrote:
> 
>> Is there a flag in compiling or using gcc/g++ so that a signal like
>> SIGSEGV in my C++ program will result in an exception being thrown?
>> If not, why not?  I mean, is this is not a reasonable expectation in
>> OOPS?
>>
>> Is there anything I need to be careful in throwing an exception in my
>> signal handlers that convert SIGSEGV to an exception. I will ensure
>> that no malloc style calls are called.
> 
> There is gcc support for this on most of the GNU/Linux platforms, but it's
> not so well supported elsewhere.  The answer to your question depends on
> your platform.

Like this:

zebedee:~ $ g++ segv.c -g -lpthread -Wall -fnon-call-exceptions -O2
zebedee:~ $ ./a.out
Hello!

Andrew.
#define _POSIX_SOURCE

#include <signal.h>
#include <stdio.h>

int *a;

class FoobarException
{
  int thingy;
};

void signal_handler(int signum, siginfo_t *info, void *)
{
  FoobarException *f = new FoobarException;
  throw f;
}

int main()
{
  struct sigaction act;

  act.sa_sigaction = signal_handler;
  sigemptyset (&act.sa_mask);
  act.sa_flags = SA_SIGINFO;
  sigaction (11, &act, NULL);

  try
    {
      printf("%d\n", *a);
    }
  catch (FoobarException *f)
    {
      printf("Hello!\n");
    }
}

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux