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