xref: /titanic_50/usr/src/cmd/mdb/common/modules/libc/libc.c (revision 0a70879558a701a07771af87e7852a12f3bfd438)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright (c) 2012 by Delphix. All rights reserved.
25  */
26 /*
27  * Copyright (c) 2012, Joyent, Inc.  All rights reserved.
28  */
29 
30 #include <sys/mdb_modapi.h>
31 #include <mdb/mdb_whatis.h>
32 #include <mdb/mdb_ctf.h>
33 #include <procfs.h>
34 #include <ucontext.h>
35 #include <siginfo.h>
36 #include <signal.h>
37 #include <setjmp.h>
38 #include <string.h>
39 #include <thr_uberdata.h>
40 #include "findstack.h"
41 
42 static const char *
43 stack_flags(const stack_t *sp)
44 {
45 	static char buf[32];
46 
47 	if (sp->ss_flags == 0)
48 		(void) strcpy(buf, " 0");
49 	else if (sp->ss_flags & ~(SS_ONSTACK | SS_DISABLE))
50 		(void) mdb_snprintf(buf, sizeof (buf), " 0x%x", sp->ss_flags);
51 	else {
52 		buf[0] = '\0';
53 		if (sp->ss_flags & SS_ONSTACK)
54 			(void) strcat(buf, "|ONSTACK");
55 		if (sp->ss_flags & SS_DISABLE)
56 			(void) strcat(buf, "|DISABLE");
57 	}
58 
59 	return (buf + 1);
60 }
61 
62 /*ARGSUSED*/
63 static int
64 d_jmp_buf(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
65 {
66 	jmp_buf jb;
67 	const ulong_t *b = (const ulong_t *)jb;
68 
69 	if (argc != 0)
70 		return (DCMD_USAGE);
71 
72 	if (mdb_vread(&jb, sizeof (jb), addr) != sizeof (jb)) {
73 		mdb_warn("failed to read jmp_buf at %p", addr);
74 		return (DCMD_ERR);
75 	}
76 
77 #if defined(__sparc)
78 	mdb_printf("  %%sp = 0x%lx\n", b[1]);
79 	mdb_printf("  %%pc = 0x%lx %lA\n", b[2], b[2]);
80 	mdb_printf("  %%fp = 0x%lx\n", b[3]);
81 	mdb_printf("  %%i7 = 0x%lx %lA\n", b[4], b[4]);
82 #elif defined(__amd64)
83 	mdb_printf("  %%rbx = 0x%lx\n", b[0]);
84 	mdb_printf("  %%r12 = 0x%lx\n", b[1]);
85 	mdb_printf("  %%r13 = 0x%lx\n", b[2]);
86 	mdb_printf("  %%r14 = 0x%lx\n", b[3]);
87 	mdb_printf("  %%r15 = 0x%lx\n", b[4]);
88 	mdb_printf("  %%rbp = 0x%lx\n", b[5]);
89 	mdb_printf("  %%rsp = 0x%lx\n", b[6]);
90 	mdb_printf("  %%rip = 0x%lx %lA\n", b[7], b[7]);
91 #elif defined(__i386)
92 	mdb_printf("  %%ebx = 0x%lx\n", b[0]);
93 	mdb_printf("  %%esi = 0x%lx\n", b[1]);
94 	mdb_printf("  %%edi = 0x%lx\n", b[2]);
95 	mdb_printf("  %%ebp = 0x%lx\n", b[3]);
96 	mdb_printf("  %%esp = 0x%lx\n", b[4]);
97 	mdb_printf("  %%eip = 0x%lx %lA\n", b[5], b[5]);
98 #endif
99 	return (DCMD_OK);
100 }
101 
102 /*ARGSUSED*/
103 static int
104 d_ucontext(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
105 {
106 	ucontext_t uc;
107 
108 	if (argc != 0)
109 		return (DCMD_USAGE);
110 
111 	if (mdb_vread(&uc, sizeof (uc), addr) != sizeof (uc)) {
112 		mdb_warn("failed to read ucontext at %p", addr);
113 		return (DCMD_ERR);
114 	}
115 
116 	mdb_printf("  flags    = 0x%lx\n", uc.uc_flags);
117 	mdb_printf("  link     = 0x%p\n", uc.uc_link);
118 	mdb_printf("  sigmask  = 0x%08x 0x%08x 0x%08x 0x%08x\n",
119 	    uc.uc_sigmask.__sigbits[0], uc.uc_sigmask.__sigbits[1],
120 	    uc.uc_sigmask.__sigbits[2], uc.uc_sigmask.__sigbits[3]);
121 	mdb_printf("  stack    = sp 0x%p size 0x%lx flags %s\n",
122 	    uc.uc_stack.ss_sp, uc.uc_stack.ss_size, stack_flags(&uc.uc_stack));
123 	mdb_printf("  mcontext = 0x%p\n",
124 	    addr + OFFSETOF(ucontext_t, uc_mcontext));
125 
126 	return (DCMD_OK);
127 }
128 
129 /*ARGSUSED*/
130 static int
131 d_sigjmp_buf(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
132 {
133 #if defined(__sparc)
134 	struct {
135 		int sjs_flags;
136 		greg_t sjs_sp;
137 		greg_t sjs_pc;
138 		greg_t sjs_fp;
139 		greg_t sjs_i7;
140 		ucontext_t *sjs_uclink;
141 		ulong_t sjs_pad[_JBLEN - 6];
142 		sigset_t sjs_sigmask;
143 #if defined(_LP64)
144 		greg_t sjs_asi;
145 		greg_t sjs_fprs;
146 #endif
147 		stack_t sjs_stack;
148 	} s;
149 
150 	if (argc != 0)
151 		return (DCMD_USAGE);
152 
153 	if (mdb_vread(&s, sizeof (s), addr) != sizeof (s)) {
154 		mdb_warn("failed to read sigjmp_buf at %p", addr);
155 		return (DCMD_ERR);
156 	}
157 
158 	mdb_printf("  flags  = 0x%x\n", s.sjs_flags);
159 	mdb_printf("  %%sp    = 0x%lx %lA\n", s.sjs_sp, s.sjs_sp);
160 	mdb_printf("  %%pc    = 0x%lx %lA\n", s.sjs_pc, s.sjs_pc);
161 	mdb_printf("  %%fp    = 0x%lx %lA\n", s.sjs_fp, s.sjs_fp);
162 	mdb_printf("  %%i7    = 0x%lx %lA\n", s.sjs_i7, s.sjs_i7);
163 	mdb_printf("  uclink = %p\n", s.sjs_uclink);
164 	mdb_printf("  sigset = 0x%08x 0x%08x 0x%08x 0x%08x\n",
165 	    s.sjs_sigmask.__sigbits[0], s.sjs_sigmask.__sigbits[1],
166 	    s.sjs_sigmask.__sigbits[2], s.sjs_sigmask.__sigbits[3]);
167 #if defined(_LP64)
168 	mdb_printf("  %%asi   = 0x%lx\n", s.sjs_asi);
169 	mdb_printf("  %%fprs  = 0x%lx\n", s.sjs_fprs);
170 #endif
171 	mdb_printf("  stack  = sp 0x%p size 0x%lx flags %s\n",
172 	    s.sjs_stack.ss_sp, s.sjs_stack.ss_size, stack_flags(&s.sjs_stack));
173 
174 	return (DCMD_OK);
175 
176 #elif defined(__i386) || defined(__amd64)
177 	return (d_ucontext(addr, flags, argc, argv));
178 #endif
179 }
180 
181 /*ARGSUSED*/
182 static int
183 d_siginfo(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
184 {
185 	static const char *const msname[] = {
186 		"USER", "SYSTEM", "TRAP", "TFAULT", "DFAULT", "KFAULT",
187 		"USER_LOCK", "SLEEP", "WAIT_CPU", "STOPPED"
188 	};
189 
190 	char signame[SIG2STR_MAX];
191 	siginfo_t si;
192 	int i;
193 
194 	if (argc != 0)
195 		return (DCMD_USAGE);
196 
197 	if (mdb_vread(&si, sizeof (si), addr) != sizeof (si)) {
198 		mdb_warn("failed to read siginfo at %p", addr);
199 		return (DCMD_ERR);
200 	}
201 
202 	if (sig2str(si.si_signo, signame) == -1)
203 		(void) strcpy(signame, "unknown");
204 
205 	mdb_printf("  signal %5d (%s)\n", si.si_signo, signame);
206 	mdb_printf("  code   %5d (", si.si_code);
207 
208 	switch (si.si_code) {
209 	case SI_NOINFO:
210 		mdb_printf("no info");
211 		break;
212 	case SI_DTRACE:
213 		mdb_printf("from DTrace raise() action");
214 		break;
215 	case SI_RCTL:
216 		mdb_printf("from rctl action");
217 		break;
218 	case SI_USER:
219 		mdb_printf("user generated via kill");
220 		break;
221 	case SI_LWP:
222 		mdb_printf("user generated via lwp_kill");
223 		break;
224 	case SI_QUEUE:
225 		mdb_printf("user generated via sigqueue");
226 		break;
227 	case SI_TIMER:
228 		mdb_printf("from timer expiration");
229 		break;
230 	case SI_ASYNCIO:
231 		mdb_printf("from async i/o completion");
232 		break;
233 	case SI_MESGQ:
234 		mdb_printf("from message arrival");
235 		break;
236 	default:
237 		if (SI_FROMUSER(&si))
238 			mdb_printf("from user process");
239 		else
240 			mdb_printf("from kernel");
241 	}
242 
243 	mdb_printf(")\n  errno  %5d (%s)\n",
244 	    si.si_errno, strerror(si.si_errno));
245 
246 	if (si.si_code == SI_USER || si.si_code == SI_QUEUE) {
247 		mdb_printf("  signal sent from PID %d (uid %d)\n",
248 		    si.si_pid, si.si_uid);
249 	}
250 
251 	if (si.si_code == SI_QUEUE) {
252 		mdb_printf("  signal value = 0t%d / %p\n",
253 		    si.si_value.sival_int, si.si_value.sival_ptr);
254 	}
255 
256 	switch (si.si_signo) {
257 	case SIGCLD:
258 		mdb_printf("  signal sent from child PID %d (uid %d)\n",
259 		    si.si_pid, si.si_uid);
260 		mdb_printf("  usr time = 0t%ld ticks, sys time = 0t%ld ticks\n",
261 		    si.si_utime, si.si_stime);
262 		mdb_printf("  wait status = 0x%x\n", si.si_status);
263 		break;
264 
265 	case SIGSEGV:
266 	case SIGBUS:
267 	case SIGILL:
268 	case SIGTRAP:
269 	case SIGFPE:
270 		mdb_printf("  fault address = 0x%p\n  trapno = %d\n",
271 		    si.si_addr, si.si_trapno);
272 		mdb_printf("  instruction address = 0x%p %lA\n",
273 		    si.si_pc, si.si_pc);
274 		break;
275 
276 	case SIGPOLL:
277 	case SIGXFSZ:
278 		mdb_printf("  fd = %d  band = 0x%lx\n",
279 		    si.si_fd, si.si_band);
280 		break;
281 
282 	case SIGPROF:
283 		mdb_printf("  last fault address = 0x%p fault type = %d\n",
284 		    si.si_faddr, si.si_fault);
285 		mdb_printf("  timestamp = 0t%ld sec 0t%ld nsec\n",
286 		    si.si_tstamp.tv_sec, si.si_tstamp.tv_nsec);
287 
288 		if (si.__data.__prof.__syscall != 0) {
289 			mdb_printf("  system call %d (", si.si_syscall);
290 			if (si.si_nsysarg > 0) {
291 				mdb_printf("%lx", si.si_sysarg[0]);
292 				for (i = 1; i < si.si_nsysarg; i++)
293 					mdb_printf(", %lx", si.si_sysarg[i]);
294 			}
295 			mdb_printf("  )\n");
296 		}
297 
298 		for (i = 0; i < sizeof (msname) / sizeof (msname[0]); i++) {
299 			mdb_printf("  mstate[\"%s\"] = %d\n",
300 			    msname[i], si.si_mstate[i]);
301 		}
302 		break;
303 	}
304 
305 	return (DCMD_OK);
306 }
307 
308 static int
309 uc_walk_step(mdb_walk_state_t *wsp)
310 {
311 	uintptr_t addr = wsp->walk_addr;
312 	ucontext_t uc;
313 
314 	if (addr == NULL)
315 		return (WALK_DONE);
316 
317 	if (mdb_vread(&uc, sizeof (uc), addr) != sizeof (uc)) {
318 		mdb_warn("failed to read ucontext at %p", addr);
319 		return (WALK_ERR);
320 	}
321 
322 	wsp->walk_addr = (uintptr_t)uc.uc_link;
323 	return (wsp->walk_callback(addr, &uc, wsp->walk_cbdata));
324 }
325 
326 static int
327 oldc_walk_init(mdb_walk_state_t *wsp)
328 {
329 	ssize_t nbytes = mdb_get_xdata("lwpstatus", NULL, 0);
330 
331 	if (nbytes <= 0) {
332 		mdb_warn("lwpstatus information not available");
333 		return (WALK_ERR);
334 	}
335 
336 	if (wsp->walk_addr != NULL) {
337 		mdb_warn("walker only supports global walk\n");
338 		return (WALK_ERR);
339 	}
340 
341 	wsp->walk_addr = nbytes; /* Use walk_addr to track size */
342 	wsp->walk_data = mdb_alloc(nbytes, UM_SLEEP);
343 
344 	if (mdb_get_xdata("lwpstatus", wsp->walk_data, nbytes) != nbytes) {
345 		mdb_warn("failed to read lwpstatus information");
346 		mdb_free(wsp->walk_data, nbytes);
347 		return (WALK_ERR);
348 	}
349 
350 	wsp->walk_arg = wsp->walk_data; /* Use walk_arg to track pointer */
351 	return (WALK_NEXT);
352 }
353 
354 static int
355 oldc_walk_step(mdb_walk_state_t *wsp)
356 {
357 	const lwpstatus_t *lsp, *end;
358 
359 	end = (const lwpstatus_t *)((uintptr_t)wsp->walk_data + wsp->walk_addr);
360 	lsp = wsp->walk_arg;
361 
362 	wsp->walk_arg = (void *)(lsp + 1);
363 
364 	if (lsp < end) {
365 		uintptr_t addr = lsp->pr_oldcontext;
366 		ucontext_t uc;
367 
368 		if (addr == NULL)
369 			return (WALK_NEXT);
370 
371 		if (mdb_vread(&uc, sizeof (uc), addr) != sizeof (uc)) {
372 			mdb_warn("failed to read ucontext at %p", addr);
373 			return (WALK_NEXT);
374 		}
375 
376 		return (wsp->walk_callback(addr, &uc, wsp->walk_cbdata));
377 	}
378 
379 	return (WALK_DONE);
380 }
381 
382 static void
383 oldc_walk_fini(mdb_walk_state_t *wsp)
384 {
385 	mdb_free(wsp->walk_data, wsp->walk_addr); /* walk_addr has size */
386 }
387 
388 /*
389  * ==================== threads ==========================
390  * These are the interfaces that used to require libthread.
391  * Now, libthread has been folded into libc.
392  * =======================================================
393  */
394 
395 /*
396  * prt_addr() is called up to three times to generate arguments for
397  * one call to mdb_printf().  We must return at least three different
398  * pointers to static storage for consecutive calls to prt_addr().
399  */
400 static const char *
401 prt_addr(void *addr, int pad)
402 {
403 	static char buffer[4][24];
404 	static int ix = 0;
405 	char *buf;
406 
407 	if (ix == 4)	/* use buffers in sequence: 0, 1, 2, 3 */
408 		ix = 0;
409 	buf = buffer[ix++];
410 	if (addr == NULL)
411 		return (pad? "<NULL>               " : "<NULL>");
412 	else {
413 #ifdef _LP64
414 		(void) mdb_snprintf(buf, sizeof (buffer[0]), "0x%016lx", addr);
415 		if (pad)
416 			(void) strcpy(buf + 18, "   ");
417 #else
418 		(void) mdb_snprintf(buf, sizeof (buffer[0]), "0x%08lx", addr);
419 		if (pad)
420 			(void) strcpy(buf + 10, "           ");
421 #endif	/* _LP64 */
422 		return (buf);
423 	}
424 }
425 
426 #define	HD(str)		mdb_printf("           " str "\n")
427 #define	OFFSTR		"+0x%-7lx "
428 #define	OFFSET(member)	((size_t)OFFSETOF(ulwp_t, member))
429 
430 /*ARGSUSED*/
431 static int
432 d_ulwp(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
433 {
434 	ulwp_t ulwp;
435 
436 	if (argc != 0 || !(flags & DCMD_ADDRSPEC))
437 		return (DCMD_USAGE);
438 
439 	if (mdb_vread(&ulwp, sizeof (ulwp), addr) != sizeof (ulwp) &&
440 	    (bzero(&ulwp, sizeof (ulwp)),
441 	    mdb_vread(&ulwp, REPLACEMENT_SIZE, addr)) != REPLACEMENT_SIZE) {
442 		mdb_warn("failed to read ulwp at 0x%p", addr);
443 		return (DCMD_ERR);
444 	}
445 
446 	mdb_printf("%#a\n", addr);
447 
448 	HD("self                  uberdata");
449 	mdb_printf(OFFSTR "%s %s\n",
450 	    OFFSET(ul_self),
451 	    prt_addr(ulwp.ul_self, 1),
452 	    prt_addr(ulwp.ul_uberdata, 0));
453 
454 	HD("tlsent                ntlsent");
455 	mdb_printf(OFFSTR "%s %ld\n",
456 	    OFFSET(ul_tlsent),
457 	    prt_addr(ulwp.ul_tlsent, 1),
458 	    ulwp.ul_ntlsent);
459 
460 	HD("forw                  back                  next");
461 	mdb_printf(OFFSTR "%s %s %s\n",
462 	    OFFSET(ul_forw),
463 	    prt_addr(ulwp.ul_forw, 1),
464 	    prt_addr(ulwp.ul_back, 1),
465 	    prt_addr(ulwp.ul_next, 0));
466 
467 	HD("hash                  rval                  stk");
468 	mdb_printf(OFFSTR "%s %s %s\n",
469 	    OFFSET(ul_hash),
470 	    prt_addr(ulwp.ul_hash, 1),
471 	    prt_addr(ulwp.ul_rval, 1),
472 	    prt_addr(ulwp.ul_stk, 0));
473 
474 	HD("mapsiz     guardsize  stktop                stksiz");
475 	mdb_printf(OFFSTR "%-10ld %-10ld %s %ld\n",
476 	    OFFSET(ul_mapsiz),
477 	    ulwp.ul_mapsiz,
478 	    ulwp.ul_guardsize,
479 	    prt_addr((void *)ulwp.ul_stktop, 1),
480 	    ulwp.ul_stksiz);
481 
482 	HD("ustack.ss_sp          ustack.ss_size        ustack.ss_flags");
483 	mdb_printf(OFFSTR "%s %-21ld %s\n",
484 	    OFFSET(ul_ustack.ss_sp),
485 	    prt_addr(ulwp.ul_ustack.ss_sp, 1),
486 	    ulwp.ul_ustack.ss_size,
487 	    stack_flags(&ulwp.ul_ustack));
488 
489 	HD("ix         lwpid      pri        epri       policy     cid");
490 	mdb_printf(OFFSTR "%-10d %-10d %-10d %-10d %-10d %d\n",
491 	    OFFSET(ul_ix),
492 	    ulwp.ul_ix,
493 	    ulwp.ul_lwpid,
494 	    ulwp.ul_pri,
495 	    ulwp.ul_epri,
496 	    ulwp.ul_policy,
497 	    ulwp.ul_cid);
498 
499 	HD("cursig     pleasestop stop       signalled  dead       unwind");
500 	mdb_printf(OFFSTR "%-10d ",
501 	    OFFSET(ul_cursig),
502 	    ulwp.ul_cursig);
503 	mdb_printf(ulwp.ul_pleasestop? "0x%-8x " : "%-10d ",
504 	    ulwp.ul_pleasestop);
505 	mdb_printf(ulwp.ul_stop? "0x%-8x " : "%-10d ",
506 	    ulwp.ul_stop);
507 	mdb_printf("%-10d %-10d %d\n",
508 	    ulwp.ul_signalled,
509 	    ulwp.ul_dead,
510 	    ulwp.ul_unwind);
511 
512 	HD("detached   writer     stopping   can'prolog preempt    savpreempt");
513 	mdb_printf(OFFSTR "%-10d %-10d %-10d %-10d %-10d %d\n",
514 	    OFFSET(ul_detached),
515 	    ulwp.ul_detached,
516 	    ulwp.ul_writer,
517 	    ulwp.ul_stopping,
518 	    ulwp.ul_cancel_prologue,
519 	    ulwp.ul_preempt,
520 	    ulwp.ul_savpreempt);
521 
522 	HD("sigsuspend main       fork       primarymap m'spinners d'noreserv");
523 	mdb_printf(OFFSTR "%-10d %-10d %-10d %-10d %-10d %d\n",
524 	    OFFSET(ul_sigsuspend),
525 	    ulwp.ul_sigsuspend,
526 	    ulwp.ul_main,
527 	    ulwp.ul_fork,
528 	    ulwp.ul_primarymap,
529 	    ulwp.ul_max_spinners,
530 	    ulwp.ul_door_noreserve);
531 
532 	HD("queue_fifo c'w'defer  e'detect'  async_safe rt         rtqueued");
533 	mdb_printf(OFFSTR "%-10d %-10d %-10d %-10d %-10d %d\n",
534 	    OFFSET(ul_queue_fifo),
535 	    ulwp.ul_queue_fifo,
536 	    ulwp.ul_cond_wait_defer,
537 	    ulwp.ul_error_detection,
538 	    ulwp.ul_async_safe,
539 	    ulwp.ul_rt,
540 	    ulwp.ul_rtqueued);
541 
542 	HD("misaligned adapt'spin queue_spin critical   sigdefer   vfork");
543 	mdb_printf(OFFSTR "%-10d %-10d %-10d %-10d %-10d %d\n",
544 	    OFFSET(ul_misaligned),
545 	    ulwp.ul_misaligned,
546 	    ulwp.ul_adaptive_spin,
547 	    ulwp.ul_queue_spin,
548 	    ulwp.ul_critical,
549 	    ulwp.ul_sigdefer,
550 	    ulwp.ul_vfork);
551 
552 	HD("cancelable c'pending  c'disabled c'async    save_async mutator");
553 	mdb_printf(OFFSTR "%-10d %-10d %-10d %-10d %-10d %d\n",
554 	    OFFSET(ul_cancelable),
555 	    ulwp.ul_cancelable,
556 	    ulwp.ul_cancel_pending,
557 	    ulwp.ul_cancel_disabled,
558 	    ulwp.ul_cancel_async,
559 	    ulwp.ul_save_async,
560 	    ulwp.ul_mutator);
561 
562 	HD("created    replace    nocancel   errno      errnop");
563 	mdb_printf(OFFSTR "%-10d %-10d %-10d %-10d %s\n",
564 	    OFFSET(ul_created),
565 	    ulwp.ul_created,
566 	    ulwp.ul_replace,
567 	    ulwp.ul_nocancel,
568 	    ulwp.ul_errno,
569 	    prt_addr(ulwp.ul_errnop, 0));
570 
571 	HD("clnup_hdr             schedctl_called       schedctl");
572 	mdb_printf(OFFSTR "%s %s %s\n",
573 	    OFFSET(ul_clnup_hdr),
574 	    prt_addr(ulwp.ul_clnup_hdr, 1),
575 	    prt_addr(ulwp.ul_schedctl_called, 1),
576 	    prt_addr((void *)ulwp.ul_schedctl, 0));
577 
578 	HD("bindflags  libc_locks stsd                  &ftsd");
579 	mdb_printf(OFFSTR,
580 	    OFFSET(ul_bindflags));
581 	mdb_printf(ulwp.ul_bindflags? "0x%-8x " : "%-10d ",
582 	    ulwp.ul_bindflags);
583 	mdb_printf("%-10d ", ulwp.ul_libc_locks);
584 	mdb_printf("%s %s\n",
585 	    prt_addr(ulwp.ul_stsd, 1),
586 	    prt_addr((void *)(addr + OFFSET(ul_ftsd[0])), 0));
587 
588 	HD("eventmask[0..1]       eventnum              eventdata");
589 	mdb_printf(OFFSTR "0x%08x 0x%08x %-21d %s\n",
590 	    OFFSET(ul_td_evbuf.eventmask.event_bits[0]),
591 	    ulwp.ul_td_evbuf.eventmask.event_bits[0],
592 	    ulwp.ul_td_evbuf.eventmask.event_bits[1],
593 	    ulwp.ul_td_evbuf.eventnum,
594 	    prt_addr(ulwp.ul_td_evbuf.eventdata, 0));
595 
596 	HD("td'enable  sync'reg   qtype      cv_wake    rtld       usropts");
597 	mdb_printf(OFFSTR "%-10d %-10d %-10d %-10d %-10d ",
598 	    OFFSET(ul_td_events_enable),
599 	    ulwp.ul_td_events_enable,
600 	    ulwp.ul_sync_obj_reg,
601 	    ulwp.ul_qtype,
602 	    ulwp.ul_cv_wake,
603 	    ulwp.ul_rtld);
604 	mdb_printf(ulwp.ul_usropts? "0x%x\n" : "%d\n",
605 	    ulwp.ul_usropts);
606 
607 	HD("startpc               startarg              wchan");
608 	mdb_printf(OFFSTR "%s %s %s\n",
609 	    OFFSET(ul_startpc),
610 	    prt_addr((void *)ulwp.ul_startpc, 1),
611 	    prt_addr(ulwp.ul_startarg, 1),
612 	    prt_addr(ulwp.ul_wchan, 0));
613 
614 	HD("link                  sleepq                cvmutex");
615 	mdb_printf(OFFSTR "%s %s %s\n",
616 	    OFFSET(ul_link),
617 	    prt_addr(ulwp.ul_link, 1),
618 	    prt_addr(ulwp.ul_sleepq, 1),
619 	    prt_addr(ulwp.ul_cvmutex, 0));
620 
621 	HD("mxchain               save_state");
622 	mdb_printf(OFFSTR "%s %d\n",
623 	    OFFSET(ul_mxchain),
624 	    prt_addr(ulwp.ul_mxchain, 1),
625 	    ulwp.ul_save_state);
626 
627 	HD("rdlockcnt             rd_rwlock             rd_count");
628 	mdb_printf(OFFSTR "%-21d %s %d\n",
629 	    OFFSET(ul_rdlockcnt),
630 	    ulwp.ul_rdlockcnt,
631 	    prt_addr(ulwp.ul_readlock.single.rd_rwlock, 1),
632 	    ulwp.ul_readlock.single.rd_count);
633 
634 	HD("heldlockcnt           heldlocks             tpdp");
635 	mdb_printf(OFFSTR "%-21d %s %s\n",
636 	    OFFSET(ul_heldlockcnt),
637 	    ulwp.ul_heldlockcnt,
638 	    prt_addr(ulwp.ul_heldlocks.single, 1),
639 	    prt_addr(ulwp.ul_tpdp, 0));
640 
641 	HD("siglink               s'l'spin   s'l'spin2  s'l'sleep  s'l'wakeup");
642 	mdb_printf(OFFSTR "%s %-10d %-10d %-10d %d\n",
643 	    OFFSET(ul_siglink),
644 	    prt_addr(ulwp.ul_siglink, 1),
645 	    ulwp.ul_spin_lock_spin,
646 	    ulwp.ul_spin_lock_spin2,
647 	    ulwp.ul_spin_lock_sleep,
648 	    ulwp.ul_spin_lock_wakeup);
649 
650 	HD("&queue_root           rtclassid  pilocks");
651 	mdb_printf(OFFSTR "%s %-10d %d\n",
652 	    OFFSET(ul_queue_root),
653 	    prt_addr((void *)(addr + OFFSET(ul_queue_root)), 1),
654 	    ulwp.ul_rtclassid,
655 	    ulwp.ul_pilocks);
656 
657 	/*
658 	 * The remainder of the ulwp_t structure
659 	 * is invalid if this is a replacement.
660 	 */
661 	if (ulwp.ul_replace)
662 		return (DCMD_OK);
663 
664 	HD("sigmask[0..3]");
665 	mdb_printf(OFFSTR "0x%08x 0x%08x 0x%08x 0x%08x\n",
666 	    OFFSET(ul_sigmask.__sigbits[0]),
667 	    ulwp.ul_sigmask.__sigbits[0],
668 	    ulwp.ul_sigmask.__sigbits[1],
669 	    ulwp.ul_sigmask.__sigbits[2],
670 	    ulwp.ul_sigmask.__sigbits[3]);
671 
672 	HD("tmpmask[0..3]");
673 	mdb_printf(OFFSTR "0x%08x 0x%08x 0x%08x 0x%08x\n",
674 	    OFFSET(ul_tmpmask.__sigbits[0]),
675 	    ulwp.ul_tmpmask.__sigbits[0],
676 	    ulwp.ul_tmpmask.__sigbits[1],
677 	    ulwp.ul_tmpmask.__sigbits[2],
678 	    ulwp.ul_tmpmask.__sigbits[3]);
679 
680 	HD("&siginfo              &spinlock             &fpuenv");
681 	mdb_printf(OFFSTR "%s %s %s\n",
682 	    OFFSET(ul_siginfo),
683 	    prt_addr((void *)(addr + OFFSET(ul_siginfo)), 1),
684 	    prt_addr((void *)(addr + OFFSET(ul_spinlock)), 1),
685 	    prt_addr((void *)(addr + OFFSET(ul_fpuenv)), 0));
686 
687 	HD("tmem.size             &tmem.roots");
688 	mdb_printf(OFFSTR "%-21H %s\n",
689 	    OFFSET(ul_tmem),
690 	    ulwp.ul_tmem.tm_size,
691 	    prt_addr((void *)(addr + OFFSET(ul_tmem) + sizeof (size_t)), 0));
692 
693 	return (DCMD_OK);
694 }
695 
696 /*
697  * Get the address of the unique uberdata_t structure.
698  */
699 static uintptr_t
700 uberdata_addr(void)
701 {
702 	uintptr_t uaddr;
703 	uintptr_t addr;
704 	GElf_Sym sym;
705 
706 	if (mdb_lookup_by_obj("libc.so.1", "_tdb_bootstrap", &sym) != 0) {
707 		mdb_warn("cannot find libc.so.1`_tdb_bootstrap");
708 		return (NULL);
709 	}
710 	if (mdb_vread(&addr, sizeof (addr), sym.st_value) == sizeof (addr) &&
711 	    addr != NULL &&
712 	    mdb_vread(&uaddr, sizeof (uaddr), addr) == sizeof (uaddr) &&
713 	    uaddr != NULL) {
714 		return (uaddr);
715 	}
716 	if (mdb_lookup_by_obj("libc.so.1", "_uberdata", &sym) != 0) {
717 		mdb_warn("cannot find libc.so.1`_uberdata");
718 		return (NULL);
719 	}
720 	return ((uintptr_t)sym.st_value);
721 }
722 
723 #undef OFFSET
724 #define	OFFSET(member)	((size_t)OFFSETOF(uberdata_t, member))
725 
726 /*ARGSUSED*/
727 static int
728 d_uberdata(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
729 {
730 	uberdata_t uberdata;
731 	int i;
732 
733 	if (argc != 0)
734 		return (DCMD_USAGE);
735 	if (!(flags & DCMD_ADDRSPEC) && (addr = uberdata_addr()) == NULL)
736 		return (DCMD_ERR);
737 
738 	if (mdb_vread(&uberdata, sizeof (uberdata), addr) !=
739 	    sizeof (uberdata)) {
740 		mdb_warn("failed to read uberdata at 0x%p", addr);
741 		return (DCMD_ERR);
742 	}
743 
744 	mdb_printf("%#a\n", addr);
745 
746 	HD("&link_lock            &ld_lock              &fork_lock");
747 	mdb_printf(OFFSTR "%s %s %s\n",
748 	    OFFSET(link_lock),
749 	    prt_addr((void *)(addr + OFFSET(link_lock)), 1),
750 	    prt_addr((void *)(addr + OFFSET(ld_lock)), 1),
751 	    prt_addr((void *)(addr + OFFSET(fork_lock)), 0));
752 
753 	HD("&atfork_lock          &callout_lock         &tdb_hash_lock");
754 	mdb_printf(OFFSTR "%s %s %s\n",
755 	    OFFSET(atfork_lock),
756 	    prt_addr((void *)(addr + OFFSET(atfork_lock)), 1),
757 	    prt_addr((void *)(addr + OFFSET(callout_lock)), 1),
758 	    prt_addr((void *)(addr + OFFSET(tdb_hash_lock)), 0));
759 
760 	HD("&tdb_hash_lock_stats  &siguaction[0]");
761 	mdb_printf(OFFSTR "%s %s\n",
762 	    OFFSET(tdb_hash_lock_stats),
763 	    prt_addr((void *)(addr + OFFSET(tdb_hash_lock_stats)), 1),
764 	    prt_addr((void *)(addr + OFFSET(siguaction)), 0));
765 
766 	HD("&bucket               free_list             chunks");
767 	for (i = 0; i < NBUCKETS; i++) {
768 		mdb_printf(OFFSTR "%s %s %ld\n",
769 		    OFFSET(bucket[i]),
770 		    prt_addr((void *)(addr + OFFSET(bucket[i])), 1),
771 		    prt_addr(uberdata.bucket[i].free_list, 1),
772 		    uberdata.bucket[i].chunks);
773 	}
774 
775 	HD("&atexit_root          head                  exit_frame_monitor");
776 	mdb_printf(OFFSTR "%s %s %s\n",
777 	    OFFSET(atexit_root),
778 	    prt_addr((void *)(addr + OFFSET(atexit_root.exitfns_lock)), 1),
779 	    prt_addr(uberdata.atexit_root.head, 1),
780 	    prt_addr(uberdata.atexit_root.exit_frame_monitor, 0));
781 
782 	HD("&tsd_metadata         tsdm_nkeys tsdm_nused tsdm_destro");
783 	mdb_printf(OFFSTR "%s %-10d %-10d %s\n",
784 	    OFFSET(tsd_metadata),
785 	    prt_addr((void *)(addr + OFFSET(tsd_metadata.tsdm_lock)), 1),
786 	    uberdata.tsd_metadata.tsdm_nkeys,
787 	    uberdata.tsd_metadata.tsdm_nused,
788 	    prt_addr((void *)uberdata.tsd_metadata.tsdm_destro, 0));
789 
790 	HD("&tls_metadata         tls_modinfo.data      tls_modinfo.size");
791 	mdb_printf(OFFSTR "%s %s %ld\n",
792 	    OFFSET(tls_metadata),
793 	    prt_addr((void *)(addr + OFFSET(tls_metadata.tls_lock)), 1),
794 	    prt_addr(uberdata.tls_metadata.tls_modinfo.tls_data, 1),
795 	    uberdata.tls_metadata.tls_modinfo.tls_size);
796 
797 	HD("                      static_tls.data       static_tls.size");
798 	mdb_printf(OFFSTR "%s %s %ld\n",
799 	    OFFSET(tls_metadata.static_tls),
800 	    "                     ",
801 	    prt_addr(uberdata.tls_metadata.static_tls.tls_data, 1),
802 	    uberdata.tls_metadata.static_tls.tls_size);
803 
804 	HD("primary_ma bucket_ini uflags.mt  uflags.pad uflags.trs uflags.ted");
805 	mdb_printf(OFFSTR "%-10d %-10d %-10d %-10d %-10d %d\n",
806 	    OFFSET(primary_map),
807 	    uberdata.primary_map,
808 	    uberdata.bucket_init,
809 	    uberdata.uberflags.uf_x.x_mt,
810 	    uberdata.uberflags.uf_x.x_pad,
811 	    uberdata.uberflags.uf_x.x_tdb_register_sync,
812 	    uberdata.uberflags.uf_x.x_thread_error_detection);
813 
814 	HD("queue_head            thr_hash_table        hash_size  hash_mask");
815 	mdb_printf(OFFSTR "%s %s %-10d 0x%x\n",
816 	    OFFSET(queue_head),
817 	    prt_addr(uberdata.queue_head, 1),
818 	    prt_addr(uberdata.thr_hash_table, 1),
819 	    uberdata.hash_size,
820 	    uberdata.hash_mask);
821 
822 	HD("ulwp_one              all_lwps              all_zombies");
823 	mdb_printf(OFFSTR "%s %s %s\n",
824 	    OFFSET(ulwp_one),
825 	    prt_addr(uberdata.ulwp_one, 1),
826 	    prt_addr(uberdata.all_lwps, 1),
827 	    prt_addr(uberdata.all_zombies, 0));
828 
829 	HD("nthreads   nzombies   ndaemons   pid        sigacthandler");
830 	mdb_printf(OFFSTR "%-10d %-10d %-10d %-10d %s\n",
831 	    OFFSET(nthreads),
832 	    uberdata.nthreads,
833 	    uberdata.nzombies,
834 	    uberdata.ndaemons,
835 	    (int)uberdata.pid,
836 	    prt_addr((void *)uberdata.sigacthandler, 0));
837 
838 	HD("lwp_stacks            lwp_laststack         nfreestack stk_cache");
839 	mdb_printf(OFFSTR "%s %s %-10d %d\n",
840 	    OFFSET(lwp_stacks),
841 	    prt_addr(uberdata.lwp_stacks, 1),
842 	    prt_addr(uberdata.lwp_laststack, 1),
843 	    uberdata.nfreestack,
844 	    uberdata.thread_stack_cache);
845 
846 	HD("ulwp_freelist         ulwp_lastfree         ulwp_replace_free");
847 	mdb_printf(OFFSTR "%s %s %s\n",
848 	    OFFSET(ulwp_freelist),
849 	    prt_addr(uberdata.ulwp_freelist, 1),
850 	    prt_addr(uberdata.ulwp_lastfree, 1),
851 	    prt_addr(uberdata.ulwp_replace_free, 0));
852 
853 	HD("ulwp_replace_last     atforklist");
854 	mdb_printf(OFFSTR "%s %s\n",
855 	    OFFSET(ulwp_replace_last),
856 	    prt_addr(uberdata.ulwp_replace_last, 1),
857 	    prt_addr(uberdata.atforklist, 0));
858 
859 	HD("robustlocks           robustlist            progname");
860 	mdb_printf(OFFSTR "%s %s %s\n",
861 	    OFFSET(robustlocks),
862 	    prt_addr(uberdata.robustlocks, 1),
863 	    prt_addr(uberdata.robustlist, 1),
864 	    prt_addr(uberdata.progname, 0));
865 
866 	HD("tdb_bootstrap         tdb_sync_addr_hash    tdb_'count tdb_'fail");
867 	mdb_printf(OFFSTR "%s %s %-10d %d\n",
868 	    OFFSET(tdb_bootstrap),
869 	    prt_addr(uberdata.tdb_bootstrap, 1),
870 	    prt_addr(uberdata.tdb.tdb_sync_addr_hash, 1),
871 	    uberdata.tdb.tdb_register_count,
872 	    uberdata.tdb.tdb_hash_alloc_failed);
873 
874 	HD("tdb_sync_addr_free    tdb_sync_addr_last    tdb_sync_alloc");
875 	mdb_printf(OFFSTR "%s %s %ld\n",
876 	    OFFSET(tdb.tdb_sync_addr_free),
877 	    prt_addr(uberdata.tdb.tdb_sync_addr_free, 1),
878 	    prt_addr(uberdata.tdb.tdb_sync_addr_last, 1),
879 	    uberdata.tdb.tdb_sync_alloc);
880 
881 	HD("tdb_ev_global_mask    tdb_events");
882 	mdb_printf(OFFSTR "0x%08x 0x%08x %s\n",
883 	    OFFSET(tdb.tdb_ev_global_mask),
884 	    uberdata.tdb.tdb_ev_global_mask.event_bits[0],
885 	    uberdata.tdb.tdb_ev_global_mask.event_bits[1],
886 	    prt_addr((void *)uberdata.tdb.tdb_events, 0));
887 
888 	return (DCMD_OK);
889 }
890 
891 static int
892 ulwp_walk_init(mdb_walk_state_t *wsp)
893 {
894 	uintptr_t addr = wsp->walk_addr;
895 	uintptr_t uber_addr;
896 
897 	if (addr == NULL &&
898 	    ((uber_addr = uberdata_addr()) == NULL ||
899 	    mdb_vread(&addr, sizeof (addr),
900 	    uber_addr + OFFSETOF(uberdata_t, all_lwps))
901 	    != sizeof (addr))) {
902 		mdb_warn("cannot find 'uberdata.all_lwps'");
903 		return (WALK_ERR);
904 	}
905 	if (addr == NULL)
906 		return (WALK_DONE);
907 	wsp->walk_addr = addr;
908 	wsp->walk_data = (void *)addr;
909 	return (WALK_NEXT);
910 }
911 
912 static int
913 ulwp_walk_step(mdb_walk_state_t *wsp)
914 {
915 	uintptr_t addr = wsp->walk_addr;
916 	ulwp_t ulwp;
917 
918 	if (addr == NULL)
919 		return (WALK_DONE);
920 	if (mdb_vread(&ulwp, sizeof (ulwp), addr) != sizeof (ulwp) &&
921 	    (bzero(&ulwp, sizeof (ulwp)),
922 	    mdb_vread(&ulwp, REPLACEMENT_SIZE, addr)) != REPLACEMENT_SIZE) {
923 		mdb_warn("failed to read ulwp at 0x%p", addr);
924 		return (WALK_ERR);
925 	}
926 	/*
927 	 * If we have looped around to the beginning
928 	 * of the circular linked list, we are done.
929 	 */
930 	if ((wsp->walk_addr = (uintptr_t)ulwp.ul_forw)
931 	    == (uintptr_t)wsp->walk_data)
932 		wsp->walk_addr = NULL;
933 	return (wsp->walk_callback(addr, &ulwp, wsp->walk_cbdata));
934 }
935 
936 /* Avoid classifying NULL pointers as part of the main stack on x86 */
937 #define	MIN_STACK_ADDR		(0x10000ul)
938 
939 static int
940 whatis_walk_ulwp(uintptr_t addr, const ulwp_t *ulwp, mdb_whatis_t *w)
941 {
942 	uintptr_t cur;
943 	lwpid_t id = ulwp->ul_lwpid;
944 	uintptr_t top, base, size;
945 
946 	while (mdb_whatis_match(w, addr, sizeof (ulwp_t), &cur))
947 		mdb_whatis_report_object(w, cur, addr,
948 		    "allocated as thread %#r's ulwp_t\n", id);
949 
950 	top = (uintptr_t)ulwp->ul_stktop;
951 	size = ulwp->ul_stksiz;
952 
953 	/*
954 	 * The main stack ends up being a little weird, especially if
955 	 * the stack ulimit is unlimited.  This tries to take that into
956 	 * account.
957 	 */
958 	if (size > top)
959 		size = top;
960 	if (top > MIN_STACK_ADDR && top - size < MIN_STACK_ADDR)
961 		size = top - MIN_STACK_ADDR;
962 
963 	base = top - size;
964 
965 	while (mdb_whatis_match(w, base, size, &cur))
966 		mdb_whatis_report_address(w, cur, "in [ stack tid=%#r ]\n", id);
967 
968 	if (ulwp->ul_ustack.ss_flags & SS_ONSTACK) {
969 		base = (uintptr_t)ulwp->ul_ustack.ss_sp;
970 		size = ulwp->ul_ustack.ss_size;
971 
972 		while (mdb_whatis_match(w, base, size, &cur))
973 			mdb_whatis_report_address(w, cur,
974 			    "in [ altstack tid=%#r ]\n", id);
975 	}
976 
977 	return (WHATIS_WALKRET(w));
978 }
979 
980 /*ARGSUSED*/
981 static int
982 whatis_run_ulwps(mdb_whatis_t *w, void *arg)
983 {
984 	if (mdb_walk("ulwps", (mdb_walk_cb_t)whatis_walk_ulwp, w) == -1) {
985 		mdb_warn("couldn't find ulwps walker");
986 		return (1);
987 	}
988 	return (0);
989 }
990 
991 /*
992  * =======================================================
993  * End of thread (previously libthread) interfaces.
994  * ==================== threads ==========================
995  */
996 
997 int
998 stacks_dcmd(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
999 {
1000 	int rval = stacks(addr, flags, argc, argv);
1001 
1002 	/*
1003 	 * For the user-level variant of ::stacks, we don't bother caching
1004 	 * state, as even a very large program is unlikely to compare to the
1005 	 * kernel in terms of number of threads.  (And if you find yourself
1006 	 * here in anger, frustrated about how long ::stacks is running on
1007 	 * your galactically complicated zillion-thread program, hopefully
1008 	 * you will find some solace in the irony.  Okay, probably not...)
1009 	 */
1010 	stacks_cleanup(B_TRUE);
1011 	return (rval);
1012 }
1013 
1014 typedef struct tid2ulwp_walk {
1015 	lwpid_t t2u_tid;
1016 	uintptr_t t2u_lwp;
1017 	boolean_t t2u_found;
1018 } tid2ulwp_walk_t;
1019 
1020 /*ARGSUSED*/
1021 static int
1022 tid2ulwp_walk(uintptr_t addr, ulwp_t *ulwp, tid2ulwp_walk_t *t2u)
1023 {
1024 	if (ulwp->ul_lwpid == t2u->t2u_tid) {
1025 		t2u->t2u_lwp = addr;
1026 		t2u->t2u_found = B_TRUE;
1027 		return (WALK_DONE);
1028 	}
1029 
1030 	return (WALK_NEXT);
1031 }
1032 
1033 static int
1034 tid2ulwp_impl(uintptr_t tid_addr, uintptr_t *ulwp_addrp)
1035 {
1036 	tid2ulwp_walk_t t2u;
1037 
1038 	bzero(&t2u, sizeof (t2u));
1039 	t2u.t2u_tid = (lwpid_t)tid_addr;
1040 
1041 	if (mdb_walk("ulwp", (mdb_walk_cb_t)tid2ulwp_walk, &t2u) != 0) {
1042 		mdb_warn("can't walk 'ulwp'");
1043 		return (DCMD_ERR);
1044 	}
1045 
1046 	if (!t2u.t2u_found) {
1047 		mdb_warn("thread ID %d not found", t2u.t2u_tid);
1048 		return (DCMD_ERR);
1049 	}
1050 	*ulwp_addrp = t2u.t2u_lwp;
1051 	return (DCMD_OK);
1052 }
1053 
1054 /*ARGSUSED*/
1055 static int
1056 tid2ulwp(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1057 {
1058 	uintptr_t ulwp_addr;
1059 	int error;
1060 
1061 	if (argc != 0)
1062 		return (DCMD_USAGE);
1063 
1064 	error = tid2ulwp_impl(addr, &ulwp_addr);
1065 	if (error == DCMD_OK)
1066 		mdb_printf("%p\n", ulwp_addr);
1067 	return (error);
1068 }
1069 
1070 typedef struct mdb_libc_ulwp {
1071 	void *ul_ftsd[TSD_NFAST];
1072 	tsd_t *ul_stsd;
1073 } mdb_libc_ulwp_t;
1074 
1075 /*
1076  * Map from thread pointer to tsd for given key
1077  */
1078 static int
1079 d_tsd(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1080 {
1081 	mdb_libc_ulwp_t u;
1082 	uintptr_t ulwp_addr;
1083 	uintptr_t key = NULL;
1084 	void *element = NULL;
1085 
1086 	if (mdb_getopts(argc, argv, 'k', MDB_OPT_UINTPTR, &key, NULL) != argc)
1087 		return (DCMD_USAGE);
1088 
1089 	if (!(flags & DCMD_ADDRSPEC) || key == NULL)
1090 		return (DCMD_USAGE);
1091 
1092 	if (tid2ulwp_impl(addr, &ulwp_addr) != DCMD_OK)
1093 		return (DCMD_ERR);
1094 
1095 	if (mdb_ctf_vread(&u, "ulwp_t", "mdb_libc_ulwp_t", ulwp_addr, 0) == -1)
1096 		return (DCMD_ERR);
1097 
1098 	if (key < TSD_NFAST) {
1099 		element = u.ul_ftsd[key];
1100 	} else if (u.ul_stsd != NULL) {
1101 		uint_t nalloc;
1102 		/* tsd_t is a union, so we can't use ctf_vread() on it. */
1103 		if (mdb_vread(&nalloc, sizeof (nalloc),
1104 		    (uintptr_t)&u.ul_stsd->tsd_nalloc) == -1) {
1105 			mdb_warn("failed to read tsd_t at %p", u.ul_stsd);
1106 			return (DCMD_ERR);
1107 		}
1108 		if (key < nalloc) {
1109 			if (mdb_vread(&element, sizeof (element),
1110 			    (uintptr_t)&u.ul_stsd->tsd_data[key]) == -1) {
1111 				mdb_warn("failed to read tsd_t at %p",
1112 				    u.ul_stsd);
1113 				return (DCMD_ERR);
1114 			}
1115 		}
1116 	}
1117 
1118 	if (element == NULL && (flags & DCMD_PIPE))
1119 		return (DCMD_OK);
1120 
1121 	mdb_printf("%p\n", element);
1122 	return (DCMD_OK);
1123 }
1124 
1125 static const mdb_dcmd_t dcmds[] = {
1126 	{ "jmp_buf", ":", "print jmp_buf contents", d_jmp_buf, NULL },
1127 	{ "sigjmp_buf", ":", "print sigjmp_buf contents", d_sigjmp_buf, NULL },
1128 	{ "siginfo", ":", "print siginfo_t structure", d_siginfo, NULL },
1129 	{ "stacks", "?[-afiv] [-c func] [-C func] [-m module] [-M module] ",
1130 		"print unique thread stacks", stacks_dcmd, stacks_help },
1131 	{ "tid2ulwp", "?", "convert TID to ulwp_t address", tid2ulwp },
1132 	{ "ucontext", ":", "print ucontext_t structure", d_ucontext, NULL },
1133 	{ "ulwp", ":", "print ulwp_t structure", d_ulwp, NULL },
1134 	{ "uberdata", ":", "print uberdata_t structure", d_uberdata, NULL },
1135 	{ "tsd", ":-k key", "print tsd for this thread", d_tsd, NULL },
1136 	{ NULL }
1137 };
1138 
1139 static const mdb_walker_t walkers[] = {
1140 	{ "ucontext", "walk ucontext_t uc_link list",
1141 		NULL, uc_walk_step, NULL, NULL },
1142 	{ "oldcontext", "walk per-lwp oldcontext pointers",
1143 		oldc_walk_init, oldc_walk_step, oldc_walk_fini, NULL },
1144 	{ "ulwps", "walk list of ulwp_t pointers",
1145 		ulwp_walk_init, ulwp_walk_step, NULL, NULL },
1146 	{ "ulwp", "walk list of ulwp_t pointers",
1147 		ulwp_walk_init, ulwp_walk_step, NULL, NULL },
1148 	{ NULL }
1149 };
1150 
1151 static const mdb_modinfo_t modinfo = { MDB_API_VERSION, dcmds, walkers };
1152 
1153 const mdb_modinfo_t *
1154 _mdb_init(void)
1155 {
1156 	mdb_whatis_register("threads", whatis_run_ulwps, NULL,
1157 	    WHATIS_PRIO_EARLY, WHATIS_REG_NO_ID);
1158 
1159 	return (&modinfo);
1160 }
1161