Is it possible to recover from SEGV?

Nate Eldredge neldredge at math.ucsd.edu
Sun Oct 12 00:51:45 UTC 2008


On Sat, 11 Oct 2008, Yuri wrote:

> Let's say I have signal(3) handler set.
> And I know exactly what instruction caused SEGV and why.
>
> Is there a way to access from signal handler CPU registers as they
> were before signal, modify some of them, clear the signal and
> continue from the instruction that caused SEGV initially?

Absolutely.  Declare your signal handler as

void handler(int sig, int code, struct sigcontext *scp);

You will need to cast the pointer passed to signal(3).  struct sigcontext 
is defined in <machine/sysarch.h> I believe.   struct sigcontext contains 
the CPU registers as they were when the faulting instruction began to 
execute.  You can modify them and then return from the signal handler. 
The program will resume the faulting instruction with the new registers. 
You can also alter the copy of the instruction pointer in the struct 
sigcontext if you want it to resume somewhere else.

There is also a libsigsegv which looks like it wraps some of this process 
in a less machine-specific way.

Out of curiosity, what are you looking to achieve with this?  And what 
architecture are you on?

-- 

Nate Eldredge
neldredge at math.ucsd.edu


More information about the freebsd-hackers mailing list