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