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