1 /* 2 * Copyright (c) 1994, Sean Eric Fagan 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by Sean Eric Fagan. 16 * 4. The name of the author may not be used to endorse or promote products 17 * derived from this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 * 31 * $FreeBSD$ 32 */ 33 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/sysproto.h> 37 #include <sys/proc.h> 38 #include <sys/vnode.h> 39 #include <sys/ptrace.h> 40 41 #include <machine/reg.h> 42 #include <vm/vm.h> 43 #include <sys/lock.h> 44 #include <vm/pmap.h> 45 #include <vm/vm_map.h> 46 #include <vm/vm_page.h> 47 48 #include <sys/user.h> 49 #include <miscfs/procfs/procfs.h> 50 51 /* use the equivalent procfs code */ 52 #if 0 53 static int 54 pread (struct proc *procp, unsigned int addr, unsigned int *retval) { 55 int rv; 56 vm_map_t map, tmap; 57 vm_object_t object; 58 vm_offset_t kva = 0; 59 int page_offset; /* offset into page */ 60 vm_offset_t pageno; /* page number */ 61 vm_map_entry_t out_entry; 62 vm_prot_t out_prot; 63 boolean_t wired; 64 vm_pindex_t pindex; 65 66 /* Map page into kernel space */ 67 68 map = &procp->p_vmspace->vm_map; 69 70 page_offset = addr - trunc_page(addr); 71 pageno = trunc_page(addr); 72 73 tmap = map; 74 rv = vm_map_lookup (&tmap, pageno, VM_PROT_READ, &out_entry, 75 &object, &pindex, &out_prot, &wired); 76 77 if (rv != KERN_SUCCESS) 78 return EINVAL; 79 80 vm_map_lookup_done (tmap, out_entry); 81 82 /* Find space in kernel_map for the page we're interested in */ 83 rv = vm_map_find (kernel_map, object, IDX_TO_OFF(pindex), 84 &kva, PAGE_SIZE, 0, VM_PROT_ALL, VM_PROT_ALL, 0); 85 86 if (!rv) { 87 vm_object_reference (object); 88 89 rv = vm_map_pageable (kernel_map, kva, kva + PAGE_SIZE, 0); 90 if (!rv) { 91 *retval = 0; 92 bcopy ((caddr_t)kva + page_offset, 93 retval, sizeof *retval); 94 } 95 vm_map_remove (kernel_map, kva, kva + PAGE_SIZE); 96 } 97 98 return rv; 99 } 100 101 static int 102 pwrite (struct proc *procp, unsigned int addr, unsigned int datum) { 103 int rv; 104 vm_map_t map, tmap; 105 vm_object_t object; 106 vm_offset_t kva = 0; 107 int page_offset; /* offset into page */ 108 vm_offset_t pageno; /* page number */ 109 vm_map_entry_t out_entry; 110 vm_prot_t out_prot; 111 boolean_t wired; 112 vm_pindex_t pindex; 113 boolean_t fix_prot = 0; 114 115 /* Map page into kernel space */ 116 117 map = &procp->p_vmspace->vm_map; 118 119 page_offset = addr - trunc_page(addr); 120 pageno = trunc_page(addr); 121 122 /* 123 * Check the permissions for the area we're interested in. 124 */ 125 126 if (vm_map_check_protection (map, pageno, pageno + PAGE_SIZE, 127 VM_PROT_WRITE) == FALSE) { 128 /* 129 * If the page was not writable, we make it so. 130 * XXX It is possible a page may *not* be read/executable, 131 * if a process changes that! 132 */ 133 fix_prot = 1; 134 /* The page isn't writable, so let's try making it so... */ 135 if ((rv = vm_map_protect (map, pageno, pageno + PAGE_SIZE, 136 VM_PROT_ALL, 0)) != KERN_SUCCESS) 137 return EFAULT; /* I guess... */ 138 } 139 140 /* 141 * Now we need to get the page. out_entry, out_prot, wired, and 142 * single_use aren't used. One would think the vm code would be 143 * a *bit* nicer... We use tmap because vm_map_lookup() can 144 * change the map argument. 145 */ 146 147 tmap = map; 148 rv = vm_map_lookup (&tmap, pageno, VM_PROT_WRITE, &out_entry, 149 &object, &pindex, &out_prot, &wired); 150 if (rv != KERN_SUCCESS) { 151 return EINVAL; 152 } 153 154 /* 155 * Okay, we've got the page. Let's release tmap. 156 */ 157 158 vm_map_lookup_done (tmap, out_entry); 159 160 /* 161 * Fault the page in... 162 */ 163 164 rv = vm_fault(map, pageno, VM_PROT_WRITE|VM_PROT_READ, FALSE); 165 if (rv != KERN_SUCCESS) 166 return EFAULT; 167 168 /* Find space in kernel_map for the page we're interested in */ 169 rv = vm_map_find (kernel_map, object, IDX_TO_OFF(pindex), 170 &kva, PAGE_SIZE, 0, 171 VM_PROT_ALL, VM_PROT_ALL, 0); 172 if (!rv) { 173 vm_object_reference (object); 174 175 rv = vm_map_pageable (kernel_map, kva, kva + PAGE_SIZE, 0); 176 if (!rv) { 177 bcopy (&datum, (caddr_t)kva + page_offset, sizeof datum); 178 } 179 vm_map_remove (kernel_map, kva, kva + PAGE_SIZE); 180 } 181 182 if (fix_prot) 183 vm_map_protect (map, pageno, pageno + PAGE_SIZE, 184 VM_PROT_READ|VM_PROT_EXECUTE, 0); 185 return rv; 186 } 187 #endif 188 189 /* 190 * Process debugging system call. 191 */ 192 #ifndef _SYS_SYSPROTO_H_ 193 struct ptrace_args { 194 int req; 195 pid_t pid; 196 caddr_t addr; 197 int data; 198 }; 199 #endif 200 201 int 202 ptrace(curp, uap) 203 struct proc *curp; 204 struct ptrace_args *uap; 205 { 206 struct proc *p; 207 struct iovec iov; 208 struct uio uio; 209 int error = 0; 210 int write; 211 int s; 212 213 write = 0; 214 if (uap->req == PT_TRACE_ME) 215 p = curp; 216 else { 217 if ((p = pfind(uap->pid)) == NULL) 218 return ESRCH; 219 } 220 if (p_can(curp, p, P_CAN_SEE, NULL)) 221 return (ESRCH); 222 223 /* 224 * Permissions check 225 */ 226 switch (uap->req) { 227 case PT_TRACE_ME: 228 /* Always legal. */ 229 break; 230 231 case PT_ATTACH: 232 /* Self */ 233 if (p->p_pid == curp->p_pid) 234 return EINVAL; 235 236 /* Already traced */ 237 if (p->p_flag & P_TRACED) 238 return EBUSY; 239 240 if ((error = p_can(curp, p, P_CAN_DEBUG, NULL))) 241 return error; 242 243 /* OK */ 244 break; 245 246 case PT_READ_I: 247 case PT_READ_D: 248 case PT_READ_U: 249 case PT_WRITE_I: 250 case PT_WRITE_D: 251 case PT_WRITE_U: 252 case PT_CONTINUE: 253 case PT_KILL: 254 case PT_STEP: 255 case PT_DETACH: 256 #ifdef PT_GETREGS 257 case PT_GETREGS: 258 #endif 259 #ifdef PT_SETREGS 260 case PT_SETREGS: 261 #endif 262 #ifdef PT_GETFPREGS 263 case PT_GETFPREGS: 264 #endif 265 #ifdef PT_SETFPREGS 266 case PT_SETFPREGS: 267 #endif 268 #ifdef PT_GETDBREGS 269 case PT_GETDBREGS: 270 #endif 271 #ifdef PT_SETDBREGS 272 case PT_SETDBREGS: 273 #endif 274 /* not being traced... */ 275 if ((p->p_flag & P_TRACED) == 0) 276 return EPERM; 277 278 /* not being traced by YOU */ 279 if (p->p_pptr != curp) 280 return EBUSY; 281 282 /* not currently stopped */ 283 if (p->p_stat != SSTOP || (p->p_flag & P_WAITED) == 0) 284 return EBUSY; 285 286 /* OK */ 287 break; 288 289 default: 290 return EINVAL; 291 } 292 293 #ifdef FIX_SSTEP 294 /* 295 * Single step fixup ala procfs 296 */ 297 FIX_SSTEP(p); 298 #endif 299 300 /* 301 * Actually do the requests 302 */ 303 304 curp->p_retval[0] = 0; 305 306 switch (uap->req) { 307 case PT_TRACE_ME: 308 /* set my trace flag and "owner" so it can read/write me */ 309 p->p_flag |= P_TRACED; 310 p->p_oppid = p->p_pptr->p_pid; 311 return 0; 312 313 case PT_ATTACH: 314 /* security check done above */ 315 p->p_flag |= P_TRACED; 316 p->p_oppid = p->p_pptr->p_pid; 317 if (p->p_pptr != curp) 318 proc_reparent(p, curp); 319 uap->data = SIGSTOP; 320 goto sendsig; /* in PT_CONTINUE below */ 321 322 case PT_STEP: 323 case PT_CONTINUE: 324 case PT_DETACH: 325 if ((uap->req != PT_STEP) && ((unsigned)uap->data >= NSIG)) 326 return EINVAL; 327 328 PHOLD(p); 329 330 if (uap->req == PT_STEP) { 331 if ((error = ptrace_single_step (p))) { 332 PRELE(p); 333 return error; 334 } 335 } 336 337 if (uap->addr != (caddr_t)1) { 338 fill_eproc (p, &p->p_addr->u_kproc.kp_eproc); 339 if ((error = ptrace_set_pc (p, 340 (u_long)(uintfptr_t)uap->addr))) { 341 PRELE(p); 342 return error; 343 } 344 } 345 PRELE(p); 346 347 if (uap->req == PT_DETACH) { 348 /* reset process parent */ 349 if (p->p_oppid != p->p_pptr->p_pid) { 350 struct proc *pp; 351 352 pp = pfind(p->p_oppid); 353 proc_reparent(p, pp ? pp : initproc); 354 } 355 356 p->p_flag &= ~(P_TRACED | P_WAITED); 357 p->p_oppid = 0; 358 359 /* should we send SIGCHLD? */ 360 361 } 362 363 sendsig: 364 /* deliver or queue signal */ 365 s = splhigh(); 366 if (p->p_stat == SSTOP) { 367 p->p_xstat = uap->data; 368 setrunnable(p); 369 } else if (uap->data) { 370 psignal(p, uap->data); 371 } 372 splx(s); 373 return 0; 374 375 case PT_WRITE_I: 376 case PT_WRITE_D: 377 write = 1; 378 /* fallthrough */ 379 case PT_READ_I: 380 case PT_READ_D: 381 /* write = 0 set above */ 382 iov.iov_base = write ? (caddr_t)&uap->data : (caddr_t)curp->p_retval; 383 iov.iov_len = sizeof(int); 384 uio.uio_iov = &iov; 385 uio.uio_iovcnt = 1; 386 uio.uio_offset = (off_t)(uintptr_t)uap->addr; 387 uio.uio_resid = sizeof(int); 388 uio.uio_segflg = UIO_SYSSPACE; /* ie: the uap */ 389 uio.uio_rw = write ? UIO_WRITE : UIO_READ; 390 uio.uio_procp = p; 391 error = procfs_domem(curp, p, NULL, &uio); 392 if (uio.uio_resid != 0) { 393 /* 394 * XXX procfs_domem() doesn't currently return ENOSPC, 395 * so I think write() can bogusly return 0. 396 * XXX what happens for short writes? We don't want 397 * to write partial data. 398 * XXX procfs_domem() returns EPERM for other invalid 399 * addresses. Convert this to EINVAL. Does this 400 * clobber returns of EPERM for other reasons? 401 */ 402 if (error == 0 || error == ENOSPC || error == EPERM) 403 error = EINVAL; /* EOF */ 404 } 405 return (error); 406 407 case PT_READ_U: 408 if ((uintptr_t)uap->addr > UPAGES * PAGE_SIZE - sizeof(int)) { 409 return EFAULT; 410 } 411 if ((uintptr_t)uap->addr & (sizeof(int) - 1)) { 412 return EFAULT; 413 } 414 if (ptrace_read_u_check(p,(vm_offset_t) uap->addr, 415 sizeof(int))) { 416 return EFAULT; 417 } 418 error = 0; 419 PHOLD(p); /* user had damn well better be incore! */ 420 if (p->p_flag & P_INMEM) { 421 p->p_addr->u_kproc.kp_proc = *p; 422 fill_eproc (p, &p->p_addr->u_kproc.kp_eproc); 423 curp->p_retval[0] = *(int *) 424 ((uintptr_t)p->p_addr + (uintptr_t)uap->addr); 425 } else { 426 curp->p_retval[0] = 0; 427 error = EFAULT; 428 } 429 PRELE(p); 430 return error; 431 432 case PT_WRITE_U: 433 PHOLD(p); /* user had damn well better be incore! */ 434 if (p->p_flag & P_INMEM) { 435 p->p_addr->u_kproc.kp_proc = *p; 436 fill_eproc (p, &p->p_addr->u_kproc.kp_eproc); 437 error = ptrace_write_u(p, (vm_offset_t)uap->addr, uap->data); 438 } else { 439 error = EFAULT; 440 } 441 PRELE(p); 442 return error; 443 444 case PT_KILL: 445 uap->data = SIGKILL; 446 goto sendsig; /* in PT_CONTINUE above */ 447 448 #ifdef PT_SETREGS 449 case PT_SETREGS: 450 write = 1; 451 /* fallthrough */ 452 #endif /* PT_SETREGS */ 453 #ifdef PT_GETREGS 454 case PT_GETREGS: 455 /* write = 0 above */ 456 #endif /* PT_SETREGS */ 457 #if defined(PT_SETREGS) || defined(PT_GETREGS) 458 if (!procfs_validregs(p)) /* no P_SYSTEM procs please */ 459 return EINVAL; 460 else { 461 iov.iov_base = uap->addr; 462 iov.iov_len = sizeof(struct reg); 463 uio.uio_iov = &iov; 464 uio.uio_iovcnt = 1; 465 uio.uio_offset = 0; 466 uio.uio_resid = sizeof(struct reg); 467 uio.uio_segflg = UIO_USERSPACE; 468 uio.uio_rw = write ? UIO_WRITE : UIO_READ; 469 uio.uio_procp = curp; 470 return (procfs_doregs(curp, p, NULL, &uio)); 471 } 472 #endif /* defined(PT_SETREGS) || defined(PT_GETREGS) */ 473 474 #ifdef PT_SETFPREGS 475 case PT_SETFPREGS: 476 write = 1; 477 /* fallthrough */ 478 #endif /* PT_SETFPREGS */ 479 #ifdef PT_GETFPREGS 480 case PT_GETFPREGS: 481 /* write = 0 above */ 482 #endif /* PT_SETFPREGS */ 483 #if defined(PT_SETFPREGS) || defined(PT_GETFPREGS) 484 if (!procfs_validfpregs(p)) /* no P_SYSTEM procs please */ 485 return EINVAL; 486 else { 487 iov.iov_base = uap->addr; 488 iov.iov_len = sizeof(struct fpreg); 489 uio.uio_iov = &iov; 490 uio.uio_iovcnt = 1; 491 uio.uio_offset = 0; 492 uio.uio_resid = sizeof(struct fpreg); 493 uio.uio_segflg = UIO_USERSPACE; 494 uio.uio_rw = write ? UIO_WRITE : UIO_READ; 495 uio.uio_procp = curp; 496 return (procfs_dofpregs(curp, p, NULL, &uio)); 497 } 498 #endif /* defined(PT_SETFPREGS) || defined(PT_GETFPREGS) */ 499 500 #ifdef PT_SETDBREGS 501 case PT_SETDBREGS: 502 write = 1; 503 /* fallthrough */ 504 #endif /* PT_SETDBREGS */ 505 #ifdef PT_GETDBREGS 506 case PT_GETDBREGS: 507 /* write = 0 above */ 508 #endif /* PT_SETDBREGS */ 509 #if defined(PT_SETDBREGS) || defined(PT_GETDBREGS) 510 if (!procfs_validdbregs(p)) /* no P_SYSTEM procs please */ 511 return EINVAL; 512 else { 513 iov.iov_base = uap->addr; 514 iov.iov_len = sizeof(struct dbreg); 515 uio.uio_iov = &iov; 516 uio.uio_iovcnt = 1; 517 uio.uio_offset = 0; 518 uio.uio_resid = sizeof(struct dbreg); 519 uio.uio_segflg = UIO_USERSPACE; 520 uio.uio_rw = write ? UIO_WRITE : UIO_READ; 521 uio.uio_procp = curp; 522 return (procfs_dodbregs(curp, p, NULL, &uio)); 523 } 524 #endif /* defined(PT_SETDBREGS) || defined(PT_GETDBREGS) */ 525 526 default: 527 break; 528 } 529 530 return 0; 531 } 532 533 int 534 trace_req(p) 535 struct proc *p; 536 { 537 return 1; 538 } 539 540 /* 541 * stopevent() 542 * Stop a process because of a procfs event; 543 * stay stopped until p->p_step is cleared 544 * (cleared by PIOCCONT in procfs). 545 */ 546 547 void 548 stopevent(struct proc *p, unsigned int event, unsigned int val) { 549 p->p_step = 1; 550 551 do { 552 p->p_xstat = val; 553 p->p_stype = event; /* Which event caused the stop? */ 554 wakeup(&p->p_stype); /* Wake up any PIOCWAIT'ing procs */ 555 tsleep(&p->p_step, PWAIT, "stopevent", 0); 556 } while (p->p_step); 557 } 558