1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright 2010 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 * 26 * Portions Copyright 2007 Chad Mynhier 27 * Copyright 2012 DEY Storage Systems, Inc. All rights reserved. 28 * Copyright (c) 2013 by Delphix. All rights reserved. 29 * Copyright 2015, Joyent, Inc. 30 * Copyright 2020 OmniOS Community Edition (OmniOSce) Association. 31 * Copyright 2026 Oxide Computer Company 32 */ 33 34 #include <assert.h> 35 #include <stdio.h> 36 #include <stdlib.h> 37 #include <unistd.h> 38 #include <ctype.h> 39 #include <fcntl.h> 40 #include <string.h> 41 #include <strings.h> 42 #include <memory.h> 43 #include <errno.h> 44 #include <dirent.h> 45 #include <limits.h> 46 #include <signal.h> 47 #include <atomic.h> 48 #include <zone.h> 49 #include <sys/types.h> 50 #include <sys/uio.h> 51 #include <sys/stat.h> 52 #include <sys/resource.h> 53 #include <sys/param.h> 54 #include <sys/stack.h> 55 #include <sys/fault.h> 56 #include <sys/syscall.h> 57 #include <sys/sysmacros.h> 58 #include <sys/systeminfo.h> 59 #include <sys/secflags.h> 60 #include <sys/mnttab.h> 61 #include <sys/mkdev.h> 62 63 #include "libproc.h" 64 #include "Pcontrol.h" 65 #include "Putil.h" 66 #include "P32ton.h" 67 68 int _libproc_debug; /* set non-zero to enable debugging printfs */ 69 int _libproc_no_qsort; /* set non-zero to inhibit sorting */ 70 /* of symbol tables */ 71 int _libproc_incore_elf; /* only use in-core elf data */ 72 int _libproc_test_fail_copyinargs; /* make Psyscall_copyinargs() fail */ 73 74 sigset_t blockable_sigs; /* signals to block when we need to be safe */ 75 static int minfd; /* minimum file descriptor returned by dupfd(fd, 0) */ 76 char procfs_path[PATH_MAX] = "/proc"; 77 78 /* 79 * Function prototypes for static routines in this module. 80 */ 81 static void deadcheck(struct ps_prochandle *); 82 static void restore_tracing_flags(struct ps_prochandle *); 83 static void Lfree_internal(struct ps_prochandle *, struct ps_lwphandle *); 84 static prheader_t *read_lfile(struct ps_prochandle *, const char *); 85 86 /* 87 * Ops vector functions for live processes. 88 */ 89 90 static ssize_t 91 Pread_live(struct ps_prochandle *P, void *buf, size_t n, uintptr_t addr, 92 void *data) 93 { 94 return (pread(P->asfd, buf, n, (off_t)addr)); 95 } 96 97 static ssize_t 98 Pwrite_live(struct ps_prochandle *P, const void *buf, size_t n, uintptr_t addr, 99 void *data) 100 { 101 return (pwrite(P->asfd, buf, n, (off_t)addr)); 102 } 103 104 static int 105 Pread_maps_live(struct ps_prochandle *P, prmap_t **Pmapp, ssize_t *nmapp, 106 void *data) 107 { 108 char mapfile[PATH_MAX]; 109 int mapfd; 110 struct stat statb; 111 ssize_t nmap; 112 prmap_t *Pmap = NULL; 113 114 (void) snprintf(mapfile, sizeof (mapfile), "%s/%d/map", 115 procfs_path, (int)P->pid); 116 if ((mapfd = open(mapfile, O_RDONLY)) < 0 || 117 fstat(mapfd, &statb) != 0 || 118 statb.st_size < sizeof (prmap_t) || 119 (Pmap = malloc(statb.st_size)) == NULL || 120 (nmap = pread(mapfd, Pmap, statb.st_size, 0L)) <= 0 || 121 (nmap /= sizeof (prmap_t)) == 0) { 122 if (Pmap != NULL) 123 free(Pmap); 124 if (mapfd >= 0) 125 (void) close(mapfd); 126 Preset_maps(P); /* utter failure; destroy tables */ 127 return (-1); 128 } 129 (void) close(mapfd); 130 131 *Pmapp = Pmap; 132 *nmapp = nmap; 133 134 return (0); 135 } 136 137 static void 138 Pread_aux_live(struct ps_prochandle *P, auxv_t **auxvp, int *nauxp, void *data) 139 { 140 char auxfile[64]; 141 int fd; 142 struct stat statb; 143 auxv_t *auxv; 144 ssize_t naux; 145 146 (void) snprintf(auxfile, sizeof (auxfile), "%s/%d/auxv", 147 procfs_path, (int)P->pid); 148 if ((fd = open(auxfile, O_RDONLY)) < 0) { 149 Pdprintf("%s: failed to open %s: %s\n", 150 __func__, auxfile, strerror(errno)); 151 return; 152 } 153 154 if (fstat(fd, &statb) == 0 && 155 statb.st_size >= sizeof (auxv_t) && 156 (auxv = malloc(statb.st_size + sizeof (auxv_t))) != NULL) { 157 if ((naux = read(fd, auxv, statb.st_size)) < 0 || 158 (naux /= sizeof (auxv_t)) < 1) { 159 Pdprintf("%s: read failed: %s\n", 160 __func__, strerror(errno)); 161 free(auxv); 162 } else { 163 auxv[naux].a_type = AT_NULL; 164 auxv[naux].a_un.a_val = 0L; 165 166 *auxvp = auxv; 167 *nauxp = (int)naux; 168 } 169 } 170 171 (void) close(fd); 172 } 173 174 static int 175 Pcred_live(struct ps_prochandle *P, prcred_t *pcrp, int ngroups, void *data) 176 { 177 return (proc_get_cred(P->pid, pcrp, ngroups)); 178 } 179 180 static int 181 Psecflags_live(struct ps_prochandle *P, prsecflags_t **psf, void *data) 182 { 183 return (proc_get_secflags(P->pid, psf)); 184 } 185 186 static int 187 Ppriv_live(struct ps_prochandle *P, prpriv_t **pprv, void *data) 188 { 189 prpriv_t *pp; 190 191 pp = proc_get_priv(P->pid); 192 if (pp == NULL) { 193 return (-1); 194 } 195 196 *pprv = pp; 197 return (0); 198 } 199 200 static const psinfo_t * 201 Ppsinfo_live(struct ps_prochandle *P, psinfo_t *psinfo, void *data) 202 { 203 if (proc_get_psinfo(P->pid, psinfo) == -1) 204 return (NULL); 205 206 return (psinfo); 207 } 208 209 static prheader_t * 210 Plstatus_live(struct ps_prochandle *P, void *data) 211 { 212 return (read_lfile(P, "lstatus")); 213 } 214 215 static prheader_t * 216 Plpsinfo_live(struct ps_prochandle *P, void *data) 217 { 218 return (read_lfile(P, "lpsinfo")); 219 } 220 221 static char * 222 Pplatform_live(struct ps_prochandle *P, char *s, size_t n, void *data) 223 { 224 if (sysinfo(SI_PLATFORM, s, n) == -1) 225 return (NULL); 226 return (s); 227 } 228 229 static int 230 Puname_live(struct ps_prochandle *P, struct utsname *u, void *data) 231 { 232 return (uname(u)); 233 } 234 235 static char * 236 Pzonename_live(struct ps_prochandle *P, char *s, size_t n, void *data) 237 { 238 if (getzonenamebyid(P->status.pr_zoneid, s, n) < 0) 239 return (NULL); 240 s[n - 1] = '\0'; 241 return (s); 242 } 243 244 /* 245 * Callback function for Pfindexec(). We return a match if we can stat the 246 * suggested pathname and confirm its device and inode number match our 247 * previous information about the /proc/<pid>/object/a.out file. 248 */ 249 static int 250 stat_exec(const char *path, void *arg) 251 { 252 struct stat64 *stp = arg; 253 struct stat64 st; 254 255 return (stat64(path, &st) == 0 && S_ISREG(st.st_mode) && 256 stp->st_dev == st.st_dev && stp->st_ino == st.st_ino); 257 } 258 259 static char * 260 Pexecname_live(struct ps_prochandle *P, char *buf, size_t buflen, void *data) 261 { 262 char exec_name[PATH_MAX]; 263 char cwd[PATH_MAX], *cwdp = NULL; 264 struct stat64 st; 265 int ret; 266 267 /* 268 * Try to get the path information first. 269 */ 270 (void) snprintf(exec_name, sizeof (exec_name), 271 "%s/%d/path/a.out", procfs_path, (int)P->pid); 272 if ((ret = readlink(exec_name, buf, buflen - 1)) > 0) { 273 buf[ret] = '\0'; 274 (void) Pfindobj(P, buf, buf, buflen); 275 return (buf); 276 } 277 278 /* 279 * Stat the executable file so we can compare Pfindexec's 280 * suggestions to the actual device and inode number. 281 */ 282 (void) snprintf(exec_name, sizeof (exec_name), 283 "%s/%d/object/a.out", procfs_path, (int)P->pid); 284 285 if (stat64(exec_name, &st) != 0 || !S_ISREG(st.st_mode)) 286 return (NULL); 287 288 /* 289 * Attempt to figure out the current working directory of the 290 * target process. This only works if the target process has 291 * not changed its current directory since it was exec'd. 292 */ 293 if (proc_get_cwd(P->pid, cwd, sizeof (cwd)) > 0) 294 cwdp = cwd; 295 296 (void) Pfindexec(P, cwdp, stat_exec, &st); 297 298 return (NULL); 299 } 300 301 /* 302 * Snapshot the cwd for a process. Note, we assume that we do not have the 303 * process held per se (pwdx does not grab the process). If our caller wants to 304 * guarantee this, they are responsible for getting a hold. 305 */ 306 static int 307 Pcwd_live(struct ps_prochandle *P, prcwd_t **cwdp, void *data) 308 { 309 prcwd_t *cwd = NULL; 310 struct statvfs st; 311 FILE *tab = NULL; 312 struct extmnttab ent; 313 int ret; 314 315 cwd = calloc(1, sizeof (prcwd_t)); 316 if (cwd == NULL) 317 goto err; 318 319 if (proc_get_cwd(P->pid, cwd->prcwd_cwd, sizeof (cwd->prcwd_cwd)) < 0) 320 goto err; 321 322 if (statvfs(cwd->prcwd_cwd, &st) != 0) 323 goto err; 324 325 cwd->prcwd_fsid = st.f_fsid; 326 (void) memcpy(cwd->prcwd_fsname, st.f_basetype, FSTYPSZ); 327 328 /* 329 * Find the corresponding mountpoint based on the fsid. If we don't find 330 * a match, that's weird, but we'll try to get folks something. There's 331 * always a possibility of a race going on as we can't stop file system 332 * mount points from changing during this operation. Similarly, in lieu 333 * of erorring on overflow we opt to truncate as tihs would again be a 334 * surprising situation based on system limits. 335 */ 336 if ((tab = fopen(MNTTAB, "r")) == NULL) 337 goto err; 338 resetmnttab(tab); 339 Pdprintf("found fsid %llx\n", cwd->prcwd_fsid); 340 while ((ret = getextmntent(tab, &ent, sizeof (struct extmnttab))) == 341 0) { 342 /* 343 * statvfs(2) always returns the compressed 32-bit compatible 344 * dev, even in an LP64 environment. We must explicitly attempt 345 * to convert things as a result to this format as the normal 346 * makedev() doesn't quite do the right thing. 347 */ 348 if (__makedev(COMPATDEV, ent.mnt_major, ent.mnt_minor) == 349 st.f_fsid) { 350 (void) strlcpy(cwd->prcwd_mntpt, ent.mnt_mountp, 351 sizeof (cwd->prcwd_mntpt)); 352 (void) strlcpy(cwd->prcwd_mntspec, ent.mnt_special, 353 sizeof (cwd->prcwd_mntspec)); 354 break; 355 } 356 } 357 358 if (ret > 0) { 359 errno = EIO; 360 goto err; 361 } 362 363 (void) fclose(tab); 364 *cwdp = cwd; 365 return (0); 366 367 err: 368 (void) fclose(tab); 369 free(cwd); 370 return (-1); 371 } 372 373 #if defined(__i386) || defined(__amd64) 374 static int 375 Pldt_live(struct ps_prochandle *P, struct ssd *pldt, int nldt, void *data) 376 { 377 return (proc_get_ldt(P->pid, pldt, nldt)); 378 } 379 #endif 380 381 static const ps_ops_t P_live_ops = { 382 .pop_pread = Pread_live, 383 .pop_pwrite = Pwrite_live, 384 .pop_read_maps = Pread_maps_live, 385 .pop_read_aux = Pread_aux_live, 386 .pop_cred = Pcred_live, 387 .pop_priv = Ppriv_live, 388 .pop_psinfo = Ppsinfo_live, 389 .pop_lstatus = Plstatus_live, 390 .pop_lpsinfo = Plpsinfo_live, 391 .pop_platform = Pplatform_live, 392 .pop_uname = Puname_live, 393 .pop_zonename = Pzonename_live, 394 .pop_execname = Pexecname_live, 395 .pop_secflags = Psecflags_live, 396 .pop_cwd = Pcwd_live, 397 #if defined(__i386) || defined(__amd64) 398 .pop_ldt = Pldt_live 399 #endif 400 }; 401 402 /* 403 * This is the library's .init handler. 404 */ 405 #pragma init(_libproc_init) 406 void 407 _libproc_init(void) 408 { 409 _libproc_debug = getenv("LIBPROC_DEBUG") != NULL; 410 _libproc_no_qsort = getenv("LIBPROC_NO_QSORT") != NULL; 411 _libproc_incore_elf = getenv("LIBPROC_INCORE_ELF") != NULL; 412 413 (void) sigfillset(&blockable_sigs); 414 (void) sigdelset(&blockable_sigs, SIGKILL); 415 (void) sigdelset(&blockable_sigs, SIGSTOP); 416 } 417 418 void 419 Pset_procfs_path(const char *path) 420 { 421 (void) snprintf(procfs_path, sizeof (procfs_path), "%s", path); 422 } 423 424 /* 425 * Call set_minfd() once before calling dupfd() several times. 426 * We assume that the application will not reduce its current file 427 * descriptor limit lower than 512 once it has set at least that value. 428 */ 429 int 430 set_minfd(void) 431 { 432 static mutex_t minfd_lock = DEFAULTMUTEX; 433 struct rlimit rlim; 434 int fd; 435 436 if ((fd = minfd) < 256) { 437 (void) mutex_lock(&minfd_lock); 438 if ((fd = minfd) < 256) { 439 if (getrlimit(RLIMIT_NOFILE, &rlim) != 0) 440 rlim.rlim_cur = rlim.rlim_max = 0; 441 if (rlim.rlim_cur >= 512) 442 fd = 256; 443 else if ((fd = rlim.rlim_cur / 2) < 3) 444 fd = 3; 445 membar_producer(); 446 minfd = fd; 447 } 448 (void) mutex_unlock(&minfd_lock); 449 } 450 return (fd); 451 } 452 453 int 454 dupfd(int fd, int dfd) 455 { 456 int mfd; 457 458 /* 459 * Make fd be greater than 255 (the 32-bit stdio limit), 460 * or at least make it greater than 2 so that the 461 * program will work when spawned by init(8). 462 * Also, if dfd is non-zero, dup the fd to be dfd. 463 */ 464 if ((mfd = minfd) == 0) 465 mfd = set_minfd(); 466 if (dfd > 0 || (0 <= fd && fd < mfd)) { 467 if (dfd <= 0) 468 dfd = mfd; 469 dfd = fcntl(fd, F_DUPFD, dfd); 470 (void) close(fd); 471 fd = dfd; 472 } 473 /* 474 * Mark it close-on-exec so any created process doesn't inherit it. 475 */ 476 if (fd >= 0) 477 (void) fcntl(fd, F_SETFD, FD_CLOEXEC); 478 return (fd); 479 } 480 481 /* 482 * Create a new controlled process. 483 * Leave it stopped on successful exit from exec() or execve(). 484 * Return an opaque pointer to its process control structure. 485 * Return NULL if process cannot be created (fork()/exec() not successful). 486 */ 487 struct ps_prochandle * 488 Pxcreate(const char *file, /* executable file name */ 489 char *const *argv, /* argument vector */ 490 char *const *envp, /* environment */ 491 int *perr, /* pointer to error return code */ 492 char *path, /* if non-null, holds exec path name on return */ 493 size_t len) /* size of the path buffer */ 494 { 495 char execpath[PATH_MAX]; 496 char procname[PATH_MAX]; 497 struct ps_prochandle *P; 498 pid_t pid; 499 int fd; 500 char *fname; 501 int rc; 502 int lasterrno = 0; 503 504 if (len == 0) /* zero length, no path */ 505 path = NULL; 506 if (path != NULL) 507 *path = '\0'; 508 509 if ((P = malloc(sizeof (struct ps_prochandle))) == NULL) { 510 *perr = C_STRANGE; 511 return (NULL); 512 } 513 514 if ((pid = fork1()) == -1) { 515 free(P); 516 *perr = C_FORK; 517 return (NULL); 518 } 519 520 if (pid == 0) { /* child process */ 521 id_t id; 522 extern char **environ; 523 524 /* 525 * If running setuid or setgid, reset credentials to normal. 526 */ 527 if ((id = getgid()) != getegid()) 528 (void) setgid(id); 529 if ((id = getuid()) != geteuid()) 530 (void) setuid(id); 531 532 Pcreate_callback(P); /* execute callback (see below) */ 533 (void) pause(); /* wait for PRSABORT from parent */ 534 535 /* 536 * This is ugly. There is no execvep() function that takes a 537 * path and an environment. We cheat here by replacing the 538 * global 'environ' variable right before we call this. 539 */ 540 if (envp) 541 environ = (char **)envp; 542 543 (void) execvp(file, argv); /* execute the program */ 544 _exit(127); 545 } 546 547 /* 548 * Initialize the process structure. 549 */ 550 (void) memset(P, 0, sizeof (*P)); 551 (void) mutex_init(&P->proc_lock, USYNC_THREAD, NULL); 552 P->flags |= CREATED; 553 P->state = PS_RUN; 554 P->pid = pid; 555 P->asfd = -1; 556 P->ctlfd = -1; 557 P->statfd = -1; 558 P->agentctlfd = -1; 559 P->agentstatfd = -1; 560 Pinit_ops(&P->ops, &P_live_ops); 561 Pinitsym(P); 562 Pinitfd(P); 563 564 /* 565 * Open the /proc/pid files. 566 */ 567 (void) snprintf(procname, sizeof (procname), "%s/%d/", 568 procfs_path, (int)pid); 569 fname = procname + strlen(procname); 570 (void) set_minfd(); 571 572 /* 573 * Exclusive write open advises others not to interfere. 574 * There is no reason for any of these open()s to fail. 575 */ 576 (void) strcpy(fname, "as"); 577 if ((fd = open(procname, (O_RDWR|O_EXCL))) < 0 || 578 (fd = dupfd(fd, 0)) < 0) { 579 Pdprintf("Pcreate: failed to open %s: %s\n", 580 procname, strerror(errno)); 581 rc = C_STRANGE; 582 goto bad; 583 } 584 P->asfd = fd; 585 586 (void) strcpy(fname, "status"); 587 if ((fd = open(procname, O_RDONLY)) < 0 || 588 (fd = dupfd(fd, 0)) < 0) { 589 Pdprintf("Pcreate: failed to open %s: %s\n", 590 procname, strerror(errno)); 591 rc = C_STRANGE; 592 goto bad; 593 } 594 P->statfd = fd; 595 596 (void) strcpy(fname, "ctl"); 597 if ((fd = open(procname, O_WRONLY)) < 0 || 598 (fd = dupfd(fd, 0)) < 0) { 599 Pdprintf("Pcreate: failed to open %s: %s\n", 600 procname, strerror(errno)); 601 rc = C_STRANGE; 602 goto bad; 603 } 604 P->ctlfd = fd; 605 606 (void) Pstop(P, 0); /* stop the controlled process */ 607 608 /* 609 * Wait for process to sleep in pause(). 610 * If the process has already called pause(), then it should be 611 * stopped (PR_REQUESTED) while asleep in pause and we are done. 612 * Else we set up to catch entry/exit to pause() and set the process 613 * running again, expecting it to stop when it reaches pause(). 614 * There is no reason for this to fail other than an interrupt. 615 */ 616 (void) Psysentry(P, SYS_pause, 1); 617 (void) Psysexit(P, SYS_pause, 1); 618 for (;;) { 619 if (P->state == PS_STOP && 620 P->status.pr_lwp.pr_syscall == SYS_pause && 621 (P->status.pr_lwp.pr_why == PR_REQUESTED || 622 P->status.pr_lwp.pr_why == PR_SYSENTRY || 623 P->status.pr_lwp.pr_why == PR_SYSEXIT)) 624 break; 625 626 if (P->state != PS_STOP || /* interrupt or process died */ 627 Psetrun(P, 0, 0) != 0) { /* can't restart */ 628 if (errno == EINTR || errno == ERESTART) 629 rc = C_INTR; 630 else { 631 Pdprintf("Pcreate: Psetrun failed: %s\n", 632 strerror(errno)); 633 rc = C_STRANGE; 634 } 635 goto bad; 636 } 637 638 (void) Pwait(P, 0); 639 } 640 (void) Psysentry(P, SYS_pause, 0); 641 (void) Psysexit(P, SYS_pause, 0); 642 643 /* 644 * Kick the process off the pause() and catch 645 * it again on entry to exec() or exit(). 646 */ 647 (void) Psysentry(P, SYS_exit, 1); 648 (void) Psysentry(P, SYS_execve, 1); 649 if (Psetrun(P, 0, PRSABORT) == -1) { 650 Pdprintf("Pcreate: Psetrun failed: %s\n", strerror(errno)); 651 rc = C_STRANGE; 652 goto bad; 653 } 654 (void) Pwait(P, 0); 655 if (P->state != PS_STOP) { 656 Pdprintf("Pcreate: Pwait failed: %s\n", strerror(errno)); 657 rc = C_STRANGE; 658 goto bad; 659 } 660 661 /* 662 * Move the process through instances of failed exec()s 663 * to reach the point of stopped on successful exec(). 664 */ 665 (void) Psysexit(P, SYS_execve, TRUE); 666 667 while (P->state == PS_STOP && 668 P->status.pr_lwp.pr_why == PR_SYSENTRY && 669 P->status.pr_lwp.pr_what == SYS_execve) { 670 /* 671 * Fetch the exec path name now, before we complete 672 * the exec(). We may lose the process and be unable 673 * to get the information later. 674 */ 675 (void) Pread_string(P, execpath, sizeof (execpath), 676 (off_t)P->status.pr_lwp.pr_sysarg[0]); 677 if (path != NULL) 678 (void) strncpy(path, execpath, len); 679 /* 680 * Set the process running and wait for 681 * it to stop on exit from the exec(). 682 */ 683 (void) Psetrun(P, 0, 0); 684 (void) Pwait(P, 0); 685 686 if (P->state == PS_LOST && /* we lost control */ 687 Preopen(P) != 0) { /* and we can't get it back */ 688 rc = C_PERM; 689 goto bad; 690 } 691 692 /* 693 * If the exec() failed, continue the loop, expecting 694 * there to be more attempts to exec(), based on PATH. 695 */ 696 if (P->state == PS_STOP && 697 P->status.pr_lwp.pr_why == PR_SYSEXIT && 698 P->status.pr_lwp.pr_what == SYS_execve && 699 (lasterrno = P->status.pr_lwp.pr_errno) != 0) { 700 /* 701 * The exec() failed. Set the process running and 702 * wait for it to stop on entry to the next exec(). 703 */ 704 (void) Psetrun(P, 0, 0); 705 (void) Pwait(P, 0); 706 707 continue; 708 } 709 break; 710 } 711 712 if (P->state == PS_STOP && 713 P->status.pr_lwp.pr_why == PR_SYSEXIT && 714 P->status.pr_lwp.pr_what == SYS_execve && 715 P->status.pr_lwp.pr_errno == 0) { 716 /* 717 * The process is stopped on successful exec() or execve(). 718 * Turn off all tracing flags and return success. 719 */ 720 restore_tracing_flags(P); 721 #ifndef _LP64 722 /* We must be a 64-bit process to deal with a 64-bit process */ 723 if (P->status.pr_dmodel == PR_MODEL_LP64) { 724 rc = C_LP64; 725 goto bad; 726 } 727 #endif 728 /* 729 * Set run-on-last-close so the controlled process 730 * runs even if we die on a signal. 731 */ 732 (void) Psetflags(P, PR_RLC); 733 *perr = 0; 734 return (P); 735 } 736 737 rc = lasterrno == ENOENT ? C_NOENT : C_NOEXEC; 738 739 bad: 740 (void) kill(pid, SIGKILL); 741 if (path != NULL && rc != C_PERM && rc != C_LP64) 742 *path = '\0'; 743 Pfree(P); 744 *perr = rc; 745 return (NULL); 746 } 747 748 struct ps_prochandle * 749 Pcreate( 750 const char *file, /* executable file name */ 751 char *const *argv, /* argument vector */ 752 int *perr, /* pointer to error return code */ 753 char *path, /* if non-null, holds exec path name on return */ 754 size_t len) /* size of the path buffer */ 755 { 756 return (Pxcreate(file, argv, NULL, perr, path, len)); 757 } 758 759 /* 760 * Return a printable string corresponding to a Pcreate() error return. 761 */ 762 const char * 763 Pcreate_error(int error) 764 { 765 const char *str; 766 767 switch (error) { 768 case C_FORK: 769 str = "cannot fork"; 770 break; 771 case C_PERM: 772 str = "file is set-id or unreadable"; 773 break; 774 case C_NOEXEC: 775 str = "cannot execute file"; 776 break; 777 case C_INTR: 778 str = "operation interrupted"; 779 break; 780 case C_LP64: 781 str = "program is _LP64, self is not"; 782 break; 783 case C_STRANGE: 784 str = "unanticipated system error"; 785 break; 786 case C_NOENT: 787 str = "cannot find executable file"; 788 break; 789 default: 790 str = "unknown error"; 791 break; 792 } 793 794 return (str); 795 } 796 797 /* 798 * Callback to execute in each child process created with Pcreate() after fork 799 * but before it execs the new process image. By default, we do nothing, but 800 * by calling this function we allow the client program to define its own 801 * version of the function which will interpose on our empty default. This 802 * may be useful for clients that need to modify signal dispositions, terminal 803 * attributes, or process group and session properties for each new victim. 804 */ 805 void 806 Pcreate_callback(struct ps_prochandle *P) 807 { 808 /* nothing to do here */ 809 } 810 811 /* 812 * Grab an existing process. 813 * Return an opaque pointer to its process control structure. 814 * 815 * pid: UNIX process ID. 816 * flags: 817 * PGRAB_RETAIN Retain tracing flags (default clears all tracing flags). 818 * PGRAB_FORCE Grab regardless of whether process is already traced. 819 * PGRAB_RDONLY Open the address space file O_RDONLY instead of O_RDWR, 820 * and do not open the process control file. 821 * PGRAB_NOSTOP Open the process but do not force it to stop. 822 * perr: pointer to error return code. 823 */ 824 struct ps_prochandle * 825 Pgrab(pid_t pid, int flags, int *perr) 826 { 827 struct ps_prochandle *P; 828 int fd, omode; 829 char procname[PATH_MAX]; 830 char *fname; 831 int rc = 0; 832 833 /* 834 * PGRAB_RDONLY means that we do not open the /proc/<pid>/control file, 835 * and so it implies RETAIN and NOSTOP since both require control. 836 */ 837 if (flags & PGRAB_RDONLY) 838 flags |= PGRAB_RETAIN | PGRAB_NOSTOP; 839 840 if ((P = malloc(sizeof (struct ps_prochandle))) == NULL) { 841 *perr = G_STRANGE; 842 return (NULL); 843 } 844 845 P->asfd = -1; 846 P->ctlfd = -1; 847 P->statfd = -1; 848 849 again: /* Come back here if we lose it in the Window of Vulnerability */ 850 if (P->ctlfd >= 0) 851 (void) close(P->ctlfd); 852 if (P->asfd >= 0) 853 (void) close(P->asfd); 854 if (P->statfd >= 0) 855 (void) close(P->statfd); 856 (void) memset(P, 0, sizeof (*P)); 857 (void) mutex_init(&P->proc_lock, USYNC_THREAD, NULL); 858 P->ctlfd = -1; 859 P->asfd = -1; 860 P->statfd = -1; 861 P->agentctlfd = -1; 862 P->agentstatfd = -1; 863 Pinit_ops(&P->ops, &P_live_ops); 864 Pinitsym(P); 865 Pinitfd(P); 866 867 /* 868 * Open the /proc/pid files 869 */ 870 (void) snprintf(procname, sizeof (procname), "%s/%d/", 871 procfs_path, (int)pid); 872 fname = procname + strlen(procname); 873 (void) set_minfd(); 874 875 /* 876 * Request exclusive open to avoid grabbing someone else's 877 * process and to prevent others from interfering afterwards. 878 * If this fails and the 'PGRAB_FORCE' flag is set, attempt to 879 * open non-exclusively. 880 */ 881 (void) strcpy(fname, "as"); 882 omode = (flags & PGRAB_RDONLY) ? O_RDONLY : O_RDWR; 883 884 if (((fd = open(procname, omode | O_EXCL)) < 0 && 885 (fd = ((flags & PGRAB_FORCE)? open(procname, omode) : -1)) < 0) || 886 (fd = dupfd(fd, 0)) < 0) { 887 switch (errno) { 888 case ENOENT: 889 rc = G_NOPROC; 890 break; 891 case EACCES: 892 case EPERM: 893 rc = G_PERM; 894 break; 895 case EMFILE: 896 rc = G_NOFD; 897 break; 898 case EBUSY: 899 if (!(flags & PGRAB_FORCE) || geteuid() != 0) { 900 rc = G_BUSY; 901 break; 902 } 903 /* FALLTHROUGH */ 904 default: 905 Pdprintf("Pgrab: failed to open %s: %s\n", 906 procname, strerror(errno)); 907 rc = G_STRANGE; 908 break; 909 } 910 goto err; 911 } 912 P->asfd = fd; 913 914 (void) strcpy(fname, "status"); 915 if ((fd = open(procname, O_RDONLY)) < 0 || 916 (fd = dupfd(fd, 0)) < 0) { 917 switch (errno) { 918 case ENOENT: 919 rc = G_NOPROC; 920 break; 921 case EMFILE: 922 rc = G_NOFD; 923 break; 924 default: 925 Pdprintf("Pgrab: failed to open %s: %s\n", 926 procname, strerror(errno)); 927 rc = G_STRANGE; 928 break; 929 } 930 goto err; 931 } 932 P->statfd = fd; 933 934 if (!(flags & PGRAB_RDONLY)) { 935 (void) strcpy(fname, "ctl"); 936 if ((fd = open(procname, O_WRONLY)) < 0 || 937 (fd = dupfd(fd, 0)) < 0) { 938 switch (errno) { 939 case ENOENT: 940 rc = G_NOPROC; 941 break; 942 case EMFILE: 943 rc = G_NOFD; 944 break; 945 default: 946 Pdprintf("Pgrab: failed to open %s: %s\n", 947 procname, strerror(errno)); 948 rc = G_STRANGE; 949 break; 950 } 951 goto err; 952 } 953 P->ctlfd = fd; 954 } 955 956 P->state = PS_RUN; 957 P->pid = pid; 958 959 /* 960 * We are now in the Window of Vulnerability (WoV). The process may 961 * exec() a setuid/setgid or unreadable object file between the open() 962 * and the PCSTOP. We will get EAGAIN in this case and must start over. 963 * As Pstopstatus will trigger the first read() from a /proc file, 964 * we also need to handle EOVERFLOW here when 32-bit as an indicator 965 * that this process is 64-bit. Finally, if the process has become 966 * a zombie (PS_UNDEAD) while we were trying to grab it, just remain 967 * silent about this and pretend there was no process. 968 */ 969 if (Pstopstatus(P, PCNULL, 0) != 0) { 970 #ifndef _LP64 971 if (errno == EOVERFLOW) { 972 rc = G_LP64; 973 goto err; 974 } 975 #endif 976 if (P->state == PS_LOST) { /* WoV */ 977 (void) mutex_destroy(&P->proc_lock); 978 goto again; 979 } 980 981 if (P->state == PS_UNDEAD) 982 rc = G_NOPROC; 983 else 984 rc = G_STRANGE; 985 986 goto err; 987 } 988 989 /* 990 * If the process is a system process, we can't control it even as root 991 */ 992 if (P->status.pr_flags & PR_ISSYS) { 993 rc = G_SYS; 994 goto err; 995 } 996 #ifndef _LP64 997 /* 998 * We must be a 64-bit process to deal with a 64-bit process 999 */ 1000 if (P->status.pr_dmodel == PR_MODEL_LP64) { 1001 rc = G_LP64; 1002 goto err; 1003 } 1004 #endif 1005 1006 /* 1007 * Remember the status for use by Prelease(). 1008 */ 1009 P->orig_status = P->status; /* structure copy */ 1010 1011 /* 1012 * Before stopping the process, make sure we are not grabbing ourselves. 1013 * If we are, make sure we are doing it PGRAB_RDONLY. 1014 */ 1015 if (pid == getpid()) { 1016 /* 1017 * Verify that the process is really ourself: 1018 * Set a magic number, read it through the 1019 * /proc file and see if the results match. 1020 */ 1021 uint32_t magic1 = 0; 1022 uint32_t magic2 = 2; 1023 1024 errno = 0; 1025 1026 if (Pread(P, &magic2, sizeof (magic2), (uintptr_t)&magic1) 1027 == sizeof (magic2) && 1028 magic2 == 0 && 1029 (magic1 = 0xfeedbeef) && 1030 Pread(P, &magic2, sizeof (magic2), (uintptr_t)&magic1) 1031 == sizeof (magic2) && 1032 magic2 == 0xfeedbeef && 1033 !(flags & PGRAB_RDONLY)) { 1034 rc = G_SELF; 1035 goto err; 1036 } 1037 } 1038 1039 /* 1040 * If the process is already stopped or has been directed 1041 * to stop via /proc, do not set run-on-last-close. 1042 */ 1043 if (!(P->status.pr_lwp.pr_flags & (PR_ISTOP|PR_DSTOP)) && 1044 !(flags & PGRAB_RDONLY)) { 1045 /* 1046 * Mark the process run-on-last-close so 1047 * it runs even if we die from SIGKILL. 1048 */ 1049 if (Psetflags(P, PR_RLC) != 0) { 1050 if (errno == EAGAIN) { /* WoV */ 1051 (void) mutex_destroy(&P->proc_lock); 1052 goto again; 1053 } 1054 if (errno == ENOENT) /* No complaint about zombies */ 1055 rc = G_ZOMB; 1056 else { 1057 Pdprintf("Pgrab: failed to set RLC\n"); 1058 rc = G_STRANGE; 1059 } 1060 goto err; 1061 } 1062 } 1063 1064 /* 1065 * If a stop directive is pending and the process has not yet stopped, 1066 * then synchronously wait for the stop directive to take effect. 1067 * Limit the time spent waiting for the process to stop by iterating 1068 * at most 10 times. The time-out of 20 ms corresponds to the time 1069 * between sending the stop directive and the process actually stopped 1070 * as measured by DTrace on a slow, busy system. If the process doesn't 1071 * stop voluntarily, clear the PR_DSTOP flag so that the code below 1072 * forces the process to stop. 1073 */ 1074 if (!(flags & PGRAB_RDONLY)) { 1075 int niter = 0; 1076 while ((P->status.pr_lwp.pr_flags & (PR_STOPPED|PR_DSTOP)) == 1077 PR_DSTOP && niter < 10 && 1078 Pstopstatus(P, PCTWSTOP, 20) != 0) { 1079 niter++; 1080 if (flags & PGRAB_NOSTOP) 1081 break; 1082 } 1083 if (niter == 10 && !(flags & PGRAB_NOSTOP)) { 1084 /* Try it harder down below */ 1085 P->status.pr_lwp.pr_flags &= ~PR_DSTOP; 1086 } 1087 } 1088 1089 /* 1090 * If the process is not already stopped or directed to stop 1091 * and PGRAB_NOSTOP was not specified, stop the process now. 1092 */ 1093 if (!(P->status.pr_lwp.pr_flags & (PR_ISTOP|PR_DSTOP)) && 1094 !(flags & PGRAB_NOSTOP)) { 1095 /* 1096 * Stop the process, get its status and signal/syscall masks. 1097 */ 1098 if (((P->status.pr_lwp.pr_flags & PR_STOPPED) && 1099 Pstopstatus(P, PCDSTOP, 0) != 0) || 1100 Pstopstatus(P, PCSTOP, 2000) != 0) { 1101 #ifndef _LP64 1102 if (errno == EOVERFLOW) { 1103 rc = G_LP64; 1104 goto err; 1105 } 1106 #endif 1107 if (P->state == PS_LOST) { /* WoV */ 1108 (void) mutex_destroy(&P->proc_lock); 1109 goto again; 1110 } 1111 if ((errno != EINTR && errno != ERESTART) || 1112 (P->state != PS_STOP && 1113 !(P->status.pr_flags & PR_DSTOP))) { 1114 if (P->state != PS_RUN && errno != ENOENT) { 1115 Pdprintf("Pgrab: failed to PCSTOP\n"); 1116 rc = G_STRANGE; 1117 } else { 1118 rc = G_ZOMB; 1119 } 1120 goto err; 1121 } 1122 } 1123 1124 /* 1125 * Process should now either be stopped via /proc or there 1126 * should be an outstanding stop directive. 1127 */ 1128 if (!(P->status.pr_flags & (PR_ISTOP|PR_DSTOP))) { 1129 Pdprintf("Pgrab: process is not stopped\n"); 1130 rc = G_STRANGE; 1131 goto err; 1132 } 1133 #ifndef _LP64 1134 /* 1135 * Test this again now because the 32-bit victim process may 1136 * have exec'd a 64-bit process in the meantime. 1137 */ 1138 if (P->status.pr_dmodel == PR_MODEL_LP64) { 1139 rc = G_LP64; 1140 goto err; 1141 } 1142 #endif 1143 } 1144 1145 /* 1146 * Cancel all tracing flags unless the PGRAB_RETAIN flag is set. 1147 */ 1148 if (!(flags & PGRAB_RETAIN)) { 1149 (void) Psysentry(P, 0, FALSE); 1150 (void) Psysexit(P, 0, FALSE); 1151 (void) Psignal(P, 0, FALSE); 1152 (void) Pfault(P, 0, FALSE); 1153 Psync(P); 1154 } 1155 1156 *perr = 0; 1157 return (P); 1158 1159 err: 1160 Pfree(P); 1161 *perr = rc; 1162 return (NULL); 1163 } 1164 1165 /* 1166 * Return a printable string corresponding to a Pgrab() error return. 1167 */ 1168 const char * 1169 Pgrab_error(int error) 1170 { 1171 const char *str; 1172 1173 switch (error) { 1174 case G_NOPROC: 1175 str = "no such process"; 1176 break; 1177 case G_NOCORE: 1178 str = "no such core file"; 1179 break; 1180 case G_NOPROCORCORE: 1181 str = "no such process or core file"; 1182 break; 1183 case G_NOEXEC: 1184 str = "cannot find executable file"; 1185 break; 1186 case G_ZOMB: 1187 str = "zombie process"; 1188 break; 1189 case G_PERM: 1190 str = "permission denied"; 1191 break; 1192 case G_BUSY: 1193 str = "process is traced"; 1194 break; 1195 case G_SYS: 1196 str = "system process"; 1197 break; 1198 case G_SELF: 1199 str = "attempt to grab self"; 1200 break; 1201 case G_INTR: 1202 str = "operation interrupted"; 1203 break; 1204 case G_LP64: 1205 str = "program is _LP64, self is not"; 1206 break; 1207 case G_FORMAT: 1208 str = "file is not an ELF core file"; 1209 break; 1210 case G_ELF: 1211 str = "libelf error"; 1212 break; 1213 case G_NOTE: 1214 str = "core file is corrupt or missing required data"; 1215 break; 1216 case G_STRANGE: 1217 str = "unanticipated system error"; 1218 break; 1219 case G_ISAINVAL: 1220 str = "wrong ELF machine type"; 1221 break; 1222 case G_BADLWPS: 1223 str = "bad lwp specification"; 1224 break; 1225 case G_NOFD: 1226 str = "too many open files"; 1227 break; 1228 default: 1229 str = "unknown error"; 1230 break; 1231 } 1232 1233 return (str); 1234 } 1235 1236 /* 1237 * Free a process control structure. 1238 * Close the file descriptors but don't do the Prelease logic. 1239 */ 1240 void 1241 Pfree(struct ps_prochandle *P) 1242 { 1243 uint_t i; 1244 fd_info_t *fip; 1245 1246 if (P->ucaddrs != NULL) { 1247 free(P->ucaddrs); 1248 P->ucaddrs = NULL; 1249 P->ucnelems = 0; 1250 } 1251 1252 (void) mutex_lock(&P->proc_lock); 1253 if (P->hashtab != NULL) { 1254 struct ps_lwphandle *L; 1255 for (i = 0; i < HASHSIZE; i++) { 1256 while ((L = P->hashtab[i]) != NULL) 1257 Lfree_internal(P, L); 1258 } 1259 free(P->hashtab); 1260 } 1261 1262 while ((fip = list_remove_head(&P->fd_head)) != NULL) { 1263 proc_fdinfo_free(fip->fd_info); 1264 free(fip); 1265 } 1266 (void) mutex_unlock(&P->proc_lock); 1267 (void) mutex_destroy(&P->proc_lock); 1268 1269 free(P->zoneroot); 1270 1271 if (P->agentctlfd >= 0) 1272 (void) close(P->agentctlfd); 1273 if (P->agentstatfd >= 0) 1274 (void) close(P->agentstatfd); 1275 if (P->ctlfd >= 0) 1276 (void) close(P->ctlfd); 1277 if (P->asfd >= 0) 1278 (void) close(P->asfd); 1279 if (P->statfd >= 0) 1280 (void) close(P->statfd); 1281 Preset_maps(P); 1282 P->ops.pop_fini(P, P->data); 1283 1284 /* clear out the structure as a precaution against reuse */ 1285 (void) memset(P, 0, sizeof (*P)); 1286 P->ctlfd = -1; 1287 P->asfd = -1; 1288 P->statfd = -1; 1289 P->agentctlfd = -1; 1290 P->agentstatfd = -1; 1291 1292 free(P); 1293 } 1294 1295 /* 1296 * Return the state of the process, one of the PS_* values. 1297 */ 1298 int 1299 Pstate(struct ps_prochandle *P) 1300 { 1301 return (P->state); 1302 } 1303 1304 /* 1305 * Return the open address space file descriptor for the process. 1306 * Clients must not close this file descriptor, not use it 1307 * after the process is freed. 1308 */ 1309 int 1310 Pasfd(struct ps_prochandle *P) 1311 { 1312 return (P->asfd); 1313 } 1314 1315 /* 1316 * Return the open control file descriptor for the process. 1317 * Clients must not close this file descriptor, not use it 1318 * after the process is freed. 1319 */ 1320 int 1321 Pctlfd(struct ps_prochandle *P) 1322 { 1323 return (P->ctlfd); 1324 } 1325 1326 /* 1327 * Return a pointer to the process psinfo structure. 1328 * Clients should not hold on to this pointer indefinitely. 1329 * It will become invalid on Prelease(). 1330 */ 1331 const psinfo_t * 1332 Ppsinfo(struct ps_prochandle *P) 1333 { 1334 return (P->ops.pop_psinfo(P, &P->psinfo, P->data)); 1335 } 1336 1337 /* 1338 * Return a pointer to the process status structure. 1339 * Clients should not hold on to this pointer indefinitely. 1340 * It will become invalid on Prelease(). 1341 */ 1342 const pstatus_t * 1343 Pstatus(struct ps_prochandle *P) 1344 { 1345 return (&P->status); 1346 } 1347 1348 static void 1349 Pread_status(struct ps_prochandle *P) 1350 { 1351 P->ops.pop_status(P, &P->status, P->data); 1352 } 1353 1354 /* 1355 * Fill in a pointer to a process credentials structure. The ngroups parameter 1356 * is the number of supplementary group entries allocated in the caller's cred 1357 * structure. It should equal zero or one unless extra space has been 1358 * allocated for the group list by the caller. 1359 */ 1360 int 1361 Pcred(struct ps_prochandle *P, prcred_t *pcrp, int ngroups) 1362 { 1363 return (P->ops.pop_cred(P, pcrp, ngroups, P->data)); 1364 } 1365 1366 /* Return an allocated prsecflags_t */ 1367 int 1368 Psecflags(struct ps_prochandle *P, prsecflags_t **psf) 1369 { 1370 int ret; 1371 1372 if ((ret = P->ops.pop_secflags(P, psf, P->data)) == 0) { 1373 if ((*psf)->pr_version != PRSECFLAGS_VERSION_1) { 1374 free(*psf); 1375 *psf = NULL; 1376 errno = EINVAL; 1377 return (-1); 1378 } 1379 } 1380 1381 return (ret); 1382 } 1383 1384 void 1385 Psecflags_free(prsecflags_t *psf) 1386 { 1387 free(psf); 1388 } 1389 1390 int 1391 Pcwd(struct ps_prochandle *P, prcwd_t **cwd) 1392 { 1393 return (P->ops.pop_cwd(P, cwd, P->data)); 1394 } 1395 1396 void 1397 Pcwd_free(prcwd_t *cwd) 1398 { 1399 free(cwd); 1400 } 1401 1402 static prheader_t * 1403 Plstatus(struct ps_prochandle *P) 1404 { 1405 return (P->ops.pop_lstatus(P, P->data)); 1406 } 1407 1408 static prheader_t * 1409 Plpsinfo(struct ps_prochandle *P) 1410 { 1411 return (P->ops.pop_lpsinfo(P, P->data)); 1412 } 1413 1414 1415 #if defined(__i386) || defined(__amd64) 1416 /* 1417 * Fill in a pointer to a process LDT structure. 1418 * The caller provides a buffer of size 'nldt * sizeof (struct ssd)'; 1419 * If pldt == NULL or nldt == 0, we return the number of existing LDT entries. 1420 * Otherwise we return the actual number of LDT entries fetched (<= nldt). 1421 */ 1422 int 1423 Pldt(struct ps_prochandle *P, struct ssd *pldt, int nldt) 1424 { 1425 return (P->ops.pop_ldt(P, pldt, nldt, P->data)); 1426 1427 } 1428 #endif /* __i386 */ 1429 1430 void 1431 Ppriv_free(struct ps_prochandle *P, prpriv_t *prv) 1432 { 1433 free(prv); 1434 } 1435 1436 /* 1437 * Return a malloced process privilege structure in *pprv. 1438 */ 1439 int 1440 Ppriv(struct ps_prochandle *P, prpriv_t **pprv) 1441 { 1442 return (P->ops.pop_priv(P, pprv, P->data)); 1443 } 1444 1445 int 1446 Psetpriv(struct ps_prochandle *P, prpriv_t *pprv) 1447 { 1448 int rc; 1449 long *ctl; 1450 size_t sz; 1451 1452 if (P->state == PS_DEAD) { 1453 errno = EBADF; 1454 return (-1); 1455 } 1456 1457 sz = PRIV_PRPRIV_SIZE(pprv) + sizeof (long); 1458 1459 sz = ((sz - 1) / sizeof (long) + 1) * sizeof (long); 1460 1461 ctl = malloc(sz); 1462 if (ctl == NULL) 1463 return (-1); 1464 1465 ctl[0] = PCSPRIV; 1466 1467 (void) memcpy(&ctl[1], pprv, PRIV_PRPRIV_SIZE(pprv)); 1468 1469 if (write(P->ctlfd, ctl, sz) != sz) 1470 rc = -1; 1471 else 1472 rc = 0; 1473 1474 free(ctl); 1475 1476 return (rc); 1477 } 1478 1479 void * 1480 Pprivinfo(struct ps_prochandle *P) 1481 { 1482 core_info_t *core = P->data; 1483 1484 /* Use default from libc */ 1485 if (P->state != PS_DEAD) 1486 return (NULL); 1487 1488 return (core->core_privinfo); 1489 } 1490 1491 /* 1492 * Ensure that all cached state is written to the process. 1493 * The cached state is the LWP's signal mask and registers 1494 * and the process's tracing flags. 1495 */ 1496 void 1497 Psync(struct ps_prochandle *P) 1498 { 1499 int ctlfd = (P->agentctlfd >= 0)? P->agentctlfd : P->ctlfd; 1500 long cmd[6]; 1501 iovec_t iov[12]; 1502 int n = 0; 1503 1504 if (P->flags & SETHOLD) { 1505 cmd[0] = PCSHOLD; 1506 iov[n].iov_base = (caddr_t)&cmd[0]; 1507 iov[n++].iov_len = sizeof (long); 1508 iov[n].iov_base = (caddr_t)&P->status.pr_lwp.pr_lwphold; 1509 iov[n++].iov_len = sizeof (P->status.pr_lwp.pr_lwphold); 1510 } 1511 if (P->flags & SETREGS) { 1512 cmd[1] = PCSREG; 1513 #ifdef __i386 1514 /* XX64 we should probably restore REG_GS after this */ 1515 if (ctlfd == P->agentctlfd) 1516 P->status.pr_lwp.pr_reg[GS] = 0; 1517 #elif defined(__amd64) 1518 /* XX64 */ 1519 #endif 1520 iov[n].iov_base = (caddr_t)&cmd[1]; 1521 iov[n++].iov_len = sizeof (long); 1522 iov[n].iov_base = (caddr_t)&P->status.pr_lwp.pr_reg[0]; 1523 iov[n++].iov_len = sizeof (P->status.pr_lwp.pr_reg); 1524 } 1525 if (P->flags & SETSIG) { 1526 cmd[2] = PCSTRACE; 1527 iov[n].iov_base = (caddr_t)&cmd[2]; 1528 iov[n++].iov_len = sizeof (long); 1529 iov[n].iov_base = (caddr_t)&P->status.pr_sigtrace; 1530 iov[n++].iov_len = sizeof (P->status.pr_sigtrace); 1531 } 1532 if (P->flags & SETFAULT) { 1533 cmd[3] = PCSFAULT; 1534 iov[n].iov_base = (caddr_t)&cmd[3]; 1535 iov[n++].iov_len = sizeof (long); 1536 iov[n].iov_base = (caddr_t)&P->status.pr_flttrace; 1537 iov[n++].iov_len = sizeof (P->status.pr_flttrace); 1538 } 1539 if (P->flags & SETENTRY) { 1540 cmd[4] = PCSENTRY; 1541 iov[n].iov_base = (caddr_t)&cmd[4]; 1542 iov[n++].iov_len = sizeof (long); 1543 iov[n].iov_base = (caddr_t)&P->status.pr_sysentry; 1544 iov[n++].iov_len = sizeof (P->status.pr_sysentry); 1545 } 1546 if (P->flags & SETEXIT) { 1547 cmd[5] = PCSEXIT; 1548 iov[n].iov_base = (caddr_t)&cmd[5]; 1549 iov[n++].iov_len = sizeof (long); 1550 iov[n].iov_base = (caddr_t)&P->status.pr_sysexit; 1551 iov[n++].iov_len = sizeof (P->status.pr_sysexit); 1552 } 1553 1554 if (n == 0 || writev(ctlfd, iov, n) < 0) 1555 return; /* nothing to do or write failed */ 1556 1557 P->flags &= ~(SETSIG|SETFAULT|SETENTRY|SETEXIT|SETHOLD|SETREGS); 1558 } 1559 1560 /* 1561 * Reopen the /proc file (after PS_LOST). 1562 */ 1563 int 1564 Preopen(struct ps_prochandle *P) 1565 { 1566 int fd; 1567 char procname[PATH_MAX]; 1568 char *fname; 1569 1570 if (P->state == PS_DEAD || P->state == PS_IDLE) 1571 return (0); 1572 1573 if (P->agentcnt > 0) { 1574 P->agentcnt = 1; 1575 Pdestroy_agent(P); 1576 } 1577 1578 (void) snprintf(procname, sizeof (procname), "%s/%d/", 1579 procfs_path, (int)P->pid); 1580 fname = procname + strlen(procname); 1581 1582 (void) strcpy(fname, "as"); 1583 if ((fd = open(procname, O_RDWR)) < 0 || 1584 close(P->asfd) < 0 || 1585 (fd = dupfd(fd, P->asfd)) != P->asfd) { 1586 Pdprintf("Preopen: failed to open %s: %s\n", 1587 procname, strerror(errno)); 1588 if (fd >= 0) 1589 (void) close(fd); 1590 return (-1); 1591 } 1592 P->asfd = fd; 1593 1594 (void) strcpy(fname, "status"); 1595 if ((fd = open(procname, O_RDONLY)) < 0 || 1596 close(P->statfd) < 0 || 1597 (fd = dupfd(fd, P->statfd)) != P->statfd) { 1598 Pdprintf("Preopen: failed to open %s: %s\n", 1599 procname, strerror(errno)); 1600 if (fd >= 0) 1601 (void) close(fd); 1602 return (-1); 1603 } 1604 P->statfd = fd; 1605 1606 (void) strcpy(fname, "ctl"); 1607 if ((fd = open(procname, O_WRONLY)) < 0 || 1608 close(P->ctlfd) < 0 || 1609 (fd = dupfd(fd, P->ctlfd)) != P->ctlfd) { 1610 Pdprintf("Preopen: failed to open %s: %s\n", 1611 procname, strerror(errno)); 1612 if (fd >= 0) 1613 (void) close(fd); 1614 return (-1); 1615 } 1616 P->ctlfd = fd; 1617 1618 /* 1619 * Set the state to PS_RUN and wait for the process to stop so that 1620 * we re-read the status from the new P->statfd. If this fails, Pwait 1621 * will reset the state to PS_LOST and we fail the reopen. Before 1622 * returning, we also forge a bit of P->status to allow the debugger to 1623 * see that we are PS_LOST following a successful exec. 1624 */ 1625 P->state = PS_RUN; 1626 if (Pwait(P, 0) == -1) { 1627 #ifdef _ILP32 1628 if (errno == EOVERFLOW) 1629 P->status.pr_dmodel = PR_MODEL_LP64; 1630 #endif 1631 P->status.pr_lwp.pr_why = PR_SYSEXIT; 1632 P->status.pr_lwp.pr_what = SYS_execve; 1633 P->status.pr_lwp.pr_errno = 0; 1634 return (-1); 1635 } 1636 1637 /* 1638 * The process should be stopped on exec (REQUESTED) 1639 * or else should be stopped on exit from exec() (SYSEXIT) 1640 */ 1641 if (P->state == PS_STOP && 1642 (P->status.pr_lwp.pr_why == PR_REQUESTED || 1643 (P->status.pr_lwp.pr_why == PR_SYSEXIT && 1644 P->status.pr_lwp.pr_what == SYS_execve))) { 1645 /* fake up stop-on-exit-from-execve */ 1646 if (P->status.pr_lwp.pr_why == PR_REQUESTED) { 1647 P->status.pr_lwp.pr_why = PR_SYSEXIT; 1648 P->status.pr_lwp.pr_what = SYS_execve; 1649 P->status.pr_lwp.pr_errno = 0; 1650 } 1651 } else { 1652 Pdprintf("Preopen: expected REQUESTED or " 1653 "SYSEXIT(SYS_execve) stop\n"); 1654 } 1655 1656 return (0); 1657 } 1658 1659 /* 1660 * Define all settable flags other than the microstate accounting flags. 1661 */ 1662 #define ALL_SETTABLE_FLAGS (PR_FORK|PR_RLC|PR_KLC|PR_ASYNC|PR_BPTADJ|PR_PTRACE) 1663 1664 /* 1665 * Restore /proc tracing flags to their original values 1666 * in preparation for releasing the process. 1667 * Also called by Pcreate() to clear all tracing flags. 1668 */ 1669 static void 1670 restore_tracing_flags(struct ps_prochandle *P) 1671 { 1672 long flags; 1673 long cmd[4]; 1674 iovec_t iov[8]; 1675 1676 if (P->flags & CREATED) { 1677 /* we created this process; clear all tracing flags */ 1678 premptyset(&P->status.pr_sigtrace); 1679 premptyset(&P->status.pr_flttrace); 1680 premptyset(&P->status.pr_sysentry); 1681 premptyset(&P->status.pr_sysexit); 1682 if ((P->status.pr_flags & ALL_SETTABLE_FLAGS) != 0) 1683 (void) Punsetflags(P, ALL_SETTABLE_FLAGS); 1684 } else { 1685 /* we grabbed the process; restore its tracing flags */ 1686 P->status.pr_sigtrace = P->orig_status.pr_sigtrace; 1687 P->status.pr_flttrace = P->orig_status.pr_flttrace; 1688 P->status.pr_sysentry = P->orig_status.pr_sysentry; 1689 P->status.pr_sysexit = P->orig_status.pr_sysexit; 1690 if ((P->status.pr_flags & ALL_SETTABLE_FLAGS) != 1691 (flags = (P->orig_status.pr_flags & ALL_SETTABLE_FLAGS))) { 1692 (void) Punsetflags(P, ALL_SETTABLE_FLAGS); 1693 if (flags) 1694 (void) Psetflags(P, flags); 1695 } 1696 } 1697 1698 cmd[0] = PCSTRACE; 1699 iov[0].iov_base = (caddr_t)&cmd[0]; 1700 iov[0].iov_len = sizeof (long); 1701 iov[1].iov_base = (caddr_t)&P->status.pr_sigtrace; 1702 iov[1].iov_len = sizeof (P->status.pr_sigtrace); 1703 1704 cmd[1] = PCSFAULT; 1705 iov[2].iov_base = (caddr_t)&cmd[1]; 1706 iov[2].iov_len = sizeof (long); 1707 iov[3].iov_base = (caddr_t)&P->status.pr_flttrace; 1708 iov[3].iov_len = sizeof (P->status.pr_flttrace); 1709 1710 cmd[2] = PCSENTRY; 1711 iov[4].iov_base = (caddr_t)&cmd[2]; 1712 iov[4].iov_len = sizeof (long); 1713 iov[5].iov_base = (caddr_t)&P->status.pr_sysentry; 1714 iov[5].iov_len = sizeof (P->status.pr_sysentry); 1715 1716 cmd[3] = PCSEXIT; 1717 iov[6].iov_base = (caddr_t)&cmd[3]; 1718 iov[6].iov_len = sizeof (long); 1719 iov[7].iov_base = (caddr_t)&P->status.pr_sysexit; 1720 iov[7].iov_len = sizeof (P->status.pr_sysexit); 1721 1722 (void) writev(P->ctlfd, iov, 8); 1723 1724 P->flags &= ~(SETSIG|SETFAULT|SETENTRY|SETEXIT); 1725 } 1726 1727 /* 1728 * Release the process. Frees the process control structure. 1729 * flags: 1730 * PRELEASE_CLEAR Clear all tracing flags. 1731 * PRELEASE_RETAIN Retain current tracing flags. 1732 * PRELEASE_HANG Leave the process stopped and abandoned. 1733 * PRELEASE_KILL Terminate the process with SIGKILL. 1734 */ 1735 void 1736 Prelease(struct ps_prochandle *P, int flags) 1737 { 1738 if (P->state == PS_DEAD) { 1739 Pdprintf("Prelease: releasing handle %p PS_DEAD of pid %d\n", 1740 (void *)P, (int)P->pid); 1741 Pfree(P); 1742 return; 1743 } 1744 1745 if (P->state == PS_IDLE) { 1746 file_info_t *fptr = list_head(&P->file_head); 1747 Pdprintf("Prelease: releasing handle %p PS_IDLE of file %s\n", 1748 (void *)P, fptr->file_pname); 1749 Pfree(P); 1750 return; 1751 } 1752 1753 Pdprintf("Prelease: releasing handle %p pid %d\n", 1754 (void *)P, (int)P->pid); 1755 1756 if (P->ctlfd == -1) { 1757 Pfree(P); 1758 return; 1759 } 1760 1761 if (P->agentcnt > 0) { 1762 P->agentcnt = 1; 1763 Pdestroy_agent(P); 1764 } 1765 1766 /* 1767 * Attempt to stop the process. 1768 */ 1769 P->state = PS_RUN; 1770 (void) Pstop(P, 1000); 1771 1772 if (flags & PRELEASE_KILL) { 1773 if (P->state == PS_STOP) 1774 (void) Psetrun(P, SIGKILL, 0); 1775 (void) kill(P->pid, SIGKILL); 1776 Pfree(P); 1777 return; 1778 } 1779 1780 /* 1781 * If we lost control, all we can do now is close the files. 1782 * In this case, the last close sets the process running. 1783 */ 1784 if (P->state != PS_STOP && 1785 (P->status.pr_lwp.pr_flags & (PR_ISTOP|PR_DSTOP)) == 0) { 1786 Pfree(P); 1787 return; 1788 } 1789 1790 /* 1791 * We didn't lose control; we do more. 1792 */ 1793 Psync(P); 1794 1795 if (flags & PRELEASE_CLEAR) 1796 P->flags |= CREATED; 1797 1798 if (!(flags & PRELEASE_RETAIN)) 1799 restore_tracing_flags(P); 1800 1801 if (flags & PRELEASE_HANG) { 1802 /* Leave the process stopped and abandoned */ 1803 (void) Punsetflags(P, PR_RLC|PR_KLC); 1804 Pfree(P); 1805 return; 1806 } 1807 1808 /* 1809 * Set the process running if we created it or if it was 1810 * not originally stopped or directed to stop via /proc 1811 * or if we were given the PRELEASE_CLEAR flag. 1812 */ 1813 if ((P->flags & CREATED) || 1814 (P->orig_status.pr_lwp.pr_flags & (PR_ISTOP|PR_DSTOP)) == 0) { 1815 (void) Psetflags(P, PR_RLC); 1816 /* 1817 * We do this repeatedly because the process may have 1818 * more than one LWP stopped on an event of interest. 1819 * This makes sure all of them are set running. 1820 */ 1821 do { 1822 if (Psetrun(P, 0, 0) == -1 && errno == EBUSY) 1823 break; /* Agent LWP may be stuck */ 1824 } while (Pstopstatus(P, PCNULL, 0) == 0 && 1825 P->status.pr_lwp.pr_flags & (PR_ISTOP|PR_DSTOP)); 1826 1827 if (P->status.pr_lwp.pr_flags & (PR_ISTOP|PR_DSTOP)) 1828 Pdprintf("Prelease: failed to set process running\n"); 1829 } 1830 1831 Pfree(P); 1832 } 1833 1834 /* debugging */ 1835 void 1836 prldump(const char *caller, lwpstatus_t *lsp) 1837 { 1838 char name[32]; 1839 uint32_t bits; 1840 1841 switch (lsp->pr_why) { 1842 case PR_REQUESTED: 1843 Pdprintf("%s: REQUESTED\n", caller); 1844 break; 1845 case PR_SIGNALLED: 1846 Pdprintf("%s: SIGNALLED %s\n", caller, 1847 proc_signame(lsp->pr_what, name, sizeof (name))); 1848 break; 1849 case PR_FAULTED: 1850 Pdprintf("%s: FAULTED %s\n", caller, 1851 proc_fltname(lsp->pr_what, name, sizeof (name))); 1852 break; 1853 case PR_SYSENTRY: 1854 Pdprintf("%s: SYSENTRY %s\n", caller, 1855 proc_sysname(lsp->pr_what, name, sizeof (name))); 1856 break; 1857 case PR_SYSEXIT: 1858 Pdprintf("%s: SYSEXIT %s\n", caller, 1859 proc_sysname(lsp->pr_what, name, sizeof (name))); 1860 break; 1861 case PR_JOBCONTROL: 1862 Pdprintf("%s: JOBCONTROL %s\n", caller, 1863 proc_signame(lsp->pr_what, name, sizeof (name))); 1864 break; 1865 case PR_SUSPENDED: 1866 Pdprintf("%s: SUSPENDED\n", caller); 1867 break; 1868 default: 1869 Pdprintf("%s: Unknown\n", caller); 1870 break; 1871 } 1872 1873 if (lsp->pr_cursig) 1874 Pdprintf("%s: p_cursig = %d\n", caller, lsp->pr_cursig); 1875 1876 bits = *((uint32_t *)&lsp->pr_lwppend); 1877 if (bits) 1878 Pdprintf("%s: pr_lwppend = 0x%.8X\n", caller, bits); 1879 } 1880 1881 /* debugging */ 1882 static void 1883 prdump(struct ps_prochandle *P) 1884 { 1885 uint32_t bits; 1886 1887 prldump("Pstopstatus", &P->status.pr_lwp); 1888 1889 bits = *((uint32_t *)&P->status.pr_sigpend); 1890 if (bits) 1891 Pdprintf("Pstopstatus: pr_sigpend = 0x%.8X\n", bits); 1892 } 1893 1894 /* 1895 * Wait for the specified process to stop or terminate. 1896 * Or, just get the current status (PCNULL). 1897 * Or, direct it to stop and get the current status (PCDSTOP). 1898 * If the agent LWP exists, do these things to the agent, 1899 * else do these things to the process as a whole. 1900 */ 1901 int 1902 Pstopstatus(struct ps_prochandle *P, 1903 long request, /* PCNULL, PCDSTOP, PCSTOP, PCWSTOP */ 1904 uint_t msec) /* if non-zero, timeout in milliseconds */ 1905 { 1906 int ctlfd = (P->agentctlfd >= 0)? P->agentctlfd : P->ctlfd; 1907 long ctl[3]; 1908 ssize_t rc; 1909 int err; 1910 int old_state = P->state; 1911 1912 switch (P->state) { 1913 case PS_RUN: 1914 break; 1915 case PS_STOP: 1916 if (request != PCNULL && request != PCDSTOP) 1917 return (0); 1918 break; 1919 case PS_LOST: 1920 if (request != PCNULL) { 1921 errno = EAGAIN; 1922 return (-1); 1923 } 1924 break; 1925 case PS_UNDEAD: 1926 case PS_DEAD: 1927 case PS_IDLE: 1928 if (request != PCNULL) { 1929 errno = ENOENT; 1930 return (-1); 1931 } 1932 break; 1933 default: /* corrupted state */ 1934 Pdprintf("Pstopstatus: corrupted state: %d\n", P->state); 1935 errno = EINVAL; 1936 return (-1); 1937 } 1938 1939 ctl[0] = PCDSTOP; 1940 ctl[1] = PCTWSTOP; 1941 ctl[2] = (long)msec; 1942 rc = 0; 1943 switch (request) { 1944 case PCSTOP: 1945 rc = write(ctlfd, &ctl[0], 3*sizeof (long)); 1946 break; 1947 case PCWSTOP: 1948 rc = write(ctlfd, &ctl[1], 2*sizeof (long)); 1949 break; 1950 case PCDSTOP: 1951 rc = write(ctlfd, &ctl[0], 1*sizeof (long)); 1952 break; 1953 case PCNULL: 1954 if (P->state == PS_DEAD || P->state == PS_IDLE) 1955 return (0); 1956 break; 1957 default: /* programming error */ 1958 errno = EINVAL; 1959 return (-1); 1960 } 1961 err = (rc < 0)? errno : 0; 1962 Psync(P); 1963 1964 if (P->agentstatfd < 0) { 1965 if (pread(P->statfd, &P->status, 1966 sizeof (P->status), (off_t)0) < 0) 1967 err = errno; 1968 } else { 1969 if (pread(P->agentstatfd, &P->status.pr_lwp, 1970 sizeof (P->status.pr_lwp), (off_t)0) < 0) 1971 err = errno; 1972 P->status.pr_flags = P->status.pr_lwp.pr_flags; 1973 } 1974 1975 if (err) { 1976 switch (err) { 1977 case EINTR: /* user typed ctl-C */ 1978 case ERESTART: 1979 Pdprintf("Pstopstatus: EINTR\n"); 1980 break; 1981 case EAGAIN: /* we lost control of the the process */ 1982 case EOVERFLOW: 1983 Pdprintf("Pstopstatus: PS_LOST, errno=%d\n", err); 1984 P->state = PS_LOST; 1985 break; 1986 default: /* check for dead process */ 1987 if (_libproc_debug) { 1988 const char *errstr; 1989 1990 switch (request) { 1991 case PCNULL: 1992 errstr = "Pstopstatus PCNULL"; break; 1993 case PCSTOP: 1994 errstr = "Pstopstatus PCSTOP"; break; 1995 case PCDSTOP: 1996 errstr = "Pstopstatus PCDSTOP"; break; 1997 case PCWSTOP: 1998 errstr = "Pstopstatus PCWSTOP"; break; 1999 default: 2000 errstr = "Pstopstatus PC???"; break; 2001 } 2002 Pdprintf("%s: %s\n", errstr, strerror(err)); 2003 } 2004 deadcheck(P); 2005 break; 2006 } 2007 if (err != EINTR && err != ERESTART) { 2008 errno = err; 2009 return (-1); 2010 } 2011 } 2012 2013 if (!(P->status.pr_flags & PR_STOPPED)) { 2014 P->state = PS_RUN; 2015 if (request == PCNULL || request == PCDSTOP || msec != 0) 2016 return (0); 2017 Pdprintf("Pstopstatus: process is not stopped\n"); 2018 errno = EPROTO; 2019 return (-1); 2020 } 2021 2022 P->state = PS_STOP; 2023 2024 if (_libproc_debug) /* debugging */ 2025 prdump(P); 2026 2027 /* 2028 * If the process was already stopped coming into Pstopstatus(), 2029 * then don't use its PC to set P->sysaddr since it may have been 2030 * changed since the time the process originally stopped. 2031 */ 2032 if (old_state == PS_STOP) 2033 return (0); 2034 2035 switch (P->status.pr_lwp.pr_why) { 2036 case PR_SYSENTRY: 2037 case PR_SYSEXIT: 2038 if (Pissyscall_prev(P, P->status.pr_lwp.pr_reg[R_PC], 2039 &P->sysaddr) == 0) 2040 P->sysaddr = P->status.pr_lwp.pr_reg[R_PC]; 2041 break; 2042 case PR_REQUESTED: 2043 case PR_SIGNALLED: 2044 case PR_FAULTED: 2045 case PR_JOBCONTROL: 2046 case PR_SUSPENDED: 2047 break; 2048 default: 2049 errno = EPROTO; 2050 return (-1); 2051 } 2052 2053 return (0); 2054 } 2055 2056 /* 2057 * Wait for the process to stop for any reason. 2058 */ 2059 int 2060 Pwait(struct ps_prochandle *P, uint_t msec) 2061 { 2062 return (Pstopstatus(P, PCWSTOP, msec)); 2063 } 2064 2065 /* 2066 * Direct the process to stop; wait for it to stop. 2067 */ 2068 int 2069 Pstop(struct ps_prochandle *P, uint_t msec) 2070 { 2071 return (Pstopstatus(P, PCSTOP, msec)); 2072 } 2073 2074 /* 2075 * Direct the process to stop; don't wait. 2076 */ 2077 int 2078 Pdstop(struct ps_prochandle *P) 2079 { 2080 return (Pstopstatus(P, PCDSTOP, 0)); 2081 } 2082 2083 static void 2084 deadcheck(struct ps_prochandle *P) 2085 { 2086 int fd; 2087 void *buf; 2088 size_t size; 2089 2090 if (P->statfd < 0) 2091 P->state = PS_UNDEAD; 2092 else { 2093 if (P->agentstatfd < 0) { 2094 fd = P->statfd; 2095 buf = &P->status; 2096 size = sizeof (P->status); 2097 } else { 2098 fd = P->agentstatfd; 2099 buf = &P->status.pr_lwp; 2100 size = sizeof (P->status.pr_lwp); 2101 } 2102 while (pread(fd, buf, size, (off_t)0) != size) { 2103 switch (errno) { 2104 default: 2105 P->state = PS_UNDEAD; 2106 break; 2107 case EINTR: 2108 case ERESTART: 2109 continue; 2110 case EAGAIN: 2111 P->state = PS_LOST; 2112 break; 2113 } 2114 break; 2115 } 2116 P->status.pr_flags = P->status.pr_lwp.pr_flags; 2117 } 2118 } 2119 2120 /* 2121 * Get the value of one register from stopped process. 2122 */ 2123 int 2124 Pgetareg(struct ps_prochandle *P, int regno, prgreg_t *preg) 2125 { 2126 if (regno < 0 || regno >= NPRGREG) { 2127 errno = EINVAL; 2128 return (-1); 2129 } 2130 2131 if (P->state == PS_IDLE) { 2132 errno = ENODATA; 2133 return (-1); 2134 } 2135 2136 if (P->state != PS_STOP && P->state != PS_DEAD) { 2137 errno = EBUSY; 2138 return (-1); 2139 } 2140 2141 *preg = P->status.pr_lwp.pr_reg[regno]; 2142 return (0); 2143 } 2144 2145 /* 2146 * Put value of one register into stopped process. 2147 */ 2148 int 2149 Pputareg(struct ps_prochandle *P, int regno, prgreg_t reg) 2150 { 2151 if (regno < 0 || regno >= NPRGREG) { 2152 errno = EINVAL; 2153 return (-1); 2154 } 2155 2156 if (P->state != PS_STOP) { 2157 errno = EBUSY; 2158 return (-1); 2159 } 2160 2161 P->status.pr_lwp.pr_reg[regno] = reg; 2162 P->flags |= SETREGS; /* set registers before continuing */ 2163 return (0); 2164 } 2165 2166 int 2167 Psetrun(struct ps_prochandle *P, 2168 int sig, /* signal to pass to process */ 2169 int flags) /* PRSTEP|PRSABORT|PRSTOP|PRCSIG|PRCFAULT */ 2170 { 2171 int ctlfd = (P->agentctlfd >= 0) ? P->agentctlfd : P->ctlfd; 2172 int sbits = (PR_DSTOP | PR_ISTOP | PR_ASLEEP); 2173 2174 long ctl[1 + /* PCCFAULT */ 2175 1 + sizeof (siginfo_t)/sizeof (long) + /* PCSSIG/PCCSIG */ 2176 2 ]; /* PCRUN */ 2177 2178 long *ctlp = ctl; 2179 size_t size; 2180 2181 if (P->state != PS_STOP && (P->status.pr_lwp.pr_flags & sbits) == 0) { 2182 errno = EBUSY; 2183 return (-1); 2184 } 2185 2186 Psync(P); /* flush tracing flags and registers */ 2187 2188 if (flags & PRCFAULT) { /* clear current fault */ 2189 *ctlp++ = PCCFAULT; 2190 flags &= ~PRCFAULT; 2191 } 2192 2193 if (flags & PRCSIG) { /* clear current signal */ 2194 *ctlp++ = PCCSIG; 2195 flags &= ~PRCSIG; 2196 } else if (sig && sig != P->status.pr_lwp.pr_cursig) { 2197 /* make current signal */ 2198 siginfo_t *infop; 2199 2200 *ctlp++ = PCSSIG; 2201 infop = (siginfo_t *)ctlp; 2202 (void) memset(infop, 0, sizeof (*infop)); 2203 infop->si_signo = sig; 2204 ctlp += sizeof (siginfo_t) / sizeof (long); 2205 } 2206 2207 *ctlp++ = PCRUN; 2208 *ctlp++ = flags; 2209 size = (char *)ctlp - (char *)ctl; 2210 2211 P->info_valid = 0; /* will need to update map and file info */ 2212 2213 /* 2214 * If we've cached ucontext-list information while we were stopped, 2215 * free it now. 2216 */ 2217 if (P->ucaddrs != NULL) { 2218 free(P->ucaddrs); 2219 P->ucaddrs = NULL; 2220 P->ucnelems = 0; 2221 } 2222 2223 if (write(ctlfd, ctl, size) != size) { 2224 /* If it is dead or lost, return the real status, not PS_RUN */ 2225 if (errno == ENOENT || errno == EAGAIN) { 2226 (void) Pstopstatus(P, PCNULL, 0); 2227 return (0); 2228 } 2229 /* If it is not in a jobcontrol stop, issue an error message */ 2230 if (errno != EBUSY || 2231 P->status.pr_lwp.pr_why != PR_JOBCONTROL) { 2232 Pdprintf("Psetrun: %s\n", strerror(errno)); 2233 return (-1); 2234 } 2235 /* Otherwise pretend that the job-stopped process is running */ 2236 } 2237 2238 P->state = PS_RUN; 2239 return (0); 2240 } 2241 2242 ssize_t 2243 Pread(struct ps_prochandle *P, 2244 void *buf, /* caller's buffer */ 2245 size_t nbyte, /* number of bytes to read */ 2246 uintptr_t address) /* address in process */ 2247 { 2248 return (P->ops.pop_pread(P, buf, nbyte, address, P->data)); 2249 } 2250 2251 ssize_t 2252 Pread_string(struct ps_prochandle *P, 2253 char *buf, /* caller's buffer */ 2254 size_t size, /* upper limit on bytes to read */ 2255 uintptr_t addr) /* address in process */ 2256 { 2257 enum { STRSZ = 40 }; 2258 char string[STRSZ + 1]; 2259 ssize_t leng = 0; 2260 int nbyte; 2261 2262 if (size < 2) { 2263 errno = EINVAL; 2264 return (-1); 2265 } 2266 2267 size--; /* ensure trailing null fits in buffer */ 2268 2269 *buf = '\0'; 2270 string[STRSZ] = '\0'; 2271 2272 for (nbyte = STRSZ; nbyte == STRSZ && leng < size; addr += STRSZ) { 2273 if ((nbyte = P->ops.pop_pread(P, string, STRSZ, addr, 2274 P->data)) <= 0) { 2275 buf[leng] = '\0'; 2276 return (leng ? leng : -1); 2277 } 2278 if ((nbyte = strlen(string)) > 0) { 2279 if (leng + nbyte > size) 2280 nbyte = size - leng; 2281 (void) strncpy(buf + leng, string, nbyte); 2282 leng += nbyte; 2283 } 2284 } 2285 buf[leng] = '\0'; 2286 return (leng); 2287 } 2288 2289 ssize_t 2290 Pwrite(struct ps_prochandle *P, 2291 const void *buf, /* caller's buffer */ 2292 size_t nbyte, /* number of bytes to write */ 2293 uintptr_t address) /* address in process */ 2294 { 2295 return (P->ops.pop_pwrite(P, buf, nbyte, address, P->data)); 2296 } 2297 2298 int 2299 Pclearsig(struct ps_prochandle *P) 2300 { 2301 int ctlfd = (P->agentctlfd >= 0)? P->agentctlfd : P->ctlfd; 2302 long ctl = PCCSIG; 2303 2304 if (write(ctlfd, &ctl, sizeof (ctl)) != sizeof (ctl)) 2305 return (-1); 2306 P->status.pr_lwp.pr_cursig = 0; 2307 return (0); 2308 } 2309 2310 int 2311 Pclearfault(struct ps_prochandle *P) 2312 { 2313 int ctlfd = (P->agentctlfd >= 0)? P->agentctlfd : P->ctlfd; 2314 long ctl = PCCFAULT; 2315 2316 if (write(ctlfd, &ctl, sizeof (ctl)) != sizeof (ctl)) 2317 return (-1); 2318 return (0); 2319 } 2320 2321 /* 2322 * Set a breakpoint trap, return original instruction. 2323 */ 2324 int 2325 Psetbkpt(struct ps_prochandle *P, uintptr_t address, ulong_t *saved) 2326 { 2327 long ctl[1 + sizeof (priovec_t) / sizeof (long) + /* PCREAD */ 2328 1 + sizeof (priovec_t) / sizeof (long)]; /* PCWRITE */ 2329 long *ctlp = ctl; 2330 size_t size; 2331 priovec_t *iovp; 2332 instr_t bpt = BPT; 2333 instr_t old; 2334 2335 if (P->state == PS_DEAD || P->state == PS_UNDEAD || 2336 P->state == PS_IDLE) { 2337 errno = ENOENT; 2338 return (-1); 2339 } 2340 2341 /* fetch the old instruction */ 2342 *ctlp++ = PCREAD; 2343 iovp = (priovec_t *)ctlp; 2344 iovp->pio_base = &old; 2345 iovp->pio_len = sizeof (old); 2346 iovp->pio_offset = address; 2347 ctlp += sizeof (priovec_t) / sizeof (long); 2348 2349 /* write the BPT instruction */ 2350 *ctlp++ = PCWRITE; 2351 iovp = (priovec_t *)ctlp; 2352 iovp->pio_base = &bpt; 2353 iovp->pio_len = sizeof (bpt); 2354 iovp->pio_offset = address; 2355 ctlp += sizeof (priovec_t) / sizeof (long); 2356 2357 size = (char *)ctlp - (char *)ctl; 2358 if (write(P->ctlfd, ctl, size) != size) 2359 return (-1); 2360 2361 /* 2362 * Fail if there was already a breakpoint there from another debugger 2363 * or DTrace's user-level tracing on x86. 2364 */ 2365 if (old == BPT) { 2366 errno = EBUSY; 2367 return (-1); 2368 } 2369 2370 *saved = (ulong_t)old; 2371 return (0); 2372 } 2373 2374 /* 2375 * Restore original instruction where a breakpoint was set. 2376 */ 2377 int 2378 Pdelbkpt(struct ps_prochandle *P, uintptr_t address, ulong_t saved) 2379 { 2380 instr_t old = (instr_t)saved; 2381 instr_t cur; 2382 2383 if (P->state == PS_DEAD || P->state == PS_UNDEAD || 2384 P->state == PS_IDLE) { 2385 errno = ENOENT; 2386 return (-1); 2387 } 2388 2389 /* 2390 * If the breakpoint instruction we had placed has been overwritten 2391 * with a new instruction, then don't try to replace it with the 2392 * old instruction. Doing do can cause problems with self-modifying 2393 * code -- PLTs for example. If the Pread() fails, we assume that we 2394 * should proceed though most likely the Pwrite() will also fail. 2395 */ 2396 if (Pread(P, &cur, sizeof (cur), address) == sizeof (cur) && 2397 cur != BPT) 2398 return (0); 2399 2400 if (Pwrite(P, &old, sizeof (old), address) != sizeof (old)) 2401 return (-1); 2402 2403 return (0); 2404 } 2405 2406 /* 2407 * Common code for Pxecbkpt() and Lxecbkpt(). 2408 * Develop the array of requests that will do the job, then 2409 * write them to the specified control file descriptor. 2410 * Return the non-zero errno if the write fails. 2411 */ 2412 static int 2413 execute_bkpt( 2414 int ctlfd, /* process or LWP control file descriptor */ 2415 const fltset_t *faultset, /* current set of traced faults */ 2416 const sigset_t *sigmask, /* current signal mask */ 2417 uintptr_t address, /* address of breakpint */ 2418 ulong_t saved) /* the saved instruction */ 2419 { 2420 long ctl[ 2421 1 + sizeof (sigset_t) / sizeof (long) + /* PCSHOLD */ 2422 1 + sizeof (fltset_t) / sizeof (long) + /* PCSFAULT */ 2423 1 + sizeof (priovec_t) / sizeof (long) + /* PCWRITE */ 2424 2 + /* PCRUN */ 2425 1 + /* PCWSTOP */ 2426 1 + /* PCCFAULT */ 2427 1 + sizeof (priovec_t) / sizeof (long) + /* PCWRITE */ 2428 1 + sizeof (fltset_t) / sizeof (long) + /* PCSFAULT */ 2429 1 + sizeof (sigset_t) / sizeof (long)]; /* PCSHOLD */ 2430 long *ctlp = ctl; 2431 sigset_t unblock; 2432 size_t size; 2433 ssize_t ssize; 2434 priovec_t *iovp; 2435 sigset_t *holdp; 2436 fltset_t *faultp; 2437 instr_t old = (instr_t)saved; 2438 instr_t bpt = BPT; 2439 int error = 0; 2440 2441 /* block our signals for the duration */ 2442 (void) sigprocmask(SIG_BLOCK, &blockable_sigs, &unblock); 2443 2444 /* hold posted signals */ 2445 *ctlp++ = PCSHOLD; 2446 holdp = (sigset_t *)ctlp; 2447 prfillset(holdp); 2448 prdelset(holdp, SIGKILL); 2449 prdelset(holdp, SIGSTOP); 2450 ctlp += sizeof (sigset_t) / sizeof (long); 2451 2452 /* force tracing of FLTTRACE */ 2453 if (!(prismember(faultset, FLTTRACE))) { 2454 *ctlp++ = PCSFAULT; 2455 faultp = (fltset_t *)ctlp; 2456 *faultp = *faultset; 2457 praddset(faultp, FLTTRACE); 2458 ctlp += sizeof (fltset_t) / sizeof (long); 2459 } 2460 2461 /* restore the old instruction */ 2462 *ctlp++ = PCWRITE; 2463 iovp = (priovec_t *)ctlp; 2464 iovp->pio_base = &old; 2465 iovp->pio_len = sizeof (old); 2466 iovp->pio_offset = address; 2467 ctlp += sizeof (priovec_t) / sizeof (long); 2468 2469 /* clear current signal and fault; set running w/ single-step */ 2470 *ctlp++ = PCRUN; 2471 *ctlp++ = PRCSIG | PRCFAULT | PRSTEP; 2472 2473 /* wait for stop, cancel the fault */ 2474 *ctlp++ = PCWSTOP; 2475 *ctlp++ = PCCFAULT; 2476 2477 /* restore the breakpoint trap */ 2478 *ctlp++ = PCWRITE; 2479 iovp = (priovec_t *)ctlp; 2480 iovp->pio_base = &bpt; 2481 iovp->pio_len = sizeof (bpt); 2482 iovp->pio_offset = address; 2483 ctlp += sizeof (priovec_t) / sizeof (long); 2484 2485 /* restore fault tracing set */ 2486 if (!(prismember(faultset, FLTTRACE))) { 2487 *ctlp++ = PCSFAULT; 2488 *(fltset_t *)ctlp = *faultset; 2489 ctlp += sizeof (fltset_t) / sizeof (long); 2490 } 2491 2492 /* restore the hold mask */ 2493 *ctlp++ = PCSHOLD; 2494 *(sigset_t *)ctlp = *sigmask; 2495 ctlp += sizeof (sigset_t) / sizeof (long); 2496 2497 size = (char *)ctlp - (char *)ctl; 2498 if ((ssize = write(ctlfd, ctl, size)) != size) 2499 error = (ssize == -1)? errno : EINTR; 2500 (void) sigprocmask(SIG_SETMASK, &unblock, NULL); 2501 return (error); 2502 } 2503 2504 /* 2505 * Step over a breakpoint, i.e., execute the instruction that 2506 * really belongs at the breakpoint location (the current %pc) 2507 * and leave the process stopped at the next instruction. 2508 */ 2509 int 2510 Pxecbkpt(struct ps_prochandle *P, ulong_t saved) 2511 { 2512 int ctlfd = (P->agentctlfd >= 0)? P->agentctlfd : P->ctlfd; 2513 int rv, error; 2514 2515 if (P->state != PS_STOP) { 2516 errno = EBUSY; 2517 return (-1); 2518 } 2519 2520 Psync(P); 2521 2522 error = execute_bkpt(ctlfd, 2523 &P->status.pr_flttrace, &P->status.pr_lwp.pr_lwphold, 2524 P->status.pr_lwp.pr_reg[R_PC], saved); 2525 rv = Pstopstatus(P, PCNULL, 0); 2526 2527 if (error != 0) { 2528 if (P->status.pr_lwp.pr_why == PR_JOBCONTROL && 2529 error == EBUSY) { /* jobcontrol stop -- back off */ 2530 P->state = PS_RUN; 2531 return (0); 2532 } 2533 if (error == ENOENT) 2534 return (0); 2535 errno = error; 2536 return (-1); 2537 } 2538 2539 return (rv); 2540 } 2541 2542 /* 2543 * Install the watchpoint described by wp. 2544 */ 2545 int 2546 Psetwapt(struct ps_prochandle *P, const prwatch_t *wp) 2547 { 2548 long ctl[1 + sizeof (prwatch_t) / sizeof (long)]; 2549 prwatch_t *cwp = (prwatch_t *)&ctl[1]; 2550 2551 if (P->state == PS_DEAD || P->state == PS_UNDEAD || 2552 P->state == PS_IDLE) { 2553 errno = ENOENT; 2554 return (-1); 2555 } 2556 2557 ctl[0] = PCWATCH; 2558 cwp->pr_vaddr = wp->pr_vaddr; 2559 cwp->pr_size = wp->pr_size; 2560 cwp->pr_wflags = wp->pr_wflags; 2561 2562 if (write(P->ctlfd, ctl, sizeof (ctl)) != sizeof (ctl)) 2563 return (-1); 2564 2565 return (0); 2566 } 2567 2568 /* 2569 * Remove the watchpoint described by wp. 2570 */ 2571 int 2572 Pdelwapt(struct ps_prochandle *P, const prwatch_t *wp) 2573 { 2574 long ctl[1 + sizeof (prwatch_t) / sizeof (long)]; 2575 prwatch_t *cwp = (prwatch_t *)&ctl[1]; 2576 2577 if (P->state == PS_DEAD || P->state == PS_UNDEAD || 2578 P->state == PS_IDLE) { 2579 errno = ENOENT; 2580 return (-1); 2581 } 2582 2583 ctl[0] = PCWATCH; 2584 cwp->pr_vaddr = wp->pr_vaddr; 2585 cwp->pr_size = wp->pr_size; 2586 cwp->pr_wflags = 0; 2587 2588 if (write(P->ctlfd, ctl, sizeof (ctl)) != sizeof (ctl)) 2589 return (-1); 2590 2591 return (0); 2592 } 2593 2594 /* 2595 * Common code for Pxecwapt() and Lxecwapt(). Develop the array of requests 2596 * that will do the job, then write them to the specified control file 2597 * descriptor. Return the non-zero errno if the write fails. 2598 */ 2599 static int 2600 execute_wapt( 2601 int ctlfd, /* process or LWP control file descriptor */ 2602 const fltset_t *faultset, /* current set of traced faults */ 2603 const sigset_t *sigmask, /* current signal mask */ 2604 const prwatch_t *wp) /* watchpoint descriptor */ 2605 { 2606 long ctl[ 2607 1 + sizeof (sigset_t) / sizeof (long) + /* PCSHOLD */ 2608 1 + sizeof (fltset_t) / sizeof (long) + /* PCSFAULT */ 2609 1 + sizeof (prwatch_t) / sizeof (long) + /* PCWATCH */ 2610 2 + /* PCRUN */ 2611 1 + /* PCWSTOP */ 2612 1 + /* PCCFAULT */ 2613 1 + sizeof (prwatch_t) / sizeof (long) + /* PCWATCH */ 2614 1 + sizeof (fltset_t) / sizeof (long) + /* PCSFAULT */ 2615 1 + sizeof (sigset_t) / sizeof (long)]; /* PCSHOLD */ 2616 2617 long *ctlp = ctl; 2618 int error = 0; 2619 2620 sigset_t unblock; 2621 sigset_t *holdp; 2622 fltset_t *faultp; 2623 prwatch_t *prw; 2624 ssize_t ssize; 2625 size_t size; 2626 2627 (void) sigprocmask(SIG_BLOCK, &blockable_sigs, &unblock); 2628 2629 /* 2630 * Hold all posted signals in the victim process prior to stepping. 2631 */ 2632 *ctlp++ = PCSHOLD; 2633 holdp = (sigset_t *)ctlp; 2634 prfillset(holdp); 2635 prdelset(holdp, SIGKILL); 2636 prdelset(holdp, SIGSTOP); 2637 ctlp += sizeof (sigset_t) / sizeof (long); 2638 2639 /* 2640 * Force tracing of FLTTRACE since we need to single step. 2641 */ 2642 if (!(prismember(faultset, FLTTRACE))) { 2643 *ctlp++ = PCSFAULT; 2644 faultp = (fltset_t *)ctlp; 2645 *faultp = *faultset; 2646 praddset(faultp, FLTTRACE); 2647 ctlp += sizeof (fltset_t) / sizeof (long); 2648 } 2649 2650 /* 2651 * Clear only the current watchpoint by setting pr_wflags to zero. 2652 */ 2653 *ctlp++ = PCWATCH; 2654 prw = (prwatch_t *)ctlp; 2655 prw->pr_vaddr = wp->pr_vaddr; 2656 prw->pr_size = wp->pr_size; 2657 prw->pr_wflags = 0; 2658 ctlp += sizeof (prwatch_t) / sizeof (long); 2659 2660 /* 2661 * Clear the current signal and fault; set running with single-step. 2662 * Then wait for the victim to stop and cancel the FLTTRACE. 2663 */ 2664 *ctlp++ = PCRUN; 2665 *ctlp++ = PRCSIG | PRCFAULT | PRSTEP; 2666 *ctlp++ = PCWSTOP; 2667 *ctlp++ = PCCFAULT; 2668 2669 /* 2670 * Restore the current watchpoint. 2671 */ 2672 *ctlp++ = PCWATCH; 2673 (void) memcpy(ctlp, wp, sizeof (prwatch_t)); 2674 ctlp += sizeof (prwatch_t) / sizeof (long); 2675 2676 /* 2677 * Restore fault tracing set if we modified it. 2678 */ 2679 if (!(prismember(faultset, FLTTRACE))) { 2680 *ctlp++ = PCSFAULT; 2681 *(fltset_t *)ctlp = *faultset; 2682 ctlp += sizeof (fltset_t) / sizeof (long); 2683 } 2684 2685 /* 2686 * Restore the hold mask to the current hold mask (i.e. the one 2687 * before we executed any of the previous operations). 2688 */ 2689 *ctlp++ = PCSHOLD; 2690 *(sigset_t *)ctlp = *sigmask; 2691 ctlp += sizeof (sigset_t) / sizeof (long); 2692 2693 size = (char *)ctlp - (char *)ctl; 2694 if ((ssize = write(ctlfd, ctl, size)) != size) 2695 error = (ssize == -1)? errno : EINTR; 2696 (void) sigprocmask(SIG_SETMASK, &unblock, NULL); 2697 return (error); 2698 } 2699 2700 /* 2701 * Step over a watchpoint, i.e., execute the instruction that was stopped by 2702 * the watchpoint, and then leave the LWP stopped at the next instruction. 2703 */ 2704 int 2705 Pxecwapt(struct ps_prochandle *P, const prwatch_t *wp) 2706 { 2707 int ctlfd = (P->agentctlfd >= 0)? P->agentctlfd : P->ctlfd; 2708 int rv, error; 2709 2710 if (P->state != PS_STOP) { 2711 errno = EBUSY; 2712 return (-1); 2713 } 2714 2715 Psync(P); 2716 error = execute_wapt(ctlfd, 2717 &P->status.pr_flttrace, &P->status.pr_lwp.pr_lwphold, wp); 2718 rv = Pstopstatus(P, PCNULL, 0); 2719 2720 if (error != 0) { 2721 if (P->status.pr_lwp.pr_why == PR_JOBCONTROL && 2722 error == EBUSY) { /* jobcontrol stop -- back off */ 2723 P->state = PS_RUN; 2724 return (0); 2725 } 2726 if (error == ENOENT) 2727 return (0); 2728 errno = error; 2729 return (-1); 2730 } 2731 2732 return (rv); 2733 } 2734 2735 int 2736 Psetflags(struct ps_prochandle *P, long flags) 2737 { 2738 int rc; 2739 long ctl[2]; 2740 2741 ctl[0] = PCSET; 2742 ctl[1] = flags; 2743 2744 if (write(P->ctlfd, ctl, 2*sizeof (long)) != 2*sizeof (long)) { 2745 rc = -1; 2746 } else { 2747 P->status.pr_flags |= flags; 2748 P->status.pr_lwp.pr_flags |= flags; 2749 rc = 0; 2750 } 2751 2752 return (rc); 2753 } 2754 2755 int 2756 Punsetflags(struct ps_prochandle *P, long flags) 2757 { 2758 int rc; 2759 long ctl[2]; 2760 2761 ctl[0] = PCUNSET; 2762 ctl[1] = flags; 2763 2764 if (write(P->ctlfd, ctl, 2*sizeof (long)) != 2*sizeof (long)) { 2765 rc = -1; 2766 } else { 2767 P->status.pr_flags &= ~flags; 2768 P->status.pr_lwp.pr_flags &= ~flags; 2769 rc = 0; 2770 } 2771 2772 return (rc); 2773 } 2774 2775 /* 2776 * Common function to allow clients to manipulate the action to be taken 2777 * on receipt of a signal, receipt of machine fault, entry to a system call, 2778 * or exit from a system call. We make use of our private prset_* functions 2779 * in order to make this code be common. The 'which' parameter identifies 2780 * the code for the event of interest (0 means change the entire set), and 2781 * the 'stop' parameter is a boolean indicating whether the process should 2782 * stop when the event of interest occurs. The previous value is returned 2783 * to the caller; -1 is returned if an error occurred. 2784 */ 2785 static int 2786 Psetaction(struct ps_prochandle *P, void *sp, size_t size, 2787 uint_t flag, int max, int which, int stop) 2788 { 2789 int oldval; 2790 2791 if (which < 0 || which > max) { 2792 errno = EINVAL; 2793 return (-1); 2794 } 2795 2796 if (P->state == PS_DEAD || P->state == PS_UNDEAD || 2797 P->state == PS_IDLE) { 2798 errno = ENOENT; 2799 return (-1); 2800 } 2801 2802 oldval = prset_ismember(sp, size, which) ? TRUE : FALSE; 2803 2804 if (stop) { 2805 if (which == 0) { 2806 prset_fill(sp, size); 2807 P->flags |= flag; 2808 } else if (!oldval) { 2809 prset_add(sp, size, which); 2810 P->flags |= flag; 2811 } 2812 } else { 2813 if (which == 0) { 2814 prset_empty(sp, size); 2815 P->flags |= flag; 2816 } else if (oldval) { 2817 prset_del(sp, size, which); 2818 P->flags |= flag; 2819 } 2820 } 2821 2822 if (P->state == PS_RUN) 2823 Psync(P); 2824 2825 return (oldval); 2826 } 2827 2828 /* 2829 * Set action on specified signal. 2830 */ 2831 int 2832 Psignal(struct ps_prochandle *P, int which, int stop) 2833 { 2834 int oldval; 2835 2836 if (which == SIGKILL && stop != 0) { 2837 errno = EINVAL; 2838 return (-1); 2839 } 2840 2841 oldval = Psetaction(P, &P->status.pr_sigtrace, sizeof (sigset_t), 2842 SETSIG, PRMAXSIG, which, stop); 2843 2844 if (oldval != -1 && which == 0 && stop != 0) 2845 prdelset(&P->status.pr_sigtrace, SIGKILL); 2846 2847 return (oldval); 2848 } 2849 2850 /* 2851 * Set all signal tracing flags. 2852 */ 2853 void 2854 Psetsignal(struct ps_prochandle *P, const sigset_t *set) 2855 { 2856 if (P->state == PS_DEAD || P->state == PS_UNDEAD || 2857 P->state == PS_IDLE) 2858 return; 2859 2860 P->status.pr_sigtrace = *set; 2861 P->flags |= SETSIG; 2862 2863 if (P->state == PS_RUN) 2864 Psync(P); 2865 } 2866 2867 /* 2868 * Set action on specified fault. 2869 */ 2870 int 2871 Pfault(struct ps_prochandle *P, int which, int stop) 2872 { 2873 return (Psetaction(P, &P->status.pr_flttrace, sizeof (fltset_t), 2874 SETFAULT, PRMAXFAULT, which, stop)); 2875 } 2876 2877 /* 2878 * Set all machine fault tracing flags. 2879 */ 2880 void 2881 Psetfault(struct ps_prochandle *P, const fltset_t *set) 2882 { 2883 if (P->state == PS_DEAD || P->state == PS_UNDEAD || 2884 P->state == PS_IDLE) 2885 return; 2886 2887 P->status.pr_flttrace = *set; 2888 P->flags |= SETFAULT; 2889 2890 if (P->state == PS_RUN) 2891 Psync(P); 2892 } 2893 2894 /* 2895 * Set action on specified system call entry. 2896 */ 2897 int 2898 Psysentry(struct ps_prochandle *P, int which, int stop) 2899 { 2900 return (Psetaction(P, &P->status.pr_sysentry, sizeof (sysset_t), 2901 SETENTRY, PRMAXSYS, which, stop)); 2902 } 2903 2904 /* 2905 * Set all system call entry tracing flags. 2906 */ 2907 void 2908 Psetsysentry(struct ps_prochandle *P, const sysset_t *set) 2909 { 2910 if (P->state == PS_DEAD || P->state == PS_UNDEAD || 2911 P->state == PS_IDLE) 2912 return; 2913 2914 P->status.pr_sysentry = *set; 2915 P->flags |= SETENTRY; 2916 2917 if (P->state == PS_RUN) 2918 Psync(P); 2919 } 2920 2921 /* 2922 * Set action on specified system call exit. 2923 */ 2924 int 2925 Psysexit(struct ps_prochandle *P, int which, int stop) 2926 { 2927 return (Psetaction(P, &P->status.pr_sysexit, sizeof (sysset_t), 2928 SETEXIT, PRMAXSYS, which, stop)); 2929 } 2930 2931 /* 2932 * Set all system call exit tracing flags. 2933 */ 2934 void 2935 Psetsysexit(struct ps_prochandle *P, const sysset_t *set) 2936 { 2937 if (P->state == PS_DEAD || P->state == PS_UNDEAD || 2938 P->state == PS_IDLE) 2939 return; 2940 2941 P->status.pr_sysexit = *set; 2942 P->flags |= SETEXIT; 2943 2944 if (P->state == PS_RUN) 2945 Psync(P); 2946 } 2947 2948 /* 2949 * Utility function to read the contents of a file that contains a 2950 * prheader_t at the start (/proc/pid/lstatus or /proc/pid/lpsinfo). 2951 * Returns a malloc()d buffer or NULL on failure. 2952 */ 2953 static prheader_t * 2954 read_lfile(struct ps_prochandle *P, const char *lname) 2955 { 2956 prheader_t *Lhp; 2957 char lpath[PATH_MAX]; 2958 struct stat64 statb; 2959 int fd; 2960 size_t size; 2961 ssize_t rval; 2962 2963 (void) snprintf(lpath, sizeof (lpath), "%s/%d/%s", procfs_path, 2964 (int)P->status.pr_pid, lname); 2965 if ((fd = open(lpath, O_RDONLY)) < 0 || fstat64(fd, &statb) != 0) { 2966 if (fd >= 0) 2967 (void) close(fd); 2968 return (NULL); 2969 } 2970 2971 /* 2972 * 'size' is just the initial guess at the buffer size. 2973 * It will have to grow if the number of lwps increases 2974 * while we are looking at the process. 2975 * 'size' must be larger than the actual file size. 2976 */ 2977 size = statb.st_size + 32; 2978 2979 for (;;) { 2980 if ((Lhp = malloc(size)) == NULL) 2981 break; 2982 if ((rval = pread(fd, Lhp, size, 0)) < 0 || 2983 rval <= sizeof (prheader_t)) { 2984 free(Lhp); 2985 Lhp = NULL; 2986 break; 2987 } 2988 if (rval < size) 2989 break; 2990 /* need a bigger buffer */ 2991 free(Lhp); 2992 size *= 2; 2993 } 2994 2995 (void) close(fd); 2996 return (Lhp); 2997 } 2998 2999 /* 3000 * LWP iteration interface. 3001 */ 3002 int 3003 Plwp_iter(struct ps_prochandle *P, proc_lwp_f *func, void *cd) 3004 { 3005 prheader_t *Lhp; 3006 lwpstatus_t *Lsp; 3007 long nlwp; 3008 int rv; 3009 3010 switch (P->state) { 3011 case PS_RUN: 3012 (void) Pstopstatus(P, PCNULL, 0); 3013 break; 3014 3015 case PS_STOP: 3016 Psync(P); 3017 break; 3018 3019 case PS_IDLE: 3020 errno = ENODATA; 3021 return (-1); 3022 } 3023 3024 /* 3025 * For either live processes or cores, the single LWP case is easy: 3026 * the pstatus_t contains the lwpstatus_t for the only LWP. 3027 */ 3028 if (P->status.pr_nlwp <= 1) 3029 return (func(cd, &P->status.pr_lwp)); 3030 3031 /* 3032 * For the core file multi-LWP case, we just iterate through the 3033 * list of LWP structs we read in from the core file. 3034 */ 3035 if (P->state == PS_DEAD) { 3036 core_info_t *core = P->data; 3037 lwp_info_t *lwp; 3038 3039 for (lwp = list_tail(&core->core_lwp_head); lwp != NULL; 3040 lwp = list_prev(&core->core_lwp_head, lwp)) { 3041 if (lwp->lwp_psinfo.pr_sname != 'Z' && 3042 (rv = func(cd, &lwp->lwp_status)) != 0) 3043 break; 3044 } 3045 3046 return (rv); 3047 } 3048 3049 /* 3050 * For the live process multi-LWP case, we have to work a little 3051 * harder: the /proc/pid/lstatus file has the array of LWP structs. 3052 */ 3053 if ((Lhp = Plstatus(P)) == NULL) 3054 return (-1); 3055 3056 for (nlwp = Lhp->pr_nent, Lsp = (lwpstatus_t *)(uintptr_t)(Lhp + 1); 3057 nlwp > 0; 3058 nlwp--, Lsp = (lwpstatus_t *)((uintptr_t)Lsp + Lhp->pr_entsize)) { 3059 if ((rv = func(cd, Lsp)) != 0) 3060 break; 3061 } 3062 3063 free(Lhp); 3064 return (rv); 3065 } 3066 3067 /* 3068 * Extended LWP iteration interface. 3069 * Iterate over all LWPs, active and zombie. 3070 */ 3071 int 3072 Plwp_iter_all(struct ps_prochandle *P, proc_lwp_all_f *func, void *cd) 3073 { 3074 prheader_t *Lhp = NULL; 3075 lwpstatus_t *Lsp; 3076 lwpstatus_t *sp; 3077 prheader_t *Lphp = NULL; 3078 lwpsinfo_t *Lpsp; 3079 long nstat; 3080 long ninfo; 3081 int rv; 3082 3083 retry: 3084 if (Lhp != NULL) 3085 free(Lhp); 3086 if (Lphp != NULL) 3087 free(Lphp); 3088 if (P->state == PS_RUN) 3089 (void) Pstopstatus(P, PCNULL, 0); 3090 (void) Ppsinfo(P); 3091 3092 if (P->state == PS_STOP) 3093 Psync(P); 3094 3095 /* 3096 * For either live processes or cores, the single LWP case is easy: 3097 * the pstatus_t contains the lwpstatus_t for the only LWP and 3098 * the psinfo_t contains the lwpsinfo_t for the only LWP. 3099 */ 3100 if (P->status.pr_nlwp + P->status.pr_nzomb <= 1) 3101 return (func(cd, &P->status.pr_lwp, &P->psinfo.pr_lwp)); 3102 3103 /* 3104 * For the core file multi-LWP case, we just iterate through the 3105 * list of LWP structs we read in from the core file. 3106 */ 3107 if (P->state == PS_DEAD) { 3108 core_info_t *core = P->data; 3109 lwp_info_t *lwp; 3110 3111 for (lwp = list_tail(&core->core_lwp_head); lwp != NULL; 3112 lwp = list_prev(&core->core_lwp_head, lwp)) { 3113 sp = (lwp->lwp_psinfo.pr_sname == 'Z')? NULL : 3114 &lwp->lwp_status; 3115 if ((rv = func(cd, sp, &lwp->lwp_psinfo)) != 0) 3116 break; 3117 } 3118 3119 return (rv); 3120 } 3121 3122 /* 3123 * For all other cases retrieve the array of lwpstatus_t's and 3124 * lwpsinfo_t's. 3125 */ 3126 if ((Lhp = Plstatus(P)) == NULL) 3127 return (-1); 3128 if ((Lphp = Plpsinfo(P)) == NULL) { 3129 free(Lhp); 3130 return (-1); 3131 } 3132 3133 /* 3134 * If we are looking at a running process, or one we do not control, 3135 * the active and zombie lwps in the process may have changed since 3136 * we read the process status structure. If so, just start over. 3137 */ 3138 if (Lhp->pr_nent != P->status.pr_nlwp || 3139 Lphp->pr_nent != P->status.pr_nlwp + P->status.pr_nzomb) 3140 goto retry; 3141 3142 /* 3143 * To be perfectly safe, prescan the two arrays, checking consistency. 3144 * We rely on /proc giving us lwpstatus_t's and lwpsinfo_t's in the 3145 * same order (the lwp directory order) in their respective files. 3146 * We also rely on there being (possibly) more lwpsinfo_t's than 3147 * lwpstatus_t's (the extra lwpsinfo_t's are for zombie lwps). 3148 */ 3149 Lsp = (lwpstatus_t *)(uintptr_t)(Lhp + 1); 3150 Lpsp = (lwpsinfo_t *)(uintptr_t)(Lphp + 1); 3151 nstat = Lhp->pr_nent; 3152 for (ninfo = Lphp->pr_nent; ninfo != 0; ninfo--) { 3153 if (Lpsp->pr_sname != 'Z') { 3154 /* 3155 * Not a zombie lwp; check for matching lwpids. 3156 */ 3157 if (nstat == 0 || Lsp->pr_lwpid != Lpsp->pr_lwpid) 3158 goto retry; 3159 Lsp = (lwpstatus_t *)((uintptr_t)Lsp + Lhp->pr_entsize); 3160 nstat--; 3161 } 3162 Lpsp = (lwpsinfo_t *)((uintptr_t)Lpsp + Lphp->pr_entsize); 3163 } 3164 if (nstat != 0) 3165 goto retry; 3166 3167 /* 3168 * Rescan, this time for real. 3169 */ 3170 Lsp = (lwpstatus_t *)(uintptr_t)(Lhp + 1); 3171 Lpsp = (lwpsinfo_t *)(uintptr_t)(Lphp + 1); 3172 for (ninfo = Lphp->pr_nent; ninfo != 0; ninfo--) { 3173 if (Lpsp->pr_sname != 'Z') { 3174 sp = Lsp; 3175 Lsp = (lwpstatus_t *)((uintptr_t)Lsp + Lhp->pr_entsize); 3176 } else { 3177 sp = NULL; 3178 } 3179 if ((rv = func(cd, sp, Lpsp)) != 0) 3180 break; 3181 Lpsp = (lwpsinfo_t *)((uintptr_t)Lpsp + Lphp->pr_entsize); 3182 } 3183 3184 free(Lhp); 3185 free(Lphp); 3186 return (rv); 3187 } 3188 3189 core_content_t 3190 Pcontent(struct ps_prochandle *P) 3191 { 3192 core_info_t *core = P->data; 3193 3194 if (P->state == PS_DEAD) 3195 return (core->core_content); 3196 if (P->state == PS_IDLE) 3197 return (CC_CONTENT_TEXT | CC_CONTENT_DATA | CC_CONTENT_CTF); 3198 3199 return (CC_CONTENT_ALL); 3200 } 3201 3202 /* 3203 * ================================================================= 3204 * The remainder of the functions in this file are for the 3205 * control of individual LWPs in the controlled process. 3206 * ================================================================= 3207 */ 3208 3209 /* 3210 * Find an entry in the process hash table for the specified lwpid. 3211 * The entry will either point to an existing struct ps_lwphandle 3212 * or it will point to an empty slot for a new struct ps_lwphandle. 3213 */ 3214 static struct ps_lwphandle ** 3215 Lfind_slot(struct ps_prochandle *P, lwpid_t lwpid) 3216 { 3217 struct ps_lwphandle **Lp; 3218 struct ps_lwphandle *L; 3219 3220 for (Lp = &P->hashtab[lwpid % (HASHSIZE - 1)]; 3221 (L = *Lp) != NULL; Lp = &L->lwp_hash) 3222 if (L->lwp_id == lwpid) 3223 break; 3224 return (Lp); 3225 } 3226 3227 /* 3228 * A wrapper around Lfind_slot() that is suitable for the rest of the internal 3229 * consumers who don't care about a slot, merely existence. 3230 */ 3231 struct ps_lwphandle * 3232 Lfind(struct ps_prochandle *P, lwpid_t lwpid) 3233 { 3234 if (P->hashtab == NULL) { 3235 return (NULL); 3236 } 3237 3238 return (*Lfind_slot(P, lwpid)); 3239 } 3240 3241 /* 3242 * Grab an LWP contained within the controlled process. 3243 * Return an opaque pointer to its LWP control structure. 3244 * perr: pointer to error return code. 3245 */ 3246 struct ps_lwphandle * 3247 Lgrab(struct ps_prochandle *P, lwpid_t lwpid, int *perr) 3248 { 3249 struct ps_lwphandle **Lp; 3250 struct ps_lwphandle *L; 3251 int fd; 3252 char procname[PATH_MAX]; 3253 char *fname; 3254 int rc = 0; 3255 3256 (void) mutex_lock(&P->proc_lock); 3257 3258 if (P->state == PS_UNDEAD || P->state == PS_IDLE) 3259 rc = G_NOPROC; 3260 else if (P->hashtab == NULL && 3261 (P->hashtab = calloc(HASHSIZE, sizeof (struct ps_lwphandle *))) 3262 == NULL) 3263 rc = G_STRANGE; 3264 else if (*(Lp = Lfind_slot(P, lwpid)) != NULL) 3265 rc = G_BUSY; 3266 else if ((L = malloc(sizeof (struct ps_lwphandle))) == NULL) 3267 rc = G_STRANGE; 3268 if (rc) { 3269 *perr = rc; 3270 (void) mutex_unlock(&P->proc_lock); 3271 return (NULL); 3272 } 3273 3274 (void) memset(L, 0, sizeof (*L)); 3275 L->lwp_ctlfd = -1; 3276 L->lwp_statfd = -1; 3277 L->lwp_proc = P; 3278 L->lwp_id = lwpid; 3279 *Lp = L; /* insert into the hash table */ 3280 3281 if (P->state == PS_DEAD) { /* core file */ 3282 if (getlwpstatus(P, lwpid, &L->lwp_status) == -1) { 3283 rc = G_NOPROC; 3284 goto err; 3285 } 3286 L->lwp_state = PS_DEAD; 3287 *perr = 0; 3288 (void) mutex_unlock(&P->proc_lock); 3289 return (L); 3290 } 3291 3292 /* 3293 * Open the /proc/<pid>/lwp/<lwpid> files 3294 */ 3295 (void) snprintf(procname, sizeof (procname), "%s/%d/lwp/%d/", 3296 procfs_path, (int)P->pid, (int)lwpid); 3297 fname = procname + strlen(procname); 3298 (void) set_minfd(); 3299 3300 (void) strcpy(fname, "lwpstatus"); 3301 if ((fd = open(procname, O_RDONLY)) < 0 || 3302 (fd = dupfd(fd, 0)) < 0) { 3303 switch (errno) { 3304 case ENOENT: 3305 rc = G_NOPROC; 3306 break; 3307 default: 3308 Pdprintf("Lgrab: failed to open %s: %s\n", 3309 procname, strerror(errno)); 3310 rc = G_STRANGE; 3311 break; 3312 } 3313 goto err; 3314 } 3315 L->lwp_statfd = fd; 3316 3317 if (pread(fd, &L->lwp_status, sizeof (L->lwp_status), (off_t)0) < 0) { 3318 switch (errno) { 3319 case ENOENT: 3320 rc = G_NOPROC; 3321 break; 3322 default: 3323 Pdprintf("Lgrab: failed to read %s: %s\n", 3324 procname, strerror(errno)); 3325 rc = G_STRANGE; 3326 break; 3327 } 3328 goto err; 3329 } 3330 3331 (void) strcpy(fname, "lwpctl"); 3332 if ((fd = open(procname, O_WRONLY)) < 0 || 3333 (fd = dupfd(fd, 0)) < 0) { 3334 switch (errno) { 3335 case ENOENT: 3336 rc = G_NOPROC; 3337 break; 3338 default: 3339 Pdprintf("Lgrab: failed to open %s: %s\n", 3340 procname, strerror(errno)); 3341 rc = G_STRANGE; 3342 break; 3343 } 3344 goto err; 3345 } 3346 L->lwp_ctlfd = fd; 3347 3348 L->lwp_state = 3349 ((L->lwp_status.pr_flags & (PR_STOPPED|PR_ISTOP)) 3350 == (PR_STOPPED|PR_ISTOP))? 3351 PS_STOP : PS_RUN; 3352 3353 *perr = 0; 3354 (void) mutex_unlock(&P->proc_lock); 3355 return (L); 3356 3357 err: 3358 Lfree_internal(P, L); 3359 *perr = rc; 3360 (void) mutex_unlock(&P->proc_lock); 3361 return (NULL); 3362 } 3363 3364 /* 3365 * Return a printable string corresponding to an Lgrab() error return. 3366 */ 3367 const char * 3368 Lgrab_error(int error) 3369 { 3370 const char *str; 3371 3372 switch (error) { 3373 case G_NOPROC: 3374 str = "no such LWP"; 3375 break; 3376 case G_BUSY: 3377 str = "LWP already grabbed"; 3378 break; 3379 case G_STRANGE: 3380 str = "unanticipated system error"; 3381 break; 3382 default: 3383 str = "unknown error"; 3384 break; 3385 } 3386 3387 return (str); 3388 } 3389 3390 /* 3391 * Free an LWP control structure. 3392 */ 3393 void 3394 Lfree(struct ps_lwphandle *L) 3395 { 3396 struct ps_prochandle *P = L->lwp_proc; 3397 3398 (void) mutex_lock(&P->proc_lock); 3399 Lfree_internal(P, L); 3400 (void) mutex_unlock(&P->proc_lock); 3401 } 3402 3403 static void 3404 Lfree_internal(struct ps_prochandle *P, struct ps_lwphandle *L) 3405 { 3406 *Lfind_slot(P, L->lwp_id) = L->lwp_hash; /* delete from hash table */ 3407 if (L->lwp_ctlfd >= 0) 3408 (void) close(L->lwp_ctlfd); 3409 if (L->lwp_statfd >= 0) 3410 (void) close(L->lwp_statfd); 3411 3412 /* clear out the structure as a precaution against reuse */ 3413 (void) memset(L, 0, sizeof (*L)); 3414 L->lwp_ctlfd = -1; 3415 L->lwp_statfd = -1; 3416 3417 free(L); 3418 } 3419 3420 /* 3421 * Return the state of the process, one of the PS_* values. 3422 */ 3423 int 3424 Lstate(struct ps_lwphandle *L) 3425 { 3426 return (L->lwp_state); 3427 } 3428 3429 /* 3430 * Return the open control file descriptor for the LWP. 3431 * Clients must not close this file descriptor, nor use it 3432 * after the LWP is freed. 3433 */ 3434 int 3435 Lctlfd(struct ps_lwphandle *L) 3436 { 3437 return (L->lwp_ctlfd); 3438 } 3439 3440 /* 3441 * Return a pointer to the LWP lwpsinfo structure. 3442 * Clients should not hold on to this pointer indefinitely. 3443 * It will become invalid on Lfree(). 3444 */ 3445 const lwpsinfo_t * 3446 Lpsinfo(struct ps_lwphandle *L) 3447 { 3448 if (Plwp_getpsinfo(L->lwp_proc, L->lwp_id, &L->lwp_psinfo) == -1) 3449 return (NULL); 3450 3451 return (&L->lwp_psinfo); 3452 } 3453 3454 /* 3455 * Return a pointer to the LWP status structure. 3456 * Clients should not hold on to this pointer indefinitely. 3457 * It will become invalid on Lfree(). 3458 */ 3459 const lwpstatus_t * 3460 Lstatus(struct ps_lwphandle *L) 3461 { 3462 return (&L->lwp_status); 3463 } 3464 3465 /* 3466 * Given an LWP handle, return the process handle. 3467 */ 3468 struct ps_prochandle * 3469 Lprochandle(struct ps_lwphandle *L) 3470 { 3471 return (L->lwp_proc); 3472 } 3473 3474 /* 3475 * Ensure that all cached state is written to the LWP. 3476 * The cached state is the LWP's signal mask and registers. 3477 */ 3478 void 3479 Lsync(struct ps_lwphandle *L) 3480 { 3481 int ctlfd = L->lwp_ctlfd; 3482 long cmd[2]; 3483 iovec_t iov[4]; 3484 int n = 0; 3485 3486 if (L->lwp_flags & SETHOLD) { 3487 cmd[0] = PCSHOLD; 3488 iov[n].iov_base = (caddr_t)&cmd[0]; 3489 iov[n++].iov_len = sizeof (long); 3490 iov[n].iov_base = (caddr_t)&L->lwp_status.pr_lwphold; 3491 iov[n++].iov_len = sizeof (L->lwp_status.pr_lwphold); 3492 } 3493 if (L->lwp_flags & SETREGS) { 3494 cmd[1] = PCSREG; 3495 iov[n].iov_base = (caddr_t)&cmd[1]; 3496 iov[n++].iov_len = sizeof (long); 3497 iov[n].iov_base = (caddr_t)&L->lwp_status.pr_reg[0]; 3498 iov[n++].iov_len = sizeof (L->lwp_status.pr_reg); 3499 } 3500 3501 if (n == 0 || writev(ctlfd, iov, n) < 0) 3502 return; /* nothing to do or write failed */ 3503 3504 L->lwp_flags &= ~(SETHOLD|SETREGS); 3505 } 3506 3507 /* 3508 * Wait for the specified LWP to stop or terminate. 3509 * Or, just get the current status (PCNULL). 3510 * Or, direct it to stop and get the current status (PCDSTOP). 3511 */ 3512 int 3513 Lstopstatus(struct ps_lwphandle *L, 3514 long request, /* PCNULL, PCDSTOP, PCSTOP, PCWSTOP */ 3515 uint_t msec) /* if non-zero, timeout in milliseconds */ 3516 { 3517 int ctlfd = L->lwp_ctlfd; 3518 long ctl[3]; 3519 ssize_t rc; 3520 int err; 3521 3522 switch (L->lwp_state) { 3523 case PS_RUN: 3524 break; 3525 case PS_STOP: 3526 if (request != PCNULL && request != PCDSTOP) 3527 return (0); 3528 break; 3529 case PS_LOST: 3530 if (request != PCNULL) { 3531 errno = EAGAIN; 3532 return (-1); 3533 } 3534 break; 3535 case PS_UNDEAD: 3536 case PS_DEAD: 3537 if (request != PCNULL) { 3538 errno = ENOENT; 3539 return (-1); 3540 } 3541 break; 3542 default: /* corrupted state */ 3543 Pdprintf("Lstopstatus: corrupted state: %d\n", L->lwp_state); 3544 errno = EINVAL; 3545 return (-1); 3546 } 3547 3548 ctl[0] = PCDSTOP; 3549 ctl[1] = PCTWSTOP; 3550 ctl[2] = (long)msec; 3551 rc = 0; 3552 switch (request) { 3553 case PCSTOP: 3554 rc = write(ctlfd, &ctl[0], 3*sizeof (long)); 3555 break; 3556 case PCWSTOP: 3557 rc = write(ctlfd, &ctl[1], 2*sizeof (long)); 3558 break; 3559 case PCDSTOP: 3560 rc = write(ctlfd, &ctl[0], 1*sizeof (long)); 3561 break; 3562 case PCNULL: 3563 if (L->lwp_state == PS_DEAD) 3564 return (0); /* Nothing else to do for cores */ 3565 break; 3566 default: /* programming error */ 3567 errno = EINVAL; 3568 return (-1); 3569 } 3570 err = (rc < 0)? errno : 0; 3571 Lsync(L); 3572 3573 if (pread(L->lwp_statfd, &L->lwp_status, 3574 sizeof (L->lwp_status), (off_t)0) < 0) 3575 err = errno; 3576 3577 if (err) { 3578 switch (err) { 3579 case EINTR: /* user typed ctl-C */ 3580 case ERESTART: 3581 Pdprintf("Lstopstatus: EINTR\n"); 3582 break; 3583 case EAGAIN: /* we lost control of the the process */ 3584 Pdprintf("Lstopstatus: EAGAIN\n"); 3585 L->lwp_state = PS_LOST; 3586 errno = err; 3587 return (-1); 3588 default: 3589 if (_libproc_debug) { 3590 const char *errstr; 3591 3592 switch (request) { 3593 case PCNULL: 3594 errstr = "Lstopstatus PCNULL"; break; 3595 case PCSTOP: 3596 errstr = "Lstopstatus PCSTOP"; break; 3597 case PCDSTOP: 3598 errstr = "Lstopstatus PCDSTOP"; break; 3599 case PCWSTOP: 3600 errstr = "Lstopstatus PCWSTOP"; break; 3601 default: 3602 errstr = "Lstopstatus PC???"; break; 3603 } 3604 Pdprintf("%s: %s\n", errstr, strerror(err)); 3605 } 3606 L->lwp_state = PS_UNDEAD; 3607 errno = err; 3608 return (-1); 3609 } 3610 } 3611 3612 if ((L->lwp_status.pr_flags & (PR_STOPPED|PR_ISTOP)) 3613 != (PR_STOPPED|PR_ISTOP)) { 3614 L->lwp_state = PS_RUN; 3615 if (request == PCNULL || request == PCDSTOP || msec != 0) 3616 return (0); 3617 Pdprintf("Lstopstatus: LWP is not stopped\n"); 3618 errno = EPROTO; 3619 return (-1); 3620 } 3621 3622 L->lwp_state = PS_STOP; 3623 3624 if (_libproc_debug) /* debugging */ 3625 prldump("Lstopstatus", &L->lwp_status); 3626 3627 switch (L->lwp_status.pr_why) { 3628 case PR_SYSENTRY: 3629 case PR_SYSEXIT: 3630 case PR_REQUESTED: 3631 case PR_SIGNALLED: 3632 case PR_FAULTED: 3633 case PR_JOBCONTROL: 3634 case PR_SUSPENDED: 3635 break; 3636 default: 3637 errno = EPROTO; 3638 return (-1); 3639 } 3640 3641 return (0); 3642 } 3643 3644 /* 3645 * Wait for the LWP to stop for any reason. 3646 */ 3647 int 3648 Lwait(struct ps_lwphandle *L, uint_t msec) 3649 { 3650 return (Lstopstatus(L, PCWSTOP, msec)); 3651 } 3652 3653 /* 3654 * Direct the LWP to stop; wait for it to stop. 3655 */ 3656 int 3657 Lstop(struct ps_lwphandle *L, uint_t msec) 3658 { 3659 return (Lstopstatus(L, PCSTOP, msec)); 3660 } 3661 3662 /* 3663 * Direct the LWP to stop; don't wait. 3664 */ 3665 int 3666 Ldstop(struct ps_lwphandle *L) 3667 { 3668 return (Lstopstatus(L, PCDSTOP, 0)); 3669 } 3670 3671 /* 3672 * Get the value of one register from stopped LWP. 3673 */ 3674 int 3675 Lgetareg(struct ps_lwphandle *L, int regno, prgreg_t *preg) 3676 { 3677 if (regno < 0 || regno >= NPRGREG) { 3678 errno = EINVAL; 3679 return (-1); 3680 } 3681 3682 if (L->lwp_state != PS_STOP) { 3683 errno = EBUSY; 3684 return (-1); 3685 } 3686 3687 *preg = L->lwp_status.pr_reg[regno]; 3688 return (0); 3689 } 3690 3691 /* 3692 * Put value of one register into stopped LWP. 3693 */ 3694 int 3695 Lputareg(struct ps_lwphandle *L, int regno, prgreg_t reg) 3696 { 3697 if (regno < 0 || regno >= NPRGREG) { 3698 errno = EINVAL; 3699 return (-1); 3700 } 3701 3702 if (L->lwp_state != PS_STOP) { 3703 errno = EBUSY; 3704 return (-1); 3705 } 3706 3707 L->lwp_status.pr_reg[regno] = reg; 3708 L->lwp_flags |= SETREGS; /* set registers before continuing */ 3709 return (0); 3710 } 3711 3712 int 3713 Lsetrun(struct ps_lwphandle *L, 3714 int sig, /* signal to pass to LWP */ 3715 int flags) /* PRSTEP|PRSABORT|PRSTOP|PRCSIG|PRCFAULT */ 3716 { 3717 int ctlfd = L->lwp_ctlfd; 3718 int sbits = (PR_DSTOP | PR_ISTOP | PR_ASLEEP); 3719 3720 long ctl[1 + /* PCCFAULT */ 3721 1 + sizeof (siginfo_t)/sizeof (long) + /* PCSSIG/PCCSIG */ 3722 2 ]; /* PCRUN */ 3723 3724 long *ctlp = ctl; 3725 size_t size; 3726 3727 if (L->lwp_state != PS_STOP && 3728 (L->lwp_status.pr_flags & sbits) == 0) { 3729 errno = EBUSY; 3730 return (-1); 3731 } 3732 3733 Lsync(L); /* flush registers */ 3734 3735 if (flags & PRCFAULT) { /* clear current fault */ 3736 *ctlp++ = PCCFAULT; 3737 flags &= ~PRCFAULT; 3738 } 3739 3740 if (flags & PRCSIG) { /* clear current signal */ 3741 *ctlp++ = PCCSIG; 3742 flags &= ~PRCSIG; 3743 } else if (sig && sig != L->lwp_status.pr_cursig) { 3744 /* make current signal */ 3745 siginfo_t *infop; 3746 3747 *ctlp++ = PCSSIG; 3748 infop = (siginfo_t *)ctlp; 3749 (void) memset(infop, 0, sizeof (*infop)); 3750 infop->si_signo = sig; 3751 ctlp += sizeof (siginfo_t) / sizeof (long); 3752 } 3753 3754 *ctlp++ = PCRUN; 3755 *ctlp++ = flags; 3756 size = (char *)ctlp - (char *)ctl; 3757 3758 L->lwp_proc->info_valid = 0; /* will need to update map and file info */ 3759 L->lwp_proc->state = PS_RUN; 3760 L->lwp_state = PS_RUN; 3761 3762 if (write(ctlfd, ctl, size) != size) { 3763 /* Pretend that a job-stopped LWP is running */ 3764 if (errno != EBUSY || L->lwp_status.pr_why != PR_JOBCONTROL) 3765 return (Lstopstatus(L, PCNULL, 0)); 3766 } 3767 3768 return (0); 3769 } 3770 3771 int 3772 Lclearsig(struct ps_lwphandle *L) 3773 { 3774 int ctlfd = L->lwp_ctlfd; 3775 long ctl = PCCSIG; 3776 3777 if (write(ctlfd, &ctl, sizeof (ctl)) != sizeof (ctl)) 3778 return (-1); 3779 L->lwp_status.pr_cursig = 0; 3780 return (0); 3781 } 3782 3783 int 3784 Lclearfault(struct ps_lwphandle *L) 3785 { 3786 int ctlfd = L->lwp_ctlfd; 3787 long ctl = PCCFAULT; 3788 3789 if (write(ctlfd, &ctl, sizeof (ctl)) != sizeof (ctl)) 3790 return (-1); 3791 return (0); 3792 } 3793 3794 /* 3795 * Step over a breakpoint, i.e., execute the instruction that 3796 * really belongs at the breakpoint location (the current %pc) 3797 * and leave the LWP stopped at the next instruction. 3798 */ 3799 int 3800 Lxecbkpt(struct ps_lwphandle *L, ulong_t saved) 3801 { 3802 struct ps_prochandle *P = L->lwp_proc; 3803 int rv, error; 3804 3805 if (L->lwp_state != PS_STOP) { 3806 errno = EBUSY; 3807 return (-1); 3808 } 3809 3810 Lsync(L); 3811 error = execute_bkpt(L->lwp_ctlfd, 3812 &P->status.pr_flttrace, &L->lwp_status.pr_lwphold, 3813 L->lwp_status.pr_reg[R_PC], saved); 3814 rv = Lstopstatus(L, PCNULL, 0); 3815 3816 if (error != 0) { 3817 if (L->lwp_status.pr_why == PR_JOBCONTROL && 3818 error == EBUSY) { /* jobcontrol stop -- back off */ 3819 L->lwp_state = PS_RUN; 3820 return (0); 3821 } 3822 if (error == ENOENT) 3823 return (0); 3824 errno = error; 3825 return (-1); 3826 } 3827 3828 return (rv); 3829 } 3830 3831 /* 3832 * Step over a watchpoint, i.e., execute the instruction that was stopped by 3833 * the watchpoint, and then leave the LWP stopped at the next instruction. 3834 */ 3835 int 3836 Lxecwapt(struct ps_lwphandle *L, const prwatch_t *wp) 3837 { 3838 struct ps_prochandle *P = L->lwp_proc; 3839 int rv, error; 3840 3841 if (L->lwp_state != PS_STOP) { 3842 errno = EBUSY; 3843 return (-1); 3844 } 3845 3846 Lsync(L); 3847 error = execute_wapt(L->lwp_ctlfd, 3848 &P->status.pr_flttrace, &L->lwp_status.pr_lwphold, wp); 3849 rv = Lstopstatus(L, PCNULL, 0); 3850 3851 if (error != 0) { 3852 if (L->lwp_status.pr_why == PR_JOBCONTROL && 3853 error == EBUSY) { /* jobcontrol stop -- back off */ 3854 L->lwp_state = PS_RUN; 3855 return (0); 3856 } 3857 if (error == ENOENT) 3858 return (0); 3859 errno = error; 3860 return (-1); 3861 } 3862 3863 return (rv); 3864 } 3865 3866 int 3867 Lstack(struct ps_lwphandle *L, stack_t *stkp) 3868 { 3869 struct ps_prochandle *P = L->lwp_proc; 3870 uintptr_t addr = L->lwp_status.pr_ustack; 3871 3872 if (P->status.pr_dmodel == PR_MODEL_NATIVE) { 3873 if (Pread(P, stkp, sizeof (*stkp), addr) != sizeof (*stkp)) 3874 return (-1); 3875 #ifdef _LP64 3876 } else { 3877 stack32_t stk32; 3878 3879 if (Pread(P, &stk32, sizeof (stk32), addr) != sizeof (stk32)) 3880 return (-1); 3881 3882 stack_32_to_n(&stk32, stkp); 3883 #endif 3884 } 3885 3886 return (0); 3887 } 3888 3889 int 3890 Lmain_stack(struct ps_lwphandle *L, stack_t *stkp) 3891 { 3892 struct ps_prochandle *P = L->lwp_proc; 3893 3894 if (Lstack(L, stkp) != 0) 3895 return (-1); 3896 3897 /* 3898 * If the SS_ONSTACK flag is set then this LWP is operating on the 3899 * alternate signal stack. We can recover the original stack from 3900 * pr_oldcontext. 3901 */ 3902 if (!(stkp->ss_flags & SS_ONSTACK)) 3903 return (0); 3904 3905 if (P->status.pr_dmodel == PR_MODEL_NATIVE) { 3906 ucontext_t *ctxp = (void *)L->lwp_status.pr_oldcontext; 3907 3908 if (Pread(P, stkp, sizeof (*stkp), 3909 (uintptr_t)&ctxp->uc_stack) != sizeof (*stkp)) 3910 return (-1); 3911 #ifdef _LP64 3912 } else { 3913 ucontext32_t *ctxp = (void *)L->lwp_status.pr_oldcontext; 3914 stack32_t stk32; 3915 3916 if (Pread(P, &stk32, sizeof (stk32), 3917 (uintptr_t)&ctxp->uc_stack) != sizeof (stk32)) 3918 return (-1); 3919 3920 stack_32_to_n(&stk32, stkp); 3921 #endif 3922 } 3923 3924 return (0); 3925 } 3926 3927 int 3928 Lalt_stack(struct ps_lwphandle *L, stack_t *stkp) 3929 { 3930 if (L->lwp_status.pr_altstack.ss_flags & SS_DISABLE) { 3931 errno = ENODATA; 3932 return (-1); 3933 } 3934 3935 *stkp = L->lwp_status.pr_altstack; 3936 3937 return (0); 3938 } 3939 3940 /* 3941 * Add a mapping to the given proc handle. Resizes the array as appropriate and 3942 * manages reference counts on the given file_info_t. 3943 * 3944 * The 'map_relocate' member is used to tell Psort_mappings() that the 3945 * associated file_map pointer needs to be relocated after the mappings have 3946 * been sorted. It is only set for the first mapping, and has no meaning 3947 * outside these two functions. 3948 */ 3949 int 3950 Padd_mapping(struct ps_prochandle *P, off64_t off, file_info_t *fp, 3951 prmap_t *pmap) 3952 { 3953 map_info_t *mp; 3954 3955 if (P->map_count == P->map_alloc) { 3956 size_t next = P->map_alloc ? P->map_alloc * 2 : 16; 3957 3958 if ((P->mappings = realloc(P->mappings, 3959 next * sizeof (map_info_t))) == NULL) 3960 return (-1); 3961 3962 P->map_alloc = next; 3963 } 3964 3965 mp = &P->mappings[P->map_count++]; 3966 3967 mp->map_offset = off; 3968 mp->map_pmap = *pmap; 3969 mp->map_relocate = 0; 3970 if ((mp->map_file = fp) != NULL) { 3971 if (fp->file_map == NULL) { 3972 fp->file_map = mp; 3973 mp->map_relocate = 1; 3974 } 3975 fp->file_ref++; 3976 } 3977 3978 return (0); 3979 } 3980 3981 static int 3982 map_sort(const void *a, const void *b) 3983 { 3984 const map_info_t *ap = a, *bp = b; 3985 3986 if (ap->map_pmap.pr_vaddr < bp->map_pmap.pr_vaddr) 3987 return (-1); 3988 else if (ap->map_pmap.pr_vaddr > bp->map_pmap.pr_vaddr) 3989 return (1); 3990 else 3991 return (0); 3992 } 3993 3994 /* 3995 * Sort the current set of mappings. Should be called during target 3996 * initialization after all calls to Padd_mapping() have been made. 3997 */ 3998 void 3999 Psort_mappings(struct ps_prochandle *P) 4000 { 4001 int i; 4002 map_info_t *mp; 4003 4004 qsort(P->mappings, P->map_count, sizeof (map_info_t), map_sort); 4005 4006 /* 4007 * Update all the file_map pointers to refer to the new locations. 4008 */ 4009 for (i = 0; i < P->map_count; i++) { 4010 mp = &P->mappings[i]; 4011 if (mp->map_relocate) 4012 mp->map_file->file_map = mp; 4013 mp->map_relocate = 0; 4014 } 4015 } 4016 4017 struct ps_prochandle * 4018 Pgrab_ops(pid_t pid, void *data, const ps_ops_t *ops, int flags) 4019 { 4020 struct ps_prochandle *P; 4021 4022 if ((P = calloc(1, sizeof (*P))) == NULL) { 4023 return (NULL); 4024 } 4025 4026 Pinit_ops(&P->ops, ops); 4027 (void) mutex_init(&P->proc_lock, USYNC_THREAD, NULL); 4028 P->pid = pid; 4029 P->state = PS_STOP; 4030 P->asfd = -1; 4031 P->ctlfd = -1; 4032 P->statfd = -1; 4033 P->agentctlfd = -1; 4034 P->agentstatfd = -1; 4035 Pinitsym(P); 4036 Pinitfd(P); 4037 P->data = data; 4038 Pread_status(P); 4039 4040 if (flags & PGRAB_INCORE) { 4041 P->flags |= INCORE; 4042 } 4043 4044 return (P); 4045 } 4046