Home > Software engineering >  How to catch SIGABRT in multithread environment?
How to catch SIGABRT in multithread environment?

Time:12-13

I want to create an Units test framwork, but to provide a good reporting I need to catch SIGABRT, SIGSEGV and probably others signals to prevent my process from being killed (And so, to be able to continue the tests processing)...

But I don't know how to do this and so, I need information:

  • SIGABRT is a thread direct signal ?
  • What happens if I only use the main thread to catch the SIGABRT (or SIGSEGV) signal? Could the thread that called abort return from its call (I hope not) ?

If you have any useful documents, links or tutorial, I'm interested. It's for a C code using pthreads.

Thanks for your help

CodePudding user response:

I need to catch SIGABRT, SIGSEGV and probably others signals to prevent my process from being killed

This is an exercise in futility. After SIGABRT or SIGSEGV is raised, you (in general) have no idea about the state of the process -- it may have corrupted heap, stack, global data internal to your test framework, global data internal to the C runtime system, etc. etc. Continuing such process is exceedingly likely to continue crashing at random (correct) places in the code.

The only sane way to handle this in a test framework is to fork and have the parent process handle child error exits, report them and continue running additional tests.

SIGABRT is a thread direct signal ?

There is no such thing as "direct signal". SIGABRT may be sent to the process from outside, or it can be raised inside the process.

What happens if I only use the main thread to catch the SIGABRT (or SIGSEGV) signal?

SIGSEGV and SIGABRT (when not sent from outside) is sent to the thread which caused the invalid memory operation (or raised it).

In addition, there is no way to "only use main thread" -- sigaction is global across all threads (though you can set a thread-specific signal mask).

  • Related