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