xref: /onnv-gate/usr/src/lib/libc/sparc/sys/ptrace.c (revision 0:68f95e015346)
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  * ptrace(2) interface built on top of proc(4).
24*0Sstevel@tonic-gate  */
25*0Sstevel@tonic-gate 
26*0Sstevel@tonic-gate /*
27*0Sstevel@tonic-gate  * Copyright 1990-2003 Sun Microsystems, Inc.  All rights reserved.
28*0Sstevel@tonic-gate  * Use is subject to license terms.
29*0Sstevel@tonic-gate  */
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate #pragma weak ptrace = _ptrace
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate #include "synonyms.h"
36*0Sstevel@tonic-gate #include <stdio.h>
37*0Sstevel@tonic-gate #include <stdlib.h>
38*0Sstevel@tonic-gate #include <unistd.h>
39*0Sstevel@tonic-gate #include <memory.h>
40*0Sstevel@tonic-gate #include <string.h>
41*0Sstevel@tonic-gate #include <fcntl.h>
42*0Sstevel@tonic-gate #include <errno.h>
43*0Sstevel@tonic-gate #include <sys/types.h>
44*0Sstevel@tonic-gate #include <sys/uio.h>
45*0Sstevel@tonic-gate #include <signal.h>
46*0Sstevel@tonic-gate #include <sys/siginfo.h>
47*0Sstevel@tonic-gate #include <sys/fault.h>
48*0Sstevel@tonic-gate #include <sys/syscall.h>
49*0Sstevel@tonic-gate #include <procfs.h>
50*0Sstevel@tonic-gate #include <sys/psw.h>
51*0Sstevel@tonic-gate #include <sys/user.h>
52*0Sstevel@tonic-gate /*
53*0Sstevel@tonic-gate  * mtlib.h must precede thread.h
54*0Sstevel@tonic-gate  */
55*0Sstevel@tonic-gate #include <mtlib.h>
56*0Sstevel@tonic-gate #include <thread.h>
57*0Sstevel@tonic-gate #include <synch.h>
58*0Sstevel@tonic-gate #include <unistd.h>
59*0Sstevel@tonic-gate 
60*0Sstevel@tonic-gate static mutex_t pt_lock = DEFAULTMUTEX;
61*0Sstevel@tonic-gate 
62*0Sstevel@tonic-gate #define	TRUE	1
63*0Sstevel@tonic-gate #define	FALSE	0
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate /*
66*0Sstevel@tonic-gate  * All my children...
67*0Sstevel@tonic-gate  */
68*0Sstevel@tonic-gate typedef struct cstatus {
69*0Sstevel@tonic-gate 	struct cstatus	*next;		/* linked list			*/
70*0Sstevel@tonic-gate 	pid_t		pid;		/* process-id			*/
71*0Sstevel@tonic-gate 	int		asfd;		/* /proc/<pid>/as		*/
72*0Sstevel@tonic-gate 	int		ctlfd;		/* /proc/<pid>/ctl		*/
73*0Sstevel@tonic-gate 	int		statusfd;	/* /proc/<pid>/status		*/
74*0Sstevel@tonic-gate 	int		flags;		/* see below			*/
75*0Sstevel@tonic-gate 	pstatus_t	pstatus;	/* from /proc/<pid>/status	*/
76*0Sstevel@tonic-gate 	user_t		user;		/* manufactured u-block		*/
77*0Sstevel@tonic-gate } cstatus_t;
78*0Sstevel@tonic-gate 
79*0Sstevel@tonic-gate /* flags */
80*0Sstevel@tonic-gate #define	CS_SETREGS	0x01		/* set registers on run		*/
81*0Sstevel@tonic-gate #define	CS_PSARGS	0x02		/* u_psargs[] has been fetched	*/
82*0Sstevel@tonic-gate #define	CS_SIGNAL	0x04		/* u_signal[] has been fetched	*/
83*0Sstevel@tonic-gate 
84*0Sstevel@tonic-gate #define	NULLCP	((cstatus_t *)0)
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate static cstatus_t *childp = NULLCP;
87*0Sstevel@tonic-gate 
88*0Sstevel@tonic-gate /* fake u-block offsets */
89*0Sstevel@tonic-gate #define	UP		((user_t *)NULL)
90*0Sstevel@tonic-gate #define	U_REG		((int)(&UP->u_reg[0]))
91*0Sstevel@tonic-gate #define	U_AR0		((int)(&UP->u_ar0))
92*0Sstevel@tonic-gate #define	U_PSARGS	((int)(&UP->u_psargs[0]))
93*0Sstevel@tonic-gate #define	U_SIGNAL	((int)(&UP->u_signal[0]))
94*0Sstevel@tonic-gate #define	U_CODE		((int)(&UP->u_code))
95*0Sstevel@tonic-gate #define	U_ADDR		((int)(&UP->u_addr))
96*0Sstevel@tonic-gate #define	U_END		((int)sizeof (user_t))
97*0Sstevel@tonic-gate #define	REGADDR		0xffff0000	/* arbitrary kernel address for u_ar0 */
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate /* external routines defined in this module */
100*0Sstevel@tonic-gate extern	int	ptrace(int, pid_t, int, int);
101*0Sstevel@tonic-gate /* static routines defined in this module */
102*0Sstevel@tonic-gate static	cstatus_t *FindProc(pid_t);
103*0Sstevel@tonic-gate static	void	CheckAllProcs(void);
104*0Sstevel@tonic-gate static	int	Dupfd(int, int);
105*0Sstevel@tonic-gate static	void	MakeProcName(char *, pid_t);
106*0Sstevel@tonic-gate static	int	OpenProc(cstatus_t *);
107*0Sstevel@tonic-gate static	void	CloseProc(cstatus_t *);
108*0Sstevel@tonic-gate static	cstatus_t *GrabProc(pid_t);
109*0Sstevel@tonic-gate static	void	ReleaseProc(cstatus_t *);
110*0Sstevel@tonic-gate static	int	ProcUpdate(cstatus_t *);
111*0Sstevel@tonic-gate static	void	MakeUser(cstatus_t *);
112*0Sstevel@tonic-gate static	void	GetPsargs(cstatus_t *);
113*0Sstevel@tonic-gate static	void	GetSignal(cstatus_t *);
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate #if PTRACE_DEBUG
116*0Sstevel@tonic-gate /* for debugging */
117*0Sstevel@tonic-gate static char *
118*0Sstevel@tonic-gate map(int request)
119*0Sstevel@tonic-gate {
120*0Sstevel@tonic-gate 	static char name[20];
121*0Sstevel@tonic-gate 
122*0Sstevel@tonic-gate 	switch (request) {
123*0Sstevel@tonic-gate 	case 0:	return ("PTRACE_TRACEME");
124*0Sstevel@tonic-gate 	case 1:	return ("PTRACE_PEEKTEXT");
125*0Sstevel@tonic-gate 	case 2:	return ("PTRACE_PEEKDATA");
126*0Sstevel@tonic-gate 	case 3:	return ("PTRACE_PEEKUSER");
127*0Sstevel@tonic-gate 	case 4:	return ("PTRACE_POKETEXT");
128*0Sstevel@tonic-gate 	case 5:	return ("PTRACE_POKEDATA");
129*0Sstevel@tonic-gate 	case 6:	return ("PTRACE_POKEUSER");
130*0Sstevel@tonic-gate 	case 7:	return ("PTRACE_CONT");
131*0Sstevel@tonic-gate 	case 8:	return ("PTRACE_KILL");
132*0Sstevel@tonic-gate 	case 9:	return ("PTRACE_SINGLESTEP");
133*0Sstevel@tonic-gate 	}
134*0Sstevel@tonic-gate 	(void) sprintf(name, "%d", request);
135*0Sstevel@tonic-gate 	return (name);
136*0Sstevel@tonic-gate }
137*0Sstevel@tonic-gate #endif
138*0Sstevel@tonic-gate 
139*0Sstevel@tonic-gate int
140*0Sstevel@tonic-gate ptrace(int request, pid_t pid, int addr, int data)
141*0Sstevel@tonic-gate {
142*0Sstevel@tonic-gate 	pstatus_t *ps;
143*0Sstevel@tonic-gate 	cstatus_t *cp;
144*0Sstevel@tonic-gate 	unsigned xaddr;
145*0Sstevel@tonic-gate 	struct {
146*0Sstevel@tonic-gate 		long cmd;
147*0Sstevel@tonic-gate 		union {
148*0Sstevel@tonic-gate 			long flags;
149*0Sstevel@tonic-gate 			sigset_t signals;
150*0Sstevel@tonic-gate 			fltset_t faults;
151*0Sstevel@tonic-gate 			sysset_t syscalls;
152*0Sstevel@tonic-gate 			siginfo_t siginfo;
153*0Sstevel@tonic-gate 		} arg;
154*0Sstevel@tonic-gate 	} ctl;
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate #if PTRACE_DEBUG
157*0Sstevel@tonic-gate 	fprintf(stderr, " ptrace(%s, 0x%X, 0x%X, 0x%X)\n",
158*0Sstevel@tonic-gate 		map(request), pid, addr, data);
159*0Sstevel@tonic-gate #endif
160*0Sstevel@tonic-gate 
161*0Sstevel@tonic-gate 	(void) _private_mutex_lock(&pt_lock);
162*0Sstevel@tonic-gate 
163*0Sstevel@tonic-gate 	if (request == 0) {	/* PTRACE_TRACEME, executed by traced process */
164*0Sstevel@tonic-gate 		/*
165*0Sstevel@tonic-gate 		 * Set stop-on-all-signals and nothing else.
166*0Sstevel@tonic-gate 		 * Turn off inherit-on-fork flag (grandchildren run away).
167*0Sstevel@tonic-gate 		 * Set ptrace-compatible flag.
168*0Sstevel@tonic-gate 		 */
169*0Sstevel@tonic-gate 		char procname[64];	/* /proc/<pid>/ctl */
170*0Sstevel@tonic-gate 		int fd;
171*0Sstevel@tonic-gate 
172*0Sstevel@tonic-gate 		MakeProcName(procname, getpid());
173*0Sstevel@tonic-gate 		(void) strcat(procname, "/ctl");
174*0Sstevel@tonic-gate 		if ((fd = open(procname, O_WRONLY, 0)) < 0)
175*0Sstevel@tonic-gate 			exit(255);
176*0Sstevel@tonic-gate 		ctl.cmd = PCSTRACE;
177*0Sstevel@tonic-gate 		prfillset(&ctl.arg.signals);
178*0Sstevel@tonic-gate 		if (write(fd, (char *)&ctl, sizeof (long)+sizeof (sigset_t))
179*0Sstevel@tonic-gate 		    != sizeof (long)+sizeof (sigset_t))
180*0Sstevel@tonic-gate 			exit(255);
181*0Sstevel@tonic-gate 		ctl.cmd = PCSFAULT;
182*0Sstevel@tonic-gate 		premptyset(&ctl.arg.faults);
183*0Sstevel@tonic-gate 		if (write(fd, (char *)&ctl, sizeof (long)+sizeof (fltset_t))
184*0Sstevel@tonic-gate 		    != sizeof (long)+sizeof (fltset_t))
185*0Sstevel@tonic-gate 			exit(255);
186*0Sstevel@tonic-gate 		ctl.cmd = PCSENTRY;
187*0Sstevel@tonic-gate 		premptyset(&ctl.arg.syscalls);
188*0Sstevel@tonic-gate 		if (write(fd, (char *)&ctl, sizeof (long)+sizeof (sysset_t))
189*0Sstevel@tonic-gate 		    != sizeof (long)+sizeof (sysset_t))
190*0Sstevel@tonic-gate 			exit(255);
191*0Sstevel@tonic-gate 		ctl.cmd = PCSEXIT;
192*0Sstevel@tonic-gate 		premptyset(&ctl.arg.syscalls);
193*0Sstevel@tonic-gate 		if (write(fd, (char *)&ctl, sizeof (long)+sizeof (sysset_t))
194*0Sstevel@tonic-gate 		    != sizeof (long)+sizeof (sysset_t))
195*0Sstevel@tonic-gate 			exit(255);
196*0Sstevel@tonic-gate 		ctl.cmd = PCUNSET;
197*0Sstevel@tonic-gate 		ctl.arg.flags = PR_FORK;
198*0Sstevel@tonic-gate 		if (write(fd, (char *)&ctl, sizeof (long)+sizeof (long))
199*0Sstevel@tonic-gate 		    != sizeof (long)+sizeof (long))
200*0Sstevel@tonic-gate 			exit(255);
201*0Sstevel@tonic-gate 		ctl.cmd = PCSET;
202*0Sstevel@tonic-gate 		ctl.arg.flags = PR_PTRACE;
203*0Sstevel@tonic-gate 		if (write(fd, (char *)&ctl, sizeof (long)+sizeof (long))
204*0Sstevel@tonic-gate 		    != sizeof (long)+sizeof (long))
205*0Sstevel@tonic-gate 			exit(255);
206*0Sstevel@tonic-gate 		if (close(fd) != 0)
207*0Sstevel@tonic-gate 			exit(255);
208*0Sstevel@tonic-gate 
209*0Sstevel@tonic-gate 		(void) _private_mutex_unlock(&pt_lock);
210*0Sstevel@tonic-gate 		return (0);
211*0Sstevel@tonic-gate 	}
212*0Sstevel@tonic-gate 
213*0Sstevel@tonic-gate again:
214*0Sstevel@tonic-gate 	errno = 0;
215*0Sstevel@tonic-gate 
216*0Sstevel@tonic-gate 	/* find the cstatus structure corresponding to pid */
217*0Sstevel@tonic-gate 	if ((cp = GrabProc(pid)) == NULLCP)
218*0Sstevel@tonic-gate 		goto esrch;
219*0Sstevel@tonic-gate 
220*0Sstevel@tonic-gate 	ps = &cp->pstatus;
221*0Sstevel@tonic-gate 	if (!(ps->pr_flags & PR_ISTOP)) {
222*0Sstevel@tonic-gate 		if (ProcUpdate(cp) != 0) {
223*0Sstevel@tonic-gate 			ReleaseProc(cp);
224*0Sstevel@tonic-gate 			goto esrch;
225*0Sstevel@tonic-gate 		}
226*0Sstevel@tonic-gate 		if (!(ps->pr_flags & PR_ISTOP))
227*0Sstevel@tonic-gate 			goto esrch;
228*0Sstevel@tonic-gate 	}
229*0Sstevel@tonic-gate 
230*0Sstevel@tonic-gate 	/*
231*0Sstevel@tonic-gate 	 * Process the request.
232*0Sstevel@tonic-gate 	 */
233*0Sstevel@tonic-gate 	errno = 0;
234*0Sstevel@tonic-gate 	switch (request) {
235*0Sstevel@tonic-gate 	case 1:		/* PTRACE_PEEKTEXT */
236*0Sstevel@tonic-gate 	case 2:		/* PTRACE_PEEKDATA */
237*0Sstevel@tonic-gate 		if (addr & 03)
238*0Sstevel@tonic-gate 			goto eio;
239*0Sstevel@tonic-gate 		if (pread(cp->asfd, (char *)&data, sizeof (data), (off_t)addr)
240*0Sstevel@tonic-gate 		    == sizeof (data)) {
241*0Sstevel@tonic-gate 			(void) _private_mutex_unlock(&pt_lock);
242*0Sstevel@tonic-gate 			return (data);
243*0Sstevel@tonic-gate 		}
244*0Sstevel@tonic-gate 		goto eio;
245*0Sstevel@tonic-gate 
246*0Sstevel@tonic-gate 	case 3:		/* PTRACE_PEEKUSER */
247*0Sstevel@tonic-gate 		if (addr & 03)
248*0Sstevel@tonic-gate 			goto eio;
249*0Sstevel@tonic-gate 		xaddr = addr;
250*0Sstevel@tonic-gate 		if (xaddr >= REGADDR && xaddr < REGADDR+sizeof (gregset_t))
251*0Sstevel@tonic-gate 			xaddr -= REGADDR-U_REG;
252*0Sstevel@tonic-gate 		if (xaddr >= U_PSARGS && xaddr < U_PSARGS+sizeof (UP->u_psargs))
253*0Sstevel@tonic-gate 			GetPsargs(cp);
254*0Sstevel@tonic-gate 		if (xaddr >= U_SIGNAL && xaddr < U_SIGNAL+sizeof (UP->u_signal))
255*0Sstevel@tonic-gate 			GetSignal(cp);
256*0Sstevel@tonic-gate 		if ((int)xaddr >= 0 && xaddr < U_END) {
257*0Sstevel@tonic-gate 			/* LINTED pointer alignment */
258*0Sstevel@tonic-gate 			data = *((int *)((caddr_t)(&cp->user) + xaddr));
259*0Sstevel@tonic-gate 			(void) _private_mutex_unlock(&pt_lock);
260*0Sstevel@tonic-gate 			return (data);
261*0Sstevel@tonic-gate 		}
262*0Sstevel@tonic-gate 		goto eio;
263*0Sstevel@tonic-gate 
264*0Sstevel@tonic-gate 	case 4:		/* PTRACE_POKETEXT */
265*0Sstevel@tonic-gate 	case 5:		/* PTRACE_POKEDATA */
266*0Sstevel@tonic-gate 		if (addr & 03)
267*0Sstevel@tonic-gate 			goto eio;
268*0Sstevel@tonic-gate 		xaddr = addr;
269*0Sstevel@tonic-gate 		if (xaddr >= (unsigned)cp->user.u_reg[REG_SP] &&
270*0Sstevel@tonic-gate 		    xaddr < (unsigned)cp->user.u_reg[REG_SP]+16*sizeof (int))
271*0Sstevel@tonic-gate 			cp->flags |= CS_SETREGS;
272*0Sstevel@tonic-gate 		if (pwrite(cp->asfd, (char *)&data, sizeof (data), (off_t)addr)
273*0Sstevel@tonic-gate 		    == sizeof (data)) {
274*0Sstevel@tonic-gate 			(void) _private_mutex_unlock(&pt_lock);
275*0Sstevel@tonic-gate 			return (data);
276*0Sstevel@tonic-gate 		}
277*0Sstevel@tonic-gate 		goto eio;
278*0Sstevel@tonic-gate 
279*0Sstevel@tonic-gate 	case 6:		/* PTRACE_POKEUSER */
280*0Sstevel@tonic-gate 		if (addr & 03)
281*0Sstevel@tonic-gate 			goto eio;
282*0Sstevel@tonic-gate 		xaddr = addr;
283*0Sstevel@tonic-gate 		if (xaddr >= REGADDR && xaddr < REGADDR+sizeof (gregset_t))
284*0Sstevel@tonic-gate 			xaddr -= REGADDR-U_REG;
285*0Sstevel@tonic-gate 		if ((int)xaddr >= U_REG && xaddr < U_REG+sizeof (gregset_t)) {
286*0Sstevel@tonic-gate 			int rx = (xaddr-U_REG)/sizeof (greg_t);
287*0Sstevel@tonic-gate 			if (rx == REG_PS)
288*0Sstevel@tonic-gate 				data = (cp->user.u_reg[REG_PS] &
289*0Sstevel@tonic-gate 				    ~PSL_USERMASK) | (data & PSL_USERMASK);
290*0Sstevel@tonic-gate 			else if (rx == REG_SP || rx == REG_PC || rx == REG_nPC)
291*0Sstevel@tonic-gate 				data &= ~03;
292*0Sstevel@tonic-gate 			cp->user.u_reg[rx] = data;
293*0Sstevel@tonic-gate 			cp->flags |= CS_SETREGS;
294*0Sstevel@tonic-gate 			(void) _private_mutex_unlock(&pt_lock);
295*0Sstevel@tonic-gate 			return (data);
296*0Sstevel@tonic-gate 		}
297*0Sstevel@tonic-gate 		goto eio;
298*0Sstevel@tonic-gate 
299*0Sstevel@tonic-gate 	case 7:		/* PTRACE_CONT */
300*0Sstevel@tonic-gate 	case 9:		/* PTRACE_SINGLESTEP */
301*0Sstevel@tonic-gate 	    {
302*0Sstevel@tonic-gate 		long runctl[3];
303*0Sstevel@tonic-gate 
304*0Sstevel@tonic-gate 		if (cp->flags & CS_SETREGS) {
305*0Sstevel@tonic-gate 			long cmd;
306*0Sstevel@tonic-gate 			iovec_t iov[2];
307*0Sstevel@tonic-gate 
308*0Sstevel@tonic-gate 			ps->pr_lwp.pr_reg[R_PSR] = cp->user.u_reg[REG_PSR];
309*0Sstevel@tonic-gate 			ps->pr_lwp.pr_reg[R_PC]  = cp->user.u_reg[REG_PC];
310*0Sstevel@tonic-gate 			ps->pr_lwp.pr_reg[R_nPC] = cp->user.u_reg[REG_nPC];
311*0Sstevel@tonic-gate 			ps->pr_lwp.pr_reg[R_Y]   = cp->user.u_reg[REG_Y];
312*0Sstevel@tonic-gate 			ps->pr_lwp.pr_reg[R_G1]  = cp->user.u_reg[REG_G1];
313*0Sstevel@tonic-gate 			ps->pr_lwp.pr_reg[R_G2]  = cp->user.u_reg[REG_G2];
314*0Sstevel@tonic-gate 			ps->pr_lwp.pr_reg[R_G3]  = cp->user.u_reg[REG_G3];
315*0Sstevel@tonic-gate 			ps->pr_lwp.pr_reg[R_G4]  = cp->user.u_reg[REG_G4];
316*0Sstevel@tonic-gate 			ps->pr_lwp.pr_reg[R_G5]  = cp->user.u_reg[REG_G5];
317*0Sstevel@tonic-gate 			ps->pr_lwp.pr_reg[R_G6]  = cp->user.u_reg[REG_G6];
318*0Sstevel@tonic-gate 			ps->pr_lwp.pr_reg[R_G7]  = cp->user.u_reg[REG_G7];
319*0Sstevel@tonic-gate 			ps->pr_lwp.pr_reg[R_O0]  = cp->user.u_reg[REG_O0];
320*0Sstevel@tonic-gate 			ps->pr_lwp.pr_reg[R_O1]  = cp->user.u_reg[REG_O1];
321*0Sstevel@tonic-gate 			ps->pr_lwp.pr_reg[R_O2]  = cp->user.u_reg[REG_O2];
322*0Sstevel@tonic-gate 			ps->pr_lwp.pr_reg[R_O3]  = cp->user.u_reg[REG_O3];
323*0Sstevel@tonic-gate 			ps->pr_lwp.pr_reg[R_O4]  = cp->user.u_reg[REG_O4];
324*0Sstevel@tonic-gate 			ps->pr_lwp.pr_reg[R_O5]  = cp->user.u_reg[REG_O5];
325*0Sstevel@tonic-gate 			ps->pr_lwp.pr_reg[R_O6]  = cp->user.u_reg[REG_O6];
326*0Sstevel@tonic-gate 			ps->pr_lwp.pr_reg[R_O7]  = cp->user.u_reg[REG_O7];
327*0Sstevel@tonic-gate 			(void) pread(cp->asfd, (char *)&ps->pr_lwp.pr_reg[R_L0],
328*0Sstevel@tonic-gate 				16*sizeof (int), (off_t)cp->user.u_reg[REG_SP]);
329*0Sstevel@tonic-gate 			cmd = PCSREG;
330*0Sstevel@tonic-gate 			iov[0].iov_base = (caddr_t)&cmd;
331*0Sstevel@tonic-gate 			iov[0].iov_len = sizeof (long);
332*0Sstevel@tonic-gate 			iov[1].iov_base = (caddr_t)&ps->pr_lwp.pr_reg[0];
333*0Sstevel@tonic-gate 			iov[1].iov_len = sizeof (ps->pr_lwp.pr_reg);
334*0Sstevel@tonic-gate 			if (writev(cp->ctlfd, iov, 2) < 0)
335*0Sstevel@tonic-gate 				goto tryagain;
336*0Sstevel@tonic-gate 		}
337*0Sstevel@tonic-gate 		if (addr != 1 &&	/* new virtual address */
338*0Sstevel@tonic-gate 		    (addr & ~03) != cp->user.u_reg[REG_PC]) {
339*0Sstevel@tonic-gate 			runctl[0] = PCSVADDR;
340*0Sstevel@tonic-gate 			runctl[1] = (addr & ~03);
341*0Sstevel@tonic-gate 			if (write(cp->ctlfd, (char *)runctl, 2*sizeof (long))
342*0Sstevel@tonic-gate 			    != 2*sizeof (long))
343*0Sstevel@tonic-gate 				goto tryagain;
344*0Sstevel@tonic-gate 		}
345*0Sstevel@tonic-gate 		/* make data the current signal */
346*0Sstevel@tonic-gate 		if (data != 0 && data != ps->pr_lwp.pr_cursig) {
347*0Sstevel@tonic-gate 			(void) memset((char *)&ctl.arg.siginfo, 0,
348*0Sstevel@tonic-gate 			    sizeof (siginfo_t));
349*0Sstevel@tonic-gate 			ctl.arg.siginfo.si_signo = data;
350*0Sstevel@tonic-gate 			ctl.cmd = PCSSIG;
351*0Sstevel@tonic-gate 			if (write(cp->ctlfd, (char *)&ctl,
352*0Sstevel@tonic-gate 			    sizeof (long)+sizeof (siginfo_t))
353*0Sstevel@tonic-gate 			    != sizeof (long)+sizeof (siginfo_t))
354*0Sstevel@tonic-gate 				goto tryagain;
355*0Sstevel@tonic-gate 		}
356*0Sstevel@tonic-gate 		if (data == 0)
357*0Sstevel@tonic-gate 			runctl[0] = PCCSIG;
358*0Sstevel@tonic-gate 		else
359*0Sstevel@tonic-gate 			runctl[0] = PCNULL;
360*0Sstevel@tonic-gate 		runctl[1] = PCRUN;
361*0Sstevel@tonic-gate 		runctl[2] = (request == 9)? PRSTEP : 0;
362*0Sstevel@tonic-gate 		if (write(cp->ctlfd, (char *)runctl, 3*sizeof (long))
363*0Sstevel@tonic-gate 		    != 3*sizeof (long)) {
364*0Sstevel@tonic-gate 			if (errno == ENOENT) {
365*0Sstevel@tonic-gate 				/* current signal must have killed it */
366*0Sstevel@tonic-gate 				ReleaseProc(cp);
367*0Sstevel@tonic-gate 				(void) _private_mutex_unlock(&pt_lock);
368*0Sstevel@tonic-gate 				return (data);
369*0Sstevel@tonic-gate 			}
370*0Sstevel@tonic-gate 			goto tryagain;
371*0Sstevel@tonic-gate 		}
372*0Sstevel@tonic-gate 		(void) memset((char *)ps, 0, sizeof (pstatus_t));
373*0Sstevel@tonic-gate 		cp->flags = 0;
374*0Sstevel@tonic-gate 		(void) _private_mutex_unlock(&pt_lock);
375*0Sstevel@tonic-gate 		return (data);
376*0Sstevel@tonic-gate 	    }
377*0Sstevel@tonic-gate 
378*0Sstevel@tonic-gate 	case 8:		/* PTRACE_KILL */
379*0Sstevel@tonic-gate 		/* overkill? */
380*0Sstevel@tonic-gate 		(void) memset((char *)&ctl.arg.siginfo, 0, sizeof (siginfo_t));
381*0Sstevel@tonic-gate 		ctl.arg.siginfo.si_signo = SIGKILL;
382*0Sstevel@tonic-gate 		ctl.cmd = PCSSIG;
383*0Sstevel@tonic-gate 		(void) write(cp->ctlfd, (char *)&ctl,
384*0Sstevel@tonic-gate 		    sizeof (long)+sizeof (siginfo_t));
385*0Sstevel@tonic-gate 		(void) kill(pid, SIGKILL);
386*0Sstevel@tonic-gate 		ReleaseProc(cp);
387*0Sstevel@tonic-gate 		(void) _private_mutex_unlock(&pt_lock);
388*0Sstevel@tonic-gate 		return (0);
389*0Sstevel@tonic-gate 
390*0Sstevel@tonic-gate 	default:
391*0Sstevel@tonic-gate 		goto eio;
392*0Sstevel@tonic-gate 	}
393*0Sstevel@tonic-gate 
394*0Sstevel@tonic-gate tryagain:
395*0Sstevel@tonic-gate 	if (errno == EAGAIN) {
396*0Sstevel@tonic-gate 		if (OpenProc(cp) == 0)
397*0Sstevel@tonic-gate 			goto again;
398*0Sstevel@tonic-gate 		ReleaseProc(cp);
399*0Sstevel@tonic-gate 	}
400*0Sstevel@tonic-gate eio:
401*0Sstevel@tonic-gate 	errno = EIO;
402*0Sstevel@tonic-gate 	(void) _private_mutex_unlock(&pt_lock);
403*0Sstevel@tonic-gate 	return (-1);
404*0Sstevel@tonic-gate esrch:
405*0Sstevel@tonic-gate 	errno = ESRCH;
406*0Sstevel@tonic-gate 	(void) _private_mutex_unlock(&pt_lock);
407*0Sstevel@tonic-gate 	return (-1);
408*0Sstevel@tonic-gate }
409*0Sstevel@tonic-gate 
410*0Sstevel@tonic-gate /*
411*0Sstevel@tonic-gate  * Find the cstatus structure corresponding to pid.
412*0Sstevel@tonic-gate  */
413*0Sstevel@tonic-gate static cstatus_t *
414*0Sstevel@tonic-gate FindProc(pid_t pid)
415*0Sstevel@tonic-gate {
416*0Sstevel@tonic-gate 	cstatus_t *cp;
417*0Sstevel@tonic-gate 
418*0Sstevel@tonic-gate 	for (cp = childp; cp != NULLCP; cp = cp->next)
419*0Sstevel@tonic-gate 		if (cp->pid == pid)
420*0Sstevel@tonic-gate 			break;
421*0Sstevel@tonic-gate 
422*0Sstevel@tonic-gate 	return (cp);
423*0Sstevel@tonic-gate }
424*0Sstevel@tonic-gate 
425*0Sstevel@tonic-gate /*
426*0Sstevel@tonic-gate  * Check every proc for existence, release those that are gone.
427*0Sstevel@tonic-gate  * Be careful about the linked list; ReleaseProc() changes it.
428*0Sstevel@tonic-gate  */
429*0Sstevel@tonic-gate static void
430*0Sstevel@tonic-gate CheckAllProcs()
431*0Sstevel@tonic-gate {
432*0Sstevel@tonic-gate 	cstatus_t *cp = childp;
433*0Sstevel@tonic-gate 
434*0Sstevel@tonic-gate 	while (cp != NULLCP) {
435*0Sstevel@tonic-gate 		cstatus_t *next = cp->next;
436*0Sstevel@tonic-gate 
437*0Sstevel@tonic-gate 		if (ProcUpdate(cp) != 0)
438*0Sstevel@tonic-gate 			ReleaseProc(cp);
439*0Sstevel@tonic-gate 		cp = next;
440*0Sstevel@tonic-gate 	}
441*0Sstevel@tonic-gate }
442*0Sstevel@tonic-gate 
443*0Sstevel@tonic-gate /*
444*0Sstevel@tonic-gate  * Utility for OpenProc().
445*0Sstevel@tonic-gate  */
446*0Sstevel@tonic-gate static int
447*0Sstevel@tonic-gate Dupfd(int fd, int dfd)
448*0Sstevel@tonic-gate {
449*0Sstevel@tonic-gate 	/*
450*0Sstevel@tonic-gate 	 * Make sure fd not one of 0, 1, or 2 to avoid stdio interference.
451*0Sstevel@tonic-gate 	 * Also, if dfd is greater than 2, dup fd to be exactly dfd.
452*0Sstevel@tonic-gate 	 */
453*0Sstevel@tonic-gate 	if (dfd > 2 || (0 <= fd && fd <= 2)) {
454*0Sstevel@tonic-gate 		if (dfd > 2 && fd != dfd)
455*0Sstevel@tonic-gate 			(void) close(dfd);
456*0Sstevel@tonic-gate 		else
457*0Sstevel@tonic-gate 			dfd = 3;
458*0Sstevel@tonic-gate 		if (fd != dfd) {
459*0Sstevel@tonic-gate 			dfd = fcntl(fd, F_DUPFD, (intptr_t)dfd);
460*0Sstevel@tonic-gate 			(void) close(fd);
461*0Sstevel@tonic-gate 			fd = dfd;
462*0Sstevel@tonic-gate 		}
463*0Sstevel@tonic-gate 	}
464*0Sstevel@tonic-gate 	/*
465*0Sstevel@tonic-gate 	 * Mark filedescriptor close-on-exec.
466*0Sstevel@tonic-gate 	 * Should also be close-on-return-from-fork-in-child.
467*0Sstevel@tonic-gate 	 */
468*0Sstevel@tonic-gate 	(void) fcntl(fd, F_SETFD, (intptr_t)1);
469*0Sstevel@tonic-gate 	return (fd);
470*0Sstevel@tonic-gate }
471*0Sstevel@tonic-gate 
472*0Sstevel@tonic-gate /*
473*0Sstevel@tonic-gate  * Construct the /proc directory name:  "/proc/<pid>"
474*0Sstevel@tonic-gate  * The name buffer passed by the caller must be large enough.
475*0Sstevel@tonic-gate  */
476*0Sstevel@tonic-gate static void
477*0Sstevel@tonic-gate MakeProcName(char *procname, pid_t pid)
478*0Sstevel@tonic-gate {
479*0Sstevel@tonic-gate 	(void) sprintf(procname, "/proc/%d", pid);
480*0Sstevel@tonic-gate }
481*0Sstevel@tonic-gate 
482*0Sstevel@tonic-gate /*
483*0Sstevel@tonic-gate  * Open/reopen the /proc/<pid> files.
484*0Sstevel@tonic-gate  */
485*0Sstevel@tonic-gate static int
486*0Sstevel@tonic-gate OpenProc(cstatus_t *cp)
487*0Sstevel@tonic-gate {
488*0Sstevel@tonic-gate 	char procname[64];		/* /proc/nnnnn/fname */
489*0Sstevel@tonic-gate 	char *fname;
490*0Sstevel@tonic-gate 	int fd;
491*0Sstevel@tonic-gate 	int omode;
492*0Sstevel@tonic-gate 
493*0Sstevel@tonic-gate 	MakeProcName(procname, cp->pid);
494*0Sstevel@tonic-gate 	fname = procname + strlen(procname);
495*0Sstevel@tonic-gate 
496*0Sstevel@tonic-gate 	/*
497*0Sstevel@tonic-gate 	 * Use exclusive-open only if this is the first open.
498*0Sstevel@tonic-gate 	 */
499*0Sstevel@tonic-gate 	omode = (cp->asfd > 0)? O_RDWR : (O_RDWR|O_EXCL);
500*0Sstevel@tonic-gate 	(void) strcpy(fname, "/as");
501*0Sstevel@tonic-gate 	if ((fd = open(procname, omode, 0)) < 0 ||
502*0Sstevel@tonic-gate 	    (cp->asfd = Dupfd(fd, cp->asfd)) < 0)
503*0Sstevel@tonic-gate 		goto err;
504*0Sstevel@tonic-gate 
505*0Sstevel@tonic-gate 	(void) strcpy(fname, "/ctl");
506*0Sstevel@tonic-gate 	if ((fd = open(procname, O_WRONLY, 0)) < 0 ||
507*0Sstevel@tonic-gate 	    (cp->ctlfd = Dupfd(fd, cp->ctlfd)) < 0)
508*0Sstevel@tonic-gate 		goto err;
509*0Sstevel@tonic-gate 
510*0Sstevel@tonic-gate 	(void) strcpy(fname, "/status");
511*0Sstevel@tonic-gate 	if ((fd = open(procname, O_RDONLY, 0)) < 0 ||
512*0Sstevel@tonic-gate 	    (cp->statusfd = Dupfd(fd, cp->statusfd)) < 0)
513*0Sstevel@tonic-gate 		goto err;
514*0Sstevel@tonic-gate 
515*0Sstevel@tonic-gate 	return (0);
516*0Sstevel@tonic-gate 
517*0Sstevel@tonic-gate err:
518*0Sstevel@tonic-gate 	CloseProc(cp);
519*0Sstevel@tonic-gate 	return (-1);
520*0Sstevel@tonic-gate }
521*0Sstevel@tonic-gate 
522*0Sstevel@tonic-gate /*
523*0Sstevel@tonic-gate  * Close the /proc/<pid> files.
524*0Sstevel@tonic-gate  */
525*0Sstevel@tonic-gate static void
526*0Sstevel@tonic-gate CloseProc(cstatus_t *cp)
527*0Sstevel@tonic-gate {
528*0Sstevel@tonic-gate 	if (cp->asfd > 0)
529*0Sstevel@tonic-gate 		(void) close(cp->asfd);
530*0Sstevel@tonic-gate 	if (cp->ctlfd > 0)
531*0Sstevel@tonic-gate 		(void) close(cp->ctlfd);
532*0Sstevel@tonic-gate 	if (cp->statusfd > 0)
533*0Sstevel@tonic-gate 		(void) close(cp->statusfd);
534*0Sstevel@tonic-gate 	cp->asfd = 0;
535*0Sstevel@tonic-gate 	cp->ctlfd = 0;
536*0Sstevel@tonic-gate 	cp->statusfd = 0;
537*0Sstevel@tonic-gate }
538*0Sstevel@tonic-gate 
539*0Sstevel@tonic-gate /*
540*0Sstevel@tonic-gate  * Take control of a child process.
541*0Sstevel@tonic-gate  */
542*0Sstevel@tonic-gate static cstatus_t *
543*0Sstevel@tonic-gate GrabProc(pid_t pid)
544*0Sstevel@tonic-gate {
545*0Sstevel@tonic-gate 	cstatus_t *cp;
546*0Sstevel@tonic-gate 	long ctl[2];
547*0Sstevel@tonic-gate 	pid_t ppid;
548*0Sstevel@tonic-gate 
549*0Sstevel@tonic-gate 	if (pid <= 0)
550*0Sstevel@tonic-gate 		return (NULLCP);
551*0Sstevel@tonic-gate 
552*0Sstevel@tonic-gate 	if ((cp = FindProc(pid)) != NULLCP)	/* already grabbed */
553*0Sstevel@tonic-gate 		return (cp);
554*0Sstevel@tonic-gate 
555*0Sstevel@tonic-gate 	CheckAllProcs();	/* clean up before grabbing new process */
556*0Sstevel@tonic-gate 
557*0Sstevel@tonic-gate 	cp = (cstatus_t *)malloc(sizeof (cstatus_t));
558*0Sstevel@tonic-gate 	if (cp == NULLCP)
559*0Sstevel@tonic-gate 		return (NULLCP);
560*0Sstevel@tonic-gate 	(void) memset((char *)cp, 0, sizeof (cstatus_t));
561*0Sstevel@tonic-gate 	cp->pid = pid;
562*0Sstevel@tonic-gate 
563*0Sstevel@tonic-gate 	ppid = getpid();
564*0Sstevel@tonic-gate 	while (OpenProc(cp) == 0) {
565*0Sstevel@tonic-gate 		ctl[0] = PCSET;
566*0Sstevel@tonic-gate 		ctl[1] = PR_RLC;
567*0Sstevel@tonic-gate 		errno = 0;
568*0Sstevel@tonic-gate 
569*0Sstevel@tonic-gate 		if (pread(cp->statusfd, (char *)&cp->pstatus,
570*0Sstevel@tonic-gate 		    sizeof (cp->pstatus), (off_t)0) == sizeof (cp->pstatus) &&
571*0Sstevel@tonic-gate 		    cp->pstatus.pr_ppid == ppid &&
572*0Sstevel@tonic-gate 		    (cp->pstatus.pr_flags & PR_PTRACE) &&
573*0Sstevel@tonic-gate 		    write(cp->ctlfd, (char *)ctl, 2*sizeof (long))
574*0Sstevel@tonic-gate 		    == 2*sizeof (long)) {
575*0Sstevel@tonic-gate 			cp->next = childp;
576*0Sstevel@tonic-gate 			childp = cp;
577*0Sstevel@tonic-gate 			MakeUser(cp);
578*0Sstevel@tonic-gate 			return (cp);
579*0Sstevel@tonic-gate 		}
580*0Sstevel@tonic-gate 
581*0Sstevel@tonic-gate 		if (errno != EAGAIN)
582*0Sstevel@tonic-gate 			break;
583*0Sstevel@tonic-gate 	}
584*0Sstevel@tonic-gate 
585*0Sstevel@tonic-gate 	free((char *)cp);
586*0Sstevel@tonic-gate 	return (NULLCP);
587*0Sstevel@tonic-gate }
588*0Sstevel@tonic-gate 
589*0Sstevel@tonic-gate /*
590*0Sstevel@tonic-gate  * Close the /proc/<pid> file, if open.
591*0Sstevel@tonic-gate  * Deallocate the memory used by the cstatus_t structure.
592*0Sstevel@tonic-gate  */
593*0Sstevel@tonic-gate static void
594*0Sstevel@tonic-gate ReleaseProc(cstatus_t *cp)
595*0Sstevel@tonic-gate {
596*0Sstevel@tonic-gate 	CloseProc(cp);
597*0Sstevel@tonic-gate 
598*0Sstevel@tonic-gate 	if (childp == cp)
599*0Sstevel@tonic-gate 		childp = cp->next;
600*0Sstevel@tonic-gate 	else {
601*0Sstevel@tonic-gate 		cstatus_t *pcp;
602*0Sstevel@tonic-gate 
603*0Sstevel@tonic-gate 		for (pcp = childp; pcp != NULLCP; pcp = pcp->next) {
604*0Sstevel@tonic-gate 			if (pcp->next == cp) {
605*0Sstevel@tonic-gate 				pcp->next = cp->next;
606*0Sstevel@tonic-gate 				break;
607*0Sstevel@tonic-gate 			}
608*0Sstevel@tonic-gate 		}
609*0Sstevel@tonic-gate 	}
610*0Sstevel@tonic-gate 
611*0Sstevel@tonic-gate 	free((char *)cp);
612*0Sstevel@tonic-gate }
613*0Sstevel@tonic-gate 
614*0Sstevel@tonic-gate /*
615*0Sstevel@tonic-gate  * Update process information from /proc.
616*0Sstevel@tonic-gate  * Return 0 on success, -1 on failure.
617*0Sstevel@tonic-gate  */
618*0Sstevel@tonic-gate static int
619*0Sstevel@tonic-gate ProcUpdate(cstatus_t *cp)
620*0Sstevel@tonic-gate {
621*0Sstevel@tonic-gate 	pstatus_t *ps = &cp->pstatus;
622*0Sstevel@tonic-gate 
623*0Sstevel@tonic-gate 	if (cp->flags & CS_SETREGS) {
624*0Sstevel@tonic-gate 		long cmd;
625*0Sstevel@tonic-gate 		iovec_t iov[2];
626*0Sstevel@tonic-gate 
627*0Sstevel@tonic-gate 		ps->pr_lwp.pr_reg[R_PSR] = cp->user.u_reg[REG_PSR];
628*0Sstevel@tonic-gate 		ps->pr_lwp.pr_reg[R_PC]  = cp->user.u_reg[REG_PC];
629*0Sstevel@tonic-gate 		ps->pr_lwp.pr_reg[R_nPC] = cp->user.u_reg[REG_nPC];
630*0Sstevel@tonic-gate 		ps->pr_lwp.pr_reg[R_Y]   = cp->user.u_reg[REG_Y];
631*0Sstevel@tonic-gate 		ps->pr_lwp.pr_reg[R_G1]  = cp->user.u_reg[REG_G1];
632*0Sstevel@tonic-gate 		ps->pr_lwp.pr_reg[R_G2]  = cp->user.u_reg[REG_G2];
633*0Sstevel@tonic-gate 		ps->pr_lwp.pr_reg[R_G3]  = cp->user.u_reg[REG_G3];
634*0Sstevel@tonic-gate 		ps->pr_lwp.pr_reg[R_G4]  = cp->user.u_reg[REG_G4];
635*0Sstevel@tonic-gate 		ps->pr_lwp.pr_reg[R_G5]  = cp->user.u_reg[REG_G5];
636*0Sstevel@tonic-gate 		ps->pr_lwp.pr_reg[R_G6]  = cp->user.u_reg[REG_G6];
637*0Sstevel@tonic-gate 		ps->pr_lwp.pr_reg[R_G7]  = cp->user.u_reg[REG_G7];
638*0Sstevel@tonic-gate 		ps->pr_lwp.pr_reg[R_O0]  = cp->user.u_reg[REG_O0];
639*0Sstevel@tonic-gate 		ps->pr_lwp.pr_reg[R_O1]  = cp->user.u_reg[REG_O1];
640*0Sstevel@tonic-gate 		ps->pr_lwp.pr_reg[R_O2]  = cp->user.u_reg[REG_O2];
641*0Sstevel@tonic-gate 		ps->pr_lwp.pr_reg[R_O3]  = cp->user.u_reg[REG_O3];
642*0Sstevel@tonic-gate 		ps->pr_lwp.pr_reg[R_O4]  = cp->user.u_reg[REG_O4];
643*0Sstevel@tonic-gate 		ps->pr_lwp.pr_reg[R_O5]  = cp->user.u_reg[REG_O5];
644*0Sstevel@tonic-gate 		ps->pr_lwp.pr_reg[R_O6]  = cp->user.u_reg[REG_O6];
645*0Sstevel@tonic-gate 		ps->pr_lwp.pr_reg[R_O7]  = cp->user.u_reg[REG_O7];
646*0Sstevel@tonic-gate 		(void) pread(cp->asfd, (char *)&ps->pr_lwp.pr_reg[R_L0],
647*0Sstevel@tonic-gate 			16*sizeof (int), (off_t)cp->user.u_reg[REG_SP]);
648*0Sstevel@tonic-gate 		cmd = PCSREG;
649*0Sstevel@tonic-gate 		iov[0].iov_base = (caddr_t)&cmd;
650*0Sstevel@tonic-gate 		iov[0].iov_len = sizeof (long);
651*0Sstevel@tonic-gate 		iov[1].iov_base = (caddr_t)&ps->pr_lwp.pr_reg[0];
652*0Sstevel@tonic-gate 		iov[1].iov_len = sizeof (ps->pr_lwp.pr_reg);
653*0Sstevel@tonic-gate 		(void) writev(cp->ctlfd, iov, 2);
654*0Sstevel@tonic-gate 		cp->flags &= ~CS_SETREGS;
655*0Sstevel@tonic-gate 	}
656*0Sstevel@tonic-gate 
657*0Sstevel@tonic-gate 	while (pread(cp->statusfd, (char *)ps, sizeof (*ps), (off_t)0) < 0) {
658*0Sstevel@tonic-gate 		/* attempt to regain control */
659*0Sstevel@tonic-gate 		if (errno != EINTR &&
660*0Sstevel@tonic-gate 		    !(errno == EAGAIN && OpenProc(cp) == 0))
661*0Sstevel@tonic-gate 			return (-1);
662*0Sstevel@tonic-gate 	}
663*0Sstevel@tonic-gate 
664*0Sstevel@tonic-gate 	if (ps->pr_flags & PR_ISTOP)
665*0Sstevel@tonic-gate 		MakeUser(cp);
666*0Sstevel@tonic-gate 	else
667*0Sstevel@tonic-gate 		(void) memset((char *)ps, 0, sizeof (pstatus_t));
668*0Sstevel@tonic-gate 
669*0Sstevel@tonic-gate 	return (0);
670*0Sstevel@tonic-gate }
671*0Sstevel@tonic-gate 
672*0Sstevel@tonic-gate /*
673*0Sstevel@tonic-gate  * Manufacture the contents of the fake u-block.
674*0Sstevel@tonic-gate  */
675*0Sstevel@tonic-gate static void
676*0Sstevel@tonic-gate MakeUser(cstatus_t *cp)
677*0Sstevel@tonic-gate {
678*0Sstevel@tonic-gate 	pstatus_t *ps = &cp->pstatus;
679*0Sstevel@tonic-gate 
680*0Sstevel@tonic-gate 	cp->user.u_reg[REG_PSR] = ps->pr_lwp.pr_reg[R_PSR];
681*0Sstevel@tonic-gate 	cp->user.u_reg[REG_PC]  = ps->pr_lwp.pr_reg[R_PC];
682*0Sstevel@tonic-gate 	cp->user.u_reg[REG_nPC] = ps->pr_lwp.pr_reg[R_nPC];
683*0Sstevel@tonic-gate 	cp->user.u_reg[REG_Y]   = ps->pr_lwp.pr_reg[R_Y];
684*0Sstevel@tonic-gate 	cp->user.u_reg[REG_G1]  = ps->pr_lwp.pr_reg[R_G1];
685*0Sstevel@tonic-gate 	cp->user.u_reg[REG_G2]  = ps->pr_lwp.pr_reg[R_G2];
686*0Sstevel@tonic-gate 	cp->user.u_reg[REG_G3]  = ps->pr_lwp.pr_reg[R_G3];
687*0Sstevel@tonic-gate 	cp->user.u_reg[REG_G4]  = ps->pr_lwp.pr_reg[R_G4];
688*0Sstevel@tonic-gate 	cp->user.u_reg[REG_G5]  = ps->pr_lwp.pr_reg[R_G5];
689*0Sstevel@tonic-gate 	cp->user.u_reg[REG_G6]  = ps->pr_lwp.pr_reg[R_G6];
690*0Sstevel@tonic-gate 	cp->user.u_reg[REG_G7]  = ps->pr_lwp.pr_reg[R_G7];
691*0Sstevel@tonic-gate 	cp->user.u_reg[REG_O0]  = ps->pr_lwp.pr_reg[R_O0];
692*0Sstevel@tonic-gate 	cp->user.u_reg[REG_O1]  = ps->pr_lwp.pr_reg[R_O1];
693*0Sstevel@tonic-gate 	cp->user.u_reg[REG_O2]  = ps->pr_lwp.pr_reg[R_O2];
694*0Sstevel@tonic-gate 	cp->user.u_reg[REG_O3]  = ps->pr_lwp.pr_reg[R_O3];
695*0Sstevel@tonic-gate 	cp->user.u_reg[REG_O4]  = ps->pr_lwp.pr_reg[R_O4];
696*0Sstevel@tonic-gate 	cp->user.u_reg[REG_O5]  = ps->pr_lwp.pr_reg[R_O5];
697*0Sstevel@tonic-gate 	cp->user.u_reg[REG_O6]  = ps->pr_lwp.pr_reg[R_O6];
698*0Sstevel@tonic-gate 	cp->user.u_reg[REG_O7]  = ps->pr_lwp.pr_reg[R_O7];
699*0Sstevel@tonic-gate 	cp->user.u_ar0 = (greg_t *)REGADDR;
700*0Sstevel@tonic-gate 	cp->user.u_code = ps->pr_lwp.pr_info.si_code;
701*0Sstevel@tonic-gate 	cp->user.u_addr = ps->pr_lwp.pr_info.si_addr;
702*0Sstevel@tonic-gate 	cp->flags &= ~(CS_PSARGS|CS_SIGNAL);
703*0Sstevel@tonic-gate }
704*0Sstevel@tonic-gate 
705*0Sstevel@tonic-gate /*
706*0Sstevel@tonic-gate  * Fetch the contents of u_psargs[].
707*0Sstevel@tonic-gate  */
708*0Sstevel@tonic-gate static void
709*0Sstevel@tonic-gate GetPsargs(cstatus_t *cp)
710*0Sstevel@tonic-gate {
711*0Sstevel@tonic-gate 	char procname[64];	/* /proc/<pid>/psinfo */
712*0Sstevel@tonic-gate 	int fd;
713*0Sstevel@tonic-gate 
714*0Sstevel@tonic-gate 	MakeProcName(procname, cp->pid);
715*0Sstevel@tonic-gate 	(void) strcat(procname, "/psinfo");
716*0Sstevel@tonic-gate 	if ((fd = open(procname, O_RDONLY, 0)) < 0) {
717*0Sstevel@tonic-gate 		(void) memset(cp->user.u_psargs, 0, PSARGSZ);
718*0Sstevel@tonic-gate 		return;
719*0Sstevel@tonic-gate 	}
720*0Sstevel@tonic-gate 	(void) pread(fd, cp->user.u_psargs, PSARGSZ,
721*0Sstevel@tonic-gate 	    (off_t)((psinfo_t *)0)->pr_psargs);
722*0Sstevel@tonic-gate 	(void) close(fd);
723*0Sstevel@tonic-gate 
724*0Sstevel@tonic-gate 	cp->flags |= CS_PSARGS;
725*0Sstevel@tonic-gate }
726*0Sstevel@tonic-gate 
727*0Sstevel@tonic-gate /*
728*0Sstevel@tonic-gate  * Fetch the contents of u_signal[].
729*0Sstevel@tonic-gate  */
730*0Sstevel@tonic-gate static void
731*0Sstevel@tonic-gate GetSignal(cstatus_t *cp)
732*0Sstevel@tonic-gate {
733*0Sstevel@tonic-gate 	char procname[64];	/* /proc/<pid>/sigact */
734*0Sstevel@tonic-gate 	int fd;
735*0Sstevel@tonic-gate 	struct sigaction action[MAXSIG];
736*0Sstevel@tonic-gate 	int i;
737*0Sstevel@tonic-gate 
738*0Sstevel@tonic-gate 	MakeProcName(procname, cp->pid);
739*0Sstevel@tonic-gate 	(void) strcat(procname, "/sigact");
740*0Sstevel@tonic-gate 	(void) memset((char *)action, 0, sizeof (action));
741*0Sstevel@tonic-gate 	if ((fd = open(procname, O_RDONLY, 0)) >= 0) {
742*0Sstevel@tonic-gate 		(void) read(fd, (char *)action, sizeof (action));
743*0Sstevel@tonic-gate 		(void) close(fd);
744*0Sstevel@tonic-gate 	}
745*0Sstevel@tonic-gate 	for (i = 0; i < MAXSIG; i++)
746*0Sstevel@tonic-gate 		cp->user.u_signal[i] = action[i].sa_handler;
747*0Sstevel@tonic-gate 	cp->flags |= CS_SIGNAL;
748*0Sstevel@tonic-gate }
749