10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52712Snn35248  * Common Development and Distribution License (the "License").
62712Snn35248  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211222Smws 
220Sstevel@tonic-gate /*
23*10201SEdward.Pilatowicz@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
255272Sjhaslam  *
265272Sjhaslam  * Portions Copyright 2007 Chad Mynhier
270Sstevel@tonic-gate  */
280Sstevel@tonic-gate 
29*10201SEdward.Pilatowicz@Sun.COM #include <assert.h>
300Sstevel@tonic-gate #include <stdio.h>
310Sstevel@tonic-gate #include <stdlib.h>
320Sstevel@tonic-gate #include <unistd.h>
330Sstevel@tonic-gate #include <ctype.h>
340Sstevel@tonic-gate #include <fcntl.h>
350Sstevel@tonic-gate #include <string.h>
362712Snn35248 #include <strings.h>
370Sstevel@tonic-gate #include <memory.h>
380Sstevel@tonic-gate #include <errno.h>
390Sstevel@tonic-gate #include <dirent.h>
400Sstevel@tonic-gate #include <limits.h>
410Sstevel@tonic-gate #include <signal.h>
423864Sraf #include <atomic.h>
430Sstevel@tonic-gate #include <sys/types.h>
440Sstevel@tonic-gate #include <sys/uio.h>
450Sstevel@tonic-gate #include <sys/stat.h>
460Sstevel@tonic-gate #include <sys/resource.h>
470Sstevel@tonic-gate #include <sys/param.h>
480Sstevel@tonic-gate #include <sys/stack.h>
490Sstevel@tonic-gate #include <sys/fault.h>
500Sstevel@tonic-gate #include <sys/syscall.h>
510Sstevel@tonic-gate #include <sys/sysmacros.h>
520Sstevel@tonic-gate 
530Sstevel@tonic-gate #include "libproc.h"
540Sstevel@tonic-gate #include "Pcontrol.h"
550Sstevel@tonic-gate #include "Putil.h"
560Sstevel@tonic-gate #include "P32ton.h"
570Sstevel@tonic-gate 
580Sstevel@tonic-gate int	_libproc_debug;		/* set non-zero to enable debugging printfs */
594753Srh87107 int	_libproc_no_qsort;	/* set non-zero to inhibit sorting */
604753Srh87107 				/* of symbol tables */
61*10201SEdward.Pilatowicz@Sun.COM int	_libproc_incore_elf;	/* only use in-core elf data */
624753Srh87107 
630Sstevel@tonic-gate sigset_t blockable_sigs;	/* signals to block when we need to be safe */
640Sstevel@tonic-gate static	int	minfd;	/* minimum file descriptor returned by dupfd(fd, 0) */
652712Snn35248 char	procfs_path[PATH_MAX] = "/proc";
660Sstevel@tonic-gate 
670Sstevel@tonic-gate /*
680Sstevel@tonic-gate  * Function prototypes for static routines in this module.
690Sstevel@tonic-gate  */
700Sstevel@tonic-gate static	void	deadcheck(struct ps_prochandle *);
710Sstevel@tonic-gate static	void	restore_tracing_flags(struct ps_prochandle *);
720Sstevel@tonic-gate static	void	Lfree_internal(struct ps_prochandle *, struct ps_lwphandle *);
730Sstevel@tonic-gate 
740Sstevel@tonic-gate /*
750Sstevel@tonic-gate  * Read/write interface for live processes: just pread/pwrite the
760Sstevel@tonic-gate  * /proc/<pid>/as file:
770Sstevel@tonic-gate  */
780Sstevel@tonic-gate 
790Sstevel@tonic-gate static ssize_t
800Sstevel@tonic-gate Pread_live(struct ps_prochandle *P, void *buf, size_t n, uintptr_t addr)
810Sstevel@tonic-gate {
820Sstevel@tonic-gate 	return (pread(P->asfd, buf, n, (off_t)addr));
830Sstevel@tonic-gate }
840Sstevel@tonic-gate 
850Sstevel@tonic-gate static ssize_t
860Sstevel@tonic-gate Pwrite_live(struct ps_prochandle *P, const void *buf, size_t n, uintptr_t addr)
870Sstevel@tonic-gate {
880Sstevel@tonic-gate 	return (pwrite(P->asfd, buf, n, (off_t)addr));
890Sstevel@tonic-gate }
900Sstevel@tonic-gate 
910Sstevel@tonic-gate static const ps_rwops_t P_live_ops = { Pread_live, Pwrite_live };
920Sstevel@tonic-gate 
930Sstevel@tonic-gate /*
940Sstevel@tonic-gate  * This is the library's .init handler.
950Sstevel@tonic-gate  */
960Sstevel@tonic-gate #pragma init(_libproc_init)
970Sstevel@tonic-gate void
980Sstevel@tonic-gate _libproc_init(void)
990Sstevel@tonic-gate {
1000Sstevel@tonic-gate 	_libproc_debug = getenv("LIBPROC_DEBUG") != NULL;
1014753Srh87107 	_libproc_no_qsort = getenv("LIBPROC_NO_QSORT") != NULL;
102*10201SEdward.Pilatowicz@Sun.COM 	_libproc_incore_elf = getenv("LIBPROC_INCORE_ELF") != NULL;
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 	(void) sigfillset(&blockable_sigs);
1050Sstevel@tonic-gate 	(void) sigdelset(&blockable_sigs, SIGKILL);
1060Sstevel@tonic-gate 	(void) sigdelset(&blockable_sigs, SIGSTOP);
1070Sstevel@tonic-gate }
1080Sstevel@tonic-gate 
1092712Snn35248 void
1102712Snn35248 Pset_procfs_path(const char *path)
1112712Snn35248 {
1122712Snn35248 	(void) snprintf(procfs_path, sizeof (procfs_path), "%s", path);
1132712Snn35248 }
1142712Snn35248 
1150Sstevel@tonic-gate /*
1160Sstevel@tonic-gate  * Call set_minfd() once before calling dupfd() several times.
1170Sstevel@tonic-gate  * We assume that the application will not reduce its current file
1180Sstevel@tonic-gate  * descriptor limit lower than 512 once it has set at least that value.
1190Sstevel@tonic-gate  */
1200Sstevel@tonic-gate int
1210Sstevel@tonic-gate set_minfd(void)
1220Sstevel@tonic-gate {
1230Sstevel@tonic-gate 	static mutex_t minfd_lock = DEFAULTMUTEX;
1240Sstevel@tonic-gate 	struct rlimit rlim;
1250Sstevel@tonic-gate 	int fd;
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 	if ((fd = minfd) < 256) {
1280Sstevel@tonic-gate 		(void) mutex_lock(&minfd_lock);
1290Sstevel@tonic-gate 		if ((fd = minfd) < 256) {
1300Sstevel@tonic-gate 			if (getrlimit(RLIMIT_NOFILE, &rlim) != 0)
1310Sstevel@tonic-gate 				rlim.rlim_cur = rlim.rlim_max = 0;
1320Sstevel@tonic-gate 			if (rlim.rlim_cur >= 512)
1330Sstevel@tonic-gate 				fd = 256;
1340Sstevel@tonic-gate 			else if ((fd = rlim.rlim_cur / 2) < 3)
1350Sstevel@tonic-gate 				fd = 3;
1363864Sraf 			membar_producer();
1370Sstevel@tonic-gate 			minfd = fd;
1380Sstevel@tonic-gate 		}
1390Sstevel@tonic-gate 		(void) mutex_unlock(&minfd_lock);
1400Sstevel@tonic-gate 	}
1410Sstevel@tonic-gate 	return (fd);
1420Sstevel@tonic-gate }
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate int
1450Sstevel@tonic-gate dupfd(int fd, int dfd)
1460Sstevel@tonic-gate {
1470Sstevel@tonic-gate 	int mfd;
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate 	/*
1500Sstevel@tonic-gate 	 * Make fd be greater than 255 (the 32-bit stdio limit),
1510Sstevel@tonic-gate 	 * or at least make it greater than 2 so that the
1520Sstevel@tonic-gate 	 * program will work when spawned by init(1m).
1530Sstevel@tonic-gate 	 * Also, if dfd is non-zero, dup the fd to be dfd.
1540Sstevel@tonic-gate 	 */
1550Sstevel@tonic-gate 	if ((mfd = minfd) == 0)
1560Sstevel@tonic-gate 		mfd = set_minfd();
1570Sstevel@tonic-gate 	if (dfd > 0 || (0 <= fd && fd < mfd)) {
1580Sstevel@tonic-gate 		if (dfd <= 0)
1590Sstevel@tonic-gate 			dfd = mfd;
1600Sstevel@tonic-gate 		dfd = fcntl(fd, F_DUPFD, dfd);
1610Sstevel@tonic-gate 		(void) close(fd);
1620Sstevel@tonic-gate 		fd = dfd;
1630Sstevel@tonic-gate 	}
1640Sstevel@tonic-gate 	/*
1650Sstevel@tonic-gate 	 * Mark it close-on-exec so any created process doesn't inherit it.
1660Sstevel@tonic-gate 	 */
1670Sstevel@tonic-gate 	if (fd >= 0)
1680Sstevel@tonic-gate 		(void) fcntl(fd, F_SETFD, FD_CLOEXEC);
1690Sstevel@tonic-gate 	return (fd);
1700Sstevel@tonic-gate }
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate /*
1730Sstevel@tonic-gate  * Create a new controlled process.
1740Sstevel@tonic-gate  * Leave it stopped on successful exit from exec() or execve().
1750Sstevel@tonic-gate  * Return an opaque pointer to its process control structure.
1760Sstevel@tonic-gate  * Return NULL if process cannot be created (fork()/exec() not successful).
1770Sstevel@tonic-gate  */
1780Sstevel@tonic-gate struct ps_prochandle *
1790Sstevel@tonic-gate Pxcreate(const char *file,	/* executable file name */
1800Sstevel@tonic-gate 	char *const *argv,	/* argument vector */
1810Sstevel@tonic-gate 	char *const *envp,	/* environment */
1820Sstevel@tonic-gate 	int *perr,	/* pointer to error return code */
1830Sstevel@tonic-gate 	char *path,	/* if non-null, holds exec path name on return */
1840Sstevel@tonic-gate 	size_t len)	/* size of the path buffer */
1850Sstevel@tonic-gate {
1860Sstevel@tonic-gate 	char execpath[PATH_MAX];
1872712Snn35248 	char procname[PATH_MAX];
1880Sstevel@tonic-gate 	struct ps_prochandle *P;
1890Sstevel@tonic-gate 	pid_t pid;
1900Sstevel@tonic-gate 	int fd;
1910Sstevel@tonic-gate 	char *fname;
1920Sstevel@tonic-gate 	int rc;
1930Sstevel@tonic-gate 	int lasterrno = 0;
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 	if (len == 0)	/* zero length, no path */
1960Sstevel@tonic-gate 		path = NULL;
1970Sstevel@tonic-gate 	if (path != NULL)
1980Sstevel@tonic-gate 		*path = '\0';
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 	if ((P = malloc(sizeof (struct ps_prochandle))) == NULL) {
2010Sstevel@tonic-gate 		*perr = C_STRANGE;
2020Sstevel@tonic-gate 		return (NULL);
2030Sstevel@tonic-gate 	}
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 	if ((pid = fork1()) == -1) {
2060Sstevel@tonic-gate 		free(P);
2070Sstevel@tonic-gate 		*perr = C_FORK;
2080Sstevel@tonic-gate 		return (NULL);
2090Sstevel@tonic-gate 	}
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 	if (pid == 0) {			/* child process */
2120Sstevel@tonic-gate 		id_t id;
2130Sstevel@tonic-gate 		extern char **environ;
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate 		/*
2160Sstevel@tonic-gate 		 * If running setuid or setgid, reset credentials to normal.
2170Sstevel@tonic-gate 		 */
2180Sstevel@tonic-gate 		if ((id = getgid()) != getegid())
2190Sstevel@tonic-gate 			(void) setgid(id);
2200Sstevel@tonic-gate 		if ((id = getuid()) != geteuid())
2210Sstevel@tonic-gate 			(void) setuid(id);
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 		Pcreate_callback(P);	/* execute callback (see below) */
2240Sstevel@tonic-gate 		(void) pause();		/* wait for PRSABORT from parent */
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate 		/*
2270Sstevel@tonic-gate 		 * This is ugly.  There is no execvep() function that takes a
2280Sstevel@tonic-gate 		 * path and an environment.  We cheat here by replacing the
2290Sstevel@tonic-gate 		 * global 'environ' variable right before we call this.
2300Sstevel@tonic-gate 		 */
2310Sstevel@tonic-gate 		if (envp)
2320Sstevel@tonic-gate 			environ = (char **)envp;
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 		(void) execvp(file, argv);  /* execute the program */
2350Sstevel@tonic-gate 		_exit(127);
2360Sstevel@tonic-gate 	}
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 	/*
2390Sstevel@tonic-gate 	 * Initialize the process structure.
2400Sstevel@tonic-gate 	 */
2410Sstevel@tonic-gate 	(void) memset(P, 0, sizeof (*P));
2420Sstevel@tonic-gate 	(void) mutex_init(&P->proc_lock, USYNC_THREAD, NULL);
2430Sstevel@tonic-gate 	P->flags |= CREATED;
2440Sstevel@tonic-gate 	P->state = PS_RUN;
2450Sstevel@tonic-gate 	P->pid = pid;
2460Sstevel@tonic-gate 	P->asfd = -1;
2470Sstevel@tonic-gate 	P->ctlfd = -1;
2480Sstevel@tonic-gate 	P->statfd = -1;
2490Sstevel@tonic-gate 	P->agentctlfd = -1;
2500Sstevel@tonic-gate 	P->agentstatfd = -1;
2510Sstevel@tonic-gate 	P->ops = &P_live_ops;
2520Sstevel@tonic-gate 	Pinitsym(P);
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 	/*
2550Sstevel@tonic-gate 	 * Open the /proc/pid files.
2560Sstevel@tonic-gate 	 */
2572712Snn35248 	(void) snprintf(procname, sizeof (procname), "%s/%d/",
2582712Snn35248 	    procfs_path, (int)pid);
2590Sstevel@tonic-gate 	fname = procname + strlen(procname);
2600Sstevel@tonic-gate 	(void) set_minfd();
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate 	/*
2630Sstevel@tonic-gate 	 * Exclusive write open advises others not to interfere.
2640Sstevel@tonic-gate 	 * There is no reason for any of these open()s to fail.
2650Sstevel@tonic-gate 	 */
2660Sstevel@tonic-gate 	(void) strcpy(fname, "as");
2670Sstevel@tonic-gate 	if ((fd = open(procname, (O_RDWR|O_EXCL))) < 0 ||
2680Sstevel@tonic-gate 	    (fd = dupfd(fd, 0)) < 0) {
2690Sstevel@tonic-gate 		dprintf("Pcreate: failed to open %s: %s\n",
2700Sstevel@tonic-gate 		    procname, strerror(errno));
2710Sstevel@tonic-gate 		rc = C_STRANGE;
2720Sstevel@tonic-gate 		goto bad;
2730Sstevel@tonic-gate 	}
2740Sstevel@tonic-gate 	P->asfd = fd;
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate 	(void) strcpy(fname, "status");
2770Sstevel@tonic-gate 	if ((fd = open(procname, O_RDONLY)) < 0 ||
2780Sstevel@tonic-gate 	    (fd = dupfd(fd, 0)) < 0) {
2790Sstevel@tonic-gate 		dprintf("Pcreate: failed to open %s: %s\n",
2800Sstevel@tonic-gate 		    procname, strerror(errno));
2810Sstevel@tonic-gate 		rc = C_STRANGE;
2820Sstevel@tonic-gate 		goto bad;
2830Sstevel@tonic-gate 	}
2840Sstevel@tonic-gate 	P->statfd = fd;
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate 	(void) strcpy(fname, "ctl");
2870Sstevel@tonic-gate 	if ((fd = open(procname, O_WRONLY)) < 0 ||
2880Sstevel@tonic-gate 	    (fd = dupfd(fd, 0)) < 0) {
2890Sstevel@tonic-gate 		dprintf("Pcreate: failed to open %s: %s\n",
2900Sstevel@tonic-gate 		    procname, strerror(errno));
2910Sstevel@tonic-gate 		rc = C_STRANGE;
2920Sstevel@tonic-gate 		goto bad;
2930Sstevel@tonic-gate 	}
2940Sstevel@tonic-gate 	P->ctlfd = fd;
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate 	(void) Pstop(P, 0);	/* stop the controlled process */
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 	/*
2990Sstevel@tonic-gate 	 * Wait for process to sleep in pause().
3000Sstevel@tonic-gate 	 * If the process has already called pause(), then it should be
3010Sstevel@tonic-gate 	 * stopped (PR_REQUESTED) while asleep in pause and we are done.
3020Sstevel@tonic-gate 	 * Else we set up to catch entry/exit to pause() and set the process
3030Sstevel@tonic-gate 	 * running again, expecting it to stop when it reaches pause().
3040Sstevel@tonic-gate 	 * There is no reason for this to fail other than an interrupt.
3050Sstevel@tonic-gate 	 */
3060Sstevel@tonic-gate 	(void) Psysentry(P, SYS_pause, 1);
3070Sstevel@tonic-gate 	(void) Psysexit(P, SYS_pause, 1);
3080Sstevel@tonic-gate 	for (;;) {
3090Sstevel@tonic-gate 		if (P->state == PS_STOP &&
3100Sstevel@tonic-gate 		    P->status.pr_lwp.pr_syscall == SYS_pause &&
3110Sstevel@tonic-gate 		    (P->status.pr_lwp.pr_why == PR_REQUESTED ||
3120Sstevel@tonic-gate 		    P->status.pr_lwp.pr_why == PR_SYSENTRY ||
3130Sstevel@tonic-gate 		    P->status.pr_lwp.pr_why == PR_SYSEXIT))
3140Sstevel@tonic-gate 			break;
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate 		if (P->state != PS_STOP ||	/* interrupt or process died */
3170Sstevel@tonic-gate 		    Psetrun(P, 0, 0) != 0) {	/* can't restart */
3180Sstevel@tonic-gate 			if (errno == EINTR || errno == ERESTART)
3190Sstevel@tonic-gate 				rc = C_INTR;
3200Sstevel@tonic-gate 			else {
3210Sstevel@tonic-gate 				dprintf("Pcreate: Psetrun failed: %s\n",
3220Sstevel@tonic-gate 				    strerror(errno));
3230Sstevel@tonic-gate 				rc = C_STRANGE;
3240Sstevel@tonic-gate 			}
3250Sstevel@tonic-gate 			goto bad;
3260Sstevel@tonic-gate 		}
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate 		(void) Pwait(P, 0);
3290Sstevel@tonic-gate 	}
3300Sstevel@tonic-gate 	(void) Psysentry(P, SYS_pause, 0);
3310Sstevel@tonic-gate 	(void) Psysexit(P, SYS_pause, 0);
3320Sstevel@tonic-gate 
3330Sstevel@tonic-gate 	/*
3340Sstevel@tonic-gate 	 * Kick the process off the pause() and catch
3350Sstevel@tonic-gate 	 * it again on entry to exec() or exit().
3360Sstevel@tonic-gate 	 */
3370Sstevel@tonic-gate 	(void) Psysentry(P, SYS_exit, 1);
3380Sstevel@tonic-gate 	(void) Psysentry(P, SYS_exec, 1);
3390Sstevel@tonic-gate 	(void) Psysentry(P, SYS_execve, 1);
3400Sstevel@tonic-gate 	if (Psetrun(P, 0, PRSABORT) == -1) {
3410Sstevel@tonic-gate 		dprintf("Pcreate: Psetrun failed: %s\n", strerror(errno));
3420Sstevel@tonic-gate 		rc = C_STRANGE;
3430Sstevel@tonic-gate 		goto bad;
3440Sstevel@tonic-gate 	}
3450Sstevel@tonic-gate 	(void) Pwait(P, 0);
3460Sstevel@tonic-gate 	if (P->state != PS_STOP) {
3470Sstevel@tonic-gate 		dprintf("Pcreate: Pwait failed: %s\n", strerror(errno));
3480Sstevel@tonic-gate 		rc = C_STRANGE;
3490Sstevel@tonic-gate 		goto bad;
3500Sstevel@tonic-gate 	}
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate 	/*
3530Sstevel@tonic-gate 	 * Move the process through instances of failed exec()s
3540Sstevel@tonic-gate 	 * to reach the point of stopped on successful exec().
3550Sstevel@tonic-gate 	 */
3560Sstevel@tonic-gate 	(void) Psysexit(P, SYS_exec, TRUE);
3570Sstevel@tonic-gate 	(void) Psysexit(P, SYS_execve, TRUE);
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate 	while (P->state == PS_STOP &&
3600Sstevel@tonic-gate 	    P->status.pr_lwp.pr_why == PR_SYSENTRY &&
3610Sstevel@tonic-gate 	    (P->status.pr_lwp.pr_what == SYS_execve ||
3620Sstevel@tonic-gate 	    P->status.pr_lwp.pr_what == SYS_exec)) {
3630Sstevel@tonic-gate 		/*
3640Sstevel@tonic-gate 		 * Fetch the exec path name now, before we complete
3650Sstevel@tonic-gate 		 * the exec().  We may lose the process and be unable
3660Sstevel@tonic-gate 		 * to get the information later.
3670Sstevel@tonic-gate 		 */
3680Sstevel@tonic-gate 		(void) Pread_string(P, execpath, sizeof (execpath),
3694753Srh87107 		    (off_t)P->status.pr_lwp.pr_sysarg[0]);
3700Sstevel@tonic-gate 		if (path != NULL)
3710Sstevel@tonic-gate 			(void) strncpy(path, execpath, len);
3720Sstevel@tonic-gate 		/*
3730Sstevel@tonic-gate 		 * Set the process running and wait for
3740Sstevel@tonic-gate 		 * it to stop on exit from the exec().
3750Sstevel@tonic-gate 		 */
3760Sstevel@tonic-gate 		(void) Psetrun(P, 0, 0);
3770Sstevel@tonic-gate 		(void) Pwait(P, 0);
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate 		if (P->state == PS_LOST &&		/* we lost control */
3800Sstevel@tonic-gate 		    Preopen(P) != 0) {		/* and we can't get it back */
3810Sstevel@tonic-gate 			rc = C_PERM;
3820Sstevel@tonic-gate 			goto bad;
3830Sstevel@tonic-gate 		}
3840Sstevel@tonic-gate 
3850Sstevel@tonic-gate 		/*
3860Sstevel@tonic-gate 		 * If the exec() failed, continue the loop, expecting
3870Sstevel@tonic-gate 		 * there to be more attempts to exec(), based on PATH.
3880Sstevel@tonic-gate 		 */
3890Sstevel@tonic-gate 		if (P->state == PS_STOP &&
3900Sstevel@tonic-gate 		    P->status.pr_lwp.pr_why == PR_SYSEXIT &&
3910Sstevel@tonic-gate 		    (P->status.pr_lwp.pr_what == SYS_execve ||
3920Sstevel@tonic-gate 		    P->status.pr_lwp.pr_what == SYS_exec) &&
3930Sstevel@tonic-gate 		    (lasterrno = P->status.pr_lwp.pr_errno) != 0) {
3940Sstevel@tonic-gate 			/*
3950Sstevel@tonic-gate 			 * The exec() failed.  Set the process running and
3960Sstevel@tonic-gate 			 * wait for it to stop on entry to the next exec().
3970Sstevel@tonic-gate 			 */
3980Sstevel@tonic-gate 			(void) Psetrun(P, 0, 0);
3990Sstevel@tonic-gate 			(void) Pwait(P, 0);
4000Sstevel@tonic-gate 
4010Sstevel@tonic-gate 			continue;
4020Sstevel@tonic-gate 		}
4030Sstevel@tonic-gate 		break;
4040Sstevel@tonic-gate 	}
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate 	if (P->state == PS_STOP &&
4070Sstevel@tonic-gate 	    P->status.pr_lwp.pr_why == PR_SYSEXIT &&
4080Sstevel@tonic-gate 	    (P->status.pr_lwp.pr_what == SYS_execve ||
4090Sstevel@tonic-gate 	    P->status.pr_lwp.pr_what == SYS_exec) &&
4100Sstevel@tonic-gate 	    P->status.pr_lwp.pr_errno == 0) {
4110Sstevel@tonic-gate 		/*
4120Sstevel@tonic-gate 		 * The process is stopped on successful exec() or execve().
4130Sstevel@tonic-gate 		 * Turn off all tracing flags and return success.
4140Sstevel@tonic-gate 		 */
4150Sstevel@tonic-gate 		restore_tracing_flags(P);
4160Sstevel@tonic-gate #ifndef _LP64
4170Sstevel@tonic-gate 		/* We must be a 64-bit process to deal with a 64-bit process */
4180Sstevel@tonic-gate 		if (P->status.pr_dmodel == PR_MODEL_LP64) {
4190Sstevel@tonic-gate 			rc = C_LP64;
4200Sstevel@tonic-gate 			goto bad;
4210Sstevel@tonic-gate 		}
4220Sstevel@tonic-gate #endif
4230Sstevel@tonic-gate 		/*
4240Sstevel@tonic-gate 		 * Set run-on-last-close so the controlled process
4250Sstevel@tonic-gate 		 * runs even if we die on a signal.
4260Sstevel@tonic-gate 		 */
4270Sstevel@tonic-gate 		(void) Psetflags(P, PR_RLC);
4280Sstevel@tonic-gate 		*perr = 0;
4290Sstevel@tonic-gate 		return (P);
4300Sstevel@tonic-gate 	}
4310Sstevel@tonic-gate 
4320Sstevel@tonic-gate 	rc = lasterrno == ENOENT ? C_NOENT : C_NOEXEC;
4330Sstevel@tonic-gate 
4340Sstevel@tonic-gate bad:
4350Sstevel@tonic-gate 	(void) kill(pid, SIGKILL);
4360Sstevel@tonic-gate 	if (path != NULL && rc != C_PERM && rc != C_LP64)
4370Sstevel@tonic-gate 		*path = '\0';
4380Sstevel@tonic-gate 	Pfree(P);
4390Sstevel@tonic-gate 	*perr = rc;
4400Sstevel@tonic-gate 	return (NULL);
4410Sstevel@tonic-gate }
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate struct ps_prochandle *
4440Sstevel@tonic-gate Pcreate(
4450Sstevel@tonic-gate 	const char *file,	/* executable file name */
4460Sstevel@tonic-gate 	char *const *argv,	/* argument vector */
4470Sstevel@tonic-gate 	int *perr,	/* pointer to error return code */
4480Sstevel@tonic-gate 	char *path,	/* if non-null, holds exec path name on return */
4490Sstevel@tonic-gate 	size_t len)	/* size of the path buffer */
4500Sstevel@tonic-gate {
4510Sstevel@tonic-gate 	return (Pxcreate(file, argv, NULL, perr, path, len));
4520Sstevel@tonic-gate }
4530Sstevel@tonic-gate 
4540Sstevel@tonic-gate /*
4550Sstevel@tonic-gate  * Return a printable string corresponding to a Pcreate() error return.
4560Sstevel@tonic-gate  */
4570Sstevel@tonic-gate const char *
4580Sstevel@tonic-gate Pcreate_error(int error)
4590Sstevel@tonic-gate {
4600Sstevel@tonic-gate 	const char *str;
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate 	switch (error) {
4630Sstevel@tonic-gate 	case C_FORK:
4640Sstevel@tonic-gate 		str = "cannot fork";
4650Sstevel@tonic-gate 		break;
4660Sstevel@tonic-gate 	case C_PERM:
4670Sstevel@tonic-gate 		str = "file is set-id or unreadable";
4680Sstevel@tonic-gate 		break;
4690Sstevel@tonic-gate 	case C_NOEXEC:
4700Sstevel@tonic-gate 		str = "cannot execute file";
4710Sstevel@tonic-gate 		break;
4720Sstevel@tonic-gate 	case C_INTR:
4730Sstevel@tonic-gate 		str = "operation interrupted";
4740Sstevel@tonic-gate 		break;
4750Sstevel@tonic-gate 	case C_LP64:
4760Sstevel@tonic-gate 		str = "program is _LP64, self is not";
4770Sstevel@tonic-gate 		break;
4780Sstevel@tonic-gate 	case C_STRANGE:
4790Sstevel@tonic-gate 		str = "unanticipated system error";
4800Sstevel@tonic-gate 		break;
4810Sstevel@tonic-gate 	case C_NOENT:
4820Sstevel@tonic-gate 		str = "cannot find executable file";
4830Sstevel@tonic-gate 		break;
4840Sstevel@tonic-gate 	default:
4850Sstevel@tonic-gate 		str = "unknown error";
4860Sstevel@tonic-gate 		break;
4870Sstevel@tonic-gate 	}
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate 	return (str);
4900Sstevel@tonic-gate }
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate /*
4930Sstevel@tonic-gate  * Callback to execute in each child process created with Pcreate() after fork
4940Sstevel@tonic-gate  * but before it execs the new process image.  By default, we do nothing, but
4950Sstevel@tonic-gate  * by calling this function we allow the client program to define its own
4960Sstevel@tonic-gate  * version of the function which will interpose on our empty default.  This
4970Sstevel@tonic-gate  * may be useful for clients that need to modify signal dispositions, terminal
4980Sstevel@tonic-gate  * attributes, or process group and session properties for each new victim.
4990Sstevel@tonic-gate  */
5000Sstevel@tonic-gate /*ARGSUSED*/
5010Sstevel@tonic-gate void
5020Sstevel@tonic-gate Pcreate_callback(struct ps_prochandle *P)
5030Sstevel@tonic-gate {
5040Sstevel@tonic-gate 	/* nothing to do here */
5050Sstevel@tonic-gate }
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate /*
5080Sstevel@tonic-gate  * Grab an existing process.
5090Sstevel@tonic-gate  * Return an opaque pointer to its process control structure.
5100Sstevel@tonic-gate  *
5110Sstevel@tonic-gate  * pid:		UNIX process ID.
5120Sstevel@tonic-gate  * flags:
5130Sstevel@tonic-gate  *	PGRAB_RETAIN	Retain tracing flags (default clears all tracing flags).
5140Sstevel@tonic-gate  *	PGRAB_FORCE	Grab regardless of whether process is already traced.
5150Sstevel@tonic-gate  *	PGRAB_RDONLY	Open the address space file O_RDONLY instead of O_RDWR,
5160Sstevel@tonic-gate  *                      and do not open the process control file.
5170Sstevel@tonic-gate  *	PGRAB_NOSTOP	Open the process but do not force it to stop.
5180Sstevel@tonic-gate  * perr:	pointer to error return code.
5190Sstevel@tonic-gate  */
5200Sstevel@tonic-gate struct ps_prochandle *
5210Sstevel@tonic-gate Pgrab(pid_t pid, int flags, int *perr)
5220Sstevel@tonic-gate {
5230Sstevel@tonic-gate 	struct ps_prochandle *P;
5240Sstevel@tonic-gate 	int fd, omode;
5252712Snn35248 	char procname[PATH_MAX];
5260Sstevel@tonic-gate 	char *fname;
5270Sstevel@tonic-gate 	int rc = 0;
5280Sstevel@tonic-gate 
5290Sstevel@tonic-gate 	/*
5300Sstevel@tonic-gate 	 * PGRAB_RDONLY means that we do not open the /proc/<pid>/control file,
5310Sstevel@tonic-gate 	 * and so it implies RETAIN and NOSTOP since both require control.
5320Sstevel@tonic-gate 	 */
5330Sstevel@tonic-gate 	if (flags & PGRAB_RDONLY)
5340Sstevel@tonic-gate 		flags |= PGRAB_RETAIN | PGRAB_NOSTOP;
5350Sstevel@tonic-gate 
5360Sstevel@tonic-gate 	if ((P = malloc(sizeof (struct ps_prochandle))) == NULL) {
5370Sstevel@tonic-gate 		*perr = G_STRANGE;
5380Sstevel@tonic-gate 		return (NULL);
5390Sstevel@tonic-gate 	}
5400Sstevel@tonic-gate 
5410Sstevel@tonic-gate 	P->asfd = -1;
5420Sstevel@tonic-gate 	P->ctlfd = -1;
5430Sstevel@tonic-gate 	P->statfd = -1;
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate again:	/* Come back here if we lose it in the Window of Vulnerability */
5460Sstevel@tonic-gate 	if (P->ctlfd >= 0)
5470Sstevel@tonic-gate 		(void) close(P->ctlfd);
5480Sstevel@tonic-gate 	if (P->asfd >= 0)
5490Sstevel@tonic-gate 		(void) close(P->asfd);
5500Sstevel@tonic-gate 	if (P->statfd >= 0)
5510Sstevel@tonic-gate 		(void) close(P->statfd);
5520Sstevel@tonic-gate 	(void) memset(P, 0, sizeof (*P));
5530Sstevel@tonic-gate 	(void) mutex_init(&P->proc_lock, USYNC_THREAD, NULL);
5540Sstevel@tonic-gate 	P->ctlfd = -1;
5550Sstevel@tonic-gate 	P->asfd = -1;
5560Sstevel@tonic-gate 	P->statfd = -1;
5570Sstevel@tonic-gate 	P->agentctlfd = -1;
5580Sstevel@tonic-gate 	P->agentstatfd = -1;
5590Sstevel@tonic-gate 	P->ops = &P_live_ops;
5600Sstevel@tonic-gate 	Pinitsym(P);
5610Sstevel@tonic-gate 
5620Sstevel@tonic-gate 	/*
5630Sstevel@tonic-gate 	 * Open the /proc/pid files
5640Sstevel@tonic-gate 	 */
5652712Snn35248 	(void) snprintf(procname, sizeof (procname), "%s/%d/",
5662712Snn35248 	    procfs_path, (int)pid);
5670Sstevel@tonic-gate 	fname = procname + strlen(procname);
5680Sstevel@tonic-gate 	(void) set_minfd();
5690Sstevel@tonic-gate 
5700Sstevel@tonic-gate 	/*
5710Sstevel@tonic-gate 	 * Request exclusive open to avoid grabbing someone else's
5720Sstevel@tonic-gate 	 * process and to prevent others from interfering afterwards.
5730Sstevel@tonic-gate 	 * If this fails and the 'PGRAB_FORCE' flag is set, attempt to
5740Sstevel@tonic-gate 	 * open non-exclusively.
5750Sstevel@tonic-gate 	 */
5760Sstevel@tonic-gate 	(void) strcpy(fname, "as");
5770Sstevel@tonic-gate 	omode = (flags & PGRAB_RDONLY) ? O_RDONLY : O_RDWR;
5780Sstevel@tonic-gate 
5790Sstevel@tonic-gate 	if (((fd = open(procname, omode | O_EXCL)) < 0 &&
5800Sstevel@tonic-gate 	    (fd = ((flags & PGRAB_FORCE)? open(procname, omode) : -1)) < 0) ||
5810Sstevel@tonic-gate 	    (fd = dupfd(fd, 0)) < 0) {
5820Sstevel@tonic-gate 		switch (errno) {
5830Sstevel@tonic-gate 		case ENOENT:
5840Sstevel@tonic-gate 			rc = G_NOPROC;
5850Sstevel@tonic-gate 			break;
5860Sstevel@tonic-gate 		case EACCES:
5870Sstevel@tonic-gate 		case EPERM:
5880Sstevel@tonic-gate 			rc = G_PERM;
5890Sstevel@tonic-gate 			break;
5905272Sjhaslam 		case EMFILE:
5915272Sjhaslam 			rc = G_NOFD;
5925272Sjhaslam 			break;
5930Sstevel@tonic-gate 		case EBUSY:
5940Sstevel@tonic-gate 			if (!(flags & PGRAB_FORCE) || geteuid() != 0) {
5950Sstevel@tonic-gate 				rc = G_BUSY;
5960Sstevel@tonic-gate 				break;
5970Sstevel@tonic-gate 			}
5980Sstevel@tonic-gate 			/* FALLTHROUGH */
5990Sstevel@tonic-gate 		default:
6000Sstevel@tonic-gate 			dprintf("Pgrab: failed to open %s: %s\n",
6010Sstevel@tonic-gate 			    procname, strerror(errno));
6020Sstevel@tonic-gate 			rc = G_STRANGE;
6030Sstevel@tonic-gate 			break;
6040Sstevel@tonic-gate 		}
6050Sstevel@tonic-gate 		goto err;
6060Sstevel@tonic-gate 	}
6070Sstevel@tonic-gate 	P->asfd = fd;
6080Sstevel@tonic-gate 
6090Sstevel@tonic-gate 	(void) strcpy(fname, "status");
6100Sstevel@tonic-gate 	if ((fd = open(procname, O_RDONLY)) < 0 ||
6110Sstevel@tonic-gate 	    (fd = dupfd(fd, 0)) < 0) {
6120Sstevel@tonic-gate 		switch (errno) {
6130Sstevel@tonic-gate 		case ENOENT:
6140Sstevel@tonic-gate 			rc = G_NOPROC;
6150Sstevel@tonic-gate 			break;
6165272Sjhaslam 		case EMFILE:
6175272Sjhaslam 			rc = G_NOFD;
6185272Sjhaslam 			break;
6190Sstevel@tonic-gate 		default:
6200Sstevel@tonic-gate 			dprintf("Pgrab: failed to open %s: %s\n",
6210Sstevel@tonic-gate 			    procname, strerror(errno));
6220Sstevel@tonic-gate 			rc = G_STRANGE;
6230Sstevel@tonic-gate 			break;
6240Sstevel@tonic-gate 		}
6250Sstevel@tonic-gate 		goto err;
6260Sstevel@tonic-gate 	}
6270Sstevel@tonic-gate 	P->statfd = fd;
6280Sstevel@tonic-gate 
6290Sstevel@tonic-gate 	if (!(flags & PGRAB_RDONLY)) {
6300Sstevel@tonic-gate 		(void) strcpy(fname, "ctl");
6310Sstevel@tonic-gate 		if ((fd = open(procname, O_WRONLY)) < 0 ||
6320Sstevel@tonic-gate 		    (fd = dupfd(fd, 0)) < 0) {
6330Sstevel@tonic-gate 			switch (errno) {
6340Sstevel@tonic-gate 			case ENOENT:
6350Sstevel@tonic-gate 				rc = G_NOPROC;
6360Sstevel@tonic-gate 				break;
6375272Sjhaslam 			case EMFILE:
6385272Sjhaslam 				rc = G_NOFD;
6395272Sjhaslam 				break;
6400Sstevel@tonic-gate 			default:
6410Sstevel@tonic-gate 				dprintf("Pgrab: failed to open %s: %s\n",
6420Sstevel@tonic-gate 				    procname, strerror(errno));
6430Sstevel@tonic-gate 				rc = G_STRANGE;
6440Sstevel@tonic-gate 				break;
6450Sstevel@tonic-gate 			}
6460Sstevel@tonic-gate 			goto err;
6470Sstevel@tonic-gate 		}
6480Sstevel@tonic-gate 		P->ctlfd = fd;
6490Sstevel@tonic-gate 	}
6500Sstevel@tonic-gate 
6510Sstevel@tonic-gate 	P->state = PS_RUN;
6520Sstevel@tonic-gate 	P->pid = pid;
6530Sstevel@tonic-gate 
6540Sstevel@tonic-gate 	/*
6550Sstevel@tonic-gate 	 * We are now in the Window of Vulnerability (WoV).  The process may
6560Sstevel@tonic-gate 	 * exec() a setuid/setgid or unreadable object file between the open()
6570Sstevel@tonic-gate 	 * and the PCSTOP.  We will get EAGAIN in this case and must start over.
6580Sstevel@tonic-gate 	 * As Pstopstatus will trigger the first read() from a /proc file,
6590Sstevel@tonic-gate 	 * we also need to handle EOVERFLOW here when 32-bit as an indicator
6600Sstevel@tonic-gate 	 * that this process is 64-bit.  Finally, if the process has become
6610Sstevel@tonic-gate 	 * a zombie (PS_UNDEAD) while we were trying to grab it, just remain
6620Sstevel@tonic-gate 	 * silent about this and pretend there was no process.
6630Sstevel@tonic-gate 	 */
6640Sstevel@tonic-gate 	if (Pstopstatus(P, PCNULL, 0) != 0) {
6650Sstevel@tonic-gate #ifndef _LP64
6660Sstevel@tonic-gate 		if (errno == EOVERFLOW) {
6670Sstevel@tonic-gate 			rc = G_LP64;
6680Sstevel@tonic-gate 			goto err;
6690Sstevel@tonic-gate 		}
6700Sstevel@tonic-gate #endif
6710Sstevel@tonic-gate 		if (P->state == PS_LOST) {	/* WoV */
6720Sstevel@tonic-gate 			(void) mutex_destroy(&P->proc_lock);
6730Sstevel@tonic-gate 			goto again;
6740Sstevel@tonic-gate 		}
6750Sstevel@tonic-gate 
6760Sstevel@tonic-gate 		if (P->state == PS_UNDEAD)
6770Sstevel@tonic-gate 			rc = G_NOPROC;
6780Sstevel@tonic-gate 		else
6790Sstevel@tonic-gate 			rc = G_STRANGE;
6800Sstevel@tonic-gate 
6810Sstevel@tonic-gate 		goto err;
6820Sstevel@tonic-gate 	}
6830Sstevel@tonic-gate 
6840Sstevel@tonic-gate 	/*
6850Sstevel@tonic-gate 	 * If the process is a system process, we can't control it even as root
6860Sstevel@tonic-gate 	 */
6870Sstevel@tonic-gate 	if (P->status.pr_flags & PR_ISSYS) {
6880Sstevel@tonic-gate 		rc = G_SYS;
6890Sstevel@tonic-gate 		goto err;
6900Sstevel@tonic-gate 	}
6910Sstevel@tonic-gate #ifndef _LP64
6920Sstevel@tonic-gate 	/*
6930Sstevel@tonic-gate 	 * We must be a 64-bit process to deal with a 64-bit process
6940Sstevel@tonic-gate 	 */
6950Sstevel@tonic-gate 	if (P->status.pr_dmodel == PR_MODEL_LP64) {
6960Sstevel@tonic-gate 		rc = G_LP64;
6970Sstevel@tonic-gate 		goto err;
6980Sstevel@tonic-gate 	}
6990Sstevel@tonic-gate #endif
7000Sstevel@tonic-gate 
7010Sstevel@tonic-gate 	/*
7020Sstevel@tonic-gate 	 * Remember the status for use by Prelease().
7030Sstevel@tonic-gate 	 */
7040Sstevel@tonic-gate 	P->orig_status = P->status;	/* structure copy */
7050Sstevel@tonic-gate 
7060Sstevel@tonic-gate 	/*
7070Sstevel@tonic-gate 	 * Before stopping the process, make sure we are not grabbing ourselves.
7080Sstevel@tonic-gate 	 * If we are, make sure we are doing it PGRAB_RDONLY.
7090Sstevel@tonic-gate 	 */
7100Sstevel@tonic-gate 	if (pid == getpid()) {
7110Sstevel@tonic-gate 		/*
7120Sstevel@tonic-gate 		 * Verify that the process is really ourself:
7130Sstevel@tonic-gate 		 * Set a magic number, read it through the
7140Sstevel@tonic-gate 		 * /proc file and see if the results match.
7150Sstevel@tonic-gate 		 */
7160Sstevel@tonic-gate 		uint32_t magic1 = 0;
7170Sstevel@tonic-gate 		uint32_t magic2 = 2;
7180Sstevel@tonic-gate 
7190Sstevel@tonic-gate 		errno = 0;
7200Sstevel@tonic-gate 
7210Sstevel@tonic-gate 		if (Pread(P, &magic2, sizeof (magic2), (uintptr_t)&magic1)
7220Sstevel@tonic-gate 		    == sizeof (magic2) &&
7230Sstevel@tonic-gate 		    magic2 == 0 &&
7240Sstevel@tonic-gate 		    (magic1 = 0xfeedbeef) &&
7250Sstevel@tonic-gate 		    Pread(P, &magic2, sizeof (magic2), (uintptr_t)&magic1)
7260Sstevel@tonic-gate 		    == sizeof (magic2) &&
7270Sstevel@tonic-gate 		    magic2 == 0xfeedbeef &&
7280Sstevel@tonic-gate 		    !(flags & PGRAB_RDONLY)) {
7290Sstevel@tonic-gate 			rc = G_SELF;
7300Sstevel@tonic-gate 			goto err;
7310Sstevel@tonic-gate 		}
7320Sstevel@tonic-gate 	}
7330Sstevel@tonic-gate 
7340Sstevel@tonic-gate 	/*
7350Sstevel@tonic-gate 	 * If the process is already stopped or has been directed
7360Sstevel@tonic-gate 	 * to stop via /proc, do not set run-on-last-close.
7370Sstevel@tonic-gate 	 */
7380Sstevel@tonic-gate 	if (!(P->status.pr_lwp.pr_flags & (PR_ISTOP|PR_DSTOP)) &&
7390Sstevel@tonic-gate 	    !(flags & PGRAB_RDONLY)) {
7400Sstevel@tonic-gate 		/*
7410Sstevel@tonic-gate 		 * Mark the process run-on-last-close so
7420Sstevel@tonic-gate 		 * it runs even if we die from SIGKILL.
7430Sstevel@tonic-gate 		 */
7440Sstevel@tonic-gate 		if (Psetflags(P, PR_RLC) != 0) {
7450Sstevel@tonic-gate 			if (errno == EAGAIN) {	/* WoV */
7460Sstevel@tonic-gate 				(void) mutex_destroy(&P->proc_lock);
7470Sstevel@tonic-gate 				goto again;
7480Sstevel@tonic-gate 			}
7490Sstevel@tonic-gate 			if (errno == ENOENT)	/* No complaint about zombies */
7500Sstevel@tonic-gate 				rc = G_ZOMB;
7510Sstevel@tonic-gate 			else {
7520Sstevel@tonic-gate 				dprintf("Pgrab: failed to set RLC\n");
7530Sstevel@tonic-gate 				rc = G_STRANGE;
7540Sstevel@tonic-gate 			}
7550Sstevel@tonic-gate 			goto err;
7560Sstevel@tonic-gate 		}
7570Sstevel@tonic-gate 	}
7580Sstevel@tonic-gate 
7590Sstevel@tonic-gate 	/*
7600Sstevel@tonic-gate 	 * If a stop directive is pending and the process has not yet stopped,
7610Sstevel@tonic-gate 	 * then synchronously wait for the stop directive to take effect.
7620Sstevel@tonic-gate 	 * Limit the time spent waiting for the process to stop by iterating
7630Sstevel@tonic-gate 	 * at most 10 times. The time-out of 20 ms corresponds to the time
7640Sstevel@tonic-gate 	 * between sending the stop directive and the process actually stopped
7650Sstevel@tonic-gate 	 * as measured by DTrace on a slow, busy system. If the process doesn't
7660Sstevel@tonic-gate 	 * stop voluntarily, clear the PR_DSTOP flag so that the code below
7670Sstevel@tonic-gate 	 * forces the process to stop.
7680Sstevel@tonic-gate 	 */
7690Sstevel@tonic-gate 	if (!(flags & PGRAB_RDONLY)) {
7700Sstevel@tonic-gate 		int niter = 0;
7710Sstevel@tonic-gate 		while ((P->status.pr_lwp.pr_flags & (PR_STOPPED|PR_DSTOP)) ==
7720Sstevel@tonic-gate 		    PR_DSTOP && niter < 10 &&
7730Sstevel@tonic-gate 		    Pstopstatus(P, PCTWSTOP, 20) != 0) {
7740Sstevel@tonic-gate 			niter++;
7750Sstevel@tonic-gate 			if (flags & PGRAB_NOSTOP)
7760Sstevel@tonic-gate 				break;
7770Sstevel@tonic-gate 		}
7780Sstevel@tonic-gate 		if (niter == 10 && !(flags & PGRAB_NOSTOP)) {
7790Sstevel@tonic-gate 			/* Try it harder down below */
7800Sstevel@tonic-gate 			P->status.pr_lwp.pr_flags &= ~PR_DSTOP;
7810Sstevel@tonic-gate 		}
7820Sstevel@tonic-gate 	}
7830Sstevel@tonic-gate 
7840Sstevel@tonic-gate 	/*
7850Sstevel@tonic-gate 	 * If the process is not already stopped or directed to stop
7860Sstevel@tonic-gate 	 * and PGRAB_NOSTOP was not specified, stop the process now.
7870Sstevel@tonic-gate 	 */
7880Sstevel@tonic-gate 	if (!(P->status.pr_lwp.pr_flags & (PR_ISTOP|PR_DSTOP)) &&
7890Sstevel@tonic-gate 	    !(flags & PGRAB_NOSTOP)) {
7900Sstevel@tonic-gate 		/*
7910Sstevel@tonic-gate 		 * Stop the process, get its status and signal/syscall masks.
7920Sstevel@tonic-gate 		 */
7930Sstevel@tonic-gate 		if (((P->status.pr_lwp.pr_flags & PR_STOPPED) &&
7940Sstevel@tonic-gate 		    Pstopstatus(P, PCDSTOP, 0) != 0) ||
7950Sstevel@tonic-gate 		    Pstopstatus(P, PCSTOP, 2000) != 0) {
7960Sstevel@tonic-gate #ifndef _LP64
7970Sstevel@tonic-gate 			if (errno == EOVERFLOW) {
7980Sstevel@tonic-gate 				rc = G_LP64;
7990Sstevel@tonic-gate 				goto err;
8000Sstevel@tonic-gate 			}
8010Sstevel@tonic-gate #endif
8020Sstevel@tonic-gate 			if (P->state == PS_LOST) {	/* WoV */
8030Sstevel@tonic-gate 				(void) mutex_destroy(&P->proc_lock);
8040Sstevel@tonic-gate 				goto again;
8050Sstevel@tonic-gate 			}
8060Sstevel@tonic-gate 			if ((errno != EINTR && errno != ERESTART) ||
8070Sstevel@tonic-gate 			    (P->state != PS_STOP &&
8080Sstevel@tonic-gate 			    !(P->status.pr_flags & PR_DSTOP))) {
8090Sstevel@tonic-gate 				if (P->state != PS_RUN && errno != ENOENT) {
8100Sstevel@tonic-gate 					dprintf("Pgrab: failed to PCSTOP\n");
8110Sstevel@tonic-gate 					rc = G_STRANGE;
8120Sstevel@tonic-gate 				} else {
8130Sstevel@tonic-gate 					rc = G_ZOMB;
8140Sstevel@tonic-gate 				}
8150Sstevel@tonic-gate 				goto err;
8160Sstevel@tonic-gate 			}
8170Sstevel@tonic-gate 		}
8180Sstevel@tonic-gate 
8190Sstevel@tonic-gate 		/*
8200Sstevel@tonic-gate 		 * Process should now either be stopped via /proc or there
8210Sstevel@tonic-gate 		 * should be an outstanding stop directive.
8220Sstevel@tonic-gate 		 */
8230Sstevel@tonic-gate 		if (!(P->status.pr_flags & (PR_ISTOP|PR_DSTOP))) {
8240Sstevel@tonic-gate 			dprintf("Pgrab: process is not stopped\n");
8250Sstevel@tonic-gate 			rc = G_STRANGE;
8260Sstevel@tonic-gate 			goto err;
8270Sstevel@tonic-gate 		}
8280Sstevel@tonic-gate #ifndef _LP64
8290Sstevel@tonic-gate 		/*
8300Sstevel@tonic-gate 		 * Test this again now because the 32-bit victim process may
8310Sstevel@tonic-gate 		 * have exec'd a 64-bit process in the meantime.
8320Sstevel@tonic-gate 		 */
8330Sstevel@tonic-gate 		if (P->status.pr_dmodel == PR_MODEL_LP64) {
8340Sstevel@tonic-gate 			rc = G_LP64;
8350Sstevel@tonic-gate 			goto err;
8360Sstevel@tonic-gate 		}
8370Sstevel@tonic-gate #endif
8380Sstevel@tonic-gate 	}
8390Sstevel@tonic-gate 
8400Sstevel@tonic-gate 	/*
8410Sstevel@tonic-gate 	 * Cancel all tracing flags unless the PGRAB_RETAIN flag is set.
8420Sstevel@tonic-gate 	 */
8430Sstevel@tonic-gate 	if (!(flags & PGRAB_RETAIN)) {
8440Sstevel@tonic-gate 		(void) Psysentry(P, 0, FALSE);
8450Sstevel@tonic-gate 		(void) Psysexit(P, 0, FALSE);
8460Sstevel@tonic-gate 		(void) Psignal(P, 0, FALSE);
8470Sstevel@tonic-gate 		(void) Pfault(P, 0, FALSE);
8480Sstevel@tonic-gate 		Psync(P);
8490Sstevel@tonic-gate 	}
8500Sstevel@tonic-gate 
8510Sstevel@tonic-gate 	*perr = 0;
8520Sstevel@tonic-gate 	return (P);
8530Sstevel@tonic-gate 
8540Sstevel@tonic-gate err:
8550Sstevel@tonic-gate 	Pfree(P);
8560Sstevel@tonic-gate 	*perr = rc;
8570Sstevel@tonic-gate 	return (NULL);
8580Sstevel@tonic-gate }
8590Sstevel@tonic-gate 
8600Sstevel@tonic-gate /*
8610Sstevel@tonic-gate  * Return a printable string corresponding to a Pgrab() error return.
8620Sstevel@tonic-gate  */
8630Sstevel@tonic-gate const char *
8640Sstevel@tonic-gate Pgrab_error(int error)
8650Sstevel@tonic-gate {
8660Sstevel@tonic-gate 	const char *str;
8670Sstevel@tonic-gate 
8680Sstevel@tonic-gate 	switch (error) {
8690Sstevel@tonic-gate 	case G_NOPROC:
8700Sstevel@tonic-gate 		str = "no such process";
8710Sstevel@tonic-gate 		break;
8720Sstevel@tonic-gate 	case G_NOCORE:
8730Sstevel@tonic-gate 		str = "no such core file";
8740Sstevel@tonic-gate 		break;
8750Sstevel@tonic-gate 	case G_NOPROCORCORE:
8760Sstevel@tonic-gate 		str = "no such process or core file";
8770Sstevel@tonic-gate 		break;
8780Sstevel@tonic-gate 	case G_NOEXEC:
8790Sstevel@tonic-gate 		str = "cannot find executable file";
8800Sstevel@tonic-gate 		break;
8810Sstevel@tonic-gate 	case G_ZOMB:
8820Sstevel@tonic-gate 		str = "zombie process";
8830Sstevel@tonic-gate 		break;
8840Sstevel@tonic-gate 	case G_PERM:
8850Sstevel@tonic-gate 		str = "permission denied";
8860Sstevel@tonic-gate 		break;
8870Sstevel@tonic-gate 	case G_BUSY:
8880Sstevel@tonic-gate 		str = "process is traced";
8890Sstevel@tonic-gate 		break;
8900Sstevel@tonic-gate 	case G_SYS:
8910Sstevel@tonic-gate 		str = "system process";
8920Sstevel@tonic-gate 		break;
8930Sstevel@tonic-gate 	case G_SELF:
8940Sstevel@tonic-gate 		str = "attempt to grab self";
8950Sstevel@tonic-gate 		break;
8960Sstevel@tonic-gate 	case G_INTR:
8970Sstevel@tonic-gate 		str = "operation interrupted";
8980Sstevel@tonic-gate 		break;
8990Sstevel@tonic-gate 	case G_LP64:
9000Sstevel@tonic-gate 		str = "program is _LP64, self is not";
9010Sstevel@tonic-gate 		break;
9020Sstevel@tonic-gate 	case G_FORMAT:
9030Sstevel@tonic-gate 		str = "file is not an ELF core file";
9040Sstevel@tonic-gate 		break;
9050Sstevel@tonic-gate 	case G_ELF:
9060Sstevel@tonic-gate 		str = "libelf error";
9070Sstevel@tonic-gate 		break;
9080Sstevel@tonic-gate 	case G_NOTE:
9090Sstevel@tonic-gate 		str = "core file is corrupt or missing required data";
9100Sstevel@tonic-gate 		break;
9110Sstevel@tonic-gate 	case G_STRANGE:
9120Sstevel@tonic-gate 		str = "unanticipated system error";
9130Sstevel@tonic-gate 		break;
9140Sstevel@tonic-gate 	case G_ISAINVAL:
9150Sstevel@tonic-gate 		str = "wrong ELF machine type";
9160Sstevel@tonic-gate 		break;
9170Sstevel@tonic-gate 	case G_BADLWPS:
9180Sstevel@tonic-gate 		str = "bad lwp specification";
9190Sstevel@tonic-gate 		break;
9205272Sjhaslam 	case G_NOFD:
9215272Sjhaslam 		str = "too many open files";
9225272Sjhaslam 		break;
9230Sstevel@tonic-gate 	default:
9240Sstevel@tonic-gate 		str = "unknown error";
9250Sstevel@tonic-gate 		break;
9260Sstevel@tonic-gate 	}
9270Sstevel@tonic-gate 
9280Sstevel@tonic-gate 	return (str);
9290Sstevel@tonic-gate }
9300Sstevel@tonic-gate 
9310Sstevel@tonic-gate /*
9320Sstevel@tonic-gate  * Free a process control structure.
9330Sstevel@tonic-gate  * Close the file descriptors but don't do the Prelease logic.
9340Sstevel@tonic-gate  */
9350Sstevel@tonic-gate void
9360Sstevel@tonic-gate Pfree(struct ps_prochandle *P)
9370Sstevel@tonic-gate {
9380Sstevel@tonic-gate 	uint_t i;
9390Sstevel@tonic-gate 
9400Sstevel@tonic-gate 	if (P->core != NULL) {
9410Sstevel@tonic-gate 		extern void __priv_free_info(void *);
9420Sstevel@tonic-gate 		lwp_info_t *nlwp, *lwp = list_next(&P->core->core_lwp_head);
9430Sstevel@tonic-gate 
9440Sstevel@tonic-gate 		for (i = 0; i < P->core->core_nlwp; i++, lwp = nlwp) {
9450Sstevel@tonic-gate 			nlwp = list_next(lwp);
9460Sstevel@tonic-gate #ifdef __sparc
9470Sstevel@tonic-gate 			if (lwp->lwp_gwins != NULL)
9480Sstevel@tonic-gate 				free(lwp->lwp_gwins);
9490Sstevel@tonic-gate 			if (lwp->lwp_xregs != NULL)
9500Sstevel@tonic-gate 				free(lwp->lwp_xregs);
9510Sstevel@tonic-gate 			if (lwp->lwp_asrs != NULL)
9520Sstevel@tonic-gate 				free(lwp->lwp_asrs);
9530Sstevel@tonic-gate #endif
9540Sstevel@tonic-gate 			free(lwp);
9550Sstevel@tonic-gate 		}
9560Sstevel@tonic-gate 
9570Sstevel@tonic-gate 		if (P->core->core_platform != NULL)
9580Sstevel@tonic-gate 			free(P->core->core_platform);
9590Sstevel@tonic-gate 		if (P->core->core_uts != NULL)
9600Sstevel@tonic-gate 			free(P->core->core_uts);
9610Sstevel@tonic-gate 		if (P->core->core_cred != NULL)
9620Sstevel@tonic-gate 			free(P->core->core_cred);
9630Sstevel@tonic-gate 		if (P->core->core_priv != NULL)
9640Sstevel@tonic-gate 			free(P->core->core_priv);
9650Sstevel@tonic-gate 		if (P->core->core_privinfo != NULL)
9660Sstevel@tonic-gate 			__priv_free_info(P->core->core_privinfo);
9670Sstevel@tonic-gate 		if (P->core->core_ppii != NULL)
9680Sstevel@tonic-gate 			free(P->core->core_ppii);
9690Sstevel@tonic-gate 		if (P->core->core_zonename != NULL)
9700Sstevel@tonic-gate 			free(P->core->core_zonename);
9710Sstevel@tonic-gate #if defined(__i386) || defined(__amd64)
9720Sstevel@tonic-gate 		if (P->core->core_ldt != NULL)
9730Sstevel@tonic-gate 			free(P->core->core_ldt);
9740Sstevel@tonic-gate #endif
9750Sstevel@tonic-gate 
9760Sstevel@tonic-gate 		free(P->core);
9770Sstevel@tonic-gate 	}
9780Sstevel@tonic-gate 
9790Sstevel@tonic-gate 	if (P->ucaddrs != NULL) {
9800Sstevel@tonic-gate 		free(P->ucaddrs);
9810Sstevel@tonic-gate 		P->ucaddrs = NULL;
9820Sstevel@tonic-gate 		P->ucnelems = 0;
9830Sstevel@tonic-gate 	}
9840Sstevel@tonic-gate 
9850Sstevel@tonic-gate 	(void) mutex_lock(&P->proc_lock);
9860Sstevel@tonic-gate 	if (P->hashtab != NULL) {
9870Sstevel@tonic-gate 		struct ps_lwphandle *L;
9880Sstevel@tonic-gate 		for (i = 0; i < HASHSIZE; i++) {
9890Sstevel@tonic-gate 			while ((L = P->hashtab[i]) != NULL)
9900Sstevel@tonic-gate 				Lfree_internal(P, L);
9910Sstevel@tonic-gate 		}
9920Sstevel@tonic-gate 		free(P->hashtab);
9930Sstevel@tonic-gate 	}
9940Sstevel@tonic-gate 	(void) mutex_unlock(&P->proc_lock);
9950Sstevel@tonic-gate 	(void) mutex_destroy(&P->proc_lock);
9960Sstevel@tonic-gate 
9970Sstevel@tonic-gate 	if (P->agentctlfd >= 0)
9980Sstevel@tonic-gate 		(void) close(P->agentctlfd);
9990Sstevel@tonic-gate 	if (P->agentstatfd >= 0)
10000Sstevel@tonic-gate 		(void) close(P->agentstatfd);
10010Sstevel@tonic-gate 	if (P->ctlfd >= 0)
10020Sstevel@tonic-gate 		(void) close(P->ctlfd);
10030Sstevel@tonic-gate 	if (P->asfd >= 0)
10040Sstevel@tonic-gate 		(void) close(P->asfd);
10050Sstevel@tonic-gate 	if (P->statfd >= 0)
10060Sstevel@tonic-gate 		(void) close(P->statfd);
10070Sstevel@tonic-gate 	Preset_maps(P);
10080Sstevel@tonic-gate 
10090Sstevel@tonic-gate 	/* clear out the structure as a precaution against reuse */
10100Sstevel@tonic-gate 	(void) memset(P, 0, sizeof (*P));
10110Sstevel@tonic-gate 	P->ctlfd = -1;
10120Sstevel@tonic-gate 	P->asfd = -1;
10130Sstevel@tonic-gate 	P->statfd = -1;
10140Sstevel@tonic-gate 	P->agentctlfd = -1;
10150Sstevel@tonic-gate 	P->agentstatfd = -1;
10160Sstevel@tonic-gate 
10170Sstevel@tonic-gate 	free(P);
10180Sstevel@tonic-gate }
10190Sstevel@tonic-gate 
10200Sstevel@tonic-gate /*
10210Sstevel@tonic-gate  * Return the state of the process, one of the PS_* values.
10220Sstevel@tonic-gate  */
10230Sstevel@tonic-gate int
10240Sstevel@tonic-gate Pstate(struct ps_prochandle *P)
10250Sstevel@tonic-gate {
10260Sstevel@tonic-gate 	return (P->state);
10270Sstevel@tonic-gate }
10280Sstevel@tonic-gate 
10290Sstevel@tonic-gate /*
10300Sstevel@tonic-gate  * Return the open address space file descriptor for the process.
10310Sstevel@tonic-gate  * Clients must not close this file descriptor, not use it
10320Sstevel@tonic-gate  * after the process is freed.
10330Sstevel@tonic-gate  */
10340Sstevel@tonic-gate int
10350Sstevel@tonic-gate Pasfd(struct ps_prochandle *P)
10360Sstevel@tonic-gate {
10370Sstevel@tonic-gate 	return (P->asfd);
10380Sstevel@tonic-gate }
10390Sstevel@tonic-gate 
10400Sstevel@tonic-gate /*
10410Sstevel@tonic-gate  * Return the open control file descriptor for the process.
10420Sstevel@tonic-gate  * Clients must not close this file descriptor, not use it
10430Sstevel@tonic-gate  * after the process is freed.
10440Sstevel@tonic-gate  */
10450Sstevel@tonic-gate int
10460Sstevel@tonic-gate Pctlfd(struct ps_prochandle *P)
10470Sstevel@tonic-gate {
10480Sstevel@tonic-gate 	return (P->ctlfd);
10490Sstevel@tonic-gate }
10500Sstevel@tonic-gate 
10510Sstevel@tonic-gate /*
10520Sstevel@tonic-gate  * Return a pointer to the process psinfo structure.
10530Sstevel@tonic-gate  * Clients should not hold on to this pointer indefinitely.
10540Sstevel@tonic-gate  * It will become invalid on Prelease().
10550Sstevel@tonic-gate  */
10560Sstevel@tonic-gate const psinfo_t *
10570Sstevel@tonic-gate Ppsinfo(struct ps_prochandle *P)
10580Sstevel@tonic-gate {
10590Sstevel@tonic-gate 	if (P->state == PS_IDLE) {
10600Sstevel@tonic-gate 		errno = ENODATA;
10610Sstevel@tonic-gate 		return (NULL);
10620Sstevel@tonic-gate 	}
10630Sstevel@tonic-gate 
10640Sstevel@tonic-gate 	if (P->state != PS_DEAD && proc_get_psinfo(P->pid, &P->psinfo) == -1)
10650Sstevel@tonic-gate 		return (NULL);
10660Sstevel@tonic-gate 
10670Sstevel@tonic-gate 	return (&P->psinfo);
10680Sstevel@tonic-gate }
10690Sstevel@tonic-gate 
10700Sstevel@tonic-gate /*
10710Sstevel@tonic-gate  * Return a pointer to the process status structure.
10720Sstevel@tonic-gate  * Clients should not hold on to this pointer indefinitely.
10730Sstevel@tonic-gate  * It will become invalid on Prelease().
10740Sstevel@tonic-gate  */
10750Sstevel@tonic-gate const pstatus_t *
10760Sstevel@tonic-gate Pstatus(struct ps_prochandle *P)
10770Sstevel@tonic-gate {
10780Sstevel@tonic-gate 	return (&P->status);
10790Sstevel@tonic-gate }
10800Sstevel@tonic-gate 
10810Sstevel@tonic-gate /*
10820Sstevel@tonic-gate  * Fill in a pointer to a process credentials structure.  The ngroups parameter
10830Sstevel@tonic-gate  * is the number of supplementary group entries allocated in the caller's cred
10840Sstevel@tonic-gate  * structure.  It should equal zero or one unless extra space has been
10850Sstevel@tonic-gate  * allocated for the group list by the caller.
10860Sstevel@tonic-gate  */
10870Sstevel@tonic-gate int
10880Sstevel@tonic-gate Pcred(struct ps_prochandle *P, prcred_t *pcrp, int ngroups)
10890Sstevel@tonic-gate {
10900Sstevel@tonic-gate 	if (P->state == PS_IDLE) {
10910Sstevel@tonic-gate 		errno = ENODATA;
10920Sstevel@tonic-gate 		return (-1);
10930Sstevel@tonic-gate 	}
10940Sstevel@tonic-gate 
10950Sstevel@tonic-gate 	if (P->state != PS_DEAD)
10960Sstevel@tonic-gate 		return (proc_get_cred(P->pid, pcrp, ngroups));
10970Sstevel@tonic-gate 
10980Sstevel@tonic-gate 	if (P->core->core_cred != NULL) {
10990Sstevel@tonic-gate 		/*
11000Sstevel@tonic-gate 		 * Avoid returning more supplementary group data than the
11010Sstevel@tonic-gate 		 * caller has allocated in their buffer.  We expect them to
11020Sstevel@tonic-gate 		 * check pr_ngroups afterward and potentially call us again.
11030Sstevel@tonic-gate 		 */
11040Sstevel@tonic-gate 		ngroups = MIN(ngroups, P->core->core_cred->pr_ngroups);
11050Sstevel@tonic-gate 
11060Sstevel@tonic-gate 		(void) memcpy(pcrp, P->core->core_cred,
11070Sstevel@tonic-gate 		    sizeof (prcred_t) + (ngroups - 1) * sizeof (gid_t));
11080Sstevel@tonic-gate 
11090Sstevel@tonic-gate 		return (0);
11100Sstevel@tonic-gate 	}
11110Sstevel@tonic-gate 
11120Sstevel@tonic-gate 	errno = ENODATA;
11130Sstevel@tonic-gate 	return (-1);
11140Sstevel@tonic-gate }
11150Sstevel@tonic-gate 
11160Sstevel@tonic-gate #if defined(__i386) || defined(__amd64)
11170Sstevel@tonic-gate /*
11180Sstevel@tonic-gate  * Fill in a pointer to a process LDT structure.
11190Sstevel@tonic-gate  * The caller provides a buffer of size 'nldt * sizeof (struct ssd)';
11200Sstevel@tonic-gate  * If pldt == NULL or nldt == 0, we return the number of existing LDT entries.
11210Sstevel@tonic-gate  * Otherwise we return the actual number of LDT entries fetched (<= nldt).
11220Sstevel@tonic-gate  */
11230Sstevel@tonic-gate int
11240Sstevel@tonic-gate Pldt(struct ps_prochandle *P, struct ssd *pldt, int nldt)
11250Sstevel@tonic-gate {
11260Sstevel@tonic-gate 	if (P->state == PS_IDLE) {
11270Sstevel@tonic-gate 		errno = ENODATA;
11280Sstevel@tonic-gate 		return (-1);
11290Sstevel@tonic-gate 	}
11300Sstevel@tonic-gate 
11310Sstevel@tonic-gate 	if (P->state != PS_DEAD)
11320Sstevel@tonic-gate 		return (proc_get_ldt(P->pid, pldt, nldt));
11330Sstevel@tonic-gate 
11340Sstevel@tonic-gate 	if (pldt == NULL || nldt == 0)
11350Sstevel@tonic-gate 		return (P->core->core_nldt);
11360Sstevel@tonic-gate 
11370Sstevel@tonic-gate 	if (P->core->core_ldt != NULL) {
11380Sstevel@tonic-gate 		nldt = MIN(nldt, P->core->core_nldt);
11390Sstevel@tonic-gate 
11400Sstevel@tonic-gate 		(void) memcpy(pldt, P->core->core_ldt,
11410Sstevel@tonic-gate 		    nldt * sizeof (struct ssd));
11420Sstevel@tonic-gate 
11430Sstevel@tonic-gate 		return (nldt);
11440Sstevel@tonic-gate 	}
11450Sstevel@tonic-gate 
11460Sstevel@tonic-gate 	errno = ENODATA;
11470Sstevel@tonic-gate 	return (-1);
11480Sstevel@tonic-gate }
11490Sstevel@tonic-gate #endif	/* __i386 */
11500Sstevel@tonic-gate 
11510Sstevel@tonic-gate /*
11520Sstevel@tonic-gate  * Fill in a pointer to a process privilege structure.
11530Sstevel@tonic-gate  */
11540Sstevel@tonic-gate ssize_t
11550Sstevel@tonic-gate Ppriv(struct ps_prochandle *P, prpriv_t *pprv, size_t size)
11560Sstevel@tonic-gate {
11570Sstevel@tonic-gate 	if (P->state != PS_DEAD) {
11580Sstevel@tonic-gate 		prpriv_t *pp = proc_get_priv(P->pid);
11590Sstevel@tonic-gate 		if (pp != NULL) {
11600Sstevel@tonic-gate 			size = MIN(size, PRIV_PRPRIV_SIZE(pp));
11610Sstevel@tonic-gate 			(void) memcpy(pprv, pp, size);
11620Sstevel@tonic-gate 			free(pp);
11630Sstevel@tonic-gate 			return (size);
11640Sstevel@tonic-gate 		}
11650Sstevel@tonic-gate 		return (-1);
11660Sstevel@tonic-gate 	}
11670Sstevel@tonic-gate 
11680Sstevel@tonic-gate 	if (P->core->core_priv != NULL) {
11690Sstevel@tonic-gate 		size = MIN(P->core->core_priv_size, size);
11700Sstevel@tonic-gate 		(void) memcpy(pprv, P->core->core_priv, size);
11710Sstevel@tonic-gate 		return (size);
11720Sstevel@tonic-gate 	}
11730Sstevel@tonic-gate 	errno = ENODATA;
11740Sstevel@tonic-gate 	return (-1);
11750Sstevel@tonic-gate }
11760Sstevel@tonic-gate 
11770Sstevel@tonic-gate int
11780Sstevel@tonic-gate Psetpriv(struct ps_prochandle *P, prpriv_t *pprv)
11790Sstevel@tonic-gate {
11800Sstevel@tonic-gate 	int rc;
11810Sstevel@tonic-gate 	long *ctl;
11820Sstevel@tonic-gate 	size_t sz;
11830Sstevel@tonic-gate 
11840Sstevel@tonic-gate 	if (P->state == PS_DEAD) {
11850Sstevel@tonic-gate 		errno = EBADF;
11860Sstevel@tonic-gate 		return (-1);
11870Sstevel@tonic-gate 	}
11880Sstevel@tonic-gate 
11890Sstevel@tonic-gate 	sz = PRIV_PRPRIV_SIZE(pprv) + sizeof (long);
11900Sstevel@tonic-gate 
11910Sstevel@tonic-gate 	sz = ((sz - 1) / sizeof (long) + 1) * sizeof (long);
11920Sstevel@tonic-gate 
11930Sstevel@tonic-gate 	ctl = malloc(sz);
11940Sstevel@tonic-gate 	if (ctl == NULL)
11950Sstevel@tonic-gate 		return (-1);
11960Sstevel@tonic-gate 
11970Sstevel@tonic-gate 	ctl[0] = PCSPRIV;
11980Sstevel@tonic-gate 
11990Sstevel@tonic-gate 	(void) memcpy(&ctl[1], pprv, PRIV_PRPRIV_SIZE(pprv));
12000Sstevel@tonic-gate 
12010Sstevel@tonic-gate 	if (write(P->ctlfd, ctl, sz) != sz)
12020Sstevel@tonic-gate 		rc = -1;
12030Sstevel@tonic-gate 	else
12040Sstevel@tonic-gate 		rc = 0;
12050Sstevel@tonic-gate 
12060Sstevel@tonic-gate 	free(ctl);
12070Sstevel@tonic-gate 
12080Sstevel@tonic-gate 	return (rc);
12090Sstevel@tonic-gate }
12100Sstevel@tonic-gate 
12110Sstevel@tonic-gate void *
12120Sstevel@tonic-gate Pprivinfo(struct ps_prochandle *P)
12130Sstevel@tonic-gate {
12140Sstevel@tonic-gate 	/* Use default from libc */
12150Sstevel@tonic-gate 	if (P->state != PS_DEAD)
12160Sstevel@tonic-gate 		return (NULL);
12170Sstevel@tonic-gate 
12180Sstevel@tonic-gate 	return (P->core->core_privinfo);
12190Sstevel@tonic-gate }
12200Sstevel@tonic-gate 
12210Sstevel@tonic-gate /*
12220Sstevel@tonic-gate  * Ensure that all cached state is written to the process.
12230Sstevel@tonic-gate  * The cached state is the LWP's signal mask and registers
12240Sstevel@tonic-gate  * and the process's tracing flags.
12250Sstevel@tonic-gate  */
12260Sstevel@tonic-gate void
12270Sstevel@tonic-gate Psync(struct ps_prochandle *P)
12280Sstevel@tonic-gate {
12290Sstevel@tonic-gate 	int ctlfd = (P->agentctlfd >= 0)? P->agentctlfd : P->ctlfd;
12300Sstevel@tonic-gate 	long cmd[6];
12310Sstevel@tonic-gate 	iovec_t iov[12];
12320Sstevel@tonic-gate 	int n = 0;
12330Sstevel@tonic-gate 
12340Sstevel@tonic-gate 	if (P->flags & SETHOLD) {
12350Sstevel@tonic-gate 		cmd[0] = PCSHOLD;
12360Sstevel@tonic-gate 		iov[n].iov_base = (caddr_t)&cmd[0];
12370Sstevel@tonic-gate 		iov[n++].iov_len = sizeof (long);
12380Sstevel@tonic-gate 		iov[n].iov_base = (caddr_t)&P->status.pr_lwp.pr_lwphold;
12390Sstevel@tonic-gate 		iov[n++].iov_len = sizeof (P->status.pr_lwp.pr_lwphold);
12400Sstevel@tonic-gate 	}
12410Sstevel@tonic-gate 	if (P->flags & SETREGS) {
12420Sstevel@tonic-gate 		cmd[1] = PCSREG;
12430Sstevel@tonic-gate #ifdef __i386
12440Sstevel@tonic-gate 		/* XX64 we should probably restore REG_GS after this */
12450Sstevel@tonic-gate 		if (ctlfd == P->agentctlfd)
12460Sstevel@tonic-gate 			P->status.pr_lwp.pr_reg[GS] = 0;
12470Sstevel@tonic-gate #elif defined(__amd64)
12480Sstevel@tonic-gate 		/* XX64 */
12490Sstevel@tonic-gate #endif
12500Sstevel@tonic-gate 		iov[n].iov_base = (caddr_t)&cmd[1];
12510Sstevel@tonic-gate 		iov[n++].iov_len = sizeof (long);
12520Sstevel@tonic-gate 		iov[n].iov_base = (caddr_t)&P->status.pr_lwp.pr_reg[0];
12530Sstevel@tonic-gate 		iov[n++].iov_len = sizeof (P->status.pr_lwp.pr_reg);
12540Sstevel@tonic-gate 	}
12550Sstevel@tonic-gate 	if (P->flags & SETSIG) {
12560Sstevel@tonic-gate 		cmd[2] = PCSTRACE;
12570Sstevel@tonic-gate 		iov[n].iov_base = (caddr_t)&cmd[2];
12580Sstevel@tonic-gate 		iov[n++].iov_len = sizeof (long);
12590Sstevel@tonic-gate 		iov[n].iov_base = (caddr_t)&P->status.pr_sigtrace;
12600Sstevel@tonic-gate 		iov[n++].iov_len = sizeof (P->status.pr_sigtrace);
12610Sstevel@tonic-gate 	}
12620Sstevel@tonic-gate 	if (P->flags & SETFAULT) {
12630Sstevel@tonic-gate 		cmd[3] = PCSFAULT;
12640Sstevel@tonic-gate 		iov[n].iov_base = (caddr_t)&cmd[3];
12650Sstevel@tonic-gate 		iov[n++].iov_len = sizeof (long);
12660Sstevel@tonic-gate 		iov[n].iov_base = (caddr_t)&P->status.pr_flttrace;
12670Sstevel@tonic-gate 		iov[n++].iov_len = sizeof (P->status.pr_flttrace);
12680Sstevel@tonic-gate 	}
12690Sstevel@tonic-gate 	if (P->flags & SETENTRY) {
12700Sstevel@tonic-gate 		cmd[4] = PCSENTRY;
12710Sstevel@tonic-gate 		iov[n].iov_base = (caddr_t)&cmd[4];
12720Sstevel@tonic-gate 		iov[n++].iov_len = sizeof (long);
12730Sstevel@tonic-gate 		iov[n].iov_base = (caddr_t)&P->status.pr_sysentry;
12740Sstevel@tonic-gate 		iov[n++].iov_len = sizeof (P->status.pr_sysentry);
12750Sstevel@tonic-gate 	}
12760Sstevel@tonic-gate 	if (P->flags & SETEXIT) {
12770Sstevel@tonic-gate 		cmd[5] = PCSEXIT;
12780Sstevel@tonic-gate 		iov[n].iov_base = (caddr_t)&cmd[5];
12790Sstevel@tonic-gate 		iov[n++].iov_len = sizeof (long);
12800Sstevel@tonic-gate 		iov[n].iov_base = (caddr_t)&P->status.pr_sysexit;
12810Sstevel@tonic-gate 		iov[n++].iov_len = sizeof (P->status.pr_sysexit);
12820Sstevel@tonic-gate 	}
12830Sstevel@tonic-gate 
12840Sstevel@tonic-gate 	if (n == 0 || writev(ctlfd, iov, n) < 0)
12850Sstevel@tonic-gate 		return;		/* nothing to do or write failed */
12860Sstevel@tonic-gate 
12870Sstevel@tonic-gate 	P->flags &= ~(SETSIG|SETFAULT|SETENTRY|SETEXIT|SETHOLD|SETREGS);
12880Sstevel@tonic-gate }
12890Sstevel@tonic-gate 
12900Sstevel@tonic-gate /*
12910Sstevel@tonic-gate  * Reopen the /proc file (after PS_LOST).
12920Sstevel@tonic-gate  */
12930Sstevel@tonic-gate int
12940Sstevel@tonic-gate Preopen(struct ps_prochandle *P)
12950Sstevel@tonic-gate {
12960Sstevel@tonic-gate 	int fd;
12972712Snn35248 	char procname[PATH_MAX];
12980Sstevel@tonic-gate 	char *fname;
12990Sstevel@tonic-gate 
13000Sstevel@tonic-gate 	if (P->state == PS_DEAD || P->state == PS_IDLE)
13010Sstevel@tonic-gate 		return (0);
13020Sstevel@tonic-gate 
13030Sstevel@tonic-gate 	if (P->agentcnt > 0) {
13040Sstevel@tonic-gate 		P->agentcnt = 1;
13050Sstevel@tonic-gate 		Pdestroy_agent(P);
13060Sstevel@tonic-gate 	}
13070Sstevel@tonic-gate 
13082712Snn35248 	(void) snprintf(procname, sizeof (procname), "%s/%d/",
13092712Snn35248 	    procfs_path, (int)P->pid);
13100Sstevel@tonic-gate 	fname = procname + strlen(procname);
13110Sstevel@tonic-gate 
13120Sstevel@tonic-gate 	(void) strcpy(fname, "as");
13130Sstevel@tonic-gate 	if ((fd = open(procname, O_RDWR)) < 0 ||
13140Sstevel@tonic-gate 	    close(P->asfd) < 0 ||
13150Sstevel@tonic-gate 	    (fd = dupfd(fd, P->asfd)) != P->asfd) {
13160Sstevel@tonic-gate 		dprintf("Preopen: failed to open %s: %s\n",
13170Sstevel@tonic-gate 		    procname, strerror(errno));
13180Sstevel@tonic-gate 		if (fd >= 0)
13190Sstevel@tonic-gate 			(void) close(fd);
13200Sstevel@tonic-gate 		return (-1);
13210Sstevel@tonic-gate 	}
13220Sstevel@tonic-gate 	P->asfd = fd;
13230Sstevel@tonic-gate 
13240Sstevel@tonic-gate 	(void) strcpy(fname, "status");
13250Sstevel@tonic-gate 	if ((fd = open(procname, O_RDONLY)) < 0 ||
13260Sstevel@tonic-gate 	    close(P->statfd) < 0 ||
13270Sstevel@tonic-gate 	    (fd = dupfd(fd, P->statfd)) != P->statfd) {
13280Sstevel@tonic-gate 		dprintf("Preopen: failed to open %s: %s\n",
13290Sstevel@tonic-gate 		    procname, strerror(errno));
13300Sstevel@tonic-gate 		if (fd >= 0)
13310Sstevel@tonic-gate 			(void) close(fd);
13320Sstevel@tonic-gate 		return (-1);
13330Sstevel@tonic-gate 	}
13340Sstevel@tonic-gate 	P->statfd = fd;
13350Sstevel@tonic-gate 
13360Sstevel@tonic-gate 	(void) strcpy(fname, "ctl");
13370Sstevel@tonic-gate 	if ((fd = open(procname, O_WRONLY)) < 0 ||
13380Sstevel@tonic-gate 	    close(P->ctlfd) < 0 ||
13390Sstevel@tonic-gate 	    (fd = dupfd(fd, P->ctlfd)) != P->ctlfd) {
13400Sstevel@tonic-gate 		dprintf("Preopen: failed to open %s: %s\n",
13410Sstevel@tonic-gate 		    procname, strerror(errno));
13420Sstevel@tonic-gate 		if (fd >= 0)
13430Sstevel@tonic-gate 			(void) close(fd);
13440Sstevel@tonic-gate 		return (-1);
13450Sstevel@tonic-gate 	}
13460Sstevel@tonic-gate 	P->ctlfd = fd;
13470Sstevel@tonic-gate 
13480Sstevel@tonic-gate 	/*
13490Sstevel@tonic-gate 	 * Set the state to PS_RUN and wait for the process to stop so that
13500Sstevel@tonic-gate 	 * we re-read the status from the new P->statfd.  If this fails, Pwait
13510Sstevel@tonic-gate 	 * will reset the state to PS_LOST and we fail the reopen.  Before
13520Sstevel@tonic-gate 	 * returning, we also forge a bit of P->status to allow the debugger to
13530Sstevel@tonic-gate 	 * see that we are PS_LOST following a successful exec.
13540Sstevel@tonic-gate 	 */
13550Sstevel@tonic-gate 	P->state = PS_RUN;
13560Sstevel@tonic-gate 	if (Pwait(P, 0) == -1) {
13570Sstevel@tonic-gate #ifdef _ILP32
13580Sstevel@tonic-gate 		if (errno == EOVERFLOW)
13590Sstevel@tonic-gate 			P->status.pr_dmodel = PR_MODEL_LP64;
13600Sstevel@tonic-gate #endif
13610Sstevel@tonic-gate 		P->status.pr_lwp.pr_why = PR_SYSEXIT;
13620Sstevel@tonic-gate 		P->status.pr_lwp.pr_what = SYS_execve;
13630Sstevel@tonic-gate 		P->status.pr_lwp.pr_errno = 0;
13640Sstevel@tonic-gate 		return (-1);
13650Sstevel@tonic-gate 	}
13660Sstevel@tonic-gate 
13670Sstevel@tonic-gate 	/*
13680Sstevel@tonic-gate 	 * The process should be stopped on exec (REQUESTED)
13690Sstevel@tonic-gate 	 * or else should be stopped on exit from exec() (SYSEXIT)
13700Sstevel@tonic-gate 	 */
13710Sstevel@tonic-gate 	if (P->state == PS_STOP &&
13720Sstevel@tonic-gate 	    (P->status.pr_lwp.pr_why == PR_REQUESTED ||
13730Sstevel@tonic-gate 	    (P->status.pr_lwp.pr_why == PR_SYSEXIT &&
13740Sstevel@tonic-gate 	    (P->status.pr_lwp.pr_what == SYS_exec ||
13750Sstevel@tonic-gate 	    P->status.pr_lwp.pr_what == SYS_execve)))) {
13760Sstevel@tonic-gate 		/* fake up stop-on-exit-from-execve */
13770Sstevel@tonic-gate 		if (P->status.pr_lwp.pr_why == PR_REQUESTED) {
13780Sstevel@tonic-gate 			P->status.pr_lwp.pr_why = PR_SYSEXIT;
13790Sstevel@tonic-gate 			P->status.pr_lwp.pr_what = SYS_execve;
13800Sstevel@tonic-gate 			P->status.pr_lwp.pr_errno = 0;
13810Sstevel@tonic-gate 		}
13820Sstevel@tonic-gate 	} else {
13830Sstevel@tonic-gate 		dprintf("Preopen: expected REQUESTED or "
13840Sstevel@tonic-gate 		    "SYSEXIT(SYS_execve) stop\n");
13850Sstevel@tonic-gate 	}
13860Sstevel@tonic-gate 
13870Sstevel@tonic-gate 	return (0);
13880Sstevel@tonic-gate }
13890Sstevel@tonic-gate 
13900Sstevel@tonic-gate /*
13910Sstevel@tonic-gate  * Define all settable flags other than the microstate accounting flags.
13920Sstevel@tonic-gate  */
13930Sstevel@tonic-gate #define	ALL_SETTABLE_FLAGS (PR_FORK|PR_RLC|PR_KLC|PR_ASYNC|PR_BPTADJ|PR_PTRACE)
13940Sstevel@tonic-gate 
13950Sstevel@tonic-gate /*
13960Sstevel@tonic-gate  * Restore /proc tracing flags to their original values
13970Sstevel@tonic-gate  * in preparation for releasing the process.
13980Sstevel@tonic-gate  * Also called by Pcreate() to clear all tracing flags.
13990Sstevel@tonic-gate  */
14000Sstevel@tonic-gate static void
14010Sstevel@tonic-gate restore_tracing_flags(struct ps_prochandle *P)
14020Sstevel@tonic-gate {
14030Sstevel@tonic-gate 	long flags;
14040Sstevel@tonic-gate 	long cmd[4];
14050Sstevel@tonic-gate 	iovec_t iov[8];
14060Sstevel@tonic-gate 
14070Sstevel@tonic-gate 	if (P->flags & CREATED) {
14080Sstevel@tonic-gate 		/* we created this process; clear all tracing flags */
14090Sstevel@tonic-gate 		premptyset(&P->status.pr_sigtrace);
14100Sstevel@tonic-gate 		premptyset(&P->status.pr_flttrace);
14110Sstevel@tonic-gate 		premptyset(&P->status.pr_sysentry);
14120Sstevel@tonic-gate 		premptyset(&P->status.pr_sysexit);
14130Sstevel@tonic-gate 		if ((P->status.pr_flags & ALL_SETTABLE_FLAGS) != 0)
14140Sstevel@tonic-gate 			(void) Punsetflags(P, ALL_SETTABLE_FLAGS);
14150Sstevel@tonic-gate 	} else {
14160Sstevel@tonic-gate 		/* we grabbed the process; restore its tracing flags */
14170Sstevel@tonic-gate 		P->status.pr_sigtrace = P->orig_status.pr_sigtrace;
14180Sstevel@tonic-gate 		P->status.pr_flttrace = P->orig_status.pr_flttrace;
14190Sstevel@tonic-gate 		P->status.pr_sysentry = P->orig_status.pr_sysentry;
14200Sstevel@tonic-gate 		P->status.pr_sysexit  = P->orig_status.pr_sysexit;
14210Sstevel@tonic-gate 		if ((P->status.pr_flags & ALL_SETTABLE_FLAGS) !=
14220Sstevel@tonic-gate 		    (flags = (P->orig_status.pr_flags & ALL_SETTABLE_FLAGS))) {
14230Sstevel@tonic-gate 			(void) Punsetflags(P, ALL_SETTABLE_FLAGS);
14240Sstevel@tonic-gate 			if (flags)
14250Sstevel@tonic-gate 				(void) Psetflags(P, flags);
14260Sstevel@tonic-gate 		}
14270Sstevel@tonic-gate 	}
14280Sstevel@tonic-gate 
14290Sstevel@tonic-gate 	cmd[0] = PCSTRACE;
14300Sstevel@tonic-gate 	iov[0].iov_base = (caddr_t)&cmd[0];
14310Sstevel@tonic-gate 	iov[0].iov_len = sizeof (long);
14320Sstevel@tonic-gate 	iov[1].iov_base = (caddr_t)&P->status.pr_sigtrace;
14330Sstevel@tonic-gate 	iov[1].iov_len = sizeof (P->status.pr_sigtrace);
14340Sstevel@tonic-gate 
14350Sstevel@tonic-gate 	cmd[1] = PCSFAULT;
14360Sstevel@tonic-gate 	iov[2].iov_base = (caddr_t)&cmd[1];
14370Sstevel@tonic-gate 	iov[2].iov_len = sizeof (long);
14380Sstevel@tonic-gate 	iov[3].iov_base = (caddr_t)&P->status.pr_flttrace;
14390Sstevel@tonic-gate 	iov[3].iov_len = sizeof (P->status.pr_flttrace);
14400Sstevel@tonic-gate 
14410Sstevel@tonic-gate 	cmd[2] = PCSENTRY;
14420Sstevel@tonic-gate 	iov[4].iov_base = (caddr_t)&cmd[2];
14430Sstevel@tonic-gate 	iov[4].iov_len = sizeof (long);
14440Sstevel@tonic-gate 	iov[5].iov_base = (caddr_t)&P->status.pr_sysentry;
14450Sstevel@tonic-gate 	iov[5].iov_len = sizeof (P->status.pr_sysentry);
14460Sstevel@tonic-gate 
14470Sstevel@tonic-gate 	cmd[3] = PCSEXIT;
14480Sstevel@tonic-gate 	iov[6].iov_base = (caddr_t)&cmd[3];
14490Sstevel@tonic-gate 	iov[6].iov_len = sizeof (long);
14500Sstevel@tonic-gate 	iov[7].iov_base = (caddr_t)&P->status.pr_sysexit;
14510Sstevel@tonic-gate 	iov[7].iov_len = sizeof (P->status.pr_sysexit);
14520Sstevel@tonic-gate 
14530Sstevel@tonic-gate 	(void) writev(P->ctlfd, iov, 8);
14540Sstevel@tonic-gate 
14550Sstevel@tonic-gate 	P->flags &= ~(SETSIG|SETFAULT|SETENTRY|SETEXIT);
14560Sstevel@tonic-gate }
14570Sstevel@tonic-gate 
14580Sstevel@tonic-gate /*
14590Sstevel@tonic-gate  * Release the process.  Frees the process control structure.
14600Sstevel@tonic-gate  * flags:
14610Sstevel@tonic-gate  *	PRELEASE_CLEAR	Clear all tracing flags.
14620Sstevel@tonic-gate  *	PRELEASE_RETAIN	Retain current tracing flags.
14630Sstevel@tonic-gate  *	PRELEASE_HANG	Leave the process stopped and abandoned.
14640Sstevel@tonic-gate  *	PRELEASE_KILL	Terminate the process with SIGKILL.
14650Sstevel@tonic-gate  */
14660Sstevel@tonic-gate void
14670Sstevel@tonic-gate Prelease(struct ps_prochandle *P, int flags)
14680Sstevel@tonic-gate {
14690Sstevel@tonic-gate 	if (P->state == PS_DEAD) {
14700Sstevel@tonic-gate 		dprintf("Prelease: releasing handle %p PS_DEAD of pid %d\n",
14710Sstevel@tonic-gate 		    (void *)P, (int)P->pid);
14720Sstevel@tonic-gate 		Pfree(P);
14730Sstevel@tonic-gate 		return;
14740Sstevel@tonic-gate 	}
14750Sstevel@tonic-gate 
14760Sstevel@tonic-gate 	if (P->state == PS_IDLE) {
14770Sstevel@tonic-gate 		file_info_t *fptr = list_next(&P->file_head);
14780Sstevel@tonic-gate 		dprintf("Prelease: releasing handle %p PS_IDLE of file %s\n",
14790Sstevel@tonic-gate 		    (void *)P, fptr->file_pname);
14800Sstevel@tonic-gate 		Pfree(P);
14810Sstevel@tonic-gate 		return;
14820Sstevel@tonic-gate 	}
14830Sstevel@tonic-gate 
14840Sstevel@tonic-gate 	dprintf("Prelease: releasing handle %p pid %d\n",
14850Sstevel@tonic-gate 	    (void *)P, (int)P->pid);
14860Sstevel@tonic-gate 
14870Sstevel@tonic-gate 	if (P->ctlfd == -1) {
14880Sstevel@tonic-gate 		Pfree(P);
14890Sstevel@tonic-gate 		return;
14900Sstevel@tonic-gate 	}
14910Sstevel@tonic-gate 
14920Sstevel@tonic-gate 	if (P->agentcnt > 0) {
14930Sstevel@tonic-gate 		P->agentcnt = 1;
14940Sstevel@tonic-gate 		Pdestroy_agent(P);
14950Sstevel@tonic-gate 	}
14960Sstevel@tonic-gate 
14970Sstevel@tonic-gate 	/*
14980Sstevel@tonic-gate 	 * Attempt to stop the process.
14990Sstevel@tonic-gate 	 */
15000Sstevel@tonic-gate 	P->state = PS_RUN;
15010Sstevel@tonic-gate 	(void) Pstop(P, 1000);
15020Sstevel@tonic-gate 
15030Sstevel@tonic-gate 	if (flags & PRELEASE_KILL) {
15040Sstevel@tonic-gate 		if (P->state == PS_STOP)
15050Sstevel@tonic-gate 			(void) Psetrun(P, SIGKILL, 0);
15060Sstevel@tonic-gate 		(void) kill(P->pid, SIGKILL);
15070Sstevel@tonic-gate 		Pfree(P);
15080Sstevel@tonic-gate 		return;
15090Sstevel@tonic-gate 	}
15100Sstevel@tonic-gate 
15110Sstevel@tonic-gate 	/*
15120Sstevel@tonic-gate 	 * If we lost control, all we can do now is close the files.
15130Sstevel@tonic-gate 	 * In this case, the last close sets the process running.
15140Sstevel@tonic-gate 	 */
15150Sstevel@tonic-gate 	if (P->state != PS_STOP &&
15160Sstevel@tonic-gate 	    (P->status.pr_lwp.pr_flags & (PR_ISTOP|PR_DSTOP)) == 0) {
15170Sstevel@tonic-gate 		Pfree(P);
15180Sstevel@tonic-gate 		return;
15190Sstevel@tonic-gate 	}
15200Sstevel@tonic-gate 
15210Sstevel@tonic-gate 	/*
15220Sstevel@tonic-gate 	 * We didn't lose control; we do more.
15230Sstevel@tonic-gate 	 */
15240Sstevel@tonic-gate 	Psync(P);
15250Sstevel@tonic-gate 
15260Sstevel@tonic-gate 	if (flags & PRELEASE_CLEAR)
15270Sstevel@tonic-gate 		P->flags |= CREATED;
15280Sstevel@tonic-gate 
15290Sstevel@tonic-gate 	if (!(flags & PRELEASE_RETAIN))
15300Sstevel@tonic-gate 		restore_tracing_flags(P);
15310Sstevel@tonic-gate 
15320Sstevel@tonic-gate 	if (flags & PRELEASE_HANG) {
15330Sstevel@tonic-gate 		/* Leave the process stopped and abandoned */
15340Sstevel@tonic-gate 		(void) Punsetflags(P, PR_RLC|PR_KLC);
15350Sstevel@tonic-gate 		Pfree(P);
15360Sstevel@tonic-gate 		return;
15370Sstevel@tonic-gate 	}
15380Sstevel@tonic-gate 
15390Sstevel@tonic-gate 	/*
15400Sstevel@tonic-gate 	 * Set the process running if we created it or if it was
15410Sstevel@tonic-gate 	 * not originally stopped or directed to stop via /proc
15420Sstevel@tonic-gate 	 * or if we were given the PRELEASE_CLEAR flag.
15430Sstevel@tonic-gate 	 */
15440Sstevel@tonic-gate 	if ((P->flags & CREATED) ||
15450Sstevel@tonic-gate 	    (P->orig_status.pr_lwp.pr_flags & (PR_ISTOP|PR_DSTOP)) == 0) {
15460Sstevel@tonic-gate 		(void) Psetflags(P, PR_RLC);
15470Sstevel@tonic-gate 		/*
15480Sstevel@tonic-gate 		 * We do this repeatedly because the process may have
15490Sstevel@tonic-gate 		 * more than one LWP stopped on an event of interest.
15500Sstevel@tonic-gate 		 * This makes sure all of them are set running.
15510Sstevel@tonic-gate 		 */
15520Sstevel@tonic-gate 		do {
15530Sstevel@tonic-gate 			if (Psetrun(P, 0, 0) == -1 && errno == EBUSY)
15540Sstevel@tonic-gate 				break; /* Agent LWP may be stuck */
15550Sstevel@tonic-gate 		} while (Pstopstatus(P, PCNULL, 0) == 0 &&
15560Sstevel@tonic-gate 		    P->status.pr_lwp.pr_flags & (PR_ISTOP|PR_DSTOP));
15570Sstevel@tonic-gate 
15580Sstevel@tonic-gate 		if (P->status.pr_lwp.pr_flags & (PR_ISTOP|PR_DSTOP))
15590Sstevel@tonic-gate 			dprintf("Prelease: failed to set process running\n");
15600Sstevel@tonic-gate 	}
15610Sstevel@tonic-gate 
15620Sstevel@tonic-gate 	Pfree(P);
15630Sstevel@tonic-gate }
15640Sstevel@tonic-gate 
15650Sstevel@tonic-gate /* debugging */
15660Sstevel@tonic-gate void
15670Sstevel@tonic-gate prldump(const char *caller, lwpstatus_t *lsp)
15680Sstevel@tonic-gate {
15690Sstevel@tonic-gate 	char name[32];
15700Sstevel@tonic-gate 	uint32_t bits;
15710Sstevel@tonic-gate 
15720Sstevel@tonic-gate 	switch (lsp->pr_why) {
15730Sstevel@tonic-gate 	case PR_REQUESTED:
15740Sstevel@tonic-gate 		dprintf("%s: REQUESTED\n", caller);
15750Sstevel@tonic-gate 		break;
15760Sstevel@tonic-gate 	case PR_SIGNALLED:
15770Sstevel@tonic-gate 		dprintf("%s: SIGNALLED %s\n", caller,
15784753Srh87107 		    proc_signame(lsp->pr_what, name, sizeof (name)));
15790Sstevel@tonic-gate 		break;
15800Sstevel@tonic-gate 	case PR_FAULTED:
15810Sstevel@tonic-gate 		dprintf("%s: FAULTED %s\n", caller,
15824753Srh87107 		    proc_fltname(lsp->pr_what, name, sizeof (name)));
15830Sstevel@tonic-gate 		break;
15840Sstevel@tonic-gate 	case PR_SYSENTRY:
15850Sstevel@tonic-gate 		dprintf("%s: SYSENTRY %s\n", caller,
15864753Srh87107 		    proc_sysname(lsp->pr_what, name, sizeof (name)));
15870Sstevel@tonic-gate 		break;
15880Sstevel@tonic-gate 	case PR_SYSEXIT:
15890Sstevel@tonic-gate 		dprintf("%s: SYSEXIT %s\n", caller,
15904753Srh87107 		    proc_sysname(lsp->pr_what, name, sizeof (name)));
15910Sstevel@tonic-gate 		break;
15920Sstevel@tonic-gate 	case PR_JOBCONTROL:
15930Sstevel@tonic-gate 		dprintf("%s: JOBCONTROL %s\n", caller,
15944753Srh87107 		    proc_signame(lsp->pr_what, name, sizeof (name)));
15950Sstevel@tonic-gate 		break;
15960Sstevel@tonic-gate 	case PR_SUSPENDED:
15970Sstevel@tonic-gate 		dprintf("%s: SUSPENDED\n", caller);
15980Sstevel@tonic-gate 		break;
15990Sstevel@tonic-gate 	default:
16000Sstevel@tonic-gate 		dprintf("%s: Unknown\n", caller);
16010Sstevel@tonic-gate 		break;
16020Sstevel@tonic-gate 	}
16030Sstevel@tonic-gate 
16040Sstevel@tonic-gate 	if (lsp->pr_cursig)
16050Sstevel@tonic-gate 		dprintf("%s: p_cursig  = %d\n", caller, lsp->pr_cursig);
16060Sstevel@tonic-gate 
16070Sstevel@tonic-gate 	bits = *((uint32_t *)&lsp->pr_lwppend);
16080Sstevel@tonic-gate 	if (bits)
16090Sstevel@tonic-gate 		dprintf("%s: pr_lwppend = 0x%.8X\n", caller, bits);
16100Sstevel@tonic-gate }
16110Sstevel@tonic-gate 
16120Sstevel@tonic-gate /* debugging */
16130Sstevel@tonic-gate static void
16140Sstevel@tonic-gate prdump(struct ps_prochandle *P)
16150Sstevel@tonic-gate {
16160Sstevel@tonic-gate 	uint32_t bits;
16170Sstevel@tonic-gate 
16180Sstevel@tonic-gate 	prldump("Pstopstatus", &P->status.pr_lwp);
16190Sstevel@tonic-gate 
16200Sstevel@tonic-gate 	bits = *((uint32_t *)&P->status.pr_sigpend);
16210Sstevel@tonic-gate 	if (bits)
16220Sstevel@tonic-gate 		dprintf("Pstopstatus: pr_sigpend = 0x%.8X\n", bits);
16230Sstevel@tonic-gate }
16240Sstevel@tonic-gate 
16250Sstevel@tonic-gate /*
16260Sstevel@tonic-gate  * Wait for the specified process to stop or terminate.
16270Sstevel@tonic-gate  * Or, just get the current status (PCNULL).
16280Sstevel@tonic-gate  * Or, direct it to stop and get the current status (PCDSTOP).
16290Sstevel@tonic-gate  * If the agent LWP exists, do these things to the agent,
16300Sstevel@tonic-gate  * else do these things to the process as a whole.
16310Sstevel@tonic-gate  */
16320Sstevel@tonic-gate int
16330Sstevel@tonic-gate Pstopstatus(struct ps_prochandle *P,
16340Sstevel@tonic-gate 	long request,		/* PCNULL, PCDSTOP, PCSTOP, PCWSTOP */
16350Sstevel@tonic-gate 	uint_t msec)		/* if non-zero, timeout in milliseconds */
16360Sstevel@tonic-gate {
16370Sstevel@tonic-gate 	int ctlfd = (P->agentctlfd >= 0)? P->agentctlfd : P->ctlfd;
16380Sstevel@tonic-gate 	long ctl[3];
16390Sstevel@tonic-gate 	ssize_t rc;
16400Sstevel@tonic-gate 	int err;
16410Sstevel@tonic-gate 	int old_state = P->state;
16420Sstevel@tonic-gate 
16430Sstevel@tonic-gate 	switch (P->state) {
16440Sstevel@tonic-gate 	case PS_RUN:
16450Sstevel@tonic-gate 		break;
16460Sstevel@tonic-gate 	case PS_STOP:
16470Sstevel@tonic-gate 		if (request != PCNULL && request != PCDSTOP)
16480Sstevel@tonic-gate 			return (0);
16490Sstevel@tonic-gate 		break;
16500Sstevel@tonic-gate 	case PS_LOST:
16510Sstevel@tonic-gate 		if (request != PCNULL) {
16520Sstevel@tonic-gate 			errno = EAGAIN;
16530Sstevel@tonic-gate 			return (-1);
16540Sstevel@tonic-gate 		}
16550Sstevel@tonic-gate 		break;
16560Sstevel@tonic-gate 	case PS_UNDEAD:
16570Sstevel@tonic-gate 	case PS_DEAD:
16580Sstevel@tonic-gate 	case PS_IDLE:
16590Sstevel@tonic-gate 		if (request != PCNULL) {
16600Sstevel@tonic-gate 			errno = ENOENT;
16610Sstevel@tonic-gate 			return (-1);
16620Sstevel@tonic-gate 		}
16630Sstevel@tonic-gate 		break;
16640Sstevel@tonic-gate 	default:	/* corrupted state */
16650Sstevel@tonic-gate 		dprintf("Pstopstatus: corrupted state: %d\n", P->state);
16660Sstevel@tonic-gate 		errno = EINVAL;
16670Sstevel@tonic-gate 		return (-1);
16680Sstevel@tonic-gate 	}
16690Sstevel@tonic-gate 
16700Sstevel@tonic-gate 	ctl[0] = PCDSTOP;
16710Sstevel@tonic-gate 	ctl[1] = PCTWSTOP;
16720Sstevel@tonic-gate 	ctl[2] = (long)msec;
16730Sstevel@tonic-gate 	rc = 0;
16740Sstevel@tonic-gate 	switch (request) {
16750Sstevel@tonic-gate 	case PCSTOP:
16760Sstevel@tonic-gate 		rc = write(ctlfd, &ctl[0], 3*sizeof (long));
16770Sstevel@tonic-gate 		break;
16780Sstevel@tonic-gate 	case PCWSTOP:
16790Sstevel@tonic-gate 		rc = write(ctlfd, &ctl[1], 2*sizeof (long));
16800Sstevel@tonic-gate 		break;
16810Sstevel@tonic-gate 	case PCDSTOP:
16820Sstevel@tonic-gate 		rc = write(ctlfd, &ctl[0], 1*sizeof (long));
16830Sstevel@tonic-gate 		break;
16840Sstevel@tonic-gate 	case PCNULL:
16850Sstevel@tonic-gate 		if (P->state == PS_DEAD || P->state == PS_IDLE)
16860Sstevel@tonic-gate 			return (0);
16870Sstevel@tonic-gate 		break;
16880Sstevel@tonic-gate 	default:	/* programming error */
16890Sstevel@tonic-gate 		errno = EINVAL;
16900Sstevel@tonic-gate 		return (-1);
16910Sstevel@tonic-gate 	}
16920Sstevel@tonic-gate 	err = (rc < 0)? errno : 0;
16930Sstevel@tonic-gate 	Psync(P);
16940Sstevel@tonic-gate 
16950Sstevel@tonic-gate 	if (P->agentstatfd < 0) {
16960Sstevel@tonic-gate 		if (pread(P->statfd, &P->status,
16970Sstevel@tonic-gate 		    sizeof (P->status), (off_t)0) < 0)
16980Sstevel@tonic-gate 			err = errno;
16990Sstevel@tonic-gate 	} else {
17000Sstevel@tonic-gate 		if (pread(P->agentstatfd, &P->status.pr_lwp,
17010Sstevel@tonic-gate 		    sizeof (P->status.pr_lwp), (off_t)0) < 0)
17020Sstevel@tonic-gate 			err = errno;
17030Sstevel@tonic-gate 		P->status.pr_flags = P->status.pr_lwp.pr_flags;
17040Sstevel@tonic-gate 	}
17050Sstevel@tonic-gate 
17060Sstevel@tonic-gate 	if (err) {
17070Sstevel@tonic-gate 		switch (err) {
17080Sstevel@tonic-gate 		case EINTR:		/* user typed ctl-C */
17090Sstevel@tonic-gate 		case ERESTART:
17100Sstevel@tonic-gate 			dprintf("Pstopstatus: EINTR\n");
17110Sstevel@tonic-gate 			break;
17120Sstevel@tonic-gate 		case EAGAIN:		/* we lost control of the the process */
17130Sstevel@tonic-gate 		case EOVERFLOW:
17140Sstevel@tonic-gate 			dprintf("Pstopstatus: PS_LOST, errno=%d\n", err);
17150Sstevel@tonic-gate 			P->state = PS_LOST;
17160Sstevel@tonic-gate 			break;
17170Sstevel@tonic-gate 		default:		/* check for dead process */
17180Sstevel@tonic-gate 			if (_libproc_debug) {
17190Sstevel@tonic-gate 				const char *errstr;
17200Sstevel@tonic-gate 
17210Sstevel@tonic-gate 				switch (request) {
17220Sstevel@tonic-gate 				case PCNULL:
17230Sstevel@tonic-gate 					errstr = "Pstopstatus PCNULL"; break;
17240Sstevel@tonic-gate 				case PCSTOP:
17250Sstevel@tonic-gate 					errstr = "Pstopstatus PCSTOP"; break;
17260Sstevel@tonic-gate 				case PCDSTOP:
17270Sstevel@tonic-gate 					errstr = "Pstopstatus PCDSTOP"; break;
17280Sstevel@tonic-gate 				case PCWSTOP:
17290Sstevel@tonic-gate 					errstr = "Pstopstatus PCWSTOP"; break;
17300Sstevel@tonic-gate 				default:
17310Sstevel@tonic-gate 					errstr = "Pstopstatus PC???"; break;
17320Sstevel@tonic-gate 				}
17330Sstevel@tonic-gate 				dprintf("%s: %s\n", errstr, strerror(err));
17340Sstevel@tonic-gate 			}
17350Sstevel@tonic-gate 			deadcheck(P);
17360Sstevel@tonic-gate 			break;
17370Sstevel@tonic-gate 		}
17380Sstevel@tonic-gate 		if (err != EINTR && err != ERESTART) {
17390Sstevel@tonic-gate 			errno = err;
17400Sstevel@tonic-gate 			return (-1);
17410Sstevel@tonic-gate 		}
17420Sstevel@tonic-gate 	}
17430Sstevel@tonic-gate 
17440Sstevel@tonic-gate 	if (!(P->status.pr_flags & PR_STOPPED)) {
17450Sstevel@tonic-gate 		P->state = PS_RUN;
17460Sstevel@tonic-gate 		if (request == PCNULL || request == PCDSTOP || msec != 0)
17470Sstevel@tonic-gate 			return (0);
17480Sstevel@tonic-gate 		dprintf("Pstopstatus: process is not stopped\n");
17490Sstevel@tonic-gate 		errno = EPROTO;
17500Sstevel@tonic-gate 		return (-1);
17510Sstevel@tonic-gate 	}
17520Sstevel@tonic-gate 
17530Sstevel@tonic-gate 	P->state = PS_STOP;
17540Sstevel@tonic-gate 
17550Sstevel@tonic-gate 	if (_libproc_debug)	/* debugging */
17560Sstevel@tonic-gate 		prdump(P);
17570Sstevel@tonic-gate 
17580Sstevel@tonic-gate 	/*
17590Sstevel@tonic-gate 	 * If the process was already stopped coming into Pstopstatus(),
17600Sstevel@tonic-gate 	 * then don't use its PC to set P->sysaddr since it may have been
17610Sstevel@tonic-gate 	 * changed since the time the process originally stopped.
17620Sstevel@tonic-gate 	 */
17630Sstevel@tonic-gate 	if (old_state == PS_STOP)
17640Sstevel@tonic-gate 		return (0);
17650Sstevel@tonic-gate 
17660Sstevel@tonic-gate 	switch (P->status.pr_lwp.pr_why) {
17670Sstevel@tonic-gate 	case PR_SYSENTRY:
17680Sstevel@tonic-gate 	case PR_SYSEXIT:
17690Sstevel@tonic-gate 		if (Pissyscall_prev(P, P->status.pr_lwp.pr_reg[R_PC],
17700Sstevel@tonic-gate 		    &P->sysaddr) == 0)
17710Sstevel@tonic-gate 			P->sysaddr = P->status.pr_lwp.pr_reg[R_PC];
17720Sstevel@tonic-gate 		break;
17730Sstevel@tonic-gate 	case PR_REQUESTED:
17740Sstevel@tonic-gate 	case PR_SIGNALLED:
17750Sstevel@tonic-gate 	case PR_FAULTED:
17760Sstevel@tonic-gate 	case PR_JOBCONTROL:
17770Sstevel@tonic-gate 	case PR_SUSPENDED:
17780Sstevel@tonic-gate 		break;
17790Sstevel@tonic-gate 	default:
17800Sstevel@tonic-gate 		errno = EPROTO;
17810Sstevel@tonic-gate 		return (-1);
17820Sstevel@tonic-gate 	}
17830Sstevel@tonic-gate 
17840Sstevel@tonic-gate 	return (0);
17850Sstevel@tonic-gate }
17860Sstevel@tonic-gate 
17870Sstevel@tonic-gate /*
17880Sstevel@tonic-gate  * Wait for the process to stop for any reason.
17890Sstevel@tonic-gate  */
17900Sstevel@tonic-gate int
17910Sstevel@tonic-gate Pwait(struct ps_prochandle *P, uint_t msec)
17920Sstevel@tonic-gate {
17930Sstevel@tonic-gate 	return (Pstopstatus(P, PCWSTOP, msec));
17940Sstevel@tonic-gate }
17950Sstevel@tonic-gate 
17960Sstevel@tonic-gate /*
17970Sstevel@tonic-gate  * Direct the process to stop; wait for it to stop.
17980Sstevel@tonic-gate  */
17990Sstevel@tonic-gate int
18000Sstevel@tonic-gate Pstop(struct ps_prochandle *P, uint_t msec)
18010Sstevel@tonic-gate {
18020Sstevel@tonic-gate 	return (Pstopstatus(P, PCSTOP, msec));
18030Sstevel@tonic-gate }
18040Sstevel@tonic-gate 
18050Sstevel@tonic-gate /*
18060Sstevel@tonic-gate  * Direct the process to stop; don't wait.
18070Sstevel@tonic-gate  */
18080Sstevel@tonic-gate int
18090Sstevel@tonic-gate Pdstop(struct ps_prochandle *P)
18100Sstevel@tonic-gate {
18110Sstevel@tonic-gate 	return (Pstopstatus(P, PCDSTOP, 0));
18120Sstevel@tonic-gate }
18130Sstevel@tonic-gate 
18140Sstevel@tonic-gate static void
18150Sstevel@tonic-gate deadcheck(struct ps_prochandle *P)
18160Sstevel@tonic-gate {
18170Sstevel@tonic-gate 	int fd;
18180Sstevel@tonic-gate 	void *buf;
18190Sstevel@tonic-gate 	size_t size;
18200Sstevel@tonic-gate 
18210Sstevel@tonic-gate 	if (P->statfd < 0)
18220Sstevel@tonic-gate 		P->state = PS_UNDEAD;
18230Sstevel@tonic-gate 	else {
18240Sstevel@tonic-gate 		if (P->agentstatfd < 0) {
18250Sstevel@tonic-gate 			fd = P->statfd;
18260Sstevel@tonic-gate 			buf = &P->status;
18270Sstevel@tonic-gate 			size = sizeof (P->status);
18280Sstevel@tonic-gate 		} else {
18290Sstevel@tonic-gate 			fd = P->agentstatfd;
18300Sstevel@tonic-gate 			buf = &P->status.pr_lwp;
18310Sstevel@tonic-gate 			size = sizeof (P->status.pr_lwp);
18320Sstevel@tonic-gate 		}
18330Sstevel@tonic-gate 		while (pread(fd, buf, size, (off_t)0) != size) {
18340Sstevel@tonic-gate 			switch (errno) {
18350Sstevel@tonic-gate 			default:
18360Sstevel@tonic-gate 				P->state = PS_UNDEAD;
18370Sstevel@tonic-gate 				break;
18380Sstevel@tonic-gate 			case EINTR:
18390Sstevel@tonic-gate 			case ERESTART:
18400Sstevel@tonic-gate 				continue;
18410Sstevel@tonic-gate 			case EAGAIN:
18420Sstevel@tonic-gate 				P->state = PS_LOST;
18430Sstevel@tonic-gate 				break;
18440Sstevel@tonic-gate 			}
18450Sstevel@tonic-gate 			break;
18460Sstevel@tonic-gate 		}
18470Sstevel@tonic-gate 		P->status.pr_flags = P->status.pr_lwp.pr_flags;
18480Sstevel@tonic-gate 	}
18490Sstevel@tonic-gate }
18500Sstevel@tonic-gate 
18510Sstevel@tonic-gate /*
18520Sstevel@tonic-gate  * Get the value of one register from stopped process.
18530Sstevel@tonic-gate  */
18540Sstevel@tonic-gate int
18550Sstevel@tonic-gate Pgetareg(struct ps_prochandle *P, int regno, prgreg_t *preg)
18560Sstevel@tonic-gate {
18570Sstevel@tonic-gate 	if (regno < 0 || regno >= NPRGREG) {
18580Sstevel@tonic-gate 		errno = EINVAL;
18590Sstevel@tonic-gate 		return (-1);
18600Sstevel@tonic-gate 	}
18610Sstevel@tonic-gate 
18620Sstevel@tonic-gate 	if (P->state == PS_IDLE) {
18630Sstevel@tonic-gate 		errno = ENODATA;
18640Sstevel@tonic-gate 		return (-1);
18650Sstevel@tonic-gate 	}
18660Sstevel@tonic-gate 
18670Sstevel@tonic-gate 	if (P->state != PS_STOP && P->state != PS_DEAD) {
18680Sstevel@tonic-gate 		errno = EBUSY;
18690Sstevel@tonic-gate 		return (-1);
18700Sstevel@tonic-gate 	}
18710Sstevel@tonic-gate 
18720Sstevel@tonic-gate 	*preg = P->status.pr_lwp.pr_reg[regno];
18730Sstevel@tonic-gate 	return (0);
18740Sstevel@tonic-gate }
18750Sstevel@tonic-gate 
18760Sstevel@tonic-gate /*
18770Sstevel@tonic-gate  * Put value of one register into stopped process.
18780Sstevel@tonic-gate  */
18790Sstevel@tonic-gate int
18800Sstevel@tonic-gate Pputareg(struct ps_prochandle *P, int regno, prgreg_t reg)
18810Sstevel@tonic-gate {
18820Sstevel@tonic-gate 	if (regno < 0 || regno >= NPRGREG) {
18830Sstevel@tonic-gate 		errno = EINVAL;
18840Sstevel@tonic-gate 		return (-1);
18850Sstevel@tonic-gate 	}
18860Sstevel@tonic-gate 
18870Sstevel@tonic-gate 	if (P->state != PS_STOP) {
18880Sstevel@tonic-gate 		errno = EBUSY;
18890Sstevel@tonic-gate 		return (-1);
18900Sstevel@tonic-gate 	}
18910Sstevel@tonic-gate 
18920Sstevel@tonic-gate 	P->status.pr_lwp.pr_reg[regno] = reg;
18930Sstevel@tonic-gate 	P->flags |= SETREGS;	/* set registers before continuing */
18940Sstevel@tonic-gate 	return (0);
18950Sstevel@tonic-gate }
18960Sstevel@tonic-gate 
18970Sstevel@tonic-gate int
18980Sstevel@tonic-gate Psetrun(struct ps_prochandle *P,
18990Sstevel@tonic-gate 	int sig,	/* signal to pass to process */
19000Sstevel@tonic-gate 	int flags)	/* PRSTEP|PRSABORT|PRSTOP|PRCSIG|PRCFAULT */
19010Sstevel@tonic-gate {
19020Sstevel@tonic-gate 	int ctlfd = (P->agentctlfd >= 0) ? P->agentctlfd : P->ctlfd;
19030Sstevel@tonic-gate 	int sbits = (PR_DSTOP | PR_ISTOP | PR_ASLEEP);
19040Sstevel@tonic-gate 
19050Sstevel@tonic-gate 	long ctl[1 +					/* PCCFAULT	*/
19064753Srh87107 	    1 + sizeof (siginfo_t)/sizeof (long) +	/* PCSSIG/PCCSIG */
19074753Srh87107 	    2 ];					/* PCRUN	*/
19080Sstevel@tonic-gate 
19090Sstevel@tonic-gate 	long *ctlp = ctl;
19100Sstevel@tonic-gate 	size_t size;
19110Sstevel@tonic-gate 
19120Sstevel@tonic-gate 	if (P->state != PS_STOP && (P->status.pr_lwp.pr_flags & sbits) == 0) {
19130Sstevel@tonic-gate 		errno = EBUSY;
19140Sstevel@tonic-gate 		return (-1);
19150Sstevel@tonic-gate 	}
19160Sstevel@tonic-gate 
19170Sstevel@tonic-gate 	Psync(P);	/* flush tracing flags and registers */
19180Sstevel@tonic-gate 
19190Sstevel@tonic-gate 	if (flags & PRCFAULT) {		/* clear current fault */
19200Sstevel@tonic-gate 		*ctlp++ = PCCFAULT;
19210Sstevel@tonic-gate 		flags &= ~PRCFAULT;
19220Sstevel@tonic-gate 	}
19230Sstevel@tonic-gate 
19240Sstevel@tonic-gate 	if (flags & PRCSIG) {		/* clear current signal */
19250Sstevel@tonic-gate 		*ctlp++ = PCCSIG;
19260Sstevel@tonic-gate 		flags &= ~PRCSIG;
19270Sstevel@tonic-gate 	} else if (sig && sig != P->status.pr_lwp.pr_cursig) {
19280Sstevel@tonic-gate 		/* make current signal */
19290Sstevel@tonic-gate 		siginfo_t *infop;
19300Sstevel@tonic-gate 
19310Sstevel@tonic-gate 		*ctlp++ = PCSSIG;
19320Sstevel@tonic-gate 		infop = (siginfo_t *)ctlp;
19330Sstevel@tonic-gate 		(void) memset(infop, 0, sizeof (*infop));
19340Sstevel@tonic-gate 		infop->si_signo = sig;
19350Sstevel@tonic-gate 		ctlp += sizeof (siginfo_t) / sizeof (long);
19360Sstevel@tonic-gate 	}
19370Sstevel@tonic-gate 
19380Sstevel@tonic-gate 	*ctlp++ = PCRUN;
19390Sstevel@tonic-gate 	*ctlp++ = flags;
19400Sstevel@tonic-gate 	size = (char *)ctlp - (char *)ctl;
19410Sstevel@tonic-gate 
19420Sstevel@tonic-gate 	P->info_valid = 0;	/* will need to update map and file info */
19430Sstevel@tonic-gate 
19440Sstevel@tonic-gate 	/*
19450Sstevel@tonic-gate 	 * If we've cached ucontext-list information while we were stopped,
19460Sstevel@tonic-gate 	 * free it now.
19470Sstevel@tonic-gate 	 */
19480Sstevel@tonic-gate 	if (P->ucaddrs != NULL) {
19490Sstevel@tonic-gate 		free(P->ucaddrs);
19500Sstevel@tonic-gate 		P->ucaddrs = NULL;
19510Sstevel@tonic-gate 		P->ucnelems = 0;
19520Sstevel@tonic-gate 	}
19530Sstevel@tonic-gate 
19540Sstevel@tonic-gate 	if (write(ctlfd, ctl, size) != size) {
19550Sstevel@tonic-gate 		/* If it is dead or lost, return the real status, not PS_RUN */
19560Sstevel@tonic-gate 		if (errno == ENOENT || errno == EAGAIN) {
19570Sstevel@tonic-gate 			(void) Pstopstatus(P, PCNULL, 0);
19580Sstevel@tonic-gate 			return (0);
19590Sstevel@tonic-gate 		}
19600Sstevel@tonic-gate 		/* If it is not in a jobcontrol stop, issue an error message */
19610Sstevel@tonic-gate 		if (errno != EBUSY ||
19620Sstevel@tonic-gate 		    P->status.pr_lwp.pr_why != PR_JOBCONTROL) {
19630Sstevel@tonic-gate 			dprintf("Psetrun: %s\n", strerror(errno));
19640Sstevel@tonic-gate 			return (-1);
19650Sstevel@tonic-gate 		}
19660Sstevel@tonic-gate 		/* Otherwise pretend that the job-stopped process is running */
19670Sstevel@tonic-gate 	}
19680Sstevel@tonic-gate 
19690Sstevel@tonic-gate 	P->state = PS_RUN;
19700Sstevel@tonic-gate 	return (0);
19710Sstevel@tonic-gate }
19720Sstevel@tonic-gate 
19730Sstevel@tonic-gate ssize_t
19740Sstevel@tonic-gate Pread(struct ps_prochandle *P,
19750Sstevel@tonic-gate 	void *buf,		/* caller's buffer */
19760Sstevel@tonic-gate 	size_t nbyte,		/* number of bytes to read */
19770Sstevel@tonic-gate 	uintptr_t address)	/* address in process */
19780Sstevel@tonic-gate {
19790Sstevel@tonic-gate 	return (P->ops->p_pread(P, buf, nbyte, address));
19800Sstevel@tonic-gate }
19810Sstevel@tonic-gate 
19820Sstevel@tonic-gate ssize_t
19830Sstevel@tonic-gate Pread_string(struct ps_prochandle *P,
19840Sstevel@tonic-gate 	char *buf, 		/* caller's buffer */
19850Sstevel@tonic-gate 	size_t size,		/* upper limit on bytes to read */
19860Sstevel@tonic-gate 	uintptr_t addr)		/* address in process */
19870Sstevel@tonic-gate {
19880Sstevel@tonic-gate 	enum { STRSZ = 40 };
19890Sstevel@tonic-gate 	char string[STRSZ + 1];
19900Sstevel@tonic-gate 	ssize_t leng = 0;
19910Sstevel@tonic-gate 	int nbyte;
19920Sstevel@tonic-gate 
19930Sstevel@tonic-gate 	if (size < 2) {
19940Sstevel@tonic-gate 		errno = EINVAL;
19950Sstevel@tonic-gate 		return (-1);
19960Sstevel@tonic-gate 	}
19970Sstevel@tonic-gate 
19980Sstevel@tonic-gate 	size--;			/* ensure trailing null fits in buffer */
19990Sstevel@tonic-gate 
20000Sstevel@tonic-gate 	*buf = '\0';
20010Sstevel@tonic-gate 	string[STRSZ] = '\0';
20020Sstevel@tonic-gate 
20030Sstevel@tonic-gate 	for (nbyte = STRSZ; nbyte == STRSZ && leng < size; addr += STRSZ) {
20040Sstevel@tonic-gate 		if ((nbyte = P->ops->p_pread(P, string, STRSZ, addr)) <= 0) {
20050Sstevel@tonic-gate 			buf[leng] = '\0';
20060Sstevel@tonic-gate 			return (leng ? leng : -1);
20070Sstevel@tonic-gate 		}
20080Sstevel@tonic-gate 		if ((nbyte = strlen(string)) > 0) {
20090Sstevel@tonic-gate 			if (leng + nbyte > size)
20100Sstevel@tonic-gate 				nbyte = size - leng;
20110Sstevel@tonic-gate 			(void) strncpy(buf + leng, string, nbyte);
20120Sstevel@tonic-gate 			leng += nbyte;
20130Sstevel@tonic-gate 		}
20140Sstevel@tonic-gate 	}
20150Sstevel@tonic-gate 	buf[leng] = '\0';
20160Sstevel@tonic-gate 	return (leng);
20170Sstevel@tonic-gate }
20180Sstevel@tonic-gate 
20190Sstevel@tonic-gate ssize_t
20200Sstevel@tonic-gate Pwrite(struct ps_prochandle *P,
20210Sstevel@tonic-gate 	const void *buf,	/* caller's buffer */
20220Sstevel@tonic-gate 	size_t nbyte,		/* number of bytes to write */
20230Sstevel@tonic-gate 	uintptr_t address)	/* address in process */
20240Sstevel@tonic-gate {
20250Sstevel@tonic-gate 	return (P->ops->p_pwrite(P, buf, nbyte, address));
20260Sstevel@tonic-gate }
20270Sstevel@tonic-gate 
20280Sstevel@tonic-gate int
20290Sstevel@tonic-gate Pclearsig(struct ps_prochandle *P)
20300Sstevel@tonic-gate {
20310Sstevel@tonic-gate 	int ctlfd = (P->agentctlfd >= 0)? P->agentctlfd : P->ctlfd;
20320Sstevel@tonic-gate 	long ctl = PCCSIG;
20330Sstevel@tonic-gate 
20340Sstevel@tonic-gate 	if (write(ctlfd, &ctl, sizeof (ctl)) != sizeof (ctl))
20350Sstevel@tonic-gate 		return (-1);
20360Sstevel@tonic-gate 	P->status.pr_lwp.pr_cursig = 0;
20370Sstevel@tonic-gate 	return (0);
20380Sstevel@tonic-gate }
20390Sstevel@tonic-gate 
20400Sstevel@tonic-gate int
20410Sstevel@tonic-gate Pclearfault(struct ps_prochandle *P)
20420Sstevel@tonic-gate {
20430Sstevel@tonic-gate 	int ctlfd = (P->agentctlfd >= 0)? P->agentctlfd : P->ctlfd;
20440Sstevel@tonic-gate 	long ctl = PCCFAULT;
20450Sstevel@tonic-gate 
20460Sstevel@tonic-gate 	if (write(ctlfd, &ctl, sizeof (ctl)) != sizeof (ctl))
20470Sstevel@tonic-gate 		return (-1);
20480Sstevel@tonic-gate 	return (0);
20490Sstevel@tonic-gate }
20500Sstevel@tonic-gate 
20510Sstevel@tonic-gate /*
20520Sstevel@tonic-gate  * Set a breakpoint trap, return original instruction.
20530Sstevel@tonic-gate  */
20540Sstevel@tonic-gate int
20550Sstevel@tonic-gate Psetbkpt(struct ps_prochandle *P, uintptr_t address, ulong_t *saved)
20560Sstevel@tonic-gate {
20570Sstevel@tonic-gate 	long ctl[1 + sizeof (priovec_t) / sizeof (long) +	/* PCREAD */
20584753Srh87107 	    1 + sizeof (priovec_t) / sizeof (long)];	/* PCWRITE */
20590Sstevel@tonic-gate 	long *ctlp = ctl;
20600Sstevel@tonic-gate 	size_t size;
20610Sstevel@tonic-gate 	priovec_t *iovp;
20620Sstevel@tonic-gate 	instr_t bpt = BPT;
20630Sstevel@tonic-gate 	instr_t old;
20640Sstevel@tonic-gate 
20650Sstevel@tonic-gate 	if (P->state == PS_DEAD || P->state == PS_UNDEAD ||
20660Sstevel@tonic-gate 	    P->state == PS_IDLE) {
20670Sstevel@tonic-gate 		errno = ENOENT;
20680Sstevel@tonic-gate 		return (-1);
20690Sstevel@tonic-gate 	}
20700Sstevel@tonic-gate 
20710Sstevel@tonic-gate 	/* fetch the old instruction */
20720Sstevel@tonic-gate 	*ctlp++ = PCREAD;
20730Sstevel@tonic-gate 	iovp = (priovec_t *)ctlp;
20740Sstevel@tonic-gate 	iovp->pio_base = &old;
20750Sstevel@tonic-gate 	iovp->pio_len = sizeof (old);
20760Sstevel@tonic-gate 	iovp->pio_offset = address;
20770Sstevel@tonic-gate 	ctlp += sizeof (priovec_t) / sizeof (long);
20780Sstevel@tonic-gate 
20790Sstevel@tonic-gate 	/* write the BPT instruction */
20800Sstevel@tonic-gate 	*ctlp++ = PCWRITE;
20810Sstevel@tonic-gate 	iovp = (priovec_t *)ctlp;
20820Sstevel@tonic-gate 	iovp->pio_base = &bpt;
20830Sstevel@tonic-gate 	iovp->pio_len = sizeof (bpt);
20840Sstevel@tonic-gate 	iovp->pio_offset = address;
20850Sstevel@tonic-gate 	ctlp += sizeof (priovec_t) / sizeof (long);
20860Sstevel@tonic-gate 
20870Sstevel@tonic-gate 	size = (char *)ctlp - (char *)ctl;
20880Sstevel@tonic-gate 	if (write(P->ctlfd, ctl, size) != size)
20890Sstevel@tonic-gate 		return (-1);
20900Sstevel@tonic-gate 
20910Sstevel@tonic-gate 	/*
20920Sstevel@tonic-gate 	 * Fail if there was already a breakpoint there from another debugger
20930Sstevel@tonic-gate 	 * or DTrace's user-level tracing on x86.
20940Sstevel@tonic-gate 	 */
20951222Smws 	if (old == BPT) {
20961222Smws 		errno = EBUSY;
20971222Smws 		return (-1);
20981222Smws 	}
20990Sstevel@tonic-gate 
21000Sstevel@tonic-gate 	*saved = (ulong_t)old;
21010Sstevel@tonic-gate 	return (0);
21020Sstevel@tonic-gate }
21030Sstevel@tonic-gate 
21040Sstevel@tonic-gate /*
21050Sstevel@tonic-gate  * Restore original instruction where a breakpoint was set.
21060Sstevel@tonic-gate  */
21070Sstevel@tonic-gate int
21080Sstevel@tonic-gate Pdelbkpt(struct ps_prochandle *P, uintptr_t address, ulong_t saved)
21090Sstevel@tonic-gate {
21100Sstevel@tonic-gate 	instr_t old = (instr_t)saved;
21110Sstevel@tonic-gate 	instr_t cur;
21120Sstevel@tonic-gate 
21130Sstevel@tonic-gate 	if (P->state == PS_DEAD || P->state == PS_UNDEAD ||
21140Sstevel@tonic-gate 	    P->state == PS_IDLE) {
21150Sstevel@tonic-gate 		errno = ENOENT;
21160Sstevel@tonic-gate 		return (-1);
21170Sstevel@tonic-gate 	}
21180Sstevel@tonic-gate 
21190Sstevel@tonic-gate 	/*
21200Sstevel@tonic-gate 	 * If the breakpoint instruction we had placed has been overwritten
21210Sstevel@tonic-gate 	 * with a new instruction, then don't try to replace it with the
21220Sstevel@tonic-gate 	 * old instruction. Doing do can cause problems with self-modifying
21230Sstevel@tonic-gate 	 * code -- PLTs for example. If the Pread() fails, we assume that we
21240Sstevel@tonic-gate 	 * should proceed though most likely the Pwrite() will also fail.
21250Sstevel@tonic-gate 	 */
21260Sstevel@tonic-gate 	if (Pread(P, &cur, sizeof (cur), address) == sizeof (cur) &&
21270Sstevel@tonic-gate 	    cur != BPT)
21280Sstevel@tonic-gate 		return (0);
21290Sstevel@tonic-gate 
21300Sstevel@tonic-gate 	if (Pwrite(P, &old, sizeof (old), address) != sizeof (old))
21310Sstevel@tonic-gate 		return (-1);
21320Sstevel@tonic-gate 
21330Sstevel@tonic-gate 	return (0);
21340Sstevel@tonic-gate }
21350Sstevel@tonic-gate 
21360Sstevel@tonic-gate /*
21370Sstevel@tonic-gate  * Common code for Pxecbkpt() and Lxecbkpt().
21380Sstevel@tonic-gate  * Develop the array of requests that will do the job, then
21390Sstevel@tonic-gate  * write them to the specified control file descriptor.
21400Sstevel@tonic-gate  * Return the non-zero errno if the write fails.
21410Sstevel@tonic-gate  */
21420Sstevel@tonic-gate static int
21430Sstevel@tonic-gate execute_bkpt(
21440Sstevel@tonic-gate 	int ctlfd,		/* process or LWP control file descriptor */
21450Sstevel@tonic-gate 	const fltset_t *faultset,	/* current set of traced faults */
21460Sstevel@tonic-gate 	const sigset_t *sigmask,	/* current signal mask */
21470Sstevel@tonic-gate 	uintptr_t address,		/* address of breakpint */
21480Sstevel@tonic-gate 	ulong_t saved)			/* the saved instruction */
21490Sstevel@tonic-gate {
21500Sstevel@tonic-gate 	long ctl[
21514753Srh87107 	    1 + sizeof (sigset_t) / sizeof (long) +		/* PCSHOLD */
21524753Srh87107 	    1 + sizeof (fltset_t) / sizeof (long) +		/* PCSFAULT */
21534753Srh87107 	    1 + sizeof (priovec_t) / sizeof (long) +		/* PCWRITE */
21544753Srh87107 	    2 +							/* PCRUN */
21554753Srh87107 	    1 +							/* PCWSTOP */
21564753Srh87107 	    1 +							/* PCCFAULT */
21574753Srh87107 	    1 + sizeof (priovec_t) / sizeof (long) +		/* PCWRITE */
21584753Srh87107 	    1 + sizeof (fltset_t) / sizeof (long) +		/* PCSFAULT */
21594753Srh87107 	    1 + sizeof (sigset_t) / sizeof (long)];		/* PCSHOLD */
21600Sstevel@tonic-gate 	long *ctlp = ctl;
21610Sstevel@tonic-gate 	sigset_t unblock;
21620Sstevel@tonic-gate 	size_t size;
21630Sstevel@tonic-gate 	ssize_t ssize;
21640Sstevel@tonic-gate 	priovec_t *iovp;
21650Sstevel@tonic-gate 	sigset_t *holdp;
21660Sstevel@tonic-gate 	fltset_t *faultp;
21670Sstevel@tonic-gate 	instr_t old = (instr_t)saved;
21680Sstevel@tonic-gate 	instr_t bpt = BPT;
21690Sstevel@tonic-gate 	int error = 0;
21700Sstevel@tonic-gate 
21710Sstevel@tonic-gate 	/* block our signals for the duration */
21720Sstevel@tonic-gate 	(void) sigprocmask(SIG_BLOCK, &blockable_sigs, &unblock);
21730Sstevel@tonic-gate 
21740Sstevel@tonic-gate 	/* hold posted signals */
21750Sstevel@tonic-gate 	*ctlp++ = PCSHOLD;
21760Sstevel@tonic-gate 	holdp = (sigset_t *)ctlp;
21770Sstevel@tonic-gate 	prfillset(holdp);
21780Sstevel@tonic-gate 	prdelset(holdp, SIGKILL);
21790Sstevel@tonic-gate 	prdelset(holdp, SIGSTOP);
21800Sstevel@tonic-gate 	ctlp += sizeof (sigset_t) / sizeof (long);
21810Sstevel@tonic-gate 
21820Sstevel@tonic-gate 	/* force tracing of FLTTRACE */
21830Sstevel@tonic-gate 	if (!(prismember(faultset, FLTTRACE))) {
21840Sstevel@tonic-gate 		*ctlp++ = PCSFAULT;
21850Sstevel@tonic-gate 		faultp = (fltset_t *)ctlp;
21860Sstevel@tonic-gate 		*faultp = *faultset;
21870Sstevel@tonic-gate 		praddset(faultp, FLTTRACE);
21880Sstevel@tonic-gate 		ctlp += sizeof (fltset_t) / sizeof (long);
21890Sstevel@tonic-gate 	}
21900Sstevel@tonic-gate 
21910Sstevel@tonic-gate 	/* restore the old instruction */
21920Sstevel@tonic-gate 	*ctlp++ = PCWRITE;
21930Sstevel@tonic-gate 	iovp = (priovec_t *)ctlp;
21940Sstevel@tonic-gate 	iovp->pio_base = &old;
21950Sstevel@tonic-gate 	iovp->pio_len = sizeof (old);
21960Sstevel@tonic-gate 	iovp->pio_offset = address;
21970Sstevel@tonic-gate 	ctlp += sizeof (priovec_t) / sizeof (long);
21980Sstevel@tonic-gate 
21990Sstevel@tonic-gate 	/* clear current signal and fault; set running w/ single-step */
22000Sstevel@tonic-gate 	*ctlp++ = PCRUN;
22010Sstevel@tonic-gate 	*ctlp++ = PRCSIG | PRCFAULT | PRSTEP;
22020Sstevel@tonic-gate 
22030Sstevel@tonic-gate 	/* wait for stop, cancel the fault */
22040Sstevel@tonic-gate 	*ctlp++ = PCWSTOP;
22050Sstevel@tonic-gate 	*ctlp++ = PCCFAULT;
22060Sstevel@tonic-gate 
22070Sstevel@tonic-gate 	/* restore the breakpoint trap */
22080Sstevel@tonic-gate 	*ctlp++ = PCWRITE;
22090Sstevel@tonic-gate 	iovp = (priovec_t *)ctlp;
22100Sstevel@tonic-gate 	iovp->pio_base = &bpt;
22110Sstevel@tonic-gate 	iovp->pio_len = sizeof (bpt);
22120Sstevel@tonic-gate 	iovp->pio_offset = address;
22130Sstevel@tonic-gate 	ctlp += sizeof (priovec_t) / sizeof (long);
22140Sstevel@tonic-gate 
22150Sstevel@tonic-gate 	/* restore fault tracing set */
22160Sstevel@tonic-gate 	if (!(prismember(faultset, FLTTRACE))) {
22170Sstevel@tonic-gate 		*ctlp++ = PCSFAULT;
22180Sstevel@tonic-gate 		*(fltset_t *)ctlp = *faultset;
22190Sstevel@tonic-gate 		ctlp += sizeof (fltset_t) / sizeof (long);
22200Sstevel@tonic-gate 	}
22210Sstevel@tonic-gate 
22220Sstevel@tonic-gate 	/* restore the hold mask */
22230Sstevel@tonic-gate 	*ctlp++ = PCSHOLD;
22240Sstevel@tonic-gate 	*(sigset_t *)ctlp = *sigmask;
22250Sstevel@tonic-gate 	ctlp += sizeof (sigset_t) / sizeof (long);
22260Sstevel@tonic-gate 
22270Sstevel@tonic-gate 	size = (char *)ctlp - (char *)ctl;
22280Sstevel@tonic-gate 	if ((ssize = write(ctlfd, ctl, size)) != size)
22290Sstevel@tonic-gate 		error = (ssize == -1)? errno : EINTR;
22300Sstevel@tonic-gate 	(void) sigprocmask(SIG_SETMASK, &unblock, NULL);
22310Sstevel@tonic-gate 	return (error);
22320Sstevel@tonic-gate }
22330Sstevel@tonic-gate 
22340Sstevel@tonic-gate /*
22350Sstevel@tonic-gate  * Step over a breakpoint, i.e., execute the instruction that
22360Sstevel@tonic-gate  * really belongs at the breakpoint location (the current %pc)
22370Sstevel@tonic-gate  * and leave the process stopped at the next instruction.
22380Sstevel@tonic-gate  */
22390Sstevel@tonic-gate int
22400Sstevel@tonic-gate Pxecbkpt(struct ps_prochandle *P, ulong_t saved)
22410Sstevel@tonic-gate {
22420Sstevel@tonic-gate 	int ctlfd = (P->agentctlfd >= 0)? P->agentctlfd : P->ctlfd;
22430Sstevel@tonic-gate 	int rv, error;
22440Sstevel@tonic-gate 
22450Sstevel@tonic-gate 	if (P->state != PS_STOP) {
22460Sstevel@tonic-gate 		errno = EBUSY;
22470Sstevel@tonic-gate 		return (-1);
22480Sstevel@tonic-gate 	}
22490Sstevel@tonic-gate 
22500Sstevel@tonic-gate 	Psync(P);
22510Sstevel@tonic-gate 
22520Sstevel@tonic-gate 	error = execute_bkpt(ctlfd,
22534753Srh87107 	    &P->status.pr_flttrace, &P->status.pr_lwp.pr_lwphold,
22544753Srh87107 	    P->status.pr_lwp.pr_reg[R_PC], saved);
22550Sstevel@tonic-gate 	rv = Pstopstatus(P, PCNULL, 0);
22560Sstevel@tonic-gate 
22570Sstevel@tonic-gate 	if (error != 0) {
22580Sstevel@tonic-gate 		if (P->status.pr_lwp.pr_why == PR_JOBCONTROL &&
22590Sstevel@tonic-gate 		    error == EBUSY) {	/* jobcontrol stop -- back off */
22600Sstevel@tonic-gate 			P->state = PS_RUN;
22610Sstevel@tonic-gate 			return (0);
22620Sstevel@tonic-gate 		}
22630Sstevel@tonic-gate 		if (error == ENOENT)
22640Sstevel@tonic-gate 			return (0);
22650Sstevel@tonic-gate 		errno = error;
22660Sstevel@tonic-gate 		return (-1);
22670Sstevel@tonic-gate 	}
22680Sstevel@tonic-gate 
22690Sstevel@tonic-gate 	return (rv);
22700Sstevel@tonic-gate }
22710Sstevel@tonic-gate 
22720Sstevel@tonic-gate /*
22730Sstevel@tonic-gate  * Install the watchpoint described by wp.
22740Sstevel@tonic-gate  */
22750Sstevel@tonic-gate int
22760Sstevel@tonic-gate Psetwapt(struct ps_prochandle *P, const prwatch_t *wp)
22770Sstevel@tonic-gate {
22780Sstevel@tonic-gate 	long ctl[1 + sizeof (prwatch_t) / sizeof (long)];
22790Sstevel@tonic-gate 	prwatch_t *cwp = (prwatch_t *)&ctl[1];
22800Sstevel@tonic-gate 
22810Sstevel@tonic-gate 	if (P->state == PS_DEAD || P->state == PS_UNDEAD ||
22820Sstevel@tonic-gate 	    P->state == PS_IDLE) {
22830Sstevel@tonic-gate 		errno = ENOENT;
22840Sstevel@tonic-gate 		return (-1);
22850Sstevel@tonic-gate 	}
22860Sstevel@tonic-gate 
22870Sstevel@tonic-gate 	ctl[0] = PCWATCH;
22880Sstevel@tonic-gate 	cwp->pr_vaddr = wp->pr_vaddr;
22890Sstevel@tonic-gate 	cwp->pr_size = wp->pr_size;
22900Sstevel@tonic-gate 	cwp->pr_wflags = wp->pr_wflags;
22910Sstevel@tonic-gate 
22920Sstevel@tonic-gate 	if (write(P->ctlfd, ctl, sizeof (ctl)) != sizeof (ctl))
22930Sstevel@tonic-gate 		return (-1);
22940Sstevel@tonic-gate 
22950Sstevel@tonic-gate 	return (0);
22960Sstevel@tonic-gate }
22970Sstevel@tonic-gate 
22980Sstevel@tonic-gate /*
22990Sstevel@tonic-gate  * Remove the watchpoint described by wp.
23000Sstevel@tonic-gate  */
23010Sstevel@tonic-gate int
23020Sstevel@tonic-gate Pdelwapt(struct ps_prochandle *P, const prwatch_t *wp)
23030Sstevel@tonic-gate {
23040Sstevel@tonic-gate 	long ctl[1 + sizeof (prwatch_t) / sizeof (long)];
23050Sstevel@tonic-gate 	prwatch_t *cwp = (prwatch_t *)&ctl[1];
23060Sstevel@tonic-gate 
23070Sstevel@tonic-gate 	if (P->state == PS_DEAD || P->state == PS_UNDEAD ||
23080Sstevel@tonic-gate 	    P->state == PS_IDLE) {
23090Sstevel@tonic-gate 		errno = ENOENT;
23100Sstevel@tonic-gate 		return (-1);
23110Sstevel@tonic-gate 	}
23120Sstevel@tonic-gate 
23130Sstevel@tonic-gate 	ctl[0] = PCWATCH;
23140Sstevel@tonic-gate 	cwp->pr_vaddr = wp->pr_vaddr;
23150Sstevel@tonic-gate 	cwp->pr_size = wp->pr_size;
23160Sstevel@tonic-gate 	cwp->pr_wflags = 0;
23170Sstevel@tonic-gate 
23180Sstevel@tonic-gate 	if (write(P->ctlfd, ctl, sizeof (ctl)) != sizeof (ctl))
23190Sstevel@tonic-gate 		return (-1);
23200Sstevel@tonic-gate 
23210Sstevel@tonic-gate 	return (0);
23220Sstevel@tonic-gate }
23230Sstevel@tonic-gate 
23240Sstevel@tonic-gate /*
23250Sstevel@tonic-gate  * Common code for Pxecwapt() and Lxecwapt().  Develop the array of requests
23260Sstevel@tonic-gate  * that will do the job, then write them to the specified control file
23270Sstevel@tonic-gate  * descriptor.  Return the non-zero errno if the write fails.
23280Sstevel@tonic-gate  */
23290Sstevel@tonic-gate static int
23300Sstevel@tonic-gate execute_wapt(
23310Sstevel@tonic-gate 	int ctlfd,		/* process or LWP control file descriptor */
23320Sstevel@tonic-gate 	const fltset_t *faultset,	/* current set of traced faults */
23330Sstevel@tonic-gate 	const sigset_t *sigmask,	/* current signal mask */
23340Sstevel@tonic-gate 	const prwatch_t *wp)		/* watchpoint descriptor */
23350Sstevel@tonic-gate {
23360Sstevel@tonic-gate 	long ctl[
23370Sstevel@tonic-gate 	    1 + sizeof (sigset_t) / sizeof (long) +		/* PCSHOLD */
23380Sstevel@tonic-gate 	    1 + sizeof (fltset_t) / sizeof (long) +		/* PCSFAULT */
23390Sstevel@tonic-gate 	    1 + sizeof (prwatch_t) / sizeof (long) +		/* PCWATCH */
23400Sstevel@tonic-gate 	    2 +							/* PCRUN */
23410Sstevel@tonic-gate 	    1 +							/* PCWSTOP */
23420Sstevel@tonic-gate 	    1 +							/* PCCFAULT */
23430Sstevel@tonic-gate 	    1 + sizeof (prwatch_t) / sizeof (long) +		/* PCWATCH */
23440Sstevel@tonic-gate 	    1 + sizeof (fltset_t) / sizeof (long) +		/* PCSFAULT */
23450Sstevel@tonic-gate 	    1 + sizeof (sigset_t) / sizeof (long)];		/* PCSHOLD */
23460Sstevel@tonic-gate 
23470Sstevel@tonic-gate 	long *ctlp = ctl;
23480Sstevel@tonic-gate 	int error = 0;
23490Sstevel@tonic-gate 
23500Sstevel@tonic-gate 	sigset_t unblock;
23510Sstevel@tonic-gate 	sigset_t *holdp;
23520Sstevel@tonic-gate 	fltset_t *faultp;
23530Sstevel@tonic-gate 	prwatch_t *prw;
23540Sstevel@tonic-gate 	ssize_t ssize;
23550Sstevel@tonic-gate 	size_t size;
23560Sstevel@tonic-gate 
23570Sstevel@tonic-gate 	(void) sigprocmask(SIG_BLOCK, &blockable_sigs, &unblock);
23580Sstevel@tonic-gate 
23590Sstevel@tonic-gate 	/*
23600Sstevel@tonic-gate 	 * Hold all posted signals in the victim process prior to stepping.
23610Sstevel@tonic-gate 	 */
23620Sstevel@tonic-gate 	*ctlp++ = PCSHOLD;
23630Sstevel@tonic-gate 	holdp = (sigset_t *)ctlp;
23640Sstevel@tonic-gate 	prfillset(holdp);
23650Sstevel@tonic-gate 	prdelset(holdp, SIGKILL);
23660Sstevel@tonic-gate 	prdelset(holdp, SIGSTOP);
23670Sstevel@tonic-gate 	ctlp += sizeof (sigset_t) / sizeof (long);
23680Sstevel@tonic-gate 
23690Sstevel@tonic-gate 	/*
23700Sstevel@tonic-gate 	 * Force tracing of FLTTRACE since we need to single step.
23710Sstevel@tonic-gate 	 */
23720Sstevel@tonic-gate 	if (!(prismember(faultset, FLTTRACE))) {
23730Sstevel@tonic-gate 		*ctlp++ = PCSFAULT;
23740Sstevel@tonic-gate 		faultp = (fltset_t *)ctlp;
23750Sstevel@tonic-gate 		*faultp = *faultset;
23760Sstevel@tonic-gate 		praddset(faultp, FLTTRACE);
23770Sstevel@tonic-gate 		ctlp += sizeof (fltset_t) / sizeof (long);
23780Sstevel@tonic-gate 	}
23790Sstevel@tonic-gate 
23800Sstevel@tonic-gate 	/*
23810Sstevel@tonic-gate 	 * Clear only the current watchpoint by setting pr_wflags to zero.
23820Sstevel@tonic-gate 	 */
23830Sstevel@tonic-gate 	*ctlp++ = PCWATCH;
23840Sstevel@tonic-gate 	prw = (prwatch_t *)ctlp;
23850Sstevel@tonic-gate 	prw->pr_vaddr = wp->pr_vaddr;
23860Sstevel@tonic-gate 	prw->pr_size = wp->pr_size;
23870Sstevel@tonic-gate 	prw->pr_wflags = 0;
23880Sstevel@tonic-gate 	ctlp += sizeof (prwatch_t) / sizeof (long);
23890Sstevel@tonic-gate 
23900Sstevel@tonic-gate 	/*
23910Sstevel@tonic-gate 	 * Clear the current signal and fault; set running with single-step.
23920Sstevel@tonic-gate 	 * Then wait for the victim to stop and cancel the FLTTRACE.
23930Sstevel@tonic-gate 	 */
23940Sstevel@tonic-gate 	*ctlp++ = PCRUN;
23950Sstevel@tonic-gate 	*ctlp++ = PRCSIG | PRCFAULT | PRSTEP;
23960Sstevel@tonic-gate 	*ctlp++ = PCWSTOP;
23970Sstevel@tonic-gate 	*ctlp++ = PCCFAULT;
23980Sstevel@tonic-gate 
23990Sstevel@tonic-gate 	/*
24000Sstevel@tonic-gate 	 * Restore the current watchpoint.
24010Sstevel@tonic-gate 	 */
24020Sstevel@tonic-gate 	*ctlp++ = PCWATCH;
24030Sstevel@tonic-gate 	(void) memcpy(ctlp, wp, sizeof (prwatch_t));
24040Sstevel@tonic-gate 	ctlp += sizeof (prwatch_t) / sizeof (long);
24050Sstevel@tonic-gate 
24060Sstevel@tonic-gate 	/*
24070Sstevel@tonic-gate 	 * Restore fault tracing set if we modified it.
24080Sstevel@tonic-gate 	 */
24090Sstevel@tonic-gate 	if (!(prismember(faultset, FLTTRACE))) {
24100Sstevel@tonic-gate 		*ctlp++ = PCSFAULT;
24110Sstevel@tonic-gate 		*(fltset_t *)ctlp = *faultset;
24120Sstevel@tonic-gate 		ctlp += sizeof (fltset_t) / sizeof (long);
24130Sstevel@tonic-gate 	}
24140Sstevel@tonic-gate 
24150Sstevel@tonic-gate 	/*
24160Sstevel@tonic-gate 	 * Restore the hold mask to the current hold mask (i.e. the one
24170Sstevel@tonic-gate 	 * before we executed any of the previous operations).
24180Sstevel@tonic-gate 	 */
24190Sstevel@tonic-gate 	*ctlp++ = PCSHOLD;
24200Sstevel@tonic-gate 	*(sigset_t *)ctlp = *sigmask;
24210Sstevel@tonic-gate 	ctlp += sizeof (sigset_t) / sizeof (long);
24220Sstevel@tonic-gate 
24230Sstevel@tonic-gate 	size = (char *)ctlp - (char *)ctl;
24240Sstevel@tonic-gate 	if ((ssize = write(ctlfd, ctl, size)) != size)
24250Sstevel@tonic-gate 		error = (ssize == -1)? errno : EINTR;
24260Sstevel@tonic-gate 	(void) sigprocmask(SIG_SETMASK, &unblock, NULL);
24270Sstevel@tonic-gate 	return (error);
24280Sstevel@tonic-gate }
24290Sstevel@tonic-gate 
24300Sstevel@tonic-gate /*
24310Sstevel@tonic-gate  * Step over a watchpoint, i.e., execute the instruction that was stopped by
24320Sstevel@tonic-gate  * the watchpoint, and then leave the LWP stopped at the next instruction.
24330Sstevel@tonic-gate  */
24340Sstevel@tonic-gate int
24350Sstevel@tonic-gate Pxecwapt(struct ps_prochandle *P, const prwatch_t *wp)
24360Sstevel@tonic-gate {
24370Sstevel@tonic-gate 	int ctlfd = (P->agentctlfd >= 0)? P->agentctlfd : P->ctlfd;
24380Sstevel@tonic-gate 	int rv, error;
24390Sstevel@tonic-gate 
24400Sstevel@tonic-gate 	if (P->state != PS_STOP) {
24410Sstevel@tonic-gate 		errno = EBUSY;
24420Sstevel@tonic-gate 		return (-1);
24430Sstevel@tonic-gate 	}
24440Sstevel@tonic-gate 
24450Sstevel@tonic-gate 	Psync(P);
24460Sstevel@tonic-gate 	error = execute_wapt(ctlfd,
24474753Srh87107 	    &P->status.pr_flttrace, &P->status.pr_lwp.pr_lwphold, wp);
24480Sstevel@tonic-gate 	rv = Pstopstatus(P, PCNULL, 0);
24490Sstevel@tonic-gate 
24500Sstevel@tonic-gate 	if (error != 0) {
24510Sstevel@tonic-gate 		if (P->status.pr_lwp.pr_why == PR_JOBCONTROL &&
24520Sstevel@tonic-gate 		    error == EBUSY) {	/* jobcontrol stop -- back off */
24530Sstevel@tonic-gate 			P->state = PS_RUN;
24540Sstevel@tonic-gate 			return (0);
24550Sstevel@tonic-gate 		}
24560Sstevel@tonic-gate 		if (error == ENOENT)
24570Sstevel@tonic-gate 			return (0);
24580Sstevel@tonic-gate 		errno = error;
24590Sstevel@tonic-gate 		return (-1);
24600Sstevel@tonic-gate 	}
24610Sstevel@tonic-gate 
24620Sstevel@tonic-gate 	return (rv);
24630Sstevel@tonic-gate }
24640Sstevel@tonic-gate 
24650Sstevel@tonic-gate int
24660Sstevel@tonic-gate Psetflags(struct ps_prochandle *P, long flags)
24670Sstevel@tonic-gate {
24680Sstevel@tonic-gate 	int rc;
24690Sstevel@tonic-gate 	long ctl[2];
24700Sstevel@tonic-gate 
24710Sstevel@tonic-gate 	ctl[0] = PCSET;
24720Sstevel@tonic-gate 	ctl[1] = flags;
24730Sstevel@tonic-gate 
24740Sstevel@tonic-gate 	if (write(P->ctlfd, ctl, 2*sizeof (long)) != 2*sizeof (long)) {
24750Sstevel@tonic-gate 		rc = -1;
24760Sstevel@tonic-gate 	} else {
24770Sstevel@tonic-gate 		P->status.pr_flags |= flags;
24780Sstevel@tonic-gate 		P->status.pr_lwp.pr_flags |= flags;
24790Sstevel@tonic-gate 		rc = 0;
24800Sstevel@tonic-gate 	}
24810Sstevel@tonic-gate 
24820Sstevel@tonic-gate 	return (rc);
24830Sstevel@tonic-gate }
24840Sstevel@tonic-gate 
24850Sstevel@tonic-gate int
24860Sstevel@tonic-gate Punsetflags(struct ps_prochandle *P, long flags)
24870Sstevel@tonic-gate {
24880Sstevel@tonic-gate 	int rc;
24890Sstevel@tonic-gate 	long ctl[2];
24900Sstevel@tonic-gate 
24910Sstevel@tonic-gate 	ctl[0] = PCUNSET;
24920Sstevel@tonic-gate 	ctl[1] = flags;
24930Sstevel@tonic-gate 
24940Sstevel@tonic-gate 	if (write(P->ctlfd, ctl, 2*sizeof (long)) != 2*sizeof (long)) {
24950Sstevel@tonic-gate 		rc = -1;
24960Sstevel@tonic-gate 	} else {
24970Sstevel@tonic-gate 		P->status.pr_flags &= ~flags;
24980Sstevel@tonic-gate 		P->status.pr_lwp.pr_flags &= ~flags;
24990Sstevel@tonic-gate 		rc = 0;
25000Sstevel@tonic-gate 	}
25010Sstevel@tonic-gate 
25020Sstevel@tonic-gate 	return (rc);
25030Sstevel@tonic-gate }
25040Sstevel@tonic-gate 
25050Sstevel@tonic-gate /*
25060Sstevel@tonic-gate  * Common function to allow clients to manipulate the action to be taken
25070Sstevel@tonic-gate  * on receipt of a signal, receipt of machine fault, entry to a system call,
25080Sstevel@tonic-gate  * or exit from a system call.  We make use of our private prset_* functions
25090Sstevel@tonic-gate  * in order to make this code be common.  The 'which' parameter identifies
25100Sstevel@tonic-gate  * the code for the event of interest (0 means change the entire set), and
25110Sstevel@tonic-gate  * the 'stop' parameter is a boolean indicating whether the process should
25120Sstevel@tonic-gate  * stop when the event of interest occurs.  The previous value is returned
25130Sstevel@tonic-gate  * to the caller; -1 is returned if an error occurred.
25140Sstevel@tonic-gate  */
25150Sstevel@tonic-gate static int
25160Sstevel@tonic-gate Psetaction(struct ps_prochandle *P, void *sp, size_t size,
25170Sstevel@tonic-gate     uint_t flag, int max, int which, int stop)
25180Sstevel@tonic-gate {
25190Sstevel@tonic-gate 	int oldval;
25200Sstevel@tonic-gate 
25210Sstevel@tonic-gate 	if (which < 0 || which > max) {
25220Sstevel@tonic-gate 		errno = EINVAL;
25230Sstevel@tonic-gate 		return (-1);
25240Sstevel@tonic-gate 	}
25250Sstevel@tonic-gate 
25260Sstevel@tonic-gate 	if (P->state == PS_DEAD || P->state == PS_UNDEAD ||
25270Sstevel@tonic-gate 	    P->state == PS_IDLE) {
25280Sstevel@tonic-gate 		errno = ENOENT;
25290Sstevel@tonic-gate 		return (-1);
25300Sstevel@tonic-gate 	}
25310Sstevel@tonic-gate 
25320Sstevel@tonic-gate 	oldval = prset_ismember(sp, size, which) ? TRUE : FALSE;
25330Sstevel@tonic-gate 
25340Sstevel@tonic-gate 	if (stop) {
25350Sstevel@tonic-gate 		if (which == 0) {
25360Sstevel@tonic-gate 			prset_fill(sp, size);
25370Sstevel@tonic-gate 			P->flags |= flag;
25380Sstevel@tonic-gate 		} else if (!oldval) {
25390Sstevel@tonic-gate 			prset_add(sp, size, which);
25400Sstevel@tonic-gate 			P->flags |= flag;
25410Sstevel@tonic-gate 		}
25420Sstevel@tonic-gate 	} else {
25430Sstevel@tonic-gate 		if (which == 0) {
25440Sstevel@tonic-gate 			prset_empty(sp, size);
25450Sstevel@tonic-gate 			P->flags |= flag;
25460Sstevel@tonic-gate 		} else if (oldval) {
25470Sstevel@tonic-gate 			prset_del(sp, size, which);
25480Sstevel@tonic-gate 			P->flags |= flag;
25490Sstevel@tonic-gate 		}
25500Sstevel@tonic-gate 	}
25510Sstevel@tonic-gate 
25520Sstevel@tonic-gate 	if (P->state == PS_RUN)
25530Sstevel@tonic-gate 		Psync(P);
25540Sstevel@tonic-gate 
25550Sstevel@tonic-gate 	return (oldval);
25560Sstevel@tonic-gate }
25570Sstevel@tonic-gate 
25580Sstevel@tonic-gate /*
25590Sstevel@tonic-gate  * Set action on specified signal.
25600Sstevel@tonic-gate  */
25610Sstevel@tonic-gate int
25620Sstevel@tonic-gate Psignal(struct ps_prochandle *P, int which, int stop)
25630Sstevel@tonic-gate {
25640Sstevel@tonic-gate 	int oldval;
25650Sstevel@tonic-gate 
25660Sstevel@tonic-gate 	if (which == SIGKILL && stop != 0) {
25670Sstevel@tonic-gate 		errno = EINVAL;
25680Sstevel@tonic-gate 		return (-1);
25690Sstevel@tonic-gate 	}
25700Sstevel@tonic-gate 
25710Sstevel@tonic-gate 	oldval = Psetaction(P, &P->status.pr_sigtrace, sizeof (sigset_t),
25720Sstevel@tonic-gate 	    SETSIG, PRMAXSIG, which, stop);
25730Sstevel@tonic-gate 
25740Sstevel@tonic-gate 	if (oldval != -1 && which == 0 && stop != 0)
25750Sstevel@tonic-gate 		prdelset(&P->status.pr_sigtrace, SIGKILL);
25760Sstevel@tonic-gate 
25770Sstevel@tonic-gate 	return (oldval);
25780Sstevel@tonic-gate }
25790Sstevel@tonic-gate 
25800Sstevel@tonic-gate /*
25810Sstevel@tonic-gate  * Set all signal tracing flags.
25820Sstevel@tonic-gate  */
25830Sstevel@tonic-gate void
25840Sstevel@tonic-gate Psetsignal(struct ps_prochandle *P, const sigset_t *set)
25850Sstevel@tonic-gate {
25860Sstevel@tonic-gate 	if (P->state == PS_DEAD || P->state == PS_UNDEAD ||
25870Sstevel@tonic-gate 	    P->state == PS_IDLE)
25880Sstevel@tonic-gate 		return;
25890Sstevel@tonic-gate 
25900Sstevel@tonic-gate 	P->status.pr_sigtrace = *set;
25910Sstevel@tonic-gate 	P->flags |= SETSIG;
25920Sstevel@tonic-gate 
25930Sstevel@tonic-gate 	if (P->state == PS_RUN)
25940Sstevel@tonic-gate 		Psync(P);
25950Sstevel@tonic-gate }
25960Sstevel@tonic-gate 
25970Sstevel@tonic-gate /*
25980Sstevel@tonic-gate  * Set action on specified fault.
25990Sstevel@tonic-gate  */
26000Sstevel@tonic-gate int
26010Sstevel@tonic-gate Pfault(struct ps_prochandle *P, int which, int stop)
26020Sstevel@tonic-gate {
26030Sstevel@tonic-gate 	return (Psetaction(P, &P->status.pr_flttrace, sizeof (fltset_t),
26040Sstevel@tonic-gate 	    SETFAULT, PRMAXFAULT, which, stop));
26050Sstevel@tonic-gate }
26060Sstevel@tonic-gate 
26070Sstevel@tonic-gate /*
26080Sstevel@tonic-gate  * Set all machine fault tracing flags.
26090Sstevel@tonic-gate  */
26100Sstevel@tonic-gate void
26110Sstevel@tonic-gate Psetfault(struct ps_prochandle *P, const fltset_t *set)
26120Sstevel@tonic-gate {
26130Sstevel@tonic-gate 	if (P->state == PS_DEAD || P->state == PS_UNDEAD ||
26140Sstevel@tonic-gate 	    P->state == PS_IDLE)
26150Sstevel@tonic-gate 		return;
26160Sstevel@tonic-gate 
26170Sstevel@tonic-gate 	P->status.pr_flttrace = *set;
26180Sstevel@tonic-gate 	P->flags |= SETFAULT;
26190Sstevel@tonic-gate 
26200Sstevel@tonic-gate 	if (P->state == PS_RUN)
26210Sstevel@tonic-gate 		Psync(P);
26220Sstevel@tonic-gate }
26230Sstevel@tonic-gate 
26240Sstevel@tonic-gate /*
26250Sstevel@tonic-gate  * Set action on specified system call entry.
26260Sstevel@tonic-gate  */
26270Sstevel@tonic-gate int
26280Sstevel@tonic-gate Psysentry(struct ps_prochandle *P, int which, int stop)
26290Sstevel@tonic-gate {
26300Sstevel@tonic-gate 	return (Psetaction(P, &P->status.pr_sysentry, sizeof (sysset_t),
26310Sstevel@tonic-gate 	    SETENTRY, PRMAXSYS, which, stop));
26320Sstevel@tonic-gate }
26330Sstevel@tonic-gate 
26340Sstevel@tonic-gate /*
26350Sstevel@tonic-gate  * Set all system call entry tracing flags.
26360Sstevel@tonic-gate  */
26370Sstevel@tonic-gate void
26380Sstevel@tonic-gate Psetsysentry(struct ps_prochandle *P, const sysset_t *set)
26390Sstevel@tonic-gate {
26400Sstevel@tonic-gate 	if (P->state == PS_DEAD || P->state == PS_UNDEAD ||
26410Sstevel@tonic-gate 	    P->state == PS_IDLE)
26420Sstevel@tonic-gate 		return;
26430Sstevel@tonic-gate 
26440Sstevel@tonic-gate 	P->status.pr_sysentry = *set;
26450Sstevel@tonic-gate 	P->flags |= SETENTRY;
26460Sstevel@tonic-gate 
26470Sstevel@tonic-gate 	if (P->state == PS_RUN)
26480Sstevel@tonic-gate 		Psync(P);
26490Sstevel@tonic-gate }
26500Sstevel@tonic-gate 
26510Sstevel@tonic-gate /*
26520Sstevel@tonic-gate  * Set action on specified system call exit.
26530Sstevel@tonic-gate  */
26540Sstevel@tonic-gate int
26550Sstevel@tonic-gate Psysexit(struct ps_prochandle *P, int which, int stop)
26560Sstevel@tonic-gate {
26570Sstevel@tonic-gate 	return (Psetaction(P, &P->status.pr_sysexit, sizeof (sysset_t),
26580Sstevel@tonic-gate 	    SETEXIT, PRMAXSYS, which, stop));
26590Sstevel@tonic-gate }
26600Sstevel@tonic-gate 
26610Sstevel@tonic-gate /*
26620Sstevel@tonic-gate  * Set all system call exit tracing flags.
26630Sstevel@tonic-gate  */
26640Sstevel@tonic-gate void
26650Sstevel@tonic-gate Psetsysexit(struct ps_prochandle *P, const sysset_t *set)
26660Sstevel@tonic-gate {
26670Sstevel@tonic-gate 	if (P->state == PS_DEAD || P->state == PS_UNDEAD ||
26680Sstevel@tonic-gate 	    P->state == PS_IDLE)
26690Sstevel@tonic-gate 		return;
26700Sstevel@tonic-gate 
26710Sstevel@tonic-gate 	P->status.pr_sysexit = *set;
26720Sstevel@tonic-gate 	P->flags |= SETEXIT;
26730Sstevel@tonic-gate 
26740Sstevel@tonic-gate 	if (P->state == PS_RUN)
26750Sstevel@tonic-gate 		Psync(P);
26760Sstevel@tonic-gate }
26770Sstevel@tonic-gate 
26780Sstevel@tonic-gate /*
26790Sstevel@tonic-gate  * Utility function to read the contents of a file that contains a
26800Sstevel@tonic-gate  * prheader_t at the start (/proc/pid/lstatus or /proc/pid/lpsinfo).
26810Sstevel@tonic-gate  * Returns a malloc()d buffer or NULL on failure.
26820Sstevel@tonic-gate  */
26830Sstevel@tonic-gate static prheader_t *
26840Sstevel@tonic-gate read_lfile(struct ps_prochandle *P, const char *lname)
26850Sstevel@tonic-gate {
26860Sstevel@tonic-gate 	prheader_t *Lhp;
26872712Snn35248 	char lpath[PATH_MAX];
26880Sstevel@tonic-gate 	struct stat64 statb;
26890Sstevel@tonic-gate 	int fd;
26900Sstevel@tonic-gate 	size_t size;
26910Sstevel@tonic-gate 	ssize_t rval;
26920Sstevel@tonic-gate 
26932712Snn35248 	(void) snprintf(lpath, sizeof (lpath), "%s/%d/%s", procfs_path,
26940Sstevel@tonic-gate 	    (int)P->status.pr_pid, lname);
26950Sstevel@tonic-gate 	if ((fd = open(lpath, O_RDONLY)) < 0 || fstat64(fd, &statb) != 0) {
26960Sstevel@tonic-gate 		if (fd >= 0)
26970Sstevel@tonic-gate 			(void) close(fd);
26980Sstevel@tonic-gate 		return (NULL);
26990Sstevel@tonic-gate 	}
27000Sstevel@tonic-gate 
27010Sstevel@tonic-gate 	/*
27020Sstevel@tonic-gate 	 * 'size' is just the initial guess at the buffer size.
27030Sstevel@tonic-gate 	 * It will have to grow if the number of lwps increases
27040Sstevel@tonic-gate 	 * while we are looking at the process.
27050Sstevel@tonic-gate 	 * 'size' must be larger than the actual file size.
27060Sstevel@tonic-gate 	 */
27070Sstevel@tonic-gate 	size = statb.st_size + 32;
27080Sstevel@tonic-gate 
27090Sstevel@tonic-gate 	for (;;) {
27100Sstevel@tonic-gate 		if ((Lhp = malloc(size)) == NULL)
27110Sstevel@tonic-gate 			break;
27120Sstevel@tonic-gate 		if ((rval = pread(fd, Lhp, size, 0)) < 0 ||
27130Sstevel@tonic-gate 		    rval <= sizeof (prheader_t)) {
27140Sstevel@tonic-gate 			free(Lhp);
27150Sstevel@tonic-gate 			Lhp = NULL;
27160Sstevel@tonic-gate 			break;
27170Sstevel@tonic-gate 		}
27180Sstevel@tonic-gate 		if (rval < size)
27190Sstevel@tonic-gate 			break;
27200Sstevel@tonic-gate 		/* need a bigger buffer */
27210Sstevel@tonic-gate 		free(Lhp);
27220Sstevel@tonic-gate 		size *= 2;
27230Sstevel@tonic-gate 	}
27240Sstevel@tonic-gate 
27250Sstevel@tonic-gate 	(void) close(fd);
27260Sstevel@tonic-gate 	return (Lhp);
27270Sstevel@tonic-gate }
27280Sstevel@tonic-gate 
27290Sstevel@tonic-gate /*
27300Sstevel@tonic-gate  * LWP iteration interface.
27310Sstevel@tonic-gate  */
27320Sstevel@tonic-gate int
27330Sstevel@tonic-gate Plwp_iter(struct ps_prochandle *P, proc_lwp_f *func, void *cd)
27340Sstevel@tonic-gate {
27350Sstevel@tonic-gate 	prheader_t *Lhp;
27360Sstevel@tonic-gate 	lwpstatus_t *Lsp;
27370Sstevel@tonic-gate 	long nlwp;
27380Sstevel@tonic-gate 	int rv;
27390Sstevel@tonic-gate 
27400Sstevel@tonic-gate 	switch (P->state) {
27410Sstevel@tonic-gate 	case PS_RUN:
27420Sstevel@tonic-gate 		(void) Pstopstatus(P, PCNULL, 0);
27430Sstevel@tonic-gate 		break;
27440Sstevel@tonic-gate 
27450Sstevel@tonic-gate 	case PS_STOP:
27460Sstevel@tonic-gate 		Psync(P);
27470Sstevel@tonic-gate 		break;
27480Sstevel@tonic-gate 
27490Sstevel@tonic-gate 	case PS_IDLE:
27500Sstevel@tonic-gate 		errno = ENODATA;
27510Sstevel@tonic-gate 		return (-1);
27520Sstevel@tonic-gate 	}
27530Sstevel@tonic-gate 
27540Sstevel@tonic-gate 	/*
27550Sstevel@tonic-gate 	 * For either live processes or cores, the single LWP case is easy:
27560Sstevel@tonic-gate 	 * the pstatus_t contains the lwpstatus_t for the only LWP.
27570Sstevel@tonic-gate 	 */
27580Sstevel@tonic-gate 	if (P->status.pr_nlwp <= 1)
27590Sstevel@tonic-gate 		return (func(cd, &P->status.pr_lwp));
27600Sstevel@tonic-gate 
27610Sstevel@tonic-gate 	/*
27620Sstevel@tonic-gate 	 * For the core file multi-LWP case, we just iterate through the
27630Sstevel@tonic-gate 	 * list of LWP structs we read in from the core file.
27640Sstevel@tonic-gate 	 */
27650Sstevel@tonic-gate 	if (P->state == PS_DEAD) {
27660Sstevel@tonic-gate 		lwp_info_t *lwp = list_prev(&P->core->core_lwp_head);
27670Sstevel@tonic-gate 		uint_t i;
27680Sstevel@tonic-gate 
27690Sstevel@tonic-gate 		for (i = 0; i < P->core->core_nlwp; i++, lwp = list_prev(lwp)) {
27700Sstevel@tonic-gate 			if (lwp->lwp_psinfo.pr_sname != 'Z' &&
27710Sstevel@tonic-gate 			    (rv = func(cd, &lwp->lwp_status)) != 0)
27720Sstevel@tonic-gate 				break;
27730Sstevel@tonic-gate 		}
27740Sstevel@tonic-gate 
27750Sstevel@tonic-gate 		return (rv);
27760Sstevel@tonic-gate 	}
27770Sstevel@tonic-gate 
27780Sstevel@tonic-gate 	/*
27790Sstevel@tonic-gate 	 * For the live process multi-LWP case, we have to work a little
27800Sstevel@tonic-gate 	 * harder: the /proc/pid/lstatus file has the array of LWP structs.
27810Sstevel@tonic-gate 	 */
27820Sstevel@tonic-gate 	if ((Lhp = read_lfile(P, "lstatus")) == NULL)
27830Sstevel@tonic-gate 		return (-1);
27840Sstevel@tonic-gate 
27850Sstevel@tonic-gate 	for (nlwp = Lhp->pr_nent, Lsp = (lwpstatus_t *)(uintptr_t)(Lhp + 1);
27860Sstevel@tonic-gate 	    nlwp > 0;
27870Sstevel@tonic-gate 	    nlwp--, Lsp = (lwpstatus_t *)((uintptr_t)Lsp + Lhp->pr_entsize)) {
27880Sstevel@tonic-gate 		if ((rv = func(cd, Lsp)) != 0)
27890Sstevel@tonic-gate 			break;
27900Sstevel@tonic-gate 	}
27910Sstevel@tonic-gate 
27920Sstevel@tonic-gate 	free(Lhp);
27930Sstevel@tonic-gate 	return (rv);
27940Sstevel@tonic-gate }
27950Sstevel@tonic-gate 
27960Sstevel@tonic-gate /*
27970Sstevel@tonic-gate  * Extended LWP iteration interface.
27980Sstevel@tonic-gate  * Iterate over all LWPs, active and zombie.
27990Sstevel@tonic-gate  */
28000Sstevel@tonic-gate int
28010Sstevel@tonic-gate Plwp_iter_all(struct ps_prochandle *P, proc_lwp_all_f *func, void *cd)
28020Sstevel@tonic-gate {
28030Sstevel@tonic-gate 	prheader_t *Lhp = NULL;
28040Sstevel@tonic-gate 	lwpstatus_t *Lsp;
28050Sstevel@tonic-gate 	lwpstatus_t *sp;
28060Sstevel@tonic-gate 	prheader_t *Lphp = NULL;
28070Sstevel@tonic-gate 	lwpsinfo_t *Lpsp;
28080Sstevel@tonic-gate 	long nstat;
28090Sstevel@tonic-gate 	long ninfo;
28100Sstevel@tonic-gate 	int rv;
28110Sstevel@tonic-gate 
28120Sstevel@tonic-gate retry:
28130Sstevel@tonic-gate 	if (Lhp != NULL)
28140Sstevel@tonic-gate 		free(Lhp);
28150Sstevel@tonic-gate 	if (Lphp != NULL)
28160Sstevel@tonic-gate 		free(Lphp);
28170Sstevel@tonic-gate 	if (P->state == PS_RUN)
28180Sstevel@tonic-gate 		(void) Pstopstatus(P, PCNULL, 0);
28190Sstevel@tonic-gate 	(void) Ppsinfo(P);
28200Sstevel@tonic-gate 
28210Sstevel@tonic-gate 	if (P->state == PS_STOP)
28220Sstevel@tonic-gate 		Psync(P);
28230Sstevel@tonic-gate 
28240Sstevel@tonic-gate 	/*
28250Sstevel@tonic-gate 	 * For either live processes or cores, the single LWP case is easy:
28260Sstevel@tonic-gate 	 * the pstatus_t contains the lwpstatus_t for the only LWP and
28270Sstevel@tonic-gate 	 * the psinfo_t contains the lwpsinfo_t for the only LWP.
28280Sstevel@tonic-gate 	 */
28290Sstevel@tonic-gate 	if (P->status.pr_nlwp + P->status.pr_nzomb <= 1)
28300Sstevel@tonic-gate 		return (func(cd, &P->status.pr_lwp, &P->psinfo.pr_lwp));
28310Sstevel@tonic-gate 
28320Sstevel@tonic-gate 	/*
28330Sstevel@tonic-gate 	 * For the core file multi-LWP case, we just iterate through the
28340Sstevel@tonic-gate 	 * list of LWP structs we read in from the core file.
28350Sstevel@tonic-gate 	 */
28360Sstevel@tonic-gate 	if (P->state == PS_DEAD) {
28370Sstevel@tonic-gate 		lwp_info_t *lwp = list_prev(&P->core->core_lwp_head);
28380Sstevel@tonic-gate 		uint_t i;
28390Sstevel@tonic-gate 
28400Sstevel@tonic-gate 		for (i = 0; i < P->core->core_nlwp; i++, lwp = list_prev(lwp)) {
28410Sstevel@tonic-gate 			sp = (lwp->lwp_psinfo.pr_sname == 'Z')? NULL :
28424753Srh87107 			    &lwp->lwp_status;
28430Sstevel@tonic-gate 			if ((rv = func(cd, sp, &lwp->lwp_psinfo)) != 0)
28440Sstevel@tonic-gate 				break;
28450Sstevel@tonic-gate 		}
28460Sstevel@tonic-gate 
28470Sstevel@tonic-gate 		return (rv);
28480Sstevel@tonic-gate 	}
28490Sstevel@tonic-gate 
28500Sstevel@tonic-gate 	/*
28510Sstevel@tonic-gate 	 * For the live process multi-LWP case, we have to work a little
28520Sstevel@tonic-gate 	 * harder: the /proc/pid/lstatus file has the array of lwpstatus_t's
28530Sstevel@tonic-gate 	 * and the /proc/pid/lpsinfo file has the array of lwpsinfo_t's.
28540Sstevel@tonic-gate 	 */
28550Sstevel@tonic-gate 	if ((Lhp = read_lfile(P, "lstatus")) == NULL)
28560Sstevel@tonic-gate 		return (-1);
28570Sstevel@tonic-gate 	if ((Lphp = read_lfile(P, "lpsinfo")) == NULL) {
28580Sstevel@tonic-gate 		free(Lhp);
28590Sstevel@tonic-gate 		return (-1);
28600Sstevel@tonic-gate 	}
28610Sstevel@tonic-gate 
28620Sstevel@tonic-gate 	/*
28630Sstevel@tonic-gate 	 * If we are looking at a running process, or one we do not control,
28640Sstevel@tonic-gate 	 * the active and zombie lwps in the process may have changed since
28650Sstevel@tonic-gate 	 * we read the process status structure.  If so, just start over.
28660Sstevel@tonic-gate 	 */
28670Sstevel@tonic-gate 	if (Lhp->pr_nent != P->status.pr_nlwp ||
28680Sstevel@tonic-gate 	    Lphp->pr_nent != P->status.pr_nlwp + P->status.pr_nzomb)
28690Sstevel@tonic-gate 		goto retry;
28700Sstevel@tonic-gate 
28710Sstevel@tonic-gate 	/*
28720Sstevel@tonic-gate 	 * To be perfectly safe, prescan the two arrays, checking consistency.
28730Sstevel@tonic-gate 	 * We rely on /proc giving us lwpstatus_t's and lwpsinfo_t's in the
28740Sstevel@tonic-gate 	 * same order (the lwp directory order) in their respective files.
28750Sstevel@tonic-gate 	 * We also rely on there being (possibly) more lwpsinfo_t's than
28760Sstevel@tonic-gate 	 * lwpstatus_t's (the extra lwpsinfo_t's are for zombie lwps).
28770Sstevel@tonic-gate 	 */
28780Sstevel@tonic-gate 	Lsp = (lwpstatus_t *)(uintptr_t)(Lhp + 1);
28790Sstevel@tonic-gate 	Lpsp = (lwpsinfo_t *)(uintptr_t)(Lphp + 1);
28800Sstevel@tonic-gate 	nstat = Lhp->pr_nent;
28810Sstevel@tonic-gate 	for (ninfo = Lphp->pr_nent; ninfo != 0; ninfo--) {
28820Sstevel@tonic-gate 		if (Lpsp->pr_sname != 'Z') {
28830Sstevel@tonic-gate 			/*
28840Sstevel@tonic-gate 			 * Not a zombie lwp; check for matching lwpids.
28850Sstevel@tonic-gate 			 */
28860Sstevel@tonic-gate 			if (nstat == 0 || Lsp->pr_lwpid != Lpsp->pr_lwpid)
28870Sstevel@tonic-gate 				goto retry;
28880Sstevel@tonic-gate 			Lsp = (lwpstatus_t *)((uintptr_t)Lsp + Lhp->pr_entsize);
28890Sstevel@tonic-gate 			nstat--;
28900Sstevel@tonic-gate 		}
28910Sstevel@tonic-gate 		Lpsp = (lwpsinfo_t *)((uintptr_t)Lpsp + Lphp->pr_entsize);
28920Sstevel@tonic-gate 	}
28930Sstevel@tonic-gate 	if (nstat != 0)
28940Sstevel@tonic-gate 		goto retry;
28950Sstevel@tonic-gate 
28960Sstevel@tonic-gate 	/*
28970Sstevel@tonic-gate 	 * Rescan, this time for real.
28980Sstevel@tonic-gate 	 */
28990Sstevel@tonic-gate 	Lsp = (lwpstatus_t *)(uintptr_t)(Lhp + 1);
29000Sstevel@tonic-gate 	Lpsp = (lwpsinfo_t *)(uintptr_t)(Lphp + 1);
29010Sstevel@tonic-gate 	for (ninfo = Lphp->pr_nent; ninfo != 0; ninfo--) {
29020Sstevel@tonic-gate 		if (Lpsp->pr_sname != 'Z') {
29030Sstevel@tonic-gate 			sp = Lsp;
29040Sstevel@tonic-gate 			Lsp = (lwpstatus_t *)((uintptr_t)Lsp + Lhp->pr_entsize);
29050Sstevel@tonic-gate 		} else {
29060Sstevel@tonic-gate 			sp = NULL;
29070Sstevel@tonic-gate 		}
29080Sstevel@tonic-gate 		if ((rv = func(cd, sp, Lpsp)) != 0)
29090Sstevel@tonic-gate 			break;
29100Sstevel@tonic-gate 		Lpsp = (lwpsinfo_t *)((uintptr_t)Lpsp + Lphp->pr_entsize);
29110Sstevel@tonic-gate 	}
29120Sstevel@tonic-gate 
29130Sstevel@tonic-gate 	free(Lhp);
29140Sstevel@tonic-gate 	free(Lphp);
29150Sstevel@tonic-gate 	return (rv);
29160Sstevel@tonic-gate }
29170Sstevel@tonic-gate 
29180Sstevel@tonic-gate core_content_t
29190Sstevel@tonic-gate Pcontent(struct ps_prochandle *P)
29200Sstevel@tonic-gate {
29210Sstevel@tonic-gate 	if (P->state == PS_DEAD)
29220Sstevel@tonic-gate 		return (P->core->core_content);
29230Sstevel@tonic-gate 	if (P->state == PS_IDLE)
29240Sstevel@tonic-gate 		return (CC_CONTENT_TEXT | CC_CONTENT_DATA | CC_CONTENT_CTF);
29250Sstevel@tonic-gate 
29260Sstevel@tonic-gate 	return (CC_CONTENT_ALL);
29270Sstevel@tonic-gate }
29280Sstevel@tonic-gate 
29290Sstevel@tonic-gate /*
29300Sstevel@tonic-gate  * =================================================================
29310Sstevel@tonic-gate  * The remainder of the functions in this file are for the
29320Sstevel@tonic-gate  * control of individual LWPs in the controlled process.
29330Sstevel@tonic-gate  * =================================================================
29340Sstevel@tonic-gate  */
29350Sstevel@tonic-gate 
29360Sstevel@tonic-gate /*
29370Sstevel@tonic-gate  * Find an entry in the process hash table for the specified lwpid.
29380Sstevel@tonic-gate  * The entry will either point to an existing struct ps_lwphandle
29390Sstevel@tonic-gate  * or it will point to an empty slot for a new struct ps_lwphandle.
29400Sstevel@tonic-gate  */
29410Sstevel@tonic-gate static struct ps_lwphandle **
29420Sstevel@tonic-gate Lfind(struct ps_prochandle *P, lwpid_t lwpid)
29430Sstevel@tonic-gate {
29440Sstevel@tonic-gate 	struct ps_lwphandle **Lp;
29450Sstevel@tonic-gate 	struct ps_lwphandle *L;
29460Sstevel@tonic-gate 
29470Sstevel@tonic-gate 	for (Lp = &P->hashtab[lwpid % (HASHSIZE - 1)];
29480Sstevel@tonic-gate 	    (L = *Lp) != NULL; Lp = &L->lwp_hash)
29490Sstevel@tonic-gate 		if (L->lwp_id == lwpid)
29500Sstevel@tonic-gate 			break;
29510Sstevel@tonic-gate 	return (Lp);
29520Sstevel@tonic-gate }
29530Sstevel@tonic-gate 
29540Sstevel@tonic-gate /*
29550Sstevel@tonic-gate  * Grab an LWP contained within the controlled process.
29560Sstevel@tonic-gate  * Return an opaque pointer to its LWP control structure.
29570Sstevel@tonic-gate  *	perr: pointer to error return code.
29580Sstevel@tonic-gate  */
29590Sstevel@tonic-gate struct ps_lwphandle *
29600Sstevel@tonic-gate Lgrab(struct ps_prochandle *P, lwpid_t lwpid, int *perr)
29610Sstevel@tonic-gate {
29620Sstevel@tonic-gate 	struct ps_lwphandle **Lp;
29630Sstevel@tonic-gate 	struct ps_lwphandle *L;
29640Sstevel@tonic-gate 	int fd;
29652712Snn35248 	char procname[PATH_MAX];
29660Sstevel@tonic-gate 	char *fname;
29670Sstevel@tonic-gate 	int rc = 0;
29680Sstevel@tonic-gate 
29690Sstevel@tonic-gate 	(void) mutex_lock(&P->proc_lock);
29700Sstevel@tonic-gate 
29710Sstevel@tonic-gate 	if (P->state == PS_UNDEAD || P->state == PS_IDLE)
29720Sstevel@tonic-gate 		rc = G_NOPROC;
29730Sstevel@tonic-gate 	else if (P->hashtab == NULL &&
29740Sstevel@tonic-gate 	    (P->hashtab = calloc(HASHSIZE, sizeof (struct ps_lwphandle *)))
29750Sstevel@tonic-gate 	    == NULL)
29760Sstevel@tonic-gate 		rc = G_STRANGE;
29770Sstevel@tonic-gate 	else if (*(Lp = Lfind(P, lwpid)) != NULL)
29780Sstevel@tonic-gate 		rc = G_BUSY;
29790Sstevel@tonic-gate 	else if ((L = malloc(sizeof (struct ps_lwphandle))) == NULL)
29800Sstevel@tonic-gate 		rc = G_STRANGE;
29810Sstevel@tonic-gate 	if (rc) {
29820Sstevel@tonic-gate 		*perr = rc;
29830Sstevel@tonic-gate 		(void) mutex_unlock(&P->proc_lock);
29840Sstevel@tonic-gate 		return (NULL);
29850Sstevel@tonic-gate 	}
29860Sstevel@tonic-gate 
29870Sstevel@tonic-gate 	(void) memset(L, 0, sizeof (*L));
29880Sstevel@tonic-gate 	L->lwp_ctlfd = -1;
29890Sstevel@tonic-gate 	L->lwp_statfd = -1;
29900Sstevel@tonic-gate 	L->lwp_proc = P;
29910Sstevel@tonic-gate 	L->lwp_id = lwpid;
29920Sstevel@tonic-gate 	*Lp = L;	/* insert into the hash table */
29930Sstevel@tonic-gate 
29940Sstevel@tonic-gate 	if (P->state == PS_DEAD) {	/* core file */
29950Sstevel@tonic-gate 		if (getlwpstatus(P, lwpid, &L->lwp_status) == -1) {
29960Sstevel@tonic-gate 			rc = G_NOPROC;
29970Sstevel@tonic-gate 			goto err;
29980Sstevel@tonic-gate 		}
29990Sstevel@tonic-gate 		L->lwp_state = PS_DEAD;
30000Sstevel@tonic-gate 		*perr = 0;
30010Sstevel@tonic-gate 		(void) mutex_unlock(&P->proc_lock);
30020Sstevel@tonic-gate 		return (L);
30030Sstevel@tonic-gate 	}
30040Sstevel@tonic-gate 
30050Sstevel@tonic-gate 	/*
30060Sstevel@tonic-gate 	 * Open the /proc/<pid>/lwp/<lwpid> files
30070Sstevel@tonic-gate 	 */
30082712Snn35248 	(void) snprintf(procname, sizeof (procname), "%s/%d/lwp/%d/",
30092712Snn35248 	    procfs_path, (int)P->pid, (int)lwpid);
30100Sstevel@tonic-gate 	fname = procname + strlen(procname);
30110Sstevel@tonic-gate 	(void) set_minfd();
30120Sstevel@tonic-gate 
30130Sstevel@tonic-gate 	(void) strcpy(fname, "lwpstatus");
30140Sstevel@tonic-gate 	if ((fd = open(procname, O_RDONLY)) < 0 ||
30150Sstevel@tonic-gate 	    (fd = dupfd(fd, 0)) < 0) {
30160Sstevel@tonic-gate 		switch (errno) {
30170Sstevel@tonic-gate 		case ENOENT:
30180Sstevel@tonic-gate 			rc = G_NOPROC;
30190Sstevel@tonic-gate 			break;
30200Sstevel@tonic-gate 		default:
30210Sstevel@tonic-gate 			dprintf("Lgrab: failed to open %s: %s\n",
30220Sstevel@tonic-gate 			    procname, strerror(errno));
30230Sstevel@tonic-gate 			rc = G_STRANGE;
30240Sstevel@tonic-gate 			break;
30250Sstevel@tonic-gate 		}
30260Sstevel@tonic-gate 		goto err;
30270Sstevel@tonic-gate 	}
30280Sstevel@tonic-gate 	L->lwp_statfd = fd;
30290Sstevel@tonic-gate 
30300Sstevel@tonic-gate 	if (pread(fd, &L->lwp_status, sizeof (L->lwp_status), (off_t)0) < 0) {
30310Sstevel@tonic-gate 		switch (errno) {
30320Sstevel@tonic-gate 		case ENOENT:
30330Sstevel@tonic-gate 			rc = G_NOPROC;
30340Sstevel@tonic-gate 			break;
30350Sstevel@tonic-gate 		default:
30360Sstevel@tonic-gate 			dprintf("Lgrab: failed to read %s: %s\n",
30370Sstevel@tonic-gate 			    procname, strerror(errno));
30380Sstevel@tonic-gate 			rc = G_STRANGE;
30390Sstevel@tonic-gate 			break;
30400Sstevel@tonic-gate 		}
30410Sstevel@tonic-gate 		goto err;
30420Sstevel@tonic-gate 	}
30430Sstevel@tonic-gate 
30440Sstevel@tonic-gate 	(void) strcpy(fname, "lwpctl");
30450Sstevel@tonic-gate 	if ((fd = open(procname, O_WRONLY)) < 0 ||
30460Sstevel@tonic-gate 	    (fd = dupfd(fd, 0)) < 0) {
30470Sstevel@tonic-gate 		switch (errno) {
30480Sstevel@tonic-gate 		case ENOENT:
30490Sstevel@tonic-gate 			rc = G_NOPROC;
30500Sstevel@tonic-gate 			break;
30510Sstevel@tonic-gate 		default:
30520Sstevel@tonic-gate 			dprintf("Lgrab: failed to open %s: %s\n",
30530Sstevel@tonic-gate 			    procname, strerror(errno));
30540Sstevel@tonic-gate 			rc = G_STRANGE;
30550Sstevel@tonic-gate 			break;
30560Sstevel@tonic-gate 		}
30570Sstevel@tonic-gate 		goto err;
30580Sstevel@tonic-gate 	}
30590Sstevel@tonic-gate 	L->lwp_ctlfd = fd;
30600Sstevel@tonic-gate 
30610Sstevel@tonic-gate 	L->lwp_state =
30624753Srh87107 	    ((L->lwp_status.pr_flags & (PR_STOPPED|PR_ISTOP))
30634753Srh87107 	    == (PR_STOPPED|PR_ISTOP))?
30644753Srh87107 	    PS_STOP : PS_RUN;
30650Sstevel@tonic-gate 
30660Sstevel@tonic-gate 	*perr = 0;
30670Sstevel@tonic-gate 	(void) mutex_unlock(&P->proc_lock);
30680Sstevel@tonic-gate 	return (L);
30690Sstevel@tonic-gate 
30700Sstevel@tonic-gate err:
30710Sstevel@tonic-gate 	Lfree_internal(P, L);
30720Sstevel@tonic-gate 	*perr = rc;
30730Sstevel@tonic-gate 	(void) mutex_unlock(&P->proc_lock);
30740Sstevel@tonic-gate 	return (NULL);
30750Sstevel@tonic-gate }
30760Sstevel@tonic-gate 
30770Sstevel@tonic-gate /*
30780Sstevel@tonic-gate  * Return a printable string corresponding to an Lgrab() error return.
30790Sstevel@tonic-gate  */
30800Sstevel@tonic-gate const char *
30810Sstevel@tonic-gate Lgrab_error(int error)
30820Sstevel@tonic-gate {
30830Sstevel@tonic-gate 	const char *str;
30840Sstevel@tonic-gate 
30850Sstevel@tonic-gate 	switch (error) {
30860Sstevel@tonic-gate 	case G_NOPROC:
30870Sstevel@tonic-gate 		str = "no such LWP";
30880Sstevel@tonic-gate 		break;
30890Sstevel@tonic-gate 	case G_BUSY:
30900Sstevel@tonic-gate 		str = "LWP already grabbed";
30910Sstevel@tonic-gate 		break;
30920Sstevel@tonic-gate 	case G_STRANGE:
30930Sstevel@tonic-gate 		str = "unanticipated system error";
30940Sstevel@tonic-gate 		break;
30950Sstevel@tonic-gate 	default:
30960Sstevel@tonic-gate 		str = "unknown error";
30970Sstevel@tonic-gate 		break;
30980Sstevel@tonic-gate 	}
30990Sstevel@tonic-gate 
31000Sstevel@tonic-gate 	return (str);
31010Sstevel@tonic-gate }
31020Sstevel@tonic-gate 
31030Sstevel@tonic-gate /*
31040Sstevel@tonic-gate  * Free an LWP control structure.
31050Sstevel@tonic-gate  */
31060Sstevel@tonic-gate void
31070Sstevel@tonic-gate Lfree(struct ps_lwphandle *L)
31080Sstevel@tonic-gate {
31090Sstevel@tonic-gate 	struct ps_prochandle *P = L->lwp_proc;
31100Sstevel@tonic-gate 
31110Sstevel@tonic-gate 	(void) mutex_lock(&P->proc_lock);
31120Sstevel@tonic-gate 	Lfree_internal(P, L);
31130Sstevel@tonic-gate 	(void) mutex_unlock(&P->proc_lock);
31140Sstevel@tonic-gate }
31150Sstevel@tonic-gate 
31160Sstevel@tonic-gate static void
31170Sstevel@tonic-gate Lfree_internal(struct ps_prochandle *P, struct ps_lwphandle *L)
31180Sstevel@tonic-gate {
31190Sstevel@tonic-gate 	*Lfind(P, L->lwp_id) = L->lwp_hash;	/* delete from hash table */
31200Sstevel@tonic-gate 	if (L->lwp_ctlfd >= 0)
31210Sstevel@tonic-gate 		(void) close(L->lwp_ctlfd);
31220Sstevel@tonic-gate 	if (L->lwp_statfd >= 0)
31230Sstevel@tonic-gate 		(void) close(L->lwp_statfd);
31240Sstevel@tonic-gate 
31250Sstevel@tonic-gate 	/* clear out the structure as a precaution against reuse */
31260Sstevel@tonic-gate 	(void) memset(L, 0, sizeof (*L));
31270Sstevel@tonic-gate 	L->lwp_ctlfd = -1;
31280Sstevel@tonic-gate 	L->lwp_statfd = -1;
31290Sstevel@tonic-gate 
31300Sstevel@tonic-gate 	free(L);
31310Sstevel@tonic-gate }
31320Sstevel@tonic-gate 
31330Sstevel@tonic-gate /*
31340Sstevel@tonic-gate  * Return the state of the process, one of the PS_* values.
31350Sstevel@tonic-gate  */
31360Sstevel@tonic-gate int
31370Sstevel@tonic-gate Lstate(struct ps_lwphandle *L)
31380Sstevel@tonic-gate {
31390Sstevel@tonic-gate 	return (L->lwp_state);
31400Sstevel@tonic-gate }
31410Sstevel@tonic-gate 
31420Sstevel@tonic-gate /*
31430Sstevel@tonic-gate  * Return the open control file descriptor for the LWP.
31440Sstevel@tonic-gate  * Clients must not close this file descriptor, nor use it
31450Sstevel@tonic-gate  * after the LWP is freed.
31460Sstevel@tonic-gate  */
31470Sstevel@tonic-gate int
31480Sstevel@tonic-gate Lctlfd(struct ps_lwphandle *L)
31490Sstevel@tonic-gate {
31500Sstevel@tonic-gate 	return (L->lwp_ctlfd);
31510Sstevel@tonic-gate }
31520Sstevel@tonic-gate 
31530Sstevel@tonic-gate /*
31540Sstevel@tonic-gate  * Return a pointer to the LWP lwpsinfo structure.
31550Sstevel@tonic-gate  * Clients should not hold on to this pointer indefinitely.
31560Sstevel@tonic-gate  * It will become invalid on Lfree().
31570Sstevel@tonic-gate  */
31580Sstevel@tonic-gate const lwpsinfo_t *
31590Sstevel@tonic-gate Lpsinfo(struct ps_lwphandle *L)
31600Sstevel@tonic-gate {
31610Sstevel@tonic-gate 	if (Plwp_getpsinfo(L->lwp_proc, L->lwp_id, &L->lwp_psinfo) == -1)
31620Sstevel@tonic-gate 		return (NULL);
31630Sstevel@tonic-gate 
31640Sstevel@tonic-gate 	return (&L->lwp_psinfo);
31650Sstevel@tonic-gate }
31660Sstevel@tonic-gate 
31670Sstevel@tonic-gate /*
31680Sstevel@tonic-gate  * Return a pointer to the LWP status structure.
31690Sstevel@tonic-gate  * Clients should not hold on to this pointer indefinitely.
31700Sstevel@tonic-gate  * It will become invalid on Lfree().
31710Sstevel@tonic-gate  */
31720Sstevel@tonic-gate const lwpstatus_t *
31730Sstevel@tonic-gate Lstatus(struct ps_lwphandle *L)
31740Sstevel@tonic-gate {
31750Sstevel@tonic-gate 	return (&L->lwp_status);
31760Sstevel@tonic-gate }
31770Sstevel@tonic-gate 
31780Sstevel@tonic-gate /*
31790Sstevel@tonic-gate  * Given an LWP handle, return the process handle.
31800Sstevel@tonic-gate  */
31810Sstevel@tonic-gate struct ps_prochandle *
31820Sstevel@tonic-gate Lprochandle(struct ps_lwphandle *L)
31830Sstevel@tonic-gate {
31840Sstevel@tonic-gate 	return (L->lwp_proc);
31850Sstevel@tonic-gate }
31860Sstevel@tonic-gate 
31870Sstevel@tonic-gate /*
31880Sstevel@tonic-gate  * Ensure that all cached state is written to the LWP.
31890Sstevel@tonic-gate  * The cached state is the LWP's signal mask and registers.
31900Sstevel@tonic-gate  */
31910Sstevel@tonic-gate void
31920Sstevel@tonic-gate Lsync(struct ps_lwphandle *L)
31930Sstevel@tonic-gate {
31940Sstevel@tonic-gate 	int ctlfd = L->lwp_ctlfd;
31950Sstevel@tonic-gate 	long cmd[2];
31960Sstevel@tonic-gate 	iovec_t iov[4];
31970Sstevel@tonic-gate 	int n = 0;
31980Sstevel@tonic-gate 
31990Sstevel@tonic-gate 	if (L->lwp_flags & SETHOLD) {
32000Sstevel@tonic-gate 		cmd[0] = PCSHOLD;
32010Sstevel@tonic-gate 		iov[n].iov_base = (caddr_t)&cmd[0];
32020Sstevel@tonic-gate 		iov[n++].iov_len = sizeof (long);
32030Sstevel@tonic-gate 		iov[n].iov_base = (caddr_t)&L->lwp_status.pr_lwphold;
32040Sstevel@tonic-gate 		iov[n++].iov_len = sizeof (L->lwp_status.pr_lwphold);
32050Sstevel@tonic-gate 	}
32060Sstevel@tonic-gate 	if (L->lwp_flags & SETREGS) {
32070Sstevel@tonic-gate 		cmd[1] = PCSREG;
32080Sstevel@tonic-gate 		iov[n].iov_base = (caddr_t)&cmd[1];
32090Sstevel@tonic-gate 		iov[n++].iov_len = sizeof (long);
32100Sstevel@tonic-gate 		iov[n].iov_base = (caddr_t)&L->lwp_status.pr_reg[0];
32110Sstevel@tonic-gate 		iov[n++].iov_len = sizeof (L->lwp_status.pr_reg);
32120Sstevel@tonic-gate 	}
32130Sstevel@tonic-gate 
32140Sstevel@tonic-gate 	if (n == 0 || writev(ctlfd, iov, n) < 0)
32150Sstevel@tonic-gate 		return;		/* nothing to do or write failed */
32160Sstevel@tonic-gate 
32170Sstevel@tonic-gate 	L->lwp_flags &= ~(SETHOLD|SETREGS);
32180Sstevel@tonic-gate }
32190Sstevel@tonic-gate 
32200Sstevel@tonic-gate /*
32210Sstevel@tonic-gate  * Wait for the specified LWP to stop or terminate.
32220Sstevel@tonic-gate  * Or, just get the current status (PCNULL).
32230Sstevel@tonic-gate  * Or, direct it to stop and get the current status (PCDSTOP).
32240Sstevel@tonic-gate  */
32250Sstevel@tonic-gate static int
32260Sstevel@tonic-gate Lstopstatus(struct ps_lwphandle *L,
32270Sstevel@tonic-gate 	long request,		/* PCNULL, PCDSTOP, PCSTOP, PCWSTOP */
32280Sstevel@tonic-gate 	uint_t msec)		/* if non-zero, timeout in milliseconds */
32290Sstevel@tonic-gate {
32300Sstevel@tonic-gate 	int ctlfd = L->lwp_ctlfd;
32310Sstevel@tonic-gate 	long ctl[3];
32320Sstevel@tonic-gate 	ssize_t rc;
32330Sstevel@tonic-gate 	int err;
32340Sstevel@tonic-gate 
32350Sstevel@tonic-gate 	switch (L->lwp_state) {
32360Sstevel@tonic-gate 	case PS_RUN:
32370Sstevel@tonic-gate 		break;
32380Sstevel@tonic-gate 	case PS_STOP:
32390Sstevel@tonic-gate 		if (request != PCNULL && request != PCDSTOP)
32400Sstevel@tonic-gate 			return (0);
32410Sstevel@tonic-gate 		break;
32420Sstevel@tonic-gate 	case PS_LOST:
32430Sstevel@tonic-gate 		if (request != PCNULL) {
32440Sstevel@tonic-gate 			errno = EAGAIN;
32450Sstevel@tonic-gate 			return (-1);
32460Sstevel@tonic-gate 		}
32470Sstevel@tonic-gate 		break;
32480Sstevel@tonic-gate 	case PS_UNDEAD:
32490Sstevel@tonic-gate 	case PS_DEAD:
32500Sstevel@tonic-gate 		if (request != PCNULL) {
32510Sstevel@tonic-gate 			errno = ENOENT;
32520Sstevel@tonic-gate 			return (-1);
32530Sstevel@tonic-gate 		}
32540Sstevel@tonic-gate 		break;
32550Sstevel@tonic-gate 	default:	/* corrupted state */
32560Sstevel@tonic-gate 		dprintf("Lstopstatus: corrupted state: %d\n", L->lwp_state);
32570Sstevel@tonic-gate 		errno = EINVAL;
32580Sstevel@tonic-gate 		return (-1);
32590Sstevel@tonic-gate 	}
32600Sstevel@tonic-gate 
32610Sstevel@tonic-gate 	ctl[0] = PCDSTOP;
32620Sstevel@tonic-gate 	ctl[1] = PCTWSTOP;
32630Sstevel@tonic-gate 	ctl[2] = (long)msec;
32640Sstevel@tonic-gate 	rc = 0;
32650Sstevel@tonic-gate 	switch (request) {
32660Sstevel@tonic-gate 	case PCSTOP:
32670Sstevel@tonic-gate 		rc = write(ctlfd, &ctl[0], 3*sizeof (long));
32680Sstevel@tonic-gate 		break;
32690Sstevel@tonic-gate 	case PCWSTOP:
32700Sstevel@tonic-gate 		rc = write(ctlfd, &ctl[1], 2*sizeof (long));
32710Sstevel@tonic-gate 		break;
32720Sstevel@tonic-gate 	case PCDSTOP:
32730Sstevel@tonic-gate 		rc = write(ctlfd, &ctl[0], 1*sizeof (long));
32740Sstevel@tonic-gate 		break;
32750Sstevel@tonic-gate 	case PCNULL:
32760Sstevel@tonic-gate 		if (L->lwp_state == PS_DEAD)
32770Sstevel@tonic-gate 			return (0); /* Nothing else to do for cores */
32780Sstevel@tonic-gate 		break;
32790Sstevel@tonic-gate 	default:	/* programming error */
32800Sstevel@tonic-gate 		errno = EINVAL;
32810Sstevel@tonic-gate 		return (-1);
32820Sstevel@tonic-gate 	}
32830Sstevel@tonic-gate 	err = (rc < 0)? errno : 0;
32840Sstevel@tonic-gate 	Lsync(L);
32850Sstevel@tonic-gate 
32860Sstevel@tonic-gate 	if (pread(L->lwp_statfd, &L->lwp_status,
32870Sstevel@tonic-gate 	    sizeof (L->lwp_status), (off_t)0) < 0)
32880Sstevel@tonic-gate 		err = errno;
32890Sstevel@tonic-gate 
32900Sstevel@tonic-gate 	if (err) {
32910Sstevel@tonic-gate 		switch (err) {
32920Sstevel@tonic-gate 		case EINTR:		/* user typed ctl-C */
32930Sstevel@tonic-gate 		case ERESTART:
32940Sstevel@tonic-gate 			dprintf("Lstopstatus: EINTR\n");
32950Sstevel@tonic-gate 			break;
32960Sstevel@tonic-gate 		case EAGAIN:		/* we lost control of the the process */
32970Sstevel@tonic-gate 			dprintf("Lstopstatus: EAGAIN\n");
32980Sstevel@tonic-gate 			L->lwp_state = PS_LOST;
32990Sstevel@tonic-gate 			errno = err;
33000Sstevel@tonic-gate 			return (-1);
33010Sstevel@tonic-gate 		default:
33020Sstevel@tonic-gate 			if (_libproc_debug) {
33030Sstevel@tonic-gate 				const char *errstr;
33040Sstevel@tonic-gate 
33050Sstevel@tonic-gate 				switch (request) {
33060Sstevel@tonic-gate 				case PCNULL:
33070Sstevel@tonic-gate 					errstr = "Lstopstatus PCNULL"; break;
33080Sstevel@tonic-gate 				case PCSTOP:
33090Sstevel@tonic-gate 					errstr = "Lstopstatus PCSTOP"; break;
33100Sstevel@tonic-gate 				case PCDSTOP:
33110Sstevel@tonic-gate 					errstr = "Lstopstatus PCDSTOP"; break;
33120Sstevel@tonic-gate 				case PCWSTOP:
33130Sstevel@tonic-gate 					errstr = "Lstopstatus PCWSTOP"; break;
33140Sstevel@tonic-gate 				default:
33150Sstevel@tonic-gate 					errstr = "Lstopstatus PC???"; break;
33160Sstevel@tonic-gate 				}
33170Sstevel@tonic-gate 				dprintf("%s: %s\n", errstr, strerror(err));
33180Sstevel@tonic-gate 			}
33190Sstevel@tonic-gate 			L->lwp_state = PS_UNDEAD;
33200Sstevel@tonic-gate 			errno = err;
33210Sstevel@tonic-gate 			return (-1);
33220Sstevel@tonic-gate 		}
33230Sstevel@tonic-gate 	}
33240Sstevel@tonic-gate 
33250Sstevel@tonic-gate 	if ((L->lwp_status.pr_flags & (PR_STOPPED|PR_ISTOP))
33260Sstevel@tonic-gate 	    != (PR_STOPPED|PR_ISTOP)) {
33270Sstevel@tonic-gate 		L->lwp_state = PS_RUN;
33280Sstevel@tonic-gate 		if (request == PCNULL || request == PCDSTOP || msec != 0)
33290Sstevel@tonic-gate 			return (0);
33300Sstevel@tonic-gate 		dprintf("Lstopstatus: LWP is not stopped\n");
33310Sstevel@tonic-gate 		errno = EPROTO;
33320Sstevel@tonic-gate 		return (-1);
33330Sstevel@tonic-gate 	}
33340Sstevel@tonic-gate 
33350Sstevel@tonic-gate 	L->lwp_state = PS_STOP;
33360Sstevel@tonic-gate 
33370Sstevel@tonic-gate 	if (_libproc_debug)	/* debugging */
33380Sstevel@tonic-gate 		prldump("Lstopstatus", &L->lwp_status);
33390Sstevel@tonic-gate 
33400Sstevel@tonic-gate 	switch (L->lwp_status.pr_why) {
33410Sstevel@tonic-gate 	case PR_SYSENTRY:
33420Sstevel@tonic-gate 	case PR_SYSEXIT:
33430Sstevel@tonic-gate 	case PR_REQUESTED:
33440Sstevel@tonic-gate 	case PR_SIGNALLED:
33450Sstevel@tonic-gate 	case PR_FAULTED:
33460Sstevel@tonic-gate 	case PR_JOBCONTROL:
33470Sstevel@tonic-gate 	case PR_SUSPENDED:
33480Sstevel@tonic-gate 		break;
33490Sstevel@tonic-gate 	default:
33500Sstevel@tonic-gate 		errno = EPROTO;
33510Sstevel@tonic-gate 		return (-1);
33520Sstevel@tonic-gate 	}
33530Sstevel@tonic-gate 
33540Sstevel@tonic-gate 	return (0);
33550Sstevel@tonic-gate }
33560Sstevel@tonic-gate 
33570Sstevel@tonic-gate /*
33580Sstevel@tonic-gate  * Wait for the LWP to stop for any reason.
33590Sstevel@tonic-gate  */
33600Sstevel@tonic-gate int
33610Sstevel@tonic-gate Lwait(struct ps_lwphandle *L, uint_t msec)
33620Sstevel@tonic-gate {
33630Sstevel@tonic-gate 	return (Lstopstatus(L, PCWSTOP, msec));
33640Sstevel@tonic-gate }
33650Sstevel@tonic-gate 
33660Sstevel@tonic-gate /*
33670Sstevel@tonic-gate  * Direct the LWP to stop; wait for it to stop.
33680Sstevel@tonic-gate  */
33690Sstevel@tonic-gate int
33700Sstevel@tonic-gate Lstop(struct ps_lwphandle *L, uint_t msec)
33710Sstevel@tonic-gate {
33720Sstevel@tonic-gate 	return (Lstopstatus(L, PCSTOP, msec));
33730Sstevel@tonic-gate }
33740Sstevel@tonic-gate 
33750Sstevel@tonic-gate /*
33760Sstevel@tonic-gate  * Direct the LWP to stop; don't wait.
33770Sstevel@tonic-gate  */
33780Sstevel@tonic-gate int
33790Sstevel@tonic-gate Ldstop(struct ps_lwphandle *L)
33800Sstevel@tonic-gate {
33810Sstevel@tonic-gate 	return (Lstopstatus(L, PCDSTOP, 0));
33820Sstevel@tonic-gate }
33830Sstevel@tonic-gate 
33840Sstevel@tonic-gate /*
33850Sstevel@tonic-gate  * Get the value of one register from stopped LWP.
33860Sstevel@tonic-gate  */
33870Sstevel@tonic-gate int
33880Sstevel@tonic-gate Lgetareg(struct ps_lwphandle *L, int regno, prgreg_t *preg)
33890Sstevel@tonic-gate {
33900Sstevel@tonic-gate 	if (regno < 0 || regno >= NPRGREG) {
33910Sstevel@tonic-gate 		errno = EINVAL;
33920Sstevel@tonic-gate 		return (-1);
33930Sstevel@tonic-gate 	}
33940Sstevel@tonic-gate 
33950Sstevel@tonic-gate 	if (L->lwp_state != PS_STOP) {
33960Sstevel@tonic-gate 		errno = EBUSY;
33970Sstevel@tonic-gate 		return (-1);
33980Sstevel@tonic-gate 	}
33990Sstevel@tonic-gate 
34000Sstevel@tonic-gate 	*preg = L->lwp_status.pr_reg[regno];
34010Sstevel@tonic-gate 	return (0);
34020Sstevel@tonic-gate }
34030Sstevel@tonic-gate 
34040Sstevel@tonic-gate /*
34050Sstevel@tonic-gate  * Put value of one register into stopped LWP.
34060Sstevel@tonic-gate  */
34070Sstevel@tonic-gate int
34080Sstevel@tonic-gate Lputareg(struct ps_lwphandle *L, int regno, prgreg_t reg)
34090Sstevel@tonic-gate {
34100Sstevel@tonic-gate 	if (regno < 0 || regno >= NPRGREG) {
34110Sstevel@tonic-gate 		errno = EINVAL;
34120Sstevel@tonic-gate 		return (-1);
34130Sstevel@tonic-gate 	}
34140Sstevel@tonic-gate 
34150Sstevel@tonic-gate 	if (L->lwp_state != PS_STOP) {
34160Sstevel@tonic-gate 		errno = EBUSY;
34170Sstevel@tonic-gate 		return (-1);
34180Sstevel@tonic-gate 	}
34190Sstevel@tonic-gate 
34200Sstevel@tonic-gate 	L->lwp_status.pr_reg[regno] = reg;
34210Sstevel@tonic-gate 	L->lwp_flags |= SETREGS;	/* set registers before continuing */
34220Sstevel@tonic-gate 	return (0);
34230Sstevel@tonic-gate }
34240Sstevel@tonic-gate 
34250Sstevel@tonic-gate int
34260Sstevel@tonic-gate Lsetrun(struct ps_lwphandle *L,
34270Sstevel@tonic-gate 	int sig,	/* signal to pass to LWP */
34280Sstevel@tonic-gate 	int flags)	/* PRSTEP|PRSABORT|PRSTOP|PRCSIG|PRCFAULT */
34290Sstevel@tonic-gate {
34300Sstevel@tonic-gate 	int ctlfd = L->lwp_ctlfd;
34310Sstevel@tonic-gate 	int sbits = (PR_DSTOP | PR_ISTOP | PR_ASLEEP);
34320Sstevel@tonic-gate 
34330Sstevel@tonic-gate 	long ctl[1 +					/* PCCFAULT	*/
34344753Srh87107 	    1 + sizeof (siginfo_t)/sizeof (long) +	/* PCSSIG/PCCSIG */
34354753Srh87107 	    2 ];					/* PCRUN	*/
34360Sstevel@tonic-gate 
34370Sstevel@tonic-gate 	long *ctlp = ctl;
34380Sstevel@tonic-gate 	size_t size;
34390Sstevel@tonic-gate 
34400Sstevel@tonic-gate 	if (L->lwp_state != PS_STOP &&
34410Sstevel@tonic-gate 	    (L->lwp_status.pr_flags & sbits) == 0) {
34420Sstevel@tonic-gate 		errno = EBUSY;
34430Sstevel@tonic-gate 		return (-1);
34440Sstevel@tonic-gate 	}
34450Sstevel@tonic-gate 
34460Sstevel@tonic-gate 	Lsync(L);	/* flush registers */
34470Sstevel@tonic-gate 
34480Sstevel@tonic-gate 	if (flags & PRCFAULT) {		/* clear current fault */
34490Sstevel@tonic-gate 		*ctlp++ = PCCFAULT;
34500Sstevel@tonic-gate 		flags &= ~PRCFAULT;
34510Sstevel@tonic-gate 	}
34520Sstevel@tonic-gate 
34530Sstevel@tonic-gate 	if (flags & PRCSIG) {		/* clear current signal */
34540Sstevel@tonic-gate 		*ctlp++ = PCCSIG;
34550Sstevel@tonic-gate 		flags &= ~PRCSIG;
34560Sstevel@tonic-gate 	} else if (sig && sig != L->lwp_status.pr_cursig) {
34570Sstevel@tonic-gate 		/* make current signal */
34580Sstevel@tonic-gate 		siginfo_t *infop;
34590Sstevel@tonic-gate 
34600Sstevel@tonic-gate 		*ctlp++ = PCSSIG;
34610Sstevel@tonic-gate 		infop = (siginfo_t *)ctlp;
34620Sstevel@tonic-gate 		(void) memset(infop, 0, sizeof (*infop));
34630Sstevel@tonic-gate 		infop->si_signo = sig;
34640Sstevel@tonic-gate 		ctlp += sizeof (siginfo_t) / sizeof (long);
34650Sstevel@tonic-gate 	}
34660Sstevel@tonic-gate 
34670Sstevel@tonic-gate 	*ctlp++ = PCRUN;
34680Sstevel@tonic-gate 	*ctlp++ = flags;
34690Sstevel@tonic-gate 	size = (char *)ctlp - (char *)ctl;
34700Sstevel@tonic-gate 
34710Sstevel@tonic-gate 	L->lwp_proc->info_valid = 0; /* will need to update map and file info */
34720Sstevel@tonic-gate 	L->lwp_proc->state = PS_RUN;
34730Sstevel@tonic-gate 	L->lwp_state = PS_RUN;
34740Sstevel@tonic-gate 
34750Sstevel@tonic-gate 	if (write(ctlfd, ctl, size) != size) {
34760Sstevel@tonic-gate 		/* Pretend that a job-stopped LWP is running */
34770Sstevel@tonic-gate 		if (errno != EBUSY || L->lwp_status.pr_why != PR_JOBCONTROL)
34780Sstevel@tonic-gate 			return (Lstopstatus(L, PCNULL, 0));
34790Sstevel@tonic-gate 	}
34800Sstevel@tonic-gate 
34810Sstevel@tonic-gate 	return (0);
34820Sstevel@tonic-gate }
34830Sstevel@tonic-gate 
34840Sstevel@tonic-gate int
34850Sstevel@tonic-gate Lclearsig(struct ps_lwphandle *L)
34860Sstevel@tonic-gate {
34870Sstevel@tonic-gate 	int ctlfd = L->lwp_ctlfd;
34880Sstevel@tonic-gate 	long ctl = PCCSIG;
34890Sstevel@tonic-gate 
34900Sstevel@tonic-gate 	if (write(ctlfd, &ctl, sizeof (ctl)) != sizeof (ctl))
34910Sstevel@tonic-gate 		return (-1);
34920Sstevel@tonic-gate 	L->lwp_status.pr_cursig = 0;
34930Sstevel@tonic-gate 	return (0);
34940Sstevel@tonic-gate }
34950Sstevel@tonic-gate 
34960Sstevel@tonic-gate int
34970Sstevel@tonic-gate Lclearfault(struct ps_lwphandle *L)
34980Sstevel@tonic-gate {
34990Sstevel@tonic-gate 	int ctlfd = L->lwp_ctlfd;
35000Sstevel@tonic-gate 	long ctl = PCCFAULT;
35010Sstevel@tonic-gate 
35020Sstevel@tonic-gate 	if (write(ctlfd, &ctl, sizeof (ctl)) != sizeof (ctl))
35030Sstevel@tonic-gate 		return (-1);
35040Sstevel@tonic-gate 	return (0);
35050Sstevel@tonic-gate }
35060Sstevel@tonic-gate 
35070Sstevel@tonic-gate /*
35080Sstevel@tonic-gate  * Step over a breakpoint, i.e., execute the instruction that
35090Sstevel@tonic-gate  * really belongs at the breakpoint location (the current %pc)
35100Sstevel@tonic-gate  * and leave the LWP stopped at the next instruction.
35110Sstevel@tonic-gate  */
35120Sstevel@tonic-gate int
35130Sstevel@tonic-gate Lxecbkpt(struct ps_lwphandle *L, ulong_t saved)
35140Sstevel@tonic-gate {
35150Sstevel@tonic-gate 	struct ps_prochandle *P = L->lwp_proc;
35160Sstevel@tonic-gate 	int rv, error;
35170Sstevel@tonic-gate 
35180Sstevel@tonic-gate 	if (L->lwp_state != PS_STOP) {
35190Sstevel@tonic-gate 		errno = EBUSY;
35200Sstevel@tonic-gate 		return (-1);
35210Sstevel@tonic-gate 	}
35220Sstevel@tonic-gate 
35230Sstevel@tonic-gate 	Lsync(L);
35240Sstevel@tonic-gate 	error = execute_bkpt(L->lwp_ctlfd,
35254753Srh87107 	    &P->status.pr_flttrace, &L->lwp_status.pr_lwphold,
35264753Srh87107 	    L->lwp_status.pr_reg[R_PC], saved);
35270Sstevel@tonic-gate 	rv = Lstopstatus(L, PCNULL, 0);
35280Sstevel@tonic-gate 
35290Sstevel@tonic-gate 	if (error != 0) {
35300Sstevel@tonic-gate 		if (L->lwp_status.pr_why == PR_JOBCONTROL &&
35310Sstevel@tonic-gate 		    error == EBUSY) {	/* jobcontrol stop -- back off */
35320Sstevel@tonic-gate 			L->lwp_state = PS_RUN;
35330Sstevel@tonic-gate 			return (0);
35340Sstevel@tonic-gate 		}
35350Sstevel@tonic-gate 		if (error == ENOENT)
35360Sstevel@tonic-gate 			return (0);
35370Sstevel@tonic-gate 		errno = error;
35380Sstevel@tonic-gate 		return (-1);
35390Sstevel@tonic-gate 	}
35400Sstevel@tonic-gate 
35410Sstevel@tonic-gate 	return (rv);
35420Sstevel@tonic-gate }
35430Sstevel@tonic-gate 
35440Sstevel@tonic-gate /*
35450Sstevel@tonic-gate  * Step over a watchpoint, i.e., execute the instruction that was stopped by
35460Sstevel@tonic-gate  * the watchpoint, and then leave the LWP stopped at the next instruction.
35470Sstevel@tonic-gate  */
35480Sstevel@tonic-gate int
35490Sstevel@tonic-gate Lxecwapt(struct ps_lwphandle *L, const prwatch_t *wp)
35500Sstevel@tonic-gate {
35510Sstevel@tonic-gate 	struct ps_prochandle *P = L->lwp_proc;
35520Sstevel@tonic-gate 	int rv, error;
35530Sstevel@tonic-gate 
35540Sstevel@tonic-gate 	if (L->lwp_state != PS_STOP) {
35550Sstevel@tonic-gate 		errno = EBUSY;
35560Sstevel@tonic-gate 		return (-1);
35570Sstevel@tonic-gate 	}
35580Sstevel@tonic-gate 
35590Sstevel@tonic-gate 	Lsync(L);
35600Sstevel@tonic-gate 	error = execute_wapt(L->lwp_ctlfd,
35614753Srh87107 	    &P->status.pr_flttrace, &L->lwp_status.pr_lwphold, wp);
35620Sstevel@tonic-gate 	rv = Lstopstatus(L, PCNULL, 0);
35630Sstevel@tonic-gate 
35640Sstevel@tonic-gate 	if (error != 0) {
35650Sstevel@tonic-gate 		if (L->lwp_status.pr_why == PR_JOBCONTROL &&
35660Sstevel@tonic-gate 		    error == EBUSY) {	/* jobcontrol stop -- back off */
35670Sstevel@tonic-gate 			L->lwp_state = PS_RUN;
35680Sstevel@tonic-gate 			return (0);
35690Sstevel@tonic-gate 		}
35700Sstevel@tonic-gate 		if (error == ENOENT)
35710Sstevel@tonic-gate 			return (0);
35720Sstevel@tonic-gate 		errno = error;
35730Sstevel@tonic-gate 		return (-1);
35740Sstevel@tonic-gate 	}
35750Sstevel@tonic-gate 
35760Sstevel@tonic-gate 	return (rv);
35770Sstevel@tonic-gate }
35780Sstevel@tonic-gate 
35790Sstevel@tonic-gate int
35800Sstevel@tonic-gate Lstack(struct ps_lwphandle *L, stack_t *stkp)
35810Sstevel@tonic-gate {
35820Sstevel@tonic-gate 	struct ps_prochandle *P = L->lwp_proc;
35830Sstevel@tonic-gate 	uintptr_t addr = L->lwp_status.pr_ustack;
35840Sstevel@tonic-gate 
35850Sstevel@tonic-gate 	if (P->status.pr_dmodel == PR_MODEL_NATIVE) {
35860Sstevel@tonic-gate 		if (Pread(P, stkp, sizeof (*stkp), addr) != sizeof (*stkp))
35870Sstevel@tonic-gate 			return (-1);
35880Sstevel@tonic-gate #ifdef _LP64
35890Sstevel@tonic-gate 	} else {
35900Sstevel@tonic-gate 		stack32_t stk32;
35910Sstevel@tonic-gate 
35920Sstevel@tonic-gate 		if (Pread(P, &stk32, sizeof (stk32), addr) != sizeof (stk32))
35930Sstevel@tonic-gate 			return (-1);
35940Sstevel@tonic-gate 
35950Sstevel@tonic-gate 		stack_32_to_n(&stk32, stkp);
35960Sstevel@tonic-gate #endif
35970Sstevel@tonic-gate 	}
35980Sstevel@tonic-gate 
35990Sstevel@tonic-gate 	return (0);
36000Sstevel@tonic-gate }
36010Sstevel@tonic-gate 
36020Sstevel@tonic-gate int
36030Sstevel@tonic-gate Lmain_stack(struct ps_lwphandle *L, stack_t *stkp)
36040Sstevel@tonic-gate {
36050Sstevel@tonic-gate 	struct ps_prochandle *P = L->lwp_proc;
36060Sstevel@tonic-gate 
36070Sstevel@tonic-gate 	if (Lstack(L, stkp) != 0)
36080Sstevel@tonic-gate 		return (-1);
36090Sstevel@tonic-gate 
36100Sstevel@tonic-gate 	/*
36110Sstevel@tonic-gate 	 * If the SS_ONSTACK flag is set then this LWP is operating on the
36120Sstevel@tonic-gate 	 * alternate signal stack. We can recover the original stack from
36130Sstevel@tonic-gate 	 * pr_oldcontext.
36140Sstevel@tonic-gate 	 */
36150Sstevel@tonic-gate 	if (!(stkp->ss_flags & SS_ONSTACK))
36160Sstevel@tonic-gate 		return (0);
36170Sstevel@tonic-gate 
36180Sstevel@tonic-gate 	if (P->status.pr_dmodel == PR_MODEL_NATIVE) {
36190Sstevel@tonic-gate 		ucontext_t *ctxp = (void *)L->lwp_status.pr_oldcontext;
36200Sstevel@tonic-gate 
36210Sstevel@tonic-gate 		if (Pread(P, stkp, sizeof (*stkp),
36220Sstevel@tonic-gate 		    (uintptr_t)&ctxp->uc_stack) != sizeof (*stkp))
36230Sstevel@tonic-gate 			return (-1);
36240Sstevel@tonic-gate #ifdef _LP64
36250Sstevel@tonic-gate 	} else {
36260Sstevel@tonic-gate 		ucontext32_t *ctxp = (void *)L->lwp_status.pr_oldcontext;
36270Sstevel@tonic-gate 		stack32_t stk32;
36280Sstevel@tonic-gate 
36290Sstevel@tonic-gate 		if (Pread(P, &stk32, sizeof (stk32),
36300Sstevel@tonic-gate 		    (uintptr_t)&ctxp->uc_stack) != sizeof (stk32))
36310Sstevel@tonic-gate 			return (-1);
36320Sstevel@tonic-gate 
36330Sstevel@tonic-gate 		stack_32_to_n(&stk32, stkp);
36340Sstevel@tonic-gate #endif
36350Sstevel@tonic-gate 	}
36360Sstevel@tonic-gate 
36370Sstevel@tonic-gate 	return (0);
36380Sstevel@tonic-gate }
36390Sstevel@tonic-gate 
36400Sstevel@tonic-gate int
36410Sstevel@tonic-gate Lalt_stack(struct ps_lwphandle *L, stack_t *stkp)
36420Sstevel@tonic-gate {
36430Sstevel@tonic-gate 	if (L->lwp_status.pr_altstack.ss_flags & SS_DISABLE) {
36440Sstevel@tonic-gate 		errno = ENODATA;
36450Sstevel@tonic-gate 		return (-1);
36460Sstevel@tonic-gate 	}
36470Sstevel@tonic-gate 
36480Sstevel@tonic-gate 	*stkp = L->lwp_status.pr_altstack;
36490Sstevel@tonic-gate 
36500Sstevel@tonic-gate 	return (0);
36510Sstevel@tonic-gate }
36520Sstevel@tonic-gate 
36530Sstevel@tonic-gate /*
36540Sstevel@tonic-gate  * Add a mapping to the given proc handle.  Resizes the array as appropriate and
36550Sstevel@tonic-gate  * manages reference counts on the given file_info_t.
36560Sstevel@tonic-gate  *
36570Sstevel@tonic-gate  * The 'map_relocate' member is used to tell Psort_mappings() that the
36580Sstevel@tonic-gate  * associated file_map pointer needs to be relocated after the mappings have
36590Sstevel@tonic-gate  * been sorted.  It is only set for the first mapping, and has no meaning
36600Sstevel@tonic-gate  * outside these two functions.
36610Sstevel@tonic-gate  */
36620Sstevel@tonic-gate int
36630Sstevel@tonic-gate Padd_mapping(struct ps_prochandle *P, off64_t off, file_info_t *fp,
36640Sstevel@tonic-gate     prmap_t *pmap)
36650Sstevel@tonic-gate {
36660Sstevel@tonic-gate 	map_info_t *mp;
36670Sstevel@tonic-gate 
36680Sstevel@tonic-gate 	if (P->map_count == P->map_alloc) {
36690Sstevel@tonic-gate 		size_t next = P->map_alloc ? P->map_alloc * 2 : 16;
36700Sstevel@tonic-gate 
36710Sstevel@tonic-gate 		if ((P->mappings = realloc(P->mappings,
36720Sstevel@tonic-gate 		    next * sizeof (map_info_t))) == NULL)
36730Sstevel@tonic-gate 			return (-1);
36740Sstevel@tonic-gate 
36750Sstevel@tonic-gate 		P->map_alloc = next;
36760Sstevel@tonic-gate 	}
36770Sstevel@tonic-gate 
36780Sstevel@tonic-gate 	mp = &P->mappings[P->map_count++];
36790Sstevel@tonic-gate 
36800Sstevel@tonic-gate 	mp->map_offset = off;
36810Sstevel@tonic-gate 	mp->map_pmap = *pmap;
36820Sstevel@tonic-gate 	mp->map_relocate = 0;
36830Sstevel@tonic-gate 	if ((mp->map_file = fp) != NULL) {
36840Sstevel@tonic-gate 		if (fp->file_map == NULL) {
36850Sstevel@tonic-gate 			fp->file_map = mp;
36860Sstevel@tonic-gate 			mp->map_relocate = 1;
36870Sstevel@tonic-gate 		}
36880Sstevel@tonic-gate 		fp->file_ref++;
36890Sstevel@tonic-gate 	}
36900Sstevel@tonic-gate 
36910Sstevel@tonic-gate 	return (0);
36920Sstevel@tonic-gate }
36930Sstevel@tonic-gate 
36940Sstevel@tonic-gate static int
36950Sstevel@tonic-gate map_sort(const void *a, const void *b)
36960Sstevel@tonic-gate {
36970Sstevel@tonic-gate 	const map_info_t *ap = a, *bp = b;
36980Sstevel@tonic-gate 
36990Sstevel@tonic-gate 	if (ap->map_pmap.pr_vaddr < bp->map_pmap.pr_vaddr)
37000Sstevel@tonic-gate 		return (-1);
37010Sstevel@tonic-gate 	else if (ap->map_pmap.pr_vaddr > bp->map_pmap.pr_vaddr)
37020Sstevel@tonic-gate 		return (1);
37030Sstevel@tonic-gate 	else
37040Sstevel@tonic-gate 		return (0);
37050Sstevel@tonic-gate }
37060Sstevel@tonic-gate 
37070Sstevel@tonic-gate /*
37080Sstevel@tonic-gate  * Sort the current set of mappings.  Should be called during target
37090Sstevel@tonic-gate  * initialization after all calls to Padd_mapping() have been made.
37100Sstevel@tonic-gate  */
37110Sstevel@tonic-gate void
37120Sstevel@tonic-gate Psort_mappings(struct ps_prochandle *P)
37130Sstevel@tonic-gate {
37140Sstevel@tonic-gate 	int i;
37150Sstevel@tonic-gate 	map_info_t *mp;
37160Sstevel@tonic-gate 
37170Sstevel@tonic-gate 	qsort(P->mappings, P->map_count, sizeof (map_info_t), map_sort);
37180Sstevel@tonic-gate 
37190Sstevel@tonic-gate 	/*
37200Sstevel@tonic-gate 	 * Update all the file_map pointers to refer to the new locations.
37210Sstevel@tonic-gate 	 */
37220Sstevel@tonic-gate 	for (i = 0; i < P->map_count; i++) {
37230Sstevel@tonic-gate 		mp = &P->mappings[i];
37240Sstevel@tonic-gate 		if (mp->map_relocate)
37250Sstevel@tonic-gate 			mp->map_file->file_map = mp;
37260Sstevel@tonic-gate 		mp->map_relocate = 0;
37270Sstevel@tonic-gate 	}
37280Sstevel@tonic-gate }
3729