I am interested in a signal handler which can identify the address of the instruction which caused the problem.
I know about siginfo_t
and __builtin_return_address
and neither seem to work:
#include <iostream>
#include <signal.h>
void handler (int, siginfo_t *, void *);
int main ()
{
begin:
std :: cerr << &&begin << " ~ " << &&before << " ~ " << &&after << "
";
struct sigaction s;
s .sa_flags = SA_SIGINFO;
sigemptyset (& s .sa_mask);
s .sa_sigaction = handler;
sigaction (SIGSEGV, &s, NULL);
int * i = NULL;
before:
*i = 0;
after:
std :: cout << "End.
";
}
void handler (int, siginfo_t *si, void *)
{
std :: cerr << "si:" << si -> si_addr << "
";
std :: cerr << "At: " << __builtin_return_address (0) << "
";
std :: cerr << "At: " << __builtin_return_address (1) << "
";
std :: cerr << "At: " << __builtin_return_address (2) << "
";
std :: cerr << "At: " << __builtin_return_address (3) << "
";
std :: cerr << "At: " << __builtin_return_address (4) << "
";
std :: cerr << "At: " << __builtin_return_address (5) << "
";
}
This outputs something like:
0x10978 ~ 0x10a4c ~ 0x10a54
si:0
At: 0xfb945364
At: 0xfb939e64
At: 0x10a40
At: 0x10740
At: 0
At: Segmentation Fault
So siginfo_t
is NULL and __builtin_return_address
is yielding values somewhere in between the named labels.
I was expecting both of these to return the value of &&before
. Am I using these functions correctly?
Tested on Linux 2.6.9-89.0.9.Elsmp and SunOS.
See Question&Answers more detail:os