0

Is here any C library with a function like install_default_signal_handlers() which will install some default signal handler for SIGSEGV and related signals, where the signal handler will print a backtrace? The signal handler, and also the installation of the signal handler should be provided by the library.

I think backward-cpp does it for C++ with backward::SignalHandling sh;.

There is also Google Breakpad but this might be overkill.

There is libSegFault which seems to do that but it only seems to be available on some Unixes (part of GlibC, and there is a FreeBSD implementation), not on MacOSX.

Some related discussion is here but this discusses mostly the code to print the backtrace but I search for a library which provides the signal handler for me.

Community
  • 1
  • 1
Albert
  • 65,406
  • 61
  • 242
  • 386

1 Answers1

0

There's no default handler installed for SIGSEGV on any of the *nix systems. But you can 1) install one yourself, 2) print the backtrace and 3) then exit. The "exit" part is required becuase, it's undefined to return from their handler (if one is installed) for some signals; SIGSEGV is one of them.

The tricky part is (2) - printing the backtrace. Glibc provides 3 interfaces: backtrace, backtrace_symbols and backtrace_symbols_fd. You can use whichever is suitable and print the stack trace.

If you are not using Glibc then you may have to do more work. For example, you can use _Unwind_Backtrace which is available on systems using gcc (not necessarily Glibc).

P.P
  • 117,907
  • 20
  • 175
  • 238
  • Yes I know that I can done that myself. But I'm searching for a library which does it for me. Not the printing, I know that there is `backtrace`, but even the installation and the signal handler itself. – Albert Jan 18 '17 at 10:17
  • There's no standard library function; anything you'd use would involve non-standard functions/extensions. Even the backward-cpp you linked to uses `_Unwind_Backtrace` which is probably your best bet if whatever libc you are using doesn't already provide something like Glibc's `backtrace`. – P.P Jan 18 '17 at 10:20
  • That's why I'm searching for some library. So you say no-one thought that it might be useful to make such a library? But for C++, there is (backward-cpp). I don't care how the library implements it. It's fine if the support on some platforms is limited. Just a simple `backtrace()` lib would be fine for me. – Albert Jan 18 '17 at 10:30