From owner-svn-src-all@FreeBSD.ORG Fri Dec 27 22:30:36 2013 Return-Path: Delivered-To: svn-src-all@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id 9CF0C2EB; Fri, 27 Dec 2013 22:30:36 +0000 (UTC) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:1900:2254:2068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.freebsd.org (Postfix) with ESMTPS id 88FC3160A; Fri, 27 Dec 2013 22:30:36 +0000 (UTC) Received: from svn.freebsd.org ([127.0.1.70]) by svn.freebsd.org (8.14.7/8.14.7) with ESMTP id rBRMUaC0053333; Fri, 27 Dec 2013 22:30:36 GMT (envelope-from markj@svn.freebsd.org) Received: (from markj@localhost) by svn.freebsd.org (8.14.7/8.14.7/Submit) id rBRMUaDi053302; Fri, 27 Dec 2013 22:30:36 GMT (envelope-from markj@svn.freebsd.org) Message-Id: <201312272230.rBRMUaDi053302@svn.freebsd.org> From: Mark Johnston Date: Fri, 27 Dec 2013 22:30:36 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-10@freebsd.org Subject: svn commit: r259969 - stable/10/lib/libproc X-SVN-Group: stable-10 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.17 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 27 Dec 2013 22:30:36 -0000 Author: markj Date: Fri Dec 27 22:30:36 2013 New Revision: 259969 URL: http://svnweb.freebsd.org/changeset/base/259969 Log: MFC r257670: Modify the libproc breakpoint add/remove functions to stop the target process if it has not already been stopped, since this is required for ptrace(2) to work. libdtrace does not seem to stop target processes before trying to remove their breakpoints, so we were previously failing to remove the breakpoint on r_debug_state() in rtld. This was causing processes to die with SIGTRAP if they called dlopen(3) after dtrace(1) had detached. Modified: stable/10/lib/libproc/proc_bkpt.c Directory Properties: stable/10/ (props changed) Modified: stable/10/lib/libproc/proc_bkpt.c ============================================================================== --- stable/10/lib/libproc/proc_bkpt.c Fri Dec 27 22:29:20 2013 (r259968) +++ stable/10/lib/libproc/proc_bkpt.c Fri Dec 27 22:30:36 2013 (r259969) @@ -37,8 +37,9 @@ __FBSDID("$FreeBSD$"); #include #include -#include #include +#include +#include #include "_libproc.h" #if defined(__i386__) || defined(__amd64__) @@ -54,12 +55,39 @@ __FBSDID("$FreeBSD$"); #error "Add support for your architecture" #endif +static void +proc_cont(struct proc_handle *phdl) +{ + + ptrace(PT_CONTINUE, proc_getpid(phdl), (caddr_t)1, 0); +} + +static int +proc_stop(struct proc_handle *phdl) +{ + int status; + + if (kill(proc_getpid(phdl), SIGSTOP) == -1) { + DPRINTF("kill %d", proc_getpid(phdl)); + return (-1); + } else if (waitpid(proc_getpid(phdl), &status, WSTOPPED) == -1) { + DPRINTF("waitpid %d", proc_getpid(phdl)); + return (-1); + } else if (!WIFSTOPPED(status)) { + DPRINTFX("waitpid: unexpected status 0x%x", status); + return (-1); + } + + return (0); +} + int proc_bkptset(struct proc_handle *phdl, uintptr_t address, unsigned long *saved) { struct ptrace_io_desc piod; unsigned long paddr, caddr; + int ret = 0; *saved = 0; if (phdl->status == PS_DEAD || phdl->status == PS_UNDEAD || @@ -68,6 +96,12 @@ proc_bkptset(struct proc_handle *phdl, u return (-1); } + DPRINTFX("adding breakpoint at 0x%lx", address); + + if (phdl->status != PS_STOP) + if (proc_stop(phdl) != 0) + return (-1); + /* * Read the original instruction. */ @@ -80,7 +114,8 @@ proc_bkptset(struct proc_handle *phdl, u if (ptrace(PT_IO, proc_getpid(phdl), (caddr_t)&piod, 0) < 0) { DPRINTF("ERROR: couldn't read instruction at address 0x%" PRIuPTR, address); - return (-1); + ret = -1; + goto done; } *saved = paddr; /* @@ -95,10 +130,16 @@ proc_bkptset(struct proc_handle *phdl, u if (ptrace(PT_IO, proc_getpid(phdl), (caddr_t)&piod, 0) < 0) { DPRINTF("ERROR: couldn't write instruction at address 0x%" PRIuPTR, address); - return (-1); + ret = -1; + goto done; } - return (0); +done: + if (phdl->status != PS_STOP) + /* Restart the process if we had to stop it. */ + proc_cont(phdl); + + return (ret); } int @@ -107,13 +148,20 @@ proc_bkptdel(struct proc_handle *phdl, u { struct ptrace_io_desc piod; unsigned long paddr, caddr; + int ret = 0; if (phdl->status == PS_DEAD || phdl->status == PS_UNDEAD || phdl->status == PS_IDLE) { errno = ENOENT; return (-1); } - DPRINTFX("removing breakpoint at 0x%lx\n", address); + + DPRINTFX("removing breakpoint at 0x%lx", address); + + if (phdl->status != PS_STOP) + if (proc_stop(phdl) != 0) + return (-1); + /* * Overwrite the breakpoint instruction that we setup previously. */ @@ -126,10 +174,14 @@ proc_bkptdel(struct proc_handle *phdl, u if (ptrace(PT_IO, proc_getpid(phdl), (caddr_t)&piod, 0) < 0) { DPRINTF("ERROR: couldn't write instruction at address 0x%" PRIuPTR, address); - return (-1); + ret = -1; } + + if (phdl->status != PS_STOP) + /* Restart the process if we had to stop it. */ + proc_cont(phdl); - return (0); + return (ret); } /*