Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Boost stacktrace async signal safe?

Tags:

c++

linux

boost

Boost has boost::stacktrace::stacktrace() that can be used to get backtraces. This is normally useful in non-signal handling contexts such as exceptions, errors, etc.

But is it safe to be called from a signal handler? i.e., it doesn't do any async-signal-unsafe activities? The example does use it from a signal handler which might mean it is safe. But I can't find anything in its documentation about async-signal-safety.

like image 348
P.P Avatar asked Oct 17 '25 06:10

P.P


1 Answers

https://www.boost.org/doc/libs/1_70_0/doc/html/stacktrace/getting_started.html#stacktrace.getting_started.handle_terminates_aborts_and_seg has:

Warning Writing a signal handler requires high attention! Only a few system calls allowed in signal handlers, so there's no cross platform way to print a stacktrace without a risk of deadlocking. The only way to deal with the problem - dump raw stacktrace into file/socket and parse it on program restart.

Warning Not all the platforms provide means for even getting stacktrace in async signal safe way. No stack trace will be saved on such platforms.

Immediately followed by:

#include <signal.h> // ::signal, ::raise
#include <boost/stacktrace.hpp>

void my_signal_handler(int signum) {
    ::signal(signum, SIG_DFL);
    boost::stacktrace::safe_dump_to("./backtrace.dump");
    ::raise(SIGABRT);
}

It goes on with usage examples, and how to do start up checks

Header synopis: https://www.boost.org/doc/libs/1_70_0/doc/html/stacktrace/reference.html#header.boost.stacktrace.safe_dump_to_hpp

namespace boost {
  namespace stacktrace {
    std::size_t safe_dump_to(void *, std::size_t);
    std::size_t safe_dump_to(std::size_t, void *, std::size_t);
    std::size_t safe_dump_to(const char *);
    std::size_t safe_dump_to(std::size_t, std::size_t, const char *);
    std::size_t safe_dump_to(platform_specific_descriptor);
    std::size_t safe_dump_to(std::size_t, std::size_t, 
                             platform_specific_descriptor);
  }
}
like image 144
sehe Avatar answered Oct 19 '25 22:10

sehe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!