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