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
5*1914Scasper  * Common Development and Distribution License (the "License").
6*1914Scasper  * 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  */
210Sstevel@tonic-gate /*
22*1914Scasper  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <stdio.h>
29*1914Scasper #include <stdio_ext.h>
300Sstevel@tonic-gate #include <stdlib.h>
310Sstevel@tonic-gate #include <unistd.h>
320Sstevel@tonic-gate #include <ctype.h>
330Sstevel@tonic-gate #include <fcntl.h>
340Sstevel@tonic-gate #include <strings.h>
350Sstevel@tonic-gate #include <dirent.h>
360Sstevel@tonic-gate #include <errno.h>
370Sstevel@tonic-gate #include <sys/types.h>
380Sstevel@tonic-gate #include <sys/int_fmtio.h>
390Sstevel@tonic-gate #include <libproc.h>
400Sstevel@tonic-gate 
410Sstevel@tonic-gate typedef struct look_arg {
420Sstevel@tonic-gate 	int pflags;
430Sstevel@tonic-gate 	const char *lwps;
440Sstevel@tonic-gate 	int count;
450Sstevel@tonic-gate } look_arg_t;
460Sstevel@tonic-gate 
470Sstevel@tonic-gate static	int	look(char *);
480Sstevel@tonic-gate static	int	lwplook(look_arg_t *, const lwpstatus_t *, const lwpsinfo_t *);
490Sstevel@tonic-gate static	char	*prflags(int);
500Sstevel@tonic-gate static	char	*prwhy(int);
510Sstevel@tonic-gate static	char	*prwhat(int, int);
520Sstevel@tonic-gate static	void	dumpregs(const prgregset_t, int);
530Sstevel@tonic-gate #if defined(__sparc) && defined(_ILP32)
540Sstevel@tonic-gate static	void	dumpregs_v8p(const prgregset_t, const prxregset_t *, int);
550Sstevel@tonic-gate #endif
560Sstevel@tonic-gate 
570Sstevel@tonic-gate static	char	*command;
580Sstevel@tonic-gate static	struct	ps_prochandle *Pr;
590Sstevel@tonic-gate 
600Sstevel@tonic-gate static	int	is64;	/* Is current process 64-bit? */
610Sstevel@tonic-gate static	int	rflag;	/* Show registers? */
620Sstevel@tonic-gate 
630Sstevel@tonic-gate #define	LWPFLAGS	\
640Sstevel@tonic-gate 	(PR_STOPPED|PR_ISTOP|PR_DSTOP|PR_ASLEEP|PR_PCINVAL|PR_STEP \
650Sstevel@tonic-gate 	|PR_ASLWP|PR_AGENT|PR_DETACH|PR_DAEMON)
660Sstevel@tonic-gate 
670Sstevel@tonic-gate #define	PROCFLAGS	\
680Sstevel@tonic-gate 	(PR_ISSYS|PR_VFORKP|PR_ORPHAN|PR_FORK|PR_RLC|PR_KLC|PR_ASYNC \
690Sstevel@tonic-gate 	|PR_BPTADJ|PR_MSACCT|PR_MSFORK|PR_PTRACE)
700Sstevel@tonic-gate 
710Sstevel@tonic-gate #define	ALLFLAGS	(LWPFLAGS|PROCFLAGS)
720Sstevel@tonic-gate 
730Sstevel@tonic-gate int
740Sstevel@tonic-gate main(int argc, char **argv)
750Sstevel@tonic-gate {
760Sstevel@tonic-gate 	int rc = 0;
770Sstevel@tonic-gate 	int errflg = 0;
780Sstevel@tonic-gate 	int opt;
790Sstevel@tonic-gate 	struct rlimit rlim;
800Sstevel@tonic-gate 
810Sstevel@tonic-gate 	if ((command = strrchr(argv[0], '/')) != NULL)
820Sstevel@tonic-gate 		command++;
830Sstevel@tonic-gate 	else
840Sstevel@tonic-gate 		command = argv[0];
850Sstevel@tonic-gate 
860Sstevel@tonic-gate 	/* options */
870Sstevel@tonic-gate 	while ((opt = getopt(argc, argv, "r")) != EOF) {
880Sstevel@tonic-gate 		switch (opt) {
890Sstevel@tonic-gate 		case 'r':		/* show registers */
900Sstevel@tonic-gate 			rflag = 1;
910Sstevel@tonic-gate 			break;
920Sstevel@tonic-gate 		default:
930Sstevel@tonic-gate 			errflg = 1;
940Sstevel@tonic-gate 			break;
950Sstevel@tonic-gate 		}
960Sstevel@tonic-gate 	}
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	argc -= optind;
990Sstevel@tonic-gate 	argv += optind;
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 	if (errflg || argc <= 0) {
1020Sstevel@tonic-gate 		(void) fprintf(stderr,
1030Sstevel@tonic-gate 		    "usage:\t%s [-r] { pid | core }[/lwps] ...\n", command);
1040Sstevel@tonic-gate 		(void) fprintf(stderr, "  (report process status flags)\n");
1050Sstevel@tonic-gate 		(void) fprintf(stderr, "  -r : report registers\n");
1060Sstevel@tonic-gate 		return (2);
1070Sstevel@tonic-gate 	}
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 	/*
1100Sstevel@tonic-gate 	 * Make sure we'll have enough file descriptors to handle a target
1110Sstevel@tonic-gate 	 * that has many many mappings.
1120Sstevel@tonic-gate 	 */
1130Sstevel@tonic-gate 	if (getrlimit(RLIMIT_NOFILE, &rlim) == 0) {
1140Sstevel@tonic-gate 		rlim.rlim_cur = rlim.rlim_max;
1150Sstevel@tonic-gate 		(void) setrlimit(RLIMIT_NOFILE, &rlim);
116*1914Scasper 		(void) enable_extended_FILE_stdio(-1, -1);
1170Sstevel@tonic-gate 	}
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate 	while (argc-- > 0)
1200Sstevel@tonic-gate 		rc += look(*argv++);
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate 	return (rc);
1230Sstevel@tonic-gate }
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate static int
1260Sstevel@tonic-gate look(char *arg)
1270Sstevel@tonic-gate {
1280Sstevel@tonic-gate 	int gcode;
1290Sstevel@tonic-gate 	int gcode2;
1300Sstevel@tonic-gate 	pstatus_t pstatus;
1310Sstevel@tonic-gate 	psinfo_t psinfo;
1320Sstevel@tonic-gate 	int flags;
1330Sstevel@tonic-gate 	sigset_t sigmask;
1340Sstevel@tonic-gate 	fltset_t fltmask;
1350Sstevel@tonic-gate 	sysset_t entryset;
1360Sstevel@tonic-gate 	sysset_t exitset;
1370Sstevel@tonic-gate 	uint32_t sigtrace, sigtrace2, fltbits;
1380Sstevel@tonic-gate 	uint32_t sigpend, sigpend2;
1390Sstevel@tonic-gate 	uint32_t *bits;
1400Sstevel@tonic-gate 	char buf[PRSIGBUFSZ];
1410Sstevel@tonic-gate 	look_arg_t lookarg;
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate 	if ((Pr = proc_arg_xgrab(arg, NULL, PR_ARG_ANY,
1440Sstevel@tonic-gate 	    PGRAB_RETAIN | PGRAB_FORCE | PGRAB_RDONLY | PGRAB_NOSTOP, &gcode,
1450Sstevel@tonic-gate 	    &lookarg.lwps)) == NULL) {
1460Sstevel@tonic-gate 		if (gcode == G_NOPROC &&
1470Sstevel@tonic-gate 		    proc_arg_psinfo(arg, PR_ARG_PIDS, &psinfo, &gcode2) > 0 &&
1480Sstevel@tonic-gate 		    psinfo.pr_nlwp == 0) {
1490Sstevel@tonic-gate 			(void) printf("%d:\t<defunct>\n\n", (int)psinfo.pr_pid);
1500Sstevel@tonic-gate 			return (0);
1510Sstevel@tonic-gate 		}
1520Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: cannot examine %s: %s\n",
1530Sstevel@tonic-gate 			command, arg, Pgrab_error(gcode));
1540Sstevel@tonic-gate 		return (1);
1550Sstevel@tonic-gate 	}
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate 	(void) memcpy(&pstatus, Pstatus(Pr), sizeof (pstatus_t));
1580Sstevel@tonic-gate 	(void) memcpy(&psinfo, Ppsinfo(Pr), sizeof (psinfo_t));
1590Sstevel@tonic-gate 	proc_unctrl_psinfo(&psinfo);
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 	if (psinfo.pr_nlwp == 0) {
1620Sstevel@tonic-gate 		(void) printf("%d:\t<defunct>\n\n", (int)psinfo.pr_pid);
1630Sstevel@tonic-gate 		Prelease(Pr, PRELEASE_RETAIN);
1640Sstevel@tonic-gate 		return (0);
1650Sstevel@tonic-gate 	}
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	is64 = (pstatus.pr_dmodel == PR_MODEL_LP64);
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 	sigmask = pstatus.pr_sigtrace;
1700Sstevel@tonic-gate 	fltmask = pstatus.pr_flttrace;
1710Sstevel@tonic-gate 	entryset = pstatus.pr_sysentry;
1720Sstevel@tonic-gate 	exitset = pstatus.pr_sysexit;
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate 	if (Pstate(Pr) == PS_DEAD) {
1750Sstevel@tonic-gate 		(void) printf("core '%s' of %d:\t%.70s\n",
1760Sstevel@tonic-gate 		    arg, (int)psinfo.pr_pid, psinfo.pr_psargs);
1770Sstevel@tonic-gate 	} else {
1780Sstevel@tonic-gate 		(void) printf("%d:\t%.70s\n",
1790Sstevel@tonic-gate 		    (int)psinfo.pr_pid, psinfo.pr_psargs);
1800Sstevel@tonic-gate 	}
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 	(void) printf("\tdata model = %s", is64? "_LP64" : "_ILP32");
1830Sstevel@tonic-gate 	if ((flags = (pstatus.pr_flags & PROCFLAGS)) != 0)
1840Sstevel@tonic-gate 		(void) printf("  flags = %s", prflags(flags));
1850Sstevel@tonic-gate 	(void) printf("\n");
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 	fltbits = *((uint32_t *)&fltmask);
1880Sstevel@tonic-gate 	if (fltbits)
1890Sstevel@tonic-gate 		(void) printf("\tflttrace = 0x%.8x\n", fltbits);
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 	sigtrace = *((uint32_t *)&sigmask);
1920Sstevel@tonic-gate 	sigtrace2 = *((uint32_t *)&sigmask + 1);
1930Sstevel@tonic-gate 	if (sigtrace | sigtrace2) {
1940Sstevel@tonic-gate 		(void) printf("\tsigtrace = 0x%.8x 0x%.8x\n\t    %s\n",
1950Sstevel@tonic-gate 		    sigtrace, sigtrace2,
1960Sstevel@tonic-gate 		    proc_sigset2str(&sigmask, "|", 1, buf, sizeof (buf)));
1970Sstevel@tonic-gate 	}
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate 	bits = ((uint32_t *)&entryset);
2000Sstevel@tonic-gate 	if (bits[0] | bits[1] | bits[2] | bits[3] |
2010Sstevel@tonic-gate 	    bits[4] | bits[5] | bits[6] | bits[7])
2020Sstevel@tonic-gate 		(void) printf(
2030Sstevel@tonic-gate 			"\tentryset = "
2040Sstevel@tonic-gate 			"0x%.8x 0x%.8x 0x%.8x 0x%.8x\n"
2050Sstevel@tonic-gate 			"\t           "
2060Sstevel@tonic-gate 			"0x%.8x 0x%.8x 0x%.8x 0x%.8x\n",
2070Sstevel@tonic-gate 			bits[0], bits[1], bits[2], bits[3],
2080Sstevel@tonic-gate 			bits[4], bits[5], bits[6], bits[7]);
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 	bits = ((uint32_t *)&exitset);
2110Sstevel@tonic-gate 	if (bits[0] | bits[1] | bits[2] | bits[3] |
2120Sstevel@tonic-gate 	    bits[4] | bits[5] | bits[6] | bits[7])
2130Sstevel@tonic-gate 		(void) printf(
2140Sstevel@tonic-gate 			"\texitset  = "
2150Sstevel@tonic-gate 			"0x%.8x 0x%.8x 0x%.8x 0x%.8x\n"
2160Sstevel@tonic-gate 			"\t           "
2170Sstevel@tonic-gate 			"0x%.8x 0x%.8x 0x%.8x 0x%.8x\n",
2180Sstevel@tonic-gate 			bits[0], bits[1], bits[2], bits[3],
2190Sstevel@tonic-gate 			bits[4], bits[5], bits[6], bits[7]);
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 	sigpend  = *((uint32_t *)&pstatus.pr_sigpend);
2220Sstevel@tonic-gate 	sigpend2 = *((uint32_t *)&pstatus.pr_sigpend + 1);
2230Sstevel@tonic-gate 	if (sigpend | sigpend2)
2240Sstevel@tonic-gate 		(void) printf("\tsigpend = 0x%.8x,0x%.8x\n",
2250Sstevel@tonic-gate 			sigpend, sigpend2);
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate 	lookarg.pflags = pstatus.pr_flags;
2280Sstevel@tonic-gate 	lookarg.count = 0;
2290Sstevel@tonic-gate 	(void) Plwp_iter_all(Pr, (proc_lwp_all_f *)lwplook, &lookarg);
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 	if (lookarg.count == 0)
2320Sstevel@tonic-gate 		(void) printf("No matching lwps found");
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 	(void) printf("\n");
2350Sstevel@tonic-gate 	Prelease(Pr, PRELEASE_RETAIN);
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate 	return (0);
2380Sstevel@tonic-gate }
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate static int
2410Sstevel@tonic-gate lwplook_zombie(const lwpsinfo_t *pip)
2420Sstevel@tonic-gate {
2430Sstevel@tonic-gate 	(void) printf(" /%d:\t<defunct>\n", (int)pip->pr_lwpid);
2440Sstevel@tonic-gate 	return (0);
2450Sstevel@tonic-gate }
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate static int
2480Sstevel@tonic-gate lwplook(look_arg_t *arg, const lwpstatus_t *psp, const lwpsinfo_t *pip)
2490Sstevel@tonic-gate {
2500Sstevel@tonic-gate 	int flags;
2510Sstevel@tonic-gate 	uint32_t sighold, sighold2;
2520Sstevel@tonic-gate 	uint32_t sigpend, sigpend2;
2530Sstevel@tonic-gate 	int cursig;
2540Sstevel@tonic-gate 	char buf[32];
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate 	if (!proc_lwp_in_set(arg->lwps, pip->pr_lwpid))
2570Sstevel@tonic-gate 		return (0);
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 	arg->count++;
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 	if (psp == NULL)
2620Sstevel@tonic-gate 		return (lwplook_zombie(pip));
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 	/*
2650Sstevel@tonic-gate 	 * PR_PCINVAL is just noise if the lwp is not stopped.
2660Sstevel@tonic-gate 	 * Don't bother reporting it unless the lwp is stopped.
2670Sstevel@tonic-gate 	 */
2680Sstevel@tonic-gate 	flags = psp->pr_flags & LWPFLAGS;
2690Sstevel@tonic-gate 	if (!(flags & PR_STOPPED))
2700Sstevel@tonic-gate 		flags &= ~PR_PCINVAL;
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate 	(void) printf(" /%d:\tflags = %s", (int)psp->pr_lwpid, prflags(flags));
2730Sstevel@tonic-gate 	if ((flags & PR_ASLEEP) || (psp->pr_syscall &&
2740Sstevel@tonic-gate 	    !(arg->pflags & PR_ISSYS))) {
2750Sstevel@tonic-gate 		if (flags & PR_ASLEEP) {
2760Sstevel@tonic-gate 			if ((flags & ~PR_ASLEEP) != 0)
2770Sstevel@tonic-gate 				(void) printf("|");
2780Sstevel@tonic-gate 			(void) printf("ASLEEP");
2790Sstevel@tonic-gate 		}
2800Sstevel@tonic-gate 		if (psp->pr_syscall && !(arg->pflags & PR_ISSYS)) {
2810Sstevel@tonic-gate 			uint_t i;
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate 			(void) printf("  %s(",
2840Sstevel@tonic-gate 			    proc_sysname(psp->pr_syscall, buf, sizeof (buf)));
2850Sstevel@tonic-gate 			for (i = 0; i < psp->pr_nsysarg; i++) {
2860Sstevel@tonic-gate 				if (i != 0)
2870Sstevel@tonic-gate 					(void) printf(",");
2880Sstevel@tonic-gate 				(void) printf("0x%lx", psp->pr_sysarg[i]);
2890Sstevel@tonic-gate 			}
2900Sstevel@tonic-gate 			(void) printf(")");
2910Sstevel@tonic-gate 		}
2920Sstevel@tonic-gate 	}
2930Sstevel@tonic-gate 	(void) printf("\n");
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate 	if (flags & PR_STOPPED) {
2960Sstevel@tonic-gate 		(void) printf("\twhy = %s", prwhy(psp->pr_why));
2970Sstevel@tonic-gate 		if (psp->pr_why != PR_REQUESTED &&
2980Sstevel@tonic-gate 		    psp->pr_why != PR_SUSPENDED)
2990Sstevel@tonic-gate 			(void) printf("  what = %s",
3000Sstevel@tonic-gate 				prwhat(psp->pr_why, psp->pr_what));
3010Sstevel@tonic-gate 		(void) printf("\n");
3020Sstevel@tonic-gate 	}
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate 	sighold  = *((uint32_t *)&psp->pr_lwphold);
3050Sstevel@tonic-gate 	sighold2 = *((uint32_t *)&psp->pr_lwphold + 1);
3060Sstevel@tonic-gate 	sigpend  = *((uint32_t *)&psp->pr_lwppend);
3070Sstevel@tonic-gate 	sigpend2 = *((uint32_t *)&psp->pr_lwppend + 1);
3080Sstevel@tonic-gate 	cursig   = psp->pr_cursig;
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate 	if (sighold | sighold2 | sigpend | sigpend2 | cursig) {
3110Sstevel@tonic-gate 		(void) printf("\t");
3120Sstevel@tonic-gate 		if (sighold | sighold2) {
3130Sstevel@tonic-gate 			(void) printf("sigmask = 0x%.8x,0x%.8x",
3140Sstevel@tonic-gate 				sighold, sighold2);
3150Sstevel@tonic-gate 			if (sigpend | sigpend2 | cursig)
3160Sstevel@tonic-gate 				(void) printf("  ");
3170Sstevel@tonic-gate 		}
3180Sstevel@tonic-gate 		if (sigpend | sigpend2) {
3190Sstevel@tonic-gate 			(void) printf("lwppend = 0x%.8x,0x%.8x",
3200Sstevel@tonic-gate 				sigpend, sigpend2);
3210Sstevel@tonic-gate 			if (cursig)
3220Sstevel@tonic-gate 				(void) printf("  ");
3230Sstevel@tonic-gate 		}
3240Sstevel@tonic-gate 		if (cursig)
3250Sstevel@tonic-gate 			(void) printf("cursig = %s",
3260Sstevel@tonic-gate 			    proc_signame(cursig, buf, sizeof (buf)));
3270Sstevel@tonic-gate 		(void) printf("\n");
3280Sstevel@tonic-gate 	}
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate 	if (rflag) {
3310Sstevel@tonic-gate 		if (Pstate(Pr) == PS_DEAD || (arg->pflags & PR_STOPPED)) {
3320Sstevel@tonic-gate #if defined(__sparc) && defined(_ILP32)
3330Sstevel@tonic-gate 			/*
3340Sstevel@tonic-gate 			 * If we're SPARC/32-bit, see if we can get extra
3350Sstevel@tonic-gate 			 * register state for this lwp.  If it's a v8plus
3360Sstevel@tonic-gate 			 * program, print the 64-bit register values.
3370Sstevel@tonic-gate 			 */
3380Sstevel@tonic-gate 			prxregset_t prx;
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate 			if (Plwp_getxregs(Pr, psp->pr_lwpid, &prx) == 0 &&
3410Sstevel@tonic-gate 			    prx.pr_type == XR_TYPE_V8P)
3420Sstevel@tonic-gate 				dumpregs_v8p(psp->pr_reg, &prx, is64);
3430Sstevel@tonic-gate 			else
3440Sstevel@tonic-gate #endif	/* __sparc && _ILP32 */
3450Sstevel@tonic-gate 				dumpregs(psp->pr_reg, is64);
3460Sstevel@tonic-gate 		} else
3470Sstevel@tonic-gate 			(void) printf("\tNot stopped, can't show registers\n");
3480Sstevel@tonic-gate 	}
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate 	return (0);
3510Sstevel@tonic-gate }
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate static char *
3540Sstevel@tonic-gate prflags(int arg)
3550Sstevel@tonic-gate {
3560Sstevel@tonic-gate 	static char code_buf[200];
3570Sstevel@tonic-gate 	char *str = code_buf;
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate 	if (arg == 0)
3600Sstevel@tonic-gate 		return ("0");
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate 	if (arg & ~ALLFLAGS)
3630Sstevel@tonic-gate 		(void) sprintf(str, "0x%x", arg & ~ALLFLAGS);
3640Sstevel@tonic-gate 	else
3650Sstevel@tonic-gate 		*str = '\0';
3660Sstevel@tonic-gate 
3670Sstevel@tonic-gate 	/*
3680Sstevel@tonic-gate 	 * Display the semi-permanent lwp flags first.
3690Sstevel@tonic-gate 	 */
3700Sstevel@tonic-gate 	if (arg & PR_DAEMON)		/* daemons are always detached so */
3710Sstevel@tonic-gate 		(void) strcat(str, "|DAEMON");
3720Sstevel@tonic-gate 	else if (arg & PR_DETACH)	/* report detach only if non-daemon */
3730Sstevel@tonic-gate 		(void) strcat(str, "|DETACH");
3740Sstevel@tonic-gate 
3750Sstevel@tonic-gate 	if (arg & PR_STOPPED)
3760Sstevel@tonic-gate 		(void) strcat(str, "|STOPPED");
3770Sstevel@tonic-gate 	if (arg & PR_ISTOP)
3780Sstevel@tonic-gate 		(void) strcat(str, "|ISTOP");
3790Sstevel@tonic-gate 	if (arg & PR_DSTOP)
3800Sstevel@tonic-gate 		(void) strcat(str, "|DSTOP");
3810Sstevel@tonic-gate #if 0		/* displayed elsewhere */
3820Sstevel@tonic-gate 	if (arg & PR_ASLEEP)
3830Sstevel@tonic-gate 		(void) strcat(str, "|ASLEEP");
3840Sstevel@tonic-gate #endif
3850Sstevel@tonic-gate 	if (arg & PR_PCINVAL)
3860Sstevel@tonic-gate 		(void) strcat(str, "|PCINVAL");
3870Sstevel@tonic-gate 	if (arg & PR_STEP)
3880Sstevel@tonic-gate 		(void) strcat(str, "|STEP");
3890Sstevel@tonic-gate 	if (arg & PR_AGENT)
3900Sstevel@tonic-gate 		(void) strcat(str, "|AGENT");
3910Sstevel@tonic-gate 	if (arg & PR_ISSYS)
3920Sstevel@tonic-gate 		(void) strcat(str, "|ISSYS");
3930Sstevel@tonic-gate 	if (arg & PR_VFORKP)
3940Sstevel@tonic-gate 		(void) strcat(str, "|VFORKP");
3950Sstevel@tonic-gate 	if (arg & PR_ORPHAN)
3960Sstevel@tonic-gate 		(void) strcat(str, "|ORPHAN");
3970Sstevel@tonic-gate 	if (arg & PR_FORK)
3980Sstevel@tonic-gate 		(void) strcat(str, "|FORK");
3990Sstevel@tonic-gate 	if (arg & PR_RLC)
4000Sstevel@tonic-gate 		(void) strcat(str, "|RLC");
4010Sstevel@tonic-gate 	if (arg & PR_KLC)
4020Sstevel@tonic-gate 		(void) strcat(str, "|KLC");
4030Sstevel@tonic-gate 	if (arg & PR_ASYNC)
4040Sstevel@tonic-gate 		(void) strcat(str, "|ASYNC");
4050Sstevel@tonic-gate 	if (arg & PR_BPTADJ)
4060Sstevel@tonic-gate 		(void) strcat(str, "|BPTADJ");
4070Sstevel@tonic-gate 	if (arg & PR_MSACCT)
4080Sstevel@tonic-gate 		(void) strcat(str, "|MSACCT");
4090Sstevel@tonic-gate 	if (arg & PR_MSFORK)
4100Sstevel@tonic-gate 		(void) strcat(str, "|MSFORK");
4110Sstevel@tonic-gate 	if (arg & PR_PTRACE)
4120Sstevel@tonic-gate 		(void) strcat(str, "|PTRACE");
4130Sstevel@tonic-gate 
4140Sstevel@tonic-gate 	if (*str == '|')
4150Sstevel@tonic-gate 		str++;
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate 	return (str);
4180Sstevel@tonic-gate }
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate static char *
4210Sstevel@tonic-gate prwhy(int why)
4220Sstevel@tonic-gate {
4230Sstevel@tonic-gate 	static char buf[20];
4240Sstevel@tonic-gate 	char *str;
4250Sstevel@tonic-gate 
4260Sstevel@tonic-gate 	switch (why) {
4270Sstevel@tonic-gate 	case PR_REQUESTED:
4280Sstevel@tonic-gate 		str = "PR_REQUESTED";
4290Sstevel@tonic-gate 		break;
4300Sstevel@tonic-gate 	case PR_SIGNALLED:
4310Sstevel@tonic-gate 		str = "PR_SIGNALLED";
4320Sstevel@tonic-gate 		break;
4330Sstevel@tonic-gate 	case PR_SYSENTRY:
4340Sstevel@tonic-gate 		str = "PR_SYSENTRY";
4350Sstevel@tonic-gate 		break;
4360Sstevel@tonic-gate 	case PR_SYSEXIT:
4370Sstevel@tonic-gate 		str = "PR_SYSEXIT";
4380Sstevel@tonic-gate 		break;
4390Sstevel@tonic-gate 	case PR_JOBCONTROL:
4400Sstevel@tonic-gate 		str = "PR_JOBCONTROL";
4410Sstevel@tonic-gate 		break;
4420Sstevel@tonic-gate 	case PR_FAULTED:
4430Sstevel@tonic-gate 		str = "PR_FAULTED";
4440Sstevel@tonic-gate 		break;
4450Sstevel@tonic-gate 	case PR_SUSPENDED:
4460Sstevel@tonic-gate 		str = "PR_SUSPENDED";
4470Sstevel@tonic-gate 		break;
4480Sstevel@tonic-gate 	default:
4490Sstevel@tonic-gate 		str = buf;
4500Sstevel@tonic-gate 		(void) sprintf(str, "%d", why);
4510Sstevel@tonic-gate 		break;
4520Sstevel@tonic-gate 	}
4530Sstevel@tonic-gate 
4540Sstevel@tonic-gate 	return (str);
4550Sstevel@tonic-gate }
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate static char *
4580Sstevel@tonic-gate prwhat(int why, int what)
4590Sstevel@tonic-gate {
4600Sstevel@tonic-gate 	static char buf[32];
4610Sstevel@tonic-gate 	char *str;
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate 	switch (why) {
4640Sstevel@tonic-gate 	case PR_SIGNALLED:
4650Sstevel@tonic-gate 	case PR_JOBCONTROL:
4660Sstevel@tonic-gate 		str = proc_signame(what, buf, sizeof (buf));
4670Sstevel@tonic-gate 		break;
4680Sstevel@tonic-gate 	case PR_SYSENTRY:
4690Sstevel@tonic-gate 	case PR_SYSEXIT:
4700Sstevel@tonic-gate 		str = proc_sysname(what, buf, sizeof (buf));
4710Sstevel@tonic-gate 		break;
4720Sstevel@tonic-gate 	case PR_FAULTED:
4730Sstevel@tonic-gate 		str = proc_fltname(what, buf, sizeof (buf));
4740Sstevel@tonic-gate 		break;
4750Sstevel@tonic-gate 	default:
4760Sstevel@tonic-gate 		(void) sprintf(str = buf, "%d", what);
4770Sstevel@tonic-gate 		break;
4780Sstevel@tonic-gate 	}
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate 	return (str);
4810Sstevel@tonic-gate }
4820Sstevel@tonic-gate 
4830Sstevel@tonic-gate #if defined(__sparc)
4840Sstevel@tonic-gate static const char * const regname[NPRGREG] = {
4850Sstevel@tonic-gate 	" %g0", " %g1", " %g2", " %g3", " %g4", " %g5", " %g6", " %g7",
4860Sstevel@tonic-gate 	" %o0", " %o1", " %o2", " %o3", " %o4", " %o5", " %sp", " %o7",
4870Sstevel@tonic-gate 	" %l0", " %l1", " %l2", " %l3", " %l4", " %l5", " %l6", " %l7",
4880Sstevel@tonic-gate 	" %i0", " %i1", " %i2", " %i3", " %i4", " %i5", " %fp", " %i7",
4890Sstevel@tonic-gate #ifdef __sparcv9
4900Sstevel@tonic-gate 	"%ccr", " %pc", "%npc", "  %y", "%asi", "%fprs"
4910Sstevel@tonic-gate #else
4920Sstevel@tonic-gate 	"%psr", " %pc", "%npc", "  %y", "%wim", "%tbr"
4930Sstevel@tonic-gate #endif
4940Sstevel@tonic-gate };
4950Sstevel@tonic-gate #endif	/* __sparc */
4960Sstevel@tonic-gate 
4970Sstevel@tonic-gate #if defined(__amd64)
4980Sstevel@tonic-gate static const char * const regname[NPRGREG] = {
4990Sstevel@tonic-gate 	"%r15", "%r14", "%r13", "%r12", "%r11", "%r10", " %r9", " %r8",
5000Sstevel@tonic-gate 	"%rdi", "%rsi", "%rbp", "%rbx", "%rdx", "%rcx", "%rax", "%trapno",
5010Sstevel@tonic-gate 	"%err", "%rip", " %cs", "%rfl", "%rsp", " %ss", " %fs", " %gs",
5020Sstevel@tonic-gate 	" %es", " %ds", "%fsbase", "%gsbase"
5030Sstevel@tonic-gate };
5040Sstevel@tonic-gate 
5050Sstevel@tonic-gate static const char * const regname32[NPRGREG32] = {
5060Sstevel@tonic-gate 	" %gs", " %fs", " %es", " %ds", "%edi", "%esi", "%ebp", "%esp",
5070Sstevel@tonic-gate 	"%ebx", "%edx", "%ecx", "%eax", "%trapno", "%err", "%eip", " %cs",
5080Sstevel@tonic-gate 	"%efl", "%uesp", " %ss"
5090Sstevel@tonic-gate };
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate /* XX64 Do we want to expose this through libproc */
5120Sstevel@tonic-gate void
5130Sstevel@tonic-gate prgregset_n_to_32(const prgreg_t *src, prgreg32_t *dst)
5140Sstevel@tonic-gate {
5150Sstevel@tonic-gate 	bzero(dst, NPRGREG32 * sizeof (prgreg32_t));
5160Sstevel@tonic-gate 	dst[GS] = src[REG_GS];
5170Sstevel@tonic-gate 	dst[FS] = src[REG_FS];
5180Sstevel@tonic-gate 	dst[DS] = src[REG_DS];
5190Sstevel@tonic-gate 	dst[ES] = src[REG_ES];
5200Sstevel@tonic-gate 	dst[EDI] = src[REG_RDI];
5210Sstevel@tonic-gate 	dst[ESI] = src[REG_RSI];
5220Sstevel@tonic-gate 	dst[EBP] = src[REG_RBP];
5230Sstevel@tonic-gate 	dst[EBX] = src[REG_RBX];
5240Sstevel@tonic-gate 	dst[EDX] = src[REG_RDX];
5250Sstevel@tonic-gate 	dst[ECX] = src[REG_RCX];
5260Sstevel@tonic-gate 	dst[EAX] = src[REG_RAX];
5270Sstevel@tonic-gate 	dst[TRAPNO] = src[REG_TRAPNO];
5280Sstevel@tonic-gate 	dst[ERR] = src[REG_ERR];
5290Sstevel@tonic-gate 	dst[EIP] = src[REG_RIP];
5300Sstevel@tonic-gate 	dst[CS] = src[REG_CS];
5310Sstevel@tonic-gate 	dst[EFL] = src[REG_RFL];
5320Sstevel@tonic-gate 	dst[UESP] = src[REG_RSP];
5330Sstevel@tonic-gate 	dst[SS] = src[REG_SS];
5340Sstevel@tonic-gate }
5350Sstevel@tonic-gate 
5360Sstevel@tonic-gate #elif defined(__i386)
5370Sstevel@tonic-gate static const char * const regname[NPRGREG] = {
5380Sstevel@tonic-gate 	" %gs", " %fs", " %es", " %ds", "%edi", "%esi", "%ebp", "%esp",
5390Sstevel@tonic-gate 	"%ebx", "%edx", "%ecx", "%eax", "%trapno", "%err", "%eip", " %cs",
5400Sstevel@tonic-gate 	"%efl", "%uesp", " %ss"
5410Sstevel@tonic-gate };
5420Sstevel@tonic-gate #endif /* __i386 */
5430Sstevel@tonic-gate 
5440Sstevel@tonic-gate #if defined(__amd64) && defined(_LP64)
5450Sstevel@tonic-gate static void
5460Sstevel@tonic-gate dumpregs32(const prgregset_t reg)
5470Sstevel@tonic-gate {
5480Sstevel@tonic-gate 	prgregset32_t reg32;
5490Sstevel@tonic-gate 	int i;
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate 	prgregset_n_to_32(reg, reg32);
5520Sstevel@tonic-gate 
5530Sstevel@tonic-gate 	for (i = 0; i < NPRGREG32; i++) {
5540Sstevel@tonic-gate 		(void) printf("  %s = 0x%.8X",
5550Sstevel@tonic-gate 			regname32[i], reg32[i]);
5560Sstevel@tonic-gate 		if ((i+1) % 4 == 0)
5570Sstevel@tonic-gate 			(void) putchar('\n');
5580Sstevel@tonic-gate 	}
5590Sstevel@tonic-gate 	if (i % 4 != 0)
5600Sstevel@tonic-gate 		(void) putchar('\n');
5610Sstevel@tonic-gate }
5620Sstevel@tonic-gate #endif
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate static void
5650Sstevel@tonic-gate dumpregs(const prgregset_t reg, int is64)
5660Sstevel@tonic-gate {
5670Sstevel@tonic-gate 	int width = is64? 16 : 8;
5680Sstevel@tonic-gate 	int cols = is64? 2 : 4;
5690Sstevel@tonic-gate 	int i;
5700Sstevel@tonic-gate 
5710Sstevel@tonic-gate #if defined(__amd64) && defined(_LP64)
5720Sstevel@tonic-gate 	if (!is64) {
5730Sstevel@tonic-gate 		dumpregs32(reg);
5740Sstevel@tonic-gate 		return;
5750Sstevel@tonic-gate 	}
5760Sstevel@tonic-gate #endif
5770Sstevel@tonic-gate 
5780Sstevel@tonic-gate 	for (i = 0; i < NPRGREG; i++) {
5790Sstevel@tonic-gate 		(void) printf("  %s = 0x%.*lX",
5800Sstevel@tonic-gate 			regname[i], width, (long)reg[i]);
5810Sstevel@tonic-gate 		if ((i+1) % cols == 0)
5820Sstevel@tonic-gate 			(void) putchar('\n');
5830Sstevel@tonic-gate 	}
5840Sstevel@tonic-gate 	if (i % cols != 0)
5850Sstevel@tonic-gate 		(void) putchar('\n');
5860Sstevel@tonic-gate }
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate #if defined(__sparc) && defined(_ILP32)
5890Sstevel@tonic-gate static void
5900Sstevel@tonic-gate dumpregs_v8p(const prgregset_t reg, const prxregset_t *xreg, int is64)
5910Sstevel@tonic-gate {
5920Sstevel@tonic-gate 	static const uint32_t zero[8] = { 0 };
5930Sstevel@tonic-gate 	int gr, xr, cols = 2;
5940Sstevel@tonic-gate 	uint64_t xval;
5950Sstevel@tonic-gate 
5960Sstevel@tonic-gate 	if (memcmp(xreg->pr_un.pr_v8p.pr_xg, zero, sizeof (zero)) == 0 &&
5970Sstevel@tonic-gate 	    memcmp(xreg->pr_un.pr_v8p.pr_xo, zero, sizeof (zero)) == 0) {
5980Sstevel@tonic-gate 		dumpregs(reg, is64);
5990Sstevel@tonic-gate 		return;
6000Sstevel@tonic-gate 	}
6010Sstevel@tonic-gate 
6020Sstevel@tonic-gate 	for (gr = R_G0, xr = XR_G0; gr <= R_G7; gr++, xr++) {
6030Sstevel@tonic-gate 		xval = (uint64_t)xreg->pr_un.pr_v8p.pr_xg[xr] << 32 |
6040Sstevel@tonic-gate 		    (uint64_t)(uint32_t)reg[gr];
6050Sstevel@tonic-gate 		(void) printf("  %s = 0x%.16" PRIX64, regname[gr], xval);
6060Sstevel@tonic-gate 		if ((gr + 1) % cols == 0)
6070Sstevel@tonic-gate 			(void) putchar('\n');
6080Sstevel@tonic-gate 	}
6090Sstevel@tonic-gate 
6100Sstevel@tonic-gate 	for (gr = R_O0, xr = XR_O0; gr <= R_O7; gr++, xr++) {
6110Sstevel@tonic-gate 		xval = (uint64_t)xreg->pr_un.pr_v8p.pr_xo[xr] << 32 |
6120Sstevel@tonic-gate 		    (uint64_t)(uint32_t)reg[gr];
6130Sstevel@tonic-gate 		(void) printf("  %s = 0x%.16" PRIX64, regname[gr], xval);
6140Sstevel@tonic-gate 		if ((gr + 1) % cols == 0)
6150Sstevel@tonic-gate 			(void) putchar('\n');
6160Sstevel@tonic-gate 	}
6170Sstevel@tonic-gate 
6180Sstevel@tonic-gate 	for (gr = R_L0; gr < NPRGREG; gr++) {
6190Sstevel@tonic-gate 		(void) printf("  %s =         0x%.8lX",
6200Sstevel@tonic-gate 		    regname[gr], (long)reg[gr]);
6210Sstevel@tonic-gate 		if ((gr + 1) % cols == 0)
6220Sstevel@tonic-gate 			(void) putchar('\n');
6230Sstevel@tonic-gate 	}
6240Sstevel@tonic-gate 
6250Sstevel@tonic-gate 	if (gr % cols != 0)
6260Sstevel@tonic-gate 		(void) putchar('\n');
6270Sstevel@tonic-gate }
6280Sstevel@tonic-gate #endif	/* __sparc && _ILP32 */
629