1 /* $NetBSD: sys_process.c,v 1.150 2009/10/04 03:15:08 elad Exp $ */ 2 3 /*- 4 * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Andrew Doran. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /*- 33 * Copyright (c) 1982, 1986, 1989, 1993 34 * The Regents of the University of California. All rights reserved. 35 * (c) UNIX System Laboratories, Inc. 36 * All or some portions of this file are derived from material licensed 37 * to the University of California by American Telephone and Telegraph 38 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 39 * the permission of UNIX System Laboratories, Inc. 40 * 41 * This code is derived from software contributed to Berkeley by 42 * Jan-Simon Pendry. 43 * 44 * Redistribution and use in source and binary forms, with or without 45 * modification, are permitted provided that the following conditions 46 * are met: 47 * 1. Redistributions of source code must retain the above copyright 48 * notice, this list of conditions and the following disclaimer. 49 * 2. Redistributions in binary form must reproduce the above copyright 50 * notice, this list of conditions and the following disclaimer in the 51 * documentation and/or other materials provided with the distribution. 52 * 3. Neither the name of the University nor the names of its contributors 53 * may be used to endorse or promote products derived from this software 54 * without specific prior written permission. 55 * 56 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 57 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 58 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 59 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 60 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 61 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 62 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 63 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 64 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 65 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 66 * SUCH DAMAGE. 67 * 68 * from: @(#)sys_process.c 8.1 (Berkeley) 6/10/93 69 */ 70 71 /*- 72 * Copyright (c) 1993 Jan-Simon Pendry. 73 * Copyright (c) 1994 Christopher G. Demetriou. All rights reserved. 74 * 75 * This code is derived from software contributed to Berkeley by 76 * Jan-Simon Pendry. 77 * 78 * Redistribution and use in source and binary forms, with or without 79 * modification, are permitted provided that the following conditions 80 * are met: 81 * 1. Redistributions of source code must retain the above copyright 82 * notice, this list of conditions and the following disclaimer. 83 * 2. Redistributions in binary form must reproduce the above copyright 84 * notice, this list of conditions and the following disclaimer in the 85 * documentation and/or other materials provided with the distribution. 86 * 3. All advertising materials mentioning features or use of this software 87 * must display the following acknowledgement: 88 * This product includes software developed by the University of 89 * California, Berkeley and its contributors. 90 * 4. Neither the name of the University nor the names of its contributors 91 * may be used to endorse or promote products derived from this software 92 * without specific prior written permission. 93 * 94 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 95 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 96 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 97 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 98 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 99 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 100 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 101 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 102 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 103 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 104 * SUCH DAMAGE. 105 * 106 * from: @(#)sys_process.c 8.1 (Berkeley) 6/10/93 107 */ 108 109 /* 110 * References: 111 * (1) Bach's "The Design of the UNIX Operating System", 112 * (2) sys/miscfs/procfs from UCB's 4.4BSD-Lite distribution, 113 * (3) the "4.4BSD Programmer's Reference Manual" published 114 * by USENIX and O'Reilly & Associates. 115 * The 4.4BSD PRM does a reasonably good job of documenting what the various 116 * ptrace() requests should actually do, and its text is quoted several times 117 * in this file. 118 */ 119 120 #include <sys/cdefs.h> 121 __KERNEL_RCSID(0, "$NetBSD: sys_process.c,v 1.150 2009/10/04 03:15:08 elad Exp $"); 122 123 #include "opt_ptrace.h" 124 #include "opt_ktrace.h" 125 126 #include <sys/param.h> 127 #include <sys/systm.h> 128 #include <sys/proc.h> 129 #include <sys/errno.h> 130 #include <sys/ptrace.h> 131 #include <sys/uio.h> 132 #include <sys/user.h> 133 #include <sys/ras.h> 134 #include <sys/kmem.h> 135 #include <sys/kauth.h> 136 #include <sys/mount.h> 137 #include <sys/syscallargs.h> 138 139 #include <uvm/uvm_extern.h> 140 141 #include <machine/reg.h> 142 143 #ifdef PTRACE 144 static kauth_listener_t ptrace_listener; 145 146 static int 147 ptrace_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie, 148 void *arg0, void *arg1, void *arg2, void *arg3) 149 { 150 struct proc *p; 151 int result; 152 153 result = KAUTH_RESULT_DEFER; 154 p = arg0; 155 156 if (action != KAUTH_PROCESS_PTRACE) 157 return result; 158 159 switch ((u_long)arg1) { 160 case PT_TRACE_ME: 161 case PT_ATTACH: 162 case PT_WRITE_I: 163 case PT_WRITE_D: 164 case PT_READ_I: 165 case PT_READ_D: 166 case PT_IO: 167 #ifdef PT_GETREGS 168 case PT_GETREGS: 169 #endif 170 #ifdef PT_SETREGS 171 case PT_SETREGS: 172 #endif 173 #ifdef PT_GETFPREGS 174 case PT_GETFPREGS: 175 #endif 176 #ifdef PT_SETFPREGS 177 case PT_SETFPREGS: 178 #endif 179 #ifdef __HAVE_PTRACE_MACHDEP 180 PTRACE_MACHDEP_REQUEST_CASES 181 #endif 182 if (kauth_cred_getuid(cred) != kauth_cred_getuid(p->p_cred) || 183 ISSET(p->p_flag, PK_SUGID)) { 184 break; 185 } 186 187 result = KAUTH_RESULT_ALLOW; 188 189 break; 190 191 #ifdef PT_STEP 192 case PT_STEP: 193 #endif 194 case PT_CONTINUE: 195 case PT_KILL: 196 case PT_DETACH: 197 case PT_LWPINFO: 198 case PT_SYSCALL: 199 case PT_DUMPCORE: 200 result = KAUTH_RESULT_ALLOW; 201 break; 202 203 default: 204 break; 205 } 206 207 return result; 208 } 209 210 void 211 ptrace_init(void) 212 { 213 214 ptrace_listener = kauth_listen_scope(KAUTH_SCOPE_PROCESS, 215 ptrace_listener_cb, NULL); 216 } 217 218 /* 219 * Process debugging system call. 220 */ 221 int 222 sys_ptrace(struct lwp *l, const struct sys_ptrace_args *uap, register_t *retval) 223 { 224 /* { 225 syscallarg(int) req; 226 syscallarg(pid_t) pid; 227 syscallarg(void *) addr; 228 syscallarg(int) data; 229 } */ 230 struct proc *p = l->l_proc; 231 struct lwp *lt; 232 struct proc *t; /* target process */ 233 struct uio uio; 234 struct iovec iov; 235 struct ptrace_io_desc piod; 236 struct ptrace_lwpinfo pl; 237 struct vmspace *vm; 238 int error, write, tmp, req, pheld; 239 int signo; 240 ksiginfo_t ksi; 241 char *path; 242 int len; 243 244 error = 0; 245 req = SCARG(uap, req); 246 247 /* 248 * If attaching or detaching, we need to get a write hold on the 249 * proclist lock so that we can re-parent the target process. 250 */ 251 mutex_enter(proc_lock); 252 253 /* "A foolish consistency..." XXX */ 254 if (req == PT_TRACE_ME) { 255 t = p; 256 mutex_enter(t->p_lock); 257 } else { 258 /* Find the process we're supposed to be operating on. */ 259 if ((t = p_find(SCARG(uap, pid), PFIND_LOCKED)) == NULL) { 260 mutex_exit(proc_lock); 261 return (ESRCH); 262 } 263 264 /* XXX-elad */ 265 mutex_enter(t->p_lock); 266 error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CANSEE, 267 t, KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL); 268 if (error) { 269 mutex_exit(proc_lock); 270 mutex_exit(t->p_lock); 271 return (ESRCH); 272 } 273 } 274 275 /* 276 * Grab a reference on the process to prevent it from execing or 277 * exiting. 278 */ 279 if (!rw_tryenter(&t->p_reflock, RW_READER)) { 280 mutex_exit(proc_lock); 281 mutex_exit(t->p_lock); 282 return EBUSY; 283 } 284 285 /* Make sure we can operate on it. */ 286 switch (req) { 287 case PT_TRACE_ME: 288 /* Saying that you're being traced is always legal. */ 289 break; 290 291 case PT_ATTACH: 292 /* 293 * You can't attach to a process if: 294 * (1) it's the process that's doing the attaching, 295 */ 296 if (t->p_pid == p->p_pid) { 297 error = EINVAL; 298 break; 299 } 300 301 /* 302 * (2) it's a system process 303 */ 304 if (t->p_flag & PK_SYSTEM) { 305 error = EPERM; 306 break; 307 } 308 309 /* 310 * (3) it's already being traced, or 311 */ 312 if (ISSET(t->p_slflag, PSL_TRACED)) { 313 error = EBUSY; 314 break; 315 } 316 317 /* 318 * (4) the tracer is chrooted, and its root directory is 319 * not at or above the root directory of the tracee 320 */ 321 mutex_exit(t->p_lock); /* XXXSMP */ 322 tmp = proc_isunder(t, l); 323 mutex_enter(t->p_lock); /* XXXSMP */ 324 if (!tmp) { 325 error = EPERM; 326 break; 327 } 328 break; 329 330 case PT_READ_I: 331 case PT_READ_D: 332 case PT_WRITE_I: 333 case PT_WRITE_D: 334 case PT_IO: 335 #ifdef PT_GETREGS 336 case PT_GETREGS: 337 #endif 338 #ifdef PT_SETREGS 339 case PT_SETREGS: 340 #endif 341 #ifdef PT_GETFPREGS 342 case PT_GETFPREGS: 343 #endif 344 #ifdef PT_SETFPREGS 345 case PT_SETFPREGS: 346 #endif 347 #ifdef __HAVE_PTRACE_MACHDEP 348 PTRACE_MACHDEP_REQUEST_CASES 349 #endif 350 /* 351 * You can't read/write the memory or registers of a process 352 * if the tracer is chrooted, and its root directory is not at 353 * or above the root directory of the tracee. 354 */ 355 mutex_exit(t->p_lock); /* XXXSMP */ 356 tmp = proc_isunder(t, l); 357 mutex_enter(t->p_lock); /* XXXSMP */ 358 if (!tmp) { 359 error = EPERM; 360 break; 361 } 362 /*FALLTHROUGH*/ 363 364 case PT_CONTINUE: 365 case PT_KILL: 366 case PT_DETACH: 367 case PT_LWPINFO: 368 case PT_SYSCALL: 369 case PT_DUMPCORE: 370 #ifdef PT_STEP 371 case PT_STEP: 372 #endif 373 /* 374 * You can't do what you want to the process if: 375 * (1) It's not being traced at all, 376 */ 377 if (!ISSET(t->p_slflag, PSL_TRACED)) { 378 error = EPERM; 379 break; 380 } 381 382 /* 383 * (2) it's being traced by procfs (which has 384 * different signal delivery semantics), 385 */ 386 if (ISSET(t->p_slflag, PSL_FSTRACE)) { 387 uprintf("file system traced\n"); 388 error = EBUSY; 389 break; 390 } 391 392 /* 393 * (3) it's not being traced by _you_, or 394 */ 395 if (t->p_pptr != p) { 396 uprintf("parent %d != %d\n", t->p_pptr->p_pid, 397 p->p_pid); 398 error = EBUSY; 399 break; 400 } 401 402 /* 403 * (4) it's not currently stopped. 404 */ 405 if (t->p_stat != SSTOP || !t->p_waited /* XXXSMP */) { 406 uprintf("stat %d flag %d\n", t->p_stat, 407 !t->p_waited); 408 error = EBUSY; 409 break; 410 } 411 break; 412 413 default: /* It was not a legal request. */ 414 error = EINVAL; 415 break; 416 } 417 418 if (error == 0) { 419 error = kauth_authorize_process(l->l_cred, 420 KAUTH_PROCESS_PTRACE, t, KAUTH_ARG(req), 421 NULL, NULL); 422 } 423 if (error == 0) { 424 lt = lwp_find_first(t); 425 if (lt == NULL) 426 error = ESRCH; 427 } 428 429 if (error != 0) { 430 mutex_exit(proc_lock); 431 mutex_exit(t->p_lock); 432 rw_exit(&t->p_reflock); 433 return error; 434 } 435 436 /* Do single-step fixup if needed. */ 437 FIX_SSTEP(t); 438 KASSERT(lt != NULL); 439 lwp_addref(lt); 440 441 /* 442 * Which locks do we need held? XXX Ugly. 443 */ 444 switch (req) { 445 #ifdef PT_STEP 446 case PT_STEP: 447 #endif 448 case PT_CONTINUE: 449 case PT_DETACH: 450 case PT_KILL: 451 case PT_SYSCALL: 452 case PT_ATTACH: 453 case PT_TRACE_ME: 454 pheld = 1; 455 break; 456 default: 457 mutex_exit(proc_lock); 458 mutex_exit(t->p_lock); 459 pheld = 0; 460 break; 461 } 462 463 /* Now do the operation. */ 464 write = 0; 465 *retval = 0; 466 tmp = 0; 467 468 switch (req) { 469 case PT_TRACE_ME: 470 /* Just set the trace flag. */ 471 SET(t->p_slflag, PSL_TRACED); 472 t->p_opptr = t->p_pptr; 473 break; 474 475 case PT_WRITE_I: /* XXX no separate I and D spaces */ 476 case PT_WRITE_D: 477 #if defined(__HAVE_RAS) 478 /* 479 * Can't write to a RAS 480 */ 481 if (ras_lookup(t, SCARG(uap, addr)) != (void *)-1) { 482 error = EACCES; 483 break; 484 } 485 #endif 486 write = 1; 487 tmp = SCARG(uap, data); 488 /* FALLTHROUGH */ 489 490 case PT_READ_I: /* XXX no separate I and D spaces */ 491 case PT_READ_D: 492 /* write = 0 done above. */ 493 iov.iov_base = (void *)&tmp; 494 iov.iov_len = sizeof(tmp); 495 uio.uio_iov = &iov; 496 uio.uio_iovcnt = 1; 497 uio.uio_offset = (off_t)(unsigned long)SCARG(uap, addr); 498 uio.uio_resid = sizeof(tmp); 499 uio.uio_rw = write ? UIO_WRITE : UIO_READ; 500 UIO_SETUP_SYSSPACE(&uio); 501 502 error = process_domem(l, lt, &uio); 503 if (!write) 504 *retval = tmp; 505 break; 506 507 case PT_IO: 508 error = copyin(SCARG(uap, addr), &piod, sizeof(piod)); 509 if (error) 510 break; 511 switch (piod.piod_op) { 512 case PIOD_READ_D: 513 case PIOD_READ_I: 514 uio.uio_rw = UIO_READ; 515 break; 516 case PIOD_WRITE_D: 517 case PIOD_WRITE_I: 518 /* 519 * Can't write to a RAS 520 */ 521 if (ras_lookup(t, SCARG(uap, addr)) != (void *)-1) { 522 return (EACCES); 523 } 524 uio.uio_rw = UIO_WRITE; 525 break; 526 default: 527 error = EINVAL; 528 break; 529 } 530 if (error) 531 break; 532 error = proc_vmspace_getref(l->l_proc, &vm); 533 if (error) 534 break; 535 iov.iov_base = piod.piod_addr; 536 iov.iov_len = piod.piod_len; 537 uio.uio_iov = &iov; 538 uio.uio_iovcnt = 1; 539 uio.uio_offset = (off_t)(unsigned long)piod.piod_offs; 540 uio.uio_resid = piod.piod_len; 541 uio.uio_vmspace = vm; 542 543 error = process_domem(l, lt, &uio); 544 piod.piod_len -= uio.uio_resid; 545 (void) copyout(&piod, SCARG(uap, addr), sizeof(piod)); 546 uvmspace_free(vm); 547 break; 548 549 case PT_DUMPCORE: 550 if ((path = SCARG(uap, addr)) != NULL) { 551 char *dst; 552 len = SCARG(uap, data); 553 554 if (len < 0 || len >= MAXPATHLEN) { 555 error = EINVAL; 556 break; 557 } 558 dst = kmem_alloc(len + 1, KM_SLEEP); 559 if ((error = copyin(path, dst, len)) != 0) { 560 kmem_free(dst, len + 1); 561 break; 562 } 563 path = dst; 564 path[len] = '\0'; 565 } 566 error = (*coredump_vec)(lt, path); 567 if (path) 568 kmem_free(path, len + 1); 569 break; 570 571 #ifdef PT_STEP 572 case PT_STEP: 573 /* 574 * From the 4.4BSD PRM: 575 * "Execution continues as in request PT_CONTINUE; however 576 * as soon as possible after execution of at least one 577 * instruction, execution stops again. [ ... ]" 578 */ 579 #endif 580 case PT_CONTINUE: 581 case PT_SYSCALL: 582 case PT_DETACH: 583 if (req == PT_SYSCALL) { 584 if (!ISSET(t->p_slflag, PSL_SYSCALL)) { 585 SET(t->p_slflag, PSL_SYSCALL); 586 #ifdef __HAVE_SYSCALL_INTERN 587 (*t->p_emul->e_syscall_intern)(t); 588 #endif 589 } 590 } else { 591 if (ISSET(t->p_slflag, PSL_SYSCALL)) { 592 CLR(t->p_slflag, PSL_SYSCALL); 593 #ifdef __HAVE_SYSCALL_INTERN 594 (*t->p_emul->e_syscall_intern)(t); 595 #endif 596 } 597 } 598 p->p_trace_enabled = trace_is_enabled(p); 599 600 /* 601 * From the 4.4BSD PRM: 602 * "The data argument is taken as a signal number and the 603 * child's execution continues at location addr as if it 604 * incurred that signal. Normally the signal number will 605 * be either 0 to indicate that the signal that caused the 606 * stop should be ignored, or that value fetched out of 607 * the process's image indicating which signal caused 608 * the stop. If addr is (int *)1 then execution continues 609 * from where it stopped." 610 */ 611 612 /* Check that the data is a valid signal number or zero. */ 613 if (SCARG(uap, data) < 0 || SCARG(uap, data) >= NSIG) { 614 error = EINVAL; 615 break; 616 } 617 618 uvm_lwp_hold(lt); 619 620 /* If the address parameter is not (int *)1, set the pc. */ 621 if ((int *)SCARG(uap, addr) != (int *)1) 622 if ((error = process_set_pc(lt, SCARG(uap, addr))) != 0) { 623 uvm_lwp_rele(lt); 624 break; 625 } 626 627 #ifdef PT_STEP 628 /* 629 * Arrange for a single-step, if that's requested and possible. 630 */ 631 error = process_sstep(lt, req == PT_STEP); 632 if (error) { 633 uvm_lwp_rele(lt); 634 break; 635 } 636 #endif 637 638 uvm_lwp_rele(lt); 639 640 if (req == PT_DETACH) { 641 CLR(t->p_slflag, PSL_TRACED|PSL_FSTRACE|PSL_SYSCALL); 642 643 /* give process back to original parent or init */ 644 if (t->p_opptr != t->p_pptr) { 645 struct proc *pp = t->p_opptr; 646 proc_reparent(t, pp ? pp : initproc); 647 } 648 649 /* not being traced any more */ 650 t->p_opptr = NULL; 651 } 652 653 signo = SCARG(uap, data); 654 sendsig: 655 /* Finally, deliver the requested signal (or none). */ 656 if (t->p_stat == SSTOP) { 657 /* 658 * Unstop the process. If it needs to take a 659 * signal, make all efforts to ensure that at 660 * an LWP runs to see it. 661 */ 662 t->p_xstat = signo; 663 proc_unstop(t); 664 } else if (signo != 0) { 665 KSI_INIT_EMPTY(&ksi); 666 ksi.ksi_signo = signo; 667 kpsignal2(t, &ksi); 668 } 669 break; 670 671 case PT_KILL: 672 /* just send the process a KILL signal. */ 673 signo = SIGKILL; 674 goto sendsig; /* in PT_CONTINUE, above. */ 675 676 case PT_ATTACH: 677 /* 678 * Go ahead and set the trace flag. 679 * Save the old parent (it's reset in 680 * _DETACH, and also in kern_exit.c:wait4() 681 * Reparent the process so that the tracing 682 * proc gets to see all the action. 683 * Stop the target. 684 */ 685 t->p_opptr = t->p_pptr; 686 if (t->p_pptr != p) { 687 struct proc *parent = t->p_pptr; 688 689 if (parent->p_lock < t->p_lock) { 690 if (!mutex_tryenter(parent->p_lock)) { 691 mutex_exit(t->p_lock); 692 mutex_enter(parent->p_lock); 693 } 694 } else if (parent->p_lock > t->p_lock) { 695 mutex_enter(parent->p_lock); 696 } 697 parent->p_slflag |= PSL_CHTRACED; 698 proc_reparent(t, p); 699 if (parent->p_lock != t->p_lock) 700 mutex_exit(parent->p_lock); 701 } 702 SET(t->p_slflag, PSL_TRACED); 703 signo = SIGSTOP; 704 goto sendsig; 705 706 case PT_LWPINFO: 707 if (SCARG(uap, data) != sizeof(pl)) { 708 error = EINVAL; 709 break; 710 } 711 error = copyin(SCARG(uap, addr), &pl, sizeof(pl)); 712 if (error) 713 break; 714 tmp = pl.pl_lwpid; 715 lwp_delref(lt); 716 mutex_enter(t->p_lock); 717 if (tmp == 0) 718 lt = lwp_find_first(t); 719 else { 720 lt = lwp_find(t, tmp); 721 if (lt == NULL) { 722 mutex_exit(t->p_lock); 723 error = ESRCH; 724 break; 725 } 726 lt = LIST_NEXT(lt, l_sibling); 727 } 728 while (lt != NULL && !lwp_alive(lt)) 729 lt = LIST_NEXT(lt, l_sibling); 730 pl.pl_lwpid = 0; 731 pl.pl_event = 0; 732 if (lt) { 733 lwp_addref(lt); 734 pl.pl_lwpid = lt->l_lid; 735 if (lt->l_lid == t->p_sigctx.ps_lwp) 736 pl.pl_event = PL_EVENT_SIGNAL; 737 } 738 mutex_exit(t->p_lock); 739 740 error = copyout(&pl, SCARG(uap, addr), sizeof(pl)); 741 break; 742 743 #ifdef PT_SETREGS 744 case PT_SETREGS: 745 write = 1; 746 #endif 747 #ifdef PT_GETREGS 748 case PT_GETREGS: 749 /* write = 0 done above. */ 750 #endif 751 #if defined(PT_SETREGS) || defined(PT_GETREGS) 752 tmp = SCARG(uap, data); 753 if (tmp != 0 && t->p_nlwps > 1) { 754 lwp_delref(lt); 755 mutex_enter(t->p_lock); 756 lt = lwp_find(t, tmp); 757 if (lt == NULL) { 758 mutex_exit(t->p_lock); 759 error = ESRCH; 760 break; 761 } 762 lwp_addref(lt); 763 mutex_exit(t->p_lock); 764 } 765 if (!process_validregs(lt)) 766 error = EINVAL; 767 else { 768 error = proc_vmspace_getref(l->l_proc, &vm); 769 if (error) 770 break; 771 iov.iov_base = SCARG(uap, addr); 772 iov.iov_len = sizeof(struct reg); 773 uio.uio_iov = &iov; 774 uio.uio_iovcnt = 1; 775 uio.uio_offset = 0; 776 uio.uio_resid = sizeof(struct reg); 777 uio.uio_rw = write ? UIO_WRITE : UIO_READ; 778 uio.uio_vmspace = vm; 779 780 error = process_doregs(l, lt, &uio); 781 uvmspace_free(vm); 782 } 783 break; 784 #endif 785 786 #ifdef PT_SETFPREGS 787 case PT_SETFPREGS: 788 write = 1; 789 #endif 790 #ifdef PT_GETFPREGS 791 case PT_GETFPREGS: 792 /* write = 0 done above. */ 793 #endif 794 #if defined(PT_SETFPREGS) || defined(PT_GETFPREGS) 795 tmp = SCARG(uap, data); 796 if (tmp != 0 && t->p_nlwps > 1) { 797 lwp_delref(lt); 798 mutex_enter(t->p_lock); 799 lt = lwp_find(t, tmp); 800 if (lt == NULL) { 801 mutex_exit(t->p_lock); 802 error = ESRCH; 803 break; 804 } 805 lwp_addref(lt); 806 mutex_exit(t->p_lock); 807 } 808 if (!process_validfpregs(lt)) 809 error = EINVAL; 810 else { 811 error = proc_vmspace_getref(l->l_proc, &vm); 812 if (error) 813 break; 814 iov.iov_base = SCARG(uap, addr); 815 iov.iov_len = sizeof(struct fpreg); 816 uio.uio_iov = &iov; 817 uio.uio_iovcnt = 1; 818 uio.uio_offset = 0; 819 uio.uio_resid = sizeof(struct fpreg); 820 uio.uio_rw = write ? UIO_WRITE : UIO_READ; 821 uio.uio_vmspace = vm; 822 823 error = process_dofpregs(l, lt, &uio); 824 uvmspace_free(vm); 825 } 826 break; 827 #endif 828 829 #ifdef __HAVE_PTRACE_MACHDEP 830 PTRACE_MACHDEP_REQUEST_CASES 831 error = ptrace_machdep_dorequest(l, lt, 832 req, SCARG(uap, addr), SCARG(uap, data)); 833 break; 834 #endif 835 } 836 837 if (pheld) { 838 mutex_exit(t->p_lock); 839 mutex_exit(proc_lock); 840 } 841 if (lt != NULL) 842 lwp_delref(lt); 843 rw_exit(&t->p_reflock); 844 845 return error; 846 } 847 848 int 849 process_doregs(struct lwp *curl /*tracer*/, 850 struct lwp *l /*traced*/, 851 struct uio *uio) 852 { 853 #if defined(PT_GETREGS) || defined(PT_SETREGS) 854 int error; 855 struct reg r; 856 char *kv; 857 int kl; 858 859 if (uio->uio_offset < 0 || uio->uio_offset > (off_t)sizeof(r)) 860 return EINVAL; 861 862 kl = sizeof(r); 863 kv = (char *)&r; 864 865 kv += uio->uio_offset; 866 kl -= uio->uio_offset; 867 if ((size_t)kl > uio->uio_resid) 868 kl = uio->uio_resid; 869 870 uvm_lwp_hold(l); 871 872 error = process_read_regs(l, &r); 873 if (error == 0) 874 error = uiomove(kv, kl, uio); 875 if (error == 0 && uio->uio_rw == UIO_WRITE) { 876 if (l->l_stat != LSSTOP) 877 error = EBUSY; 878 else 879 error = process_write_regs(l, &r); 880 } 881 882 uvm_lwp_rele(l); 883 884 uio->uio_offset = 0; 885 return (error); 886 #else 887 return (EINVAL); 888 #endif 889 } 890 891 int 892 process_validregs(struct lwp *l) 893 { 894 895 #if defined(PT_SETREGS) || defined(PT_GETREGS) 896 return ((l->l_flag & LW_SYSTEM) == 0); 897 #else 898 return (0); 899 #endif 900 } 901 902 int 903 process_dofpregs(struct lwp *curl /*tracer*/, 904 struct lwp *l /*traced*/, 905 struct uio *uio) 906 { 907 #if defined(PT_GETFPREGS) || defined(PT_SETFPREGS) 908 int error; 909 struct fpreg r; 910 char *kv; 911 int kl; 912 913 if (uio->uio_offset < 0 || uio->uio_offset > (off_t)sizeof(r)) 914 return EINVAL; 915 916 kl = sizeof(r); 917 kv = (char *)&r; 918 919 kv += uio->uio_offset; 920 kl -= uio->uio_offset; 921 if ((size_t)kl > uio->uio_resid) 922 kl = uio->uio_resid; 923 924 uvm_lwp_hold(l); 925 926 error = process_read_fpregs(l, &r); 927 if (error == 0) 928 error = uiomove(kv, kl, uio); 929 if (error == 0 && uio->uio_rw == UIO_WRITE) { 930 if (l->l_stat != LSSTOP) 931 error = EBUSY; 932 else 933 error = process_write_fpregs(l, &r); 934 } 935 936 uvm_lwp_rele(l); 937 938 uio->uio_offset = 0; 939 return (error); 940 #else 941 return (EINVAL); 942 #endif 943 } 944 945 int 946 process_validfpregs(struct lwp *l) 947 { 948 949 #if defined(PT_SETFPREGS) || defined(PT_GETFPREGS) 950 return ((l->l_flag & LW_SYSTEM) == 0); 951 #else 952 return (0); 953 #endif 954 } 955 #endif /* PTRACE */ 956 957 #if defined(KTRACE) || defined(PTRACE) 958 int 959 process_domem(struct lwp *curl /*tracer*/, 960 struct lwp *l /*traced*/, 961 struct uio *uio) 962 { 963 struct proc *p = l->l_proc; /* traced */ 964 struct vmspace *vm; 965 int error; 966 967 size_t len; 968 #ifdef PMAP_NEED_PROCWR 969 vaddr_t addr; 970 #endif 971 972 error = 0; 973 len = uio->uio_resid; 974 975 if (len == 0) 976 return (0); 977 978 #ifdef PMAP_NEED_PROCWR 979 addr = uio->uio_offset; 980 #endif 981 982 vm = p->p_vmspace; 983 984 mutex_enter(&vm->vm_map.misc_lock); 985 if ((l->l_flag & LW_WEXIT) || vm->vm_refcnt < 1) 986 error = EFAULT; 987 if (error == 0) 988 p->p_vmspace->vm_refcnt++; /* XXX */ 989 mutex_exit(&vm->vm_map.misc_lock); 990 if (error != 0) 991 return (error); 992 error = uvm_io(&vm->vm_map, uio); 993 uvmspace_free(vm); 994 995 #ifdef PMAP_NEED_PROCWR 996 if (error == 0 && uio->uio_rw == UIO_WRITE) 997 pmap_procwr(p, addr, len); 998 #endif 999 return (error); 1000 } 1001 #endif /* KTRACE || PTRACE */ 1002 1003 #if defined(KTRACE) || defined(PTRACE) 1004 void 1005 process_stoptrace(void) 1006 { 1007 struct lwp *l = curlwp; 1008 struct proc *p = l->l_proc, *pp; 1009 1010 mutex_enter(proc_lock); 1011 mutex_enter(p->p_lock); 1012 pp = p->p_pptr; 1013 if (pp->p_pid == 1) { 1014 CLR(p->p_slflag, PSL_SYSCALL); /* XXXSMP */ 1015 mutex_exit(p->p_lock); 1016 mutex_exit(proc_lock); 1017 return; 1018 } 1019 1020 p->p_xstat = SIGTRAP; 1021 proc_stop(p, 1, SIGSTOP); 1022 mutex_exit(proc_lock); 1023 1024 /* 1025 * Call issignal() once only, to have it take care of the 1026 * pending stop. Signal processing will take place as usual 1027 * from userret(). 1028 */ 1029 KERNEL_UNLOCK_ALL(l, &l->l_biglocks); 1030 (void)issignal(l); 1031 mutex_exit(p->p_lock); 1032 KERNEL_LOCK(l->l_biglocks, l); 1033 } 1034 #endif /* KTRACE || PTRACE */ 1035