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