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