The idea of building a crash logger with a signal handler is fundamentally flawed. From the "NOTE" section of the sigaction man page:
The following functions are either reentrant or not interruptible by
signals and are async-signal safe. Therefore applications may invoke
them, without restriction, from signal-catching functions:
[… async-signal safe function list omitted …]
All functions not in the above lists are considered to be unsafe with
respect to signals. That is to say, the behaviour of such functions
when called from a signal handler is undefined. In general though,
signal handlers should do little more than set a flag; most other
actions are not safe.
None of backtrace()
, backtrace_symbols()
, free()
, or exit()
are safe to call within a signal handler. Almost certainly, stuff within your Logger
or operator<<
is also unsafe.
As to why the signal-provoking function is not in the backtrace, it's evidently cause by how the signal handler context is set up by the kernel. A backtrace is obtained by examining stack frames as constructed by call statements and function prologs. A signal handler is not invoked as a normal function call, so there's no particular reason to expect that the chain of stack frames would be set up properly.
In fact, signal handlers can be made to run on a completely different stack than normal code. See sigaltstack()
.