xref: /titanic_52/usr/src/cmd/mdb/common/modules/genunix/genunix.c (revision 1959748cbddf37d4734c107dadfa449e076045e3)
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  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <mdb/mdb_param.h>
29 #include <mdb/mdb_modapi.h>
30 #include <mdb/mdb_ks.h>
31 #include <mdb/mdb_ctf.h>
32 
33 #include <sys/types.h>
34 #include <sys/thread.h>
35 #include <sys/session.h>
36 #include <sys/user.h>
37 #include <sys/proc.h>
38 #include <sys/var.h>
39 #include <sys/t_lock.h>
40 #include <sys/callo.h>
41 #include <sys/priocntl.h>
42 #include <sys/class.h>
43 #include <sys/regset.h>
44 #include <sys/stack.h>
45 #include <sys/cpuvar.h>
46 #include <sys/vnode.h>
47 #include <sys/vfs.h>
48 #include <sys/flock_impl.h>
49 #include <sys/kmem_impl.h>
50 #include <sys/vmem_impl.h>
51 #include <sys/kstat.h>
52 #include <vm/seg_vn.h>
53 #include <vm/anon.h>
54 #include <vm/as.h>
55 #include <vm/seg_map.h>
56 #include <sys/dditypes.h>
57 #include <sys/ddi_impldefs.h>
58 #include <sys/sysmacros.h>
59 #include <sys/sysconf.h>
60 #include <sys/task.h>
61 #include <sys/project.h>
62 #include <sys/taskq.h>
63 #include <sys/taskq_impl.h>
64 #include <sys/errorq_impl.h>
65 #include <sys/cred_impl.h>
66 #include <sys/zone.h>
67 #include <sys/panic.h>
68 #include <regex.h>
69 #include <sys/port_impl.h>
70 
71 #include "avl.h"
72 #include "contract.h"
73 #include "cpupart_mdb.h"
74 #include "devinfo.h"
75 #include "leaky.h"
76 #include "lgrp.h"
77 #include "pg.h"
78 #include "group.h"
79 #include "list.h"
80 #include "log.h"
81 #include "kgrep.h"
82 #include "kmem.h"
83 #include "bio.h"
84 #include "streams.h"
85 #include "cyclic.h"
86 #include "findstack.h"
87 #include "ndievents.h"
88 #include "mmd.h"
89 #include "net.h"
90 #include "netstack.h"
91 #include "nvpair.h"
92 #include "ctxop.h"
93 #include "tsd.h"
94 #include "thread.h"
95 #include "memory.h"
96 #include "sobj.h"
97 #include "sysevent.h"
98 #include "rctl.h"
99 #include "tsol.h"
100 #include "typegraph.h"
101 #include "ldi.h"
102 #include "vfs.h"
103 #include "zone.h"
104 #include "modhash.h"
105 #include "mdi.h"
106 #include "fm.h"
107 
108 /*
109  * Surely this is defined somewhere...
110  */
111 #define	NINTR		16
112 
113 #define	KILOS		10
114 #define	MEGS		20
115 #define	GIGS		30
116 
117 #ifndef STACK_BIAS
118 #define	STACK_BIAS	0
119 #endif
120 
121 static char
122 pstat2ch(uchar_t state)
123 {
124 	switch (state) {
125 		case SSLEEP: return ('S');
126 		case SRUN: return ('R');
127 		case SZOMB: return ('Z');
128 		case SIDL: return ('I');
129 		case SONPROC: return ('O');
130 		case SSTOP: return ('T');
131 		case SWAIT: return ('W');
132 		default: return ('?');
133 	}
134 }
135 
136 #define	PS_PRTTHREADS	0x1
137 #define	PS_PRTLWPS	0x2
138 #define	PS_PSARGS	0x4
139 #define	PS_TASKS	0x8
140 #define	PS_PROJECTS	0x10
141 #define	PS_ZONES	0x20
142 
143 static int
144 ps_threadprint(uintptr_t addr, const void *data, void *private)
145 {
146 	const kthread_t *t = (const kthread_t *)data;
147 	uint_t prt_flags = *((uint_t *)private);
148 
149 	static const mdb_bitmask_t t_state_bits[] = {
150 		{ "TS_FREE",	UINT_MAX,	TS_FREE		},
151 		{ "TS_SLEEP",	TS_SLEEP,	TS_SLEEP	},
152 		{ "TS_RUN",	TS_RUN,		TS_RUN		},
153 		{ "TS_ONPROC",	TS_ONPROC,	TS_ONPROC	},
154 		{ "TS_ZOMB",	TS_ZOMB,	TS_ZOMB		},
155 		{ "TS_STOPPED",	TS_STOPPED,	TS_STOPPED	},
156 		{ "TS_WAIT",	TS_WAIT,	TS_WAIT		},
157 		{ NULL,		0,		0		}
158 	};
159 
160 	if (prt_flags & PS_PRTTHREADS)
161 		mdb_printf("\tT  %?a <%b>\n", addr, t->t_state, t_state_bits);
162 
163 	if (prt_flags & PS_PRTLWPS)
164 		mdb_printf("\tL  %?a ID: %u\n", t->t_lwp, t->t_tid);
165 
166 	return (WALK_NEXT);
167 }
168 
169 int
170 ps(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
171 {
172 	uint_t prt_flags = 0;
173 	proc_t pr;
174 	struct pid pid, pgid, sid;
175 	sess_t session;
176 	cred_t cred;
177 	task_t tk;
178 	kproject_t pj;
179 	zone_t zn;
180 
181 	if (!(flags & DCMD_ADDRSPEC)) {
182 		if (mdb_walk_dcmd("proc", "ps", argc, argv) == -1) {
183 			mdb_warn("can't walk 'proc'");
184 			return (DCMD_ERR);
185 		}
186 		return (DCMD_OK);
187 	}
188 
189 	if (mdb_getopts(argc, argv,
190 	    'f', MDB_OPT_SETBITS, PS_PSARGS, &prt_flags,
191 	    'l', MDB_OPT_SETBITS, PS_PRTLWPS, &prt_flags,
192 	    'T', MDB_OPT_SETBITS, PS_TASKS, &prt_flags,
193 	    'P', MDB_OPT_SETBITS, PS_PROJECTS, &prt_flags,
194 	    'z', MDB_OPT_SETBITS, PS_ZONES, &prt_flags,
195 	    't', MDB_OPT_SETBITS, PS_PRTTHREADS, &prt_flags, NULL) != argc)
196 		return (DCMD_USAGE);
197 
198 	if (DCMD_HDRSPEC(flags)) {
199 		mdb_printf("%<u>%1s %6s %6s %6s %6s ",
200 		    "S", "PID", "PPID", "PGID", "SID");
201 		if (prt_flags & PS_TASKS)
202 			mdb_printf("%5s ", "TASK");
203 		if (prt_flags & PS_PROJECTS)
204 			mdb_printf("%5s ", "PROJ");
205 		if (prt_flags & PS_ZONES)
206 			mdb_printf("%5s ", "ZONE");
207 		mdb_printf("%6s %10s %?s %s%</u>\n",
208 		    "UID", "FLAGS", "ADDR", "NAME");
209 	}
210 
211 	mdb_vread(&pr, sizeof (pr), addr);
212 	mdb_vread(&pid, sizeof (pid), (uintptr_t)pr.p_pidp);
213 	mdb_vread(&pgid, sizeof (pgid), (uintptr_t)pr.p_pgidp);
214 	mdb_vread(&cred, sizeof (cred), (uintptr_t)pr.p_cred);
215 	mdb_vread(&session, sizeof (session), (uintptr_t)pr.p_sessp);
216 	mdb_vread(&sid, sizeof (sid), (uintptr_t)session.s_sidp);
217 	if (prt_flags & (PS_TASKS | PS_PROJECTS))
218 		mdb_vread(&tk, sizeof (tk), (uintptr_t)pr.p_task);
219 	if (prt_flags & PS_PROJECTS)
220 		mdb_vread(&pj, sizeof (pj), (uintptr_t)tk.tk_proj);
221 	if (prt_flags & PS_ZONES)
222 		mdb_vread(&zn, sizeof (zone_t), (uintptr_t)pr.p_zone);
223 
224 	mdb_printf("%c %6d %6d %6d %6d ",
225 	    pstat2ch(pr.p_stat), pid.pid_id, pr.p_ppid, pgid.pid_id,
226 	    sid.pid_id);
227 	if (prt_flags & PS_TASKS)
228 		mdb_printf("%5d ", tk.tk_tkid);
229 	if (prt_flags & PS_PROJECTS)
230 		mdb_printf("%5d ", pj.kpj_id);
231 	if (prt_flags & PS_ZONES)
232 		mdb_printf("%5d ", zn.zone_id);
233 	mdb_printf("%6d 0x%08x %0?p %s\n",
234 	    cred.cr_uid, pr.p_flag, addr,
235 	    (prt_flags & PS_PSARGS) ? pr.p_user.u_psargs : pr.p_user.u_comm);
236 
237 	if (prt_flags & ~PS_PSARGS)
238 		(void) mdb_pwalk("thread", ps_threadprint, &prt_flags, addr);
239 
240 	return (DCMD_OK);
241 }
242 
243 #define	PG_NEWEST	0x0001
244 #define	PG_OLDEST	0x0002
245 #define	PG_PIPE_OUT	0x0004
246 #define	PG_EXACT_MATCH	0x0008
247 
248 typedef struct pgrep_data {
249 	uint_t pg_flags;
250 	uint_t pg_psflags;
251 	uintptr_t pg_xaddr;
252 	hrtime_t pg_xstart;
253 	const char *pg_pat;
254 #ifndef _KMDB
255 	regex_t pg_reg;
256 #endif
257 } pgrep_data_t;
258 
259 /*ARGSUSED*/
260 static int
261 pgrep_cb(uintptr_t addr, const void *pdata, void *data)
262 {
263 	const proc_t *prp = pdata;
264 	pgrep_data_t *pgp = data;
265 #ifndef _KMDB
266 	regmatch_t pmatch;
267 #endif
268 
269 	/*
270 	 * kmdb doesn't have access to the reg* functions, so we fall back
271 	 * to strstr/strcmp.
272 	 */
273 #ifdef _KMDB
274 	if ((pgp->pg_flags & PG_EXACT_MATCH) ?
275 	    (strcmp(prp->p_user.u_comm, pgp->pg_pat) != 0) :
276 	    (strstr(prp->p_user.u_comm, pgp->pg_pat) == NULL))
277 		return (WALK_NEXT);
278 #else
279 	if (regexec(&pgp->pg_reg, prp->p_user.u_comm, 1, &pmatch, 0) != 0)
280 		return (WALK_NEXT);
281 
282 	if ((pgp->pg_flags & PG_EXACT_MATCH) &&
283 	    (pmatch.rm_so != 0 || prp->p_user.u_comm[pmatch.rm_eo] != '\0'))
284 		return (WALK_NEXT);
285 #endif
286 
287 	if (pgp->pg_flags & (PG_NEWEST | PG_OLDEST)) {
288 		hrtime_t start;
289 
290 		start = (hrtime_t)prp->p_user.u_start.tv_sec * NANOSEC +
291 		    prp->p_user.u_start.tv_nsec;
292 
293 		if (pgp->pg_flags & PG_NEWEST) {
294 			if (pgp->pg_xaddr == NULL || start > pgp->pg_xstart) {
295 				pgp->pg_xaddr = addr;
296 				pgp->pg_xstart = start;
297 			}
298 		} else {
299 			if (pgp->pg_xaddr == NULL || start < pgp->pg_xstart) {
300 				pgp->pg_xaddr = addr;
301 				pgp->pg_xstart = start;
302 			}
303 		}
304 
305 	} else if (pgp->pg_flags & PG_PIPE_OUT) {
306 		mdb_printf("%p\n", addr);
307 
308 	} else {
309 		if (mdb_call_dcmd("ps", addr, pgp->pg_psflags, 0, NULL) != 0) {
310 			mdb_warn("can't invoke 'ps'");
311 			return (WALK_DONE);
312 		}
313 		pgp->pg_psflags &= ~DCMD_LOOPFIRST;
314 	}
315 
316 	return (WALK_NEXT);
317 }
318 
319 /*ARGSUSED*/
320 int
321 pgrep(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
322 {
323 	pgrep_data_t pg;
324 	int i;
325 #ifndef _KMDB
326 	int err;
327 #endif
328 
329 	if (flags & DCMD_ADDRSPEC)
330 		return (DCMD_USAGE);
331 
332 	pg.pg_flags = 0;
333 	pg.pg_xaddr = 0;
334 
335 	i = mdb_getopts(argc, argv,
336 	    'n', MDB_OPT_SETBITS, PG_NEWEST, &pg.pg_flags,
337 	    'o', MDB_OPT_SETBITS, PG_OLDEST, &pg.pg_flags,
338 	    'x', MDB_OPT_SETBITS, PG_EXACT_MATCH, &pg.pg_flags,
339 	    NULL);
340 
341 	argc -= i;
342 	argv += i;
343 
344 	if (argc != 1)
345 		return (DCMD_USAGE);
346 
347 	/*
348 	 * -n and -o are mutually exclusive.
349 	 */
350 	if ((pg.pg_flags & PG_NEWEST) && (pg.pg_flags & PG_OLDEST))
351 		return (DCMD_USAGE);
352 
353 	if (argv->a_type != MDB_TYPE_STRING)
354 		return (DCMD_USAGE);
355 
356 	if (flags & DCMD_PIPE_OUT)
357 		pg.pg_flags |= PG_PIPE_OUT;
358 
359 	pg.pg_pat = argv->a_un.a_str;
360 	if (DCMD_HDRSPEC(flags))
361 		pg.pg_psflags = DCMD_ADDRSPEC | DCMD_LOOP | DCMD_LOOPFIRST;
362 	else
363 		pg.pg_psflags = DCMD_ADDRSPEC | DCMD_LOOP;
364 
365 #ifndef _KMDB
366 	if ((err = regcomp(&pg.pg_reg, pg.pg_pat, REG_EXTENDED)) != 0) {
367 		size_t nbytes;
368 		char *buf;
369 
370 		nbytes = regerror(err, &pg.pg_reg, NULL, 0);
371 		buf = mdb_alloc(nbytes + 1, UM_SLEEP | UM_GC);
372 		(void) regerror(err, &pg.pg_reg, buf, nbytes);
373 		mdb_warn("%s\n", buf);
374 
375 		return (DCMD_ERR);
376 	}
377 #endif
378 
379 	if (mdb_walk("proc", pgrep_cb, &pg) != 0) {
380 		mdb_warn("can't walk 'proc'");
381 		return (DCMD_ERR);
382 	}
383 
384 	if (pg.pg_xaddr != 0 && (pg.pg_flags & (PG_NEWEST | PG_OLDEST))) {
385 		if (pg.pg_flags & PG_PIPE_OUT) {
386 			mdb_printf("%p\n", pg.pg_xaddr);
387 		} else {
388 			if (mdb_call_dcmd("ps", pg.pg_xaddr, pg.pg_psflags,
389 			    0, NULL) != 0) {
390 				mdb_warn("can't invoke 'ps'");
391 				return (DCMD_ERR);
392 			}
393 		}
394 	}
395 
396 	return (DCMD_OK);
397 }
398 
399 int
400 task(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
401 {
402 	task_t tk;
403 	kproject_t pj;
404 
405 	if (!(flags & DCMD_ADDRSPEC)) {
406 		if (mdb_walk_dcmd("task_cache", "task", argc, argv) == -1) {
407 			mdb_warn("can't walk task_cache");
408 			return (DCMD_ERR);
409 		}
410 		return (DCMD_OK);
411 	}
412 	if (DCMD_HDRSPEC(flags)) {
413 		mdb_printf("%<u>%?s %6s %6s %6s %6s %10s%</u>\n",
414 		    "ADDR", "TASKID", "PROJID", "ZONEID", "REFCNT", "FLAGS");
415 	}
416 	if (mdb_vread(&tk, sizeof (task_t), addr) == -1) {
417 		mdb_warn("can't read task_t structure at %p", addr);
418 		return (DCMD_ERR);
419 	}
420 	if (mdb_vread(&pj, sizeof (kproject_t), (uintptr_t)tk.tk_proj) == -1) {
421 		mdb_warn("can't read project_t structure at %p", addr);
422 		return (DCMD_ERR);
423 	}
424 	mdb_printf("%0?p %6d %6d %6d %6u 0x%08x\n",
425 	    addr, tk.tk_tkid, pj.kpj_id, pj.kpj_zoneid, tk.tk_hold_count,
426 	    tk.tk_flags);
427 	return (DCMD_OK);
428 }
429 
430 int
431 project(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
432 {
433 	kproject_t pj;
434 
435 	if (!(flags & DCMD_ADDRSPEC)) {
436 		if (mdb_walk_dcmd("projects", "project", argc, argv) == -1) {
437 			mdb_warn("can't walk projects");
438 			return (DCMD_ERR);
439 		}
440 		return (DCMD_OK);
441 	}
442 	if (DCMD_HDRSPEC(flags)) {
443 		mdb_printf("%<u>%?s %6s %6s %6s%</u>\n",
444 		    "ADDR", "PROJID", "ZONEID", "REFCNT");
445 	}
446 	if (mdb_vread(&pj, sizeof (kproject_t), addr) == -1) {
447 		mdb_warn("can't read kproject_t structure at %p", addr);
448 		return (DCMD_ERR);
449 	}
450 	mdb_printf("%0?p %6d %6d %6u\n", addr, pj.kpj_id, pj.kpj_zoneid,
451 	    pj.kpj_count);
452 	return (DCMD_OK);
453 }
454 
455 /*ARGSUSED*/
456 int
457 callout(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
458 {
459 	callout_table_t	*co_ktable[CALLOUT_TABLES];
460 	int co_kfanout;
461 	callout_table_t co_table;
462 	callout_t co_callout;
463 	callout_t *co_ptr;
464 	int co_id;
465 	clock_t lbolt;
466 	int i, j, k;
467 	const char *lbolt_sym;
468 
469 	if ((flags & DCMD_ADDRSPEC) || argc != 0)
470 		return (DCMD_USAGE);
471 
472 	if (mdb_prop_postmortem)
473 		lbolt_sym = "panic_lbolt";
474 	else
475 		lbolt_sym = "lbolt";
476 
477 	if (mdb_readvar(&lbolt, lbolt_sym) == -1) {
478 		mdb_warn("failed to read '%s'", lbolt_sym);
479 		return (DCMD_ERR);
480 	}
481 
482 	if (mdb_readvar(&co_kfanout, "callout_fanout") == -1) {
483 		mdb_warn("failed to read callout_fanout");
484 		return (DCMD_ERR);
485 	}
486 
487 	if (mdb_readvar(&co_ktable, "callout_table") == -1) {
488 		mdb_warn("failed to read callout_table");
489 		return (DCMD_ERR);
490 	}
491 
492 	mdb_printf("%<u>%-24s %-?s %-?s %-?s%</u>\n",
493 	    "FUNCTION", "ARGUMENT", "ID", "TIME");
494 
495 	for (i = 0; i < CALLOUT_NTYPES; i++) {
496 		for (j = 0; j < co_kfanout; j++) {
497 
498 			co_id = CALLOUT_TABLE(i, j);
499 
500 			if (mdb_vread(&co_table, sizeof (co_table),
501 			    (uintptr_t)co_ktable[co_id]) == -1) {
502 				mdb_warn("failed to read table at %p",
503 				    (uintptr_t)co_ktable[co_id]);
504 				continue;
505 			}
506 
507 			for (k = 0; k < CALLOUT_BUCKETS; k++) {
508 				co_ptr = co_table.ct_idhash[k];
509 
510 				while (co_ptr != NULL) {
511 					mdb_vread(&co_callout,
512 					    sizeof (co_callout),
513 					    (uintptr_t)co_ptr);
514 
515 					mdb_printf("%-24a %0?p %0?lx %?lx "
516 					    "(T%+ld)\n", co_callout.c_func,
517 					    co_callout.c_arg, co_callout.c_xid,
518 					    co_callout.c_runtime,
519 					    co_callout.c_runtime - lbolt);
520 
521 					co_ptr = co_callout.c_idnext;
522 				}
523 			}
524 		}
525 	}
526 
527 	return (DCMD_OK);
528 }
529 
530 /*ARGSUSED*/
531 int
532 class(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
533 {
534 	long num_classes, i;
535 	sclass_t *class_tbl;
536 	GElf_Sym g_sclass;
537 	char class_name[PC_CLNMSZ];
538 	size_t tbl_size;
539 
540 	if (mdb_lookup_by_name("sclass", &g_sclass) == -1) {
541 		mdb_warn("failed to find symbol sclass\n");
542 		return (DCMD_ERR);
543 	}
544 
545 	tbl_size = (size_t)g_sclass.st_size;
546 	num_classes = tbl_size / (sizeof (sclass_t));
547 	class_tbl = mdb_alloc(tbl_size, UM_SLEEP | UM_GC);
548 
549 	if (mdb_readsym(class_tbl, tbl_size, "sclass") == -1) {
550 		mdb_warn("failed to read sclass");
551 		return (DCMD_ERR);
552 	}
553 
554 	mdb_printf("%<u>%4s %-10s %-24s %-24s%</u>\n", "SLOT", "NAME",
555 	    "INIT FCN", "CLASS FCN");
556 
557 	for (i = 0; i < num_classes; i++) {
558 		if (mdb_vread(class_name, sizeof (class_name),
559 		    (uintptr_t)class_tbl[i].cl_name) == -1)
560 			(void) strcpy(class_name, "???");
561 
562 		mdb_printf("%4ld %-10s %-24a %-24a\n", i, class_name,
563 		    class_tbl[i].cl_init, class_tbl[i].cl_funcs);
564 	}
565 
566 	return (DCMD_OK);
567 }
568 
569 #define	FSNAMELEN	32	/* Max len of FS name we read from vnodeops */
570 
571 int
572 vnode2path(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
573 {
574 	uintptr_t rootdir;
575 	vnode_t vn;
576 	char buf[MAXPATHLEN];
577 
578 	uint_t opt_F = FALSE;
579 
580 	if (mdb_getopts(argc, argv,
581 	    'F', MDB_OPT_SETBITS, TRUE, &opt_F, NULL) != argc)
582 		return (DCMD_USAGE);
583 
584 	if (!(flags & DCMD_ADDRSPEC)) {
585 		mdb_warn("expected explicit vnode_t address before ::\n");
586 		return (DCMD_USAGE);
587 	}
588 
589 	if (mdb_readvar(&rootdir, "rootdir") == -1) {
590 		mdb_warn("failed to read rootdir");
591 		return (DCMD_ERR);
592 	}
593 
594 	if (mdb_vnode2path(addr, buf, sizeof (buf)) == -1)
595 		return (DCMD_ERR);
596 
597 	if (*buf == '\0') {
598 		mdb_printf("??\n");
599 		return (DCMD_OK);
600 	}
601 
602 	mdb_printf("%s", buf);
603 	if (opt_F && buf[strlen(buf)-1] != '/' &&
604 	    mdb_vread(&vn, sizeof (vn), addr) == sizeof (vn))
605 		mdb_printf("%c", mdb_vtype2chr(vn.v_type, 0));
606 	mdb_printf("\n");
607 
608 	return (DCMD_OK);
609 }
610 
611 int
612 ld_walk_init(mdb_walk_state_t *wsp)
613 {
614 	wsp->walk_data = (void *)wsp->walk_addr;
615 	return (WALK_NEXT);
616 }
617 
618 int
619 ld_walk_step(mdb_walk_state_t *wsp)
620 {
621 	int status;
622 	lock_descriptor_t ld;
623 
624 	if (mdb_vread(&ld, sizeof (lock_descriptor_t), wsp->walk_addr) == -1) {
625 		mdb_warn("couldn't read lock_descriptor_t at %p\n",
626 		    wsp->walk_addr);
627 		return (WALK_ERR);
628 	}
629 
630 	status = wsp->walk_callback(wsp->walk_addr, &ld, wsp->walk_cbdata);
631 	if (status == WALK_ERR)
632 		return (WALK_ERR);
633 
634 	wsp->walk_addr = (uintptr_t)ld.l_next;
635 	if (wsp->walk_addr == (uintptr_t)wsp->walk_data)
636 		return (WALK_DONE);
637 
638 	return (status);
639 }
640 
641 int
642 lg_walk_init(mdb_walk_state_t *wsp)
643 {
644 	GElf_Sym sym;
645 
646 	if (mdb_lookup_by_name("lock_graph", &sym) == -1) {
647 		mdb_warn("failed to find symbol 'lock_graph'\n");
648 		return (WALK_ERR);
649 	}
650 
651 	wsp->walk_addr = (uintptr_t)sym.st_value;
652 	wsp->walk_data = (void *)(uintptr_t)(sym.st_value + sym.st_size);
653 
654 	return (WALK_NEXT);
655 }
656 
657 typedef struct lg_walk_data {
658 	uintptr_t startaddr;
659 	mdb_walk_cb_t callback;
660 	void *data;
661 } lg_walk_data_t;
662 
663 /*
664  * We can't use ::walk lock_descriptor directly, because the head of each graph
665  * is really a dummy lock.  Rather than trying to dynamically determine if this
666  * is a dummy node or not, we just filter out the initial element of the
667  * list.
668  */
669 static int
670 lg_walk_cb(uintptr_t addr, const void *data, void *priv)
671 {
672 	lg_walk_data_t *lw = priv;
673 
674 	if (addr != lw->startaddr)
675 		return (lw->callback(addr, data, lw->data));
676 
677 	return (WALK_NEXT);
678 }
679 
680 int
681 lg_walk_step(mdb_walk_state_t *wsp)
682 {
683 	graph_t *graph;
684 	lg_walk_data_t lw;
685 
686 	if (wsp->walk_addr >= (uintptr_t)wsp->walk_data)
687 		return (WALK_DONE);
688 
689 	if (mdb_vread(&graph, sizeof (graph), wsp->walk_addr) == -1) {
690 		mdb_warn("failed to read graph_t at %p", wsp->walk_addr);
691 		return (WALK_ERR);
692 	}
693 
694 	wsp->walk_addr += sizeof (graph);
695 
696 	if (graph == NULL)
697 		return (WALK_NEXT);
698 
699 	lw.callback = wsp->walk_callback;
700 	lw.data = wsp->walk_cbdata;
701 
702 	lw.startaddr = (uintptr_t)&(graph->active_locks);
703 	if (mdb_pwalk("lock_descriptor", lg_walk_cb, &lw, lw.startaddr)) {
704 		mdb_warn("couldn't walk lock_descriptor at %p\n", lw.startaddr);
705 		return (WALK_ERR);
706 	}
707 
708 	lw.startaddr = (uintptr_t)&(graph->sleeping_locks);
709 	if (mdb_pwalk("lock_descriptor", lg_walk_cb, &lw, lw.startaddr)) {
710 		mdb_warn("couldn't walk lock_descriptor at %p\n", lw.startaddr);
711 		return (WALK_ERR);
712 	}
713 
714 	return (WALK_NEXT);
715 }
716 
717 /*
718  * The space available for the path corresponding to the locked vnode depends
719  * on whether we are printing 32- or 64-bit addresses.
720  */
721 #ifdef _LP64
722 #define	LM_VNPATHLEN	20
723 #else
724 #define	LM_VNPATHLEN	30
725 #endif
726 
727 /*ARGSUSED*/
728 static int
729 lminfo_cb(uintptr_t addr, const void *data, void *priv)
730 {
731 	const lock_descriptor_t *ld = data;
732 	char buf[LM_VNPATHLEN];
733 	proc_t p;
734 
735 	mdb_printf("%-?p %2s %04x %6d %-16s %-?p ",
736 	    addr, ld->l_type == F_RDLCK ? "RD" :
737 	    ld->l_type == F_WRLCK ? "WR" : "??",
738 	    ld->l_state, ld->l_flock.l_pid,
739 	    ld->l_flock.l_pid == 0 ? "<kernel>" :
740 	    mdb_pid2proc(ld->l_flock.l_pid, &p) == NULL ?
741 	    "<defunct>" : p.p_user.u_comm,
742 	    ld->l_vnode);
743 
744 	mdb_vnode2path((uintptr_t)ld->l_vnode, buf,
745 	    sizeof (buf));
746 	mdb_printf("%s\n", buf);
747 
748 	return (WALK_NEXT);
749 }
750 
751 /*ARGSUSED*/
752 int
753 lminfo(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
754 {
755 	if (DCMD_HDRSPEC(flags))
756 		mdb_printf("%<u>%-?s %2s %4s %6s %-16s %-?s %s%</u>\n",
757 		    "ADDR", "TP", "FLAG", "PID", "COMM", "VNODE", "PATH");
758 
759 	return (mdb_pwalk("lock_graph", lminfo_cb, NULL, NULL));
760 }
761 
762 /*ARGSUSED*/
763 int
764 seg(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
765 {
766 	struct seg s;
767 
768 	if (argc != 0)
769 		return (DCMD_USAGE);
770 
771 	if ((flags & DCMD_LOOPFIRST) || !(flags & DCMD_LOOP)) {
772 		mdb_printf("%<u>%?s %?s %?s %?s %s%</u>\n",
773 		    "SEG", "BASE", "SIZE", "DATA", "OPS");
774 	}
775 
776 	if (mdb_vread(&s, sizeof (s), addr) == -1) {
777 		mdb_warn("failed to read seg at %p", addr);
778 		return (DCMD_ERR);
779 	}
780 
781 	mdb_printf("%?p %?p %?lx %?p %a\n",
782 	    addr, s.s_base, s.s_size, s.s_data, s.s_ops);
783 
784 	return (DCMD_OK);
785 }
786 
787 /*ARGSUSED*/
788 static int
789 pmap_walk_anon(uintptr_t addr, const struct anon *anon, int *nres)
790 {
791 	uintptr_t pp =
792 	    mdb_vnode2page((uintptr_t)anon->an_vp, (uintptr_t)anon->an_off);
793 
794 	if (pp != NULL)
795 		(*nres)++;
796 
797 	return (WALK_NEXT);
798 }
799 
800 static int
801 pmap_walk_seg(uintptr_t addr, const struct seg *seg, uintptr_t segvn)
802 {
803 
804 	mdb_printf("%0?p %0?p %7dk", addr, seg->s_base, seg->s_size / 1024);
805 
806 	if (segvn == (uintptr_t)seg->s_ops) {
807 		struct segvn_data svn;
808 		int nres = 0;
809 
810 		(void) mdb_vread(&svn, sizeof (svn), (uintptr_t)seg->s_data);
811 
812 		if (svn.amp == NULL) {
813 			mdb_printf(" %8s", "");
814 			goto drive_on;
815 		}
816 
817 		/*
818 		 * We've got an amp for this segment; walk through
819 		 * the amp, and determine mappings.
820 		 */
821 		if (mdb_pwalk("anon", (mdb_walk_cb_t)pmap_walk_anon,
822 		    &nres, (uintptr_t)svn.amp) == -1)
823 			mdb_warn("failed to walk anon (amp=%p)", svn.amp);
824 
825 		mdb_printf(" %7dk", (nres * PAGESIZE) / 1024);
826 drive_on:
827 
828 		if (svn.vp != NULL) {
829 			char buf[29];
830 
831 			mdb_vnode2path((uintptr_t)svn.vp, buf, sizeof (buf));
832 			mdb_printf(" %s", buf);
833 		} else
834 			mdb_printf(" [ anon ]");
835 	}
836 
837 	mdb_printf("\n");
838 	return (WALK_NEXT);
839 }
840 
841 static int
842 pmap_walk_seg_quick(uintptr_t addr, const struct seg *seg, uintptr_t segvn)
843 {
844 	mdb_printf("%0?p %0?p %7dk", addr, seg->s_base, seg->s_size / 1024);
845 
846 	if (segvn == (uintptr_t)seg->s_ops) {
847 		struct segvn_data svn;
848 
849 		(void) mdb_vread(&svn, sizeof (svn), (uintptr_t)seg->s_data);
850 
851 		if (svn.vp != NULL) {
852 			mdb_printf(" %0?p", svn.vp);
853 		} else {
854 			mdb_printf(" [ anon ]");
855 		}
856 	}
857 
858 	mdb_printf("\n");
859 	return (WALK_NEXT);
860 }
861 
862 /*ARGSUSED*/
863 int
864 pmap(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
865 {
866 	uintptr_t segvn;
867 	proc_t proc;
868 	uint_t quick = FALSE;
869 	mdb_walk_cb_t cb = (mdb_walk_cb_t)pmap_walk_seg;
870 
871 	GElf_Sym sym;
872 
873 	if (!(flags & DCMD_ADDRSPEC))
874 		return (DCMD_USAGE);
875 
876 	if (mdb_getopts(argc, argv,
877 	    'q', MDB_OPT_SETBITS, TRUE, &quick, NULL) != argc)
878 		return (DCMD_USAGE);
879 
880 	if (mdb_vread(&proc, sizeof (proc), addr) == -1) {
881 		mdb_warn("failed to read proc at %p", addr);
882 		return (DCMD_ERR);
883 	}
884 
885 	if (mdb_lookup_by_name("segvn_ops", &sym) == 0)
886 		segvn = (uintptr_t)sym.st_value;
887 	else
888 		segvn = NULL;
889 
890 	mdb_printf("%?s %?s %8s ", "SEG", "BASE", "SIZE");
891 
892 	if (quick) {
893 		mdb_printf("VNODE\n");
894 		cb = (mdb_walk_cb_t)pmap_walk_seg_quick;
895 	} else {
896 		mdb_printf("%8s %s\n", "RES", "PATH");
897 	}
898 
899 	if (mdb_pwalk("seg", cb, (void *)segvn, (uintptr_t)proc.p_as) == -1) {
900 		mdb_warn("failed to walk segments of as %p", proc.p_as);
901 		return (DCMD_ERR);
902 	}
903 
904 	return (DCMD_OK);
905 }
906 
907 typedef struct anon_walk_data {
908 	uintptr_t *aw_levone;
909 	uintptr_t *aw_levtwo;
910 	int aw_nlevone;
911 	int aw_levone_ndx;
912 	int aw_levtwo_ndx;
913 	struct anon_map aw_amp;
914 	struct anon_hdr aw_ahp;
915 } anon_walk_data_t;
916 
917 int
918 anon_walk_init(mdb_walk_state_t *wsp)
919 {
920 	anon_walk_data_t *aw;
921 
922 	if (wsp->walk_addr == NULL) {
923 		mdb_warn("anon walk doesn't support global walks\n");
924 		return (WALK_ERR);
925 	}
926 
927 	aw = mdb_alloc(sizeof (anon_walk_data_t), UM_SLEEP);
928 
929 	if (mdb_vread(&aw->aw_amp, sizeof (aw->aw_amp), wsp->walk_addr) == -1) {
930 		mdb_warn("failed to read anon map at %p", wsp->walk_addr);
931 		mdb_free(aw, sizeof (anon_walk_data_t));
932 		return (WALK_ERR);
933 	}
934 
935 	if (mdb_vread(&aw->aw_ahp, sizeof (aw->aw_ahp),
936 	    (uintptr_t)(aw->aw_amp.ahp)) == -1) {
937 		mdb_warn("failed to read anon hdr ptr at %p", aw->aw_amp.ahp);
938 		mdb_free(aw, sizeof (anon_walk_data_t));
939 		return (WALK_ERR);
940 	}
941 
942 	if (aw->aw_ahp.size <= ANON_CHUNK_SIZE ||
943 	    (aw->aw_ahp.flags & ANON_ALLOC_FORCE)) {
944 		aw->aw_nlevone = aw->aw_ahp.size;
945 		aw->aw_levtwo = NULL;
946 	} else {
947 		aw->aw_nlevone =
948 		    (aw->aw_ahp.size + ANON_CHUNK_OFF) >> ANON_CHUNK_SHIFT;
949 		aw->aw_levtwo =
950 		    mdb_zalloc(ANON_CHUNK_SIZE * sizeof (uintptr_t), UM_SLEEP);
951 	}
952 
953 	aw->aw_levone =
954 	    mdb_alloc(aw->aw_nlevone * sizeof (uintptr_t), UM_SLEEP);
955 
956 	aw->aw_levone_ndx = 0;
957 	aw->aw_levtwo_ndx = 0;
958 
959 	mdb_vread(aw->aw_levone, aw->aw_nlevone * sizeof (uintptr_t),
960 	    (uintptr_t)aw->aw_ahp.array_chunk);
961 
962 	if (aw->aw_levtwo != NULL) {
963 		while (aw->aw_levone[aw->aw_levone_ndx] == NULL) {
964 			aw->aw_levone_ndx++;
965 			if (aw->aw_levone_ndx == aw->aw_nlevone) {
966 				mdb_warn("corrupt anon; couldn't"
967 				    "find ptr to lev two map");
968 				goto out;
969 			}
970 		}
971 
972 		mdb_vread(aw->aw_levtwo, ANON_CHUNK_SIZE * sizeof (uintptr_t),
973 		    aw->aw_levone[aw->aw_levone_ndx]);
974 	}
975 
976 out:
977 	wsp->walk_data = aw;
978 	return (0);
979 }
980 
981 int
982 anon_walk_step(mdb_walk_state_t *wsp)
983 {
984 	int status;
985 	anon_walk_data_t *aw = (anon_walk_data_t *)wsp->walk_data;
986 	struct anon anon;
987 	uintptr_t anonptr;
988 
989 again:
990 	/*
991 	 * Once we've walked through level one, we're done.
992 	 */
993 	if (aw->aw_levone_ndx == aw->aw_nlevone)
994 		return (WALK_DONE);
995 
996 	if (aw->aw_levtwo == NULL) {
997 		anonptr = aw->aw_levone[aw->aw_levone_ndx];
998 		aw->aw_levone_ndx++;
999 	} else {
1000 		anonptr = aw->aw_levtwo[aw->aw_levtwo_ndx];
1001 		aw->aw_levtwo_ndx++;
1002 
1003 		if (aw->aw_levtwo_ndx == ANON_CHUNK_SIZE) {
1004 			aw->aw_levtwo_ndx = 0;
1005 
1006 			do {
1007 				aw->aw_levone_ndx++;
1008 
1009 				if (aw->aw_levone_ndx == aw->aw_nlevone)
1010 					return (WALK_DONE);
1011 			} while (aw->aw_levone[aw->aw_levone_ndx] == NULL);
1012 
1013 			mdb_vread(aw->aw_levtwo, ANON_CHUNK_SIZE *
1014 			    sizeof (uintptr_t),
1015 			    aw->aw_levone[aw->aw_levone_ndx]);
1016 		}
1017 	}
1018 
1019 	if (anonptr != NULL) {
1020 		mdb_vread(&anon, sizeof (anon), anonptr);
1021 		status = wsp->walk_callback(anonptr, &anon, wsp->walk_cbdata);
1022 	} else
1023 		goto again;
1024 
1025 	return (status);
1026 }
1027 
1028 void
1029 anon_walk_fini(mdb_walk_state_t *wsp)
1030 {
1031 	anon_walk_data_t *aw = (anon_walk_data_t *)wsp->walk_data;
1032 
1033 	if (aw->aw_levtwo != NULL)
1034 		mdb_free(aw->aw_levtwo, ANON_CHUNK_SIZE * sizeof (uintptr_t));
1035 
1036 	mdb_free(aw->aw_levone, aw->aw_nlevone * sizeof (uintptr_t));
1037 	mdb_free(aw, sizeof (anon_walk_data_t));
1038 }
1039 
1040 /*ARGSUSED*/
1041 int
1042 whereopen_fwalk(uintptr_t addr, struct file *f, uintptr_t *target)
1043 {
1044 	if ((uintptr_t)f->f_vnode == *target) {
1045 		mdb_printf("file %p\n", addr);
1046 		*target = NULL;
1047 	}
1048 
1049 	return (WALK_NEXT);
1050 }
1051 
1052 /*ARGSUSED*/
1053 int
1054 whereopen_pwalk(uintptr_t addr, void *ignored, uintptr_t *target)
1055 {
1056 	uintptr_t t = *target;
1057 
1058 	if (mdb_pwalk("file", (mdb_walk_cb_t)whereopen_fwalk, &t, addr) == -1) {
1059 		mdb_warn("couldn't file walk proc %p", addr);
1060 		return (WALK_ERR);
1061 	}
1062 
1063 	if (t == NULL)
1064 		mdb_printf("%p\n", addr);
1065 
1066 	return (WALK_NEXT);
1067 }
1068 
1069 /*ARGSUSED*/
1070 int
1071 whereopen(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1072 {
1073 	uintptr_t target = addr;
1074 
1075 	if (!(flags & DCMD_ADDRSPEC) || addr == NULL)
1076 		return (DCMD_USAGE);
1077 
1078 	if (mdb_walk("proc", (mdb_walk_cb_t)whereopen_pwalk, &target) == -1) {
1079 		mdb_warn("can't proc walk");
1080 		return (DCMD_ERR);
1081 	}
1082 
1083 	return (DCMD_OK);
1084 }
1085 
1086 typedef struct datafmt {
1087 	char	*hdr1;
1088 	char	*hdr2;
1089 	char	*dashes;
1090 	char	*fmt;
1091 } datafmt_t;
1092 
1093 static datafmt_t kmemfmt[] = {
1094 	{ "cache                    ", "name                     ",
1095 	"-------------------------", "%-25s "				},
1096 	{ "   buf",	"  size",	"------",	"%6u "		},
1097 	{ "   buf",	"in use",	"------",	"%6u "		},
1098 	{ "   buf",	" total",	"------",	"%6u "		},
1099 	{ "   memory",	"   in use",	"----------",	"%9u%c "	},
1100 	{ "    alloc",	"  succeed",	"---------",	"%9u "		},
1101 	{ "alloc",	" fail",	"-----",	"%5u "		},
1102 	{ NULL,		NULL,		NULL,		NULL		}
1103 };
1104 
1105 static datafmt_t vmemfmt[] = {
1106 	{ "vmem                     ", "name                     ",
1107 	"-------------------------", "%-*s "				},
1108 	{ "   memory",	"   in use",	"----------",	"%9llu%c "	},
1109 	{ "    memory",	"     total",	"-----------",	"%10llu%c "	},
1110 	{ "   memory",	"   import",	"----------",	"%9llu%c "	},
1111 	{ "    alloc",	"  succeed",	"---------",	"%9llu "	},
1112 	{ "alloc",	" fail",	"-----",	"%5llu "	},
1113 	{ NULL,		NULL,		NULL,		NULL		}
1114 };
1115 
1116 /*ARGSUSED*/
1117 static int
1118 kmastat_cpu_avail(uintptr_t addr, const kmem_cpu_cache_t *ccp, int *avail)
1119 {
1120 	if (ccp->cc_rounds > 0)
1121 		*avail += ccp->cc_rounds;
1122 	if (ccp->cc_prounds > 0)
1123 		*avail += ccp->cc_prounds;
1124 
1125 	return (WALK_NEXT);
1126 }
1127 
1128 /*ARGSUSED*/
1129 static int
1130 kmastat_cpu_alloc(uintptr_t addr, const kmem_cpu_cache_t *ccp, int *alloc)
1131 {
1132 	*alloc += ccp->cc_alloc;
1133 
1134 	return (WALK_NEXT);
1135 }
1136 
1137 /*ARGSUSED*/
1138 static int
1139 kmastat_slab_avail(uintptr_t addr, const kmem_slab_t *sp, int *avail)
1140 {
1141 	*avail += sp->slab_chunks - sp->slab_refcnt;
1142 
1143 	return (WALK_NEXT);
1144 }
1145 
1146 typedef struct kmastat_vmem {
1147 	uintptr_t kv_addr;
1148 	struct kmastat_vmem *kv_next;
1149 	int kv_meminuse;
1150 	int kv_alloc;
1151 	int kv_fail;
1152 } kmastat_vmem_t;
1153 
1154 typedef struct kmastat_args {
1155 	kmastat_vmem_t **ka_kvpp;
1156 	uint_t ka_shift;
1157 } kmastat_args_t;
1158 
1159 static int
1160 kmastat_cache(uintptr_t addr, const kmem_cache_t *cp, kmastat_args_t *kap)
1161 {
1162 	kmastat_vmem_t **kvp = kap->ka_kvpp;
1163 	kmastat_vmem_t *kv;
1164 	datafmt_t *dfp = kmemfmt;
1165 	int magsize;
1166 
1167 	int avail, alloc, total;
1168 	size_t meminuse = (cp->cache_slab_create - cp->cache_slab_destroy) *
1169 	    cp->cache_slabsize;
1170 
1171 	mdb_walk_cb_t cpu_avail = (mdb_walk_cb_t)kmastat_cpu_avail;
1172 	mdb_walk_cb_t cpu_alloc = (mdb_walk_cb_t)kmastat_cpu_alloc;
1173 	mdb_walk_cb_t slab_avail = (mdb_walk_cb_t)kmastat_slab_avail;
1174 
1175 	magsize = kmem_get_magsize(cp);
1176 
1177 	alloc = cp->cache_slab_alloc + cp->cache_full.ml_alloc;
1178 	avail = cp->cache_full.ml_total * magsize;
1179 	total = cp->cache_buftotal;
1180 
1181 	(void) mdb_pwalk("kmem_cpu_cache", cpu_alloc, &alloc, addr);
1182 	(void) mdb_pwalk("kmem_cpu_cache", cpu_avail, &avail, addr);
1183 	(void) mdb_pwalk("kmem_slab_partial", slab_avail, &avail, addr);
1184 
1185 	for (kv = *kvp; kv != NULL; kv = kv->kv_next) {
1186 		if (kv->kv_addr == (uintptr_t)cp->cache_arena)
1187 			goto out;
1188 	}
1189 
1190 	kv = mdb_zalloc(sizeof (kmastat_vmem_t), UM_SLEEP | UM_GC);
1191 	kv->kv_next = *kvp;
1192 	kv->kv_addr = (uintptr_t)cp->cache_arena;
1193 	*kvp = kv;
1194 out:
1195 	kv->kv_meminuse += meminuse;
1196 	kv->kv_alloc += alloc;
1197 	kv->kv_fail += cp->cache_alloc_fail;
1198 
1199 	mdb_printf((dfp++)->fmt, cp->cache_name);
1200 	mdb_printf((dfp++)->fmt, cp->cache_bufsize);
1201 	mdb_printf((dfp++)->fmt, total - avail);
1202 	mdb_printf((dfp++)->fmt, total);
1203 	mdb_printf((dfp++)->fmt, meminuse >> kap->ka_shift,
1204 	    kap->ka_shift == GIGS ? 'G' : kap->ka_shift == MEGS ? 'M' :
1205 	    kap->ka_shift == KILOS ? 'K' : 'B');
1206 	mdb_printf((dfp++)->fmt, alloc);
1207 	mdb_printf((dfp++)->fmt, cp->cache_alloc_fail);
1208 	mdb_printf("\n");
1209 
1210 	return (WALK_NEXT);
1211 }
1212 
1213 static int
1214 kmastat_vmem_totals(uintptr_t addr, const vmem_t *v, kmastat_args_t *kap)
1215 {
1216 	kmastat_vmem_t *kv = *kap->ka_kvpp;
1217 	size_t len;
1218 
1219 	while (kv != NULL && kv->kv_addr != addr)
1220 		kv = kv->kv_next;
1221 
1222 	if (kv == NULL || kv->kv_alloc == 0)
1223 		return (WALK_NEXT);
1224 
1225 	len = MIN(17, strlen(v->vm_name));
1226 
1227 	mdb_printf("Total [%s]%*s %6s %6s %6s %9u%c %9u %5u\n", v->vm_name,
1228 	    17 - len, "", "", "", "",
1229 	    kv->kv_meminuse >> kap->ka_shift,
1230 	    kap->ka_shift == GIGS ? 'G' : kap->ka_shift == MEGS ? 'M' :
1231 	    kap->ka_shift == KILOS ? 'K' : 'B', kv->kv_alloc, kv->kv_fail);
1232 
1233 	return (WALK_NEXT);
1234 }
1235 
1236 /*ARGSUSED*/
1237 static int
1238 kmastat_vmem(uintptr_t addr, const vmem_t *v, const uint_t *shiftp)
1239 {
1240 	datafmt_t *dfp = vmemfmt;
1241 	const vmem_kstat_t *vkp = &v->vm_kstat;
1242 	uintptr_t paddr;
1243 	vmem_t parent;
1244 	int ident = 0;
1245 
1246 	for (paddr = (uintptr_t)v->vm_source; paddr != NULL; ident += 4) {
1247 		if (mdb_vread(&parent, sizeof (parent), paddr) == -1) {
1248 			mdb_warn("couldn't trace %p's ancestry", addr);
1249 			ident = 0;
1250 			break;
1251 		}
1252 		paddr = (uintptr_t)parent.vm_source;
1253 	}
1254 
1255 	mdb_printf("%*s", ident, "");
1256 	mdb_printf((dfp++)->fmt, 25 - ident, v->vm_name);
1257 	mdb_printf((dfp++)->fmt, vkp->vk_mem_inuse.value.ui64 >> *shiftp,
1258 	    *shiftp == GIGS ? 'G' : *shiftp == MEGS ? 'M' :
1259 	    *shiftp == KILOS ? 'K' : 'B');
1260 	mdb_printf((dfp++)->fmt, vkp->vk_mem_total.value.ui64 >> *shiftp,
1261 	    *shiftp == GIGS ? 'G' : *shiftp == MEGS ? 'M' :
1262 	    *shiftp == KILOS ? 'K' : 'B');
1263 	mdb_printf((dfp++)->fmt, vkp->vk_mem_import.value.ui64 >> *shiftp,
1264 	    *shiftp == GIGS ? 'G' : *shiftp == MEGS ? 'M' :
1265 	    *shiftp == KILOS ? 'K' : 'B');
1266 	mdb_printf((dfp++)->fmt, vkp->vk_alloc.value.ui64);
1267 	mdb_printf((dfp++)->fmt, vkp->vk_fail.value.ui64);
1268 
1269 	mdb_printf("\n");
1270 
1271 	return (WALK_NEXT);
1272 }
1273 
1274 /*ARGSUSED*/
1275 int
1276 kmastat(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1277 {
1278 	kmastat_vmem_t *kv = NULL;
1279 	datafmt_t *dfp;
1280 	kmastat_args_t ka;
1281 
1282 	ka.ka_shift = 0;
1283 	if (mdb_getopts(argc, argv,
1284 	    'k', MDB_OPT_SETBITS, KILOS, &ka.ka_shift,
1285 	    'm', MDB_OPT_SETBITS, MEGS, &ka.ka_shift,
1286 	    'g', MDB_OPT_SETBITS, GIGS, &ka.ka_shift, NULL) != argc)
1287 		return (DCMD_USAGE);
1288 
1289 	for (dfp = kmemfmt; dfp->hdr1 != NULL; dfp++)
1290 		mdb_printf("%s ", dfp->hdr1);
1291 	mdb_printf("\n");
1292 
1293 	for (dfp = kmemfmt; dfp->hdr1 != NULL; dfp++)
1294 		mdb_printf("%s ", dfp->hdr2);
1295 	mdb_printf("\n");
1296 
1297 	for (dfp = kmemfmt; dfp->hdr1 != NULL; dfp++)
1298 		mdb_printf("%s ", dfp->dashes);
1299 	mdb_printf("\n");
1300 
1301 	ka.ka_kvpp = &kv;
1302 	if (mdb_walk("kmem_cache", (mdb_walk_cb_t)kmastat_cache, &ka) == -1) {
1303 		mdb_warn("can't walk 'kmem_cache'");
1304 		return (DCMD_ERR);
1305 	}
1306 
1307 	for (dfp = kmemfmt; dfp->hdr1 != NULL; dfp++)
1308 		mdb_printf("%s ", dfp->dashes);
1309 	mdb_printf("\n");
1310 
1311 	if (mdb_walk("vmem", (mdb_walk_cb_t)kmastat_vmem_totals, &ka) == -1) {
1312 		mdb_warn("can't walk 'vmem'");
1313 		return (DCMD_ERR);
1314 	}
1315 
1316 	for (dfp = kmemfmt; dfp->hdr1 != NULL; dfp++)
1317 		mdb_printf("%s ", dfp->dashes);
1318 	mdb_printf("\n");
1319 
1320 	mdb_printf("\n");
1321 
1322 	for (dfp = vmemfmt; dfp->hdr1 != NULL; dfp++)
1323 		mdb_printf("%s ", dfp->hdr1);
1324 	mdb_printf("\n");
1325 
1326 	for (dfp = vmemfmt; dfp->hdr1 != NULL; dfp++)
1327 		mdb_printf("%s ", dfp->hdr2);
1328 	mdb_printf("\n");
1329 
1330 	for (dfp = vmemfmt; dfp->hdr1 != NULL; dfp++)
1331 		mdb_printf("%s ", dfp->dashes);
1332 	mdb_printf("\n");
1333 
1334 	if (mdb_walk("vmem", (mdb_walk_cb_t)kmastat_vmem, &ka.ka_shift) == -1) {
1335 		mdb_warn("can't walk 'vmem'");
1336 		return (DCMD_ERR);
1337 	}
1338 
1339 	for (dfp = vmemfmt; dfp->hdr1 != NULL; dfp++)
1340 		mdb_printf("%s ", dfp->dashes);
1341 	mdb_printf("\n");
1342 	return (DCMD_OK);
1343 }
1344 
1345 /*
1346  * Our ::kgrep callback scans the entire kernel VA space (kas).  kas is made
1347  * up of a set of 'struct seg's.  We could just scan each seg en masse, but
1348  * unfortunately, a few of the segs are both large and sparse, so we could
1349  * spend quite a bit of time scanning VAs which have no backing pages.
1350  *
1351  * So for the few very sparse segs, we skip the segment itself, and scan
1352  * the allocated vmem_segs in the vmem arena which manages that part of kas.
1353  * Currently, we do this for:
1354  *
1355  *	SEG		VMEM ARENA
1356  *	kvseg		heap_arena
1357  *	kvseg32		heap32_arena
1358  *	kvseg_core	heap_core_arena
1359  *
1360  * In addition, we skip the segkpm segment in its entirety, since it is very
1361  * sparse, and contains no new kernel data.
1362  */
1363 typedef struct kgrep_walk_data {
1364 	kgrep_cb_func *kg_cb;
1365 	void *kg_cbdata;
1366 	uintptr_t kg_kvseg;
1367 	uintptr_t kg_kvseg32;
1368 	uintptr_t kg_kvseg_core;
1369 	uintptr_t kg_segkpm;
1370 	uintptr_t kg_heap_lp_base;
1371 	uintptr_t kg_heap_lp_end;
1372 } kgrep_walk_data_t;
1373 
1374 static int
1375 kgrep_walk_seg(uintptr_t addr, const struct seg *seg, kgrep_walk_data_t *kg)
1376 {
1377 	uintptr_t base = (uintptr_t)seg->s_base;
1378 
1379 	if (addr == kg->kg_kvseg || addr == kg->kg_kvseg32 ||
1380 	    addr == kg->kg_kvseg_core)
1381 		return (WALK_NEXT);
1382 
1383 	if ((uintptr_t)seg->s_ops == kg->kg_segkpm)
1384 		return (WALK_NEXT);
1385 
1386 	return (kg->kg_cb(base, base + seg->s_size, kg->kg_cbdata));
1387 }
1388 
1389 /*ARGSUSED*/
1390 static int
1391 kgrep_walk_vseg(uintptr_t addr, const vmem_seg_t *seg, kgrep_walk_data_t *kg)
1392 {
1393 	/*
1394 	 * skip large page heap address range - it is scanned by walking
1395 	 * allocated vmem_segs in the heap_lp_arena
1396 	 */
1397 	if (seg->vs_start == kg->kg_heap_lp_base &&
1398 	    seg->vs_end == kg->kg_heap_lp_end)
1399 		return (WALK_NEXT);
1400 
1401 	return (kg->kg_cb(seg->vs_start, seg->vs_end, kg->kg_cbdata));
1402 }
1403 
1404 /*ARGSUSED*/
1405 static int
1406 kgrep_xwalk_vseg(uintptr_t addr, const vmem_seg_t *seg, kgrep_walk_data_t *kg)
1407 {
1408 	return (kg->kg_cb(seg->vs_start, seg->vs_end, kg->kg_cbdata));
1409 }
1410 
1411 static int
1412 kgrep_walk_vmem(uintptr_t addr, const vmem_t *vmem, kgrep_walk_data_t *kg)
1413 {
1414 	mdb_walk_cb_t walk_vseg = (mdb_walk_cb_t)kgrep_walk_vseg;
1415 
1416 	if (strcmp(vmem->vm_name, "heap") != 0 &&
1417 	    strcmp(vmem->vm_name, "heap32") != 0 &&
1418 	    strcmp(vmem->vm_name, "heap_core") != 0 &&
1419 	    strcmp(vmem->vm_name, "heap_lp") != 0)
1420 		return (WALK_NEXT);
1421 
1422 	if (strcmp(vmem->vm_name, "heap_lp") == 0)
1423 		walk_vseg = (mdb_walk_cb_t)kgrep_xwalk_vseg;
1424 
1425 	if (mdb_pwalk("vmem_alloc", walk_vseg, kg, addr) == -1) {
1426 		mdb_warn("couldn't walk vmem_alloc for vmem %p", addr);
1427 		return (WALK_ERR);
1428 	}
1429 
1430 	return (WALK_NEXT);
1431 }
1432 
1433 int
1434 kgrep_subr(kgrep_cb_func *cb, void *cbdata)
1435 {
1436 	GElf_Sym kas, kvseg, kvseg32, kvseg_core, segkpm;
1437 	kgrep_walk_data_t kg;
1438 
1439 	if (mdb_get_state() == MDB_STATE_RUNNING) {
1440 		mdb_warn("kgrep can only be run on a system "
1441 		    "dump or under kmdb; see dumpadm(1M)\n");
1442 		return (DCMD_ERR);
1443 	}
1444 
1445 	if (mdb_lookup_by_name("kas", &kas) == -1) {
1446 		mdb_warn("failed to locate 'kas' symbol\n");
1447 		return (DCMD_ERR);
1448 	}
1449 
1450 	if (mdb_lookup_by_name("kvseg", &kvseg) == -1) {
1451 		mdb_warn("failed to locate 'kvseg' symbol\n");
1452 		return (DCMD_ERR);
1453 	}
1454 
1455 	if (mdb_lookup_by_name("kvseg32", &kvseg32) == -1) {
1456 		mdb_warn("failed to locate 'kvseg32' symbol\n");
1457 		return (DCMD_ERR);
1458 	}
1459 
1460 	if (mdb_lookup_by_name("kvseg_core", &kvseg_core) == -1) {
1461 		mdb_warn("failed to locate 'kvseg_core' symbol\n");
1462 		return (DCMD_ERR);
1463 	}
1464 
1465 	if (mdb_lookup_by_name("segkpm_ops", &segkpm) == -1) {
1466 		mdb_warn("failed to locate 'segkpm_ops' symbol\n");
1467 		return (DCMD_ERR);
1468 	}
1469 
1470 	if (mdb_readvar(&kg.kg_heap_lp_base, "heap_lp_base") == -1) {
1471 		mdb_warn("failed to read 'heap_lp_base'\n");
1472 		return (DCMD_ERR);
1473 	}
1474 
1475 	if (mdb_readvar(&kg.kg_heap_lp_end, "heap_lp_end") == -1) {
1476 		mdb_warn("failed to read 'heap_lp_end'\n");
1477 		return (DCMD_ERR);
1478 	}
1479 
1480 	kg.kg_cb = cb;
1481 	kg.kg_cbdata = cbdata;
1482 	kg.kg_kvseg = (uintptr_t)kvseg.st_value;
1483 	kg.kg_kvseg32 = (uintptr_t)kvseg32.st_value;
1484 	kg.kg_kvseg_core = (uintptr_t)kvseg_core.st_value;
1485 	kg.kg_segkpm = (uintptr_t)segkpm.st_value;
1486 
1487 	if (mdb_pwalk("seg", (mdb_walk_cb_t)kgrep_walk_seg,
1488 	    &kg, kas.st_value) == -1) {
1489 		mdb_warn("failed to walk kas segments");
1490 		return (DCMD_ERR);
1491 	}
1492 
1493 	if (mdb_walk("vmem", (mdb_walk_cb_t)kgrep_walk_vmem, &kg) == -1) {
1494 		mdb_warn("failed to walk heap/heap32 vmem arenas");
1495 		return (DCMD_ERR);
1496 	}
1497 
1498 	return (DCMD_OK);
1499 }
1500 
1501 size_t
1502 kgrep_subr_pagesize(void)
1503 {
1504 	return (PAGESIZE);
1505 }
1506 
1507 typedef struct file_walk_data {
1508 	struct uf_entry *fw_flist;
1509 	int fw_flistsz;
1510 	int fw_ndx;
1511 	int fw_nofiles;
1512 } file_walk_data_t;
1513 
1514 int
1515 file_walk_init(mdb_walk_state_t *wsp)
1516 {
1517 	file_walk_data_t *fw;
1518 	proc_t p;
1519 
1520 	if (wsp->walk_addr == NULL) {
1521 		mdb_warn("file walk doesn't support global walks\n");
1522 		return (WALK_ERR);
1523 	}
1524 
1525 	fw = mdb_alloc(sizeof (file_walk_data_t), UM_SLEEP);
1526 
1527 	if (mdb_vread(&p, sizeof (p), wsp->walk_addr) == -1) {
1528 		mdb_free(fw, sizeof (file_walk_data_t));
1529 		mdb_warn("failed to read proc structure at %p", wsp->walk_addr);
1530 		return (WALK_ERR);
1531 	}
1532 
1533 	if (p.p_user.u_finfo.fi_nfiles == 0) {
1534 		mdb_free(fw, sizeof (file_walk_data_t));
1535 		return (WALK_DONE);
1536 	}
1537 
1538 	fw->fw_nofiles = p.p_user.u_finfo.fi_nfiles;
1539 	fw->fw_flistsz = sizeof (struct uf_entry) * fw->fw_nofiles;
1540 	fw->fw_flist = mdb_alloc(fw->fw_flistsz, UM_SLEEP);
1541 
1542 	if (mdb_vread(fw->fw_flist, fw->fw_flistsz,
1543 	    (uintptr_t)p.p_user.u_finfo.fi_list) == -1) {
1544 		mdb_warn("failed to read file array at %p",
1545 		    p.p_user.u_finfo.fi_list);
1546 		mdb_free(fw->fw_flist, fw->fw_flistsz);
1547 		mdb_free(fw, sizeof (file_walk_data_t));
1548 		return (WALK_ERR);
1549 	}
1550 
1551 	fw->fw_ndx = 0;
1552 	wsp->walk_data = fw;
1553 
1554 	return (WALK_NEXT);
1555 }
1556 
1557 int
1558 file_walk_step(mdb_walk_state_t *wsp)
1559 {
1560 	file_walk_data_t *fw = (file_walk_data_t *)wsp->walk_data;
1561 	struct file file;
1562 	uintptr_t fp;
1563 
1564 again:
1565 	if (fw->fw_ndx == fw->fw_nofiles)
1566 		return (WALK_DONE);
1567 
1568 	if ((fp = (uintptr_t)fw->fw_flist[fw->fw_ndx++].uf_file) == NULL)
1569 		goto again;
1570 
1571 	(void) mdb_vread(&file, sizeof (file), (uintptr_t)fp);
1572 	return (wsp->walk_callback(fp, &file, wsp->walk_cbdata));
1573 }
1574 
1575 int
1576 allfile_walk_step(mdb_walk_state_t *wsp)
1577 {
1578 	file_walk_data_t *fw = (file_walk_data_t *)wsp->walk_data;
1579 	struct file file;
1580 	uintptr_t fp;
1581 
1582 	if (fw->fw_ndx == fw->fw_nofiles)
1583 		return (WALK_DONE);
1584 
1585 	if ((fp = (uintptr_t)fw->fw_flist[fw->fw_ndx++].uf_file) != NULL)
1586 		(void) mdb_vread(&file, sizeof (file), (uintptr_t)fp);
1587 	else
1588 		bzero(&file, sizeof (file));
1589 
1590 	return (wsp->walk_callback(fp, &file, wsp->walk_cbdata));
1591 }
1592 
1593 void
1594 file_walk_fini(mdb_walk_state_t *wsp)
1595 {
1596 	file_walk_data_t *fw = (file_walk_data_t *)wsp->walk_data;
1597 
1598 	mdb_free(fw->fw_flist, fw->fw_flistsz);
1599 	mdb_free(fw, sizeof (file_walk_data_t));
1600 }
1601 
1602 int
1603 port_walk_init(mdb_walk_state_t *wsp)
1604 {
1605 	if (wsp->walk_addr == NULL) {
1606 		mdb_warn("port walk doesn't support global walks\n");
1607 		return (WALK_ERR);
1608 	}
1609 
1610 	if (mdb_layered_walk("file", wsp) == -1) {
1611 		mdb_warn("couldn't walk 'file'");
1612 		return (WALK_ERR);
1613 	}
1614 	return (WALK_NEXT);
1615 }
1616 
1617 int
1618 port_walk_step(mdb_walk_state_t *wsp)
1619 {
1620 	struct vnode	vn;
1621 	uintptr_t	vp;
1622 	uintptr_t	pp;
1623 	struct port	port;
1624 
1625 	vp = (uintptr_t)((struct file *)wsp->walk_layer)->f_vnode;
1626 	if (mdb_vread(&vn, sizeof (vn), vp) == -1) {
1627 		mdb_warn("failed to read vnode_t at %p", vp);
1628 		return (WALK_ERR);
1629 	}
1630 	if (vn.v_type != VPORT)
1631 		return (WALK_NEXT);
1632 
1633 	pp = (uintptr_t)vn.v_data;
1634 	if (mdb_vread(&port, sizeof (port), pp) == -1) {
1635 		mdb_warn("failed to read port_t at %p", pp);
1636 		return (WALK_ERR);
1637 	}
1638 	return (wsp->walk_callback(pp, &port, wsp->walk_cbdata));
1639 }
1640 
1641 typedef struct portev_walk_data {
1642 	list_node_t	*pev_node;
1643 	list_node_t	*pev_last;
1644 	size_t		pev_offset;
1645 } portev_walk_data_t;
1646 
1647 int
1648 portev_walk_init(mdb_walk_state_t *wsp)
1649 {
1650 	portev_walk_data_t *pevd;
1651 	struct port	port;
1652 	struct vnode	vn;
1653 	struct list	*list;
1654 	uintptr_t	vp;
1655 
1656 	if (wsp->walk_addr == NULL) {
1657 		mdb_warn("portev walk doesn't support global walks\n");
1658 		return (WALK_ERR);
1659 	}
1660 
1661 	pevd = mdb_alloc(sizeof (portev_walk_data_t), UM_SLEEP);
1662 
1663 	if (mdb_vread(&port, sizeof (port), wsp->walk_addr) == -1) {
1664 		mdb_free(pevd, sizeof (portev_walk_data_t));
1665 		mdb_warn("failed to read port structure at %p", wsp->walk_addr);
1666 		return (WALK_ERR);
1667 	}
1668 
1669 	vp = (uintptr_t)port.port_vnode;
1670 	if (mdb_vread(&vn, sizeof (vn), vp) == -1) {
1671 		mdb_free(pevd, sizeof (portev_walk_data_t));
1672 		mdb_warn("failed to read vnode_t at %p", vp);
1673 		return (WALK_ERR);
1674 	}
1675 
1676 	if (vn.v_type != VPORT) {
1677 		mdb_free(pevd, sizeof (portev_walk_data_t));
1678 		mdb_warn("input address (%p) does not point to an event port",
1679 		    wsp->walk_addr);
1680 		return (WALK_ERR);
1681 	}
1682 
1683 	if (port.port_queue.portq_nent == 0) {
1684 		mdb_free(pevd, sizeof (portev_walk_data_t));
1685 		return (WALK_DONE);
1686 	}
1687 	list = &port.port_queue.portq_list;
1688 	pevd->pev_offset = list->list_offset;
1689 	pevd->pev_last = list->list_head.list_prev;
1690 	pevd->pev_node = list->list_head.list_next;
1691 	wsp->walk_data = pevd;
1692 	return (WALK_NEXT);
1693 }
1694 
1695 int
1696 portev_walk_step(mdb_walk_state_t *wsp)
1697 {
1698 	portev_walk_data_t	*pevd;
1699 	struct port_kevent	ev;
1700 	uintptr_t		evp;
1701 
1702 	pevd = (portev_walk_data_t *)wsp->walk_data;
1703 
1704 	if (pevd->pev_last == NULL)
1705 		return (WALK_DONE);
1706 	if (pevd->pev_node == pevd->pev_last)
1707 		pevd->pev_last = NULL;		/* last round */
1708 
1709 	evp = ((uintptr_t)(((char *)pevd->pev_node) - pevd->pev_offset));
1710 	if (mdb_vread(&ev, sizeof (ev), evp) == -1) {
1711 		mdb_warn("failed to read port_kevent at %p", evp);
1712 		return (WALK_DONE);
1713 	}
1714 	pevd->pev_node = ev.portkev_node.list_next;
1715 	return (wsp->walk_callback(evp, &ev, wsp->walk_cbdata));
1716 }
1717 
1718 void
1719 portev_walk_fini(mdb_walk_state_t *wsp)
1720 {
1721 	portev_walk_data_t *pevd = (portev_walk_data_t *)wsp->walk_data;
1722 
1723 	if (pevd != NULL)
1724 		mdb_free(pevd, sizeof (portev_walk_data_t));
1725 }
1726 
1727 typedef struct proc_walk_data {
1728 	uintptr_t *pw_stack;
1729 	int pw_depth;
1730 	int pw_max;
1731 } proc_walk_data_t;
1732 
1733 int
1734 proc_walk_init(mdb_walk_state_t *wsp)
1735 {
1736 	GElf_Sym sym;
1737 	proc_walk_data_t *pw;
1738 
1739 	if (wsp->walk_addr == NULL) {
1740 		if (mdb_lookup_by_name("p0", &sym) == -1) {
1741 			mdb_warn("failed to read 'practive'");
1742 			return (WALK_ERR);
1743 		}
1744 		wsp->walk_addr = (uintptr_t)sym.st_value;
1745 	}
1746 
1747 	pw = mdb_zalloc(sizeof (proc_walk_data_t), UM_SLEEP);
1748 
1749 	if (mdb_readvar(&pw->pw_max, "nproc") == -1) {
1750 		mdb_warn("failed to read 'nproc'");
1751 		mdb_free(pw, sizeof (pw));
1752 		return (WALK_ERR);
1753 	}
1754 
1755 	pw->pw_stack = mdb_alloc(pw->pw_max * sizeof (uintptr_t), UM_SLEEP);
1756 	wsp->walk_data = pw;
1757 
1758 	return (WALK_NEXT);
1759 }
1760 
1761 int
1762 proc_walk_step(mdb_walk_state_t *wsp)
1763 {
1764 	proc_walk_data_t *pw = wsp->walk_data;
1765 	uintptr_t addr = wsp->walk_addr;
1766 	uintptr_t cld, sib;
1767 
1768 	int status;
1769 	proc_t pr;
1770 
1771 	if (mdb_vread(&pr, sizeof (proc_t), addr) == -1) {
1772 		mdb_warn("failed to read proc at %p", addr);
1773 		return (WALK_DONE);
1774 	}
1775 
1776 	cld = (uintptr_t)pr.p_child;
1777 	sib = (uintptr_t)pr.p_sibling;
1778 
1779 	if (pw->pw_depth > 0 && addr == pw->pw_stack[pw->pw_depth - 1]) {
1780 		pw->pw_depth--;
1781 		goto sib;
1782 	}
1783 
1784 	status = wsp->walk_callback(addr, &pr, wsp->walk_cbdata);
1785 
1786 	if (status != WALK_NEXT)
1787 		return (status);
1788 
1789 	if ((wsp->walk_addr = cld) != NULL) {
1790 		if (mdb_vread(&pr, sizeof (proc_t), cld) == -1) {
1791 			mdb_warn("proc %p has invalid p_child %p; skipping\n",
1792 			    addr, cld);
1793 			goto sib;
1794 		}
1795 
1796 		pw->pw_stack[pw->pw_depth++] = addr;
1797 
1798 		if (pw->pw_depth == pw->pw_max) {
1799 			mdb_warn("depth %d exceeds max depth; try again\n",
1800 			    pw->pw_depth);
1801 			return (WALK_DONE);
1802 		}
1803 		return (WALK_NEXT);
1804 	}
1805 
1806 sib:
1807 	/*
1808 	 * We know that p0 has no siblings, and if another starting proc
1809 	 * was given, we don't want to walk its siblings anyway.
1810 	 */
1811 	if (pw->pw_depth == 0)
1812 		return (WALK_DONE);
1813 
1814 	if (sib != NULL && mdb_vread(&pr, sizeof (proc_t), sib) == -1) {
1815 		mdb_warn("proc %p has invalid p_sibling %p; skipping\n",
1816 		    addr, sib);
1817 		sib = NULL;
1818 	}
1819 
1820 	if ((wsp->walk_addr = sib) == NULL) {
1821 		if (pw->pw_depth > 0) {
1822 			wsp->walk_addr = pw->pw_stack[pw->pw_depth - 1];
1823 			return (WALK_NEXT);
1824 		}
1825 		return (WALK_DONE);
1826 	}
1827 
1828 	return (WALK_NEXT);
1829 }
1830 
1831 void
1832 proc_walk_fini(mdb_walk_state_t *wsp)
1833 {
1834 	proc_walk_data_t *pw = wsp->walk_data;
1835 
1836 	mdb_free(pw->pw_stack, pw->pw_max * sizeof (uintptr_t));
1837 	mdb_free(pw, sizeof (proc_walk_data_t));
1838 }
1839 
1840 int
1841 task_walk_init(mdb_walk_state_t *wsp)
1842 {
1843 	task_t task;
1844 
1845 	if (mdb_vread(&task, sizeof (task_t), wsp->walk_addr) == -1) {
1846 		mdb_warn("failed to read task at %p", wsp->walk_addr);
1847 		return (WALK_ERR);
1848 	}
1849 	wsp->walk_addr = (uintptr_t)task.tk_memb_list;
1850 	wsp->walk_data = task.tk_memb_list;
1851 	return (WALK_NEXT);
1852 }
1853 
1854 int
1855 task_walk_step(mdb_walk_state_t *wsp)
1856 {
1857 	proc_t proc;
1858 	int status;
1859 
1860 	if (mdb_vread(&proc, sizeof (proc_t), wsp->walk_addr) == -1) {
1861 		mdb_warn("failed to read proc at %p", wsp->walk_addr);
1862 		return (WALK_DONE);
1863 	}
1864 
1865 	status = wsp->walk_callback(wsp->walk_addr, NULL, wsp->walk_cbdata);
1866 
1867 	if (proc.p_tasknext == wsp->walk_data)
1868 		return (WALK_DONE);
1869 
1870 	wsp->walk_addr = (uintptr_t)proc.p_tasknext;
1871 	return (status);
1872 }
1873 
1874 int
1875 project_walk_init(mdb_walk_state_t *wsp)
1876 {
1877 	if (wsp->walk_addr == NULL) {
1878 		if (mdb_readvar(&wsp->walk_addr, "proj0p") == -1) {
1879 			mdb_warn("failed to read 'proj0p'");
1880 			return (WALK_ERR);
1881 		}
1882 	}
1883 	wsp->walk_data = (void *)wsp->walk_addr;
1884 	return (WALK_NEXT);
1885 }
1886 
1887 int
1888 project_walk_step(mdb_walk_state_t *wsp)
1889 {
1890 	uintptr_t addr = wsp->walk_addr;
1891 	kproject_t pj;
1892 	int status;
1893 
1894 	if (mdb_vread(&pj, sizeof (kproject_t), addr) == -1) {
1895 		mdb_warn("failed to read project at %p", addr);
1896 		return (WALK_DONE);
1897 	}
1898 	status = wsp->walk_callback(addr, &pj, wsp->walk_cbdata);
1899 	if (status != WALK_NEXT)
1900 		return (status);
1901 	wsp->walk_addr = (uintptr_t)pj.kpj_next;
1902 	if ((void *)wsp->walk_addr == wsp->walk_data)
1903 		return (WALK_DONE);
1904 	return (WALK_NEXT);
1905 }
1906 
1907 static int
1908 generic_walk_step(mdb_walk_state_t *wsp)
1909 {
1910 	return (wsp->walk_callback(wsp->walk_addr, wsp->walk_layer,
1911 	    wsp->walk_cbdata));
1912 }
1913 
1914 int
1915 seg_walk_init(mdb_walk_state_t *wsp)
1916 {
1917 	if (wsp->walk_addr == NULL) {
1918 		mdb_warn("seg walk must begin at struct as *\n");
1919 		return (WALK_ERR);
1920 	}
1921 
1922 	/*
1923 	 * this is really just a wrapper to AVL tree walk
1924 	 */
1925 	wsp->walk_addr = (uintptr_t)&((struct as *)wsp->walk_addr)->a_segtree;
1926 	return (avl_walk_init(wsp));
1927 }
1928 
1929 static int
1930 cpu_walk_cmp(const void *l, const void *r)
1931 {
1932 	uintptr_t lhs = *((uintptr_t *)l);
1933 	uintptr_t rhs = *((uintptr_t *)r);
1934 	cpu_t lcpu, rcpu;
1935 
1936 	(void) mdb_vread(&lcpu, sizeof (lcpu), lhs);
1937 	(void) mdb_vread(&rcpu, sizeof (rcpu), rhs);
1938 
1939 	if (lcpu.cpu_id < rcpu.cpu_id)
1940 		return (-1);
1941 
1942 	if (lcpu.cpu_id > rcpu.cpu_id)
1943 		return (1);
1944 
1945 	return (0);
1946 }
1947 
1948 typedef struct cpu_walk {
1949 	uintptr_t *cw_array;
1950 	int cw_ndx;
1951 } cpu_walk_t;
1952 
1953 int
1954 cpu_walk_init(mdb_walk_state_t *wsp)
1955 {
1956 	cpu_walk_t *cw;
1957 	int max_ncpus, i = 0;
1958 	uintptr_t current, first;
1959 	cpu_t cpu, panic_cpu;
1960 	uintptr_t panicstr, addr;
1961 	GElf_Sym sym;
1962 
1963 	cw = mdb_zalloc(sizeof (cpu_walk_t), UM_SLEEP | UM_GC);
1964 
1965 	if (mdb_readvar(&max_ncpus, "max_ncpus") == -1) {
1966 		mdb_warn("failed to read 'max_ncpus'");
1967 		return (WALK_ERR);
1968 	}
1969 
1970 	if (mdb_readvar(&panicstr, "panicstr") == -1) {
1971 		mdb_warn("failed to read 'panicstr'");
1972 		return (WALK_ERR);
1973 	}
1974 
1975 	if (panicstr != NULL) {
1976 		if (mdb_lookup_by_name("panic_cpu", &sym) == -1) {
1977 			mdb_warn("failed to find 'panic_cpu'");
1978 			return (WALK_ERR);
1979 		}
1980 
1981 		addr = (uintptr_t)sym.st_value;
1982 
1983 		if (mdb_vread(&panic_cpu, sizeof (cpu_t), addr) == -1) {
1984 			mdb_warn("failed to read 'panic_cpu'");
1985 			return (WALK_ERR);
1986 		}
1987 	}
1988 
1989 	/*
1990 	 * Unfortunately, there is no platform-independent way to walk
1991 	 * CPUs in ID order.  We therefore loop through in cpu_next order,
1992 	 * building an array of CPU pointers which will subsequently be
1993 	 * sorted.
1994 	 */
1995 	cw->cw_array =
1996 	    mdb_zalloc((max_ncpus + 1) * sizeof (uintptr_t), UM_SLEEP | UM_GC);
1997 
1998 	if (mdb_readvar(&first, "cpu_list") == -1) {
1999 		mdb_warn("failed to read 'cpu_list'");
2000 		return (WALK_ERR);
2001 	}
2002 
2003 	current = first;
2004 	do {
2005 		if (mdb_vread(&cpu, sizeof (cpu), current) == -1) {
2006 			mdb_warn("failed to read cpu at %p", current);
2007 			return (WALK_ERR);
2008 		}
2009 
2010 		if (panicstr != NULL && panic_cpu.cpu_id == cpu.cpu_id) {
2011 			cw->cw_array[i++] = addr;
2012 		} else {
2013 			cw->cw_array[i++] = current;
2014 		}
2015 	} while ((current = (uintptr_t)cpu.cpu_next) != first);
2016 
2017 	qsort(cw->cw_array, i, sizeof (uintptr_t), cpu_walk_cmp);
2018 	wsp->walk_data = cw;
2019 
2020 	return (WALK_NEXT);
2021 }
2022 
2023 int
2024 cpu_walk_step(mdb_walk_state_t *wsp)
2025 {
2026 	cpu_walk_t *cw = wsp->walk_data;
2027 	cpu_t cpu;
2028 	uintptr_t addr = cw->cw_array[cw->cw_ndx++];
2029 
2030 	if (addr == NULL)
2031 		return (WALK_DONE);
2032 
2033 	if (mdb_vread(&cpu, sizeof (cpu), addr) == -1) {
2034 		mdb_warn("failed to read cpu at %p", addr);
2035 		return (WALK_DONE);
2036 	}
2037 
2038 	return (wsp->walk_callback(addr, &cpu, wsp->walk_cbdata));
2039 }
2040 
2041 typedef struct cpuinfo_data {
2042 	intptr_t cid_cpu;
2043 	uintptr_t cid_lbolt;
2044 	uintptr_t **cid_ithr;
2045 	char	cid_print_head;
2046 	char	cid_print_thr;
2047 	char	cid_print_ithr;
2048 	char	cid_print_flags;
2049 } cpuinfo_data_t;
2050 
2051 int
2052 cpuinfo_walk_ithread(uintptr_t addr, const kthread_t *thr, cpuinfo_data_t *cid)
2053 {
2054 	cpu_t c;
2055 	int id;
2056 	uint8_t pil;
2057 
2058 	if (!(thr->t_flag & T_INTR_THREAD) || thr->t_state == TS_FREE)
2059 		return (WALK_NEXT);
2060 
2061 	if (thr->t_bound_cpu == NULL) {
2062 		mdb_warn("thr %p is intr thread w/out a CPU\n", addr);
2063 		return (WALK_NEXT);
2064 	}
2065 
2066 	(void) mdb_vread(&c, sizeof (c), (uintptr_t)thr->t_bound_cpu);
2067 
2068 	if ((id = c.cpu_id) >= NCPU) {
2069 		mdb_warn("CPU %p has id (%d) greater than NCPU (%d)\n",
2070 		    thr->t_bound_cpu, id, NCPU);
2071 		return (WALK_NEXT);
2072 	}
2073 
2074 	if ((pil = thr->t_pil) >= NINTR) {
2075 		mdb_warn("thread %p has pil (%d) greater than %d\n",
2076 		    addr, pil, NINTR);
2077 		return (WALK_NEXT);
2078 	}
2079 
2080 	if (cid->cid_ithr[id][pil] != NULL) {
2081 		mdb_warn("CPU %d has multiple threads at pil %d (at least "
2082 		    "%p and %p)\n", id, pil, addr, cid->cid_ithr[id][pil]);
2083 		return (WALK_NEXT);
2084 	}
2085 
2086 	cid->cid_ithr[id][pil] = addr;
2087 
2088 	return (WALK_NEXT);
2089 }
2090 
2091 #define	CPUINFO_IDWIDTH		3
2092 #define	CPUINFO_FLAGWIDTH	9
2093 
2094 #ifdef _LP64
2095 #if defined(__amd64)
2096 #define	CPUINFO_TWIDTH		16
2097 #define	CPUINFO_CPUWIDTH	16
2098 #else
2099 #define	CPUINFO_CPUWIDTH	11
2100 #define	CPUINFO_TWIDTH		11
2101 #endif
2102 #else
2103 #define	CPUINFO_CPUWIDTH	8
2104 #define	CPUINFO_TWIDTH		8
2105 #endif
2106 
2107 #define	CPUINFO_THRDELT		(CPUINFO_IDWIDTH + CPUINFO_CPUWIDTH + 9)
2108 #define	CPUINFO_FLAGDELT	(CPUINFO_IDWIDTH + CPUINFO_CPUWIDTH + 4)
2109 #define	CPUINFO_ITHRDELT	4
2110 
2111 #define	CPUINFO_INDENT	mdb_printf("%*s", CPUINFO_THRDELT, \
2112     flagline < nflaglines ? flagbuf[flagline++] : "")
2113 
2114 int
2115 cpuinfo_walk_cpu(uintptr_t addr, const cpu_t *cpu, cpuinfo_data_t *cid)
2116 {
2117 	kthread_t t;
2118 	disp_t disp;
2119 	proc_t p;
2120 	uintptr_t pinned;
2121 	char **flagbuf;
2122 	int nflaglines = 0, flagline = 0, bspl, rval = WALK_NEXT;
2123 
2124 	const char *flags[] = {
2125 	    "RUNNING", "READY", "QUIESCED", "EXISTS",
2126 	    "ENABLE", "OFFLINE", "POWEROFF", "FROZEN",
2127 	    "SPARE", "FAULTED", NULL
2128 	};
2129 
2130 	if (cid->cid_cpu != -1) {
2131 		if (addr != cid->cid_cpu && cpu->cpu_id != cid->cid_cpu)
2132 			return (WALK_NEXT);
2133 
2134 		/*
2135 		 * Set cid_cpu to -1 to indicate that we found a matching CPU.
2136 		 */
2137 		cid->cid_cpu = -1;
2138 		rval = WALK_DONE;
2139 	}
2140 
2141 	if (cid->cid_print_head) {
2142 		mdb_printf("%3s %-*s %3s %4s %4s %3s %4s %5s %-6s %-*s %s\n",
2143 		    "ID", CPUINFO_CPUWIDTH, "ADDR", "FLG", "NRUN", "BSPL",
2144 		    "PRI", "RNRN", "KRNRN", "SWITCH", CPUINFO_TWIDTH, "THREAD",
2145 		    "PROC");
2146 		cid->cid_print_head = FALSE;
2147 	}
2148 
2149 	bspl = cpu->cpu_base_spl;
2150 
2151 	if (mdb_vread(&disp, sizeof (disp_t), (uintptr_t)cpu->cpu_disp) == -1) {
2152 		mdb_warn("failed to read disp_t at %p", cpu->cpu_disp);
2153 		return (WALK_ERR);
2154 	}
2155 
2156 	mdb_printf("%3d %0*p %3x %4d %4d ",
2157 	    cpu->cpu_id, CPUINFO_CPUWIDTH, addr, cpu->cpu_flags,
2158 	    disp.disp_nrunnable, bspl);
2159 
2160 	if (mdb_vread(&t, sizeof (t), (uintptr_t)cpu->cpu_thread) != -1) {
2161 		mdb_printf("%3d ", t.t_pri);
2162 	} else {
2163 		mdb_printf("%3s ", "-");
2164 	}
2165 
2166 	mdb_printf("%4s %5s ", cpu->cpu_runrun ? "yes" : "no",
2167 	    cpu->cpu_kprunrun ? "yes" : "no");
2168 
2169 	if (cpu->cpu_last_swtch) {
2170 		clock_t lbolt;
2171 
2172 		if (mdb_vread(&lbolt, sizeof (lbolt), cid->cid_lbolt) == -1) {
2173 			mdb_warn("failed to read lbolt at %p", cid->cid_lbolt);
2174 			return (WALK_ERR);
2175 		}
2176 		mdb_printf("t-%-4d ", lbolt - cpu->cpu_last_swtch);
2177 	} else {
2178 		mdb_printf("%-6s ", "-");
2179 	}
2180 
2181 	mdb_printf("%0*p", CPUINFO_TWIDTH, cpu->cpu_thread);
2182 
2183 	if (cpu->cpu_thread == cpu->cpu_idle_thread)
2184 		mdb_printf(" (idle)\n");
2185 	else if (cpu->cpu_thread == NULL)
2186 		mdb_printf(" -\n");
2187 	else {
2188 		if (mdb_vread(&p, sizeof (p), (uintptr_t)t.t_procp) != -1) {
2189 			mdb_printf(" %s\n", p.p_user.u_comm);
2190 		} else {
2191 			mdb_printf(" ?\n");
2192 		}
2193 	}
2194 
2195 	flagbuf = mdb_zalloc(sizeof (flags), UM_SLEEP | UM_GC);
2196 
2197 	if (cid->cid_print_flags) {
2198 		int first = 1, i, j, k;
2199 		char *s;
2200 
2201 		cid->cid_print_head = TRUE;
2202 
2203 		for (i = 1, j = 0; flags[j] != NULL; i <<= 1, j++) {
2204 			if (!(cpu->cpu_flags & i))
2205 				continue;
2206 
2207 			if (first) {
2208 				s = mdb_alloc(CPUINFO_THRDELT + 1,
2209 				    UM_GC | UM_SLEEP);
2210 
2211 				(void) mdb_snprintf(s, CPUINFO_THRDELT + 1,
2212 				    "%*s|%*s", CPUINFO_FLAGDELT, "",
2213 				    CPUINFO_THRDELT - 1 - CPUINFO_FLAGDELT, "");
2214 				flagbuf[nflaglines++] = s;
2215 			}
2216 
2217 			s = mdb_alloc(CPUINFO_THRDELT + 1, UM_GC | UM_SLEEP);
2218 			(void) mdb_snprintf(s, CPUINFO_THRDELT + 1, "%*s%*s %s",
2219 			    CPUINFO_IDWIDTH + CPUINFO_CPUWIDTH -
2220 			    CPUINFO_FLAGWIDTH, "", CPUINFO_FLAGWIDTH, flags[j],
2221 			    first ? "<--+" : "");
2222 
2223 			for (k = strlen(s); k < CPUINFO_THRDELT; k++)
2224 				s[k] = ' ';
2225 			s[k] = '\0';
2226 
2227 			flagbuf[nflaglines++] = s;
2228 			first = 0;
2229 		}
2230 	}
2231 
2232 	if (cid->cid_print_ithr) {
2233 		int i, found_one = FALSE;
2234 		int print_thr = disp.disp_nrunnable && cid->cid_print_thr;
2235 
2236 		for (i = NINTR - 1; i >= 0; i--) {
2237 			uintptr_t iaddr = cid->cid_ithr[cpu->cpu_id][i];
2238 
2239 			if (iaddr == NULL)
2240 				continue;
2241 
2242 			if (!found_one) {
2243 				found_one = TRUE;
2244 
2245 				CPUINFO_INDENT;
2246 				mdb_printf("%c%*s|\n", print_thr ? '|' : ' ',
2247 				    CPUINFO_ITHRDELT, "");
2248 
2249 				CPUINFO_INDENT;
2250 				mdb_printf("%c%*s+--> %3s %s\n",
2251 				    print_thr ? '|' : ' ', CPUINFO_ITHRDELT,
2252 				    "", "PIL", "THREAD");
2253 			}
2254 
2255 			if (mdb_vread(&t, sizeof (t), iaddr) == -1) {
2256 				mdb_warn("failed to read kthread_t at %p",
2257 				    iaddr);
2258 				return (WALK_ERR);
2259 			}
2260 
2261 			CPUINFO_INDENT;
2262 			mdb_printf("%c%*s     %3d %0*p\n",
2263 			    print_thr ? '|' : ' ', CPUINFO_ITHRDELT, "",
2264 			    t.t_pil, CPUINFO_TWIDTH, iaddr);
2265 
2266 			pinned = (uintptr_t)t.t_intr;
2267 		}
2268 
2269 		if (found_one && pinned != NULL) {
2270 			cid->cid_print_head = TRUE;
2271 			(void) strcpy(p.p_user.u_comm, "?");
2272 
2273 			if (mdb_vread(&t, sizeof (t),
2274 			    (uintptr_t)pinned) == -1) {
2275 				mdb_warn("failed to read kthread_t at %p",
2276 				    pinned);
2277 				return (WALK_ERR);
2278 			}
2279 			if (mdb_vread(&p, sizeof (p),
2280 			    (uintptr_t)t.t_procp) == -1) {
2281 				mdb_warn("failed to read proc_t at %p",
2282 				    t.t_procp);
2283 				return (WALK_ERR);
2284 			}
2285 
2286 			CPUINFO_INDENT;
2287 			mdb_printf("%c%*s     %3s %0*p %s\n",
2288 			    print_thr ? '|' : ' ', CPUINFO_ITHRDELT, "", "-",
2289 			    CPUINFO_TWIDTH, pinned,
2290 			    pinned == (uintptr_t)cpu->cpu_idle_thread ?
2291 			    "(idle)" : p.p_user.u_comm);
2292 		}
2293 	}
2294 
2295 	if (disp.disp_nrunnable && cid->cid_print_thr) {
2296 		dispq_t *dq;
2297 
2298 		int i, npri = disp.disp_npri;
2299 
2300 		dq = mdb_alloc(sizeof (dispq_t) * npri, UM_SLEEP | UM_GC);
2301 
2302 		if (mdb_vread(dq, sizeof (dispq_t) * npri,
2303 		    (uintptr_t)disp.disp_q) == -1) {
2304 			mdb_warn("failed to read dispq_t at %p", disp.disp_q);
2305 			return (WALK_ERR);
2306 		}
2307 
2308 		CPUINFO_INDENT;
2309 		mdb_printf("|\n");
2310 
2311 		CPUINFO_INDENT;
2312 		mdb_printf("+-->  %3s %-*s %s\n", "PRI",
2313 		    CPUINFO_TWIDTH, "THREAD", "PROC");
2314 
2315 		for (i = npri - 1; i >= 0; i--) {
2316 			uintptr_t taddr = (uintptr_t)dq[i].dq_first;
2317 
2318 			while (taddr != NULL) {
2319 				if (mdb_vread(&t, sizeof (t), taddr) == -1) {
2320 					mdb_warn("failed to read kthread_t "
2321 					    "at %p", taddr);
2322 					return (WALK_ERR);
2323 				}
2324 				if (mdb_vread(&p, sizeof (p),
2325 				    (uintptr_t)t.t_procp) == -1) {
2326 					mdb_warn("failed to read proc_t at %p",
2327 					    t.t_procp);
2328 					return (WALK_ERR);
2329 				}
2330 
2331 				CPUINFO_INDENT;
2332 				mdb_printf("      %3d %0*p %s\n", t.t_pri,
2333 				    CPUINFO_TWIDTH, taddr, p.p_user.u_comm);
2334 
2335 				taddr = (uintptr_t)t.t_link;
2336 			}
2337 		}
2338 		cid->cid_print_head = TRUE;
2339 	}
2340 
2341 	while (flagline < nflaglines)
2342 		mdb_printf("%s\n", flagbuf[flagline++]);
2343 
2344 	if (cid->cid_print_head)
2345 		mdb_printf("\n");
2346 
2347 	return (rval);
2348 }
2349 
2350 int
2351 cpuinfo(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2352 {
2353 	uint_t verbose = FALSE;
2354 	cpuinfo_data_t cid;
2355 	GElf_Sym sym;
2356 	clock_t lbolt;
2357 
2358 	cid.cid_print_ithr = FALSE;
2359 	cid.cid_print_thr = FALSE;
2360 	cid.cid_print_flags = FALSE;
2361 	cid.cid_print_head = DCMD_HDRSPEC(flags) ? TRUE : FALSE;
2362 	cid.cid_cpu = -1;
2363 
2364 	if (flags & DCMD_ADDRSPEC)
2365 		cid.cid_cpu = addr;
2366 
2367 	if (mdb_getopts(argc, argv,
2368 	    'v', MDB_OPT_SETBITS, TRUE, &verbose, NULL) != argc)
2369 		return (DCMD_USAGE);
2370 
2371 	if (verbose) {
2372 		cid.cid_print_ithr = TRUE;
2373 		cid.cid_print_thr = TRUE;
2374 		cid.cid_print_flags = TRUE;
2375 		cid.cid_print_head = TRUE;
2376 	}
2377 
2378 	if (cid.cid_print_ithr) {
2379 		int i;
2380 
2381 		cid.cid_ithr = mdb_alloc(sizeof (uintptr_t **)
2382 		    * NCPU, UM_SLEEP | UM_GC);
2383 
2384 		for (i = 0; i < NCPU; i++)
2385 			cid.cid_ithr[i] = mdb_zalloc(sizeof (uintptr_t *) *
2386 			    NINTR, UM_SLEEP | UM_GC);
2387 
2388 		if (mdb_walk("thread", (mdb_walk_cb_t)cpuinfo_walk_ithread,
2389 		    &cid) == -1) {
2390 			mdb_warn("couldn't walk thread");
2391 			return (DCMD_ERR);
2392 		}
2393 	}
2394 
2395 	if (mdb_lookup_by_name("panic_lbolt", &sym) == -1) {
2396 		mdb_warn("failed to find panic_lbolt");
2397 		return (DCMD_ERR);
2398 	}
2399 
2400 	cid.cid_lbolt = (uintptr_t)sym.st_value;
2401 
2402 	if (mdb_vread(&lbolt, sizeof (lbolt), cid.cid_lbolt) == -1) {
2403 		mdb_warn("failed to read panic_lbolt");
2404 		return (DCMD_ERR);
2405 	}
2406 
2407 	if (lbolt == 0) {
2408 		if (mdb_lookup_by_name("lbolt", &sym) == -1) {
2409 			mdb_warn("failed to find lbolt");
2410 			return (DCMD_ERR);
2411 		}
2412 		cid.cid_lbolt = (uintptr_t)sym.st_value;
2413 	}
2414 
2415 	if (mdb_walk("cpu", (mdb_walk_cb_t)cpuinfo_walk_cpu, &cid) == -1) {
2416 		mdb_warn("can't walk cpus");
2417 		return (DCMD_ERR);
2418 	}
2419 
2420 	if (cid.cid_cpu != -1) {
2421 		/*
2422 		 * We didn't find this CPU when we walked through the CPUs
2423 		 * (i.e. the address specified doesn't show up in the "cpu"
2424 		 * walk).  However, the specified address may still correspond
2425 		 * to a valid cpu_t (for example, if the specified address is
2426 		 * the actual panicking cpu_t and not the cached panic_cpu).
2427 		 * Point is:  even if we didn't find it, we still want to try
2428 		 * to print the specified address as a cpu_t.
2429 		 */
2430 		cpu_t cpu;
2431 
2432 		if (mdb_vread(&cpu, sizeof (cpu), cid.cid_cpu) == -1) {
2433 			mdb_warn("%p is neither a valid CPU ID nor a "
2434 			    "valid cpu_t address\n", cid.cid_cpu);
2435 			return (DCMD_ERR);
2436 		}
2437 
2438 		(void) cpuinfo_walk_cpu(cid.cid_cpu, &cpu, &cid);
2439 	}
2440 
2441 	return (DCMD_OK);
2442 }
2443 
2444 /*ARGSUSED*/
2445 int
2446 flipone(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2447 {
2448 	int i;
2449 
2450 	if (!(flags & DCMD_ADDRSPEC))
2451 		return (DCMD_USAGE);
2452 
2453 	for (i = 0; i < sizeof (addr) * NBBY; i++)
2454 		mdb_printf("%p\n", addr ^ (1UL << i));
2455 
2456 	return (DCMD_OK);
2457 }
2458 
2459 /*
2460  * Grumble, grumble.
2461  */
2462 #define	SMAP_HASHFUNC(vp, off)	\
2463 	((((uintptr_t)(vp) >> 6) + ((uintptr_t)(vp) >> 3) + \
2464 	((off) >> MAXBSHIFT)) & smd_hashmsk)
2465 
2466 int
2467 vnode2smap(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2468 {
2469 	long smd_hashmsk;
2470 	int hash;
2471 	uintptr_t offset = 0;
2472 	struct smap smp;
2473 	uintptr_t saddr, kaddr;
2474 	uintptr_t smd_hash, smd_smap;
2475 	struct seg seg;
2476 
2477 	if (!(flags & DCMD_ADDRSPEC))
2478 		return (DCMD_USAGE);
2479 
2480 	if (mdb_readvar(&smd_hashmsk, "smd_hashmsk") == -1) {
2481 		mdb_warn("failed to read smd_hashmsk");
2482 		return (DCMD_ERR);
2483 	}
2484 
2485 	if (mdb_readvar(&smd_hash, "smd_hash") == -1) {
2486 		mdb_warn("failed to read smd_hash");
2487 		return (DCMD_ERR);
2488 	}
2489 
2490 	if (mdb_readvar(&smd_smap, "smd_smap") == -1) {
2491 		mdb_warn("failed to read smd_hash");
2492 		return (DCMD_ERR);
2493 	}
2494 
2495 	if (mdb_readvar(&kaddr, "segkmap") == -1) {
2496 		mdb_warn("failed to read segkmap");
2497 		return (DCMD_ERR);
2498 	}
2499 
2500 	if (mdb_vread(&seg, sizeof (seg), kaddr) == -1) {
2501 		mdb_warn("failed to read segkmap at %p", kaddr);
2502 		return (DCMD_ERR);
2503 	}
2504 
2505 	if (argc != 0) {
2506 		const mdb_arg_t *arg = &argv[0];
2507 
2508 		if (arg->a_type == MDB_TYPE_IMMEDIATE)
2509 			offset = arg->a_un.a_val;
2510 		else
2511 			offset = (uintptr_t)mdb_strtoull(arg->a_un.a_str);
2512 	}
2513 
2514 	hash = SMAP_HASHFUNC(addr, offset);
2515 
2516 	if (mdb_vread(&saddr, sizeof (saddr),
2517 	    smd_hash + hash * sizeof (uintptr_t)) == -1) {
2518 		mdb_warn("couldn't read smap at %p",
2519 		    smd_hash + hash * sizeof (uintptr_t));
2520 		return (DCMD_ERR);
2521 	}
2522 
2523 	do {
2524 		if (mdb_vread(&smp, sizeof (smp), saddr) == -1) {
2525 			mdb_warn("couldn't read smap at %p", saddr);
2526 			return (DCMD_ERR);
2527 		}
2528 
2529 		if ((uintptr_t)smp.sm_vp == addr && smp.sm_off == offset) {
2530 			mdb_printf("vnode %p, offs %p is smap %p, vaddr %p\n",
2531 			    addr, offset, saddr, ((saddr - smd_smap) /
2532 			    sizeof (smp)) * MAXBSIZE + seg.s_base);
2533 			return (DCMD_OK);
2534 		}
2535 
2536 		saddr = (uintptr_t)smp.sm_hash;
2537 	} while (saddr != NULL);
2538 
2539 	mdb_printf("no smap for vnode %p, offs %p\n", addr, offset);
2540 	return (DCMD_OK);
2541 }
2542 
2543 /*ARGSUSED*/
2544 int
2545 addr2smap(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2546 {
2547 	uintptr_t kaddr;
2548 	struct seg seg;
2549 	struct segmap_data sd;
2550 
2551 	if (!(flags & DCMD_ADDRSPEC))
2552 		return (DCMD_USAGE);
2553 
2554 	if (mdb_readvar(&kaddr, "segkmap") == -1) {
2555 		mdb_warn("failed to read segkmap");
2556 		return (DCMD_ERR);
2557 	}
2558 
2559 	if (mdb_vread(&seg, sizeof (seg), kaddr) == -1) {
2560 		mdb_warn("failed to read segkmap at %p", kaddr);
2561 		return (DCMD_ERR);
2562 	}
2563 
2564 	if (mdb_vread(&sd, sizeof (sd), (uintptr_t)seg.s_data) == -1) {
2565 		mdb_warn("failed to read segmap_data at %p", seg.s_data);
2566 		return (DCMD_ERR);
2567 	}
2568 
2569 	mdb_printf("%p is smap %p\n", addr,
2570 	    ((addr - (uintptr_t)seg.s_base) >> MAXBSHIFT) *
2571 	    sizeof (struct smap) + (uintptr_t)sd.smd_sm);
2572 
2573 	return (DCMD_OK);
2574 }
2575 
2576 int
2577 as2proc_walk(uintptr_t addr, const proc_t *p, struct as **asp)
2578 {
2579 	if (p->p_as == *asp)
2580 		mdb_printf("%p\n", addr);
2581 	return (WALK_NEXT);
2582 }
2583 
2584 /*ARGSUSED*/
2585 int
2586 as2proc(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2587 {
2588 	if (!(flags & DCMD_ADDRSPEC) || argc != 0)
2589 		return (DCMD_USAGE);
2590 
2591 	if (mdb_walk("proc", (mdb_walk_cb_t)as2proc_walk, &addr) == -1) {
2592 		mdb_warn("failed to walk proc");
2593 		return (DCMD_ERR);
2594 	}
2595 
2596 	return (DCMD_OK);
2597 }
2598 
2599 /*ARGSUSED*/
2600 int
2601 ptree_walk(uintptr_t addr, const proc_t *p, void *ignored)
2602 {
2603 	proc_t parent;
2604 	int ident = 0;
2605 	uintptr_t paddr;
2606 
2607 	for (paddr = (uintptr_t)p->p_parent; paddr != NULL; ident += 5) {
2608 		mdb_vread(&parent, sizeof (parent), paddr);
2609 		paddr = (uintptr_t)parent.p_parent;
2610 	}
2611 
2612 	mdb_inc_indent(ident);
2613 	mdb_printf("%0?p  %s\n", addr, p->p_user.u_comm);
2614 	mdb_dec_indent(ident);
2615 
2616 	return (WALK_NEXT);
2617 }
2618 
2619 void
2620 ptree_ancestors(uintptr_t addr, uintptr_t start)
2621 {
2622 	proc_t p;
2623 
2624 	if (mdb_vread(&p, sizeof (p), addr) == -1) {
2625 		mdb_warn("couldn't read ancestor at %p", addr);
2626 		return;
2627 	}
2628 
2629 	if (p.p_parent != NULL)
2630 		ptree_ancestors((uintptr_t)p.p_parent, start);
2631 
2632 	if (addr != start)
2633 		(void) ptree_walk(addr, &p, NULL);
2634 }
2635 
2636 /*ARGSUSED*/
2637 int
2638 ptree(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2639 {
2640 	if (!(flags & DCMD_ADDRSPEC))
2641 		addr = NULL;
2642 	else
2643 		ptree_ancestors(addr, addr);
2644 
2645 	if (mdb_pwalk("proc", (mdb_walk_cb_t)ptree_walk, NULL, addr) == -1) {
2646 		mdb_warn("couldn't walk 'proc'");
2647 		return (DCMD_ERR);
2648 	}
2649 
2650 	return (DCMD_OK);
2651 }
2652 
2653 /*ARGSUSED*/
2654 static int
2655 fd(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2656 {
2657 	int fdnum;
2658 	const mdb_arg_t *argp = &argv[0];
2659 	proc_t p;
2660 	uf_entry_t uf;
2661 
2662 	if ((flags & DCMD_ADDRSPEC) == 0) {
2663 		mdb_warn("fd doesn't give global information\n");
2664 		return (DCMD_ERR);
2665 	}
2666 	if (argc != 1)
2667 		return (DCMD_USAGE);
2668 
2669 	if (argp->a_type == MDB_TYPE_IMMEDIATE)
2670 		fdnum = argp->a_un.a_val;
2671 	else
2672 		fdnum = mdb_strtoull(argp->a_un.a_str);
2673 
2674 	if (mdb_vread(&p, sizeof (struct proc), addr) == -1) {
2675 		mdb_warn("couldn't read proc_t at %p", addr);
2676 		return (DCMD_ERR);
2677 	}
2678 	if (fdnum > p.p_user.u_finfo.fi_nfiles) {
2679 		mdb_warn("process %p only has %d files open.\n",
2680 		    addr, p.p_user.u_finfo.fi_nfiles);
2681 		return (DCMD_ERR);
2682 	}
2683 	if (mdb_vread(&uf, sizeof (uf_entry_t),
2684 	    (uintptr_t)&p.p_user.u_finfo.fi_list[fdnum]) == -1) {
2685 		mdb_warn("couldn't read uf_entry_t at %p",
2686 		    &p.p_user.u_finfo.fi_list[fdnum]);
2687 		return (DCMD_ERR);
2688 	}
2689 
2690 	mdb_printf("%p\n", uf.uf_file);
2691 	return (DCMD_OK);
2692 }
2693 
2694 /*ARGSUSED*/
2695 static int
2696 pid2proc(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2697 {
2698 	pid_t pid = (pid_t)addr;
2699 
2700 	if (argc != 0)
2701 		return (DCMD_USAGE);
2702 
2703 	if ((addr = mdb_pid2proc(pid, NULL)) == NULL) {
2704 		mdb_warn("PID 0t%d not found\n", pid);
2705 		return (DCMD_ERR);
2706 	}
2707 
2708 	mdb_printf("%p\n", addr);
2709 	return (DCMD_OK);
2710 }
2711 
2712 static char *sysfile_cmd[] = {
2713 	"exclude:",
2714 	"include:",
2715 	"forceload:",
2716 	"rootdev:",
2717 	"rootfs:",
2718 	"swapdev:",
2719 	"swapfs:",
2720 	"moddir:",
2721 	"set",
2722 	"unknown",
2723 };
2724 
2725 static char *sysfile_ops[] = { "", "=", "&", "|" };
2726 
2727 /*ARGSUSED*/
2728 static int
2729 sysfile_vmem_seg(uintptr_t addr, const vmem_seg_t *vsp, void **target)
2730 {
2731 	if (vsp->vs_type == VMEM_ALLOC && (void *)vsp->vs_start == *target) {
2732 		*target = NULL;
2733 		return (WALK_DONE);
2734 	}
2735 	return (WALK_NEXT);
2736 }
2737 
2738 /*ARGSUSED*/
2739 static int
2740 sysfile(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2741 {
2742 	struct sysparam *sysp, sys;
2743 	char var[256];
2744 	char modname[256];
2745 	char val[256];
2746 	char strval[256];
2747 	vmem_t *mod_sysfile_arena;
2748 	void *straddr;
2749 
2750 	if (mdb_readvar(&sysp, "sysparam_hd") == -1) {
2751 		mdb_warn("failed to read sysparam_hd");
2752 		return (DCMD_ERR);
2753 	}
2754 
2755 	if (mdb_readvar(&mod_sysfile_arena, "mod_sysfile_arena") == -1) {
2756 		mdb_warn("failed to read mod_sysfile_arena");
2757 		return (DCMD_ERR);
2758 	}
2759 
2760 	while (sysp != NULL) {
2761 		var[0] = '\0';
2762 		val[0] = '\0';
2763 		modname[0] = '\0';
2764 		if (mdb_vread(&sys, sizeof (sys), (uintptr_t)sysp) == -1) {
2765 			mdb_warn("couldn't read sysparam %p", sysp);
2766 			return (DCMD_ERR);
2767 		}
2768 		if (sys.sys_modnam != NULL &&
2769 		    mdb_readstr(modname, 256,
2770 		    (uintptr_t)sys.sys_modnam) == -1) {
2771 			mdb_warn("couldn't read modname in %p", sysp);
2772 			return (DCMD_ERR);
2773 		}
2774 		if (sys.sys_ptr != NULL &&
2775 		    mdb_readstr(var, 256, (uintptr_t)sys.sys_ptr) == -1) {
2776 			mdb_warn("couldn't read ptr in %p", sysp);
2777 			return (DCMD_ERR);
2778 		}
2779 		if (sys.sys_op != SETOP_NONE) {
2780 			/*
2781 			 * Is this an int or a string?  We determine this
2782 			 * by checking whether straddr is contained in
2783 			 * mod_sysfile_arena.  If so, the walker will set
2784 			 * straddr to NULL.
2785 			 */
2786 			straddr = (void *)(uintptr_t)sys.sys_info;
2787 			if (sys.sys_op == SETOP_ASSIGN &&
2788 			    sys.sys_info != 0 &&
2789 			    mdb_pwalk("vmem_seg",
2790 			    (mdb_walk_cb_t)sysfile_vmem_seg, &straddr,
2791 			    (uintptr_t)mod_sysfile_arena) == 0 &&
2792 			    straddr == NULL &&
2793 			    mdb_readstr(strval, 256,
2794 			    (uintptr_t)sys.sys_info) != -1) {
2795 				(void) mdb_snprintf(val, sizeof (val), "\"%s\"",
2796 				    strval);
2797 			} else {
2798 				(void) mdb_snprintf(val, sizeof (val),
2799 				    "0x%llx [0t%llu]", sys.sys_info,
2800 				    sys.sys_info);
2801 			}
2802 		}
2803 		mdb_printf("%s %s%s%s%s%s\n", sysfile_cmd[sys.sys_type],
2804 		    modname, modname[0] == '\0' ? "" : ":",
2805 		    var, sysfile_ops[sys.sys_op], val);
2806 
2807 		sysp = sys.sys_next;
2808 	}
2809 
2810 	return (DCMD_OK);
2811 }
2812 
2813 /*
2814  * Dump a taskq_ent_t given its address.
2815  */
2816 /*ARGSUSED*/
2817 int
2818 taskq_ent(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2819 {
2820 	taskq_ent_t	taskq_ent;
2821 	GElf_Sym	sym;
2822 	char		buf[MDB_SYM_NAMLEN+1];
2823 
2824 
2825 	if (!(flags & DCMD_ADDRSPEC)) {
2826 		mdb_warn("expected explicit taskq_ent_t address before ::\n");
2827 		return (DCMD_USAGE);
2828 	}
2829 
2830 	if (mdb_vread(&taskq_ent, sizeof (taskq_ent_t), addr) == -1) {
2831 		mdb_warn("failed to read taskq_ent_t at %p", addr);
2832 		return (DCMD_ERR);
2833 	}
2834 
2835 	if (DCMD_HDRSPEC(flags)) {
2836 		mdb_printf("%<u>%-?s    %-?s    %-s%</u>\n",
2837 		"ENTRY", "ARG", "FUNCTION");
2838 	}
2839 
2840 	if (mdb_lookup_by_addr((uintptr_t)taskq_ent.tqent_func, MDB_SYM_EXACT,
2841 	    buf, sizeof (buf), &sym) == -1) {
2842 		(void) strcpy(buf, "????");
2843 	}
2844 
2845 	mdb_printf("%-?p    %-?p    %s\n", addr, taskq_ent.tqent_arg, buf);
2846 
2847 	return (DCMD_OK);
2848 }
2849 
2850 /*
2851  * Given the address of the (taskq_t) task queue head, walk the queue listing
2852  * the address of every taskq_ent_t.
2853  */
2854 int
2855 taskq_walk_init(mdb_walk_state_t *wsp)
2856 {
2857 	taskq_t	tq_head;
2858 
2859 
2860 	if (wsp->walk_addr == NULL) {
2861 		mdb_warn("start address required\n");
2862 		return (WALK_ERR);
2863 	}
2864 
2865 
2866 	/*
2867 	 * Save the address of the list head entry.  This terminates the list.
2868 	 */
2869 	wsp->walk_data = (void *)
2870 	    ((size_t)wsp->walk_addr + offsetof(taskq_t, tq_task));
2871 
2872 
2873 	/*
2874 	 * Read in taskq head, set walk_addr to point to first taskq_ent_t.
2875 	 */
2876 	if (mdb_vread((void *)&tq_head, sizeof (taskq_t), wsp->walk_addr) ==
2877 	    -1) {
2878 		mdb_warn("failed to read taskq list head at %p",
2879 		    wsp->walk_addr);
2880 	}
2881 	wsp->walk_addr = (uintptr_t)tq_head.tq_task.tqent_next;
2882 
2883 
2884 	/*
2885 	 * Check for null list (next=head)
2886 	 */
2887 	if (wsp->walk_addr == (uintptr_t)wsp->walk_data) {
2888 		return (WALK_DONE);
2889 	}
2890 
2891 	return (WALK_NEXT);
2892 }
2893 
2894 
2895 int
2896 taskq_walk_step(mdb_walk_state_t *wsp)
2897 {
2898 	taskq_ent_t	tq_ent;
2899 	int		status;
2900 
2901 
2902 	if (mdb_vread((void *)&tq_ent, sizeof (taskq_ent_t), wsp->walk_addr) ==
2903 	    -1) {
2904 		mdb_warn("failed to read taskq_ent_t at %p", wsp->walk_addr);
2905 		return (DCMD_ERR);
2906 	}
2907 
2908 	status = wsp->walk_callback(wsp->walk_addr, (void *)&tq_ent,
2909 	    wsp->walk_cbdata);
2910 
2911 	wsp->walk_addr = (uintptr_t)tq_ent.tqent_next;
2912 
2913 
2914 	/* Check if we're at the last element (next=head) */
2915 	if (wsp->walk_addr == (uintptr_t)wsp->walk_data) {
2916 		return (WALK_DONE);
2917 	}
2918 
2919 	return (status);
2920 }
2921 
2922 int
2923 didmatch(uintptr_t addr, const kthread_t *thr, kt_did_t *didp)
2924 {
2925 
2926 	if (*didp == thr->t_did) {
2927 		mdb_printf("%p\n", addr);
2928 		return (WALK_DONE);
2929 	} else
2930 		return (WALK_NEXT);
2931 }
2932 
2933 /*ARGSUSED*/
2934 int
2935 did2thread(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2936 {
2937 	const mdb_arg_t *argp = &argv[0];
2938 	kt_did_t	did;
2939 
2940 	if (argc != 1)
2941 		return (DCMD_USAGE);
2942 
2943 	did = (kt_did_t)mdb_strtoull(argp->a_un.a_str);
2944 
2945 	if (mdb_walk("thread", (mdb_walk_cb_t)didmatch, (void *)&did) == -1) {
2946 		mdb_warn("failed to walk thread");
2947 		return (DCMD_ERR);
2948 
2949 	}
2950 	return (DCMD_OK);
2951 
2952 }
2953 
2954 static int
2955 errorq_walk_init(mdb_walk_state_t *wsp)
2956 {
2957 	if (wsp->walk_addr == NULL &&
2958 	    mdb_readvar(&wsp->walk_addr, "errorq_list") == -1) {
2959 		mdb_warn("failed to read errorq_list");
2960 		return (WALK_ERR);
2961 	}
2962 
2963 	return (WALK_NEXT);
2964 }
2965 
2966 static int
2967 errorq_walk_step(mdb_walk_state_t *wsp)
2968 {
2969 	uintptr_t addr = wsp->walk_addr;
2970 	errorq_t eq;
2971 
2972 	if (addr == NULL)
2973 		return (WALK_DONE);
2974 
2975 	if (mdb_vread(&eq, sizeof (eq), addr) == -1) {
2976 		mdb_warn("failed to read errorq at %p", addr);
2977 		return (WALK_ERR);
2978 	}
2979 
2980 	wsp->walk_addr = (uintptr_t)eq.eq_next;
2981 	return (wsp->walk_callback(addr, &eq, wsp->walk_cbdata));
2982 }
2983 
2984 typedef struct eqd_walk_data {
2985 	uintptr_t *eqd_stack;
2986 	void *eqd_buf;
2987 	ulong_t eqd_qpos;
2988 	ulong_t eqd_qlen;
2989 	size_t eqd_size;
2990 } eqd_walk_data_t;
2991 
2992 /*
2993  * In order to walk the list of pending error queue elements, we push the
2994  * addresses of the corresponding data buffers in to the eqd_stack array.
2995  * The error lists are in reverse chronological order when iterating using
2996  * eqe_prev, so we then pop things off the top in eqd_walk_step so that the
2997  * walker client gets addresses in order from oldest error to newest error.
2998  */
2999 static void
3000 eqd_push_list(eqd_walk_data_t *eqdp, uintptr_t addr)
3001 {
3002 	errorq_elem_t eqe;
3003 
3004 	while (addr != NULL) {
3005 		if (mdb_vread(&eqe, sizeof (eqe), addr) != sizeof (eqe)) {
3006 			mdb_warn("failed to read errorq element at %p", addr);
3007 			break;
3008 		}
3009 
3010 		if (eqdp->eqd_qpos == eqdp->eqd_qlen) {
3011 			mdb_warn("errorq is overfull -- more than %lu "
3012 			    "elems found\n", eqdp->eqd_qlen);
3013 			break;
3014 		}
3015 
3016 		eqdp->eqd_stack[eqdp->eqd_qpos++] = (uintptr_t)eqe.eqe_data;
3017 		addr = (uintptr_t)eqe.eqe_prev;
3018 	}
3019 }
3020 
3021 static int
3022 eqd_walk_init(mdb_walk_state_t *wsp)
3023 {
3024 	eqd_walk_data_t *eqdp;
3025 	errorq_elem_t eqe, *addr;
3026 	errorq_t eq;
3027 	ulong_t i;
3028 
3029 	if (mdb_vread(&eq, sizeof (eq), wsp->walk_addr) == -1) {
3030 		mdb_warn("failed to read errorq at %p", wsp->walk_addr);
3031 		return (WALK_ERR);
3032 	}
3033 
3034 	if (eq.eq_ptail != NULL &&
3035 	    mdb_vread(&eqe, sizeof (eqe), (uintptr_t)eq.eq_ptail) == -1) {
3036 		mdb_warn("failed to read errorq element at %p", eq.eq_ptail);
3037 		return (WALK_ERR);
3038 	}
3039 
3040 	eqdp = mdb_alloc(sizeof (eqd_walk_data_t), UM_SLEEP);
3041 	wsp->walk_data = eqdp;
3042 
3043 	eqdp->eqd_stack = mdb_zalloc(sizeof (uintptr_t) * eq.eq_qlen, UM_SLEEP);
3044 	eqdp->eqd_buf = mdb_alloc(eq.eq_size, UM_SLEEP);
3045 	eqdp->eqd_qlen = eq.eq_qlen;
3046 	eqdp->eqd_qpos = 0;
3047 	eqdp->eqd_size = eq.eq_size;
3048 
3049 	/*
3050 	 * The newest elements in the queue are on the pending list, so we
3051 	 * push those on to our stack first.
3052 	 */
3053 	eqd_push_list(eqdp, (uintptr_t)eq.eq_pend);
3054 
3055 	/*
3056 	 * If eq_ptail is set, it may point to a subset of the errors on the
3057 	 * pending list in the event a casptr() failed; if ptail's data is
3058 	 * already in our stack, NULL out eq_ptail and ignore it.
3059 	 */
3060 	if (eq.eq_ptail != NULL) {
3061 		for (i = 0; i < eqdp->eqd_qpos; i++) {
3062 			if (eqdp->eqd_stack[i] == (uintptr_t)eqe.eqe_data) {
3063 				eq.eq_ptail = NULL;
3064 				break;
3065 			}
3066 		}
3067 	}
3068 
3069 	/*
3070 	 * If eq_phead is set, it has the processing list in order from oldest
3071 	 * to newest.  Use this to recompute eq_ptail as best we can and then
3072 	 * we nicely fall into eqd_push_list() of eq_ptail below.
3073 	 */
3074 	for (addr = eq.eq_phead; addr != NULL && mdb_vread(&eqe, sizeof (eqe),
3075 	    (uintptr_t)addr) == sizeof (eqe); addr = eqe.eqe_next)
3076 		eq.eq_ptail = addr;
3077 
3078 	/*
3079 	 * The oldest elements in the queue are on the processing list, subject
3080 	 * to machinations in the if-clauses above.  Push any such elements.
3081 	 */
3082 	eqd_push_list(eqdp, (uintptr_t)eq.eq_ptail);
3083 	return (WALK_NEXT);
3084 }
3085 
3086 static int
3087 eqd_walk_step(mdb_walk_state_t *wsp)
3088 {
3089 	eqd_walk_data_t *eqdp = wsp->walk_data;
3090 	uintptr_t addr;
3091 
3092 	if (eqdp->eqd_qpos == 0)
3093 		return (WALK_DONE);
3094 
3095 	addr = eqdp->eqd_stack[--eqdp->eqd_qpos];
3096 
3097 	if (mdb_vread(eqdp->eqd_buf, eqdp->eqd_size, addr) != eqdp->eqd_size) {
3098 		mdb_warn("failed to read errorq data at %p", addr);
3099 		return (WALK_ERR);
3100 	}
3101 
3102 	return (wsp->walk_callback(addr, eqdp->eqd_buf, wsp->walk_cbdata));
3103 }
3104 
3105 static void
3106 eqd_walk_fini(mdb_walk_state_t *wsp)
3107 {
3108 	eqd_walk_data_t *eqdp = wsp->walk_data;
3109 
3110 	mdb_free(eqdp->eqd_stack, sizeof (uintptr_t) * eqdp->eqd_qlen);
3111 	mdb_free(eqdp->eqd_buf, eqdp->eqd_size);
3112 	mdb_free(eqdp, sizeof (eqd_walk_data_t));
3113 }
3114 
3115 #define	EQKSVAL(eqv, what) (eqv.eq_kstat.what.value.ui64)
3116 
3117 static int
3118 errorq(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
3119 {
3120 	int i;
3121 	errorq_t eq;
3122 	uint_t opt_v = FALSE;
3123 
3124 	if (!(flags & DCMD_ADDRSPEC)) {
3125 		if (mdb_walk_dcmd("errorq", "errorq", argc, argv) == -1) {
3126 			mdb_warn("can't walk 'errorq'");
3127 			return (DCMD_ERR);
3128 		}
3129 		return (DCMD_OK);
3130 	}
3131 
3132 	i = mdb_getopts(argc, argv, 'v', MDB_OPT_SETBITS, TRUE, &opt_v, NULL);
3133 	argc -= i;
3134 	argv += i;
3135 
3136 	if (argc != 0)
3137 		return (DCMD_USAGE);
3138 
3139 	if (opt_v || DCMD_HDRSPEC(flags)) {
3140 		mdb_printf("%<u>%-11s %-16s %1s %1s %1s ",
3141 		    "ADDR", "NAME", "S", "V", "N");
3142 		if (!opt_v) {
3143 			mdb_printf("%7s %7s %7s%</u>\n",
3144 			    "ACCEPT", "DROP", "LOG");
3145 		} else {
3146 			mdb_printf("%5s %6s %6s %3s %16s%</u>\n",
3147 			    "KSTAT", "QLEN", "SIZE", "IPL", "FUNC");
3148 		}
3149 	}
3150 
3151 	if (mdb_vread(&eq, sizeof (eq), addr) != sizeof (eq)) {
3152 		mdb_warn("failed to read errorq at %p", addr);
3153 		return (DCMD_ERR);
3154 	}
3155 
3156 	mdb_printf("%-11p %-16s %c %c %c ", addr, eq.eq_name,
3157 	    (eq.eq_flags & ERRORQ_ACTIVE) ? '+' : '-',
3158 	    (eq.eq_flags & ERRORQ_VITAL) ? '!' : ' ',
3159 	    (eq.eq_flags & ERRORQ_NVLIST) ? '*' : ' ');
3160 
3161 	if (!opt_v) {
3162 		mdb_printf("%7llu %7llu %7llu\n",
3163 		    EQKSVAL(eq, eqk_dispatched) + EQKSVAL(eq, eqk_committed),
3164 		    EQKSVAL(eq, eqk_dropped) + EQKSVAL(eq, eqk_reserve_fail) +
3165 		    EQKSVAL(eq, eqk_commit_fail), EQKSVAL(eq, eqk_logged));
3166 	} else {
3167 		mdb_printf("%5s %6lu %6lu %3u %a\n",
3168 		    "  |  ", eq.eq_qlen, eq.eq_size, eq.eq_ipl, eq.eq_func);
3169 		mdb_printf("%38s\n%41s"
3170 		    "%12s %llu\n"
3171 		    "%53s %llu\n"
3172 		    "%53s %llu\n"
3173 		    "%53s %llu\n"
3174 		    "%53s %llu\n"
3175 		    "%53s %llu\n"
3176 		    "%53s %llu\n"
3177 		    "%53s %llu\n\n",
3178 		    "|", "+-> ",
3179 		    "DISPATCHED",	EQKSVAL(eq, eqk_dispatched),
3180 		    "DROPPED",		EQKSVAL(eq, eqk_dropped),
3181 		    "LOGGED",		EQKSVAL(eq, eqk_logged),
3182 		    "RESERVED",		EQKSVAL(eq, eqk_reserved),
3183 		    "RESERVE FAIL",	EQKSVAL(eq, eqk_reserve_fail),
3184 		    "COMMITTED",	EQKSVAL(eq, eqk_committed),
3185 		    "COMMIT FAIL",	EQKSVAL(eq, eqk_commit_fail),
3186 		    "CANCELLED",	EQKSVAL(eq, eqk_cancelled));
3187 	}
3188 
3189 	return (DCMD_OK);
3190 }
3191 
3192 /*ARGSUSED*/
3193 static int
3194 panicinfo(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
3195 {
3196 	cpu_t panic_cpu;
3197 	kthread_t *panic_thread;
3198 	void *buf;
3199 	panic_data_t *pd;
3200 	int i, n;
3201 
3202 	if (!mdb_prop_postmortem) {
3203 		mdb_warn("panicinfo can only be run on a system "
3204 		    "dump; see dumpadm(1M)\n");
3205 		return (DCMD_ERR);
3206 	}
3207 
3208 	if (flags & DCMD_ADDRSPEC || argc != 0)
3209 		return (DCMD_USAGE);
3210 
3211 	if (mdb_readsym(&panic_cpu, sizeof (cpu_t), "panic_cpu") == -1)
3212 		mdb_warn("failed to read 'panic_cpu'");
3213 	else
3214 		mdb_printf("%16s %?d\n", "cpu", panic_cpu.cpu_id);
3215 
3216 	if (mdb_readvar(&panic_thread, "panic_thread") == -1)
3217 		mdb_warn("failed to read 'panic_thread'");
3218 	else
3219 		mdb_printf("%16s %?p\n", "thread", panic_thread);
3220 
3221 	buf = mdb_alloc(PANICBUFSIZE, UM_SLEEP);
3222 	pd = (panic_data_t *)buf;
3223 
3224 	if (mdb_readsym(buf, PANICBUFSIZE, "panicbuf") == -1 ||
3225 	    pd->pd_version != PANICBUFVERS) {
3226 		mdb_warn("failed to read 'panicbuf'");
3227 		mdb_free(buf, PANICBUFSIZE);
3228 		return (DCMD_ERR);
3229 	}
3230 
3231 	mdb_printf("%16s %s\n", "message",  (char *)buf + pd->pd_msgoff);
3232 
3233 	n = (pd->pd_msgoff - (sizeof (panic_data_t) -
3234 	    sizeof (panic_nv_t))) / sizeof (panic_nv_t);
3235 
3236 	for (i = 0; i < n; i++)
3237 		mdb_printf("%16s %?llx\n",
3238 		    pd->pd_nvdata[i].pnv_name, pd->pd_nvdata[i].pnv_value);
3239 
3240 	mdb_free(buf, PANICBUFSIZE);
3241 	return (DCMD_OK);
3242 }
3243 
3244 static const mdb_dcmd_t dcmds[] = {
3245 
3246 	/* from genunix.c */
3247 	{ "addr2smap", ":[offset]", "translate address to smap", addr2smap },
3248 	{ "as2proc", ":", "convert as to proc_t address", as2proc },
3249 	{ "binding_hash_entry", ":", "print driver names hash table entry",
3250 		binding_hash_entry },
3251 	{ "callout", NULL, "print callout table", callout },
3252 	{ "class", NULL, "print process scheduler classes", class },
3253 	{ "cpuinfo", "?[-v]", "print CPUs and runnable threads", cpuinfo },
3254 	{ "did2thread", "? kt_did", "find kernel thread for this id",
3255 		did2thread },
3256 	{ "errorq", "?[-v]", "display kernel error queues", errorq },
3257 	{ "fd", ":[fd num]", "get a file pointer from an fd", fd },
3258 	{ "flipone", ":", "the vik_rev_level 2 special", flipone },
3259 	{ "lminfo", NULL, "print lock manager information", lminfo },
3260 	{ "ndi_event_hdl", "?", "print ndi_event_hdl", ndi_event_hdl },
3261 	{ "panicinfo", NULL, "print panic information", panicinfo },
3262 	{ "pid2proc", "?", "convert PID to proc_t address", pid2proc },
3263 	{ "pmap", ":[-q]", "print process memory map", pmap },
3264 	{ "project", NULL, "display kernel project(s)", project },
3265 	{ "ps", "[-fltzTP]", "list processes (and associated thr,lwp)", ps },
3266 	{ "pgrep", "[-x] [-n | -o] pattern",
3267 		"pattern match against all processes", pgrep },
3268 	{ "ptree", NULL, "print process tree", ptree },
3269 	{ "seg", ":", "print address space segment", seg },
3270 	{ "sysevent", "?[-sv]", "print sysevent pending or sent queue",
3271 		sysevent},
3272 	{ "sysevent_channel", "?", "print sysevent channel database",
3273 		sysevent_channel},
3274 	{ "sysevent_class_list", ":", "print sysevent class list",
3275 		sysevent_class_list},
3276 	{ "sysevent_subclass_list", ":",
3277 		"print sysevent subclass list", sysevent_subclass_list},
3278 	{ "system", NULL, "print contents of /etc/system file", sysfile },
3279 	{ "task", NULL, "display kernel task(s)", task },
3280 	{ "taskq_entry", ":", "display a taskq_ent_t", taskq_ent },
3281 	{ "vnode2path", ":[-F]", "vnode address to pathname", vnode2path },
3282 	{ "vnode2smap", ":[offset]", "translate vnode to smap", vnode2smap },
3283 	{ "whereopen", ":", "given a vnode, dumps procs which have it open",
3284 	    whereopen },
3285 
3286 	/* from zone.c */
3287 	{ "zone", "?", "display kernel zone(s)", zoneprt },
3288 	{ "zsd", ":[zsd key]", "lookup zsd value from a key", zsd },
3289 
3290 	/* from bio.c */
3291 	{ "bufpagefind", ":addr", "find page_t on buf_t list", bufpagefind },
3292 
3293 	/* from contract.c */
3294 	{ "contract", "?", "display a contract", cmd_contract },
3295 	{ "ctevent", ":", "display a contract event", cmd_ctevent },
3296 	{ "ctid", ":", "convert id to a contract pointer", cmd_ctid },
3297 
3298 	/* from cpupart.c */
3299 	{ "cpupart", "?[-v]", "print cpu partition info", cpupart },
3300 
3301 	/* from cyclic.c */
3302 	{ "cyccover", NULL, "dump cyclic coverage information", cyccover },
3303 	{ "cycid", "?", "dump a cyclic id", cycid },
3304 	{ "cycinfo", "?", "dump cyc_cpu info", cycinfo },
3305 	{ "cyclic", ":", "developer information", cyclic },
3306 	{ "cyctrace", "?", "dump cyclic trace buffer", cyctrace },
3307 
3308 	/* from devinfo.c */
3309 	{ "devbindings", "?[-qs] [device-name | major-num]",
3310 	    "print devinfo nodes bound to device-name or major-num",
3311 	    devbindings, devinfo_help },
3312 	{ "devinfo", ":[-qs]", "detailed devinfo of one node", devinfo,
3313 	    devinfo_help },
3314 	{ "devinfo_audit", ":[-v]", "devinfo configuration audit record",
3315 	    devinfo_audit },
3316 	{ "devinfo_audit_log", "?[-v]", "system wide devinfo configuration log",
3317 	    devinfo_audit_log },
3318 	{ "devinfo_audit_node", ":[-v]", "devinfo node configuration history",
3319 	    devinfo_audit_node },
3320 	{ "devinfo2driver", ":", "find driver name for this devinfo node",
3321 	    devinfo2driver },
3322 	{ "devnames", "?[-vm] [num]", "print devnames array", devnames },
3323 	{ "dev2major", "?<dev_t>", "convert dev_t to a major number",
3324 	    dev2major },
3325 	{ "dev2minor", "?<dev_t>", "convert dev_t to a minor number",
3326 	    dev2minor },
3327 	{ "devt", "?<dev_t>", "display a dev_t's major and minor numbers",
3328 	    devt },
3329 	{ "major2name", "?<major-num>", "convert major number to dev name",
3330 	    major2name },
3331 	{ "minornodes", ":", "given a devinfo node, print its minor nodes",
3332 	    minornodes },
3333 	{ "modctl2devinfo", ":", "given a modctl, list its devinfos",
3334 	    modctl2devinfo },
3335 	{ "name2major", "<dev-name>", "convert dev name to major number",
3336 	    name2major },
3337 	{ "prtconf", "?[-vpc]", "print devinfo tree", prtconf, prtconf_help },
3338 	{ "softstate", ":<instance>", "retrieve soft-state pointer",
3339 	    softstate },
3340 	{ "devinfo_fm", ":", "devinfo fault managment configuration",
3341 	    devinfo_fm },
3342 	{ "devinfo_fmce", ":", "devinfo fault managment cache entry",
3343 	    devinfo_fmce},
3344 
3345 	/* from fm.c */
3346 	{ "ereport", "[-v]", "print ereports logged in dump",
3347 	    ereport },
3348 
3349 	/* from findstack.c */
3350 	{ "findstack", ":[-v]", "find kernel thread stack", findstack },
3351 	{ "findstack_debug", NULL, "toggle findstack debugging",
3352 		findstack_debug },
3353 
3354 	/* from kgrep.c + genunix.c */
3355 	{ "kgrep", KGREP_USAGE, "search kernel as for a pointer", kgrep,
3356 		kgrep_help },
3357 
3358 	/* from kmem.c */
3359 	{ "allocdby", ":", "given a thread, print its allocated buffers",
3360 		allocdby },
3361 	{ "bufctl", ":[-vh] [-a addr] [-c caller] [-e earliest] [-l latest] "
3362 		"[-t thd]", "print or filter a bufctl", bufctl, bufctl_help },
3363 	{ "freedby", ":", "given a thread, print its freed buffers", freedby },
3364 	{ "kmalog", "?[ fail | slab ]",
3365 	    "display kmem transaction log and stack traces", kmalog },
3366 	{ "kmastat", "[-kmg]", "kernel memory allocator stats",
3367 	    kmastat },
3368 	{ "kmausers", "?[-ef] [cache ...]", "current medium and large users "
3369 		"of the kmem allocator", kmausers, kmausers_help },
3370 	{ "kmem_cache", "?", "print kernel memory caches", kmem_cache },
3371 	{ "kmem_slabs", "?[-v] [-n cache] [-b maxbins] [-B minbinsize]",
3372 		"display slab usage per kmem cache",
3373 		kmem_slabs, kmem_slabs_help },
3374 	{ "kmem_debug", NULL, "toggle kmem dcmd/walk debugging", kmem_debug },
3375 	{ "kmem_log", "?[-b]", "dump kmem transaction log", kmem_log },
3376 	{ "kmem_verify", "?", "check integrity of kmem-managed memory",
3377 		kmem_verify },
3378 	{ "vmem", "?", "print a vmem_t", vmem },
3379 	{ "vmem_seg", ":[-sv] [-c caller] [-e earliest] [-l latest] "
3380 		"[-m minsize] [-M maxsize] [-t thread] [-T type]",
3381 		"print or filter a vmem_seg", vmem_seg, vmem_seg_help },
3382 	{ "whatis", ":[-abiv]", "given an address, return information", whatis,
3383 		whatis_help },
3384 	{ "whatthread", ":[-v]", "print threads whose stack contains the "
3385 		"given address", whatthread },
3386 
3387 	/* from ldi.c */
3388 	{ "ldi_handle", "?[-i]", "display a layered driver handle",
3389 	    ldi_handle, ldi_handle_help },
3390 	{ "ldi_ident", NULL, "display a layered driver identifier",
3391 	    ldi_ident, ldi_ident_help },
3392 
3393 	/* from leaky.c + leaky_subr.c */
3394 	{ "findleaks", FINDLEAKS_USAGE,
3395 	    "search for potential kernel memory leaks", findleaks,
3396 	    findleaks_help },
3397 
3398 	/* from lgrp.c */
3399 	{ "lgrp", "?[-q] [-p | -Pih]", "display an lgrp", lgrp},
3400 	{ "lgrp_set", "", "display bitmask of lgroups as a list", lgrp_set},
3401 
3402 	/* from log.c */
3403 	{ "msgbuf", "?[-v]", "print most recent console messages", msgbuf },
3404 
3405 	/* from memory.c */
3406 	{ "page", "?", "display a summarized page_t", page },
3407 	{ "memstat", NULL, "display memory usage summary", memstat },
3408 	{ "memlist", "?[-iav]", "display a struct memlist", memlist },
3409 	{ "swapinfo", "?", "display a struct swapinfo", swapinfof },
3410 
3411 	/* from mmd.c */
3412 	{ "multidata", ":[-sv]", "display a summarized multidata_t",
3413 		multidata },
3414 	{ "pattbl", ":", "display a summarized multidata attribute table",
3415 		pattbl },
3416 	{ "pattr2multidata", ":", "print multidata pointer from pattr_t",
3417 		pattr2multidata },
3418 	{ "pdesc2slab", ":", "print pdesc slab pointer from pdesc_t",
3419 		pdesc2slab },
3420 	{ "pdesc_verify", ":", "verify integrity of a pdesc_t", pdesc_verify },
3421 	{ "slab2multidata", ":", "print multidata pointer from pdesc_slab_t",
3422 		slab2multidata },
3423 
3424 	/* from modhash.c */
3425 	{ "modhash", "?[-ceht] [-k key] [-v val] [-i index]",
3426 		"display information about one or all mod_hash structures",
3427 		modhash, modhash_help },
3428 	{ "modent", ":[-k | -v | -t type]",
3429 		"display information about a mod_hash_entry", modent,
3430 		modent_help },
3431 
3432 	/* from net.c */
3433 	{ "mi", ":[-p] [-d | -m]", "filter and display MI object or payload",
3434 		mi },
3435 	{ "netstat", "[-arv] [-f inet | inet6 | unix] [-P tcp | udp]",
3436 		"show network statistics", netstat },
3437 	{ "sonode", "?[-f inet | inet6 | unix | #] "
3438 		"[-t stream | dgram | raw | #] [-p #]",
3439 		"filter and display sonode", sonode },
3440 
3441 	/* from netstack.c */
3442 	{ "netstack", "", "show stack instances", netstack },
3443 
3444 	/* from nvpair.c */
3445 	{ NVPAIR_DCMD_NAME, NVPAIR_DCMD_USAGE, NVPAIR_DCMD_DESCR,
3446 		nvpair_print },
3447 	{ NVLIST_DCMD_NAME, NVLIST_DCMD_USAGE, NVLIST_DCMD_DESCR,
3448 		print_nvlist },
3449 
3450 	/* from pg.c */
3451 	{ "pg", "?[-q]", "display a pg", pg},
3452 	/* from group.c */
3453 	{ "group", "?[-q]", "display a group", group},
3454 
3455 	/* from log.c */
3456 	/* from rctl.c */
3457 	{ "rctl_dict", "?", "print systemwide default rctl definitions",
3458 		rctl_dict },
3459 	{ "rctl_list", ":[handle]", "print rctls for the given proc",
3460 		rctl_list },
3461 	{ "rctl", ":[handle]", "print a rctl_t, only if it matches the handle",
3462 		rctl },
3463 	{ "rctl_validate", ":[-v] [-n #]", "test resource control value "
3464 		"sequence", rctl_validate },
3465 
3466 	/* from sobj.c */
3467 	{ "rwlock", ":", "dump out a readers/writer lock", rwlock },
3468 	{ "mutex", ":[-f]", "dump out an adaptive or spin mutex", mutex,
3469 		mutex_help },
3470 	{ "sobj2ts", ":", "perform turnstile lookup on synch object", sobj2ts },
3471 	{ "wchaninfo", "?[-v]", "dump condition variable", wchaninfo },
3472 	{ "turnstile", "?", "display a turnstile", turnstile },
3473 
3474 	/* from stream.c */
3475 	{ "mblk", ":[-q|v] [-f|F flag] [-t|T type] [-l|L|B len] [-d dbaddr]",
3476 		"print an mblk", mblk_prt, mblk_help },
3477 	{ "mblk_verify", "?", "verify integrity of an mblk", mblk_verify },
3478 	{ "mblk2dblk", ":", "convert mblk_t address to dblk_t address",
3479 		mblk2dblk },
3480 	{ "q2otherq", ":", "print peer queue for a given queue", q2otherq },
3481 	{ "q2rdq", ":", "print read queue for a given queue", q2rdq },
3482 	{ "q2syncq", ":", "print syncq for a given queue", q2syncq },
3483 	{ "q2stream", ":", "print stream pointer for a given queue", q2stream },
3484 	{ "q2wrq", ":", "print write queue for a given queue", q2wrq },
3485 	{ "queue", ":[-q|v] [-m mod] [-f flag] [-F flag] [-s syncq_addr]",
3486 		"filter and display STREAM queue", queue, queue_help },
3487 	{ "stdata", ":[-q|v] [-f flag] [-F flag]",
3488 		"filter and display STREAM head", stdata, stdata_help },
3489 	{ "str2mate", ":", "print mate of this stream", str2mate },
3490 	{ "str2wrq", ":", "print write queue of this stream", str2wrq },
3491 	{ "stream", ":", "display STREAM", stream },
3492 	{ "strftevent", ":", "print STREAMS flow trace event", strftevent },
3493 	{ "syncq", ":[-q|v] [-f flag] [-F flag] [-t type] [-T type]",
3494 		"filter and display STREAM sync queue", syncq, syncq_help },
3495 	{ "syncq2q", ":", "print queue for a given syncq", syncq2q },
3496 
3497 	/* from thread.c */
3498 	{ "thread", "?[-bdfimps]", "display a summarized kthread_t", thread,
3499 		thread_help },
3500 	{ "threadlist", "?[-v [count]]",
3501 		"display threads and associated C stack traces", threadlist,
3502 		threadlist_help },
3503 
3504 	/* from tsd.c */
3505 	{ "tsd", ":-k key", "print tsd[key-1] for this thread", ttotsd },
3506 	{ "tsdtot", ":", "find thread with this tsd", tsdtot },
3507 
3508 	/*
3509 	 * typegraph does not work under kmdb, as it requires too much memory
3510 	 * for its internal data structures.
3511 	 */
3512 #ifndef _KMDB
3513 	/* from typegraph.c */
3514 	{ "findlocks", ":", "find locks held by specified thread", findlocks },
3515 	{ "findfalse", "?[-v]", "find potentially falsely shared structures",
3516 		findfalse },
3517 	{ "typegraph", NULL, "build type graph", typegraph },
3518 	{ "istype", ":type", "manually set object type", istype },
3519 	{ "notype", ":", "manually clear object type", notype },
3520 	{ "whattype", ":", "determine object type", whattype },
3521 #endif
3522 
3523 	/* from vfs.c */
3524 	{ "fsinfo", "?[-v]", "print mounted filesystems", fsinfo },
3525 	{ "pfiles", ":[-fp]", "print process file information", pfiles,
3526 		pfiles_help },
3527 
3528 	/* from mdi.c */
3529 	{ "mdipi", NULL, "given a path, dump mdi_pathinfo "
3530 		"and detailed pi_prop list", mdipi },
3531 	{ "mdiprops", NULL, "given a pi_prop, dump the pi_prop list",
3532 		mdiprops },
3533 	{ "mdiphci", NULL, "given a phci, dump mdi_phci and "
3534 		"list all paths", mdiphci },
3535 	{ "mdivhci", NULL, "given a vhci, dump mdi_vhci and list "
3536 		"all phcis", mdivhci },
3537 	{ "mdiclient_paths", NULL, "given a path, walk mdi_pathinfo "
3538 		"client links", mdiclient_paths },
3539 	{ "mdiphci_paths", NULL, "given a path, walk through mdi_pathinfo "
3540 		"phci links", mdiphci_paths },
3541 	{ "mdiphcis", NULL, "given a phci, walk through mdi_phci ph_next links",
3542 		mdiphcis },
3543 
3544 	{ NULL }
3545 };
3546 
3547 static const mdb_walker_t walkers[] = {
3548 
3549 	/* from genunix.c */
3550 	{ "anon", "given an amp, list of anon structures",
3551 		anon_walk_init, anon_walk_step, anon_walk_fini },
3552 	{ "cpu", "walk cpu structures", cpu_walk_init, cpu_walk_step },
3553 	{ "ereportq_dump", "walk list of ereports in dump error queue",
3554 		ereportq_dump_walk_init, ereportq_dump_walk_step, NULL },
3555 	{ "ereportq_pend", "walk list of ereports in pending error queue",
3556 		ereportq_pend_walk_init, ereportq_pend_walk_step, NULL },
3557 	{ "errorq", "walk list of system error queues",
3558 		errorq_walk_init, errorq_walk_step, NULL },
3559 	{ "errorq_data", "walk pending error queue data buffers",
3560 		eqd_walk_init, eqd_walk_step, eqd_walk_fini },
3561 	{ "allfile", "given a proc pointer, list all file pointers",
3562 		file_walk_init, allfile_walk_step, file_walk_fini },
3563 	{ "file", "given a proc pointer, list of open file pointers",
3564 		file_walk_init, file_walk_step, file_walk_fini },
3565 	{ "lock_descriptor", "walk lock_descriptor_t structures",
3566 		ld_walk_init, ld_walk_step, NULL },
3567 	{ "lock_graph", "walk lock graph",
3568 		lg_walk_init, lg_walk_step, NULL },
3569 	{ "port", "given a proc pointer, list of created event ports",
3570 		port_walk_init, port_walk_step, NULL },
3571 	{ "portev", "given a port pointer, list of events in the queue",
3572 		portev_walk_init, portev_walk_step, portev_walk_fini },
3573 	{ "proc", "list of active proc_t structures",
3574 		proc_walk_init, proc_walk_step, proc_walk_fini },
3575 	{ "projects", "walk a list of kernel projects",
3576 		project_walk_init, project_walk_step, NULL },
3577 	{ "seg", "given an as, list of segments",
3578 		seg_walk_init, avl_walk_step, avl_walk_fini },
3579 	{ "sysevent_pend", "walk sysevent pending queue",
3580 		sysevent_pend_walk_init, sysevent_walk_step,
3581 		sysevent_walk_fini},
3582 	{ "sysevent_sent", "walk sysevent sent queue", sysevent_sent_walk_init,
3583 		sysevent_walk_step, sysevent_walk_fini},
3584 	{ "sysevent_channel", "walk sysevent channel subscriptions",
3585 		sysevent_channel_walk_init, sysevent_channel_walk_step,
3586 		sysevent_channel_walk_fini},
3587 	{ "sysevent_class_list", "walk sysevent subscription's class list",
3588 		sysevent_class_list_walk_init, sysevent_class_list_walk_step,
3589 		sysevent_class_list_walk_fini},
3590 	{ "sysevent_subclass_list",
3591 		"walk sysevent subscription's subclass list",
3592 		sysevent_subclass_list_walk_init,
3593 		sysevent_subclass_list_walk_step,
3594 		sysevent_subclass_list_walk_fini},
3595 	{ "task", "given a task pointer, walk its processes",
3596 		task_walk_init, task_walk_step, NULL },
3597 	{ "taskq_entry", "given a taskq_t*, list all taskq_ent_t in the list",
3598 		taskq_walk_init, taskq_walk_step, NULL, NULL },
3599 
3600 	/* from avl.c */
3601 	{ AVL_WALK_NAME, AVL_WALK_DESC,
3602 		avl_walk_init, avl_walk_step, avl_walk_fini },
3603 
3604 	/* from zone.c */
3605 	{ "zone", "walk a list of kernel zones",
3606 		zone_walk_init, zone_walk_step, NULL },
3607 	{ "zsd", "walk list of zsd entries for a zone",
3608 		zsd_walk_init, zsd_walk_step, NULL },
3609 
3610 	/* from bio.c */
3611 	{ "buf", "walk the bio buf hash",
3612 		buf_walk_init, buf_walk_step, buf_walk_fini },
3613 
3614 	/* from contract.c */
3615 	{ "contract", "walk all contracts, or those of the specified type",
3616 		ct_walk_init, generic_walk_step, NULL },
3617 	{ "ct_event", "walk events on a contract event queue",
3618 		ct_event_walk_init, generic_walk_step, NULL },
3619 	{ "ct_listener", "walk contract event queue listeners",
3620 		ct_listener_walk_init, generic_walk_step, NULL },
3621 
3622 	/* from cpupart.c */
3623 	{ "cpupart_cpulist", "given an cpupart_t, walk cpus in partition",
3624 		cpupart_cpulist_walk_init, cpupart_cpulist_walk_step,
3625 		NULL },
3626 	{ "cpupart_walk", "walk the set of cpu partitions",
3627 		cpupart_walk_init, cpupart_walk_step, NULL },
3628 
3629 	/* from ctxop.c */
3630 	{ "ctxop", "walk list of context ops on a thread",
3631 		ctxop_walk_init, ctxop_walk_step, ctxop_walk_fini },
3632 
3633 	/* from cyclic.c */
3634 	{ "cyccpu", "walk per-CPU cyc_cpu structures",
3635 		cyccpu_walk_init, cyccpu_walk_step, NULL },
3636 	{ "cycomni", "for an omnipresent cyclic, walk cyc_omni_cpu list",
3637 		cycomni_walk_init, cycomni_walk_step, NULL },
3638 	{ "cyctrace", "walk cyclic trace buffer",
3639 		cyctrace_walk_init, cyctrace_walk_step, cyctrace_walk_fini },
3640 
3641 	/* from devinfo.c */
3642 	{ "binding_hash", "walk all entries in binding hash table",
3643 		binding_hash_walk_init, binding_hash_walk_step, NULL },
3644 	{ "devinfo", "walk devinfo tree or subtree",
3645 		devinfo_walk_init, devinfo_walk_step, devinfo_walk_fini },
3646 	{ "devinfo_audit_log", "walk devinfo audit system-wide log",
3647 		devinfo_audit_log_walk_init, devinfo_audit_log_walk_step,
3648 		devinfo_audit_log_walk_fini},
3649 	{ "devinfo_audit_node", "walk per-devinfo audit history",
3650 		devinfo_audit_node_walk_init, devinfo_audit_node_walk_step,
3651 		devinfo_audit_node_walk_fini},
3652 	{ "devinfo_children", "walk children of devinfo node",
3653 		devinfo_children_walk_init, devinfo_children_walk_step,
3654 		devinfo_children_walk_fini },
3655 	{ "devinfo_parents", "walk ancestors of devinfo node",
3656 		devinfo_parents_walk_init, devinfo_parents_walk_step,
3657 		devinfo_parents_walk_fini },
3658 	{ "devinfo_siblings", "walk siblings of devinfo node",
3659 		devinfo_siblings_walk_init, devinfo_siblings_walk_step, NULL },
3660 	{ "devi_next", "walk devinfo list",
3661 		NULL, devi_next_walk_step, NULL },
3662 	{ "devnames", "walk devnames array",
3663 		devnames_walk_init, devnames_walk_step, devnames_walk_fini },
3664 	{ "minornode", "given a devinfo node, walk minor nodes",
3665 		minornode_walk_init, minornode_walk_step, NULL },
3666 	{ "softstate",
3667 		"given an i_ddi_soft_state*, list all in-use driver stateps",
3668 		soft_state_walk_init, soft_state_walk_step,
3669 		NULL, NULL },
3670 	{ "softstate_all",
3671 		"given an i_ddi_soft_state*, list all driver stateps",
3672 		soft_state_walk_init, soft_state_all_walk_step,
3673 		NULL, NULL },
3674 	{ "devinfo_fmc",
3675 		"walk a fault management handle cache active list",
3676 		devinfo_fmc_walk_init, devinfo_fmc_walk_step, NULL },
3677 
3678 	/* from kmem.c */
3679 	{ "allocdby", "given a thread, walk its allocated bufctls",
3680 		allocdby_walk_init, allocdby_walk_step, allocdby_walk_fini },
3681 	{ "bufctl", "walk a kmem cache's bufctls",
3682 		bufctl_walk_init, kmem_walk_step, kmem_walk_fini },
3683 	{ "bufctl_history", "walk the available history of a bufctl",
3684 		bufctl_history_walk_init, bufctl_history_walk_step,
3685 		bufctl_history_walk_fini },
3686 	{ "freedby", "given a thread, walk its freed bufctls",
3687 		freedby_walk_init, allocdby_walk_step, allocdby_walk_fini },
3688 	{ "freectl", "walk a kmem cache's free bufctls",
3689 		freectl_walk_init, kmem_walk_step, kmem_walk_fini },
3690 	{ "freectl_constructed", "walk a kmem cache's constructed free bufctls",
3691 		freectl_constructed_walk_init, kmem_walk_step, kmem_walk_fini },
3692 	{ "freemem", "walk a kmem cache's free memory",
3693 		freemem_walk_init, kmem_walk_step, kmem_walk_fini },
3694 	{ "freemem_constructed", "walk a kmem cache's constructed free memory",
3695 		freemem_constructed_walk_init, kmem_walk_step, kmem_walk_fini },
3696 	{ "kmem", "walk a kmem cache",
3697 		kmem_walk_init, kmem_walk_step, kmem_walk_fini },
3698 	{ "kmem_cpu_cache", "given a kmem cache, walk its per-CPU caches",
3699 		kmem_cpu_cache_walk_init, kmem_cpu_cache_walk_step, NULL },
3700 	{ "kmem_hash", "given a kmem cache, walk its allocated hash table",
3701 		kmem_hash_walk_init, kmem_hash_walk_step, kmem_hash_walk_fini },
3702 	{ "kmem_log", "walk the kmem transaction log",
3703 		kmem_log_walk_init, kmem_log_walk_step, kmem_log_walk_fini },
3704 	{ "kmem_slab", "given a kmem cache, walk its slabs",
3705 		kmem_slab_walk_init, kmem_slab_walk_step, NULL },
3706 	{ "kmem_slab_partial",
3707 	    "given a kmem cache, walk its partially allocated slabs (min 1)",
3708 		kmem_slab_walk_partial_init, kmem_slab_walk_step, NULL },
3709 	{ "vmem", "walk vmem structures in pre-fix, depth-first order",
3710 		vmem_walk_init, vmem_walk_step, vmem_walk_fini },
3711 	{ "vmem_alloc", "given a vmem_t, walk its allocated vmem_segs",
3712 		vmem_alloc_walk_init, vmem_seg_walk_step, vmem_seg_walk_fini },
3713 	{ "vmem_free", "given a vmem_t, walk its free vmem_segs",
3714 		vmem_free_walk_init, vmem_seg_walk_step, vmem_seg_walk_fini },
3715 	{ "vmem_postfix", "walk vmem structures in post-fix, depth-first order",
3716 		vmem_walk_init, vmem_postfix_walk_step, vmem_walk_fini },
3717 	{ "vmem_seg", "given a vmem_t, walk all of its vmem_segs",
3718 		vmem_seg_walk_init, vmem_seg_walk_step, vmem_seg_walk_fini },
3719 	{ "vmem_span", "given a vmem_t, walk its spanning vmem_segs",
3720 		vmem_span_walk_init, vmem_seg_walk_step, vmem_seg_walk_fini },
3721 
3722 	/* from ldi.c */
3723 	{ "ldi_handle", "walk the layered driver handle hash",
3724 		ldi_handle_walk_init, ldi_handle_walk_step, NULL },
3725 	{ "ldi_ident", "walk the layered driver identifier hash",
3726 		ldi_ident_walk_init, ldi_ident_walk_step, NULL },
3727 
3728 	/* from leaky.c + leaky_subr.c */
3729 	{ "leak", "given a leaked bufctl or vmem_seg, find leaks w/ same "
3730 	    "stack trace",
3731 		leaky_walk_init, leaky_walk_step, leaky_walk_fini },
3732 	{ "leakbuf", "given a leaked bufctl or vmem_seg, walk buffers for "
3733 	    "leaks w/ same stack trace",
3734 		leaky_walk_init, leaky_buf_walk_step, leaky_walk_fini },
3735 
3736 	/* from lgrp.c */
3737 	{ "lgrp_cpulist", "walk CPUs in a given lgroup",
3738 		lgrp_cpulist_walk_init, lgrp_cpulist_walk_step, NULL },
3739 	{ "lgrptbl", "walk lgroup table",
3740 		lgrp_walk_init, lgrp_walk_step, NULL },
3741 	{ "lgrp_parents", "walk up lgroup lineage from given lgroup",
3742 		lgrp_parents_walk_init, lgrp_parents_walk_step, NULL },
3743 	{ "lgrp_rsrc_mem", "walk lgroup memory resources of given lgroup",
3744 		lgrp_rsrc_mem_walk_init, lgrp_set_walk_step, NULL },
3745 	{ "lgrp_rsrc_cpu", "walk lgroup CPU resources of given lgroup",
3746 		lgrp_rsrc_cpu_walk_init, lgrp_set_walk_step, NULL },
3747 
3748 	/* from group.c */
3749 	{ "group", "walk all elements of a group",
3750 		group_walk_init, group_walk_step, NULL },
3751 
3752 	/* from list.c */
3753 	{ LIST_WALK_NAME, LIST_WALK_DESC,
3754 		list_walk_init, list_walk_step, list_walk_fini },
3755 
3756 	/* from memory.c */
3757 	{ "page", "walk all pages, or those from the specified vnode",
3758 		page_walk_init, page_walk_step, page_walk_fini },
3759 	{ "memlist", "walk specified memlist",
3760 		NULL, memlist_walk_step, NULL },
3761 	{ "swapinfo", "walk swapinfo structures",
3762 		swap_walk_init, swap_walk_step, NULL },
3763 
3764 	/* from mmd.c */
3765 	{ "pattr", "walk pattr_t structures", pattr_walk_init,
3766 		mmdq_walk_step, mmdq_walk_fini },
3767 	{ "pdesc", "walk pdesc_t structures",
3768 		pdesc_walk_init, mmdq_walk_step, mmdq_walk_fini },
3769 	{ "pdesc_slab", "walk pdesc_slab_t structures",
3770 		pdesc_slab_walk_init, mmdq_walk_step, mmdq_walk_fini },
3771 
3772 	/* from modhash.c */
3773 	{ "modhash", "walk list of mod_hash structures", modhash_walk_init,
3774 		modhash_walk_step, NULL },
3775 	{ "modent", "walk list of entries in a given mod_hash",
3776 		modent_walk_init, modent_walk_step, modent_walk_fini },
3777 	{ "modchain", "walk list of entries in a given mod_hash_entry",
3778 		NULL, modchain_walk_step, NULL },
3779 
3780 	/* from net.c */
3781 	{ "ar", "walk ar_t structures using MI for all stacks",
3782 		mi_payload_walk_init, mi_payload_walk_step, NULL, &mi_ar_arg },
3783 	{ "icmp", "walk ICMP control structures using MI for all stacks",
3784 		mi_payload_walk_init, mi_payload_walk_step, NULL,
3785 		&mi_icmp_arg },
3786 	{ "ill", "walk ill_t structures using MI for all stacks",
3787 		mi_payload_walk_init, mi_payload_walk_step, NULL, &mi_ill_arg },
3788 
3789 	{ "mi", "given a MI_O, walk the MI",
3790 		mi_walk_init, mi_walk_step, mi_walk_fini, NULL },
3791 	{ "sonode", "given a sonode, walk its children",
3792 		sonode_walk_init, sonode_walk_step, sonode_walk_fini, NULL },
3793 
3794 	{ "ar_stacks", "walk all the ar_stack_t",
3795 		ar_stacks_walk_init, ar_stacks_walk_step, NULL },
3796 	{ "icmp_stacks", "walk all the icmp_stack_t",
3797 		icmp_stacks_walk_init, icmp_stacks_walk_step, NULL },
3798 	{ "tcp_stacks", "walk all the tcp_stack_t",
3799 		tcp_stacks_walk_init, tcp_stacks_walk_step, NULL },
3800 	{ "udp_stacks", "walk all the udp_stack_t",
3801 		udp_stacks_walk_init, udp_stacks_walk_step, NULL },
3802 
3803 	/* from nvpair.c */
3804 	{ NVPAIR_WALKER_NAME, NVPAIR_WALKER_DESCR,
3805 		nvpair_walk_init, nvpair_walk_step, NULL },
3806 
3807 	/* from rctl.c */
3808 	{ "rctl_dict_list", "walk all rctl_dict_entry_t's from rctl_lists",
3809 		rctl_dict_walk_init, rctl_dict_walk_step, NULL },
3810 	{ "rctl_set", "given a rctl_set, walk all rctls", rctl_set_walk_init,
3811 		rctl_set_walk_step, NULL },
3812 	{ "rctl_val", "given a rctl_t, walk all rctl_val entries associated",
3813 		rctl_val_walk_init, rctl_val_walk_step },
3814 
3815 	/* from sobj.c */
3816 	{ "blocked", "walk threads blocked on a given sobj",
3817 		blocked_walk_init, blocked_walk_step, NULL },
3818 	{ "wchan", "given a wchan, list of blocked threads",
3819 		wchan_walk_init, wchan_walk_step, wchan_walk_fini },
3820 
3821 	/* from stream.c */
3822 	{ "b_cont", "walk mblk_t list using b_cont",
3823 		mblk_walk_init, b_cont_step, mblk_walk_fini },
3824 	{ "b_next", "walk mblk_t list using b_next",
3825 		mblk_walk_init, b_next_step, mblk_walk_fini },
3826 	{ "qlink", "walk queue_t list using q_link",
3827 		queue_walk_init, queue_link_step, queue_walk_fini },
3828 	{ "qnext", "walk queue_t list using q_next",
3829 		queue_walk_init, queue_next_step, queue_walk_fini },
3830 	{ "strftblk", "given a dblk_t, walk STREAMS flow trace event list",
3831 		strftblk_walk_init, strftblk_step, strftblk_walk_fini },
3832 	{ "readq", "walk read queue side of stdata",
3833 		str_walk_init, strr_walk_step, str_walk_fini },
3834 	{ "writeq", "walk write queue side of stdata",
3835 		str_walk_init, strw_walk_step, str_walk_fini },
3836 
3837 	/* from thread.c */
3838 	{ "deathrow", "walk threads on both lwp_ and thread_deathrow",
3839 		deathrow_walk_init, deathrow_walk_step, NULL },
3840 	{ "cpu_dispq", "given a cpu_t, walk threads in dispatcher queues",
3841 		cpu_dispq_walk_init, dispq_walk_step, dispq_walk_fini },
3842 	{ "cpupart_dispq",
3843 		"given a cpupart_t, walk threads in dispatcher queues",
3844 		cpupart_dispq_walk_init, dispq_walk_step, dispq_walk_fini },
3845 	{ "lwp_deathrow", "walk lwp_deathrow",
3846 		lwp_deathrow_walk_init, deathrow_walk_step, NULL },
3847 	{ "thread", "global or per-process kthread_t structures",
3848 		thread_walk_init, thread_walk_step, thread_walk_fini },
3849 	{ "thread_deathrow", "walk threads on thread_deathrow",
3850 		thread_deathrow_walk_init, deathrow_walk_step, NULL },
3851 
3852 	/* from tsd.c */
3853 	{ "tsd", "walk list of thread-specific data",
3854 		tsd_walk_init, tsd_walk_step, tsd_walk_fini },
3855 
3856 	/* from tsol.c */
3857 	{ "tnrh", "walk remote host cache structures",
3858 	    tnrh_walk_init, tnrh_walk_step, tnrh_walk_fini },
3859 	{ "tnrhtp", "walk remote host template structures",
3860 	    tnrhtp_walk_init, tnrhtp_walk_step, tnrhtp_walk_fini },
3861 
3862 	/*
3863 	 * typegraph does not work under kmdb, as it requires too much memory
3864 	 * for its internal data structures.
3865 	 */
3866 #ifndef _KMDB
3867 	/* from typegraph.c */
3868 	{ "typeconflict", "walk buffers with conflicting type inferences",
3869 		typegraph_walk_init, typeconflict_walk_step },
3870 	{ "typeunknown", "walk buffers with unknown types",
3871 		typegraph_walk_init, typeunknown_walk_step },
3872 #endif
3873 
3874 	/* from vfs.c */
3875 	{ "vfs", "walk file system list",
3876 		vfs_walk_init, vfs_walk_step },
3877 
3878 	/* from mdi.c */
3879 	{ "mdipi_client_list", "Walker for mdi_pathinfo pi_client_link",
3880 		mdi_pi_client_link_walk_init,
3881 		mdi_pi_client_link_walk_step,
3882 		mdi_pi_client_link_walk_fini },
3883 
3884 	{ "mdipi_phci_list", "Walker for mdi_pathinfo pi_phci_link",
3885 		mdi_pi_phci_link_walk_init,
3886 		mdi_pi_phci_link_walk_step,
3887 		mdi_pi_phci_link_walk_fini },
3888 
3889 	{ "mdiphci_list", "Walker for mdi_phci ph_next link",
3890 		mdi_phci_ph_next_walk_init,
3891 		mdi_phci_ph_next_walk_step,
3892 		mdi_phci_ph_next_walk_fini },
3893 
3894 	/* from netstack.c */
3895 	{ "netstack", "walk a list of kernel netstacks",
3896 		netstack_walk_init, netstack_walk_step, NULL },
3897 
3898 	{ NULL }
3899 };
3900 
3901 static const mdb_modinfo_t modinfo = { MDB_API_VERSION, dcmds, walkers };
3902 
3903 const mdb_modinfo_t *
3904 _mdb_init(void)
3905 {
3906 	if (mdb_readvar(&devinfo_root, "top_devinfo") == -1) {
3907 		mdb_warn("failed to read 'top_devinfo'");
3908 		return (NULL);
3909 	}
3910 
3911 	if (findstack_init() != DCMD_OK)
3912 		return (NULL);
3913 
3914 	kmem_init();
3915 
3916 	return (&modinfo);
3917 }
3918 
3919 void
3920 _mdb_fini(void)
3921 {
3922 	/*
3923 	 * Force ::findleaks to let go any cached memory
3924 	 */
3925 	leaky_cleanup(1);
3926 }
3927