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