17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
58cd4554raf * Common Development and Distribution License (the "License").
68cd4554raf * You may not use this file except in compliance with the License.
77c478bdstevel@tonic-gate *
87c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bdstevel@tonic-gate * See the License for the specific language governing permissions
117c478bdstevel@tonic-gate * and limitations under the License.
127c478bdstevel@tonic-gate *
137c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bdstevel@tonic-gate *
197c478bdstevel@tonic-gate * CDDL HEADER END
207c478bdstevel@tonic-gate */
218cd4554raf
227c478bdstevel@tonic-gate/*
238cd4554raf * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
248cd4554raf * Use is subject to license terms.
257c478bdstevel@tonic-gate */
267c478bdstevel@tonic-gate
277c478bdstevel@tonic-gate/*
288cd4554raf * ptrace(2) interface built on top of proc(4).
297c478bdstevel@tonic-gate */
307c478bdstevel@tonic-gate
317c478bdstevel@tonic-gate
327257d1braf#pragma weak _ptrace = ptrace
337c478bdstevel@tonic-gate
347257d1braf#include "lint.h"
357c478bdstevel@tonic-gate#include <stdio.h>
367c478bdstevel@tonic-gate#include <stdlib.h>
377c478bdstevel@tonic-gate#include <unistd.h>
387c478bdstevel@tonic-gate#include <memory.h>
397c478bdstevel@tonic-gate#include <string.h>
407c478bdstevel@tonic-gate#include <fcntl.h>
417c478bdstevel@tonic-gate#include <errno.h>
427c478bdstevel@tonic-gate#include <sys/types.h>
437c478bdstevel@tonic-gate#include <sys/uio.h>
447c478bdstevel@tonic-gate#include <signal.h>
457c478bdstevel@tonic-gate#include <sys/siginfo.h>
467c478bdstevel@tonic-gate#include <sys/fault.h>
477c478bdstevel@tonic-gate#include <sys/syscall.h>
487c478bdstevel@tonic-gate#include <procfs.h>
497c478bdstevel@tonic-gate#include <sys/psw.h>
507c478bdstevel@tonic-gate#include <sys/user.h>
517c478bdstevel@tonic-gate/*
527c478bdstevel@tonic-gate * mtlib.h must precede thread.h
537c478bdstevel@tonic-gate */
547c478bdstevel@tonic-gate#include <mtlib.h>
557c478bdstevel@tonic-gate#include <thread.h>
567c478bdstevel@tonic-gate#include <synch.h>
577c478bdstevel@tonic-gate#include <unistd.h>
587c478bdstevel@tonic-gate
597c478bdstevel@tonic-gatestatic mutex_t pt_lock = DEFAULTMUTEX;
607c478bdstevel@tonic-gate
617c478bdstevel@tonic-gate#define	TRUE	1
627c478bdstevel@tonic-gate#define	FALSE	0
637c478bdstevel@tonic-gate
647c478bdstevel@tonic-gate/*
657c478bdstevel@tonic-gate * All my children...
667c478bdstevel@tonic-gate */
677c478bdstevel@tonic-gatetypedef struct cstatus {
687c478bdstevel@tonic-gate	struct cstatus	*next;		/* linked list			*/
697c478bdstevel@tonic-gate	pid_t		pid;		/* process-id			*/
707c478bdstevel@tonic-gate	int		asfd;		/* /proc/<pid>/as		*/
717c478bdstevel@tonic-gate	int		ctlfd;		/* /proc/<pid>/ctl		*/
727c478bdstevel@tonic-gate	int		statusfd;	/* /proc/<pid>/status		*/
737c478bdstevel@tonic-gate	int		flags;		/* see below			*/
747c478bdstevel@tonic-gate	pstatus_t	pstatus;	/* from /proc/<pid>/status	*/
757c478bdstevel@tonic-gate	user_t		user;		/* manufactured u-block		*/
767c478bdstevel@tonic-gate} cstatus_t;
777c478bdstevel@tonic-gate
787c478bdstevel@tonic-gate/* flags */
797c478bdstevel@tonic-gate#define	CS_SETREGS	0x01		/* set registers on run		*/
807c478bdstevel@tonic-gate#define	CS_PSARGS	0x02		/* u_psargs[] has been fetched	*/
817c478bdstevel@tonic-gate#define	CS_SIGNAL	0x04		/* u_signal[] has been fetched	*/
827c478bdstevel@tonic-gate
837c478bdstevel@tonic-gate#define	NULLCP	((cstatus_t *)0)
847c478bdstevel@tonic-gate
857c478bdstevel@tonic-gatestatic cstatus_t *childp = NULLCP;
867c478bdstevel@tonic-gate
877c478bdstevel@tonic-gate/* fake u-block offsets */
887c478bdstevel@tonic-gate#define	UP		((user_t *)NULL)
897c478bdstevel@tonic-gate#define	U_REG		((int)(&UP->u_reg[0]))
907c478bdstevel@tonic-gate#define	U_AR0		((int)(&UP->u_ar0))
917c478bdstevel@tonic-gate#define	U_PSARGS	((int)(&UP->u_psargs[0]))
927c478bdstevel@tonic-gate#define	U_SIGNAL	((int)(&UP->u_signal[0]))
937c478bdstevel@tonic-gate#define	U_CODE		((int)(&UP->u_code))
947c478bdstevel@tonic-gate#define	U_ADDR		((int)(&UP->u_addr))
957c478bdstevel@tonic-gate#define	U_END		((int)sizeof (user_t))
967c478bdstevel@tonic-gate#define	REGADDR		0xffff0000	/* arbitrary kernel address for u_ar0 */
977c478bdstevel@tonic-gate
987c478bdstevel@tonic-gate/* external routines defined in this module */
997c478bdstevel@tonic-gateextern	int	ptrace(int, pid_t, int, int);
1007c478bdstevel@tonic-gate/* static routines defined in this module */
1017c478bdstevel@tonic-gatestatic	cstatus_t *FindProc(pid_t);
1027c478bdstevel@tonic-gatestatic	void	CheckAllProcs(void);
1037c478bdstevel@tonic-gatestatic	int	Dupfd(int, int);
1047c478bdstevel@tonic-gatestatic	void	MakeProcName(char *, pid_t);
1057c478bdstevel@tonic-gatestatic	int	OpenProc(cstatus_t *);
1067c478bdstevel@tonic-gatestatic	void	CloseProc(cstatus_t *);
1077c478bdstevel@tonic-gatestatic	cstatus_t *GrabProc(pid_t);
1087c478bdstevel@tonic-gatestatic	void	ReleaseProc(cstatus_t *);
1097c478bdstevel@tonic-gatestatic	int	ProcUpdate(cstatus_t *);
1107c478bdstevel@tonic-gatestatic	void	MakeUser(cstatus_t *);
1117c478bdstevel@tonic-gatestatic	void	GetPsargs(cstatus_t *);
1127c478bdstevel@tonic-gatestatic	void	GetSignal(cstatus_t *);
1137c478bdstevel@tonic-gate
1147c478bdstevel@tonic-gate#if PTRACE_DEBUG
1157c478bdstevel@tonic-gate/* for debugging */
1167c478bdstevel@tonic-gatestatic char *
1177c478bdstevel@tonic-gatemap(int request)
1187c478bdstevel@tonic-gate{
1197c478bdstevel@tonic-gate	static char name[20];
1207c478bdstevel@tonic-gate
1217c478bdstevel@tonic-gate	switch (request) {
1227c478bdstevel@tonic-gate	case 0:	return ("PTRACE_TRACEME");
1237c478bdstevel@tonic-gate	case 1:	return ("PTRACE_PEEKTEXT");
1247c478bdstevel@tonic-gate	case 2:	return ("PTRACE_PEEKDATA");
1257c478bdstevel@tonic-gate	case 3:	return ("PTRACE_PEEKUSER");
1267c478bdstevel@tonic-gate	case 4:	return ("PTRACE_POKETEXT");
1277c478bdstevel@tonic-gate	case 5:	return ("PTRACE_POKEDATA");
1287c478bdstevel@tonic-gate	case 6:	return ("PTRACE_POKEUSER");
1297c478bdstevel@tonic-gate	case 7:	return ("PTRACE_CONT");
1307c478bdstevel@tonic-gate	case 8:	return ("PTRACE_KILL");
1317c478bdstevel@tonic-gate	case 9:	return ("PTRACE_SINGLESTEP");
1327c478bdstevel@tonic-gate	}
1337c478bdstevel@tonic-gate	(void) sprintf(name, "%d", request);
1347c478bdstevel@tonic-gate	return (name);
1357c478bdstevel@tonic-gate}
1367c478bdstevel@tonic-gate#endif
1377c478bdstevel@tonic-gate
1387c478bdstevel@tonic-gateint
1397c478bdstevel@tonic-gateptrace(int request, pid_t pid, int addr, int data)
1407c478bdstevel@tonic-gate{
1417c478bdstevel@tonic-gate	pstatus_t *ps;
1427c478bdstevel@tonic-gate	cstatus_t *cp;
1437c478bdstevel@tonic-gate	unsigned xaddr;
1447c478bdstevel@tonic-gate	struct {
1457c478bdstevel@tonic-gate		long cmd;
1467c478bdstevel@tonic-gate		union {
1477c478bdstevel@tonic-gate			long flags;
1487c478bdstevel@tonic-gate			sigset_t signals;
1497c478bdstevel@tonic-gate			fltset_t faults;
1507c478bdstevel@tonic-gate			sysset_t syscalls;
1517c478bdstevel@tonic-gate			siginfo_t siginfo;
1527c478bdstevel@tonic-gate		} arg;
1537c478bdstevel@tonic-gate	} ctl;
1547c478bdstevel@tonic-gate
1557c478bdstevel@tonic-gate#if PTRACE_DEBUG
1567c478bdstevel@tonic-gate	fprintf(stderr, " ptrace(%s, 0x%X, 0x%X, 0x%X)\n",
1578cd4554raf	    map(request), pid, addr, data);
1587c478bdstevel@tonic-gate#endif
1597c478bdstevel@tonic-gate
1608cd4554raf	(void) mutex_lock(&pt_lock);
1617c478bdstevel@tonic-gate
1627c478bdstevel@tonic-gate	if (request == 0) {	/* PTRACE_TRACEME, executed by traced process */
1637c478bdstevel@tonic-gate		/*
1647c478bdstevel@tonic-gate		 * Set stop-on-all-signals and nothing else.
1657c478bdstevel@tonic-gate		 * Turn off inherit-on-fork flag (grandchildren run away).
1667c478bdstevel@tonic-gate		 * Set ptrace-compatible flag.
1677c478bdstevel@tonic-gate		 */
1687c478bdstevel@tonic-gate		char procname[64];	/* /proc/<pid>/ctl */
1697c478bdstevel@tonic-gate		int fd;
1707c478bdstevel@tonic-gate
1717c478bdstevel@tonic-gate		MakeProcName(procname, getpid());
1727c478bdstevel@tonic-gate		(void) strcat(procname, "/ctl");
1737c478bdstevel@tonic-gate		if ((fd = open(procname, O_WRONLY, 0)) < 0)
1747c478bdstevel@tonic-gate			exit(255);
1757c478bdstevel@tonic-gate		ctl.cmd = PCSTRACE;
1767c478bdstevel@tonic-gate		prfillset(&ctl.arg.signals);
1777c478bdstevel@tonic-gate		if (write(fd, (char *)&ctl, sizeof (long)+sizeof (sigset_t))
1787c478bdstevel@tonic-gate		    != sizeof (long)+sizeof (sigset_t))
1797c478bdstevel@tonic-gate			exit(255);
1807c478bdstevel@tonic-gate		ctl.cmd = PCSFAULT;
1817c478bdstevel@tonic-gate		premptyset(&ctl.arg.faults);
1827c478bdstevel@tonic-gate		if (write(fd, (char *)&ctl, sizeof (long)+sizeof (fltset_t))
1837c478bdstevel@tonic-gate		    != sizeof (long)+sizeof (fltset_t))
1847c478bdstevel@tonic-gate			exit(255);
1857c478bdstevel@tonic-gate		ctl.cmd = PCSENTRY;
1867c478bdstevel@tonic-gate		premptyset(&ctl.arg.syscalls);
1877c478bdstevel@tonic-gate		if (write(fd, (char *)&ctl, sizeof (long)+sizeof (sysset_t))
1887c478bdstevel@tonic-gate		    != sizeof (long)+sizeof (sysset_t))
1897c478bdstevel@tonic-gate			exit(255);
1907c478bdstevel@tonic-gate		ctl.cmd = PCSEXIT;
1917c478bdstevel@tonic-gate		premptyset(&ctl.arg.syscalls);
1927c478bdstevel@tonic-gate		if (write(fd, (char *)&ctl, sizeof (long)+sizeof (sysset_t))
1937c478bdstevel@tonic-gate		    != sizeof (long)+sizeof (sysset_t))
1947c478bdstevel@tonic-gate			exit(255);
1957c478bdstevel@tonic-gate		ctl.cmd = PCUNSET;
1967c478bdstevel@tonic-gate		ctl.arg.flags = PR_FORK;
1977c478bdstevel@tonic-gate		if (write(fd, (char *)&ctl, sizeof (long)+sizeof (long))
1987c478bdstevel@tonic-gate		    != sizeof (long)+sizeof (long))
1997c478bdstevel@tonic-gate			exit(255);
2007c478bdstevel@tonic-gate		ctl.cmd = PCSET;
2017c478bdstevel@tonic-gate		ctl.arg.flags = PR_PTRACE;
2027c478bdstevel@tonic-gate		if (write(fd, (char *)&ctl, sizeof (long)+sizeof (long))
2037c478bdstevel@tonic-gate		    != sizeof (long)+sizeof (long))
2047c478bdstevel@tonic-gate			exit(255);
2057c478bdstevel@tonic-gate		if (close(fd) != 0)
2067c478bdstevel@tonic-gate			exit(255);
2077c478bdstevel@tonic-gate
2088cd4554raf		(void) mutex_unlock(&pt_lock);
2097c478bdstevel@tonic-gate		return (0);
2107c478bdstevel@tonic-gate	}
2117c478bdstevel@tonic-gate
2127c478bdstevel@tonic-gateagain:
2137c478bdstevel@tonic-gate	errno = 0;
2147c478bdstevel@tonic-gate
2157c478bdstevel@tonic-gate	/* find the cstatus structure corresponding to pid */
2167c478bdstevel@tonic-gate	if ((cp = GrabProc(pid)) == NULLCP)
2177c478bdstevel@tonic-gate		goto esrch;
2187c478bdstevel@tonic-gate
2197c478bdstevel@tonic-gate	ps = &cp->pstatus;
2207c478bdstevel@tonic-gate	if (!(ps->pr_flags & PR_ISTOP)) {
2217c478bdstevel@tonic-gate		if (ProcUpdate(cp) != 0) {
2227c478bdstevel@tonic-gate			ReleaseProc(cp);
2237c478bdstevel@tonic-gate			goto esrch;
2247c478bdstevel@tonic-gate		}
2257c478bdstevel@tonic-gate		if (!(ps->pr_flags & PR_ISTOP))
2267c478bdstevel@tonic-gate			goto esrch;
2277c478bdstevel@tonic-gate	}
2287c478bdstevel@tonic-gate
2297c478bdstevel@tonic-gate	/*
2307c478bdstevel@tonic-gate	 * Process the request.
2317c478bdstevel@tonic-gate	 */
2327c478bdstevel@tonic-gate	errno = 0;
2337c478bdstevel@tonic-gate	switch (request) {
2347c478bdstevel@tonic-gate	case 1:		/* PTRACE_PEEKTEXT */
2357c478bdstevel@tonic-gate	case 2:		/* PTRACE_PEEKDATA */
2367c478bdstevel@tonic-gate		if (addr & 03)
2377c478bdstevel@tonic-gate			goto eio;
2387c478bdstevel@tonic-gate		if (pread(cp->asfd, (char *)&data, sizeof (data), (off_t)addr)
2397c478bdstevel@tonic-gate		    == sizeof (data)) {
2408cd4554raf			(void) mutex_unlock(&pt_lock);
2417c478bdstevel@tonic-gate			return (data);
2427c478bdstevel@tonic-gate		}
2437c478bdstevel@tonic-gate		goto eio;
2447c478bdstevel@tonic-gate
2457c478bdstevel@tonic-gate	case 3:		/* PTRACE_PEEKUSER */
2467c478bdstevel@tonic-gate		if (addr & 03)
2477c478bdstevel@tonic-gate			goto eio;
2487c478bdstevel@tonic-gate		xaddr = addr;
2497c478bdstevel@tonic-gate		if (xaddr >= REGADDR && xaddr < REGADDR+sizeof (gregset_t))
2507c478bdstevel@tonic-gate			xaddr -= REGADDR-U_REG;
2517c478bdstevel@tonic-gate		if (xaddr >= U_PSARGS && xaddr < U_PSARGS+sizeof (UP->u_psargs))
2527c478bdstevel@tonic-gate			GetPsargs(cp);
2537c478bdstevel@tonic-gate		if (xaddr >= U_SIGNAL && xaddr < U_SIGNAL+sizeof (UP->u_signal))
2547c478bdstevel@tonic-gate			GetSignal(cp);
2557c478bdstevel@tonic-gate		if ((int)xaddr >= 0 && xaddr < U_END) {
2567c478bdstevel@tonic-gate			/* LINTED pointer alignment */
2577c478bdstevel@tonic-gate			data = *((int *)((caddr_t)(&cp->user) + xaddr));
2588cd4554raf			(void) mutex_unlock(&pt_lock);
2597c478bdstevel@tonic-gate			return (data);
2607c478bdstevel@tonic-gate		}
2617c478bdstevel@tonic-gate		goto eio;
2627c478bdstevel@tonic-gate
2637c478bdstevel@tonic-gate	case 4:		/* PTRACE_POKETEXT */
2647c478bdstevel@tonic-gate	case 5:		/* PTRACE_POKEDATA */
2657c478bdstevel@tonic-gate		if (addr & 03)
2667c478bdstevel@tonic-gate			goto eio;
2677c478bdstevel@tonic-gate		xaddr = addr;
2687c478bdstevel@tonic-gate		if (xaddr >= (unsigned)cp->user.u_reg[REG_SP] &&
2697c478bdstevel@tonic-gate		    xaddr < (unsigned)cp->user.u_reg[REG_SP]+16*sizeof (int))
2707c478bdstevel@tonic-gate			cp->flags |= CS_SETREGS;
2717c478bdstevel@tonic-gate		if (pwrite(cp->asfd, (char *)&data, sizeof (data), (off_t)addr)
2727c478bdstevel@tonic-gate		    == sizeof (data)) {
2738cd4554raf			(void) mutex_unlock(&pt_lock);
2747c478bdstevel@tonic-gate			return (data);
2757c478bdstevel@tonic-gate		}
2767c478bdstevel@tonic-gate		goto eio;
2777c478bdstevel@tonic-gate
2787c478bdstevel@tonic-gate	case 6:		/* PTRACE_POKEUSER */
2797c478bdstevel@tonic-gate		if (addr & 03)
2807c478bdstevel@tonic-gate			goto eio;
2817c478bdstevel@tonic-gate		xaddr = addr;
2827c478bdstevel@tonic-gate		if (xaddr >= REGADDR && xaddr < REGADDR+sizeof (gregset_t))
2837c478bdstevel@tonic-gate			xaddr -= REGADDR-U_REG;
2847c478bdstevel@tonic-gate		if ((int)xaddr >= U_REG && xaddr < U_REG+sizeof (gregset_t)) {
2857c478bdstevel@tonic-gate			int rx = (xaddr-U_REG)/sizeof (greg_t);
2867c478bdstevel@tonic-gate			if (rx == REG_PS)
2877c478bdstevel@tonic-gate				data = (cp->user.u_reg[REG_PS] &
2887c478bdstevel@tonic-gate				    ~PSL_USERMASK) | (data & PSL_USERMASK);
2897c478bdstevel@tonic-gate			else if (rx == REG_SP || rx == REG_PC || rx == REG_nPC)
2907c478bdstevel@tonic-gate				data &= ~03;
2917c478bdstevel@tonic-gate			cp->user.u_reg[rx] = data;
2927c478bdstevel@tonic-gate			cp->flags |= CS_SETREGS;
2938cd4554raf			(void) mutex_unlock(&pt_lock);
2947c478bdstevel@tonic-gate			return (data);
2957c478bdstevel@tonic-gate		}
2967c478bdstevel@tonic-gate		goto eio;
2977c478bdstevel@tonic-gate
2987c478bdstevel@tonic-gate	case 7:		/* PTRACE_CONT */
2997c478bdstevel@tonic-gate	case 9:		/* PTRACE_SINGLESTEP */
3008cd4554raf	{
3017c478bdstevel@tonic-gate		long runctl[3];
3027c478bdstevel@tonic-gate
3037c478bdstevel@tonic-gate		if (cp->flags & CS_SETREGS) {
3047c478bdstevel@tonic-gate			long cmd;
3057c478bdstevel@tonic-gate			iovec_t iov[2];
3067c478bdstevel@tonic-gate
3077c478bdstevel@tonic-gate			ps->pr_lwp.pr_reg[R_PSR] = cp->user.u_reg[REG_PSR];
3087c478bdstevel@tonic-gate			ps->pr_lwp.pr_reg[R_PC]  = cp->user.u_reg[REG_PC];
3097c478bdstevel@tonic-gate			ps->pr_lwp.pr_reg[R_nPC] = cp->user.u_reg[REG_nPC];
3107c478bdstevel@tonic-gate			ps->pr_lwp.pr_reg[R_Y]   = cp->user.u_reg[REG_Y];
3117c478bdstevel@tonic-gate			ps->pr_lwp.pr_reg[R_G1]  = cp->user.u_reg[REG_G1];
3127c478bdstevel@tonic-gate			ps->pr_lwp.pr_reg[R_G2]  = cp->user.u_reg[REG_G2];
3137c478bdstevel@tonic-gate			ps->pr_lwp.pr_reg[R_G3]  = cp->user.u_reg[REG_G3];
3147c478bdstevel@tonic-gate			ps->pr_lwp.pr_reg[R_G4]  = cp->user.u_reg[REG_G4];
3157c478bdstevel@tonic-gate			ps->pr_lwp.pr_reg[R_G5]  = cp->user.u_reg[REG_G5];
3167c478bdstevel@tonic-gate			ps->pr_lwp.pr_reg[R_G6]  = cp->user.u_reg[REG_G6];
3177c478bdstevel@tonic-gate			ps->pr_lwp.pr_reg[R_G7]  = cp->user.u_reg[REG_G7];
3187c478bdstevel@tonic-gate			ps->pr_lwp.pr_reg[R_O0]  = cp->user.u_reg[REG_O0];
3197c478bdstevel@tonic-gate			ps->pr_lwp.pr_reg[R_O1]  = cp->user.u_reg[REG_O1];
3207c478bdstevel@tonic-gate			ps->pr_lwp.pr_reg[R_O2]  = cp->user.u_reg[REG_O2];
3217c478bdstevel@tonic-gate			ps->pr_lwp.pr_reg[R_O3]  = cp->user.u_reg[REG_O3];
3227c478bdstevel@tonic-gate			ps->pr_lwp.pr_reg[R_O4]  = cp->user.u_reg[REG_O4];
3237c478bdstevel@tonic-gate			ps->pr_lwp.pr_reg[R_O5]  = cp->user.u_reg[REG_O5];
3247c478bdstevel@tonic-gate			ps->pr_lwp.pr_reg[R_O6]  = cp->user.u_reg[REG_O6];
3257c478bdstevel@tonic-gate			ps->pr_lwp.pr_reg[R_O7]  = cp->user.u_reg[REG_O7];
3267c478bdstevel@tonic-gate			(void) pread(cp->asfd, (char *)&ps->pr_lwp.pr_reg[R_L0],
3278cd4554raf			    16*sizeof (int), (off_t)cp->user.u_reg[REG_SP]);
3287c478bdstevel@tonic-gate			cmd = PCSREG;
3297c478bdstevel@tonic-gate			iov[0].iov_base = (caddr_t)&cmd;
3307c478bdstevel@tonic-gate			iov[0].iov_len = sizeof (long);
3317c478bdstevel@tonic-gate			iov[1].iov_base = (caddr_t)&ps->pr_lwp.pr_reg[0];
3327c478bdstevel@tonic-gate			iov[1].iov_len = sizeof (ps->pr_lwp.pr_reg);
3337c478bdstevel@tonic-gate			if (writev(cp->ctlfd, iov, 2) < 0)
3347c478bdstevel@tonic-gate				goto tryagain;
3357c478bdstevel@tonic-gate		}
3367c478bdstevel@tonic-gate		if (addr != 1 &&	/* new virtual address */
3377c478bdstevel@tonic-gate		    (addr & ~03) != cp->user.u_reg[REG_PC]) {
3387c478bdstevel@tonic-gate			runctl[0] = PCSVADDR;
3397c478bdstevel@tonic-gate			runctl[1] = (addr & ~03);
3407c478bdstevel@tonic-gate			if (write(cp->ctlfd, (char *)runctl, 2*sizeof (long))
3417c478bdstevel@tonic-gate			    != 2*sizeof (long))
3427c478bdstevel@tonic-gate				goto tryagain;
3437c478bdstevel@tonic-gate		}
3447c478bdstevel@tonic-gate		/* make data the current signal */
3457c478bdstevel@tonic-gate		if (data != 0 && data != ps->pr_lwp.pr_cursig) {
3467c478bdstevel@tonic-gate			(void) memset((char *)&ctl.arg.siginfo, 0,
3477c478bdstevel@tonic-gate			    sizeof (siginfo_t));
3487c478bdstevel@tonic-gate			ctl.arg.siginfo.si_signo = data;
3497c478bdstevel@tonic-gate			ctl.cmd = PCSSIG;
3507c478bdstevel@tonic-gate			if (write(cp->ctlfd, (char *)&ctl,
3517c478bdstevel@tonic-gate			    sizeof (long)+sizeof (siginfo_t))
3527c478bdstevel@tonic-gate			    != sizeof (long)+sizeof (siginfo_t))
3537c478bdstevel@tonic-gate				goto tryagain;
3547c478bdstevel@tonic-gate		}
3557c478bdstevel@tonic-gate		if (data == 0)
3567c478bdstevel@tonic-gate			runctl[0] = PCCSIG;
3577c478bdstevel@tonic-gate		else
3587c478bdstevel@tonic-gate			runctl[0] = PCNULL;
3597c478bdstevel@tonic-gate		runctl[1] = PCRUN;
3607c478bdstevel@tonic-gate		runctl[2] = (request == 9)? PRSTEP : 0;
3617c478bdstevel@tonic-gate		if (write(cp->ctlfd, (char *)runctl, 3*sizeof (long))
3627c478bdstevel@tonic-gate		    != 3*sizeof (long)) {
3637c478bdstevel@tonic-gate			if (errno == ENOENT) {
3647c478bdstevel@tonic-gate				/* current signal must have killed it */
3657c478bdstevel@tonic-gate				ReleaseProc(cp);
3668cd4554raf				(void) mutex_unlock(&pt_lock);
3677c478bdstevel@tonic-gate				return (data);
3687c478bdstevel@tonic-gate			}
3697c478bdstevel@tonic-gate			goto tryagain;
3707c478bdstevel@tonic-gate		}
3717c478bdstevel@tonic-gate		(void) memset((char *)ps, 0, sizeof (pstatus_t));
3727c478bdstevel@tonic-gate		cp->flags = 0;
3738cd4554raf		(void) mutex_unlock(&pt_lock);
3747c478bdstevel@tonic-gate		return (data);
3758cd4554raf	}
3767c478bdstevel@tonic-gate
3777c478bdstevel@tonic-gate	case 8:		/* PTRACE_KILL */
3787c478bdstevel@tonic-gate		/* overkill? */
3797c478bdstevel@tonic-gate		(void) memset((char *)&ctl.arg.siginfo, 0, sizeof (siginfo_t));
3807c478bdstevel@tonic-gate		ctl.arg.siginfo.si_signo = SIGKILL;
3817c478bdstevel@tonic-gate		ctl.cmd = PCSSIG;
3827c478bdstevel@tonic-gate		(void) write(cp->ctlfd, (char *)&ctl,
3837c478bdstevel@tonic-gate		    sizeof (long)+sizeof (siginfo_t));
3847c478bdstevel@tonic-gate		(void) kill(pid, SIGKILL);
3857c478bdstevel@tonic-gate		ReleaseProc(cp);
3868cd4554raf		(void) mutex_unlock(&pt_lock);
3877c478bdstevel@tonic-gate		return (0);
3887c478bdstevel@tonic-gate
3897c478bdstevel@tonic-gate	default:
3907c478bdstevel@tonic-gate		goto eio;
3917c478bdstevel@tonic-gate	}
3927c478bdstevel@tonic-gate
3937c478bdstevel@tonic-gatetryagain:
3947c478bdstevel@tonic-gate	if (errno == EAGAIN) {
3957c478bdstevel@tonic-gate		if (OpenProc(cp) == 0)
3967c478bdstevel@tonic-gate			goto again;
3977c478bdstevel@tonic-gate		ReleaseProc(cp);
3987c478bdstevel@tonic-gate	}
3997c478bdstevel@tonic-gateeio:
4007c478bdstevel@tonic-gate	errno = EIO;
4018cd4554raf	(void) mutex_unlock(&pt_lock);
4027c478bdstevel@tonic-gate	return (-1);
4037c478bdstevel@tonic-gateesrch:
4047c478bdstevel@tonic-gate	errno = ESRCH;
4058cd4554raf	(void) mutex_unlock(&pt_lock);
4067c478bdstevel@tonic-gate	return (-1);
4077c478bdstevel@tonic-gate}
4087c478bdstevel@tonic-gate
4097c478bdstevel@tonic-gate/*
4107c478bdstevel@tonic-gate * Find the cstatus structure corresponding to pid.
4117c478bdstevel@tonic-gate */
4127c478bdstevel@tonic-gatestatic cstatus_t *
4137c478bdstevel@tonic-gateFindProc(pid_t pid)
4147c478bdstevel@tonic-gate{
4157c478bdstevel@tonic-gate	cstatus_t *cp;
4167c478bdstevel@tonic-gate
4177c478bdstevel@tonic-gate	for (cp = childp; cp != NULLCP; cp = cp->next)
4187c478bdstevel@tonic-gate		if (cp->pid == pid)
4197c478bdstevel@tonic-gate			break;
4207c478bdstevel@tonic-gate
4217c478bdstevel@tonic-gate	return (cp);
4227c478bdstevel@tonic-gate}
4237c478bdstevel@tonic-gate
4247c478bdstevel@tonic-gate/*
4257c478bdstevel@tonic-gate * Check every proc for existence, release those that are gone.
4267c478bdstevel@tonic-gate * Be careful about the linked list; ReleaseProc() changes it.
4277c478bdstevel@tonic-gate */
4287c478bdstevel@tonic-gatestatic void
4297c478bdstevel@tonic-gateCheckAllProcs()
4307c478bdstevel@tonic-gate{
4317c478bdstevel@tonic-gate	cstatus_t *cp = childp;
4327c478bdstevel@tonic-gate
4337c478bdstevel@tonic-gate	while (cp != NULLCP) {
4347c478bdstevel@tonic-gate		cstatus_t *next = cp->next;
4357c478bdstevel@tonic-gate
4367c478bdstevel@tonic-gate		if (ProcUpdate(cp) != 0)
4377c478bdstevel@tonic-gate			ReleaseProc(cp);
4387c478bdstevel@tonic-gate		cp = next;
4397c478bdstevel@tonic-gate	}
4407c478bdstevel@tonic-gate}
4417c478bdstevel@tonic-gate
4427c478bdstevel@tonic-gate/*
4437c478bdstevel@tonic-gate * Utility for OpenProc().
4447c478bdstevel@tonic-gate */
4457c478bdstevel@tonic-gatestatic int
4467c478bdstevel@tonic-gateDupfd(int fd, int dfd)
4477c478bdstevel@tonic-gate{
4487c478bdstevel@tonic-gate	/*
4497c478bdstevel@tonic-gate	 * Make sure fd not one of 0, 1, or 2 to avoid stdio interference.
4507c478bdstevel@tonic-gate	 * Also, if dfd is greater than 2, dup fd to be exactly dfd.
4517c478bdstevel@tonic-gate	 */
4527c478bdstevel@tonic-gate	if (dfd > 2 || (0 <= fd && fd <= 2)) {
4537c478bdstevel@tonic-gate		if (dfd > 2 && fd != dfd)
4547c478bdstevel@tonic-gate			(void) close(dfd);
4557c478bdstevel@tonic-gate		else
4567c478bdstevel@tonic-gate			dfd = 3;
4577c478bdstevel@tonic-gate		if (fd != dfd) {
4587c478bdstevel@tonic-gate			dfd = fcntl(fd, F_DUPFD, (intptr_t)dfd);
4597c478bdstevel@tonic-gate			(void) close(fd);
4607c478bdstevel@tonic-gate			fd = dfd;
4617c478bdstevel@tonic-gate		}
4627c478bdstevel@tonic-gate	}
4637c478bdstevel@tonic-gate	/*
4647c478bdstevel@tonic-gate	 * Mark filedescriptor close-on-exec.
4657c478bdstevel@tonic-gate	 * Should also be close-on-return-from-fork-in-child.
4667c478bdstevel@tonic-gate	 */
4677c478bdstevel@tonic-gate	(void) fcntl(fd, F_SETFD, (intptr_t)1);
4687c478bdstevel@tonic-gate	return (fd);
4697c478bdstevel@tonic-gate}
4707c478bdstevel@tonic-gate
4717c478bdstevel@tonic-gate/*
4727c478bdstevel@tonic-gate * Construct the /proc directory name:  "/proc/<pid>"
4737c478bdstevel@tonic-gate * The name buffer passed by the caller must be large enough.
4747c478bdstevel@tonic-gate */
4757c478bdstevel@tonic-gatestatic void
4767c478bdstevel@tonic-gateMakeProcName(char *procname, pid_t pid)
4777c478bdstevel@tonic-gate{
4788793b36Nick Todd	(void) sprintf(procname, "/proc/%d", (int)pid);
4797c478bdstevel@tonic-gate}
4807c478bdstevel@tonic-gate
4817c478bdstevel@tonic-gate/*
4827c478bdstevel@tonic-gate * Open/reopen the /proc/<pid> files.
4837c478bdstevel@tonic-gate */
4847c478bdstevel@tonic-gatestatic int
4857c478bdstevel@tonic-gateOpenProc(cstatus_t *cp)
4867c478bdstevel@tonic-gate{
4877c478bdstevel@tonic-gate	char procname[64];		/* /proc/nnnnn/fname */
4887c478bdstevel@tonic-gate	char *fname;
4897c478bdstevel@tonic-gate	int fd;
4907c478bdstevel@tonic-gate	int omode;
4917c478bdstevel@tonic-gate
4927c478bdstevel@tonic-gate	MakeProcName(procname, cp->pid);
4937c478bdstevel@tonic-gate	fname = procname + strlen(procname);
4947c478bdstevel@tonic-gate
4957c478bdstevel@tonic-gate	/*
4967c478bdstevel@tonic-gate	 * Use exclusive-open only if this is the first open.
4977c478bdstevel@tonic-gate	 */
4987c478bdstevel@tonic-gate	omode = (cp->asfd > 0)? O_RDWR : (O_RDWR|O_EXCL);
4997c478bdstevel@tonic-gate	(void) strcpy(fname, "/as");
5007c478bdstevel@tonic-gate	if ((fd = open(procname, omode, 0)) < 0 ||
5017c478bdstevel@tonic-gate	    (cp->asfd = Dupfd(fd, cp->asfd)) < 0)
5027c478bdstevel@tonic-gate		goto err;
5037c478bdstevel@tonic-gate
5047c478bdstevel@tonic-gate	(void) strcpy(fname, "/ctl");
5057c478bdstevel@tonic-gate	if ((fd = open(procname, O_WRONLY, 0)) < 0 ||
5067c478bdstevel@tonic-gate	    (cp->ctlfd = Dupfd(fd, cp->ctlfd)) < 0)
5077c478bdstevel@tonic-gate		goto err;
5087c478bdstevel@tonic-gate
5097c478bdstevel@tonic-gate	(void) strcpy(fname, "/status");
5107c478bdstevel@tonic-gate	if ((fd = open(procname, O_RDONLY, 0)) < 0 ||
5117c478bdstevel@tonic-gate	    (cp->statusfd = Dupfd(fd, cp->statusfd)) < 0)
5127c478bdstevel@tonic-gate		goto err;
5137c478bdstevel@tonic-gate
5147c478bdstevel@tonic-gate	return (0);
5157c478bdstevel@tonic-gate
5167c478bdstevel@tonic-gateerr:
5177c478bdstevel@tonic-gate	CloseProc(cp);
5187c478bdstevel@tonic-gate	return (-1);
5197c478bdstevel@tonic-gate}
5207c478bdstevel@tonic-gate
5217c478bdstevel@tonic-gate/*
5227c478bdstevel@tonic-gate * Close the /proc/<pid> files.
5237c478bdstevel@tonic-gate */
5247c478bdstevel@tonic-gatestatic void
5257c478bdstevel@tonic-gateCloseProc(cstatus_t *cp)
5267c478bdstevel@tonic-gate{
5277c478bdstevel@tonic-gate	if (cp->asfd > 0)
5287c478bdstevel@tonic-gate		(void) close(cp->asfd);
5297c478bdstevel@tonic-gate	if (cp->ctlfd > 0)
5307c478bdstevel@tonic-gate		(void) close(cp->ctlfd);
5317c478bdstevel@tonic-gate	if (cp->statusfd > 0)
5327c478bdstevel@tonic-gate		(void) close(cp->statusfd);
5337c478bdstevel@tonic-gate	cp->asfd = 0;
5347c478bdstevel@tonic-gate	cp->ctlfd = 0;
5357c478bdstevel@tonic-gate	cp->statusfd = 0;
5367c478bdstevel@tonic-gate}
5377c478bdstevel@tonic-gate
5387c478bdstevel@tonic-gate/*
5397c478bdstevel@tonic-gate * Take control of a child process.
5407c478bdstevel@tonic-gate */
5417c478bdstevel@tonic-gatestatic cstatus_t *
5427c478bdstevel@tonic-gateGrabProc(pid_t pid)
5437c478bdstevel@tonic-gate{
5447c478bdstevel@tonic-gate	cstatus_t *cp;
5457c478bdstevel@tonic-gate	long ctl[2];
5467c478bdstevel@tonic-gate	pid_t ppid;
5477c478bdstevel@tonic-gate
5487c478bdstevel@tonic-gate	if (pid <= 0)
5497c478bdstevel@tonic-gate		return (NULLCP);
5507c478bdstevel@tonic-gate
5517c478bdstevel@tonic-gate	if ((cp = FindProc(pid)) != NULLCP)	/* already grabbed */
5527c478bdstevel@tonic-gate		return (cp);
5537c478bdstevel@tonic-gate
5547c478bdstevel@tonic-gate	CheckAllProcs();	/* clean up before grabbing new process */
5557c478bdstevel@tonic-gate
5567c478bdstevel@tonic-gate	cp = (cstatus_t *)malloc(sizeof (cstatus_t));
5577c478bdstevel@tonic-gate	if (cp == NULLCP)
5587c478bdstevel@tonic-gate		return (NULLCP);
5597c478bdstevel@tonic-gate	(void) memset((char *)cp, 0, sizeof (cstatus_t));
5607c478bdstevel@tonic-gate	cp->pid = pid;
5617c478bdstevel@tonic-gate
5627c478bdstevel@tonic-gate	ppid = getpid();
5637c478bdstevel@tonic-gate	while (OpenProc(cp) == 0) {
5647c478bdstevel@tonic-gate		ctl[0] = PCSET;
5657c478bdstevel@tonic-gate		ctl[1] = PR_RLC;
5667c478bdstevel@tonic-gate		errno = 0;
5677c478bdstevel@tonic-gate
5687c478bdstevel@tonic-gate		if (pread(cp->statusfd, (char *)&cp->pstatus,
5697c478bdstevel@tonic-gate		    sizeof (cp->pstatus), (off_t)0) == sizeof (cp->pstatus) &&
5707c478bdstevel@tonic-gate		    cp->pstatus.pr_ppid == ppid &&
5717c478bdstevel@tonic-gate		    (cp->pstatus.pr_flags & PR_PTRACE) &&
5727c478bdstevel@tonic-gate		    write(cp->ctlfd, (char *)ctl, 2*sizeof (long))
5737c478bdstevel@tonic-gate		    == 2*sizeof (long)) {
5747c478bdstevel@tonic-gate			cp->next = childp;
5757c478bdstevel@tonic-gate			childp = cp;
5767c478bdstevel@tonic-gate			MakeUser(cp);
5777c478bdstevel@tonic-gate			return (cp);
5787c478bdstevel@tonic-gate		}
5797c478bdstevel@tonic-gate
5807c478bdstevel@tonic-gate		if (errno != EAGAIN)
5817c478bdstevel@tonic-gate			break;
5827c478bdstevel@tonic-gate	}
5837c478bdstevel@tonic-gate
5847c478bdstevel@tonic-gate	free((char *)cp);
5857c478bdstevel@tonic-gate	return (NULLCP);
5867c478bdstevel@tonic-gate}
5877c478bdstevel@tonic-gate
5887c478bdstevel@tonic-gate/*
5897c478bdstevel@tonic-gate * Close the /proc/<pid> file, if open.
5907c478bdstevel@tonic-gate * Deallocate the memory used by the cstatus_t structure.
5917c478bdstevel@tonic-gate */
5927c478bdstevel@tonic-gatestatic void
5937c478bdstevel@tonic-gateReleaseProc(cstatus_t *cp)
5947c478bdstevel@tonic-gate{
5957c478bdstevel@tonic-gate	CloseProc(cp);
5967c478bdstevel@tonic-gate
5977c478bdstevel@tonic-gate	if (childp == cp)
5987c478bdstevel@tonic-gate		childp = cp->next;
5997c478bdstevel@tonic-gate	else {
6007c478bdstevel@tonic-gate		cstatus_t *pcp;
6017c478bdstevel@tonic-gate
6027c478bdstevel@tonic-gate		for (pcp = childp; pcp != NULLCP; pcp = pcp->next) {
6037c478bdstevel@tonic-gate			if (pcp->next == cp) {
6047c478bdstevel@tonic-gate				pcp->next = cp->next;
6057c478bdstevel@tonic-gate				break;
6067c478bdstevel@tonic-gate			}
6077c478bdstevel@tonic-gate		}
6087c478bdstevel@tonic-gate	}
6097c478bdstevel@tonic-gate
6107c478bdstevel@tonic-gate	free((char *)cp);
6117c478bdstevel@tonic-gate}
6127c478bdstevel@tonic-gate
6137c478bdstevel@tonic-gate/*
6147c478bdstevel@tonic-gate * Update process information from /proc.
6157c478bdstevel@tonic-gate * Return 0 on success, -1 on failure.
6167c478bdstevel@tonic-gate */
6177c478bdstevel@tonic-gatestatic int
6187c478bdstevel@tonic-gateProcUpdate(cstatus_t *cp)
6197c478bdstevel@tonic-gate{
6207c478bdstevel@tonic-gate	pstatus_t *ps = &cp->pstatus;
6217c478bdstevel@tonic-gate
6227c478bdstevel@tonic-gate	if (cp->flags & CS_SETREGS) {
6237c478bdstevel@tonic-gate		long cmd;
6247c478bdstevel@tonic-gate		iovec_t iov[2];
6257c478bdstevel@tonic-gate
6267c478bdstevel@tonic-gate		ps->pr_lwp.pr_reg[R_PSR] = cp->user.u_reg[REG_PSR];
6277c478bdstevel@tonic-gate		ps->pr_lwp.pr_reg[R_PC]  = cp->user.u_reg[REG_PC];
6287c478bdstevel@tonic-gate		ps->pr_lwp.pr_reg[R_nPC] = cp->user.u_reg[REG_nPC];
6297c478bdstevel@tonic-gate		ps->pr_lwp.pr_reg[R_Y]   = cp->user.u_reg[REG_Y];
6307c478bdstevel@tonic-gate		ps->pr_lwp.pr_reg[R_G1]  = cp->user.u_reg[REG_G1];
6317c478bdstevel@tonic-gate		ps->pr_lwp.pr_reg[R_G2]  = cp->user.u_reg[REG_G2];
6327c478bdstevel@tonic-gate		ps->pr_lwp.pr_reg[R_G3]  = cp->user.u_reg[REG_G3];
6337c478bdstevel@tonic-gate		ps->pr_lwp.pr_reg[R_G4]  = cp->user.u_reg[REG_G4];
6347c478bdstevel@tonic-gate		ps->pr_lwp.pr_reg[R_G5]  = cp->user.u_reg[REG_G5];
6357c478bdstevel@tonic-gate		ps->pr_lwp.pr_reg[R_G6]  = cp->user.u_reg[REG_G6];
6367c478bdstevel@tonic-gate		ps->pr_lwp.pr_reg[R_G7]  = cp->user.u_reg[REG_G7];
6377c478bdstevel@tonic-gate		ps->pr_lwp.pr_reg[R_O0]  = cp->user.u_reg[REG_O0];
6387c478bdstevel@tonic-gate		ps->pr_lwp.pr_reg[R_O1]  = cp->user.u_reg[REG_O1];
6397c478bdstevel@tonic-gate		ps->pr_lwp.pr_reg[R_O2]  = cp->user.u_reg[REG_O2];
6407c478bdstevel@tonic-gate		ps->pr_lwp.pr_reg[R_O3]  = cp->user.u_reg[REG_O3];
6417c478bdstevel@tonic-gate		ps->pr_lwp.pr_reg[R_O4]  = cp->user.u_reg[REG_O4];
6427c478bdstevel@tonic-gate		ps->pr_lwp.pr_reg[R_O5]  = cp->user.u_reg[REG_O5];
6437c478bdstevel@tonic-gate		ps->pr_lwp.pr_reg[R_O6]  = cp->user.u_reg[REG_O6];
6447c478bdstevel@tonic-gate		ps->pr_lwp.pr_reg[R_O7]  = cp->user.u_reg[REG_O7];
6457c478bdstevel@tonic-gate		(void) pread(cp->asfd, (char *)&ps->pr_lwp.pr_reg[R_L0],
6468cd4554raf		    16*sizeof (int), (off_t)cp->user.u_reg[REG_SP]);
6477c478bdstevel@tonic-gate		cmd = PCSREG;
6487c478bdstevel@tonic-gate		iov[0].iov_base = (caddr_t)&cmd;
6497c478bdstevel@tonic-gate		iov[0].iov_len = sizeof (long);
6507c478bdstevel@tonic-gate		iov[1].iov_base = (caddr_t)&ps->pr_lwp.pr_reg[0];
6517c478bdstevel@tonic-gate		iov[1].iov_len = sizeof (ps->pr_lwp.pr_reg);
6527c478bdstevel@tonic-gate		(void) writev(cp->ctlfd, iov, 2);
6537c478bdstevel@tonic-gate		cp->flags &= ~CS_SETREGS;
6547c478bdstevel@tonic-gate	}
6557c478bdstevel@tonic-gate
6567c478bdstevel@tonic-gate	while (pread(cp->statusfd, (char *)ps, sizeof (*ps), (off_t)0) < 0) {
6577c478bdstevel@tonic-gate		/* attempt to regain control */
6587c478bdstevel@tonic-gate		if (errno != EINTR &&
6597c478bdstevel@tonic-gate		    !(errno == EAGAIN && OpenProc(cp) == 0))
6607c478bdstevel@tonic-gate			return (-1);
6617c478bdstevel@tonic-gate	}
6627c478bdstevel@tonic-gate
6637c478bdstevel@tonic-gate	if (ps->pr_flags & PR_ISTOP)
6647c478bdstevel@tonic-gate		MakeUser(cp);
6657c478bdstevel@tonic-gate	else
6667c478bdstevel@tonic-gate		(void) memset((char *)ps, 0, sizeof (pstatus_t));
6677c478bdstevel@tonic-gate
6687c478bdstevel@tonic-gate	return (0);
6697c478bdstevel@tonic-gate}
6707c478bdstevel@tonic-gate
6717c478bdstevel@tonic-gate/*
6727c478bdstevel@tonic-gate * Manufacture the contents of the fake u-block.
6737c478bdstevel@tonic-gate */
6747c478bdstevel@tonic-gatestatic void
6757c478bdstevel@tonic-gateMakeUser(cstatus_t *cp)
6767c478bdstevel@tonic-gate{
6777c478bdstevel@tonic-gate	pstatus_t *ps = &cp->pstatus;
6787c478bdstevel@tonic-gate
6797c478bdstevel@tonic-gate	cp->user.u_reg[REG_PSR] = ps->pr_lwp.pr_reg[R_PSR];
6807c478bdstevel@tonic-gate	cp->user.u_reg[REG_PC]  = ps->pr_lwp.pr_reg[R_PC];
6817c478bdstevel@tonic-gate	cp->user.u_reg[REG_nPC] = ps->pr_lwp.pr_reg[R_nPC];
6827c478bdstevel@tonic-gate	cp->user.u_reg[REG_Y]   = ps->pr_lwp.pr_reg[R_Y];
6837c478bdstevel@tonic-gate	cp->user.u_reg[REG_G1]  = ps->pr_lwp.pr_reg[R_G1];
6847c478bdstevel@tonic-gate	cp->user.u_reg[REG_G2]  = ps->pr_lwp.pr_reg[R_G2];
6857c478bdstevel@tonic-gate	cp->user.u_reg[REG_G3]  = ps->pr_lwp.pr_reg[R_G3];
6867c478bdstevel@tonic-gate	cp->user.u_reg[REG_G4]  = ps->pr_lwp.pr_reg[R_G4];
6877c478bdstevel@tonic-gate	cp->user.u_reg[REG_G5]  = ps->pr_lwp.pr_reg[R_G5];
6887c478bdstevel@tonic-gate	cp->user.u_reg[REG_G6]  = ps->pr_lwp.pr_reg[R_G6];
6897c478bdstevel@tonic-gate	cp->user.u_reg[REG_G7]  = ps->pr_lwp.pr_reg[R_G7];
6907c478bdstevel@tonic-gate	cp->user.u_reg[REG_O0]  = ps->pr_lwp.pr_reg[R_O0];
6917c478bdstevel@tonic-gate	cp->user.u_reg[REG_O1]  = ps->pr_lwp.pr_reg[R_O1];
6927c478bdstevel@tonic-gate	cp->user.u_reg[REG_O2]  = ps->pr_lwp.pr_reg[R_O2];
6937c478bdstevel@tonic-gate	cp->user.u_reg[REG_O3]  = ps->pr_lwp.pr_reg[R_O3];
6947c478bdstevel@tonic-gate	cp->user.u_reg[REG_O4]  = ps->pr_lwp.pr_reg[R_O4];
6957c478bdstevel@tonic-gate	cp->user.u_reg[REG_O5]  = ps->pr_lwp.pr_reg[R_O5];
6967c478bdstevel@tonic-gate	cp->user.u_reg[REG_O6]  = ps->pr_lwp.pr_reg[R_O6];
6977c478bdstevel@tonic-gate	cp->user.u_reg[REG_O7]  = ps->pr_lwp.pr_reg[R_O7];
6987c478bdstevel@tonic-gate	cp->user.u_ar0 = (greg_t *)REGADDR;
6997c478bdstevel@tonic-gate	cp->user.u_code = ps->pr_lwp.pr_info.si_code;
7007c478bdstevel@tonic-gate	cp->user.u_addr = ps->pr_lwp.pr_info.si_addr;
7017c478bdstevel@tonic-gate	cp->flags &= ~(CS_PSARGS|CS_SIGNAL);
7027c478bdstevel@tonic-gate}
7037c478bdstevel@tonic-gate
7047c478bdstevel@tonic-gate/*
7057c478bdstevel@tonic-gate * Fetch the contents of u_psargs[].
7067c478bdstevel@tonic-gate */
7077c478bdstevel@tonic-gatestatic void
7087c478bdstevel@tonic-gateGetPsargs(cstatus_t *cp)
7097c478bdstevel@tonic-gate{
7107c478bdstevel@tonic-gate	char procname[64];	/* /proc/<pid>/psinfo */
7117c478bdstevel@tonic-gate	int fd;
7127c478bdstevel@tonic-gate
7137c478bdstevel@tonic-gate	MakeProcName(procname, cp->pid);
7147c478bdstevel@tonic-gate	(void) strcat(procname, "/psinfo");
7157c478bdstevel@tonic-gate	if ((fd = open(procname, O_RDONLY, 0)) < 0) {
7167c478bdstevel@tonic-gate		(void) memset(cp->user.u_psargs, 0, PSARGSZ);
7177c478bdstevel@tonic-gate		return;
7187c478bdstevel@tonic-gate	}
7197c478bdstevel@tonic-gate	(void) pread(fd, cp->user.u_psargs, PSARGSZ,
7207c478bdstevel@tonic-gate	    (off_t)((psinfo_t *)0)->pr_psargs);
7217c478bdstevel@tonic-gate	(void) close(fd);
7227c478bdstevel@tonic-gate
7237c478bdstevel@tonic-gate	cp->flags |= CS_PSARGS;
7247c478bdstevel@tonic-gate}
7257c478bdstevel@tonic-gate
7267c478bdstevel@tonic-gate/*
7277c478bdstevel@tonic-gate * Fetch the contents of u_signal[].
7287c478bdstevel@tonic-gate */
7297c478bdstevel@tonic-gatestatic void
7307c478bdstevel@tonic-gateGetSignal(cstatus_t *cp)
7317c478bdstevel@tonic-gate{
7327c478bdstevel@tonic-gate	char procname[64];	/* /proc/<pid>/sigact */
7337c478bdstevel@tonic-gate	int fd;
7347c478bdstevel@tonic-gate	struct sigaction action[MAXSIG];
7357c478bdstevel@tonic-gate	int i;
7367c478bdstevel@tonic-gate
7377c478bdstevel@tonic-gate	MakeProcName(procname, cp->pid);
7387c478bdstevel@tonic-gate	(void) strcat(procname, "/sigact");
7397c478bdstevel@tonic-gate	(void) memset((char *)action, 0, sizeof (action));
7407c478bdstevel@tonic-gate	if ((fd = open(procname, O_RDONLY, 0)) >= 0) {
7417c478bdstevel@tonic-gate		(void) read(fd, (char *)action, sizeof (action));
7427c478bdstevel@tonic-gate		(void) close(fd);
7437c478bdstevel@tonic-gate	}
7447c478bdstevel@tonic-gate	for (i = 0; i < MAXSIG; i++)
7457c478bdstevel@tonic-gate		cp->user.u_signal[i] = action[i].sa_handler;
7467c478bdstevel@tonic-gate	cp->flags |= CS_SIGNAL;
7477c478bdstevel@tonic-gate}
748