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