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