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