简体   繁体   English

公共C库,用于为SIGSEGV安装标准信号处理程序

[英]common C library to install standard signal handler for SIGSEGV

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? 这里是否有任何具有类似install_default_signal_handlers()类的函数的C库,该函数将为SIGSEGV和相关信号安装一些默认信号处理程序,该信号处理程序将在其中打印回溯? 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; 我认为向后cpp可以使用backward::SignalHandling sh;为C ++ backward::SignalHandling sh; .

There is also Google Breakpad but this might be overkill. 也有Google Breakpad,但这可能会过大。

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. libSegFault似乎可以做到这一点,但它似乎仅在某些Unix(GlibC的一部分,并且有FreeBSD实现)上可用,而在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. 这里有一些相关的讨论但这主要讨论打印回溯的代码,但是我搜索了一个为我提供信号处理程序的库。

There's no default handler installed for SIGSEGV on any of the *nix systems. 在任何* nix系统上都没有为SIGSEGV安装默认处理程序。 But you can 1) install one yourself, 2) print the backtrace and 3) then exit. 但是您可以1)自己安装一个,2)打印回溯记录,3)然后退出。 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. SIGSEGV就是其中之一。

The tricky part is (2) - printing the backtrace. 棘手的部分是(2)-打印回溯。 Glibc provides 3 interfaces : backtrace , backtrace_symbols and backtrace_symbols_fd . Glibc提供了3个接口backtracebacktrace_symbolsbacktrace_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. 如果您使用Glibc,则可能需要做更多的工作。 For example, you can use _Unwind_Backtrace which is available on systems using gcc (not necessarily Glibc). 例如,您可以使用_Unwind_Backtrace ,它在使用gcc(不一定是Glibc)的系统上可用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM