1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <sys/types.h>
30*0Sstevel@tonic-gate #include <sys/uio.h>
31*0Sstevel@tonic-gate #include <string.h>
32*0Sstevel@tonic-gate #include <errno.h>
33*0Sstevel@tonic-gate 
34*0Sstevel@tonic-gate #include "Pcontrol.h"
35*0Sstevel@tonic-gate #include "P32ton.h"
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate /*
38*0Sstevel@tonic-gate  * This file implements the routines to read and write per-lwp register
39*0Sstevel@tonic-gate  * information from either a live process or core file opened with libproc.
40*0Sstevel@tonic-gate  * We build up a few common routines for reading and writing register
41*0Sstevel@tonic-gate  * information, and then the public functions are all trivial calls to these.
42*0Sstevel@tonic-gate  */
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate /*
45*0Sstevel@tonic-gate  * Utility function to return a pointer to the structure of cached information
46*0Sstevel@tonic-gate  * about an lwp in the core file, given its lwpid.
47*0Sstevel@tonic-gate  */
48*0Sstevel@tonic-gate static lwp_info_t *
49*0Sstevel@tonic-gate getlwpcore(struct ps_prochandle *P, lwpid_t lwpid)
50*0Sstevel@tonic-gate {
51*0Sstevel@tonic-gate 	lwp_info_t *lwp = list_next(&P->core->core_lwp_head);
52*0Sstevel@tonic-gate 	uint_t i;
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate 	for (i = 0; i < P->core->core_nlwp; i++, lwp = list_next(lwp)) {
55*0Sstevel@tonic-gate 		if (lwp->lwp_id == lwpid)
56*0Sstevel@tonic-gate 			return (lwp);
57*0Sstevel@tonic-gate 	}
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate 	errno = EINVAL;
60*0Sstevel@tonic-gate 	return (NULL);
61*0Sstevel@tonic-gate }
62*0Sstevel@tonic-gate 
63*0Sstevel@tonic-gate /*
64*0Sstevel@tonic-gate  * Utility function to open and read the contents of a per-lwp /proc file.
65*0Sstevel@tonic-gate  * This function is used to slurp in lwpstatus, xregs, and asrs.
66*0Sstevel@tonic-gate  */
67*0Sstevel@tonic-gate static int
68*0Sstevel@tonic-gate getlwpfile(struct ps_prochandle *P, lwpid_t lwpid,
69*0Sstevel@tonic-gate     const char *fbase, void *rp, size_t n)
70*0Sstevel@tonic-gate {
71*0Sstevel@tonic-gate 	char fname[64];
72*0Sstevel@tonic-gate 	int fd;
73*0Sstevel@tonic-gate 
74*0Sstevel@tonic-gate 	(void) snprintf(fname, sizeof (fname), "/proc/%d/lwp/%d/%s",
75*0Sstevel@tonic-gate 		(int)P->status.pr_pid, (int)lwpid, fbase);
76*0Sstevel@tonic-gate 
77*0Sstevel@tonic-gate 	if ((fd = open(fname, O_RDONLY)) >= 0) {
78*0Sstevel@tonic-gate 		if (read(fd, rp, n) > 0) {
79*0Sstevel@tonic-gate 			(void) close(fd);
80*0Sstevel@tonic-gate 			return (0);
81*0Sstevel@tonic-gate 		}
82*0Sstevel@tonic-gate 		(void) close(fd);
83*0Sstevel@tonic-gate 	}
84*0Sstevel@tonic-gate 	return (-1);
85*0Sstevel@tonic-gate }
86*0Sstevel@tonic-gate 
87*0Sstevel@tonic-gate /*
88*0Sstevel@tonic-gate  * Get the lwpstatus_t for an lwp from either the live process or our
89*0Sstevel@tonic-gate  * cached information from the core file.  This is used to get the
90*0Sstevel@tonic-gate  * general-purpose registers or floating point registers.
91*0Sstevel@tonic-gate  */
92*0Sstevel@tonic-gate int
93*0Sstevel@tonic-gate getlwpstatus(struct ps_prochandle *P, lwpid_t lwpid, lwpstatus_t *lps)
94*0Sstevel@tonic-gate {
95*0Sstevel@tonic-gate 	lwp_info_t *lwp;
96*0Sstevel@tonic-gate 
97*0Sstevel@tonic-gate 	/*
98*0Sstevel@tonic-gate 	 * For both live processes and cores, our job is easy if the lwpid
99*0Sstevel@tonic-gate 	 * matches that of the representative lwp:
100*0Sstevel@tonic-gate 	 */
101*0Sstevel@tonic-gate 	if (P->status.pr_lwp.pr_lwpid == lwpid) {
102*0Sstevel@tonic-gate 		(void) memcpy(lps, &P->status.pr_lwp, sizeof (lwpstatus_t));
103*0Sstevel@tonic-gate 		return (0);
104*0Sstevel@tonic-gate 	}
105*0Sstevel@tonic-gate 
106*0Sstevel@tonic-gate 	/*
107*0Sstevel@tonic-gate 	 * If this is a live process, then just read the information out
108*0Sstevel@tonic-gate 	 * of the per-lwp status file:
109*0Sstevel@tonic-gate 	 */
110*0Sstevel@tonic-gate 	if (P->state != PS_DEAD) {
111*0Sstevel@tonic-gate 		return (getlwpfile(P, lwpid, "lwpstatus",
112*0Sstevel@tonic-gate 		    lps, sizeof (lwpstatus_t)));
113*0Sstevel@tonic-gate 	}
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate 	/*
116*0Sstevel@tonic-gate 	 * If this is a core file, we need to iterate through our list of
117*0Sstevel@tonic-gate 	 * cached lwp information and then copy out the status.
118*0Sstevel@tonic-gate 	 */
119*0Sstevel@tonic-gate 	if (P->core != NULL && (lwp = getlwpcore(P, lwpid)) != NULL) {
120*0Sstevel@tonic-gate 		(void) memcpy(lps, &lwp->lwp_status, sizeof (lwpstatus_t));
121*0Sstevel@tonic-gate 		return (0);
122*0Sstevel@tonic-gate 	}
123*0Sstevel@tonic-gate 
124*0Sstevel@tonic-gate 	return (-1);
125*0Sstevel@tonic-gate }
126*0Sstevel@tonic-gate 
127*0Sstevel@tonic-gate /*
128*0Sstevel@tonic-gate  * Utility function to modify lwp registers.  This is done using either the
129*0Sstevel@tonic-gate  * process control file or per-lwp control file as necessary.
130*0Sstevel@tonic-gate  */
131*0Sstevel@tonic-gate static int
132*0Sstevel@tonic-gate setlwpregs(struct ps_prochandle *P, lwpid_t lwpid, long cmd,
133*0Sstevel@tonic-gate     const void *rp, size_t n)
134*0Sstevel@tonic-gate {
135*0Sstevel@tonic-gate 	iovec_t iov[2];
136*0Sstevel@tonic-gate 	char fname[64];
137*0Sstevel@tonic-gate 	int fd;
138*0Sstevel@tonic-gate 
139*0Sstevel@tonic-gate 	if (P->state != PS_STOP) {
140*0Sstevel@tonic-gate 		errno = EBUSY;
141*0Sstevel@tonic-gate 		return (-1);
142*0Sstevel@tonic-gate 	}
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate 	iov[0].iov_base = (caddr_t)&cmd;
145*0Sstevel@tonic-gate 	iov[0].iov_len = sizeof (long);
146*0Sstevel@tonic-gate 	iov[1].iov_base = (caddr_t)rp;
147*0Sstevel@tonic-gate 	iov[1].iov_len = n;
148*0Sstevel@tonic-gate 
149*0Sstevel@tonic-gate 	/*
150*0Sstevel@tonic-gate 	 * Writing the process control file writes the representative lwp.
151*0Sstevel@tonic-gate 	 * Psync before we write to make sure we are consistent with the
152*0Sstevel@tonic-gate 	 * primary interfaces.  Similarly, make sure to update P->status
153*0Sstevel@tonic-gate 	 * afterward if we are modifying one of its register sets.
154*0Sstevel@tonic-gate 	 */
155*0Sstevel@tonic-gate 	if (P->status.pr_lwp.pr_lwpid == lwpid) {
156*0Sstevel@tonic-gate 		Psync(P);
157*0Sstevel@tonic-gate 
158*0Sstevel@tonic-gate 		if (writev(P->ctlfd, iov, 2) == -1)
159*0Sstevel@tonic-gate 			return (-1);
160*0Sstevel@tonic-gate 
161*0Sstevel@tonic-gate 		if (cmd == PCSREG)
162*0Sstevel@tonic-gate 			(void) memcpy(P->status.pr_lwp.pr_reg, rp, n);
163*0Sstevel@tonic-gate 		else if (cmd == PCSFPREG)
164*0Sstevel@tonic-gate 			(void) memcpy(&P->status.pr_lwp.pr_fpreg, rp, n);
165*0Sstevel@tonic-gate 
166*0Sstevel@tonic-gate 		return (0);
167*0Sstevel@tonic-gate 	}
168*0Sstevel@tonic-gate 
169*0Sstevel@tonic-gate 	/*
170*0Sstevel@tonic-gate 	 * If the lwp we want is not the representative lwp, we need to
171*0Sstevel@tonic-gate 	 * open the ctl file for that specific lwp.
172*0Sstevel@tonic-gate 	 */
173*0Sstevel@tonic-gate 	(void) snprintf(fname, sizeof (fname), "/proc/%d/lwp/%d/lwpctl",
174*0Sstevel@tonic-gate 	    (int)P->status.pr_pid, (int)lwpid);
175*0Sstevel@tonic-gate 
176*0Sstevel@tonic-gate 	if ((fd = open(fname, O_WRONLY)) >= 0) {
177*0Sstevel@tonic-gate 		if (writev(fd, iov, 2) > 0) {
178*0Sstevel@tonic-gate 			(void) close(fd);
179*0Sstevel@tonic-gate 			return (0);
180*0Sstevel@tonic-gate 		}
181*0Sstevel@tonic-gate 		(void) close(fd);
182*0Sstevel@tonic-gate 	}
183*0Sstevel@tonic-gate 	return (-1);
184*0Sstevel@tonic-gate }
185*0Sstevel@tonic-gate 
186*0Sstevel@tonic-gate int
187*0Sstevel@tonic-gate Plwp_getregs(struct ps_prochandle *P, lwpid_t lwpid, prgregset_t gregs)
188*0Sstevel@tonic-gate {
189*0Sstevel@tonic-gate 	lwpstatus_t lps;
190*0Sstevel@tonic-gate 
191*0Sstevel@tonic-gate 	if (getlwpstatus(P, lwpid, &lps) == -1)
192*0Sstevel@tonic-gate 		return (-1);
193*0Sstevel@tonic-gate 
194*0Sstevel@tonic-gate 	(void) memcpy(gregs, lps.pr_reg, sizeof (prgregset_t));
195*0Sstevel@tonic-gate 	return (0);
196*0Sstevel@tonic-gate }
197*0Sstevel@tonic-gate 
198*0Sstevel@tonic-gate int
199*0Sstevel@tonic-gate Plwp_setregs(struct ps_prochandle *P, lwpid_t lwpid, const prgregset_t gregs)
200*0Sstevel@tonic-gate {
201*0Sstevel@tonic-gate 	return (setlwpregs(P, lwpid, PCSREG, gregs, sizeof (prgregset_t)));
202*0Sstevel@tonic-gate }
203*0Sstevel@tonic-gate 
204*0Sstevel@tonic-gate int
205*0Sstevel@tonic-gate Plwp_getfpregs(struct ps_prochandle *P, lwpid_t lwpid, prfpregset_t *fpregs)
206*0Sstevel@tonic-gate {
207*0Sstevel@tonic-gate 	lwpstatus_t lps;
208*0Sstevel@tonic-gate 
209*0Sstevel@tonic-gate 	if (getlwpstatus(P, lwpid, &lps) == -1)
210*0Sstevel@tonic-gate 		return (-1);
211*0Sstevel@tonic-gate 
212*0Sstevel@tonic-gate 	(void) memcpy(fpregs, &lps.pr_fpreg, sizeof (prfpregset_t));
213*0Sstevel@tonic-gate 	return (0);
214*0Sstevel@tonic-gate }
215*0Sstevel@tonic-gate 
216*0Sstevel@tonic-gate int Plwp_setfpregs(struct ps_prochandle *P, lwpid_t lwpid,
217*0Sstevel@tonic-gate     const prfpregset_t *fpregs)
218*0Sstevel@tonic-gate {
219*0Sstevel@tonic-gate 	return (setlwpregs(P, lwpid, PCSFPREG, fpregs, sizeof (prfpregset_t)));
220*0Sstevel@tonic-gate }
221*0Sstevel@tonic-gate 
222*0Sstevel@tonic-gate #if defined(sparc) || defined(__sparc)
223*0Sstevel@tonic-gate int
224*0Sstevel@tonic-gate Plwp_getxregs(struct ps_prochandle *P, lwpid_t lwpid, prxregset_t *xregs)
225*0Sstevel@tonic-gate {
226*0Sstevel@tonic-gate 	lwp_info_t *lwp;
227*0Sstevel@tonic-gate 
228*0Sstevel@tonic-gate 	if (P->state == PS_IDLE) {
229*0Sstevel@tonic-gate 		errno = ENODATA;
230*0Sstevel@tonic-gate 		return (-1);
231*0Sstevel@tonic-gate 	}
232*0Sstevel@tonic-gate 
233*0Sstevel@tonic-gate 	if (P->state != PS_DEAD) {
234*0Sstevel@tonic-gate 		if (P->state != PS_STOP) {
235*0Sstevel@tonic-gate 			errno = EBUSY;
236*0Sstevel@tonic-gate 			return (-1);
237*0Sstevel@tonic-gate 		}
238*0Sstevel@tonic-gate 
239*0Sstevel@tonic-gate 		return (getlwpfile(P, lwpid, "xregs",
240*0Sstevel@tonic-gate 		    xregs, sizeof (prxregset_t)));
241*0Sstevel@tonic-gate 	}
242*0Sstevel@tonic-gate 
243*0Sstevel@tonic-gate 	if ((lwp = getlwpcore(P, lwpid)) != NULL && lwp->lwp_xregs != NULL) {
244*0Sstevel@tonic-gate 		(void) memcpy(xregs, lwp->lwp_xregs, sizeof (prxregset_t));
245*0Sstevel@tonic-gate 		return (0);
246*0Sstevel@tonic-gate 	}
247*0Sstevel@tonic-gate 
248*0Sstevel@tonic-gate 	if (lwp != NULL)
249*0Sstevel@tonic-gate 		errno = ENODATA;
250*0Sstevel@tonic-gate 	return (-1);
251*0Sstevel@tonic-gate }
252*0Sstevel@tonic-gate 
253*0Sstevel@tonic-gate int
254*0Sstevel@tonic-gate Plwp_setxregs(struct ps_prochandle *P, lwpid_t lwpid, const prxregset_t *xregs)
255*0Sstevel@tonic-gate {
256*0Sstevel@tonic-gate 	return (setlwpregs(P, lwpid, PCSXREG, xregs, sizeof (prxregset_t)));
257*0Sstevel@tonic-gate }
258*0Sstevel@tonic-gate 
259*0Sstevel@tonic-gate int
260*0Sstevel@tonic-gate Plwp_getgwindows(struct ps_prochandle *P, lwpid_t lwpid, gwindows_t *gwins)
261*0Sstevel@tonic-gate {
262*0Sstevel@tonic-gate 	lwp_info_t *lwp;
263*0Sstevel@tonic-gate 
264*0Sstevel@tonic-gate 	if (P->state == PS_IDLE) {
265*0Sstevel@tonic-gate 		errno = ENODATA;
266*0Sstevel@tonic-gate 		return (-1);
267*0Sstevel@tonic-gate 	}
268*0Sstevel@tonic-gate 
269*0Sstevel@tonic-gate 	if (P->state != PS_DEAD) {
270*0Sstevel@tonic-gate 		if (P->state != PS_STOP) {
271*0Sstevel@tonic-gate 			errno = EBUSY;
272*0Sstevel@tonic-gate 			return (-1);
273*0Sstevel@tonic-gate 		}
274*0Sstevel@tonic-gate 
275*0Sstevel@tonic-gate 		return (getlwpfile(P, lwpid, "gwindows",
276*0Sstevel@tonic-gate 		    gwins, sizeof (gwindows_t)));
277*0Sstevel@tonic-gate 	}
278*0Sstevel@tonic-gate 
279*0Sstevel@tonic-gate 	if ((lwp = getlwpcore(P, lwpid)) != NULL && lwp->lwp_gwins != NULL) {
280*0Sstevel@tonic-gate 		*gwins = *lwp->lwp_gwins;
281*0Sstevel@tonic-gate 		return (0);
282*0Sstevel@tonic-gate 	}
283*0Sstevel@tonic-gate 
284*0Sstevel@tonic-gate 	if (lwp != NULL)
285*0Sstevel@tonic-gate 		errno = ENODATA;
286*0Sstevel@tonic-gate 	return (-1);
287*0Sstevel@tonic-gate }
288*0Sstevel@tonic-gate 
289*0Sstevel@tonic-gate #if defined(__sparcv9)
290*0Sstevel@tonic-gate int
291*0Sstevel@tonic-gate Plwp_getasrs(struct ps_prochandle *P, lwpid_t lwpid, asrset_t asrs)
292*0Sstevel@tonic-gate {
293*0Sstevel@tonic-gate 	lwp_info_t *lwp;
294*0Sstevel@tonic-gate 
295*0Sstevel@tonic-gate 	if (P->state == PS_IDLE) {
296*0Sstevel@tonic-gate 		errno = ENODATA;
297*0Sstevel@tonic-gate 		return (-1);
298*0Sstevel@tonic-gate 	}
299*0Sstevel@tonic-gate 
300*0Sstevel@tonic-gate 	if (P->state != PS_DEAD) {
301*0Sstevel@tonic-gate 		if (P->state != PS_STOP) {
302*0Sstevel@tonic-gate 			errno = EBUSY;
303*0Sstevel@tonic-gate 			return (-1);
304*0Sstevel@tonic-gate 		}
305*0Sstevel@tonic-gate 
306*0Sstevel@tonic-gate 		return (getlwpfile(P, lwpid, "asrs", asrs, sizeof (asrset_t)));
307*0Sstevel@tonic-gate 	}
308*0Sstevel@tonic-gate 
309*0Sstevel@tonic-gate 	if ((lwp = getlwpcore(P, lwpid)) != NULL && lwp->lwp_asrs != NULL) {
310*0Sstevel@tonic-gate 		(void) memcpy(asrs, lwp->lwp_asrs, sizeof (asrset_t));
311*0Sstevel@tonic-gate 		return (0);
312*0Sstevel@tonic-gate 	}
313*0Sstevel@tonic-gate 
314*0Sstevel@tonic-gate 	if (lwp != NULL)
315*0Sstevel@tonic-gate 		errno = ENODATA;
316*0Sstevel@tonic-gate 	return (-1);
317*0Sstevel@tonic-gate 
318*0Sstevel@tonic-gate }
319*0Sstevel@tonic-gate 
320*0Sstevel@tonic-gate int
321*0Sstevel@tonic-gate Plwp_setasrs(struct ps_prochandle *P, lwpid_t lwpid, const asrset_t asrs)
322*0Sstevel@tonic-gate {
323*0Sstevel@tonic-gate 	return (setlwpregs(P, lwpid, PCSASRS, asrs, sizeof (asrset_t)));
324*0Sstevel@tonic-gate }
325*0Sstevel@tonic-gate #endif	/* __sparcv9 */
326*0Sstevel@tonic-gate #endif	/* __sparc */
327*0Sstevel@tonic-gate 
328*0Sstevel@tonic-gate int
329*0Sstevel@tonic-gate Plwp_getpsinfo(struct ps_prochandle *P, lwpid_t lwpid, lwpsinfo_t *lps)
330*0Sstevel@tonic-gate {
331*0Sstevel@tonic-gate 	lwp_info_t *lwp;
332*0Sstevel@tonic-gate 
333*0Sstevel@tonic-gate 	if (P->state == PS_IDLE) {
334*0Sstevel@tonic-gate 		errno = ENODATA;
335*0Sstevel@tonic-gate 		return (-1);
336*0Sstevel@tonic-gate 	}
337*0Sstevel@tonic-gate 
338*0Sstevel@tonic-gate 	if (P->state != PS_DEAD) {
339*0Sstevel@tonic-gate 		return (getlwpfile(P, lwpid, "lwpsinfo",
340*0Sstevel@tonic-gate 		    lps, sizeof (lwpsinfo_t)));
341*0Sstevel@tonic-gate 	}
342*0Sstevel@tonic-gate 
343*0Sstevel@tonic-gate 	if ((lwp = getlwpcore(P, lwpid)) != NULL) {
344*0Sstevel@tonic-gate 		(void) memcpy(lps, &lwp->lwp_psinfo, sizeof (lwpsinfo_t));
345*0Sstevel@tonic-gate 		return (0);
346*0Sstevel@tonic-gate 	}
347*0Sstevel@tonic-gate 
348*0Sstevel@tonic-gate 	return (-1);
349*0Sstevel@tonic-gate }
350*0Sstevel@tonic-gate 
351*0Sstevel@tonic-gate int
352*0Sstevel@tonic-gate Plwp_stack(struct ps_prochandle *P, lwpid_t lwpid, stack_t *stkp)
353*0Sstevel@tonic-gate {
354*0Sstevel@tonic-gate 	uintptr_t addr;
355*0Sstevel@tonic-gate 
356*0Sstevel@tonic-gate 	if (P->state == PS_IDLE) {
357*0Sstevel@tonic-gate 		errno = ENODATA;
358*0Sstevel@tonic-gate 		return (-1);
359*0Sstevel@tonic-gate 	}
360*0Sstevel@tonic-gate 
361*0Sstevel@tonic-gate 	if (P->state != PS_DEAD) {
362*0Sstevel@tonic-gate 		lwpstatus_t ls;
363*0Sstevel@tonic-gate 		if (getlwpfile(P, lwpid, "lwpstatus", &ls, sizeof (ls)) != 0)
364*0Sstevel@tonic-gate 			return (-1);
365*0Sstevel@tonic-gate 		addr = ls.pr_ustack;
366*0Sstevel@tonic-gate 	} else {
367*0Sstevel@tonic-gate 		lwp_info_t *lwp;
368*0Sstevel@tonic-gate 		if ((lwp = getlwpcore(P, lwpid)) == NULL)
369*0Sstevel@tonic-gate 			return (-1);
370*0Sstevel@tonic-gate 		addr = lwp->lwp_status.pr_ustack;
371*0Sstevel@tonic-gate 	}
372*0Sstevel@tonic-gate 
373*0Sstevel@tonic-gate 
374*0Sstevel@tonic-gate 	if (P->status.pr_dmodel == PR_MODEL_NATIVE) {
375*0Sstevel@tonic-gate 		if (Pread(P, stkp, sizeof (*stkp), addr) != sizeof (*stkp))
376*0Sstevel@tonic-gate 			return (-1);
377*0Sstevel@tonic-gate #ifdef _LP64
378*0Sstevel@tonic-gate 	} else {
379*0Sstevel@tonic-gate 		stack32_t stk32;
380*0Sstevel@tonic-gate 
381*0Sstevel@tonic-gate 		if (Pread(P, &stk32, sizeof (stk32), addr) != sizeof (stk32))
382*0Sstevel@tonic-gate 			return (-1);
383*0Sstevel@tonic-gate 
384*0Sstevel@tonic-gate 		stack_32_to_n(&stk32, stkp);
385*0Sstevel@tonic-gate #endif
386*0Sstevel@tonic-gate 	}
387*0Sstevel@tonic-gate 
388*0Sstevel@tonic-gate 	return (0);
389*0Sstevel@tonic-gate }
390*0Sstevel@tonic-gate 
391*0Sstevel@tonic-gate int
392*0Sstevel@tonic-gate Plwp_main_stack(struct ps_prochandle *P, lwpid_t lwpid, stack_t *stkp)
393*0Sstevel@tonic-gate {
394*0Sstevel@tonic-gate 	uintptr_t addr;
395*0Sstevel@tonic-gate 	lwpstatus_t ls;
396*0Sstevel@tonic-gate 
397*0Sstevel@tonic-gate 	if (P->state == PS_IDLE) {
398*0Sstevel@tonic-gate 		errno = ENODATA;
399*0Sstevel@tonic-gate 		return (-1);
400*0Sstevel@tonic-gate 	}
401*0Sstevel@tonic-gate 
402*0Sstevel@tonic-gate 	if (P->state != PS_DEAD) {
403*0Sstevel@tonic-gate 		if (getlwpfile(P, lwpid, "lwpstatus", &ls, sizeof (ls)) != 0)
404*0Sstevel@tonic-gate 			return (-1);
405*0Sstevel@tonic-gate 	} else {
406*0Sstevel@tonic-gate 		lwp_info_t *lwp;
407*0Sstevel@tonic-gate 		if ((lwp = getlwpcore(P, lwpid)) == NULL)
408*0Sstevel@tonic-gate 			return (-1);
409*0Sstevel@tonic-gate 		ls = lwp->lwp_status;
410*0Sstevel@tonic-gate 	}
411*0Sstevel@tonic-gate 
412*0Sstevel@tonic-gate 	addr = ls.pr_ustack;
413*0Sstevel@tonic-gate 
414*0Sstevel@tonic-gate 	/*
415*0Sstevel@tonic-gate 	 * Read out the current stack; if the SS_ONSTACK flag is set then
416*0Sstevel@tonic-gate 	 * this LWP is operating on the alternate signal stack. We can
417*0Sstevel@tonic-gate 	 * recover the original stack from pr_oldcontext.
418*0Sstevel@tonic-gate 	 */
419*0Sstevel@tonic-gate 	if (P->status.pr_dmodel == PR_MODEL_NATIVE) {
420*0Sstevel@tonic-gate 		if (Pread(P, stkp, sizeof (*stkp), addr) != sizeof (*stkp))
421*0Sstevel@tonic-gate 			return (-1);
422*0Sstevel@tonic-gate 
423*0Sstevel@tonic-gate 		if (stkp->ss_flags & SS_ONSTACK)
424*0Sstevel@tonic-gate 			goto on_altstack;
425*0Sstevel@tonic-gate #ifdef _LP64
426*0Sstevel@tonic-gate 	} else {
427*0Sstevel@tonic-gate 		stack32_t stk32;
428*0Sstevel@tonic-gate 
429*0Sstevel@tonic-gate 		if (Pread(P, &stk32, sizeof (stk32), addr) != sizeof (stk32))
430*0Sstevel@tonic-gate 			return (-1);
431*0Sstevel@tonic-gate 
432*0Sstevel@tonic-gate 		if (stk32.ss_flags & SS_ONSTACK)
433*0Sstevel@tonic-gate 			goto on_altstack;
434*0Sstevel@tonic-gate 
435*0Sstevel@tonic-gate 		stack_32_to_n(&stk32, stkp);
436*0Sstevel@tonic-gate #endif
437*0Sstevel@tonic-gate 	}
438*0Sstevel@tonic-gate 
439*0Sstevel@tonic-gate 	return (0);
440*0Sstevel@tonic-gate 
441*0Sstevel@tonic-gate on_altstack:
442*0Sstevel@tonic-gate 
443*0Sstevel@tonic-gate 	if (P->status.pr_dmodel == PR_MODEL_NATIVE) {
444*0Sstevel@tonic-gate 		ucontext_t *ctxp = (void *)ls.pr_oldcontext;
445*0Sstevel@tonic-gate 
446*0Sstevel@tonic-gate 		if (Pread(P, stkp, sizeof (*stkp),
447*0Sstevel@tonic-gate 		    (uintptr_t)&ctxp->uc_stack) != sizeof (*stkp))
448*0Sstevel@tonic-gate 			return (-1);
449*0Sstevel@tonic-gate #ifdef _LP64
450*0Sstevel@tonic-gate 	} else {
451*0Sstevel@tonic-gate 		ucontext32_t *ctxp = (void *)ls.pr_oldcontext;
452*0Sstevel@tonic-gate 		stack32_t stk32;
453*0Sstevel@tonic-gate 
454*0Sstevel@tonic-gate 		if (Pread(P, &stk32, sizeof (stk32),
455*0Sstevel@tonic-gate 		    (uintptr_t)&ctxp->uc_stack) != sizeof (stk32))
456*0Sstevel@tonic-gate 			return (-1);
457*0Sstevel@tonic-gate 
458*0Sstevel@tonic-gate 		stack_32_to_n(&stk32, stkp);
459*0Sstevel@tonic-gate #endif
460*0Sstevel@tonic-gate 	}
461*0Sstevel@tonic-gate 
462*0Sstevel@tonic-gate 	return (0);
463*0Sstevel@tonic-gate }
464*0Sstevel@tonic-gate 
465*0Sstevel@tonic-gate int
466*0Sstevel@tonic-gate Plwp_alt_stack(struct ps_prochandle *P, lwpid_t lwpid, stack_t *stkp)
467*0Sstevel@tonic-gate {
468*0Sstevel@tonic-gate 	if (P->state == PS_IDLE) {
469*0Sstevel@tonic-gate 		errno = ENODATA;
470*0Sstevel@tonic-gate 		return (-1);
471*0Sstevel@tonic-gate 	}
472*0Sstevel@tonic-gate 
473*0Sstevel@tonic-gate 	if (P->state != PS_DEAD) {
474*0Sstevel@tonic-gate 		lwpstatus_t ls;
475*0Sstevel@tonic-gate 
476*0Sstevel@tonic-gate 		if (getlwpfile(P, lwpid, "lwpstatus", &ls, sizeof (ls)) != 0)
477*0Sstevel@tonic-gate 			return (-1);
478*0Sstevel@tonic-gate 
479*0Sstevel@tonic-gate 		if (ls.pr_altstack.ss_flags & SS_DISABLE) {
480*0Sstevel@tonic-gate 			errno = ENODATA;
481*0Sstevel@tonic-gate 			return (-1);
482*0Sstevel@tonic-gate 		}
483*0Sstevel@tonic-gate 
484*0Sstevel@tonic-gate 		*stkp = ls.pr_altstack;
485*0Sstevel@tonic-gate 	} else {
486*0Sstevel@tonic-gate 		lwp_info_t *lwp;
487*0Sstevel@tonic-gate 
488*0Sstevel@tonic-gate 		if ((lwp = getlwpcore(P, lwpid)) == NULL)
489*0Sstevel@tonic-gate 			return (-1);
490*0Sstevel@tonic-gate 
491*0Sstevel@tonic-gate 		if (lwp->lwp_status.pr_altstack.ss_flags & SS_DISABLE) {
492*0Sstevel@tonic-gate 			errno = ENODATA;
493*0Sstevel@tonic-gate 			return (-1);
494*0Sstevel@tonic-gate 		}
495*0Sstevel@tonic-gate 
496*0Sstevel@tonic-gate 		*stkp = lwp->lwp_status.pr_altstack;
497*0Sstevel@tonic-gate 	}
498*0Sstevel@tonic-gate 
499*0Sstevel@tonic-gate 	return (0);
500*0Sstevel@tonic-gate }
501