2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2010 The FreeBSD Foundation
5 * Copyright (c) 2008 John Birrell (jb@freebsd.org)
8 * Portions of this software were developed by Rui Paulo under sponsorship
9 * from the FreeBSD Foundation.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/types.h>
37 #include <sys/ptrace.h>
49 proc_clearflags(struct proc_handle *phdl, int mask)
61 * NB: we return -1 as the Solaris libproc Psetrun() function.
64 proc_continue(struct proc_handle *phdl)
71 if (phdl->status == PS_STOP && WSTOPSIG(phdl->wstat) != SIGTRAP)
72 pending = WSTOPSIG(phdl->wstat);
75 if (ptrace(PT_CONTINUE, proc_getpid(phdl), (caddr_t)(uintptr_t)1,
79 phdl->status = PS_RUN;
85 proc_detach(struct proc_handle *phdl, int reason)
92 if (reason == PRELEASE_HANG)
94 if (reason == PRELEASE_KILL) {
95 kill(proc_getpid(phdl), SIGKILL);
98 if ((phdl->flags & PATTACH_RDONLY) != 0)
100 pid = proc_getpid(phdl);
101 if (ptrace(PT_DETACH, pid, 0, 0) != 0 && errno == ESRCH)
103 if (errno == EBUSY) {
105 waitpid(pid, &status, WUNTRACED);
106 ptrace(PT_DETACH, pid, 0, 0);
115 proc_getflags(struct proc_handle *phdl)
121 return (phdl->flags);
125 proc_setflags(struct proc_handle *phdl, int mask)
137 proc_state(struct proc_handle *phdl)
143 return (phdl->status);
147 proc_getmodel(struct proc_handle *phdl)
153 return (phdl->model);
157 proc_wstatus(struct proc_handle *phdl)
163 if (waitpid(proc_getpid(phdl), &status, WUNTRACED) < 0) {
168 if (WIFSTOPPED(status))
169 phdl->status = PS_STOP;
170 if (WIFEXITED(status) || WIFSIGNALED(status))
171 phdl->status = PS_UNDEAD;
172 phdl->wstat = status;
174 return (phdl->status);
178 proc_getwstat(struct proc_handle *phdl)
184 return (phdl->wstat);
188 proc_signame(int sig, char *name, size_t namesz)
191 strlcpy(name, strsignal(sig), namesz);
197 proc_read(struct proc_handle *phdl, void *buf, size_t size, size_t addr)
199 struct ptrace_io_desc piod;
203 piod.piod_op = PIOD_READ_D;
204 piod.piod_len = size;
205 piod.piod_addr = (void *)buf;
206 piod.piod_offs = (void *)addr;
208 if (ptrace(PT_IO, proc_getpid(phdl), (caddr_t)&piod, 0) < 0)
210 return (piod.piod_len);
214 proc_getlwpstatus(struct proc_handle *phdl)
216 struct ptrace_lwpinfo lwpinfo;
217 lwpstatus_t *psp = &phdl->lwps;
222 if (ptrace(PT_LWPINFO, proc_getpid(phdl), (caddr_t)&lwpinfo,
223 sizeof(lwpinfo)) < 0)
225 siginfo = &lwpinfo.pl_siginfo;
226 if (lwpinfo.pl_event == PL_EVENT_SIGNAL &&
227 (lwpinfo.pl_flags & PL_FLAG_SI) != 0) {
228 if (siginfo->si_signo == SIGTRAP &&
229 (siginfo->si_code == TRAP_BRKPT ||
230 siginfo->si_code == TRAP_TRACE)) {
231 psp->pr_why = PR_FAULTED;
232 psp->pr_what = FLTBPT;
234 psp->pr_why = PR_SIGNALLED;
235 psp->pr_what = siginfo->si_signo;
237 } else if (lwpinfo.pl_flags & PL_FLAG_SCE) {
238 psp->pr_why = PR_SYSENTRY;
239 } else if (lwpinfo.pl_flags & PL_FLAG_SCX) {
240 psp->pr_why = PR_SYSEXIT;