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/sysent.h> 43 #include <sys/sysproto.h> 44 #include <sys/proc.h> 45 #include <sys/vnode.h> 46 #include <sys/ptrace.h> 47 #include <sys/sx.h> 48 #include <sys/malloc.h> 49 #include <sys/signalvar.h> 50 51 #include <machine/reg.h> 52 53 #include <security/audit/audit.h> 54 55 #include <vm/vm.h> 56 #include <vm/pmap.h> 57 #include <vm/vm_extern.h> 58 #include <vm/vm_map.h> 59 #include <vm/vm_kern.h> 60 #include <vm/vm_object.h> 61 #include <vm/vm_page.h> 62 #include <vm/vm_param.h> 63 64 #ifdef COMPAT_IA32 65 #include <sys/procfs.h> 66 #include <machine/fpu.h> 67 #include <compat/ia32/ia32_reg.h> 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_flag & P_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 vm_map_t map; 216 vm_object_t backing_object, object = NULL; 217 vm_offset_t pageno = 0; /* page number */ 218 vm_prot_t reqprot; 219 int error, fault_flags, writing; 220 221 /* 222 * Assert that someone has locked this vmspace. (Should be 223 * curthread but we can't assert that.) This keeps the process 224 * from exiting out from under us until this operation completes. 225 */ 226 KASSERT(p->p_lock >= 1, ("%s: process %p (pid %d) not held", __func__, 227 p, p->p_pid)); 228 229 /* 230 * The map we want... 231 */ 232 map = &p->p_vmspace->vm_map; 233 234 writing = uio->uio_rw == UIO_WRITE; 235 reqprot = writing ? (VM_PROT_WRITE | VM_PROT_OVERRIDE_WRITE) : 236 VM_PROT_READ; 237 fault_flags = writing ? VM_FAULT_DIRTY : VM_FAULT_NORMAL; 238 239 /* 240 * Only map in one page at a time. We don't have to, but it 241 * makes things easier. This way is trivial - right? 242 */ 243 do { 244 vm_map_t tmap; 245 vm_offset_t uva; 246 int page_offset; /* offset into page */ 247 vm_map_entry_t out_entry; 248 vm_prot_t out_prot; 249 boolean_t wired; 250 vm_pindex_t pindex; 251 u_int len; 252 vm_page_t m; 253 254 object = NULL; 255 256 uva = (vm_offset_t)uio->uio_offset; 257 258 /* 259 * Get the page number of this segment. 260 */ 261 pageno = trunc_page(uva); 262 page_offset = uva - pageno; 263 264 /* 265 * How many bytes to copy 266 */ 267 len = min(PAGE_SIZE - page_offset, uio->uio_resid); 268 269 /* 270 * Fault the page on behalf of the process 271 */ 272 error = vm_fault(map, pageno, reqprot, fault_flags); 273 if (error) { 274 if (error == KERN_RESOURCE_SHORTAGE) 275 error = ENOMEM; 276 else 277 error = EFAULT; 278 break; 279 } 280 281 /* 282 * Now we need to get the page. out_entry, out_prot, wired, 283 * and single_use aren't used. One would think the vm code 284 * would be a *bit* nicer... We use tmap because 285 * vm_map_lookup() can change the map argument. 286 */ 287 tmap = map; 288 error = vm_map_lookup(&tmap, pageno, reqprot, &out_entry, 289 &object, &pindex, &out_prot, &wired); 290 if (error) { 291 error = EFAULT; 292 break; 293 } 294 VM_OBJECT_LOCK(object); 295 while ((m = vm_page_lookup(object, pindex)) == NULL && 296 !writing && 297 (backing_object = object->backing_object) != NULL) { 298 /* 299 * Allow fallback to backing objects if we are reading. 300 */ 301 VM_OBJECT_LOCK(backing_object); 302 pindex += OFF_TO_IDX(object->backing_object_offset); 303 VM_OBJECT_UNLOCK(object); 304 object = backing_object; 305 } 306 VM_OBJECT_UNLOCK(object); 307 if (m == NULL) { 308 vm_map_lookup_done(tmap, out_entry); 309 error = EFAULT; 310 break; 311 } 312 313 /* 314 * Hold the page in memory. 315 */ 316 vm_page_lock_queues(); 317 vm_page_hold(m); 318 vm_page_unlock_queues(); 319 320 /* 321 * We're done with tmap now. 322 */ 323 vm_map_lookup_done(tmap, out_entry); 324 325 /* 326 * Now do the i/o move. 327 */ 328 error = uiomove_fromphys(&m, page_offset, len, uio); 329 330 /* 331 * Release the page. 332 */ 333 vm_page_lock_queues(); 334 vm_page_unhold(m); 335 vm_page_unlock_queues(); 336 337 } while (error == 0 && uio->uio_resid > 0); 338 339 return (error); 340 } 341 342 /* 343 * Process debugging system call. 344 */ 345 #ifndef _SYS_SYSPROTO_H_ 346 struct ptrace_args { 347 int req; 348 pid_t pid; 349 caddr_t addr; 350 int data; 351 }; 352 #endif 353 354 #ifdef COMPAT_IA32 355 /* 356 * This CPP subterfuge is to try and reduce the number of ifdefs in 357 * the body of the code. 358 * COPYIN(uap->addr, &r.reg, sizeof r.reg); 359 * becomes either: 360 * copyin(uap->addr, &r.reg, sizeof r.reg); 361 * or 362 * copyin(uap->addr, &r.reg32, sizeof r.reg32); 363 * .. except this is done at runtime. 364 */ 365 #define COPYIN(u, k, s) wrap32 ? \ 366 copyin(u, k ## 32, s ## 32) : \ 367 copyin(u, k, s) 368 #define COPYOUT(k, u, s) wrap32 ? \ 369 copyout(k ## 32, u, s ## 32) : \ 370 copyout(k, u, s) 371 #else 372 #define COPYIN(u, k, s) copyin(u, k, s) 373 #define COPYOUT(k, u, s) copyout(k, u, s) 374 #endif 375 int 376 ptrace(struct thread *td, struct ptrace_args *uap) 377 { 378 /* 379 * XXX this obfuscation is to reduce stack usage, but the register 380 * structs may be too large to put on the stack anyway. 381 */ 382 union { 383 struct ptrace_io_desc piod; 384 struct ptrace_lwpinfo pl; 385 struct dbreg dbreg; 386 struct fpreg fpreg; 387 struct reg reg; 388 #ifdef COMPAT_IA32 389 struct dbreg32 dbreg32; 390 struct fpreg32 fpreg32; 391 struct reg32 reg32; 392 struct ptrace_io_desc32 piod32; 393 #endif 394 } r; 395 void *addr; 396 int error = 0; 397 #ifdef COMPAT_IA32 398 int wrap32 = 0; 399 400 if (SV_CURPROC_FLAG(SV_ILP32)) 401 wrap32 = 1; 402 #endif 403 AUDIT_ARG_PID(uap->pid); 404 AUDIT_ARG_CMD(uap->req); 405 AUDIT_ARG_VALUE(uap->data); 406 addr = &r; 407 switch (uap->req) { 408 case PT_GETREGS: 409 case PT_GETFPREGS: 410 case PT_GETDBREGS: 411 case PT_LWPINFO: 412 break; 413 case PT_SETREGS: 414 error = COPYIN(uap->addr, &r.reg, sizeof r.reg); 415 break; 416 case PT_SETFPREGS: 417 error = COPYIN(uap->addr, &r.fpreg, sizeof r.fpreg); 418 break; 419 case PT_SETDBREGS: 420 error = COPYIN(uap->addr, &r.dbreg, sizeof r.dbreg); 421 break; 422 case PT_IO: 423 error = COPYIN(uap->addr, &r.piod, sizeof r.piod); 424 break; 425 default: 426 addr = uap->addr; 427 break; 428 } 429 if (error) 430 return (error); 431 432 error = kern_ptrace(td, uap->req, uap->pid, addr, uap->data); 433 if (error) 434 return (error); 435 436 switch (uap->req) { 437 case PT_IO: 438 error = COPYOUT(&r.piod, uap->addr, sizeof r.piod); 439 break; 440 case PT_GETREGS: 441 error = COPYOUT(&r.reg, uap->addr, sizeof r.reg); 442 break; 443 case PT_GETFPREGS: 444 error = COPYOUT(&r.fpreg, uap->addr, sizeof r.fpreg); 445 break; 446 case PT_GETDBREGS: 447 error = COPYOUT(&r.dbreg, uap->addr, sizeof r.dbreg); 448 break; 449 case PT_LWPINFO: 450 error = copyout(&r.pl, uap->addr, uap->data); 451 break; 452 } 453 454 return (error); 455 } 456 #undef COPYIN 457 #undef COPYOUT 458 459 #ifdef COMPAT_IA32 460 /* 461 * PROC_READ(regs, td2, addr); 462 * becomes either: 463 * proc_read_regs(td2, addr); 464 * or 465 * proc_read_regs32(td2, addr); 466 * .. except this is done at runtime. There is an additional 467 * complication in that PROC_WRITE disallows 32 bit consumers 468 * from writing to 64 bit address space targets. 469 */ 470 #define PROC_READ(w, t, a) wrap32 ? \ 471 proc_read_ ## w ## 32(t, a) : \ 472 proc_read_ ## w (t, a) 473 #define PROC_WRITE(w, t, a) wrap32 ? \ 474 (safe ? proc_write_ ## w ## 32(t, a) : EINVAL ) : \ 475 proc_write_ ## w (t, a) 476 #else 477 #define PROC_READ(w, t, a) proc_read_ ## w (t, a) 478 #define PROC_WRITE(w, t, a) proc_write_ ## w (t, a) 479 #endif 480 481 int 482 kern_ptrace(struct thread *td, int req, pid_t pid, void *addr, int data) 483 { 484 struct iovec iov; 485 struct uio uio; 486 struct proc *curp, *p, *pp; 487 struct thread *td2 = NULL; 488 struct ptrace_io_desc *piod = NULL; 489 struct ptrace_lwpinfo *pl; 490 int error, write, tmp, num; 491 int proctree_locked = 0; 492 lwpid_t tid = 0, *buf; 493 #ifdef COMPAT_IA32 494 int wrap32 = 0, safe = 0; 495 struct ptrace_io_desc32 *piod32 = NULL; 496 #endif 497 498 curp = td->td_proc; 499 500 /* Lock proctree before locking the process. */ 501 switch (req) { 502 case PT_TRACE_ME: 503 case PT_ATTACH: 504 case PT_STEP: 505 case PT_CONTINUE: 506 case PT_TO_SCE: 507 case PT_TO_SCX: 508 case PT_SYSCALL: 509 case PT_DETACH: 510 sx_xlock(&proctree_lock); 511 proctree_locked = 1; 512 break; 513 default: 514 break; 515 } 516 517 write = 0; 518 if (req == PT_TRACE_ME) { 519 p = td->td_proc; 520 PROC_LOCK(p); 521 } else { 522 if (pid <= PID_MAX) { 523 if ((p = pfind(pid)) == NULL) { 524 if (proctree_locked) 525 sx_xunlock(&proctree_lock); 526 return (ESRCH); 527 } 528 } else { 529 /* this is slow, should be optimized */ 530 sx_slock(&allproc_lock); 531 FOREACH_PROC_IN_SYSTEM(p) { 532 PROC_LOCK(p); 533 FOREACH_THREAD_IN_PROC(p, td2) { 534 if (td2->td_tid == pid) 535 break; 536 } 537 if (td2 != NULL) 538 break; /* proc lock held */ 539 PROC_UNLOCK(p); 540 } 541 sx_sunlock(&allproc_lock); 542 if (p == NULL) { 543 if (proctree_locked) 544 sx_xunlock(&proctree_lock); 545 return (ESRCH); 546 } 547 tid = pid; 548 pid = p->p_pid; 549 } 550 } 551 AUDIT_ARG_PROCESS(p); 552 553 if ((p->p_flag & P_WEXIT) != 0) { 554 error = ESRCH; 555 goto fail; 556 } 557 if ((error = p_cansee(td, p)) != 0) 558 goto fail; 559 560 if ((error = p_candebug(td, p)) != 0) 561 goto fail; 562 563 /* 564 * System processes can't be debugged. 565 */ 566 if ((p->p_flag & P_SYSTEM) != 0) { 567 error = EINVAL; 568 goto fail; 569 } 570 571 if (tid == 0) { 572 if ((p->p_flag & P_STOPPED_TRACE) != 0) { 573 KASSERT(p->p_xthread != NULL, ("NULL p_xthread")); 574 td2 = p->p_xthread; 575 } else { 576 td2 = FIRST_THREAD_IN_PROC(p); 577 } 578 tid = td2->td_tid; 579 } 580 581 #ifdef COMPAT_IA32 582 /* 583 * Test if we're a 32 bit client and what the target is. 584 * Set the wrap controls accordingly. 585 */ 586 if (SV_CURPROC_FLAG(SV_ILP32)) { 587 if (td2->td_proc->p_sysent->sv_flags & SV_ILP32) 588 safe = 1; 589 wrap32 = 1; 590 } 591 #endif 592 /* 593 * Permissions check 594 */ 595 switch (req) { 596 case PT_TRACE_ME: 597 /* Always legal. */ 598 break; 599 600 case PT_ATTACH: 601 /* Self */ 602 if (p->p_pid == td->td_proc->p_pid) { 603 error = EINVAL; 604 goto fail; 605 } 606 607 /* Already traced */ 608 if (p->p_flag & P_TRACED) { 609 error = EBUSY; 610 goto fail; 611 } 612 613 /* Can't trace an ancestor if you're being traced. */ 614 if (curp->p_flag & P_TRACED) { 615 for (pp = curp->p_pptr; pp != NULL; pp = pp->p_pptr) { 616 if (pp == p) { 617 error = EINVAL; 618 goto fail; 619 } 620 } 621 } 622 623 624 /* OK */ 625 break; 626 627 case PT_CLEARSTEP: 628 /* Allow thread to clear single step for itself */ 629 if (td->td_tid == tid) 630 break; 631 632 /* FALLTHROUGH */ 633 default: 634 /* not being traced... */ 635 if ((p->p_flag & P_TRACED) == 0) { 636 error = EPERM; 637 goto fail; 638 } 639 640 /* not being traced by YOU */ 641 if (p->p_pptr != td->td_proc) { 642 error = EBUSY; 643 goto fail; 644 } 645 646 /* not currently stopped */ 647 if ((p->p_flag & (P_STOPPED_SIG | P_STOPPED_TRACE)) == 0 || 648 p->p_suspcount != p->p_numthreads || 649 (p->p_flag & P_WAITED) == 0) { 650 error = EBUSY; 651 goto fail; 652 } 653 654 if ((p->p_flag & P_STOPPED_TRACE) == 0) { 655 static int count = 0; 656 if (count++ == 0) 657 printf("P_STOPPED_TRACE not set.\n"); 658 } 659 660 /* OK */ 661 break; 662 } 663 664 /* Keep this process around until we finish this request. */ 665 _PHOLD(p); 666 667 #ifdef FIX_SSTEP 668 /* 669 * Single step fixup ala procfs 670 */ 671 FIX_SSTEP(td2); 672 #endif 673 674 /* 675 * Actually do the requests 676 */ 677 678 td->td_retval[0] = 0; 679 680 switch (req) { 681 case PT_TRACE_ME: 682 /* set my trace flag and "owner" so it can read/write me */ 683 p->p_flag |= P_TRACED; 684 p->p_oppid = p->p_pptr->p_pid; 685 break; 686 687 case PT_ATTACH: 688 /* security check done above */ 689 p->p_flag |= P_TRACED; 690 p->p_oppid = p->p_pptr->p_pid; 691 if (p->p_pptr != td->td_proc) 692 proc_reparent(p, td->td_proc); 693 data = SIGSTOP; 694 goto sendsig; /* in PT_CONTINUE below */ 695 696 case PT_CLEARSTEP: 697 error = ptrace_clear_single_step(td2); 698 break; 699 700 case PT_SETSTEP: 701 error = ptrace_single_step(td2); 702 break; 703 704 case PT_SUSPEND: 705 td2->td_dbgflags |= TDB_SUSPEND; 706 thread_lock(td2); 707 td2->td_flags |= TDF_NEEDSUSPCHK; 708 thread_unlock(td2); 709 break; 710 711 case PT_RESUME: 712 td2->td_dbgflags &= ~TDB_SUSPEND; 713 break; 714 715 case PT_STEP: 716 case PT_CONTINUE: 717 case PT_TO_SCE: 718 case PT_TO_SCX: 719 case PT_SYSCALL: 720 case PT_DETACH: 721 /* Zero means do not send any signal */ 722 if (data < 0 || data > _SIG_MAXSIG) { 723 error = EINVAL; 724 break; 725 } 726 727 switch (req) { 728 case PT_STEP: 729 error = ptrace_single_step(td2); 730 if (error) 731 goto out; 732 break; 733 case PT_TO_SCE: 734 p->p_stops |= S_PT_SCE; 735 break; 736 case PT_TO_SCX: 737 p->p_stops |= S_PT_SCX; 738 break; 739 case PT_SYSCALL: 740 p->p_stops |= S_PT_SCE | S_PT_SCX; 741 break; 742 } 743 744 if (addr != (void *)1) { 745 error = ptrace_set_pc(td2, (u_long)(uintfptr_t)addr); 746 if (error) 747 break; 748 } 749 750 if (req == PT_DETACH) { 751 /* reset process parent */ 752 if (p->p_oppid != p->p_pptr->p_pid) { 753 struct proc *pp; 754 755 PROC_LOCK(p->p_pptr); 756 sigqueue_take(p->p_ksi); 757 PROC_UNLOCK(p->p_pptr); 758 759 PROC_UNLOCK(p); 760 pp = pfind(p->p_oppid); 761 if (pp == NULL) 762 pp = initproc; 763 else 764 PROC_UNLOCK(pp); 765 PROC_LOCK(p); 766 proc_reparent(p, pp); 767 if (pp == initproc) 768 p->p_sigparent = SIGCHLD; 769 } 770 p->p_flag &= ~(P_TRACED | P_WAITED); 771 p->p_oppid = 0; 772 773 /* should we send SIGCHLD? */ 774 /* childproc_continued(p); */ 775 } 776 777 sendsig: 778 if (proctree_locked) { 779 sx_xunlock(&proctree_lock); 780 proctree_locked = 0; 781 } 782 p->p_xstat = data; 783 p->p_xthread = NULL; 784 if ((p->p_flag & (P_STOPPED_SIG | P_STOPPED_TRACE)) != 0) { 785 /* deliver or queue signal */ 786 td2->td_dbgflags &= ~TDB_XSIG; 787 td2->td_xsig = data; 788 789 if (req == PT_DETACH) { 790 struct thread *td3; 791 FOREACH_THREAD_IN_PROC(p, td3) { 792 td3->td_dbgflags &= ~TDB_SUSPEND; 793 } 794 } 795 /* 796 * unsuspend all threads, to not let a thread run, 797 * you should use PT_SUSPEND to suspend it before 798 * continuing process. 799 */ 800 PROC_SLOCK(p); 801 p->p_flag &= ~(P_STOPPED_TRACE|P_STOPPED_SIG|P_WAITED); 802 thread_unsuspend(p); 803 PROC_SUNLOCK(p); 804 } else { 805 if (data) 806 psignal(p, data); 807 } 808 break; 809 810 case PT_WRITE_I: 811 case PT_WRITE_D: 812 write = 1; 813 /* FALLTHROUGH */ 814 case PT_READ_I: 815 case PT_READ_D: 816 PROC_UNLOCK(p); 817 tmp = 0; 818 /* write = 0 set above */ 819 iov.iov_base = write ? (caddr_t)&data : (caddr_t)&tmp; 820 iov.iov_len = sizeof(int); 821 uio.uio_iov = &iov; 822 uio.uio_iovcnt = 1; 823 uio.uio_offset = (off_t)(uintptr_t)addr; 824 uio.uio_resid = sizeof(int); 825 uio.uio_segflg = UIO_SYSSPACE; /* i.e.: the uap */ 826 uio.uio_rw = write ? UIO_WRITE : UIO_READ; 827 uio.uio_td = td; 828 error = proc_rwmem(p, &uio); 829 if (uio.uio_resid != 0) { 830 /* 831 * XXX proc_rwmem() doesn't currently return ENOSPC, 832 * so I think write() can bogusly return 0. 833 * XXX what happens for short writes? We don't want 834 * to write partial data. 835 * XXX proc_rwmem() returns EPERM for other invalid 836 * addresses. Convert this to EINVAL. Does this 837 * clobber returns of EPERM for other reasons? 838 */ 839 if (error == 0 || error == ENOSPC || error == EPERM) 840 error = EINVAL; /* EOF */ 841 } 842 if (!write) 843 td->td_retval[0] = tmp; 844 PROC_LOCK(p); 845 break; 846 847 case PT_IO: 848 #ifdef COMPAT_IA32 849 if (wrap32) { 850 piod32 = addr; 851 iov.iov_base = (void *)(uintptr_t)piod32->piod_addr; 852 iov.iov_len = piod32->piod_len; 853 uio.uio_offset = (off_t)(uintptr_t)piod32->piod_offs; 854 uio.uio_resid = piod32->piod_len; 855 } else 856 #endif 857 { 858 piod = addr; 859 iov.iov_base = piod->piod_addr; 860 iov.iov_len = piod->piod_len; 861 uio.uio_offset = (off_t)(uintptr_t)piod->piod_offs; 862 uio.uio_resid = piod->piod_len; 863 } 864 uio.uio_iov = &iov; 865 uio.uio_iovcnt = 1; 866 uio.uio_segflg = UIO_USERSPACE; 867 uio.uio_td = td; 868 #ifdef COMPAT_IA32 869 tmp = wrap32 ? piod32->piod_op : piod->piod_op; 870 #else 871 tmp = piod->piod_op; 872 #endif 873 switch (tmp) { 874 case PIOD_READ_D: 875 case PIOD_READ_I: 876 uio.uio_rw = UIO_READ; 877 break; 878 case PIOD_WRITE_D: 879 case PIOD_WRITE_I: 880 uio.uio_rw = UIO_WRITE; 881 break; 882 default: 883 error = EINVAL; 884 goto out; 885 } 886 PROC_UNLOCK(p); 887 error = proc_rwmem(p, &uio); 888 #ifdef COMPAT_IA32 889 if (wrap32) 890 piod32->piod_len -= uio.uio_resid; 891 else 892 #endif 893 piod->piod_len -= uio.uio_resid; 894 PROC_LOCK(p); 895 break; 896 897 case PT_KILL: 898 data = SIGKILL; 899 goto sendsig; /* in PT_CONTINUE above */ 900 901 case PT_SETREGS: 902 error = PROC_WRITE(regs, td2, addr); 903 break; 904 905 case PT_GETREGS: 906 error = PROC_READ(regs, td2, addr); 907 break; 908 909 case PT_SETFPREGS: 910 error = PROC_WRITE(fpregs, td2, addr); 911 break; 912 913 case PT_GETFPREGS: 914 error = PROC_READ(fpregs, td2, addr); 915 break; 916 917 case PT_SETDBREGS: 918 error = PROC_WRITE(dbregs, td2, addr); 919 break; 920 921 case PT_GETDBREGS: 922 error = PROC_READ(dbregs, td2, addr); 923 break; 924 925 case PT_LWPINFO: 926 if (data <= 0 || data > sizeof(*pl)) { 927 error = EINVAL; 928 break; 929 } 930 pl = addr; 931 pl->pl_lwpid = td2->td_tid; 932 if (td2->td_dbgflags & TDB_XSIG) 933 pl->pl_event = PL_EVENT_SIGNAL; 934 else 935 pl->pl_event = 0; 936 pl->pl_flags = 0; 937 pl->pl_sigmask = td2->td_sigmask; 938 pl->pl_siglist = td2->td_siglist; 939 break; 940 941 case PT_GETNUMLWPS: 942 td->td_retval[0] = p->p_numthreads; 943 break; 944 945 case PT_GETLWPLIST: 946 if (data <= 0) { 947 error = EINVAL; 948 break; 949 } 950 num = imin(p->p_numthreads, data); 951 PROC_UNLOCK(p); 952 buf = malloc(num * sizeof(lwpid_t), M_TEMP, M_WAITOK); 953 tmp = 0; 954 PROC_LOCK(p); 955 FOREACH_THREAD_IN_PROC(p, td2) { 956 if (tmp >= num) 957 break; 958 buf[tmp++] = td2->td_tid; 959 } 960 PROC_UNLOCK(p); 961 error = copyout(buf, addr, tmp * sizeof(lwpid_t)); 962 free(buf, M_TEMP); 963 if (!error) 964 td->td_retval[0] = tmp; 965 PROC_LOCK(p); 966 break; 967 968 default: 969 #ifdef __HAVE_PTRACE_MACHDEP 970 if (req >= PT_FIRSTMACH) { 971 PROC_UNLOCK(p); 972 error = cpu_ptrace(td2, req, addr, data); 973 PROC_LOCK(p); 974 } else 975 #endif 976 /* Unknown request. */ 977 error = EINVAL; 978 break; 979 } 980 981 out: 982 /* Drop our hold on this process now that the request has completed. */ 983 _PRELE(p); 984 fail: 985 PROC_UNLOCK(p); 986 if (proctree_locked) 987 sx_xunlock(&proctree_lock); 988 return (error); 989 } 990 #undef PROC_READ 991 #undef PROC_WRITE 992 993 /* 994 * Stop a process because of a debugging event; 995 * stay stopped until p->p_step is cleared 996 * (cleared by PIOCCONT in procfs). 997 */ 998 void 999 stopevent(struct proc *p, unsigned int event, unsigned int val) 1000 { 1001 1002 PROC_LOCK_ASSERT(p, MA_OWNED); 1003 p->p_step = 1; 1004 do { 1005 p->p_xstat = val; 1006 p->p_xthread = NULL; 1007 p->p_stype = event; /* Which event caused the stop? */ 1008 wakeup(&p->p_stype); /* Wake up any PIOCWAIT'ing procs */ 1009 msleep(&p->p_step, &p->p_mtx, PWAIT, "stopevent", 0); 1010 } while (p->p_step); 1011 } 1012