1 /*- 2 * SPDX-License-Identifier: BSD-4-Clause 3 * 4 * Copyright (c) 1994, Sean Eric Fagan 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by Sean Eric Fagan. 18 * 4. The name of the author may not be used to endorse or promote products 19 * derived from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #include <sys/cdefs.h> 35 __FBSDID("$FreeBSD$"); 36 37 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/ktr.h> 40 #include <sys/limits.h> 41 #include <sys/lock.h> 42 #include <sys/mutex.h> 43 #include <sys/reg.h> 44 #include <sys/syscallsubr.h> 45 #include <sys/sysent.h> 46 #include <sys/sysproto.h> 47 #include <sys/priv.h> 48 #include <sys/proc.h> 49 #include <sys/vnode.h> 50 #include <sys/ptrace.h> 51 #include <sys/rwlock.h> 52 #include <sys/sx.h> 53 #include <sys/malloc.h> 54 #include <sys/signalvar.h> 55 #include <sys/caprights.h> 56 #include <sys/filedesc.h> 57 58 #include <security/audit/audit.h> 59 60 #include <vm/vm.h> 61 #include <vm/pmap.h> 62 #include <vm/vm_extern.h> 63 #include <vm/vm_map.h> 64 #include <vm/vm_kern.h> 65 #include <vm/vm_object.h> 66 #include <vm/vm_page.h> 67 #include <vm/vm_param.h> 68 69 #ifdef COMPAT_FREEBSD32 70 #include <sys/procfs.h> 71 #endif 72 73 /* 74 * Functions implemented using PROC_ACTION(): 75 * 76 * proc_read_regs(proc, regs) 77 * Get the current user-visible register set from the process 78 * and copy it into the regs structure (<machine/reg.h>). 79 * The process is stopped at the time read_regs is called. 80 * 81 * proc_write_regs(proc, regs) 82 * Update the current register set from the passed in regs 83 * structure. Take care to avoid clobbering special CPU 84 * registers or privileged bits in the PSL. 85 * Depending on the architecture this may have fix-up work to do, 86 * especially if the IAR or PCW are modified. 87 * The process is stopped at the time write_regs is called. 88 * 89 * proc_read_fpregs, proc_write_fpregs 90 * deal with the floating point register set, otherwise as above. 91 * 92 * proc_read_dbregs, proc_write_dbregs 93 * deal with the processor debug register set, otherwise as above. 94 * 95 * proc_sstep(proc) 96 * Arrange for the process to trap after executing a single instruction. 97 */ 98 99 #define PROC_ACTION(action) do { \ 100 int error; \ 101 \ 102 PROC_LOCK_ASSERT(td->td_proc, MA_OWNED); \ 103 if ((td->td_proc->p_flag & P_INMEM) == 0) \ 104 error = EIO; \ 105 else \ 106 error = (action); \ 107 return (error); \ 108 } while (0) 109 110 int 111 proc_read_regs(struct thread *td, struct reg *regs) 112 { 113 114 PROC_ACTION(fill_regs(td, regs)); 115 } 116 117 int 118 proc_write_regs(struct thread *td, struct reg *regs) 119 { 120 121 PROC_ACTION(set_regs(td, regs)); 122 } 123 124 int 125 proc_read_dbregs(struct thread *td, struct dbreg *dbregs) 126 { 127 128 PROC_ACTION(fill_dbregs(td, dbregs)); 129 } 130 131 int 132 proc_write_dbregs(struct thread *td, struct dbreg *dbregs) 133 { 134 135 PROC_ACTION(set_dbregs(td, dbregs)); 136 } 137 138 /* 139 * Ptrace doesn't support fpregs at all, and there are no security holes 140 * or translations for fpregs, so we can just copy them. 141 */ 142 int 143 proc_read_fpregs(struct thread *td, struct fpreg *fpregs) 144 { 145 146 PROC_ACTION(fill_fpregs(td, fpregs)); 147 } 148 149 int 150 proc_write_fpregs(struct thread *td, struct fpreg *fpregs) 151 { 152 153 PROC_ACTION(set_fpregs(td, fpregs)); 154 } 155 156 #ifdef COMPAT_FREEBSD32 157 /* For 32 bit binaries, we need to expose the 32 bit regs layouts. */ 158 int 159 proc_read_regs32(struct thread *td, struct reg32 *regs32) 160 { 161 162 PROC_ACTION(fill_regs32(td, regs32)); 163 } 164 165 int 166 proc_write_regs32(struct thread *td, struct reg32 *regs32) 167 { 168 169 PROC_ACTION(set_regs32(td, regs32)); 170 } 171 172 int 173 proc_read_dbregs32(struct thread *td, struct dbreg32 *dbregs32) 174 { 175 176 PROC_ACTION(fill_dbregs32(td, dbregs32)); 177 } 178 179 int 180 proc_write_dbregs32(struct thread *td, struct dbreg32 *dbregs32) 181 { 182 183 PROC_ACTION(set_dbregs32(td, dbregs32)); 184 } 185 186 int 187 proc_read_fpregs32(struct thread *td, struct fpreg32 *fpregs32) 188 { 189 190 PROC_ACTION(fill_fpregs32(td, fpregs32)); 191 } 192 193 int 194 proc_write_fpregs32(struct thread *td, struct fpreg32 *fpregs32) 195 { 196 197 PROC_ACTION(set_fpregs32(td, fpregs32)); 198 } 199 #endif 200 201 int 202 proc_sstep(struct thread *td) 203 { 204 205 PROC_ACTION(ptrace_single_step(td)); 206 } 207 208 int 209 proc_rwmem(struct proc *p, struct uio *uio) 210 { 211 vm_map_t map; 212 vm_offset_t pageno; /* page number */ 213 vm_prot_t reqprot; 214 int error, fault_flags, page_offset, writing; 215 216 /* 217 * Assert that someone has locked this vmspace. (Should be 218 * curthread but we can't assert that.) This keeps the process 219 * from exiting out from under us until this operation completes. 220 */ 221 PROC_ASSERT_HELD(p); 222 PROC_LOCK_ASSERT(p, MA_NOTOWNED); 223 224 /* 225 * The map we want... 226 */ 227 map = &p->p_vmspace->vm_map; 228 229 /* 230 * If we are writing, then we request vm_fault() to create a private 231 * copy of each page. Since these copies will not be writeable by the 232 * process, we must explicity request that they be dirtied. 233 */ 234 writing = uio->uio_rw == UIO_WRITE; 235 reqprot = writing ? VM_PROT_COPY | VM_PROT_READ : VM_PROT_READ; 236 fault_flags = writing ? VM_FAULT_DIRTY : VM_FAULT_NORMAL; 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_offset_t uva; 244 u_int len; 245 vm_page_t m; 246 247 uva = (vm_offset_t)uio->uio_offset; 248 249 /* 250 * Get the page number of this segment. 251 */ 252 pageno = trunc_page(uva); 253 page_offset = uva - pageno; 254 255 /* 256 * How many bytes to copy 257 */ 258 len = min(PAGE_SIZE - page_offset, uio->uio_resid); 259 260 /* 261 * Fault and hold the page on behalf of the process. 262 */ 263 error = vm_fault(map, pageno, reqprot, fault_flags, &m); 264 if (error != KERN_SUCCESS) { 265 if (error == KERN_RESOURCE_SHORTAGE) 266 error = ENOMEM; 267 else 268 error = EFAULT; 269 break; 270 } 271 272 /* 273 * Now do the i/o move. 274 */ 275 error = uiomove_fromphys(&m, page_offset, len, uio); 276 277 /* Make the I-cache coherent for breakpoints. */ 278 if (writing && error == 0) { 279 vm_map_lock_read(map); 280 if (vm_map_check_protection(map, pageno, pageno + 281 PAGE_SIZE, VM_PROT_EXECUTE)) 282 vm_sync_icache(map, uva, len); 283 vm_map_unlock_read(map); 284 } 285 286 /* 287 * Release the page. 288 */ 289 vm_page_unwire(m, PQ_ACTIVE); 290 291 } while (error == 0 && uio->uio_resid > 0); 292 293 return (error); 294 } 295 296 static ssize_t 297 proc_iop(struct thread *td, struct proc *p, vm_offset_t va, void *buf, 298 size_t len, enum uio_rw rw) 299 { 300 struct iovec iov; 301 struct uio uio; 302 ssize_t slen; 303 304 MPASS(len < SSIZE_MAX); 305 slen = (ssize_t)len; 306 307 iov.iov_base = (caddr_t)buf; 308 iov.iov_len = len; 309 uio.uio_iov = &iov; 310 uio.uio_iovcnt = 1; 311 uio.uio_offset = va; 312 uio.uio_resid = slen; 313 uio.uio_segflg = UIO_SYSSPACE; 314 uio.uio_rw = rw; 315 uio.uio_td = td; 316 proc_rwmem(p, &uio); 317 if (uio.uio_resid == slen) 318 return (-1); 319 return (slen - uio.uio_resid); 320 } 321 322 ssize_t 323 proc_readmem(struct thread *td, struct proc *p, vm_offset_t va, void *buf, 324 size_t len) 325 { 326 327 return (proc_iop(td, p, va, buf, len, UIO_READ)); 328 } 329 330 ssize_t 331 proc_writemem(struct thread *td, struct proc *p, vm_offset_t va, void *buf, 332 size_t len) 333 { 334 335 return (proc_iop(td, p, va, buf, len, UIO_WRITE)); 336 } 337 338 static int 339 ptrace_vm_entry(struct thread *td, struct proc *p, struct ptrace_vm_entry *pve) 340 { 341 struct vattr vattr; 342 vm_map_t map; 343 vm_map_entry_t entry; 344 vm_object_t obj, tobj, lobj; 345 struct vmspace *vm; 346 struct vnode *vp; 347 char *freepath, *fullpath; 348 u_int pathlen; 349 int error, index; 350 351 error = 0; 352 obj = NULL; 353 354 vm = vmspace_acquire_ref(p); 355 map = &vm->vm_map; 356 vm_map_lock_read(map); 357 358 do { 359 KASSERT((map->header.eflags & MAP_ENTRY_IS_SUB_MAP) == 0, 360 ("Submap in map header")); 361 index = 0; 362 VM_MAP_ENTRY_FOREACH(entry, map) { 363 if (index >= pve->pve_entry && 364 (entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) 365 break; 366 index++; 367 } 368 if (index < pve->pve_entry) { 369 error = EINVAL; 370 break; 371 } 372 if (entry == &map->header) { 373 error = ENOENT; 374 break; 375 } 376 377 /* We got an entry. */ 378 pve->pve_entry = index + 1; 379 pve->pve_timestamp = map->timestamp; 380 pve->pve_start = entry->start; 381 pve->pve_end = entry->end - 1; 382 pve->pve_offset = entry->offset; 383 pve->pve_prot = entry->protection; 384 385 /* Backing object's path needed? */ 386 if (pve->pve_pathlen == 0) 387 break; 388 389 pathlen = pve->pve_pathlen; 390 pve->pve_pathlen = 0; 391 392 obj = entry->object.vm_object; 393 if (obj != NULL) 394 VM_OBJECT_RLOCK(obj); 395 } while (0); 396 397 vm_map_unlock_read(map); 398 399 pve->pve_fsid = VNOVAL; 400 pve->pve_fileid = VNOVAL; 401 402 if (error == 0 && obj != NULL) { 403 lobj = obj; 404 for (tobj = obj; tobj != NULL; tobj = tobj->backing_object) { 405 if (tobj != obj) 406 VM_OBJECT_RLOCK(tobj); 407 if (lobj != obj) 408 VM_OBJECT_RUNLOCK(lobj); 409 lobj = tobj; 410 pve->pve_offset += tobj->backing_object_offset; 411 } 412 vp = vm_object_vnode(lobj); 413 if (vp != NULL) 414 vref(vp); 415 if (lobj != obj) 416 VM_OBJECT_RUNLOCK(lobj); 417 VM_OBJECT_RUNLOCK(obj); 418 419 if (vp != NULL) { 420 freepath = NULL; 421 fullpath = NULL; 422 vn_fullpath(vp, &fullpath, &freepath); 423 vn_lock(vp, LK_SHARED | LK_RETRY); 424 if (VOP_GETATTR(vp, &vattr, td->td_ucred) == 0) { 425 pve->pve_fileid = vattr.va_fileid; 426 pve->pve_fsid = vattr.va_fsid; 427 } 428 vput(vp); 429 430 if (fullpath != NULL) { 431 pve->pve_pathlen = strlen(fullpath) + 1; 432 if (pve->pve_pathlen <= pathlen) { 433 error = copyout(fullpath, pve->pve_path, 434 pve->pve_pathlen); 435 } else 436 error = ENAMETOOLONG; 437 } 438 if (freepath != NULL) 439 free(freepath, M_TEMP); 440 } 441 } 442 vmspace_free(vm); 443 if (error == 0) 444 CTR3(KTR_PTRACE, "PT_VM_ENTRY: pid %d, entry %d, start %p", 445 p->p_pid, pve->pve_entry, pve->pve_start); 446 447 return (error); 448 } 449 450 /* 451 * Process debugging system call. 452 */ 453 #ifndef _SYS_SYSPROTO_H_ 454 struct ptrace_args { 455 int req; 456 pid_t pid; 457 caddr_t addr; 458 int data; 459 }; 460 #endif 461 462 int 463 sys_ptrace(struct thread *td, struct ptrace_args *uap) 464 { 465 /* 466 * XXX this obfuscation is to reduce stack usage, but the register 467 * structs may be too large to put on the stack anyway. 468 */ 469 union { 470 struct ptrace_io_desc piod; 471 struct ptrace_lwpinfo pl; 472 struct ptrace_vm_entry pve; 473 struct ptrace_coredump pc; 474 struct dbreg dbreg; 475 struct fpreg fpreg; 476 struct reg reg; 477 char args[sizeof(td->td_sa.args)]; 478 struct ptrace_sc_ret psr; 479 int ptevents; 480 } r; 481 void *addr; 482 int error; 483 484 if (!allow_ptrace) 485 return (ENOSYS); 486 error = 0; 487 488 AUDIT_ARG_PID(uap->pid); 489 AUDIT_ARG_CMD(uap->req); 490 AUDIT_ARG_VALUE(uap->data); 491 addr = &r; 492 switch (uap->req) { 493 case PT_GET_EVENT_MASK: 494 case PT_LWPINFO: 495 case PT_GET_SC_ARGS: 496 case PT_GET_SC_RET: 497 break; 498 case PT_GETREGS: 499 bzero(&r.reg, sizeof(r.reg)); 500 break; 501 case PT_GETFPREGS: 502 bzero(&r.fpreg, sizeof(r.fpreg)); 503 break; 504 case PT_GETDBREGS: 505 bzero(&r.dbreg, sizeof(r.dbreg)); 506 break; 507 case PT_SETREGS: 508 error = copyin(uap->addr, &r.reg, sizeof(r.reg)); 509 break; 510 case PT_SETFPREGS: 511 error = copyin(uap->addr, &r.fpreg, sizeof(r.fpreg)); 512 break; 513 case PT_SETDBREGS: 514 error = copyin(uap->addr, &r.dbreg, sizeof(r.dbreg)); 515 break; 516 case PT_SET_EVENT_MASK: 517 if (uap->data != sizeof(r.ptevents)) 518 error = EINVAL; 519 else 520 error = copyin(uap->addr, &r.ptevents, uap->data); 521 break; 522 case PT_IO: 523 error = copyin(uap->addr, &r.piod, sizeof(r.piod)); 524 break; 525 case PT_VM_ENTRY: 526 error = copyin(uap->addr, &r.pve, sizeof(r.pve)); 527 break; 528 case PT_COREDUMP: 529 if (uap->data != sizeof(r.pc)) 530 error = EINVAL; 531 else 532 error = copyin(uap->addr, &r.pc, uap->data); 533 break; 534 default: 535 addr = uap->addr; 536 break; 537 } 538 if (error) 539 return (error); 540 541 error = kern_ptrace(td, uap->req, uap->pid, addr, uap->data); 542 if (error) 543 return (error); 544 545 switch (uap->req) { 546 case PT_VM_ENTRY: 547 error = copyout(&r.pve, uap->addr, sizeof(r.pve)); 548 break; 549 case PT_IO: 550 error = copyout(&r.piod, uap->addr, sizeof(r.piod)); 551 break; 552 case PT_GETREGS: 553 error = copyout(&r.reg, uap->addr, sizeof(r.reg)); 554 break; 555 case PT_GETFPREGS: 556 error = copyout(&r.fpreg, uap->addr, sizeof(r.fpreg)); 557 break; 558 case PT_GETDBREGS: 559 error = copyout(&r.dbreg, uap->addr, sizeof(r.dbreg)); 560 break; 561 case PT_GET_EVENT_MASK: 562 /* NB: The size in uap->data is validated in kern_ptrace(). */ 563 error = copyout(&r.ptevents, uap->addr, uap->data); 564 break; 565 case PT_LWPINFO: 566 /* NB: The size in uap->data is validated in kern_ptrace(). */ 567 error = copyout(&r.pl, uap->addr, uap->data); 568 break; 569 case PT_GET_SC_ARGS: 570 error = copyout(r.args, uap->addr, MIN(uap->data, 571 sizeof(r.args))); 572 break; 573 case PT_GET_SC_RET: 574 error = copyout(&r.psr, uap->addr, MIN(uap->data, 575 sizeof(r.psr))); 576 break; 577 } 578 579 return (error); 580 } 581 582 #ifdef COMPAT_FREEBSD32 583 /* 584 * PROC_READ(regs, td2, addr); 585 * becomes either: 586 * proc_read_regs(td2, addr); 587 * or 588 * proc_read_regs32(td2, addr); 589 * .. except this is done at runtime. There is an additional 590 * complication in that PROC_WRITE disallows 32 bit consumers 591 * from writing to 64 bit address space targets. 592 */ 593 #define PROC_READ(w, t, a) wrap32 ? \ 594 proc_read_ ## w ## 32(t, a) : \ 595 proc_read_ ## w (t, a) 596 #define PROC_WRITE(w, t, a) wrap32 ? \ 597 (safe ? proc_write_ ## w ## 32(t, a) : EINVAL ) : \ 598 proc_write_ ## w (t, a) 599 #else 600 #define PROC_READ(w, t, a) proc_read_ ## w (t, a) 601 #define PROC_WRITE(w, t, a) proc_write_ ## w (t, a) 602 #endif 603 604 void 605 proc_set_traced(struct proc *p, bool stop) 606 { 607 608 sx_assert(&proctree_lock, SX_XLOCKED); 609 PROC_LOCK_ASSERT(p, MA_OWNED); 610 p->p_flag |= P_TRACED; 611 if (stop) 612 p->p_flag2 |= P2_PTRACE_FSTP; 613 p->p_ptevents = PTRACE_DEFAULT; 614 } 615 616 void 617 ptrace_unsuspend(struct proc *p) 618 { 619 PROC_LOCK_ASSERT(p, MA_OWNED); 620 621 PROC_SLOCK(p); 622 p->p_flag &= ~(P_STOPPED_TRACE | P_STOPPED_SIG | P_WAITED); 623 thread_unsuspend(p); 624 PROC_SUNLOCK(p); 625 itimer_proc_continue(p); 626 kqtimer_proc_continue(p); 627 } 628 629 static int 630 proc_can_ptrace(struct thread *td, struct proc *p) 631 { 632 int error; 633 634 PROC_LOCK_ASSERT(p, MA_OWNED); 635 636 if ((p->p_flag & P_WEXIT) != 0) 637 return (ESRCH); 638 639 if ((error = p_cansee(td, p)) != 0) 640 return (error); 641 if ((error = p_candebug(td, p)) != 0) 642 return (error); 643 644 /* not being traced... */ 645 if ((p->p_flag & P_TRACED) == 0) 646 return (EPERM); 647 648 /* not being traced by YOU */ 649 if (p->p_pptr != td->td_proc) 650 return (EBUSY); 651 652 /* not currently stopped */ 653 if ((p->p_flag & P_STOPPED_TRACE) == 0 || 654 p->p_suspcount != p->p_numthreads || 655 (p->p_flag & P_WAITED) == 0) 656 return (EBUSY); 657 658 return (0); 659 } 660 661 static struct thread * 662 ptrace_sel_coredump_thread(struct proc *p) 663 { 664 struct thread *td2; 665 666 PROC_LOCK_ASSERT(p, MA_OWNED); 667 MPASS((p->p_flag & P_STOPPED_TRACE) != 0); 668 669 FOREACH_THREAD_IN_PROC(p, td2) { 670 if ((td2->td_dbgflags & TDB_SSWITCH) != 0) 671 return (td2); 672 } 673 return (NULL); 674 } 675 676 int 677 kern_ptrace(struct thread *td, int req, pid_t pid, void *addr, int data) 678 { 679 struct iovec iov; 680 struct uio uio; 681 struct proc *curp, *p, *pp; 682 struct thread *td2 = NULL, *td3; 683 struct ptrace_io_desc *piod = NULL; 684 struct ptrace_lwpinfo *pl; 685 struct ptrace_sc_ret *psr; 686 struct file *fp; 687 struct ptrace_coredump *pc; 688 struct thr_coredump_req *tcq; 689 int error, num, tmp; 690 lwpid_t tid = 0, *buf; 691 #ifdef COMPAT_FREEBSD32 692 int wrap32 = 0, safe = 0; 693 #endif 694 bool proctree_locked, p2_req_set; 695 696 curp = td->td_proc; 697 proctree_locked = false; 698 p2_req_set = false; 699 700 /* Lock proctree before locking the process. */ 701 switch (req) { 702 case PT_TRACE_ME: 703 case PT_ATTACH: 704 case PT_STEP: 705 case PT_CONTINUE: 706 case PT_TO_SCE: 707 case PT_TO_SCX: 708 case PT_SYSCALL: 709 case PT_FOLLOW_FORK: 710 case PT_LWP_EVENTS: 711 case PT_GET_EVENT_MASK: 712 case PT_SET_EVENT_MASK: 713 case PT_DETACH: 714 case PT_GET_SC_ARGS: 715 sx_xlock(&proctree_lock); 716 proctree_locked = true; 717 break; 718 default: 719 break; 720 } 721 722 if (req == PT_TRACE_ME) { 723 p = td->td_proc; 724 PROC_LOCK(p); 725 } else { 726 if (pid <= PID_MAX) { 727 if ((p = pfind(pid)) == NULL) { 728 if (proctree_locked) 729 sx_xunlock(&proctree_lock); 730 return (ESRCH); 731 } 732 } else { 733 td2 = tdfind(pid, -1); 734 if (td2 == NULL) { 735 if (proctree_locked) 736 sx_xunlock(&proctree_lock); 737 return (ESRCH); 738 } 739 p = td2->td_proc; 740 tid = pid; 741 pid = p->p_pid; 742 } 743 } 744 AUDIT_ARG_PROCESS(p); 745 746 if ((p->p_flag & P_WEXIT) != 0) { 747 error = ESRCH; 748 goto fail; 749 } 750 if ((error = p_cansee(td, p)) != 0) 751 goto fail; 752 753 if ((error = p_candebug(td, p)) != 0) 754 goto fail; 755 756 /* 757 * System processes can't be debugged. 758 */ 759 if ((p->p_flag & P_SYSTEM) != 0) { 760 error = EINVAL; 761 goto fail; 762 } 763 764 if (tid == 0) { 765 if ((p->p_flag & P_STOPPED_TRACE) != 0) { 766 KASSERT(p->p_xthread != NULL, ("NULL p_xthread")); 767 td2 = p->p_xthread; 768 } else { 769 td2 = FIRST_THREAD_IN_PROC(p); 770 } 771 tid = td2->td_tid; 772 } 773 774 #ifdef COMPAT_FREEBSD32 775 /* 776 * Test if we're a 32 bit client and what the target is. 777 * Set the wrap controls accordingly. 778 */ 779 if (SV_CURPROC_FLAG(SV_ILP32)) { 780 if (SV_PROC_FLAG(td2->td_proc, SV_ILP32)) 781 safe = 1; 782 wrap32 = 1; 783 } 784 #endif 785 /* 786 * Permissions check 787 */ 788 switch (req) { 789 case PT_TRACE_ME: 790 /* 791 * Always legal, when there is a parent process which 792 * could trace us. Otherwise, reject. 793 */ 794 if ((p->p_flag & P_TRACED) != 0) { 795 error = EBUSY; 796 goto fail; 797 } 798 if (p->p_pptr == initproc) { 799 error = EPERM; 800 goto fail; 801 } 802 break; 803 804 case PT_ATTACH: 805 /* Self */ 806 if (p == td->td_proc) { 807 error = EINVAL; 808 goto fail; 809 } 810 811 /* Already traced */ 812 if (p->p_flag & P_TRACED) { 813 error = EBUSY; 814 goto fail; 815 } 816 817 /* Can't trace an ancestor if you're being traced. */ 818 if (curp->p_flag & P_TRACED) { 819 for (pp = curp->p_pptr; pp != NULL; pp = pp->p_pptr) { 820 if (pp == p) { 821 error = EINVAL; 822 goto fail; 823 } 824 } 825 } 826 827 /* OK */ 828 break; 829 830 case PT_CLEARSTEP: 831 /* Allow thread to clear single step for itself */ 832 if (td->td_tid == tid) 833 break; 834 835 /* FALLTHROUGH */ 836 default: 837 /* 838 * Check for ptrace eligibility before waiting for 839 * holds to drain. 840 */ 841 error = proc_can_ptrace(td, p); 842 if (error != 0) 843 goto fail; 844 845 /* 846 * Block parallel ptrace requests. Most important, do 847 * not allow other thread in debugger to continue the 848 * debuggee until coredump finished. 849 */ 850 while ((p->p_flag2 & P2_PTRACEREQ) != 0) { 851 if (proctree_locked) 852 sx_xunlock(&proctree_lock); 853 error = msleep(&p->p_flag2, &p->p_mtx, PPAUSE | PCATCH | 854 (proctree_locked ? PDROP : 0), "pptrace", 0); 855 if (proctree_locked) { 856 sx_xlock(&proctree_lock); 857 PROC_LOCK(p); 858 } 859 if (error == 0 && td2->td_proc != p) 860 error = ESRCH; 861 if (error == 0) 862 error = proc_can_ptrace(td, p); 863 if (error != 0) 864 goto fail; 865 } 866 867 /* Ok */ 868 break; 869 } 870 871 /* 872 * Keep this process around and request parallel ptrace() 873 * request to wait until we finish this request. 874 */ 875 MPASS((p->p_flag2 & P2_PTRACEREQ) == 0); 876 p->p_flag2 |= P2_PTRACEREQ; 877 p2_req_set = true; 878 _PHOLD(p); 879 880 /* 881 * Actually do the requests 882 */ 883 884 td->td_retval[0] = 0; 885 886 switch (req) { 887 case PT_TRACE_ME: 888 /* set my trace flag and "owner" so it can read/write me */ 889 proc_set_traced(p, false); 890 if (p->p_flag & P_PPWAIT) 891 p->p_flag |= P_PPTRACE; 892 CTR1(KTR_PTRACE, "PT_TRACE_ME: pid %d", p->p_pid); 893 break; 894 895 case PT_ATTACH: 896 /* security check done above */ 897 /* 898 * It would be nice if the tracing relationship was separate 899 * from the parent relationship but that would require 900 * another set of links in the proc struct or for "wait" 901 * to scan the entire proc table. To make life easier, 902 * we just re-parent the process we're trying to trace. 903 * The old parent is remembered so we can put things back 904 * on a "detach". 905 */ 906 proc_set_traced(p, true); 907 proc_reparent(p, td->td_proc, false); 908 CTR2(KTR_PTRACE, "PT_ATTACH: pid %d, oppid %d", p->p_pid, 909 p->p_oppid); 910 911 sx_xunlock(&proctree_lock); 912 proctree_locked = false; 913 MPASS(p->p_xthread == NULL); 914 MPASS((p->p_flag & P_STOPPED_TRACE) == 0); 915 916 /* 917 * If already stopped due to a stop signal, clear the 918 * existing stop before triggering a traced SIGSTOP. 919 */ 920 if ((p->p_flag & P_STOPPED_SIG) != 0) { 921 PROC_SLOCK(p); 922 p->p_flag &= ~(P_STOPPED_SIG | P_WAITED); 923 thread_unsuspend(p); 924 PROC_SUNLOCK(p); 925 } 926 927 kern_psignal(p, SIGSTOP); 928 break; 929 930 case PT_CLEARSTEP: 931 CTR2(KTR_PTRACE, "PT_CLEARSTEP: tid %d (pid %d)", td2->td_tid, 932 p->p_pid); 933 error = ptrace_clear_single_step(td2); 934 break; 935 936 case PT_SETSTEP: 937 CTR2(KTR_PTRACE, "PT_SETSTEP: tid %d (pid %d)", td2->td_tid, 938 p->p_pid); 939 error = ptrace_single_step(td2); 940 break; 941 942 case PT_SUSPEND: 943 CTR2(KTR_PTRACE, "PT_SUSPEND: tid %d (pid %d)", td2->td_tid, 944 p->p_pid); 945 td2->td_dbgflags |= TDB_SUSPEND; 946 thread_lock(td2); 947 td2->td_flags |= TDF_NEEDSUSPCHK; 948 thread_unlock(td2); 949 break; 950 951 case PT_RESUME: 952 CTR2(KTR_PTRACE, "PT_RESUME: tid %d (pid %d)", td2->td_tid, 953 p->p_pid); 954 td2->td_dbgflags &= ~TDB_SUSPEND; 955 break; 956 957 case PT_FOLLOW_FORK: 958 CTR3(KTR_PTRACE, "PT_FOLLOW_FORK: pid %d %s -> %s", p->p_pid, 959 p->p_ptevents & PTRACE_FORK ? "enabled" : "disabled", 960 data ? "enabled" : "disabled"); 961 if (data) 962 p->p_ptevents |= PTRACE_FORK; 963 else 964 p->p_ptevents &= ~PTRACE_FORK; 965 break; 966 967 case PT_LWP_EVENTS: 968 CTR3(KTR_PTRACE, "PT_LWP_EVENTS: pid %d %s -> %s", p->p_pid, 969 p->p_ptevents & PTRACE_LWP ? "enabled" : "disabled", 970 data ? "enabled" : "disabled"); 971 if (data) 972 p->p_ptevents |= PTRACE_LWP; 973 else 974 p->p_ptevents &= ~PTRACE_LWP; 975 break; 976 977 case PT_GET_EVENT_MASK: 978 if (data != sizeof(p->p_ptevents)) { 979 error = EINVAL; 980 break; 981 } 982 CTR2(KTR_PTRACE, "PT_GET_EVENT_MASK: pid %d mask %#x", p->p_pid, 983 p->p_ptevents); 984 *(int *)addr = p->p_ptevents; 985 break; 986 987 case PT_SET_EVENT_MASK: 988 if (data != sizeof(p->p_ptevents)) { 989 error = EINVAL; 990 break; 991 } 992 tmp = *(int *)addr; 993 if ((tmp & ~(PTRACE_EXEC | PTRACE_SCE | PTRACE_SCX | 994 PTRACE_FORK | PTRACE_LWP | PTRACE_VFORK)) != 0) { 995 error = EINVAL; 996 break; 997 } 998 CTR3(KTR_PTRACE, "PT_SET_EVENT_MASK: pid %d mask %#x -> %#x", 999 p->p_pid, p->p_ptevents, tmp); 1000 p->p_ptevents = tmp; 1001 break; 1002 1003 case PT_GET_SC_ARGS: 1004 CTR1(KTR_PTRACE, "PT_GET_SC_ARGS: pid %d", p->p_pid); 1005 if ((td2->td_dbgflags & (TDB_SCE | TDB_SCX)) == 0 1006 #ifdef COMPAT_FREEBSD32 1007 || (wrap32 && !safe) 1008 #endif 1009 ) { 1010 error = EINVAL; 1011 break; 1012 } 1013 bzero(addr, sizeof(td2->td_sa.args)); 1014 /* See the explanation in linux_ptrace_get_syscall_info(). */ 1015 bcopy(td2->td_sa.args, addr, SV_PROC_ABI(td->td_proc) == 1016 SV_ABI_LINUX ? sizeof(td2->td_sa.args) : 1017 td2->td_sa.callp->sy_narg * sizeof(register_t)); 1018 break; 1019 1020 case PT_GET_SC_RET: 1021 if ((td2->td_dbgflags & (TDB_SCX)) == 0 1022 #ifdef COMPAT_FREEBSD32 1023 || (wrap32 && !safe) 1024 #endif 1025 ) { 1026 error = EINVAL; 1027 break; 1028 } 1029 psr = addr; 1030 bzero(psr, sizeof(*psr)); 1031 psr->sr_error = td2->td_errno; 1032 if (psr->sr_error == 0) { 1033 psr->sr_retval[0] = td2->td_retval[0]; 1034 psr->sr_retval[1] = td2->td_retval[1]; 1035 } 1036 CTR4(KTR_PTRACE, 1037 "PT_GET_SC_RET: pid %d error %d retval %#lx,%#lx", 1038 p->p_pid, psr->sr_error, psr->sr_retval[0], 1039 psr->sr_retval[1]); 1040 break; 1041 1042 case PT_STEP: 1043 case PT_CONTINUE: 1044 case PT_TO_SCE: 1045 case PT_TO_SCX: 1046 case PT_SYSCALL: 1047 case PT_DETACH: 1048 /* Zero means do not send any signal */ 1049 if (data < 0 || data > _SIG_MAXSIG) { 1050 error = EINVAL; 1051 break; 1052 } 1053 1054 switch (req) { 1055 case PT_STEP: 1056 CTR3(KTR_PTRACE, "PT_STEP: tid %d (pid %d), sig = %d", 1057 td2->td_tid, p->p_pid, data); 1058 error = ptrace_single_step(td2); 1059 if (error) 1060 goto out; 1061 break; 1062 case PT_CONTINUE: 1063 case PT_TO_SCE: 1064 case PT_TO_SCX: 1065 case PT_SYSCALL: 1066 if (addr != (void *)1) { 1067 error = ptrace_set_pc(td2, 1068 (u_long)(uintfptr_t)addr); 1069 if (error) 1070 goto out; 1071 } 1072 switch (req) { 1073 case PT_TO_SCE: 1074 p->p_ptevents |= PTRACE_SCE; 1075 CTR4(KTR_PTRACE, 1076 "PT_TO_SCE: pid %d, events = %#x, PC = %#lx, sig = %d", 1077 p->p_pid, p->p_ptevents, 1078 (u_long)(uintfptr_t)addr, data); 1079 break; 1080 case PT_TO_SCX: 1081 p->p_ptevents |= PTRACE_SCX; 1082 CTR4(KTR_PTRACE, 1083 "PT_TO_SCX: pid %d, events = %#x, PC = %#lx, sig = %d", 1084 p->p_pid, p->p_ptevents, 1085 (u_long)(uintfptr_t)addr, data); 1086 break; 1087 case PT_SYSCALL: 1088 p->p_ptevents |= PTRACE_SYSCALL; 1089 CTR4(KTR_PTRACE, 1090 "PT_SYSCALL: pid %d, events = %#x, PC = %#lx, sig = %d", 1091 p->p_pid, p->p_ptevents, 1092 (u_long)(uintfptr_t)addr, data); 1093 break; 1094 case PT_CONTINUE: 1095 CTR3(KTR_PTRACE, 1096 "PT_CONTINUE: pid %d, PC = %#lx, sig = %d", 1097 p->p_pid, (u_long)(uintfptr_t)addr, data); 1098 break; 1099 } 1100 break; 1101 case PT_DETACH: 1102 /* 1103 * Clear P_TRACED before reparenting 1104 * a detached process back to its original 1105 * parent. Otherwise the debugee will be set 1106 * as an orphan of the debugger. 1107 */ 1108 p->p_flag &= ~(P_TRACED | P_WAITED); 1109 1110 /* 1111 * Reset the process parent. 1112 */ 1113 if (p->p_oppid != p->p_pptr->p_pid) { 1114 PROC_LOCK(p->p_pptr); 1115 sigqueue_take(p->p_ksi); 1116 PROC_UNLOCK(p->p_pptr); 1117 1118 pp = proc_realparent(p); 1119 proc_reparent(p, pp, false); 1120 if (pp == initproc) 1121 p->p_sigparent = SIGCHLD; 1122 CTR3(KTR_PTRACE, 1123 "PT_DETACH: pid %d reparented to pid %d, sig %d", 1124 p->p_pid, pp->p_pid, data); 1125 } else { 1126 CTR2(KTR_PTRACE, "PT_DETACH: pid %d, sig %d", 1127 p->p_pid, data); 1128 } 1129 1130 p->p_ptevents = 0; 1131 FOREACH_THREAD_IN_PROC(p, td3) { 1132 if ((td3->td_dbgflags & TDB_FSTP) != 0) { 1133 sigqueue_delete(&td3->td_sigqueue, 1134 SIGSTOP); 1135 } 1136 td3->td_dbgflags &= ~(TDB_XSIG | TDB_FSTP | 1137 TDB_SUSPEND); 1138 } 1139 1140 if ((p->p_flag2 & P2_PTRACE_FSTP) != 0) { 1141 sigqueue_delete(&p->p_sigqueue, SIGSTOP); 1142 p->p_flag2 &= ~P2_PTRACE_FSTP; 1143 } 1144 1145 /* should we send SIGCHLD? */ 1146 /* childproc_continued(p); */ 1147 break; 1148 } 1149 1150 sx_xunlock(&proctree_lock); 1151 proctree_locked = false; 1152 1153 sendsig: 1154 MPASS(!proctree_locked); 1155 1156 /* 1157 * Clear the pending event for the thread that just 1158 * reported its event (p_xthread). This may not be 1159 * the thread passed to PT_CONTINUE, PT_STEP, etc. if 1160 * the debugger is resuming a different thread. 1161 * 1162 * Deliver any pending signal via the reporting thread. 1163 */ 1164 MPASS(p->p_xthread != NULL); 1165 p->p_xthread->td_dbgflags &= ~TDB_XSIG; 1166 p->p_xthread->td_xsig = data; 1167 p->p_xthread = NULL; 1168 p->p_xsig = data; 1169 1170 /* 1171 * P_WKILLED is insurance that a PT_KILL/SIGKILL 1172 * always works immediately, even if another thread is 1173 * unsuspended first and attempts to handle a 1174 * different signal or if the POSIX.1b style signal 1175 * queue cannot accommodate any new signals. 1176 */ 1177 if (data == SIGKILL) 1178 proc_wkilled(p); 1179 1180 /* 1181 * Unsuspend all threads. To leave a thread 1182 * suspended, use PT_SUSPEND to suspend it before 1183 * continuing the process. 1184 */ 1185 ptrace_unsuspend(p); 1186 break; 1187 1188 case PT_WRITE_I: 1189 case PT_WRITE_D: 1190 td2->td_dbgflags |= TDB_USERWR; 1191 PROC_UNLOCK(p); 1192 error = 0; 1193 if (proc_writemem(td, p, (off_t)(uintptr_t)addr, &data, 1194 sizeof(int)) != sizeof(int)) 1195 error = ENOMEM; 1196 else 1197 CTR3(KTR_PTRACE, "PT_WRITE: pid %d: %p <= %#x", 1198 p->p_pid, addr, data); 1199 PROC_LOCK(p); 1200 break; 1201 1202 case PT_READ_I: 1203 case PT_READ_D: 1204 PROC_UNLOCK(p); 1205 error = tmp = 0; 1206 if (proc_readmem(td, p, (off_t)(uintptr_t)addr, &tmp, 1207 sizeof(int)) != sizeof(int)) 1208 error = ENOMEM; 1209 else 1210 CTR3(KTR_PTRACE, "PT_READ: pid %d: %p >= %#x", 1211 p->p_pid, addr, tmp); 1212 td->td_retval[0] = tmp; 1213 PROC_LOCK(p); 1214 break; 1215 1216 case PT_IO: 1217 piod = addr; 1218 iov.iov_base = piod->piod_addr; 1219 iov.iov_len = piod->piod_len; 1220 uio.uio_offset = (off_t)(uintptr_t)piod->piod_offs; 1221 uio.uio_resid = piod->piod_len; 1222 uio.uio_iov = &iov; 1223 uio.uio_iovcnt = 1; 1224 uio.uio_segflg = UIO_USERSPACE; 1225 uio.uio_td = td; 1226 switch (piod->piod_op) { 1227 case PIOD_READ_D: 1228 case PIOD_READ_I: 1229 CTR3(KTR_PTRACE, "PT_IO: pid %d: READ (%p, %#x)", 1230 p->p_pid, (uintptr_t)uio.uio_offset, uio.uio_resid); 1231 uio.uio_rw = UIO_READ; 1232 break; 1233 case PIOD_WRITE_D: 1234 case PIOD_WRITE_I: 1235 CTR3(KTR_PTRACE, "PT_IO: pid %d: WRITE (%p, %#x)", 1236 p->p_pid, (uintptr_t)uio.uio_offset, uio.uio_resid); 1237 td2->td_dbgflags |= TDB_USERWR; 1238 uio.uio_rw = UIO_WRITE; 1239 break; 1240 default: 1241 error = EINVAL; 1242 goto out; 1243 } 1244 PROC_UNLOCK(p); 1245 error = proc_rwmem(p, &uio); 1246 piod->piod_len -= uio.uio_resid; 1247 PROC_LOCK(p); 1248 break; 1249 1250 case PT_KILL: 1251 CTR1(KTR_PTRACE, "PT_KILL: pid %d", p->p_pid); 1252 data = SIGKILL; 1253 goto sendsig; /* in PT_CONTINUE above */ 1254 1255 case PT_SETREGS: 1256 CTR2(KTR_PTRACE, "PT_SETREGS: tid %d (pid %d)", td2->td_tid, 1257 p->p_pid); 1258 td2->td_dbgflags |= TDB_USERWR; 1259 error = PROC_WRITE(regs, td2, addr); 1260 break; 1261 1262 case PT_GETREGS: 1263 CTR2(KTR_PTRACE, "PT_GETREGS: tid %d (pid %d)", td2->td_tid, 1264 p->p_pid); 1265 error = PROC_READ(regs, td2, addr); 1266 break; 1267 1268 case PT_SETFPREGS: 1269 CTR2(KTR_PTRACE, "PT_SETFPREGS: tid %d (pid %d)", td2->td_tid, 1270 p->p_pid); 1271 td2->td_dbgflags |= TDB_USERWR; 1272 error = PROC_WRITE(fpregs, td2, addr); 1273 break; 1274 1275 case PT_GETFPREGS: 1276 CTR2(KTR_PTRACE, "PT_GETFPREGS: tid %d (pid %d)", td2->td_tid, 1277 p->p_pid); 1278 error = PROC_READ(fpregs, td2, addr); 1279 break; 1280 1281 case PT_SETDBREGS: 1282 CTR2(KTR_PTRACE, "PT_SETDBREGS: tid %d (pid %d)", td2->td_tid, 1283 p->p_pid); 1284 td2->td_dbgflags |= TDB_USERWR; 1285 error = PROC_WRITE(dbregs, td2, addr); 1286 break; 1287 1288 case PT_GETDBREGS: 1289 CTR2(KTR_PTRACE, "PT_GETDBREGS: tid %d (pid %d)", td2->td_tid, 1290 p->p_pid); 1291 error = PROC_READ(dbregs, td2, addr); 1292 break; 1293 1294 case PT_LWPINFO: 1295 if (data <= 0 || data > sizeof(*pl)) { 1296 error = EINVAL; 1297 break; 1298 } 1299 pl = addr; 1300 bzero(pl, sizeof(*pl)); 1301 pl->pl_lwpid = td2->td_tid; 1302 pl->pl_event = PL_EVENT_NONE; 1303 pl->pl_flags = 0; 1304 if (td2->td_dbgflags & TDB_XSIG) { 1305 pl->pl_event = PL_EVENT_SIGNAL; 1306 if (td2->td_si.si_signo != 0 && 1307 data >= offsetof(struct ptrace_lwpinfo, pl_siginfo) 1308 + sizeof(pl->pl_siginfo)){ 1309 pl->pl_flags |= PL_FLAG_SI; 1310 pl->pl_siginfo = td2->td_si; 1311 } 1312 } 1313 if (td2->td_dbgflags & TDB_SCE) 1314 pl->pl_flags |= PL_FLAG_SCE; 1315 else if (td2->td_dbgflags & TDB_SCX) 1316 pl->pl_flags |= PL_FLAG_SCX; 1317 if (td2->td_dbgflags & TDB_EXEC) 1318 pl->pl_flags |= PL_FLAG_EXEC; 1319 if (td2->td_dbgflags & TDB_FORK) { 1320 pl->pl_flags |= PL_FLAG_FORKED; 1321 pl->pl_child_pid = td2->td_dbg_forked; 1322 if (td2->td_dbgflags & TDB_VFORK) 1323 pl->pl_flags |= PL_FLAG_VFORKED; 1324 } else if ((td2->td_dbgflags & (TDB_SCX | TDB_VFORK)) == 1325 TDB_VFORK) 1326 pl->pl_flags |= PL_FLAG_VFORK_DONE; 1327 if (td2->td_dbgflags & TDB_CHILD) 1328 pl->pl_flags |= PL_FLAG_CHILD; 1329 if (td2->td_dbgflags & TDB_BORN) 1330 pl->pl_flags |= PL_FLAG_BORN; 1331 if (td2->td_dbgflags & TDB_EXIT) 1332 pl->pl_flags |= PL_FLAG_EXITED; 1333 pl->pl_sigmask = td2->td_sigmask; 1334 pl->pl_siglist = td2->td_siglist; 1335 strcpy(pl->pl_tdname, td2->td_name); 1336 if ((td2->td_dbgflags & (TDB_SCE | TDB_SCX)) != 0) { 1337 pl->pl_syscall_code = td2->td_sa.code; 1338 pl->pl_syscall_narg = td2->td_sa.callp->sy_narg; 1339 } else { 1340 pl->pl_syscall_code = 0; 1341 pl->pl_syscall_narg = 0; 1342 } 1343 CTR6(KTR_PTRACE, 1344 "PT_LWPINFO: tid %d (pid %d) event %d flags %#x child pid %d syscall %d", 1345 td2->td_tid, p->p_pid, pl->pl_event, pl->pl_flags, 1346 pl->pl_child_pid, pl->pl_syscall_code); 1347 break; 1348 1349 case PT_GETNUMLWPS: 1350 CTR2(KTR_PTRACE, "PT_GETNUMLWPS: pid %d: %d threads", p->p_pid, 1351 p->p_numthreads); 1352 td->td_retval[0] = p->p_numthreads; 1353 break; 1354 1355 case PT_GETLWPLIST: 1356 CTR3(KTR_PTRACE, "PT_GETLWPLIST: pid %d: data %d, actual %d", 1357 p->p_pid, data, p->p_numthreads); 1358 if (data <= 0) { 1359 error = EINVAL; 1360 break; 1361 } 1362 num = imin(p->p_numthreads, data); 1363 PROC_UNLOCK(p); 1364 buf = malloc(num * sizeof(lwpid_t), M_TEMP, M_WAITOK); 1365 tmp = 0; 1366 PROC_LOCK(p); 1367 FOREACH_THREAD_IN_PROC(p, td2) { 1368 if (tmp >= num) 1369 break; 1370 buf[tmp++] = td2->td_tid; 1371 } 1372 PROC_UNLOCK(p); 1373 error = copyout(buf, addr, tmp * sizeof(lwpid_t)); 1374 free(buf, M_TEMP); 1375 if (!error) 1376 td->td_retval[0] = tmp; 1377 PROC_LOCK(p); 1378 break; 1379 1380 case PT_VM_TIMESTAMP: 1381 CTR2(KTR_PTRACE, "PT_VM_TIMESTAMP: pid %d: timestamp %d", 1382 p->p_pid, p->p_vmspace->vm_map.timestamp); 1383 td->td_retval[0] = p->p_vmspace->vm_map.timestamp; 1384 break; 1385 1386 case PT_VM_ENTRY: 1387 PROC_UNLOCK(p); 1388 error = ptrace_vm_entry(td, p, addr); 1389 PROC_LOCK(p); 1390 break; 1391 1392 case PT_COREDUMP: 1393 pc = addr; 1394 CTR2(KTR_PTRACE, "PT_COREDUMP: pid %d, fd %d", 1395 p->p_pid, pc->pc_fd); 1396 1397 if ((pc->pc_flags & ~(PC_COMPRESS | PC_ALL)) != 0) { 1398 error = EINVAL; 1399 break; 1400 } 1401 PROC_UNLOCK(p); 1402 1403 tcq = malloc(sizeof(*tcq), M_TEMP, M_WAITOK | M_ZERO); 1404 fp = NULL; 1405 error = fget_write(td, pc->pc_fd, &cap_write_rights, &fp); 1406 if (error != 0) 1407 goto coredump_cleanup_nofp; 1408 if (fp->f_type != DTYPE_VNODE || fp->f_vnode->v_type != VREG) { 1409 error = EPIPE; 1410 goto coredump_cleanup; 1411 } 1412 1413 PROC_LOCK(p); 1414 error = proc_can_ptrace(td, p); 1415 if (error != 0) 1416 goto coredump_cleanup_locked; 1417 1418 td2 = ptrace_sel_coredump_thread(p); 1419 if (td2 == NULL) { 1420 error = EBUSY; 1421 goto coredump_cleanup_locked; 1422 } 1423 KASSERT((td2->td_dbgflags & TDB_COREDUMPRQ) == 0, 1424 ("proc %d tid %d req coredump", p->p_pid, td2->td_tid)); 1425 1426 tcq->tc_vp = fp->f_vnode; 1427 tcq->tc_limit = pc->pc_limit == 0 ? OFF_MAX : pc->pc_limit; 1428 tcq->tc_flags = SVC_PT_COREDUMP; 1429 if ((pc->pc_flags & PC_COMPRESS) == 0) 1430 tcq->tc_flags |= SVC_NOCOMPRESS; 1431 if ((pc->pc_flags & PC_ALL) != 0) 1432 tcq->tc_flags |= SVC_ALL; 1433 td2->td_coredump = tcq; 1434 td2->td_dbgflags |= TDB_COREDUMPRQ; 1435 thread_run_flash(td2); 1436 while ((td2->td_dbgflags & TDB_COREDUMPRQ) != 0) 1437 msleep(p, &p->p_mtx, PPAUSE, "crdmp", 0); 1438 error = tcq->tc_error; 1439 coredump_cleanup_locked: 1440 PROC_UNLOCK(p); 1441 coredump_cleanup: 1442 fdrop(fp, td); 1443 coredump_cleanup_nofp: 1444 free(tcq, M_TEMP); 1445 PROC_LOCK(p); 1446 break; 1447 1448 default: 1449 #ifdef __HAVE_PTRACE_MACHDEP 1450 if (req >= PT_FIRSTMACH) { 1451 PROC_UNLOCK(p); 1452 error = cpu_ptrace(td2, req, addr, data); 1453 PROC_LOCK(p); 1454 } else 1455 #endif 1456 /* Unknown request. */ 1457 error = EINVAL; 1458 break; 1459 } 1460 out: 1461 /* Drop our hold on this process now that the request has completed. */ 1462 _PRELE(p); 1463 fail: 1464 if (p2_req_set) { 1465 if ((p->p_flag2 & P2_PTRACEREQ) != 0) 1466 wakeup(&p->p_flag2); 1467 p->p_flag2 &= ~P2_PTRACEREQ; 1468 } 1469 PROC_UNLOCK(p); 1470 if (proctree_locked) 1471 sx_xunlock(&proctree_lock); 1472 return (error); 1473 } 1474 #undef PROC_READ 1475 #undef PROC_WRITE 1476