17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5004388ebScasper * Common Development and Distribution License (the "License").
6004388ebScasper * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
21657b1f3dSraf
227c478bd9Sstevel@tonic-gate /*
23bc5ed3ddSRoger A. Faulkner * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
27f971a346SBryan Cantrill /*
28f971a346SBryan Cantrill * Copyright (c) 2013, Joyent, Inc. All rights reserved.
29f971a346SBryan Cantrill */
30f971a346SBryan Cantrill
317c478bd9Sstevel@tonic-gate #include <stdio.h>
32004388ebScasper #include <stdio_ext.h>
337c478bd9Sstevel@tonic-gate #include <stdlib.h>
347c478bd9Sstevel@tonic-gate #include <unistd.h>
357c478bd9Sstevel@tonic-gate #include <ctype.h>
367c478bd9Sstevel@tonic-gate #include <fcntl.h>
377c478bd9Sstevel@tonic-gate #include <strings.h>
387c478bd9Sstevel@tonic-gate #include <dirent.h>
397c478bd9Sstevel@tonic-gate #include <errno.h>
407c478bd9Sstevel@tonic-gate #include <sys/types.h>
417c478bd9Sstevel@tonic-gate #include <sys/int_fmtio.h>
427c478bd9Sstevel@tonic-gate #include <libproc.h>
437c478bd9Sstevel@tonic-gate
447c478bd9Sstevel@tonic-gate typedef struct look_arg {
457c478bd9Sstevel@tonic-gate int pflags;
467c478bd9Sstevel@tonic-gate const char *lwps;
477c478bd9Sstevel@tonic-gate int count;
487c478bd9Sstevel@tonic-gate } look_arg_t;
497c478bd9Sstevel@tonic-gate
507c478bd9Sstevel@tonic-gate static int look(char *);
517c478bd9Sstevel@tonic-gate static int lwplook(look_arg_t *, const lwpstatus_t *, const lwpsinfo_t *);
527c478bd9Sstevel@tonic-gate static char *prflags(int);
537c478bd9Sstevel@tonic-gate static char *prwhy(int);
547c478bd9Sstevel@tonic-gate static char *prwhat(int, int);
557c478bd9Sstevel@tonic-gate static void dumpregs(const prgregset_t, int);
567c478bd9Sstevel@tonic-gate
577c478bd9Sstevel@tonic-gate static char *command;
587c478bd9Sstevel@tonic-gate static struct ps_prochandle *Pr;
597c478bd9Sstevel@tonic-gate
607c478bd9Sstevel@tonic-gate static int is64; /* Is current process 64-bit? */
617c478bd9Sstevel@tonic-gate static int rflag; /* Show registers? */
627c478bd9Sstevel@tonic-gate
637c478bd9Sstevel@tonic-gate #define LWPFLAGS \
647c478bd9Sstevel@tonic-gate (PR_STOPPED|PR_ISTOP|PR_DSTOP|PR_ASLEEP|PR_PCINVAL|PR_STEP \
65657b1f3dSraf |PR_AGENT|PR_DETACH|PR_DAEMON)
667c478bd9Sstevel@tonic-gate
677c478bd9Sstevel@tonic-gate #define PROCFLAGS \
68657b1f3dSraf (PR_ISSYS|PR_VFORKP|PR_ORPHAN|PR_NOSIGCHLD|PR_WAITPID \
69657b1f3dSraf |PR_FORK|PR_RLC|PR_KLC|PR_ASYNC|PR_BPTADJ|PR_MSACCT|PR_MSFORK|PR_PTRACE)
707c478bd9Sstevel@tonic-gate
717c478bd9Sstevel@tonic-gate #define ALLFLAGS (LWPFLAGS|PROCFLAGS)
727c478bd9Sstevel@tonic-gate
737c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)747c478bd9Sstevel@tonic-gate main(int argc, char **argv)
757c478bd9Sstevel@tonic-gate {
767c478bd9Sstevel@tonic-gate int rc = 0;
777c478bd9Sstevel@tonic-gate int errflg = 0;
787c478bd9Sstevel@tonic-gate int opt;
797c478bd9Sstevel@tonic-gate struct rlimit rlim;
807c478bd9Sstevel@tonic-gate
817c478bd9Sstevel@tonic-gate if ((command = strrchr(argv[0], '/')) != NULL)
827c478bd9Sstevel@tonic-gate command++;
837c478bd9Sstevel@tonic-gate else
847c478bd9Sstevel@tonic-gate command = argv[0];
857c478bd9Sstevel@tonic-gate
867c478bd9Sstevel@tonic-gate /* options */
877c478bd9Sstevel@tonic-gate while ((opt = getopt(argc, argv, "r")) != EOF) {
887c478bd9Sstevel@tonic-gate switch (opt) {
897c478bd9Sstevel@tonic-gate case 'r': /* show registers */
907c478bd9Sstevel@tonic-gate rflag = 1;
917c478bd9Sstevel@tonic-gate break;
927c478bd9Sstevel@tonic-gate default:
937c478bd9Sstevel@tonic-gate errflg = 1;
947c478bd9Sstevel@tonic-gate break;
957c478bd9Sstevel@tonic-gate }
967c478bd9Sstevel@tonic-gate }
977c478bd9Sstevel@tonic-gate
987c478bd9Sstevel@tonic-gate argc -= optind;
997c478bd9Sstevel@tonic-gate argv += optind;
1007c478bd9Sstevel@tonic-gate
1017c478bd9Sstevel@tonic-gate if (errflg || argc <= 0) {
1027c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
1037c478bd9Sstevel@tonic-gate "usage:\t%s [-r] { pid | core }[/lwps] ...\n", command);
1047c478bd9Sstevel@tonic-gate (void) fprintf(stderr, " (report process status flags)\n");
1057c478bd9Sstevel@tonic-gate (void) fprintf(stderr, " -r : report registers\n");
1067c478bd9Sstevel@tonic-gate return (2);
1077c478bd9Sstevel@tonic-gate }
1087c478bd9Sstevel@tonic-gate
1097c478bd9Sstevel@tonic-gate /*
1107c478bd9Sstevel@tonic-gate * Make sure we'll have enough file descriptors to handle a target
1117c478bd9Sstevel@tonic-gate * that has many many mappings.
1127c478bd9Sstevel@tonic-gate */
1137c478bd9Sstevel@tonic-gate if (getrlimit(RLIMIT_NOFILE, &rlim) == 0) {
1147c478bd9Sstevel@tonic-gate rlim.rlim_cur = rlim.rlim_max;
1157c478bd9Sstevel@tonic-gate (void) setrlimit(RLIMIT_NOFILE, &rlim);
116004388ebScasper (void) enable_extended_FILE_stdio(-1, -1);
1177c478bd9Sstevel@tonic-gate }
1187c478bd9Sstevel@tonic-gate
1197c478bd9Sstevel@tonic-gate while (argc-- > 0)
1207c478bd9Sstevel@tonic-gate rc += look(*argv++);
1217c478bd9Sstevel@tonic-gate
1227c478bd9Sstevel@tonic-gate return (rc);
1237c478bd9Sstevel@tonic-gate }
1247c478bd9Sstevel@tonic-gate
1257c478bd9Sstevel@tonic-gate static int
look(char * arg)1267c478bd9Sstevel@tonic-gate look(char *arg)
1277c478bd9Sstevel@tonic-gate {
1287c478bd9Sstevel@tonic-gate int gcode;
1297c478bd9Sstevel@tonic-gate int gcode2;
1307c478bd9Sstevel@tonic-gate pstatus_t pstatus;
1317c478bd9Sstevel@tonic-gate psinfo_t psinfo;
1327c478bd9Sstevel@tonic-gate int flags;
1337c478bd9Sstevel@tonic-gate sigset_t sigmask;
1347c478bd9Sstevel@tonic-gate fltset_t fltmask;
1357c478bd9Sstevel@tonic-gate sysset_t entryset;
1367c478bd9Sstevel@tonic-gate sysset_t exitset;
137bc5ed3ddSRoger A. Faulkner uint32_t sigtrace, sigtrace1, sigtrace2, fltbits;
138bc5ed3ddSRoger A. Faulkner uint32_t sigpend, sigpend1, sigpend2;
1397c478bd9Sstevel@tonic-gate uint32_t *bits;
1407c478bd9Sstevel@tonic-gate char buf[PRSIGBUFSZ];
1417c478bd9Sstevel@tonic-gate look_arg_t lookarg;
1427c478bd9Sstevel@tonic-gate
1437c478bd9Sstevel@tonic-gate if ((Pr = proc_arg_xgrab(arg, NULL, PR_ARG_ANY,
1447c478bd9Sstevel@tonic-gate PGRAB_RETAIN | PGRAB_FORCE | PGRAB_RDONLY | PGRAB_NOSTOP, &gcode,
1457c478bd9Sstevel@tonic-gate &lookarg.lwps)) == NULL) {
1467c478bd9Sstevel@tonic-gate if (gcode == G_NOPROC &&
1477c478bd9Sstevel@tonic-gate proc_arg_psinfo(arg, PR_ARG_PIDS, &psinfo, &gcode2) > 0 &&
1487c478bd9Sstevel@tonic-gate psinfo.pr_nlwp == 0) {
1497c478bd9Sstevel@tonic-gate (void) printf("%d:\t<defunct>\n\n", (int)psinfo.pr_pid);
1507c478bd9Sstevel@tonic-gate return (0);
1517c478bd9Sstevel@tonic-gate }
1527c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: cannot examine %s: %s\n",
153bc5ed3ddSRoger A. Faulkner command, arg, Pgrab_error(gcode));
1547c478bd9Sstevel@tonic-gate return (1);
1557c478bd9Sstevel@tonic-gate }
1567c478bd9Sstevel@tonic-gate
1577c478bd9Sstevel@tonic-gate (void) memcpy(&pstatus, Pstatus(Pr), sizeof (pstatus_t));
1587c478bd9Sstevel@tonic-gate (void) memcpy(&psinfo, Ppsinfo(Pr), sizeof (psinfo_t));
1597c478bd9Sstevel@tonic-gate proc_unctrl_psinfo(&psinfo);
1607c478bd9Sstevel@tonic-gate
1617c478bd9Sstevel@tonic-gate if (psinfo.pr_nlwp == 0) {
1627c478bd9Sstevel@tonic-gate (void) printf("%d:\t<defunct>\n\n", (int)psinfo.pr_pid);
1637c478bd9Sstevel@tonic-gate Prelease(Pr, PRELEASE_RETAIN);
1647c478bd9Sstevel@tonic-gate return (0);
1657c478bd9Sstevel@tonic-gate }
1667c478bd9Sstevel@tonic-gate
1677c478bd9Sstevel@tonic-gate is64 = (pstatus.pr_dmodel == PR_MODEL_LP64);
1687c478bd9Sstevel@tonic-gate
1697c478bd9Sstevel@tonic-gate sigmask = pstatus.pr_sigtrace;
1707c478bd9Sstevel@tonic-gate fltmask = pstatus.pr_flttrace;
1717c478bd9Sstevel@tonic-gate entryset = pstatus.pr_sysentry;
1727c478bd9Sstevel@tonic-gate exitset = pstatus.pr_sysexit;
1737c478bd9Sstevel@tonic-gate
1747c478bd9Sstevel@tonic-gate if (Pstate(Pr) == PS_DEAD) {
1757c478bd9Sstevel@tonic-gate (void) printf("core '%s' of %d:\t%.70s\n",
1767c478bd9Sstevel@tonic-gate arg, (int)psinfo.pr_pid, psinfo.pr_psargs);
1777c478bd9Sstevel@tonic-gate } else {
1787c478bd9Sstevel@tonic-gate (void) printf("%d:\t%.70s\n",
1797c478bd9Sstevel@tonic-gate (int)psinfo.pr_pid, psinfo.pr_psargs);
1807c478bd9Sstevel@tonic-gate }
1817c478bd9Sstevel@tonic-gate
1827c478bd9Sstevel@tonic-gate (void) printf("\tdata model = %s", is64? "_LP64" : "_ILP32");
1837c478bd9Sstevel@tonic-gate if ((flags = (pstatus.pr_flags & PROCFLAGS)) != 0)
1847c478bd9Sstevel@tonic-gate (void) printf(" flags = %s", prflags(flags));
1857c478bd9Sstevel@tonic-gate (void) printf("\n");
1867c478bd9Sstevel@tonic-gate
1877c478bd9Sstevel@tonic-gate fltbits = *((uint32_t *)&fltmask);
1887c478bd9Sstevel@tonic-gate if (fltbits)
1897c478bd9Sstevel@tonic-gate (void) printf("\tflttrace = 0x%.8x\n", fltbits);
1907c478bd9Sstevel@tonic-gate
191bc5ed3ddSRoger A. Faulkner #if (MAXSIG > 2 * 32) && (MAXSIG <= 3 * 32) /* assumption */
1927c478bd9Sstevel@tonic-gate sigtrace = *((uint32_t *)&sigmask);
193bc5ed3ddSRoger A. Faulkner sigtrace1 = *((uint32_t *)&sigmask + 1);
194bc5ed3ddSRoger A. Faulkner sigtrace2 = *((uint32_t *)&sigmask + 2);
195bc5ed3ddSRoger A. Faulkner #else
196bc5ed3ddSRoger A. Faulkner #error "fix me: MAXSIG out of bounds"
197bc5ed3ddSRoger A. Faulkner #endif
198bc5ed3ddSRoger A. Faulkner if (sigtrace | sigtrace1 | sigtrace2)
199bc5ed3ddSRoger A. Faulkner (void) printf("\tsigtrace = 0x%.8x 0x%.8x 0x%.8x\n\t %s\n",
200bc5ed3ddSRoger A. Faulkner sigtrace, sigtrace1, sigtrace2,
2017c478bd9Sstevel@tonic-gate proc_sigset2str(&sigmask, "|", 1, buf, sizeof (buf)));
2027c478bd9Sstevel@tonic-gate
2037c478bd9Sstevel@tonic-gate bits = ((uint32_t *)&entryset);
2047c478bd9Sstevel@tonic-gate if (bits[0] | bits[1] | bits[2] | bits[3] |
2057c478bd9Sstevel@tonic-gate bits[4] | bits[5] | bits[6] | bits[7])
2067c478bd9Sstevel@tonic-gate (void) printf(
207bc5ed3ddSRoger A. Faulkner "\tentryset = "
208bc5ed3ddSRoger A. Faulkner "0x%.8x 0x%.8x 0x%.8x 0x%.8x\n"
209bc5ed3ddSRoger A. Faulkner "\t "
210bc5ed3ddSRoger A. Faulkner "0x%.8x 0x%.8x 0x%.8x 0x%.8x\n",
211bc5ed3ddSRoger A. Faulkner bits[0], bits[1], bits[2], bits[3],
212bc5ed3ddSRoger A. Faulkner bits[4], bits[5], bits[6], bits[7]);
2137c478bd9Sstevel@tonic-gate
2147c478bd9Sstevel@tonic-gate bits = ((uint32_t *)&exitset);
2157c478bd9Sstevel@tonic-gate if (bits[0] | bits[1] | bits[2] | bits[3] |
2167c478bd9Sstevel@tonic-gate bits[4] | bits[5] | bits[6] | bits[7])
2177c478bd9Sstevel@tonic-gate (void) printf(
218bc5ed3ddSRoger A. Faulkner "\texitset = "
219bc5ed3ddSRoger A. Faulkner "0x%.8x 0x%.8x 0x%.8x 0x%.8x\n"
220bc5ed3ddSRoger A. Faulkner "\t "
221bc5ed3ddSRoger A. Faulkner "0x%.8x 0x%.8x 0x%.8x 0x%.8x\n",
222bc5ed3ddSRoger A. Faulkner bits[0], bits[1], bits[2], bits[3],
223bc5ed3ddSRoger A. Faulkner bits[4], bits[5], bits[6], bits[7]);
224bc5ed3ddSRoger A. Faulkner
225bc5ed3ddSRoger A. Faulkner #if (MAXSIG > 2 * 32) && (MAXSIG <= 3 * 32) /* assumption */
2267c478bd9Sstevel@tonic-gate sigpend = *((uint32_t *)&pstatus.pr_sigpend);
227bc5ed3ddSRoger A. Faulkner sigpend1 = *((uint32_t *)&pstatus.pr_sigpend + 1);
228bc5ed3ddSRoger A. Faulkner sigpend2 = *((uint32_t *)&pstatus.pr_sigpend + 2);
229bc5ed3ddSRoger A. Faulkner #else
230bc5ed3ddSRoger A. Faulkner #error "fix me: MAXSIG out of bounds"
231bc5ed3ddSRoger A. Faulkner #endif
232bc5ed3ddSRoger A. Faulkner if (sigpend | sigpend1 | sigpend2)
233bc5ed3ddSRoger A. Faulkner (void) printf("\tsigpend = 0x%.8x,0x%.8x,0x%.8x\n",
234bc5ed3ddSRoger A. Faulkner sigpend, sigpend1, sigpend2);
2357c478bd9Sstevel@tonic-gate
2367c478bd9Sstevel@tonic-gate lookarg.pflags = pstatus.pr_flags;
2377c478bd9Sstevel@tonic-gate lookarg.count = 0;
2387c478bd9Sstevel@tonic-gate (void) Plwp_iter_all(Pr, (proc_lwp_all_f *)lwplook, &lookarg);
2397c478bd9Sstevel@tonic-gate
2407c478bd9Sstevel@tonic-gate if (lookarg.count == 0)
2417c478bd9Sstevel@tonic-gate (void) printf("No matching lwps found");
2427c478bd9Sstevel@tonic-gate
2437c478bd9Sstevel@tonic-gate (void) printf("\n");
2447c478bd9Sstevel@tonic-gate Prelease(Pr, PRELEASE_RETAIN);
2457c478bd9Sstevel@tonic-gate
2467c478bd9Sstevel@tonic-gate return (0);
2477c478bd9Sstevel@tonic-gate }
2487c478bd9Sstevel@tonic-gate
2497c478bd9Sstevel@tonic-gate static int
lwplook_zombie(const lwpsinfo_t * pip)2507c478bd9Sstevel@tonic-gate lwplook_zombie(const lwpsinfo_t *pip)
2517c478bd9Sstevel@tonic-gate {
2527c478bd9Sstevel@tonic-gate (void) printf(" /%d:\t<defunct>\n", (int)pip->pr_lwpid);
2537c478bd9Sstevel@tonic-gate return (0);
2547c478bd9Sstevel@tonic-gate }
2557c478bd9Sstevel@tonic-gate
2567c478bd9Sstevel@tonic-gate static int
lwplook(look_arg_t * arg,const lwpstatus_t * psp,const lwpsinfo_t * pip)2577c478bd9Sstevel@tonic-gate lwplook(look_arg_t *arg, const lwpstatus_t *psp, const lwpsinfo_t *pip)
2587c478bd9Sstevel@tonic-gate {
2597c478bd9Sstevel@tonic-gate int flags;
260bc5ed3ddSRoger A. Faulkner uint32_t sighold, sighold1, sighold2;
261bc5ed3ddSRoger A. Faulkner uint32_t sigpend, sigpend1, sigpend2;
262f971a346SBryan Cantrill psinfo_t ps;
2637c478bd9Sstevel@tonic-gate int cursig;
2647c478bd9Sstevel@tonic-gate char buf[32];
2657c478bd9Sstevel@tonic-gate
2667c478bd9Sstevel@tonic-gate if (!proc_lwp_in_set(arg->lwps, pip->pr_lwpid))
2677c478bd9Sstevel@tonic-gate return (0);
2687c478bd9Sstevel@tonic-gate
2697c478bd9Sstevel@tonic-gate arg->count++;
2707c478bd9Sstevel@tonic-gate
2717c478bd9Sstevel@tonic-gate if (psp == NULL)
2727c478bd9Sstevel@tonic-gate return (lwplook_zombie(pip));
2737c478bd9Sstevel@tonic-gate
2747c478bd9Sstevel@tonic-gate /*
2757c478bd9Sstevel@tonic-gate * PR_PCINVAL is just noise if the lwp is not stopped.
2767c478bd9Sstevel@tonic-gate * Don't bother reporting it unless the lwp is stopped.
2777c478bd9Sstevel@tonic-gate */
2787c478bd9Sstevel@tonic-gate flags = psp->pr_flags & LWPFLAGS;
2797c478bd9Sstevel@tonic-gate if (!(flags & PR_STOPPED))
2807c478bd9Sstevel@tonic-gate flags &= ~PR_PCINVAL;
2817c478bd9Sstevel@tonic-gate
2827c478bd9Sstevel@tonic-gate (void) printf(" /%d:\tflags = %s", (int)psp->pr_lwpid, prflags(flags));
2837c478bd9Sstevel@tonic-gate if ((flags & PR_ASLEEP) || (psp->pr_syscall &&
2847c478bd9Sstevel@tonic-gate !(arg->pflags & PR_ISSYS))) {
2857c478bd9Sstevel@tonic-gate if (flags & PR_ASLEEP) {
2867c478bd9Sstevel@tonic-gate if ((flags & ~PR_ASLEEP) != 0)
2877c478bd9Sstevel@tonic-gate (void) printf("|");
2887c478bd9Sstevel@tonic-gate (void) printf("ASLEEP");
2897c478bd9Sstevel@tonic-gate }
2907c478bd9Sstevel@tonic-gate if (psp->pr_syscall && !(arg->pflags & PR_ISSYS)) {
2917c478bd9Sstevel@tonic-gate uint_t i;
2927c478bd9Sstevel@tonic-gate
2937c478bd9Sstevel@tonic-gate (void) printf(" %s(",
2947c478bd9Sstevel@tonic-gate proc_sysname(psp->pr_syscall, buf, sizeof (buf)));
2957c478bd9Sstevel@tonic-gate for (i = 0; i < psp->pr_nsysarg; i++) {
2967c478bd9Sstevel@tonic-gate if (i != 0)
2977c478bd9Sstevel@tonic-gate (void) printf(",");
2987c478bd9Sstevel@tonic-gate (void) printf("0x%lx", psp->pr_sysarg[i]);
2997c478bd9Sstevel@tonic-gate }
3007c478bd9Sstevel@tonic-gate (void) printf(")");
3017c478bd9Sstevel@tonic-gate }
3027c478bd9Sstevel@tonic-gate }
3037c478bd9Sstevel@tonic-gate (void) printf("\n");
3047c478bd9Sstevel@tonic-gate
3057c478bd9Sstevel@tonic-gate if (flags & PR_STOPPED) {
3067c478bd9Sstevel@tonic-gate (void) printf("\twhy = %s", prwhy(psp->pr_why));
3077c478bd9Sstevel@tonic-gate if (psp->pr_why != PR_REQUESTED &&
3087c478bd9Sstevel@tonic-gate psp->pr_why != PR_SUSPENDED)
3097c478bd9Sstevel@tonic-gate (void) printf(" what = %s",
310bc5ed3ddSRoger A. Faulkner prwhat(psp->pr_why, psp->pr_what));
3117c478bd9Sstevel@tonic-gate (void) printf("\n");
3127c478bd9Sstevel@tonic-gate }
3137c478bd9Sstevel@tonic-gate
314bc5ed3ddSRoger A. Faulkner #if (MAXSIG > 2 * 32) && (MAXSIG <= 3 * 32) /* assumption */
3157c478bd9Sstevel@tonic-gate sighold = *((uint32_t *)&psp->pr_lwphold);
316bc5ed3ddSRoger A. Faulkner sighold1 = *((uint32_t *)&psp->pr_lwphold + 1);
317bc5ed3ddSRoger A. Faulkner sighold2 = *((uint32_t *)&psp->pr_lwphold + 2);
3187c478bd9Sstevel@tonic-gate sigpend = *((uint32_t *)&psp->pr_lwppend);
319bc5ed3ddSRoger A. Faulkner sigpend1 = *((uint32_t *)&psp->pr_lwppend + 1);
320bc5ed3ddSRoger A. Faulkner sigpend2 = *((uint32_t *)&psp->pr_lwppend + 2);
321bc5ed3ddSRoger A. Faulkner #else
322bc5ed3ddSRoger A. Faulkner #error "fix me: MAXSIG out of bounds"
323bc5ed3ddSRoger A. Faulkner #endif
3247c478bd9Sstevel@tonic-gate cursig = psp->pr_cursig;
3257c478bd9Sstevel@tonic-gate
326bc5ed3ddSRoger A. Faulkner if (sighold | sighold1 | sighold2)
327bc5ed3ddSRoger A. Faulkner (void) printf("\tsigmask = 0x%.8x,0x%.8x,0x%.8x\n",
328bc5ed3ddSRoger A. Faulkner sighold, sighold1, sighold2);
329bc5ed3ddSRoger A. Faulkner if (sigpend | sigpend1 | sigpend2)
330bc5ed3ddSRoger A. Faulkner (void) printf("\tlwppend = 0x%.8x,0x%.8x,0x%.8x\n",
331bc5ed3ddSRoger A. Faulkner sigpend, sigpend1, sigpend2);
332bc5ed3ddSRoger A. Faulkner if (cursig)
333bc5ed3ddSRoger A. Faulkner (void) printf("\tcursig = %s\n",
334bc5ed3ddSRoger A. Faulkner proc_signame(cursig, buf, sizeof (buf)));
3357c478bd9Sstevel@tonic-gate
336f971a346SBryan Cantrill if ((flags & PR_AGENT) &&
337f971a346SBryan Cantrill Plwp_getspymaster(Pr, pip->pr_lwpid, &ps) == 0) {
338f971a346SBryan Cantrill time_t time = ps.pr_time.tv_sec;
339f971a346SBryan Cantrill char t[64];
340f971a346SBryan Cantrill
341f971a346SBryan Cantrill (void) strftime(t, sizeof (t), "%F:%H.%M.%S", localtime(&time));
342f971a346SBryan Cantrill
343f971a346SBryan Cantrill (void) printf("\tspymaster = pid %d, \"%s\" at %s\n",
344f971a346SBryan Cantrill (int)ps.pr_pid, ps.pr_psargs, t);
345f971a346SBryan Cantrill }
346f971a346SBryan Cantrill
3477c478bd9Sstevel@tonic-gate if (rflag) {
3487c478bd9Sstevel@tonic-gate if (Pstate(Pr) == PS_DEAD || (arg->pflags & PR_STOPPED)) {
349*ed093b41SRobert Mustacchi dumpregs(psp->pr_reg, is64);
350*ed093b41SRobert Mustacchi } else {
3517c478bd9Sstevel@tonic-gate (void) printf("\tNot stopped, can't show registers\n");
352*ed093b41SRobert Mustacchi }
3537c478bd9Sstevel@tonic-gate }
3547c478bd9Sstevel@tonic-gate
3557c478bd9Sstevel@tonic-gate return (0);
3567c478bd9Sstevel@tonic-gate }
3577c478bd9Sstevel@tonic-gate
3587c478bd9Sstevel@tonic-gate static char *
prflags(int arg)3597c478bd9Sstevel@tonic-gate prflags(int arg)
3607c478bd9Sstevel@tonic-gate {
3617c478bd9Sstevel@tonic-gate static char code_buf[200];
3627c478bd9Sstevel@tonic-gate char *str = code_buf;
3637c478bd9Sstevel@tonic-gate
3647c478bd9Sstevel@tonic-gate if (arg == 0)
3657c478bd9Sstevel@tonic-gate return ("0");
3667c478bd9Sstevel@tonic-gate
3677c478bd9Sstevel@tonic-gate if (arg & ~ALLFLAGS)
3687c478bd9Sstevel@tonic-gate (void) sprintf(str, "0x%x", arg & ~ALLFLAGS);
3697c478bd9Sstevel@tonic-gate else
3707c478bd9Sstevel@tonic-gate *str = '\0';
3717c478bd9Sstevel@tonic-gate
3727c478bd9Sstevel@tonic-gate /*
3737c478bd9Sstevel@tonic-gate * Display the semi-permanent lwp flags first.
3747c478bd9Sstevel@tonic-gate */
3757c478bd9Sstevel@tonic-gate if (arg & PR_DAEMON) /* daemons are always detached so */
3767c478bd9Sstevel@tonic-gate (void) strcat(str, "|DAEMON");
3777c478bd9Sstevel@tonic-gate else if (arg & PR_DETACH) /* report detach only if non-daemon */
3787c478bd9Sstevel@tonic-gate (void) strcat(str, "|DETACH");
3797c478bd9Sstevel@tonic-gate
3807c478bd9Sstevel@tonic-gate if (arg & PR_STOPPED)
3817c478bd9Sstevel@tonic-gate (void) strcat(str, "|STOPPED");
3827c478bd9Sstevel@tonic-gate if (arg & PR_ISTOP)
3837c478bd9Sstevel@tonic-gate (void) strcat(str, "|ISTOP");
3847c478bd9Sstevel@tonic-gate if (arg & PR_DSTOP)
3857c478bd9Sstevel@tonic-gate (void) strcat(str, "|DSTOP");
3867c478bd9Sstevel@tonic-gate #if 0 /* displayed elsewhere */
3877c478bd9Sstevel@tonic-gate if (arg & PR_ASLEEP)
3887c478bd9Sstevel@tonic-gate (void) strcat(str, "|ASLEEP");
3897c478bd9Sstevel@tonic-gate #endif
3907c478bd9Sstevel@tonic-gate if (arg & PR_PCINVAL)
3917c478bd9Sstevel@tonic-gate (void) strcat(str, "|PCINVAL");
3927c478bd9Sstevel@tonic-gate if (arg & PR_STEP)
3937c478bd9Sstevel@tonic-gate (void) strcat(str, "|STEP");
3947c478bd9Sstevel@tonic-gate if (arg & PR_AGENT)
3957c478bd9Sstevel@tonic-gate (void) strcat(str, "|AGENT");
3967c478bd9Sstevel@tonic-gate if (arg & PR_ISSYS)
3977c478bd9Sstevel@tonic-gate (void) strcat(str, "|ISSYS");
3987c478bd9Sstevel@tonic-gate if (arg & PR_VFORKP)
3997c478bd9Sstevel@tonic-gate (void) strcat(str, "|VFORKP");
4007c478bd9Sstevel@tonic-gate if (arg & PR_ORPHAN)
4017c478bd9Sstevel@tonic-gate (void) strcat(str, "|ORPHAN");
402657b1f3dSraf if (arg & PR_NOSIGCHLD)
403657b1f3dSraf (void) strcat(str, "|NOSIGCHLD");
404657b1f3dSraf if (arg & PR_WAITPID)
405657b1f3dSraf (void) strcat(str, "|WAITPID");
4067c478bd9Sstevel@tonic-gate if (arg & PR_FORK)
4077c478bd9Sstevel@tonic-gate (void) strcat(str, "|FORK");
4087c478bd9Sstevel@tonic-gate if (arg & PR_RLC)
4097c478bd9Sstevel@tonic-gate (void) strcat(str, "|RLC");
4107c478bd9Sstevel@tonic-gate if (arg & PR_KLC)
4117c478bd9Sstevel@tonic-gate (void) strcat(str, "|KLC");
4127c478bd9Sstevel@tonic-gate if (arg & PR_ASYNC)
4137c478bd9Sstevel@tonic-gate (void) strcat(str, "|ASYNC");
4147c478bd9Sstevel@tonic-gate if (arg & PR_BPTADJ)
4157c478bd9Sstevel@tonic-gate (void) strcat(str, "|BPTADJ");
4167c478bd9Sstevel@tonic-gate if (arg & PR_MSACCT)
4177c478bd9Sstevel@tonic-gate (void) strcat(str, "|MSACCT");
4187c478bd9Sstevel@tonic-gate if (arg & PR_MSFORK)
4197c478bd9Sstevel@tonic-gate (void) strcat(str, "|MSFORK");
4207c478bd9Sstevel@tonic-gate if (arg & PR_PTRACE)
4217c478bd9Sstevel@tonic-gate (void) strcat(str, "|PTRACE");
4227c478bd9Sstevel@tonic-gate
4237c478bd9Sstevel@tonic-gate if (*str == '|')
4247c478bd9Sstevel@tonic-gate str++;
4257c478bd9Sstevel@tonic-gate
4267c478bd9Sstevel@tonic-gate return (str);
4277c478bd9Sstevel@tonic-gate }
4287c478bd9Sstevel@tonic-gate
4297c478bd9Sstevel@tonic-gate static char *
prwhy(int why)4307c478bd9Sstevel@tonic-gate prwhy(int why)
4317c478bd9Sstevel@tonic-gate {
4327c478bd9Sstevel@tonic-gate static char buf[20];
4337c478bd9Sstevel@tonic-gate char *str;
4347c478bd9Sstevel@tonic-gate
4357c478bd9Sstevel@tonic-gate switch (why) {
4367c478bd9Sstevel@tonic-gate case PR_REQUESTED:
4377c478bd9Sstevel@tonic-gate str = "PR_REQUESTED";
4387c478bd9Sstevel@tonic-gate break;
4397c478bd9Sstevel@tonic-gate case PR_SIGNALLED:
4407c478bd9Sstevel@tonic-gate str = "PR_SIGNALLED";
4417c478bd9Sstevel@tonic-gate break;
4427c478bd9Sstevel@tonic-gate case PR_SYSENTRY:
4437c478bd9Sstevel@tonic-gate str = "PR_SYSENTRY";
4447c478bd9Sstevel@tonic-gate break;
4457c478bd9Sstevel@tonic-gate case PR_SYSEXIT:
4467c478bd9Sstevel@tonic-gate str = "PR_SYSEXIT";
4477c478bd9Sstevel@tonic-gate break;
4487c478bd9Sstevel@tonic-gate case PR_JOBCONTROL:
4497c478bd9Sstevel@tonic-gate str = "PR_JOBCONTROL";
4507c478bd9Sstevel@tonic-gate break;
4517c478bd9Sstevel@tonic-gate case PR_FAULTED:
4527c478bd9Sstevel@tonic-gate str = "PR_FAULTED";
4537c478bd9Sstevel@tonic-gate break;
4547c478bd9Sstevel@tonic-gate case PR_SUSPENDED:
4557c478bd9Sstevel@tonic-gate str = "PR_SUSPENDED";
4567c478bd9Sstevel@tonic-gate break;
4577c478bd9Sstevel@tonic-gate default:
4587c478bd9Sstevel@tonic-gate str = buf;
4597c478bd9Sstevel@tonic-gate (void) sprintf(str, "%d", why);
4607c478bd9Sstevel@tonic-gate break;
4617c478bd9Sstevel@tonic-gate }
4627c478bd9Sstevel@tonic-gate
4637c478bd9Sstevel@tonic-gate return (str);
4647c478bd9Sstevel@tonic-gate }
4657c478bd9Sstevel@tonic-gate
4667c478bd9Sstevel@tonic-gate static char *
prwhat(int why,int what)4677c478bd9Sstevel@tonic-gate prwhat(int why, int what)
4687c478bd9Sstevel@tonic-gate {
4697c478bd9Sstevel@tonic-gate static char buf[32];
4707c478bd9Sstevel@tonic-gate char *str;
4717c478bd9Sstevel@tonic-gate
4727c478bd9Sstevel@tonic-gate switch (why) {
4737c478bd9Sstevel@tonic-gate case PR_SIGNALLED:
4747c478bd9Sstevel@tonic-gate case PR_JOBCONTROL:
4757c478bd9Sstevel@tonic-gate str = proc_signame(what, buf, sizeof (buf));
4767c478bd9Sstevel@tonic-gate break;
4777c478bd9Sstevel@tonic-gate case PR_SYSENTRY:
4787c478bd9Sstevel@tonic-gate case PR_SYSEXIT:
4797c478bd9Sstevel@tonic-gate str = proc_sysname(what, buf, sizeof (buf));
4807c478bd9Sstevel@tonic-gate break;
4817c478bd9Sstevel@tonic-gate case PR_FAULTED:
4827c478bd9Sstevel@tonic-gate str = proc_fltname(what, buf, sizeof (buf));
4837c478bd9Sstevel@tonic-gate break;
4847c478bd9Sstevel@tonic-gate default:
4857c478bd9Sstevel@tonic-gate (void) sprintf(str = buf, "%d", what);
4867c478bd9Sstevel@tonic-gate break;
4877c478bd9Sstevel@tonic-gate }
4887c478bd9Sstevel@tonic-gate
4897c478bd9Sstevel@tonic-gate return (str);
4907c478bd9Sstevel@tonic-gate }
4917c478bd9Sstevel@tonic-gate
4927c478bd9Sstevel@tonic-gate #if defined(__amd64)
4937c478bd9Sstevel@tonic-gate static const char * const regname[NPRGREG] = {
4947c478bd9Sstevel@tonic-gate "%r15", "%r14", "%r13", "%r12", "%r11", "%r10", " %r9", " %r8",
4957c478bd9Sstevel@tonic-gate "%rdi", "%rsi", "%rbp", "%rbx", "%rdx", "%rcx", "%rax", "%trapno",
4967c478bd9Sstevel@tonic-gate "%err", "%rip", " %cs", "%rfl", "%rsp", " %ss", " %fs", " %gs",
4977c478bd9Sstevel@tonic-gate " %es", " %ds", "%fsbase", "%gsbase"
4987c478bd9Sstevel@tonic-gate };
4997c478bd9Sstevel@tonic-gate
5007c478bd9Sstevel@tonic-gate static const char * const regname32[NPRGREG32] = {
5017c478bd9Sstevel@tonic-gate " %gs", " %fs", " %es", " %ds", "%edi", "%esi", "%ebp", "%esp",
5027c478bd9Sstevel@tonic-gate "%ebx", "%edx", "%ecx", "%eax", "%trapno", "%err", "%eip", " %cs",
5037c478bd9Sstevel@tonic-gate "%efl", "%uesp", " %ss"
5047c478bd9Sstevel@tonic-gate };
5057c478bd9Sstevel@tonic-gate
5067c478bd9Sstevel@tonic-gate /* XX64 Do we want to expose this through libproc */
5077c478bd9Sstevel@tonic-gate void
prgregset_n_to_32(const prgreg_t * src,prgreg32_t * dst)5087c478bd9Sstevel@tonic-gate prgregset_n_to_32(const prgreg_t *src, prgreg32_t *dst)
5097c478bd9Sstevel@tonic-gate {
5107c478bd9Sstevel@tonic-gate bzero(dst, NPRGREG32 * sizeof (prgreg32_t));
5117c478bd9Sstevel@tonic-gate dst[GS] = src[REG_GS];
5127c478bd9Sstevel@tonic-gate dst[FS] = src[REG_FS];
5137c478bd9Sstevel@tonic-gate dst[DS] = src[REG_DS];
5147c478bd9Sstevel@tonic-gate dst[ES] = src[REG_ES];
5157c478bd9Sstevel@tonic-gate dst[EDI] = src[REG_RDI];
5167c478bd9Sstevel@tonic-gate dst[ESI] = src[REG_RSI];
5177c478bd9Sstevel@tonic-gate dst[EBP] = src[REG_RBP];
5187c478bd9Sstevel@tonic-gate dst[EBX] = src[REG_RBX];
5197c478bd9Sstevel@tonic-gate dst[EDX] = src[REG_RDX];
5207c478bd9Sstevel@tonic-gate dst[ECX] = src[REG_RCX];
5217c478bd9Sstevel@tonic-gate dst[EAX] = src[REG_RAX];
5227c478bd9Sstevel@tonic-gate dst[TRAPNO] = src[REG_TRAPNO];
5237c478bd9Sstevel@tonic-gate dst[ERR] = src[REG_ERR];
5247c478bd9Sstevel@tonic-gate dst[EIP] = src[REG_RIP];
5257c478bd9Sstevel@tonic-gate dst[CS] = src[REG_CS];
5267c478bd9Sstevel@tonic-gate dst[EFL] = src[REG_RFL];
5277c478bd9Sstevel@tonic-gate dst[UESP] = src[REG_RSP];
5287c478bd9Sstevel@tonic-gate dst[SS] = src[REG_SS];
5297c478bd9Sstevel@tonic-gate }
5307c478bd9Sstevel@tonic-gate
5317c478bd9Sstevel@tonic-gate #elif defined(__i386)
5327c478bd9Sstevel@tonic-gate static const char * const regname[NPRGREG] = {
5337c478bd9Sstevel@tonic-gate " %gs", " %fs", " %es", " %ds", "%edi", "%esi", "%ebp", "%esp",
5347c478bd9Sstevel@tonic-gate "%ebx", "%edx", "%ecx", "%eax", "%trapno", "%err", "%eip", " %cs",
5357c478bd9Sstevel@tonic-gate "%efl", "%uesp", " %ss"
5367c478bd9Sstevel@tonic-gate };
5377c478bd9Sstevel@tonic-gate #endif /* __i386 */
5387c478bd9Sstevel@tonic-gate
539*ed093b41SRobert Mustacchi #if defined(__amd64)
5407c478bd9Sstevel@tonic-gate static void
dumpregs32(const prgregset_t reg)5417c478bd9Sstevel@tonic-gate dumpregs32(const prgregset_t reg)
5427c478bd9Sstevel@tonic-gate {
5437c478bd9Sstevel@tonic-gate prgregset32_t reg32;
5447c478bd9Sstevel@tonic-gate int i;
5457c478bd9Sstevel@tonic-gate
5467c478bd9Sstevel@tonic-gate prgregset_n_to_32(reg, reg32);
5477c478bd9Sstevel@tonic-gate
5487c478bd9Sstevel@tonic-gate for (i = 0; i < NPRGREG32; i++) {
5497c478bd9Sstevel@tonic-gate (void) printf(" %s = 0x%.8X",
550bc5ed3ddSRoger A. Faulkner regname32[i], reg32[i]);
5517c478bd9Sstevel@tonic-gate if ((i+1) % 4 == 0)
5527c478bd9Sstevel@tonic-gate (void) putchar('\n');
5537c478bd9Sstevel@tonic-gate }
5547c478bd9Sstevel@tonic-gate if (i % 4 != 0)
5557c478bd9Sstevel@tonic-gate (void) putchar('\n');
5567c478bd9Sstevel@tonic-gate }
557*ed093b41SRobert Mustacchi #endif /* __amd64 */
5587c478bd9Sstevel@tonic-gate
5597c478bd9Sstevel@tonic-gate static void
dumpregs(const prgregset_t reg,int is64)5607c478bd9Sstevel@tonic-gate dumpregs(const prgregset_t reg, int is64)
5617c478bd9Sstevel@tonic-gate {
5627c478bd9Sstevel@tonic-gate int width = is64? 16 : 8;
5637c478bd9Sstevel@tonic-gate int cols = is64? 2 : 4;
5647c478bd9Sstevel@tonic-gate int i;
5657c478bd9Sstevel@tonic-gate
566*ed093b41SRobert Mustacchi #if defined(__amd64)
5677c478bd9Sstevel@tonic-gate if (!is64) {
5687c478bd9Sstevel@tonic-gate dumpregs32(reg);
5697c478bd9Sstevel@tonic-gate return;
5707c478bd9Sstevel@tonic-gate }
571*ed093b41SRobert Mustacchi #endif /* __amd64 */
5727c478bd9Sstevel@tonic-gate
5737c478bd9Sstevel@tonic-gate for (i = 0; i < NPRGREG; i++) {
5747c478bd9Sstevel@tonic-gate (void) printf(" %s = 0x%.*lX",
575bc5ed3ddSRoger A. Faulkner regname[i], width, (long)reg[i]);
5767c478bd9Sstevel@tonic-gate if ((i+1) % cols == 0)
5777c478bd9Sstevel@tonic-gate (void) putchar('\n');
5787c478bd9Sstevel@tonic-gate }
5797c478bd9Sstevel@tonic-gate if (i % cols != 0)
5807c478bd9Sstevel@tonic-gate (void) putchar('\n');
5817c478bd9Sstevel@tonic-gate }
582