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
51885Sraf  * Common Development and Distribution License (the "License").
61885Sraf  * 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  */
211885Sraf 
220Sstevel@tonic-gate /*
239170SRoger.Faulkner@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #include <sys/mdb_modapi.h>
28*10610SJonathan.Adams@Sun.COM #include <mdb/mdb_whatis.h>
290Sstevel@tonic-gate #include <procfs.h>
300Sstevel@tonic-gate #include <ucontext.h>
310Sstevel@tonic-gate #include <siginfo.h>
320Sstevel@tonic-gate #include <signal.h>
330Sstevel@tonic-gate #include <setjmp.h>
340Sstevel@tonic-gate #include <string.h>
350Sstevel@tonic-gate #include <thr_uberdata.h>
360Sstevel@tonic-gate 
370Sstevel@tonic-gate static const char *
380Sstevel@tonic-gate stack_flags(const stack_t *sp)
390Sstevel@tonic-gate {
400Sstevel@tonic-gate 	static char buf[32];
410Sstevel@tonic-gate 
420Sstevel@tonic-gate 	if (sp->ss_flags == 0)
430Sstevel@tonic-gate 		(void) strcpy(buf, " 0");
440Sstevel@tonic-gate 	else if (sp->ss_flags & ~(SS_ONSTACK | SS_DISABLE))
450Sstevel@tonic-gate 		(void) mdb_snprintf(buf, sizeof (buf), " 0x%x", sp->ss_flags);
460Sstevel@tonic-gate 	else {
470Sstevel@tonic-gate 		buf[0] = '\0';
480Sstevel@tonic-gate 		if (sp->ss_flags & SS_ONSTACK)
490Sstevel@tonic-gate 			(void) strcat(buf, "|ONSTACK");
500Sstevel@tonic-gate 		if (sp->ss_flags & SS_DISABLE)
510Sstevel@tonic-gate 			(void) strcat(buf, "|DISABLE");
520Sstevel@tonic-gate 	}
530Sstevel@tonic-gate 
540Sstevel@tonic-gate 	return (buf + 1);
550Sstevel@tonic-gate }
560Sstevel@tonic-gate 
570Sstevel@tonic-gate /*ARGSUSED*/
580Sstevel@tonic-gate static int
590Sstevel@tonic-gate d_jmp_buf(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
600Sstevel@tonic-gate {
610Sstevel@tonic-gate 	jmp_buf jb;
620Sstevel@tonic-gate 	const ulong_t *b = (const ulong_t *)jb;
630Sstevel@tonic-gate 
640Sstevel@tonic-gate 	if (argc != 0)
650Sstevel@tonic-gate 		return (DCMD_USAGE);
660Sstevel@tonic-gate 
670Sstevel@tonic-gate 	if (mdb_vread(&jb, sizeof (jb), addr) != sizeof (jb)) {
680Sstevel@tonic-gate 		mdb_warn("failed to read jmp_buf at %p", addr);
690Sstevel@tonic-gate 		return (DCMD_ERR);
700Sstevel@tonic-gate 	}
710Sstevel@tonic-gate 
720Sstevel@tonic-gate #if defined(__sparc)
730Sstevel@tonic-gate 	mdb_printf("  %%sp = 0x%lx\n", b[1]);
740Sstevel@tonic-gate 	mdb_printf("  %%pc = 0x%lx %lA\n", b[2], b[2]);
750Sstevel@tonic-gate 	mdb_printf("  %%fp = 0x%lx\n", b[3]);
760Sstevel@tonic-gate 	mdb_printf("  %%i7 = 0x%lx %lA\n", b[4], b[4]);
770Sstevel@tonic-gate #elif defined(__amd64)
780Sstevel@tonic-gate 	mdb_printf("  %%rbx = 0x%lx\n", b[0]);
790Sstevel@tonic-gate 	mdb_printf("  %%r12 = 0x%lx\n", b[1]);
800Sstevel@tonic-gate 	mdb_printf("  %%r13 = 0x%lx\n", b[2]);
810Sstevel@tonic-gate 	mdb_printf("  %%r14 = 0x%lx\n", b[3]);
820Sstevel@tonic-gate 	mdb_printf("  %%r15 = 0x%lx\n", b[4]);
830Sstevel@tonic-gate 	mdb_printf("  %%rbp = 0x%lx\n", b[5]);
840Sstevel@tonic-gate 	mdb_printf("  %%rsp = 0x%lx\n", b[6]);
850Sstevel@tonic-gate 	mdb_printf("  %%rip = 0x%lx %lA\n", b[7], b[7]);
860Sstevel@tonic-gate #elif defined(__i386)
870Sstevel@tonic-gate 	mdb_printf("  %%ebx = 0x%lx\n", b[0]);
880Sstevel@tonic-gate 	mdb_printf("  %%esi = 0x%lx\n", b[1]);
890Sstevel@tonic-gate 	mdb_printf("  %%edi = 0x%lx\n", b[2]);
900Sstevel@tonic-gate 	mdb_printf("  %%ebp = 0x%lx\n", b[3]);
910Sstevel@tonic-gate 	mdb_printf("  %%esp = 0x%lx\n", b[4]);
920Sstevel@tonic-gate 	mdb_printf("  %%eip = 0x%lx %lA\n", b[5], b[5]);
930Sstevel@tonic-gate #endif
940Sstevel@tonic-gate 	return (DCMD_OK);
950Sstevel@tonic-gate }
960Sstevel@tonic-gate 
970Sstevel@tonic-gate /*ARGSUSED*/
980Sstevel@tonic-gate static int
990Sstevel@tonic-gate d_ucontext(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1000Sstevel@tonic-gate {
1010Sstevel@tonic-gate 	ucontext_t uc;
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate 	if (argc != 0)
1040Sstevel@tonic-gate 		return (DCMD_USAGE);
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate 	if (mdb_vread(&uc, sizeof (uc), addr) != sizeof (uc)) {
1070Sstevel@tonic-gate 		mdb_warn("failed to read ucontext at %p", addr);
1080Sstevel@tonic-gate 		return (DCMD_ERR);
1090Sstevel@tonic-gate 	}
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 	mdb_printf("  flags    = 0x%lx\n", uc.uc_flags);
1120Sstevel@tonic-gate 	mdb_printf("  link     = 0x%p\n", uc.uc_link);
1130Sstevel@tonic-gate 	mdb_printf("  sigmask  = 0x%08x 0x%08x 0x%08x 0x%08x\n",
1140Sstevel@tonic-gate 	    uc.uc_sigmask.__sigbits[0], uc.uc_sigmask.__sigbits[1],
1150Sstevel@tonic-gate 	    uc.uc_sigmask.__sigbits[2], uc.uc_sigmask.__sigbits[3]);
1160Sstevel@tonic-gate 	mdb_printf("  stack    = sp 0x%p size 0x%lx flags %s\n",
1170Sstevel@tonic-gate 	    uc.uc_stack.ss_sp, uc.uc_stack.ss_size, stack_flags(&uc.uc_stack));
1180Sstevel@tonic-gate 	mdb_printf("  mcontext = 0x%p\n",
1190Sstevel@tonic-gate 	    addr + OFFSETOF(ucontext_t, uc_mcontext));
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate 	return (DCMD_OK);
1220Sstevel@tonic-gate }
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate /*ARGSUSED*/
1250Sstevel@tonic-gate static int
1260Sstevel@tonic-gate d_sigjmp_buf(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1270Sstevel@tonic-gate {
1280Sstevel@tonic-gate #if defined(__sparc)
1290Sstevel@tonic-gate 	struct {
1300Sstevel@tonic-gate 		int sjs_flags;
1310Sstevel@tonic-gate 		greg_t sjs_sp;
1320Sstevel@tonic-gate 		greg_t sjs_pc;
1330Sstevel@tonic-gate 		greg_t sjs_fp;
1340Sstevel@tonic-gate 		greg_t sjs_i7;
1350Sstevel@tonic-gate 		ucontext_t *sjs_uclink;
1360Sstevel@tonic-gate 		ulong_t sjs_pad[_JBLEN - 6];
1370Sstevel@tonic-gate 		sigset_t sjs_sigmask;
1380Sstevel@tonic-gate #if defined(_LP64)
1390Sstevel@tonic-gate 		ulong_t sjs_pad1[2];
1400Sstevel@tonic-gate #endif
1410Sstevel@tonic-gate 		stack_t sjs_stack;
1420Sstevel@tonic-gate 	} s;
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 	if (argc != 0)
1450Sstevel@tonic-gate 		return (DCMD_USAGE);
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 	if (mdb_vread(&s, sizeof (s), addr) != sizeof (s)) {
1480Sstevel@tonic-gate 		mdb_warn("failed to read sigjmp_buf at %p", addr);
1490Sstevel@tonic-gate 		return (DCMD_ERR);
1500Sstevel@tonic-gate 	}
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 	mdb_printf("  flags  = 0x%x\n", s.sjs_flags);
1530Sstevel@tonic-gate 	mdb_printf("  %%sp    = 0x%lx %lA\n", s.sjs_sp, s.sjs_sp);
1540Sstevel@tonic-gate 	mdb_printf("  %%pc    = 0x%lx %lA\n", s.sjs_pc, s.sjs_pc);
1550Sstevel@tonic-gate 	mdb_printf("  %%fp    = 0x%lx %lA\n", s.sjs_fp, s.sjs_fp);
1560Sstevel@tonic-gate 	mdb_printf("  %%i7    = 0x%lx %lA\n", s.sjs_i7, s.sjs_i7);
1570Sstevel@tonic-gate 	mdb_printf("  uclink = %p\n", s.sjs_uclink);
1580Sstevel@tonic-gate 	mdb_printf("  sigset = 0x%08x 0x%08x 0x%08x 0x%08x\n",
1590Sstevel@tonic-gate 	    s.sjs_sigmask.__sigbits[0], s.sjs_sigmask.__sigbits[1],
1600Sstevel@tonic-gate 	    s.sjs_sigmask.__sigbits[2], s.sjs_sigmask.__sigbits[3]);
1610Sstevel@tonic-gate 	mdb_printf("  stack  = sp 0x%p size 0x%lx flags %s\n",
1620Sstevel@tonic-gate 	    s.sjs_stack.ss_sp, s.sjs_stack.ss_size, stack_flags(&s.sjs_stack));
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 	return (DCMD_OK);
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate #elif defined(__i386) || defined(__amd64)
1670Sstevel@tonic-gate 	return (d_ucontext(addr, flags, argc, argv));
1680Sstevel@tonic-gate #endif
1690Sstevel@tonic-gate }
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate /*ARGSUSED*/
1720Sstevel@tonic-gate static int
1730Sstevel@tonic-gate d_siginfo(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1740Sstevel@tonic-gate {
1750Sstevel@tonic-gate 	static const char *const msname[] = {
1760Sstevel@tonic-gate 		"USER", "SYSTEM", "TRAP", "TFAULT", "DFAULT", "KFAULT",
1770Sstevel@tonic-gate 		"USER_LOCK", "SLEEP", "WAIT_CPU", "STOPPED"
1780Sstevel@tonic-gate 	};
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 	char signame[SIG2STR_MAX];
1810Sstevel@tonic-gate 	siginfo_t si;
1820Sstevel@tonic-gate 	int i;
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate 	if (argc != 0)
1850Sstevel@tonic-gate 		return (DCMD_USAGE);
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 	if (mdb_vread(&si, sizeof (si), addr) != sizeof (si)) {
1880Sstevel@tonic-gate 		mdb_warn("failed to read siginfo at %p", addr);
1890Sstevel@tonic-gate 		return (DCMD_ERR);
1900Sstevel@tonic-gate 	}
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 	if (sig2str(si.si_signo, signame) == -1)
1930Sstevel@tonic-gate 		(void) strcpy(signame, "unknown");
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 	mdb_printf("  signal %5d (%s)\n", si.si_signo, signame);
1960Sstevel@tonic-gate 	mdb_printf("  code   %5d (", si.si_code);
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate 	switch (si.si_code) {
1990Sstevel@tonic-gate 	case SI_NOINFO:
2000Sstevel@tonic-gate 		mdb_printf("no info");
2010Sstevel@tonic-gate 		break;
2020Sstevel@tonic-gate 	case SI_DTRACE:
2030Sstevel@tonic-gate 		mdb_printf("from DTrace raise() action");
2040Sstevel@tonic-gate 		break;
2050Sstevel@tonic-gate 	case SI_RCTL:
2060Sstevel@tonic-gate 		mdb_printf("from rctl action");
2070Sstevel@tonic-gate 		break;
2080Sstevel@tonic-gate 	case SI_USER:
2090Sstevel@tonic-gate 		mdb_printf("user generated via kill");
2100Sstevel@tonic-gate 		break;
2110Sstevel@tonic-gate 	case SI_LWP:
2120Sstevel@tonic-gate 		mdb_printf("user generated via lwp_kill");
2130Sstevel@tonic-gate 		break;
2140Sstevel@tonic-gate 	case SI_QUEUE:
2150Sstevel@tonic-gate 		mdb_printf("user generated via sigqueue");
2160Sstevel@tonic-gate 		break;
2170Sstevel@tonic-gate 	case SI_TIMER:
2180Sstevel@tonic-gate 		mdb_printf("from timer expiration");
2190Sstevel@tonic-gate 		break;
2200Sstevel@tonic-gate 	case SI_ASYNCIO:
2210Sstevel@tonic-gate 		mdb_printf("from async i/o completion");
2220Sstevel@tonic-gate 		break;
2230Sstevel@tonic-gate 	case SI_MESGQ:
2240Sstevel@tonic-gate 		mdb_printf("from message arrival");
2250Sstevel@tonic-gate 		break;
2260Sstevel@tonic-gate 	default:
2270Sstevel@tonic-gate 		if (SI_FROMUSER(&si))
2280Sstevel@tonic-gate 			mdb_printf("from user process");
2290Sstevel@tonic-gate 		else
2300Sstevel@tonic-gate 			mdb_printf("from kernel");
2310Sstevel@tonic-gate 	}
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate 	mdb_printf(")\n  errno  %5d (%s)\n",
2340Sstevel@tonic-gate 	    si.si_errno, strerror(si.si_errno));
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate 	if (si.si_code == SI_USER || si.si_code == SI_QUEUE) {
2370Sstevel@tonic-gate 		mdb_printf("  signal sent from PID %d (uid %d)\n",
2380Sstevel@tonic-gate 		    si.si_pid, si.si_uid);
2390Sstevel@tonic-gate 	}
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 	if (si.si_code == SI_QUEUE) {
2420Sstevel@tonic-gate 		mdb_printf("  signal value = 0t%d / %p\n",
2430Sstevel@tonic-gate 		    si.si_value.sival_int, si.si_value.sival_ptr);
2440Sstevel@tonic-gate 	}
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate 	switch (si.si_signo) {
2470Sstevel@tonic-gate 	case SIGCLD:
2480Sstevel@tonic-gate 		mdb_printf("  signal sent from child PID %d (uid %d)\n",
2490Sstevel@tonic-gate 		    si.si_pid, si.si_uid);
2500Sstevel@tonic-gate 		mdb_printf("  usr time = 0t%ld ticks, sys time = 0t%ld ticks\n",
2510Sstevel@tonic-gate 		    si.si_utime, si.si_stime);
2520Sstevel@tonic-gate 		mdb_printf("  wait status = 0x%x\n", si.si_status);
2530Sstevel@tonic-gate 		break;
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate 	case SIGSEGV:
2560Sstevel@tonic-gate 	case SIGBUS:
2570Sstevel@tonic-gate 	case SIGILL:
2580Sstevel@tonic-gate 	case SIGTRAP:
2590Sstevel@tonic-gate 	case SIGFPE:
2600Sstevel@tonic-gate 		mdb_printf("  fault address = 0x%p\n  trapno = %d\n",
2610Sstevel@tonic-gate 		    si.si_addr, si.si_trapno);
2620Sstevel@tonic-gate 		mdb_printf("  instruction address = 0x%p %lA\n",
2630Sstevel@tonic-gate 		    si.si_pc, si.si_pc);
2640Sstevel@tonic-gate 		break;
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate 	case SIGPOLL:
2670Sstevel@tonic-gate 	case SIGXFSZ:
2680Sstevel@tonic-gate 		mdb_printf("  fd = %d  band = 0x%lx\n",
2690Sstevel@tonic-gate 		    si.si_fd, si.si_band);
2700Sstevel@tonic-gate 		break;
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate 	case SIGPROF:
2730Sstevel@tonic-gate 		mdb_printf("  last fault address = 0x%p fault type = %d\n",
2740Sstevel@tonic-gate 		    si.si_faddr, si.si_fault);
2750Sstevel@tonic-gate 		mdb_printf("  timestamp = 0t%ld sec 0t%ld nsec\n",
2760Sstevel@tonic-gate 		    si.si_tstamp.tv_sec, si.si_tstamp.tv_nsec);
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate 		if (si.__data.__prof.__syscall != 0) {
2790Sstevel@tonic-gate 			mdb_printf("  system call %d (", si.si_syscall);
2800Sstevel@tonic-gate 			if (si.si_nsysarg > 0) {
2810Sstevel@tonic-gate 				mdb_printf("%lx", si.si_sysarg[0]);
2820Sstevel@tonic-gate 				for (i = 1; i < si.si_nsysarg; i++)
2830Sstevel@tonic-gate 					mdb_printf(", %lx", si.si_sysarg[i]);
2840Sstevel@tonic-gate 			}
2850Sstevel@tonic-gate 			mdb_printf("  )\n");
2860Sstevel@tonic-gate 		}
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate 		for (i = 0; i < sizeof (msname) / sizeof (msname[0]); i++) {
2890Sstevel@tonic-gate 			mdb_printf("  mstate[\"%s\"] = %d\n",
2900Sstevel@tonic-gate 			    msname[i], si.si_mstate[i]);
2910Sstevel@tonic-gate 		}
2920Sstevel@tonic-gate 		break;
2930Sstevel@tonic-gate 	}
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate 	return (DCMD_OK);
2960Sstevel@tonic-gate }
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate static int
2990Sstevel@tonic-gate uc_walk_step(mdb_walk_state_t *wsp)
3000Sstevel@tonic-gate {
3010Sstevel@tonic-gate 	uintptr_t addr = wsp->walk_addr;
3020Sstevel@tonic-gate 	ucontext_t uc;
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate 	if (addr == NULL)
3050Sstevel@tonic-gate 		return (WALK_DONE);
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate 	if (mdb_vread(&uc, sizeof (uc), addr) != sizeof (uc)) {
3080Sstevel@tonic-gate 		mdb_warn("failed to read ucontext at %p", addr);
3090Sstevel@tonic-gate 		return (WALK_ERR);
3100Sstevel@tonic-gate 	}
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 	wsp->walk_addr = (uintptr_t)uc.uc_link;
3130Sstevel@tonic-gate 	return (wsp->walk_callback(addr, &uc, wsp->walk_cbdata));
3140Sstevel@tonic-gate }
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate static int
3170Sstevel@tonic-gate oldc_walk_init(mdb_walk_state_t *wsp)
3180Sstevel@tonic-gate {
3190Sstevel@tonic-gate 	ssize_t nbytes = mdb_get_xdata("lwpstatus", NULL, 0);
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate 	if (nbytes <= 0) {
3220Sstevel@tonic-gate 		mdb_warn("lwpstatus information not available");
3230Sstevel@tonic-gate 		return (WALK_ERR);
3240Sstevel@tonic-gate 	}
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate 	if (wsp->walk_addr != NULL) {
3270Sstevel@tonic-gate 		mdb_warn("walker only supports global walk\n");
3280Sstevel@tonic-gate 		return (WALK_ERR);
3290Sstevel@tonic-gate 	}
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate 	wsp->walk_addr = nbytes; /* Use walk_addr to track size */
3320Sstevel@tonic-gate 	wsp->walk_data = mdb_alloc(nbytes, UM_SLEEP);
3330Sstevel@tonic-gate 
3340Sstevel@tonic-gate 	if (mdb_get_xdata("lwpstatus", wsp->walk_data, nbytes) != nbytes) {
3350Sstevel@tonic-gate 		mdb_warn("failed to read lwpstatus information");
3360Sstevel@tonic-gate 		mdb_free(wsp->walk_data, nbytes);
3370Sstevel@tonic-gate 		return (WALK_ERR);
3380Sstevel@tonic-gate 	}
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate 	wsp->walk_arg = wsp->walk_data; /* Use walk_arg to track pointer */
3410Sstevel@tonic-gate 	return (WALK_NEXT);
3420Sstevel@tonic-gate }
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate static int
3450Sstevel@tonic-gate oldc_walk_step(mdb_walk_state_t *wsp)
3460Sstevel@tonic-gate {
3470Sstevel@tonic-gate 	const lwpstatus_t *lsp, *end;
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate 	end = (const lwpstatus_t *)((uintptr_t)wsp->walk_data + wsp->walk_addr);
3500Sstevel@tonic-gate 	lsp = wsp->walk_arg;
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate 	wsp->walk_arg = (void *)(lsp + 1);
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate 	if (lsp < end) {
3550Sstevel@tonic-gate 		uintptr_t addr = lsp->pr_oldcontext;
3560Sstevel@tonic-gate 		ucontext_t uc;
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate 		if (addr == NULL)
3590Sstevel@tonic-gate 			return (WALK_NEXT);
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate 		if (mdb_vread(&uc, sizeof (uc), addr) != sizeof (uc)) {
3620Sstevel@tonic-gate 			mdb_warn("failed to read ucontext at %p", addr);
3630Sstevel@tonic-gate 			return (WALK_NEXT);
3640Sstevel@tonic-gate 		}
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate 		return (wsp->walk_callback(addr, &uc, wsp->walk_cbdata));
3670Sstevel@tonic-gate 	}
3680Sstevel@tonic-gate 
3690Sstevel@tonic-gate 	return (WALK_DONE);
3700Sstevel@tonic-gate }
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate static void
3730Sstevel@tonic-gate oldc_walk_fini(mdb_walk_state_t *wsp)
3740Sstevel@tonic-gate {
3750Sstevel@tonic-gate 	mdb_free(wsp->walk_data, wsp->walk_addr); /* walk_addr has size */
3760Sstevel@tonic-gate }
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate /*
3790Sstevel@tonic-gate  * ==================== threads ==========================
3800Sstevel@tonic-gate  * These are the interfaces that used to require libthread.
3810Sstevel@tonic-gate  * Now, libthread has been folded into libc.
3820Sstevel@tonic-gate  * =======================================================
3830Sstevel@tonic-gate  */
3840Sstevel@tonic-gate 
3850Sstevel@tonic-gate /*
3860Sstevel@tonic-gate  * prt_addr() is called up to three times to generate arguments for
3870Sstevel@tonic-gate  * one call to mdb_printf().  We must return at least three different
3880Sstevel@tonic-gate  * pointers to static storage for consecutive calls to prt_addr().
3890Sstevel@tonic-gate  */
3900Sstevel@tonic-gate static const char *
3910Sstevel@tonic-gate prt_addr(void *addr, int pad)
3920Sstevel@tonic-gate {
3930Sstevel@tonic-gate 	static char buffer[4][24];
3940Sstevel@tonic-gate 	static int ix = 0;
3950Sstevel@tonic-gate 	char *buf;
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 	if (ix == 4)	/* use buffers in sequence: 0, 1, 2, 3 */
3980Sstevel@tonic-gate 		ix = 0;
3990Sstevel@tonic-gate 	buf = buffer[ix++];
4000Sstevel@tonic-gate 	if (addr == NULL)
4010Sstevel@tonic-gate 		return (pad? "<NULL>               " : "<NULL>");
4020Sstevel@tonic-gate 	else {
4030Sstevel@tonic-gate #ifdef _LP64
4040Sstevel@tonic-gate 		(void) mdb_snprintf(buf, sizeof (buffer[0]), "0x%016lx", addr);
4050Sstevel@tonic-gate 		if (pad)
4060Sstevel@tonic-gate 			(void) strcpy(buf + 18, "   ");
4070Sstevel@tonic-gate #else
4080Sstevel@tonic-gate 		(void) mdb_snprintf(buf, sizeof (buffer[0]), "0x%08lx", addr);
4090Sstevel@tonic-gate 		if (pad)
4100Sstevel@tonic-gate 			(void) strcpy(buf + 10, "           ");
4110Sstevel@tonic-gate #endif	/* _LP64 */
4120Sstevel@tonic-gate 		return (buf);
4130Sstevel@tonic-gate 	}
4140Sstevel@tonic-gate }
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate #define	HD(str)		mdb_printf("           " str "\n")
4170Sstevel@tonic-gate #define	OFFSTR		"+0x%-7lx "
4180Sstevel@tonic-gate #define	OFFSET(member)	((size_t)OFFSETOF(ulwp_t, member))
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate /*ARGSUSED*/
4210Sstevel@tonic-gate static int
4220Sstevel@tonic-gate d_ulwp(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
4230Sstevel@tonic-gate {
4240Sstevel@tonic-gate 	ulwp_t ulwp;
4250Sstevel@tonic-gate 
4260Sstevel@tonic-gate 	if (argc != 0 || !(flags & DCMD_ADDRSPEC))
4270Sstevel@tonic-gate 		return (DCMD_USAGE);
4280Sstevel@tonic-gate 
4290Sstevel@tonic-gate 	if (mdb_vread(&ulwp, sizeof (ulwp), addr) != sizeof (ulwp) &&
4300Sstevel@tonic-gate 	    (bzero(&ulwp, sizeof (ulwp)),
4310Sstevel@tonic-gate 	    mdb_vread(&ulwp, REPLACEMENT_SIZE, addr)) != REPLACEMENT_SIZE) {
4320Sstevel@tonic-gate 		mdb_warn("failed to read ulwp at 0x%p", addr);
4330Sstevel@tonic-gate 		return (DCMD_ERR);
4340Sstevel@tonic-gate 	}
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate 	mdb_printf("%#a\n", addr);
4370Sstevel@tonic-gate 
4380Sstevel@tonic-gate 	HD("self                  uberdata");
4390Sstevel@tonic-gate 	mdb_printf(OFFSTR "%s %s\n",
4406515Sraf 	    OFFSET(ul_self),
4416515Sraf 	    prt_addr(ulwp.ul_self, 1),
4426515Sraf 	    prt_addr(ulwp.ul_uberdata, 0));
4430Sstevel@tonic-gate 
4440Sstevel@tonic-gate 	HD("tlsent                ntlsent");
4450Sstevel@tonic-gate 	mdb_printf(OFFSTR "%s %ld\n",
4466515Sraf 	    OFFSET(ul_tlsent),
4476515Sraf 	    prt_addr(ulwp.ul_tlsent, 1),
4486515Sraf 	    ulwp.ul_ntlsent);
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate 	HD("forw                  back                  next");
4510Sstevel@tonic-gate 	mdb_printf(OFFSTR "%s %s %s\n",
4526515Sraf 	    OFFSET(ul_forw),
4536515Sraf 	    prt_addr(ulwp.ul_forw, 1),
4546515Sraf 	    prt_addr(ulwp.ul_back, 1),
4556515Sraf 	    prt_addr(ulwp.ul_next, 0));
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate 	HD("hash                  rval                  stk");
4580Sstevel@tonic-gate 	mdb_printf(OFFSTR "%s %s %s\n",
4596515Sraf 	    OFFSET(ul_hash),
4606515Sraf 	    prt_addr(ulwp.ul_hash, 1),
4616515Sraf 	    prt_addr(ulwp.ul_rval, 1),
4626515Sraf 	    prt_addr(ulwp.ul_stk, 0));
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate 	HD("mapsiz     guardsize  stktop                stksiz");
4650Sstevel@tonic-gate 	mdb_printf(OFFSTR "%-10ld %-10ld %s %ld\n",
4666515Sraf 	    OFFSET(ul_mapsiz),
4676515Sraf 	    ulwp.ul_mapsiz,
4686515Sraf 	    ulwp.ul_guardsize,
4696515Sraf 	    prt_addr((void *)ulwp.ul_stktop, 1),
4706515Sraf 	    ulwp.ul_stksiz);
4710Sstevel@tonic-gate 
4720Sstevel@tonic-gate 	HD("ustack.ss_sp          ustack.ss_size        ustack.ss_flags");
4730Sstevel@tonic-gate 	mdb_printf(OFFSTR "%s %-21ld %s\n",
4746515Sraf 	    OFFSET(ul_ustack.ss_sp),
4756515Sraf 	    prt_addr(ulwp.ul_ustack.ss_sp, 1),
4766515Sraf 	    ulwp.ul_ustack.ss_size,
4776515Sraf 	    stack_flags(&ulwp.ul_ustack));
4780Sstevel@tonic-gate 
4796247Sraf 	HD("ix         lwpid      pri        epri       policy     cid");
4800Sstevel@tonic-gate 	mdb_printf(OFFSTR "%-10d %-10d %-10d %-10d %-10d %d\n",
4816515Sraf 	    OFFSET(ul_ix),
4826515Sraf 	    ulwp.ul_ix,
4836515Sraf 	    ulwp.ul_lwpid,
4846515Sraf 	    ulwp.ul_pri,
4856515Sraf 	    ulwp.ul_epri,
4866515Sraf 	    ulwp.ul_policy,
4876515Sraf 	    ulwp.ul_cid);
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate 	HD("cursig     pleasestop stop       signalled  dead       unwind");
4900Sstevel@tonic-gate 	mdb_printf(OFFSTR "%-10d ",
4916515Sraf 	    OFFSET(ul_cursig),
4926515Sraf 	    ulwp.ul_cursig);
4930Sstevel@tonic-gate 	mdb_printf(ulwp.ul_pleasestop? "0x%-8x " : "%-10d ",
4946515Sraf 	    ulwp.ul_pleasestop);
4950Sstevel@tonic-gate 	mdb_printf(ulwp.ul_stop? "0x%-8x " : "%-10d ",
4966515Sraf 	    ulwp.ul_stop);
4970Sstevel@tonic-gate 	mdb_printf("%-10d %-10d %d\n",
4986515Sraf 	    ulwp.ul_signalled,
4996515Sraf 	    ulwp.ul_dead,
5006515Sraf 	    ulwp.ul_unwind);
5010Sstevel@tonic-gate 
5021885Sraf 	HD("detached   writer     stopping   can'prolog preempt    savpreempt");
5030Sstevel@tonic-gate 	mdb_printf(OFFSTR "%-10d %-10d %-10d %-10d %-10d %d\n",
5046515Sraf 	    OFFSET(ul_detached),
5056515Sraf 	    ulwp.ul_detached,
5066515Sraf 	    ulwp.ul_writer,
5076515Sraf 	    ulwp.ul_stopping,
5086515Sraf 	    ulwp.ul_cancel_prologue,
5096515Sraf 	    ulwp.ul_preempt,
5106515Sraf 	    ulwp.ul_savpreempt);
5110Sstevel@tonic-gate 
5120Sstevel@tonic-gate 	HD("sigsuspend main       fork       primarymap m'spinners d'noreserv");
5130Sstevel@tonic-gate 	mdb_printf(OFFSTR "%-10d %-10d %-10d %-10d %-10d %d\n",
5146515Sraf 	    OFFSET(ul_sigsuspend),
5156515Sraf 	    ulwp.ul_sigsuspend,
5166515Sraf 	    ulwp.ul_main,
5176515Sraf 	    ulwp.ul_fork,
5186515Sraf 	    ulwp.ul_primarymap,
5196515Sraf 	    ulwp.ul_max_spinners,
5206515Sraf 	    ulwp.ul_door_noreserve);
5210Sstevel@tonic-gate 
5226247Sraf 	HD("queue_fifo c'w'defer  e'detect'  async_safe rt         rtqueued");
5230Sstevel@tonic-gate 	mdb_printf(OFFSTR "%-10d %-10d %-10d %-10d %-10d %d\n",
5246515Sraf 	    OFFSET(ul_queue_fifo),
5256515Sraf 	    ulwp.ul_queue_fifo,
5266515Sraf 	    ulwp.ul_cond_wait_defer,
5276515Sraf 	    ulwp.ul_error_detection,
5286515Sraf 	    ulwp.ul_async_safe,
5296515Sraf 	    ulwp.ul_rt,
5306515Sraf 	    ulwp.ul_rtqueued);
5310Sstevel@tonic-gate 
5327255Sraf 	HD("misaligned adapt'spin queue_spin critical   sigdefer   vfork");
5337255Sraf 	mdb_printf(OFFSTR "%-10d %-10d %-10d %-10d %-10d %d\n",
5347255Sraf 	    OFFSET(ul_misaligned),
5357255Sraf 	    ulwp.ul_misaligned,
5366515Sraf 	    ulwp.ul_adaptive_spin,
5376515Sraf 	    ulwp.ul_queue_spin,
5386515Sraf 	    ulwp.ul_critical,
5396515Sraf 	    ulwp.ul_sigdefer,
5406515Sraf 	    ulwp.ul_vfork);
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate 	HD("cancelable c'pending  c'disabled c'async    save_async mutator");
5430Sstevel@tonic-gate 	mdb_printf(OFFSTR "%-10d %-10d %-10d %-10d %-10d %d\n",
5446515Sraf 	    OFFSET(ul_cancelable),
5456515Sraf 	    ulwp.ul_cancelable,
5466515Sraf 	    ulwp.ul_cancel_pending,
5476515Sraf 	    ulwp.ul_cancel_disabled,
5486515Sraf 	    ulwp.ul_cancel_async,
5496515Sraf 	    ulwp.ul_save_async,
5506515Sraf 	    ulwp.ul_mutator);
5510Sstevel@tonic-gate 
5520Sstevel@tonic-gate 	HD("created    replace    nocancel   errno      errnop");
5530Sstevel@tonic-gate 	mdb_printf(OFFSTR "%-10d %-10d %-10d %-10d %s\n",
5546515Sraf 	    OFFSET(ul_created),
5556515Sraf 	    ulwp.ul_created,
5566515Sraf 	    ulwp.ul_replace,
5576515Sraf 	    ulwp.ul_nocancel,
5586515Sraf 	    ulwp.ul_errno,
5596515Sraf 	    prt_addr(ulwp.ul_errnop, 0));
5600Sstevel@tonic-gate 
5610Sstevel@tonic-gate 	HD("clnup_hdr             schedctl_called       schedctl");
5620Sstevel@tonic-gate 	mdb_printf(OFFSTR "%s %s %s\n",
5636515Sraf 	    OFFSET(ul_clnup_hdr),
5646515Sraf 	    prt_addr(ulwp.ul_clnup_hdr, 1),
5656515Sraf 	    prt_addr(ulwp.ul_schedctl_called, 1),
5666515Sraf 	    prt_addr((void *)ulwp.ul_schedctl, 0));
5670Sstevel@tonic-gate 
5685891Sraf 	HD("bindflags  libc_locks stsd                  &ftsd");
5690Sstevel@tonic-gate 	mdb_printf(OFFSTR,
5706515Sraf 	    OFFSET(ul_bindflags));
5710Sstevel@tonic-gate 	mdb_printf(ulwp.ul_bindflags? "0x%-8x " : "%-10d ",
5726515Sraf 	    ulwp.ul_bindflags);
5735891Sraf 	mdb_printf("%-10d ", ulwp.ul_libc_locks);
5740Sstevel@tonic-gate 	mdb_printf("%s %s\n",
5756515Sraf 	    prt_addr(ulwp.ul_stsd, 1),
5766515Sraf 	    prt_addr((void *)(addr + OFFSET(ul_ftsd[0])), 0));
5770Sstevel@tonic-gate 
5780Sstevel@tonic-gate 	HD("eventmask[0..1]       eventnum              eventdata");
5790Sstevel@tonic-gate 	mdb_printf(OFFSTR "0x%08x 0x%08x %-21d %s\n",
5806515Sraf 	    OFFSET(ul_td_evbuf.eventmask.event_bits[0]),
5816515Sraf 	    ulwp.ul_td_evbuf.eventmask.event_bits[0],
5826515Sraf 	    ulwp.ul_td_evbuf.eventmask.event_bits[1],
5836515Sraf 	    ulwp.ul_td_evbuf.eventnum,
5846515Sraf 	    prt_addr(ulwp.ul_td_evbuf.eventdata, 0));
5850Sstevel@tonic-gate 
5860Sstevel@tonic-gate 	HD("td'enable  sync'reg   qtype      cv_wake    usropts");
5870Sstevel@tonic-gate 	mdb_printf(OFFSTR "%-10d %-10d %-10d %-10d ",
5886515Sraf 	    OFFSET(ul_td_events_enable),
5896515Sraf 	    ulwp.ul_td_events_enable,
5906515Sraf 	    ulwp.ul_sync_obj_reg,
5916515Sraf 	    ulwp.ul_qtype,
5926515Sraf 	    ulwp.ul_cv_wake);
5930Sstevel@tonic-gate 	mdb_printf(ulwp.ul_usropts? "0x%x\n" : "%d\n",
5946515Sraf 	    ulwp.ul_usropts);
5950Sstevel@tonic-gate 
5960Sstevel@tonic-gate 	HD("startpc               startarg              wchan");
5970Sstevel@tonic-gate 	mdb_printf(OFFSTR "%s %s %s\n",
5986515Sraf 	    OFFSET(ul_startpc),
5996515Sraf 	    prt_addr((void *)ulwp.ul_startpc, 1),
6006515Sraf 	    prt_addr(ulwp.ul_startarg, 1),
6016515Sraf 	    prt_addr(ulwp.ul_wchan, 0));
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate 	HD("link                  sleepq                cvmutex");
6040Sstevel@tonic-gate 	mdb_printf(OFFSTR "%s %s %s\n",
6056515Sraf 	    OFFSET(ul_link),
6066515Sraf 	    prt_addr(ulwp.ul_link, 1),
6076515Sraf 	    prt_addr(ulwp.ul_sleepq, 1),
6086515Sraf 	    prt_addr(ulwp.ul_cvmutex, 0));
6090Sstevel@tonic-gate 
6106247Sraf 	HD("mxchain               save_state");
6116247Sraf 	mdb_printf(OFFSTR "%s %d\n",
6126515Sraf 	    OFFSET(ul_mxchain),
6136515Sraf 	    prt_addr(ulwp.ul_mxchain, 1),
6146515Sraf 	    ulwp.ul_save_state);
6150Sstevel@tonic-gate 
6164574Sraf 	HD("rdlockcnt             rd_rwlock             rd_count");
6174574Sraf 	mdb_printf(OFFSTR "%-21d %s %d\n",
6186515Sraf 	    OFFSET(ul_rdlockcnt),
6196515Sraf 	    ulwp.ul_rdlockcnt,
6206515Sraf 	    prt_addr(ulwp.ul_readlock.single.rd_rwlock, 1),
6216515Sraf 	    ulwp.ul_readlock.single.rd_count);
6224574Sraf 
6234574Sraf 	HD("heldlockcnt           heldlocks             tpdp");
6244574Sraf 	mdb_printf(OFFSTR "%-21d %s %s\n",
6256515Sraf 	    OFFSET(ul_heldlockcnt),
6266515Sraf 	    ulwp.ul_heldlockcnt,
6276515Sraf 	    prt_addr(ulwp.ul_heldlocks.single, 1),
6286515Sraf 	    prt_addr(ulwp.ul_tpdp, 0));
6290Sstevel@tonic-gate 
6300Sstevel@tonic-gate 	HD("siglink               s'l'spin   s'l'spin2  s'l'sleep  s'l'wakeup");
6316247Sraf 	mdb_printf(OFFSTR "%s %-10d %-10d %-10d %d\n",
6326515Sraf 	    OFFSET(ul_siglink),
6336515Sraf 	    prt_addr(ulwp.ul_siglink, 1),
6346515Sraf 	    ulwp.ul_spin_lock_spin,
6356515Sraf 	    ulwp.ul_spin_lock_spin2,
6366515Sraf 	    ulwp.ul_spin_lock_sleep,
6376515Sraf 	    ulwp.ul_spin_lock_wakeup);
6380Sstevel@tonic-gate 
6396247Sraf 	HD("&queue_root           rtclassid  pilocks");
6406247Sraf 	mdb_printf(OFFSTR "%s %-10d %d\n",
6416515Sraf 	    OFFSET(ul_queue_root),
6426515Sraf 	    prt_addr((void *)(addr + OFFSET(ul_queue_root)), 1),
6436515Sraf 	    ulwp.ul_rtclassid,
6446515Sraf 	    ulwp.ul_pilocks);
6456247Sraf 
6460Sstevel@tonic-gate 	/*
6470Sstevel@tonic-gate 	 * The remainder of the ulwp_t structure
6480Sstevel@tonic-gate 	 * is invalid if this is a replacement.
6490Sstevel@tonic-gate 	 */
6500Sstevel@tonic-gate 	if (ulwp.ul_replace)
6510Sstevel@tonic-gate 		return (DCMD_OK);
6520Sstevel@tonic-gate 
6530Sstevel@tonic-gate 	HD("sigmask[0..3]");
6540Sstevel@tonic-gate 	mdb_printf(OFFSTR "0x%08x 0x%08x 0x%08x 0x%08x\n",
6556515Sraf 	    OFFSET(ul_sigmask.__sigbits[0]),
6566515Sraf 	    ulwp.ul_sigmask.__sigbits[0],
6576515Sraf 	    ulwp.ul_sigmask.__sigbits[1],
6586515Sraf 	    ulwp.ul_sigmask.__sigbits[2],
6596515Sraf 	    ulwp.ul_sigmask.__sigbits[3]);
6600Sstevel@tonic-gate 
6610Sstevel@tonic-gate 	HD("tmpmask[0..3]");
6620Sstevel@tonic-gate 	mdb_printf(OFFSTR "0x%08x 0x%08x 0x%08x 0x%08x\n",
6636515Sraf 	    OFFSET(ul_tmpmask.__sigbits[0]),
6646515Sraf 	    ulwp.ul_tmpmask.__sigbits[0],
6656515Sraf 	    ulwp.ul_tmpmask.__sigbits[1],
6666515Sraf 	    ulwp.ul_tmpmask.__sigbits[2],
6676515Sraf 	    ulwp.ul_tmpmask.__sigbits[3]);
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate 	HD("&siginfo              &spinlock             &fpuenv");
6700Sstevel@tonic-gate 	mdb_printf(OFFSTR "%s %s %s\n",
6716515Sraf 	    OFFSET(ul_siginfo),
6726515Sraf 	    prt_addr((void *)(addr + OFFSET(ul_siginfo)), 1),
6736515Sraf 	    prt_addr((void *)(addr + OFFSET(ul_spinlock)), 1),
6746515Sraf 	    prt_addr((void *)(addr + OFFSET(ul_fpuenv)), 0));
6750Sstevel@tonic-gate 
6760Sstevel@tonic-gate 	return (DCMD_OK);
6770Sstevel@tonic-gate }
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate /*
6800Sstevel@tonic-gate  * Get the address of the unique uberdata_t structure.
6810Sstevel@tonic-gate  */
6820Sstevel@tonic-gate static uintptr_t
6830Sstevel@tonic-gate uberdata_addr(void)
6840Sstevel@tonic-gate {
6850Sstevel@tonic-gate 	uintptr_t uaddr;
6860Sstevel@tonic-gate 	uintptr_t addr;
6870Sstevel@tonic-gate 	GElf_Sym sym;
6880Sstevel@tonic-gate 
6890Sstevel@tonic-gate 	if (mdb_lookup_by_obj("libc.so.1", "_tdb_bootstrap", &sym) != 0) {
6900Sstevel@tonic-gate 		mdb_warn("cannot find libc.so.1`_tdb_bootstrap");
6910Sstevel@tonic-gate 		return (NULL);
6920Sstevel@tonic-gate 	}
6930Sstevel@tonic-gate 	if (mdb_vread(&addr, sizeof (addr), sym.st_value) == sizeof (addr) &&
6940Sstevel@tonic-gate 	    addr != NULL &&
6950Sstevel@tonic-gate 	    mdb_vread(&uaddr, sizeof (uaddr), addr) == sizeof (uaddr) &&
6960Sstevel@tonic-gate 	    uaddr != NULL) {
6970Sstevel@tonic-gate 		return (uaddr);
6980Sstevel@tonic-gate 	}
6990Sstevel@tonic-gate 	if (mdb_lookup_by_obj("libc.so.1", "_uberdata", &sym) != 0) {
7000Sstevel@tonic-gate 		mdb_warn("cannot find libc.so.1`_uberdata");
7010Sstevel@tonic-gate 		return (NULL);
7020Sstevel@tonic-gate 	}
7030Sstevel@tonic-gate 	return ((uintptr_t)sym.st_value);
7040Sstevel@tonic-gate }
7050Sstevel@tonic-gate 
7060Sstevel@tonic-gate #undef OFFSET
7070Sstevel@tonic-gate #define	OFFSET(member)	((size_t)OFFSETOF(uberdata_t, member))
7080Sstevel@tonic-gate 
7090Sstevel@tonic-gate /*ARGSUSED*/
7100Sstevel@tonic-gate static int
7110Sstevel@tonic-gate d_uberdata(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
7120Sstevel@tonic-gate {
7130Sstevel@tonic-gate 	uberdata_t uberdata;
7140Sstevel@tonic-gate 	int i;
7150Sstevel@tonic-gate 
7160Sstevel@tonic-gate 	if (argc != 0)
7170Sstevel@tonic-gate 		return (DCMD_USAGE);
7180Sstevel@tonic-gate 	if (!(flags & DCMD_ADDRSPEC) && (addr = uberdata_addr()) == NULL)
7190Sstevel@tonic-gate 		return (DCMD_ERR);
7200Sstevel@tonic-gate 
7210Sstevel@tonic-gate 	if (mdb_vread(&uberdata, sizeof (uberdata), addr) !=
7220Sstevel@tonic-gate 	    sizeof (uberdata)) {
7230Sstevel@tonic-gate 		mdb_warn("failed to read uberdata at 0x%p", addr);
7240Sstevel@tonic-gate 		return (DCMD_ERR);
7250Sstevel@tonic-gate 	}
7260Sstevel@tonic-gate 
7270Sstevel@tonic-gate 	mdb_printf("%#a\n", addr);
7280Sstevel@tonic-gate 
7296515Sraf 	HD("&link_lock            &ld_lock              &fork_lock");
7300Sstevel@tonic-gate 	mdb_printf(OFFSTR "%s %s %s\n",
7316515Sraf 	    OFFSET(link_lock),
7326515Sraf 	    prt_addr((void *)(addr + OFFSET(link_lock)), 1),
7336515Sraf 	    prt_addr((void *)(addr + OFFSET(ld_lock)), 1),
7346515Sraf 	    prt_addr((void *)(addr + OFFSET(fork_lock)), 0));
7350Sstevel@tonic-gate 
7366515Sraf 	HD("&atfork_lock          &callout_lock         &tdb_hash_lock");
7370Sstevel@tonic-gate 	mdb_printf(OFFSTR "%s %s %s\n",
7386515Sraf 	    OFFSET(atfork_lock),
7396515Sraf 	    prt_addr((void *)(addr + OFFSET(atfork_lock)), 1),
7406515Sraf 	    prt_addr((void *)(addr + OFFSET(callout_lock)), 1),
7416515Sraf 	    prt_addr((void *)(addr + OFFSET(tdb_hash_lock)), 0));
7426515Sraf 
7436515Sraf 	HD("&tdb_hash_lock_stats  &siguaction[0]");
7446515Sraf 	mdb_printf(OFFSTR "%s %s\n",
7456515Sraf 	    OFFSET(tdb_hash_lock_stats),
7466515Sraf 	    prt_addr((void *)(addr + OFFSET(tdb_hash_lock_stats)), 1),
7476515Sraf 	    prt_addr((void *)(addr + OFFSET(siguaction)), 0));
7480Sstevel@tonic-gate 
7490Sstevel@tonic-gate 	HD("&bucket               free_list             chunks");
7500Sstevel@tonic-gate 	for (i = 0; i < NBUCKETS; i++) {
7510Sstevel@tonic-gate 		mdb_printf(OFFSTR "%s %s %ld\n",
7526515Sraf 		    OFFSET(bucket[i]),
7536515Sraf 		    prt_addr((void *)(addr + OFFSET(bucket[i])), 1),
7546515Sraf 		    prt_addr(uberdata.bucket[i].free_list, 1),
7556515Sraf 		    uberdata.bucket[i].chunks);
7560Sstevel@tonic-gate 	}
7570Sstevel@tonic-gate 
7580Sstevel@tonic-gate 	HD("&atexit_root          head                  exit_frame_monitor");
7590Sstevel@tonic-gate 	mdb_printf(OFFSTR "%s %s %s\n",
7606515Sraf 	    OFFSET(atexit_root),
7616515Sraf 	    prt_addr((void *)(addr + OFFSET(atexit_root.exitfns_lock)), 1),
7626515Sraf 	    prt_addr(uberdata.atexit_root.head, 1),
7636515Sraf 	    prt_addr(uberdata.atexit_root.exit_frame_monitor, 0));
7640Sstevel@tonic-gate 
7650Sstevel@tonic-gate 	HD("&tsd_metadata         tsdm_nkeys tsdm_nused tsdm_destro");
7660Sstevel@tonic-gate 	mdb_printf(OFFSTR "%s %-10d %-10d %s\n",
7676515Sraf 	    OFFSET(tsd_metadata),
7686515Sraf 	    prt_addr((void *)(addr + OFFSET(tsd_metadata.tsdm_lock)), 1),
7696515Sraf 	    uberdata.tsd_metadata.tsdm_nkeys,
7706515Sraf 	    uberdata.tsd_metadata.tsdm_nused,
7716515Sraf 	    prt_addr((void *)uberdata.tsd_metadata.tsdm_destro, 0));
7720Sstevel@tonic-gate 
7730Sstevel@tonic-gate 	HD("&tls_metadata         tls_modinfo.data      tls_modinfo.size");
7740Sstevel@tonic-gate 	mdb_printf(OFFSTR "%s %s %ld\n",
7756515Sraf 	    OFFSET(tls_metadata),
7766515Sraf 	    prt_addr((void *)(addr + OFFSET(tls_metadata.tls_lock)), 1),
7776515Sraf 	    prt_addr(uberdata.tls_metadata.tls_modinfo.tls_data, 1),
7786515Sraf 	    uberdata.tls_metadata.tls_modinfo.tls_size);
7790Sstevel@tonic-gate 
7800Sstevel@tonic-gate 	HD("                      static_tls.data       static_tls.size");
7810Sstevel@tonic-gate 	mdb_printf(OFFSTR "%s %s %ld\n",
7826515Sraf 	    OFFSET(tls_metadata.static_tls),
7836515Sraf 	    "                     ",
7846515Sraf 	    prt_addr(uberdata.tls_metadata.static_tls.tls_data, 1),
7856515Sraf 	    uberdata.tls_metadata.static_tls.tls_size);
7860Sstevel@tonic-gate 
7870Sstevel@tonic-gate 	HD("primary_ma bucket_ini uflags.mt  uflags.pad uflags.trs uflags.ted");
7880Sstevel@tonic-gate 	mdb_printf(OFFSTR "%-10d %-10d %-10d %-10d %-10d %d\n",
7896515Sraf 	    OFFSET(primary_map),
7906515Sraf 	    uberdata.primary_map,
7916515Sraf 	    uberdata.bucket_init,
7926515Sraf 	    uberdata.uberflags.uf_x.x_mt,
7936515Sraf 	    uberdata.uberflags.uf_x.x_pad,
7946515Sraf 	    uberdata.uberflags.uf_x.x_tdb_register_sync,
7956515Sraf 	    uberdata.uberflags.uf_x.x_thread_error_detection);
7960Sstevel@tonic-gate 
7970Sstevel@tonic-gate 	HD("queue_head            thr_hash_table        hash_size  hash_mask");
7980Sstevel@tonic-gate 	mdb_printf(OFFSTR "%s %s %-10d 0x%x\n",
7996515Sraf 	    OFFSET(queue_head),
8006515Sraf 	    prt_addr(uberdata.queue_head, 1),
8016515Sraf 	    prt_addr(uberdata.thr_hash_table, 1),
8026515Sraf 	    uberdata.hash_size,
8036515Sraf 	    uberdata.hash_mask);
8040Sstevel@tonic-gate 
8050Sstevel@tonic-gate 	HD("ulwp_one              all_lwps              all_zombies");
8060Sstevel@tonic-gate 	mdb_printf(OFFSTR "%s %s %s\n",
8076515Sraf 	    OFFSET(ulwp_one),
8086515Sraf 	    prt_addr(uberdata.ulwp_one, 1),
8096515Sraf 	    prt_addr(uberdata.all_lwps, 1),
8106515Sraf 	    prt_addr(uberdata.all_zombies, 0));
8110Sstevel@tonic-gate 
8120Sstevel@tonic-gate 	HD("nthreads   nzombies   ndaemons   pid        sigacthandler");
8130Sstevel@tonic-gate 	mdb_printf(OFFSTR "%-10d %-10d %-10d %-10d %s\n",
8146515Sraf 	    OFFSET(nthreads),
8156515Sraf 	    uberdata.nthreads,
8166515Sraf 	    uberdata.nzombies,
8176515Sraf 	    uberdata.ndaemons,
8186515Sraf 	    (int)uberdata.pid,
8196515Sraf 	    prt_addr((void *)uberdata.sigacthandler, 0));
8200Sstevel@tonic-gate 
8210Sstevel@tonic-gate 	HD("lwp_stacks            lwp_laststack         nfreestack stk_cache");
8220Sstevel@tonic-gate 	mdb_printf(OFFSTR "%s %s %-10d %d\n",
8236515Sraf 	    OFFSET(lwp_stacks),
8246515Sraf 	    prt_addr(uberdata.lwp_stacks, 1),
8256515Sraf 	    prt_addr(uberdata.lwp_laststack, 1),
8266515Sraf 	    uberdata.nfreestack,
8276515Sraf 	    uberdata.thread_stack_cache);
8280Sstevel@tonic-gate 
8296515Sraf 	HD("ulwp_freelist         ulwp_lastfree         ulwp_replace_free");
8306515Sraf 	mdb_printf(OFFSTR "%s %s %s\n",
8316515Sraf 	    OFFSET(ulwp_freelist),
8326515Sraf 	    prt_addr(uberdata.ulwp_freelist, 1),
8336515Sraf 	    prt_addr(uberdata.ulwp_lastfree, 1),
8346515Sraf 	    prt_addr(uberdata.ulwp_replace_free, 0));
8354574Sraf 
8369170SRoger.Faulkner@Sun.COM 	HD("ulwp_replace_last     atforklist");
8379170SRoger.Faulkner@Sun.COM 	mdb_printf(OFFSTR "%s %s\n",
8386515Sraf 	    OFFSET(ulwp_replace_last),
8396515Sraf 	    prt_addr(uberdata.ulwp_replace_last, 1),
8409170SRoger.Faulkner@Sun.COM 	    prt_addr(uberdata.atforklist, 0));
8419170SRoger.Faulkner@Sun.COM 
8429170SRoger.Faulkner@Sun.COM 	HD("robustlocks           robustlist");
8439170SRoger.Faulkner@Sun.COM 	mdb_printf(OFFSTR "%s %s\n",
8449170SRoger.Faulkner@Sun.COM 	    OFFSET(robustlocks),
8459170SRoger.Faulkner@Sun.COM 	    prt_addr(uberdata.robustlocks, 1),
8469170SRoger.Faulkner@Sun.COM 	    prt_addr(uberdata.robustlist, 0));
8470Sstevel@tonic-gate 
8480Sstevel@tonic-gate 	HD("tdb_bootstrap         tdb_sync_addr_hash    tdb_'count tdb_'fail");
8490Sstevel@tonic-gate 	mdb_printf(OFFSTR "%s %s %-10d %d\n",
8506515Sraf 	    OFFSET(tdb_bootstrap),
8516515Sraf 	    prt_addr(uberdata.tdb_bootstrap, 1),
8526515Sraf 	    prt_addr(uberdata.tdb.tdb_sync_addr_hash, 1),
8536515Sraf 	    uberdata.tdb.tdb_register_count,
8546515Sraf 	    uberdata.tdb.tdb_hash_alloc_failed);
8550Sstevel@tonic-gate 
8560Sstevel@tonic-gate 	HD("tdb_sync_addr_free    tdb_sync_addr_last    tdb_sync_alloc");
8570Sstevel@tonic-gate 	mdb_printf(OFFSTR "%s %s %ld\n",
8586515Sraf 	    OFFSET(tdb.tdb_sync_addr_free),
8596515Sraf 	    prt_addr(uberdata.tdb.tdb_sync_addr_free, 1),
8606515Sraf 	    prt_addr(uberdata.tdb.tdb_sync_addr_last, 1),
8616515Sraf 	    uberdata.tdb.tdb_sync_alloc);
8620Sstevel@tonic-gate 
8630Sstevel@tonic-gate 	HD("tdb_ev_global_mask    tdb_events");
8640Sstevel@tonic-gate 	mdb_printf(OFFSTR "0x%08x 0x%08x %s\n",
8656515Sraf 	    OFFSET(tdb.tdb_ev_global_mask),
8666515Sraf 	    uberdata.tdb.tdb_ev_global_mask.event_bits[0],
8676515Sraf 	    uberdata.tdb.tdb_ev_global_mask.event_bits[1],
8686515Sraf 	    prt_addr((void *)uberdata.tdb.tdb_events, 0));
8690Sstevel@tonic-gate 
8700Sstevel@tonic-gate 	return (DCMD_OK);
8710Sstevel@tonic-gate }
8720Sstevel@tonic-gate 
8730Sstevel@tonic-gate static int
8740Sstevel@tonic-gate ulwp_walk_init(mdb_walk_state_t *wsp)
8750Sstevel@tonic-gate {
8760Sstevel@tonic-gate 	uintptr_t addr = wsp->walk_addr;
8770Sstevel@tonic-gate 	uintptr_t uber_addr;
8780Sstevel@tonic-gate 
8790Sstevel@tonic-gate 	if (addr == NULL &&
8800Sstevel@tonic-gate 	    ((uber_addr = uberdata_addr()) == NULL ||
8810Sstevel@tonic-gate 	    mdb_vread(&addr, sizeof (addr),
8820Sstevel@tonic-gate 	    uber_addr + OFFSETOF(uberdata_t, all_lwps))
8830Sstevel@tonic-gate 	    != sizeof (addr))) {
8840Sstevel@tonic-gate 		mdb_warn("cannot find 'uberdata.all_lwps'");
8850Sstevel@tonic-gate 		return (WALK_ERR);
8860Sstevel@tonic-gate 	}
8870Sstevel@tonic-gate 	if (addr == NULL)
8880Sstevel@tonic-gate 		return (WALK_DONE);
8890Sstevel@tonic-gate 	wsp->walk_addr = addr;
8900Sstevel@tonic-gate 	wsp->walk_data = (void *)addr;
8910Sstevel@tonic-gate 	return (WALK_NEXT);
8920Sstevel@tonic-gate }
8930Sstevel@tonic-gate 
8940Sstevel@tonic-gate static int
8950Sstevel@tonic-gate ulwp_walk_step(mdb_walk_state_t *wsp)
8960Sstevel@tonic-gate {
8970Sstevel@tonic-gate 	uintptr_t addr = wsp->walk_addr;
8980Sstevel@tonic-gate 	ulwp_t ulwp;
8990Sstevel@tonic-gate 
9000Sstevel@tonic-gate 	if (addr == NULL)
9010Sstevel@tonic-gate 		return (WALK_DONE);
9020Sstevel@tonic-gate 	if (mdb_vread(&ulwp, sizeof (ulwp), addr) != sizeof (ulwp) &&
9030Sstevel@tonic-gate 	    (bzero(&ulwp, sizeof (ulwp)),
9040Sstevel@tonic-gate 	    mdb_vread(&ulwp, REPLACEMENT_SIZE, addr)) != REPLACEMENT_SIZE) {
9050Sstevel@tonic-gate 		mdb_warn("failed to read ulwp at 0x%p", addr);
9060Sstevel@tonic-gate 		return (WALK_ERR);
9070Sstevel@tonic-gate 	}
9080Sstevel@tonic-gate 	/*
9090Sstevel@tonic-gate 	 * If we have looped around to the beginning
9100Sstevel@tonic-gate 	 * of the circular linked list, we are done.
9110Sstevel@tonic-gate 	 */
9120Sstevel@tonic-gate 	if ((wsp->walk_addr = (uintptr_t)ulwp.ul_forw)
9130Sstevel@tonic-gate 	    == (uintptr_t)wsp->walk_data)
9140Sstevel@tonic-gate 		wsp->walk_addr = NULL;
9150Sstevel@tonic-gate 	return (wsp->walk_callback(addr, &ulwp, wsp->walk_cbdata));
9160Sstevel@tonic-gate }
9170Sstevel@tonic-gate 
918*10610SJonathan.Adams@Sun.COM /* Avoid classifying NULL pointers as part of the main stack on x86 */
919*10610SJonathan.Adams@Sun.COM #define	MIN_STACK_ADDR		(0x10000ul)
920*10610SJonathan.Adams@Sun.COM 
921*10610SJonathan.Adams@Sun.COM static int
922*10610SJonathan.Adams@Sun.COM whatis_walk_ulwp(uintptr_t addr, const ulwp_t *ulwp, mdb_whatis_t *w)
923*10610SJonathan.Adams@Sun.COM {
924*10610SJonathan.Adams@Sun.COM 	uintptr_t cur;
925*10610SJonathan.Adams@Sun.COM 	lwpid_t id = ulwp->ul_lwpid;
926*10610SJonathan.Adams@Sun.COM 	uintptr_t top, base, size;
927*10610SJonathan.Adams@Sun.COM 
928*10610SJonathan.Adams@Sun.COM 	while (mdb_whatis_match(w, addr, sizeof (ulwp_t), &cur))
929*10610SJonathan.Adams@Sun.COM 		mdb_whatis_report_object(w, cur, addr,
930*10610SJonathan.Adams@Sun.COM 		    "allocated as thread %#r's ulwp_t\n", id);
931*10610SJonathan.Adams@Sun.COM 
932*10610SJonathan.Adams@Sun.COM 	top = (uintptr_t)ulwp->ul_stktop;
933*10610SJonathan.Adams@Sun.COM 	size = ulwp->ul_stksiz;
934*10610SJonathan.Adams@Sun.COM 
935*10610SJonathan.Adams@Sun.COM 	/*
936*10610SJonathan.Adams@Sun.COM 	 * The main stack ends up being a little weird, especially if
937*10610SJonathan.Adams@Sun.COM 	 * the stack ulimit is unlimited.  This tries to take that into
938*10610SJonathan.Adams@Sun.COM 	 * account.
939*10610SJonathan.Adams@Sun.COM 	 */
940*10610SJonathan.Adams@Sun.COM 	if (size > top)
941*10610SJonathan.Adams@Sun.COM 		size = top;
942*10610SJonathan.Adams@Sun.COM 	if (top > MIN_STACK_ADDR && top - size < MIN_STACK_ADDR)
943*10610SJonathan.Adams@Sun.COM 		size = top - MIN_STACK_ADDR;
944*10610SJonathan.Adams@Sun.COM 
945*10610SJonathan.Adams@Sun.COM 	base = top - size;
946*10610SJonathan.Adams@Sun.COM 
947*10610SJonathan.Adams@Sun.COM 	while (mdb_whatis_match(w, base, size, &cur))
948*10610SJonathan.Adams@Sun.COM 		mdb_whatis_report_address(w, cur, "in [ stack tid=%#r ]\n", id);
949*10610SJonathan.Adams@Sun.COM 
950*10610SJonathan.Adams@Sun.COM 	if (ulwp->ul_ustack.ss_flags & SS_ONSTACK) {
951*10610SJonathan.Adams@Sun.COM 		base = (uintptr_t)ulwp->ul_ustack.ss_sp;
952*10610SJonathan.Adams@Sun.COM 		size = ulwp->ul_ustack.ss_size;
953*10610SJonathan.Adams@Sun.COM 
954*10610SJonathan.Adams@Sun.COM 		while (mdb_whatis_match(w, base, size, &cur))
955*10610SJonathan.Adams@Sun.COM 			mdb_whatis_report_address(w, cur,
956*10610SJonathan.Adams@Sun.COM 			    "in [ altstack tid=%#r ]\n", id);
957*10610SJonathan.Adams@Sun.COM 	}
958*10610SJonathan.Adams@Sun.COM 
959*10610SJonathan.Adams@Sun.COM 	return (WHATIS_WALKRET(w));
960*10610SJonathan.Adams@Sun.COM }
961*10610SJonathan.Adams@Sun.COM 
962*10610SJonathan.Adams@Sun.COM /*ARGSUSED*/
963*10610SJonathan.Adams@Sun.COM static int
964*10610SJonathan.Adams@Sun.COM whatis_run_ulwps(mdb_whatis_t *w, void *arg)
965*10610SJonathan.Adams@Sun.COM {
966*10610SJonathan.Adams@Sun.COM 	if (mdb_walk("ulwps", (mdb_walk_cb_t)whatis_walk_ulwp, w) == -1) {
967*10610SJonathan.Adams@Sun.COM 		mdb_warn("couldn't find ulwps walker");
968*10610SJonathan.Adams@Sun.COM 		return (1);
969*10610SJonathan.Adams@Sun.COM 	}
970*10610SJonathan.Adams@Sun.COM 	return (0);
971*10610SJonathan.Adams@Sun.COM }
972*10610SJonathan.Adams@Sun.COM 
9730Sstevel@tonic-gate /*
9740Sstevel@tonic-gate  * =======================================================
9750Sstevel@tonic-gate  * End of thread (previously libthread) interfaces.
9760Sstevel@tonic-gate  * ==================== threads ==========================
9770Sstevel@tonic-gate  */
9780Sstevel@tonic-gate 
9790Sstevel@tonic-gate static const mdb_dcmd_t dcmds[] = {
9800Sstevel@tonic-gate 	{ "jmp_buf", ":", "print jmp_buf contents", d_jmp_buf, NULL },
9810Sstevel@tonic-gate 	{ "sigjmp_buf", ":", "print sigjmp_buf contents", d_sigjmp_buf, NULL },
9820Sstevel@tonic-gate 	{ "siginfo", ":", "print siginfo_t structure", d_siginfo, NULL },
9830Sstevel@tonic-gate 	{ "ucontext", ":", "print ucontext_t structure", d_ucontext, NULL },
9840Sstevel@tonic-gate 	{ "ulwp", ":", "print ulwp_t structure", d_ulwp, NULL },
9850Sstevel@tonic-gate 	{ "uberdata", ":", "print uberdata_t structure", d_uberdata, NULL },
9860Sstevel@tonic-gate 	{ NULL }
9870Sstevel@tonic-gate };
9880Sstevel@tonic-gate 
9890Sstevel@tonic-gate static const mdb_walker_t walkers[] = {
9900Sstevel@tonic-gate 	{ "ucontext", "walk ucontext_t uc_link list",
9910Sstevel@tonic-gate 		NULL, uc_walk_step, NULL, NULL },
9920Sstevel@tonic-gate 	{ "oldcontext", "walk per-lwp oldcontext pointers",
9930Sstevel@tonic-gate 		oldc_walk_init, oldc_walk_step, oldc_walk_fini, NULL },
9940Sstevel@tonic-gate 	{ "ulwps", "walk list of ulwp_t pointers",
9950Sstevel@tonic-gate 		ulwp_walk_init, ulwp_walk_step, NULL, NULL },
9960Sstevel@tonic-gate 	{ NULL }
9970Sstevel@tonic-gate };
9980Sstevel@tonic-gate 
9990Sstevel@tonic-gate static const mdb_modinfo_t modinfo = { MDB_API_VERSION, dcmds, walkers };
10000Sstevel@tonic-gate 
10010Sstevel@tonic-gate const mdb_modinfo_t *
10020Sstevel@tonic-gate _mdb_init(void)
10030Sstevel@tonic-gate {
1004*10610SJonathan.Adams@Sun.COM 	mdb_whatis_register("threads", whatis_run_ulwps, NULL,
1005*10610SJonathan.Adams@Sun.COM 	    WHATIS_PRIO_EARLY, WHATIS_REG_NO_ID);
1006*10610SJonathan.Adams@Sun.COM 
10070Sstevel@tonic-gate 	return (&modinfo);
10080Sstevel@tonic-gate }
1009