xref: /onnv-gate/usr/src/lib/libc/sparc/sys/ptrace.c (revision 6812:febeba71273d)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
56515Sraf  * Common Development and Distribution License (the "License").
66515Sraf  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
216515Sraf 
220Sstevel@tonic-gate /*
236515Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
246515Sraf  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*
286515Sraf  * ptrace(2) interface built on top of proc(4).
290Sstevel@tonic-gate  */
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
320Sstevel@tonic-gate 
33*6812Sraf #pragma weak _ptrace = ptrace
340Sstevel@tonic-gate 
35*6812Sraf #include "lint.h"
360Sstevel@tonic-gate #include <stdio.h>
370Sstevel@tonic-gate #include <stdlib.h>
380Sstevel@tonic-gate #include <unistd.h>
390Sstevel@tonic-gate #include <memory.h>
400Sstevel@tonic-gate #include <string.h>
410Sstevel@tonic-gate #include <fcntl.h>
420Sstevel@tonic-gate #include <errno.h>
430Sstevel@tonic-gate #include <sys/types.h>
440Sstevel@tonic-gate #include <sys/uio.h>
450Sstevel@tonic-gate #include <signal.h>
460Sstevel@tonic-gate #include <sys/siginfo.h>
470Sstevel@tonic-gate #include <sys/fault.h>
480Sstevel@tonic-gate #include <sys/syscall.h>
490Sstevel@tonic-gate #include <procfs.h>
500Sstevel@tonic-gate #include <sys/psw.h>
510Sstevel@tonic-gate #include <sys/user.h>
520Sstevel@tonic-gate /*
530Sstevel@tonic-gate  * mtlib.h must precede thread.h
540Sstevel@tonic-gate  */
550Sstevel@tonic-gate #include <mtlib.h>
560Sstevel@tonic-gate #include <thread.h>
570Sstevel@tonic-gate #include <synch.h>
580Sstevel@tonic-gate #include <unistd.h>
590Sstevel@tonic-gate 
600Sstevel@tonic-gate static mutex_t pt_lock = DEFAULTMUTEX;
610Sstevel@tonic-gate 
620Sstevel@tonic-gate #define	TRUE	1
630Sstevel@tonic-gate #define	FALSE	0
640Sstevel@tonic-gate 
650Sstevel@tonic-gate /*
660Sstevel@tonic-gate  * All my children...
670Sstevel@tonic-gate  */
680Sstevel@tonic-gate typedef struct cstatus {
690Sstevel@tonic-gate 	struct cstatus	*next;		/* linked list			*/
700Sstevel@tonic-gate 	pid_t		pid;		/* process-id			*/
710Sstevel@tonic-gate 	int		asfd;		/* /proc/<pid>/as		*/
720Sstevel@tonic-gate 	int		ctlfd;		/* /proc/<pid>/ctl		*/
730Sstevel@tonic-gate 	int		statusfd;	/* /proc/<pid>/status		*/
740Sstevel@tonic-gate 	int		flags;		/* see below			*/
750Sstevel@tonic-gate 	pstatus_t	pstatus;	/* from /proc/<pid>/status	*/
760Sstevel@tonic-gate 	user_t		user;		/* manufactured u-block		*/
770Sstevel@tonic-gate } cstatus_t;
780Sstevel@tonic-gate 
790Sstevel@tonic-gate /* flags */
800Sstevel@tonic-gate #define	CS_SETREGS	0x01		/* set registers on run		*/
810Sstevel@tonic-gate #define	CS_PSARGS	0x02		/* u_psargs[] has been fetched	*/
820Sstevel@tonic-gate #define	CS_SIGNAL	0x04		/* u_signal[] has been fetched	*/
830Sstevel@tonic-gate 
840Sstevel@tonic-gate #define	NULLCP	((cstatus_t *)0)
850Sstevel@tonic-gate 
860Sstevel@tonic-gate static cstatus_t *childp = NULLCP;
870Sstevel@tonic-gate 
880Sstevel@tonic-gate /* fake u-block offsets */
890Sstevel@tonic-gate #define	UP		((user_t *)NULL)
900Sstevel@tonic-gate #define	U_REG		((int)(&UP->u_reg[0]))
910Sstevel@tonic-gate #define	U_AR0		((int)(&UP->u_ar0))
920Sstevel@tonic-gate #define	U_PSARGS	((int)(&UP->u_psargs[0]))
930Sstevel@tonic-gate #define	U_SIGNAL	((int)(&UP->u_signal[0]))
940Sstevel@tonic-gate #define	U_CODE		((int)(&UP->u_code))
950Sstevel@tonic-gate #define	U_ADDR		((int)(&UP->u_addr))
960Sstevel@tonic-gate #define	U_END		((int)sizeof (user_t))
970Sstevel@tonic-gate #define	REGADDR		0xffff0000	/* arbitrary kernel address for u_ar0 */
980Sstevel@tonic-gate 
990Sstevel@tonic-gate /* external routines defined in this module */
1000Sstevel@tonic-gate extern	int	ptrace(int, pid_t, int, int);
1010Sstevel@tonic-gate /* static routines defined in this module */
1020Sstevel@tonic-gate static	cstatus_t *FindProc(pid_t);
1030Sstevel@tonic-gate static	void	CheckAllProcs(void);
1040Sstevel@tonic-gate static	int	Dupfd(int, int);
1050Sstevel@tonic-gate static	void	MakeProcName(char *, pid_t);
1060Sstevel@tonic-gate static	int	OpenProc(cstatus_t *);
1070Sstevel@tonic-gate static	void	CloseProc(cstatus_t *);
1080Sstevel@tonic-gate static	cstatus_t *GrabProc(pid_t);
1090Sstevel@tonic-gate static	void	ReleaseProc(cstatus_t *);
1100Sstevel@tonic-gate static	int	ProcUpdate(cstatus_t *);
1110Sstevel@tonic-gate static	void	MakeUser(cstatus_t *);
1120Sstevel@tonic-gate static	void	GetPsargs(cstatus_t *);
1130Sstevel@tonic-gate static	void	GetSignal(cstatus_t *);
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate #if PTRACE_DEBUG
1160Sstevel@tonic-gate /* for debugging */
1170Sstevel@tonic-gate static char *
1180Sstevel@tonic-gate map(int request)
1190Sstevel@tonic-gate {
1200Sstevel@tonic-gate 	static char name[20];
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate 	switch (request) {
1230Sstevel@tonic-gate 	case 0:	return ("PTRACE_TRACEME");
1240Sstevel@tonic-gate 	case 1:	return ("PTRACE_PEEKTEXT");
1250Sstevel@tonic-gate 	case 2:	return ("PTRACE_PEEKDATA");
1260Sstevel@tonic-gate 	case 3:	return ("PTRACE_PEEKUSER");
1270Sstevel@tonic-gate 	case 4:	return ("PTRACE_POKETEXT");
1280Sstevel@tonic-gate 	case 5:	return ("PTRACE_POKEDATA");
1290Sstevel@tonic-gate 	case 6:	return ("PTRACE_POKEUSER");
1300Sstevel@tonic-gate 	case 7:	return ("PTRACE_CONT");
1310Sstevel@tonic-gate 	case 8:	return ("PTRACE_KILL");
1320Sstevel@tonic-gate 	case 9:	return ("PTRACE_SINGLESTEP");
1330Sstevel@tonic-gate 	}
1340Sstevel@tonic-gate 	(void) sprintf(name, "%d", request);
1350Sstevel@tonic-gate 	return (name);
1360Sstevel@tonic-gate }
1370Sstevel@tonic-gate #endif
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate int
1400Sstevel@tonic-gate ptrace(int request, pid_t pid, int addr, int data)
1410Sstevel@tonic-gate {
1420Sstevel@tonic-gate 	pstatus_t *ps;
1430Sstevel@tonic-gate 	cstatus_t *cp;
1440Sstevel@tonic-gate 	unsigned xaddr;
1450Sstevel@tonic-gate 	struct {
1460Sstevel@tonic-gate 		long cmd;
1470Sstevel@tonic-gate 		union {
1480Sstevel@tonic-gate 			long flags;
1490Sstevel@tonic-gate 			sigset_t signals;
1500Sstevel@tonic-gate 			fltset_t faults;
1510Sstevel@tonic-gate 			sysset_t syscalls;
1520Sstevel@tonic-gate 			siginfo_t siginfo;
1530Sstevel@tonic-gate 		} arg;
1540Sstevel@tonic-gate 	} ctl;
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate #if PTRACE_DEBUG
1570Sstevel@tonic-gate 	fprintf(stderr, " ptrace(%s, 0x%X, 0x%X, 0x%X)\n",
1586515Sraf 	    map(request), pid, addr, data);
1590Sstevel@tonic-gate #endif
1600Sstevel@tonic-gate 
1616515Sraf 	(void) mutex_lock(&pt_lock);
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 	if (request == 0) {	/* PTRACE_TRACEME, executed by traced process */
1640Sstevel@tonic-gate 		/*
1650Sstevel@tonic-gate 		 * Set stop-on-all-signals and nothing else.
1660Sstevel@tonic-gate 		 * Turn off inherit-on-fork flag (grandchildren run away).
1670Sstevel@tonic-gate 		 * Set ptrace-compatible flag.
1680Sstevel@tonic-gate 		 */
1690Sstevel@tonic-gate 		char procname[64];	/* /proc/<pid>/ctl */
1700Sstevel@tonic-gate 		int fd;
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 		MakeProcName(procname, getpid());
1730Sstevel@tonic-gate 		(void) strcat(procname, "/ctl");
1740Sstevel@tonic-gate 		if ((fd = open(procname, O_WRONLY, 0)) < 0)
1750Sstevel@tonic-gate 			exit(255);
1760Sstevel@tonic-gate 		ctl.cmd = PCSTRACE;
1770Sstevel@tonic-gate 		prfillset(&ctl.arg.signals);
1780Sstevel@tonic-gate 		if (write(fd, (char *)&ctl, sizeof (long)+sizeof (sigset_t))
1790Sstevel@tonic-gate 		    != sizeof (long)+sizeof (sigset_t))
1800Sstevel@tonic-gate 			exit(255);
1810Sstevel@tonic-gate 		ctl.cmd = PCSFAULT;
1820Sstevel@tonic-gate 		premptyset(&ctl.arg.faults);
1830Sstevel@tonic-gate 		if (write(fd, (char *)&ctl, sizeof (long)+sizeof (fltset_t))
1840Sstevel@tonic-gate 		    != sizeof (long)+sizeof (fltset_t))
1850Sstevel@tonic-gate 			exit(255);
1860Sstevel@tonic-gate 		ctl.cmd = PCSENTRY;
1870Sstevel@tonic-gate 		premptyset(&ctl.arg.syscalls);
1880Sstevel@tonic-gate 		if (write(fd, (char *)&ctl, sizeof (long)+sizeof (sysset_t))
1890Sstevel@tonic-gate 		    != sizeof (long)+sizeof (sysset_t))
1900Sstevel@tonic-gate 			exit(255);
1910Sstevel@tonic-gate 		ctl.cmd = PCSEXIT;
1920Sstevel@tonic-gate 		premptyset(&ctl.arg.syscalls);
1930Sstevel@tonic-gate 		if (write(fd, (char *)&ctl, sizeof (long)+sizeof (sysset_t))
1940Sstevel@tonic-gate 		    != sizeof (long)+sizeof (sysset_t))
1950Sstevel@tonic-gate 			exit(255);
1960Sstevel@tonic-gate 		ctl.cmd = PCUNSET;
1970Sstevel@tonic-gate 		ctl.arg.flags = PR_FORK;
1980Sstevel@tonic-gate 		if (write(fd, (char *)&ctl, sizeof (long)+sizeof (long))
1990Sstevel@tonic-gate 		    != sizeof (long)+sizeof (long))
2000Sstevel@tonic-gate 			exit(255);
2010Sstevel@tonic-gate 		ctl.cmd = PCSET;
2020Sstevel@tonic-gate 		ctl.arg.flags = PR_PTRACE;
2030Sstevel@tonic-gate 		if (write(fd, (char *)&ctl, sizeof (long)+sizeof (long))
2040Sstevel@tonic-gate 		    != sizeof (long)+sizeof (long))
2050Sstevel@tonic-gate 			exit(255);
2060Sstevel@tonic-gate 		if (close(fd) != 0)
2070Sstevel@tonic-gate 			exit(255);
2080Sstevel@tonic-gate 
2096515Sraf 		(void) mutex_unlock(&pt_lock);
2100Sstevel@tonic-gate 		return (0);
2110Sstevel@tonic-gate 	}
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate again:
2140Sstevel@tonic-gate 	errno = 0;
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 	/* find the cstatus structure corresponding to pid */
2170Sstevel@tonic-gate 	if ((cp = GrabProc(pid)) == NULLCP)
2180Sstevel@tonic-gate 		goto esrch;
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 	ps = &cp->pstatus;
2210Sstevel@tonic-gate 	if (!(ps->pr_flags & PR_ISTOP)) {
2220Sstevel@tonic-gate 		if (ProcUpdate(cp) != 0) {
2230Sstevel@tonic-gate 			ReleaseProc(cp);
2240Sstevel@tonic-gate 			goto esrch;
2250Sstevel@tonic-gate 		}
2260Sstevel@tonic-gate 		if (!(ps->pr_flags & PR_ISTOP))
2270Sstevel@tonic-gate 			goto esrch;
2280Sstevel@tonic-gate 	}
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 	/*
2310Sstevel@tonic-gate 	 * Process the request.
2320Sstevel@tonic-gate 	 */
2330Sstevel@tonic-gate 	errno = 0;
2340Sstevel@tonic-gate 	switch (request) {
2350Sstevel@tonic-gate 	case 1:		/* PTRACE_PEEKTEXT */
2360Sstevel@tonic-gate 	case 2:		/* PTRACE_PEEKDATA */
2370Sstevel@tonic-gate 		if (addr & 03)
2380Sstevel@tonic-gate 			goto eio;
2390Sstevel@tonic-gate 		if (pread(cp->asfd, (char *)&data, sizeof (data), (off_t)addr)
2400Sstevel@tonic-gate 		    == sizeof (data)) {
2416515Sraf 			(void) mutex_unlock(&pt_lock);
2420Sstevel@tonic-gate 			return (data);
2430Sstevel@tonic-gate 		}
2440Sstevel@tonic-gate 		goto eio;
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate 	case 3:		/* PTRACE_PEEKUSER */
2470Sstevel@tonic-gate 		if (addr & 03)
2480Sstevel@tonic-gate 			goto eio;
2490Sstevel@tonic-gate 		xaddr = addr;
2500Sstevel@tonic-gate 		if (xaddr >= REGADDR && xaddr < REGADDR+sizeof (gregset_t))
2510Sstevel@tonic-gate 			xaddr -= REGADDR-U_REG;
2520Sstevel@tonic-gate 		if (xaddr >= U_PSARGS && xaddr < U_PSARGS+sizeof (UP->u_psargs))
2530Sstevel@tonic-gate 			GetPsargs(cp);
2540Sstevel@tonic-gate 		if (xaddr >= U_SIGNAL && xaddr < U_SIGNAL+sizeof (UP->u_signal))
2550Sstevel@tonic-gate 			GetSignal(cp);
2560Sstevel@tonic-gate 		if ((int)xaddr >= 0 && xaddr < U_END) {
2570Sstevel@tonic-gate 			/* LINTED pointer alignment */
2580Sstevel@tonic-gate 			data = *((int *)((caddr_t)(&cp->user) + xaddr));
2596515Sraf 			(void) mutex_unlock(&pt_lock);
2600Sstevel@tonic-gate 			return (data);
2610Sstevel@tonic-gate 		}
2620Sstevel@tonic-gate 		goto eio;
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 	case 4:		/* PTRACE_POKETEXT */
2650Sstevel@tonic-gate 	case 5:		/* PTRACE_POKEDATA */
2660Sstevel@tonic-gate 		if (addr & 03)
2670Sstevel@tonic-gate 			goto eio;
2680Sstevel@tonic-gate 		xaddr = addr;
2690Sstevel@tonic-gate 		if (xaddr >= (unsigned)cp->user.u_reg[REG_SP] &&
2700Sstevel@tonic-gate 		    xaddr < (unsigned)cp->user.u_reg[REG_SP]+16*sizeof (int))
2710Sstevel@tonic-gate 			cp->flags |= CS_SETREGS;
2720Sstevel@tonic-gate 		if (pwrite(cp->asfd, (char *)&data, sizeof (data), (off_t)addr)
2730Sstevel@tonic-gate 		    == sizeof (data)) {
2746515Sraf 			(void) mutex_unlock(&pt_lock);
2750Sstevel@tonic-gate 			return (data);
2760Sstevel@tonic-gate 		}
2770Sstevel@tonic-gate 		goto eio;
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 	case 6:		/* PTRACE_POKEUSER */
2800Sstevel@tonic-gate 		if (addr & 03)
2810Sstevel@tonic-gate 			goto eio;
2820Sstevel@tonic-gate 		xaddr = addr;
2830Sstevel@tonic-gate 		if (xaddr >= REGADDR && xaddr < REGADDR+sizeof (gregset_t))
2840Sstevel@tonic-gate 			xaddr -= REGADDR-U_REG;
2850Sstevel@tonic-gate 		if ((int)xaddr >= U_REG && xaddr < U_REG+sizeof (gregset_t)) {
2860Sstevel@tonic-gate 			int rx = (xaddr-U_REG)/sizeof (greg_t);
2870Sstevel@tonic-gate 			if (rx == REG_PS)
2880Sstevel@tonic-gate 				data = (cp->user.u_reg[REG_PS] &
2890Sstevel@tonic-gate 				    ~PSL_USERMASK) | (data & PSL_USERMASK);
2900Sstevel@tonic-gate 			else if (rx == REG_SP || rx == REG_PC || rx == REG_nPC)
2910Sstevel@tonic-gate 				data &= ~03;
2920Sstevel@tonic-gate 			cp->user.u_reg[rx] = data;
2930Sstevel@tonic-gate 			cp->flags |= CS_SETREGS;
2946515Sraf 			(void) mutex_unlock(&pt_lock);
2950Sstevel@tonic-gate 			return (data);
2960Sstevel@tonic-gate 		}
2970Sstevel@tonic-gate 		goto eio;
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate 	case 7:		/* PTRACE_CONT */
3000Sstevel@tonic-gate 	case 9:		/* PTRACE_SINGLESTEP */
3016515Sraf 	{
3020Sstevel@tonic-gate 		long runctl[3];
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate 		if (cp->flags & CS_SETREGS) {
3050Sstevel@tonic-gate 			long cmd;
3060Sstevel@tonic-gate 			iovec_t iov[2];
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate 			ps->pr_lwp.pr_reg[R_PSR] = cp->user.u_reg[REG_PSR];
3090Sstevel@tonic-gate 			ps->pr_lwp.pr_reg[R_PC]  = cp->user.u_reg[REG_PC];
3100Sstevel@tonic-gate 			ps->pr_lwp.pr_reg[R_nPC] = cp->user.u_reg[REG_nPC];
3110Sstevel@tonic-gate 			ps->pr_lwp.pr_reg[R_Y]   = cp->user.u_reg[REG_Y];
3120Sstevel@tonic-gate 			ps->pr_lwp.pr_reg[R_G1]  = cp->user.u_reg[REG_G1];
3130Sstevel@tonic-gate 			ps->pr_lwp.pr_reg[R_G2]  = cp->user.u_reg[REG_G2];
3140Sstevel@tonic-gate 			ps->pr_lwp.pr_reg[R_G3]  = cp->user.u_reg[REG_G3];
3150Sstevel@tonic-gate 			ps->pr_lwp.pr_reg[R_G4]  = cp->user.u_reg[REG_G4];
3160Sstevel@tonic-gate 			ps->pr_lwp.pr_reg[R_G5]  = cp->user.u_reg[REG_G5];
3170Sstevel@tonic-gate 			ps->pr_lwp.pr_reg[R_G6]  = cp->user.u_reg[REG_G6];
3180Sstevel@tonic-gate 			ps->pr_lwp.pr_reg[R_G7]  = cp->user.u_reg[REG_G7];
3190Sstevel@tonic-gate 			ps->pr_lwp.pr_reg[R_O0]  = cp->user.u_reg[REG_O0];
3200Sstevel@tonic-gate 			ps->pr_lwp.pr_reg[R_O1]  = cp->user.u_reg[REG_O1];
3210Sstevel@tonic-gate 			ps->pr_lwp.pr_reg[R_O2]  = cp->user.u_reg[REG_O2];
3220Sstevel@tonic-gate 			ps->pr_lwp.pr_reg[R_O3]  = cp->user.u_reg[REG_O3];
3230Sstevel@tonic-gate 			ps->pr_lwp.pr_reg[R_O4]  = cp->user.u_reg[REG_O4];
3240Sstevel@tonic-gate 			ps->pr_lwp.pr_reg[R_O5]  = cp->user.u_reg[REG_O5];
3250Sstevel@tonic-gate 			ps->pr_lwp.pr_reg[R_O6]  = cp->user.u_reg[REG_O6];
3260Sstevel@tonic-gate 			ps->pr_lwp.pr_reg[R_O7]  = cp->user.u_reg[REG_O7];
3270Sstevel@tonic-gate 			(void) pread(cp->asfd, (char *)&ps->pr_lwp.pr_reg[R_L0],
3286515Sraf 			    16*sizeof (int), (off_t)cp->user.u_reg[REG_SP]);
3290Sstevel@tonic-gate 			cmd = PCSREG;
3300Sstevel@tonic-gate 			iov[0].iov_base = (caddr_t)&cmd;
3310Sstevel@tonic-gate 			iov[0].iov_len = sizeof (long);
3320Sstevel@tonic-gate 			iov[1].iov_base = (caddr_t)&ps->pr_lwp.pr_reg[0];
3330Sstevel@tonic-gate 			iov[1].iov_len = sizeof (ps->pr_lwp.pr_reg);
3340Sstevel@tonic-gate 			if (writev(cp->ctlfd, iov, 2) < 0)
3350Sstevel@tonic-gate 				goto tryagain;
3360Sstevel@tonic-gate 		}
3370Sstevel@tonic-gate 		if (addr != 1 &&	/* new virtual address */
3380Sstevel@tonic-gate 		    (addr & ~03) != cp->user.u_reg[REG_PC]) {
3390Sstevel@tonic-gate 			runctl[0] = PCSVADDR;
3400Sstevel@tonic-gate 			runctl[1] = (addr & ~03);
3410Sstevel@tonic-gate 			if (write(cp->ctlfd, (char *)runctl, 2*sizeof (long))
3420Sstevel@tonic-gate 			    != 2*sizeof (long))
3430Sstevel@tonic-gate 				goto tryagain;
3440Sstevel@tonic-gate 		}
3450Sstevel@tonic-gate 		/* make data the current signal */
3460Sstevel@tonic-gate 		if (data != 0 && data != ps->pr_lwp.pr_cursig) {
3470Sstevel@tonic-gate 			(void) memset((char *)&ctl.arg.siginfo, 0,
3480Sstevel@tonic-gate 			    sizeof (siginfo_t));
3490Sstevel@tonic-gate 			ctl.arg.siginfo.si_signo = data;
3500Sstevel@tonic-gate 			ctl.cmd = PCSSIG;
3510Sstevel@tonic-gate 			if (write(cp->ctlfd, (char *)&ctl,
3520Sstevel@tonic-gate 			    sizeof (long)+sizeof (siginfo_t))
3530Sstevel@tonic-gate 			    != sizeof (long)+sizeof (siginfo_t))
3540Sstevel@tonic-gate 				goto tryagain;
3550Sstevel@tonic-gate 		}
3560Sstevel@tonic-gate 		if (data == 0)
3570Sstevel@tonic-gate 			runctl[0] = PCCSIG;
3580Sstevel@tonic-gate 		else
3590Sstevel@tonic-gate 			runctl[0] = PCNULL;
3600Sstevel@tonic-gate 		runctl[1] = PCRUN;
3610Sstevel@tonic-gate 		runctl[2] = (request == 9)? PRSTEP : 0;
3620Sstevel@tonic-gate 		if (write(cp->ctlfd, (char *)runctl, 3*sizeof (long))
3630Sstevel@tonic-gate 		    != 3*sizeof (long)) {
3640Sstevel@tonic-gate 			if (errno == ENOENT) {
3650Sstevel@tonic-gate 				/* current signal must have killed it */
3660Sstevel@tonic-gate 				ReleaseProc(cp);
3676515Sraf 				(void) mutex_unlock(&pt_lock);
3680Sstevel@tonic-gate 				return (data);
3690Sstevel@tonic-gate 			}
3700Sstevel@tonic-gate 			goto tryagain;
3710Sstevel@tonic-gate 		}
3720Sstevel@tonic-gate 		(void) memset((char *)ps, 0, sizeof (pstatus_t));
3730Sstevel@tonic-gate 		cp->flags = 0;
3746515Sraf 		(void) mutex_unlock(&pt_lock);
3750Sstevel@tonic-gate 		return (data);
3766515Sraf 	}
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate 	case 8:		/* PTRACE_KILL */
3790Sstevel@tonic-gate 		/* overkill? */
3800Sstevel@tonic-gate 		(void) memset((char *)&ctl.arg.siginfo, 0, sizeof (siginfo_t));
3810Sstevel@tonic-gate 		ctl.arg.siginfo.si_signo = SIGKILL;
3820Sstevel@tonic-gate 		ctl.cmd = PCSSIG;
3830Sstevel@tonic-gate 		(void) write(cp->ctlfd, (char *)&ctl,
3840Sstevel@tonic-gate 		    sizeof (long)+sizeof (siginfo_t));
3850Sstevel@tonic-gate 		(void) kill(pid, SIGKILL);
3860Sstevel@tonic-gate 		ReleaseProc(cp);
3876515Sraf 		(void) mutex_unlock(&pt_lock);
3880Sstevel@tonic-gate 		return (0);
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate 	default:
3910Sstevel@tonic-gate 		goto eio;
3920Sstevel@tonic-gate 	}
3930Sstevel@tonic-gate 
3940Sstevel@tonic-gate tryagain:
3950Sstevel@tonic-gate 	if (errno == EAGAIN) {
3960Sstevel@tonic-gate 		if (OpenProc(cp) == 0)
3970Sstevel@tonic-gate 			goto again;
3980Sstevel@tonic-gate 		ReleaseProc(cp);
3990Sstevel@tonic-gate 	}
4000Sstevel@tonic-gate eio:
4010Sstevel@tonic-gate 	errno = EIO;
4026515Sraf 	(void) mutex_unlock(&pt_lock);
4030Sstevel@tonic-gate 	return (-1);
4040Sstevel@tonic-gate esrch:
4050Sstevel@tonic-gate 	errno = ESRCH;
4066515Sraf 	(void) mutex_unlock(&pt_lock);
4070Sstevel@tonic-gate 	return (-1);
4080Sstevel@tonic-gate }
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate /*
4110Sstevel@tonic-gate  * Find the cstatus structure corresponding to pid.
4120Sstevel@tonic-gate  */
4130Sstevel@tonic-gate static cstatus_t *
4140Sstevel@tonic-gate FindProc(pid_t pid)
4150Sstevel@tonic-gate {
4160Sstevel@tonic-gate 	cstatus_t *cp;
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate 	for (cp = childp; cp != NULLCP; cp = cp->next)
4190Sstevel@tonic-gate 		if (cp->pid == pid)
4200Sstevel@tonic-gate 			break;
4210Sstevel@tonic-gate 
4220Sstevel@tonic-gate 	return (cp);
4230Sstevel@tonic-gate }
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate /*
4260Sstevel@tonic-gate  * Check every proc for existence, release those that are gone.
4270Sstevel@tonic-gate  * Be careful about the linked list; ReleaseProc() changes it.
4280Sstevel@tonic-gate  */
4290Sstevel@tonic-gate static void
4300Sstevel@tonic-gate CheckAllProcs()
4310Sstevel@tonic-gate {
4320Sstevel@tonic-gate 	cstatus_t *cp = childp;
4330Sstevel@tonic-gate 
4340Sstevel@tonic-gate 	while (cp != NULLCP) {
4350Sstevel@tonic-gate 		cstatus_t *next = cp->next;
4360Sstevel@tonic-gate 
4370Sstevel@tonic-gate 		if (ProcUpdate(cp) != 0)
4380Sstevel@tonic-gate 			ReleaseProc(cp);
4390Sstevel@tonic-gate 		cp = next;
4400Sstevel@tonic-gate 	}
4410Sstevel@tonic-gate }
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate /*
4440Sstevel@tonic-gate  * Utility for OpenProc().
4450Sstevel@tonic-gate  */
4460Sstevel@tonic-gate static int
4470Sstevel@tonic-gate Dupfd(int fd, int dfd)
4480Sstevel@tonic-gate {
4490Sstevel@tonic-gate 	/*
4500Sstevel@tonic-gate 	 * Make sure fd not one of 0, 1, or 2 to avoid stdio interference.
4510Sstevel@tonic-gate 	 * Also, if dfd is greater than 2, dup fd to be exactly dfd.
4520Sstevel@tonic-gate 	 */
4530Sstevel@tonic-gate 	if (dfd > 2 || (0 <= fd && fd <= 2)) {
4540Sstevel@tonic-gate 		if (dfd > 2 && fd != dfd)
4550Sstevel@tonic-gate 			(void) close(dfd);
4560Sstevel@tonic-gate 		else
4570Sstevel@tonic-gate 			dfd = 3;
4580Sstevel@tonic-gate 		if (fd != dfd) {
4590Sstevel@tonic-gate 			dfd = fcntl(fd, F_DUPFD, (intptr_t)dfd);
4600Sstevel@tonic-gate 			(void) close(fd);
4610Sstevel@tonic-gate 			fd = dfd;
4620Sstevel@tonic-gate 		}
4630Sstevel@tonic-gate 	}
4640Sstevel@tonic-gate 	/*
4650Sstevel@tonic-gate 	 * Mark filedescriptor close-on-exec.
4660Sstevel@tonic-gate 	 * Should also be close-on-return-from-fork-in-child.
4670Sstevel@tonic-gate 	 */
4680Sstevel@tonic-gate 	(void) fcntl(fd, F_SETFD, (intptr_t)1);
4690Sstevel@tonic-gate 	return (fd);
4700Sstevel@tonic-gate }
4710Sstevel@tonic-gate 
4720Sstevel@tonic-gate /*
4730Sstevel@tonic-gate  * Construct the /proc directory name:  "/proc/<pid>"
4740Sstevel@tonic-gate  * The name buffer passed by the caller must be large enough.
4750Sstevel@tonic-gate  */
4760Sstevel@tonic-gate static void
4770Sstevel@tonic-gate MakeProcName(char *procname, pid_t pid)
4780Sstevel@tonic-gate {
4790Sstevel@tonic-gate 	(void) sprintf(procname, "/proc/%d", pid);
4800Sstevel@tonic-gate }
4810Sstevel@tonic-gate 
4820Sstevel@tonic-gate /*
4830Sstevel@tonic-gate  * Open/reopen the /proc/<pid> files.
4840Sstevel@tonic-gate  */
4850Sstevel@tonic-gate static int
4860Sstevel@tonic-gate OpenProc(cstatus_t *cp)
4870Sstevel@tonic-gate {
4880Sstevel@tonic-gate 	char procname[64];		/* /proc/nnnnn/fname */
4890Sstevel@tonic-gate 	char *fname;
4900Sstevel@tonic-gate 	int fd;
4910Sstevel@tonic-gate 	int omode;
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate 	MakeProcName(procname, cp->pid);
4940Sstevel@tonic-gate 	fname = procname + strlen(procname);
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate 	/*
4970Sstevel@tonic-gate 	 * Use exclusive-open only if this is the first open.
4980Sstevel@tonic-gate 	 */
4990Sstevel@tonic-gate 	omode = (cp->asfd > 0)? O_RDWR : (O_RDWR|O_EXCL);
5000Sstevel@tonic-gate 	(void) strcpy(fname, "/as");
5010Sstevel@tonic-gate 	if ((fd = open(procname, omode, 0)) < 0 ||
5020Sstevel@tonic-gate 	    (cp->asfd = Dupfd(fd, cp->asfd)) < 0)
5030Sstevel@tonic-gate 		goto err;
5040Sstevel@tonic-gate 
5050Sstevel@tonic-gate 	(void) strcpy(fname, "/ctl");
5060Sstevel@tonic-gate 	if ((fd = open(procname, O_WRONLY, 0)) < 0 ||
5070Sstevel@tonic-gate 	    (cp->ctlfd = Dupfd(fd, cp->ctlfd)) < 0)
5080Sstevel@tonic-gate 		goto err;
5090Sstevel@tonic-gate 
5100Sstevel@tonic-gate 	(void) strcpy(fname, "/status");
5110Sstevel@tonic-gate 	if ((fd = open(procname, O_RDONLY, 0)) < 0 ||
5120Sstevel@tonic-gate 	    (cp->statusfd = Dupfd(fd, cp->statusfd)) < 0)
5130Sstevel@tonic-gate 		goto err;
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate 	return (0);
5160Sstevel@tonic-gate 
5170Sstevel@tonic-gate err:
5180Sstevel@tonic-gate 	CloseProc(cp);
5190Sstevel@tonic-gate 	return (-1);
5200Sstevel@tonic-gate }
5210Sstevel@tonic-gate 
5220Sstevel@tonic-gate /*
5230Sstevel@tonic-gate  * Close the /proc/<pid> files.
5240Sstevel@tonic-gate  */
5250Sstevel@tonic-gate static void
5260Sstevel@tonic-gate CloseProc(cstatus_t *cp)
5270Sstevel@tonic-gate {
5280Sstevel@tonic-gate 	if (cp->asfd > 0)
5290Sstevel@tonic-gate 		(void) close(cp->asfd);
5300Sstevel@tonic-gate 	if (cp->ctlfd > 0)
5310Sstevel@tonic-gate 		(void) close(cp->ctlfd);
5320Sstevel@tonic-gate 	if (cp->statusfd > 0)
5330Sstevel@tonic-gate 		(void) close(cp->statusfd);
5340Sstevel@tonic-gate 	cp->asfd = 0;
5350Sstevel@tonic-gate 	cp->ctlfd = 0;
5360Sstevel@tonic-gate 	cp->statusfd = 0;
5370Sstevel@tonic-gate }
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate /*
5400Sstevel@tonic-gate  * Take control of a child process.
5410Sstevel@tonic-gate  */
5420Sstevel@tonic-gate static cstatus_t *
5430Sstevel@tonic-gate GrabProc(pid_t pid)
5440Sstevel@tonic-gate {
5450Sstevel@tonic-gate 	cstatus_t *cp;
5460Sstevel@tonic-gate 	long ctl[2];
5470Sstevel@tonic-gate 	pid_t ppid;
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate 	if (pid <= 0)
5500Sstevel@tonic-gate 		return (NULLCP);
5510Sstevel@tonic-gate 
5520Sstevel@tonic-gate 	if ((cp = FindProc(pid)) != NULLCP)	/* already grabbed */
5530Sstevel@tonic-gate 		return (cp);
5540Sstevel@tonic-gate 
5550Sstevel@tonic-gate 	CheckAllProcs();	/* clean up before grabbing new process */
5560Sstevel@tonic-gate 
5570Sstevel@tonic-gate 	cp = (cstatus_t *)malloc(sizeof (cstatus_t));
5580Sstevel@tonic-gate 	if (cp == NULLCP)
5590Sstevel@tonic-gate 		return (NULLCP);
5600Sstevel@tonic-gate 	(void) memset((char *)cp, 0, sizeof (cstatus_t));
5610Sstevel@tonic-gate 	cp->pid = pid;
5620Sstevel@tonic-gate 
5630Sstevel@tonic-gate 	ppid = getpid();
5640Sstevel@tonic-gate 	while (OpenProc(cp) == 0) {
5650Sstevel@tonic-gate 		ctl[0] = PCSET;
5660Sstevel@tonic-gate 		ctl[1] = PR_RLC;
5670Sstevel@tonic-gate 		errno = 0;
5680Sstevel@tonic-gate 
5690Sstevel@tonic-gate 		if (pread(cp->statusfd, (char *)&cp->pstatus,
5700Sstevel@tonic-gate 		    sizeof (cp->pstatus), (off_t)0) == sizeof (cp->pstatus) &&
5710Sstevel@tonic-gate 		    cp->pstatus.pr_ppid == ppid &&
5720Sstevel@tonic-gate 		    (cp->pstatus.pr_flags & PR_PTRACE) &&
5730Sstevel@tonic-gate 		    write(cp->ctlfd, (char *)ctl, 2*sizeof (long))
5740Sstevel@tonic-gate 		    == 2*sizeof (long)) {
5750Sstevel@tonic-gate 			cp->next = childp;
5760Sstevel@tonic-gate 			childp = cp;
5770Sstevel@tonic-gate 			MakeUser(cp);
5780Sstevel@tonic-gate 			return (cp);
5790Sstevel@tonic-gate 		}
5800Sstevel@tonic-gate 
5810Sstevel@tonic-gate 		if (errno != EAGAIN)
5820Sstevel@tonic-gate 			break;
5830Sstevel@tonic-gate 	}
5840Sstevel@tonic-gate 
5850Sstevel@tonic-gate 	free((char *)cp);
5860Sstevel@tonic-gate 	return (NULLCP);
5870Sstevel@tonic-gate }
5880Sstevel@tonic-gate 
5890Sstevel@tonic-gate /*
5900Sstevel@tonic-gate  * Close the /proc/<pid> file, if open.
5910Sstevel@tonic-gate  * Deallocate the memory used by the cstatus_t structure.
5920Sstevel@tonic-gate  */
5930Sstevel@tonic-gate static void
5940Sstevel@tonic-gate ReleaseProc(cstatus_t *cp)
5950Sstevel@tonic-gate {
5960Sstevel@tonic-gate 	CloseProc(cp);
5970Sstevel@tonic-gate 
5980Sstevel@tonic-gate 	if (childp == cp)
5990Sstevel@tonic-gate 		childp = cp->next;
6000Sstevel@tonic-gate 	else {
6010Sstevel@tonic-gate 		cstatus_t *pcp;
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate 		for (pcp = childp; pcp != NULLCP; pcp = pcp->next) {
6040Sstevel@tonic-gate 			if (pcp->next == cp) {
6050Sstevel@tonic-gate 				pcp->next = cp->next;
6060Sstevel@tonic-gate 				break;
6070Sstevel@tonic-gate 			}
6080Sstevel@tonic-gate 		}
6090Sstevel@tonic-gate 	}
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate 	free((char *)cp);
6120Sstevel@tonic-gate }
6130Sstevel@tonic-gate 
6140Sstevel@tonic-gate /*
6150Sstevel@tonic-gate  * Update process information from /proc.
6160Sstevel@tonic-gate  * Return 0 on success, -1 on failure.
6170Sstevel@tonic-gate  */
6180Sstevel@tonic-gate static int
6190Sstevel@tonic-gate ProcUpdate(cstatus_t *cp)
6200Sstevel@tonic-gate {
6210Sstevel@tonic-gate 	pstatus_t *ps = &cp->pstatus;
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate 	if (cp->flags & CS_SETREGS) {
6240Sstevel@tonic-gate 		long cmd;
6250Sstevel@tonic-gate 		iovec_t iov[2];
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate 		ps->pr_lwp.pr_reg[R_PSR] = cp->user.u_reg[REG_PSR];
6280Sstevel@tonic-gate 		ps->pr_lwp.pr_reg[R_PC]  = cp->user.u_reg[REG_PC];
6290Sstevel@tonic-gate 		ps->pr_lwp.pr_reg[R_nPC] = cp->user.u_reg[REG_nPC];
6300Sstevel@tonic-gate 		ps->pr_lwp.pr_reg[R_Y]   = cp->user.u_reg[REG_Y];
6310Sstevel@tonic-gate 		ps->pr_lwp.pr_reg[R_G1]  = cp->user.u_reg[REG_G1];
6320Sstevel@tonic-gate 		ps->pr_lwp.pr_reg[R_G2]  = cp->user.u_reg[REG_G2];
6330Sstevel@tonic-gate 		ps->pr_lwp.pr_reg[R_G3]  = cp->user.u_reg[REG_G3];
6340Sstevel@tonic-gate 		ps->pr_lwp.pr_reg[R_G4]  = cp->user.u_reg[REG_G4];
6350Sstevel@tonic-gate 		ps->pr_lwp.pr_reg[R_G5]  = cp->user.u_reg[REG_G5];
6360Sstevel@tonic-gate 		ps->pr_lwp.pr_reg[R_G6]  = cp->user.u_reg[REG_G6];
6370Sstevel@tonic-gate 		ps->pr_lwp.pr_reg[R_G7]  = cp->user.u_reg[REG_G7];
6380Sstevel@tonic-gate 		ps->pr_lwp.pr_reg[R_O0]  = cp->user.u_reg[REG_O0];
6390Sstevel@tonic-gate 		ps->pr_lwp.pr_reg[R_O1]  = cp->user.u_reg[REG_O1];
6400Sstevel@tonic-gate 		ps->pr_lwp.pr_reg[R_O2]  = cp->user.u_reg[REG_O2];
6410Sstevel@tonic-gate 		ps->pr_lwp.pr_reg[R_O3]  = cp->user.u_reg[REG_O3];
6420Sstevel@tonic-gate 		ps->pr_lwp.pr_reg[R_O4]  = cp->user.u_reg[REG_O4];
6430Sstevel@tonic-gate 		ps->pr_lwp.pr_reg[R_O5]  = cp->user.u_reg[REG_O5];
6440Sstevel@tonic-gate 		ps->pr_lwp.pr_reg[R_O6]  = cp->user.u_reg[REG_O6];
6450Sstevel@tonic-gate 		ps->pr_lwp.pr_reg[R_O7]  = cp->user.u_reg[REG_O7];
6460Sstevel@tonic-gate 		(void) pread(cp->asfd, (char *)&ps->pr_lwp.pr_reg[R_L0],
6476515Sraf 		    16*sizeof (int), (off_t)cp->user.u_reg[REG_SP]);
6480Sstevel@tonic-gate 		cmd = PCSREG;
6490Sstevel@tonic-gate 		iov[0].iov_base = (caddr_t)&cmd;
6500Sstevel@tonic-gate 		iov[0].iov_len = sizeof (long);
6510Sstevel@tonic-gate 		iov[1].iov_base = (caddr_t)&ps->pr_lwp.pr_reg[0];
6520Sstevel@tonic-gate 		iov[1].iov_len = sizeof (ps->pr_lwp.pr_reg);
6530Sstevel@tonic-gate 		(void) writev(cp->ctlfd, iov, 2);
6540Sstevel@tonic-gate 		cp->flags &= ~CS_SETREGS;
6550Sstevel@tonic-gate 	}
6560Sstevel@tonic-gate 
6570Sstevel@tonic-gate 	while (pread(cp->statusfd, (char *)ps, sizeof (*ps), (off_t)0) < 0) {
6580Sstevel@tonic-gate 		/* attempt to regain control */
6590Sstevel@tonic-gate 		if (errno != EINTR &&
6600Sstevel@tonic-gate 		    !(errno == EAGAIN && OpenProc(cp) == 0))
6610Sstevel@tonic-gate 			return (-1);
6620Sstevel@tonic-gate 	}
6630Sstevel@tonic-gate 
6640Sstevel@tonic-gate 	if (ps->pr_flags & PR_ISTOP)
6650Sstevel@tonic-gate 		MakeUser(cp);
6660Sstevel@tonic-gate 	else
6670Sstevel@tonic-gate 		(void) memset((char *)ps, 0, sizeof (pstatus_t));
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate 	return (0);
6700Sstevel@tonic-gate }
6710Sstevel@tonic-gate 
6720Sstevel@tonic-gate /*
6730Sstevel@tonic-gate  * Manufacture the contents of the fake u-block.
6740Sstevel@tonic-gate  */
6750Sstevel@tonic-gate static void
6760Sstevel@tonic-gate MakeUser(cstatus_t *cp)
6770Sstevel@tonic-gate {
6780Sstevel@tonic-gate 	pstatus_t *ps = &cp->pstatus;
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate 	cp->user.u_reg[REG_PSR] = ps->pr_lwp.pr_reg[R_PSR];
6810Sstevel@tonic-gate 	cp->user.u_reg[REG_PC]  = ps->pr_lwp.pr_reg[R_PC];
6820Sstevel@tonic-gate 	cp->user.u_reg[REG_nPC] = ps->pr_lwp.pr_reg[R_nPC];
6830Sstevel@tonic-gate 	cp->user.u_reg[REG_Y]   = ps->pr_lwp.pr_reg[R_Y];
6840Sstevel@tonic-gate 	cp->user.u_reg[REG_G1]  = ps->pr_lwp.pr_reg[R_G1];
6850Sstevel@tonic-gate 	cp->user.u_reg[REG_G2]  = ps->pr_lwp.pr_reg[R_G2];
6860Sstevel@tonic-gate 	cp->user.u_reg[REG_G3]  = ps->pr_lwp.pr_reg[R_G3];
6870Sstevel@tonic-gate 	cp->user.u_reg[REG_G4]  = ps->pr_lwp.pr_reg[R_G4];
6880Sstevel@tonic-gate 	cp->user.u_reg[REG_G5]  = ps->pr_lwp.pr_reg[R_G5];
6890Sstevel@tonic-gate 	cp->user.u_reg[REG_G6]  = ps->pr_lwp.pr_reg[R_G6];
6900Sstevel@tonic-gate 	cp->user.u_reg[REG_G7]  = ps->pr_lwp.pr_reg[R_G7];
6910Sstevel@tonic-gate 	cp->user.u_reg[REG_O0]  = ps->pr_lwp.pr_reg[R_O0];
6920Sstevel@tonic-gate 	cp->user.u_reg[REG_O1]  = ps->pr_lwp.pr_reg[R_O1];
6930Sstevel@tonic-gate 	cp->user.u_reg[REG_O2]  = ps->pr_lwp.pr_reg[R_O2];
6940Sstevel@tonic-gate 	cp->user.u_reg[REG_O3]  = ps->pr_lwp.pr_reg[R_O3];
6950Sstevel@tonic-gate 	cp->user.u_reg[REG_O4]  = ps->pr_lwp.pr_reg[R_O4];
6960Sstevel@tonic-gate 	cp->user.u_reg[REG_O5]  = ps->pr_lwp.pr_reg[R_O5];
6970Sstevel@tonic-gate 	cp->user.u_reg[REG_O6]  = ps->pr_lwp.pr_reg[R_O6];
6980Sstevel@tonic-gate 	cp->user.u_reg[REG_O7]  = ps->pr_lwp.pr_reg[R_O7];
6990Sstevel@tonic-gate 	cp->user.u_ar0 = (greg_t *)REGADDR;
7000Sstevel@tonic-gate 	cp->user.u_code = ps->pr_lwp.pr_info.si_code;
7010Sstevel@tonic-gate 	cp->user.u_addr = ps->pr_lwp.pr_info.si_addr;
7020Sstevel@tonic-gate 	cp->flags &= ~(CS_PSARGS|CS_SIGNAL);
7030Sstevel@tonic-gate }
7040Sstevel@tonic-gate 
7050Sstevel@tonic-gate /*
7060Sstevel@tonic-gate  * Fetch the contents of u_psargs[].
7070Sstevel@tonic-gate  */
7080Sstevel@tonic-gate static void
7090Sstevel@tonic-gate GetPsargs(cstatus_t *cp)
7100Sstevel@tonic-gate {
7110Sstevel@tonic-gate 	char procname[64];	/* /proc/<pid>/psinfo */
7120Sstevel@tonic-gate 	int fd;
7130Sstevel@tonic-gate 
7140Sstevel@tonic-gate 	MakeProcName(procname, cp->pid);
7150Sstevel@tonic-gate 	(void) strcat(procname, "/psinfo");
7160Sstevel@tonic-gate 	if ((fd = open(procname, O_RDONLY, 0)) < 0) {
7170Sstevel@tonic-gate 		(void) memset(cp->user.u_psargs, 0, PSARGSZ);
7180Sstevel@tonic-gate 		return;
7190Sstevel@tonic-gate 	}
7200Sstevel@tonic-gate 	(void) pread(fd, cp->user.u_psargs, PSARGSZ,
7210Sstevel@tonic-gate 	    (off_t)((psinfo_t *)0)->pr_psargs);
7220Sstevel@tonic-gate 	(void) close(fd);
7230Sstevel@tonic-gate 
7240Sstevel@tonic-gate 	cp->flags |= CS_PSARGS;
7250Sstevel@tonic-gate }
7260Sstevel@tonic-gate 
7270Sstevel@tonic-gate /*
7280Sstevel@tonic-gate  * Fetch the contents of u_signal[].
7290Sstevel@tonic-gate  */
7300Sstevel@tonic-gate static void
7310Sstevel@tonic-gate GetSignal(cstatus_t *cp)
7320Sstevel@tonic-gate {
7330Sstevel@tonic-gate 	char procname[64];	/* /proc/<pid>/sigact */
7340Sstevel@tonic-gate 	int fd;
7350Sstevel@tonic-gate 	struct sigaction action[MAXSIG];
7360Sstevel@tonic-gate 	int i;
7370Sstevel@tonic-gate 
7380Sstevel@tonic-gate 	MakeProcName(procname, cp->pid);
7390Sstevel@tonic-gate 	(void) strcat(procname, "/sigact");
7400Sstevel@tonic-gate 	(void) memset((char *)action, 0, sizeof (action));
7410Sstevel@tonic-gate 	if ((fd = open(procname, O_RDONLY, 0)) >= 0) {
7420Sstevel@tonic-gate 		(void) read(fd, (char *)action, sizeof (action));
7430Sstevel@tonic-gate 		(void) close(fd);
7440Sstevel@tonic-gate 	}
7450Sstevel@tonic-gate 	for (i = 0; i < MAXSIG; i++)
7460Sstevel@tonic-gate 		cp->user.u_signal[i] = action[i].sa_handler;
7470Sstevel@tonic-gate 	cp->flags |= CS_SIGNAL;
7480Sstevel@tonic-gate }
749