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 = 0; 484 485 AUDIT_ARG_PID(uap->pid); 486 AUDIT_ARG_CMD(uap->req); 487 AUDIT_ARG_VALUE(uap->data); 488 addr = &r; 489 switch (uap->req) { 490 case PT_GET_EVENT_MASK: 491 case PT_LWPINFO: 492 case PT_GET_SC_ARGS: 493 case PT_GET_SC_RET: 494 break; 495 case PT_GETREGS: 496 bzero(&r.reg, sizeof(r.reg)); 497 break; 498 case PT_GETFPREGS: 499 bzero(&r.fpreg, sizeof(r.fpreg)); 500 break; 501 case PT_GETDBREGS: 502 bzero(&r.dbreg, sizeof(r.dbreg)); 503 break; 504 case PT_SETREGS: 505 error = copyin(uap->addr, &r.reg, sizeof(r.reg)); 506 break; 507 case PT_SETFPREGS: 508 error = copyin(uap->addr, &r.fpreg, sizeof(r.fpreg)); 509 break; 510 case PT_SETDBREGS: 511 error = copyin(uap->addr, &r.dbreg, sizeof(r.dbreg)); 512 break; 513 case PT_SET_EVENT_MASK: 514 if (uap->data != sizeof(r.ptevents)) 515 error = EINVAL; 516 else 517 error = copyin(uap->addr, &r.ptevents, uap->data); 518 break; 519 case PT_IO: 520 error = copyin(uap->addr, &r.piod, sizeof(r.piod)); 521 break; 522 case PT_VM_ENTRY: 523 error = copyin(uap->addr, &r.pve, sizeof(r.pve)); 524 break; 525 case PT_COREDUMP: 526 if (uap->data != sizeof(r.pc)) 527 error = EINVAL; 528 else 529 error = copyin(uap->addr, &r.pc, uap->data); 530 break; 531 default: 532 addr = uap->addr; 533 break; 534 } 535 if (error) 536 return (error); 537 538 error = kern_ptrace(td, uap->req, uap->pid, addr, uap->data); 539 if (error) 540 return (error); 541 542 switch (uap->req) { 543 case PT_VM_ENTRY: 544 error = copyout(&r.pve, uap->addr, sizeof(r.pve)); 545 break; 546 case PT_IO: 547 error = copyout(&r.piod, uap->addr, sizeof(r.piod)); 548 break; 549 case PT_GETREGS: 550 error = copyout(&r.reg, uap->addr, sizeof(r.reg)); 551 break; 552 case PT_GETFPREGS: 553 error = copyout(&r.fpreg, uap->addr, sizeof(r.fpreg)); 554 break; 555 case PT_GETDBREGS: 556 error = copyout(&r.dbreg, uap->addr, sizeof(r.dbreg)); 557 break; 558 case PT_GET_EVENT_MASK: 559 /* NB: The size in uap->data is validated in kern_ptrace(). */ 560 error = copyout(&r.ptevents, uap->addr, uap->data); 561 break; 562 case PT_LWPINFO: 563 /* NB: The size in uap->data is validated in kern_ptrace(). */ 564 error = copyout(&r.pl, uap->addr, uap->data); 565 break; 566 case PT_GET_SC_ARGS: 567 error = copyout(r.args, uap->addr, MIN(uap->data, 568 sizeof(r.args))); 569 break; 570 case PT_GET_SC_RET: 571 error = copyout(&r.psr, uap->addr, MIN(uap->data, 572 sizeof(r.psr))); 573 break; 574 } 575 576 return (error); 577 } 578 579 #ifdef COMPAT_FREEBSD32 580 /* 581 * PROC_READ(regs, td2, addr); 582 * becomes either: 583 * proc_read_regs(td2, addr); 584 * or 585 * proc_read_regs32(td2, addr); 586 * .. except this is done at runtime. There is an additional 587 * complication in that PROC_WRITE disallows 32 bit consumers 588 * from writing to 64 bit address space targets. 589 */ 590 #define PROC_READ(w, t, a) wrap32 ? \ 591 proc_read_ ## w ## 32(t, a) : \ 592 proc_read_ ## w (t, a) 593 #define PROC_WRITE(w, t, a) wrap32 ? \ 594 (safe ? proc_write_ ## w ## 32(t, a) : EINVAL ) : \ 595 proc_write_ ## w (t, a) 596 #else 597 #define PROC_READ(w, t, a) proc_read_ ## w (t, a) 598 #define PROC_WRITE(w, t, a) proc_write_ ## w (t, a) 599 #endif 600 601 void 602 proc_set_traced(struct proc *p, bool stop) 603 { 604 605 sx_assert(&proctree_lock, SX_XLOCKED); 606 PROC_LOCK_ASSERT(p, MA_OWNED); 607 p->p_flag |= P_TRACED; 608 if (stop) 609 p->p_flag2 |= P2_PTRACE_FSTP; 610 p->p_ptevents = PTRACE_DEFAULT; 611 } 612 613 static int 614 proc_can_ptrace(struct thread *td, struct proc *p) 615 { 616 int error; 617 618 PROC_LOCK_ASSERT(p, MA_OWNED); 619 620 if ((p->p_flag & P_WEXIT) != 0) 621 return (ESRCH); 622 623 if ((error = p_cansee(td, p)) != 0) 624 return (error); 625 if ((error = p_candebug(td, p)) != 0) 626 return (error); 627 628 /* not being traced... */ 629 if ((p->p_flag & P_TRACED) == 0) 630 return (EPERM); 631 632 /* not being traced by YOU */ 633 if (p->p_pptr != td->td_proc) 634 return (EBUSY); 635 636 /* not currently stopped */ 637 if ((p->p_flag & P_STOPPED_TRACE) == 0 || 638 p->p_suspcount != p->p_numthreads || 639 (p->p_flag & P_WAITED) == 0) 640 return (EBUSY); 641 642 return (0); 643 } 644 645 static struct thread * 646 ptrace_sel_coredump_thread(struct proc *p) 647 { 648 struct thread *td2; 649 650 PROC_LOCK_ASSERT(p, MA_OWNED); 651 MPASS((p->p_flag & P_STOPPED_TRACE) != 0); 652 653 FOREACH_THREAD_IN_PROC(p, td2) { 654 if ((td2->td_dbgflags & TDB_SSWITCH) != 0) 655 return (td2); 656 } 657 return (NULL); 658 } 659 660 int 661 kern_ptrace(struct thread *td, int req, pid_t pid, void *addr, int data) 662 { 663 struct iovec iov; 664 struct uio uio; 665 struct proc *curp, *p, *pp; 666 struct thread *td2 = NULL, *td3; 667 struct ptrace_io_desc *piod = NULL; 668 struct ptrace_lwpinfo *pl; 669 struct ptrace_sc_ret *psr; 670 struct file *fp; 671 struct ptrace_coredump *pc; 672 struct thr_coredump_req *tcq; 673 int error, num, tmp; 674 lwpid_t tid = 0, *buf; 675 #ifdef COMPAT_FREEBSD32 676 int wrap32 = 0, safe = 0; 677 #endif 678 bool proctree_locked, p2_req_set; 679 680 curp = td->td_proc; 681 proctree_locked = false; 682 p2_req_set = false; 683 684 /* Lock proctree before locking the process. */ 685 switch (req) { 686 case PT_TRACE_ME: 687 case PT_ATTACH: 688 case PT_STEP: 689 case PT_CONTINUE: 690 case PT_TO_SCE: 691 case PT_TO_SCX: 692 case PT_SYSCALL: 693 case PT_FOLLOW_FORK: 694 case PT_LWP_EVENTS: 695 case PT_GET_EVENT_MASK: 696 case PT_SET_EVENT_MASK: 697 case PT_DETACH: 698 case PT_GET_SC_ARGS: 699 sx_xlock(&proctree_lock); 700 proctree_locked = true; 701 break; 702 default: 703 break; 704 } 705 706 if (req == PT_TRACE_ME) { 707 p = td->td_proc; 708 PROC_LOCK(p); 709 } else { 710 if (pid <= PID_MAX) { 711 if ((p = pfind(pid)) == NULL) { 712 if (proctree_locked) 713 sx_xunlock(&proctree_lock); 714 return (ESRCH); 715 } 716 } else { 717 td2 = tdfind(pid, -1); 718 if (td2 == NULL) { 719 if (proctree_locked) 720 sx_xunlock(&proctree_lock); 721 return (ESRCH); 722 } 723 p = td2->td_proc; 724 tid = pid; 725 pid = p->p_pid; 726 } 727 } 728 AUDIT_ARG_PROCESS(p); 729 730 if ((p->p_flag & P_WEXIT) != 0) { 731 error = ESRCH; 732 goto fail; 733 } 734 if ((error = p_cansee(td, p)) != 0) 735 goto fail; 736 737 if ((error = p_candebug(td, p)) != 0) 738 goto fail; 739 740 /* 741 * System processes can't be debugged. 742 */ 743 if ((p->p_flag & P_SYSTEM) != 0) { 744 error = EINVAL; 745 goto fail; 746 } 747 748 if (tid == 0) { 749 if ((p->p_flag & P_STOPPED_TRACE) != 0) { 750 KASSERT(p->p_xthread != NULL, ("NULL p_xthread")); 751 td2 = p->p_xthread; 752 } else { 753 td2 = FIRST_THREAD_IN_PROC(p); 754 } 755 tid = td2->td_tid; 756 } 757 758 #ifdef COMPAT_FREEBSD32 759 /* 760 * Test if we're a 32 bit client and what the target is. 761 * Set the wrap controls accordingly. 762 */ 763 if (SV_CURPROC_FLAG(SV_ILP32)) { 764 if (SV_PROC_FLAG(td2->td_proc, SV_ILP32)) 765 safe = 1; 766 wrap32 = 1; 767 } 768 #endif 769 /* 770 * Permissions check 771 */ 772 switch (req) { 773 case PT_TRACE_ME: 774 /* 775 * Always legal, when there is a parent process which 776 * could trace us. Otherwise, reject. 777 */ 778 if ((p->p_flag & P_TRACED) != 0) { 779 error = EBUSY; 780 goto fail; 781 } 782 if (p->p_pptr == initproc) { 783 error = EPERM; 784 goto fail; 785 } 786 break; 787 788 case PT_ATTACH: 789 /* Self */ 790 if (p == td->td_proc) { 791 error = EINVAL; 792 goto fail; 793 } 794 795 /* Already traced */ 796 if (p->p_flag & P_TRACED) { 797 error = EBUSY; 798 goto fail; 799 } 800 801 /* Can't trace an ancestor if you're being traced. */ 802 if (curp->p_flag & P_TRACED) { 803 for (pp = curp->p_pptr; pp != NULL; pp = pp->p_pptr) { 804 if (pp == p) { 805 error = EINVAL; 806 goto fail; 807 } 808 } 809 } 810 811 /* OK */ 812 break; 813 814 case PT_CLEARSTEP: 815 /* Allow thread to clear single step for itself */ 816 if (td->td_tid == tid) 817 break; 818 819 /* FALLTHROUGH */ 820 default: 821 /* 822 * Check for ptrace eligibility before waiting for 823 * holds to drain. 824 */ 825 error = proc_can_ptrace(td, p); 826 if (error != 0) 827 goto fail; 828 829 /* 830 * Block parallel ptrace requests. Most important, do 831 * not allow other thread in debugger to continue the 832 * debuggee until coredump finished. 833 */ 834 while ((p->p_flag2 & P2_PTRACEREQ) != 0) { 835 if (proctree_locked) 836 sx_xunlock(&proctree_lock); 837 error = msleep(&p->p_flag2, &p->p_mtx, PPAUSE | PCATCH | 838 (proctree_locked ? PDROP : 0), "pptrace", 0); 839 if (proctree_locked) { 840 sx_xlock(&proctree_lock); 841 PROC_LOCK(p); 842 } 843 if (error == 0 && td2->td_proc != p) 844 error = ESRCH; 845 if (error == 0) 846 error = proc_can_ptrace(td, p); 847 if (error != 0) 848 goto fail; 849 } 850 851 /* Ok */ 852 break; 853 } 854 855 /* 856 * Keep this process around and request parallel ptrace() 857 * request to wait until we finish this request. 858 */ 859 MPASS((p->p_flag2 & P2_PTRACEREQ) == 0); 860 p->p_flag2 |= P2_PTRACEREQ; 861 p2_req_set = true; 862 _PHOLD(p); 863 864 /* 865 * Actually do the requests 866 */ 867 868 td->td_retval[0] = 0; 869 870 switch (req) { 871 case PT_TRACE_ME: 872 /* set my trace flag and "owner" so it can read/write me */ 873 proc_set_traced(p, false); 874 if (p->p_flag & P_PPWAIT) 875 p->p_flag |= P_PPTRACE; 876 CTR1(KTR_PTRACE, "PT_TRACE_ME: pid %d", p->p_pid); 877 break; 878 879 case PT_ATTACH: 880 /* security check done above */ 881 /* 882 * It would be nice if the tracing relationship was separate 883 * from the parent relationship but that would require 884 * another set of links in the proc struct or for "wait" 885 * to scan the entire proc table. To make life easier, 886 * we just re-parent the process we're trying to trace. 887 * The old parent is remembered so we can put things back 888 * on a "detach". 889 */ 890 proc_set_traced(p, true); 891 proc_reparent(p, td->td_proc, false); 892 CTR2(KTR_PTRACE, "PT_ATTACH: pid %d, oppid %d", p->p_pid, 893 p->p_oppid); 894 895 sx_xunlock(&proctree_lock); 896 proctree_locked = false; 897 MPASS(p->p_xthread == NULL); 898 MPASS((p->p_flag & P_STOPPED_TRACE) == 0); 899 900 /* 901 * If already stopped due to a stop signal, clear the 902 * existing stop before triggering a traced SIGSTOP. 903 */ 904 if ((p->p_flag & P_STOPPED_SIG) != 0) { 905 PROC_SLOCK(p); 906 p->p_flag &= ~(P_STOPPED_SIG | P_WAITED); 907 thread_unsuspend(p); 908 PROC_SUNLOCK(p); 909 } 910 911 kern_psignal(p, SIGSTOP); 912 break; 913 914 case PT_CLEARSTEP: 915 CTR2(KTR_PTRACE, "PT_CLEARSTEP: tid %d (pid %d)", td2->td_tid, 916 p->p_pid); 917 error = ptrace_clear_single_step(td2); 918 break; 919 920 case PT_SETSTEP: 921 CTR2(KTR_PTRACE, "PT_SETSTEP: tid %d (pid %d)", td2->td_tid, 922 p->p_pid); 923 error = ptrace_single_step(td2); 924 break; 925 926 case PT_SUSPEND: 927 CTR2(KTR_PTRACE, "PT_SUSPEND: tid %d (pid %d)", td2->td_tid, 928 p->p_pid); 929 td2->td_dbgflags |= TDB_SUSPEND; 930 thread_lock(td2); 931 td2->td_flags |= TDF_NEEDSUSPCHK; 932 thread_unlock(td2); 933 break; 934 935 case PT_RESUME: 936 CTR2(KTR_PTRACE, "PT_RESUME: tid %d (pid %d)", td2->td_tid, 937 p->p_pid); 938 td2->td_dbgflags &= ~TDB_SUSPEND; 939 break; 940 941 case PT_FOLLOW_FORK: 942 CTR3(KTR_PTRACE, "PT_FOLLOW_FORK: pid %d %s -> %s", p->p_pid, 943 p->p_ptevents & PTRACE_FORK ? "enabled" : "disabled", 944 data ? "enabled" : "disabled"); 945 if (data) 946 p->p_ptevents |= PTRACE_FORK; 947 else 948 p->p_ptevents &= ~PTRACE_FORK; 949 break; 950 951 case PT_LWP_EVENTS: 952 CTR3(KTR_PTRACE, "PT_LWP_EVENTS: pid %d %s -> %s", p->p_pid, 953 p->p_ptevents & PTRACE_LWP ? "enabled" : "disabled", 954 data ? "enabled" : "disabled"); 955 if (data) 956 p->p_ptevents |= PTRACE_LWP; 957 else 958 p->p_ptevents &= ~PTRACE_LWP; 959 break; 960 961 case PT_GET_EVENT_MASK: 962 if (data != sizeof(p->p_ptevents)) { 963 error = EINVAL; 964 break; 965 } 966 CTR2(KTR_PTRACE, "PT_GET_EVENT_MASK: pid %d mask %#x", p->p_pid, 967 p->p_ptevents); 968 *(int *)addr = p->p_ptevents; 969 break; 970 971 case PT_SET_EVENT_MASK: 972 if (data != sizeof(p->p_ptevents)) { 973 error = EINVAL; 974 break; 975 } 976 tmp = *(int *)addr; 977 if ((tmp & ~(PTRACE_EXEC | PTRACE_SCE | PTRACE_SCX | 978 PTRACE_FORK | PTRACE_LWP | PTRACE_VFORK)) != 0) { 979 error = EINVAL; 980 break; 981 } 982 CTR3(KTR_PTRACE, "PT_SET_EVENT_MASK: pid %d mask %#x -> %#x", 983 p->p_pid, p->p_ptevents, tmp); 984 p->p_ptevents = tmp; 985 break; 986 987 case PT_GET_SC_ARGS: 988 CTR1(KTR_PTRACE, "PT_GET_SC_ARGS: pid %d", p->p_pid); 989 if ((td2->td_dbgflags & (TDB_SCE | TDB_SCX)) == 0 990 #ifdef COMPAT_FREEBSD32 991 || (wrap32 && !safe) 992 #endif 993 ) { 994 error = EINVAL; 995 break; 996 } 997 bzero(addr, sizeof(td2->td_sa.args)); 998 bcopy(td2->td_sa.args, addr, td2->td_sa.callp->sy_narg * 999 sizeof(register_t)); 1000 break; 1001 1002 case PT_GET_SC_RET: 1003 if ((td2->td_dbgflags & (TDB_SCX)) == 0 1004 #ifdef COMPAT_FREEBSD32 1005 || (wrap32 && !safe) 1006 #endif 1007 ) { 1008 error = EINVAL; 1009 break; 1010 } 1011 psr = addr; 1012 bzero(psr, sizeof(*psr)); 1013 psr->sr_error = td2->td_errno; 1014 if (psr->sr_error == 0) { 1015 psr->sr_retval[0] = td2->td_retval[0]; 1016 psr->sr_retval[1] = td2->td_retval[1]; 1017 } 1018 CTR4(KTR_PTRACE, 1019 "PT_GET_SC_RET: pid %d error %d retval %#lx,%#lx", 1020 p->p_pid, psr->sr_error, psr->sr_retval[0], 1021 psr->sr_retval[1]); 1022 break; 1023 1024 case PT_STEP: 1025 case PT_CONTINUE: 1026 case PT_TO_SCE: 1027 case PT_TO_SCX: 1028 case PT_SYSCALL: 1029 case PT_DETACH: 1030 /* Zero means do not send any signal */ 1031 if (data < 0 || data > _SIG_MAXSIG) { 1032 error = EINVAL; 1033 break; 1034 } 1035 1036 switch (req) { 1037 case PT_STEP: 1038 CTR3(KTR_PTRACE, "PT_STEP: tid %d (pid %d), sig = %d", 1039 td2->td_tid, p->p_pid, data); 1040 error = ptrace_single_step(td2); 1041 if (error) 1042 goto out; 1043 break; 1044 case PT_CONTINUE: 1045 case PT_TO_SCE: 1046 case PT_TO_SCX: 1047 case PT_SYSCALL: 1048 if (addr != (void *)1) { 1049 error = ptrace_set_pc(td2, 1050 (u_long)(uintfptr_t)addr); 1051 if (error) 1052 goto out; 1053 } 1054 switch (req) { 1055 case PT_TO_SCE: 1056 p->p_ptevents |= PTRACE_SCE; 1057 CTR4(KTR_PTRACE, 1058 "PT_TO_SCE: pid %d, events = %#x, PC = %#lx, sig = %d", 1059 p->p_pid, p->p_ptevents, 1060 (u_long)(uintfptr_t)addr, data); 1061 break; 1062 case PT_TO_SCX: 1063 p->p_ptevents |= PTRACE_SCX; 1064 CTR4(KTR_PTRACE, 1065 "PT_TO_SCX: pid %d, events = %#x, PC = %#lx, sig = %d", 1066 p->p_pid, p->p_ptevents, 1067 (u_long)(uintfptr_t)addr, data); 1068 break; 1069 case PT_SYSCALL: 1070 p->p_ptevents |= PTRACE_SYSCALL; 1071 CTR4(KTR_PTRACE, 1072 "PT_SYSCALL: pid %d, events = %#x, PC = %#lx, sig = %d", 1073 p->p_pid, p->p_ptevents, 1074 (u_long)(uintfptr_t)addr, data); 1075 break; 1076 case PT_CONTINUE: 1077 CTR3(KTR_PTRACE, 1078 "PT_CONTINUE: pid %d, PC = %#lx, sig = %d", 1079 p->p_pid, (u_long)(uintfptr_t)addr, data); 1080 break; 1081 } 1082 break; 1083 case PT_DETACH: 1084 /* 1085 * Clear P_TRACED before reparenting 1086 * a detached process back to its original 1087 * parent. Otherwise the debugee will be set 1088 * as an orphan of the debugger. 1089 */ 1090 p->p_flag &= ~(P_TRACED | P_WAITED); 1091 1092 /* 1093 * Reset the process parent. 1094 */ 1095 if (p->p_oppid != p->p_pptr->p_pid) { 1096 PROC_LOCK(p->p_pptr); 1097 sigqueue_take(p->p_ksi); 1098 PROC_UNLOCK(p->p_pptr); 1099 1100 pp = proc_realparent(p); 1101 proc_reparent(p, pp, false); 1102 if (pp == initproc) 1103 p->p_sigparent = SIGCHLD; 1104 CTR3(KTR_PTRACE, 1105 "PT_DETACH: pid %d reparented to pid %d, sig %d", 1106 p->p_pid, pp->p_pid, data); 1107 } else { 1108 CTR2(KTR_PTRACE, "PT_DETACH: pid %d, sig %d", 1109 p->p_pid, data); 1110 } 1111 1112 p->p_ptevents = 0; 1113 FOREACH_THREAD_IN_PROC(p, td3) { 1114 if ((td3->td_dbgflags & TDB_FSTP) != 0) { 1115 sigqueue_delete(&td3->td_sigqueue, 1116 SIGSTOP); 1117 } 1118 td3->td_dbgflags &= ~(TDB_XSIG | TDB_FSTP | 1119 TDB_SUSPEND); 1120 } 1121 1122 if ((p->p_flag2 & P2_PTRACE_FSTP) != 0) { 1123 sigqueue_delete(&p->p_sigqueue, SIGSTOP); 1124 p->p_flag2 &= ~P2_PTRACE_FSTP; 1125 } 1126 1127 /* should we send SIGCHLD? */ 1128 /* childproc_continued(p); */ 1129 break; 1130 } 1131 1132 sx_xunlock(&proctree_lock); 1133 proctree_locked = false; 1134 1135 sendsig: 1136 MPASS(!proctree_locked); 1137 1138 /* 1139 * Clear the pending event for the thread that just 1140 * reported its event (p_xthread). This may not be 1141 * the thread passed to PT_CONTINUE, PT_STEP, etc. if 1142 * the debugger is resuming a different thread. 1143 * 1144 * Deliver any pending signal via the reporting thread. 1145 */ 1146 MPASS(p->p_xthread != NULL); 1147 p->p_xthread->td_dbgflags &= ~TDB_XSIG; 1148 p->p_xthread->td_xsig = data; 1149 p->p_xthread = NULL; 1150 p->p_xsig = data; 1151 1152 /* 1153 * P_WKILLED is insurance that a PT_KILL/SIGKILL 1154 * always works immediately, even if another thread is 1155 * unsuspended first and attempts to handle a 1156 * different signal or if the POSIX.1b style signal 1157 * queue cannot accommodate any new signals. 1158 */ 1159 if (data == SIGKILL) 1160 proc_wkilled(p); 1161 1162 /* 1163 * Unsuspend all threads. To leave a thread 1164 * suspended, use PT_SUSPEND to suspend it before 1165 * continuing the process. 1166 */ 1167 PROC_SLOCK(p); 1168 p->p_flag &= ~(P_STOPPED_TRACE | P_STOPPED_SIG | P_WAITED); 1169 thread_unsuspend(p); 1170 PROC_SUNLOCK(p); 1171 itimer_proc_continue(p); 1172 kqtimer_proc_continue(p); 1173 break; 1174 1175 case PT_WRITE_I: 1176 case PT_WRITE_D: 1177 td2->td_dbgflags |= TDB_USERWR; 1178 PROC_UNLOCK(p); 1179 error = 0; 1180 if (proc_writemem(td, p, (off_t)(uintptr_t)addr, &data, 1181 sizeof(int)) != sizeof(int)) 1182 error = ENOMEM; 1183 else 1184 CTR3(KTR_PTRACE, "PT_WRITE: pid %d: %p <= %#x", 1185 p->p_pid, addr, data); 1186 PROC_LOCK(p); 1187 break; 1188 1189 case PT_READ_I: 1190 case PT_READ_D: 1191 PROC_UNLOCK(p); 1192 error = tmp = 0; 1193 if (proc_readmem(td, p, (off_t)(uintptr_t)addr, &tmp, 1194 sizeof(int)) != sizeof(int)) 1195 error = ENOMEM; 1196 else 1197 CTR3(KTR_PTRACE, "PT_READ: pid %d: %p >= %#x", 1198 p->p_pid, addr, tmp); 1199 td->td_retval[0] = tmp; 1200 PROC_LOCK(p); 1201 break; 1202 1203 case PT_IO: 1204 piod = addr; 1205 iov.iov_base = piod->piod_addr; 1206 iov.iov_len = piod->piod_len; 1207 uio.uio_offset = (off_t)(uintptr_t)piod->piod_offs; 1208 uio.uio_resid = piod->piod_len; 1209 uio.uio_iov = &iov; 1210 uio.uio_iovcnt = 1; 1211 uio.uio_segflg = UIO_USERSPACE; 1212 uio.uio_td = td; 1213 switch (piod->piod_op) { 1214 case PIOD_READ_D: 1215 case PIOD_READ_I: 1216 CTR3(KTR_PTRACE, "PT_IO: pid %d: READ (%p, %#x)", 1217 p->p_pid, (uintptr_t)uio.uio_offset, uio.uio_resid); 1218 uio.uio_rw = UIO_READ; 1219 break; 1220 case PIOD_WRITE_D: 1221 case PIOD_WRITE_I: 1222 CTR3(KTR_PTRACE, "PT_IO: pid %d: WRITE (%p, %#x)", 1223 p->p_pid, (uintptr_t)uio.uio_offset, uio.uio_resid); 1224 td2->td_dbgflags |= TDB_USERWR; 1225 uio.uio_rw = UIO_WRITE; 1226 break; 1227 default: 1228 error = EINVAL; 1229 goto out; 1230 } 1231 PROC_UNLOCK(p); 1232 error = proc_rwmem(p, &uio); 1233 piod->piod_len -= uio.uio_resid; 1234 PROC_LOCK(p); 1235 break; 1236 1237 case PT_KILL: 1238 CTR1(KTR_PTRACE, "PT_KILL: pid %d", p->p_pid); 1239 data = SIGKILL; 1240 goto sendsig; /* in PT_CONTINUE above */ 1241 1242 case PT_SETREGS: 1243 CTR2(KTR_PTRACE, "PT_SETREGS: tid %d (pid %d)", td2->td_tid, 1244 p->p_pid); 1245 td2->td_dbgflags |= TDB_USERWR; 1246 error = PROC_WRITE(regs, td2, addr); 1247 break; 1248 1249 case PT_GETREGS: 1250 CTR2(KTR_PTRACE, "PT_GETREGS: tid %d (pid %d)", td2->td_tid, 1251 p->p_pid); 1252 error = PROC_READ(regs, td2, addr); 1253 break; 1254 1255 case PT_SETFPREGS: 1256 CTR2(KTR_PTRACE, "PT_SETFPREGS: tid %d (pid %d)", td2->td_tid, 1257 p->p_pid); 1258 td2->td_dbgflags |= TDB_USERWR; 1259 error = PROC_WRITE(fpregs, td2, addr); 1260 break; 1261 1262 case PT_GETFPREGS: 1263 CTR2(KTR_PTRACE, "PT_GETFPREGS: tid %d (pid %d)", td2->td_tid, 1264 p->p_pid); 1265 error = PROC_READ(fpregs, td2, addr); 1266 break; 1267 1268 case PT_SETDBREGS: 1269 CTR2(KTR_PTRACE, "PT_SETDBREGS: tid %d (pid %d)", td2->td_tid, 1270 p->p_pid); 1271 td2->td_dbgflags |= TDB_USERWR; 1272 error = PROC_WRITE(dbregs, td2, addr); 1273 break; 1274 1275 case PT_GETDBREGS: 1276 CTR2(KTR_PTRACE, "PT_GETDBREGS: tid %d (pid %d)", td2->td_tid, 1277 p->p_pid); 1278 error = PROC_READ(dbregs, td2, addr); 1279 break; 1280 1281 case PT_LWPINFO: 1282 if (data <= 0 || data > sizeof(*pl)) { 1283 error = EINVAL; 1284 break; 1285 } 1286 pl = addr; 1287 bzero(pl, sizeof(*pl)); 1288 pl->pl_lwpid = td2->td_tid; 1289 pl->pl_event = PL_EVENT_NONE; 1290 pl->pl_flags = 0; 1291 if (td2->td_dbgflags & TDB_XSIG) { 1292 pl->pl_event = PL_EVENT_SIGNAL; 1293 if (td2->td_si.si_signo != 0 && 1294 data >= offsetof(struct ptrace_lwpinfo, pl_siginfo) 1295 + sizeof(pl->pl_siginfo)){ 1296 pl->pl_flags |= PL_FLAG_SI; 1297 pl->pl_siginfo = td2->td_si; 1298 } 1299 } 1300 if (td2->td_dbgflags & TDB_SCE) 1301 pl->pl_flags |= PL_FLAG_SCE; 1302 else if (td2->td_dbgflags & TDB_SCX) 1303 pl->pl_flags |= PL_FLAG_SCX; 1304 if (td2->td_dbgflags & TDB_EXEC) 1305 pl->pl_flags |= PL_FLAG_EXEC; 1306 if (td2->td_dbgflags & TDB_FORK) { 1307 pl->pl_flags |= PL_FLAG_FORKED; 1308 pl->pl_child_pid = td2->td_dbg_forked; 1309 if (td2->td_dbgflags & TDB_VFORK) 1310 pl->pl_flags |= PL_FLAG_VFORKED; 1311 } else if ((td2->td_dbgflags & (TDB_SCX | TDB_VFORK)) == 1312 TDB_VFORK) 1313 pl->pl_flags |= PL_FLAG_VFORK_DONE; 1314 if (td2->td_dbgflags & TDB_CHILD) 1315 pl->pl_flags |= PL_FLAG_CHILD; 1316 if (td2->td_dbgflags & TDB_BORN) 1317 pl->pl_flags |= PL_FLAG_BORN; 1318 if (td2->td_dbgflags & TDB_EXIT) 1319 pl->pl_flags |= PL_FLAG_EXITED; 1320 pl->pl_sigmask = td2->td_sigmask; 1321 pl->pl_siglist = td2->td_siglist; 1322 strcpy(pl->pl_tdname, td2->td_name); 1323 if ((td2->td_dbgflags & (TDB_SCE | TDB_SCX)) != 0) { 1324 pl->pl_syscall_code = td2->td_sa.code; 1325 pl->pl_syscall_narg = td2->td_sa.callp->sy_narg; 1326 } else { 1327 pl->pl_syscall_code = 0; 1328 pl->pl_syscall_narg = 0; 1329 } 1330 CTR6(KTR_PTRACE, 1331 "PT_LWPINFO: tid %d (pid %d) event %d flags %#x child pid %d syscall %d", 1332 td2->td_tid, p->p_pid, pl->pl_event, pl->pl_flags, 1333 pl->pl_child_pid, pl->pl_syscall_code); 1334 break; 1335 1336 case PT_GETNUMLWPS: 1337 CTR2(KTR_PTRACE, "PT_GETNUMLWPS: pid %d: %d threads", p->p_pid, 1338 p->p_numthreads); 1339 td->td_retval[0] = p->p_numthreads; 1340 break; 1341 1342 case PT_GETLWPLIST: 1343 CTR3(KTR_PTRACE, "PT_GETLWPLIST: pid %d: data %d, actual %d", 1344 p->p_pid, data, p->p_numthreads); 1345 if (data <= 0) { 1346 error = EINVAL; 1347 break; 1348 } 1349 num = imin(p->p_numthreads, data); 1350 PROC_UNLOCK(p); 1351 buf = malloc(num * sizeof(lwpid_t), M_TEMP, M_WAITOK); 1352 tmp = 0; 1353 PROC_LOCK(p); 1354 FOREACH_THREAD_IN_PROC(p, td2) { 1355 if (tmp >= num) 1356 break; 1357 buf[tmp++] = td2->td_tid; 1358 } 1359 PROC_UNLOCK(p); 1360 error = copyout(buf, addr, tmp * sizeof(lwpid_t)); 1361 free(buf, M_TEMP); 1362 if (!error) 1363 td->td_retval[0] = tmp; 1364 PROC_LOCK(p); 1365 break; 1366 1367 case PT_VM_TIMESTAMP: 1368 CTR2(KTR_PTRACE, "PT_VM_TIMESTAMP: pid %d: timestamp %d", 1369 p->p_pid, p->p_vmspace->vm_map.timestamp); 1370 td->td_retval[0] = p->p_vmspace->vm_map.timestamp; 1371 break; 1372 1373 case PT_VM_ENTRY: 1374 PROC_UNLOCK(p); 1375 error = ptrace_vm_entry(td, p, addr); 1376 PROC_LOCK(p); 1377 break; 1378 1379 case PT_COREDUMP: 1380 pc = addr; 1381 CTR2(KTR_PTRACE, "PT_COREDUMP: pid %d, fd %d", 1382 p->p_pid, pc->pc_fd); 1383 1384 if ((pc->pc_flags & ~(PC_COMPRESS | PC_ALL)) != 0) { 1385 error = EINVAL; 1386 break; 1387 } 1388 PROC_UNLOCK(p); 1389 1390 tcq = malloc(sizeof(*tcq), M_TEMP, M_WAITOK | M_ZERO); 1391 fp = NULL; 1392 error = fget_write(td, pc->pc_fd, &cap_write_rights, &fp); 1393 if (error != 0) 1394 goto coredump_cleanup_nofp; 1395 if (fp->f_type != DTYPE_VNODE || fp->f_vnode->v_type != VREG) { 1396 error = EPIPE; 1397 goto coredump_cleanup; 1398 } 1399 1400 PROC_LOCK(p); 1401 error = proc_can_ptrace(td, p); 1402 if (error != 0) 1403 goto coredump_cleanup_locked; 1404 1405 td2 = ptrace_sel_coredump_thread(p); 1406 if (td2 == NULL) { 1407 error = EBUSY; 1408 goto coredump_cleanup_locked; 1409 } 1410 KASSERT((td2->td_dbgflags & TDB_COREDUMPRQ) == 0, 1411 ("proc %d tid %d req coredump", p->p_pid, td2->td_tid)); 1412 1413 tcq->tc_vp = fp->f_vnode; 1414 tcq->tc_limit = pc->pc_limit == 0 ? OFF_MAX : pc->pc_limit; 1415 tcq->tc_flags = SVC_PT_COREDUMP; 1416 if ((pc->pc_flags & PC_COMPRESS) == 0) 1417 tcq->tc_flags |= SVC_NOCOMPRESS; 1418 if ((pc->pc_flags & PC_ALL) != 0) 1419 tcq->tc_flags |= SVC_ALL; 1420 td2->td_coredump = tcq; 1421 td2->td_dbgflags |= TDB_COREDUMPRQ; 1422 thread_run_flash(td2); 1423 while ((td2->td_dbgflags & TDB_COREDUMPRQ) != 0) 1424 msleep(p, &p->p_mtx, PPAUSE, "crdmp", 0); 1425 error = tcq->tc_error; 1426 coredump_cleanup_locked: 1427 PROC_UNLOCK(p); 1428 coredump_cleanup: 1429 fdrop(fp, td); 1430 coredump_cleanup_nofp: 1431 free(tcq, M_TEMP); 1432 PROC_LOCK(p); 1433 break; 1434 1435 default: 1436 #ifdef __HAVE_PTRACE_MACHDEP 1437 if (req >= PT_FIRSTMACH) { 1438 PROC_UNLOCK(p); 1439 error = cpu_ptrace(td2, req, addr, data); 1440 PROC_LOCK(p); 1441 } else 1442 #endif 1443 /* Unknown request. */ 1444 error = EINVAL; 1445 break; 1446 } 1447 out: 1448 /* Drop our hold on this process now that the request has completed. */ 1449 _PRELE(p); 1450 fail: 1451 if (p2_req_set) { 1452 if ((p->p_flag2 & P2_PTRACEREQ) != 0) 1453 wakeup(&p->p_flag2); 1454 p->p_flag2 &= ~P2_PTRACEREQ; 1455 } 1456 PROC_UNLOCK(p); 1457 if (proctree_locked) 1458 sx_xunlock(&proctree_lock); 1459 return (error); 1460 } 1461 #undef PROC_READ 1462 #undef PROC_WRITE 1463