Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux: Snoop a signal without trashing the registers for the subsequent core dump?

When I get coredump-causing signal, I want to run my own handler to copy the siginfo_t and ucontext_t structures to global variables, so that they may be accessed in the core dump. Currently at the end of my handler I reassign the default handler and call raise(thesig). The problem with doing that is that the core dump "info registers" shows the state of the registers in my handler, and not at the time of the original signal. I realise that since I've saved ucontext_t, I can look in that for the original register values, but that knowledge is likely to get lost/forgotten when core dumps are passed around the team.

So my question is: is there a way to reraise a signal, and ensure the core dump file holds the register state of the original signal? I thought maybe I could use some inline asm to manually restore all the regs at the end of the handler, then return to the instruction that caused the signal instead of calling raise(), but I'm not sure if we can guarantee that re-attempting the instruction will cause the same signalling behaviour as the first attempt.

like image 726
gimmeamilk Avatar asked Oct 23 '22 23:10

gimmeamilk


1 Answers

copy the siginfo_t and ucontext_t structures to global variables, so that they may be accessed in the core dump

If you re-raise the signal in your handler, then there is no need to copy anything -- the values will be on stack and accessible in the core dump.

"info registers" shows the state of the registers in my handler, and not at the time of the original signal.

Just do up 5 (or however many levels you need to step up to get to the crash point) and info reg again.

is there a way to reraise a signal, and ensure the core dump file holds the register state of the original signal?

Yes: set signal disposition to SIG_DFL using signal(signum, SIG_DFL); and return from your handler. The instruction that caused SIGSEGV will be restarted, and will now cause immediate core dump.

like image 184
Employed Russian Avatar answered Oct 27 '22 11:10

Employed Russian