From owner-freebsd-hackers@FreeBSD.ORG Sun Oct 12 00:51:45 2008 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id CE36B1065687 for ; Sun, 12 Oct 2008 00:51:45 +0000 (UTC) (envelope-from neldredge@math.ucsd.edu) Received: from euclid.ucsd.edu (euclid.ucsd.edu [132.239.145.52]) by mx1.freebsd.org (Postfix) with ESMTP id A41F48FC20 for ; Sun, 12 Oct 2008 00:51:45 +0000 (UTC) (envelope-from neldredge@math.ucsd.edu) Received: from zeno.ucsd.edu (zeno.ucsd.edu [132.239.145.22]) by euclid.ucsd.edu (8.11.7p3+Sun/8.11.7) with ESMTP id m9C0pj601087; Sat, 11 Oct 2008 17:51:45 -0700 (PDT) Received: from localhost (neldredg@localhost) by zeno.ucsd.edu (8.11.7p3+Sun/8.11.7) with ESMTP id m9C0pjn11966; Sat, 11 Oct 2008 17:51:45 -0700 (PDT) X-Authentication-Warning: zeno.ucsd.edu: neldredg owned process doing -bs Date: Sat, 11 Oct 2008 17:51:44 -0700 (PDT) From: Nate Eldredge X-X-Sender: neldredg@zeno.ucsd.edu To: Yuri In-Reply-To: <48F147A5.1040107@rawbw.com> Message-ID: References: <48F147A5.1040107@rawbw.com> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed Cc: freebsd-hackers@freebsd.org Subject: Re: Is it possible to recover from SEGV? X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 12 Oct 2008 00:51:45 -0000 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 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@math.ucsd.edu