g++ and signal handlers that use sigcontext
Claudio Bley
bley@CS.uni-magdeburg.de
Tue Feb 17 22:23:00 GMT 2004
On Tue, Feb 17, 2004 at 11:11:20AM -0800, David L wrote:
> 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.)
It's illegal to use a function in C++ prior to declaring it. `exit' is
declared in "stdlib.h".
> 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 )?
See `man sigaction'. You want to assign to sa_sigaction instead of
sa_handler as shown in listing 3 of the article you just referenced above.
But leave out the stupid old style cast and just make sure bt_sighandler
has the expected signature.
> #include <stdio.h>
> #include <signal.h>
> #include <execinfo.h>
>
> void bt_sighandler(int sig, struct sigcontext ctx) {
[...]
> exit(0);
> }
[...]
> 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 */
>
Cheers.
--
Claudio Bley ASCII ribbon campaign (")
Debian GNU/Linux user - against HTML email X
http://www.cs.uni-magdeburg.de/~bley/ & vCards / \
More information about the Gcc-help
mailing list