xref: /onnv-gate/usr/src/cmd/mdb/common/modules/libc/libc.c (revision 13093:48f2dbca79a2)
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 /*
2312061SRoger.Faulkner@Oracle.COM  * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <sys/mdb_modapi.h>
2710610SJonathan.Adams@Sun.COM #include <mdb/mdb_whatis.h>
280Sstevel@tonic-gate #include <procfs.h>
290Sstevel@tonic-gate #include <ucontext.h>
300Sstevel@tonic-gate #include <siginfo.h>
310Sstevel@tonic-gate #include <signal.h>
320Sstevel@tonic-gate #include <setjmp.h>
330Sstevel@tonic-gate #include <string.h>
340Sstevel@tonic-gate #include <thr_uberdata.h>
3512902SBryan.Cantrill@Sun.COM #include "findstack.h"
360Sstevel@tonic-gate 
370Sstevel@tonic-gate static const char *
stack_flags(const stack_t * sp)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
d_jmp_buf(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)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
d_ucontext(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)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
d_sigjmp_buf(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)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)
13912061SRoger.Faulkner@Oracle.COM 		greg_t sjs_asi;
14012061SRoger.Faulkner@Oracle.COM 		greg_t sjs_fprs;
1410Sstevel@tonic-gate #endif
1420Sstevel@tonic-gate 		stack_t sjs_stack;
1430Sstevel@tonic-gate 	} s;
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 	if (argc != 0)
1460Sstevel@tonic-gate 		return (DCMD_USAGE);
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	if (mdb_vread(&s, sizeof (s), addr) != sizeof (s)) {
1490Sstevel@tonic-gate 		mdb_warn("failed to read sigjmp_buf at %p", addr);
1500Sstevel@tonic-gate 		return (DCMD_ERR);
1510Sstevel@tonic-gate 	}
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 	mdb_printf("  flags  = 0x%x\n", s.sjs_flags);
1540Sstevel@tonic-gate 	mdb_printf("  %%sp    = 0x%lx %lA\n", s.sjs_sp, s.sjs_sp);
1550Sstevel@tonic-gate 	mdb_printf("  %%pc    = 0x%lx %lA\n", s.sjs_pc, s.sjs_pc);
1560Sstevel@tonic-gate 	mdb_printf("  %%fp    = 0x%lx %lA\n", s.sjs_fp, s.sjs_fp);
1570Sstevel@tonic-gate 	mdb_printf("  %%i7    = 0x%lx %lA\n", s.sjs_i7, s.sjs_i7);
1580Sstevel@tonic-gate 	mdb_printf("  uclink = %p\n", s.sjs_uclink);
1590Sstevel@tonic-gate 	mdb_printf("  sigset = 0x%08x 0x%08x 0x%08x 0x%08x\n",
1600Sstevel@tonic-gate 	    s.sjs_sigmask.__sigbits[0], s.sjs_sigmask.__sigbits[1],
1610Sstevel@tonic-gate 	    s.sjs_sigmask.__sigbits[2], s.sjs_sigmask.__sigbits[3]);
16212061SRoger.Faulkner@Oracle.COM #if defined(_LP64)
16312061SRoger.Faulkner@Oracle.COM 	mdb_printf("  %%asi   = 0x%lx\n", s.sjs_asi);
16412061SRoger.Faulkner@Oracle.COM 	mdb_printf("  %%fprs  = 0x%lx\n", s.sjs_fprs);
16512061SRoger.Faulkner@Oracle.COM #endif
1660Sstevel@tonic-gate 	mdb_printf("  stack  = sp 0x%p size 0x%lx flags %s\n",
1670Sstevel@tonic-gate 	    s.sjs_stack.ss_sp, s.sjs_stack.ss_size, stack_flags(&s.sjs_stack));
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 	return (DCMD_OK);
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate #elif defined(__i386) || defined(__amd64)
1720Sstevel@tonic-gate 	return (d_ucontext(addr, flags, argc, argv));
1730Sstevel@tonic-gate #endif
1740Sstevel@tonic-gate }
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate /*ARGSUSED*/
1770Sstevel@tonic-gate static int
d_siginfo(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)1780Sstevel@tonic-gate d_siginfo(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1790Sstevel@tonic-gate {
1800Sstevel@tonic-gate 	static const char *const msname[] = {
1810Sstevel@tonic-gate 		"USER", "SYSTEM", "TRAP", "TFAULT", "DFAULT", "KFAULT",
1820Sstevel@tonic-gate 		"USER_LOCK", "SLEEP", "WAIT_CPU", "STOPPED"
1830Sstevel@tonic-gate 	};
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 	char signame[SIG2STR_MAX];
1860Sstevel@tonic-gate 	siginfo_t si;
1870Sstevel@tonic-gate 	int i;
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 	if (argc != 0)
1900Sstevel@tonic-gate 		return (DCMD_USAGE);
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 	if (mdb_vread(&si, sizeof (si), addr) != sizeof (si)) {
1930Sstevel@tonic-gate 		mdb_warn("failed to read siginfo at %p", addr);
1940Sstevel@tonic-gate 		return (DCMD_ERR);
1950Sstevel@tonic-gate 	}
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 	if (sig2str(si.si_signo, signame) == -1)
1980Sstevel@tonic-gate 		(void) strcpy(signame, "unknown");
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 	mdb_printf("  signal %5d (%s)\n", si.si_signo, signame);
2010Sstevel@tonic-gate 	mdb_printf("  code   %5d (", si.si_code);
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate 	switch (si.si_code) {
2040Sstevel@tonic-gate 	case SI_NOINFO:
2050Sstevel@tonic-gate 		mdb_printf("no info");
2060Sstevel@tonic-gate 		break;
2070Sstevel@tonic-gate 	case SI_DTRACE:
2080Sstevel@tonic-gate 		mdb_printf("from DTrace raise() action");
2090Sstevel@tonic-gate 		break;
2100Sstevel@tonic-gate 	case SI_RCTL:
2110Sstevel@tonic-gate 		mdb_printf("from rctl action");
2120Sstevel@tonic-gate 		break;
2130Sstevel@tonic-gate 	case SI_USER:
2140Sstevel@tonic-gate 		mdb_printf("user generated via kill");
2150Sstevel@tonic-gate 		break;
2160Sstevel@tonic-gate 	case SI_LWP:
2170Sstevel@tonic-gate 		mdb_printf("user generated via lwp_kill");
2180Sstevel@tonic-gate 		break;
2190Sstevel@tonic-gate 	case SI_QUEUE:
2200Sstevel@tonic-gate 		mdb_printf("user generated via sigqueue");
2210Sstevel@tonic-gate 		break;
2220Sstevel@tonic-gate 	case SI_TIMER:
2230Sstevel@tonic-gate 		mdb_printf("from timer expiration");
2240Sstevel@tonic-gate 		break;
2250Sstevel@tonic-gate 	case SI_ASYNCIO:
2260Sstevel@tonic-gate 		mdb_printf("from async i/o completion");
2270Sstevel@tonic-gate 		break;
2280Sstevel@tonic-gate 	case SI_MESGQ:
2290Sstevel@tonic-gate 		mdb_printf("from message arrival");
2300Sstevel@tonic-gate 		break;
2310Sstevel@tonic-gate 	default:
2320Sstevel@tonic-gate 		if (SI_FROMUSER(&si))
2330Sstevel@tonic-gate 			mdb_printf("from user process");
2340Sstevel@tonic-gate 		else
2350Sstevel@tonic-gate 			mdb_printf("from kernel");
2360Sstevel@tonic-gate 	}
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 	mdb_printf(")\n  errno  %5d (%s)\n",
2390Sstevel@tonic-gate 	    si.si_errno, strerror(si.si_errno));
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 	if (si.si_code == SI_USER || si.si_code == SI_QUEUE) {
2420Sstevel@tonic-gate 		mdb_printf("  signal sent from PID %d (uid %d)\n",
2430Sstevel@tonic-gate 		    si.si_pid, si.si_uid);
2440Sstevel@tonic-gate 	}
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate 	if (si.si_code == SI_QUEUE) {
2470Sstevel@tonic-gate 		mdb_printf("  signal value = 0t%d / %p\n",
2480Sstevel@tonic-gate 		    si.si_value.sival_int, si.si_value.sival_ptr);
2490Sstevel@tonic-gate 	}
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate 	switch (si.si_signo) {
2520Sstevel@tonic-gate 	case SIGCLD:
2530Sstevel@tonic-gate 		mdb_printf("  signal sent from child PID %d (uid %d)\n",
2540Sstevel@tonic-gate 		    si.si_pid, si.si_uid);
2550Sstevel@tonic-gate 		mdb_printf("  usr time = 0t%ld ticks, sys time = 0t%ld ticks\n",
2560Sstevel@tonic-gate 		    si.si_utime, si.si_stime);
2570Sstevel@tonic-gate 		mdb_printf("  wait status = 0x%x\n", si.si_status);
2580Sstevel@tonic-gate 		break;
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 	case SIGSEGV:
2610Sstevel@tonic-gate 	case SIGBUS:
2620Sstevel@tonic-gate 	case SIGILL:
2630Sstevel@tonic-gate 	case SIGTRAP:
2640Sstevel@tonic-gate 	case SIGFPE:
2650Sstevel@tonic-gate 		mdb_printf("  fault address = 0x%p\n  trapno = %d\n",
2660Sstevel@tonic-gate 		    si.si_addr, si.si_trapno);
2670Sstevel@tonic-gate 		mdb_printf("  instruction address = 0x%p %lA\n",
2680Sstevel@tonic-gate 		    si.si_pc, si.si_pc);
2690Sstevel@tonic-gate 		break;
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate 	case SIGPOLL:
2720Sstevel@tonic-gate 	case SIGXFSZ:
2730Sstevel@tonic-gate 		mdb_printf("  fd = %d  band = 0x%lx\n",
2740Sstevel@tonic-gate 		    si.si_fd, si.si_band);
2750Sstevel@tonic-gate 		break;
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 	case SIGPROF:
2780Sstevel@tonic-gate 		mdb_printf("  last fault address = 0x%p fault type = %d\n",
2790Sstevel@tonic-gate 		    si.si_faddr, si.si_fault);
2800Sstevel@tonic-gate 		mdb_printf("  timestamp = 0t%ld sec 0t%ld nsec\n",
2810Sstevel@tonic-gate 		    si.si_tstamp.tv_sec, si.si_tstamp.tv_nsec);
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate 		if (si.__data.__prof.__syscall != 0) {
2840Sstevel@tonic-gate 			mdb_printf("  system call %d (", si.si_syscall);
2850Sstevel@tonic-gate 			if (si.si_nsysarg > 0) {
2860Sstevel@tonic-gate 				mdb_printf("%lx", si.si_sysarg[0]);
2870Sstevel@tonic-gate 				for (i = 1; i < si.si_nsysarg; i++)
2880Sstevel@tonic-gate 					mdb_printf(", %lx", si.si_sysarg[i]);
2890Sstevel@tonic-gate 			}
2900Sstevel@tonic-gate 			mdb_printf("  )\n");
2910Sstevel@tonic-gate 		}
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 		for (i = 0; i < sizeof (msname) / sizeof (msname[0]); i++) {
2940Sstevel@tonic-gate 			mdb_printf("  mstate[\"%s\"] = %d\n",
2950Sstevel@tonic-gate 			    msname[i], si.si_mstate[i]);
2960Sstevel@tonic-gate 		}
2970Sstevel@tonic-gate 		break;
2980Sstevel@tonic-gate 	}
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 	return (DCMD_OK);
3010Sstevel@tonic-gate }
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate static int
uc_walk_step(mdb_walk_state_t * wsp)3040Sstevel@tonic-gate uc_walk_step(mdb_walk_state_t *wsp)
3050Sstevel@tonic-gate {
3060Sstevel@tonic-gate 	uintptr_t addr = wsp->walk_addr;
3070Sstevel@tonic-gate 	ucontext_t uc;
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate 	if (addr == NULL)
3100Sstevel@tonic-gate 		return (WALK_DONE);
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 	if (mdb_vread(&uc, sizeof (uc), addr) != sizeof (uc)) {
3130Sstevel@tonic-gate 		mdb_warn("failed to read ucontext at %p", addr);
3140Sstevel@tonic-gate 		return (WALK_ERR);
3150Sstevel@tonic-gate 	}
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 	wsp->walk_addr = (uintptr_t)uc.uc_link;
3180Sstevel@tonic-gate 	return (wsp->walk_callback(addr, &uc, wsp->walk_cbdata));
3190Sstevel@tonic-gate }
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate static int
oldc_walk_init(mdb_walk_state_t * wsp)3220Sstevel@tonic-gate oldc_walk_init(mdb_walk_state_t *wsp)
3230Sstevel@tonic-gate {
3240Sstevel@tonic-gate 	ssize_t nbytes = mdb_get_xdata("lwpstatus", NULL, 0);
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate 	if (nbytes <= 0) {
3270Sstevel@tonic-gate 		mdb_warn("lwpstatus information not available");
3280Sstevel@tonic-gate 		return (WALK_ERR);
3290Sstevel@tonic-gate 	}
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate 	if (wsp->walk_addr != NULL) {
3320Sstevel@tonic-gate 		mdb_warn("walker only supports global walk\n");
3330Sstevel@tonic-gate 		return (WALK_ERR);
3340Sstevel@tonic-gate 	}
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate 	wsp->walk_addr = nbytes; /* Use walk_addr to track size */
3370Sstevel@tonic-gate 	wsp->walk_data = mdb_alloc(nbytes, UM_SLEEP);
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate 	if (mdb_get_xdata("lwpstatus", wsp->walk_data, nbytes) != nbytes) {
3400Sstevel@tonic-gate 		mdb_warn("failed to read lwpstatus information");
3410Sstevel@tonic-gate 		mdb_free(wsp->walk_data, nbytes);
3420Sstevel@tonic-gate 		return (WALK_ERR);
3430Sstevel@tonic-gate 	}
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate 	wsp->walk_arg = wsp->walk_data; /* Use walk_arg to track pointer */
3460Sstevel@tonic-gate 	return (WALK_NEXT);
3470Sstevel@tonic-gate }
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate static int
oldc_walk_step(mdb_walk_state_t * wsp)3500Sstevel@tonic-gate oldc_walk_step(mdb_walk_state_t *wsp)
3510Sstevel@tonic-gate {
3520Sstevel@tonic-gate 	const lwpstatus_t *lsp, *end;
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate 	end = (const lwpstatus_t *)((uintptr_t)wsp->walk_data + wsp->walk_addr);
3550Sstevel@tonic-gate 	lsp = wsp->walk_arg;
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate 	wsp->walk_arg = (void *)(lsp + 1);
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate 	if (lsp < end) {
3600Sstevel@tonic-gate 		uintptr_t addr = lsp->pr_oldcontext;
3610Sstevel@tonic-gate 		ucontext_t uc;
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate 		if (addr == NULL)
3640Sstevel@tonic-gate 			return (WALK_NEXT);
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate 		if (mdb_vread(&uc, sizeof (uc), addr) != sizeof (uc)) {
3670Sstevel@tonic-gate 			mdb_warn("failed to read ucontext at %p", addr);
3680Sstevel@tonic-gate 			return (WALK_NEXT);
3690Sstevel@tonic-gate 		}
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate 		return (wsp->walk_callback(addr, &uc, wsp->walk_cbdata));
3720Sstevel@tonic-gate 	}
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate 	return (WALK_DONE);
3750Sstevel@tonic-gate }
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate static void
oldc_walk_fini(mdb_walk_state_t * wsp)3780Sstevel@tonic-gate oldc_walk_fini(mdb_walk_state_t *wsp)
3790Sstevel@tonic-gate {
3800Sstevel@tonic-gate 	mdb_free(wsp->walk_data, wsp->walk_addr); /* walk_addr has size */
3810Sstevel@tonic-gate }
3820Sstevel@tonic-gate 
3830Sstevel@tonic-gate /*
3840Sstevel@tonic-gate  * ==================== threads ==========================
3850Sstevel@tonic-gate  * These are the interfaces that used to require libthread.
3860Sstevel@tonic-gate  * Now, libthread has been folded into libc.
3870Sstevel@tonic-gate  * =======================================================
3880Sstevel@tonic-gate  */
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate /*
3910Sstevel@tonic-gate  * prt_addr() is called up to three times to generate arguments for
3920Sstevel@tonic-gate  * one call to mdb_printf().  We must return at least three different
3930Sstevel@tonic-gate  * pointers to static storage for consecutive calls to prt_addr().
3940Sstevel@tonic-gate  */
3950Sstevel@tonic-gate static const char *
prt_addr(void * addr,int pad)3960Sstevel@tonic-gate prt_addr(void *addr, int pad)
3970Sstevel@tonic-gate {
3980Sstevel@tonic-gate 	static char buffer[4][24];
3990Sstevel@tonic-gate 	static int ix = 0;
4000Sstevel@tonic-gate 	char *buf;
4010Sstevel@tonic-gate 
4020Sstevel@tonic-gate 	if (ix == 4)	/* use buffers in sequence: 0, 1, 2, 3 */
4030Sstevel@tonic-gate 		ix = 0;
4040Sstevel@tonic-gate 	buf = buffer[ix++];
4050Sstevel@tonic-gate 	if (addr == NULL)
4060Sstevel@tonic-gate 		return (pad? "<NULL>               " : "<NULL>");
4070Sstevel@tonic-gate 	else {
4080Sstevel@tonic-gate #ifdef _LP64
4090Sstevel@tonic-gate 		(void) mdb_snprintf(buf, sizeof (buffer[0]), "0x%016lx", addr);
4100Sstevel@tonic-gate 		if (pad)
4110Sstevel@tonic-gate 			(void) strcpy(buf + 18, "   ");
4120Sstevel@tonic-gate #else
4130Sstevel@tonic-gate 		(void) mdb_snprintf(buf, sizeof (buffer[0]), "0x%08lx", addr);
4140Sstevel@tonic-gate 		if (pad)
4150Sstevel@tonic-gate 			(void) strcpy(buf + 10, "           ");
4160Sstevel@tonic-gate #endif	/* _LP64 */
4170Sstevel@tonic-gate 		return (buf);
4180Sstevel@tonic-gate 	}
4190Sstevel@tonic-gate }
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate #define	HD(str)		mdb_printf("           " str "\n")
4220Sstevel@tonic-gate #define	OFFSTR		"+0x%-7lx "
4230Sstevel@tonic-gate #define	OFFSET(member)	((size_t)OFFSETOF(ulwp_t, member))
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate /*ARGSUSED*/
4260Sstevel@tonic-gate static int
d_ulwp(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)4270Sstevel@tonic-gate d_ulwp(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
4280Sstevel@tonic-gate {
4290Sstevel@tonic-gate 	ulwp_t ulwp;
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 	if (argc != 0 || !(flags & DCMD_ADDRSPEC))
4320Sstevel@tonic-gate 		return (DCMD_USAGE);
4330Sstevel@tonic-gate 
4340Sstevel@tonic-gate 	if (mdb_vread(&ulwp, sizeof (ulwp), addr) != sizeof (ulwp) &&
4350Sstevel@tonic-gate 	    (bzero(&ulwp, sizeof (ulwp)),
4360Sstevel@tonic-gate 	    mdb_vread(&ulwp, REPLACEMENT_SIZE, addr)) != REPLACEMENT_SIZE) {
4370Sstevel@tonic-gate 		mdb_warn("failed to read ulwp at 0x%p", addr);
4380Sstevel@tonic-gate 		return (DCMD_ERR);
4390Sstevel@tonic-gate 	}
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate 	mdb_printf("%#a\n", addr);
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate 	HD("self                  uberdata");
4440Sstevel@tonic-gate 	mdb_printf(OFFSTR "%s %s\n",
4456515Sraf 	    OFFSET(ul_self),
4466515Sraf 	    prt_addr(ulwp.ul_self, 1),
4476515Sraf 	    prt_addr(ulwp.ul_uberdata, 0));
4480Sstevel@tonic-gate 
4490Sstevel@tonic-gate 	HD("tlsent                ntlsent");
4500Sstevel@tonic-gate 	mdb_printf(OFFSTR "%s %ld\n",
4516515Sraf 	    OFFSET(ul_tlsent),
4526515Sraf 	    prt_addr(ulwp.ul_tlsent, 1),
4536515Sraf 	    ulwp.ul_ntlsent);
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate 	HD("forw                  back                  next");
4560Sstevel@tonic-gate 	mdb_printf(OFFSTR "%s %s %s\n",
4576515Sraf 	    OFFSET(ul_forw),
4586515Sraf 	    prt_addr(ulwp.ul_forw, 1),
4596515Sraf 	    prt_addr(ulwp.ul_back, 1),
4606515Sraf 	    prt_addr(ulwp.ul_next, 0));
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate 	HD("hash                  rval                  stk");
4630Sstevel@tonic-gate 	mdb_printf(OFFSTR "%s %s %s\n",
4646515Sraf 	    OFFSET(ul_hash),
4656515Sraf 	    prt_addr(ulwp.ul_hash, 1),
4666515Sraf 	    prt_addr(ulwp.ul_rval, 1),
4676515Sraf 	    prt_addr(ulwp.ul_stk, 0));
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate 	HD("mapsiz     guardsize  stktop                stksiz");
4700Sstevel@tonic-gate 	mdb_printf(OFFSTR "%-10ld %-10ld %s %ld\n",
4716515Sraf 	    OFFSET(ul_mapsiz),
4726515Sraf 	    ulwp.ul_mapsiz,
4736515Sraf 	    ulwp.ul_guardsize,
4746515Sraf 	    prt_addr((void *)ulwp.ul_stktop, 1),
4756515Sraf 	    ulwp.ul_stksiz);
4760Sstevel@tonic-gate 
4770Sstevel@tonic-gate 	HD("ustack.ss_sp          ustack.ss_size        ustack.ss_flags");
4780Sstevel@tonic-gate 	mdb_printf(OFFSTR "%s %-21ld %s\n",
4796515Sraf 	    OFFSET(ul_ustack.ss_sp),
4806515Sraf 	    prt_addr(ulwp.ul_ustack.ss_sp, 1),
4816515Sraf 	    ulwp.ul_ustack.ss_size,
4826515Sraf 	    stack_flags(&ulwp.ul_ustack));
4830Sstevel@tonic-gate 
4846247Sraf 	HD("ix         lwpid      pri        epri       policy     cid");
4850Sstevel@tonic-gate 	mdb_printf(OFFSTR "%-10d %-10d %-10d %-10d %-10d %d\n",
4866515Sraf 	    OFFSET(ul_ix),
4876515Sraf 	    ulwp.ul_ix,
4886515Sraf 	    ulwp.ul_lwpid,
4896515Sraf 	    ulwp.ul_pri,
4906515Sraf 	    ulwp.ul_epri,
4916515Sraf 	    ulwp.ul_policy,
4926515Sraf 	    ulwp.ul_cid);
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate 	HD("cursig     pleasestop stop       signalled  dead       unwind");
4950Sstevel@tonic-gate 	mdb_printf(OFFSTR "%-10d ",
4966515Sraf 	    OFFSET(ul_cursig),
4976515Sraf 	    ulwp.ul_cursig);
4980Sstevel@tonic-gate 	mdb_printf(ulwp.ul_pleasestop? "0x%-8x " : "%-10d ",
4996515Sraf 	    ulwp.ul_pleasestop);
5000Sstevel@tonic-gate 	mdb_printf(ulwp.ul_stop? "0x%-8x " : "%-10d ",
5016515Sraf 	    ulwp.ul_stop);
5020Sstevel@tonic-gate 	mdb_printf("%-10d %-10d %d\n",
5036515Sraf 	    ulwp.ul_signalled,
5046515Sraf 	    ulwp.ul_dead,
5056515Sraf 	    ulwp.ul_unwind);
5060Sstevel@tonic-gate 
5071885Sraf 	HD("detached   writer     stopping   can'prolog preempt    savpreempt");
5080Sstevel@tonic-gate 	mdb_printf(OFFSTR "%-10d %-10d %-10d %-10d %-10d %d\n",
5096515Sraf 	    OFFSET(ul_detached),
5106515Sraf 	    ulwp.ul_detached,
5116515Sraf 	    ulwp.ul_writer,
5126515Sraf 	    ulwp.ul_stopping,
5136515Sraf 	    ulwp.ul_cancel_prologue,
5146515Sraf 	    ulwp.ul_preempt,
5156515Sraf 	    ulwp.ul_savpreempt);
5160Sstevel@tonic-gate 
5170Sstevel@tonic-gate 	HD("sigsuspend main       fork       primarymap m'spinners d'noreserv");
5180Sstevel@tonic-gate 	mdb_printf(OFFSTR "%-10d %-10d %-10d %-10d %-10d %d\n",
5196515Sraf 	    OFFSET(ul_sigsuspend),
5206515Sraf 	    ulwp.ul_sigsuspend,
5216515Sraf 	    ulwp.ul_main,
5226515Sraf 	    ulwp.ul_fork,
5236515Sraf 	    ulwp.ul_primarymap,
5246515Sraf 	    ulwp.ul_max_spinners,
5256515Sraf 	    ulwp.ul_door_noreserve);
5260Sstevel@tonic-gate 
5276247Sraf 	HD("queue_fifo c'w'defer  e'detect'  async_safe rt         rtqueued");
5280Sstevel@tonic-gate 	mdb_printf(OFFSTR "%-10d %-10d %-10d %-10d %-10d %d\n",
5296515Sraf 	    OFFSET(ul_queue_fifo),
5306515Sraf 	    ulwp.ul_queue_fifo,
5316515Sraf 	    ulwp.ul_cond_wait_defer,
5326515Sraf 	    ulwp.ul_error_detection,
5336515Sraf 	    ulwp.ul_async_safe,
5346515Sraf 	    ulwp.ul_rt,
5356515Sraf 	    ulwp.ul_rtqueued);
5360Sstevel@tonic-gate 
5377255Sraf 	HD("misaligned adapt'spin queue_spin critical   sigdefer   vfork");
5387255Sraf 	mdb_printf(OFFSTR "%-10d %-10d %-10d %-10d %-10d %d\n",
5397255Sraf 	    OFFSET(ul_misaligned),
5407255Sraf 	    ulwp.ul_misaligned,
5416515Sraf 	    ulwp.ul_adaptive_spin,
5426515Sraf 	    ulwp.ul_queue_spin,
5436515Sraf 	    ulwp.ul_critical,
5446515Sraf 	    ulwp.ul_sigdefer,
5456515Sraf 	    ulwp.ul_vfork);
5460Sstevel@tonic-gate 
5470Sstevel@tonic-gate 	HD("cancelable c'pending  c'disabled c'async    save_async mutator");
5480Sstevel@tonic-gate 	mdb_printf(OFFSTR "%-10d %-10d %-10d %-10d %-10d %d\n",
5496515Sraf 	    OFFSET(ul_cancelable),
5506515Sraf 	    ulwp.ul_cancelable,
5516515Sraf 	    ulwp.ul_cancel_pending,
5526515Sraf 	    ulwp.ul_cancel_disabled,
5536515Sraf 	    ulwp.ul_cancel_async,
5546515Sraf 	    ulwp.ul_save_async,
5556515Sraf 	    ulwp.ul_mutator);
5560Sstevel@tonic-gate 
5570Sstevel@tonic-gate 	HD("created    replace    nocancel   errno      errnop");
5580Sstevel@tonic-gate 	mdb_printf(OFFSTR "%-10d %-10d %-10d %-10d %s\n",
5596515Sraf 	    OFFSET(ul_created),
5606515Sraf 	    ulwp.ul_created,
5616515Sraf 	    ulwp.ul_replace,
5626515Sraf 	    ulwp.ul_nocancel,
5636515Sraf 	    ulwp.ul_errno,
5646515Sraf 	    prt_addr(ulwp.ul_errnop, 0));
5650Sstevel@tonic-gate 
5660Sstevel@tonic-gate 	HD("clnup_hdr             schedctl_called       schedctl");
5670Sstevel@tonic-gate 	mdb_printf(OFFSTR "%s %s %s\n",
5686515Sraf 	    OFFSET(ul_clnup_hdr),
5696515Sraf 	    prt_addr(ulwp.ul_clnup_hdr, 1),
5706515Sraf 	    prt_addr(ulwp.ul_schedctl_called, 1),
5716515Sraf 	    prt_addr((void *)ulwp.ul_schedctl, 0));
5720Sstevel@tonic-gate 
5735891Sraf 	HD("bindflags  libc_locks stsd                  &ftsd");
5740Sstevel@tonic-gate 	mdb_printf(OFFSTR,
5756515Sraf 	    OFFSET(ul_bindflags));
5760Sstevel@tonic-gate 	mdb_printf(ulwp.ul_bindflags? "0x%-8x " : "%-10d ",
5776515Sraf 	    ulwp.ul_bindflags);
5785891Sraf 	mdb_printf("%-10d ", ulwp.ul_libc_locks);
5790Sstevel@tonic-gate 	mdb_printf("%s %s\n",
5806515Sraf 	    prt_addr(ulwp.ul_stsd, 1),
5816515Sraf 	    prt_addr((void *)(addr + OFFSET(ul_ftsd[0])), 0));
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate 	HD("eventmask[0..1]       eventnum              eventdata");
5840Sstevel@tonic-gate 	mdb_printf(OFFSTR "0x%08x 0x%08x %-21d %s\n",
5856515Sraf 	    OFFSET(ul_td_evbuf.eventmask.event_bits[0]),
5866515Sraf 	    ulwp.ul_td_evbuf.eventmask.event_bits[0],
5876515Sraf 	    ulwp.ul_td_evbuf.eventmask.event_bits[1],
5886515Sraf 	    ulwp.ul_td_evbuf.eventnum,
5896515Sraf 	    prt_addr(ulwp.ul_td_evbuf.eventdata, 0));
5900Sstevel@tonic-gate 
59110637SRoger.Faulkner@Sun.COM 	HD("td'enable  sync'reg   qtype      cv_wake    rtld       usropts");
59210637SRoger.Faulkner@Sun.COM 	mdb_printf(OFFSTR "%-10d %-10d %-10d %-10d %-10d ",
5936515Sraf 	    OFFSET(ul_td_events_enable),
5946515Sraf 	    ulwp.ul_td_events_enable,
5956515Sraf 	    ulwp.ul_sync_obj_reg,
5966515Sraf 	    ulwp.ul_qtype,
59710637SRoger.Faulkner@Sun.COM 	    ulwp.ul_cv_wake,
59810637SRoger.Faulkner@Sun.COM 	    ulwp.ul_rtld);
5990Sstevel@tonic-gate 	mdb_printf(ulwp.ul_usropts? "0x%x\n" : "%d\n",
6006515Sraf 	    ulwp.ul_usropts);
6010Sstevel@tonic-gate 
6020Sstevel@tonic-gate 	HD("startpc               startarg              wchan");
6030Sstevel@tonic-gate 	mdb_printf(OFFSTR "%s %s %s\n",
6046515Sraf 	    OFFSET(ul_startpc),
6056515Sraf 	    prt_addr((void *)ulwp.ul_startpc, 1),
6066515Sraf 	    prt_addr(ulwp.ul_startarg, 1),
6076515Sraf 	    prt_addr(ulwp.ul_wchan, 0));
6080Sstevel@tonic-gate 
6090Sstevel@tonic-gate 	HD("link                  sleepq                cvmutex");
6100Sstevel@tonic-gate 	mdb_printf(OFFSTR "%s %s %s\n",
6116515Sraf 	    OFFSET(ul_link),
6126515Sraf 	    prt_addr(ulwp.ul_link, 1),
6136515Sraf 	    prt_addr(ulwp.ul_sleepq, 1),
6146515Sraf 	    prt_addr(ulwp.ul_cvmutex, 0));
6150Sstevel@tonic-gate 
6166247Sraf 	HD("mxchain               save_state");
6176247Sraf 	mdb_printf(OFFSTR "%s %d\n",
6186515Sraf 	    OFFSET(ul_mxchain),
6196515Sraf 	    prt_addr(ulwp.ul_mxchain, 1),
6206515Sraf 	    ulwp.ul_save_state);
6210Sstevel@tonic-gate 
6224574Sraf 	HD("rdlockcnt             rd_rwlock             rd_count");
6234574Sraf 	mdb_printf(OFFSTR "%-21d %s %d\n",
6246515Sraf 	    OFFSET(ul_rdlockcnt),
6256515Sraf 	    ulwp.ul_rdlockcnt,
6266515Sraf 	    prt_addr(ulwp.ul_readlock.single.rd_rwlock, 1),
6276515Sraf 	    ulwp.ul_readlock.single.rd_count);
6284574Sraf 
6294574Sraf 	HD("heldlockcnt           heldlocks             tpdp");
6304574Sraf 	mdb_printf(OFFSTR "%-21d %s %s\n",
6316515Sraf 	    OFFSET(ul_heldlockcnt),
6326515Sraf 	    ulwp.ul_heldlockcnt,
6336515Sraf 	    prt_addr(ulwp.ul_heldlocks.single, 1),
6346515Sraf 	    prt_addr(ulwp.ul_tpdp, 0));
6350Sstevel@tonic-gate 
6360Sstevel@tonic-gate 	HD("siglink               s'l'spin   s'l'spin2  s'l'sleep  s'l'wakeup");
6376247Sraf 	mdb_printf(OFFSTR "%s %-10d %-10d %-10d %d\n",
6386515Sraf 	    OFFSET(ul_siglink),
6396515Sraf 	    prt_addr(ulwp.ul_siglink, 1),
6406515Sraf 	    ulwp.ul_spin_lock_spin,
6416515Sraf 	    ulwp.ul_spin_lock_spin2,
6426515Sraf 	    ulwp.ul_spin_lock_sleep,
6436515Sraf 	    ulwp.ul_spin_lock_wakeup);
6440Sstevel@tonic-gate 
6456247Sraf 	HD("&queue_root           rtclassid  pilocks");
6466247Sraf 	mdb_printf(OFFSTR "%s %-10d %d\n",
6476515Sraf 	    OFFSET(ul_queue_root),
6486515Sraf 	    prt_addr((void *)(addr + OFFSET(ul_queue_root)), 1),
6496515Sraf 	    ulwp.ul_rtclassid,
6506515Sraf 	    ulwp.ul_pilocks);
6516247Sraf 
6520Sstevel@tonic-gate 	/*
6530Sstevel@tonic-gate 	 * The remainder of the ulwp_t structure
6540Sstevel@tonic-gate 	 * is invalid if this is a replacement.
6550Sstevel@tonic-gate 	 */
6560Sstevel@tonic-gate 	if (ulwp.ul_replace)
6570Sstevel@tonic-gate 		return (DCMD_OK);
6580Sstevel@tonic-gate 
6590Sstevel@tonic-gate 	HD("sigmask[0..3]");
6600Sstevel@tonic-gate 	mdb_printf(OFFSTR "0x%08x 0x%08x 0x%08x 0x%08x\n",
6616515Sraf 	    OFFSET(ul_sigmask.__sigbits[0]),
6626515Sraf 	    ulwp.ul_sigmask.__sigbits[0],
6636515Sraf 	    ulwp.ul_sigmask.__sigbits[1],
6646515Sraf 	    ulwp.ul_sigmask.__sigbits[2],
6656515Sraf 	    ulwp.ul_sigmask.__sigbits[3]);
6660Sstevel@tonic-gate 
6670Sstevel@tonic-gate 	HD("tmpmask[0..3]");
6680Sstevel@tonic-gate 	mdb_printf(OFFSTR "0x%08x 0x%08x 0x%08x 0x%08x\n",
6696515Sraf 	    OFFSET(ul_tmpmask.__sigbits[0]),
6706515Sraf 	    ulwp.ul_tmpmask.__sigbits[0],
6716515Sraf 	    ulwp.ul_tmpmask.__sigbits[1],
6726515Sraf 	    ulwp.ul_tmpmask.__sigbits[2],
6736515Sraf 	    ulwp.ul_tmpmask.__sigbits[3]);
6740Sstevel@tonic-gate 
6750Sstevel@tonic-gate 	HD("&siginfo              &spinlock             &fpuenv");
6760Sstevel@tonic-gate 	mdb_printf(OFFSTR "%s %s %s\n",
6776515Sraf 	    OFFSET(ul_siginfo),
6786515Sraf 	    prt_addr((void *)(addr + OFFSET(ul_siginfo)), 1),
6796515Sraf 	    prt_addr((void *)(addr + OFFSET(ul_spinlock)), 1),
6806515Sraf 	    prt_addr((void *)(addr + OFFSET(ul_fpuenv)), 0));
6810Sstevel@tonic-gate 
6820Sstevel@tonic-gate 	return (DCMD_OK);
6830Sstevel@tonic-gate }
6840Sstevel@tonic-gate 
6850Sstevel@tonic-gate /*
6860Sstevel@tonic-gate  * Get the address of the unique uberdata_t structure.
6870Sstevel@tonic-gate  */
6880Sstevel@tonic-gate static uintptr_t
uberdata_addr(void)6890Sstevel@tonic-gate uberdata_addr(void)
6900Sstevel@tonic-gate {
6910Sstevel@tonic-gate 	uintptr_t uaddr;
6920Sstevel@tonic-gate 	uintptr_t addr;
6930Sstevel@tonic-gate 	GElf_Sym sym;
6940Sstevel@tonic-gate 
6950Sstevel@tonic-gate 	if (mdb_lookup_by_obj("libc.so.1", "_tdb_bootstrap", &sym) != 0) {
6960Sstevel@tonic-gate 		mdb_warn("cannot find libc.so.1`_tdb_bootstrap");
6970Sstevel@tonic-gate 		return (NULL);
6980Sstevel@tonic-gate 	}
6990Sstevel@tonic-gate 	if (mdb_vread(&addr, sizeof (addr), sym.st_value) == sizeof (addr) &&
7000Sstevel@tonic-gate 	    addr != NULL &&
7010Sstevel@tonic-gate 	    mdb_vread(&uaddr, sizeof (uaddr), addr) == sizeof (uaddr) &&
7020Sstevel@tonic-gate 	    uaddr != NULL) {
7030Sstevel@tonic-gate 		return (uaddr);
7040Sstevel@tonic-gate 	}
7050Sstevel@tonic-gate 	if (mdb_lookup_by_obj("libc.so.1", "_uberdata", &sym) != 0) {
7060Sstevel@tonic-gate 		mdb_warn("cannot find libc.so.1`_uberdata");
7070Sstevel@tonic-gate 		return (NULL);
7080Sstevel@tonic-gate 	}
7090Sstevel@tonic-gate 	return ((uintptr_t)sym.st_value);
7100Sstevel@tonic-gate }
7110Sstevel@tonic-gate 
7120Sstevel@tonic-gate #undef OFFSET
7130Sstevel@tonic-gate #define	OFFSET(member)	((size_t)OFFSETOF(uberdata_t, member))
7140Sstevel@tonic-gate 
7150Sstevel@tonic-gate /*ARGSUSED*/
7160Sstevel@tonic-gate static int
d_uberdata(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)7170Sstevel@tonic-gate d_uberdata(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
7180Sstevel@tonic-gate {
7190Sstevel@tonic-gate 	uberdata_t uberdata;
7200Sstevel@tonic-gate 	int i;
7210Sstevel@tonic-gate 
7220Sstevel@tonic-gate 	if (argc != 0)
7230Sstevel@tonic-gate 		return (DCMD_USAGE);
7240Sstevel@tonic-gate 	if (!(flags & DCMD_ADDRSPEC) && (addr = uberdata_addr()) == NULL)
7250Sstevel@tonic-gate 		return (DCMD_ERR);
7260Sstevel@tonic-gate 
7270Sstevel@tonic-gate 	if (mdb_vread(&uberdata, sizeof (uberdata), addr) !=
7280Sstevel@tonic-gate 	    sizeof (uberdata)) {
7290Sstevel@tonic-gate 		mdb_warn("failed to read uberdata at 0x%p", addr);
7300Sstevel@tonic-gate 		return (DCMD_ERR);
7310Sstevel@tonic-gate 	}
7320Sstevel@tonic-gate 
7330Sstevel@tonic-gate 	mdb_printf("%#a\n", addr);
7340Sstevel@tonic-gate 
7356515Sraf 	HD("&link_lock            &ld_lock              &fork_lock");
7360Sstevel@tonic-gate 	mdb_printf(OFFSTR "%s %s %s\n",
7376515Sraf 	    OFFSET(link_lock),
7386515Sraf 	    prt_addr((void *)(addr + OFFSET(link_lock)), 1),
7396515Sraf 	    prt_addr((void *)(addr + OFFSET(ld_lock)), 1),
7406515Sraf 	    prt_addr((void *)(addr + OFFSET(fork_lock)), 0));
7410Sstevel@tonic-gate 
7426515Sraf 	HD("&atfork_lock          &callout_lock         &tdb_hash_lock");
7430Sstevel@tonic-gate 	mdb_printf(OFFSTR "%s %s %s\n",
7446515Sraf 	    OFFSET(atfork_lock),
7456515Sraf 	    prt_addr((void *)(addr + OFFSET(atfork_lock)), 1),
7466515Sraf 	    prt_addr((void *)(addr + OFFSET(callout_lock)), 1),
7476515Sraf 	    prt_addr((void *)(addr + OFFSET(tdb_hash_lock)), 0));
7486515Sraf 
7496515Sraf 	HD("&tdb_hash_lock_stats  &siguaction[0]");
7506515Sraf 	mdb_printf(OFFSTR "%s %s\n",
7516515Sraf 	    OFFSET(tdb_hash_lock_stats),
7526515Sraf 	    prt_addr((void *)(addr + OFFSET(tdb_hash_lock_stats)), 1),
7536515Sraf 	    prt_addr((void *)(addr + OFFSET(siguaction)), 0));
7540Sstevel@tonic-gate 
7550Sstevel@tonic-gate 	HD("&bucket               free_list             chunks");
7560Sstevel@tonic-gate 	for (i = 0; i < NBUCKETS; i++) {
7570Sstevel@tonic-gate 		mdb_printf(OFFSTR "%s %s %ld\n",
7586515Sraf 		    OFFSET(bucket[i]),
7596515Sraf 		    prt_addr((void *)(addr + OFFSET(bucket[i])), 1),
7606515Sraf 		    prt_addr(uberdata.bucket[i].free_list, 1),
7616515Sraf 		    uberdata.bucket[i].chunks);
7620Sstevel@tonic-gate 	}
7630Sstevel@tonic-gate 
7640Sstevel@tonic-gate 	HD("&atexit_root          head                  exit_frame_monitor");
7650Sstevel@tonic-gate 	mdb_printf(OFFSTR "%s %s %s\n",
7666515Sraf 	    OFFSET(atexit_root),
7676515Sraf 	    prt_addr((void *)(addr + OFFSET(atexit_root.exitfns_lock)), 1),
7686515Sraf 	    prt_addr(uberdata.atexit_root.head, 1),
7696515Sraf 	    prt_addr(uberdata.atexit_root.exit_frame_monitor, 0));
7700Sstevel@tonic-gate 
7710Sstevel@tonic-gate 	HD("&tsd_metadata         tsdm_nkeys tsdm_nused tsdm_destro");
7720Sstevel@tonic-gate 	mdb_printf(OFFSTR "%s %-10d %-10d %s\n",
7736515Sraf 	    OFFSET(tsd_metadata),
7746515Sraf 	    prt_addr((void *)(addr + OFFSET(tsd_metadata.tsdm_lock)), 1),
7756515Sraf 	    uberdata.tsd_metadata.tsdm_nkeys,
7766515Sraf 	    uberdata.tsd_metadata.tsdm_nused,
7776515Sraf 	    prt_addr((void *)uberdata.tsd_metadata.tsdm_destro, 0));
7780Sstevel@tonic-gate 
7790Sstevel@tonic-gate 	HD("&tls_metadata         tls_modinfo.data      tls_modinfo.size");
7800Sstevel@tonic-gate 	mdb_printf(OFFSTR "%s %s %ld\n",
7816515Sraf 	    OFFSET(tls_metadata),
7826515Sraf 	    prt_addr((void *)(addr + OFFSET(tls_metadata.tls_lock)), 1),
7836515Sraf 	    prt_addr(uberdata.tls_metadata.tls_modinfo.tls_data, 1),
7846515Sraf 	    uberdata.tls_metadata.tls_modinfo.tls_size);
7850Sstevel@tonic-gate 
7860Sstevel@tonic-gate 	HD("                      static_tls.data       static_tls.size");
7870Sstevel@tonic-gate 	mdb_printf(OFFSTR "%s %s %ld\n",
7886515Sraf 	    OFFSET(tls_metadata.static_tls),
7896515Sraf 	    "                     ",
7906515Sraf 	    prt_addr(uberdata.tls_metadata.static_tls.tls_data, 1),
7916515Sraf 	    uberdata.tls_metadata.static_tls.tls_size);
7920Sstevel@tonic-gate 
7930Sstevel@tonic-gate 	HD("primary_ma bucket_ini uflags.mt  uflags.pad uflags.trs uflags.ted");
7940Sstevel@tonic-gate 	mdb_printf(OFFSTR "%-10d %-10d %-10d %-10d %-10d %d\n",
7956515Sraf 	    OFFSET(primary_map),
7966515Sraf 	    uberdata.primary_map,
7976515Sraf 	    uberdata.bucket_init,
7986515Sraf 	    uberdata.uberflags.uf_x.x_mt,
7996515Sraf 	    uberdata.uberflags.uf_x.x_pad,
8006515Sraf 	    uberdata.uberflags.uf_x.x_tdb_register_sync,
8016515Sraf 	    uberdata.uberflags.uf_x.x_thread_error_detection);
8020Sstevel@tonic-gate 
8030Sstevel@tonic-gate 	HD("queue_head            thr_hash_table        hash_size  hash_mask");
8040Sstevel@tonic-gate 	mdb_printf(OFFSTR "%s %s %-10d 0x%x\n",
8056515Sraf 	    OFFSET(queue_head),
8066515Sraf 	    prt_addr(uberdata.queue_head, 1),
8076515Sraf 	    prt_addr(uberdata.thr_hash_table, 1),
8086515Sraf 	    uberdata.hash_size,
8096515Sraf 	    uberdata.hash_mask);
8100Sstevel@tonic-gate 
8110Sstevel@tonic-gate 	HD("ulwp_one              all_lwps              all_zombies");
8120Sstevel@tonic-gate 	mdb_printf(OFFSTR "%s %s %s\n",
8136515Sraf 	    OFFSET(ulwp_one),
8146515Sraf 	    prt_addr(uberdata.ulwp_one, 1),
8156515Sraf 	    prt_addr(uberdata.all_lwps, 1),
8166515Sraf 	    prt_addr(uberdata.all_zombies, 0));
8170Sstevel@tonic-gate 
8180Sstevel@tonic-gate 	HD("nthreads   nzombies   ndaemons   pid        sigacthandler");
8190Sstevel@tonic-gate 	mdb_printf(OFFSTR "%-10d %-10d %-10d %-10d %s\n",
8206515Sraf 	    OFFSET(nthreads),
8216515Sraf 	    uberdata.nthreads,
8226515Sraf 	    uberdata.nzombies,
8236515Sraf 	    uberdata.ndaemons,
8246515Sraf 	    (int)uberdata.pid,
8256515Sraf 	    prt_addr((void *)uberdata.sigacthandler, 0));
8260Sstevel@tonic-gate 
8270Sstevel@tonic-gate 	HD("lwp_stacks            lwp_laststack         nfreestack stk_cache");
8280Sstevel@tonic-gate 	mdb_printf(OFFSTR "%s %s %-10d %d\n",
8296515Sraf 	    OFFSET(lwp_stacks),
8306515Sraf 	    prt_addr(uberdata.lwp_stacks, 1),
8316515Sraf 	    prt_addr(uberdata.lwp_laststack, 1),
8326515Sraf 	    uberdata.nfreestack,
8336515Sraf 	    uberdata.thread_stack_cache);
8340Sstevel@tonic-gate 
8356515Sraf 	HD("ulwp_freelist         ulwp_lastfree         ulwp_replace_free");
8366515Sraf 	mdb_printf(OFFSTR "%s %s %s\n",
8376515Sraf 	    OFFSET(ulwp_freelist),
8386515Sraf 	    prt_addr(uberdata.ulwp_freelist, 1),
8396515Sraf 	    prt_addr(uberdata.ulwp_lastfree, 1),
8406515Sraf 	    prt_addr(uberdata.ulwp_replace_free, 0));
8414574Sraf 
8429170SRoger.Faulkner@Sun.COM 	HD("ulwp_replace_last     atforklist");
8439170SRoger.Faulkner@Sun.COM 	mdb_printf(OFFSTR "%s %s\n",
8446515Sraf 	    OFFSET(ulwp_replace_last),
8456515Sraf 	    prt_addr(uberdata.ulwp_replace_last, 1),
8469170SRoger.Faulkner@Sun.COM 	    prt_addr(uberdata.atforklist, 0));
8479170SRoger.Faulkner@Sun.COM 
848*13093SRoger.Faulkner@Oracle.COM 	HD("robustlocks           robustlist            progname");
849*13093SRoger.Faulkner@Oracle.COM 	mdb_printf(OFFSTR "%s %s %s\n",
8509170SRoger.Faulkner@Sun.COM 	    OFFSET(robustlocks),
8519170SRoger.Faulkner@Sun.COM 	    prt_addr(uberdata.robustlocks, 1),
852*13093SRoger.Faulkner@Oracle.COM 	    prt_addr(uberdata.robustlist, 1),
853*13093SRoger.Faulkner@Oracle.COM 	    prt_addr(uberdata.progname, 0));
8540Sstevel@tonic-gate 
8550Sstevel@tonic-gate 	HD("tdb_bootstrap         tdb_sync_addr_hash    tdb_'count tdb_'fail");
8560Sstevel@tonic-gate 	mdb_printf(OFFSTR "%s %s %-10d %d\n",
8576515Sraf 	    OFFSET(tdb_bootstrap),
8586515Sraf 	    prt_addr(uberdata.tdb_bootstrap, 1),
8596515Sraf 	    prt_addr(uberdata.tdb.tdb_sync_addr_hash, 1),
8606515Sraf 	    uberdata.tdb.tdb_register_count,
8616515Sraf 	    uberdata.tdb.tdb_hash_alloc_failed);
8620Sstevel@tonic-gate 
8630Sstevel@tonic-gate 	HD("tdb_sync_addr_free    tdb_sync_addr_last    tdb_sync_alloc");
8640Sstevel@tonic-gate 	mdb_printf(OFFSTR "%s %s %ld\n",
8656515Sraf 	    OFFSET(tdb.tdb_sync_addr_free),
8666515Sraf 	    prt_addr(uberdata.tdb.tdb_sync_addr_free, 1),
8676515Sraf 	    prt_addr(uberdata.tdb.tdb_sync_addr_last, 1),
8686515Sraf 	    uberdata.tdb.tdb_sync_alloc);
8690Sstevel@tonic-gate 
8700Sstevel@tonic-gate 	HD("tdb_ev_global_mask    tdb_events");
8710Sstevel@tonic-gate 	mdb_printf(OFFSTR "0x%08x 0x%08x %s\n",
8726515Sraf 	    OFFSET(tdb.tdb_ev_global_mask),
8736515Sraf 	    uberdata.tdb.tdb_ev_global_mask.event_bits[0],
8746515Sraf 	    uberdata.tdb.tdb_ev_global_mask.event_bits[1],
8756515Sraf 	    prt_addr((void *)uberdata.tdb.tdb_events, 0));
8760Sstevel@tonic-gate 
8770Sstevel@tonic-gate 	return (DCMD_OK);
8780Sstevel@tonic-gate }
8790Sstevel@tonic-gate 
8800Sstevel@tonic-gate static int
ulwp_walk_init(mdb_walk_state_t * wsp)8810Sstevel@tonic-gate ulwp_walk_init(mdb_walk_state_t *wsp)
8820Sstevel@tonic-gate {
8830Sstevel@tonic-gate 	uintptr_t addr = wsp->walk_addr;
8840Sstevel@tonic-gate 	uintptr_t uber_addr;
8850Sstevel@tonic-gate 
8860Sstevel@tonic-gate 	if (addr == NULL &&
8870Sstevel@tonic-gate 	    ((uber_addr = uberdata_addr()) == NULL ||
8880Sstevel@tonic-gate 	    mdb_vread(&addr, sizeof (addr),
8890Sstevel@tonic-gate 	    uber_addr + OFFSETOF(uberdata_t, all_lwps))
8900Sstevel@tonic-gate 	    != sizeof (addr))) {
8910Sstevel@tonic-gate 		mdb_warn("cannot find 'uberdata.all_lwps'");
8920Sstevel@tonic-gate 		return (WALK_ERR);
8930Sstevel@tonic-gate 	}
8940Sstevel@tonic-gate 	if (addr == NULL)
8950Sstevel@tonic-gate 		return (WALK_DONE);
8960Sstevel@tonic-gate 	wsp->walk_addr = addr;
8970Sstevel@tonic-gate 	wsp->walk_data = (void *)addr;
8980Sstevel@tonic-gate 	return (WALK_NEXT);
8990Sstevel@tonic-gate }
9000Sstevel@tonic-gate 
9010Sstevel@tonic-gate static int
ulwp_walk_step(mdb_walk_state_t * wsp)9020Sstevel@tonic-gate ulwp_walk_step(mdb_walk_state_t *wsp)
9030Sstevel@tonic-gate {
9040Sstevel@tonic-gate 	uintptr_t addr = wsp->walk_addr;
9050Sstevel@tonic-gate 	ulwp_t ulwp;
9060Sstevel@tonic-gate 
9070Sstevel@tonic-gate 	if (addr == NULL)
9080Sstevel@tonic-gate 		return (WALK_DONE);
9090Sstevel@tonic-gate 	if (mdb_vread(&ulwp, sizeof (ulwp), addr) != sizeof (ulwp) &&
9100Sstevel@tonic-gate 	    (bzero(&ulwp, sizeof (ulwp)),
9110Sstevel@tonic-gate 	    mdb_vread(&ulwp, REPLACEMENT_SIZE, addr)) != REPLACEMENT_SIZE) {
9120Sstevel@tonic-gate 		mdb_warn("failed to read ulwp at 0x%p", addr);
9130Sstevel@tonic-gate 		return (WALK_ERR);
9140Sstevel@tonic-gate 	}
9150Sstevel@tonic-gate 	/*
9160Sstevel@tonic-gate 	 * If we have looped around to the beginning
9170Sstevel@tonic-gate 	 * of the circular linked list, we are done.
9180Sstevel@tonic-gate 	 */
9190Sstevel@tonic-gate 	if ((wsp->walk_addr = (uintptr_t)ulwp.ul_forw)
9200Sstevel@tonic-gate 	    == (uintptr_t)wsp->walk_data)
9210Sstevel@tonic-gate 		wsp->walk_addr = NULL;
9220Sstevel@tonic-gate 	return (wsp->walk_callback(addr, &ulwp, wsp->walk_cbdata));
9230Sstevel@tonic-gate }
9240Sstevel@tonic-gate 
92510610SJonathan.Adams@Sun.COM /* Avoid classifying NULL pointers as part of the main stack on x86 */
92610610SJonathan.Adams@Sun.COM #define	MIN_STACK_ADDR		(0x10000ul)
92710610SJonathan.Adams@Sun.COM 
92810610SJonathan.Adams@Sun.COM static int
whatis_walk_ulwp(uintptr_t addr,const ulwp_t * ulwp,mdb_whatis_t * w)92910610SJonathan.Adams@Sun.COM whatis_walk_ulwp(uintptr_t addr, const ulwp_t *ulwp, mdb_whatis_t *w)
93010610SJonathan.Adams@Sun.COM {
93110610SJonathan.Adams@Sun.COM 	uintptr_t cur;
93210610SJonathan.Adams@Sun.COM 	lwpid_t id = ulwp->ul_lwpid;
93310610SJonathan.Adams@Sun.COM 	uintptr_t top, base, size;
93410610SJonathan.Adams@Sun.COM 
93510610SJonathan.Adams@Sun.COM 	while (mdb_whatis_match(w, addr, sizeof (ulwp_t), &cur))
93610610SJonathan.Adams@Sun.COM 		mdb_whatis_report_object(w, cur, addr,
93710610SJonathan.Adams@Sun.COM 		    "allocated as thread %#r's ulwp_t\n", id);
93810610SJonathan.Adams@Sun.COM 
93910610SJonathan.Adams@Sun.COM 	top = (uintptr_t)ulwp->ul_stktop;
94010610SJonathan.Adams@Sun.COM 	size = ulwp->ul_stksiz;
94110610SJonathan.Adams@Sun.COM 
94210610SJonathan.Adams@Sun.COM 	/*
94310610SJonathan.Adams@Sun.COM 	 * The main stack ends up being a little weird, especially if
94410610SJonathan.Adams@Sun.COM 	 * the stack ulimit is unlimited.  This tries to take that into
94510610SJonathan.Adams@Sun.COM 	 * account.
94610610SJonathan.Adams@Sun.COM 	 */
94710610SJonathan.Adams@Sun.COM 	if (size > top)
94810610SJonathan.Adams@Sun.COM 		size = top;
94910610SJonathan.Adams@Sun.COM 	if (top > MIN_STACK_ADDR && top - size < MIN_STACK_ADDR)
95010610SJonathan.Adams@Sun.COM 		size = top - MIN_STACK_ADDR;
95110610SJonathan.Adams@Sun.COM 
95210610SJonathan.Adams@Sun.COM 	base = top - size;
95310610SJonathan.Adams@Sun.COM 
95410610SJonathan.Adams@Sun.COM 	while (mdb_whatis_match(w, base, size, &cur))
95510610SJonathan.Adams@Sun.COM 		mdb_whatis_report_address(w, cur, "in [ stack tid=%#r ]\n", id);
95610610SJonathan.Adams@Sun.COM 
95710610SJonathan.Adams@Sun.COM 	if (ulwp->ul_ustack.ss_flags & SS_ONSTACK) {
95810610SJonathan.Adams@Sun.COM 		base = (uintptr_t)ulwp->ul_ustack.ss_sp;
95910610SJonathan.Adams@Sun.COM 		size = ulwp->ul_ustack.ss_size;
96010610SJonathan.Adams@Sun.COM 
96110610SJonathan.Adams@Sun.COM 		while (mdb_whatis_match(w, base, size, &cur))
96210610SJonathan.Adams@Sun.COM 			mdb_whatis_report_address(w, cur,
96310610SJonathan.Adams@Sun.COM 			    "in [ altstack tid=%#r ]\n", id);
96410610SJonathan.Adams@Sun.COM 	}
96510610SJonathan.Adams@Sun.COM 
96610610SJonathan.Adams@Sun.COM 	return (WHATIS_WALKRET(w));
96710610SJonathan.Adams@Sun.COM }
96810610SJonathan.Adams@Sun.COM 
96910610SJonathan.Adams@Sun.COM /*ARGSUSED*/
97010610SJonathan.Adams@Sun.COM static int
whatis_run_ulwps(mdb_whatis_t * w,void * arg)97110610SJonathan.Adams@Sun.COM whatis_run_ulwps(mdb_whatis_t *w, void *arg)
97210610SJonathan.Adams@Sun.COM {
97310610SJonathan.Adams@Sun.COM 	if (mdb_walk("ulwps", (mdb_walk_cb_t)whatis_walk_ulwp, w) == -1) {
97410610SJonathan.Adams@Sun.COM 		mdb_warn("couldn't find ulwps walker");
97510610SJonathan.Adams@Sun.COM 		return (1);
97610610SJonathan.Adams@Sun.COM 	}
97710610SJonathan.Adams@Sun.COM 	return (0);
97810610SJonathan.Adams@Sun.COM }
97910610SJonathan.Adams@Sun.COM 
9800Sstevel@tonic-gate /*
9810Sstevel@tonic-gate  * =======================================================
9820Sstevel@tonic-gate  * End of thread (previously libthread) interfaces.
9830Sstevel@tonic-gate  * ==================== threads ==========================
9840Sstevel@tonic-gate  */
9850Sstevel@tonic-gate 
98612902SBryan.Cantrill@Sun.COM int
stacks_dcmd(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)98712902SBryan.Cantrill@Sun.COM stacks_dcmd(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
98812902SBryan.Cantrill@Sun.COM {
98912902SBryan.Cantrill@Sun.COM 	int rval = stacks(addr, flags, argc, argv);
99012902SBryan.Cantrill@Sun.COM 
99112902SBryan.Cantrill@Sun.COM 	/*
99212902SBryan.Cantrill@Sun.COM 	 * For the user-level variant of ::stacks, we don't bother caching
99312902SBryan.Cantrill@Sun.COM 	 * state, as even a very large program is unlikely to compare to the
99412902SBryan.Cantrill@Sun.COM 	 * kernel in terms of number of threads.  (And if you find yourself
99512902SBryan.Cantrill@Sun.COM 	 * here in anger, frustrated about how long ::stacks is running on
99612902SBryan.Cantrill@Sun.COM 	 * your galactically complicated zillion-thread program, hopefully
99712902SBryan.Cantrill@Sun.COM 	 * you will find some solace in the irony.  Okay, probably not...)
99812902SBryan.Cantrill@Sun.COM 	 */
99912902SBryan.Cantrill@Sun.COM 	stacks_cleanup(B_TRUE);
100012902SBryan.Cantrill@Sun.COM 	return (rval);
100112902SBryan.Cantrill@Sun.COM }
100212902SBryan.Cantrill@Sun.COM 
100312902SBryan.Cantrill@Sun.COM typedef struct tid2ulwp_walk {
100412902SBryan.Cantrill@Sun.COM 	lwpid_t t2u_tid;
100512902SBryan.Cantrill@Sun.COM 	uintptr_t t2u_lwp;
100612902SBryan.Cantrill@Sun.COM 	boolean_t t2u_found;
100712902SBryan.Cantrill@Sun.COM } tid2ulwp_walk_t;
100812902SBryan.Cantrill@Sun.COM 
100912902SBryan.Cantrill@Sun.COM /*ARGSUSED*/
101012902SBryan.Cantrill@Sun.COM static int
tid2ulwp_walk(uintptr_t addr,ulwp_t * ulwp,tid2ulwp_walk_t * t2u)101112902SBryan.Cantrill@Sun.COM tid2ulwp_walk(uintptr_t addr, ulwp_t *ulwp, tid2ulwp_walk_t *t2u)
101212902SBryan.Cantrill@Sun.COM {
101312902SBryan.Cantrill@Sun.COM 	if (ulwp->ul_lwpid == t2u->t2u_tid) {
101412902SBryan.Cantrill@Sun.COM 		t2u->t2u_lwp = addr;
101512902SBryan.Cantrill@Sun.COM 		t2u->t2u_found = B_TRUE;
101612902SBryan.Cantrill@Sun.COM 		return (WALK_DONE);
101712902SBryan.Cantrill@Sun.COM 	}
101812902SBryan.Cantrill@Sun.COM 
101912902SBryan.Cantrill@Sun.COM 	return (WALK_NEXT);
102012902SBryan.Cantrill@Sun.COM }
102112902SBryan.Cantrill@Sun.COM 
102212902SBryan.Cantrill@Sun.COM /*ARGSUSED*/
102312902SBryan.Cantrill@Sun.COM static int
tid2ulwp(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)102412902SBryan.Cantrill@Sun.COM tid2ulwp(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
102512902SBryan.Cantrill@Sun.COM {
102612902SBryan.Cantrill@Sun.COM 	tid2ulwp_walk_t t2u;
102712902SBryan.Cantrill@Sun.COM 
102812902SBryan.Cantrill@Sun.COM 	if (argc != 0)
102912902SBryan.Cantrill@Sun.COM 		return (DCMD_USAGE);
103012902SBryan.Cantrill@Sun.COM 
103112902SBryan.Cantrill@Sun.COM 	bzero(&t2u, sizeof (t2u));
103212902SBryan.Cantrill@Sun.COM 	t2u.t2u_tid = (lwpid_t)addr;
103312902SBryan.Cantrill@Sun.COM 
103412902SBryan.Cantrill@Sun.COM 	if (mdb_walk("ulwp", (mdb_walk_cb_t)tid2ulwp_walk, &t2u) != 0) {
103512902SBryan.Cantrill@Sun.COM 		mdb_warn("can't walk 'ulwp'");
103612902SBryan.Cantrill@Sun.COM 		return (DCMD_ERR);
103712902SBryan.Cantrill@Sun.COM 	}
103812902SBryan.Cantrill@Sun.COM 
103912902SBryan.Cantrill@Sun.COM 	if (!t2u.t2u_found) {
104012902SBryan.Cantrill@Sun.COM 		mdb_warn("thread ID %d not found", t2u.t2u_tid);
104112902SBryan.Cantrill@Sun.COM 		return (DCMD_ERR);
104212902SBryan.Cantrill@Sun.COM 	}
104312902SBryan.Cantrill@Sun.COM 
104412902SBryan.Cantrill@Sun.COM 	mdb_printf("%p\n", t2u.t2u_lwp);
104512902SBryan.Cantrill@Sun.COM 
104612902SBryan.Cantrill@Sun.COM 	return (DCMD_OK);
104712902SBryan.Cantrill@Sun.COM }
104812902SBryan.Cantrill@Sun.COM 
10490Sstevel@tonic-gate static const mdb_dcmd_t dcmds[] = {
10500Sstevel@tonic-gate 	{ "jmp_buf", ":", "print jmp_buf contents", d_jmp_buf, NULL },
10510Sstevel@tonic-gate 	{ "sigjmp_buf", ":", "print sigjmp_buf contents", d_sigjmp_buf, NULL },
10520Sstevel@tonic-gate 	{ "siginfo", ":", "print siginfo_t structure", d_siginfo, NULL },
105312902SBryan.Cantrill@Sun.COM 	{ "stacks", "?[-afiv] [-c func] [-C func] [-m module] [-M module] ",
105412902SBryan.Cantrill@Sun.COM 		"print unique thread stacks", stacks_dcmd, stacks_help },
105512902SBryan.Cantrill@Sun.COM 	{ "tid2ulwp", "?", "convert TID to ulwp_t address", tid2ulwp },
10560Sstevel@tonic-gate 	{ "ucontext", ":", "print ucontext_t structure", d_ucontext, NULL },
10570Sstevel@tonic-gate 	{ "ulwp", ":", "print ulwp_t structure", d_ulwp, NULL },
10580Sstevel@tonic-gate 	{ "uberdata", ":", "print uberdata_t structure", d_uberdata, NULL },
10590Sstevel@tonic-gate 	{ NULL }
10600Sstevel@tonic-gate };
10610Sstevel@tonic-gate 
10620Sstevel@tonic-gate static const mdb_walker_t walkers[] = {
10630Sstevel@tonic-gate 	{ "ucontext", "walk ucontext_t uc_link list",
10640Sstevel@tonic-gate 		NULL, uc_walk_step, NULL, NULL },
10650Sstevel@tonic-gate 	{ "oldcontext", "walk per-lwp oldcontext pointers",
10660Sstevel@tonic-gate 		oldc_walk_init, oldc_walk_step, oldc_walk_fini, NULL },
10670Sstevel@tonic-gate 	{ "ulwps", "walk list of ulwp_t pointers",
10680Sstevel@tonic-gate 		ulwp_walk_init, ulwp_walk_step, NULL, NULL },
106912902SBryan.Cantrill@Sun.COM 	{ "ulwp", "walk list of ulwp_t pointers",
107012902SBryan.Cantrill@Sun.COM 		ulwp_walk_init, ulwp_walk_step, NULL, NULL },
10710Sstevel@tonic-gate 	{ NULL }
10720Sstevel@tonic-gate };
10730Sstevel@tonic-gate 
10740Sstevel@tonic-gate static const mdb_modinfo_t modinfo = { MDB_API_VERSION, dcmds, walkers };
10750Sstevel@tonic-gate 
10760Sstevel@tonic-gate const mdb_modinfo_t *
_mdb_init(void)10770Sstevel@tonic-gate _mdb_init(void)
10780Sstevel@tonic-gate {
107910610SJonathan.Adams@Sun.COM 	mdb_whatis_register("threads", whatis_run_ulwps, NULL,
108010610SJonathan.Adams@Sun.COM 	    WHATIS_PRIO_EARLY, WHATIS_REG_NO_ID);
108110610SJonathan.Adams@Sun.COM 
10820Sstevel@tonic-gate 	return (&modinfo);
10830Sstevel@tonic-gate }
1084