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