g++ and signal handlers that use sigcontext
David L
idht4n@hotmail.com
Tue Feb 17 19:11:00 GMT 2004
I found some example code regarding how to use backtrace from signal
handlers at: http://www.linuxjournal.com/article.php?sid=6391 . That
example code compiled with warnings but no errors using old versions of g++
(eg: egcs-2.91), but now has an error:
foo.cc: In function `void bt_sighandler(int, sigcontext)':
foo.cc:26: `exit' undeclared (first use this function)
foo.cc:26: (Each undeclared identifier is reported only once for each
function
it appears in.)
foo.cc: In function `int main()':
foo.cc:56: invalid conversion from `void*' to `void (*)(int)'
What is the proper way to set up a signal handler with a 2nd argument
(sigcontext )?
Thanks... David
PS - Here is the example code:
#include <stdio.h>
#include <signal.h>
#include <execinfo.h>
void bt_sighandler(int sig, struct sigcontext ctx) {
void *trace[16];
char **messages = (char **)NULL;
int i, trace_size = 0;
if (sig == SIGSEGV)
printf("Got signal %d, faulty address is %p, "
"from %p\n", sig, ctx.cr2, ctx.eip);
else
printf("Got signal %d\n", sig);
trace_size = backtrace(trace, 16);
/* overwrite sigaction with caller's address */
trace[1] = (void *)ctx.eip;
messages = backtrace_symbols(trace, trace_size);
/* skip first stack frame (points here) */
printf("[bt] Execution path:\n");
for (i=1; i<trace_size; ++i)
printf("[bt] %s\n", messages[i]);
exit(0);
}
int func_a(int a, char b) {
char *p = (char *)0xdeadbeef;
a = a + b;
*p = 10; /* CRASH here!! */
return 2*a;
}
int func_b() {
int res, a = 5;
res = 5 + func_a(a, 't');
return res;
}
int main() {
/* Install our signal handler */
struct sigaction sa;
sa.sa_handler = (void *)bt_sighandler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
sigaction(SIGSEGV, &sa, NULL);
sigaction(SIGUSR1, &sa, NULL);
/* ... add any other signal here */
/* Do something */
printf("%d\n", func_b());
}
_________________________________________________________________
Say Âgood-bye to spam, viruses and pop-ups with MSN Premium -- free trial
offer! http://click.atdmt.com/AVE/go/onm00200359ave/direct/01/
More information about the Gcc-help
mailing list