Hrvoje Niksic wrote:
> > (In fact, on *real* Unixes, we could simply use sigaction()
to trap
> > SIGSEGV and have the handler check whether the SEGV was caused by
> > modifying a pure cons, and signal an error in that case. Of course,
> > this would not work on Linux.)
>
> Why?
Because I am unable to make this trivial program (or a variation
thereof) to compile and run on Linux:
[snip]
Try the following instead.
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <sigcontext.h>
void
segv_handler(int sig, struct sigcontext ctx)
{
printf ("Fault address: %p\n", ctx.cr2);
exit (0);
}
int
main (void)
{
struct sigaction act;
sigaction (SIGSEGV, NULL, &act);
act.sa_handler = segv_handler;
sigemptyset (&act.sa_mask);
act.sa_flags = 0;
sigaction (SIGSEGV, &act, NULL);
*(char *)0xdeadbeef = 'x';
return 0; /* unreached */
}
Clearly it isn't portable (the sigcontext structure is
processor-specific), but then I'm not sure that there is any portable
method. Stevens[1] indicates that SA_SIGINFO and struct siginfo are
specific to SVR4, and that BSD 4.3+ signal handlers have the
prototype:
handler(int signo, int code, struct sigcontext *scp)
--
Glynn Clements <glynn(a)sensei.co.uk>
[1]
Advanced Programming in the Unix Environment
W Richard Stevens
Addison Wesley