3 ms·
Another place where SIGSEGV handlers are common: showing backtraces and "how to report a bug" messages. For example here's where it's done in the Ruby VM: http
by jez 6y ago
Another place where SIGSEGV handlers are common: showing backtraces and "how to report a bug" messages. For example here's where it's done in the Ruby VM:
https://github.com/ruby/ruby/blob/5445e0435260b449decf2ac16f9d09bae3cafe72/signal.c#L946 https://github.com/ruby/ruby/blob/5445e0435260b449decf2ac16f...
They have a bug report handler that shows a bunch of helpful information:
- the Ruby-level backtrace
- the stack of VM control frames (an internal data structure)
- the C-level backtrace (which Ruby implementation functions were running)
- which files had been required and loaded into memory
- what segments of memory are mapped and what aren't
Another thing: it's designed to work even when the program ran out of memory!
The sigaltstack(2) system call lets you preallocate a buffer that the kernel knows how to set up before it runs your SIGSEGV handler, so you can have just enough stack memory to compute and print all this debug info before the program ultimately crashes. Here's that code, again in the Ruby VM:
https://github.com/ruby/ruby/blob/5445e0435260b449decf2ac16f9d09bae3cafe72/signal.c#L548-L561 https://github.com/ruby/ruby/blob/5445e0435260b449decf2ac16f...