xref: /titanic_44/usr/src/lib/libproc/common/Pcontrol.c (revision 34bdffbf3e3c188027e767e631f717b10159316d)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
59acbbeafSnn35248  * Common Development and Distribution License (the "License").
69acbbeafSnn35248  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21e4586ebfSmws 
227c478bd9Sstevel@tonic-gate /*
238fd04b83SRoger A. Faulkner  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25cd11e192Sjhaslam  *
26cd11e192Sjhaslam  * Portions Copyright 2007 Chad Mynhier
27*34bdffbfSGarrett D'Amore  * Copyright 2012 DEY Storage Systems, Inc.  All rights reserved.
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
30d9452f23SEdward Pilatowicz #include <assert.h>
317c478bd9Sstevel@tonic-gate #include <stdio.h>
327c478bd9Sstevel@tonic-gate #include <stdlib.h>
337c478bd9Sstevel@tonic-gate #include <unistd.h>
347c478bd9Sstevel@tonic-gate #include <ctype.h>
357c478bd9Sstevel@tonic-gate #include <fcntl.h>
367c478bd9Sstevel@tonic-gate #include <string.h>
379acbbeafSnn35248 #include <strings.h>
387c478bd9Sstevel@tonic-gate #include <memory.h>
397c478bd9Sstevel@tonic-gate #include <errno.h>
407c478bd9Sstevel@tonic-gate #include <dirent.h>
417c478bd9Sstevel@tonic-gate #include <limits.h>
427c478bd9Sstevel@tonic-gate #include <signal.h>
43cb620785Sraf #include <atomic.h>
447c478bd9Sstevel@tonic-gate #include <sys/types.h>
457c478bd9Sstevel@tonic-gate #include <sys/uio.h>
467c478bd9Sstevel@tonic-gate #include <sys/stat.h>
477c478bd9Sstevel@tonic-gate #include <sys/resource.h>
487c478bd9Sstevel@tonic-gate #include <sys/param.h>
497c478bd9Sstevel@tonic-gate #include <sys/stack.h>
507c478bd9Sstevel@tonic-gate #include <sys/fault.h>
517c478bd9Sstevel@tonic-gate #include <sys/syscall.h>
527c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate #include "libproc.h"
557c478bd9Sstevel@tonic-gate #include "Pcontrol.h"
567c478bd9Sstevel@tonic-gate #include "Putil.h"
577c478bd9Sstevel@tonic-gate #include "P32ton.h"
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate int	_libproc_debug;		/* set non-zero to enable debugging printfs */
60d7755b5aSrh87107 int	_libproc_no_qsort;	/* set non-zero to inhibit sorting */
61d7755b5aSrh87107 				/* of symbol tables */
62d9452f23SEdward Pilatowicz int	_libproc_incore_elf;	/* only use in-core elf data */
63d7755b5aSrh87107 
647c478bd9Sstevel@tonic-gate sigset_t blockable_sigs;	/* signals to block when we need to be safe */
657c478bd9Sstevel@tonic-gate static	int	minfd;	/* minimum file descriptor returned by dupfd(fd, 0) */
669acbbeafSnn35248 char	procfs_path[PATH_MAX] = "/proc";
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate /*
697c478bd9Sstevel@tonic-gate  * Function prototypes for static routines in this module.
707c478bd9Sstevel@tonic-gate  */
717c478bd9Sstevel@tonic-gate static	void	deadcheck(struct ps_prochandle *);
727c478bd9Sstevel@tonic-gate static	void	restore_tracing_flags(struct ps_prochandle *);
737c478bd9Sstevel@tonic-gate static	void	Lfree_internal(struct ps_prochandle *, struct ps_lwphandle *);
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate /*
767c478bd9Sstevel@tonic-gate  * Read/write interface for live processes: just pread/pwrite the
777c478bd9Sstevel@tonic-gate  * /proc/<pid>/as file:
787c478bd9Sstevel@tonic-gate  */
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate static ssize_t
817c478bd9Sstevel@tonic-gate Pread_live(struct ps_prochandle *P, void *buf, size_t n, uintptr_t addr)
827c478bd9Sstevel@tonic-gate {
837c478bd9Sstevel@tonic-gate 	return (pread(P->asfd, buf, n, (off_t)addr));
847c478bd9Sstevel@tonic-gate }
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate static ssize_t
877c478bd9Sstevel@tonic-gate Pwrite_live(struct ps_prochandle *P, const void *buf, size_t n, uintptr_t addr)
887c478bd9Sstevel@tonic-gate {
897c478bd9Sstevel@tonic-gate 	return (pwrite(P->asfd, buf, n, (off_t)addr));
907c478bd9Sstevel@tonic-gate }
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate static const ps_rwops_t P_live_ops = { Pread_live, Pwrite_live };
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate /*
957c478bd9Sstevel@tonic-gate  * This is the library's .init handler.
967c478bd9Sstevel@tonic-gate  */
977c478bd9Sstevel@tonic-gate #pragma init(_libproc_init)
987c478bd9Sstevel@tonic-gate void
997c478bd9Sstevel@tonic-gate _libproc_init(void)
1007c478bd9Sstevel@tonic-gate {
1017c478bd9Sstevel@tonic-gate 	_libproc_debug = getenv("LIBPROC_DEBUG") != NULL;
102d7755b5aSrh87107 	_libproc_no_qsort = getenv("LIBPROC_NO_QSORT") != NULL;
103d9452f23SEdward Pilatowicz 	_libproc_incore_elf = getenv("LIBPROC_INCORE_ELF") != NULL;
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 	(void) sigfillset(&blockable_sigs);
1067c478bd9Sstevel@tonic-gate 	(void) sigdelset(&blockable_sigs, SIGKILL);
1077c478bd9Sstevel@tonic-gate 	(void) sigdelset(&blockable_sigs, SIGSTOP);
1087c478bd9Sstevel@tonic-gate }
1097c478bd9Sstevel@tonic-gate 
1109acbbeafSnn35248 void
1119acbbeafSnn35248 Pset_procfs_path(const char *path)
1129acbbeafSnn35248 {
1139acbbeafSnn35248 	(void) snprintf(procfs_path, sizeof (procfs_path), "%s", path);
1149acbbeafSnn35248 }
1159acbbeafSnn35248 
1167c478bd9Sstevel@tonic-gate /*
1177c478bd9Sstevel@tonic-gate  * Call set_minfd() once before calling dupfd() several times.
1187c478bd9Sstevel@tonic-gate  * We assume that the application will not reduce its current file
1197c478bd9Sstevel@tonic-gate  * descriptor limit lower than 512 once it has set at least that value.
1207c478bd9Sstevel@tonic-gate  */
1217c478bd9Sstevel@tonic-gate int
1227c478bd9Sstevel@tonic-gate set_minfd(void)
1237c478bd9Sstevel@tonic-gate {
1247c478bd9Sstevel@tonic-gate 	static mutex_t minfd_lock = DEFAULTMUTEX;
1257c478bd9Sstevel@tonic-gate 	struct rlimit rlim;
1267c478bd9Sstevel@tonic-gate 	int fd;
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	if ((fd = minfd) < 256) {
1297c478bd9Sstevel@tonic-gate 		(void) mutex_lock(&minfd_lock);
1307c478bd9Sstevel@tonic-gate 		if ((fd = minfd) < 256) {
1317c478bd9Sstevel@tonic-gate 			if (getrlimit(RLIMIT_NOFILE, &rlim) != 0)
1327c478bd9Sstevel@tonic-gate 				rlim.rlim_cur = rlim.rlim_max = 0;
1337c478bd9Sstevel@tonic-gate 			if (rlim.rlim_cur >= 512)
1347c478bd9Sstevel@tonic-gate 				fd = 256;
1357c478bd9Sstevel@tonic-gate 			else if ((fd = rlim.rlim_cur / 2) < 3)
1367c478bd9Sstevel@tonic-gate 				fd = 3;
137cb620785Sraf 			membar_producer();
1387c478bd9Sstevel@tonic-gate 			minfd = fd;
1397c478bd9Sstevel@tonic-gate 		}
1407c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&minfd_lock);
1417c478bd9Sstevel@tonic-gate 	}
1427c478bd9Sstevel@tonic-gate 	return (fd);
1437c478bd9Sstevel@tonic-gate }
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate int
1467c478bd9Sstevel@tonic-gate dupfd(int fd, int dfd)
1477c478bd9Sstevel@tonic-gate {
1487c478bd9Sstevel@tonic-gate 	int mfd;
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	/*
1517c478bd9Sstevel@tonic-gate 	 * Make fd be greater than 255 (the 32-bit stdio limit),
1527c478bd9Sstevel@tonic-gate 	 * or at least make it greater than 2 so that the
1537c478bd9Sstevel@tonic-gate 	 * program will work when spawned by init(1m).
1547c478bd9Sstevel@tonic-gate 	 * Also, if dfd is non-zero, dup the fd to be dfd.
1557c478bd9Sstevel@tonic-gate 	 */
1567c478bd9Sstevel@tonic-gate 	if ((mfd = minfd) == 0)
1577c478bd9Sstevel@tonic-gate 		mfd = set_minfd();
1587c478bd9Sstevel@tonic-gate 	if (dfd > 0 || (0 <= fd && fd < mfd)) {
1597c478bd9Sstevel@tonic-gate 		if (dfd <= 0)
1607c478bd9Sstevel@tonic-gate 			dfd = mfd;
1617c478bd9Sstevel@tonic-gate 		dfd = fcntl(fd, F_DUPFD, dfd);
1627c478bd9Sstevel@tonic-gate 		(void) close(fd);
1637c478bd9Sstevel@tonic-gate 		fd = dfd;
1647c478bd9Sstevel@tonic-gate 	}
1657c478bd9Sstevel@tonic-gate 	/*
1667c478bd9Sstevel@tonic-gate 	 * Mark it close-on-exec so any created process doesn't inherit it.
1677c478bd9Sstevel@tonic-gate 	 */
1687c478bd9Sstevel@tonic-gate 	if (fd >= 0)
1697c478bd9Sstevel@tonic-gate 		(void) fcntl(fd, F_SETFD, FD_CLOEXEC);
1707c478bd9Sstevel@tonic-gate 	return (fd);
1717c478bd9Sstevel@tonic-gate }
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate /*
1747c478bd9Sstevel@tonic-gate  * Create a new controlled process.
1757c478bd9Sstevel@tonic-gate  * Leave it stopped on successful exit from exec() or execve().
1767c478bd9Sstevel@tonic-gate  * Return an opaque pointer to its process control structure.
1777c478bd9Sstevel@tonic-gate  * Return NULL if process cannot be created (fork()/exec() not successful).
1787c478bd9Sstevel@tonic-gate  */
1797c478bd9Sstevel@tonic-gate struct ps_prochandle *
1807c478bd9Sstevel@tonic-gate Pxcreate(const char *file,	/* executable file name */
1817c478bd9Sstevel@tonic-gate 	char *const *argv,	/* argument vector */
1827c478bd9Sstevel@tonic-gate 	char *const *envp,	/* environment */
1837c478bd9Sstevel@tonic-gate 	int *perr,	/* pointer to error return code */
1847c478bd9Sstevel@tonic-gate 	char *path,	/* if non-null, holds exec path name on return */
1857c478bd9Sstevel@tonic-gate 	size_t len)	/* size of the path buffer */
1867c478bd9Sstevel@tonic-gate {
1877c478bd9Sstevel@tonic-gate 	char execpath[PATH_MAX];
1889acbbeafSnn35248 	char procname[PATH_MAX];
1897c478bd9Sstevel@tonic-gate 	struct ps_prochandle *P;
1907c478bd9Sstevel@tonic-gate 	pid_t pid;
1917c478bd9Sstevel@tonic-gate 	int fd;
1927c478bd9Sstevel@tonic-gate 	char *fname;
1937c478bd9Sstevel@tonic-gate 	int rc;
1947c478bd9Sstevel@tonic-gate 	int lasterrno = 0;
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	if (len == 0)	/* zero length, no path */
1977c478bd9Sstevel@tonic-gate 		path = NULL;
1987c478bd9Sstevel@tonic-gate 	if (path != NULL)
1997c478bd9Sstevel@tonic-gate 		*path = '\0';
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	if ((P = malloc(sizeof (struct ps_prochandle))) == NULL) {
2027c478bd9Sstevel@tonic-gate 		*perr = C_STRANGE;
2037c478bd9Sstevel@tonic-gate 		return (NULL);
2047c478bd9Sstevel@tonic-gate 	}
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	if ((pid = fork1()) == -1) {
2077c478bd9Sstevel@tonic-gate 		free(P);
2087c478bd9Sstevel@tonic-gate 		*perr = C_FORK;
2097c478bd9Sstevel@tonic-gate 		return (NULL);
2107c478bd9Sstevel@tonic-gate 	}
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	if (pid == 0) {			/* child process */
2137c478bd9Sstevel@tonic-gate 		id_t id;
2147c478bd9Sstevel@tonic-gate 		extern char **environ;
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 		/*
2177c478bd9Sstevel@tonic-gate 		 * If running setuid or setgid, reset credentials to normal.
2187c478bd9Sstevel@tonic-gate 		 */
2197c478bd9Sstevel@tonic-gate 		if ((id = getgid()) != getegid())
2207c478bd9Sstevel@tonic-gate 			(void) setgid(id);
2217c478bd9Sstevel@tonic-gate 		if ((id = getuid()) != geteuid())
2227c478bd9Sstevel@tonic-gate 			(void) setuid(id);
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 		Pcreate_callback(P);	/* execute callback (see below) */
2257c478bd9Sstevel@tonic-gate 		(void) pause();		/* wait for PRSABORT from parent */
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 		/*
2287c478bd9Sstevel@tonic-gate 		 * This is ugly.  There is no execvep() function that takes a
2297c478bd9Sstevel@tonic-gate 		 * path and an environment.  We cheat here by replacing the
2307c478bd9Sstevel@tonic-gate 		 * global 'environ' variable right before we call this.
2317c478bd9Sstevel@tonic-gate 		 */
2327c478bd9Sstevel@tonic-gate 		if (envp)
2337c478bd9Sstevel@tonic-gate 			environ = (char **)envp;
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 		(void) execvp(file, argv);  /* execute the program */
2367c478bd9Sstevel@tonic-gate 		_exit(127);
2377c478bd9Sstevel@tonic-gate 	}
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 	/*
2407c478bd9Sstevel@tonic-gate 	 * Initialize the process structure.
2417c478bd9Sstevel@tonic-gate 	 */
2427c478bd9Sstevel@tonic-gate 	(void) memset(P, 0, sizeof (*P));
2437c478bd9Sstevel@tonic-gate 	(void) mutex_init(&P->proc_lock, USYNC_THREAD, NULL);
2447c478bd9Sstevel@tonic-gate 	P->flags |= CREATED;
2457c478bd9Sstevel@tonic-gate 	P->state = PS_RUN;
2467c478bd9Sstevel@tonic-gate 	P->pid = pid;
2477c478bd9Sstevel@tonic-gate 	P->asfd = -1;
2487c478bd9Sstevel@tonic-gate 	P->ctlfd = -1;
2497c478bd9Sstevel@tonic-gate 	P->statfd = -1;
2507c478bd9Sstevel@tonic-gate 	P->agentctlfd = -1;
2517c478bd9Sstevel@tonic-gate 	P->agentstatfd = -1;
2527c478bd9Sstevel@tonic-gate 	P->ops = &P_live_ops;
2537c478bd9Sstevel@tonic-gate 	Pinitsym(P);
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 	/*
2567c478bd9Sstevel@tonic-gate 	 * Open the /proc/pid files.
2577c478bd9Sstevel@tonic-gate 	 */
2589acbbeafSnn35248 	(void) snprintf(procname, sizeof (procname), "%s/%d/",
2599acbbeafSnn35248 	    procfs_path, (int)pid);
2607c478bd9Sstevel@tonic-gate 	fname = procname + strlen(procname);
2617c478bd9Sstevel@tonic-gate 	(void) set_minfd();
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	/*
2647c478bd9Sstevel@tonic-gate 	 * Exclusive write open advises others not to interfere.
2657c478bd9Sstevel@tonic-gate 	 * There is no reason for any of these open()s to fail.
2667c478bd9Sstevel@tonic-gate 	 */
2677c478bd9Sstevel@tonic-gate 	(void) strcpy(fname, "as");
2687c478bd9Sstevel@tonic-gate 	if ((fd = open(procname, (O_RDWR|O_EXCL))) < 0 ||
2697c478bd9Sstevel@tonic-gate 	    (fd = dupfd(fd, 0)) < 0) {
2707c478bd9Sstevel@tonic-gate 		dprintf("Pcreate: failed to open %s: %s\n",
2717c478bd9Sstevel@tonic-gate 		    procname, strerror(errno));
2727c478bd9Sstevel@tonic-gate 		rc = C_STRANGE;
2737c478bd9Sstevel@tonic-gate 		goto bad;
2747c478bd9Sstevel@tonic-gate 	}
2757c478bd9Sstevel@tonic-gate 	P->asfd = fd;
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 	(void) strcpy(fname, "status");
2787c478bd9Sstevel@tonic-gate 	if ((fd = open(procname, O_RDONLY)) < 0 ||
2797c478bd9Sstevel@tonic-gate 	    (fd = dupfd(fd, 0)) < 0) {
2807c478bd9Sstevel@tonic-gate 		dprintf("Pcreate: failed to open %s: %s\n",
2817c478bd9Sstevel@tonic-gate 		    procname, strerror(errno));
2827c478bd9Sstevel@tonic-gate 		rc = C_STRANGE;
2837c478bd9Sstevel@tonic-gate 		goto bad;
2847c478bd9Sstevel@tonic-gate 	}
2857c478bd9Sstevel@tonic-gate 	P->statfd = fd;
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 	(void) strcpy(fname, "ctl");
2887c478bd9Sstevel@tonic-gate 	if ((fd = open(procname, O_WRONLY)) < 0 ||
2897c478bd9Sstevel@tonic-gate 	    (fd = dupfd(fd, 0)) < 0) {
2907c478bd9Sstevel@tonic-gate 		dprintf("Pcreate: failed to open %s: %s\n",
2917c478bd9Sstevel@tonic-gate 		    procname, strerror(errno));
2927c478bd9Sstevel@tonic-gate 		rc = C_STRANGE;
2937c478bd9Sstevel@tonic-gate 		goto bad;
2947c478bd9Sstevel@tonic-gate 	}
2957c478bd9Sstevel@tonic-gate 	P->ctlfd = fd;
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 	(void) Pstop(P, 0);	/* stop the controlled process */
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	/*
3007c478bd9Sstevel@tonic-gate 	 * Wait for process to sleep in pause().
3017c478bd9Sstevel@tonic-gate 	 * If the process has already called pause(), then it should be
3027c478bd9Sstevel@tonic-gate 	 * stopped (PR_REQUESTED) while asleep in pause and we are done.
3037c478bd9Sstevel@tonic-gate 	 * Else we set up to catch entry/exit to pause() and set the process
3047c478bd9Sstevel@tonic-gate 	 * running again, expecting it to stop when it reaches pause().
3057c478bd9Sstevel@tonic-gate 	 * There is no reason for this to fail other than an interrupt.
3067c478bd9Sstevel@tonic-gate 	 */
3077c478bd9Sstevel@tonic-gate 	(void) Psysentry(P, SYS_pause, 1);
3087c478bd9Sstevel@tonic-gate 	(void) Psysexit(P, SYS_pause, 1);
3097c478bd9Sstevel@tonic-gate 	for (;;) {
3107c478bd9Sstevel@tonic-gate 		if (P->state == PS_STOP &&
3117c478bd9Sstevel@tonic-gate 		    P->status.pr_lwp.pr_syscall == SYS_pause &&
3127c478bd9Sstevel@tonic-gate 		    (P->status.pr_lwp.pr_why == PR_REQUESTED ||
3137c478bd9Sstevel@tonic-gate 		    P->status.pr_lwp.pr_why == PR_SYSENTRY ||
3147c478bd9Sstevel@tonic-gate 		    P->status.pr_lwp.pr_why == PR_SYSEXIT))
3157c478bd9Sstevel@tonic-gate 			break;
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 		if (P->state != PS_STOP ||	/* interrupt or process died */
3187c478bd9Sstevel@tonic-gate 		    Psetrun(P, 0, 0) != 0) {	/* can't restart */
3197c478bd9Sstevel@tonic-gate 			if (errno == EINTR || errno == ERESTART)
3207c478bd9Sstevel@tonic-gate 				rc = C_INTR;
3217c478bd9Sstevel@tonic-gate 			else {
3227c478bd9Sstevel@tonic-gate 				dprintf("Pcreate: Psetrun failed: %s\n",
3237c478bd9Sstevel@tonic-gate 				    strerror(errno));
3247c478bd9Sstevel@tonic-gate 				rc = C_STRANGE;
3257c478bd9Sstevel@tonic-gate 			}
3267c478bd9Sstevel@tonic-gate 			goto bad;
3277c478bd9Sstevel@tonic-gate 		}
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 		(void) Pwait(P, 0);
3307c478bd9Sstevel@tonic-gate 	}
3317c478bd9Sstevel@tonic-gate 	(void) Psysentry(P, SYS_pause, 0);
3327c478bd9Sstevel@tonic-gate 	(void) Psysexit(P, SYS_pause, 0);
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 	/*
3357c478bd9Sstevel@tonic-gate 	 * Kick the process off the pause() and catch
3367c478bd9Sstevel@tonic-gate 	 * it again on entry to exec() or exit().
3377c478bd9Sstevel@tonic-gate 	 */
3387c478bd9Sstevel@tonic-gate 	(void) Psysentry(P, SYS_exit, 1);
3397c478bd9Sstevel@tonic-gate 	(void) Psysentry(P, SYS_execve, 1);
3407c478bd9Sstevel@tonic-gate 	if (Psetrun(P, 0, PRSABORT) == -1) {
3417c478bd9Sstevel@tonic-gate 		dprintf("Pcreate: Psetrun failed: %s\n", strerror(errno));
3427c478bd9Sstevel@tonic-gate 		rc = C_STRANGE;
3437c478bd9Sstevel@tonic-gate 		goto bad;
3447c478bd9Sstevel@tonic-gate 	}
3457c478bd9Sstevel@tonic-gate 	(void) Pwait(P, 0);
3467c478bd9Sstevel@tonic-gate 	if (P->state != PS_STOP) {
3477c478bd9Sstevel@tonic-gate 		dprintf("Pcreate: Pwait failed: %s\n", strerror(errno));
3487c478bd9Sstevel@tonic-gate 		rc = C_STRANGE;
3497c478bd9Sstevel@tonic-gate 		goto bad;
3507c478bd9Sstevel@tonic-gate 	}
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 	/*
3537c478bd9Sstevel@tonic-gate 	 * Move the process through instances of failed exec()s
3547c478bd9Sstevel@tonic-gate 	 * to reach the point of stopped on successful exec().
3557c478bd9Sstevel@tonic-gate 	 */
3567c478bd9Sstevel@tonic-gate 	(void) Psysexit(P, SYS_execve, TRUE);
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	while (P->state == PS_STOP &&
3597c478bd9Sstevel@tonic-gate 	    P->status.pr_lwp.pr_why == PR_SYSENTRY &&
3608fd04b83SRoger A. Faulkner 	    P->status.pr_lwp.pr_what == SYS_execve) {
3617c478bd9Sstevel@tonic-gate 		/*
3627c478bd9Sstevel@tonic-gate 		 * Fetch the exec path name now, before we complete
3637c478bd9Sstevel@tonic-gate 		 * the exec().  We may lose the process and be unable
3647c478bd9Sstevel@tonic-gate 		 * to get the information later.
3657c478bd9Sstevel@tonic-gate 		 */
3667c478bd9Sstevel@tonic-gate 		(void) Pread_string(P, execpath, sizeof (execpath),
3677c478bd9Sstevel@tonic-gate 		    (off_t)P->status.pr_lwp.pr_sysarg[0]);
3687c478bd9Sstevel@tonic-gate 		if (path != NULL)
3697c478bd9Sstevel@tonic-gate 			(void) strncpy(path, execpath, len);
3707c478bd9Sstevel@tonic-gate 		/*
3717c478bd9Sstevel@tonic-gate 		 * Set the process running and wait for
3727c478bd9Sstevel@tonic-gate 		 * it to stop on exit from the exec().
3737c478bd9Sstevel@tonic-gate 		 */
3747c478bd9Sstevel@tonic-gate 		(void) Psetrun(P, 0, 0);
3757c478bd9Sstevel@tonic-gate 		(void) Pwait(P, 0);
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 		if (P->state == PS_LOST &&		/* we lost control */
3787c478bd9Sstevel@tonic-gate 		    Preopen(P) != 0) {		/* and we can't get it back */
3797c478bd9Sstevel@tonic-gate 			rc = C_PERM;
3807c478bd9Sstevel@tonic-gate 			goto bad;
3817c478bd9Sstevel@tonic-gate 		}
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate 		/*
3847c478bd9Sstevel@tonic-gate 		 * If the exec() failed, continue the loop, expecting
3857c478bd9Sstevel@tonic-gate 		 * there to be more attempts to exec(), based on PATH.
3867c478bd9Sstevel@tonic-gate 		 */
3877c478bd9Sstevel@tonic-gate 		if (P->state == PS_STOP &&
3887c478bd9Sstevel@tonic-gate 		    P->status.pr_lwp.pr_why == PR_SYSEXIT &&
3898fd04b83SRoger A. Faulkner 		    P->status.pr_lwp.pr_what == SYS_execve &&
3907c478bd9Sstevel@tonic-gate 		    (lasterrno = P->status.pr_lwp.pr_errno) != 0) {
3917c478bd9Sstevel@tonic-gate 			/*
3927c478bd9Sstevel@tonic-gate 			 * The exec() failed.  Set the process running and
3937c478bd9Sstevel@tonic-gate 			 * wait for it to stop on entry to the next exec().
3947c478bd9Sstevel@tonic-gate 			 */
3957c478bd9Sstevel@tonic-gate 			(void) Psetrun(P, 0, 0);
3967c478bd9Sstevel@tonic-gate 			(void) Pwait(P, 0);
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 			continue;
3997c478bd9Sstevel@tonic-gate 		}
4007c478bd9Sstevel@tonic-gate 		break;
4017c478bd9Sstevel@tonic-gate 	}
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 	if (P->state == PS_STOP &&
4047c478bd9Sstevel@tonic-gate 	    P->status.pr_lwp.pr_why == PR_SYSEXIT &&
4058fd04b83SRoger A. Faulkner 	    P->status.pr_lwp.pr_what == SYS_execve &&
4067c478bd9Sstevel@tonic-gate 	    P->status.pr_lwp.pr_errno == 0) {
4077c478bd9Sstevel@tonic-gate 		/*
4087c478bd9Sstevel@tonic-gate 		 * The process is stopped on successful exec() or execve().
4097c478bd9Sstevel@tonic-gate 		 * Turn off all tracing flags and return success.
4107c478bd9Sstevel@tonic-gate 		 */
4117c478bd9Sstevel@tonic-gate 		restore_tracing_flags(P);
4127c478bd9Sstevel@tonic-gate #ifndef _LP64
4137c478bd9Sstevel@tonic-gate 		/* We must be a 64-bit process to deal with a 64-bit process */
4147c478bd9Sstevel@tonic-gate 		if (P->status.pr_dmodel == PR_MODEL_LP64) {
4157c478bd9Sstevel@tonic-gate 			rc = C_LP64;
4167c478bd9Sstevel@tonic-gate 			goto bad;
4177c478bd9Sstevel@tonic-gate 		}
4187c478bd9Sstevel@tonic-gate #endif
4197c478bd9Sstevel@tonic-gate 		/*
4207c478bd9Sstevel@tonic-gate 		 * Set run-on-last-close so the controlled process
4217c478bd9Sstevel@tonic-gate 		 * runs even if we die on a signal.
4227c478bd9Sstevel@tonic-gate 		 */
4237c478bd9Sstevel@tonic-gate 		(void) Psetflags(P, PR_RLC);
4247c478bd9Sstevel@tonic-gate 		*perr = 0;
4257c478bd9Sstevel@tonic-gate 		return (P);
4267c478bd9Sstevel@tonic-gate 	}
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 	rc = lasterrno == ENOENT ? C_NOENT : C_NOEXEC;
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate bad:
4317c478bd9Sstevel@tonic-gate 	(void) kill(pid, SIGKILL);
4327c478bd9Sstevel@tonic-gate 	if (path != NULL && rc != C_PERM && rc != C_LP64)
4337c478bd9Sstevel@tonic-gate 		*path = '\0';
4347c478bd9Sstevel@tonic-gate 	Pfree(P);
4357c478bd9Sstevel@tonic-gate 	*perr = rc;
4367c478bd9Sstevel@tonic-gate 	return (NULL);
4377c478bd9Sstevel@tonic-gate }
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate struct ps_prochandle *
4407c478bd9Sstevel@tonic-gate Pcreate(
4417c478bd9Sstevel@tonic-gate 	const char *file,	/* executable file name */
4427c478bd9Sstevel@tonic-gate 	char *const *argv,	/* argument vector */
4437c478bd9Sstevel@tonic-gate 	int *perr,	/* pointer to error return code */
4447c478bd9Sstevel@tonic-gate 	char *path,	/* if non-null, holds exec path name on return */
4457c478bd9Sstevel@tonic-gate 	size_t len)	/* size of the path buffer */
4467c478bd9Sstevel@tonic-gate {
4477c478bd9Sstevel@tonic-gate 	return (Pxcreate(file, argv, NULL, perr, path, len));
4487c478bd9Sstevel@tonic-gate }
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate /*
4517c478bd9Sstevel@tonic-gate  * Return a printable string corresponding to a Pcreate() error return.
4527c478bd9Sstevel@tonic-gate  */
4537c478bd9Sstevel@tonic-gate const char *
4547c478bd9Sstevel@tonic-gate Pcreate_error(int error)
4557c478bd9Sstevel@tonic-gate {
4567c478bd9Sstevel@tonic-gate 	const char *str;
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 	switch (error) {
4597c478bd9Sstevel@tonic-gate 	case C_FORK:
4607c478bd9Sstevel@tonic-gate 		str = "cannot fork";
4617c478bd9Sstevel@tonic-gate 		break;
4627c478bd9Sstevel@tonic-gate 	case C_PERM:
4637c478bd9Sstevel@tonic-gate 		str = "file is set-id or unreadable";
4647c478bd9Sstevel@tonic-gate 		break;
4657c478bd9Sstevel@tonic-gate 	case C_NOEXEC:
4667c478bd9Sstevel@tonic-gate 		str = "cannot execute file";
4677c478bd9Sstevel@tonic-gate 		break;
4687c478bd9Sstevel@tonic-gate 	case C_INTR:
4697c478bd9Sstevel@tonic-gate 		str = "operation interrupted";
4707c478bd9Sstevel@tonic-gate 		break;
4717c478bd9Sstevel@tonic-gate 	case C_LP64:
4727c478bd9Sstevel@tonic-gate 		str = "program is _LP64, self is not";
4737c478bd9Sstevel@tonic-gate 		break;
4747c478bd9Sstevel@tonic-gate 	case C_STRANGE:
4757c478bd9Sstevel@tonic-gate 		str = "unanticipated system error";
4767c478bd9Sstevel@tonic-gate 		break;
4777c478bd9Sstevel@tonic-gate 	case C_NOENT:
4787c478bd9Sstevel@tonic-gate 		str = "cannot find executable file";
4797c478bd9Sstevel@tonic-gate 		break;
4807c478bd9Sstevel@tonic-gate 	default:
4817c478bd9Sstevel@tonic-gate 		str = "unknown error";
4827c478bd9Sstevel@tonic-gate 		break;
4837c478bd9Sstevel@tonic-gate 	}
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 	return (str);
4867c478bd9Sstevel@tonic-gate }
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate /*
4897c478bd9Sstevel@tonic-gate  * Callback to execute in each child process created with Pcreate() after fork
4907c478bd9Sstevel@tonic-gate  * but before it execs the new process image.  By default, we do nothing, but
4917c478bd9Sstevel@tonic-gate  * by calling this function we allow the client program to define its own
4927c478bd9Sstevel@tonic-gate  * version of the function which will interpose on our empty default.  This
4937c478bd9Sstevel@tonic-gate  * may be useful for clients that need to modify signal dispositions, terminal
4947c478bd9Sstevel@tonic-gate  * attributes, or process group and session properties for each new victim.
4957c478bd9Sstevel@tonic-gate  */
4967c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4977c478bd9Sstevel@tonic-gate void
4987c478bd9Sstevel@tonic-gate Pcreate_callback(struct ps_prochandle *P)
4997c478bd9Sstevel@tonic-gate {
5007c478bd9Sstevel@tonic-gate 	/* nothing to do here */
5017c478bd9Sstevel@tonic-gate }
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate /*
5047c478bd9Sstevel@tonic-gate  * Grab an existing process.
5057c478bd9Sstevel@tonic-gate  * Return an opaque pointer to its process control structure.
5067c478bd9Sstevel@tonic-gate  *
5077c478bd9Sstevel@tonic-gate  * pid:		UNIX process ID.
5087c478bd9Sstevel@tonic-gate  * flags:
5097c478bd9Sstevel@tonic-gate  *	PGRAB_RETAIN	Retain tracing flags (default clears all tracing flags).
5107c478bd9Sstevel@tonic-gate  *	PGRAB_FORCE	Grab regardless of whether process is already traced.
5117c478bd9Sstevel@tonic-gate  *	PGRAB_RDONLY	Open the address space file O_RDONLY instead of O_RDWR,
5127c478bd9Sstevel@tonic-gate  *                      and do not open the process control file.
5137c478bd9Sstevel@tonic-gate  *	PGRAB_NOSTOP	Open the process but do not force it to stop.
5147c478bd9Sstevel@tonic-gate  * perr:	pointer to error return code.
5157c478bd9Sstevel@tonic-gate  */
5167c478bd9Sstevel@tonic-gate struct ps_prochandle *
5177c478bd9Sstevel@tonic-gate Pgrab(pid_t pid, int flags, int *perr)
5187c478bd9Sstevel@tonic-gate {
5197c478bd9Sstevel@tonic-gate 	struct ps_prochandle *P;
5207c478bd9Sstevel@tonic-gate 	int fd, omode;
5219acbbeafSnn35248 	char procname[PATH_MAX];
5227c478bd9Sstevel@tonic-gate 	char *fname;
5237c478bd9Sstevel@tonic-gate 	int rc = 0;
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 	/*
5267c478bd9Sstevel@tonic-gate 	 * PGRAB_RDONLY means that we do not open the /proc/<pid>/control file,
5277c478bd9Sstevel@tonic-gate 	 * and so it implies RETAIN and NOSTOP since both require control.
5287c478bd9Sstevel@tonic-gate 	 */
5297c478bd9Sstevel@tonic-gate 	if (flags & PGRAB_RDONLY)
5307c478bd9Sstevel@tonic-gate 		flags |= PGRAB_RETAIN | PGRAB_NOSTOP;
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 	if ((P = malloc(sizeof (struct ps_prochandle))) == NULL) {
5337c478bd9Sstevel@tonic-gate 		*perr = G_STRANGE;
5347c478bd9Sstevel@tonic-gate 		return (NULL);
5357c478bd9Sstevel@tonic-gate 	}
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate 	P->asfd = -1;
5387c478bd9Sstevel@tonic-gate 	P->ctlfd = -1;
5397c478bd9Sstevel@tonic-gate 	P->statfd = -1;
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate again:	/* Come back here if we lose it in the Window of Vulnerability */
5427c478bd9Sstevel@tonic-gate 	if (P->ctlfd >= 0)
5437c478bd9Sstevel@tonic-gate 		(void) close(P->ctlfd);
5447c478bd9Sstevel@tonic-gate 	if (P->asfd >= 0)
5457c478bd9Sstevel@tonic-gate 		(void) close(P->asfd);
5467c478bd9Sstevel@tonic-gate 	if (P->statfd >= 0)
5477c478bd9Sstevel@tonic-gate 		(void) close(P->statfd);
5487c478bd9Sstevel@tonic-gate 	(void) memset(P, 0, sizeof (*P));
5497c478bd9Sstevel@tonic-gate 	(void) mutex_init(&P->proc_lock, USYNC_THREAD, NULL);
5507c478bd9Sstevel@tonic-gate 	P->ctlfd = -1;
5517c478bd9Sstevel@tonic-gate 	P->asfd = -1;
5527c478bd9Sstevel@tonic-gate 	P->statfd = -1;
5537c478bd9Sstevel@tonic-gate 	P->agentctlfd = -1;
5547c478bd9Sstevel@tonic-gate 	P->agentstatfd = -1;
5557c478bd9Sstevel@tonic-gate 	P->ops = &P_live_ops;
5567c478bd9Sstevel@tonic-gate 	Pinitsym(P);
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate 	/*
5597c478bd9Sstevel@tonic-gate 	 * Open the /proc/pid files
5607c478bd9Sstevel@tonic-gate 	 */
5619acbbeafSnn35248 	(void) snprintf(procname, sizeof (procname), "%s/%d/",
5629acbbeafSnn35248 	    procfs_path, (int)pid);
5637c478bd9Sstevel@tonic-gate 	fname = procname + strlen(procname);
5647c478bd9Sstevel@tonic-gate 	(void) set_minfd();
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 	/*
5677c478bd9Sstevel@tonic-gate 	 * Request exclusive open to avoid grabbing someone else's
5687c478bd9Sstevel@tonic-gate 	 * process and to prevent others from interfering afterwards.
5697c478bd9Sstevel@tonic-gate 	 * If this fails and the 'PGRAB_FORCE' flag is set, attempt to
5707c478bd9Sstevel@tonic-gate 	 * open non-exclusively.
5717c478bd9Sstevel@tonic-gate 	 */
5727c478bd9Sstevel@tonic-gate 	(void) strcpy(fname, "as");
5737c478bd9Sstevel@tonic-gate 	omode = (flags & PGRAB_RDONLY) ? O_RDONLY : O_RDWR;
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate 	if (((fd = open(procname, omode | O_EXCL)) < 0 &&
5767c478bd9Sstevel@tonic-gate 	    (fd = ((flags & PGRAB_FORCE)? open(procname, omode) : -1)) < 0) ||
5777c478bd9Sstevel@tonic-gate 	    (fd = dupfd(fd, 0)) < 0) {
5787c478bd9Sstevel@tonic-gate 		switch (errno) {
5797c478bd9Sstevel@tonic-gate 		case ENOENT:
5807c478bd9Sstevel@tonic-gate 			rc = G_NOPROC;
5817c478bd9Sstevel@tonic-gate 			break;
5827c478bd9Sstevel@tonic-gate 		case EACCES:
5837c478bd9Sstevel@tonic-gate 		case EPERM:
5847c478bd9Sstevel@tonic-gate 			rc = G_PERM;
5857c478bd9Sstevel@tonic-gate 			break;
586cd11e192Sjhaslam 		case EMFILE:
587cd11e192Sjhaslam 			rc = G_NOFD;
588cd11e192Sjhaslam 			break;
5897c478bd9Sstevel@tonic-gate 		case EBUSY:
5907c478bd9Sstevel@tonic-gate 			if (!(flags & PGRAB_FORCE) || geteuid() != 0) {
5917c478bd9Sstevel@tonic-gate 				rc = G_BUSY;
5927c478bd9Sstevel@tonic-gate 				break;
5937c478bd9Sstevel@tonic-gate 			}
5947c478bd9Sstevel@tonic-gate 			/* FALLTHROUGH */
5957c478bd9Sstevel@tonic-gate 		default:
5967c478bd9Sstevel@tonic-gate 			dprintf("Pgrab: failed to open %s: %s\n",
5977c478bd9Sstevel@tonic-gate 			    procname, strerror(errno));
5987c478bd9Sstevel@tonic-gate 			rc = G_STRANGE;
5997c478bd9Sstevel@tonic-gate 			break;
6007c478bd9Sstevel@tonic-gate 		}
6017c478bd9Sstevel@tonic-gate 		goto err;
6027c478bd9Sstevel@tonic-gate 	}
6037c478bd9Sstevel@tonic-gate 	P->asfd = fd;
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate 	(void) strcpy(fname, "status");
6067c478bd9Sstevel@tonic-gate 	if ((fd = open(procname, O_RDONLY)) < 0 ||
6077c478bd9Sstevel@tonic-gate 	    (fd = dupfd(fd, 0)) < 0) {
6087c478bd9Sstevel@tonic-gate 		switch (errno) {
6097c478bd9Sstevel@tonic-gate 		case ENOENT:
6107c478bd9Sstevel@tonic-gate 			rc = G_NOPROC;
6117c478bd9Sstevel@tonic-gate 			break;
612cd11e192Sjhaslam 		case EMFILE:
613cd11e192Sjhaslam 			rc = G_NOFD;
614cd11e192Sjhaslam 			break;
6157c478bd9Sstevel@tonic-gate 		default:
6167c478bd9Sstevel@tonic-gate 			dprintf("Pgrab: failed to open %s: %s\n",
6177c478bd9Sstevel@tonic-gate 			    procname, strerror(errno));
6187c478bd9Sstevel@tonic-gate 			rc = G_STRANGE;
6197c478bd9Sstevel@tonic-gate 			break;
6207c478bd9Sstevel@tonic-gate 		}
6217c478bd9Sstevel@tonic-gate 		goto err;
6227c478bd9Sstevel@tonic-gate 	}
6237c478bd9Sstevel@tonic-gate 	P->statfd = fd;
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate 	if (!(flags & PGRAB_RDONLY)) {
6267c478bd9Sstevel@tonic-gate 		(void) strcpy(fname, "ctl");
6277c478bd9Sstevel@tonic-gate 		if ((fd = open(procname, O_WRONLY)) < 0 ||
6287c478bd9Sstevel@tonic-gate 		    (fd = dupfd(fd, 0)) < 0) {
6297c478bd9Sstevel@tonic-gate 			switch (errno) {
6307c478bd9Sstevel@tonic-gate 			case ENOENT:
6317c478bd9Sstevel@tonic-gate 				rc = G_NOPROC;
6327c478bd9Sstevel@tonic-gate 				break;
633cd11e192Sjhaslam 			case EMFILE:
634cd11e192Sjhaslam 				rc = G_NOFD;
635cd11e192Sjhaslam 				break;
6367c478bd9Sstevel@tonic-gate 			default:
6377c478bd9Sstevel@tonic-gate 				dprintf("Pgrab: failed to open %s: %s\n",
6387c478bd9Sstevel@tonic-gate 				    procname, strerror(errno));
6397c478bd9Sstevel@tonic-gate 				rc = G_STRANGE;
6407c478bd9Sstevel@tonic-gate 				break;
6417c478bd9Sstevel@tonic-gate 			}
6427c478bd9Sstevel@tonic-gate 			goto err;
6437c478bd9Sstevel@tonic-gate 		}
6447c478bd9Sstevel@tonic-gate 		P->ctlfd = fd;
6457c478bd9Sstevel@tonic-gate 	}
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate 	P->state = PS_RUN;
6487c478bd9Sstevel@tonic-gate 	P->pid = pid;
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate 	/*
6517c478bd9Sstevel@tonic-gate 	 * We are now in the Window of Vulnerability (WoV).  The process may
6527c478bd9Sstevel@tonic-gate 	 * exec() a setuid/setgid or unreadable object file between the open()
6537c478bd9Sstevel@tonic-gate 	 * and the PCSTOP.  We will get EAGAIN in this case and must start over.
6547c478bd9Sstevel@tonic-gate 	 * As Pstopstatus will trigger the first read() from a /proc file,
6557c478bd9Sstevel@tonic-gate 	 * we also need to handle EOVERFLOW here when 32-bit as an indicator
6567c478bd9Sstevel@tonic-gate 	 * that this process is 64-bit.  Finally, if the process has become
6577c478bd9Sstevel@tonic-gate 	 * a zombie (PS_UNDEAD) while we were trying to grab it, just remain
6587c478bd9Sstevel@tonic-gate 	 * silent about this and pretend there was no process.
6597c478bd9Sstevel@tonic-gate 	 */
6607c478bd9Sstevel@tonic-gate 	if (Pstopstatus(P, PCNULL, 0) != 0) {
6617c478bd9Sstevel@tonic-gate #ifndef _LP64
6627c478bd9Sstevel@tonic-gate 		if (errno == EOVERFLOW) {
6637c478bd9Sstevel@tonic-gate 			rc = G_LP64;
6647c478bd9Sstevel@tonic-gate 			goto err;
6657c478bd9Sstevel@tonic-gate 		}
6667c478bd9Sstevel@tonic-gate #endif
6677c478bd9Sstevel@tonic-gate 		if (P->state == PS_LOST) {	/* WoV */
6687c478bd9Sstevel@tonic-gate 			(void) mutex_destroy(&P->proc_lock);
6697c478bd9Sstevel@tonic-gate 			goto again;
6707c478bd9Sstevel@tonic-gate 		}
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate 		if (P->state == PS_UNDEAD)
6737c478bd9Sstevel@tonic-gate 			rc = G_NOPROC;
6747c478bd9Sstevel@tonic-gate 		else
6757c478bd9Sstevel@tonic-gate 			rc = G_STRANGE;
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate 		goto err;
6787c478bd9Sstevel@tonic-gate 	}
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate 	/*
6817c478bd9Sstevel@tonic-gate 	 * If the process is a system process, we can't control it even as root
6827c478bd9Sstevel@tonic-gate 	 */
6837c478bd9Sstevel@tonic-gate 	if (P->status.pr_flags & PR_ISSYS) {
6847c478bd9Sstevel@tonic-gate 		rc = G_SYS;
6857c478bd9Sstevel@tonic-gate 		goto err;
6867c478bd9Sstevel@tonic-gate 	}
6877c478bd9Sstevel@tonic-gate #ifndef _LP64
6887c478bd9Sstevel@tonic-gate 	/*
6897c478bd9Sstevel@tonic-gate 	 * We must be a 64-bit process to deal with a 64-bit process
6907c478bd9Sstevel@tonic-gate 	 */
6917c478bd9Sstevel@tonic-gate 	if (P->status.pr_dmodel == PR_MODEL_LP64) {
6927c478bd9Sstevel@tonic-gate 		rc = G_LP64;
6937c478bd9Sstevel@tonic-gate 		goto err;
6947c478bd9Sstevel@tonic-gate 	}
6957c478bd9Sstevel@tonic-gate #endif
6967c478bd9Sstevel@tonic-gate 
6977c478bd9Sstevel@tonic-gate 	/*
6987c478bd9Sstevel@tonic-gate 	 * Remember the status for use by Prelease().
6997c478bd9Sstevel@tonic-gate 	 */
7007c478bd9Sstevel@tonic-gate 	P->orig_status = P->status;	/* structure copy */
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate 	/*
7037c478bd9Sstevel@tonic-gate 	 * Before stopping the process, make sure we are not grabbing ourselves.
7047c478bd9Sstevel@tonic-gate 	 * If we are, make sure we are doing it PGRAB_RDONLY.
7057c478bd9Sstevel@tonic-gate 	 */
7067c478bd9Sstevel@tonic-gate 	if (pid == getpid()) {
7077c478bd9Sstevel@tonic-gate 		/*
7087c478bd9Sstevel@tonic-gate 		 * Verify that the process is really ourself:
7097c478bd9Sstevel@tonic-gate 		 * Set a magic number, read it through the
7107c478bd9Sstevel@tonic-gate 		 * /proc file and see if the results match.
7117c478bd9Sstevel@tonic-gate 		 */
7127c478bd9Sstevel@tonic-gate 		uint32_t magic1 = 0;
7137c478bd9Sstevel@tonic-gate 		uint32_t magic2 = 2;
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate 		errno = 0;
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate 		if (Pread(P, &magic2, sizeof (magic2), (uintptr_t)&magic1)
7187c478bd9Sstevel@tonic-gate 		    == sizeof (magic2) &&
7197c478bd9Sstevel@tonic-gate 		    magic2 == 0 &&
7207c478bd9Sstevel@tonic-gate 		    (magic1 = 0xfeedbeef) &&
7217c478bd9Sstevel@tonic-gate 		    Pread(P, &magic2, sizeof (magic2), (uintptr_t)&magic1)
7227c478bd9Sstevel@tonic-gate 		    == sizeof (magic2) &&
7237c478bd9Sstevel@tonic-gate 		    magic2 == 0xfeedbeef &&
7247c478bd9Sstevel@tonic-gate 		    !(flags & PGRAB_RDONLY)) {
7257c478bd9Sstevel@tonic-gate 			rc = G_SELF;
7267c478bd9Sstevel@tonic-gate 			goto err;
7277c478bd9Sstevel@tonic-gate 		}
7287c478bd9Sstevel@tonic-gate 	}
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate 	/*
7317c478bd9Sstevel@tonic-gate 	 * If the process is already stopped or has been directed
7327c478bd9Sstevel@tonic-gate 	 * to stop via /proc, do not set run-on-last-close.
7337c478bd9Sstevel@tonic-gate 	 */
7347c478bd9Sstevel@tonic-gate 	if (!(P->status.pr_lwp.pr_flags & (PR_ISTOP|PR_DSTOP)) &&
7357c478bd9Sstevel@tonic-gate 	    !(flags & PGRAB_RDONLY)) {
7367c478bd9Sstevel@tonic-gate 		/*
7377c478bd9Sstevel@tonic-gate 		 * Mark the process run-on-last-close so
7387c478bd9Sstevel@tonic-gate 		 * it runs even if we die from SIGKILL.
7397c478bd9Sstevel@tonic-gate 		 */
7407c478bd9Sstevel@tonic-gate 		if (Psetflags(P, PR_RLC) != 0) {
7417c478bd9Sstevel@tonic-gate 			if (errno == EAGAIN) {	/* WoV */
7427c478bd9Sstevel@tonic-gate 				(void) mutex_destroy(&P->proc_lock);
7437c478bd9Sstevel@tonic-gate 				goto again;
7447c478bd9Sstevel@tonic-gate 			}
7457c478bd9Sstevel@tonic-gate 			if (errno == ENOENT)	/* No complaint about zombies */
7467c478bd9Sstevel@tonic-gate 				rc = G_ZOMB;
7477c478bd9Sstevel@tonic-gate 			else {
7487c478bd9Sstevel@tonic-gate 				dprintf("Pgrab: failed to set RLC\n");
7497c478bd9Sstevel@tonic-gate 				rc = G_STRANGE;
7507c478bd9Sstevel@tonic-gate 			}
7517c478bd9Sstevel@tonic-gate 			goto err;
7527c478bd9Sstevel@tonic-gate 		}
7537c478bd9Sstevel@tonic-gate 	}
7547c478bd9Sstevel@tonic-gate 
7557c478bd9Sstevel@tonic-gate 	/*
7567c478bd9Sstevel@tonic-gate 	 * If a stop directive is pending and the process has not yet stopped,
7577c478bd9Sstevel@tonic-gate 	 * then synchronously wait for the stop directive to take effect.
7587c478bd9Sstevel@tonic-gate 	 * Limit the time spent waiting for the process to stop by iterating
7597c478bd9Sstevel@tonic-gate 	 * at most 10 times. The time-out of 20 ms corresponds to the time
7607c478bd9Sstevel@tonic-gate 	 * between sending the stop directive and the process actually stopped
7617c478bd9Sstevel@tonic-gate 	 * as measured by DTrace on a slow, busy system. If the process doesn't
7627c478bd9Sstevel@tonic-gate 	 * stop voluntarily, clear the PR_DSTOP flag so that the code below
7637c478bd9Sstevel@tonic-gate 	 * forces the process to stop.
7647c478bd9Sstevel@tonic-gate 	 */
7657c478bd9Sstevel@tonic-gate 	if (!(flags & PGRAB_RDONLY)) {
7667c478bd9Sstevel@tonic-gate 		int niter = 0;
7677c478bd9Sstevel@tonic-gate 		while ((P->status.pr_lwp.pr_flags & (PR_STOPPED|PR_DSTOP)) ==
7687c478bd9Sstevel@tonic-gate 		    PR_DSTOP && niter < 10 &&
7697c478bd9Sstevel@tonic-gate 		    Pstopstatus(P, PCTWSTOP, 20) != 0) {
7707c478bd9Sstevel@tonic-gate 			niter++;
7717c478bd9Sstevel@tonic-gate 			if (flags & PGRAB_NOSTOP)
7727c478bd9Sstevel@tonic-gate 				break;
7737c478bd9Sstevel@tonic-gate 		}
7747c478bd9Sstevel@tonic-gate 		if (niter == 10 && !(flags & PGRAB_NOSTOP)) {
7757c478bd9Sstevel@tonic-gate 			/* Try it harder down below */
7767c478bd9Sstevel@tonic-gate 			P->status.pr_lwp.pr_flags &= ~PR_DSTOP;
7777c478bd9Sstevel@tonic-gate 		}
7787c478bd9Sstevel@tonic-gate 	}
7797c478bd9Sstevel@tonic-gate 
7807c478bd9Sstevel@tonic-gate 	/*
7817c478bd9Sstevel@tonic-gate 	 * If the process is not already stopped or directed to stop
7827c478bd9Sstevel@tonic-gate 	 * and PGRAB_NOSTOP was not specified, stop the process now.
7837c478bd9Sstevel@tonic-gate 	 */
7847c478bd9Sstevel@tonic-gate 	if (!(P->status.pr_lwp.pr_flags & (PR_ISTOP|PR_DSTOP)) &&
7857c478bd9Sstevel@tonic-gate 	    !(flags & PGRAB_NOSTOP)) {
7867c478bd9Sstevel@tonic-gate 		/*
7877c478bd9Sstevel@tonic-gate 		 * Stop the process, get its status and signal/syscall masks.
7887c478bd9Sstevel@tonic-gate 		 */
7897c478bd9Sstevel@tonic-gate 		if (((P->status.pr_lwp.pr_flags & PR_STOPPED) &&
7907c478bd9Sstevel@tonic-gate 		    Pstopstatus(P, PCDSTOP, 0) != 0) ||
7917c478bd9Sstevel@tonic-gate 		    Pstopstatus(P, PCSTOP, 2000) != 0) {
7927c478bd9Sstevel@tonic-gate #ifndef _LP64
7937c478bd9Sstevel@tonic-gate 			if (errno == EOVERFLOW) {
7947c478bd9Sstevel@tonic-gate 				rc = G_LP64;
7957c478bd9Sstevel@tonic-gate 				goto err;
7967c478bd9Sstevel@tonic-gate 			}
7977c478bd9Sstevel@tonic-gate #endif
7987c478bd9Sstevel@tonic-gate 			if (P->state == PS_LOST) {	/* WoV */
7997c478bd9Sstevel@tonic-gate 				(void) mutex_destroy(&P->proc_lock);
8007c478bd9Sstevel@tonic-gate 				goto again;
8017c478bd9Sstevel@tonic-gate 			}
8027c478bd9Sstevel@tonic-gate 			if ((errno != EINTR && errno != ERESTART) ||
8037c478bd9Sstevel@tonic-gate 			    (P->state != PS_STOP &&
8047c478bd9Sstevel@tonic-gate 			    !(P->status.pr_flags & PR_DSTOP))) {
8057c478bd9Sstevel@tonic-gate 				if (P->state != PS_RUN && errno != ENOENT) {
8067c478bd9Sstevel@tonic-gate 					dprintf("Pgrab: failed to PCSTOP\n");
8077c478bd9Sstevel@tonic-gate 					rc = G_STRANGE;
8087c478bd9Sstevel@tonic-gate 				} else {
8097c478bd9Sstevel@tonic-gate 					rc = G_ZOMB;
8107c478bd9Sstevel@tonic-gate 				}
8117c478bd9Sstevel@tonic-gate 				goto err;
8127c478bd9Sstevel@tonic-gate 			}
8137c478bd9Sstevel@tonic-gate 		}
8147c478bd9Sstevel@tonic-gate 
8157c478bd9Sstevel@tonic-gate 		/*
8167c478bd9Sstevel@tonic-gate 		 * Process should now either be stopped via /proc or there
8177c478bd9Sstevel@tonic-gate 		 * should be an outstanding stop directive.
8187c478bd9Sstevel@tonic-gate 		 */
8197c478bd9Sstevel@tonic-gate 		if (!(P->status.pr_flags & (PR_ISTOP|PR_DSTOP))) {
8207c478bd9Sstevel@tonic-gate 			dprintf("Pgrab: process is not stopped\n");
8217c478bd9Sstevel@tonic-gate 			rc = G_STRANGE;
8227c478bd9Sstevel@tonic-gate 			goto err;
8237c478bd9Sstevel@tonic-gate 		}
8247c478bd9Sstevel@tonic-gate #ifndef _LP64
8257c478bd9Sstevel@tonic-gate 		/*
8267c478bd9Sstevel@tonic-gate 		 * Test this again now because the 32-bit victim process may
8277c478bd9Sstevel@tonic-gate 		 * have exec'd a 64-bit process in the meantime.
8287c478bd9Sstevel@tonic-gate 		 */
8297c478bd9Sstevel@tonic-gate 		if (P->status.pr_dmodel == PR_MODEL_LP64) {
8307c478bd9Sstevel@tonic-gate 			rc = G_LP64;
8317c478bd9Sstevel@tonic-gate 			goto err;
8327c478bd9Sstevel@tonic-gate 		}
8337c478bd9Sstevel@tonic-gate #endif
8347c478bd9Sstevel@tonic-gate 	}
8357c478bd9Sstevel@tonic-gate 
8367c478bd9Sstevel@tonic-gate 	/*
8377c478bd9Sstevel@tonic-gate 	 * Cancel all tracing flags unless the PGRAB_RETAIN flag is set.
8387c478bd9Sstevel@tonic-gate 	 */
8397c478bd9Sstevel@tonic-gate 	if (!(flags & PGRAB_RETAIN)) {
8407c478bd9Sstevel@tonic-gate 		(void) Psysentry(P, 0, FALSE);
8417c478bd9Sstevel@tonic-gate 		(void) Psysexit(P, 0, FALSE);
8427c478bd9Sstevel@tonic-gate 		(void) Psignal(P, 0, FALSE);
8437c478bd9Sstevel@tonic-gate 		(void) Pfault(P, 0, FALSE);
8447c478bd9Sstevel@tonic-gate 		Psync(P);
8457c478bd9Sstevel@tonic-gate 	}
8467c478bd9Sstevel@tonic-gate 
8477c478bd9Sstevel@tonic-gate 	*perr = 0;
8487c478bd9Sstevel@tonic-gate 	return (P);
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate err:
8517c478bd9Sstevel@tonic-gate 	Pfree(P);
8527c478bd9Sstevel@tonic-gate 	*perr = rc;
8537c478bd9Sstevel@tonic-gate 	return (NULL);
8547c478bd9Sstevel@tonic-gate }
8557c478bd9Sstevel@tonic-gate 
8567c478bd9Sstevel@tonic-gate /*
8577c478bd9Sstevel@tonic-gate  * Return a printable string corresponding to a Pgrab() error return.
8587c478bd9Sstevel@tonic-gate  */
8597c478bd9Sstevel@tonic-gate const char *
8607c478bd9Sstevel@tonic-gate Pgrab_error(int error)
8617c478bd9Sstevel@tonic-gate {
8627c478bd9Sstevel@tonic-gate 	const char *str;
8637c478bd9Sstevel@tonic-gate 
8647c478bd9Sstevel@tonic-gate 	switch (error) {
8657c478bd9Sstevel@tonic-gate 	case G_NOPROC:
8667c478bd9Sstevel@tonic-gate 		str = "no such process";
8677c478bd9Sstevel@tonic-gate 		break;
8687c478bd9Sstevel@tonic-gate 	case G_NOCORE:
8697c478bd9Sstevel@tonic-gate 		str = "no such core file";
8707c478bd9Sstevel@tonic-gate 		break;
8717c478bd9Sstevel@tonic-gate 	case G_NOPROCORCORE:
8727c478bd9Sstevel@tonic-gate 		str = "no such process or core file";
8737c478bd9Sstevel@tonic-gate 		break;
8747c478bd9Sstevel@tonic-gate 	case G_NOEXEC:
8757c478bd9Sstevel@tonic-gate 		str = "cannot find executable file";
8767c478bd9Sstevel@tonic-gate 		break;
8777c478bd9Sstevel@tonic-gate 	case G_ZOMB:
8787c478bd9Sstevel@tonic-gate 		str = "zombie process";
8797c478bd9Sstevel@tonic-gate 		break;
8807c478bd9Sstevel@tonic-gate 	case G_PERM:
8817c478bd9Sstevel@tonic-gate 		str = "permission denied";
8827c478bd9Sstevel@tonic-gate 		break;
8837c478bd9Sstevel@tonic-gate 	case G_BUSY:
8847c478bd9Sstevel@tonic-gate 		str = "process is traced";
8857c478bd9Sstevel@tonic-gate 		break;
8867c478bd9Sstevel@tonic-gate 	case G_SYS:
8877c478bd9Sstevel@tonic-gate 		str = "system process";
8887c478bd9Sstevel@tonic-gate 		break;
8897c478bd9Sstevel@tonic-gate 	case G_SELF:
8907c478bd9Sstevel@tonic-gate 		str = "attempt to grab self";
8917c478bd9Sstevel@tonic-gate 		break;
8927c478bd9Sstevel@tonic-gate 	case G_INTR:
8937c478bd9Sstevel@tonic-gate 		str = "operation interrupted";
8947c478bd9Sstevel@tonic-gate 		break;
8957c478bd9Sstevel@tonic-gate 	case G_LP64:
8967c478bd9Sstevel@tonic-gate 		str = "program is _LP64, self is not";
8977c478bd9Sstevel@tonic-gate 		break;
8987c478bd9Sstevel@tonic-gate 	case G_FORMAT:
8997c478bd9Sstevel@tonic-gate 		str = "file is not an ELF core file";
9007c478bd9Sstevel@tonic-gate 		break;
9017c478bd9Sstevel@tonic-gate 	case G_ELF:
9027c478bd9Sstevel@tonic-gate 		str = "libelf error";
9037c478bd9Sstevel@tonic-gate 		break;
9047c478bd9Sstevel@tonic-gate 	case G_NOTE:
9057c478bd9Sstevel@tonic-gate 		str = "core file is corrupt or missing required data";
9067c478bd9Sstevel@tonic-gate 		break;
9077c478bd9Sstevel@tonic-gate 	case G_STRANGE:
9087c478bd9Sstevel@tonic-gate 		str = "unanticipated system error";
9097c478bd9Sstevel@tonic-gate 		break;
9107c478bd9Sstevel@tonic-gate 	case G_ISAINVAL:
9117c478bd9Sstevel@tonic-gate 		str = "wrong ELF machine type";
9127c478bd9Sstevel@tonic-gate 		break;
9137c478bd9Sstevel@tonic-gate 	case G_BADLWPS:
9147c478bd9Sstevel@tonic-gate 		str = "bad lwp specification";
9157c478bd9Sstevel@tonic-gate 		break;
916cd11e192Sjhaslam 	case G_NOFD:
917cd11e192Sjhaslam 		str = "too many open files";
918cd11e192Sjhaslam 		break;
9197c478bd9Sstevel@tonic-gate 	default:
9207c478bd9Sstevel@tonic-gate 		str = "unknown error";
9217c478bd9Sstevel@tonic-gate 		break;
9227c478bd9Sstevel@tonic-gate 	}
9237c478bd9Sstevel@tonic-gate 
9247c478bd9Sstevel@tonic-gate 	return (str);
9257c478bd9Sstevel@tonic-gate }
9267c478bd9Sstevel@tonic-gate 
9277c478bd9Sstevel@tonic-gate /*
9287c478bd9Sstevel@tonic-gate  * Free a process control structure.
9297c478bd9Sstevel@tonic-gate  * Close the file descriptors but don't do the Prelease logic.
9307c478bd9Sstevel@tonic-gate  */
9317c478bd9Sstevel@tonic-gate void
9327c478bd9Sstevel@tonic-gate Pfree(struct ps_prochandle *P)
9337c478bd9Sstevel@tonic-gate {
9347c478bd9Sstevel@tonic-gate 	uint_t i;
9357c478bd9Sstevel@tonic-gate 
9367c478bd9Sstevel@tonic-gate 	if (P->core != NULL) {
9377c478bd9Sstevel@tonic-gate 		extern void __priv_free_info(void *);
9387c478bd9Sstevel@tonic-gate 		lwp_info_t *nlwp, *lwp = list_next(&P->core->core_lwp_head);
9397c478bd9Sstevel@tonic-gate 
9407c478bd9Sstevel@tonic-gate 		for (i = 0; i < P->core->core_nlwp; i++, lwp = nlwp) {
9417c478bd9Sstevel@tonic-gate 			nlwp = list_next(lwp);
9427c478bd9Sstevel@tonic-gate #ifdef __sparc
9437c478bd9Sstevel@tonic-gate 			if (lwp->lwp_gwins != NULL)
9447c478bd9Sstevel@tonic-gate 				free(lwp->lwp_gwins);
9457c478bd9Sstevel@tonic-gate 			if (lwp->lwp_xregs != NULL)
9467c478bd9Sstevel@tonic-gate 				free(lwp->lwp_xregs);
9477c478bd9Sstevel@tonic-gate 			if (lwp->lwp_asrs != NULL)
9487c478bd9Sstevel@tonic-gate 				free(lwp->lwp_asrs);
9497c478bd9Sstevel@tonic-gate #endif
9507c478bd9Sstevel@tonic-gate 			free(lwp);
9517c478bd9Sstevel@tonic-gate 		}
9527c478bd9Sstevel@tonic-gate 
9537c478bd9Sstevel@tonic-gate 		if (P->core->core_platform != NULL)
9547c478bd9Sstevel@tonic-gate 			free(P->core->core_platform);
9557c478bd9Sstevel@tonic-gate 		if (P->core->core_uts != NULL)
9567c478bd9Sstevel@tonic-gate 			free(P->core->core_uts);
9577c478bd9Sstevel@tonic-gate 		if (P->core->core_cred != NULL)
9587c478bd9Sstevel@tonic-gate 			free(P->core->core_cred);
9597c478bd9Sstevel@tonic-gate 		if (P->core->core_priv != NULL)
9607c478bd9Sstevel@tonic-gate 			free(P->core->core_priv);
9617c478bd9Sstevel@tonic-gate 		if (P->core->core_privinfo != NULL)
9627c478bd9Sstevel@tonic-gate 			__priv_free_info(P->core->core_privinfo);
9637c478bd9Sstevel@tonic-gate 		if (P->core->core_ppii != NULL)
9647c478bd9Sstevel@tonic-gate 			free(P->core->core_ppii);
9657c478bd9Sstevel@tonic-gate 		if (P->core->core_zonename != NULL)
9667c478bd9Sstevel@tonic-gate 			free(P->core->core_zonename);
9677c478bd9Sstevel@tonic-gate #if defined(__i386) || defined(__amd64)
9687c478bd9Sstevel@tonic-gate 		if (P->core->core_ldt != NULL)
9697c478bd9Sstevel@tonic-gate 			free(P->core->core_ldt);
9707c478bd9Sstevel@tonic-gate #endif
9717c478bd9Sstevel@tonic-gate 
9727c478bd9Sstevel@tonic-gate 		free(P->core);
9737c478bd9Sstevel@tonic-gate 	}
9747c478bd9Sstevel@tonic-gate 
9757c478bd9Sstevel@tonic-gate 	if (P->ucaddrs != NULL) {
9767c478bd9Sstevel@tonic-gate 		free(P->ucaddrs);
9777c478bd9Sstevel@tonic-gate 		P->ucaddrs = NULL;
9787c478bd9Sstevel@tonic-gate 		P->ucnelems = 0;
9797c478bd9Sstevel@tonic-gate 	}
9807c478bd9Sstevel@tonic-gate 
9817c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&P->proc_lock);
9827c478bd9Sstevel@tonic-gate 	if (P->hashtab != NULL) {
9837c478bd9Sstevel@tonic-gate 		struct ps_lwphandle *L;
9847c478bd9Sstevel@tonic-gate 		for (i = 0; i < HASHSIZE; i++) {
9857c478bd9Sstevel@tonic-gate 			while ((L = P->hashtab[i]) != NULL)
9867c478bd9Sstevel@tonic-gate 				Lfree_internal(P, L);
9877c478bd9Sstevel@tonic-gate 		}
9887c478bd9Sstevel@tonic-gate 		free(P->hashtab);
9897c478bd9Sstevel@tonic-gate 	}
990*34bdffbfSGarrett D'Amore 
991*34bdffbfSGarrett D'Amore 	while (P->num_fd > 0) {
992*34bdffbfSGarrett D'Amore 		fd_info_t *fip = list_next(&P->fd_head);
993*34bdffbfSGarrett D'Amore 		list_unlink(fip);
994*34bdffbfSGarrett D'Amore 		free(fip);
995*34bdffbfSGarrett D'Amore 		P->num_fd--;
996*34bdffbfSGarrett D'Amore 	}
9977c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&P->proc_lock);
9987c478bd9Sstevel@tonic-gate 	(void) mutex_destroy(&P->proc_lock);
9997c478bd9Sstevel@tonic-gate 
10007c478bd9Sstevel@tonic-gate 	if (P->agentctlfd >= 0)
10017c478bd9Sstevel@tonic-gate 		(void) close(P->agentctlfd);
10027c478bd9Sstevel@tonic-gate 	if (P->agentstatfd >= 0)
10037c478bd9Sstevel@tonic-gate 		(void) close(P->agentstatfd);
10047c478bd9Sstevel@tonic-gate 	if (P->ctlfd >= 0)
10057c478bd9Sstevel@tonic-gate 		(void) close(P->ctlfd);
10067c478bd9Sstevel@tonic-gate 	if (P->asfd >= 0)
10077c478bd9Sstevel@tonic-gate 		(void) close(P->asfd);
10087c478bd9Sstevel@tonic-gate 	if (P->statfd >= 0)
10097c478bd9Sstevel@tonic-gate 		(void) close(P->statfd);
10107c478bd9Sstevel@tonic-gate 	Preset_maps(P);
10117c478bd9Sstevel@tonic-gate 
10127c478bd9Sstevel@tonic-gate 	/* clear out the structure as a precaution against reuse */
10137c478bd9Sstevel@tonic-gate 	(void) memset(P, 0, sizeof (*P));
10147c478bd9Sstevel@tonic-gate 	P->ctlfd = -1;
10157c478bd9Sstevel@tonic-gate 	P->asfd = -1;
10167c478bd9Sstevel@tonic-gate 	P->statfd = -1;
10177c478bd9Sstevel@tonic-gate 	P->agentctlfd = -1;
10187c478bd9Sstevel@tonic-gate 	P->agentstatfd = -1;
10197c478bd9Sstevel@tonic-gate 
10207c478bd9Sstevel@tonic-gate 	free(P);
10217c478bd9Sstevel@tonic-gate }
10227c478bd9Sstevel@tonic-gate 
10237c478bd9Sstevel@tonic-gate /*
10247c478bd9Sstevel@tonic-gate  * Return the state of the process, one of the PS_* values.
10257c478bd9Sstevel@tonic-gate  */
10267c478bd9Sstevel@tonic-gate int
10277c478bd9Sstevel@tonic-gate Pstate(struct ps_prochandle *P)
10287c478bd9Sstevel@tonic-gate {
10297c478bd9Sstevel@tonic-gate 	return (P->state);
10307c478bd9Sstevel@tonic-gate }
10317c478bd9Sstevel@tonic-gate 
10327c478bd9Sstevel@tonic-gate /*
10337c478bd9Sstevel@tonic-gate  * Return the open address space file descriptor for the process.
10347c478bd9Sstevel@tonic-gate  * Clients must not close this file descriptor, not use it
10357c478bd9Sstevel@tonic-gate  * after the process is freed.
10367c478bd9Sstevel@tonic-gate  */
10377c478bd9Sstevel@tonic-gate int
10387c478bd9Sstevel@tonic-gate Pasfd(struct ps_prochandle *P)
10397c478bd9Sstevel@tonic-gate {
10407c478bd9Sstevel@tonic-gate 	return (P->asfd);
10417c478bd9Sstevel@tonic-gate }
10427c478bd9Sstevel@tonic-gate 
10437c478bd9Sstevel@tonic-gate /*
10447c478bd9Sstevel@tonic-gate  * Return the open control file descriptor for the process.
10457c478bd9Sstevel@tonic-gate  * Clients must not close this file descriptor, not use it
10467c478bd9Sstevel@tonic-gate  * after the process is freed.
10477c478bd9Sstevel@tonic-gate  */
10487c478bd9Sstevel@tonic-gate int
10497c478bd9Sstevel@tonic-gate Pctlfd(struct ps_prochandle *P)
10507c478bd9Sstevel@tonic-gate {
10517c478bd9Sstevel@tonic-gate 	return (P->ctlfd);
10527c478bd9Sstevel@tonic-gate }
10537c478bd9Sstevel@tonic-gate 
10547c478bd9Sstevel@tonic-gate /*
10557c478bd9Sstevel@tonic-gate  * Return a pointer to the process psinfo structure.
10567c478bd9Sstevel@tonic-gate  * Clients should not hold on to this pointer indefinitely.
10577c478bd9Sstevel@tonic-gate  * It will become invalid on Prelease().
10587c478bd9Sstevel@tonic-gate  */
10597c478bd9Sstevel@tonic-gate const psinfo_t *
10607c478bd9Sstevel@tonic-gate Ppsinfo(struct ps_prochandle *P)
10617c478bd9Sstevel@tonic-gate {
10627c478bd9Sstevel@tonic-gate 	if (P->state == PS_IDLE) {
10637c478bd9Sstevel@tonic-gate 		errno = ENODATA;
10647c478bd9Sstevel@tonic-gate 		return (NULL);
10657c478bd9Sstevel@tonic-gate 	}
10667c478bd9Sstevel@tonic-gate 
10677c478bd9Sstevel@tonic-gate 	if (P->state != PS_DEAD && proc_get_psinfo(P->pid, &P->psinfo) == -1)
10687c478bd9Sstevel@tonic-gate 		return (NULL);
10697c478bd9Sstevel@tonic-gate 
10707c478bd9Sstevel@tonic-gate 	return (&P->psinfo);
10717c478bd9Sstevel@tonic-gate }
10727c478bd9Sstevel@tonic-gate 
10737c478bd9Sstevel@tonic-gate /*
10747c478bd9Sstevel@tonic-gate  * Return a pointer to the process status structure.
10757c478bd9Sstevel@tonic-gate  * Clients should not hold on to this pointer indefinitely.
10767c478bd9Sstevel@tonic-gate  * It will become invalid on Prelease().
10777c478bd9Sstevel@tonic-gate  */
10787c478bd9Sstevel@tonic-gate const pstatus_t *
10797c478bd9Sstevel@tonic-gate Pstatus(struct ps_prochandle *P)
10807c478bd9Sstevel@tonic-gate {
10817c478bd9Sstevel@tonic-gate 	return (&P->status);
10827c478bd9Sstevel@tonic-gate }
10837c478bd9Sstevel@tonic-gate 
10847c478bd9Sstevel@tonic-gate /*
10857c478bd9Sstevel@tonic-gate  * Fill in a pointer to a process credentials structure.  The ngroups parameter
10867c478bd9Sstevel@tonic-gate  * is the number of supplementary group entries allocated in the caller's cred
10877c478bd9Sstevel@tonic-gate  * structure.  It should equal zero or one unless extra space has been
10887c478bd9Sstevel@tonic-gate  * allocated for the group list by the caller.
10897c478bd9Sstevel@tonic-gate  */
10907c478bd9Sstevel@tonic-gate int
10917c478bd9Sstevel@tonic-gate Pcred(struct ps_prochandle *P, prcred_t *pcrp, int ngroups)
10927c478bd9Sstevel@tonic-gate {
10937c478bd9Sstevel@tonic-gate 	if (P->state == PS_IDLE) {
10947c478bd9Sstevel@tonic-gate 		errno = ENODATA;
10957c478bd9Sstevel@tonic-gate 		return (-1);
10967c478bd9Sstevel@tonic-gate 	}
10977c478bd9Sstevel@tonic-gate 
10987c478bd9Sstevel@tonic-gate 	if (P->state != PS_DEAD)
10997c478bd9Sstevel@tonic-gate 		return (proc_get_cred(P->pid, pcrp, ngroups));
11007c478bd9Sstevel@tonic-gate 
11017c478bd9Sstevel@tonic-gate 	if (P->core->core_cred != NULL) {
11027c478bd9Sstevel@tonic-gate 		/*
11037c478bd9Sstevel@tonic-gate 		 * Avoid returning more supplementary group data than the
11047c478bd9Sstevel@tonic-gate 		 * caller has allocated in their buffer.  We expect them to
11057c478bd9Sstevel@tonic-gate 		 * check pr_ngroups afterward and potentially call us again.
11067c478bd9Sstevel@tonic-gate 		 */
11077c478bd9Sstevel@tonic-gate 		ngroups = MIN(ngroups, P->core->core_cred->pr_ngroups);
11087c478bd9Sstevel@tonic-gate 
11097c478bd9Sstevel@tonic-gate 		(void) memcpy(pcrp, P->core->core_cred,
11107c478bd9Sstevel@tonic-gate 		    sizeof (prcred_t) + (ngroups - 1) * sizeof (gid_t));
11117c478bd9Sstevel@tonic-gate 
11127c478bd9Sstevel@tonic-gate 		return (0);
11137c478bd9Sstevel@tonic-gate 	}
11147c478bd9Sstevel@tonic-gate 
11157c478bd9Sstevel@tonic-gate 	errno = ENODATA;
11167c478bd9Sstevel@tonic-gate 	return (-1);
11177c478bd9Sstevel@tonic-gate }
11187c478bd9Sstevel@tonic-gate 
11197c478bd9Sstevel@tonic-gate #if defined(__i386) || defined(__amd64)
11207c478bd9Sstevel@tonic-gate /*
11217c478bd9Sstevel@tonic-gate  * Fill in a pointer to a process LDT structure.
11227c478bd9Sstevel@tonic-gate  * The caller provides a buffer of size 'nldt * sizeof (struct ssd)';
11237c478bd9Sstevel@tonic-gate  * If pldt == NULL or nldt == 0, we return the number of existing LDT entries.
11247c478bd9Sstevel@tonic-gate  * Otherwise we return the actual number of LDT entries fetched (<= nldt).
11257c478bd9Sstevel@tonic-gate  */
11267c478bd9Sstevel@tonic-gate int
11277c478bd9Sstevel@tonic-gate Pldt(struct ps_prochandle *P, struct ssd *pldt, int nldt)
11287c478bd9Sstevel@tonic-gate {
11297c478bd9Sstevel@tonic-gate 	if (P->state == PS_IDLE) {
11307c478bd9Sstevel@tonic-gate 		errno = ENODATA;
11317c478bd9Sstevel@tonic-gate 		return (-1);
11327c478bd9Sstevel@tonic-gate 	}
11337c478bd9Sstevel@tonic-gate 
11347c478bd9Sstevel@tonic-gate 	if (P->state != PS_DEAD)
11357c478bd9Sstevel@tonic-gate 		return (proc_get_ldt(P->pid, pldt, nldt));
11367c478bd9Sstevel@tonic-gate 
11377c478bd9Sstevel@tonic-gate 	if (pldt == NULL || nldt == 0)
11387c478bd9Sstevel@tonic-gate 		return (P->core->core_nldt);
11397c478bd9Sstevel@tonic-gate 
11407c478bd9Sstevel@tonic-gate 	if (P->core->core_ldt != NULL) {
11417c478bd9Sstevel@tonic-gate 		nldt = MIN(nldt, P->core->core_nldt);
11427c478bd9Sstevel@tonic-gate 
11437c478bd9Sstevel@tonic-gate 		(void) memcpy(pldt, P->core->core_ldt,
11447c478bd9Sstevel@tonic-gate 		    nldt * sizeof (struct ssd));
11457c478bd9Sstevel@tonic-gate 
11467c478bd9Sstevel@tonic-gate 		return (nldt);
11477c478bd9Sstevel@tonic-gate 	}
11487c478bd9Sstevel@tonic-gate 
11497c478bd9Sstevel@tonic-gate 	errno = ENODATA;
11507c478bd9Sstevel@tonic-gate 	return (-1);
11517c478bd9Sstevel@tonic-gate }
11527c478bd9Sstevel@tonic-gate #endif	/* __i386 */
11537c478bd9Sstevel@tonic-gate 
11547c478bd9Sstevel@tonic-gate /*
11557c478bd9Sstevel@tonic-gate  * Fill in a pointer to a process privilege structure.
11567c478bd9Sstevel@tonic-gate  */
11577c478bd9Sstevel@tonic-gate ssize_t
11587c478bd9Sstevel@tonic-gate Ppriv(struct ps_prochandle *P, prpriv_t *pprv, size_t size)
11597c478bd9Sstevel@tonic-gate {
11607c478bd9Sstevel@tonic-gate 	if (P->state != PS_DEAD) {
11617c478bd9Sstevel@tonic-gate 		prpriv_t *pp = proc_get_priv(P->pid);
11627c478bd9Sstevel@tonic-gate 		if (pp != NULL) {
11637c478bd9Sstevel@tonic-gate 			size = MIN(size, PRIV_PRPRIV_SIZE(pp));
11647c478bd9Sstevel@tonic-gate 			(void) memcpy(pprv, pp, size);
11657c478bd9Sstevel@tonic-gate 			free(pp);
11667c478bd9Sstevel@tonic-gate 			return (size);
11677c478bd9Sstevel@tonic-gate 		}
11687c478bd9Sstevel@tonic-gate 		return (-1);
11697c478bd9Sstevel@tonic-gate 	}
11707c478bd9Sstevel@tonic-gate 
11717c478bd9Sstevel@tonic-gate 	if (P->core->core_priv != NULL) {
11727c478bd9Sstevel@tonic-gate 		size = MIN(P->core->core_priv_size, size);
11737c478bd9Sstevel@tonic-gate 		(void) memcpy(pprv, P->core->core_priv, size);
11747c478bd9Sstevel@tonic-gate 		return (size);
11757c478bd9Sstevel@tonic-gate 	}
11767c478bd9Sstevel@tonic-gate 	errno = ENODATA;
11777c478bd9Sstevel@tonic-gate 	return (-1);
11787c478bd9Sstevel@tonic-gate }
11797c478bd9Sstevel@tonic-gate 
11807c478bd9Sstevel@tonic-gate int
11817c478bd9Sstevel@tonic-gate Psetpriv(struct ps_prochandle *P, prpriv_t *pprv)
11827c478bd9Sstevel@tonic-gate {
11837c478bd9Sstevel@tonic-gate 	int rc;
11847c478bd9Sstevel@tonic-gate 	long *ctl;
11857c478bd9Sstevel@tonic-gate 	size_t sz;
11867c478bd9Sstevel@tonic-gate 
11877c478bd9Sstevel@tonic-gate 	if (P->state == PS_DEAD) {
11887c478bd9Sstevel@tonic-gate 		errno = EBADF;
11897c478bd9Sstevel@tonic-gate 		return (-1);
11907c478bd9Sstevel@tonic-gate 	}
11917c478bd9Sstevel@tonic-gate 
11927c478bd9Sstevel@tonic-gate 	sz = PRIV_PRPRIV_SIZE(pprv) + sizeof (long);
11937c478bd9Sstevel@tonic-gate 
11947c478bd9Sstevel@tonic-gate 	sz = ((sz - 1) / sizeof (long) + 1) * sizeof (long);
11957c478bd9Sstevel@tonic-gate 
11967c478bd9Sstevel@tonic-gate 	ctl = malloc(sz);
11977c478bd9Sstevel@tonic-gate 	if (ctl == NULL)
11987c478bd9Sstevel@tonic-gate 		return (-1);
11997c478bd9Sstevel@tonic-gate 
12007c478bd9Sstevel@tonic-gate 	ctl[0] = PCSPRIV;
12017c478bd9Sstevel@tonic-gate 
12027c478bd9Sstevel@tonic-gate 	(void) memcpy(&ctl[1], pprv, PRIV_PRPRIV_SIZE(pprv));
12037c478bd9Sstevel@tonic-gate 
12047c478bd9Sstevel@tonic-gate 	if (write(P->ctlfd, ctl, sz) != sz)
12057c478bd9Sstevel@tonic-gate 		rc = -1;
12067c478bd9Sstevel@tonic-gate 	else
12077c478bd9Sstevel@tonic-gate 		rc = 0;
12087c478bd9Sstevel@tonic-gate 
12097c478bd9Sstevel@tonic-gate 	free(ctl);
12107c478bd9Sstevel@tonic-gate 
12117c478bd9Sstevel@tonic-gate 	return (rc);
12127c478bd9Sstevel@tonic-gate }
12137c478bd9Sstevel@tonic-gate 
12147c478bd9Sstevel@tonic-gate void *
12157c478bd9Sstevel@tonic-gate Pprivinfo(struct ps_prochandle *P)
12167c478bd9Sstevel@tonic-gate {
12177c478bd9Sstevel@tonic-gate 	/* Use default from libc */
12187c478bd9Sstevel@tonic-gate 	if (P->state != PS_DEAD)
12197c478bd9Sstevel@tonic-gate 		return (NULL);
12207c478bd9Sstevel@tonic-gate 
12217c478bd9Sstevel@tonic-gate 	return (P->core->core_privinfo);
12227c478bd9Sstevel@tonic-gate }
12237c478bd9Sstevel@tonic-gate 
12247c478bd9Sstevel@tonic-gate /*
12257c478bd9Sstevel@tonic-gate  * Ensure that all cached state is written to the process.
12267c478bd9Sstevel@tonic-gate  * The cached state is the LWP's signal mask and registers
12277c478bd9Sstevel@tonic-gate  * and the process's tracing flags.
12287c478bd9Sstevel@tonic-gate  */
12297c478bd9Sstevel@tonic-gate void
12307c478bd9Sstevel@tonic-gate Psync(struct ps_prochandle *P)
12317c478bd9Sstevel@tonic-gate {
12327c478bd9Sstevel@tonic-gate 	int ctlfd = (P->agentctlfd >= 0)? P->agentctlfd : P->ctlfd;
12337c478bd9Sstevel@tonic-gate 	long cmd[6];
12347c478bd9Sstevel@tonic-gate 	iovec_t iov[12];
12357c478bd9Sstevel@tonic-gate 	int n = 0;
12367c478bd9Sstevel@tonic-gate 
12377c478bd9Sstevel@tonic-gate 	if (P->flags & SETHOLD) {
12387c478bd9Sstevel@tonic-gate 		cmd[0] = PCSHOLD;
12397c478bd9Sstevel@tonic-gate 		iov[n].iov_base = (caddr_t)&cmd[0];
12407c478bd9Sstevel@tonic-gate 		iov[n++].iov_len = sizeof (long);
12417c478bd9Sstevel@tonic-gate 		iov[n].iov_base = (caddr_t)&P->status.pr_lwp.pr_lwphold;
12427c478bd9Sstevel@tonic-gate 		iov[n++].iov_len = sizeof (P->status.pr_lwp.pr_lwphold);
12437c478bd9Sstevel@tonic-gate 	}
12447c478bd9Sstevel@tonic-gate 	if (P->flags & SETREGS) {
12457c478bd9Sstevel@tonic-gate 		cmd[1] = PCSREG;
12467c478bd9Sstevel@tonic-gate #ifdef __i386
12477c478bd9Sstevel@tonic-gate 		/* XX64 we should probably restore REG_GS after this */
12487c478bd9Sstevel@tonic-gate 		if (ctlfd == P->agentctlfd)
12497c478bd9Sstevel@tonic-gate 			P->status.pr_lwp.pr_reg[GS] = 0;
12507c478bd9Sstevel@tonic-gate #elif defined(__amd64)
12517c478bd9Sstevel@tonic-gate 		/* XX64 */
12527c478bd9Sstevel@tonic-gate #endif
12537c478bd9Sstevel@tonic-gate 		iov[n].iov_base = (caddr_t)&cmd[1];
12547c478bd9Sstevel@tonic-gate 		iov[n++].iov_len = sizeof (long);
12557c478bd9Sstevel@tonic-gate 		iov[n].iov_base = (caddr_t)&P->status.pr_lwp.pr_reg[0];
12567c478bd9Sstevel@tonic-gate 		iov[n++].iov_len = sizeof (P->status.pr_lwp.pr_reg);
12577c478bd9Sstevel@tonic-gate 	}
12587c478bd9Sstevel@tonic-gate 	if (P->flags & SETSIG) {
12597c478bd9Sstevel@tonic-gate 		cmd[2] = PCSTRACE;
12607c478bd9Sstevel@tonic-gate 		iov[n].iov_base = (caddr_t)&cmd[2];
12617c478bd9Sstevel@tonic-gate 		iov[n++].iov_len = sizeof (long);
12627c478bd9Sstevel@tonic-gate 		iov[n].iov_base = (caddr_t)&P->status.pr_sigtrace;
12637c478bd9Sstevel@tonic-gate 		iov[n++].iov_len = sizeof (P->status.pr_sigtrace);
12647c478bd9Sstevel@tonic-gate 	}
12657c478bd9Sstevel@tonic-gate 	if (P->flags & SETFAULT) {
12667c478bd9Sstevel@tonic-gate 		cmd[3] = PCSFAULT;
12677c478bd9Sstevel@tonic-gate 		iov[n].iov_base = (caddr_t)&cmd[3];
12687c478bd9Sstevel@tonic-gate 		iov[n++].iov_len = sizeof (long);
12697c478bd9Sstevel@tonic-gate 		iov[n].iov_base = (caddr_t)&P->status.pr_flttrace;
12707c478bd9Sstevel@tonic-gate 		iov[n++].iov_len = sizeof (P->status.pr_flttrace);
12717c478bd9Sstevel@tonic-gate 	}
12727c478bd9Sstevel@tonic-gate 	if (P->flags & SETENTRY) {
12737c478bd9Sstevel@tonic-gate 		cmd[4] = PCSENTRY;
12747c478bd9Sstevel@tonic-gate 		iov[n].iov_base = (caddr_t)&cmd[4];
12757c478bd9Sstevel@tonic-gate 		iov[n++].iov_len = sizeof (long);
12767c478bd9Sstevel@tonic-gate 		iov[n].iov_base = (caddr_t)&P->status.pr_sysentry;
12777c478bd9Sstevel@tonic-gate 		iov[n++].iov_len = sizeof (P->status.pr_sysentry);
12787c478bd9Sstevel@tonic-gate 	}
12797c478bd9Sstevel@tonic-gate 	if (P->flags & SETEXIT) {
12807c478bd9Sstevel@tonic-gate 		cmd[5] = PCSEXIT;
12817c478bd9Sstevel@tonic-gate 		iov[n].iov_base = (caddr_t)&cmd[5];
12827c478bd9Sstevel@tonic-gate 		iov[n++].iov_len = sizeof (long);
12837c478bd9Sstevel@tonic-gate 		iov[n].iov_base = (caddr_t)&P->status.pr_sysexit;
12847c478bd9Sstevel@tonic-gate 		iov[n++].iov_len = sizeof (P->status.pr_sysexit);
12857c478bd9Sstevel@tonic-gate 	}
12867c478bd9Sstevel@tonic-gate 
12877c478bd9Sstevel@tonic-gate 	if (n == 0 || writev(ctlfd, iov, n) < 0)
12887c478bd9Sstevel@tonic-gate 		return;		/* nothing to do or write failed */
12897c478bd9Sstevel@tonic-gate 
12907c478bd9Sstevel@tonic-gate 	P->flags &= ~(SETSIG|SETFAULT|SETENTRY|SETEXIT|SETHOLD|SETREGS);
12917c478bd9Sstevel@tonic-gate }
12927c478bd9Sstevel@tonic-gate 
12937c478bd9Sstevel@tonic-gate /*
12947c478bd9Sstevel@tonic-gate  * Reopen the /proc file (after PS_LOST).
12957c478bd9Sstevel@tonic-gate  */
12967c478bd9Sstevel@tonic-gate int
12977c478bd9Sstevel@tonic-gate Preopen(struct ps_prochandle *P)
12987c478bd9Sstevel@tonic-gate {
12997c478bd9Sstevel@tonic-gate 	int fd;
13009acbbeafSnn35248 	char procname[PATH_MAX];
13017c478bd9Sstevel@tonic-gate 	char *fname;
13027c478bd9Sstevel@tonic-gate 
13037c478bd9Sstevel@tonic-gate 	if (P->state == PS_DEAD || P->state == PS_IDLE)
13047c478bd9Sstevel@tonic-gate 		return (0);
13057c478bd9Sstevel@tonic-gate 
13067c478bd9Sstevel@tonic-gate 	if (P->agentcnt > 0) {
13077c478bd9Sstevel@tonic-gate 		P->agentcnt = 1;
13087c478bd9Sstevel@tonic-gate 		Pdestroy_agent(P);
13097c478bd9Sstevel@tonic-gate 	}
13107c478bd9Sstevel@tonic-gate 
13119acbbeafSnn35248 	(void) snprintf(procname, sizeof (procname), "%s/%d/",
13129acbbeafSnn35248 	    procfs_path, (int)P->pid);
13137c478bd9Sstevel@tonic-gate 	fname = procname + strlen(procname);
13147c478bd9Sstevel@tonic-gate 
13157c478bd9Sstevel@tonic-gate 	(void) strcpy(fname, "as");
13167c478bd9Sstevel@tonic-gate 	if ((fd = open(procname, O_RDWR)) < 0 ||
13177c478bd9Sstevel@tonic-gate 	    close(P->asfd) < 0 ||
13187c478bd9Sstevel@tonic-gate 	    (fd = dupfd(fd, P->asfd)) != P->asfd) {
13197c478bd9Sstevel@tonic-gate 		dprintf("Preopen: failed to open %s: %s\n",
13207c478bd9Sstevel@tonic-gate 		    procname, strerror(errno));
13217c478bd9Sstevel@tonic-gate 		if (fd >= 0)
13227c478bd9Sstevel@tonic-gate 			(void) close(fd);
13237c478bd9Sstevel@tonic-gate 		return (-1);
13247c478bd9Sstevel@tonic-gate 	}
13257c478bd9Sstevel@tonic-gate 	P->asfd = fd;
13267c478bd9Sstevel@tonic-gate 
13277c478bd9Sstevel@tonic-gate 	(void) strcpy(fname, "status");
13287c478bd9Sstevel@tonic-gate 	if ((fd = open(procname, O_RDONLY)) < 0 ||
13297c478bd9Sstevel@tonic-gate 	    close(P->statfd) < 0 ||
13307c478bd9Sstevel@tonic-gate 	    (fd = dupfd(fd, P->statfd)) != P->statfd) {
13317c478bd9Sstevel@tonic-gate 		dprintf("Preopen: failed to open %s: %s\n",
13327c478bd9Sstevel@tonic-gate 		    procname, strerror(errno));
13337c478bd9Sstevel@tonic-gate 		if (fd >= 0)
13347c478bd9Sstevel@tonic-gate 			(void) close(fd);
13357c478bd9Sstevel@tonic-gate 		return (-1);
13367c478bd9Sstevel@tonic-gate 	}
13377c478bd9Sstevel@tonic-gate 	P->statfd = fd;
13387c478bd9Sstevel@tonic-gate 
13397c478bd9Sstevel@tonic-gate 	(void) strcpy(fname, "ctl");
13407c478bd9Sstevel@tonic-gate 	if ((fd = open(procname, O_WRONLY)) < 0 ||
13417c478bd9Sstevel@tonic-gate 	    close(P->ctlfd) < 0 ||
13427c478bd9Sstevel@tonic-gate 	    (fd = dupfd(fd, P->ctlfd)) != P->ctlfd) {
13437c478bd9Sstevel@tonic-gate 		dprintf("Preopen: failed to open %s: %s\n",
13447c478bd9Sstevel@tonic-gate 		    procname, strerror(errno));
13457c478bd9Sstevel@tonic-gate 		if (fd >= 0)
13467c478bd9Sstevel@tonic-gate 			(void) close(fd);
13477c478bd9Sstevel@tonic-gate 		return (-1);
13487c478bd9Sstevel@tonic-gate 	}
13497c478bd9Sstevel@tonic-gate 	P->ctlfd = fd;
13507c478bd9Sstevel@tonic-gate 
13517c478bd9Sstevel@tonic-gate 	/*
13527c478bd9Sstevel@tonic-gate 	 * Set the state to PS_RUN and wait for the process to stop so that
13537c478bd9Sstevel@tonic-gate 	 * we re-read the status from the new P->statfd.  If this fails, Pwait
13547c478bd9Sstevel@tonic-gate 	 * will reset the state to PS_LOST and we fail the reopen.  Before
13557c478bd9Sstevel@tonic-gate 	 * returning, we also forge a bit of P->status to allow the debugger to
13567c478bd9Sstevel@tonic-gate 	 * see that we are PS_LOST following a successful exec.
13577c478bd9Sstevel@tonic-gate 	 */
13587c478bd9Sstevel@tonic-gate 	P->state = PS_RUN;
13597c478bd9Sstevel@tonic-gate 	if (Pwait(P, 0) == -1) {
13607c478bd9Sstevel@tonic-gate #ifdef _ILP32
13617c478bd9Sstevel@tonic-gate 		if (errno == EOVERFLOW)
13627c478bd9Sstevel@tonic-gate 			P->status.pr_dmodel = PR_MODEL_LP64;
13637c478bd9Sstevel@tonic-gate #endif
13647c478bd9Sstevel@tonic-gate 		P->status.pr_lwp.pr_why = PR_SYSEXIT;
13657c478bd9Sstevel@tonic-gate 		P->status.pr_lwp.pr_what = SYS_execve;
13667c478bd9Sstevel@tonic-gate 		P->status.pr_lwp.pr_errno = 0;
13677c478bd9Sstevel@tonic-gate 		return (-1);
13687c478bd9Sstevel@tonic-gate 	}
13697c478bd9Sstevel@tonic-gate 
13707c478bd9Sstevel@tonic-gate 	/*
13717c478bd9Sstevel@tonic-gate 	 * The process should be stopped on exec (REQUESTED)
13727c478bd9Sstevel@tonic-gate 	 * or else should be stopped on exit from exec() (SYSEXIT)
13737c478bd9Sstevel@tonic-gate 	 */
13747c478bd9Sstevel@tonic-gate 	if (P->state == PS_STOP &&
13757c478bd9Sstevel@tonic-gate 	    (P->status.pr_lwp.pr_why == PR_REQUESTED ||
13767c478bd9Sstevel@tonic-gate 	    (P->status.pr_lwp.pr_why == PR_SYSEXIT &&
13778fd04b83SRoger A. Faulkner 	    P->status.pr_lwp.pr_what == SYS_execve))) {
13787c478bd9Sstevel@tonic-gate 		/* fake up stop-on-exit-from-execve */
13797c478bd9Sstevel@tonic-gate 		if (P->status.pr_lwp.pr_why == PR_REQUESTED) {
13807c478bd9Sstevel@tonic-gate 			P->status.pr_lwp.pr_why = PR_SYSEXIT;
13817c478bd9Sstevel@tonic-gate 			P->status.pr_lwp.pr_what = SYS_execve;
13827c478bd9Sstevel@tonic-gate 			P->status.pr_lwp.pr_errno = 0;
13837c478bd9Sstevel@tonic-gate 		}
13847c478bd9Sstevel@tonic-gate 	} else {
13857c478bd9Sstevel@tonic-gate 		dprintf("Preopen: expected REQUESTED or "
13867c478bd9Sstevel@tonic-gate 		    "SYSEXIT(SYS_execve) stop\n");
13877c478bd9Sstevel@tonic-gate 	}
13887c478bd9Sstevel@tonic-gate 
13897c478bd9Sstevel@tonic-gate 	return (0);
13907c478bd9Sstevel@tonic-gate }
13917c478bd9Sstevel@tonic-gate 
13927c478bd9Sstevel@tonic-gate /*
13937c478bd9Sstevel@tonic-gate  * Define all settable flags other than the microstate accounting flags.
13947c478bd9Sstevel@tonic-gate  */
13957c478bd9Sstevel@tonic-gate #define	ALL_SETTABLE_FLAGS (PR_FORK|PR_RLC|PR_KLC|PR_ASYNC|PR_BPTADJ|PR_PTRACE)
13967c478bd9Sstevel@tonic-gate 
13977c478bd9Sstevel@tonic-gate /*
13987c478bd9Sstevel@tonic-gate  * Restore /proc tracing flags to their original values
13997c478bd9Sstevel@tonic-gate  * in preparation for releasing the process.
14007c478bd9Sstevel@tonic-gate  * Also called by Pcreate() to clear all tracing flags.
14017c478bd9Sstevel@tonic-gate  */
14027c478bd9Sstevel@tonic-gate static void
14037c478bd9Sstevel@tonic-gate restore_tracing_flags(struct ps_prochandle *P)
14047c478bd9Sstevel@tonic-gate {
14057c478bd9Sstevel@tonic-gate 	long flags;
14067c478bd9Sstevel@tonic-gate 	long cmd[4];
14077c478bd9Sstevel@tonic-gate 	iovec_t iov[8];
14087c478bd9Sstevel@tonic-gate 
14097c478bd9Sstevel@tonic-gate 	if (P->flags & CREATED) {
14107c478bd9Sstevel@tonic-gate 		/* we created this process; clear all tracing flags */
14117c478bd9Sstevel@tonic-gate 		premptyset(&P->status.pr_sigtrace);
14127c478bd9Sstevel@tonic-gate 		premptyset(&P->status.pr_flttrace);
14137c478bd9Sstevel@tonic-gate 		premptyset(&P->status.pr_sysentry);
14147c478bd9Sstevel@tonic-gate 		premptyset(&P->status.pr_sysexit);
14157c478bd9Sstevel@tonic-gate 		if ((P->status.pr_flags & ALL_SETTABLE_FLAGS) != 0)
14167c478bd9Sstevel@tonic-gate 			(void) Punsetflags(P, ALL_SETTABLE_FLAGS);
14177c478bd9Sstevel@tonic-gate 	} else {
14187c478bd9Sstevel@tonic-gate 		/* we grabbed the process; restore its tracing flags */
14197c478bd9Sstevel@tonic-gate 		P->status.pr_sigtrace = P->orig_status.pr_sigtrace;
14207c478bd9Sstevel@tonic-gate 		P->status.pr_flttrace = P->orig_status.pr_flttrace;
14217c478bd9Sstevel@tonic-gate 		P->status.pr_sysentry = P->orig_status.pr_sysentry;
14227c478bd9Sstevel@tonic-gate 		P->status.pr_sysexit  = P->orig_status.pr_sysexit;
14237c478bd9Sstevel@tonic-gate 		if ((P->status.pr_flags & ALL_SETTABLE_FLAGS) !=
14247c478bd9Sstevel@tonic-gate 		    (flags = (P->orig_status.pr_flags & ALL_SETTABLE_FLAGS))) {
14257c478bd9Sstevel@tonic-gate 			(void) Punsetflags(P, ALL_SETTABLE_FLAGS);
14267c478bd9Sstevel@tonic-gate 			if (flags)
14277c478bd9Sstevel@tonic-gate 				(void) Psetflags(P, flags);
14287c478bd9Sstevel@tonic-gate 		}
14297c478bd9Sstevel@tonic-gate 	}
14307c478bd9Sstevel@tonic-gate 
14317c478bd9Sstevel@tonic-gate 	cmd[0] = PCSTRACE;
14327c478bd9Sstevel@tonic-gate 	iov[0].iov_base = (caddr_t)&cmd[0];
14337c478bd9Sstevel@tonic-gate 	iov[0].iov_len = sizeof (long);
14347c478bd9Sstevel@tonic-gate 	iov[1].iov_base = (caddr_t)&P->status.pr_sigtrace;
14357c478bd9Sstevel@tonic-gate 	iov[1].iov_len = sizeof (P->status.pr_sigtrace);
14367c478bd9Sstevel@tonic-gate 
14377c478bd9Sstevel@tonic-gate 	cmd[1] = PCSFAULT;
14387c478bd9Sstevel@tonic-gate 	iov[2].iov_base = (caddr_t)&cmd[1];
14397c478bd9Sstevel@tonic-gate 	iov[2].iov_len = sizeof (long);
14407c478bd9Sstevel@tonic-gate 	iov[3].iov_base = (caddr_t)&P->status.pr_flttrace;
14417c478bd9Sstevel@tonic-gate 	iov[3].iov_len = sizeof (P->status.pr_flttrace);
14427c478bd9Sstevel@tonic-gate 
14437c478bd9Sstevel@tonic-gate 	cmd[2] = PCSENTRY;
14447c478bd9Sstevel@tonic-gate 	iov[4].iov_base = (caddr_t)&cmd[2];
14457c478bd9Sstevel@tonic-gate 	iov[4].iov_len = sizeof (long);
14467c478bd9Sstevel@tonic-gate 	iov[5].iov_base = (caddr_t)&P->status.pr_sysentry;
14477c478bd9Sstevel@tonic-gate 	iov[5].iov_len = sizeof (P->status.pr_sysentry);
14487c478bd9Sstevel@tonic-gate 
14497c478bd9Sstevel@tonic-gate 	cmd[3] = PCSEXIT;
14507c478bd9Sstevel@tonic-gate 	iov[6].iov_base = (caddr_t)&cmd[3];
14517c478bd9Sstevel@tonic-gate 	iov[6].iov_len = sizeof (long);
14527c478bd9Sstevel@tonic-gate 	iov[7].iov_base = (caddr_t)&P->status.pr_sysexit;
14537c478bd9Sstevel@tonic-gate 	iov[7].iov_len = sizeof (P->status.pr_sysexit);
14547c478bd9Sstevel@tonic-gate 
14557c478bd9Sstevel@tonic-gate 	(void) writev(P->ctlfd, iov, 8);
14567c478bd9Sstevel@tonic-gate 
14577c478bd9Sstevel@tonic-gate 	P->flags &= ~(SETSIG|SETFAULT|SETENTRY|SETEXIT);
14587c478bd9Sstevel@tonic-gate }
14597c478bd9Sstevel@tonic-gate 
14607c478bd9Sstevel@tonic-gate /*
14617c478bd9Sstevel@tonic-gate  * Release the process.  Frees the process control structure.
14627c478bd9Sstevel@tonic-gate  * flags:
14637c478bd9Sstevel@tonic-gate  *	PRELEASE_CLEAR	Clear all tracing flags.
14647c478bd9Sstevel@tonic-gate  *	PRELEASE_RETAIN	Retain current tracing flags.
14657c478bd9Sstevel@tonic-gate  *	PRELEASE_HANG	Leave the process stopped and abandoned.
14667c478bd9Sstevel@tonic-gate  *	PRELEASE_KILL	Terminate the process with SIGKILL.
14677c478bd9Sstevel@tonic-gate  */
14687c478bd9Sstevel@tonic-gate void
14697c478bd9Sstevel@tonic-gate Prelease(struct ps_prochandle *P, int flags)
14707c478bd9Sstevel@tonic-gate {
14717c478bd9Sstevel@tonic-gate 	if (P->state == PS_DEAD) {
14727c478bd9Sstevel@tonic-gate 		dprintf("Prelease: releasing handle %p PS_DEAD of pid %d\n",
14737c478bd9Sstevel@tonic-gate 		    (void *)P, (int)P->pid);
14747c478bd9Sstevel@tonic-gate 		Pfree(P);
14757c478bd9Sstevel@tonic-gate 		return;
14767c478bd9Sstevel@tonic-gate 	}
14777c478bd9Sstevel@tonic-gate 
14787c478bd9Sstevel@tonic-gate 	if (P->state == PS_IDLE) {
14797c478bd9Sstevel@tonic-gate 		file_info_t *fptr = list_next(&P->file_head);
14807c478bd9Sstevel@tonic-gate 		dprintf("Prelease: releasing handle %p PS_IDLE of file %s\n",
14817c478bd9Sstevel@tonic-gate 		    (void *)P, fptr->file_pname);
14827c478bd9Sstevel@tonic-gate 		Pfree(P);
14837c478bd9Sstevel@tonic-gate 		return;
14847c478bd9Sstevel@tonic-gate 	}
14857c478bd9Sstevel@tonic-gate 
14867c478bd9Sstevel@tonic-gate 	dprintf("Prelease: releasing handle %p pid %d\n",
14877c478bd9Sstevel@tonic-gate 	    (void *)P, (int)P->pid);
14887c478bd9Sstevel@tonic-gate 
14897c478bd9Sstevel@tonic-gate 	if (P->ctlfd == -1) {
14907c478bd9Sstevel@tonic-gate 		Pfree(P);
14917c478bd9Sstevel@tonic-gate 		return;
14927c478bd9Sstevel@tonic-gate 	}
14937c478bd9Sstevel@tonic-gate 
14947c478bd9Sstevel@tonic-gate 	if (P->agentcnt > 0) {
14957c478bd9Sstevel@tonic-gate 		P->agentcnt = 1;
14967c478bd9Sstevel@tonic-gate 		Pdestroy_agent(P);
14977c478bd9Sstevel@tonic-gate 	}
14987c478bd9Sstevel@tonic-gate 
14997c478bd9Sstevel@tonic-gate 	/*
15007c478bd9Sstevel@tonic-gate 	 * Attempt to stop the process.
15017c478bd9Sstevel@tonic-gate 	 */
15027c478bd9Sstevel@tonic-gate 	P->state = PS_RUN;
15037c478bd9Sstevel@tonic-gate 	(void) Pstop(P, 1000);
15047c478bd9Sstevel@tonic-gate 
15057c478bd9Sstevel@tonic-gate 	if (flags & PRELEASE_KILL) {
15067c478bd9Sstevel@tonic-gate 		if (P->state == PS_STOP)
15077c478bd9Sstevel@tonic-gate 			(void) Psetrun(P, SIGKILL, 0);
15087c478bd9Sstevel@tonic-gate 		(void) kill(P->pid, SIGKILL);
15097c478bd9Sstevel@tonic-gate 		Pfree(P);
15107c478bd9Sstevel@tonic-gate 		return;
15117c478bd9Sstevel@tonic-gate 	}
15127c478bd9Sstevel@tonic-gate 
15137c478bd9Sstevel@tonic-gate 	/*
15147c478bd9Sstevel@tonic-gate 	 * If we lost control, all we can do now is close the files.
15157c478bd9Sstevel@tonic-gate 	 * In this case, the last close sets the process running.
15167c478bd9Sstevel@tonic-gate 	 */
15177c478bd9Sstevel@tonic-gate 	if (P->state != PS_STOP &&
15187c478bd9Sstevel@tonic-gate 	    (P->status.pr_lwp.pr_flags & (PR_ISTOP|PR_DSTOP)) == 0) {
15197c478bd9Sstevel@tonic-gate 		Pfree(P);
15207c478bd9Sstevel@tonic-gate 		return;
15217c478bd9Sstevel@tonic-gate 	}
15227c478bd9Sstevel@tonic-gate 
15237c478bd9Sstevel@tonic-gate 	/*
15247c478bd9Sstevel@tonic-gate 	 * We didn't lose control; we do more.
15257c478bd9Sstevel@tonic-gate 	 */
15267c478bd9Sstevel@tonic-gate 	Psync(P);
15277c478bd9Sstevel@tonic-gate 
15287c478bd9Sstevel@tonic-gate 	if (flags & PRELEASE_CLEAR)
15297c478bd9Sstevel@tonic-gate 		P->flags |= CREATED;
15307c478bd9Sstevel@tonic-gate 
15317c478bd9Sstevel@tonic-gate 	if (!(flags & PRELEASE_RETAIN))
15327c478bd9Sstevel@tonic-gate 		restore_tracing_flags(P);
15337c478bd9Sstevel@tonic-gate 
15347c478bd9Sstevel@tonic-gate 	if (flags & PRELEASE_HANG) {
15357c478bd9Sstevel@tonic-gate 		/* Leave the process stopped and abandoned */
15367c478bd9Sstevel@tonic-gate 		(void) Punsetflags(P, PR_RLC|PR_KLC);
15377c478bd9Sstevel@tonic-gate 		Pfree(P);
15387c478bd9Sstevel@tonic-gate 		return;
15397c478bd9Sstevel@tonic-gate 	}
15407c478bd9Sstevel@tonic-gate 
15417c478bd9Sstevel@tonic-gate 	/*
15427c478bd9Sstevel@tonic-gate 	 * Set the process running if we created it or if it was
15437c478bd9Sstevel@tonic-gate 	 * not originally stopped or directed to stop via /proc
15447c478bd9Sstevel@tonic-gate 	 * or if we were given the PRELEASE_CLEAR flag.
15457c478bd9Sstevel@tonic-gate 	 */
15467c478bd9Sstevel@tonic-gate 	if ((P->flags & CREATED) ||
15477c478bd9Sstevel@tonic-gate 	    (P->orig_status.pr_lwp.pr_flags & (PR_ISTOP|PR_DSTOP)) == 0) {
15487c478bd9Sstevel@tonic-gate 		(void) Psetflags(P, PR_RLC);
15497c478bd9Sstevel@tonic-gate 		/*
15507c478bd9Sstevel@tonic-gate 		 * We do this repeatedly because the process may have
15517c478bd9Sstevel@tonic-gate 		 * more than one LWP stopped on an event of interest.
15527c478bd9Sstevel@tonic-gate 		 * This makes sure all of them are set running.
15537c478bd9Sstevel@tonic-gate 		 */
15547c478bd9Sstevel@tonic-gate 		do {
15557c478bd9Sstevel@tonic-gate 			if (Psetrun(P, 0, 0) == -1 && errno == EBUSY)
15567c478bd9Sstevel@tonic-gate 				break; /* Agent LWP may be stuck */
15577c478bd9Sstevel@tonic-gate 		} while (Pstopstatus(P, PCNULL, 0) == 0 &&
15587c478bd9Sstevel@tonic-gate 		    P->status.pr_lwp.pr_flags & (PR_ISTOP|PR_DSTOP));
15597c478bd9Sstevel@tonic-gate 
15607c478bd9Sstevel@tonic-gate 		if (P->status.pr_lwp.pr_flags & (PR_ISTOP|PR_DSTOP))
15617c478bd9Sstevel@tonic-gate 			dprintf("Prelease: failed to set process running\n");
15627c478bd9Sstevel@tonic-gate 	}
15637c478bd9Sstevel@tonic-gate 
15647c478bd9Sstevel@tonic-gate 	Pfree(P);
15657c478bd9Sstevel@tonic-gate }
15667c478bd9Sstevel@tonic-gate 
15677c478bd9Sstevel@tonic-gate /* debugging */
15687c478bd9Sstevel@tonic-gate void
15697c478bd9Sstevel@tonic-gate prldump(const char *caller, lwpstatus_t *lsp)
15707c478bd9Sstevel@tonic-gate {
15717c478bd9Sstevel@tonic-gate 	char name[32];
15727c478bd9Sstevel@tonic-gate 	uint32_t bits;
15737c478bd9Sstevel@tonic-gate 
15747c478bd9Sstevel@tonic-gate 	switch (lsp->pr_why) {
15757c478bd9Sstevel@tonic-gate 	case PR_REQUESTED:
15767c478bd9Sstevel@tonic-gate 		dprintf("%s: REQUESTED\n", caller);
15777c478bd9Sstevel@tonic-gate 		break;
15787c478bd9Sstevel@tonic-gate 	case PR_SIGNALLED:
15797c478bd9Sstevel@tonic-gate 		dprintf("%s: SIGNALLED %s\n", caller,
15807c478bd9Sstevel@tonic-gate 		    proc_signame(lsp->pr_what, name, sizeof (name)));
15817c478bd9Sstevel@tonic-gate 		break;
15827c478bd9Sstevel@tonic-gate 	case PR_FAULTED:
15837c478bd9Sstevel@tonic-gate 		dprintf("%s: FAULTED %s\n", caller,
15847c478bd9Sstevel@tonic-gate 		    proc_fltname(lsp->pr_what, name, sizeof (name)));
15857c478bd9Sstevel@tonic-gate 		break;
15867c478bd9Sstevel@tonic-gate 	case PR_SYSENTRY:
15877c478bd9Sstevel@tonic-gate 		dprintf("%s: SYSENTRY %s\n", caller,
15887c478bd9Sstevel@tonic-gate 		    proc_sysname(lsp->pr_what, name, sizeof (name)));
15897c478bd9Sstevel@tonic-gate 		break;
15907c478bd9Sstevel@tonic-gate 	case PR_SYSEXIT:
15917c478bd9Sstevel@tonic-gate 		dprintf("%s: SYSEXIT %s\n", caller,
15927c478bd9Sstevel@tonic-gate 		    proc_sysname(lsp->pr_what, name, sizeof (name)));
15937c478bd9Sstevel@tonic-gate 		break;
15947c478bd9Sstevel@tonic-gate 	case PR_JOBCONTROL:
15957c478bd9Sstevel@tonic-gate 		dprintf("%s: JOBCONTROL %s\n", caller,
15967c478bd9Sstevel@tonic-gate 		    proc_signame(lsp->pr_what, name, sizeof (name)));
15977c478bd9Sstevel@tonic-gate 		break;
15987c478bd9Sstevel@tonic-gate 	case PR_SUSPENDED:
15997c478bd9Sstevel@tonic-gate 		dprintf("%s: SUSPENDED\n", caller);
16007c478bd9Sstevel@tonic-gate 		break;
16017c478bd9Sstevel@tonic-gate 	default:
16027c478bd9Sstevel@tonic-gate 		dprintf("%s: Unknown\n", caller);
16037c478bd9Sstevel@tonic-gate 		break;
16047c478bd9Sstevel@tonic-gate 	}
16057c478bd9Sstevel@tonic-gate 
16067c478bd9Sstevel@tonic-gate 	if (lsp->pr_cursig)
16077c478bd9Sstevel@tonic-gate 		dprintf("%s: p_cursig  = %d\n", caller, lsp->pr_cursig);
16087c478bd9Sstevel@tonic-gate 
16097c478bd9Sstevel@tonic-gate 	bits = *((uint32_t *)&lsp->pr_lwppend);
16107c478bd9Sstevel@tonic-gate 	if (bits)
16117c478bd9Sstevel@tonic-gate 		dprintf("%s: pr_lwppend = 0x%.8X\n", caller, bits);
16127c478bd9Sstevel@tonic-gate }
16137c478bd9Sstevel@tonic-gate 
16147c478bd9Sstevel@tonic-gate /* debugging */
16157c478bd9Sstevel@tonic-gate static void
16167c478bd9Sstevel@tonic-gate prdump(struct ps_prochandle *P)
16177c478bd9Sstevel@tonic-gate {
16187c478bd9Sstevel@tonic-gate 	uint32_t bits;
16197c478bd9Sstevel@tonic-gate 
16207c478bd9Sstevel@tonic-gate 	prldump("Pstopstatus", &P->status.pr_lwp);
16217c478bd9Sstevel@tonic-gate 
16227c478bd9Sstevel@tonic-gate 	bits = *((uint32_t *)&P->status.pr_sigpend);
16237c478bd9Sstevel@tonic-gate 	if (bits)
16247c478bd9Sstevel@tonic-gate 		dprintf("Pstopstatus: pr_sigpend = 0x%.8X\n", bits);
16257c478bd9Sstevel@tonic-gate }
16267c478bd9Sstevel@tonic-gate 
16277c478bd9Sstevel@tonic-gate /*
16287c478bd9Sstevel@tonic-gate  * Wait for the specified process to stop or terminate.
16297c478bd9Sstevel@tonic-gate  * Or, just get the current status (PCNULL).
16307c478bd9Sstevel@tonic-gate  * Or, direct it to stop and get the current status (PCDSTOP).
16317c478bd9Sstevel@tonic-gate  * If the agent LWP exists, do these things to the agent,
16327c478bd9Sstevel@tonic-gate  * else do these things to the process as a whole.
16337c478bd9Sstevel@tonic-gate  */
16347c478bd9Sstevel@tonic-gate int
16357c478bd9Sstevel@tonic-gate Pstopstatus(struct ps_prochandle *P,
16367c478bd9Sstevel@tonic-gate 	long request,		/* PCNULL, PCDSTOP, PCSTOP, PCWSTOP */
16377c478bd9Sstevel@tonic-gate 	uint_t msec)		/* if non-zero, timeout in milliseconds */
16387c478bd9Sstevel@tonic-gate {
16397c478bd9Sstevel@tonic-gate 	int ctlfd = (P->agentctlfd >= 0)? P->agentctlfd : P->ctlfd;
16407c478bd9Sstevel@tonic-gate 	long ctl[3];
16417c478bd9Sstevel@tonic-gate 	ssize_t rc;
16427c478bd9Sstevel@tonic-gate 	int err;
16437c478bd9Sstevel@tonic-gate 	int old_state = P->state;
16447c478bd9Sstevel@tonic-gate 
16457c478bd9Sstevel@tonic-gate 	switch (P->state) {
16467c478bd9Sstevel@tonic-gate 	case PS_RUN:
16477c478bd9Sstevel@tonic-gate 		break;
16487c478bd9Sstevel@tonic-gate 	case PS_STOP:
16497c478bd9Sstevel@tonic-gate 		if (request != PCNULL && request != PCDSTOP)
16507c478bd9Sstevel@tonic-gate 			return (0);
16517c478bd9Sstevel@tonic-gate 		break;
16527c478bd9Sstevel@tonic-gate 	case PS_LOST:
16537c478bd9Sstevel@tonic-gate 		if (request != PCNULL) {
16547c478bd9Sstevel@tonic-gate 			errno = EAGAIN;
16557c478bd9Sstevel@tonic-gate 			return (-1);
16567c478bd9Sstevel@tonic-gate 		}
16577c478bd9Sstevel@tonic-gate 		break;
16587c478bd9Sstevel@tonic-gate 	case PS_UNDEAD:
16597c478bd9Sstevel@tonic-gate 	case PS_DEAD:
16607c478bd9Sstevel@tonic-gate 	case PS_IDLE:
16617c478bd9Sstevel@tonic-gate 		if (request != PCNULL) {
16627c478bd9Sstevel@tonic-gate 			errno = ENOENT;
16637c478bd9Sstevel@tonic-gate 			return (-1);
16647c478bd9Sstevel@tonic-gate 		}
16657c478bd9Sstevel@tonic-gate 		break;
16667c478bd9Sstevel@tonic-gate 	default:	/* corrupted state */
16677c478bd9Sstevel@tonic-gate 		dprintf("Pstopstatus: corrupted state: %d\n", P->state);
16687c478bd9Sstevel@tonic-gate 		errno = EINVAL;
16697c478bd9Sstevel@tonic-gate 		return (-1);
16707c478bd9Sstevel@tonic-gate 	}
16717c478bd9Sstevel@tonic-gate 
16727c478bd9Sstevel@tonic-gate 	ctl[0] = PCDSTOP;
16737c478bd9Sstevel@tonic-gate 	ctl[1] = PCTWSTOP;
16747c478bd9Sstevel@tonic-gate 	ctl[2] = (long)msec;
16757c478bd9Sstevel@tonic-gate 	rc = 0;
16767c478bd9Sstevel@tonic-gate 	switch (request) {
16777c478bd9Sstevel@tonic-gate 	case PCSTOP:
16787c478bd9Sstevel@tonic-gate 		rc = write(ctlfd, &ctl[0], 3*sizeof (long));
16797c478bd9Sstevel@tonic-gate 		break;
16807c478bd9Sstevel@tonic-gate 	case PCWSTOP:
16817c478bd9Sstevel@tonic-gate 		rc = write(ctlfd, &ctl[1], 2*sizeof (long));
16827c478bd9Sstevel@tonic-gate 		break;
16837c478bd9Sstevel@tonic-gate 	case PCDSTOP:
16847c478bd9Sstevel@tonic-gate 		rc = write(ctlfd, &ctl[0], 1*sizeof (long));
16857c478bd9Sstevel@tonic-gate 		break;
16867c478bd9Sstevel@tonic-gate 	case PCNULL:
16877c478bd9Sstevel@tonic-gate 		if (P->state == PS_DEAD || P->state == PS_IDLE)
16887c478bd9Sstevel@tonic-gate 			return (0);
16897c478bd9Sstevel@tonic-gate 		break;
16907c478bd9Sstevel@tonic-gate 	default:	/* programming error */
16917c478bd9Sstevel@tonic-gate 		errno = EINVAL;
16927c478bd9Sstevel@tonic-gate 		return (-1);
16937c478bd9Sstevel@tonic-gate 	}
16947c478bd9Sstevel@tonic-gate 	err = (rc < 0)? errno : 0;
16957c478bd9Sstevel@tonic-gate 	Psync(P);
16967c478bd9Sstevel@tonic-gate 
16977c478bd9Sstevel@tonic-gate 	if (P->agentstatfd < 0) {
16987c478bd9Sstevel@tonic-gate 		if (pread(P->statfd, &P->status,
16997c478bd9Sstevel@tonic-gate 		    sizeof (P->status), (off_t)0) < 0)
17007c478bd9Sstevel@tonic-gate 			err = errno;
17017c478bd9Sstevel@tonic-gate 	} else {
17027c478bd9Sstevel@tonic-gate 		if (pread(P->agentstatfd, &P->status.pr_lwp,
17037c478bd9Sstevel@tonic-gate 		    sizeof (P->status.pr_lwp), (off_t)0) < 0)
17047c478bd9Sstevel@tonic-gate 			err = errno;
17057c478bd9Sstevel@tonic-gate 		P->status.pr_flags = P->status.pr_lwp.pr_flags;
17067c478bd9Sstevel@tonic-gate 	}
17077c478bd9Sstevel@tonic-gate 
17087c478bd9Sstevel@tonic-gate 	if (err) {
17097c478bd9Sstevel@tonic-gate 		switch (err) {
17107c478bd9Sstevel@tonic-gate 		case EINTR:		/* user typed ctl-C */
17117c478bd9Sstevel@tonic-gate 		case ERESTART:
17127c478bd9Sstevel@tonic-gate 			dprintf("Pstopstatus: EINTR\n");
17137c478bd9Sstevel@tonic-gate 			break;
17147c478bd9Sstevel@tonic-gate 		case EAGAIN:		/* we lost control of the the process */
17157c478bd9Sstevel@tonic-gate 		case EOVERFLOW:
17167c478bd9Sstevel@tonic-gate 			dprintf("Pstopstatus: PS_LOST, errno=%d\n", err);
17177c478bd9Sstevel@tonic-gate 			P->state = PS_LOST;
17187c478bd9Sstevel@tonic-gate 			break;
17197c478bd9Sstevel@tonic-gate 		default:		/* check for dead process */
17207c478bd9Sstevel@tonic-gate 			if (_libproc_debug) {
17217c478bd9Sstevel@tonic-gate 				const char *errstr;
17227c478bd9Sstevel@tonic-gate 
17237c478bd9Sstevel@tonic-gate 				switch (request) {
17247c478bd9Sstevel@tonic-gate 				case PCNULL:
17257c478bd9Sstevel@tonic-gate 					errstr = "Pstopstatus PCNULL"; break;
17267c478bd9Sstevel@tonic-gate 				case PCSTOP:
17277c478bd9Sstevel@tonic-gate 					errstr = "Pstopstatus PCSTOP"; break;
17287c478bd9Sstevel@tonic-gate 				case PCDSTOP:
17297c478bd9Sstevel@tonic-gate 					errstr = "Pstopstatus PCDSTOP"; break;
17307c478bd9Sstevel@tonic-gate 				case PCWSTOP:
17317c478bd9Sstevel@tonic-gate 					errstr = "Pstopstatus PCWSTOP"; break;
17327c478bd9Sstevel@tonic-gate 				default:
17337c478bd9Sstevel@tonic-gate 					errstr = "Pstopstatus PC???"; break;
17347c478bd9Sstevel@tonic-gate 				}
17357c478bd9Sstevel@tonic-gate 				dprintf("%s: %s\n", errstr, strerror(err));
17367c478bd9Sstevel@tonic-gate 			}
17377c478bd9Sstevel@tonic-gate 			deadcheck(P);
17387c478bd9Sstevel@tonic-gate 			break;
17397c478bd9Sstevel@tonic-gate 		}
17407c478bd9Sstevel@tonic-gate 		if (err != EINTR && err != ERESTART) {
17417c478bd9Sstevel@tonic-gate 			errno = err;
17427c478bd9Sstevel@tonic-gate 			return (-1);
17437c478bd9Sstevel@tonic-gate 		}
17447c478bd9Sstevel@tonic-gate 	}
17457c478bd9Sstevel@tonic-gate 
17467c478bd9Sstevel@tonic-gate 	if (!(P->status.pr_flags & PR_STOPPED)) {
17477c478bd9Sstevel@tonic-gate 		P->state = PS_RUN;
17487c478bd9Sstevel@tonic-gate 		if (request == PCNULL || request == PCDSTOP || msec != 0)
17497c478bd9Sstevel@tonic-gate 			return (0);
17507c478bd9Sstevel@tonic-gate 		dprintf("Pstopstatus: process is not stopped\n");
17517c478bd9Sstevel@tonic-gate 		errno = EPROTO;
17527c478bd9Sstevel@tonic-gate 		return (-1);
17537c478bd9Sstevel@tonic-gate 	}
17547c478bd9Sstevel@tonic-gate 
17557c478bd9Sstevel@tonic-gate 	P->state = PS_STOP;
17567c478bd9Sstevel@tonic-gate 
17577c478bd9Sstevel@tonic-gate 	if (_libproc_debug)	/* debugging */
17587c478bd9Sstevel@tonic-gate 		prdump(P);
17597c478bd9Sstevel@tonic-gate 
17607c478bd9Sstevel@tonic-gate 	/*
17617c478bd9Sstevel@tonic-gate 	 * If the process was already stopped coming into Pstopstatus(),
17627c478bd9Sstevel@tonic-gate 	 * then don't use its PC to set P->sysaddr since it may have been
17637c478bd9Sstevel@tonic-gate 	 * changed since the time the process originally stopped.
17647c478bd9Sstevel@tonic-gate 	 */
17657c478bd9Sstevel@tonic-gate 	if (old_state == PS_STOP)
17667c478bd9Sstevel@tonic-gate 		return (0);
17677c478bd9Sstevel@tonic-gate 
17687c478bd9Sstevel@tonic-gate 	switch (P->status.pr_lwp.pr_why) {
17697c478bd9Sstevel@tonic-gate 	case PR_SYSENTRY:
17707c478bd9Sstevel@tonic-gate 	case PR_SYSEXIT:
17717c478bd9Sstevel@tonic-gate 		if (Pissyscall_prev(P, P->status.pr_lwp.pr_reg[R_PC],
17727c478bd9Sstevel@tonic-gate 		    &P->sysaddr) == 0)
17737c478bd9Sstevel@tonic-gate 			P->sysaddr = P->status.pr_lwp.pr_reg[R_PC];
17747c478bd9Sstevel@tonic-gate 		break;
17757c478bd9Sstevel@tonic-gate 	case PR_REQUESTED:
17767c478bd9Sstevel@tonic-gate 	case PR_SIGNALLED:
17777c478bd9Sstevel@tonic-gate 	case PR_FAULTED:
17787c478bd9Sstevel@tonic-gate 	case PR_JOBCONTROL:
17797c478bd9Sstevel@tonic-gate 	case PR_SUSPENDED:
17807c478bd9Sstevel@tonic-gate 		break;
17817c478bd9Sstevel@tonic-gate 	default:
17827c478bd9Sstevel@tonic-gate 		errno = EPROTO;
17837c478bd9Sstevel@tonic-gate 		return (-1);
17847c478bd9Sstevel@tonic-gate 	}
17857c478bd9Sstevel@tonic-gate 
17867c478bd9Sstevel@tonic-gate 	return (0);
17877c478bd9Sstevel@tonic-gate }
17887c478bd9Sstevel@tonic-gate 
17897c478bd9Sstevel@tonic-gate /*
17907c478bd9Sstevel@tonic-gate  * Wait for the process to stop for any reason.
17917c478bd9Sstevel@tonic-gate  */
17927c478bd9Sstevel@tonic-gate int
17937c478bd9Sstevel@tonic-gate Pwait(struct ps_prochandle *P, uint_t msec)
17947c478bd9Sstevel@tonic-gate {
17957c478bd9Sstevel@tonic-gate 	return (Pstopstatus(P, PCWSTOP, msec));
17967c478bd9Sstevel@tonic-gate }
17977c478bd9Sstevel@tonic-gate 
17987c478bd9Sstevel@tonic-gate /*
17997c478bd9Sstevel@tonic-gate  * Direct the process to stop; wait for it to stop.
18007c478bd9Sstevel@tonic-gate  */
18017c478bd9Sstevel@tonic-gate int
18027c478bd9Sstevel@tonic-gate Pstop(struct ps_prochandle *P, uint_t msec)
18037c478bd9Sstevel@tonic-gate {
18047c478bd9Sstevel@tonic-gate 	return (Pstopstatus(P, PCSTOP, msec));
18057c478bd9Sstevel@tonic-gate }
18067c478bd9Sstevel@tonic-gate 
18077c478bd9Sstevel@tonic-gate /*
18087c478bd9Sstevel@tonic-gate  * Direct the process to stop; don't wait.
18097c478bd9Sstevel@tonic-gate  */
18107c478bd9Sstevel@tonic-gate int
18117c478bd9Sstevel@tonic-gate Pdstop(struct ps_prochandle *P)
18127c478bd9Sstevel@tonic-gate {
18137c478bd9Sstevel@tonic-gate 	return (Pstopstatus(P, PCDSTOP, 0));
18147c478bd9Sstevel@tonic-gate }
18157c478bd9Sstevel@tonic-gate 
18167c478bd9Sstevel@tonic-gate static void
18177c478bd9Sstevel@tonic-gate deadcheck(struct ps_prochandle *P)
18187c478bd9Sstevel@tonic-gate {
18197c478bd9Sstevel@tonic-gate 	int fd;
18207c478bd9Sstevel@tonic-gate 	void *buf;
18217c478bd9Sstevel@tonic-gate 	size_t size;
18227c478bd9Sstevel@tonic-gate 
18237c478bd9Sstevel@tonic-gate 	if (P->statfd < 0)
18247c478bd9Sstevel@tonic-gate 		P->state = PS_UNDEAD;
18257c478bd9Sstevel@tonic-gate 	else {
18267c478bd9Sstevel@tonic-gate 		if (P->agentstatfd < 0) {
18277c478bd9Sstevel@tonic-gate 			fd = P->statfd;
18287c478bd9Sstevel@tonic-gate 			buf = &P->status;
18297c478bd9Sstevel@tonic-gate 			size = sizeof (P->status);
18307c478bd9Sstevel@tonic-gate 		} else {
18317c478bd9Sstevel@tonic-gate 			fd = P->agentstatfd;
18327c478bd9Sstevel@tonic-gate 			buf = &P->status.pr_lwp;
18337c478bd9Sstevel@tonic-gate 			size = sizeof (P->status.pr_lwp);
18347c478bd9Sstevel@tonic-gate 		}
18357c478bd9Sstevel@tonic-gate 		while (pread(fd, buf, size, (off_t)0) != size) {
18367c478bd9Sstevel@tonic-gate 			switch (errno) {
18377c478bd9Sstevel@tonic-gate 			default:
18387c478bd9Sstevel@tonic-gate 				P->state = PS_UNDEAD;
18397c478bd9Sstevel@tonic-gate 				break;
18407c478bd9Sstevel@tonic-gate 			case EINTR:
18417c478bd9Sstevel@tonic-gate 			case ERESTART:
18427c478bd9Sstevel@tonic-gate 				continue;
18437c478bd9Sstevel@tonic-gate 			case EAGAIN:
18447c478bd9Sstevel@tonic-gate 				P->state = PS_LOST;
18457c478bd9Sstevel@tonic-gate 				break;
18467c478bd9Sstevel@tonic-gate 			}
18477c478bd9Sstevel@tonic-gate 			break;
18487c478bd9Sstevel@tonic-gate 		}
18497c478bd9Sstevel@tonic-gate 		P->status.pr_flags = P->status.pr_lwp.pr_flags;
18507c478bd9Sstevel@tonic-gate 	}
18517c478bd9Sstevel@tonic-gate }
18527c478bd9Sstevel@tonic-gate 
18537c478bd9Sstevel@tonic-gate /*
18547c478bd9Sstevel@tonic-gate  * Get the value of one register from stopped process.
18557c478bd9Sstevel@tonic-gate  */
18567c478bd9Sstevel@tonic-gate int
18577c478bd9Sstevel@tonic-gate Pgetareg(struct ps_prochandle *P, int regno, prgreg_t *preg)
18587c478bd9Sstevel@tonic-gate {
18597c478bd9Sstevel@tonic-gate 	if (regno < 0 || regno >= NPRGREG) {
18607c478bd9Sstevel@tonic-gate 		errno = EINVAL;
18617c478bd9Sstevel@tonic-gate 		return (-1);
18627c478bd9Sstevel@tonic-gate 	}
18637c478bd9Sstevel@tonic-gate 
18647c478bd9Sstevel@tonic-gate 	if (P->state == PS_IDLE) {
18657c478bd9Sstevel@tonic-gate 		errno = ENODATA;
18667c478bd9Sstevel@tonic-gate 		return (-1);
18677c478bd9Sstevel@tonic-gate 	}
18687c478bd9Sstevel@tonic-gate 
18697c478bd9Sstevel@tonic-gate 	if (P->state != PS_STOP && P->state != PS_DEAD) {
18707c478bd9Sstevel@tonic-gate 		errno = EBUSY;
18717c478bd9Sstevel@tonic-gate 		return (-1);
18727c478bd9Sstevel@tonic-gate 	}
18737c478bd9Sstevel@tonic-gate 
18747c478bd9Sstevel@tonic-gate 	*preg = P->status.pr_lwp.pr_reg[regno];
18757c478bd9Sstevel@tonic-gate 	return (0);
18767c478bd9Sstevel@tonic-gate }
18777c478bd9Sstevel@tonic-gate 
18787c478bd9Sstevel@tonic-gate /*
18797c478bd9Sstevel@tonic-gate  * Put value of one register into stopped process.
18807c478bd9Sstevel@tonic-gate  */
18817c478bd9Sstevel@tonic-gate int
18827c478bd9Sstevel@tonic-gate Pputareg(struct ps_prochandle *P, int regno, prgreg_t reg)
18837c478bd9Sstevel@tonic-gate {
18847c478bd9Sstevel@tonic-gate 	if (regno < 0 || regno >= NPRGREG) {
18857c478bd9Sstevel@tonic-gate 		errno = EINVAL;
18867c478bd9Sstevel@tonic-gate 		return (-1);
18877c478bd9Sstevel@tonic-gate 	}
18887c478bd9Sstevel@tonic-gate 
18897c478bd9Sstevel@tonic-gate 	if (P->state != PS_STOP) {
18907c478bd9Sstevel@tonic-gate 		errno = EBUSY;
18917c478bd9Sstevel@tonic-gate 		return (-1);
18927c478bd9Sstevel@tonic-gate 	}
18937c478bd9Sstevel@tonic-gate 
18947c478bd9Sstevel@tonic-gate 	P->status.pr_lwp.pr_reg[regno] = reg;
18957c478bd9Sstevel@tonic-gate 	P->flags |= SETREGS;	/* set registers before continuing */
18967c478bd9Sstevel@tonic-gate 	return (0);
18977c478bd9Sstevel@tonic-gate }
18987c478bd9Sstevel@tonic-gate 
18997c478bd9Sstevel@tonic-gate int
19007c478bd9Sstevel@tonic-gate Psetrun(struct ps_prochandle *P,
19017c478bd9Sstevel@tonic-gate 	int sig,	/* signal to pass to process */
19027c478bd9Sstevel@tonic-gate 	int flags)	/* PRSTEP|PRSABORT|PRSTOP|PRCSIG|PRCFAULT */
19037c478bd9Sstevel@tonic-gate {
19047c478bd9Sstevel@tonic-gate 	int ctlfd = (P->agentctlfd >= 0) ? P->agentctlfd : P->ctlfd;
19057c478bd9Sstevel@tonic-gate 	int sbits = (PR_DSTOP | PR_ISTOP | PR_ASLEEP);
19067c478bd9Sstevel@tonic-gate 
19077c478bd9Sstevel@tonic-gate 	long ctl[1 +					/* PCCFAULT	*/
19087c478bd9Sstevel@tonic-gate 	    1 + sizeof (siginfo_t)/sizeof (long) +	/* PCSSIG/PCCSIG */
19097c478bd9Sstevel@tonic-gate 	    2 ];					/* PCRUN	*/
19107c478bd9Sstevel@tonic-gate 
19117c478bd9Sstevel@tonic-gate 	long *ctlp = ctl;
19127c478bd9Sstevel@tonic-gate 	size_t size;
19137c478bd9Sstevel@tonic-gate 
19147c478bd9Sstevel@tonic-gate 	if (P->state != PS_STOP && (P->status.pr_lwp.pr_flags & sbits) == 0) {
19157c478bd9Sstevel@tonic-gate 		errno = EBUSY;
19167c478bd9Sstevel@tonic-gate 		return (-1);
19177c478bd9Sstevel@tonic-gate 	}
19187c478bd9Sstevel@tonic-gate 
19197c478bd9Sstevel@tonic-gate 	Psync(P);	/* flush tracing flags and registers */
19207c478bd9Sstevel@tonic-gate 
19217c478bd9Sstevel@tonic-gate 	if (flags & PRCFAULT) {		/* clear current fault */
19227c478bd9Sstevel@tonic-gate 		*ctlp++ = PCCFAULT;
19237c478bd9Sstevel@tonic-gate 		flags &= ~PRCFAULT;
19247c478bd9Sstevel@tonic-gate 	}
19257c478bd9Sstevel@tonic-gate 
19267c478bd9Sstevel@tonic-gate 	if (flags & PRCSIG) {		/* clear current signal */
19277c478bd9Sstevel@tonic-gate 		*ctlp++ = PCCSIG;
19287c478bd9Sstevel@tonic-gate 		flags &= ~PRCSIG;
19297c478bd9Sstevel@tonic-gate 	} else if (sig && sig != P->status.pr_lwp.pr_cursig) {
19307c478bd9Sstevel@tonic-gate 		/* make current signal */
19317c478bd9Sstevel@tonic-gate 		siginfo_t *infop;
19327c478bd9Sstevel@tonic-gate 
19337c478bd9Sstevel@tonic-gate 		*ctlp++ = PCSSIG;
19347c478bd9Sstevel@tonic-gate 		infop = (siginfo_t *)ctlp;
19357c478bd9Sstevel@tonic-gate 		(void) memset(infop, 0, sizeof (*infop));
19367c478bd9Sstevel@tonic-gate 		infop->si_signo = sig;
19377c478bd9Sstevel@tonic-gate 		ctlp += sizeof (siginfo_t) / sizeof (long);
19387c478bd9Sstevel@tonic-gate 	}
19397c478bd9Sstevel@tonic-gate 
19407c478bd9Sstevel@tonic-gate 	*ctlp++ = PCRUN;
19417c478bd9Sstevel@tonic-gate 	*ctlp++ = flags;
19427c478bd9Sstevel@tonic-gate 	size = (char *)ctlp - (char *)ctl;
19437c478bd9Sstevel@tonic-gate 
19447c478bd9Sstevel@tonic-gate 	P->info_valid = 0;	/* will need to update map and file info */
19457c478bd9Sstevel@tonic-gate 
19467c478bd9Sstevel@tonic-gate 	/*
19477c478bd9Sstevel@tonic-gate 	 * If we've cached ucontext-list information while we were stopped,
19487c478bd9Sstevel@tonic-gate 	 * free it now.
19497c478bd9Sstevel@tonic-gate 	 */
19507c478bd9Sstevel@tonic-gate 	if (P->ucaddrs != NULL) {
19517c478bd9Sstevel@tonic-gate 		free(P->ucaddrs);
19527c478bd9Sstevel@tonic-gate 		P->ucaddrs = NULL;
19537c478bd9Sstevel@tonic-gate 		P->ucnelems = 0;
19547c478bd9Sstevel@tonic-gate 	}
19557c478bd9Sstevel@tonic-gate 
19567c478bd9Sstevel@tonic-gate 	if (write(ctlfd, ctl, size) != size) {
19577c478bd9Sstevel@tonic-gate 		/* If it is dead or lost, return the real status, not PS_RUN */
19587c478bd9Sstevel@tonic-gate 		if (errno == ENOENT || errno == EAGAIN) {
19597c478bd9Sstevel@tonic-gate 			(void) Pstopstatus(P, PCNULL, 0);
19607c478bd9Sstevel@tonic-gate 			return (0);
19617c478bd9Sstevel@tonic-gate 		}
19627c478bd9Sstevel@tonic-gate 		/* If it is not in a jobcontrol stop, issue an error message */
19637c478bd9Sstevel@tonic-gate 		if (errno != EBUSY ||
19647c478bd9Sstevel@tonic-gate 		    P->status.pr_lwp.pr_why != PR_JOBCONTROL) {
19657c478bd9Sstevel@tonic-gate 			dprintf("Psetrun: %s\n", strerror(errno));
19667c478bd9Sstevel@tonic-gate 			return (-1);
19677c478bd9Sstevel@tonic-gate 		}
19687c478bd9Sstevel@tonic-gate 		/* Otherwise pretend that the job-stopped process is running */
19697c478bd9Sstevel@tonic-gate 	}
19707c478bd9Sstevel@tonic-gate 
19717c478bd9Sstevel@tonic-gate 	P->state = PS_RUN;
19727c478bd9Sstevel@tonic-gate 	return (0);
19737c478bd9Sstevel@tonic-gate }
19747c478bd9Sstevel@tonic-gate 
19757c478bd9Sstevel@tonic-gate ssize_t
19767c478bd9Sstevel@tonic-gate Pread(struct ps_prochandle *P,
19777c478bd9Sstevel@tonic-gate 	void *buf,		/* caller's buffer */
19787c478bd9Sstevel@tonic-gate 	size_t nbyte,		/* number of bytes to read */
19797c478bd9Sstevel@tonic-gate 	uintptr_t address)	/* address in process */
19807c478bd9Sstevel@tonic-gate {
19817c478bd9Sstevel@tonic-gate 	return (P->ops->p_pread(P, buf, nbyte, address));
19827c478bd9Sstevel@tonic-gate }
19837c478bd9Sstevel@tonic-gate 
19847c478bd9Sstevel@tonic-gate ssize_t
19857c478bd9Sstevel@tonic-gate Pread_string(struct ps_prochandle *P,
19867c478bd9Sstevel@tonic-gate 	char *buf, 		/* caller's buffer */
19877c478bd9Sstevel@tonic-gate 	size_t size,		/* upper limit on bytes to read */
19887c478bd9Sstevel@tonic-gate 	uintptr_t addr)		/* address in process */
19897c478bd9Sstevel@tonic-gate {
19907c478bd9Sstevel@tonic-gate 	enum { STRSZ = 40 };
19917c478bd9Sstevel@tonic-gate 	char string[STRSZ + 1];
19927c478bd9Sstevel@tonic-gate 	ssize_t leng = 0;
19937c478bd9Sstevel@tonic-gate 	int nbyte;
19947c478bd9Sstevel@tonic-gate 
19957c478bd9Sstevel@tonic-gate 	if (size < 2) {
19967c478bd9Sstevel@tonic-gate 		errno = EINVAL;
19977c478bd9Sstevel@tonic-gate 		return (-1);
19987c478bd9Sstevel@tonic-gate 	}
19997c478bd9Sstevel@tonic-gate 
20007c478bd9Sstevel@tonic-gate 	size--;			/* ensure trailing null fits in buffer */
20017c478bd9Sstevel@tonic-gate 
20027c478bd9Sstevel@tonic-gate 	*buf = '\0';
20037c478bd9Sstevel@tonic-gate 	string[STRSZ] = '\0';
20047c478bd9Sstevel@tonic-gate 
20057c478bd9Sstevel@tonic-gate 	for (nbyte = STRSZ; nbyte == STRSZ && leng < size; addr += STRSZ) {
20067c478bd9Sstevel@tonic-gate 		if ((nbyte = P->ops->p_pread(P, string, STRSZ, addr)) <= 0) {
20077c478bd9Sstevel@tonic-gate 			buf[leng] = '\0';
20087c478bd9Sstevel@tonic-gate 			return (leng ? leng : -1);
20097c478bd9Sstevel@tonic-gate 		}
20107c478bd9Sstevel@tonic-gate 		if ((nbyte = strlen(string)) > 0) {
20117c478bd9Sstevel@tonic-gate 			if (leng + nbyte > size)
20127c478bd9Sstevel@tonic-gate 				nbyte = size - leng;
20137c478bd9Sstevel@tonic-gate 			(void) strncpy(buf + leng, string, nbyte);
20147c478bd9Sstevel@tonic-gate 			leng += nbyte;
20157c478bd9Sstevel@tonic-gate 		}
20167c478bd9Sstevel@tonic-gate 	}
20177c478bd9Sstevel@tonic-gate 	buf[leng] = '\0';
20187c478bd9Sstevel@tonic-gate 	return (leng);
20197c478bd9Sstevel@tonic-gate }
20207c478bd9Sstevel@tonic-gate 
20217c478bd9Sstevel@tonic-gate ssize_t
20227c478bd9Sstevel@tonic-gate Pwrite(struct ps_prochandle *P,
20237c478bd9Sstevel@tonic-gate 	const void *buf,	/* caller's buffer */
20247c478bd9Sstevel@tonic-gate 	size_t nbyte,		/* number of bytes to write */
20257c478bd9Sstevel@tonic-gate 	uintptr_t address)	/* address in process */
20267c478bd9Sstevel@tonic-gate {
20277c478bd9Sstevel@tonic-gate 	return (P->ops->p_pwrite(P, buf, nbyte, address));
20287c478bd9Sstevel@tonic-gate }
20297c478bd9Sstevel@tonic-gate 
20307c478bd9Sstevel@tonic-gate int
20317c478bd9Sstevel@tonic-gate Pclearsig(struct ps_prochandle *P)
20327c478bd9Sstevel@tonic-gate {
20337c478bd9Sstevel@tonic-gate 	int ctlfd = (P->agentctlfd >= 0)? P->agentctlfd : P->ctlfd;
20347c478bd9Sstevel@tonic-gate 	long ctl = PCCSIG;
20357c478bd9Sstevel@tonic-gate 
20367c478bd9Sstevel@tonic-gate 	if (write(ctlfd, &ctl, sizeof (ctl)) != sizeof (ctl))
20377c478bd9Sstevel@tonic-gate 		return (-1);
20387c478bd9Sstevel@tonic-gate 	P->status.pr_lwp.pr_cursig = 0;
20397c478bd9Sstevel@tonic-gate 	return (0);
20407c478bd9Sstevel@tonic-gate }
20417c478bd9Sstevel@tonic-gate 
20427c478bd9Sstevel@tonic-gate int
20437c478bd9Sstevel@tonic-gate Pclearfault(struct ps_prochandle *P)
20447c478bd9Sstevel@tonic-gate {
20457c478bd9Sstevel@tonic-gate 	int ctlfd = (P->agentctlfd >= 0)? P->agentctlfd : P->ctlfd;
20467c478bd9Sstevel@tonic-gate 	long ctl = PCCFAULT;
20477c478bd9Sstevel@tonic-gate 
20487c478bd9Sstevel@tonic-gate 	if (write(ctlfd, &ctl, sizeof (ctl)) != sizeof (ctl))
20497c478bd9Sstevel@tonic-gate 		return (-1);
20507c478bd9Sstevel@tonic-gate 	return (0);
20517c478bd9Sstevel@tonic-gate }
20527c478bd9Sstevel@tonic-gate 
20537c478bd9Sstevel@tonic-gate /*
20547c478bd9Sstevel@tonic-gate  * Set a breakpoint trap, return original instruction.
20557c478bd9Sstevel@tonic-gate  */
20567c478bd9Sstevel@tonic-gate int
20577c478bd9Sstevel@tonic-gate Psetbkpt(struct ps_prochandle *P, uintptr_t address, ulong_t *saved)
20587c478bd9Sstevel@tonic-gate {
20597c478bd9Sstevel@tonic-gate 	long ctl[1 + sizeof (priovec_t) / sizeof (long) +	/* PCREAD */
20607c478bd9Sstevel@tonic-gate 	    1 + sizeof (priovec_t) / sizeof (long)];	/* PCWRITE */
20617c478bd9Sstevel@tonic-gate 	long *ctlp = ctl;
20627c478bd9Sstevel@tonic-gate 	size_t size;
20637c478bd9Sstevel@tonic-gate 	priovec_t *iovp;
20647c478bd9Sstevel@tonic-gate 	instr_t bpt = BPT;
20657c478bd9Sstevel@tonic-gate 	instr_t old;
20667c478bd9Sstevel@tonic-gate 
20677c478bd9Sstevel@tonic-gate 	if (P->state == PS_DEAD || P->state == PS_UNDEAD ||
20687c478bd9Sstevel@tonic-gate 	    P->state == PS_IDLE) {
20697c478bd9Sstevel@tonic-gate 		errno = ENOENT;
20707c478bd9Sstevel@tonic-gate 		return (-1);
20717c478bd9Sstevel@tonic-gate 	}
20727c478bd9Sstevel@tonic-gate 
20737c478bd9Sstevel@tonic-gate 	/* fetch the old instruction */
20747c478bd9Sstevel@tonic-gate 	*ctlp++ = PCREAD;
20757c478bd9Sstevel@tonic-gate 	iovp = (priovec_t *)ctlp;
20767c478bd9Sstevel@tonic-gate 	iovp->pio_base = &old;
20777c478bd9Sstevel@tonic-gate 	iovp->pio_len = sizeof (old);
20787c478bd9Sstevel@tonic-gate 	iovp->pio_offset = address;
20797c478bd9Sstevel@tonic-gate 	ctlp += sizeof (priovec_t) / sizeof (long);
20807c478bd9Sstevel@tonic-gate 
20817c478bd9Sstevel@tonic-gate 	/* write the BPT instruction */
20827c478bd9Sstevel@tonic-gate 	*ctlp++ = PCWRITE;
20837c478bd9Sstevel@tonic-gate 	iovp = (priovec_t *)ctlp;
20847c478bd9Sstevel@tonic-gate 	iovp->pio_base = &bpt;
20857c478bd9Sstevel@tonic-gate 	iovp->pio_len = sizeof (bpt);
20867c478bd9Sstevel@tonic-gate 	iovp->pio_offset = address;
20877c478bd9Sstevel@tonic-gate 	ctlp += sizeof (priovec_t) / sizeof (long);
20887c478bd9Sstevel@tonic-gate 
20897c478bd9Sstevel@tonic-gate 	size = (char *)ctlp - (char *)ctl;
20907c478bd9Sstevel@tonic-gate 	if (write(P->ctlfd, ctl, size) != size)
20917c478bd9Sstevel@tonic-gate 		return (-1);
20927c478bd9Sstevel@tonic-gate 
20937c478bd9Sstevel@tonic-gate 	/*
20947c478bd9Sstevel@tonic-gate 	 * Fail if there was already a breakpoint there from another debugger
20957c478bd9Sstevel@tonic-gate 	 * or DTrace's user-level tracing on x86.
20967c478bd9Sstevel@tonic-gate 	 */
2097e4586ebfSmws 	if (old == BPT) {
2098e4586ebfSmws 		errno = EBUSY;
2099e4586ebfSmws 		return (-1);
2100e4586ebfSmws 	}
21017c478bd9Sstevel@tonic-gate 
21027c478bd9Sstevel@tonic-gate 	*saved = (ulong_t)old;
21037c478bd9Sstevel@tonic-gate 	return (0);
21047c478bd9Sstevel@tonic-gate }
21057c478bd9Sstevel@tonic-gate 
21067c478bd9Sstevel@tonic-gate /*
21077c478bd9Sstevel@tonic-gate  * Restore original instruction where a breakpoint was set.
21087c478bd9Sstevel@tonic-gate  */
21097c478bd9Sstevel@tonic-gate int
21107c478bd9Sstevel@tonic-gate Pdelbkpt(struct ps_prochandle *P, uintptr_t address, ulong_t saved)
21117c478bd9Sstevel@tonic-gate {
21127c478bd9Sstevel@tonic-gate 	instr_t old = (instr_t)saved;
21137c478bd9Sstevel@tonic-gate 	instr_t cur;
21147c478bd9Sstevel@tonic-gate 
21157c478bd9Sstevel@tonic-gate 	if (P->state == PS_DEAD || P->state == PS_UNDEAD ||
21167c478bd9Sstevel@tonic-gate 	    P->state == PS_IDLE) {
21177c478bd9Sstevel@tonic-gate 		errno = ENOENT;
21187c478bd9Sstevel@tonic-gate 		return (-1);
21197c478bd9Sstevel@tonic-gate 	}
21207c478bd9Sstevel@tonic-gate 
21217c478bd9Sstevel@tonic-gate 	/*
21227c478bd9Sstevel@tonic-gate 	 * If the breakpoint instruction we had placed has been overwritten
21237c478bd9Sstevel@tonic-gate 	 * with a new instruction, then don't try to replace it with the
21247c478bd9Sstevel@tonic-gate 	 * old instruction. Doing do can cause problems with self-modifying
21257c478bd9Sstevel@tonic-gate 	 * code -- PLTs for example. If the Pread() fails, we assume that we
21267c478bd9Sstevel@tonic-gate 	 * should proceed though most likely the Pwrite() will also fail.
21277c478bd9Sstevel@tonic-gate 	 */
21287c478bd9Sstevel@tonic-gate 	if (Pread(P, &cur, sizeof (cur), address) == sizeof (cur) &&
21297c478bd9Sstevel@tonic-gate 	    cur != BPT)
21307c478bd9Sstevel@tonic-gate 		return (0);
21317c478bd9Sstevel@tonic-gate 
21327c478bd9Sstevel@tonic-gate 	if (Pwrite(P, &old, sizeof (old), address) != sizeof (old))
21337c478bd9Sstevel@tonic-gate 		return (-1);
21347c478bd9Sstevel@tonic-gate 
21357c478bd9Sstevel@tonic-gate 	return (0);
21367c478bd9Sstevel@tonic-gate }
21377c478bd9Sstevel@tonic-gate 
21387c478bd9Sstevel@tonic-gate /*
21397c478bd9Sstevel@tonic-gate  * Common code for Pxecbkpt() and Lxecbkpt().
21407c478bd9Sstevel@tonic-gate  * Develop the array of requests that will do the job, then
21417c478bd9Sstevel@tonic-gate  * write them to the specified control file descriptor.
21427c478bd9Sstevel@tonic-gate  * Return the non-zero errno if the write fails.
21437c478bd9Sstevel@tonic-gate  */
21447c478bd9Sstevel@tonic-gate static int
21457c478bd9Sstevel@tonic-gate execute_bkpt(
21467c478bd9Sstevel@tonic-gate 	int ctlfd,		/* process or LWP control file descriptor */
21477c478bd9Sstevel@tonic-gate 	const fltset_t *faultset,	/* current set of traced faults */
21487c478bd9Sstevel@tonic-gate 	const sigset_t *sigmask,	/* current signal mask */
21497c478bd9Sstevel@tonic-gate 	uintptr_t address,		/* address of breakpint */
21507c478bd9Sstevel@tonic-gate 	ulong_t saved)			/* the saved instruction */
21517c478bd9Sstevel@tonic-gate {
21527c478bd9Sstevel@tonic-gate 	long ctl[
21537c478bd9Sstevel@tonic-gate 	    1 + sizeof (sigset_t) / sizeof (long) +		/* PCSHOLD */
21547c478bd9Sstevel@tonic-gate 	    1 + sizeof (fltset_t) / sizeof (long) +		/* PCSFAULT */
21557c478bd9Sstevel@tonic-gate 	    1 + sizeof (priovec_t) / sizeof (long) +		/* PCWRITE */
21567c478bd9Sstevel@tonic-gate 	    2 +							/* PCRUN */
21577c478bd9Sstevel@tonic-gate 	    1 +							/* PCWSTOP */
21587c478bd9Sstevel@tonic-gate 	    1 +							/* PCCFAULT */
21597c478bd9Sstevel@tonic-gate 	    1 + sizeof (priovec_t) / sizeof (long) +		/* PCWRITE */
21607c478bd9Sstevel@tonic-gate 	    1 + sizeof (fltset_t) / sizeof (long) +		/* PCSFAULT */
21617c478bd9Sstevel@tonic-gate 	    1 + sizeof (sigset_t) / sizeof (long)];		/* PCSHOLD */
21627c478bd9Sstevel@tonic-gate 	long *ctlp = ctl;
21637c478bd9Sstevel@tonic-gate 	sigset_t unblock;
21647c478bd9Sstevel@tonic-gate 	size_t size;
21657c478bd9Sstevel@tonic-gate 	ssize_t ssize;
21667c478bd9Sstevel@tonic-gate 	priovec_t *iovp;
21677c478bd9Sstevel@tonic-gate 	sigset_t *holdp;
21687c478bd9Sstevel@tonic-gate 	fltset_t *faultp;
21697c478bd9Sstevel@tonic-gate 	instr_t old = (instr_t)saved;
21707c478bd9Sstevel@tonic-gate 	instr_t bpt = BPT;
21717c478bd9Sstevel@tonic-gate 	int error = 0;
21727c478bd9Sstevel@tonic-gate 
21737c478bd9Sstevel@tonic-gate 	/* block our signals for the duration */
21747c478bd9Sstevel@tonic-gate 	(void) sigprocmask(SIG_BLOCK, &blockable_sigs, &unblock);
21757c478bd9Sstevel@tonic-gate 
21767c478bd9Sstevel@tonic-gate 	/* hold posted signals */
21777c478bd9Sstevel@tonic-gate 	*ctlp++ = PCSHOLD;
21787c478bd9Sstevel@tonic-gate 	holdp = (sigset_t *)ctlp;
21797c478bd9Sstevel@tonic-gate 	prfillset(holdp);
21807c478bd9Sstevel@tonic-gate 	prdelset(holdp, SIGKILL);
21817c478bd9Sstevel@tonic-gate 	prdelset(holdp, SIGSTOP);
21827c478bd9Sstevel@tonic-gate 	ctlp += sizeof (sigset_t) / sizeof (long);
21837c478bd9Sstevel@tonic-gate 
21847c478bd9Sstevel@tonic-gate 	/* force tracing of FLTTRACE */
21857c478bd9Sstevel@tonic-gate 	if (!(prismember(faultset, FLTTRACE))) {
21867c478bd9Sstevel@tonic-gate 		*ctlp++ = PCSFAULT;
21877c478bd9Sstevel@tonic-gate 		faultp = (fltset_t *)ctlp;
21887c478bd9Sstevel@tonic-gate 		*faultp = *faultset;
21897c478bd9Sstevel@tonic-gate 		praddset(faultp, FLTTRACE);
21907c478bd9Sstevel@tonic-gate 		ctlp += sizeof (fltset_t) / sizeof (long);
21917c478bd9Sstevel@tonic-gate 	}
21927c478bd9Sstevel@tonic-gate 
21937c478bd9Sstevel@tonic-gate 	/* restore the old instruction */
21947c478bd9Sstevel@tonic-gate 	*ctlp++ = PCWRITE;
21957c478bd9Sstevel@tonic-gate 	iovp = (priovec_t *)ctlp;
21967c478bd9Sstevel@tonic-gate 	iovp->pio_base = &old;
21977c478bd9Sstevel@tonic-gate 	iovp->pio_len = sizeof (old);
21987c478bd9Sstevel@tonic-gate 	iovp->pio_offset = address;
21997c478bd9Sstevel@tonic-gate 	ctlp += sizeof (priovec_t) / sizeof (long);
22007c478bd9Sstevel@tonic-gate 
22017c478bd9Sstevel@tonic-gate 	/* clear current signal and fault; set running w/ single-step */
22027c478bd9Sstevel@tonic-gate 	*ctlp++ = PCRUN;
22037c478bd9Sstevel@tonic-gate 	*ctlp++ = PRCSIG | PRCFAULT | PRSTEP;
22047c478bd9Sstevel@tonic-gate 
22057c478bd9Sstevel@tonic-gate 	/* wait for stop, cancel the fault */
22067c478bd9Sstevel@tonic-gate 	*ctlp++ = PCWSTOP;
22077c478bd9Sstevel@tonic-gate 	*ctlp++ = PCCFAULT;
22087c478bd9Sstevel@tonic-gate 
22097c478bd9Sstevel@tonic-gate 	/* restore the breakpoint trap */
22107c478bd9Sstevel@tonic-gate 	*ctlp++ = PCWRITE;
22117c478bd9Sstevel@tonic-gate 	iovp = (priovec_t *)ctlp;
22127c478bd9Sstevel@tonic-gate 	iovp->pio_base = &bpt;
22137c478bd9Sstevel@tonic-gate 	iovp->pio_len = sizeof (bpt);
22147c478bd9Sstevel@tonic-gate 	iovp->pio_offset = address;
22157c478bd9Sstevel@tonic-gate 	ctlp += sizeof (priovec_t) / sizeof (long);
22167c478bd9Sstevel@tonic-gate 
22177c478bd9Sstevel@tonic-gate 	/* restore fault tracing set */
22187c478bd9Sstevel@tonic-gate 	if (!(prismember(faultset, FLTTRACE))) {
22197c478bd9Sstevel@tonic-gate 		*ctlp++ = PCSFAULT;
22207c478bd9Sstevel@tonic-gate 		*(fltset_t *)ctlp = *faultset;
22217c478bd9Sstevel@tonic-gate 		ctlp += sizeof (fltset_t) / sizeof (long);
22227c478bd9Sstevel@tonic-gate 	}
22237c478bd9Sstevel@tonic-gate 
22247c478bd9Sstevel@tonic-gate 	/* restore the hold mask */
22257c478bd9Sstevel@tonic-gate 	*ctlp++ = PCSHOLD;
22267c478bd9Sstevel@tonic-gate 	*(sigset_t *)ctlp = *sigmask;
22277c478bd9Sstevel@tonic-gate 	ctlp += sizeof (sigset_t) / sizeof (long);
22287c478bd9Sstevel@tonic-gate 
22297c478bd9Sstevel@tonic-gate 	size = (char *)ctlp - (char *)ctl;
22307c478bd9Sstevel@tonic-gate 	if ((ssize = write(ctlfd, ctl, size)) != size)
22317c478bd9Sstevel@tonic-gate 		error = (ssize == -1)? errno : EINTR;
22327c478bd9Sstevel@tonic-gate 	(void) sigprocmask(SIG_SETMASK, &unblock, NULL);
22337c478bd9Sstevel@tonic-gate 	return (error);
22347c478bd9Sstevel@tonic-gate }
22357c478bd9Sstevel@tonic-gate 
22367c478bd9Sstevel@tonic-gate /*
22377c478bd9Sstevel@tonic-gate  * Step over a breakpoint, i.e., execute the instruction that
22387c478bd9Sstevel@tonic-gate  * really belongs at the breakpoint location (the current %pc)
22397c478bd9Sstevel@tonic-gate  * and leave the process stopped at the next instruction.
22407c478bd9Sstevel@tonic-gate  */
22417c478bd9Sstevel@tonic-gate int
22427c478bd9Sstevel@tonic-gate Pxecbkpt(struct ps_prochandle *P, ulong_t saved)
22437c478bd9Sstevel@tonic-gate {
22447c478bd9Sstevel@tonic-gate 	int ctlfd = (P->agentctlfd >= 0)? P->agentctlfd : P->ctlfd;
22457c478bd9Sstevel@tonic-gate 	int rv, error;
22467c478bd9Sstevel@tonic-gate 
22477c478bd9Sstevel@tonic-gate 	if (P->state != PS_STOP) {
22487c478bd9Sstevel@tonic-gate 		errno = EBUSY;
22497c478bd9Sstevel@tonic-gate 		return (-1);
22507c478bd9Sstevel@tonic-gate 	}
22517c478bd9Sstevel@tonic-gate 
22527c478bd9Sstevel@tonic-gate 	Psync(P);
22537c478bd9Sstevel@tonic-gate 
22547c478bd9Sstevel@tonic-gate 	error = execute_bkpt(ctlfd,
22557c478bd9Sstevel@tonic-gate 	    &P->status.pr_flttrace, &P->status.pr_lwp.pr_lwphold,
22567c478bd9Sstevel@tonic-gate 	    P->status.pr_lwp.pr_reg[R_PC], saved);
22577c478bd9Sstevel@tonic-gate 	rv = Pstopstatus(P, PCNULL, 0);
22587c478bd9Sstevel@tonic-gate 
22597c478bd9Sstevel@tonic-gate 	if (error != 0) {
22607c478bd9Sstevel@tonic-gate 		if (P->status.pr_lwp.pr_why == PR_JOBCONTROL &&
22617c478bd9Sstevel@tonic-gate 		    error == EBUSY) {	/* jobcontrol stop -- back off */
22627c478bd9Sstevel@tonic-gate 			P->state = PS_RUN;
22637c478bd9Sstevel@tonic-gate 			return (0);
22647c478bd9Sstevel@tonic-gate 		}
22657c478bd9Sstevel@tonic-gate 		if (error == ENOENT)
22667c478bd9Sstevel@tonic-gate 			return (0);
22677c478bd9Sstevel@tonic-gate 		errno = error;
22687c478bd9Sstevel@tonic-gate 		return (-1);
22697c478bd9Sstevel@tonic-gate 	}
22707c478bd9Sstevel@tonic-gate 
22717c478bd9Sstevel@tonic-gate 	return (rv);
22727c478bd9Sstevel@tonic-gate }
22737c478bd9Sstevel@tonic-gate 
22747c478bd9Sstevel@tonic-gate /*
22757c478bd9Sstevel@tonic-gate  * Install the watchpoint described by wp.
22767c478bd9Sstevel@tonic-gate  */
22777c478bd9Sstevel@tonic-gate int
22787c478bd9Sstevel@tonic-gate Psetwapt(struct ps_prochandle *P, const prwatch_t *wp)
22797c478bd9Sstevel@tonic-gate {
22807c478bd9Sstevel@tonic-gate 	long ctl[1 + sizeof (prwatch_t) / sizeof (long)];
22817c478bd9Sstevel@tonic-gate 	prwatch_t *cwp = (prwatch_t *)&ctl[1];
22827c478bd9Sstevel@tonic-gate 
22837c478bd9Sstevel@tonic-gate 	if (P->state == PS_DEAD || P->state == PS_UNDEAD ||
22847c478bd9Sstevel@tonic-gate 	    P->state == PS_IDLE) {
22857c478bd9Sstevel@tonic-gate 		errno = ENOENT;
22867c478bd9Sstevel@tonic-gate 		return (-1);
22877c478bd9Sstevel@tonic-gate 	}
22887c478bd9Sstevel@tonic-gate 
22897c478bd9Sstevel@tonic-gate 	ctl[0] = PCWATCH;
22907c478bd9Sstevel@tonic-gate 	cwp->pr_vaddr = wp->pr_vaddr;
22917c478bd9Sstevel@tonic-gate 	cwp->pr_size = wp->pr_size;
22927c478bd9Sstevel@tonic-gate 	cwp->pr_wflags = wp->pr_wflags;
22937c478bd9Sstevel@tonic-gate 
22947c478bd9Sstevel@tonic-gate 	if (write(P->ctlfd, ctl, sizeof (ctl)) != sizeof (ctl))
22957c478bd9Sstevel@tonic-gate 		return (-1);
22967c478bd9Sstevel@tonic-gate 
22977c478bd9Sstevel@tonic-gate 	return (0);
22987c478bd9Sstevel@tonic-gate }
22997c478bd9Sstevel@tonic-gate 
23007c478bd9Sstevel@tonic-gate /*
23017c478bd9Sstevel@tonic-gate  * Remove the watchpoint described by wp.
23027c478bd9Sstevel@tonic-gate  */
23037c478bd9Sstevel@tonic-gate int
23047c478bd9Sstevel@tonic-gate Pdelwapt(struct ps_prochandle *P, const prwatch_t *wp)
23057c478bd9Sstevel@tonic-gate {
23067c478bd9Sstevel@tonic-gate 	long ctl[1 + sizeof (prwatch_t) / sizeof (long)];
23077c478bd9Sstevel@tonic-gate 	prwatch_t *cwp = (prwatch_t *)&ctl[1];
23087c478bd9Sstevel@tonic-gate 
23097c478bd9Sstevel@tonic-gate 	if (P->state == PS_DEAD || P->state == PS_UNDEAD ||
23107c478bd9Sstevel@tonic-gate 	    P->state == PS_IDLE) {
23117c478bd9Sstevel@tonic-gate 		errno = ENOENT;
23127c478bd9Sstevel@tonic-gate 		return (-1);
23137c478bd9Sstevel@tonic-gate 	}
23147c478bd9Sstevel@tonic-gate 
23157c478bd9Sstevel@tonic-gate 	ctl[0] = PCWATCH;
23167c478bd9Sstevel@tonic-gate 	cwp->pr_vaddr = wp->pr_vaddr;
23177c478bd9Sstevel@tonic-gate 	cwp->pr_size = wp->pr_size;
23187c478bd9Sstevel@tonic-gate 	cwp->pr_wflags = 0;
23197c478bd9Sstevel@tonic-gate 
23207c478bd9Sstevel@tonic-gate 	if (write(P->ctlfd, ctl, sizeof (ctl)) != sizeof (ctl))
23217c478bd9Sstevel@tonic-gate 		return (-1);
23227c478bd9Sstevel@tonic-gate 
23237c478bd9Sstevel@tonic-gate 	return (0);
23247c478bd9Sstevel@tonic-gate }
23257c478bd9Sstevel@tonic-gate 
23267c478bd9Sstevel@tonic-gate /*
23277c478bd9Sstevel@tonic-gate  * Common code for Pxecwapt() and Lxecwapt().  Develop the array of requests
23287c478bd9Sstevel@tonic-gate  * that will do the job, then write them to the specified control file
23297c478bd9Sstevel@tonic-gate  * descriptor.  Return the non-zero errno if the write fails.
23307c478bd9Sstevel@tonic-gate  */
23317c478bd9Sstevel@tonic-gate static int
23327c478bd9Sstevel@tonic-gate execute_wapt(
23337c478bd9Sstevel@tonic-gate 	int ctlfd,		/* process or LWP control file descriptor */
23347c478bd9Sstevel@tonic-gate 	const fltset_t *faultset,	/* current set of traced faults */
23357c478bd9Sstevel@tonic-gate 	const sigset_t *sigmask,	/* current signal mask */
23367c478bd9Sstevel@tonic-gate 	const prwatch_t *wp)		/* watchpoint descriptor */
23377c478bd9Sstevel@tonic-gate {
23387c478bd9Sstevel@tonic-gate 	long ctl[
23397c478bd9Sstevel@tonic-gate 	    1 + sizeof (sigset_t) / sizeof (long) +		/* PCSHOLD */
23407c478bd9Sstevel@tonic-gate 	    1 + sizeof (fltset_t) / sizeof (long) +		/* PCSFAULT */
23417c478bd9Sstevel@tonic-gate 	    1 + sizeof (prwatch_t) / sizeof (long) +		/* PCWATCH */
23427c478bd9Sstevel@tonic-gate 	    2 +							/* PCRUN */
23437c478bd9Sstevel@tonic-gate 	    1 +							/* PCWSTOP */
23447c478bd9Sstevel@tonic-gate 	    1 +							/* PCCFAULT */
23457c478bd9Sstevel@tonic-gate 	    1 + sizeof (prwatch_t) / sizeof (long) +		/* PCWATCH */
23467c478bd9Sstevel@tonic-gate 	    1 + sizeof (fltset_t) / sizeof (long) +		/* PCSFAULT */
23477c478bd9Sstevel@tonic-gate 	    1 + sizeof (sigset_t) / sizeof (long)];		/* PCSHOLD */
23487c478bd9Sstevel@tonic-gate 
23497c478bd9Sstevel@tonic-gate 	long *ctlp = ctl;
23507c478bd9Sstevel@tonic-gate 	int error = 0;
23517c478bd9Sstevel@tonic-gate 
23527c478bd9Sstevel@tonic-gate 	sigset_t unblock;
23537c478bd9Sstevel@tonic-gate 	sigset_t *holdp;
23547c478bd9Sstevel@tonic-gate 	fltset_t *faultp;
23557c478bd9Sstevel@tonic-gate 	prwatch_t *prw;
23567c478bd9Sstevel@tonic-gate 	ssize_t ssize;
23577c478bd9Sstevel@tonic-gate 	size_t size;
23587c478bd9Sstevel@tonic-gate 
23597c478bd9Sstevel@tonic-gate 	(void) sigprocmask(SIG_BLOCK, &blockable_sigs, &unblock);
23607c478bd9Sstevel@tonic-gate 
23617c478bd9Sstevel@tonic-gate 	/*
23627c478bd9Sstevel@tonic-gate 	 * Hold all posted signals in the victim process prior to stepping.
23637c478bd9Sstevel@tonic-gate 	 */
23647c478bd9Sstevel@tonic-gate 	*ctlp++ = PCSHOLD;
23657c478bd9Sstevel@tonic-gate 	holdp = (sigset_t *)ctlp;
23667c478bd9Sstevel@tonic-gate 	prfillset(holdp);
23677c478bd9Sstevel@tonic-gate 	prdelset(holdp, SIGKILL);
23687c478bd9Sstevel@tonic-gate 	prdelset(holdp, SIGSTOP);
23697c478bd9Sstevel@tonic-gate 	ctlp += sizeof (sigset_t) / sizeof (long);
23707c478bd9Sstevel@tonic-gate 
23717c478bd9Sstevel@tonic-gate 	/*
23727c478bd9Sstevel@tonic-gate 	 * Force tracing of FLTTRACE since we need to single step.
23737c478bd9Sstevel@tonic-gate 	 */
23747c478bd9Sstevel@tonic-gate 	if (!(prismember(faultset, FLTTRACE))) {
23757c478bd9Sstevel@tonic-gate 		*ctlp++ = PCSFAULT;
23767c478bd9Sstevel@tonic-gate 		faultp = (fltset_t *)ctlp;
23777c478bd9Sstevel@tonic-gate 		*faultp = *faultset;
23787c478bd9Sstevel@tonic-gate 		praddset(faultp, FLTTRACE);
23797c478bd9Sstevel@tonic-gate 		ctlp += sizeof (fltset_t) / sizeof (long);
23807c478bd9Sstevel@tonic-gate 	}
23817c478bd9Sstevel@tonic-gate 
23827c478bd9Sstevel@tonic-gate 	/*
23837c478bd9Sstevel@tonic-gate 	 * Clear only the current watchpoint by setting pr_wflags to zero.
23847c478bd9Sstevel@tonic-gate 	 */
23857c478bd9Sstevel@tonic-gate 	*ctlp++ = PCWATCH;
23867c478bd9Sstevel@tonic-gate 	prw = (prwatch_t *)ctlp;
23877c478bd9Sstevel@tonic-gate 	prw->pr_vaddr = wp->pr_vaddr;
23887c478bd9Sstevel@tonic-gate 	prw->pr_size = wp->pr_size;
23897c478bd9Sstevel@tonic-gate 	prw->pr_wflags = 0;
23907c478bd9Sstevel@tonic-gate 	ctlp += sizeof (prwatch_t) / sizeof (long);
23917c478bd9Sstevel@tonic-gate 
23927c478bd9Sstevel@tonic-gate 	/*
23937c478bd9Sstevel@tonic-gate 	 * Clear the current signal and fault; set running with single-step.
23947c478bd9Sstevel@tonic-gate 	 * Then wait for the victim to stop and cancel the FLTTRACE.
23957c478bd9Sstevel@tonic-gate 	 */
23967c478bd9Sstevel@tonic-gate 	*ctlp++ = PCRUN;
23977c478bd9Sstevel@tonic-gate 	*ctlp++ = PRCSIG | PRCFAULT | PRSTEP;
23987c478bd9Sstevel@tonic-gate 	*ctlp++ = PCWSTOP;
23997c478bd9Sstevel@tonic-gate 	*ctlp++ = PCCFAULT;
24007c478bd9Sstevel@tonic-gate 
24017c478bd9Sstevel@tonic-gate 	/*
24027c478bd9Sstevel@tonic-gate 	 * Restore the current watchpoint.
24037c478bd9Sstevel@tonic-gate 	 */
24047c478bd9Sstevel@tonic-gate 	*ctlp++ = PCWATCH;
24057c478bd9Sstevel@tonic-gate 	(void) memcpy(ctlp, wp, sizeof (prwatch_t));
24067c478bd9Sstevel@tonic-gate 	ctlp += sizeof (prwatch_t) / sizeof (long);
24077c478bd9Sstevel@tonic-gate 
24087c478bd9Sstevel@tonic-gate 	/*
24097c478bd9Sstevel@tonic-gate 	 * Restore fault tracing set if we modified it.
24107c478bd9Sstevel@tonic-gate 	 */
24117c478bd9Sstevel@tonic-gate 	if (!(prismember(faultset, FLTTRACE))) {
24127c478bd9Sstevel@tonic-gate 		*ctlp++ = PCSFAULT;
24137c478bd9Sstevel@tonic-gate 		*(fltset_t *)ctlp = *faultset;
24147c478bd9Sstevel@tonic-gate 		ctlp += sizeof (fltset_t) / sizeof (long);
24157c478bd9Sstevel@tonic-gate 	}
24167c478bd9Sstevel@tonic-gate 
24177c478bd9Sstevel@tonic-gate 	/*
24187c478bd9Sstevel@tonic-gate 	 * Restore the hold mask to the current hold mask (i.e. the one
24197c478bd9Sstevel@tonic-gate 	 * before we executed any of the previous operations).
24207c478bd9Sstevel@tonic-gate 	 */
24217c478bd9Sstevel@tonic-gate 	*ctlp++ = PCSHOLD;
24227c478bd9Sstevel@tonic-gate 	*(sigset_t *)ctlp = *sigmask;
24237c478bd9Sstevel@tonic-gate 	ctlp += sizeof (sigset_t) / sizeof (long);
24247c478bd9Sstevel@tonic-gate 
24257c478bd9Sstevel@tonic-gate 	size = (char *)ctlp - (char *)ctl;
24267c478bd9Sstevel@tonic-gate 	if ((ssize = write(ctlfd, ctl, size)) != size)
24277c478bd9Sstevel@tonic-gate 		error = (ssize == -1)? errno : EINTR;
24287c478bd9Sstevel@tonic-gate 	(void) sigprocmask(SIG_SETMASK, &unblock, NULL);
24297c478bd9Sstevel@tonic-gate 	return (error);
24307c478bd9Sstevel@tonic-gate }
24317c478bd9Sstevel@tonic-gate 
24327c478bd9Sstevel@tonic-gate /*
24337c478bd9Sstevel@tonic-gate  * Step over a watchpoint, i.e., execute the instruction that was stopped by
24347c478bd9Sstevel@tonic-gate  * the watchpoint, and then leave the LWP stopped at the next instruction.
24357c478bd9Sstevel@tonic-gate  */
24367c478bd9Sstevel@tonic-gate int
24377c478bd9Sstevel@tonic-gate Pxecwapt(struct ps_prochandle *P, const prwatch_t *wp)
24387c478bd9Sstevel@tonic-gate {
24397c478bd9Sstevel@tonic-gate 	int ctlfd = (P->agentctlfd >= 0)? P->agentctlfd : P->ctlfd;
24407c478bd9Sstevel@tonic-gate 	int rv, error;
24417c478bd9Sstevel@tonic-gate 
24427c478bd9Sstevel@tonic-gate 	if (P->state != PS_STOP) {
24437c478bd9Sstevel@tonic-gate 		errno = EBUSY;
24447c478bd9Sstevel@tonic-gate 		return (-1);
24457c478bd9Sstevel@tonic-gate 	}
24467c478bd9Sstevel@tonic-gate 
24477c478bd9Sstevel@tonic-gate 	Psync(P);
24487c478bd9Sstevel@tonic-gate 	error = execute_wapt(ctlfd,
24497c478bd9Sstevel@tonic-gate 	    &P->status.pr_flttrace, &P->status.pr_lwp.pr_lwphold, wp);
24507c478bd9Sstevel@tonic-gate 	rv = Pstopstatus(P, PCNULL, 0);
24517c478bd9Sstevel@tonic-gate 
24527c478bd9Sstevel@tonic-gate 	if (error != 0) {
24537c478bd9Sstevel@tonic-gate 		if (P->status.pr_lwp.pr_why == PR_JOBCONTROL &&
24547c478bd9Sstevel@tonic-gate 		    error == EBUSY) {	/* jobcontrol stop -- back off */
24557c478bd9Sstevel@tonic-gate 			P->state = PS_RUN;
24567c478bd9Sstevel@tonic-gate 			return (0);
24577c478bd9Sstevel@tonic-gate 		}
24587c478bd9Sstevel@tonic-gate 		if (error == ENOENT)
24597c478bd9Sstevel@tonic-gate 			return (0);
24607c478bd9Sstevel@tonic-gate 		errno = error;
24617c478bd9Sstevel@tonic-gate 		return (-1);
24627c478bd9Sstevel@tonic-gate 	}
24637c478bd9Sstevel@tonic-gate 
24647c478bd9Sstevel@tonic-gate 	return (rv);
24657c478bd9Sstevel@tonic-gate }
24667c478bd9Sstevel@tonic-gate 
24677c478bd9Sstevel@tonic-gate int
24687c478bd9Sstevel@tonic-gate Psetflags(struct ps_prochandle *P, long flags)
24697c478bd9Sstevel@tonic-gate {
24707c478bd9Sstevel@tonic-gate 	int rc;
24717c478bd9Sstevel@tonic-gate 	long ctl[2];
24727c478bd9Sstevel@tonic-gate 
24737c478bd9Sstevel@tonic-gate 	ctl[0] = PCSET;
24747c478bd9Sstevel@tonic-gate 	ctl[1] = flags;
24757c478bd9Sstevel@tonic-gate 
24767c478bd9Sstevel@tonic-gate 	if (write(P->ctlfd, ctl, 2*sizeof (long)) != 2*sizeof (long)) {
24777c478bd9Sstevel@tonic-gate 		rc = -1;
24787c478bd9Sstevel@tonic-gate 	} else {
24797c478bd9Sstevel@tonic-gate 		P->status.pr_flags |= flags;
24807c478bd9Sstevel@tonic-gate 		P->status.pr_lwp.pr_flags |= flags;
24817c478bd9Sstevel@tonic-gate 		rc = 0;
24827c478bd9Sstevel@tonic-gate 	}
24837c478bd9Sstevel@tonic-gate 
24847c478bd9Sstevel@tonic-gate 	return (rc);
24857c478bd9Sstevel@tonic-gate }
24867c478bd9Sstevel@tonic-gate 
24877c478bd9Sstevel@tonic-gate int
24887c478bd9Sstevel@tonic-gate Punsetflags(struct ps_prochandle *P, long flags)
24897c478bd9Sstevel@tonic-gate {
24907c478bd9Sstevel@tonic-gate 	int rc;
24917c478bd9Sstevel@tonic-gate 	long ctl[2];
24927c478bd9Sstevel@tonic-gate 
24937c478bd9Sstevel@tonic-gate 	ctl[0] = PCUNSET;
24947c478bd9Sstevel@tonic-gate 	ctl[1] = flags;
24957c478bd9Sstevel@tonic-gate 
24967c478bd9Sstevel@tonic-gate 	if (write(P->ctlfd, ctl, 2*sizeof (long)) != 2*sizeof (long)) {
24977c478bd9Sstevel@tonic-gate 		rc = -1;
24987c478bd9Sstevel@tonic-gate 	} else {
24997c478bd9Sstevel@tonic-gate 		P->status.pr_flags &= ~flags;
25007c478bd9Sstevel@tonic-gate 		P->status.pr_lwp.pr_flags &= ~flags;
25017c478bd9Sstevel@tonic-gate 		rc = 0;
25027c478bd9Sstevel@tonic-gate 	}
25037c478bd9Sstevel@tonic-gate 
25047c478bd9Sstevel@tonic-gate 	return (rc);
25057c478bd9Sstevel@tonic-gate }
25067c478bd9Sstevel@tonic-gate 
25077c478bd9Sstevel@tonic-gate /*
25087c478bd9Sstevel@tonic-gate  * Common function to allow clients to manipulate the action to be taken
25097c478bd9Sstevel@tonic-gate  * on receipt of a signal, receipt of machine fault, entry to a system call,
25107c478bd9Sstevel@tonic-gate  * or exit from a system call.  We make use of our private prset_* functions
25117c478bd9Sstevel@tonic-gate  * in order to make this code be common.  The 'which' parameter identifies
25127c478bd9Sstevel@tonic-gate  * the code for the event of interest (0 means change the entire set), and
25137c478bd9Sstevel@tonic-gate  * the 'stop' parameter is a boolean indicating whether the process should
25147c478bd9Sstevel@tonic-gate  * stop when the event of interest occurs.  The previous value is returned
25157c478bd9Sstevel@tonic-gate  * to the caller; -1 is returned if an error occurred.
25167c478bd9Sstevel@tonic-gate  */
25177c478bd9Sstevel@tonic-gate static int
25187c478bd9Sstevel@tonic-gate Psetaction(struct ps_prochandle *P, void *sp, size_t size,
25197c478bd9Sstevel@tonic-gate     uint_t flag, int max, int which, int stop)
25207c478bd9Sstevel@tonic-gate {
25217c478bd9Sstevel@tonic-gate 	int oldval;
25227c478bd9Sstevel@tonic-gate 
25237c478bd9Sstevel@tonic-gate 	if (which < 0 || which > max) {
25247c478bd9Sstevel@tonic-gate 		errno = EINVAL;
25257c478bd9Sstevel@tonic-gate 		return (-1);
25267c478bd9Sstevel@tonic-gate 	}
25277c478bd9Sstevel@tonic-gate 
25287c478bd9Sstevel@tonic-gate 	if (P->state == PS_DEAD || P->state == PS_UNDEAD ||
25297c478bd9Sstevel@tonic-gate 	    P->state == PS_IDLE) {
25307c478bd9Sstevel@tonic-gate 		errno = ENOENT;
25317c478bd9Sstevel@tonic-gate 		return (-1);
25327c478bd9Sstevel@tonic-gate 	}
25337c478bd9Sstevel@tonic-gate 
25347c478bd9Sstevel@tonic-gate 	oldval = prset_ismember(sp, size, which) ? TRUE : FALSE;
25357c478bd9Sstevel@tonic-gate 
25367c478bd9Sstevel@tonic-gate 	if (stop) {
25377c478bd9Sstevel@tonic-gate 		if (which == 0) {
25387c478bd9Sstevel@tonic-gate 			prset_fill(sp, size);
25397c478bd9Sstevel@tonic-gate 			P->flags |= flag;
25407c478bd9Sstevel@tonic-gate 		} else if (!oldval) {
25417c478bd9Sstevel@tonic-gate 			prset_add(sp, size, which);
25427c478bd9Sstevel@tonic-gate 			P->flags |= flag;
25437c478bd9Sstevel@tonic-gate 		}
25447c478bd9Sstevel@tonic-gate 	} else {
25457c478bd9Sstevel@tonic-gate 		if (which == 0) {
25467c478bd9Sstevel@tonic-gate 			prset_empty(sp, size);
25477c478bd9Sstevel@tonic-gate 			P->flags |= flag;
25487c478bd9Sstevel@tonic-gate 		} else if (oldval) {
25497c478bd9Sstevel@tonic-gate 			prset_del(sp, size, which);
25507c478bd9Sstevel@tonic-gate 			P->flags |= flag;
25517c478bd9Sstevel@tonic-gate 		}
25527c478bd9Sstevel@tonic-gate 	}
25537c478bd9Sstevel@tonic-gate 
25547c478bd9Sstevel@tonic-gate 	if (P->state == PS_RUN)
25557c478bd9Sstevel@tonic-gate 		Psync(P);
25567c478bd9Sstevel@tonic-gate 
25577c478bd9Sstevel@tonic-gate 	return (oldval);
25587c478bd9Sstevel@tonic-gate }
25597c478bd9Sstevel@tonic-gate 
25607c478bd9Sstevel@tonic-gate /*
25617c478bd9Sstevel@tonic-gate  * Set action on specified signal.
25627c478bd9Sstevel@tonic-gate  */
25637c478bd9Sstevel@tonic-gate int
25647c478bd9Sstevel@tonic-gate Psignal(struct ps_prochandle *P, int which, int stop)
25657c478bd9Sstevel@tonic-gate {
25667c478bd9Sstevel@tonic-gate 	int oldval;
25677c478bd9Sstevel@tonic-gate 
25687c478bd9Sstevel@tonic-gate 	if (which == SIGKILL && stop != 0) {
25697c478bd9Sstevel@tonic-gate 		errno = EINVAL;
25707c478bd9Sstevel@tonic-gate 		return (-1);
25717c478bd9Sstevel@tonic-gate 	}
25727c478bd9Sstevel@tonic-gate 
25737c478bd9Sstevel@tonic-gate 	oldval = Psetaction(P, &P->status.pr_sigtrace, sizeof (sigset_t),
25747c478bd9Sstevel@tonic-gate 	    SETSIG, PRMAXSIG, which, stop);
25757c478bd9Sstevel@tonic-gate 
25767c478bd9Sstevel@tonic-gate 	if (oldval != -1 && which == 0 && stop != 0)
25777c478bd9Sstevel@tonic-gate 		prdelset(&P->status.pr_sigtrace, SIGKILL);
25787c478bd9Sstevel@tonic-gate 
25797c478bd9Sstevel@tonic-gate 	return (oldval);
25807c478bd9Sstevel@tonic-gate }
25817c478bd9Sstevel@tonic-gate 
25827c478bd9Sstevel@tonic-gate /*
25837c478bd9Sstevel@tonic-gate  * Set all signal tracing flags.
25847c478bd9Sstevel@tonic-gate  */
25857c478bd9Sstevel@tonic-gate void
25867c478bd9Sstevel@tonic-gate Psetsignal(struct ps_prochandle *P, const sigset_t *set)
25877c478bd9Sstevel@tonic-gate {
25887c478bd9Sstevel@tonic-gate 	if (P->state == PS_DEAD || P->state == PS_UNDEAD ||
25897c478bd9Sstevel@tonic-gate 	    P->state == PS_IDLE)
25907c478bd9Sstevel@tonic-gate 		return;
25917c478bd9Sstevel@tonic-gate 
25927c478bd9Sstevel@tonic-gate 	P->status.pr_sigtrace = *set;
25937c478bd9Sstevel@tonic-gate 	P->flags |= SETSIG;
25947c478bd9Sstevel@tonic-gate 
25957c478bd9Sstevel@tonic-gate 	if (P->state == PS_RUN)
25967c478bd9Sstevel@tonic-gate 		Psync(P);
25977c478bd9Sstevel@tonic-gate }
25987c478bd9Sstevel@tonic-gate 
25997c478bd9Sstevel@tonic-gate /*
26007c478bd9Sstevel@tonic-gate  * Set action on specified fault.
26017c478bd9Sstevel@tonic-gate  */
26027c478bd9Sstevel@tonic-gate int
26037c478bd9Sstevel@tonic-gate Pfault(struct ps_prochandle *P, int which, int stop)
26047c478bd9Sstevel@tonic-gate {
26057c478bd9Sstevel@tonic-gate 	return (Psetaction(P, &P->status.pr_flttrace, sizeof (fltset_t),
26067c478bd9Sstevel@tonic-gate 	    SETFAULT, PRMAXFAULT, which, stop));
26077c478bd9Sstevel@tonic-gate }
26087c478bd9Sstevel@tonic-gate 
26097c478bd9Sstevel@tonic-gate /*
26107c478bd9Sstevel@tonic-gate  * Set all machine fault tracing flags.
26117c478bd9Sstevel@tonic-gate  */
26127c478bd9Sstevel@tonic-gate void
26137c478bd9Sstevel@tonic-gate Psetfault(struct ps_prochandle *P, const fltset_t *set)
26147c478bd9Sstevel@tonic-gate {
26157c478bd9Sstevel@tonic-gate 	if (P->state == PS_DEAD || P->state == PS_UNDEAD ||
26167c478bd9Sstevel@tonic-gate 	    P->state == PS_IDLE)
26177c478bd9Sstevel@tonic-gate 		return;
26187c478bd9Sstevel@tonic-gate 
26197c478bd9Sstevel@tonic-gate 	P->status.pr_flttrace = *set;
26207c478bd9Sstevel@tonic-gate 	P->flags |= SETFAULT;
26217c478bd9Sstevel@tonic-gate 
26227c478bd9Sstevel@tonic-gate 	if (P->state == PS_RUN)
26237c478bd9Sstevel@tonic-gate 		Psync(P);
26247c478bd9Sstevel@tonic-gate }
26257c478bd9Sstevel@tonic-gate 
26267c478bd9Sstevel@tonic-gate /*
26277c478bd9Sstevel@tonic-gate  * Set action on specified system call entry.
26287c478bd9Sstevel@tonic-gate  */
26297c478bd9Sstevel@tonic-gate int
26307c478bd9Sstevel@tonic-gate Psysentry(struct ps_prochandle *P, int which, int stop)
26317c478bd9Sstevel@tonic-gate {
26327c478bd9Sstevel@tonic-gate 	return (Psetaction(P, &P->status.pr_sysentry, sizeof (sysset_t),
26337c478bd9Sstevel@tonic-gate 	    SETENTRY, PRMAXSYS, which, stop));
26347c478bd9Sstevel@tonic-gate }
26357c478bd9Sstevel@tonic-gate 
26367c478bd9Sstevel@tonic-gate /*
26377c478bd9Sstevel@tonic-gate  * Set all system call entry tracing flags.
26387c478bd9Sstevel@tonic-gate  */
26397c478bd9Sstevel@tonic-gate void
26407c478bd9Sstevel@tonic-gate Psetsysentry(struct ps_prochandle *P, const sysset_t *set)
26417c478bd9Sstevel@tonic-gate {
26427c478bd9Sstevel@tonic-gate 	if (P->state == PS_DEAD || P->state == PS_UNDEAD ||
26437c478bd9Sstevel@tonic-gate 	    P->state == PS_IDLE)
26447c478bd9Sstevel@tonic-gate 		return;
26457c478bd9Sstevel@tonic-gate 
26467c478bd9Sstevel@tonic-gate 	P->status.pr_sysentry = *set;
26477c478bd9Sstevel@tonic-gate 	P->flags |= SETENTRY;
26487c478bd9Sstevel@tonic-gate 
26497c478bd9Sstevel@tonic-gate 	if (P->state == PS_RUN)
26507c478bd9Sstevel@tonic-gate 		Psync(P);
26517c478bd9Sstevel@tonic-gate }
26527c478bd9Sstevel@tonic-gate 
26537c478bd9Sstevel@tonic-gate /*
26547c478bd9Sstevel@tonic-gate  * Set action on specified system call exit.
26557c478bd9Sstevel@tonic-gate  */
26567c478bd9Sstevel@tonic-gate int
26577c478bd9Sstevel@tonic-gate Psysexit(struct ps_prochandle *P, int which, int stop)
26587c478bd9Sstevel@tonic-gate {
26597c478bd9Sstevel@tonic-gate 	return (Psetaction(P, &P->status.pr_sysexit, sizeof (sysset_t),
26607c478bd9Sstevel@tonic-gate 	    SETEXIT, PRMAXSYS, which, stop));
26617c478bd9Sstevel@tonic-gate }
26627c478bd9Sstevel@tonic-gate 
26637c478bd9Sstevel@tonic-gate /*
26647c478bd9Sstevel@tonic-gate  * Set all system call exit tracing flags.
26657c478bd9Sstevel@tonic-gate  */
26667c478bd9Sstevel@tonic-gate void
26677c478bd9Sstevel@tonic-gate Psetsysexit(struct ps_prochandle *P, const sysset_t *set)
26687c478bd9Sstevel@tonic-gate {
26697c478bd9Sstevel@tonic-gate 	if (P->state == PS_DEAD || P->state == PS_UNDEAD ||
26707c478bd9Sstevel@tonic-gate 	    P->state == PS_IDLE)
26717c478bd9Sstevel@tonic-gate 		return;
26727c478bd9Sstevel@tonic-gate 
26737c478bd9Sstevel@tonic-gate 	P->status.pr_sysexit = *set;
26747c478bd9Sstevel@tonic-gate 	P->flags |= SETEXIT;
26757c478bd9Sstevel@tonic-gate 
26767c478bd9Sstevel@tonic-gate 	if (P->state == PS_RUN)
26777c478bd9Sstevel@tonic-gate 		Psync(P);
26787c478bd9Sstevel@tonic-gate }
26797c478bd9Sstevel@tonic-gate 
26807c478bd9Sstevel@tonic-gate /*
26817c478bd9Sstevel@tonic-gate  * Utility function to read the contents of a file that contains a
26827c478bd9Sstevel@tonic-gate  * prheader_t at the start (/proc/pid/lstatus or /proc/pid/lpsinfo).
26837c478bd9Sstevel@tonic-gate  * Returns a malloc()d buffer or NULL on failure.
26847c478bd9Sstevel@tonic-gate  */
26857c478bd9Sstevel@tonic-gate static prheader_t *
26867c478bd9Sstevel@tonic-gate read_lfile(struct ps_prochandle *P, const char *lname)
26877c478bd9Sstevel@tonic-gate {
26887c478bd9Sstevel@tonic-gate 	prheader_t *Lhp;
26899acbbeafSnn35248 	char lpath[PATH_MAX];
26907c478bd9Sstevel@tonic-gate 	struct stat64 statb;
26917c478bd9Sstevel@tonic-gate 	int fd;
26927c478bd9Sstevel@tonic-gate 	size_t size;
26937c478bd9Sstevel@tonic-gate 	ssize_t rval;
26947c478bd9Sstevel@tonic-gate 
26959acbbeafSnn35248 	(void) snprintf(lpath, sizeof (lpath), "%s/%d/%s", procfs_path,
26967c478bd9Sstevel@tonic-gate 	    (int)P->status.pr_pid, lname);
26977c478bd9Sstevel@tonic-gate 	if ((fd = open(lpath, O_RDONLY)) < 0 || fstat64(fd, &statb) != 0) {
26987c478bd9Sstevel@tonic-gate 		if (fd >= 0)
26997c478bd9Sstevel@tonic-gate 			(void) close(fd);
27007c478bd9Sstevel@tonic-gate 		return (NULL);
27017c478bd9Sstevel@tonic-gate 	}
27027c478bd9Sstevel@tonic-gate 
27037c478bd9Sstevel@tonic-gate 	/*
27047c478bd9Sstevel@tonic-gate 	 * 'size' is just the initial guess at the buffer size.
27057c478bd9Sstevel@tonic-gate 	 * It will have to grow if the number of lwps increases
27067c478bd9Sstevel@tonic-gate 	 * while we are looking at the process.
27077c478bd9Sstevel@tonic-gate 	 * 'size' must be larger than the actual file size.
27087c478bd9Sstevel@tonic-gate 	 */
27097c478bd9Sstevel@tonic-gate 	size = statb.st_size + 32;
27107c478bd9Sstevel@tonic-gate 
27117c478bd9Sstevel@tonic-gate 	for (;;) {
27127c478bd9Sstevel@tonic-gate 		if ((Lhp = malloc(size)) == NULL)
27137c478bd9Sstevel@tonic-gate 			break;
27147c478bd9Sstevel@tonic-gate 		if ((rval = pread(fd, Lhp, size, 0)) < 0 ||
27157c478bd9Sstevel@tonic-gate 		    rval <= sizeof (prheader_t)) {
27167c478bd9Sstevel@tonic-gate 			free(Lhp);
27177c478bd9Sstevel@tonic-gate 			Lhp = NULL;
27187c478bd9Sstevel@tonic-gate 			break;
27197c478bd9Sstevel@tonic-gate 		}
27207c478bd9Sstevel@tonic-gate 		if (rval < size)
27217c478bd9Sstevel@tonic-gate 			break;
27227c478bd9Sstevel@tonic-gate 		/* need a bigger buffer */
27237c478bd9Sstevel@tonic-gate 		free(Lhp);
27247c478bd9Sstevel@tonic-gate 		size *= 2;
27257c478bd9Sstevel@tonic-gate 	}
27267c478bd9Sstevel@tonic-gate 
27277c478bd9Sstevel@tonic-gate 	(void) close(fd);
27287c478bd9Sstevel@tonic-gate 	return (Lhp);
27297c478bd9Sstevel@tonic-gate }
27307c478bd9Sstevel@tonic-gate 
27317c478bd9Sstevel@tonic-gate /*
27327c478bd9Sstevel@tonic-gate  * LWP iteration interface.
27337c478bd9Sstevel@tonic-gate  */
27347c478bd9Sstevel@tonic-gate int
27357c478bd9Sstevel@tonic-gate Plwp_iter(struct ps_prochandle *P, proc_lwp_f *func, void *cd)
27367c478bd9Sstevel@tonic-gate {
27377c478bd9Sstevel@tonic-gate 	prheader_t *Lhp;
27387c478bd9Sstevel@tonic-gate 	lwpstatus_t *Lsp;
27397c478bd9Sstevel@tonic-gate 	long nlwp;
27407c478bd9Sstevel@tonic-gate 	int rv;
27417c478bd9Sstevel@tonic-gate 
27427c478bd9Sstevel@tonic-gate 	switch (P->state) {
27437c478bd9Sstevel@tonic-gate 	case PS_RUN:
27447c478bd9Sstevel@tonic-gate 		(void) Pstopstatus(P, PCNULL, 0);
27457c478bd9Sstevel@tonic-gate 		break;
27467c478bd9Sstevel@tonic-gate 
27477c478bd9Sstevel@tonic-gate 	case PS_STOP:
27487c478bd9Sstevel@tonic-gate 		Psync(P);
27497c478bd9Sstevel@tonic-gate 		break;
27507c478bd9Sstevel@tonic-gate 
27517c478bd9Sstevel@tonic-gate 	case PS_IDLE:
27527c478bd9Sstevel@tonic-gate 		errno = ENODATA;
27537c478bd9Sstevel@tonic-gate 		return (-1);
27547c478bd9Sstevel@tonic-gate 	}
27557c478bd9Sstevel@tonic-gate 
27567c478bd9Sstevel@tonic-gate 	/*
27577c478bd9Sstevel@tonic-gate 	 * For either live processes or cores, the single LWP case is easy:
27587c478bd9Sstevel@tonic-gate 	 * the pstatus_t contains the lwpstatus_t for the only LWP.
27597c478bd9Sstevel@tonic-gate 	 */
27607c478bd9Sstevel@tonic-gate 	if (P->status.pr_nlwp <= 1)
27617c478bd9Sstevel@tonic-gate 		return (func(cd, &P->status.pr_lwp));
27627c478bd9Sstevel@tonic-gate 
27637c478bd9Sstevel@tonic-gate 	/*
27647c478bd9Sstevel@tonic-gate 	 * For the core file multi-LWP case, we just iterate through the
27657c478bd9Sstevel@tonic-gate 	 * list of LWP structs we read in from the core file.
27667c478bd9Sstevel@tonic-gate 	 */
27677c478bd9Sstevel@tonic-gate 	if (P->state == PS_DEAD) {
27687c478bd9Sstevel@tonic-gate 		lwp_info_t *lwp = list_prev(&P->core->core_lwp_head);
27697c478bd9Sstevel@tonic-gate 		uint_t i;
27707c478bd9Sstevel@tonic-gate 
27717c478bd9Sstevel@tonic-gate 		for (i = 0; i < P->core->core_nlwp; i++, lwp = list_prev(lwp)) {
27727c478bd9Sstevel@tonic-gate 			if (lwp->lwp_psinfo.pr_sname != 'Z' &&
27737c478bd9Sstevel@tonic-gate 			    (rv = func(cd, &lwp->lwp_status)) != 0)
27747c478bd9Sstevel@tonic-gate 				break;
27757c478bd9Sstevel@tonic-gate 		}
27767c478bd9Sstevel@tonic-gate 
27777c478bd9Sstevel@tonic-gate 		return (rv);
27787c478bd9Sstevel@tonic-gate 	}
27797c478bd9Sstevel@tonic-gate 
27807c478bd9Sstevel@tonic-gate 	/*
27817c478bd9Sstevel@tonic-gate 	 * For the live process multi-LWP case, we have to work a little
27827c478bd9Sstevel@tonic-gate 	 * harder: the /proc/pid/lstatus file has the array of LWP structs.
27837c478bd9Sstevel@tonic-gate 	 */
27847c478bd9Sstevel@tonic-gate 	if ((Lhp = read_lfile(P, "lstatus")) == NULL)
27857c478bd9Sstevel@tonic-gate 		return (-1);
27867c478bd9Sstevel@tonic-gate 
27877c478bd9Sstevel@tonic-gate 	for (nlwp = Lhp->pr_nent, Lsp = (lwpstatus_t *)(uintptr_t)(Lhp + 1);
27887c478bd9Sstevel@tonic-gate 	    nlwp > 0;
27897c478bd9Sstevel@tonic-gate 	    nlwp--, Lsp = (lwpstatus_t *)((uintptr_t)Lsp + Lhp->pr_entsize)) {
27907c478bd9Sstevel@tonic-gate 		if ((rv = func(cd, Lsp)) != 0)
27917c478bd9Sstevel@tonic-gate 			break;
27927c478bd9Sstevel@tonic-gate 	}
27937c478bd9Sstevel@tonic-gate 
27947c478bd9Sstevel@tonic-gate 	free(Lhp);
27957c478bd9Sstevel@tonic-gate 	return (rv);
27967c478bd9Sstevel@tonic-gate }
27977c478bd9Sstevel@tonic-gate 
27987c478bd9Sstevel@tonic-gate /*
27997c478bd9Sstevel@tonic-gate  * Extended LWP iteration interface.
28007c478bd9Sstevel@tonic-gate  * Iterate over all LWPs, active and zombie.
28017c478bd9Sstevel@tonic-gate  */
28027c478bd9Sstevel@tonic-gate int
28037c478bd9Sstevel@tonic-gate Plwp_iter_all(struct ps_prochandle *P, proc_lwp_all_f *func, void *cd)
28047c478bd9Sstevel@tonic-gate {
28057c478bd9Sstevel@tonic-gate 	prheader_t *Lhp = NULL;
28067c478bd9Sstevel@tonic-gate 	lwpstatus_t *Lsp;
28077c478bd9Sstevel@tonic-gate 	lwpstatus_t *sp;
28087c478bd9Sstevel@tonic-gate 	prheader_t *Lphp = NULL;
28097c478bd9Sstevel@tonic-gate 	lwpsinfo_t *Lpsp;
28107c478bd9Sstevel@tonic-gate 	long nstat;
28117c478bd9Sstevel@tonic-gate 	long ninfo;
28127c478bd9Sstevel@tonic-gate 	int rv;
28137c478bd9Sstevel@tonic-gate 
28147c478bd9Sstevel@tonic-gate retry:
28157c478bd9Sstevel@tonic-gate 	if (Lhp != NULL)
28167c478bd9Sstevel@tonic-gate 		free(Lhp);
28177c478bd9Sstevel@tonic-gate 	if (Lphp != NULL)
28187c478bd9Sstevel@tonic-gate 		free(Lphp);
28197c478bd9Sstevel@tonic-gate 	if (P->state == PS_RUN)
28207c478bd9Sstevel@tonic-gate 		(void) Pstopstatus(P, PCNULL, 0);
28217c478bd9Sstevel@tonic-gate 	(void) Ppsinfo(P);
28227c478bd9Sstevel@tonic-gate 
28237c478bd9Sstevel@tonic-gate 	if (P->state == PS_STOP)
28247c478bd9Sstevel@tonic-gate 		Psync(P);
28257c478bd9Sstevel@tonic-gate 
28267c478bd9Sstevel@tonic-gate 	/*
28277c478bd9Sstevel@tonic-gate 	 * For either live processes or cores, the single LWP case is easy:
28287c478bd9Sstevel@tonic-gate 	 * the pstatus_t contains the lwpstatus_t for the only LWP and
28297c478bd9Sstevel@tonic-gate 	 * the psinfo_t contains the lwpsinfo_t for the only LWP.
28307c478bd9Sstevel@tonic-gate 	 */
28317c478bd9Sstevel@tonic-gate 	if (P->status.pr_nlwp + P->status.pr_nzomb <= 1)
28327c478bd9Sstevel@tonic-gate 		return (func(cd, &P->status.pr_lwp, &P->psinfo.pr_lwp));
28337c478bd9Sstevel@tonic-gate 
28347c478bd9Sstevel@tonic-gate 	/*
28357c478bd9Sstevel@tonic-gate 	 * For the core file multi-LWP case, we just iterate through the
28367c478bd9Sstevel@tonic-gate 	 * list of LWP structs we read in from the core file.
28377c478bd9Sstevel@tonic-gate 	 */
28387c478bd9Sstevel@tonic-gate 	if (P->state == PS_DEAD) {
28397c478bd9Sstevel@tonic-gate 		lwp_info_t *lwp = list_prev(&P->core->core_lwp_head);
28407c478bd9Sstevel@tonic-gate 		uint_t i;
28417c478bd9Sstevel@tonic-gate 
28427c478bd9Sstevel@tonic-gate 		for (i = 0; i < P->core->core_nlwp; i++, lwp = list_prev(lwp)) {
28437c478bd9Sstevel@tonic-gate 			sp = (lwp->lwp_psinfo.pr_sname == 'Z')? NULL :
28447c478bd9Sstevel@tonic-gate 			    &lwp->lwp_status;
28457c478bd9Sstevel@tonic-gate 			if ((rv = func(cd, sp, &lwp->lwp_psinfo)) != 0)
28467c478bd9Sstevel@tonic-gate 				break;
28477c478bd9Sstevel@tonic-gate 		}
28487c478bd9Sstevel@tonic-gate 
28497c478bd9Sstevel@tonic-gate 		return (rv);
28507c478bd9Sstevel@tonic-gate 	}
28517c478bd9Sstevel@tonic-gate 
28527c478bd9Sstevel@tonic-gate 	/*
28537c478bd9Sstevel@tonic-gate 	 * For the live process multi-LWP case, we have to work a little
28547c478bd9Sstevel@tonic-gate 	 * harder: the /proc/pid/lstatus file has the array of lwpstatus_t's
28557c478bd9Sstevel@tonic-gate 	 * and the /proc/pid/lpsinfo file has the array of lwpsinfo_t's.
28567c478bd9Sstevel@tonic-gate 	 */
28577c478bd9Sstevel@tonic-gate 	if ((Lhp = read_lfile(P, "lstatus")) == NULL)
28587c478bd9Sstevel@tonic-gate 		return (-1);
28597c478bd9Sstevel@tonic-gate 	if ((Lphp = read_lfile(P, "lpsinfo")) == NULL) {
28607c478bd9Sstevel@tonic-gate 		free(Lhp);
28617c478bd9Sstevel@tonic-gate 		return (-1);
28627c478bd9Sstevel@tonic-gate 	}
28637c478bd9Sstevel@tonic-gate 
28647c478bd9Sstevel@tonic-gate 	/*
28657c478bd9Sstevel@tonic-gate 	 * If we are looking at a running process, or one we do not control,
28667c478bd9Sstevel@tonic-gate 	 * the active and zombie lwps in the process may have changed since
28677c478bd9Sstevel@tonic-gate 	 * we read the process status structure.  If so, just start over.
28687c478bd9Sstevel@tonic-gate 	 */
28697c478bd9Sstevel@tonic-gate 	if (Lhp->pr_nent != P->status.pr_nlwp ||
28707c478bd9Sstevel@tonic-gate 	    Lphp->pr_nent != P->status.pr_nlwp + P->status.pr_nzomb)
28717c478bd9Sstevel@tonic-gate 		goto retry;
28727c478bd9Sstevel@tonic-gate 
28737c478bd9Sstevel@tonic-gate 	/*
28747c478bd9Sstevel@tonic-gate 	 * To be perfectly safe, prescan the two arrays, checking consistency.
28757c478bd9Sstevel@tonic-gate 	 * We rely on /proc giving us lwpstatus_t's and lwpsinfo_t's in the
28767c478bd9Sstevel@tonic-gate 	 * same order (the lwp directory order) in their respective files.
28777c478bd9Sstevel@tonic-gate 	 * We also rely on there being (possibly) more lwpsinfo_t's than
28787c478bd9Sstevel@tonic-gate 	 * lwpstatus_t's (the extra lwpsinfo_t's are for zombie lwps).
28797c478bd9Sstevel@tonic-gate 	 */
28807c478bd9Sstevel@tonic-gate 	Lsp = (lwpstatus_t *)(uintptr_t)(Lhp + 1);
28817c478bd9Sstevel@tonic-gate 	Lpsp = (lwpsinfo_t *)(uintptr_t)(Lphp + 1);
28827c478bd9Sstevel@tonic-gate 	nstat = Lhp->pr_nent;
28837c478bd9Sstevel@tonic-gate 	for (ninfo = Lphp->pr_nent; ninfo != 0; ninfo--) {
28847c478bd9Sstevel@tonic-gate 		if (Lpsp->pr_sname != 'Z') {
28857c478bd9Sstevel@tonic-gate 			/*
28867c478bd9Sstevel@tonic-gate 			 * Not a zombie lwp; check for matching lwpids.
28877c478bd9Sstevel@tonic-gate 			 */
28887c478bd9Sstevel@tonic-gate 			if (nstat == 0 || Lsp->pr_lwpid != Lpsp->pr_lwpid)
28897c478bd9Sstevel@tonic-gate 				goto retry;
28907c478bd9Sstevel@tonic-gate 			Lsp = (lwpstatus_t *)((uintptr_t)Lsp + Lhp->pr_entsize);
28917c478bd9Sstevel@tonic-gate 			nstat--;
28927c478bd9Sstevel@tonic-gate 		}
28937c478bd9Sstevel@tonic-gate 		Lpsp = (lwpsinfo_t *)((uintptr_t)Lpsp + Lphp->pr_entsize);
28947c478bd9Sstevel@tonic-gate 	}
28957c478bd9Sstevel@tonic-gate 	if (nstat != 0)
28967c478bd9Sstevel@tonic-gate 		goto retry;
28977c478bd9Sstevel@tonic-gate 
28987c478bd9Sstevel@tonic-gate 	/*
28997c478bd9Sstevel@tonic-gate 	 * Rescan, this time for real.
29007c478bd9Sstevel@tonic-gate 	 */
29017c478bd9Sstevel@tonic-gate 	Lsp = (lwpstatus_t *)(uintptr_t)(Lhp + 1);
29027c478bd9Sstevel@tonic-gate 	Lpsp = (lwpsinfo_t *)(uintptr_t)(Lphp + 1);
29037c478bd9Sstevel@tonic-gate 	for (ninfo = Lphp->pr_nent; ninfo != 0; ninfo--) {
29047c478bd9Sstevel@tonic-gate 		if (Lpsp->pr_sname != 'Z') {
29057c478bd9Sstevel@tonic-gate 			sp = Lsp;
29067c478bd9Sstevel@tonic-gate 			Lsp = (lwpstatus_t *)((uintptr_t)Lsp + Lhp->pr_entsize);
29077c478bd9Sstevel@tonic-gate 		} else {
29087c478bd9Sstevel@tonic-gate 			sp = NULL;
29097c478bd9Sstevel@tonic-gate 		}
29107c478bd9Sstevel@tonic-gate 		if ((rv = func(cd, sp, Lpsp)) != 0)
29117c478bd9Sstevel@tonic-gate 			break;
29127c478bd9Sstevel@tonic-gate 		Lpsp = (lwpsinfo_t *)((uintptr_t)Lpsp + Lphp->pr_entsize);
29137c478bd9Sstevel@tonic-gate 	}
29147c478bd9Sstevel@tonic-gate 
29157c478bd9Sstevel@tonic-gate 	free(Lhp);
29167c478bd9Sstevel@tonic-gate 	free(Lphp);
29177c478bd9Sstevel@tonic-gate 	return (rv);
29187c478bd9Sstevel@tonic-gate }
29197c478bd9Sstevel@tonic-gate 
29207c478bd9Sstevel@tonic-gate core_content_t
29217c478bd9Sstevel@tonic-gate Pcontent(struct ps_prochandle *P)
29227c478bd9Sstevel@tonic-gate {
29237c478bd9Sstevel@tonic-gate 	if (P->state == PS_DEAD)
29247c478bd9Sstevel@tonic-gate 		return (P->core->core_content);
29257c478bd9Sstevel@tonic-gate 	if (P->state == PS_IDLE)
29267c478bd9Sstevel@tonic-gate 		return (CC_CONTENT_TEXT | CC_CONTENT_DATA | CC_CONTENT_CTF);
29277c478bd9Sstevel@tonic-gate 
29287c478bd9Sstevel@tonic-gate 	return (CC_CONTENT_ALL);
29297c478bd9Sstevel@tonic-gate }
29307c478bd9Sstevel@tonic-gate 
29317c478bd9Sstevel@tonic-gate /*
29327c478bd9Sstevel@tonic-gate  * =================================================================
29337c478bd9Sstevel@tonic-gate  * The remainder of the functions in this file are for the
29347c478bd9Sstevel@tonic-gate  * control of individual LWPs in the controlled process.
29357c478bd9Sstevel@tonic-gate  * =================================================================
29367c478bd9Sstevel@tonic-gate  */
29377c478bd9Sstevel@tonic-gate 
29387c478bd9Sstevel@tonic-gate /*
29397c478bd9Sstevel@tonic-gate  * Find an entry in the process hash table for the specified lwpid.
29407c478bd9Sstevel@tonic-gate  * The entry will either point to an existing struct ps_lwphandle
29417c478bd9Sstevel@tonic-gate  * or it will point to an empty slot for a new struct ps_lwphandle.
29427c478bd9Sstevel@tonic-gate  */
29437c478bd9Sstevel@tonic-gate static struct ps_lwphandle **
29447c478bd9Sstevel@tonic-gate Lfind(struct ps_prochandle *P, lwpid_t lwpid)
29457c478bd9Sstevel@tonic-gate {
29467c478bd9Sstevel@tonic-gate 	struct ps_lwphandle **Lp;
29477c478bd9Sstevel@tonic-gate 	struct ps_lwphandle *L;
29487c478bd9Sstevel@tonic-gate 
29497c478bd9Sstevel@tonic-gate 	for (Lp = &P->hashtab[lwpid % (HASHSIZE - 1)];
29507c478bd9Sstevel@tonic-gate 	    (L = *Lp) != NULL; Lp = &L->lwp_hash)
29517c478bd9Sstevel@tonic-gate 		if (L->lwp_id == lwpid)
29527c478bd9Sstevel@tonic-gate 			break;
29537c478bd9Sstevel@tonic-gate 	return (Lp);
29547c478bd9Sstevel@tonic-gate }
29557c478bd9Sstevel@tonic-gate 
29567c478bd9Sstevel@tonic-gate /*
29577c478bd9Sstevel@tonic-gate  * Grab an LWP contained within the controlled process.
29587c478bd9Sstevel@tonic-gate  * Return an opaque pointer to its LWP control structure.
29597c478bd9Sstevel@tonic-gate  *	perr: pointer to error return code.
29607c478bd9Sstevel@tonic-gate  */
29617c478bd9Sstevel@tonic-gate struct ps_lwphandle *
29627c478bd9Sstevel@tonic-gate Lgrab(struct ps_prochandle *P, lwpid_t lwpid, int *perr)
29637c478bd9Sstevel@tonic-gate {
29647c478bd9Sstevel@tonic-gate 	struct ps_lwphandle **Lp;
29657c478bd9Sstevel@tonic-gate 	struct ps_lwphandle *L;
29667c478bd9Sstevel@tonic-gate 	int fd;
29679acbbeafSnn35248 	char procname[PATH_MAX];
29687c478bd9Sstevel@tonic-gate 	char *fname;
29697c478bd9Sstevel@tonic-gate 	int rc = 0;
29707c478bd9Sstevel@tonic-gate 
29717c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&P->proc_lock);
29727c478bd9Sstevel@tonic-gate 
29737c478bd9Sstevel@tonic-gate 	if (P->state == PS_UNDEAD || P->state == PS_IDLE)
29747c478bd9Sstevel@tonic-gate 		rc = G_NOPROC;
29757c478bd9Sstevel@tonic-gate 	else if (P->hashtab == NULL &&
29767c478bd9Sstevel@tonic-gate 	    (P->hashtab = calloc(HASHSIZE, sizeof (struct ps_lwphandle *)))
29777c478bd9Sstevel@tonic-gate 	    == NULL)
29787c478bd9Sstevel@tonic-gate 		rc = G_STRANGE;
29797c478bd9Sstevel@tonic-gate 	else if (*(Lp = Lfind(P, lwpid)) != NULL)
29807c478bd9Sstevel@tonic-gate 		rc = G_BUSY;
29817c478bd9Sstevel@tonic-gate 	else if ((L = malloc(sizeof (struct ps_lwphandle))) == NULL)
29827c478bd9Sstevel@tonic-gate 		rc = G_STRANGE;
29837c478bd9Sstevel@tonic-gate 	if (rc) {
29847c478bd9Sstevel@tonic-gate 		*perr = rc;
29857c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&P->proc_lock);
29867c478bd9Sstevel@tonic-gate 		return (NULL);
29877c478bd9Sstevel@tonic-gate 	}
29887c478bd9Sstevel@tonic-gate 
29897c478bd9Sstevel@tonic-gate 	(void) memset(L, 0, sizeof (*L));
29907c478bd9Sstevel@tonic-gate 	L->lwp_ctlfd = -1;
29917c478bd9Sstevel@tonic-gate 	L->lwp_statfd = -1;
29927c478bd9Sstevel@tonic-gate 	L->lwp_proc = P;
29937c478bd9Sstevel@tonic-gate 	L->lwp_id = lwpid;
29947c478bd9Sstevel@tonic-gate 	*Lp = L;	/* insert into the hash table */
29957c478bd9Sstevel@tonic-gate 
29967c478bd9Sstevel@tonic-gate 	if (P->state == PS_DEAD) {	/* core file */
29977c478bd9Sstevel@tonic-gate 		if (getlwpstatus(P, lwpid, &L->lwp_status) == -1) {
29987c478bd9Sstevel@tonic-gate 			rc = G_NOPROC;
29997c478bd9Sstevel@tonic-gate 			goto err;
30007c478bd9Sstevel@tonic-gate 		}
30017c478bd9Sstevel@tonic-gate 		L->lwp_state = PS_DEAD;
30027c478bd9Sstevel@tonic-gate 		*perr = 0;
30037c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&P->proc_lock);
30047c478bd9Sstevel@tonic-gate 		return (L);
30057c478bd9Sstevel@tonic-gate 	}
30067c478bd9Sstevel@tonic-gate 
30077c478bd9Sstevel@tonic-gate 	/*
30087c478bd9Sstevel@tonic-gate 	 * Open the /proc/<pid>/lwp/<lwpid> files
30097c478bd9Sstevel@tonic-gate 	 */
30109acbbeafSnn35248 	(void) snprintf(procname, sizeof (procname), "%s/%d/lwp/%d/",
30119acbbeafSnn35248 	    procfs_path, (int)P->pid, (int)lwpid);
30127c478bd9Sstevel@tonic-gate 	fname = procname + strlen(procname);
30137c478bd9Sstevel@tonic-gate 	(void) set_minfd();
30147c478bd9Sstevel@tonic-gate 
30157c478bd9Sstevel@tonic-gate 	(void) strcpy(fname, "lwpstatus");
30167c478bd9Sstevel@tonic-gate 	if ((fd = open(procname, O_RDONLY)) < 0 ||
30177c478bd9Sstevel@tonic-gate 	    (fd = dupfd(fd, 0)) < 0) {
30187c478bd9Sstevel@tonic-gate 		switch (errno) {
30197c478bd9Sstevel@tonic-gate 		case ENOENT:
30207c478bd9Sstevel@tonic-gate 			rc = G_NOPROC;
30217c478bd9Sstevel@tonic-gate 			break;
30227c478bd9Sstevel@tonic-gate 		default:
30237c478bd9Sstevel@tonic-gate 			dprintf("Lgrab: failed to open %s: %s\n",
30247c478bd9Sstevel@tonic-gate 			    procname, strerror(errno));
30257c478bd9Sstevel@tonic-gate 			rc = G_STRANGE;
30267c478bd9Sstevel@tonic-gate 			break;
30277c478bd9Sstevel@tonic-gate 		}
30287c478bd9Sstevel@tonic-gate 		goto err;
30297c478bd9Sstevel@tonic-gate 	}
30307c478bd9Sstevel@tonic-gate 	L->lwp_statfd = fd;
30317c478bd9Sstevel@tonic-gate 
30327c478bd9Sstevel@tonic-gate 	if (pread(fd, &L->lwp_status, sizeof (L->lwp_status), (off_t)0) < 0) {
30337c478bd9Sstevel@tonic-gate 		switch (errno) {
30347c478bd9Sstevel@tonic-gate 		case ENOENT:
30357c478bd9Sstevel@tonic-gate 			rc = G_NOPROC;
30367c478bd9Sstevel@tonic-gate 			break;
30377c478bd9Sstevel@tonic-gate 		default:
30387c478bd9Sstevel@tonic-gate 			dprintf("Lgrab: failed to read %s: %s\n",
30397c478bd9Sstevel@tonic-gate 			    procname, strerror(errno));
30407c478bd9Sstevel@tonic-gate 			rc = G_STRANGE;
30417c478bd9Sstevel@tonic-gate 			break;
30427c478bd9Sstevel@tonic-gate 		}
30437c478bd9Sstevel@tonic-gate 		goto err;
30447c478bd9Sstevel@tonic-gate 	}
30457c478bd9Sstevel@tonic-gate 
30467c478bd9Sstevel@tonic-gate 	(void) strcpy(fname, "lwpctl");
30477c478bd9Sstevel@tonic-gate 	if ((fd = open(procname, O_WRONLY)) < 0 ||
30487c478bd9Sstevel@tonic-gate 	    (fd = dupfd(fd, 0)) < 0) {
30497c478bd9Sstevel@tonic-gate 		switch (errno) {
30507c478bd9Sstevel@tonic-gate 		case ENOENT:
30517c478bd9Sstevel@tonic-gate 			rc = G_NOPROC;
30527c478bd9Sstevel@tonic-gate 			break;
30537c478bd9Sstevel@tonic-gate 		default:
30547c478bd9Sstevel@tonic-gate 			dprintf("Lgrab: failed to open %s: %s\n",
30557c478bd9Sstevel@tonic-gate 			    procname, strerror(errno));
30567c478bd9Sstevel@tonic-gate 			rc = G_STRANGE;
30577c478bd9Sstevel@tonic-gate 			break;
30587c478bd9Sstevel@tonic-gate 		}
30597c478bd9Sstevel@tonic-gate 		goto err;
30607c478bd9Sstevel@tonic-gate 	}
30617c478bd9Sstevel@tonic-gate 	L->lwp_ctlfd = fd;
30627c478bd9Sstevel@tonic-gate 
30637c478bd9Sstevel@tonic-gate 	L->lwp_state =
30647c478bd9Sstevel@tonic-gate 	    ((L->lwp_status.pr_flags & (PR_STOPPED|PR_ISTOP))
30657c478bd9Sstevel@tonic-gate 	    == (PR_STOPPED|PR_ISTOP))?
30667c478bd9Sstevel@tonic-gate 	    PS_STOP : PS_RUN;
30677c478bd9Sstevel@tonic-gate 
30687c478bd9Sstevel@tonic-gate 	*perr = 0;
30697c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&P->proc_lock);
30707c478bd9Sstevel@tonic-gate 	return (L);
30717c478bd9Sstevel@tonic-gate 
30727c478bd9Sstevel@tonic-gate err:
30737c478bd9Sstevel@tonic-gate 	Lfree_internal(P, L);
30747c478bd9Sstevel@tonic-gate 	*perr = rc;
30757c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&P->proc_lock);
30767c478bd9Sstevel@tonic-gate 	return (NULL);
30777c478bd9Sstevel@tonic-gate }
30787c478bd9Sstevel@tonic-gate 
30797c478bd9Sstevel@tonic-gate /*
30807c478bd9Sstevel@tonic-gate  * Return a printable string corresponding to an Lgrab() error return.
30817c478bd9Sstevel@tonic-gate  */
30827c478bd9Sstevel@tonic-gate const char *
30837c478bd9Sstevel@tonic-gate Lgrab_error(int error)
30847c478bd9Sstevel@tonic-gate {
30857c478bd9Sstevel@tonic-gate 	const char *str;
30867c478bd9Sstevel@tonic-gate 
30877c478bd9Sstevel@tonic-gate 	switch (error) {
30887c478bd9Sstevel@tonic-gate 	case G_NOPROC:
30897c478bd9Sstevel@tonic-gate 		str = "no such LWP";
30907c478bd9Sstevel@tonic-gate 		break;
30917c478bd9Sstevel@tonic-gate 	case G_BUSY:
30927c478bd9Sstevel@tonic-gate 		str = "LWP already grabbed";
30937c478bd9Sstevel@tonic-gate 		break;
30947c478bd9Sstevel@tonic-gate 	case G_STRANGE:
30957c478bd9Sstevel@tonic-gate 		str = "unanticipated system error";
30967c478bd9Sstevel@tonic-gate 		break;
30977c478bd9Sstevel@tonic-gate 	default:
30987c478bd9Sstevel@tonic-gate 		str = "unknown error";
30997c478bd9Sstevel@tonic-gate 		break;
31007c478bd9Sstevel@tonic-gate 	}
31017c478bd9Sstevel@tonic-gate 
31027c478bd9Sstevel@tonic-gate 	return (str);
31037c478bd9Sstevel@tonic-gate }
31047c478bd9Sstevel@tonic-gate 
31057c478bd9Sstevel@tonic-gate /*
31067c478bd9Sstevel@tonic-gate  * Free an LWP control structure.
31077c478bd9Sstevel@tonic-gate  */
31087c478bd9Sstevel@tonic-gate void
31097c478bd9Sstevel@tonic-gate Lfree(struct ps_lwphandle *L)
31107c478bd9Sstevel@tonic-gate {
31117c478bd9Sstevel@tonic-gate 	struct ps_prochandle *P = L->lwp_proc;
31127c478bd9Sstevel@tonic-gate 
31137c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&P->proc_lock);
31147c478bd9Sstevel@tonic-gate 	Lfree_internal(P, L);
31157c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&P->proc_lock);
31167c478bd9Sstevel@tonic-gate }
31177c478bd9Sstevel@tonic-gate 
31187c478bd9Sstevel@tonic-gate static void
31197c478bd9Sstevel@tonic-gate Lfree_internal(struct ps_prochandle *P, struct ps_lwphandle *L)
31207c478bd9Sstevel@tonic-gate {
31217c478bd9Sstevel@tonic-gate 	*Lfind(P, L->lwp_id) = L->lwp_hash;	/* delete from hash table */
31227c478bd9Sstevel@tonic-gate 	if (L->lwp_ctlfd >= 0)
31237c478bd9Sstevel@tonic-gate 		(void) close(L->lwp_ctlfd);
31247c478bd9Sstevel@tonic-gate 	if (L->lwp_statfd >= 0)
31257c478bd9Sstevel@tonic-gate 		(void) close(L->lwp_statfd);
31267c478bd9Sstevel@tonic-gate 
31277c478bd9Sstevel@tonic-gate 	/* clear out the structure as a precaution against reuse */
31287c478bd9Sstevel@tonic-gate 	(void) memset(L, 0, sizeof (*L));
31297c478bd9Sstevel@tonic-gate 	L->lwp_ctlfd = -1;
31307c478bd9Sstevel@tonic-gate 	L->lwp_statfd = -1;
31317c478bd9Sstevel@tonic-gate 
31327c478bd9Sstevel@tonic-gate 	free(L);
31337c478bd9Sstevel@tonic-gate }
31347c478bd9Sstevel@tonic-gate 
31357c478bd9Sstevel@tonic-gate /*
31367c478bd9Sstevel@tonic-gate  * Return the state of the process, one of the PS_* values.
31377c478bd9Sstevel@tonic-gate  */
31387c478bd9Sstevel@tonic-gate int
31397c478bd9Sstevel@tonic-gate Lstate(struct ps_lwphandle *L)
31407c478bd9Sstevel@tonic-gate {
31417c478bd9Sstevel@tonic-gate 	return (L->lwp_state);
31427c478bd9Sstevel@tonic-gate }
31437c478bd9Sstevel@tonic-gate 
31447c478bd9Sstevel@tonic-gate /*
31457c478bd9Sstevel@tonic-gate  * Return the open control file descriptor for the LWP.
31467c478bd9Sstevel@tonic-gate  * Clients must not close this file descriptor, nor use it
31477c478bd9Sstevel@tonic-gate  * after the LWP is freed.
31487c478bd9Sstevel@tonic-gate  */
31497c478bd9Sstevel@tonic-gate int
31507c478bd9Sstevel@tonic-gate Lctlfd(struct ps_lwphandle *L)
31517c478bd9Sstevel@tonic-gate {
31527c478bd9Sstevel@tonic-gate 	return (L->lwp_ctlfd);
31537c478bd9Sstevel@tonic-gate }
31547c478bd9Sstevel@tonic-gate 
31557c478bd9Sstevel@tonic-gate /*
31567c478bd9Sstevel@tonic-gate  * Return a pointer to the LWP lwpsinfo structure.
31577c478bd9Sstevel@tonic-gate  * Clients should not hold on to this pointer indefinitely.
31587c478bd9Sstevel@tonic-gate  * It will become invalid on Lfree().
31597c478bd9Sstevel@tonic-gate  */
31607c478bd9Sstevel@tonic-gate const lwpsinfo_t *
31617c478bd9Sstevel@tonic-gate Lpsinfo(struct ps_lwphandle *L)
31627c478bd9Sstevel@tonic-gate {
31637c478bd9Sstevel@tonic-gate 	if (Plwp_getpsinfo(L->lwp_proc, L->lwp_id, &L->lwp_psinfo) == -1)
31647c478bd9Sstevel@tonic-gate 		return (NULL);
31657c478bd9Sstevel@tonic-gate 
31667c478bd9Sstevel@tonic-gate 	return (&L->lwp_psinfo);
31677c478bd9Sstevel@tonic-gate }
31687c478bd9Sstevel@tonic-gate 
31697c478bd9Sstevel@tonic-gate /*
31707c478bd9Sstevel@tonic-gate  * Return a pointer to the LWP status structure.
31717c478bd9Sstevel@tonic-gate  * Clients should not hold on to this pointer indefinitely.
31727c478bd9Sstevel@tonic-gate  * It will become invalid on Lfree().
31737c478bd9Sstevel@tonic-gate  */
31747c478bd9Sstevel@tonic-gate const lwpstatus_t *
31757c478bd9Sstevel@tonic-gate Lstatus(struct ps_lwphandle *L)
31767c478bd9Sstevel@tonic-gate {
31777c478bd9Sstevel@tonic-gate 	return (&L->lwp_status);
31787c478bd9Sstevel@tonic-gate }
31797c478bd9Sstevel@tonic-gate 
31807c478bd9Sstevel@tonic-gate /*
31817c478bd9Sstevel@tonic-gate  * Given an LWP handle, return the process handle.
31827c478bd9Sstevel@tonic-gate  */
31837c478bd9Sstevel@tonic-gate struct ps_prochandle *
31847c478bd9Sstevel@tonic-gate Lprochandle(struct ps_lwphandle *L)
31857c478bd9Sstevel@tonic-gate {
31867c478bd9Sstevel@tonic-gate 	return (L->lwp_proc);
31877c478bd9Sstevel@tonic-gate }
31887c478bd9Sstevel@tonic-gate 
31897c478bd9Sstevel@tonic-gate /*
31907c478bd9Sstevel@tonic-gate  * Ensure that all cached state is written to the LWP.
31917c478bd9Sstevel@tonic-gate  * The cached state is the LWP's signal mask and registers.
31927c478bd9Sstevel@tonic-gate  */
31937c478bd9Sstevel@tonic-gate void
31947c478bd9Sstevel@tonic-gate Lsync(struct ps_lwphandle *L)
31957c478bd9Sstevel@tonic-gate {
31967c478bd9Sstevel@tonic-gate 	int ctlfd = L->lwp_ctlfd;
31977c478bd9Sstevel@tonic-gate 	long cmd[2];
31987c478bd9Sstevel@tonic-gate 	iovec_t iov[4];
31997c478bd9Sstevel@tonic-gate 	int n = 0;
32007c478bd9Sstevel@tonic-gate 
32017c478bd9Sstevel@tonic-gate 	if (L->lwp_flags & SETHOLD) {
32027c478bd9Sstevel@tonic-gate 		cmd[0] = PCSHOLD;
32037c478bd9Sstevel@tonic-gate 		iov[n].iov_base = (caddr_t)&cmd[0];
32047c478bd9Sstevel@tonic-gate 		iov[n++].iov_len = sizeof (long);
32057c478bd9Sstevel@tonic-gate 		iov[n].iov_base = (caddr_t)&L->lwp_status.pr_lwphold;
32067c478bd9Sstevel@tonic-gate 		iov[n++].iov_len = sizeof (L->lwp_status.pr_lwphold);
32077c478bd9Sstevel@tonic-gate 	}
32087c478bd9Sstevel@tonic-gate 	if (L->lwp_flags & SETREGS) {
32097c478bd9Sstevel@tonic-gate 		cmd[1] = PCSREG;
32107c478bd9Sstevel@tonic-gate 		iov[n].iov_base = (caddr_t)&cmd[1];
32117c478bd9Sstevel@tonic-gate 		iov[n++].iov_len = sizeof (long);
32127c478bd9Sstevel@tonic-gate 		iov[n].iov_base = (caddr_t)&L->lwp_status.pr_reg[0];
32137c478bd9Sstevel@tonic-gate 		iov[n++].iov_len = sizeof (L->lwp_status.pr_reg);
32147c478bd9Sstevel@tonic-gate 	}
32157c478bd9Sstevel@tonic-gate 
32167c478bd9Sstevel@tonic-gate 	if (n == 0 || writev(ctlfd, iov, n) < 0)
32177c478bd9Sstevel@tonic-gate 		return;		/* nothing to do or write failed */
32187c478bd9Sstevel@tonic-gate 
32197c478bd9Sstevel@tonic-gate 	L->lwp_flags &= ~(SETHOLD|SETREGS);
32207c478bd9Sstevel@tonic-gate }
32217c478bd9Sstevel@tonic-gate 
32227c478bd9Sstevel@tonic-gate /*
32237c478bd9Sstevel@tonic-gate  * Wait for the specified LWP to stop or terminate.
32247c478bd9Sstevel@tonic-gate  * Or, just get the current status (PCNULL).
32257c478bd9Sstevel@tonic-gate  * Or, direct it to stop and get the current status (PCDSTOP).
32267c478bd9Sstevel@tonic-gate  */
32277c478bd9Sstevel@tonic-gate static int
32287c478bd9Sstevel@tonic-gate Lstopstatus(struct ps_lwphandle *L,
32297c478bd9Sstevel@tonic-gate 	long request,		/* PCNULL, PCDSTOP, PCSTOP, PCWSTOP */
32307c478bd9Sstevel@tonic-gate 	uint_t msec)		/* if non-zero, timeout in milliseconds */
32317c478bd9Sstevel@tonic-gate {
32327c478bd9Sstevel@tonic-gate 	int ctlfd = L->lwp_ctlfd;
32337c478bd9Sstevel@tonic-gate 	long ctl[3];
32347c478bd9Sstevel@tonic-gate 	ssize_t rc;
32357c478bd9Sstevel@tonic-gate 	int err;
32367c478bd9Sstevel@tonic-gate 
32377c478bd9Sstevel@tonic-gate 	switch (L->lwp_state) {
32387c478bd9Sstevel@tonic-gate 	case PS_RUN:
32397c478bd9Sstevel@tonic-gate 		break;
32407c478bd9Sstevel@tonic-gate 	case PS_STOP:
32417c478bd9Sstevel@tonic-gate 		if (request != PCNULL && request != PCDSTOP)
32427c478bd9Sstevel@tonic-gate 			return (0);
32437c478bd9Sstevel@tonic-gate 		break;
32447c478bd9Sstevel@tonic-gate 	case PS_LOST:
32457c478bd9Sstevel@tonic-gate 		if (request != PCNULL) {
32467c478bd9Sstevel@tonic-gate 			errno = EAGAIN;
32477c478bd9Sstevel@tonic-gate 			return (-1);
32487c478bd9Sstevel@tonic-gate 		}
32497c478bd9Sstevel@tonic-gate 		break;
32507c478bd9Sstevel@tonic-gate 	case PS_UNDEAD:
32517c478bd9Sstevel@tonic-gate 	case PS_DEAD:
32527c478bd9Sstevel@tonic-gate 		if (request != PCNULL) {
32537c478bd9Sstevel@tonic-gate 			errno = ENOENT;
32547c478bd9Sstevel@tonic-gate 			return (-1);
32557c478bd9Sstevel@tonic-gate 		}
32567c478bd9Sstevel@tonic-gate 		break;
32577c478bd9Sstevel@tonic-gate 	default:	/* corrupted state */
32587c478bd9Sstevel@tonic-gate 		dprintf("Lstopstatus: corrupted state: %d\n", L->lwp_state);
32597c478bd9Sstevel@tonic-gate 		errno = EINVAL;
32607c478bd9Sstevel@tonic-gate 		return (-1);
32617c478bd9Sstevel@tonic-gate 	}
32627c478bd9Sstevel@tonic-gate 
32637c478bd9Sstevel@tonic-gate 	ctl[0] = PCDSTOP;
32647c478bd9Sstevel@tonic-gate 	ctl[1] = PCTWSTOP;
32657c478bd9Sstevel@tonic-gate 	ctl[2] = (long)msec;
32667c478bd9Sstevel@tonic-gate 	rc = 0;
32677c478bd9Sstevel@tonic-gate 	switch (request) {
32687c478bd9Sstevel@tonic-gate 	case PCSTOP:
32697c478bd9Sstevel@tonic-gate 		rc = write(ctlfd, &ctl[0], 3*sizeof (long));
32707c478bd9Sstevel@tonic-gate 		break;
32717c478bd9Sstevel@tonic-gate 	case PCWSTOP:
32727c478bd9Sstevel@tonic-gate 		rc = write(ctlfd, &ctl[1], 2*sizeof (long));
32737c478bd9Sstevel@tonic-gate 		break;
32747c478bd9Sstevel@tonic-gate 	case PCDSTOP:
32757c478bd9Sstevel@tonic-gate 		rc = write(ctlfd, &ctl[0], 1*sizeof (long));
32767c478bd9Sstevel@tonic-gate 		break;
32777c478bd9Sstevel@tonic-gate 	case PCNULL:
32787c478bd9Sstevel@tonic-gate 		if (L->lwp_state == PS_DEAD)
32797c478bd9Sstevel@tonic-gate 			return (0); /* Nothing else to do for cores */
32807c478bd9Sstevel@tonic-gate 		break;
32817c478bd9Sstevel@tonic-gate 	default:	/* programming error */
32827c478bd9Sstevel@tonic-gate 		errno = EINVAL;
32837c478bd9Sstevel@tonic-gate 		return (-1);
32847c478bd9Sstevel@tonic-gate 	}
32857c478bd9Sstevel@tonic-gate 	err = (rc < 0)? errno : 0;
32867c478bd9Sstevel@tonic-gate 	Lsync(L);
32877c478bd9Sstevel@tonic-gate 
32887c478bd9Sstevel@tonic-gate 	if (pread(L->lwp_statfd, &L->lwp_status,
32897c478bd9Sstevel@tonic-gate 	    sizeof (L->lwp_status), (off_t)0) < 0)
32907c478bd9Sstevel@tonic-gate 		err = errno;
32917c478bd9Sstevel@tonic-gate 
32927c478bd9Sstevel@tonic-gate 	if (err) {
32937c478bd9Sstevel@tonic-gate 		switch (err) {
32947c478bd9Sstevel@tonic-gate 		case EINTR:		/* user typed ctl-C */
32957c478bd9Sstevel@tonic-gate 		case ERESTART:
32967c478bd9Sstevel@tonic-gate 			dprintf("Lstopstatus: EINTR\n");
32977c478bd9Sstevel@tonic-gate 			break;
32987c478bd9Sstevel@tonic-gate 		case EAGAIN:		/* we lost control of the the process */
32997c478bd9Sstevel@tonic-gate 			dprintf("Lstopstatus: EAGAIN\n");
33007c478bd9Sstevel@tonic-gate 			L->lwp_state = PS_LOST;
33017c478bd9Sstevel@tonic-gate 			errno = err;
33027c478bd9Sstevel@tonic-gate 			return (-1);
33037c478bd9Sstevel@tonic-gate 		default:
33047c478bd9Sstevel@tonic-gate 			if (_libproc_debug) {
33057c478bd9Sstevel@tonic-gate 				const char *errstr;
33067c478bd9Sstevel@tonic-gate 
33077c478bd9Sstevel@tonic-gate 				switch (request) {
33087c478bd9Sstevel@tonic-gate 				case PCNULL:
33097c478bd9Sstevel@tonic-gate 					errstr = "Lstopstatus PCNULL"; break;
33107c478bd9Sstevel@tonic-gate 				case PCSTOP:
33117c478bd9Sstevel@tonic-gate 					errstr = "Lstopstatus PCSTOP"; break;
33127c478bd9Sstevel@tonic-gate 				case PCDSTOP:
33137c478bd9Sstevel@tonic-gate 					errstr = "Lstopstatus PCDSTOP"; break;
33147c478bd9Sstevel@tonic-gate 				case PCWSTOP:
33157c478bd9Sstevel@tonic-gate 					errstr = "Lstopstatus PCWSTOP"; break;
33167c478bd9Sstevel@tonic-gate 				default:
33177c478bd9Sstevel@tonic-gate 					errstr = "Lstopstatus PC???"; break;
33187c478bd9Sstevel@tonic-gate 				}
33197c478bd9Sstevel@tonic-gate 				dprintf("%s: %s\n", errstr, strerror(err));
33207c478bd9Sstevel@tonic-gate 			}
33217c478bd9Sstevel@tonic-gate 			L->lwp_state = PS_UNDEAD;
33227c478bd9Sstevel@tonic-gate 			errno = err;
33237c478bd9Sstevel@tonic-gate 			return (-1);
33247c478bd9Sstevel@tonic-gate 		}
33257c478bd9Sstevel@tonic-gate 	}
33267c478bd9Sstevel@tonic-gate 
33277c478bd9Sstevel@tonic-gate 	if ((L->lwp_status.pr_flags & (PR_STOPPED|PR_ISTOP))
33287c478bd9Sstevel@tonic-gate 	    != (PR_STOPPED|PR_ISTOP)) {
33297c478bd9Sstevel@tonic-gate 		L->lwp_state = PS_RUN;
33307c478bd9Sstevel@tonic-gate 		if (request == PCNULL || request == PCDSTOP || msec != 0)
33317c478bd9Sstevel@tonic-gate 			return (0);
33327c478bd9Sstevel@tonic-gate 		dprintf("Lstopstatus: LWP is not stopped\n");
33337c478bd9Sstevel@tonic-gate 		errno = EPROTO;
33347c478bd9Sstevel@tonic-gate 		return (-1);
33357c478bd9Sstevel@tonic-gate 	}
33367c478bd9Sstevel@tonic-gate 
33377c478bd9Sstevel@tonic-gate 	L->lwp_state = PS_STOP;
33387c478bd9Sstevel@tonic-gate 
33397c478bd9Sstevel@tonic-gate 	if (_libproc_debug)	/* debugging */
33407c478bd9Sstevel@tonic-gate 		prldump("Lstopstatus", &L->lwp_status);
33417c478bd9Sstevel@tonic-gate 
33427c478bd9Sstevel@tonic-gate 	switch (L->lwp_status.pr_why) {
33437c478bd9Sstevel@tonic-gate 	case PR_SYSENTRY:
33447c478bd9Sstevel@tonic-gate 	case PR_SYSEXIT:
33457c478bd9Sstevel@tonic-gate 	case PR_REQUESTED:
33467c478bd9Sstevel@tonic-gate 	case PR_SIGNALLED:
33477c478bd9Sstevel@tonic-gate 	case PR_FAULTED:
33487c478bd9Sstevel@tonic-gate 	case PR_JOBCONTROL:
33497c478bd9Sstevel@tonic-gate 	case PR_SUSPENDED:
33507c478bd9Sstevel@tonic-gate 		break;
33517c478bd9Sstevel@tonic-gate 	default:
33527c478bd9Sstevel@tonic-gate 		errno = EPROTO;
33537c478bd9Sstevel@tonic-gate 		return (-1);
33547c478bd9Sstevel@tonic-gate 	}
33557c478bd9Sstevel@tonic-gate 
33567c478bd9Sstevel@tonic-gate 	return (0);
33577c478bd9Sstevel@tonic-gate }
33587c478bd9Sstevel@tonic-gate 
33597c478bd9Sstevel@tonic-gate /*
33607c478bd9Sstevel@tonic-gate  * Wait for the LWP to stop for any reason.
33617c478bd9Sstevel@tonic-gate  */
33627c478bd9Sstevel@tonic-gate int
33637c478bd9Sstevel@tonic-gate Lwait(struct ps_lwphandle *L, uint_t msec)
33647c478bd9Sstevel@tonic-gate {
33657c478bd9Sstevel@tonic-gate 	return (Lstopstatus(L, PCWSTOP, msec));
33667c478bd9Sstevel@tonic-gate }
33677c478bd9Sstevel@tonic-gate 
33687c478bd9Sstevel@tonic-gate /*
33697c478bd9Sstevel@tonic-gate  * Direct the LWP to stop; wait for it to stop.
33707c478bd9Sstevel@tonic-gate  */
33717c478bd9Sstevel@tonic-gate int
33727c478bd9Sstevel@tonic-gate Lstop(struct ps_lwphandle *L, uint_t msec)
33737c478bd9Sstevel@tonic-gate {
33747c478bd9Sstevel@tonic-gate 	return (Lstopstatus(L, PCSTOP, msec));
33757c478bd9Sstevel@tonic-gate }
33767c478bd9Sstevel@tonic-gate 
33777c478bd9Sstevel@tonic-gate /*
33787c478bd9Sstevel@tonic-gate  * Direct the LWP to stop; don't wait.
33797c478bd9Sstevel@tonic-gate  */
33807c478bd9Sstevel@tonic-gate int
33817c478bd9Sstevel@tonic-gate Ldstop(struct ps_lwphandle *L)
33827c478bd9Sstevel@tonic-gate {
33837c478bd9Sstevel@tonic-gate 	return (Lstopstatus(L, PCDSTOP, 0));
33847c478bd9Sstevel@tonic-gate }
33857c478bd9Sstevel@tonic-gate 
33867c478bd9Sstevel@tonic-gate /*
33877c478bd9Sstevel@tonic-gate  * Get the value of one register from stopped LWP.
33887c478bd9Sstevel@tonic-gate  */
33897c478bd9Sstevel@tonic-gate int
33907c478bd9Sstevel@tonic-gate Lgetareg(struct ps_lwphandle *L, int regno, prgreg_t *preg)
33917c478bd9Sstevel@tonic-gate {
33927c478bd9Sstevel@tonic-gate 	if (regno < 0 || regno >= NPRGREG) {
33937c478bd9Sstevel@tonic-gate 		errno = EINVAL;
33947c478bd9Sstevel@tonic-gate 		return (-1);
33957c478bd9Sstevel@tonic-gate 	}
33967c478bd9Sstevel@tonic-gate 
33977c478bd9Sstevel@tonic-gate 	if (L->lwp_state != PS_STOP) {
33987c478bd9Sstevel@tonic-gate 		errno = EBUSY;
33997c478bd9Sstevel@tonic-gate 		return (-1);
34007c478bd9Sstevel@tonic-gate 	}
34017c478bd9Sstevel@tonic-gate 
34027c478bd9Sstevel@tonic-gate 	*preg = L->lwp_status.pr_reg[regno];
34037c478bd9Sstevel@tonic-gate 	return (0);
34047c478bd9Sstevel@tonic-gate }
34057c478bd9Sstevel@tonic-gate 
34067c478bd9Sstevel@tonic-gate /*
34077c478bd9Sstevel@tonic-gate  * Put value of one register into stopped LWP.
34087c478bd9Sstevel@tonic-gate  */
34097c478bd9Sstevel@tonic-gate int
34107c478bd9Sstevel@tonic-gate Lputareg(struct ps_lwphandle *L, int regno, prgreg_t reg)
34117c478bd9Sstevel@tonic-gate {
34127c478bd9Sstevel@tonic-gate 	if (regno < 0 || regno >= NPRGREG) {
34137c478bd9Sstevel@tonic-gate 		errno = EINVAL;
34147c478bd9Sstevel@tonic-gate 		return (-1);
34157c478bd9Sstevel@tonic-gate 	}
34167c478bd9Sstevel@tonic-gate 
34177c478bd9Sstevel@tonic-gate 	if (L->lwp_state != PS_STOP) {
34187c478bd9Sstevel@tonic-gate 		errno = EBUSY;
34197c478bd9Sstevel@tonic-gate 		return (-1);
34207c478bd9Sstevel@tonic-gate 	}
34217c478bd9Sstevel@tonic-gate 
34227c478bd9Sstevel@tonic-gate 	L->lwp_status.pr_reg[regno] = reg;
34237c478bd9Sstevel@tonic-gate 	L->lwp_flags |= SETREGS;	/* set registers before continuing */
34247c478bd9Sstevel@tonic-gate 	return (0);
34257c478bd9Sstevel@tonic-gate }
34267c478bd9Sstevel@tonic-gate 
34277c478bd9Sstevel@tonic-gate int
34287c478bd9Sstevel@tonic-gate Lsetrun(struct ps_lwphandle *L,
34297c478bd9Sstevel@tonic-gate 	int sig,	/* signal to pass to LWP */
34307c478bd9Sstevel@tonic-gate 	int flags)	/* PRSTEP|PRSABORT|PRSTOP|PRCSIG|PRCFAULT */
34317c478bd9Sstevel@tonic-gate {
34327c478bd9Sstevel@tonic-gate 	int ctlfd = L->lwp_ctlfd;
34337c478bd9Sstevel@tonic-gate 	int sbits = (PR_DSTOP | PR_ISTOP | PR_ASLEEP);
34347c478bd9Sstevel@tonic-gate 
34357c478bd9Sstevel@tonic-gate 	long ctl[1 +					/* PCCFAULT	*/
34367c478bd9Sstevel@tonic-gate 	    1 + sizeof (siginfo_t)/sizeof (long) +	/* PCSSIG/PCCSIG */
34377c478bd9Sstevel@tonic-gate 	    2 ];					/* PCRUN	*/
34387c478bd9Sstevel@tonic-gate 
34397c478bd9Sstevel@tonic-gate 	long *ctlp = ctl;
34407c478bd9Sstevel@tonic-gate 	size_t size;
34417c478bd9Sstevel@tonic-gate 
34427c478bd9Sstevel@tonic-gate 	if (L->lwp_state != PS_STOP &&
34437c478bd9Sstevel@tonic-gate 	    (L->lwp_status.pr_flags & sbits) == 0) {
34447c478bd9Sstevel@tonic-gate 		errno = EBUSY;
34457c478bd9Sstevel@tonic-gate 		return (-1);
34467c478bd9Sstevel@tonic-gate 	}
34477c478bd9Sstevel@tonic-gate 
34487c478bd9Sstevel@tonic-gate 	Lsync(L);	/* flush registers */
34497c478bd9Sstevel@tonic-gate 
34507c478bd9Sstevel@tonic-gate 	if (flags & PRCFAULT) {		/* clear current fault */
34517c478bd9Sstevel@tonic-gate 		*ctlp++ = PCCFAULT;
34527c478bd9Sstevel@tonic-gate 		flags &= ~PRCFAULT;
34537c478bd9Sstevel@tonic-gate 	}
34547c478bd9Sstevel@tonic-gate 
34557c478bd9Sstevel@tonic-gate 	if (flags & PRCSIG) {		/* clear current signal */
34567c478bd9Sstevel@tonic-gate 		*ctlp++ = PCCSIG;
34577c478bd9Sstevel@tonic-gate 		flags &= ~PRCSIG;
34587c478bd9Sstevel@tonic-gate 	} else if (sig && sig != L->lwp_status.pr_cursig) {
34597c478bd9Sstevel@tonic-gate 		/* make current signal */
34607c478bd9Sstevel@tonic-gate 		siginfo_t *infop;
34617c478bd9Sstevel@tonic-gate 
34627c478bd9Sstevel@tonic-gate 		*ctlp++ = PCSSIG;
34637c478bd9Sstevel@tonic-gate 		infop = (siginfo_t *)ctlp;
34647c478bd9Sstevel@tonic-gate 		(void) memset(infop, 0, sizeof (*infop));
34657c478bd9Sstevel@tonic-gate 		infop->si_signo = sig;
34667c478bd9Sstevel@tonic-gate 		ctlp += sizeof (siginfo_t) / sizeof (long);
34677c478bd9Sstevel@tonic-gate 	}
34687c478bd9Sstevel@tonic-gate 
34697c478bd9Sstevel@tonic-gate 	*ctlp++ = PCRUN;
34707c478bd9Sstevel@tonic-gate 	*ctlp++ = flags;
34717c478bd9Sstevel@tonic-gate 	size = (char *)ctlp - (char *)ctl;
34727c478bd9Sstevel@tonic-gate 
34737c478bd9Sstevel@tonic-gate 	L->lwp_proc->info_valid = 0; /* will need to update map and file info */
34747c478bd9Sstevel@tonic-gate 	L->lwp_proc->state = PS_RUN;
34757c478bd9Sstevel@tonic-gate 	L->lwp_state = PS_RUN;
34767c478bd9Sstevel@tonic-gate 
34777c478bd9Sstevel@tonic-gate 	if (write(ctlfd, ctl, size) != size) {
34787c478bd9Sstevel@tonic-gate 		/* Pretend that a job-stopped LWP is running */
34797c478bd9Sstevel@tonic-gate 		if (errno != EBUSY || L->lwp_status.pr_why != PR_JOBCONTROL)
34807c478bd9Sstevel@tonic-gate 			return (Lstopstatus(L, PCNULL, 0));
34817c478bd9Sstevel@tonic-gate 	}
34827c478bd9Sstevel@tonic-gate 
34837c478bd9Sstevel@tonic-gate 	return (0);
34847c478bd9Sstevel@tonic-gate }
34857c478bd9Sstevel@tonic-gate 
34867c478bd9Sstevel@tonic-gate int
34877c478bd9Sstevel@tonic-gate Lclearsig(struct ps_lwphandle *L)
34887c478bd9Sstevel@tonic-gate {
34897c478bd9Sstevel@tonic-gate 	int ctlfd = L->lwp_ctlfd;
34907c478bd9Sstevel@tonic-gate 	long ctl = PCCSIG;
34917c478bd9Sstevel@tonic-gate 
34927c478bd9Sstevel@tonic-gate 	if (write(ctlfd, &ctl, sizeof (ctl)) != sizeof (ctl))
34937c478bd9Sstevel@tonic-gate 		return (-1);
34947c478bd9Sstevel@tonic-gate 	L->lwp_status.pr_cursig = 0;
34957c478bd9Sstevel@tonic-gate 	return (0);
34967c478bd9Sstevel@tonic-gate }
34977c478bd9Sstevel@tonic-gate 
34987c478bd9Sstevel@tonic-gate int
34997c478bd9Sstevel@tonic-gate Lclearfault(struct ps_lwphandle *L)
35007c478bd9Sstevel@tonic-gate {
35017c478bd9Sstevel@tonic-gate 	int ctlfd = L->lwp_ctlfd;
35027c478bd9Sstevel@tonic-gate 	long ctl = PCCFAULT;
35037c478bd9Sstevel@tonic-gate 
35047c478bd9Sstevel@tonic-gate 	if (write(ctlfd, &ctl, sizeof (ctl)) != sizeof (ctl))
35057c478bd9Sstevel@tonic-gate 		return (-1);
35067c478bd9Sstevel@tonic-gate 	return (0);
35077c478bd9Sstevel@tonic-gate }
35087c478bd9Sstevel@tonic-gate 
35097c478bd9Sstevel@tonic-gate /*
35107c478bd9Sstevel@tonic-gate  * Step over a breakpoint, i.e., execute the instruction that
35117c478bd9Sstevel@tonic-gate  * really belongs at the breakpoint location (the current %pc)
35127c478bd9Sstevel@tonic-gate  * and leave the LWP stopped at the next instruction.
35137c478bd9Sstevel@tonic-gate  */
35147c478bd9Sstevel@tonic-gate int
35157c478bd9Sstevel@tonic-gate Lxecbkpt(struct ps_lwphandle *L, ulong_t saved)
35167c478bd9Sstevel@tonic-gate {
35177c478bd9Sstevel@tonic-gate 	struct ps_prochandle *P = L->lwp_proc;
35187c478bd9Sstevel@tonic-gate 	int rv, error;
35197c478bd9Sstevel@tonic-gate 
35207c478bd9Sstevel@tonic-gate 	if (L->lwp_state != PS_STOP) {
35217c478bd9Sstevel@tonic-gate 		errno = EBUSY;
35227c478bd9Sstevel@tonic-gate 		return (-1);
35237c478bd9Sstevel@tonic-gate 	}
35247c478bd9Sstevel@tonic-gate 
35257c478bd9Sstevel@tonic-gate 	Lsync(L);
35267c478bd9Sstevel@tonic-gate 	error = execute_bkpt(L->lwp_ctlfd,
35277c478bd9Sstevel@tonic-gate 	    &P->status.pr_flttrace, &L->lwp_status.pr_lwphold,
35287c478bd9Sstevel@tonic-gate 	    L->lwp_status.pr_reg[R_PC], saved);
35297c478bd9Sstevel@tonic-gate 	rv = Lstopstatus(L, PCNULL, 0);
35307c478bd9Sstevel@tonic-gate 
35317c478bd9Sstevel@tonic-gate 	if (error != 0) {
35327c478bd9Sstevel@tonic-gate 		if (L->lwp_status.pr_why == PR_JOBCONTROL &&
35337c478bd9Sstevel@tonic-gate 		    error == EBUSY) {	/* jobcontrol stop -- back off */
35347c478bd9Sstevel@tonic-gate 			L->lwp_state = PS_RUN;
35357c478bd9Sstevel@tonic-gate 			return (0);
35367c478bd9Sstevel@tonic-gate 		}
35377c478bd9Sstevel@tonic-gate 		if (error == ENOENT)
35387c478bd9Sstevel@tonic-gate 			return (0);
35397c478bd9Sstevel@tonic-gate 		errno = error;
35407c478bd9Sstevel@tonic-gate 		return (-1);
35417c478bd9Sstevel@tonic-gate 	}
35427c478bd9Sstevel@tonic-gate 
35437c478bd9Sstevel@tonic-gate 	return (rv);
35447c478bd9Sstevel@tonic-gate }
35457c478bd9Sstevel@tonic-gate 
35467c478bd9Sstevel@tonic-gate /*
35477c478bd9Sstevel@tonic-gate  * Step over a watchpoint, i.e., execute the instruction that was stopped by
35487c478bd9Sstevel@tonic-gate  * the watchpoint, and then leave the LWP stopped at the next instruction.
35497c478bd9Sstevel@tonic-gate  */
35507c478bd9Sstevel@tonic-gate int
35517c478bd9Sstevel@tonic-gate Lxecwapt(struct ps_lwphandle *L, const prwatch_t *wp)
35527c478bd9Sstevel@tonic-gate {
35537c478bd9Sstevel@tonic-gate 	struct ps_prochandle *P = L->lwp_proc;
35547c478bd9Sstevel@tonic-gate 	int rv, error;
35557c478bd9Sstevel@tonic-gate 
35567c478bd9Sstevel@tonic-gate 	if (L->lwp_state != PS_STOP) {
35577c478bd9Sstevel@tonic-gate 		errno = EBUSY;
35587c478bd9Sstevel@tonic-gate 		return (-1);
35597c478bd9Sstevel@tonic-gate 	}
35607c478bd9Sstevel@tonic-gate 
35617c478bd9Sstevel@tonic-gate 	Lsync(L);
35627c478bd9Sstevel@tonic-gate 	error = execute_wapt(L->lwp_ctlfd,
35637c478bd9Sstevel@tonic-gate 	    &P->status.pr_flttrace, &L->lwp_status.pr_lwphold, wp);
35647c478bd9Sstevel@tonic-gate 	rv = Lstopstatus(L, PCNULL, 0);
35657c478bd9Sstevel@tonic-gate 
35667c478bd9Sstevel@tonic-gate 	if (error != 0) {
35677c478bd9Sstevel@tonic-gate 		if (L->lwp_status.pr_why == PR_JOBCONTROL &&
35687c478bd9Sstevel@tonic-gate 		    error == EBUSY) {	/* jobcontrol stop -- back off */
35697c478bd9Sstevel@tonic-gate 			L->lwp_state = PS_RUN;
35707c478bd9Sstevel@tonic-gate 			return (0);
35717c478bd9Sstevel@tonic-gate 		}
35727c478bd9Sstevel@tonic-gate 		if (error == ENOENT)
35737c478bd9Sstevel@tonic-gate 			return (0);
35747c478bd9Sstevel@tonic-gate 		errno = error;
35757c478bd9Sstevel@tonic-gate 		return (-1);
35767c478bd9Sstevel@tonic-gate 	}
35777c478bd9Sstevel@tonic-gate 
35787c478bd9Sstevel@tonic-gate 	return (rv);
35797c478bd9Sstevel@tonic-gate }
35807c478bd9Sstevel@tonic-gate 
35817c478bd9Sstevel@tonic-gate int
35827c478bd9Sstevel@tonic-gate Lstack(struct ps_lwphandle *L, stack_t *stkp)
35837c478bd9Sstevel@tonic-gate {
35847c478bd9Sstevel@tonic-gate 	struct ps_prochandle *P = L->lwp_proc;
35857c478bd9Sstevel@tonic-gate 	uintptr_t addr = L->lwp_status.pr_ustack;
35867c478bd9Sstevel@tonic-gate 
35877c478bd9Sstevel@tonic-gate 	if (P->status.pr_dmodel == PR_MODEL_NATIVE) {
35887c478bd9Sstevel@tonic-gate 		if (Pread(P, stkp, sizeof (*stkp), addr) != sizeof (*stkp))
35897c478bd9Sstevel@tonic-gate 			return (-1);
35907c478bd9Sstevel@tonic-gate #ifdef _LP64
35917c478bd9Sstevel@tonic-gate 	} else {
35927c478bd9Sstevel@tonic-gate 		stack32_t stk32;
35937c478bd9Sstevel@tonic-gate 
35947c478bd9Sstevel@tonic-gate 		if (Pread(P, &stk32, sizeof (stk32), addr) != sizeof (stk32))
35957c478bd9Sstevel@tonic-gate 			return (-1);
35967c478bd9Sstevel@tonic-gate 
35977c478bd9Sstevel@tonic-gate 		stack_32_to_n(&stk32, stkp);
35987c478bd9Sstevel@tonic-gate #endif
35997c478bd9Sstevel@tonic-gate 	}
36007c478bd9Sstevel@tonic-gate 
36017c478bd9Sstevel@tonic-gate 	return (0);
36027c478bd9Sstevel@tonic-gate }
36037c478bd9Sstevel@tonic-gate 
36047c478bd9Sstevel@tonic-gate int
36057c478bd9Sstevel@tonic-gate Lmain_stack(struct ps_lwphandle *L, stack_t *stkp)
36067c478bd9Sstevel@tonic-gate {
36077c478bd9Sstevel@tonic-gate 	struct ps_prochandle *P = L->lwp_proc;
36087c478bd9Sstevel@tonic-gate 
36097c478bd9Sstevel@tonic-gate 	if (Lstack(L, stkp) != 0)
36107c478bd9Sstevel@tonic-gate 		return (-1);
36117c478bd9Sstevel@tonic-gate 
36127c478bd9Sstevel@tonic-gate 	/*
36137c478bd9Sstevel@tonic-gate 	 * If the SS_ONSTACK flag is set then this LWP is operating on the
36147c478bd9Sstevel@tonic-gate 	 * alternate signal stack. We can recover the original stack from
36157c478bd9Sstevel@tonic-gate 	 * pr_oldcontext.
36167c478bd9Sstevel@tonic-gate 	 */
36177c478bd9Sstevel@tonic-gate 	if (!(stkp->ss_flags & SS_ONSTACK))
36187c478bd9Sstevel@tonic-gate 		return (0);
36197c478bd9Sstevel@tonic-gate 
36207c478bd9Sstevel@tonic-gate 	if (P->status.pr_dmodel == PR_MODEL_NATIVE) {
36217c478bd9Sstevel@tonic-gate 		ucontext_t *ctxp = (void *)L->lwp_status.pr_oldcontext;
36227c478bd9Sstevel@tonic-gate 
36237c478bd9Sstevel@tonic-gate 		if (Pread(P, stkp, sizeof (*stkp),
36247c478bd9Sstevel@tonic-gate 		    (uintptr_t)&ctxp->uc_stack) != sizeof (*stkp))
36257c478bd9Sstevel@tonic-gate 			return (-1);
36267c478bd9Sstevel@tonic-gate #ifdef _LP64
36277c478bd9Sstevel@tonic-gate 	} else {
36287c478bd9Sstevel@tonic-gate 		ucontext32_t *ctxp = (void *)L->lwp_status.pr_oldcontext;
36297c478bd9Sstevel@tonic-gate 		stack32_t stk32;
36307c478bd9Sstevel@tonic-gate 
36317c478bd9Sstevel@tonic-gate 		if (Pread(P, &stk32, sizeof (stk32),
36327c478bd9Sstevel@tonic-gate 		    (uintptr_t)&ctxp->uc_stack) != sizeof (stk32))
36337c478bd9Sstevel@tonic-gate 			return (-1);
36347c478bd9Sstevel@tonic-gate 
36357c478bd9Sstevel@tonic-gate 		stack_32_to_n(&stk32, stkp);
36367c478bd9Sstevel@tonic-gate #endif
36377c478bd9Sstevel@tonic-gate 	}
36387c478bd9Sstevel@tonic-gate 
36397c478bd9Sstevel@tonic-gate 	return (0);
36407c478bd9Sstevel@tonic-gate }
36417c478bd9Sstevel@tonic-gate 
36427c478bd9Sstevel@tonic-gate int
36437c478bd9Sstevel@tonic-gate Lalt_stack(struct ps_lwphandle *L, stack_t *stkp)
36447c478bd9Sstevel@tonic-gate {
36457c478bd9Sstevel@tonic-gate 	if (L->lwp_status.pr_altstack.ss_flags & SS_DISABLE) {
36467c478bd9Sstevel@tonic-gate 		errno = ENODATA;
36477c478bd9Sstevel@tonic-gate 		return (-1);
36487c478bd9Sstevel@tonic-gate 	}
36497c478bd9Sstevel@tonic-gate 
36507c478bd9Sstevel@tonic-gate 	*stkp = L->lwp_status.pr_altstack;
36517c478bd9Sstevel@tonic-gate 
36527c478bd9Sstevel@tonic-gate 	return (0);
36537c478bd9Sstevel@tonic-gate }
36547c478bd9Sstevel@tonic-gate 
36557c478bd9Sstevel@tonic-gate /*
36567c478bd9Sstevel@tonic-gate  * Add a mapping to the given proc handle.  Resizes the array as appropriate and
36577c478bd9Sstevel@tonic-gate  * manages reference counts on the given file_info_t.
36587c478bd9Sstevel@tonic-gate  *
36597c478bd9Sstevel@tonic-gate  * The 'map_relocate' member is used to tell Psort_mappings() that the
36607c478bd9Sstevel@tonic-gate  * associated file_map pointer needs to be relocated after the mappings have
36617c478bd9Sstevel@tonic-gate  * been sorted.  It is only set for the first mapping, and has no meaning
36627c478bd9Sstevel@tonic-gate  * outside these two functions.
36637c478bd9Sstevel@tonic-gate  */
36647c478bd9Sstevel@tonic-gate int
36657c478bd9Sstevel@tonic-gate Padd_mapping(struct ps_prochandle *P, off64_t off, file_info_t *fp,
36667c478bd9Sstevel@tonic-gate     prmap_t *pmap)
36677c478bd9Sstevel@tonic-gate {
36687c478bd9Sstevel@tonic-gate 	map_info_t *mp;
36697c478bd9Sstevel@tonic-gate 
36707c478bd9Sstevel@tonic-gate 	if (P->map_count == P->map_alloc) {
36717c478bd9Sstevel@tonic-gate 		size_t next = P->map_alloc ? P->map_alloc * 2 : 16;
36727c478bd9Sstevel@tonic-gate 
36737c478bd9Sstevel@tonic-gate 		if ((P->mappings = realloc(P->mappings,
36747c478bd9Sstevel@tonic-gate 		    next * sizeof (map_info_t))) == NULL)
36757c478bd9Sstevel@tonic-gate 			return (-1);
36767c478bd9Sstevel@tonic-gate 
36777c478bd9Sstevel@tonic-gate 		P->map_alloc = next;
36787c478bd9Sstevel@tonic-gate 	}
36797c478bd9Sstevel@tonic-gate 
36807c478bd9Sstevel@tonic-gate 	mp = &P->mappings[P->map_count++];
36817c478bd9Sstevel@tonic-gate 
36827c478bd9Sstevel@tonic-gate 	mp->map_offset = off;
36837c478bd9Sstevel@tonic-gate 	mp->map_pmap = *pmap;
36847c478bd9Sstevel@tonic-gate 	mp->map_relocate = 0;
36857c478bd9Sstevel@tonic-gate 	if ((mp->map_file = fp) != NULL) {
36867c478bd9Sstevel@tonic-gate 		if (fp->file_map == NULL) {
36877c478bd9Sstevel@tonic-gate 			fp->file_map = mp;
36887c478bd9Sstevel@tonic-gate 			mp->map_relocate = 1;
36897c478bd9Sstevel@tonic-gate 		}
36907c478bd9Sstevel@tonic-gate 		fp->file_ref++;
36917c478bd9Sstevel@tonic-gate 	}
36927c478bd9Sstevel@tonic-gate 
36937c478bd9Sstevel@tonic-gate 	return (0);
36947c478bd9Sstevel@tonic-gate }
36957c478bd9Sstevel@tonic-gate 
36967c478bd9Sstevel@tonic-gate static int
36977c478bd9Sstevel@tonic-gate map_sort(const void *a, const void *b)
36987c478bd9Sstevel@tonic-gate {
36997c478bd9Sstevel@tonic-gate 	const map_info_t *ap = a, *bp = b;
37007c478bd9Sstevel@tonic-gate 
37017c478bd9Sstevel@tonic-gate 	if (ap->map_pmap.pr_vaddr < bp->map_pmap.pr_vaddr)
37027c478bd9Sstevel@tonic-gate 		return (-1);
37037c478bd9Sstevel@tonic-gate 	else if (ap->map_pmap.pr_vaddr > bp->map_pmap.pr_vaddr)
37047c478bd9Sstevel@tonic-gate 		return (1);
37057c478bd9Sstevel@tonic-gate 	else
37067c478bd9Sstevel@tonic-gate 		return (0);
37077c478bd9Sstevel@tonic-gate }
37087c478bd9Sstevel@tonic-gate 
37097c478bd9Sstevel@tonic-gate /*
37107c478bd9Sstevel@tonic-gate  * Sort the current set of mappings.  Should be called during target
37117c478bd9Sstevel@tonic-gate  * initialization after all calls to Padd_mapping() have been made.
37127c478bd9Sstevel@tonic-gate  */
37137c478bd9Sstevel@tonic-gate void
37147c478bd9Sstevel@tonic-gate Psort_mappings(struct ps_prochandle *P)
37157c478bd9Sstevel@tonic-gate {
37167c478bd9Sstevel@tonic-gate 	int i;
37177c478bd9Sstevel@tonic-gate 	map_info_t *mp;
37187c478bd9Sstevel@tonic-gate 
37197c478bd9Sstevel@tonic-gate 	qsort(P->mappings, P->map_count, sizeof (map_info_t), map_sort);
37207c478bd9Sstevel@tonic-gate 
37217c478bd9Sstevel@tonic-gate 	/*
37227c478bd9Sstevel@tonic-gate 	 * Update all the file_map pointers to refer to the new locations.
37237c478bd9Sstevel@tonic-gate 	 */
37247c478bd9Sstevel@tonic-gate 	for (i = 0; i < P->map_count; i++) {
37257c478bd9Sstevel@tonic-gate 		mp = &P->mappings[i];
37267c478bd9Sstevel@tonic-gate 		if (mp->map_relocate)
37277c478bd9Sstevel@tonic-gate 			mp->map_file->file_map = mp;
37287c478bd9Sstevel@tonic-gate 		mp->map_relocate = 0;
37297c478bd9Sstevel@tonic-gate 	}
37307c478bd9Sstevel@tonic-gate }
3731