1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2010 The FreeBSD Foundation 5 * Copyright (c) 2008 John Birrell (jb@freebsd.org) 6 * All rights reserved. 7 * 8 * Portions of this software were developed by Rui Paulo under sponsorship 9 * from the FreeBSD Foundation. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 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. 19 * 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 30 * SUCH DAMAGE. 31 */ 32 33 #include <sys/cdefs.h> 34 __FBSDID("$FreeBSD$"); 35 36 #include <sys/types.h> 37 #include <sys/ptrace.h> 38 #include <sys/wait.h> 39 40 #include <err.h> 41 #include <errno.h> 42 #include <signal.h> 43 #include <string.h> 44 #include <unistd.h> 45 46 #include "_libproc.h" 47 48 int 49 proc_clearflags(struct proc_handle *phdl, int mask) 50 { 51 52 if (phdl == NULL) 53 return (EINVAL); 54 55 phdl->flags &= ~mask; 56 57 return (0); 58 } 59 60 /* 61 * NB: we return -1 as the Solaris libproc Psetrun() function. 62 */ 63 int 64 proc_continue(struct proc_handle *phdl) 65 { 66 int pending; 67 68 if (phdl == NULL) 69 return (-1); 70 71 if (phdl->status == PS_STOP && WSTOPSIG(phdl->wstat) != SIGTRAP) 72 pending = WSTOPSIG(phdl->wstat); 73 else 74 pending = 0; 75 if (ptrace(PT_CONTINUE, proc_getpid(phdl), (caddr_t)(uintptr_t)1, 76 pending) != 0) 77 return (-1); 78 79 phdl->status = PS_RUN; 80 81 return (0); 82 } 83 84 int 85 proc_detach(struct proc_handle *phdl, int reason) 86 { 87 int status; 88 int request; 89 pid_t pid; 90 91 if (phdl == NULL) 92 return (EINVAL); 93 if (reason == PRELEASE_HANG) 94 return (EINVAL); 95 if ((phdl->flags & PATTACH_RDONLY) != 0) 96 goto free; 97 request = (reason == PRELEASE_KILL) ? PT_KILL : PT_DETACH; 98 pid = proc_getpid(phdl); 99 if (ptrace(request, pid, 0, 0) != 0 && errno == EBUSY) { 100 kill(pid, SIGSTOP); 101 waitpid(pid, &status, WUNTRACED); 102 ptrace(request, pid, 0, 0); 103 kill(pid, SIGCONT); 104 } 105 free: 106 proc_free(phdl); 107 return (0); 108 } 109 110 int 111 proc_getflags(struct proc_handle *phdl) 112 { 113 114 if (phdl == NULL) 115 return (-1); 116 117 return (phdl->flags); 118 } 119 120 int 121 proc_setflags(struct proc_handle *phdl, int mask) 122 { 123 124 if (phdl == NULL) 125 return (EINVAL); 126 127 phdl->flags |= mask; 128 129 return (0); 130 } 131 132 int 133 proc_state(struct proc_handle *phdl) 134 { 135 136 if (phdl == NULL) 137 return (-1); 138 139 return (phdl->status); 140 } 141 142 int 143 proc_getmodel(struct proc_handle *phdl) 144 { 145 146 if (phdl == NULL) 147 return (-1); 148 149 return (phdl->model); 150 } 151 152 int 153 proc_wstatus(struct proc_handle *phdl) 154 { 155 int status; 156 157 if (phdl == NULL) 158 return (-1); 159 if (waitpid(proc_getpid(phdl), &status, WUNTRACED) < 0) { 160 if (errno != EINTR) 161 DPRINTF("waitpid"); 162 return (-1); 163 } 164 if (WIFSTOPPED(status)) 165 phdl->status = PS_STOP; 166 if (WIFEXITED(status) || WIFSIGNALED(status)) 167 phdl->status = PS_UNDEAD; 168 phdl->wstat = status; 169 170 return (phdl->status); 171 } 172 173 int 174 proc_getwstat(struct proc_handle *phdl) 175 { 176 177 if (phdl == NULL) 178 return (-1); 179 180 return (phdl->wstat); 181 } 182 183 char * 184 proc_signame(int sig, char *name, size_t namesz) 185 { 186 187 strlcpy(name, strsignal(sig), namesz); 188 189 return (name); 190 } 191 192 int 193 proc_read(struct proc_handle *phdl, void *buf, size_t size, size_t addr) 194 { 195 struct ptrace_io_desc piod; 196 197 if (phdl == NULL) 198 return (-1); 199 piod.piod_op = PIOD_READ_D; 200 piod.piod_len = size; 201 piod.piod_addr = (void *)buf; 202 piod.piod_offs = (void *)addr; 203 204 if (ptrace(PT_IO, proc_getpid(phdl), (caddr_t)&piod, 0) < 0) 205 return (-1); 206 return (piod.piod_len); 207 } 208 209 const lwpstatus_t * 210 proc_getlwpstatus(struct proc_handle *phdl) 211 { 212 struct ptrace_lwpinfo lwpinfo; 213 lwpstatus_t *psp = &phdl->lwps; 214 siginfo_t *siginfo; 215 216 if (phdl == NULL) 217 return (NULL); 218 if (ptrace(PT_LWPINFO, proc_getpid(phdl), (caddr_t)&lwpinfo, 219 sizeof(lwpinfo)) < 0) 220 return (NULL); 221 siginfo = &lwpinfo.pl_siginfo; 222 if (lwpinfo.pl_event == PL_EVENT_SIGNAL && 223 (lwpinfo.pl_flags & PL_FLAG_SI) != 0) { 224 if (siginfo->si_signo == SIGTRAP && 225 (siginfo->si_code == TRAP_BRKPT || 226 siginfo->si_code == TRAP_TRACE)) { 227 psp->pr_why = PR_FAULTED; 228 psp->pr_what = FLTBPT; 229 } else { 230 psp->pr_why = PR_SIGNALLED; 231 psp->pr_what = siginfo->si_signo; 232 } 233 } else if (lwpinfo.pl_flags & PL_FLAG_SCE) { 234 psp->pr_why = PR_SYSENTRY; 235 } else if (lwpinfo.pl_flags & PL_FLAG_SCX) { 236 psp->pr_why = PR_SYSEXIT; 237 } 238 239 return (psp); 240 } 241