1 /*- 2 * Copyright (c) 1994, Sean Eric Fagan 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by Sean Eric Fagan. 16 * 4. The name of the author may not be used to endorse or promote products 17 * derived from this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __FBSDID("$FreeBSD$"); 34 35 #include "opt_compat.h" 36 37 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/lock.h> 40 #include <sys/mutex.h> 41 #include <sys/syscallsubr.h> 42 #include <sys/sysproto.h> 43 #include <sys/proc.h> 44 #include <sys/vnode.h> 45 #include <sys/ptrace.h> 46 #include <sys/sx.h> 47 #include <sys/malloc.h> 48 #include <sys/signalvar.h> 49 50 #include <machine/reg.h> 51 52 #include <security/audit/audit.h> 53 54 #include <vm/vm.h> 55 #include <vm/pmap.h> 56 #include <vm/vm_extern.h> 57 #include <vm/vm_map.h> 58 #include <vm/vm_kern.h> 59 #include <vm/vm_object.h> 60 #include <vm/vm_page.h> 61 62 #ifdef COMPAT_IA32 63 #include <sys/procfs.h> 64 #include <machine/fpu.h> 65 #include <compat/ia32/ia32_reg.h> 66 67 extern struct sysentvec ia32_freebsd_sysvec; 68 69 struct ptrace_io_desc32 { 70 int piod_op; 71 u_int32_t piod_offs; 72 u_int32_t piod_addr; 73 u_int32_t piod_len; 74 }; 75 #endif 76 77 /* 78 * Functions implemented using PROC_ACTION(): 79 * 80 * proc_read_regs(proc, regs) 81 * Get the current user-visible register set from the process 82 * and copy it into the regs structure (<machine/reg.h>). 83 * The process is stopped at the time read_regs is called. 84 * 85 * proc_write_regs(proc, regs) 86 * Update the current register set from the passed in regs 87 * structure. Take care to avoid clobbering special CPU 88 * registers or privileged bits in the PSL. 89 * Depending on the architecture this may have fix-up work to do, 90 * especially if the IAR or PCW are modified. 91 * The process is stopped at the time write_regs is called. 92 * 93 * proc_read_fpregs, proc_write_fpregs 94 * deal with the floating point register set, otherwise as above. 95 * 96 * proc_read_dbregs, proc_write_dbregs 97 * deal with the processor debug register set, otherwise as above. 98 * 99 * proc_sstep(proc) 100 * Arrange for the process to trap after executing a single instruction. 101 */ 102 103 #define PROC_ACTION(action) do { \ 104 int error; \ 105 \ 106 PROC_LOCK_ASSERT(td->td_proc, MA_OWNED); \ 107 if ((td->td_proc->p_sflag & PS_INMEM) == 0) \ 108 error = EIO; \ 109 else \ 110 error = (action); \ 111 return (error); \ 112 } while(0) 113 114 int 115 proc_read_regs(struct thread *td, struct reg *regs) 116 { 117 118 PROC_ACTION(fill_regs(td, regs)); 119 } 120 121 int 122 proc_write_regs(struct thread *td, struct reg *regs) 123 { 124 125 PROC_ACTION(set_regs(td, regs)); 126 } 127 128 int 129 proc_read_dbregs(struct thread *td, struct dbreg *dbregs) 130 { 131 132 PROC_ACTION(fill_dbregs(td, dbregs)); 133 } 134 135 int 136 proc_write_dbregs(struct thread *td, struct dbreg *dbregs) 137 { 138 139 PROC_ACTION(set_dbregs(td, dbregs)); 140 } 141 142 /* 143 * Ptrace doesn't support fpregs at all, and there are no security holes 144 * or translations for fpregs, so we can just copy them. 145 */ 146 int 147 proc_read_fpregs(struct thread *td, struct fpreg *fpregs) 148 { 149 150 PROC_ACTION(fill_fpregs(td, fpregs)); 151 } 152 153 int 154 proc_write_fpregs(struct thread *td, struct fpreg *fpregs) 155 { 156 157 PROC_ACTION(set_fpregs(td, fpregs)); 158 } 159 160 #ifdef COMPAT_IA32 161 /* For 32 bit binaries, we need to expose the 32 bit regs layouts. */ 162 int 163 proc_read_regs32(struct thread *td, struct reg32 *regs32) 164 { 165 166 PROC_ACTION(fill_regs32(td, regs32)); 167 } 168 169 int 170 proc_write_regs32(struct thread *td, struct reg32 *regs32) 171 { 172 173 PROC_ACTION(set_regs32(td, regs32)); 174 } 175 176 int 177 proc_read_dbregs32(struct thread *td, struct dbreg32 *dbregs32) 178 { 179 180 PROC_ACTION(fill_dbregs32(td, dbregs32)); 181 } 182 183 int 184 proc_write_dbregs32(struct thread *td, struct dbreg32 *dbregs32) 185 { 186 187 PROC_ACTION(set_dbregs32(td, dbregs32)); 188 } 189 190 int 191 proc_read_fpregs32(struct thread *td, struct fpreg32 *fpregs32) 192 { 193 194 PROC_ACTION(fill_fpregs32(td, fpregs32)); 195 } 196 197 int 198 proc_write_fpregs32(struct thread *td, struct fpreg32 *fpregs32) 199 { 200 201 PROC_ACTION(set_fpregs32(td, fpregs32)); 202 } 203 #endif 204 205 int 206 proc_sstep(struct thread *td) 207 { 208 209 PROC_ACTION(ptrace_single_step(td)); 210 } 211 212 int 213 proc_rwmem(struct proc *p, struct uio *uio) 214 { 215 struct vmspace *vm; 216 vm_map_t map; 217 vm_object_t backing_object, object = NULL; 218 vm_offset_t pageno = 0; /* page number */ 219 vm_prot_t reqprot; 220 int error, refcnt, writing; 221 222 /* 223 * if the vmspace is in the midst of being deallocated or the 224 * process is exiting, don't try to grab anything. The page table 225 * usage in that process can be messed up. 226 */ 227 vm = p->p_vmspace; 228 if ((p->p_flag & P_WEXIT)) 229 return (EFAULT); 230 do { 231 if ((refcnt = vm->vm_refcnt) < 1) 232 return (EFAULT); 233 } while (!atomic_cmpset_int(&vm->vm_refcnt, refcnt, refcnt + 1)); 234 235 /* 236 * The map we want... 237 */ 238 map = &vm->vm_map; 239 240 writing = uio->uio_rw == UIO_WRITE; 241 reqprot = writing ? (VM_PROT_WRITE | VM_PROT_OVERRIDE_WRITE) : 242 VM_PROT_READ; 243 244 /* 245 * Only map in one page at a time. We don't have to, but it 246 * makes things easier. This way is trivial - right? 247 */ 248 do { 249 vm_map_t tmap; 250 vm_offset_t uva; 251 int page_offset; /* offset into page */ 252 vm_map_entry_t out_entry; 253 vm_prot_t out_prot; 254 boolean_t wired; 255 vm_pindex_t pindex; 256 u_int len; 257 vm_page_t m; 258 259 object = NULL; 260 261 uva = (vm_offset_t)uio->uio_offset; 262 263 /* 264 * Get the page number of this segment. 265 */ 266 pageno = trunc_page(uva); 267 page_offset = uva - pageno; 268 269 /* 270 * How many bytes to copy 271 */ 272 len = min(PAGE_SIZE - page_offset, uio->uio_resid); 273 274 /* 275 * Fault the page on behalf of the process 276 */ 277 error = vm_fault(map, pageno, reqprot, VM_FAULT_NORMAL); 278 if (error) { 279 error = EFAULT; 280 break; 281 } 282 283 /* 284 * Now we need to get the page. out_entry, out_prot, wired, 285 * and single_use aren't used. One would think the vm code 286 * would be a *bit* nicer... We use tmap because 287 * vm_map_lookup() can change the map argument. 288 */ 289 tmap = map; 290 error = vm_map_lookup(&tmap, pageno, reqprot, &out_entry, 291 &object, &pindex, &out_prot, &wired); 292 if (error) { 293 error = EFAULT; 294 break; 295 } 296 VM_OBJECT_LOCK(object); 297 while ((m = vm_page_lookup(object, pindex)) == NULL && 298 !writing && 299 (backing_object = object->backing_object) != NULL) { 300 /* 301 * Allow fallback to backing objects if we are reading. 302 */ 303 VM_OBJECT_LOCK(backing_object); 304 pindex += OFF_TO_IDX(object->backing_object_offset); 305 VM_OBJECT_UNLOCK(object); 306 object = backing_object; 307 } 308 VM_OBJECT_UNLOCK(object); 309 if (m == NULL) { 310 vm_map_lookup_done(tmap, out_entry); 311 error = EFAULT; 312 break; 313 } 314 315 /* 316 * Hold the page in memory. 317 */ 318 vm_page_lock_queues(); 319 vm_page_hold(m); 320 vm_page_unlock_queues(); 321 322 /* 323 * We're done with tmap now. 324 */ 325 vm_map_lookup_done(tmap, out_entry); 326 327 /* 328 * Now do the i/o move. 329 */ 330 error = uiomove_fromphys(&m, page_offset, len, uio); 331 332 /* 333 * Release the page. 334 */ 335 vm_page_lock_queues(); 336 vm_page_unhold(m); 337 vm_page_unlock_queues(); 338 339 } while (error == 0 && uio->uio_resid > 0); 340 341 vmspace_free(vm); 342 return (error); 343 } 344 345 /* 346 * Process debugging system call. 347 */ 348 #ifndef _SYS_SYSPROTO_H_ 349 struct ptrace_args { 350 int req; 351 pid_t pid; 352 caddr_t addr; 353 int data; 354 }; 355 #endif 356 357 #ifdef COMPAT_IA32 358 /* 359 * This CPP subterfuge is to try and reduce the number of ifdefs in 360 * the body of the code. 361 * COPYIN(uap->addr, &r.reg, sizeof r.reg); 362 * becomes either: 363 * copyin(uap->addr, &r.reg, sizeof r.reg); 364 * or 365 * copyin(uap->addr, &r.reg32, sizeof r.reg32); 366 * .. except this is done at runtime. 367 */ 368 #define COPYIN(u, k, s) wrap32 ? \ 369 copyin(u, k ## 32, s ## 32) : \ 370 copyin(u, k, s) 371 #define COPYOUT(k, u, s) wrap32 ? \ 372 copyout(k ## 32, u, s ## 32) : \ 373 copyout(k, u, s) 374 #else 375 #define COPYIN(u, k, s) copyin(u, k, s) 376 #define COPYOUT(k, u, s) copyout(k, u, s) 377 #endif 378 /* 379 * MPSAFE 380 */ 381 int 382 ptrace(struct thread *td, struct ptrace_args *uap) 383 { 384 /* 385 * XXX this obfuscation is to reduce stack usage, but the register 386 * structs may be too large to put on the stack anyway. 387 */ 388 union { 389 struct ptrace_io_desc piod; 390 struct ptrace_lwpinfo pl; 391 struct dbreg dbreg; 392 struct fpreg fpreg; 393 struct reg reg; 394 #ifdef COMPAT_IA32 395 struct dbreg32 dbreg32; 396 struct fpreg32 fpreg32; 397 struct reg32 reg32; 398 struct ptrace_io_desc32 piod32; 399 #endif 400 } r; 401 void *addr; 402 int error = 0; 403 #ifdef COMPAT_IA32 404 int wrap32 = 0; 405 406 if (td->td_proc->p_sysent == &ia32_freebsd_sysvec) 407 wrap32 = 1; 408 #endif 409 AUDIT_ARG(pid, uap->pid); 410 AUDIT_ARG(cmd, uap->req); 411 AUDIT_ARG(addr, uap->addr); 412 AUDIT_ARG(value, uap->data); 413 addr = &r; 414 switch (uap->req) { 415 case PT_GETREGS: 416 case PT_GETFPREGS: 417 case PT_GETDBREGS: 418 case PT_LWPINFO: 419 break; 420 case PT_SETREGS: 421 error = COPYIN(uap->addr, &r.reg, sizeof r.reg); 422 break; 423 case PT_SETFPREGS: 424 error = COPYIN(uap->addr, &r.fpreg, sizeof r.fpreg); 425 break; 426 case PT_SETDBREGS: 427 error = COPYIN(uap->addr, &r.dbreg, sizeof r.dbreg); 428 break; 429 case PT_IO: 430 error = COPYIN(uap->addr, &r.piod, sizeof r.piod); 431 break; 432 default: 433 addr = uap->addr; 434 break; 435 } 436 if (error) 437 return (error); 438 439 error = kern_ptrace(td, uap->req, uap->pid, addr, uap->data); 440 if (error) 441 return (error); 442 443 switch (uap->req) { 444 case PT_IO: 445 error = COPYOUT(&r.piod, uap->addr, sizeof r.piod); 446 break; 447 case PT_GETREGS: 448 error = COPYOUT(&r.reg, uap->addr, sizeof r.reg); 449 break; 450 case PT_GETFPREGS: 451 error = COPYOUT(&r.fpreg, uap->addr, sizeof r.fpreg); 452 break; 453 case PT_GETDBREGS: 454 error = COPYOUT(&r.dbreg, uap->addr, sizeof r.dbreg); 455 break; 456 case PT_LWPINFO: 457 error = copyout(&r.pl, uap->addr, uap->data); 458 break; 459 } 460 461 return (error); 462 } 463 #undef COPYIN 464 #undef COPYOUT 465 466 #ifdef COMPAT_IA32 467 /* 468 * PROC_READ(regs, td2, addr); 469 * becomes either: 470 * proc_read_regs(td2, addr); 471 * or 472 * proc_read_regs32(td2, addr); 473 * .. except this is done at runtime. There is an additional 474 * complication in that PROC_WRITE disallows 32 bit consumers 475 * from writing to 64 bit address space targets. 476 */ 477 #define PROC_READ(w, t, a) wrap32 ? \ 478 proc_read_ ## w ## 32(t, a) : \ 479 proc_read_ ## w (t, a) 480 #define PROC_WRITE(w, t, a) wrap32 ? \ 481 (safe ? proc_write_ ## w ## 32(t, a) : EINVAL ) : \ 482 proc_write_ ## w (t, a) 483 #else 484 #define PROC_READ(w, t, a) proc_read_ ## w (t, a) 485 #define PROC_WRITE(w, t, a) proc_write_ ## w (t, a) 486 #endif 487 488 int 489 kern_ptrace(struct thread *td, int req, pid_t pid, void *addr, int data) 490 { 491 struct iovec iov; 492 struct uio uio; 493 struct proc *curp, *p, *pp; 494 struct thread *td2 = NULL; 495 struct ptrace_io_desc *piod = NULL; 496 struct ptrace_lwpinfo *pl; 497 int error, write, tmp, num; 498 int proctree_locked = 0; 499 lwpid_t tid = 0, *buf; 500 #ifdef COMPAT_IA32 501 int wrap32 = 0, safe = 0; 502 struct ptrace_io_desc32 *piod32 = NULL; 503 #endif 504 505 curp = td->td_proc; 506 507 /* Lock proctree before locking the process. */ 508 switch (req) { 509 case PT_TRACE_ME: 510 case PT_ATTACH: 511 case PT_STEP: 512 case PT_CONTINUE: 513 case PT_TO_SCE: 514 case PT_TO_SCX: 515 case PT_SYSCALL: 516 case PT_DETACH: 517 sx_xlock(&proctree_lock); 518 proctree_locked = 1; 519 break; 520 default: 521 break; 522 } 523 524 write = 0; 525 if (req == PT_TRACE_ME) { 526 p = td->td_proc; 527 PROC_LOCK(p); 528 } else { 529 if (pid <= PID_MAX) { 530 if ((p = pfind(pid)) == NULL) { 531 if (proctree_locked) 532 sx_xunlock(&proctree_lock); 533 return (ESRCH); 534 } 535 } else { 536 /* this is slow, should be optimized */ 537 sx_slock(&allproc_lock); 538 FOREACH_PROC_IN_SYSTEM(p) { 539 PROC_LOCK(p); 540 mtx_lock_spin(&sched_lock); 541 FOREACH_THREAD_IN_PROC(p, td2) { 542 if (td2->td_tid == pid) 543 break; 544 } 545 mtx_unlock_spin(&sched_lock); 546 if (td2 != NULL) 547 break; /* proc lock held */ 548 PROC_UNLOCK(p); 549 } 550 sx_sunlock(&allproc_lock); 551 if (p == NULL) { 552 if (proctree_locked) 553 sx_xunlock(&proctree_lock); 554 return (ESRCH); 555 } 556 tid = pid; 557 pid = p->p_pid; 558 } 559 } 560 AUDIT_ARG(process, p); 561 if ((error = p_cansee(td, p)) != 0) 562 goto fail; 563 564 if ((error = p_candebug(td, p)) != 0) 565 goto fail; 566 567 /* 568 * System processes can't be debugged. 569 */ 570 if ((p->p_flag & P_SYSTEM) != 0) { 571 error = EINVAL; 572 goto fail; 573 } 574 575 if (tid == 0) { 576 if ((p->p_flag & P_STOPPED_TRACE) != 0) { 577 KASSERT(p->p_xthread != NULL, ("NULL p_xthread")); 578 td2 = p->p_xthread; 579 } else { 580 td2 = FIRST_THREAD_IN_PROC(p); 581 } 582 tid = td2->td_tid; 583 } 584 585 #ifdef COMPAT_IA32 586 /* 587 * Test if we're a 32 bit client and what the target is. 588 * Set the wrap controls accordingly. 589 */ 590 if (td->td_proc->p_sysent == &ia32_freebsd_sysvec) { 591 if (td2->td_proc->p_sysent == &ia32_freebsd_sysvec) 592 safe = 1; 593 wrap32 = 1; 594 } 595 #endif 596 /* 597 * Permissions check 598 */ 599 switch (req) { 600 case PT_TRACE_ME: 601 /* Always legal. */ 602 break; 603 604 case PT_ATTACH: 605 /* Self */ 606 if (p->p_pid == td->td_proc->p_pid) { 607 error = EINVAL; 608 goto fail; 609 } 610 611 /* Already traced */ 612 if (p->p_flag & P_TRACED) { 613 error = EBUSY; 614 goto fail; 615 } 616 617 /* Can't trace an ancestor if you're being traced. */ 618 if (curp->p_flag & P_TRACED) { 619 for (pp = curp->p_pptr; pp != NULL; pp = pp->p_pptr) { 620 if (pp == p) { 621 error = EINVAL; 622 goto fail; 623 } 624 } 625 } 626 627 628 /* OK */ 629 break; 630 631 case PT_CLEARSTEP: 632 /* Allow thread to clear single step for itself */ 633 if (td->td_tid == tid) 634 break; 635 636 /* FALLTHROUGH */ 637 default: 638 /* not being traced... */ 639 if ((p->p_flag & P_TRACED) == 0) { 640 error = EPERM; 641 goto fail; 642 } 643 644 /* not being traced by YOU */ 645 if (p->p_pptr != td->td_proc) { 646 error = EBUSY; 647 goto fail; 648 } 649 650 /* not currently stopped */ 651 if ((p->p_flag & (P_STOPPED_SIG | P_STOPPED_TRACE)) == 0 || 652 p->p_suspcount != p->p_numthreads || 653 (p->p_flag & P_WAITED) == 0) { 654 error = EBUSY; 655 goto fail; 656 } 657 658 if ((p->p_flag & P_STOPPED_TRACE) == 0) { 659 static int count = 0; 660 if (count++ == 0) 661 printf("P_STOPPED_TRACE not set.\n"); 662 } 663 664 /* OK */ 665 break; 666 } 667 668 #ifdef FIX_SSTEP 669 /* 670 * Single step fixup ala procfs 671 */ 672 FIX_SSTEP(td2); /* XXXKSE */ 673 #endif 674 675 /* 676 * Actually do the requests 677 */ 678 679 td->td_retval[0] = 0; 680 681 switch (req) { 682 case PT_TRACE_ME: 683 /* set my trace flag and "owner" so it can read/write me */ 684 p->p_flag |= P_TRACED; 685 p->p_oppid = p->p_pptr->p_pid; 686 PROC_UNLOCK(p); 687 sx_xunlock(&proctree_lock); 688 return (0); 689 690 case PT_ATTACH: 691 /* security check done above */ 692 p->p_flag |= P_TRACED; 693 p->p_oppid = p->p_pptr->p_pid; 694 if (p->p_pptr != td->td_proc) { 695 PROC_LOCK(p->p_pptr); 696 sigqueue_take(p->p_ksi); 697 PROC_UNLOCK(p->p_pptr); 698 proc_reparent(p, td->td_proc); 699 } 700 data = SIGSTOP; 701 goto sendsig; /* in PT_CONTINUE below */ 702 703 case PT_CLEARSTEP: 704 _PHOLD(p); 705 error = ptrace_clear_single_step(td2); 706 _PRELE(p); 707 if (error) 708 goto fail; 709 PROC_UNLOCK(p); 710 return (0); 711 712 case PT_SETSTEP: 713 _PHOLD(p); 714 error = ptrace_single_step(td2); 715 _PRELE(p); 716 if (error) 717 goto fail; 718 PROC_UNLOCK(p); 719 return (0); 720 721 case PT_SUSPEND: 722 _PHOLD(p); 723 mtx_lock_spin(&sched_lock); 724 td2->td_flags |= TDF_DBSUSPEND; 725 mtx_unlock_spin(&sched_lock); 726 _PRELE(p); 727 PROC_UNLOCK(p); 728 return (0); 729 730 case PT_RESUME: 731 _PHOLD(p); 732 mtx_lock_spin(&sched_lock); 733 td2->td_flags &= ~TDF_DBSUSPEND; 734 mtx_unlock_spin(&sched_lock); 735 _PRELE(p); 736 PROC_UNLOCK(p); 737 return (0); 738 739 case PT_STEP: 740 case PT_CONTINUE: 741 case PT_TO_SCE: 742 case PT_TO_SCX: 743 case PT_SYSCALL: 744 case PT_DETACH: 745 /* Zero means do not send any signal */ 746 if (data < 0 || data > _SIG_MAXSIG) { 747 error = EINVAL; 748 goto fail; 749 } 750 751 _PHOLD(p); 752 753 switch (req) { 754 case PT_STEP: 755 PROC_UNLOCK(p); 756 error = ptrace_single_step(td2); 757 if (error) { 758 PRELE(p); 759 goto fail_noproc; 760 } 761 PROC_LOCK(p); 762 break; 763 case PT_TO_SCE: 764 p->p_stops |= S_PT_SCE; 765 break; 766 case PT_TO_SCX: 767 p->p_stops |= S_PT_SCX; 768 break; 769 case PT_SYSCALL: 770 p->p_stops |= S_PT_SCE | S_PT_SCX; 771 break; 772 } 773 774 if (addr != (void *)1) { 775 PROC_UNLOCK(p); 776 error = ptrace_set_pc(td2, (u_long)(uintfptr_t)addr); 777 if (error) { 778 PRELE(p); 779 goto fail_noproc; 780 } 781 PROC_LOCK(p); 782 } 783 _PRELE(p); 784 785 if (req == PT_DETACH) { 786 /* reset process parent */ 787 if (p->p_oppid != p->p_pptr->p_pid) { 788 struct proc *pp; 789 790 PROC_LOCK(p->p_pptr); 791 sigqueue_take(p->p_ksi); 792 PROC_UNLOCK(p->p_pptr); 793 794 PROC_UNLOCK(p); 795 pp = pfind(p->p_oppid); 796 if (pp == NULL) 797 pp = initproc; 798 else 799 PROC_UNLOCK(pp); 800 PROC_LOCK(p); 801 proc_reparent(p, pp); 802 if (pp == initproc) 803 p->p_sigparent = SIGCHLD; 804 } 805 p->p_flag &= ~(P_TRACED | P_WAITED); 806 p->p_oppid = 0; 807 808 /* should we send SIGCHLD? */ 809 /* childproc_continued(p); */ 810 } 811 812 sendsig: 813 if (proctree_locked) 814 sx_xunlock(&proctree_lock); 815 /* deliver or queue signal */ 816 mtx_lock_spin(&sched_lock); 817 td2->td_flags &= ~TDF_XSIG; 818 mtx_unlock_spin(&sched_lock); 819 td2->td_xsig = data; 820 p->p_xstat = data; 821 p->p_xthread = NULL; 822 if ((p->p_flag & (P_STOPPED_SIG | P_STOPPED_TRACE)) != 0) { 823 mtx_lock_spin(&sched_lock); 824 if (req == PT_DETACH) { 825 struct thread *td3; 826 FOREACH_THREAD_IN_PROC(p, td3) 827 td3->td_flags &= ~TDF_DBSUSPEND; 828 } 829 /* 830 * unsuspend all threads, to not let a thread run, 831 * you should use PT_SUSPEND to suspend it before 832 * continuing process. 833 */ 834 mtx_unlock_spin(&sched_lock); 835 thread_continued(p); 836 p->p_flag &= ~(P_STOPPED_TRACE|P_STOPPED_SIG|P_WAITED); 837 mtx_lock_spin(&sched_lock); 838 thread_unsuspend(p); 839 mtx_unlock_spin(&sched_lock); 840 } 841 842 if (data) 843 psignal(p, data); 844 845 PROC_UNLOCK(p); 846 return (0); 847 848 case PT_WRITE_I: 849 case PT_WRITE_D: 850 write = 1; 851 /* FALLTHROUGH */ 852 case PT_READ_I: 853 case PT_READ_D: 854 PROC_UNLOCK(p); 855 tmp = 0; 856 /* write = 0 set above */ 857 iov.iov_base = write ? (caddr_t)&data : (caddr_t)&tmp; 858 iov.iov_len = sizeof(int); 859 uio.uio_iov = &iov; 860 uio.uio_iovcnt = 1; 861 uio.uio_offset = (off_t)(uintptr_t)addr; 862 uio.uio_resid = sizeof(int); 863 uio.uio_segflg = UIO_SYSSPACE; /* i.e.: the uap */ 864 uio.uio_rw = write ? UIO_WRITE : UIO_READ; 865 uio.uio_td = td; 866 error = proc_rwmem(p, &uio); 867 if (uio.uio_resid != 0) { 868 /* 869 * XXX proc_rwmem() doesn't currently return ENOSPC, 870 * so I think write() can bogusly return 0. 871 * XXX what happens for short writes? We don't want 872 * to write partial data. 873 * XXX proc_rwmem() returns EPERM for other invalid 874 * addresses. Convert this to EINVAL. Does this 875 * clobber returns of EPERM for other reasons? 876 */ 877 if (error == 0 || error == ENOSPC || error == EPERM) 878 error = EINVAL; /* EOF */ 879 } 880 if (!write) 881 td->td_retval[0] = tmp; 882 return (error); 883 884 case PT_IO: 885 PROC_UNLOCK(p); 886 #ifdef COMPAT_IA32 887 if (wrap32) { 888 piod32 = addr; 889 iov.iov_base = (void *)(uintptr_t)piod32->piod_addr; 890 iov.iov_len = piod32->piod_len; 891 uio.uio_offset = (off_t)(uintptr_t)piod32->piod_offs; 892 uio.uio_resid = piod32->piod_len; 893 } else 894 #endif 895 { 896 piod = addr; 897 iov.iov_base = piod->piod_addr; 898 iov.iov_len = piod->piod_len; 899 uio.uio_offset = (off_t)(uintptr_t)piod->piod_offs; 900 uio.uio_resid = piod->piod_len; 901 } 902 uio.uio_iov = &iov; 903 uio.uio_iovcnt = 1; 904 uio.uio_segflg = UIO_USERSPACE; 905 uio.uio_td = td; 906 #ifdef COMPAT_IA32 907 tmp = wrap32 ? piod32->piod_op : piod->piod_op; 908 #else 909 tmp = piod->piod_op; 910 #endif 911 switch (tmp) { 912 case PIOD_READ_D: 913 case PIOD_READ_I: 914 uio.uio_rw = UIO_READ; 915 break; 916 case PIOD_WRITE_D: 917 case PIOD_WRITE_I: 918 uio.uio_rw = UIO_WRITE; 919 break; 920 default: 921 return (EINVAL); 922 } 923 error = proc_rwmem(p, &uio); 924 #ifdef COMPAT_IA32 925 if (wrap32) 926 piod32->piod_len -= uio.uio_resid; 927 else 928 #endif 929 piod->piod_len -= uio.uio_resid; 930 return (error); 931 932 case PT_KILL: 933 data = SIGKILL; 934 goto sendsig; /* in PT_CONTINUE above */ 935 936 case PT_SETREGS: 937 _PHOLD(p); 938 error = PROC_WRITE(regs, td2, addr); 939 _PRELE(p); 940 PROC_UNLOCK(p); 941 return (error); 942 943 case PT_GETREGS: 944 _PHOLD(p); 945 error = PROC_READ(regs, td2, addr); 946 _PRELE(p); 947 PROC_UNLOCK(p); 948 return (error); 949 950 case PT_SETFPREGS: 951 _PHOLD(p); 952 error = PROC_WRITE(fpregs, td2, addr); 953 _PRELE(p); 954 PROC_UNLOCK(p); 955 return (error); 956 957 case PT_GETFPREGS: 958 _PHOLD(p); 959 error = PROC_READ(fpregs, td2, addr); 960 _PRELE(p); 961 PROC_UNLOCK(p); 962 return (error); 963 964 case PT_SETDBREGS: 965 _PHOLD(p); 966 error = PROC_WRITE(dbregs, td2, addr); 967 _PRELE(p); 968 PROC_UNLOCK(p); 969 return (error); 970 971 case PT_GETDBREGS: 972 _PHOLD(p); 973 error = PROC_READ(dbregs, td2, addr); 974 _PRELE(p); 975 PROC_UNLOCK(p); 976 return (error); 977 978 case PT_LWPINFO: 979 if (data == 0 || data > sizeof(*pl)) 980 return (EINVAL); 981 pl = addr; 982 _PHOLD(p); 983 pl->pl_lwpid = td2->td_tid; 984 if (td2->td_flags & TDF_XSIG) 985 pl->pl_event = PL_EVENT_SIGNAL; 986 else 987 pl->pl_event = 0; 988 if (td2->td_pflags & TDP_SA) { 989 pl->pl_flags = PL_FLAG_SA; 990 if (td2->td_upcall && !TD_CAN_UNBIND(td2)) 991 pl->pl_flags |= PL_FLAG_BOUND; 992 } else { 993 pl->pl_flags = 0; 994 } 995 pl->pl_sigmask = td2->td_sigmask; 996 pl->pl_siglist = td2->td_siglist; 997 _PRELE(p); 998 PROC_UNLOCK(p); 999 return (0); 1000 1001 case PT_GETNUMLWPS: 1002 td->td_retval[0] = p->p_numthreads; 1003 PROC_UNLOCK(p); 1004 return (0); 1005 1006 case PT_GETLWPLIST: 1007 if (data <= 0) { 1008 PROC_UNLOCK(p); 1009 return (EINVAL); 1010 } 1011 num = imin(p->p_numthreads, data); 1012 PROC_UNLOCK(p); 1013 buf = malloc(num * sizeof(lwpid_t), M_TEMP, M_WAITOK); 1014 tmp = 0; 1015 PROC_LOCK(p); 1016 mtx_lock_spin(&sched_lock); 1017 FOREACH_THREAD_IN_PROC(p, td2) { 1018 if (tmp >= num) 1019 break; 1020 buf[tmp++] = td2->td_tid; 1021 } 1022 mtx_unlock_spin(&sched_lock); 1023 PROC_UNLOCK(p); 1024 error = copyout(buf, addr, tmp * sizeof(lwpid_t)); 1025 free(buf, M_TEMP); 1026 if (!error) 1027 td->td_retval[0] = num; 1028 return (error); 1029 1030 default: 1031 #ifdef __HAVE_PTRACE_MACHDEP 1032 if (req >= PT_FIRSTMACH) { 1033 _PHOLD(p); 1034 PROC_UNLOCK(p); 1035 error = cpu_ptrace(td2, req, addr, data); 1036 PRELE(p); 1037 return (error); 1038 } 1039 #endif 1040 break; 1041 } 1042 1043 /* Unknown request. */ 1044 error = EINVAL; 1045 1046 fail: 1047 PROC_UNLOCK(p); 1048 fail_noproc: 1049 if (proctree_locked) 1050 sx_xunlock(&proctree_lock); 1051 return (error); 1052 } 1053 #undef PROC_READ 1054 #undef PROC_WRITE 1055 1056 /* 1057 * Stop a process because of a debugging event; 1058 * stay stopped until p->p_step is cleared 1059 * (cleared by PIOCCONT in procfs). 1060 */ 1061 void 1062 stopevent(struct proc *p, unsigned int event, unsigned int val) 1063 { 1064 1065 PROC_LOCK_ASSERT(p, MA_OWNED); 1066 p->p_step = 1; 1067 do { 1068 p->p_xstat = val; 1069 p->p_xthread = NULL; 1070 p->p_stype = event; /* Which event caused the stop? */ 1071 wakeup(&p->p_stype); /* Wake up any PIOCWAIT'ing procs */ 1072 msleep(&p->p_step, &p->p_mtx, PWAIT, "stopevent", 0); 1073 } while (p->p_step); 1074 } 1075