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