xref: /titanic_51/usr/src/lib/libproc/common/Plwpregs.c (revision 2a12f85ad140e332791b4bad1208a734c3f26bf3)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
59acbbeafSnn35248  * Common Development and Distribution License (the "License").
69acbbeafSnn35248  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
229acbbeafSnn35248  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
26f971a346SBryan Cantrill /*
27f971a346SBryan Cantrill  * Copyright (c) 2013, Joyent, Inc. All rights reserved.
28*2a12f85aSJeremy Jones  * Copyright (c) 2013 by Delphix. All rights reserved.
29f971a346SBryan Cantrill  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <sys/types.h>
327c478bd9Sstevel@tonic-gate #include <sys/uio.h>
337c478bd9Sstevel@tonic-gate #include <string.h>
347c478bd9Sstevel@tonic-gate #include <errno.h>
359acbbeafSnn35248 #include <limits.h>
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #include "Pcontrol.h"
387c478bd9Sstevel@tonic-gate #include "P32ton.h"
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate /*
417c478bd9Sstevel@tonic-gate  * This file implements the routines to read and write per-lwp register
427c478bd9Sstevel@tonic-gate  * information from either a live process or core file opened with libproc.
437c478bd9Sstevel@tonic-gate  * We build up a few common routines for reading and writing register
447c478bd9Sstevel@tonic-gate  * information, and then the public functions are all trivial calls to these.
457c478bd9Sstevel@tonic-gate  */
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate /*
487c478bd9Sstevel@tonic-gate  * Utility function to return a pointer to the structure of cached information
497c478bd9Sstevel@tonic-gate  * about an lwp in the core file, given its lwpid.
507c478bd9Sstevel@tonic-gate  */
517c478bd9Sstevel@tonic-gate static lwp_info_t *
527c478bd9Sstevel@tonic-gate getlwpcore(struct ps_prochandle *P, lwpid_t lwpid)
537c478bd9Sstevel@tonic-gate {
54*2a12f85aSJeremy Jones 	core_info_t *core = P->data;
55*2a12f85aSJeremy Jones 	lwp_info_t *lwp = list_next(&core->core_lwp_head);
567c478bd9Sstevel@tonic-gate 	uint_t i;
577c478bd9Sstevel@tonic-gate 
58*2a12f85aSJeremy Jones 	for (i = 0; i < core->core_nlwp; i++, lwp = list_next(lwp)) {
597c478bd9Sstevel@tonic-gate 		if (lwp->lwp_id == lwpid)
607c478bd9Sstevel@tonic-gate 			return (lwp);
617c478bd9Sstevel@tonic-gate 	}
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate 	errno = EINVAL;
647c478bd9Sstevel@tonic-gate 	return (NULL);
657c478bd9Sstevel@tonic-gate }
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate /*
687c478bd9Sstevel@tonic-gate  * Utility function to open and read the contents of a per-lwp /proc file.
697c478bd9Sstevel@tonic-gate  * This function is used to slurp in lwpstatus, xregs, and asrs.
707c478bd9Sstevel@tonic-gate  */
717c478bd9Sstevel@tonic-gate static int
727c478bd9Sstevel@tonic-gate getlwpfile(struct ps_prochandle *P, lwpid_t lwpid,
737c478bd9Sstevel@tonic-gate     const char *fbase, void *rp, size_t n)
747c478bd9Sstevel@tonic-gate {
759acbbeafSnn35248 	char fname[PATH_MAX];
767c478bd9Sstevel@tonic-gate 	int fd;
777c478bd9Sstevel@tonic-gate 
789acbbeafSnn35248 	(void) snprintf(fname, sizeof (fname), "%s/%d/lwp/%d/%s",
799acbbeafSnn35248 	    procfs_path, (int)P->status.pr_pid, (int)lwpid, fbase);
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate 	if ((fd = open(fname, O_RDONLY)) >= 0) {
827c478bd9Sstevel@tonic-gate 		if (read(fd, rp, n) > 0) {
837c478bd9Sstevel@tonic-gate 			(void) close(fd);
847c478bd9Sstevel@tonic-gate 			return (0);
857c478bd9Sstevel@tonic-gate 		}
867c478bd9Sstevel@tonic-gate 		(void) close(fd);
877c478bd9Sstevel@tonic-gate 	}
887c478bd9Sstevel@tonic-gate 	return (-1);
897c478bd9Sstevel@tonic-gate }
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate /*
927c478bd9Sstevel@tonic-gate  * Get the lwpstatus_t for an lwp from either the live process or our
937c478bd9Sstevel@tonic-gate  * cached information from the core file.  This is used to get the
947c478bd9Sstevel@tonic-gate  * general-purpose registers or floating point registers.
957c478bd9Sstevel@tonic-gate  */
967c478bd9Sstevel@tonic-gate int
977c478bd9Sstevel@tonic-gate getlwpstatus(struct ps_prochandle *P, lwpid_t lwpid, lwpstatus_t *lps)
987c478bd9Sstevel@tonic-gate {
997c478bd9Sstevel@tonic-gate 	lwp_info_t *lwp;
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 	/*
1027c478bd9Sstevel@tonic-gate 	 * For both live processes and cores, our job is easy if the lwpid
1037c478bd9Sstevel@tonic-gate 	 * matches that of the representative lwp:
1047c478bd9Sstevel@tonic-gate 	 */
1057c478bd9Sstevel@tonic-gate 	if (P->status.pr_lwp.pr_lwpid == lwpid) {
1067c478bd9Sstevel@tonic-gate 		(void) memcpy(lps, &P->status.pr_lwp, sizeof (lwpstatus_t));
1077c478bd9Sstevel@tonic-gate 		return (0);
1087c478bd9Sstevel@tonic-gate 	}
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	/*
1117c478bd9Sstevel@tonic-gate 	 * If this is a live process, then just read the information out
1127c478bd9Sstevel@tonic-gate 	 * of the per-lwp status file:
1137c478bd9Sstevel@tonic-gate 	 */
1147c478bd9Sstevel@tonic-gate 	if (P->state != PS_DEAD) {
1157c478bd9Sstevel@tonic-gate 		return (getlwpfile(P, lwpid, "lwpstatus",
1167c478bd9Sstevel@tonic-gate 		    lps, sizeof (lwpstatus_t)));
1177c478bd9Sstevel@tonic-gate 	}
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	/*
1207c478bd9Sstevel@tonic-gate 	 * If this is a core file, we need to iterate through our list of
1217c478bd9Sstevel@tonic-gate 	 * cached lwp information and then copy out the status.
1227c478bd9Sstevel@tonic-gate 	 */
123*2a12f85aSJeremy Jones 	if (P->data != NULL && (lwp = getlwpcore(P, lwpid)) != NULL) {
1247c478bd9Sstevel@tonic-gate 		(void) memcpy(lps, &lwp->lwp_status, sizeof (lwpstatus_t));
1257c478bd9Sstevel@tonic-gate 		return (0);
1267c478bd9Sstevel@tonic-gate 	}
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	return (-1);
1297c478bd9Sstevel@tonic-gate }
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate /*
1327c478bd9Sstevel@tonic-gate  * Utility function to modify lwp registers.  This is done using either the
1337c478bd9Sstevel@tonic-gate  * process control file or per-lwp control file as necessary.
1347c478bd9Sstevel@tonic-gate  */
1357c478bd9Sstevel@tonic-gate static int
1367c478bd9Sstevel@tonic-gate setlwpregs(struct ps_prochandle *P, lwpid_t lwpid, long cmd,
1377c478bd9Sstevel@tonic-gate     const void *rp, size_t n)
1387c478bd9Sstevel@tonic-gate {
1397c478bd9Sstevel@tonic-gate 	iovec_t iov[2];
1409acbbeafSnn35248 	char fname[PATH_MAX];
1417c478bd9Sstevel@tonic-gate 	int fd;
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	if (P->state != PS_STOP) {
1447c478bd9Sstevel@tonic-gate 		errno = EBUSY;
1457c478bd9Sstevel@tonic-gate 		return (-1);
1467c478bd9Sstevel@tonic-gate 	}
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	iov[0].iov_base = (caddr_t)&cmd;
1497c478bd9Sstevel@tonic-gate 	iov[0].iov_len = sizeof (long);
1507c478bd9Sstevel@tonic-gate 	iov[1].iov_base = (caddr_t)rp;
1517c478bd9Sstevel@tonic-gate 	iov[1].iov_len = n;
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	/*
1547c478bd9Sstevel@tonic-gate 	 * Writing the process control file writes the representative lwp.
1557c478bd9Sstevel@tonic-gate 	 * Psync before we write to make sure we are consistent with the
1567c478bd9Sstevel@tonic-gate 	 * primary interfaces.  Similarly, make sure to update P->status
1577c478bd9Sstevel@tonic-gate 	 * afterward if we are modifying one of its register sets.
1587c478bd9Sstevel@tonic-gate 	 */
1597c478bd9Sstevel@tonic-gate 	if (P->status.pr_lwp.pr_lwpid == lwpid) {
1607c478bd9Sstevel@tonic-gate 		Psync(P);
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 		if (writev(P->ctlfd, iov, 2) == -1)
1637c478bd9Sstevel@tonic-gate 			return (-1);
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 		if (cmd == PCSREG)
1667c478bd9Sstevel@tonic-gate 			(void) memcpy(P->status.pr_lwp.pr_reg, rp, n);
1677c478bd9Sstevel@tonic-gate 		else if (cmd == PCSFPREG)
1687c478bd9Sstevel@tonic-gate 			(void) memcpy(&P->status.pr_lwp.pr_fpreg, rp, n);
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 		return (0);
1717c478bd9Sstevel@tonic-gate 	}
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	/*
1747c478bd9Sstevel@tonic-gate 	 * If the lwp we want is not the representative lwp, we need to
1757c478bd9Sstevel@tonic-gate 	 * open the ctl file for that specific lwp.
1767c478bd9Sstevel@tonic-gate 	 */
1779acbbeafSnn35248 	(void) snprintf(fname, sizeof (fname), "%s/%d/lwp/%d/lwpctl",
1789acbbeafSnn35248 	    procfs_path, (int)P->status.pr_pid, (int)lwpid);
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	if ((fd = open(fname, O_WRONLY)) >= 0) {
1817c478bd9Sstevel@tonic-gate 		if (writev(fd, iov, 2) > 0) {
1827c478bd9Sstevel@tonic-gate 			(void) close(fd);
1837c478bd9Sstevel@tonic-gate 			return (0);
1847c478bd9Sstevel@tonic-gate 		}
1857c478bd9Sstevel@tonic-gate 		(void) close(fd);
1867c478bd9Sstevel@tonic-gate 	}
1877c478bd9Sstevel@tonic-gate 	return (-1);
1887c478bd9Sstevel@tonic-gate }
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate int
1917c478bd9Sstevel@tonic-gate Plwp_getregs(struct ps_prochandle *P, lwpid_t lwpid, prgregset_t gregs)
1927c478bd9Sstevel@tonic-gate {
1937c478bd9Sstevel@tonic-gate 	lwpstatus_t lps;
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	if (getlwpstatus(P, lwpid, &lps) == -1)
1967c478bd9Sstevel@tonic-gate 		return (-1);
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 	(void) memcpy(gregs, lps.pr_reg, sizeof (prgregset_t));
1997c478bd9Sstevel@tonic-gate 	return (0);
2007c478bd9Sstevel@tonic-gate }
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate int
2037c478bd9Sstevel@tonic-gate Plwp_setregs(struct ps_prochandle *P, lwpid_t lwpid, const prgregset_t gregs)
2047c478bd9Sstevel@tonic-gate {
2057c478bd9Sstevel@tonic-gate 	return (setlwpregs(P, lwpid, PCSREG, gregs, sizeof (prgregset_t)));
2067c478bd9Sstevel@tonic-gate }
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate int
2097c478bd9Sstevel@tonic-gate Plwp_getfpregs(struct ps_prochandle *P, lwpid_t lwpid, prfpregset_t *fpregs)
2107c478bd9Sstevel@tonic-gate {
2117c478bd9Sstevel@tonic-gate 	lwpstatus_t lps;
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 	if (getlwpstatus(P, lwpid, &lps) == -1)
2147c478bd9Sstevel@tonic-gate 		return (-1);
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 	(void) memcpy(fpregs, &lps.pr_fpreg, sizeof (prfpregset_t));
2177c478bd9Sstevel@tonic-gate 	return (0);
2187c478bd9Sstevel@tonic-gate }
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate int Plwp_setfpregs(struct ps_prochandle *P, lwpid_t lwpid,
2217c478bd9Sstevel@tonic-gate     const prfpregset_t *fpregs)
2227c478bd9Sstevel@tonic-gate {
2237c478bd9Sstevel@tonic-gate 	return (setlwpregs(P, lwpid, PCSFPREG, fpregs, sizeof (prfpregset_t)));
2247c478bd9Sstevel@tonic-gate }
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate #if defined(sparc) || defined(__sparc)
2277c478bd9Sstevel@tonic-gate int
2287c478bd9Sstevel@tonic-gate Plwp_getxregs(struct ps_prochandle *P, lwpid_t lwpid, prxregset_t *xregs)
2297c478bd9Sstevel@tonic-gate {
2307c478bd9Sstevel@tonic-gate 	lwp_info_t *lwp;
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 	if (P->state == PS_IDLE) {
2337c478bd9Sstevel@tonic-gate 		errno = ENODATA;
2347c478bd9Sstevel@tonic-gate 		return (-1);
2357c478bd9Sstevel@tonic-gate 	}
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	if (P->state != PS_DEAD) {
2387c478bd9Sstevel@tonic-gate 		if (P->state != PS_STOP) {
2397c478bd9Sstevel@tonic-gate 			errno = EBUSY;
2407c478bd9Sstevel@tonic-gate 			return (-1);
2417c478bd9Sstevel@tonic-gate 		}
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 		return (getlwpfile(P, lwpid, "xregs",
2447c478bd9Sstevel@tonic-gate 		    xregs, sizeof (prxregset_t)));
2457c478bd9Sstevel@tonic-gate 	}
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 	if ((lwp = getlwpcore(P, lwpid)) != NULL && lwp->lwp_xregs != NULL) {
2487c478bd9Sstevel@tonic-gate 		(void) memcpy(xregs, lwp->lwp_xregs, sizeof (prxregset_t));
2497c478bd9Sstevel@tonic-gate 		return (0);
2507c478bd9Sstevel@tonic-gate 	}
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	if (lwp != NULL)
2537c478bd9Sstevel@tonic-gate 		errno = ENODATA;
2547c478bd9Sstevel@tonic-gate 	return (-1);
2557c478bd9Sstevel@tonic-gate }
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate int
2587c478bd9Sstevel@tonic-gate Plwp_setxregs(struct ps_prochandle *P, lwpid_t lwpid, const prxregset_t *xregs)
2597c478bd9Sstevel@tonic-gate {
2607c478bd9Sstevel@tonic-gate 	return (setlwpregs(P, lwpid, PCSXREG, xregs, sizeof (prxregset_t)));
2617c478bd9Sstevel@tonic-gate }
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate int
2647c478bd9Sstevel@tonic-gate Plwp_getgwindows(struct ps_prochandle *P, lwpid_t lwpid, gwindows_t *gwins)
2657c478bd9Sstevel@tonic-gate {
2667c478bd9Sstevel@tonic-gate 	lwp_info_t *lwp;
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 	if (P->state == PS_IDLE) {
2697c478bd9Sstevel@tonic-gate 		errno = ENODATA;
2707c478bd9Sstevel@tonic-gate 		return (-1);
2717c478bd9Sstevel@tonic-gate 	}
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	if (P->state != PS_DEAD) {
2747c478bd9Sstevel@tonic-gate 		if (P->state != PS_STOP) {
2757c478bd9Sstevel@tonic-gate 			errno = EBUSY;
2767c478bd9Sstevel@tonic-gate 			return (-1);
2777c478bd9Sstevel@tonic-gate 		}
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 		return (getlwpfile(P, lwpid, "gwindows",
2807c478bd9Sstevel@tonic-gate 		    gwins, sizeof (gwindows_t)));
2817c478bd9Sstevel@tonic-gate 	}
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 	if ((lwp = getlwpcore(P, lwpid)) != NULL && lwp->lwp_gwins != NULL) {
2847c478bd9Sstevel@tonic-gate 		*gwins = *lwp->lwp_gwins;
2857c478bd9Sstevel@tonic-gate 		return (0);
2867c478bd9Sstevel@tonic-gate 	}
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 	if (lwp != NULL)
2897c478bd9Sstevel@tonic-gate 		errno = ENODATA;
2907c478bd9Sstevel@tonic-gate 	return (-1);
2917c478bd9Sstevel@tonic-gate }
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate #if defined(__sparcv9)
2947c478bd9Sstevel@tonic-gate int
2957c478bd9Sstevel@tonic-gate Plwp_getasrs(struct ps_prochandle *P, lwpid_t lwpid, asrset_t asrs)
2967c478bd9Sstevel@tonic-gate {
2977c478bd9Sstevel@tonic-gate 	lwp_info_t *lwp;
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	if (P->state == PS_IDLE) {
3007c478bd9Sstevel@tonic-gate 		errno = ENODATA;
3017c478bd9Sstevel@tonic-gate 		return (-1);
3027c478bd9Sstevel@tonic-gate 	}
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 	if (P->state != PS_DEAD) {
3057c478bd9Sstevel@tonic-gate 		if (P->state != PS_STOP) {
3067c478bd9Sstevel@tonic-gate 			errno = EBUSY;
3077c478bd9Sstevel@tonic-gate 			return (-1);
3087c478bd9Sstevel@tonic-gate 		}
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 		return (getlwpfile(P, lwpid, "asrs", asrs, sizeof (asrset_t)));
3117c478bd9Sstevel@tonic-gate 	}
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 	if ((lwp = getlwpcore(P, lwpid)) != NULL && lwp->lwp_asrs != NULL) {
3147c478bd9Sstevel@tonic-gate 		(void) memcpy(asrs, lwp->lwp_asrs, sizeof (asrset_t));
3157c478bd9Sstevel@tonic-gate 		return (0);
3167c478bd9Sstevel@tonic-gate 	}
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 	if (lwp != NULL)
3197c478bd9Sstevel@tonic-gate 		errno = ENODATA;
3207c478bd9Sstevel@tonic-gate 	return (-1);
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate }
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate int
3257c478bd9Sstevel@tonic-gate Plwp_setasrs(struct ps_prochandle *P, lwpid_t lwpid, const asrset_t asrs)
3267c478bd9Sstevel@tonic-gate {
3277c478bd9Sstevel@tonic-gate 	return (setlwpregs(P, lwpid, PCSASRS, asrs, sizeof (asrset_t)));
3287c478bd9Sstevel@tonic-gate }
3297c478bd9Sstevel@tonic-gate #endif	/* __sparcv9 */
3307c478bd9Sstevel@tonic-gate #endif	/* __sparc */
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate int
3337c478bd9Sstevel@tonic-gate Plwp_getpsinfo(struct ps_prochandle *P, lwpid_t lwpid, lwpsinfo_t *lps)
3347c478bd9Sstevel@tonic-gate {
3357c478bd9Sstevel@tonic-gate 	lwp_info_t *lwp;
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 	if (P->state == PS_IDLE) {
3387c478bd9Sstevel@tonic-gate 		errno = ENODATA;
3397c478bd9Sstevel@tonic-gate 		return (-1);
3407c478bd9Sstevel@tonic-gate 	}
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 	if (P->state != PS_DEAD) {
3437c478bd9Sstevel@tonic-gate 		return (getlwpfile(P, lwpid, "lwpsinfo",
3447c478bd9Sstevel@tonic-gate 		    lps, sizeof (lwpsinfo_t)));
3457c478bd9Sstevel@tonic-gate 	}
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 	if ((lwp = getlwpcore(P, lwpid)) != NULL) {
3487c478bd9Sstevel@tonic-gate 		(void) memcpy(lps, &lwp->lwp_psinfo, sizeof (lwpsinfo_t));
3497c478bd9Sstevel@tonic-gate 		return (0);
3507c478bd9Sstevel@tonic-gate 	}
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 	return (-1);
3537c478bd9Sstevel@tonic-gate }
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate int
356f971a346SBryan Cantrill Plwp_getspymaster(struct ps_prochandle *P, lwpid_t lwpid, psinfo_t *ps)
357f971a346SBryan Cantrill {
358f971a346SBryan Cantrill 	lwpstatus_t lps;
359f971a346SBryan Cantrill 
360f971a346SBryan Cantrill 	if (P->state == PS_IDLE) {
361f971a346SBryan Cantrill 		errno = ENODATA;
362f971a346SBryan Cantrill 		return (-1);
363f971a346SBryan Cantrill 	}
364f971a346SBryan Cantrill 
365f971a346SBryan Cantrill 	if (getlwpstatus(P, lwpid, &lps) != 0)
366f971a346SBryan Cantrill 		return (-1);
367f971a346SBryan Cantrill 
368f971a346SBryan Cantrill 	if (!(lps.pr_flags & PR_AGENT)) {
369f971a346SBryan Cantrill 		errno = EINVAL;
370f971a346SBryan Cantrill 		return (-1);
371f971a346SBryan Cantrill 	}
372f971a346SBryan Cantrill 
373f971a346SBryan Cantrill 	if (P->state != PS_DEAD) {
374f971a346SBryan Cantrill 		return (getlwpfile(P, lwpid, "spymaster",
375f971a346SBryan Cantrill 		    ps, sizeof (psinfo_t)));
376f971a346SBryan Cantrill 	}
377f971a346SBryan Cantrill 
378f971a346SBryan Cantrill 	if (P->spymaster.pr_nlwp != 0) {
379f971a346SBryan Cantrill 		(void) memcpy(ps, &P->spymaster, sizeof (psinfo_t));
380f971a346SBryan Cantrill 		return (0);
381f971a346SBryan Cantrill 	}
382f971a346SBryan Cantrill 
383f971a346SBryan Cantrill 	errno = ENODATA;
384f971a346SBryan Cantrill 
385f971a346SBryan Cantrill 	return (-1);
386f971a346SBryan Cantrill }
387f971a346SBryan Cantrill 
388f971a346SBryan Cantrill int
3897c478bd9Sstevel@tonic-gate Plwp_stack(struct ps_prochandle *P, lwpid_t lwpid, stack_t *stkp)
3907c478bd9Sstevel@tonic-gate {
3917c478bd9Sstevel@tonic-gate 	uintptr_t addr;
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 	if (P->state == PS_IDLE) {
3947c478bd9Sstevel@tonic-gate 		errno = ENODATA;
3957c478bd9Sstevel@tonic-gate 		return (-1);
3967c478bd9Sstevel@tonic-gate 	}
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 	if (P->state != PS_DEAD) {
3997c478bd9Sstevel@tonic-gate 		lwpstatus_t ls;
4007c478bd9Sstevel@tonic-gate 		if (getlwpfile(P, lwpid, "lwpstatus", &ls, sizeof (ls)) != 0)
4017c478bd9Sstevel@tonic-gate 			return (-1);
4027c478bd9Sstevel@tonic-gate 		addr = ls.pr_ustack;
4037c478bd9Sstevel@tonic-gate 	} else {
4047c478bd9Sstevel@tonic-gate 		lwp_info_t *lwp;
4057c478bd9Sstevel@tonic-gate 		if ((lwp = getlwpcore(P, lwpid)) == NULL)
4067c478bd9Sstevel@tonic-gate 			return (-1);
4077c478bd9Sstevel@tonic-gate 		addr = lwp->lwp_status.pr_ustack;
4087c478bd9Sstevel@tonic-gate 	}
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 	if (P->status.pr_dmodel == PR_MODEL_NATIVE) {
4127c478bd9Sstevel@tonic-gate 		if (Pread(P, stkp, sizeof (*stkp), addr) != sizeof (*stkp))
4137c478bd9Sstevel@tonic-gate 			return (-1);
4147c478bd9Sstevel@tonic-gate #ifdef _LP64
4157c478bd9Sstevel@tonic-gate 	} else {
4167c478bd9Sstevel@tonic-gate 		stack32_t stk32;
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 		if (Pread(P, &stk32, sizeof (stk32), addr) != sizeof (stk32))
4197c478bd9Sstevel@tonic-gate 			return (-1);
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate 		stack_32_to_n(&stk32, stkp);
4227c478bd9Sstevel@tonic-gate #endif
4237c478bd9Sstevel@tonic-gate 	}
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 	return (0);
4267c478bd9Sstevel@tonic-gate }
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate int
4297c478bd9Sstevel@tonic-gate Plwp_main_stack(struct ps_prochandle *P, lwpid_t lwpid, stack_t *stkp)
4307c478bd9Sstevel@tonic-gate {
4317c478bd9Sstevel@tonic-gate 	uintptr_t addr;
4327c478bd9Sstevel@tonic-gate 	lwpstatus_t ls;
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 	if (P->state == PS_IDLE) {
4357c478bd9Sstevel@tonic-gate 		errno = ENODATA;
4367c478bd9Sstevel@tonic-gate 		return (-1);
4377c478bd9Sstevel@tonic-gate 	}
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate 	if (P->state != PS_DEAD) {
4407c478bd9Sstevel@tonic-gate 		if (getlwpfile(P, lwpid, "lwpstatus", &ls, sizeof (ls)) != 0)
4417c478bd9Sstevel@tonic-gate 			return (-1);
4427c478bd9Sstevel@tonic-gate 	} else {
4437c478bd9Sstevel@tonic-gate 		lwp_info_t *lwp;
4447c478bd9Sstevel@tonic-gate 		if ((lwp = getlwpcore(P, lwpid)) == NULL)
4457c478bd9Sstevel@tonic-gate 			return (-1);
4467c478bd9Sstevel@tonic-gate 		ls = lwp->lwp_status;
4477c478bd9Sstevel@tonic-gate 	}
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate 	addr = ls.pr_ustack;
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 	/*
4527c478bd9Sstevel@tonic-gate 	 * Read out the current stack; if the SS_ONSTACK flag is set then
4537c478bd9Sstevel@tonic-gate 	 * this LWP is operating on the alternate signal stack. We can
4547c478bd9Sstevel@tonic-gate 	 * recover the original stack from pr_oldcontext.
4557c478bd9Sstevel@tonic-gate 	 */
4567c478bd9Sstevel@tonic-gate 	if (P->status.pr_dmodel == PR_MODEL_NATIVE) {
4577c478bd9Sstevel@tonic-gate 		if (Pread(P, stkp, sizeof (*stkp), addr) != sizeof (*stkp))
4587c478bd9Sstevel@tonic-gate 			return (-1);
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 		if (stkp->ss_flags & SS_ONSTACK)
4617c478bd9Sstevel@tonic-gate 			goto on_altstack;
4627c478bd9Sstevel@tonic-gate #ifdef _LP64
4637c478bd9Sstevel@tonic-gate 	} else {
4647c478bd9Sstevel@tonic-gate 		stack32_t stk32;
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 		if (Pread(P, &stk32, sizeof (stk32), addr) != sizeof (stk32))
4677c478bd9Sstevel@tonic-gate 			return (-1);
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 		if (stk32.ss_flags & SS_ONSTACK)
4707c478bd9Sstevel@tonic-gate 			goto on_altstack;
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 		stack_32_to_n(&stk32, stkp);
4737c478bd9Sstevel@tonic-gate #endif
4747c478bd9Sstevel@tonic-gate 	}
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 	return (0);
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate on_altstack:
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 	if (P->status.pr_dmodel == PR_MODEL_NATIVE) {
4817c478bd9Sstevel@tonic-gate 		ucontext_t *ctxp = (void *)ls.pr_oldcontext;
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 		if (Pread(P, stkp, sizeof (*stkp),
4847c478bd9Sstevel@tonic-gate 		    (uintptr_t)&ctxp->uc_stack) != sizeof (*stkp))
4857c478bd9Sstevel@tonic-gate 			return (-1);
4867c478bd9Sstevel@tonic-gate #ifdef _LP64
4877c478bd9Sstevel@tonic-gate 	} else {
4887c478bd9Sstevel@tonic-gate 		ucontext32_t *ctxp = (void *)ls.pr_oldcontext;
4897c478bd9Sstevel@tonic-gate 		stack32_t stk32;
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 		if (Pread(P, &stk32, sizeof (stk32),
4927c478bd9Sstevel@tonic-gate 		    (uintptr_t)&ctxp->uc_stack) != sizeof (stk32))
4937c478bd9Sstevel@tonic-gate 			return (-1);
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate 		stack_32_to_n(&stk32, stkp);
4967c478bd9Sstevel@tonic-gate #endif
4977c478bd9Sstevel@tonic-gate 	}
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate 	return (0);
5007c478bd9Sstevel@tonic-gate }
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate int
5037c478bd9Sstevel@tonic-gate Plwp_alt_stack(struct ps_prochandle *P, lwpid_t lwpid, stack_t *stkp)
5047c478bd9Sstevel@tonic-gate {
5057c478bd9Sstevel@tonic-gate 	if (P->state == PS_IDLE) {
5067c478bd9Sstevel@tonic-gate 		errno = ENODATA;
5077c478bd9Sstevel@tonic-gate 		return (-1);
5087c478bd9Sstevel@tonic-gate 	}
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate 	if (P->state != PS_DEAD) {
5117c478bd9Sstevel@tonic-gate 		lwpstatus_t ls;
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 		if (getlwpfile(P, lwpid, "lwpstatus", &ls, sizeof (ls)) != 0)
5147c478bd9Sstevel@tonic-gate 			return (-1);
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate 		if (ls.pr_altstack.ss_flags & SS_DISABLE) {
5177c478bd9Sstevel@tonic-gate 			errno = ENODATA;
5187c478bd9Sstevel@tonic-gate 			return (-1);
5197c478bd9Sstevel@tonic-gate 		}
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 		*stkp = ls.pr_altstack;
5227c478bd9Sstevel@tonic-gate 	} else {
5237c478bd9Sstevel@tonic-gate 		lwp_info_t *lwp;
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 		if ((lwp = getlwpcore(P, lwpid)) == NULL)
5267c478bd9Sstevel@tonic-gate 			return (-1);
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate 		if (lwp->lwp_status.pr_altstack.ss_flags & SS_DISABLE) {
5297c478bd9Sstevel@tonic-gate 			errno = ENODATA;
5307c478bd9Sstevel@tonic-gate 			return (-1);
5317c478bd9Sstevel@tonic-gate 		}
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 		*stkp = lwp->lwp_status.pr_altstack;
5347c478bd9Sstevel@tonic-gate 	}
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate 	return (0);
5377c478bd9Sstevel@tonic-gate }
538