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