2 * Copyright (c) 2010 The FreeBSD Foundation
3 * Copyright (c) 2008 John Birrell (jb@freebsd.org)
6 * Portions of this software were developed by Rui Paulo under sponsorship
7 * from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/types.h>
35 #include <sys/ptrace.h>
47 proc_clearflags(struct proc_handle *phdl, int mask)
59 * NB: we return -1 as the Solaris libproc Psetrun() function.
62 proc_continue(struct proc_handle *phdl)
69 if (phdl->status == PS_STOP && WSTOPSIG(phdl->wstat) != SIGTRAP)
70 pending = WSTOPSIG(phdl->wstat);
73 if (ptrace(PT_CONTINUE, phdl->pid, (caddr_t)(uintptr_t)1, pending) != 0)
76 phdl->status = PS_RUN;
82 proc_detach(struct proc_handle *phdl, int reason)
88 if (reason == PRELEASE_KILL) {
89 kill(phdl->pid, SIGKILL);
92 if (ptrace(PT_DETACH, phdl->pid, 0, 0) != 0 && errno == ESRCH)
95 kill(phdl->pid, SIGSTOP);
96 waitpid(phdl->pid, &status, WUNTRACED);
97 ptrace(PT_DETACH, phdl->pid, 0, 0);
98 kill(phdl->pid, SIGCONT);
106 proc_getflags(struct proc_handle *phdl)
112 return (phdl->flags);
116 proc_setflags(struct proc_handle *phdl, int mask)
128 proc_state(struct proc_handle *phdl)
134 return (phdl->status);
138 proc_getpid(struct proc_handle *phdl)
148 proc_wstatus(struct proc_handle *phdl)
154 if (waitpid(phdl->pid, &status, WUNTRACED) < 0) {
159 if (WIFSTOPPED(status))
160 phdl->status = PS_STOP;
161 if (WIFEXITED(status) || WIFSIGNALED(status))
162 phdl->status = PS_UNDEAD;
163 phdl->wstat = status;
165 return (phdl->status);
169 proc_getwstat(struct proc_handle *phdl)
175 return (phdl->wstat);
179 proc_signame(int sig, char *name, size_t namesz)
182 strlcpy(name, strsignal(sig), namesz);
188 proc_read(struct proc_handle *phdl, void *buf, size_t size, size_t addr)
190 struct ptrace_io_desc piod;
194 piod.piod_op = PIOD_READ_D;
195 piod.piod_len = size;
196 piod.piod_addr = (void *)buf;
197 piod.piod_offs = (void *)addr;
199 if (ptrace(PT_IO, phdl->pid, (caddr_t)&piod, 0) < 0)
201 return (piod.piod_len);
205 proc_getlwpstatus(struct proc_handle *phdl)
207 struct ptrace_lwpinfo lwpinfo;
208 lwpstatus_t *psp = &phdl->lwps;
213 if (ptrace(PT_LWPINFO, phdl->pid, (caddr_t)&lwpinfo,
214 sizeof(lwpinfo)) < 0)
216 siginfo = &lwpinfo.pl_siginfo;
217 if (lwpinfo.pl_event == PL_EVENT_SIGNAL &&
218 (lwpinfo.pl_flags & PL_FLAG_SI) != 0) {
219 if (siginfo->si_signo == SIGTRAP &&
220 (siginfo->si_code == TRAP_BRKPT ||
221 siginfo->si_code == TRAP_TRACE)) {
222 psp->pr_why = PR_FAULTED;
223 psp->pr_what = FLTBPT;
225 psp->pr_why = PR_SIGNALLED;
226 psp->pr_what = siginfo->si_signo;
228 } else if (lwpinfo.pl_flags & PL_FLAG_SCE) {
229 psp->pr_why = PR_SYSENTRY;
230 } else if (lwpinfo.pl_flags & PL_FLAG_SCX) {
231 psp->pr_why = PR_SYSEXIT;