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