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