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