1 /* $NetBSD: sys_process.c,v 1.40 1994/09/14 16:40:29 cgd Exp $ */ 2 3 /*- 4 * Copyright (c) 1994 Christopher G. Demetriou. All rights reserved. 5 * Copyright (c) 1982, 1986, 1989, 1993 6 * The Regents of the University of California. All rights reserved. 7 * (c) UNIX System Laboratories, Inc. 8 * All or some portions of this file are derived from material licensed 9 * to the University of California by American Telephone and Telegraph 10 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 11 * the permission of UNIX System Laboratories, Inc. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. All advertising materials mentioning features or use of this software 22 * must display the following acknowledgement: 23 * This product includes software developed by the University of 24 * California, Berkeley and its contributors. 25 * 4. Neither the name of the University nor the names of its contributors 26 * may be used to endorse or promote products derived from this software 27 * without specific prior written permission. 28 * 29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 39 * SUCH DAMAGE. 40 * 41 * from: @(#)sys_process.c 8.1 (Berkeley) 6/10/93 42 */ 43 44 /* 45 * References: 46 * (1) Bach's "The Design of the UNIX Operating System", 47 * (2) sys/miscfs/procfs from UCB's 4.4BSD-Lite distribution, 48 * (3) the "4.4BSD Programmer's Reference Manual" published 49 * by USENIX and O'Reilly & Associates. 50 * The 4.4BSD PRM does a reasonably good job of documenting what the various 51 * ptrace() requests should actually do, and its text is quoted several times 52 * in this file. 53 */ 54 55 #include <sys/param.h> 56 #include <sys/proc.h> 57 #include <sys/errno.h> 58 #include <sys/ptrace.h> 59 #include <sys/uio.h> 60 #include <sys/user.h> 61 62 #include <machine/reg.h> 63 64 /* Macros to clear/set/test flags. */ 65 #define SET(t, f) (t) |= (f) 66 #define CLR(t, f) (t) &= ~(f) 67 #define ISSET(t, f) ((t) & (f)) 68 69 /* 70 * Process debugging system call. 71 */ 72 struct ptrace_args { 73 int req; 74 pid_t pid; 75 caddr_t addr; 76 int data; 77 }; 78 ptrace(p, uap, retval) 79 struct proc *p; 80 struct ptrace_args *uap; 81 int *retval; 82 { 83 struct proc *t; /* target process */ 84 struct uio uio; 85 struct iovec iov; 86 int error, step, write; 87 88 /* "A foolish consistency..." XXX */ 89 if (uap->req == PT_TRACE_ME) 90 t = p; 91 else { 92 /* Find the process we're supposed to be operating on. */ 93 if ((t = pfind(uap->pid)) == NULL) 94 return (ESRCH); 95 } 96 97 /* Make sure we can operate on it. */ 98 switch (uap->req) { 99 case PT_TRACE_ME: 100 /* Saying that you're being traced is always legal. */ 101 break; 102 103 case PT_ATTACH: 104 /* 105 * You can't attach to a process if: 106 * (1) it's the process that's doing the attaching, 107 */ 108 if (t->p_pid == p->p_pid) 109 return (EINVAL); 110 111 /* 112 * (2) it's already being traced, or 113 */ 114 if (!ISSET(t->p_flag, P_TRACED)) 115 return (EBUSY); 116 117 /* 118 * (3) it's not owned by you, or is set-id on exec 119 * (unless you're root). 120 */ 121 if ((t->p_cred->p_ruid != p->p_cred->p_ruid || 122 ISSET(t->p_flag, P_SUGID)) && 123 (error = suser(p->p_ucred, &p->p_acflag)) != 0) 124 return (error); 125 break; 126 127 case PT_READ_I: 128 case PT_READ_D: 129 case PT_READ_U: 130 case PT_WRITE_I: 131 case PT_WRITE_D: 132 case PT_WRITE_U: 133 case PT_CONTINUE: 134 case PT_KILL: 135 case PT_DETACH: 136 #ifdef PT_STEP 137 case PT_STEP: 138 #endif 139 #ifdef PT_GETREGS 140 case PT_GETREGS: 141 #endif 142 #ifdef PT_SETREGS 143 case PT_SETREGS: 144 #endif 145 #ifdef PT_GETFPREGS 146 case PT_GETFPREGS: 147 #endif 148 #ifdef PT_SETFPREGS 149 case PT_SETFPREGS: 150 #endif 151 /* 152 * You can't do what you want to the process if: 153 * (1) It's not being traced at all, 154 */ 155 if (!ISSET(t->p_flag, P_TRACED)) 156 return (EPERM); 157 158 /* 159 * (2) it's not being traced by _you_, or 160 */ 161 if (t->p_pptr != p) 162 return (EBUSY); 163 164 /* 165 * (3) it's not currently stopped. 166 */ 167 if (t->p_stat != SSTOP || !ISSET(t->p_flag, P_WAITED)) 168 return (EBUSY); 169 break; 170 171 default: /* It was not a legal request. */ 172 return (EINVAL); 173 } 174 175 /* Do single-step fixup if needed. */ 176 FIX_SSTEP(t); 177 178 /* Now do the operation. */ 179 step = write = 0; 180 *retval = 0; 181 182 switch (uap->req) { 183 case PT_TRACE_ME: 184 /* Just set the trace flag. */ 185 SET(t->p_flag, P_TRACED); 186 return (0); 187 188 case PT_WRITE_I: /* XXX no seperate I and D spaces */ 189 case PT_WRITE_D: 190 write = 1; 191 case PT_READ_I: /* XXX no seperate I and D spaces */ 192 case PT_READ_D: 193 /* write = 0 done above. */ 194 iov.iov_base = write ? (caddr_t)&uap->data : (caddr_t)retval; 195 iov.iov_len = sizeof(int); 196 uio.uio_iov = &iov; 197 uio.uio_iovcnt = 1; 198 uio.uio_offset = (off_t)uap->addr; 199 uio.uio_resid = sizeof(int); 200 uio.uio_segflg = UIO_SYSSPACE; 201 uio.uio_rw = write ? UIO_WRITE : UIO_READ; 202 uio.uio_procp = p; 203 return (procfs_domem(p, t, NULL, &uio)); 204 205 case PT_READ_U: 206 /* 207 * The 4.4BSD PRM says that only the first 512 bytes 208 * of the user area are accessible for reading. This 209 * disagrees with common practice, and would render PT_READ_U 210 * almost worthless. Additionally, we _always_ require 211 * that the address be int-aligned. 212 */ 213 if ((u_long)uap->addr > USPACE - sizeof(int) || 214 #ifdef m68k /* XXX */ 215 ((u_long)uap->addr & 1) != 0) 216 #else /* !m68k XXX */ 217 ((u_long)uap->addr & (sizeof(int) - 1)) != 0) 218 #endif /* !m68k XXX */ 219 return (EINVAL); 220 221 /* 222 * Fill in eproc in user area, because user.h says that 223 * it's valid for ptrace(). Do it the same way as coredump(). 224 */ 225 bcopy(t, &t->p_addr->u_kproc.kp_proc, sizeof(struct proc)); 226 fill_eproc(t, &t->p_addr->u_kproc.kp_eproc); 227 228 /* Finally, pull the appropriate int out of the user area. */ 229 *retval = *(int *)((caddr_t)t->p_addr + (u_long)uap->addr); 230 return (0); 231 232 case PT_WRITE_U: 233 /* 234 * Mostly the same as PT_READ_U, but write data instead of 235 * reading it. Don't bother filling in the eproc, because 236 * it won't be used for anything anyway. 237 */ 238 if ((u_long)uap->addr > USPACE - sizeof(int) || 239 #ifdef m68k /* XXX */ 240 ((u_long)uap->addr & 1) != 0) 241 #else /* !m68k XXX */ 242 ((u_long)uap->addr & (sizeof(int) - 1)) != 0) 243 #endif /* !m68k XXX */ 244 return (EINVAL); 245 246 /* And write the data. */ 247 *(int *)((caddr_t)t->p_addr + (u_long)uap->addr) = uap->data; 248 return (0); 249 250 #ifdef PT_STEP 251 case PT_STEP: 252 /* 253 * From the 4.4BSD PRM: 254 * "Execution continues as in request PT_CONTINUE; however 255 * as soon as possible after execution of at least one 256 * instruction, execution stops again. [ ... ]" 257 */ 258 step = 1; 259 #endif 260 case PT_CONTINUE: 261 /* 262 * From the 4.4BSD PRM: 263 * "The data argument is taken as a signal number and the 264 * child's execution continues at location addr as if it 265 * incurred that signal. Nromally the signal number will 266 * be either 0 to indicate that the signal that caused the 267 * stop should be ignored, or that value fetched out of 268 * the process's image indicating which signal caused 269 * the stop. If addr is (int *)1 then execution continues 270 * from where it stopped." 271 */ 272 /* step = 0 done above. */ 273 274 /* Check that uap->data is a valid signal number or zero. */ 275 if (uap->data < 0 || uap->data >= NSIG) 276 return (EINVAL); 277 278 /* 279 * Arrange for a single-step, if that's requested and possible. 280 */ 281 if (error = process_sstep(t, step)) 282 return (error); 283 284 /* If the address paramter is not (int *)1, set the pc. */ 285 if ((int *)uap->addr != (int *)1) 286 if (error = process_set_pc(t, uap->addr)) 287 return (error); 288 289 /* Finally, deliver the requested signal (or none). */ 290 sendsig: 291 t->p_xstat = uap->data; 292 setrunnable(t); 293 return (0); 294 295 case PT_KILL: 296 /* just send the process a KILL signal. */ 297 uap->data = SIGKILL; 298 goto sendsig; /* in PT_CONTINUE, above. */ 299 300 case PT_ATTACH: 301 /* 302 * As done in procfs: 303 * Go ahead and set the trace flag. 304 * Save the old parent (it's reset in 305 * _DETACH, and also in kern_exit.c:wait4() 306 * Reparent the process so that the tracing 307 * proc gets to see all the action. 308 * Stop the target. 309 */ 310 SET(t->p_flag, P_TRACED); 311 t->p_xstat = 0; /* XXX ? */ 312 if (t->p_pptr != p) { 313 t->p_oppid = t->p_pptr->p_pid; 314 proc_reparent(t, p); 315 } 316 psignal(t, SIGSTOP); 317 return (0); 318 319 case PT_DETACH: 320 /* Again, as done in procfs: */ 321 322 #ifdef notdef /* not allowed, by checks above. */ 323 /* if not being traced, then this is a painless no-op */ 324 if (!ISSET(t->p_flag, P_TRACED)) 325 return (0); 326 #endif 327 328 /* not being traced any more */ 329 CLR(t->p_flag, P_TRACED); 330 331 /* give process back to original parent */ 332 if (t->p_oppid != t->p_pptr->p_pid) { 333 struct proc *pp; 334 335 pp = pfind(t->p_oppid); 336 if (pp) 337 proc_reparent(t, pp); 338 } 339 340 t->p_oppid = 0; 341 CLR(t->p_flag, P_WAITED); /* XXX? */ 342 343 /* and deliver any signal requested by tracer. */ 344 if (t->p_stat == SSTOP) { 345 t->p_xstat = uap->data; 346 setrunnable(t); 347 } else if (uap->data) 348 psignal(t, uap->data); 349 350 return (0); 351 352 #ifdef PT_SETREGS 353 case PT_SETREGS: 354 write = 1; 355 #endif 356 #ifdef PT_GETREGS 357 case PT_GETREGS: 358 /* write = 0 done above. */ 359 #endif 360 #if defined(PT_SETREGS) || defined(PT_GETREGS) 361 if (!procfs_validregs(t)) 362 return (EINVAL); 363 else { 364 iov.iov_base = uap->addr; 365 iov.iov_len = sizeof(struct reg); 366 uio.uio_iov = &iov; 367 uio.uio_iovcnt = 1; 368 uio.uio_offset = 0; 369 uio.uio_resid = sizeof(struct reg); 370 uio.uio_segflg = UIO_USERSPACE; 371 uio.uio_rw = write ? UIO_WRITE : UIO_READ; 372 uio.uio_procp = p; 373 return (procfs_doregs(p, t, NULL, &uio)); 374 } 375 #endif 376 377 #ifdef PT_SETFPREGS 378 case PT_SETFPREGS: 379 write = 1; 380 #endif 381 #ifdef PT_GETFPREGS 382 case PT_GETFPREGS: 383 /* write = 0 done above. */ 384 #endif 385 #if defined(PT_SETFPREGS) || defined(PT_GETFPREGS) 386 if (!procfs_validfpregs(t)) 387 return (EINVAL); 388 else { 389 iov.iov_base = uap->addr; 390 iov.iov_len = sizeof(struct fpreg); 391 uio.uio_iov = &iov; 392 uio.uio_iovcnt = 1; 393 uio.uio_offset = 0; 394 uio.uio_resid = sizeof(struct fpreg); 395 uio.uio_segflg = UIO_USERSPACE; 396 uio.uio_rw = write ? UIO_WRITE : UIO_READ; 397 uio.uio_procp = p; 398 return (procfs_dofpregs(p, t, NULL, &uio)); 399 } 400 #endif 401 } 402 403 #ifdef DIAGNOSTIC 404 panic("ptrace: impossible"); 405 #endif 406 } 407 408 trace_req(a1) 409 struct proc *a1; 410 { 411 412 /* just return 1 to keep other parts of the system happy */ 413 return (1); 414 } 415