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