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