xref: /titanic_41/usr/src/cmd/mdb/common/modules/libc/libc.c (revision 4df55fde49134f9735f84011f23a767c75e393c7)
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 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <sys/mdb_modapi.h>
28 #include <mdb/mdb_whatis.h>
29 #include <procfs.h>
30 #include <ucontext.h>
31 #include <siginfo.h>
32 #include <signal.h>
33 #include <setjmp.h>
34 #include <string.h>
35 #include <thr_uberdata.h>
36 
37 static const char *
38 stack_flags(const stack_t *sp)
39 {
40 	static char buf[32];
41 
42 	if (sp->ss_flags == 0)
43 		(void) strcpy(buf, " 0");
44 	else if (sp->ss_flags & ~(SS_ONSTACK | SS_DISABLE))
45 		(void) mdb_snprintf(buf, sizeof (buf), " 0x%x", sp->ss_flags);
46 	else {
47 		buf[0] = '\0';
48 		if (sp->ss_flags & SS_ONSTACK)
49 			(void) strcat(buf, "|ONSTACK");
50 		if (sp->ss_flags & SS_DISABLE)
51 			(void) strcat(buf, "|DISABLE");
52 	}
53 
54 	return (buf + 1);
55 }
56 
57 /*ARGSUSED*/
58 static int
59 d_jmp_buf(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
60 {
61 	jmp_buf jb;
62 	const ulong_t *b = (const ulong_t *)jb;
63 
64 	if (argc != 0)
65 		return (DCMD_USAGE);
66 
67 	if (mdb_vread(&jb, sizeof (jb), addr) != sizeof (jb)) {
68 		mdb_warn("failed to read jmp_buf at %p", addr);
69 		return (DCMD_ERR);
70 	}
71 
72 #if defined(__sparc)
73 	mdb_printf("  %%sp = 0x%lx\n", b[1]);
74 	mdb_printf("  %%pc = 0x%lx %lA\n", b[2], b[2]);
75 	mdb_printf("  %%fp = 0x%lx\n", b[3]);
76 	mdb_printf("  %%i7 = 0x%lx %lA\n", b[4], b[4]);
77 #elif defined(__amd64)
78 	mdb_printf("  %%rbx = 0x%lx\n", b[0]);
79 	mdb_printf("  %%r12 = 0x%lx\n", b[1]);
80 	mdb_printf("  %%r13 = 0x%lx\n", b[2]);
81 	mdb_printf("  %%r14 = 0x%lx\n", b[3]);
82 	mdb_printf("  %%r15 = 0x%lx\n", b[4]);
83 	mdb_printf("  %%rbp = 0x%lx\n", b[5]);
84 	mdb_printf("  %%rsp = 0x%lx\n", b[6]);
85 	mdb_printf("  %%rip = 0x%lx %lA\n", b[7], b[7]);
86 #elif defined(__i386)
87 	mdb_printf("  %%ebx = 0x%lx\n", b[0]);
88 	mdb_printf("  %%esi = 0x%lx\n", b[1]);
89 	mdb_printf("  %%edi = 0x%lx\n", b[2]);
90 	mdb_printf("  %%ebp = 0x%lx\n", b[3]);
91 	mdb_printf("  %%esp = 0x%lx\n", b[4]);
92 	mdb_printf("  %%eip = 0x%lx %lA\n", b[5], b[5]);
93 #endif
94 	return (DCMD_OK);
95 }
96 
97 /*ARGSUSED*/
98 static int
99 d_ucontext(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
100 {
101 	ucontext_t uc;
102 
103 	if (argc != 0)
104 		return (DCMD_USAGE);
105 
106 	if (mdb_vread(&uc, sizeof (uc), addr) != sizeof (uc)) {
107 		mdb_warn("failed to read ucontext at %p", addr);
108 		return (DCMD_ERR);
109 	}
110 
111 	mdb_printf("  flags    = 0x%lx\n", uc.uc_flags);
112 	mdb_printf("  link     = 0x%p\n", uc.uc_link);
113 	mdb_printf("  sigmask  = 0x%08x 0x%08x 0x%08x 0x%08x\n",
114 	    uc.uc_sigmask.__sigbits[0], uc.uc_sigmask.__sigbits[1],
115 	    uc.uc_sigmask.__sigbits[2], uc.uc_sigmask.__sigbits[3]);
116 	mdb_printf("  stack    = sp 0x%p size 0x%lx flags %s\n",
117 	    uc.uc_stack.ss_sp, uc.uc_stack.ss_size, stack_flags(&uc.uc_stack));
118 	mdb_printf("  mcontext = 0x%p\n",
119 	    addr + OFFSETOF(ucontext_t, uc_mcontext));
120 
121 	return (DCMD_OK);
122 }
123 
124 /*ARGSUSED*/
125 static int
126 d_sigjmp_buf(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
127 {
128 #if defined(__sparc)
129 	struct {
130 		int sjs_flags;
131 		greg_t sjs_sp;
132 		greg_t sjs_pc;
133 		greg_t sjs_fp;
134 		greg_t sjs_i7;
135 		ucontext_t *sjs_uclink;
136 		ulong_t sjs_pad[_JBLEN - 6];
137 		sigset_t sjs_sigmask;
138 #if defined(_LP64)
139 		ulong_t sjs_pad1[2];
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 	mdb_printf("  stack  = sp 0x%p size 0x%lx flags %s\n",
162 	    s.sjs_stack.ss_sp, s.sjs_stack.ss_size, stack_flags(&s.sjs_stack));
163 
164 	return (DCMD_OK);
165 
166 #elif defined(__i386) || defined(__amd64)
167 	return (d_ucontext(addr, flags, argc, argv));
168 #endif
169 }
170 
171 /*ARGSUSED*/
172 static int
173 d_siginfo(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
174 {
175 	static const char *const msname[] = {
176 		"USER", "SYSTEM", "TRAP", "TFAULT", "DFAULT", "KFAULT",
177 		"USER_LOCK", "SLEEP", "WAIT_CPU", "STOPPED"
178 	};
179 
180 	char signame[SIG2STR_MAX];
181 	siginfo_t si;
182 	int i;
183 
184 	if (argc != 0)
185 		return (DCMD_USAGE);
186 
187 	if (mdb_vread(&si, sizeof (si), addr) != sizeof (si)) {
188 		mdb_warn("failed to read siginfo at %p", addr);
189 		return (DCMD_ERR);
190 	}
191 
192 	if (sig2str(si.si_signo, signame) == -1)
193 		(void) strcpy(signame, "unknown");
194 
195 	mdb_printf("  signal %5d (%s)\n", si.si_signo, signame);
196 	mdb_printf("  code   %5d (", si.si_code);
197 
198 	switch (si.si_code) {
199 	case SI_NOINFO:
200 		mdb_printf("no info");
201 		break;
202 	case SI_DTRACE:
203 		mdb_printf("from DTrace raise() action");
204 		break;
205 	case SI_RCTL:
206 		mdb_printf("from rctl action");
207 		break;
208 	case SI_USER:
209 		mdb_printf("user generated via kill");
210 		break;
211 	case SI_LWP:
212 		mdb_printf("user generated via lwp_kill");
213 		break;
214 	case SI_QUEUE:
215 		mdb_printf("user generated via sigqueue");
216 		break;
217 	case SI_TIMER:
218 		mdb_printf("from timer expiration");
219 		break;
220 	case SI_ASYNCIO:
221 		mdb_printf("from async i/o completion");
222 		break;
223 	case SI_MESGQ:
224 		mdb_printf("from message arrival");
225 		break;
226 	default:
227 		if (SI_FROMUSER(&si))
228 			mdb_printf("from user process");
229 		else
230 			mdb_printf("from kernel");
231 	}
232 
233 	mdb_printf(")\n  errno  %5d (%s)\n",
234 	    si.si_errno, strerror(si.si_errno));
235 
236 	if (si.si_code == SI_USER || si.si_code == SI_QUEUE) {
237 		mdb_printf("  signal sent from PID %d (uid %d)\n",
238 		    si.si_pid, si.si_uid);
239 	}
240 
241 	if (si.si_code == SI_QUEUE) {
242 		mdb_printf("  signal value = 0t%d / %p\n",
243 		    si.si_value.sival_int, si.si_value.sival_ptr);
244 	}
245 
246 	switch (si.si_signo) {
247 	case SIGCLD:
248 		mdb_printf("  signal sent from child PID %d (uid %d)\n",
249 		    si.si_pid, si.si_uid);
250 		mdb_printf("  usr time = 0t%ld ticks, sys time = 0t%ld ticks\n",
251 		    si.si_utime, si.si_stime);
252 		mdb_printf("  wait status = 0x%x\n", si.si_status);
253 		break;
254 
255 	case SIGSEGV:
256 	case SIGBUS:
257 	case SIGILL:
258 	case SIGTRAP:
259 	case SIGFPE:
260 		mdb_printf("  fault address = 0x%p\n  trapno = %d\n",
261 		    si.si_addr, si.si_trapno);
262 		mdb_printf("  instruction address = 0x%p %lA\n",
263 		    si.si_pc, si.si_pc);
264 		break;
265 
266 	case SIGPOLL:
267 	case SIGXFSZ:
268 		mdb_printf("  fd = %d  band = 0x%lx\n",
269 		    si.si_fd, si.si_band);
270 		break;
271 
272 	case SIGPROF:
273 		mdb_printf("  last fault address = 0x%p fault type = %d\n",
274 		    si.si_faddr, si.si_fault);
275 		mdb_printf("  timestamp = 0t%ld sec 0t%ld nsec\n",
276 		    si.si_tstamp.tv_sec, si.si_tstamp.tv_nsec);
277 
278 		if (si.__data.__prof.__syscall != 0) {
279 			mdb_printf("  system call %d (", si.si_syscall);
280 			if (si.si_nsysarg > 0) {
281 				mdb_printf("%lx", si.si_sysarg[0]);
282 				for (i = 1; i < si.si_nsysarg; i++)
283 					mdb_printf(", %lx", si.si_sysarg[i]);
284 			}
285 			mdb_printf("  )\n");
286 		}
287 
288 		for (i = 0; i < sizeof (msname) / sizeof (msname[0]); i++) {
289 			mdb_printf("  mstate[\"%s\"] = %d\n",
290 			    msname[i], si.si_mstate[i]);
291 		}
292 		break;
293 	}
294 
295 	return (DCMD_OK);
296 }
297 
298 static int
299 uc_walk_step(mdb_walk_state_t *wsp)
300 {
301 	uintptr_t addr = wsp->walk_addr;
302 	ucontext_t uc;
303 
304 	if (addr == NULL)
305 		return (WALK_DONE);
306 
307 	if (mdb_vread(&uc, sizeof (uc), addr) != sizeof (uc)) {
308 		mdb_warn("failed to read ucontext at %p", addr);
309 		return (WALK_ERR);
310 	}
311 
312 	wsp->walk_addr = (uintptr_t)uc.uc_link;
313 	return (wsp->walk_callback(addr, &uc, wsp->walk_cbdata));
314 }
315 
316 static int
317 oldc_walk_init(mdb_walk_state_t *wsp)
318 {
319 	ssize_t nbytes = mdb_get_xdata("lwpstatus", NULL, 0);
320 
321 	if (nbytes <= 0) {
322 		mdb_warn("lwpstatus information not available");
323 		return (WALK_ERR);
324 	}
325 
326 	if (wsp->walk_addr != NULL) {
327 		mdb_warn("walker only supports global walk\n");
328 		return (WALK_ERR);
329 	}
330 
331 	wsp->walk_addr = nbytes; /* Use walk_addr to track size */
332 	wsp->walk_data = mdb_alloc(nbytes, UM_SLEEP);
333 
334 	if (mdb_get_xdata("lwpstatus", wsp->walk_data, nbytes) != nbytes) {
335 		mdb_warn("failed to read lwpstatus information");
336 		mdb_free(wsp->walk_data, nbytes);
337 		return (WALK_ERR);
338 	}
339 
340 	wsp->walk_arg = wsp->walk_data; /* Use walk_arg to track pointer */
341 	return (WALK_NEXT);
342 }
343 
344 static int
345 oldc_walk_step(mdb_walk_state_t *wsp)
346 {
347 	const lwpstatus_t *lsp, *end;
348 
349 	end = (const lwpstatus_t *)((uintptr_t)wsp->walk_data + wsp->walk_addr);
350 	lsp = wsp->walk_arg;
351 
352 	wsp->walk_arg = (void *)(lsp + 1);
353 
354 	if (lsp < end) {
355 		uintptr_t addr = lsp->pr_oldcontext;
356 		ucontext_t uc;
357 
358 		if (addr == NULL)
359 			return (WALK_NEXT);
360 
361 		if (mdb_vread(&uc, sizeof (uc), addr) != sizeof (uc)) {
362 			mdb_warn("failed to read ucontext at %p", addr);
363 			return (WALK_NEXT);
364 		}
365 
366 		return (wsp->walk_callback(addr, &uc, wsp->walk_cbdata));
367 	}
368 
369 	return (WALK_DONE);
370 }
371 
372 static void
373 oldc_walk_fini(mdb_walk_state_t *wsp)
374 {
375 	mdb_free(wsp->walk_data, wsp->walk_addr); /* walk_addr has size */
376 }
377 
378 /*
379  * ==================== threads ==========================
380  * These are the interfaces that used to require libthread.
381  * Now, libthread has been folded into libc.
382  * =======================================================
383  */
384 
385 /*
386  * prt_addr() is called up to three times to generate arguments for
387  * one call to mdb_printf().  We must return at least three different
388  * pointers to static storage for consecutive calls to prt_addr().
389  */
390 static const char *
391 prt_addr(void *addr, int pad)
392 {
393 	static char buffer[4][24];
394 	static int ix = 0;
395 	char *buf;
396 
397 	if (ix == 4)	/* use buffers in sequence: 0, 1, 2, 3 */
398 		ix = 0;
399 	buf = buffer[ix++];
400 	if (addr == NULL)
401 		return (pad? "<NULL>               " : "<NULL>");
402 	else {
403 #ifdef _LP64
404 		(void) mdb_snprintf(buf, sizeof (buffer[0]), "0x%016lx", addr);
405 		if (pad)
406 			(void) strcpy(buf + 18, "   ");
407 #else
408 		(void) mdb_snprintf(buf, sizeof (buffer[0]), "0x%08lx", addr);
409 		if (pad)
410 			(void) strcpy(buf + 10, "           ");
411 #endif	/* _LP64 */
412 		return (buf);
413 	}
414 }
415 
416 #define	HD(str)		mdb_printf("           " str "\n")
417 #define	OFFSTR		"+0x%-7lx "
418 #define	OFFSET(member)	((size_t)OFFSETOF(ulwp_t, member))
419 
420 /*ARGSUSED*/
421 static int
422 d_ulwp(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
423 {
424 	ulwp_t ulwp;
425 
426 	if (argc != 0 || !(flags & DCMD_ADDRSPEC))
427 		return (DCMD_USAGE);
428 
429 	if (mdb_vread(&ulwp, sizeof (ulwp), addr) != sizeof (ulwp) &&
430 	    (bzero(&ulwp, sizeof (ulwp)),
431 	    mdb_vread(&ulwp, REPLACEMENT_SIZE, addr)) != REPLACEMENT_SIZE) {
432 		mdb_warn("failed to read ulwp at 0x%p", addr);
433 		return (DCMD_ERR);
434 	}
435 
436 	mdb_printf("%#a\n", addr);
437 
438 	HD("self                  uberdata");
439 	mdb_printf(OFFSTR "%s %s\n",
440 	    OFFSET(ul_self),
441 	    prt_addr(ulwp.ul_self, 1),
442 	    prt_addr(ulwp.ul_uberdata, 0));
443 
444 	HD("tlsent                ntlsent");
445 	mdb_printf(OFFSTR "%s %ld\n",
446 	    OFFSET(ul_tlsent),
447 	    prt_addr(ulwp.ul_tlsent, 1),
448 	    ulwp.ul_ntlsent);
449 
450 	HD("forw                  back                  next");
451 	mdb_printf(OFFSTR "%s %s %s\n",
452 	    OFFSET(ul_forw),
453 	    prt_addr(ulwp.ul_forw, 1),
454 	    prt_addr(ulwp.ul_back, 1),
455 	    prt_addr(ulwp.ul_next, 0));
456 
457 	HD("hash                  rval                  stk");
458 	mdb_printf(OFFSTR "%s %s %s\n",
459 	    OFFSET(ul_hash),
460 	    prt_addr(ulwp.ul_hash, 1),
461 	    prt_addr(ulwp.ul_rval, 1),
462 	    prt_addr(ulwp.ul_stk, 0));
463 
464 	HD("mapsiz     guardsize  stktop                stksiz");
465 	mdb_printf(OFFSTR "%-10ld %-10ld %s %ld\n",
466 	    OFFSET(ul_mapsiz),
467 	    ulwp.ul_mapsiz,
468 	    ulwp.ul_guardsize,
469 	    prt_addr((void *)ulwp.ul_stktop, 1),
470 	    ulwp.ul_stksiz);
471 
472 	HD("ustack.ss_sp          ustack.ss_size        ustack.ss_flags");
473 	mdb_printf(OFFSTR "%s %-21ld %s\n",
474 	    OFFSET(ul_ustack.ss_sp),
475 	    prt_addr(ulwp.ul_ustack.ss_sp, 1),
476 	    ulwp.ul_ustack.ss_size,
477 	    stack_flags(&ulwp.ul_ustack));
478 
479 	HD("ix         lwpid      pri        epri       policy     cid");
480 	mdb_printf(OFFSTR "%-10d %-10d %-10d %-10d %-10d %d\n",
481 	    OFFSET(ul_ix),
482 	    ulwp.ul_ix,
483 	    ulwp.ul_lwpid,
484 	    ulwp.ul_pri,
485 	    ulwp.ul_epri,
486 	    ulwp.ul_policy,
487 	    ulwp.ul_cid);
488 
489 	HD("cursig     pleasestop stop       signalled  dead       unwind");
490 	mdb_printf(OFFSTR "%-10d ",
491 	    OFFSET(ul_cursig),
492 	    ulwp.ul_cursig);
493 	mdb_printf(ulwp.ul_pleasestop? "0x%-8x " : "%-10d ",
494 	    ulwp.ul_pleasestop);
495 	mdb_printf(ulwp.ul_stop? "0x%-8x " : "%-10d ",
496 	    ulwp.ul_stop);
497 	mdb_printf("%-10d %-10d %d\n",
498 	    ulwp.ul_signalled,
499 	    ulwp.ul_dead,
500 	    ulwp.ul_unwind);
501 
502 	HD("detached   writer     stopping   can'prolog preempt    savpreempt");
503 	mdb_printf(OFFSTR "%-10d %-10d %-10d %-10d %-10d %d\n",
504 	    OFFSET(ul_detached),
505 	    ulwp.ul_detached,
506 	    ulwp.ul_writer,
507 	    ulwp.ul_stopping,
508 	    ulwp.ul_cancel_prologue,
509 	    ulwp.ul_preempt,
510 	    ulwp.ul_savpreempt);
511 
512 	HD("sigsuspend main       fork       primarymap m'spinners d'noreserv");
513 	mdb_printf(OFFSTR "%-10d %-10d %-10d %-10d %-10d %d\n",
514 	    OFFSET(ul_sigsuspend),
515 	    ulwp.ul_sigsuspend,
516 	    ulwp.ul_main,
517 	    ulwp.ul_fork,
518 	    ulwp.ul_primarymap,
519 	    ulwp.ul_max_spinners,
520 	    ulwp.ul_door_noreserve);
521 
522 	HD("queue_fifo c'w'defer  e'detect'  async_safe rt         rtqueued");
523 	mdb_printf(OFFSTR "%-10d %-10d %-10d %-10d %-10d %d\n",
524 	    OFFSET(ul_queue_fifo),
525 	    ulwp.ul_queue_fifo,
526 	    ulwp.ul_cond_wait_defer,
527 	    ulwp.ul_error_detection,
528 	    ulwp.ul_async_safe,
529 	    ulwp.ul_rt,
530 	    ulwp.ul_rtqueued);
531 
532 	HD("misaligned adapt'spin queue_spin critical   sigdefer   vfork");
533 	mdb_printf(OFFSTR "%-10d %-10d %-10d %-10d %-10d %d\n",
534 	    OFFSET(ul_misaligned),
535 	    ulwp.ul_misaligned,
536 	    ulwp.ul_adaptive_spin,
537 	    ulwp.ul_queue_spin,
538 	    ulwp.ul_critical,
539 	    ulwp.ul_sigdefer,
540 	    ulwp.ul_vfork);
541 
542 	HD("cancelable c'pending  c'disabled c'async    save_async mutator");
543 	mdb_printf(OFFSTR "%-10d %-10d %-10d %-10d %-10d %d\n",
544 	    OFFSET(ul_cancelable),
545 	    ulwp.ul_cancelable,
546 	    ulwp.ul_cancel_pending,
547 	    ulwp.ul_cancel_disabled,
548 	    ulwp.ul_cancel_async,
549 	    ulwp.ul_save_async,
550 	    ulwp.ul_mutator);
551 
552 	HD("created    replace    nocancel   errno      errnop");
553 	mdb_printf(OFFSTR "%-10d %-10d %-10d %-10d %s\n",
554 	    OFFSET(ul_created),
555 	    ulwp.ul_created,
556 	    ulwp.ul_replace,
557 	    ulwp.ul_nocancel,
558 	    ulwp.ul_errno,
559 	    prt_addr(ulwp.ul_errnop, 0));
560 
561 	HD("clnup_hdr             schedctl_called       schedctl");
562 	mdb_printf(OFFSTR "%s %s %s\n",
563 	    OFFSET(ul_clnup_hdr),
564 	    prt_addr(ulwp.ul_clnup_hdr, 1),
565 	    prt_addr(ulwp.ul_schedctl_called, 1),
566 	    prt_addr((void *)ulwp.ul_schedctl, 0));
567 
568 	HD("bindflags  libc_locks stsd                  &ftsd");
569 	mdb_printf(OFFSTR,
570 	    OFFSET(ul_bindflags));
571 	mdb_printf(ulwp.ul_bindflags? "0x%-8x " : "%-10d ",
572 	    ulwp.ul_bindflags);
573 	mdb_printf("%-10d ", ulwp.ul_libc_locks);
574 	mdb_printf("%s %s\n",
575 	    prt_addr(ulwp.ul_stsd, 1),
576 	    prt_addr((void *)(addr + OFFSET(ul_ftsd[0])), 0));
577 
578 	HD("eventmask[0..1]       eventnum              eventdata");
579 	mdb_printf(OFFSTR "0x%08x 0x%08x %-21d %s\n",
580 	    OFFSET(ul_td_evbuf.eventmask.event_bits[0]),
581 	    ulwp.ul_td_evbuf.eventmask.event_bits[0],
582 	    ulwp.ul_td_evbuf.eventmask.event_bits[1],
583 	    ulwp.ul_td_evbuf.eventnum,
584 	    prt_addr(ulwp.ul_td_evbuf.eventdata, 0));
585 
586 	HD("td'enable  sync'reg   qtype      cv_wake    rtld       usropts");
587 	mdb_printf(OFFSTR "%-10d %-10d %-10d %-10d %-10d ",
588 	    OFFSET(ul_td_events_enable),
589 	    ulwp.ul_td_events_enable,
590 	    ulwp.ul_sync_obj_reg,
591 	    ulwp.ul_qtype,
592 	    ulwp.ul_cv_wake,
593 	    ulwp.ul_rtld);
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");
838 	mdb_printf(OFFSTR "%s %s\n",
839 	    OFFSET(ulwp_replace_last),
840 	    prt_addr(uberdata.ulwp_replace_last, 1),
841 	    prt_addr(uberdata.atforklist, 0));
842 
843 	HD("robustlocks           robustlist");
844 	mdb_printf(OFFSTR "%s %s\n",
845 	    OFFSET(robustlocks),
846 	    prt_addr(uberdata.robustlocks, 1),
847 	    prt_addr(uberdata.robustlist, 0));
848 
849 	HD("tdb_bootstrap         tdb_sync_addr_hash    tdb_'count tdb_'fail");
850 	mdb_printf(OFFSTR "%s %s %-10d %d\n",
851 	    OFFSET(tdb_bootstrap),
852 	    prt_addr(uberdata.tdb_bootstrap, 1),
853 	    prt_addr(uberdata.tdb.tdb_sync_addr_hash, 1),
854 	    uberdata.tdb.tdb_register_count,
855 	    uberdata.tdb.tdb_hash_alloc_failed);
856 
857 	HD("tdb_sync_addr_free    tdb_sync_addr_last    tdb_sync_alloc");
858 	mdb_printf(OFFSTR "%s %s %ld\n",
859 	    OFFSET(tdb.tdb_sync_addr_free),
860 	    prt_addr(uberdata.tdb.tdb_sync_addr_free, 1),
861 	    prt_addr(uberdata.tdb.tdb_sync_addr_last, 1),
862 	    uberdata.tdb.tdb_sync_alloc);
863 
864 	HD("tdb_ev_global_mask    tdb_events");
865 	mdb_printf(OFFSTR "0x%08x 0x%08x %s\n",
866 	    OFFSET(tdb.tdb_ev_global_mask),
867 	    uberdata.tdb.tdb_ev_global_mask.event_bits[0],
868 	    uberdata.tdb.tdb_ev_global_mask.event_bits[1],
869 	    prt_addr((void *)uberdata.tdb.tdb_events, 0));
870 
871 	return (DCMD_OK);
872 }
873 
874 static int
875 ulwp_walk_init(mdb_walk_state_t *wsp)
876 {
877 	uintptr_t addr = wsp->walk_addr;
878 	uintptr_t uber_addr;
879 
880 	if (addr == NULL &&
881 	    ((uber_addr = uberdata_addr()) == NULL ||
882 	    mdb_vread(&addr, sizeof (addr),
883 	    uber_addr + OFFSETOF(uberdata_t, all_lwps))
884 	    != sizeof (addr))) {
885 		mdb_warn("cannot find 'uberdata.all_lwps'");
886 		return (WALK_ERR);
887 	}
888 	if (addr == NULL)
889 		return (WALK_DONE);
890 	wsp->walk_addr = addr;
891 	wsp->walk_data = (void *)addr;
892 	return (WALK_NEXT);
893 }
894 
895 static int
896 ulwp_walk_step(mdb_walk_state_t *wsp)
897 {
898 	uintptr_t addr = wsp->walk_addr;
899 	ulwp_t ulwp;
900 
901 	if (addr == NULL)
902 		return (WALK_DONE);
903 	if (mdb_vread(&ulwp, sizeof (ulwp), addr) != sizeof (ulwp) &&
904 	    (bzero(&ulwp, sizeof (ulwp)),
905 	    mdb_vread(&ulwp, REPLACEMENT_SIZE, addr)) != REPLACEMENT_SIZE) {
906 		mdb_warn("failed to read ulwp at 0x%p", addr);
907 		return (WALK_ERR);
908 	}
909 	/*
910 	 * If we have looped around to the beginning
911 	 * of the circular linked list, we are done.
912 	 */
913 	if ((wsp->walk_addr = (uintptr_t)ulwp.ul_forw)
914 	    == (uintptr_t)wsp->walk_data)
915 		wsp->walk_addr = NULL;
916 	return (wsp->walk_callback(addr, &ulwp, wsp->walk_cbdata));
917 }
918 
919 /* Avoid classifying NULL pointers as part of the main stack on x86 */
920 #define	MIN_STACK_ADDR		(0x10000ul)
921 
922 static int
923 whatis_walk_ulwp(uintptr_t addr, const ulwp_t *ulwp, mdb_whatis_t *w)
924 {
925 	uintptr_t cur;
926 	lwpid_t id = ulwp->ul_lwpid;
927 	uintptr_t top, base, size;
928 
929 	while (mdb_whatis_match(w, addr, sizeof (ulwp_t), &cur))
930 		mdb_whatis_report_object(w, cur, addr,
931 		    "allocated as thread %#r's ulwp_t\n", id);
932 
933 	top = (uintptr_t)ulwp->ul_stktop;
934 	size = ulwp->ul_stksiz;
935 
936 	/*
937 	 * The main stack ends up being a little weird, especially if
938 	 * the stack ulimit is unlimited.  This tries to take that into
939 	 * account.
940 	 */
941 	if (size > top)
942 		size = top;
943 	if (top > MIN_STACK_ADDR && top - size < MIN_STACK_ADDR)
944 		size = top - MIN_STACK_ADDR;
945 
946 	base = top - size;
947 
948 	while (mdb_whatis_match(w, base, size, &cur))
949 		mdb_whatis_report_address(w, cur, "in [ stack tid=%#r ]\n", id);
950 
951 	if (ulwp->ul_ustack.ss_flags & SS_ONSTACK) {
952 		base = (uintptr_t)ulwp->ul_ustack.ss_sp;
953 		size = ulwp->ul_ustack.ss_size;
954 
955 		while (mdb_whatis_match(w, base, size, &cur))
956 			mdb_whatis_report_address(w, cur,
957 			    "in [ altstack tid=%#r ]\n", id);
958 	}
959 
960 	return (WHATIS_WALKRET(w));
961 }
962 
963 /*ARGSUSED*/
964 static int
965 whatis_run_ulwps(mdb_whatis_t *w, void *arg)
966 {
967 	if (mdb_walk("ulwps", (mdb_walk_cb_t)whatis_walk_ulwp, w) == -1) {
968 		mdb_warn("couldn't find ulwps walker");
969 		return (1);
970 	}
971 	return (0);
972 }
973 
974 /*
975  * =======================================================
976  * End of thread (previously libthread) interfaces.
977  * ==================== threads ==========================
978  */
979 
980 static const mdb_dcmd_t dcmds[] = {
981 	{ "jmp_buf", ":", "print jmp_buf contents", d_jmp_buf, NULL },
982 	{ "sigjmp_buf", ":", "print sigjmp_buf contents", d_sigjmp_buf, NULL },
983 	{ "siginfo", ":", "print siginfo_t structure", d_siginfo, NULL },
984 	{ "ucontext", ":", "print ucontext_t structure", d_ucontext, NULL },
985 	{ "ulwp", ":", "print ulwp_t structure", d_ulwp, NULL },
986 	{ "uberdata", ":", "print uberdata_t structure", d_uberdata, NULL },
987 	{ NULL }
988 };
989 
990 static const mdb_walker_t walkers[] = {
991 	{ "ucontext", "walk ucontext_t uc_link list",
992 		NULL, uc_walk_step, NULL, NULL },
993 	{ "oldcontext", "walk per-lwp oldcontext pointers",
994 		oldc_walk_init, oldc_walk_step, oldc_walk_fini, NULL },
995 	{ "ulwps", "walk list of ulwp_t pointers",
996 		ulwp_walk_init, ulwp_walk_step, NULL, NULL },
997 	{ NULL }
998 };
999 
1000 static const mdb_modinfo_t modinfo = { MDB_API_VERSION, dcmds, walkers };
1001 
1002 const mdb_modinfo_t *
1003 _mdb_init(void)
1004 {
1005 	mdb_whatis_register("threads", whatis_run_ulwps, NULL,
1006 	    WHATIS_PRIO_EARLY, WHATIS_REG_NO_ID);
1007 
1008 	return (&modinfo);
1009 }
1010