xref: /titanic_41/usr/src/lib/libc/i386/sys/ptrace.c (revision 903a11ebdc8df157c4700150f41f1f262f4a8ae8)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
327c478bd9Sstevel@tonic-gate 
337257d1b4Sraf #pragma weak _ptrace = ptrace
347c478bd9Sstevel@tonic-gate 
357257d1b4Sraf #include "lint.h"
367c478bd9Sstevel@tonic-gate #include <stdio.h>
377c478bd9Sstevel@tonic-gate #include <stdlib.h>
387c478bd9Sstevel@tonic-gate #include <unistd.h>
397c478bd9Sstevel@tonic-gate #include <memory.h>
407c478bd9Sstevel@tonic-gate #include <string.h>
417c478bd9Sstevel@tonic-gate #include <fcntl.h>
427c478bd9Sstevel@tonic-gate #include <errno.h>
437c478bd9Sstevel@tonic-gate #include <sys/types.h>
447c478bd9Sstevel@tonic-gate #include <sys/uio.h>
457c478bd9Sstevel@tonic-gate #include <signal.h>
467c478bd9Sstevel@tonic-gate #include <sys/siginfo.h>
477c478bd9Sstevel@tonic-gate #include <sys/fault.h>
487c478bd9Sstevel@tonic-gate #include <sys/syscall.h>
497c478bd9Sstevel@tonic-gate #include <procfs.h>
507c478bd9Sstevel@tonic-gate #include <sys/psw.h>
517c478bd9Sstevel@tonic-gate #include <sys/user.h>
527c478bd9Sstevel@tonic-gate /*
537c478bd9Sstevel@tonic-gate  * mtlib.h must precede thread.h
547c478bd9Sstevel@tonic-gate  */
557c478bd9Sstevel@tonic-gate #include <mtlib.h>
567c478bd9Sstevel@tonic-gate #include <thread.h>
577c478bd9Sstevel@tonic-gate #include <synch.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 		if (pwrite(cp->asfd, (char *)&data, sizeof (data), (off_t)addr)
2687c478bd9Sstevel@tonic-gate 		    == sizeof (data)) {
2698cd45542Sraf 			(void) mutex_unlock(&pt_lock);
2707c478bd9Sstevel@tonic-gate 			return (data);
2717c478bd9Sstevel@tonic-gate 		}
2727c478bd9Sstevel@tonic-gate 		goto eio;
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 	case 6:		/* PTRACE_POKEUSER */
2757c478bd9Sstevel@tonic-gate 		if (addr & 03)
2767c478bd9Sstevel@tonic-gate 			goto eio;
2777c478bd9Sstevel@tonic-gate 		xaddr = addr;
2787c478bd9Sstevel@tonic-gate 		if (xaddr >= REGADDR && xaddr < REGADDR+sizeof (gregset_t))
2797c478bd9Sstevel@tonic-gate 			xaddr -= REGADDR-U_REG;
2807c478bd9Sstevel@tonic-gate 		if ((int)xaddr >= U_REG && xaddr < U_REG+sizeof (gregset_t)) {
2817c478bd9Sstevel@tonic-gate 			int rx = (xaddr-U_REG)/sizeof (greg_t);
2827c478bd9Sstevel@tonic-gate 			if (rx == EFL)
2837c478bd9Sstevel@tonic-gate 				data = (cp->user.u_reg[EFL] & ~PSL_USERMASK) |
2847c478bd9Sstevel@tonic-gate 				    (data & PSL_USERMASK);
2857c478bd9Sstevel@tonic-gate 			cp->user.u_reg[rx] = data;
2867c478bd9Sstevel@tonic-gate 			cp->flags |= CS_SETREGS;
2878cd45542Sraf 			(void) mutex_unlock(&pt_lock);
2887c478bd9Sstevel@tonic-gate 			return (data);
2897c478bd9Sstevel@tonic-gate 		}
2907c478bd9Sstevel@tonic-gate 		goto eio;
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 	case 7:		/* PTRACE_CONT */
2937c478bd9Sstevel@tonic-gate 	case 9:		/* PTRACE_SINGLESTEP */
2947c478bd9Sstevel@tonic-gate 	{
2957c478bd9Sstevel@tonic-gate 		long runctl[3];
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 		if (cp->flags & CS_SETREGS) {
2987c478bd9Sstevel@tonic-gate 			long cmd;
2997c478bd9Sstevel@tonic-gate 			iovec_t iov[2];
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 			ps->pr_lwp.pr_reg[GS] = cp->user.u_reg[GS];
3027c478bd9Sstevel@tonic-gate 			ps->pr_lwp.pr_reg[FS] = cp->user.u_reg[FS];
3037c478bd9Sstevel@tonic-gate 			ps->pr_lwp.pr_reg[ES] = cp->user.u_reg[ES];
3047c478bd9Sstevel@tonic-gate 			ps->pr_lwp.pr_reg[DS] = cp->user.u_reg[DS];
3057c478bd9Sstevel@tonic-gate 			ps->pr_lwp.pr_reg[EDI] = cp->user.u_reg[EDI];
3067c478bd9Sstevel@tonic-gate 			ps->pr_lwp.pr_reg[ESI] = cp->user.u_reg[ESI];
3077c478bd9Sstevel@tonic-gate 			ps->pr_lwp.pr_reg[EBP] = cp->user.u_reg[EBP];
3087c478bd9Sstevel@tonic-gate 			ps->pr_lwp.pr_reg[ESP] = cp->user.u_reg[ESP];
3097c478bd9Sstevel@tonic-gate 			ps->pr_lwp.pr_reg[EBX] = cp->user.u_reg[EBX];
3107c478bd9Sstevel@tonic-gate 			ps->pr_lwp.pr_reg[EDX] = cp->user.u_reg[EDX];
3117c478bd9Sstevel@tonic-gate 			ps->pr_lwp.pr_reg[ECX] = cp->user.u_reg[ECX];
3127c478bd9Sstevel@tonic-gate 			ps->pr_lwp.pr_reg[EAX] = cp->user.u_reg[EAX];
3137c478bd9Sstevel@tonic-gate 			ps->pr_lwp.pr_reg[TRAPNO] = cp->user.u_reg[TRAPNO];
3147c478bd9Sstevel@tonic-gate 			ps->pr_lwp.pr_reg[ERR] = cp->user.u_reg[ERR];
3157c478bd9Sstevel@tonic-gate 			ps->pr_lwp.pr_reg[EIP] = cp->user.u_reg[EIP];
3167c478bd9Sstevel@tonic-gate 			ps->pr_lwp.pr_reg[CS] = cp->user.u_reg[CS];
3177c478bd9Sstevel@tonic-gate 			ps->pr_lwp.pr_reg[EFL] = cp->user.u_reg[EFL];
3187c478bd9Sstevel@tonic-gate 			ps->pr_lwp.pr_reg[UESP] = cp->user.u_reg[UESP];
3197c478bd9Sstevel@tonic-gate 			ps->pr_lwp.pr_reg[SS] = cp->user.u_reg[SS];
3207c478bd9Sstevel@tonic-gate 			cmd = PCSREG;
3217c478bd9Sstevel@tonic-gate 			iov[0].iov_base = (caddr_t)&cmd;
3227c478bd9Sstevel@tonic-gate 			iov[0].iov_len = sizeof (long);
3237c478bd9Sstevel@tonic-gate 			iov[1].iov_base = (caddr_t)&ps->pr_lwp.pr_reg[0];
3247c478bd9Sstevel@tonic-gate 			iov[1].iov_len = sizeof (ps->pr_lwp.pr_reg);
3257c478bd9Sstevel@tonic-gate 			if (writev(cp->ctlfd, iov, 2) < 0)
3267c478bd9Sstevel@tonic-gate 				goto tryagain;
3277c478bd9Sstevel@tonic-gate 		}
3287c478bd9Sstevel@tonic-gate 		if (addr != 1 &&	/* new virtual address */
3297c478bd9Sstevel@tonic-gate 		    addr != cp->user.u_reg[EIP]) {
3307c478bd9Sstevel@tonic-gate 			runctl[0] = PCSVADDR;
3317c478bd9Sstevel@tonic-gate 			runctl[1] = addr;
3327c478bd9Sstevel@tonic-gate 			if (write(cp->ctlfd, (char *)runctl, 2*sizeof (long))
3337c478bd9Sstevel@tonic-gate 			    != 2*sizeof (long))
3347c478bd9Sstevel@tonic-gate 				goto tryagain;
3357c478bd9Sstevel@tonic-gate 		}
3367c478bd9Sstevel@tonic-gate 		/* make data the current signal */
3377c478bd9Sstevel@tonic-gate 		if (data != 0 && data != ps->pr_lwp.pr_cursig) {
3387c478bd9Sstevel@tonic-gate 			(void) memset((char *)&ctl.arg.siginfo, 0,
3397c478bd9Sstevel@tonic-gate 			    sizeof (siginfo_t));
3407c478bd9Sstevel@tonic-gate 			ctl.arg.siginfo.si_signo = data;
3417c478bd9Sstevel@tonic-gate 			ctl.cmd = PCSSIG;
3427c478bd9Sstevel@tonic-gate 			if (write(cp->ctlfd, (char *)&ctl,
3437c478bd9Sstevel@tonic-gate 			    sizeof (long)+sizeof (siginfo_t))
3447c478bd9Sstevel@tonic-gate 			    != sizeof (long)+sizeof (siginfo_t))
3457c478bd9Sstevel@tonic-gate 				goto tryagain;
3467c478bd9Sstevel@tonic-gate 		}
3477c478bd9Sstevel@tonic-gate 		if (data == 0)
3487c478bd9Sstevel@tonic-gate 			runctl[0] = PCCSIG;
3497c478bd9Sstevel@tonic-gate 		else
3507c478bd9Sstevel@tonic-gate 			runctl[0] = PCNULL;
3517c478bd9Sstevel@tonic-gate 		runctl[1] = PCRUN;
3527c478bd9Sstevel@tonic-gate 		runctl[2] = (request == 9)? PRSTEP : 0;
3537c478bd9Sstevel@tonic-gate 		if (write(cp->ctlfd, (char *)runctl, 3*sizeof (long))
3547c478bd9Sstevel@tonic-gate 		    != 3*sizeof (long)) {
3557c478bd9Sstevel@tonic-gate 			if (errno == ENOENT) {
3567c478bd9Sstevel@tonic-gate 				/* current signal must have killed it */
3577c478bd9Sstevel@tonic-gate 				ReleaseProc(cp);
3588cd45542Sraf 				(void) mutex_unlock(&pt_lock);
3597c478bd9Sstevel@tonic-gate 				return (data);
3607c478bd9Sstevel@tonic-gate 			}
3617c478bd9Sstevel@tonic-gate 			goto tryagain;
3627c478bd9Sstevel@tonic-gate 		}
3637c478bd9Sstevel@tonic-gate 		(void) memset((char *)ps, 0, sizeof (pstatus_t));
3647c478bd9Sstevel@tonic-gate 		cp->flags = 0;
3658cd45542Sraf 		(void) mutex_unlock(&pt_lock);
3667c478bd9Sstevel@tonic-gate 		return (data);
3677c478bd9Sstevel@tonic-gate 	}
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 	case 8:		/* PTRACE_KILL */
3707c478bd9Sstevel@tonic-gate 		/* overkill? */
3717c478bd9Sstevel@tonic-gate 		(void) memset((char *)&ctl.arg.siginfo, 0, sizeof (siginfo_t));
3727c478bd9Sstevel@tonic-gate 		ctl.arg.siginfo.si_signo = SIGKILL;
3737c478bd9Sstevel@tonic-gate 		ctl.cmd = PCSSIG;
3747c478bd9Sstevel@tonic-gate 		(void) write(cp->ctlfd, (char *)&ctl,
3757c478bd9Sstevel@tonic-gate 		    sizeof (long)+sizeof (siginfo_t));
3767c478bd9Sstevel@tonic-gate 		(void) kill(pid, SIGKILL);
3777c478bd9Sstevel@tonic-gate 		ReleaseProc(cp);
3788cd45542Sraf 		(void) mutex_unlock(&pt_lock);
3797c478bd9Sstevel@tonic-gate 		return (0);
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 	default:
3827c478bd9Sstevel@tonic-gate 		goto eio;
3837c478bd9Sstevel@tonic-gate 	}
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate tryagain:
3867c478bd9Sstevel@tonic-gate 	if (errno == EAGAIN) {
3877c478bd9Sstevel@tonic-gate 		if (OpenProc(cp) == 0)
3887c478bd9Sstevel@tonic-gate 			goto again;
3897c478bd9Sstevel@tonic-gate 		ReleaseProc(cp);
3907c478bd9Sstevel@tonic-gate 	}
3917c478bd9Sstevel@tonic-gate eio:
3927c478bd9Sstevel@tonic-gate 	errno = EIO;
3938cd45542Sraf 	(void) mutex_unlock(&pt_lock);
3947c478bd9Sstevel@tonic-gate 	return (-1);
3957c478bd9Sstevel@tonic-gate esrch:
3967c478bd9Sstevel@tonic-gate 	errno = ESRCH;
3978cd45542Sraf 	(void) mutex_unlock(&pt_lock);
3987c478bd9Sstevel@tonic-gate 	return (-1);
3997c478bd9Sstevel@tonic-gate }
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate /*
4027c478bd9Sstevel@tonic-gate  * Find the cstatus structure corresponding to pid.
4037c478bd9Sstevel@tonic-gate  */
4047c478bd9Sstevel@tonic-gate static cstatus_t *
FindProc(pid_t pid)4057c478bd9Sstevel@tonic-gate FindProc(pid_t pid)
4067c478bd9Sstevel@tonic-gate {
4077c478bd9Sstevel@tonic-gate 	cstatus_t *cp;
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 	for (cp = childp; cp != NULLCP; cp = cp->next)
4107c478bd9Sstevel@tonic-gate 		if (cp->pid == pid)
4117c478bd9Sstevel@tonic-gate 			break;
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 	return (cp);
4147c478bd9Sstevel@tonic-gate }
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate /*
4177c478bd9Sstevel@tonic-gate  * Check every proc for existence, release those that are gone.
4187c478bd9Sstevel@tonic-gate  * Be careful about the linked list; ReleaseProc() changes it.
4197c478bd9Sstevel@tonic-gate  */
4207c478bd9Sstevel@tonic-gate static void
CheckAllProcs()4217c478bd9Sstevel@tonic-gate CheckAllProcs()
4227c478bd9Sstevel@tonic-gate {
4237c478bd9Sstevel@tonic-gate 	cstatus_t *cp = childp;
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 	while (cp != NULLCP) {
4267c478bd9Sstevel@tonic-gate 		cstatus_t *next = cp->next;
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 		if (ProcUpdate(cp) != 0)
4297c478bd9Sstevel@tonic-gate 			ReleaseProc(cp);
4307c478bd9Sstevel@tonic-gate 		cp = next;
4317c478bd9Sstevel@tonic-gate 	}
4327c478bd9Sstevel@tonic-gate }
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate /*
4357c478bd9Sstevel@tonic-gate  * Utility for OpenProc().
4367c478bd9Sstevel@tonic-gate  */
4377c478bd9Sstevel@tonic-gate static int
Dupfd(int fd,int dfd)4387c478bd9Sstevel@tonic-gate Dupfd(int fd, int dfd)
4397c478bd9Sstevel@tonic-gate {
4407c478bd9Sstevel@tonic-gate 	/*
4417c478bd9Sstevel@tonic-gate 	 * Make sure fd not one of 0, 1, or 2 to avoid stdio interference.
4427c478bd9Sstevel@tonic-gate 	 * Also, if dfd is greater than 2, dup fd to be exactly dfd.
4437c478bd9Sstevel@tonic-gate 	 */
4447c478bd9Sstevel@tonic-gate 	if (dfd > 2 || (0 <= fd && fd <= 2)) {
4457c478bd9Sstevel@tonic-gate 		if (dfd > 2 && fd != dfd)
4467c478bd9Sstevel@tonic-gate 			(void) close(dfd);
4477c478bd9Sstevel@tonic-gate 		else
4487c478bd9Sstevel@tonic-gate 			dfd = 3;
4497c478bd9Sstevel@tonic-gate 		if (fd != dfd) {
4507c478bd9Sstevel@tonic-gate 			dfd = fcntl(fd, F_DUPFD, (intptr_t)dfd);
4517c478bd9Sstevel@tonic-gate 			(void) close(fd);
4527c478bd9Sstevel@tonic-gate 			fd = dfd;
4537c478bd9Sstevel@tonic-gate 		}
4547c478bd9Sstevel@tonic-gate 	}
4557c478bd9Sstevel@tonic-gate 	/*
4567c478bd9Sstevel@tonic-gate 	 * Mark filedescriptor close-on-exec.
4577c478bd9Sstevel@tonic-gate 	 * Should also be close-on-return-from-fork-in-child.
4587c478bd9Sstevel@tonic-gate 	 */
4597c478bd9Sstevel@tonic-gate 	(void) fcntl(fd, F_SETFD, (intptr_t)1);
4607c478bd9Sstevel@tonic-gate 	return (fd);
4617c478bd9Sstevel@tonic-gate }
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate /*
4647c478bd9Sstevel@tonic-gate  * Construct the /proc directory name:  "/proc/<pid>"
4657c478bd9Sstevel@tonic-gate  * The name buffer passed by the caller must be large enough.
4667c478bd9Sstevel@tonic-gate  */
4677c478bd9Sstevel@tonic-gate static void
MakeProcName(char * procname,pid_t pid)4687c478bd9Sstevel@tonic-gate MakeProcName(char *procname, pid_t pid)
4697c478bd9Sstevel@tonic-gate {
470*903a11ebSrh87107 	(void) sprintf(procname, "/proc/%ld", pid);
4717c478bd9Sstevel@tonic-gate }
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate /*
4747c478bd9Sstevel@tonic-gate  * Open/reopen the /proc/<pid> files.
4757c478bd9Sstevel@tonic-gate  */
4767c478bd9Sstevel@tonic-gate static int
OpenProc(cstatus_t * cp)4777c478bd9Sstevel@tonic-gate OpenProc(cstatus_t *cp)
4787c478bd9Sstevel@tonic-gate {
4797c478bd9Sstevel@tonic-gate 	char procname[64];		/* /proc/nnnnn/fname */
4807c478bd9Sstevel@tonic-gate 	char *fname;
4817c478bd9Sstevel@tonic-gate 	int fd;
4827c478bd9Sstevel@tonic-gate 	int omode;
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 	MakeProcName(procname, cp->pid);
4857c478bd9Sstevel@tonic-gate 	fname = procname + strlen(procname);
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 	/*
4887c478bd9Sstevel@tonic-gate 	 * Use exclusive-open only if this is the first open.
4897c478bd9Sstevel@tonic-gate 	 */
4907c478bd9Sstevel@tonic-gate 	omode = (cp->asfd > 0)? O_RDWR : (O_RDWR|O_EXCL);
4917c478bd9Sstevel@tonic-gate 	(void) strcpy(fname, "/as");
4927c478bd9Sstevel@tonic-gate 	if ((fd = open(procname, omode, 0)) < 0 ||
4937c478bd9Sstevel@tonic-gate 	    (cp->asfd = Dupfd(fd, cp->asfd)) < 0)
4947c478bd9Sstevel@tonic-gate 		goto err;
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate 	(void) strcpy(fname, "/ctl");
4977c478bd9Sstevel@tonic-gate 	if ((fd = open(procname, O_WRONLY, 0)) < 0 ||
4987c478bd9Sstevel@tonic-gate 	    (cp->ctlfd = Dupfd(fd, cp->ctlfd)) < 0)
4997c478bd9Sstevel@tonic-gate 		goto err;
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate 	(void) strcpy(fname, "/status");
5027c478bd9Sstevel@tonic-gate 	if ((fd = open(procname, O_RDONLY, 0)) < 0 ||
5037c478bd9Sstevel@tonic-gate 	    (cp->statusfd = Dupfd(fd, cp->statusfd)) < 0)
5047c478bd9Sstevel@tonic-gate 		goto err;
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 	return (0);
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate err:
5097c478bd9Sstevel@tonic-gate 	CloseProc(cp);
5107c478bd9Sstevel@tonic-gate 	return (-1);
5117c478bd9Sstevel@tonic-gate }
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate /*
5147c478bd9Sstevel@tonic-gate  * Close the /proc/<pid> files.
5157c478bd9Sstevel@tonic-gate  */
5167c478bd9Sstevel@tonic-gate static void
CloseProc(cstatus_t * cp)5177c478bd9Sstevel@tonic-gate CloseProc(cstatus_t *cp)
5187c478bd9Sstevel@tonic-gate {
5197c478bd9Sstevel@tonic-gate 	if (cp->asfd > 0)
5207c478bd9Sstevel@tonic-gate 		(void) close(cp->asfd);
5217c478bd9Sstevel@tonic-gate 	if (cp->ctlfd > 0)
5227c478bd9Sstevel@tonic-gate 		(void) close(cp->ctlfd);
5237c478bd9Sstevel@tonic-gate 	if (cp->statusfd > 0)
5247c478bd9Sstevel@tonic-gate 		(void) close(cp->statusfd);
5257c478bd9Sstevel@tonic-gate 	cp->asfd = 0;
5267c478bd9Sstevel@tonic-gate 	cp->ctlfd = 0;
5277c478bd9Sstevel@tonic-gate 	cp->statusfd = 0;
5287c478bd9Sstevel@tonic-gate }
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate /*
5317c478bd9Sstevel@tonic-gate  * Take control of a child process.
5327c478bd9Sstevel@tonic-gate  */
5337c478bd9Sstevel@tonic-gate static cstatus_t *
GrabProc(pid_t pid)5347c478bd9Sstevel@tonic-gate GrabProc(pid_t pid)
5357c478bd9Sstevel@tonic-gate {
5367c478bd9Sstevel@tonic-gate 	cstatus_t *cp;
5377c478bd9Sstevel@tonic-gate 	long ctl[2];
5387c478bd9Sstevel@tonic-gate 	pid_t ppid;
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate 	if (pid <= 0)
5417c478bd9Sstevel@tonic-gate 		return (NULLCP);
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate 	if ((cp = FindProc(pid)) != NULLCP)	/* already grabbed */
5447c478bd9Sstevel@tonic-gate 		return (cp);
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 	CheckAllProcs();	/* clean up before grabbing new process */
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 	cp = (cstatus_t *)malloc(sizeof (cstatus_t));
5497c478bd9Sstevel@tonic-gate 	if (cp == NULLCP)
5507c478bd9Sstevel@tonic-gate 		return (NULLCP);
5517c478bd9Sstevel@tonic-gate 	(void) memset((char *)cp, 0, sizeof (cstatus_t));
5527c478bd9Sstevel@tonic-gate 	cp->pid = pid;
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 	ppid = getpid();
5557c478bd9Sstevel@tonic-gate 	while (OpenProc(cp) == 0) {
5567c478bd9Sstevel@tonic-gate 		ctl[0] = PCSET;
5577c478bd9Sstevel@tonic-gate 		ctl[1] = PR_RLC;
5587c478bd9Sstevel@tonic-gate 		errno = 0;
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate 		if (pread(cp->statusfd, (char *)&cp->pstatus,
5617c478bd9Sstevel@tonic-gate 		    sizeof (cp->pstatus), (off_t)0) == sizeof (cp->pstatus) &&
5627c478bd9Sstevel@tonic-gate 		    cp->pstatus.pr_ppid == ppid &&
5637c478bd9Sstevel@tonic-gate 		    (cp->pstatus.pr_flags & PR_PTRACE) &&
5647c478bd9Sstevel@tonic-gate 		    write(cp->ctlfd, (char *)ctl, 2*sizeof (long))
5657c478bd9Sstevel@tonic-gate 		    == 2*sizeof (long)) {
5667c478bd9Sstevel@tonic-gate 			cp->next = childp;
5677c478bd9Sstevel@tonic-gate 			childp = cp;
5687c478bd9Sstevel@tonic-gate 			MakeUser(cp);
5697c478bd9Sstevel@tonic-gate 			return (cp);
5707c478bd9Sstevel@tonic-gate 		}
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate 		if (errno != EAGAIN)
5737c478bd9Sstevel@tonic-gate 			break;
5747c478bd9Sstevel@tonic-gate 	}
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate 	free((char *)cp);
5777c478bd9Sstevel@tonic-gate 	return (NULLCP);
5787c478bd9Sstevel@tonic-gate }
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate /*
5817c478bd9Sstevel@tonic-gate  * Close the /proc/<pid> file, if open.
5827c478bd9Sstevel@tonic-gate  * Deallocate the memory used by the cstatus_t structure.
5837c478bd9Sstevel@tonic-gate  */
5847c478bd9Sstevel@tonic-gate static void
ReleaseProc(cstatus_t * cp)5857c478bd9Sstevel@tonic-gate ReleaseProc(cstatus_t *cp)
5867c478bd9Sstevel@tonic-gate {
5877c478bd9Sstevel@tonic-gate 	CloseProc(cp);
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 	if (childp == cp)
5907c478bd9Sstevel@tonic-gate 		childp = cp->next;
5917c478bd9Sstevel@tonic-gate 	else {
5927c478bd9Sstevel@tonic-gate 		cstatus_t *pcp;
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 		for (pcp = childp; pcp != NULLCP; pcp = pcp->next) {
5957c478bd9Sstevel@tonic-gate 			if (pcp->next == cp) {
5967c478bd9Sstevel@tonic-gate 				pcp->next = cp->next;
5977c478bd9Sstevel@tonic-gate 				break;
5987c478bd9Sstevel@tonic-gate 			}
5997c478bd9Sstevel@tonic-gate 		}
6007c478bd9Sstevel@tonic-gate 	}
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate 	free((char *)cp);
6037c478bd9Sstevel@tonic-gate }
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate /*
6067c478bd9Sstevel@tonic-gate  * Update process information from /proc.
6077c478bd9Sstevel@tonic-gate  * Return 0 on success, -1 on failure.
6087c478bd9Sstevel@tonic-gate  */
6097c478bd9Sstevel@tonic-gate static int
ProcUpdate(cstatus_t * cp)6107c478bd9Sstevel@tonic-gate ProcUpdate(cstatus_t *cp)
6117c478bd9Sstevel@tonic-gate {
6127c478bd9Sstevel@tonic-gate 	pstatus_t *ps = &cp->pstatus;
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 	if (cp->flags & CS_SETREGS) {
6157c478bd9Sstevel@tonic-gate 		long cmd;
6167c478bd9Sstevel@tonic-gate 		iovec_t iov[2];
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate 		ps->pr_lwp.pr_reg[GS]   = cp->user.u_reg[GS];
6197c478bd9Sstevel@tonic-gate 		ps->pr_lwp.pr_reg[FS]   = cp->user.u_reg[FS];
6207c478bd9Sstevel@tonic-gate 		ps->pr_lwp.pr_reg[ES]   = cp->user.u_reg[ES];
6217c478bd9Sstevel@tonic-gate 		ps->pr_lwp.pr_reg[DS]   = cp->user.u_reg[DS];
6227c478bd9Sstevel@tonic-gate 		ps->pr_lwp.pr_reg[EDI]  = cp->user.u_reg[EDI];
6237c478bd9Sstevel@tonic-gate 		ps->pr_lwp.pr_reg[ESI]  = cp->user.u_reg[ESI];
6247c478bd9Sstevel@tonic-gate 		ps->pr_lwp.pr_reg[EBP]  = cp->user.u_reg[EBP];
6257c478bd9Sstevel@tonic-gate 		ps->pr_lwp.pr_reg[ESP]  = cp->user.u_reg[ESP];
6267c478bd9Sstevel@tonic-gate 		ps->pr_lwp.pr_reg[EBX]  = cp->user.u_reg[EBX];
6277c478bd9Sstevel@tonic-gate 		ps->pr_lwp.pr_reg[EDX]  = cp->user.u_reg[EDX];
6287c478bd9Sstevel@tonic-gate 		ps->pr_lwp.pr_reg[ECX]  = cp->user.u_reg[ECX];
6297c478bd9Sstevel@tonic-gate 		ps->pr_lwp.pr_reg[EAX]  = cp->user.u_reg[EAX];
6307c478bd9Sstevel@tonic-gate 		ps->pr_lwp.pr_reg[TRAPNO] = cp->user.u_reg[TRAPNO];
6317c478bd9Sstevel@tonic-gate 		ps->pr_lwp.pr_reg[ERR]  = cp->user.u_reg[ERR];
6327c478bd9Sstevel@tonic-gate 		ps->pr_lwp.pr_reg[EIP]  = cp->user.u_reg[EIP];
6337c478bd9Sstevel@tonic-gate 		ps->pr_lwp.pr_reg[CS]   = cp->user.u_reg[CS];
6347c478bd9Sstevel@tonic-gate 		ps->pr_lwp.pr_reg[EFL]  = cp->user.u_reg[EFL];
6357c478bd9Sstevel@tonic-gate 		ps->pr_lwp.pr_reg[UESP] = cp->user.u_reg[UESP];
6367c478bd9Sstevel@tonic-gate 		ps->pr_lwp.pr_reg[SS]   = cp->user.u_reg[SS];
6377c478bd9Sstevel@tonic-gate 		cmd = PCSREG;
6387c478bd9Sstevel@tonic-gate 		iov[0].iov_base = (caddr_t)&cmd;
6397c478bd9Sstevel@tonic-gate 		iov[0].iov_len = sizeof (long);
6407c478bd9Sstevel@tonic-gate 		iov[1].iov_base = (caddr_t)&ps->pr_lwp.pr_reg[0];
6417c478bd9Sstevel@tonic-gate 		iov[1].iov_len = sizeof (ps->pr_lwp.pr_reg);
6427c478bd9Sstevel@tonic-gate 		(void) writev(cp->ctlfd, iov, 2);
6437c478bd9Sstevel@tonic-gate 		cp->flags &= ~CS_SETREGS;
6447c478bd9Sstevel@tonic-gate 	}
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate 	while (pread(cp->statusfd, (char *)ps, sizeof (*ps), (off_t)0) < 0) {
6477c478bd9Sstevel@tonic-gate 		/* attempt to regain control */
6487c478bd9Sstevel@tonic-gate 		if (errno != EINTR &&
6497c478bd9Sstevel@tonic-gate 		    !(errno == EAGAIN && OpenProc(cp) == 0))
6507c478bd9Sstevel@tonic-gate 			return (-1);
6517c478bd9Sstevel@tonic-gate 	}
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate 	if (ps->pr_flags & PR_ISTOP)
6547c478bd9Sstevel@tonic-gate 		MakeUser(cp);
6557c478bd9Sstevel@tonic-gate 	else
6567c478bd9Sstevel@tonic-gate 		(void) memset((char *)ps, 0, sizeof (pstatus_t));
6577c478bd9Sstevel@tonic-gate 
6587c478bd9Sstevel@tonic-gate 	return (0);
6597c478bd9Sstevel@tonic-gate }
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate /*
6627c478bd9Sstevel@tonic-gate  * Manufacture the contents of the fake u-block.
6637c478bd9Sstevel@tonic-gate  */
6647c478bd9Sstevel@tonic-gate static void
MakeUser(cstatus_t * cp)6657c478bd9Sstevel@tonic-gate MakeUser(cstatus_t *cp)
6667c478bd9Sstevel@tonic-gate {
6677c478bd9Sstevel@tonic-gate 	pstatus_t *ps = &cp->pstatus;
6687c478bd9Sstevel@tonic-gate 
6697c478bd9Sstevel@tonic-gate 	cp->user.u_reg[GS]   = ps->pr_lwp.pr_reg[GS];
6707c478bd9Sstevel@tonic-gate 	cp->user.u_reg[FS]   = ps->pr_lwp.pr_reg[FS];
6717c478bd9Sstevel@tonic-gate 	cp->user.u_reg[ES]   = ps->pr_lwp.pr_reg[ES];
6727c478bd9Sstevel@tonic-gate 	cp->user.u_reg[DS]   = ps->pr_lwp.pr_reg[DS];
6737c478bd9Sstevel@tonic-gate 	cp->user.u_reg[EDI]  = ps->pr_lwp.pr_reg[EDI];
6747c478bd9Sstevel@tonic-gate 	cp->user.u_reg[ESI]  = ps->pr_lwp.pr_reg[ESI];
6757c478bd9Sstevel@tonic-gate 	cp->user.u_reg[EBP]  = ps->pr_lwp.pr_reg[EBP];
6767c478bd9Sstevel@tonic-gate 	cp->user.u_reg[ESP]  = ps->pr_lwp.pr_reg[ESP];
6777c478bd9Sstevel@tonic-gate 	cp->user.u_reg[EBX]  = ps->pr_lwp.pr_reg[EBX];
6787c478bd9Sstevel@tonic-gate 	cp->user.u_reg[EDX]  = ps->pr_lwp.pr_reg[EDX];
6797c478bd9Sstevel@tonic-gate 	cp->user.u_reg[ECX]  = ps->pr_lwp.pr_reg[ECX];
6807c478bd9Sstevel@tonic-gate 	cp->user.u_reg[EAX]  = ps->pr_lwp.pr_reg[EAX];
6817c478bd9Sstevel@tonic-gate 	cp->user.u_reg[TRAPNO] = ps->pr_lwp.pr_reg[TRAPNO];
6827c478bd9Sstevel@tonic-gate 	cp->user.u_reg[ERR]  = ps->pr_lwp.pr_reg[ERR];
6837c478bd9Sstevel@tonic-gate 	cp->user.u_reg[EIP]  = ps->pr_lwp.pr_reg[EIP];
6847c478bd9Sstevel@tonic-gate 	cp->user.u_reg[CS]   = ps->pr_lwp.pr_reg[CS];
6857c478bd9Sstevel@tonic-gate 	cp->user.u_reg[EFL]  = ps->pr_lwp.pr_reg[EFL];
6867c478bd9Sstevel@tonic-gate 	cp->user.u_reg[UESP] = ps->pr_lwp.pr_reg[UESP];
6877c478bd9Sstevel@tonic-gate 	cp->user.u_reg[SS]   = ps->pr_lwp.pr_reg[SS];
6887c478bd9Sstevel@tonic-gate 	cp->user.u_ar0 = (greg_t *)REGADDR;
6897c478bd9Sstevel@tonic-gate 	cp->user.u_code = ps->pr_lwp.pr_info.si_code;
6907c478bd9Sstevel@tonic-gate 	cp->user.u_addr = ps->pr_lwp.pr_info.si_addr;
6917c478bd9Sstevel@tonic-gate 	cp->flags &= ~(CS_PSARGS|CS_SIGNAL);
6927c478bd9Sstevel@tonic-gate }
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate /*
6957c478bd9Sstevel@tonic-gate  * Fetch the contents of u_psargs[].
6967c478bd9Sstevel@tonic-gate  */
6977c478bd9Sstevel@tonic-gate static void
GetPsargs(cstatus_t * cp)6987c478bd9Sstevel@tonic-gate GetPsargs(cstatus_t *cp)
6997c478bd9Sstevel@tonic-gate {
7007c478bd9Sstevel@tonic-gate 	char procname[64];	/* /proc/<pid>/psinfo */
7017c478bd9Sstevel@tonic-gate 	int fd;
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate 	MakeProcName(procname, cp->pid);
7047c478bd9Sstevel@tonic-gate 	(void) strcat(procname, "/psinfo");
7057c478bd9Sstevel@tonic-gate 	if ((fd = open(procname, O_RDONLY, 0)) < 0) {
7067c478bd9Sstevel@tonic-gate 		(void) memset(cp->user.u_psargs, 0, PSARGSZ);
7077c478bd9Sstevel@tonic-gate 		return;
7087c478bd9Sstevel@tonic-gate 	}
7097c478bd9Sstevel@tonic-gate 	(void) pread(fd, cp->user.u_psargs, PSARGSZ,
7107c478bd9Sstevel@tonic-gate 	    (off_t)((psinfo_t *)0)->pr_psargs);
7117c478bd9Sstevel@tonic-gate 	(void) close(fd);
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate 	cp->flags |= CS_PSARGS;
7147c478bd9Sstevel@tonic-gate }
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate /*
7177c478bd9Sstevel@tonic-gate  * Fetch the contents of u_signal[].
7187c478bd9Sstevel@tonic-gate  */
7197c478bd9Sstevel@tonic-gate static void
GetSignal(cstatus_t * cp)7207c478bd9Sstevel@tonic-gate GetSignal(cstatus_t *cp)
7217c478bd9Sstevel@tonic-gate {
7227c478bd9Sstevel@tonic-gate 	char procname[64];	/* /proc/<pid>/sigact */
7237c478bd9Sstevel@tonic-gate 	int fd;
7247c478bd9Sstevel@tonic-gate 	struct sigaction action[MAXSIG];
7257c478bd9Sstevel@tonic-gate 	int i;
7267c478bd9Sstevel@tonic-gate 
7277c478bd9Sstevel@tonic-gate 	MakeProcName(procname, cp->pid);
7287c478bd9Sstevel@tonic-gate 	(void) strcat(procname, "/sigact");
7297c478bd9Sstevel@tonic-gate 	(void) memset((char *)action, 0, sizeof (action));
7307c478bd9Sstevel@tonic-gate 	if ((fd = open(procname, O_RDONLY, 0)) >= 0) {
7317c478bd9Sstevel@tonic-gate 		(void) read(fd, (char *)action, sizeof (action));
7327c478bd9Sstevel@tonic-gate 		(void) close(fd);
7337c478bd9Sstevel@tonic-gate 	}
7347c478bd9Sstevel@tonic-gate 	for (i = 0; i < MAXSIG; i++)
7357c478bd9Sstevel@tonic-gate 		cp->user.u_signal[i] = action[i].sa_handler;
7367c478bd9Sstevel@tonic-gate 	cp->flags |= CS_SIGNAL;
7377c478bd9Sstevel@tonic-gate }
738