1

I am able to handle segmentation fault once. But when it occurs again it causes segmentation fault. Is it possible to handle sigsegv signal multiple times?

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

int *a;

class FoobarException
{
  int thingy;
};

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

void call(int * c)
{
  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");
    }
}

int main()
{ 
  int *b;
  call(b);
  printf("I am here.\n");
  call(a);
}

First call(b) handles seg fault but second call(a) raises a seg fault.

Niall
  • 30,036
  • 10
  • 99
  • 142
  • 1
    You need to make sure you can recover from the segmentation fault. Check [this](http://stackoverflow.com/questions/8401689/best-practices-for-recovering-from-a-segmentation-fault) question for more details. In general, Why not debug to a point where you **don't** have segmentation faults at all? – ArnonZ Sep 17 '14 at 09:51
  • 1
    I'm pretty sure that `signal_handler` is going to be called from c code, so you can't catch the exception that you throw. Catching doesn't work in here: http://ideone.com/4GiOiq . Also, you would leak the `FoobarException` even if you do catch it. – eerorika Sep 17 '14 at 09:55
  • You are probably intrested in gcc -fnon-call-exceptions feature, otherwise the thrown exception will just terminate the signal handler and continue crashing – PlasmaHH Sep 17 '14 at 10:46
  • I thought `try/catch` was designated to catch exception that are thrown in other sections of your program **as part of your SW design**, not to handle HW and SW traps. – barak manos Sep 17 '14 at 11:10
  • Is there a reason you are trying to handle multiple seg-faults in the first place? Only reason I can think of to catch it once is to generate custom report and/or handle it 'gracefully' before exiting. AFAIK, when a seg-fault occurs, your program has already entered an 'unsafe' state (think mortally wounded animal) – Baldrickk Sep 17 '14 at 11:11

2 Answers2

2

Once you have Segmentation fault, and you handle the exception, but you are not recovering the cause. Bet would be to avoid seg fault. In your example program, you have pointers, a, b but did not allocate, so, a, b contains garbage (unallocated), accessing *a is therefore risky, causing the seg fault.

Instead, allocate sufficient space for a, b, for example,

a = (int *) malloc(sizeof (int));

Similarly, for b.

Don't forget to deallocate when done, like,

free(a);
Niall
  • 30,036
  • 10
  • 99
  • 142
Dr. Debasish Jana
  • 6,980
  • 4
  • 30
  • 69
1

You can't use a C++ try/catch block to handle a signal because POSIX signals are not the same as C++ exceptions. For example signals are asynchronous, fired by the kernel, whereas C++ exceptions are synchronous:

You need to take special care in writing handler functions because they can be called asynchronously. That is, a handler might be called at any point in the program, unpredictably. If two signals arrive during a very short interval, one handler can run within another.

You can find here more information on how to write proper signal handlers. Take a look at volatile sig_atomic_t

4pie0
  • 29,204
  • 9
  • 82
  • 118