xref: /illumos-gate/usr/src/cmd/mdb/common/modules/genunix/genunix.c (revision 60405de4d8688d96dd05157c28db3ade5c9bc234)
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 static int
1145 kmastat_cache(uintptr_t addr, const kmem_cache_t *cp, kmastat_vmem_t **kvp)
1146 {
1147 	kmastat_vmem_t *kv;
1148 	datafmt_t *dfp = kmemfmt;
1149 	int magsize;
1150 
1151 	int avail, alloc, total;
1152 	size_t meminuse = (cp->cache_slab_create - cp->cache_slab_destroy) *
1153 	    cp->cache_slabsize;
1154 
1155 	mdb_walk_cb_t cpu_avail = (mdb_walk_cb_t)kmastat_cpu_avail;
1156 	mdb_walk_cb_t cpu_alloc = (mdb_walk_cb_t)kmastat_cpu_alloc;
1157 	mdb_walk_cb_t slab_avail = (mdb_walk_cb_t)kmastat_slab_avail;
1158 
1159 	magsize = kmem_get_magsize(cp);
1160 
1161 	alloc = cp->cache_slab_alloc + cp->cache_full.ml_alloc;
1162 	avail = cp->cache_full.ml_total * magsize;
1163 	total = cp->cache_buftotal;
1164 
1165 	(void) mdb_pwalk("kmem_cpu_cache", cpu_alloc, &alloc, addr);
1166 	(void) mdb_pwalk("kmem_cpu_cache", cpu_avail, &avail, addr);
1167 	(void) mdb_pwalk("kmem_slab_partial", slab_avail, &avail, addr);
1168 
1169 	for (kv = *kvp; kv != NULL; kv = kv->kv_next) {
1170 		if (kv->kv_addr == (uintptr_t)cp->cache_arena)
1171 			goto out;
1172 	}
1173 
1174 	kv = mdb_zalloc(sizeof (kmastat_vmem_t), UM_SLEEP | UM_GC);
1175 	kv->kv_next = *kvp;
1176 	kv->kv_addr = (uintptr_t)cp->cache_arena;
1177 	*kvp = kv;
1178 out:
1179 	kv->kv_meminuse += meminuse;
1180 	kv->kv_alloc += alloc;
1181 	kv->kv_fail += cp->cache_alloc_fail;
1182 
1183 	mdb_printf((dfp++)->fmt, cp->cache_name);
1184 	mdb_printf((dfp++)->fmt, cp->cache_bufsize);
1185 	mdb_printf((dfp++)->fmt, total - avail);
1186 	mdb_printf((dfp++)->fmt, total);
1187 	mdb_printf((dfp++)->fmt, meminuse);
1188 	mdb_printf((dfp++)->fmt, alloc);
1189 	mdb_printf((dfp++)->fmt, cp->cache_alloc_fail);
1190 	mdb_printf("\n");
1191 
1192 	return (WALK_NEXT);
1193 }
1194 
1195 static int
1196 kmastat_vmem_totals(uintptr_t addr, const vmem_t *v, kmastat_vmem_t *kv)
1197 {
1198 	size_t len;
1199 
1200 	while (kv != NULL && kv->kv_addr != addr)
1201 		kv = kv->kv_next;
1202 
1203 	if (kv == NULL || kv->kv_alloc == 0)
1204 		return (WALK_NEXT);
1205 
1206 	len = MIN(17, strlen(v->vm_name));
1207 
1208 	mdb_printf("Total [%s]%*s %6s %6s %6s %9u %9u %5u\n", v->vm_name,
1209 	    17 - len, "", "", "", "",
1210 	    kv->kv_meminuse, kv->kv_alloc, kv->kv_fail);
1211 
1212 	return (WALK_NEXT);
1213 }
1214 
1215 /*ARGSUSED*/
1216 static int
1217 kmastat_vmem(uintptr_t addr, const vmem_t *v, void *ignored)
1218 {
1219 	datafmt_t *dfp = vmemfmt;
1220 	const vmem_kstat_t *vkp = &v->vm_kstat;
1221 	uintptr_t paddr;
1222 	vmem_t parent;
1223 	int ident = 0;
1224 
1225 	for (paddr = (uintptr_t)v->vm_source; paddr != NULL; ident += 4) {
1226 		if (mdb_vread(&parent, sizeof (parent), paddr) == -1) {
1227 			mdb_warn("couldn't trace %p's ancestry", addr);
1228 			ident = 0;
1229 			break;
1230 		}
1231 		paddr = (uintptr_t)parent.vm_source;
1232 	}
1233 
1234 	mdb_printf("%*s", ident, "");
1235 	mdb_printf((dfp++)->fmt, 25 - ident, v->vm_name);
1236 	mdb_printf((dfp++)->fmt, vkp->vk_mem_inuse.value.ui64);
1237 	mdb_printf((dfp++)->fmt, vkp->vk_mem_total.value.ui64);
1238 	mdb_printf((dfp++)->fmt, vkp->vk_mem_import.value.ui64);
1239 	mdb_printf((dfp++)->fmt, vkp->vk_alloc.value.ui64);
1240 	mdb_printf((dfp++)->fmt, vkp->vk_fail.value.ui64);
1241 
1242 	mdb_printf("\n");
1243 
1244 	return (WALK_NEXT);
1245 }
1246 
1247 /*ARGSUSED*/
1248 int
1249 kmastat(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1250 {
1251 	kmastat_vmem_t *kv = NULL;
1252 	datafmt_t *dfp;
1253 
1254 	if (argc != 0)
1255 		return (DCMD_USAGE);
1256 
1257 	for (dfp = kmemfmt; dfp->hdr1 != NULL; dfp++)
1258 		mdb_printf("%s ", dfp->hdr1);
1259 	mdb_printf("\n");
1260 
1261 	for (dfp = kmemfmt; dfp->hdr1 != NULL; dfp++)
1262 		mdb_printf("%s ", dfp->hdr2);
1263 	mdb_printf("\n");
1264 
1265 	for (dfp = kmemfmt; dfp->hdr1 != NULL; dfp++)
1266 		mdb_printf("%s ", dfp->dashes);
1267 	mdb_printf("\n");
1268 
1269 	if (mdb_walk("kmem_cache", (mdb_walk_cb_t)kmastat_cache, &kv) == -1) {
1270 		mdb_warn("can't walk 'kmem_cache'");
1271 		return (DCMD_ERR);
1272 	}
1273 
1274 	for (dfp = kmemfmt; dfp->hdr1 != NULL; dfp++)
1275 		mdb_printf("%s ", dfp->dashes);
1276 	mdb_printf("\n");
1277 
1278 	if (mdb_walk("vmem", (mdb_walk_cb_t)kmastat_vmem_totals, kv) == -1) {
1279 		mdb_warn("can't walk 'vmem'");
1280 		return (DCMD_ERR);
1281 	}
1282 
1283 	for (dfp = kmemfmt; dfp->hdr1 != NULL; dfp++)
1284 		mdb_printf("%s ", dfp->dashes);
1285 	mdb_printf("\n");
1286 
1287 	mdb_printf("\n");
1288 
1289 	for (dfp = vmemfmt; dfp->hdr1 != NULL; dfp++)
1290 		mdb_printf("%s ", dfp->hdr1);
1291 	mdb_printf("\n");
1292 
1293 	for (dfp = vmemfmt; dfp->hdr1 != NULL; dfp++)
1294 		mdb_printf("%s ", dfp->hdr2);
1295 	mdb_printf("\n");
1296 
1297 	for (dfp = vmemfmt; dfp->hdr1 != NULL; dfp++)
1298 		mdb_printf("%s ", dfp->dashes);
1299 	mdb_printf("\n");
1300 
1301 	if (mdb_walk("vmem", (mdb_walk_cb_t)kmastat_vmem, NULL) == -1) {
1302 		mdb_warn("can't walk 'vmem'");
1303 		return (DCMD_ERR);
1304 	}
1305 
1306 	for (dfp = vmemfmt; dfp->hdr1 != NULL; dfp++)
1307 		mdb_printf("%s ", dfp->dashes);
1308 	mdb_printf("\n");
1309 	return (DCMD_OK);
1310 }
1311 
1312 /*
1313  * Our ::kgrep callback scans the entire kernel VA space (kas).  kas is made
1314  * up of a set of 'struct seg's.  We could just scan each seg en masse, but
1315  * unfortunately, a few of the segs are both large and sparse, so we could
1316  * spend quite a bit of time scanning VAs which have no backing pages.
1317  *
1318  * So for the few very sparse segs, we skip the segment itself, and scan
1319  * the allocated vmem_segs in the vmem arena which manages that part of kas.
1320  * Currently, we do this for:
1321  *
1322  *	SEG		VMEM ARENA
1323  *	kvseg		heap_arena
1324  *	kvseg32		heap32_arena
1325  *	kvseg_core	heap_core_arena
1326  *
1327  * In addition, we skip the segkpm segment in its entirety, since it is very
1328  * sparse, and contains no new kernel data.
1329  */
1330 typedef struct kgrep_walk_data {
1331 	kgrep_cb_func *kg_cb;
1332 	void *kg_cbdata;
1333 	uintptr_t kg_kvseg;
1334 	uintptr_t kg_kvseg32;
1335 	uintptr_t kg_kvseg_core;
1336 	uintptr_t kg_segkpm;
1337 	uintptr_t kg_heap_lp_base;
1338 	uintptr_t kg_heap_lp_end;
1339 } kgrep_walk_data_t;
1340 
1341 static int
1342 kgrep_walk_seg(uintptr_t addr, const struct seg *seg, kgrep_walk_data_t *kg)
1343 {
1344 	uintptr_t base = (uintptr_t)seg->s_base;
1345 
1346 	if (addr == kg->kg_kvseg || addr == kg->kg_kvseg32 ||
1347 	    addr == kg->kg_kvseg_core)
1348 		return (WALK_NEXT);
1349 
1350 	if ((uintptr_t)seg->s_ops == kg->kg_segkpm)
1351 		return (WALK_NEXT);
1352 
1353 	return (kg->kg_cb(base, base + seg->s_size, kg->kg_cbdata));
1354 }
1355 
1356 /*ARGSUSED*/
1357 static int
1358 kgrep_walk_vseg(uintptr_t addr, const vmem_seg_t *seg, kgrep_walk_data_t *kg)
1359 {
1360 	/*
1361 	 * skip large page heap address range - it is scanned by walking
1362 	 * allocated vmem_segs in the heap_lp_arena
1363 	 */
1364 	if (seg->vs_start == kg->kg_heap_lp_base &&
1365 	    seg->vs_end == kg->kg_heap_lp_end)
1366 		return (WALK_NEXT);
1367 
1368 	return (kg->kg_cb(seg->vs_start, seg->vs_end, kg->kg_cbdata));
1369 }
1370 
1371 /*ARGSUSED*/
1372 static int
1373 kgrep_xwalk_vseg(uintptr_t addr, const vmem_seg_t *seg, kgrep_walk_data_t *kg)
1374 {
1375 	return (kg->kg_cb(seg->vs_start, seg->vs_end, kg->kg_cbdata));
1376 }
1377 
1378 static int
1379 kgrep_walk_vmem(uintptr_t addr, const vmem_t *vmem, kgrep_walk_data_t *kg)
1380 {
1381 	mdb_walk_cb_t walk_vseg = (mdb_walk_cb_t)kgrep_walk_vseg;
1382 
1383 	if (strcmp(vmem->vm_name, "heap") != 0 &&
1384 	    strcmp(vmem->vm_name, "heap32") != 0 &&
1385 	    strcmp(vmem->vm_name, "heap_core") != 0 &&
1386 	    strcmp(vmem->vm_name, "heap_lp") != 0)
1387 		return (WALK_NEXT);
1388 
1389 	if (strcmp(vmem->vm_name, "heap_lp") == 0)
1390 		walk_vseg = (mdb_walk_cb_t)kgrep_xwalk_vseg;
1391 
1392 	if (mdb_pwalk("vmem_alloc", walk_vseg, kg, addr) == -1) {
1393 		mdb_warn("couldn't walk vmem_alloc for vmem %p", addr);
1394 		return (WALK_ERR);
1395 	}
1396 
1397 	return (WALK_NEXT);
1398 }
1399 
1400 int
1401 kgrep_subr(kgrep_cb_func *cb, void *cbdata)
1402 {
1403 	GElf_Sym kas, kvseg, kvseg32, kvseg_core, segkpm;
1404 	kgrep_walk_data_t kg;
1405 
1406 	if (mdb_get_state() == MDB_STATE_RUNNING) {
1407 		mdb_warn("kgrep can only be run on a system "
1408 		    "dump or under kmdb; see dumpadm(1M)\n");
1409 		return (DCMD_ERR);
1410 	}
1411 
1412 	if (mdb_lookup_by_name("kas", &kas) == -1) {
1413 		mdb_warn("failed to locate 'kas' symbol\n");
1414 		return (DCMD_ERR);
1415 	}
1416 
1417 	if (mdb_lookup_by_name("kvseg", &kvseg) == -1) {
1418 		mdb_warn("failed to locate 'kvseg' symbol\n");
1419 		return (DCMD_ERR);
1420 	}
1421 
1422 	if (mdb_lookup_by_name("kvseg32", &kvseg32) == -1) {
1423 		mdb_warn("failed to locate 'kvseg32' symbol\n");
1424 		return (DCMD_ERR);
1425 	}
1426 
1427 	if (mdb_lookup_by_name("kvseg_core", &kvseg_core) == -1) {
1428 		mdb_warn("failed to locate 'kvseg_core' symbol\n");
1429 		return (DCMD_ERR);
1430 	}
1431 
1432 	if (mdb_lookup_by_name("segkpm_ops", &segkpm) == -1) {
1433 		mdb_warn("failed to locate 'segkpm_ops' symbol\n");
1434 		return (DCMD_ERR);
1435 	}
1436 
1437 	if (mdb_readvar(&kg.kg_heap_lp_base, "heap_lp_base") == -1) {
1438 		mdb_warn("failed to read 'heap_lp_base'\n");
1439 		return (DCMD_ERR);
1440 	}
1441 
1442 	if (mdb_readvar(&kg.kg_heap_lp_end, "heap_lp_end") == -1) {
1443 		mdb_warn("failed to read 'heap_lp_end'\n");
1444 		return (DCMD_ERR);
1445 	}
1446 
1447 	kg.kg_cb = cb;
1448 	kg.kg_cbdata = cbdata;
1449 	kg.kg_kvseg = (uintptr_t)kvseg.st_value;
1450 	kg.kg_kvseg32 = (uintptr_t)kvseg32.st_value;
1451 	kg.kg_kvseg_core = (uintptr_t)kvseg_core.st_value;
1452 	kg.kg_segkpm = (uintptr_t)segkpm.st_value;
1453 
1454 	if (mdb_pwalk("seg", (mdb_walk_cb_t)kgrep_walk_seg,
1455 	    &kg, kas.st_value) == -1) {
1456 		mdb_warn("failed to walk kas segments");
1457 		return (DCMD_ERR);
1458 	}
1459 
1460 	if (mdb_walk("vmem", (mdb_walk_cb_t)kgrep_walk_vmem, &kg) == -1) {
1461 		mdb_warn("failed to walk heap/heap32 vmem arenas");
1462 		return (DCMD_ERR);
1463 	}
1464 
1465 	return (DCMD_OK);
1466 }
1467 
1468 size_t
1469 kgrep_subr_pagesize(void)
1470 {
1471 	return (PAGESIZE);
1472 }
1473 
1474 typedef struct file_walk_data {
1475 	struct uf_entry *fw_flist;
1476 	int fw_flistsz;
1477 	int fw_ndx;
1478 	int fw_nofiles;
1479 } file_walk_data_t;
1480 
1481 int
1482 file_walk_init(mdb_walk_state_t *wsp)
1483 {
1484 	file_walk_data_t *fw;
1485 	proc_t p;
1486 
1487 	if (wsp->walk_addr == NULL) {
1488 		mdb_warn("file walk doesn't support global walks\n");
1489 		return (WALK_ERR);
1490 	}
1491 
1492 	fw = mdb_alloc(sizeof (file_walk_data_t), UM_SLEEP);
1493 
1494 	if (mdb_vread(&p, sizeof (p), wsp->walk_addr) == -1) {
1495 		mdb_free(fw, sizeof (file_walk_data_t));
1496 		mdb_warn("failed to read proc structure at %p", wsp->walk_addr);
1497 		return (WALK_ERR);
1498 	}
1499 
1500 	if (p.p_user.u_finfo.fi_nfiles == 0) {
1501 		mdb_free(fw, sizeof (file_walk_data_t));
1502 		return (WALK_DONE);
1503 	}
1504 
1505 	fw->fw_nofiles = p.p_user.u_finfo.fi_nfiles;
1506 	fw->fw_flistsz = sizeof (struct uf_entry) * fw->fw_nofiles;
1507 	fw->fw_flist = mdb_alloc(fw->fw_flistsz, UM_SLEEP);
1508 
1509 	if (mdb_vread(fw->fw_flist, fw->fw_flistsz,
1510 	    (uintptr_t)p.p_user.u_finfo.fi_list) == -1) {
1511 		mdb_warn("failed to read file array at %p",
1512 		    p.p_user.u_finfo.fi_list);
1513 		mdb_free(fw->fw_flist, fw->fw_flistsz);
1514 		mdb_free(fw, sizeof (file_walk_data_t));
1515 		return (WALK_ERR);
1516 	}
1517 
1518 	fw->fw_ndx = 0;
1519 	wsp->walk_data = fw;
1520 
1521 	return (WALK_NEXT);
1522 }
1523 
1524 int
1525 file_walk_step(mdb_walk_state_t *wsp)
1526 {
1527 	file_walk_data_t *fw = (file_walk_data_t *)wsp->walk_data;
1528 	struct file file;
1529 	uintptr_t fp;
1530 
1531 again:
1532 	if (fw->fw_ndx == fw->fw_nofiles)
1533 		return (WALK_DONE);
1534 
1535 	if ((fp = (uintptr_t)fw->fw_flist[fw->fw_ndx++].uf_file) == NULL)
1536 		goto again;
1537 
1538 	(void) mdb_vread(&file, sizeof (file), (uintptr_t)fp);
1539 	return (wsp->walk_callback(fp, &file, wsp->walk_cbdata));
1540 }
1541 
1542 int
1543 allfile_walk_step(mdb_walk_state_t *wsp)
1544 {
1545 	file_walk_data_t *fw = (file_walk_data_t *)wsp->walk_data;
1546 	struct file file;
1547 	uintptr_t fp;
1548 
1549 	if (fw->fw_ndx == fw->fw_nofiles)
1550 		return (WALK_DONE);
1551 
1552 	if ((fp = (uintptr_t)fw->fw_flist[fw->fw_ndx++].uf_file) != NULL)
1553 		(void) mdb_vread(&file, sizeof (file), (uintptr_t)fp);
1554 	else
1555 		bzero(&file, sizeof (file));
1556 
1557 	return (wsp->walk_callback(fp, &file, wsp->walk_cbdata));
1558 }
1559 
1560 void
1561 file_walk_fini(mdb_walk_state_t *wsp)
1562 {
1563 	file_walk_data_t *fw = (file_walk_data_t *)wsp->walk_data;
1564 
1565 	mdb_free(fw->fw_flist, fw->fw_flistsz);
1566 	mdb_free(fw, sizeof (file_walk_data_t));
1567 }
1568 
1569 int
1570 port_walk_init(mdb_walk_state_t *wsp)
1571 {
1572 	if (wsp->walk_addr == NULL) {
1573 		mdb_warn("port walk doesn't support global walks\n");
1574 		return (WALK_ERR);
1575 	}
1576 
1577 	if (mdb_layered_walk("file", wsp) == -1) {
1578 		mdb_warn("couldn't walk 'file'");
1579 		return (WALK_ERR);
1580 	}
1581 	return (WALK_NEXT);
1582 }
1583 
1584 int
1585 port_walk_step(mdb_walk_state_t *wsp)
1586 {
1587 	struct vnode	vn;
1588 	uintptr_t	vp;
1589 	uintptr_t	pp;
1590 	struct port	port;
1591 
1592 	vp = (uintptr_t)((struct file *)wsp->walk_layer)->f_vnode;
1593 	if (mdb_vread(&vn, sizeof (vn), vp) == -1) {
1594 		mdb_warn("failed to read vnode_t at %p", vp);
1595 		return (WALK_ERR);
1596 	}
1597 	if (vn.v_type != VPORT)
1598 		return (WALK_NEXT);
1599 
1600 	pp = (uintptr_t)vn.v_data;
1601 	if (mdb_vread(&port, sizeof (port), pp) == -1) {
1602 		mdb_warn("failed to read port_t at %p", pp);
1603 		return (WALK_ERR);
1604 	}
1605 	return (wsp->walk_callback(pp, &port, wsp->walk_cbdata));
1606 }
1607 
1608 typedef struct portev_walk_data {
1609 	list_node_t	*pev_node;
1610 	list_node_t	*pev_last;
1611 	size_t		pev_offset;
1612 } portev_walk_data_t;
1613 
1614 int
1615 portev_walk_init(mdb_walk_state_t *wsp)
1616 {
1617 	portev_walk_data_t *pevd;
1618 	struct port	port;
1619 	struct vnode	vn;
1620 	struct list	*list;
1621 	uintptr_t	vp;
1622 
1623 	if (wsp->walk_addr == NULL) {
1624 		mdb_warn("portev walk doesn't support global walks\n");
1625 		return (WALK_ERR);
1626 	}
1627 
1628 	pevd = mdb_alloc(sizeof (portev_walk_data_t), UM_SLEEP);
1629 
1630 	if (mdb_vread(&port, sizeof (port), wsp->walk_addr) == -1) {
1631 		mdb_free(pevd, sizeof (portev_walk_data_t));
1632 		mdb_warn("failed to read port structure at %p", wsp->walk_addr);
1633 		return (WALK_ERR);
1634 	}
1635 
1636 	vp = (uintptr_t)port.port_vnode;
1637 	if (mdb_vread(&vn, sizeof (vn), vp) == -1) {
1638 		mdb_free(pevd, sizeof (portev_walk_data_t));
1639 		mdb_warn("failed to read vnode_t at %p", vp);
1640 		return (WALK_ERR);
1641 	}
1642 
1643 	if (vn.v_type != VPORT) {
1644 		mdb_free(pevd, sizeof (portev_walk_data_t));
1645 		mdb_warn("input address (%p) does not point to an event port",
1646 		    wsp->walk_addr);
1647 		return (WALK_ERR);
1648 	}
1649 
1650 	if (port.port_queue.portq_nent == 0) {
1651 		mdb_free(pevd, sizeof (portev_walk_data_t));
1652 		return (WALK_DONE);
1653 	}
1654 	list = &port.port_queue.portq_list;
1655 	pevd->pev_offset = list->list_offset;
1656 	pevd->pev_last = list->list_head.list_prev;
1657 	pevd->pev_node = list->list_head.list_next;
1658 	wsp->walk_data = pevd;
1659 	return (WALK_NEXT);
1660 }
1661 
1662 int
1663 portev_walk_step(mdb_walk_state_t *wsp)
1664 {
1665 	portev_walk_data_t	*pevd;
1666 	struct port_kevent	ev;
1667 	uintptr_t		evp;
1668 
1669 	pevd = (portev_walk_data_t *)wsp->walk_data;
1670 
1671 	if (pevd->pev_last == NULL)
1672 		return (WALK_DONE);
1673 	if (pevd->pev_node == pevd->pev_last)
1674 		pevd->pev_last = NULL;		/* last round */
1675 
1676 	evp = ((uintptr_t)(((char *)pevd->pev_node) - pevd->pev_offset));
1677 	if (mdb_vread(&ev, sizeof (ev), evp) == -1) {
1678 		mdb_warn("failed to read port_kevent at %p", evp);
1679 		return (WALK_DONE);
1680 	}
1681 	pevd->pev_node = ev.portkev_node.list_next;
1682 	return (wsp->walk_callback(evp, &ev, wsp->walk_cbdata));
1683 }
1684 
1685 void
1686 portev_walk_fini(mdb_walk_state_t *wsp)
1687 {
1688 	portev_walk_data_t *pevd = (portev_walk_data_t *)wsp->walk_data;
1689 
1690 	if (pevd != NULL)
1691 		mdb_free(pevd, sizeof (portev_walk_data_t));
1692 }
1693 
1694 typedef struct proc_walk_data {
1695 	uintptr_t *pw_stack;
1696 	int pw_depth;
1697 	int pw_max;
1698 } proc_walk_data_t;
1699 
1700 int
1701 proc_walk_init(mdb_walk_state_t *wsp)
1702 {
1703 	GElf_Sym sym;
1704 	proc_walk_data_t *pw;
1705 
1706 	if (wsp->walk_addr == NULL) {
1707 		if (mdb_lookup_by_name("p0", &sym) == -1) {
1708 			mdb_warn("failed to read 'practive'");
1709 			return (WALK_ERR);
1710 		}
1711 		wsp->walk_addr = (uintptr_t)sym.st_value;
1712 	}
1713 
1714 	pw = mdb_zalloc(sizeof (proc_walk_data_t), UM_SLEEP);
1715 
1716 	if (mdb_readvar(&pw->pw_max, "nproc") == -1) {
1717 		mdb_warn("failed to read 'nproc'");
1718 		mdb_free(pw, sizeof (pw));
1719 		return (WALK_ERR);
1720 	}
1721 
1722 	pw->pw_stack = mdb_alloc(pw->pw_max * sizeof (uintptr_t), UM_SLEEP);
1723 	wsp->walk_data = pw;
1724 
1725 	return (WALK_NEXT);
1726 }
1727 
1728 int
1729 proc_walk_step(mdb_walk_state_t *wsp)
1730 {
1731 	proc_walk_data_t *pw = wsp->walk_data;
1732 	uintptr_t addr = wsp->walk_addr;
1733 	uintptr_t cld, sib;
1734 
1735 	int status;
1736 	proc_t pr;
1737 
1738 	if (mdb_vread(&pr, sizeof (proc_t), addr) == -1) {
1739 		mdb_warn("failed to read proc at %p", addr);
1740 		return (WALK_DONE);
1741 	}
1742 
1743 	cld = (uintptr_t)pr.p_child;
1744 	sib = (uintptr_t)pr.p_sibling;
1745 
1746 	if (pw->pw_depth > 0 && addr == pw->pw_stack[pw->pw_depth - 1]) {
1747 		pw->pw_depth--;
1748 		goto sib;
1749 	}
1750 
1751 	status = wsp->walk_callback(addr, &pr, wsp->walk_cbdata);
1752 
1753 	if (status != WALK_NEXT)
1754 		return (status);
1755 
1756 	if ((wsp->walk_addr = cld) != NULL) {
1757 		if (mdb_vread(&pr, sizeof (proc_t), cld) == -1) {
1758 			mdb_warn("proc %p has invalid p_child %p; skipping\n",
1759 			    addr, cld);
1760 			goto sib;
1761 		}
1762 
1763 		pw->pw_stack[pw->pw_depth++] = addr;
1764 
1765 		if (pw->pw_depth == pw->pw_max) {
1766 			mdb_warn("depth %d exceeds max depth; try again\n",
1767 			    pw->pw_depth);
1768 			return (WALK_DONE);
1769 		}
1770 		return (WALK_NEXT);
1771 	}
1772 
1773 sib:
1774 	/*
1775 	 * We know that p0 has no siblings, and if another starting proc
1776 	 * was given, we don't want to walk its siblings anyway.
1777 	 */
1778 	if (pw->pw_depth == 0)
1779 		return (WALK_DONE);
1780 
1781 	if (sib != NULL && mdb_vread(&pr, sizeof (proc_t), sib) == -1) {
1782 		mdb_warn("proc %p has invalid p_sibling %p; skipping\n",
1783 		    addr, sib);
1784 		sib = NULL;
1785 	}
1786 
1787 	if ((wsp->walk_addr = sib) == NULL) {
1788 		if (pw->pw_depth > 0) {
1789 			wsp->walk_addr = pw->pw_stack[pw->pw_depth - 1];
1790 			return (WALK_NEXT);
1791 		}
1792 		return (WALK_DONE);
1793 	}
1794 
1795 	return (WALK_NEXT);
1796 }
1797 
1798 void
1799 proc_walk_fini(mdb_walk_state_t *wsp)
1800 {
1801 	proc_walk_data_t *pw = wsp->walk_data;
1802 
1803 	mdb_free(pw->pw_stack, pw->pw_max * sizeof (uintptr_t));
1804 	mdb_free(pw, sizeof (proc_walk_data_t));
1805 }
1806 
1807 int
1808 task_walk_init(mdb_walk_state_t *wsp)
1809 {
1810 	task_t task;
1811 
1812 	if (mdb_vread(&task, sizeof (task_t), wsp->walk_addr) == -1) {
1813 		mdb_warn("failed to read task at %p", wsp->walk_addr);
1814 		return (WALK_ERR);
1815 	}
1816 	wsp->walk_addr = (uintptr_t)task.tk_memb_list;
1817 	wsp->walk_data = task.tk_memb_list;
1818 	return (WALK_NEXT);
1819 }
1820 
1821 int
1822 task_walk_step(mdb_walk_state_t *wsp)
1823 {
1824 	proc_t proc;
1825 	int status;
1826 
1827 	if (mdb_vread(&proc, sizeof (proc_t), wsp->walk_addr) == -1) {
1828 		mdb_warn("failed to read proc at %p", wsp->walk_addr);
1829 		return (WALK_DONE);
1830 	}
1831 
1832 	status = wsp->walk_callback(wsp->walk_addr, NULL, wsp->walk_cbdata);
1833 
1834 	if (proc.p_tasknext == wsp->walk_data)
1835 		return (WALK_DONE);
1836 
1837 	wsp->walk_addr = (uintptr_t)proc.p_tasknext;
1838 	return (status);
1839 }
1840 
1841 int
1842 project_walk_init(mdb_walk_state_t *wsp)
1843 {
1844 	if (wsp->walk_addr == NULL) {
1845 		if (mdb_readvar(&wsp->walk_addr, "proj0p") == -1) {
1846 			mdb_warn("failed to read 'proj0p'");
1847 			return (WALK_ERR);
1848 		}
1849 	}
1850 	wsp->walk_data = (void *)wsp->walk_addr;
1851 	return (WALK_NEXT);
1852 }
1853 
1854 int
1855 project_walk_step(mdb_walk_state_t *wsp)
1856 {
1857 	uintptr_t addr = wsp->walk_addr;
1858 	kproject_t pj;
1859 	int status;
1860 
1861 	if (mdb_vread(&pj, sizeof (kproject_t), addr) == -1) {
1862 		mdb_warn("failed to read project at %p", addr);
1863 		return (WALK_DONE);
1864 	}
1865 	status = wsp->walk_callback(addr, &pj, wsp->walk_cbdata);
1866 	if (status != WALK_NEXT)
1867 		return (status);
1868 	wsp->walk_addr = (uintptr_t)pj.kpj_next;
1869 	if ((void *)wsp->walk_addr == wsp->walk_data)
1870 		return (WALK_DONE);
1871 	return (WALK_NEXT);
1872 }
1873 
1874 static int
1875 generic_walk_step(mdb_walk_state_t *wsp)
1876 {
1877 	return (wsp->walk_callback(wsp->walk_addr, wsp->walk_layer,
1878 	    wsp->walk_cbdata));
1879 }
1880 
1881 int
1882 seg_walk_init(mdb_walk_state_t *wsp)
1883 {
1884 	if (wsp->walk_addr == NULL) {
1885 		mdb_warn("seg walk must begin at struct as *\n");
1886 		return (WALK_ERR);
1887 	}
1888 
1889 	/*
1890 	 * this is really just a wrapper to AVL tree walk
1891 	 */
1892 	wsp->walk_addr = (uintptr_t)&((struct as *)wsp->walk_addr)->a_segtree;
1893 	return (avl_walk_init(wsp));
1894 }
1895 
1896 static int
1897 cpu_walk_cmp(const void *l, const void *r)
1898 {
1899 	uintptr_t lhs = *((uintptr_t *)l);
1900 	uintptr_t rhs = *((uintptr_t *)r);
1901 	cpu_t lcpu, rcpu;
1902 
1903 	(void) mdb_vread(&lcpu, sizeof (lcpu), lhs);
1904 	(void) mdb_vread(&rcpu, sizeof (rcpu), rhs);
1905 
1906 	if (lcpu.cpu_id < rcpu.cpu_id)
1907 		return (-1);
1908 
1909 	if (lcpu.cpu_id > rcpu.cpu_id)
1910 		return (1);
1911 
1912 	return (0);
1913 }
1914 
1915 typedef struct cpu_walk {
1916 	uintptr_t *cw_array;
1917 	int cw_ndx;
1918 } cpu_walk_t;
1919 
1920 int
1921 cpu_walk_init(mdb_walk_state_t *wsp)
1922 {
1923 	cpu_walk_t *cw;
1924 	int max_ncpus, i = 0;
1925 	uintptr_t current, first;
1926 	cpu_t cpu, panic_cpu;
1927 	uintptr_t panicstr, addr;
1928 	GElf_Sym sym;
1929 
1930 	cw = mdb_zalloc(sizeof (cpu_walk_t), UM_SLEEP | UM_GC);
1931 
1932 	if (mdb_readvar(&max_ncpus, "max_ncpus") == -1) {
1933 		mdb_warn("failed to read 'max_ncpus'");
1934 		return (WALK_ERR);
1935 	}
1936 
1937 	if (mdb_readvar(&panicstr, "panicstr") == -1) {
1938 		mdb_warn("failed to read 'panicstr'");
1939 		return (WALK_ERR);
1940 	}
1941 
1942 	if (panicstr != NULL) {
1943 		if (mdb_lookup_by_name("panic_cpu", &sym) == -1) {
1944 			mdb_warn("failed to find 'panic_cpu'");
1945 			return (WALK_ERR);
1946 		}
1947 
1948 		addr = (uintptr_t)sym.st_value;
1949 
1950 		if (mdb_vread(&panic_cpu, sizeof (cpu_t), addr) == -1) {
1951 			mdb_warn("failed to read 'panic_cpu'");
1952 			return (WALK_ERR);
1953 		}
1954 	}
1955 
1956 	/*
1957 	 * Unfortunately, there is no platform-independent way to walk
1958 	 * CPUs in ID order.  We therefore loop through in cpu_next order,
1959 	 * building an array of CPU pointers which will subsequently be
1960 	 * sorted.
1961 	 */
1962 	cw->cw_array =
1963 	    mdb_zalloc((max_ncpus + 1) * sizeof (uintptr_t), UM_SLEEP | UM_GC);
1964 
1965 	if (mdb_readvar(&first, "cpu_list") == -1) {
1966 		mdb_warn("failed to read 'cpu_list'");
1967 		return (WALK_ERR);
1968 	}
1969 
1970 	current = first;
1971 	do {
1972 		if (mdb_vread(&cpu, sizeof (cpu), current) == -1) {
1973 			mdb_warn("failed to read cpu at %p", current);
1974 			return (WALK_ERR);
1975 		}
1976 
1977 		if (panicstr != NULL && panic_cpu.cpu_id == cpu.cpu_id) {
1978 			cw->cw_array[i++] = addr;
1979 		} else {
1980 			cw->cw_array[i++] = current;
1981 		}
1982 	} while ((current = (uintptr_t)cpu.cpu_next) != first);
1983 
1984 	qsort(cw->cw_array, i, sizeof (uintptr_t), cpu_walk_cmp);
1985 	wsp->walk_data = cw;
1986 
1987 	return (WALK_NEXT);
1988 }
1989 
1990 int
1991 cpu_walk_step(mdb_walk_state_t *wsp)
1992 {
1993 	cpu_walk_t *cw = wsp->walk_data;
1994 	cpu_t cpu;
1995 	uintptr_t addr = cw->cw_array[cw->cw_ndx++];
1996 
1997 	if (addr == NULL)
1998 		return (WALK_DONE);
1999 
2000 	if (mdb_vread(&cpu, sizeof (cpu), addr) == -1) {
2001 		mdb_warn("failed to read cpu at %p", addr);
2002 		return (WALK_DONE);
2003 	}
2004 
2005 	return (wsp->walk_callback(addr, &cpu, wsp->walk_cbdata));
2006 }
2007 
2008 typedef struct cpuinfo_data {
2009 	intptr_t cid_cpu;
2010 	uintptr_t cid_lbolt;
2011 	uintptr_t **cid_ithr;
2012 	char	cid_print_head;
2013 	char	cid_print_thr;
2014 	char	cid_print_ithr;
2015 	char	cid_print_flags;
2016 } cpuinfo_data_t;
2017 
2018 int
2019 cpuinfo_walk_ithread(uintptr_t addr, const kthread_t *thr, cpuinfo_data_t *cid)
2020 {
2021 	cpu_t c;
2022 	int id;
2023 	uint8_t pil;
2024 
2025 	if (!(thr->t_flag & T_INTR_THREAD) || thr->t_state == TS_FREE)
2026 		return (WALK_NEXT);
2027 
2028 	if (thr->t_bound_cpu == NULL) {
2029 		mdb_warn("thr %p is intr thread w/out a CPU\n", addr);
2030 		return (WALK_NEXT);
2031 	}
2032 
2033 	(void) mdb_vread(&c, sizeof (c), (uintptr_t)thr->t_bound_cpu);
2034 
2035 	if ((id = c.cpu_id) >= NCPU) {
2036 		mdb_warn("CPU %p has id (%d) greater than NCPU (%d)\n",
2037 		    thr->t_bound_cpu, id, NCPU);
2038 		return (WALK_NEXT);
2039 	}
2040 
2041 	if ((pil = thr->t_pil) >= NINTR) {
2042 		mdb_warn("thread %p has pil (%d) greater than %d\n",
2043 		    addr, pil, NINTR);
2044 		return (WALK_NEXT);
2045 	}
2046 
2047 	if (cid->cid_ithr[id][pil] != NULL) {
2048 		mdb_warn("CPU %d has multiple threads at pil %d (at least "
2049 		    "%p and %p)\n", id, pil, addr, cid->cid_ithr[id][pil]);
2050 		return (WALK_NEXT);
2051 	}
2052 
2053 	cid->cid_ithr[id][pil] = addr;
2054 
2055 	return (WALK_NEXT);
2056 }
2057 
2058 #define	CPUINFO_IDWIDTH		3
2059 #define	CPUINFO_FLAGWIDTH	9
2060 
2061 #ifdef _LP64
2062 #if defined(__amd64)
2063 #define	CPUINFO_TWIDTH		16
2064 #define	CPUINFO_CPUWIDTH	16
2065 #else
2066 #define	CPUINFO_CPUWIDTH	11
2067 #define	CPUINFO_TWIDTH		11
2068 #endif
2069 #else
2070 #define	CPUINFO_CPUWIDTH	8
2071 #define	CPUINFO_TWIDTH		8
2072 #endif
2073 
2074 #define	CPUINFO_THRDELT		(CPUINFO_IDWIDTH + CPUINFO_CPUWIDTH + 9)
2075 #define	CPUINFO_FLAGDELT	(CPUINFO_IDWIDTH + CPUINFO_CPUWIDTH + 4)
2076 #define	CPUINFO_ITHRDELT	4
2077 
2078 #define	CPUINFO_INDENT	mdb_printf("%*s", CPUINFO_THRDELT, \
2079     flagline < nflaglines ? flagbuf[flagline++] : "")
2080 
2081 int
2082 cpuinfo_walk_cpu(uintptr_t addr, const cpu_t *cpu, cpuinfo_data_t *cid)
2083 {
2084 	kthread_t t;
2085 	disp_t disp;
2086 	proc_t p;
2087 	uintptr_t pinned;
2088 	char **flagbuf;
2089 	int nflaglines = 0, flagline = 0, bspl, rval = WALK_NEXT;
2090 
2091 	const char *flags[] = {
2092 	    "RUNNING", "READY", "QUIESCED", "EXISTS",
2093 	    "ENABLE", "OFFLINE", "POWEROFF", "FROZEN",
2094 	    "SPARE", "FAULTED", NULL
2095 	};
2096 
2097 	if (cid->cid_cpu != -1) {
2098 		if (addr != cid->cid_cpu && cpu->cpu_id != cid->cid_cpu)
2099 			return (WALK_NEXT);
2100 
2101 		/*
2102 		 * Set cid_cpu to -1 to indicate that we found a matching CPU.
2103 		 */
2104 		cid->cid_cpu = -1;
2105 		rval = WALK_DONE;
2106 	}
2107 
2108 	if (cid->cid_print_head) {
2109 		mdb_printf("%3s %-*s %3s %4s %4s %3s %4s %5s %-6s %-*s %s\n",
2110 		    "ID", CPUINFO_CPUWIDTH, "ADDR", "FLG", "NRUN", "BSPL",
2111 		    "PRI", "RNRN", "KRNRN", "SWITCH", CPUINFO_TWIDTH, "THREAD",
2112 		    "PROC");
2113 		cid->cid_print_head = FALSE;
2114 	}
2115 
2116 	bspl = cpu->cpu_base_spl;
2117 
2118 	if (mdb_vread(&disp, sizeof (disp_t), (uintptr_t)cpu->cpu_disp) == -1) {
2119 		mdb_warn("failed to read disp_t at %p", cpu->cpu_disp);
2120 		return (WALK_ERR);
2121 	}
2122 
2123 	mdb_printf("%3d %0*p %3x %4d %4d ",
2124 	    cpu->cpu_id, CPUINFO_CPUWIDTH, addr, cpu->cpu_flags,
2125 	    disp.disp_nrunnable, bspl);
2126 
2127 	if (mdb_vread(&t, sizeof (t), (uintptr_t)cpu->cpu_thread) != -1) {
2128 		mdb_printf("%3d ", t.t_pri);
2129 	} else {
2130 		mdb_printf("%3s ", "-");
2131 	}
2132 
2133 	mdb_printf("%4s %5s ", cpu->cpu_runrun ? "yes" : "no",
2134 	    cpu->cpu_kprunrun ? "yes" : "no");
2135 
2136 	if (cpu->cpu_last_swtch) {
2137 		clock_t lbolt;
2138 
2139 		if (mdb_vread(&lbolt, sizeof (lbolt), cid->cid_lbolt) == -1) {
2140 			mdb_warn("failed to read lbolt at %p", cid->cid_lbolt);
2141 			return (WALK_ERR);
2142 		}
2143 		mdb_printf("t-%-4d ", lbolt - cpu->cpu_last_swtch);
2144 	} else {
2145 		mdb_printf("%-6s ", "-");
2146 	}
2147 
2148 	mdb_printf("%0*p", CPUINFO_TWIDTH, cpu->cpu_thread);
2149 
2150 	if (cpu->cpu_thread == cpu->cpu_idle_thread)
2151 		mdb_printf(" (idle)\n");
2152 	else if (cpu->cpu_thread == NULL)
2153 		mdb_printf(" -\n");
2154 	else {
2155 		if (mdb_vread(&p, sizeof (p), (uintptr_t)t.t_procp) != -1) {
2156 			mdb_printf(" %s\n", p.p_user.u_comm);
2157 		} else {
2158 			mdb_printf(" ?\n");
2159 		}
2160 	}
2161 
2162 	flagbuf = mdb_zalloc(sizeof (flags), UM_SLEEP | UM_GC);
2163 
2164 	if (cid->cid_print_flags) {
2165 		int first = 1, i, j, k;
2166 		char *s;
2167 
2168 		cid->cid_print_head = TRUE;
2169 
2170 		for (i = 1, j = 0; flags[j] != NULL; i <<= 1, j++) {
2171 			if (!(cpu->cpu_flags & i))
2172 				continue;
2173 
2174 			if (first) {
2175 				s = mdb_alloc(CPUINFO_THRDELT + 1,
2176 				    UM_GC | UM_SLEEP);
2177 
2178 				(void) mdb_snprintf(s, CPUINFO_THRDELT + 1,
2179 				    "%*s|%*s", CPUINFO_FLAGDELT, "",
2180 				    CPUINFO_THRDELT - 1 - CPUINFO_FLAGDELT, "");
2181 				flagbuf[nflaglines++] = s;
2182 			}
2183 
2184 			s = mdb_alloc(CPUINFO_THRDELT + 1, UM_GC | UM_SLEEP);
2185 			(void) mdb_snprintf(s, CPUINFO_THRDELT + 1, "%*s%*s %s",
2186 			    CPUINFO_IDWIDTH + CPUINFO_CPUWIDTH -
2187 			    CPUINFO_FLAGWIDTH, "", CPUINFO_FLAGWIDTH, flags[j],
2188 			    first ? "<--+" : "");
2189 
2190 			for (k = strlen(s); k < CPUINFO_THRDELT; k++)
2191 				s[k] = ' ';
2192 			s[k] = '\0';
2193 
2194 			flagbuf[nflaglines++] = s;
2195 			first = 0;
2196 		}
2197 	}
2198 
2199 	if (cid->cid_print_ithr) {
2200 		int i, found_one = FALSE;
2201 		int print_thr = disp.disp_nrunnable && cid->cid_print_thr;
2202 
2203 		for (i = NINTR - 1; i >= 0; i--) {
2204 			uintptr_t iaddr = cid->cid_ithr[cpu->cpu_id][i];
2205 
2206 			if (iaddr == NULL)
2207 				continue;
2208 
2209 			if (!found_one) {
2210 				found_one = TRUE;
2211 
2212 				CPUINFO_INDENT;
2213 				mdb_printf("%c%*s|\n", print_thr ? '|' : ' ',
2214 				    CPUINFO_ITHRDELT, "");
2215 
2216 				CPUINFO_INDENT;
2217 				mdb_printf("%c%*s+--> %3s %s\n",
2218 				    print_thr ? '|' : ' ', CPUINFO_ITHRDELT,
2219 				    "", "PIL", "THREAD");
2220 			}
2221 
2222 			if (mdb_vread(&t, sizeof (t), iaddr) == -1) {
2223 				mdb_warn("failed to read kthread_t at %p",
2224 				    iaddr);
2225 				return (WALK_ERR);
2226 			}
2227 
2228 			CPUINFO_INDENT;
2229 			mdb_printf("%c%*s     %3d %0*p\n",
2230 			    print_thr ? '|' : ' ', CPUINFO_ITHRDELT, "",
2231 			    t.t_pil, CPUINFO_TWIDTH, iaddr);
2232 
2233 			pinned = (uintptr_t)t.t_intr;
2234 		}
2235 
2236 		if (found_one && pinned != NULL) {
2237 			cid->cid_print_head = TRUE;
2238 			(void) strcpy(p.p_user.u_comm, "?");
2239 
2240 			if (mdb_vread(&t, sizeof (t),
2241 			    (uintptr_t)pinned) == -1) {
2242 				mdb_warn("failed to read kthread_t at %p",
2243 				    pinned);
2244 				return (WALK_ERR);
2245 			}
2246 			if (mdb_vread(&p, sizeof (p),
2247 			    (uintptr_t)t.t_procp) == -1) {
2248 				mdb_warn("failed to read proc_t at %p",
2249 				    t.t_procp);
2250 				return (WALK_ERR);
2251 			}
2252 
2253 			CPUINFO_INDENT;
2254 			mdb_printf("%c%*s     %3s %0*p %s\n",
2255 			    print_thr ? '|' : ' ', CPUINFO_ITHRDELT, "", "-",
2256 			    CPUINFO_TWIDTH, pinned,
2257 			    pinned == (uintptr_t)cpu->cpu_idle_thread ?
2258 			    "(idle)" : p.p_user.u_comm);
2259 		}
2260 	}
2261 
2262 	if (disp.disp_nrunnable && cid->cid_print_thr) {
2263 		dispq_t *dq;
2264 
2265 		int i, npri = disp.disp_npri;
2266 
2267 		dq = mdb_alloc(sizeof (dispq_t) * npri, UM_SLEEP | UM_GC);
2268 
2269 		if (mdb_vread(dq, sizeof (dispq_t) * npri,
2270 		    (uintptr_t)disp.disp_q) == -1) {
2271 			mdb_warn("failed to read dispq_t at %p", disp.disp_q);
2272 			return (WALK_ERR);
2273 		}
2274 
2275 		CPUINFO_INDENT;
2276 		mdb_printf("|\n");
2277 
2278 		CPUINFO_INDENT;
2279 		mdb_printf("+-->  %3s %-*s %s\n", "PRI",
2280 		    CPUINFO_TWIDTH, "THREAD", "PROC");
2281 
2282 		for (i = npri - 1; i >= 0; i--) {
2283 			uintptr_t taddr = (uintptr_t)dq[i].dq_first;
2284 
2285 			while (taddr != NULL) {
2286 				if (mdb_vread(&t, sizeof (t), taddr) == -1) {
2287 					mdb_warn("failed to read kthread_t "
2288 					    "at %p", taddr);
2289 					return (WALK_ERR);
2290 				}
2291 				if (mdb_vread(&p, sizeof (p),
2292 				    (uintptr_t)t.t_procp) == -1) {
2293 					mdb_warn("failed to read proc_t at %p",
2294 					    t.t_procp);
2295 					return (WALK_ERR);
2296 				}
2297 
2298 				CPUINFO_INDENT;
2299 				mdb_printf("      %3d %0*p %s\n", t.t_pri,
2300 				    CPUINFO_TWIDTH, taddr, p.p_user.u_comm);
2301 
2302 				taddr = (uintptr_t)t.t_link;
2303 			}
2304 		}
2305 		cid->cid_print_head = TRUE;
2306 	}
2307 
2308 	while (flagline < nflaglines)
2309 		mdb_printf("%s\n", flagbuf[flagline++]);
2310 
2311 	if (cid->cid_print_head)
2312 		mdb_printf("\n");
2313 
2314 	return (rval);
2315 }
2316 
2317 int
2318 cpuinfo(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2319 {
2320 	uint_t verbose = FALSE;
2321 	cpuinfo_data_t cid;
2322 	GElf_Sym sym;
2323 	clock_t lbolt;
2324 
2325 	cid.cid_print_ithr = FALSE;
2326 	cid.cid_print_thr = FALSE;
2327 	cid.cid_print_flags = FALSE;
2328 	cid.cid_print_head = DCMD_HDRSPEC(flags) ? TRUE : FALSE;
2329 	cid.cid_cpu = -1;
2330 
2331 	if (flags & DCMD_ADDRSPEC)
2332 		cid.cid_cpu = addr;
2333 
2334 	if (mdb_getopts(argc, argv,
2335 	    'v', MDB_OPT_SETBITS, TRUE, &verbose, NULL) != argc)
2336 		return (DCMD_USAGE);
2337 
2338 	if (verbose) {
2339 		cid.cid_print_ithr = TRUE;
2340 		cid.cid_print_thr = TRUE;
2341 		cid.cid_print_flags = TRUE;
2342 		cid.cid_print_head = TRUE;
2343 	}
2344 
2345 	if (cid.cid_print_ithr) {
2346 		int i;
2347 
2348 		cid.cid_ithr = mdb_alloc(sizeof (uintptr_t **)
2349 		    * NCPU, UM_SLEEP | UM_GC);
2350 
2351 		for (i = 0; i < NCPU; i++)
2352 			cid.cid_ithr[i] = mdb_zalloc(sizeof (uintptr_t *) *
2353 			    NINTR, UM_SLEEP | UM_GC);
2354 
2355 		if (mdb_walk("thread", (mdb_walk_cb_t)cpuinfo_walk_ithread,
2356 		    &cid) == -1) {
2357 			mdb_warn("couldn't walk thread");
2358 			return (DCMD_ERR);
2359 		}
2360 	}
2361 
2362 	if (mdb_lookup_by_name("panic_lbolt", &sym) == -1) {
2363 		mdb_warn("failed to find panic_lbolt");
2364 		return (DCMD_ERR);
2365 	}
2366 
2367 	cid.cid_lbolt = (uintptr_t)sym.st_value;
2368 
2369 	if (mdb_vread(&lbolt, sizeof (lbolt), cid.cid_lbolt) == -1) {
2370 		mdb_warn("failed to read panic_lbolt");
2371 		return (DCMD_ERR);
2372 	}
2373 
2374 	if (lbolt == 0) {
2375 		if (mdb_lookup_by_name("lbolt", &sym) == -1) {
2376 			mdb_warn("failed to find lbolt");
2377 			return (DCMD_ERR);
2378 		}
2379 		cid.cid_lbolt = (uintptr_t)sym.st_value;
2380 	}
2381 
2382 	if (mdb_walk("cpu", (mdb_walk_cb_t)cpuinfo_walk_cpu, &cid) == -1) {
2383 		mdb_warn("can't walk cpus");
2384 		return (DCMD_ERR);
2385 	}
2386 
2387 	if (cid.cid_cpu != -1) {
2388 		/*
2389 		 * We didn't find this CPU when we walked through the CPUs
2390 		 * (i.e. the address specified doesn't show up in the "cpu"
2391 		 * walk).  However, the specified address may still correspond
2392 		 * to a valid cpu_t (for example, if the specified address is
2393 		 * the actual panicking cpu_t and not the cached panic_cpu).
2394 		 * Point is:  even if we didn't find it, we still want to try
2395 		 * to print the specified address as a cpu_t.
2396 		 */
2397 		cpu_t cpu;
2398 
2399 		if (mdb_vread(&cpu, sizeof (cpu), cid.cid_cpu) == -1) {
2400 			mdb_warn("%p is neither a valid CPU ID nor a "
2401 			    "valid cpu_t address\n", cid.cid_cpu);
2402 			return (DCMD_ERR);
2403 		}
2404 
2405 		(void) cpuinfo_walk_cpu(cid.cid_cpu, &cpu, &cid);
2406 	}
2407 
2408 	return (DCMD_OK);
2409 }
2410 
2411 /*ARGSUSED*/
2412 int
2413 flipone(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2414 {
2415 	int i;
2416 
2417 	if (!(flags & DCMD_ADDRSPEC))
2418 		return (DCMD_USAGE);
2419 
2420 	for (i = 0; i < sizeof (addr) * NBBY; i++)
2421 		mdb_printf("%p\n", addr ^ (1UL << i));
2422 
2423 	return (DCMD_OK);
2424 }
2425 
2426 /*
2427  * Grumble, grumble.
2428  */
2429 #define	SMAP_HASHFUNC(vp, off)	\
2430 	((((uintptr_t)(vp) >> 6) + ((uintptr_t)(vp) >> 3) + \
2431 	((off) >> MAXBSHIFT)) & smd_hashmsk)
2432 
2433 int
2434 vnode2smap(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2435 {
2436 	long smd_hashmsk;
2437 	int hash;
2438 	uintptr_t offset = 0;
2439 	struct smap smp;
2440 	uintptr_t saddr, kaddr;
2441 	uintptr_t smd_hash, smd_smap;
2442 	struct seg seg;
2443 
2444 	if (!(flags & DCMD_ADDRSPEC))
2445 		return (DCMD_USAGE);
2446 
2447 	if (mdb_readvar(&smd_hashmsk, "smd_hashmsk") == -1) {
2448 		mdb_warn("failed to read smd_hashmsk");
2449 		return (DCMD_ERR);
2450 	}
2451 
2452 	if (mdb_readvar(&smd_hash, "smd_hash") == -1) {
2453 		mdb_warn("failed to read smd_hash");
2454 		return (DCMD_ERR);
2455 	}
2456 
2457 	if (mdb_readvar(&smd_smap, "smd_smap") == -1) {
2458 		mdb_warn("failed to read smd_hash");
2459 		return (DCMD_ERR);
2460 	}
2461 
2462 	if (mdb_readvar(&kaddr, "segkmap") == -1) {
2463 		mdb_warn("failed to read segkmap");
2464 		return (DCMD_ERR);
2465 	}
2466 
2467 	if (mdb_vread(&seg, sizeof (seg), kaddr) == -1) {
2468 		mdb_warn("failed to read segkmap at %p", kaddr);
2469 		return (DCMD_ERR);
2470 	}
2471 
2472 	if (argc != 0) {
2473 		const mdb_arg_t *arg = &argv[0];
2474 
2475 		if (arg->a_type == MDB_TYPE_IMMEDIATE)
2476 			offset = arg->a_un.a_val;
2477 		else
2478 			offset = (uintptr_t)mdb_strtoull(arg->a_un.a_str);
2479 	}
2480 
2481 	hash = SMAP_HASHFUNC(addr, offset);
2482 
2483 	if (mdb_vread(&saddr, sizeof (saddr),
2484 	    smd_hash + hash * sizeof (uintptr_t)) == -1) {
2485 		mdb_warn("couldn't read smap at %p",
2486 		    smd_hash + hash * sizeof (uintptr_t));
2487 		return (DCMD_ERR);
2488 	}
2489 
2490 	do {
2491 		if (mdb_vread(&smp, sizeof (smp), saddr) == -1) {
2492 			mdb_warn("couldn't read smap at %p", saddr);
2493 			return (DCMD_ERR);
2494 		}
2495 
2496 		if ((uintptr_t)smp.sm_vp == addr && smp.sm_off == offset) {
2497 			mdb_printf("vnode %p, offs %p is smap %p, vaddr %p\n",
2498 			    addr, offset, saddr, ((saddr - smd_smap) /
2499 			    sizeof (smp)) * MAXBSIZE + seg.s_base);
2500 			return (DCMD_OK);
2501 		}
2502 
2503 		saddr = (uintptr_t)smp.sm_hash;
2504 	} while (saddr != NULL);
2505 
2506 	mdb_printf("no smap for vnode %p, offs %p\n", addr, offset);
2507 	return (DCMD_OK);
2508 }
2509 
2510 /*ARGSUSED*/
2511 int
2512 addr2smap(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2513 {
2514 	uintptr_t kaddr;
2515 	struct seg seg;
2516 	struct segmap_data sd;
2517 
2518 	if (!(flags & DCMD_ADDRSPEC))
2519 		return (DCMD_USAGE);
2520 
2521 	if (mdb_readvar(&kaddr, "segkmap") == -1) {
2522 		mdb_warn("failed to read segkmap");
2523 		return (DCMD_ERR);
2524 	}
2525 
2526 	if (mdb_vread(&seg, sizeof (seg), kaddr) == -1) {
2527 		mdb_warn("failed to read segkmap at %p", kaddr);
2528 		return (DCMD_ERR);
2529 	}
2530 
2531 	if (mdb_vread(&sd, sizeof (sd), (uintptr_t)seg.s_data) == -1) {
2532 		mdb_warn("failed to read segmap_data at %p", seg.s_data);
2533 		return (DCMD_ERR);
2534 	}
2535 
2536 	mdb_printf("%p is smap %p\n", addr,
2537 	    ((addr - (uintptr_t)seg.s_base) >> MAXBSHIFT) *
2538 	    sizeof (struct smap) + (uintptr_t)sd.smd_sm);
2539 
2540 	return (DCMD_OK);
2541 }
2542 
2543 int
2544 as2proc_walk(uintptr_t addr, const proc_t *p, struct as **asp)
2545 {
2546 	if (p->p_as == *asp)
2547 		mdb_printf("%p\n", addr);
2548 	return (WALK_NEXT);
2549 }
2550 
2551 /*ARGSUSED*/
2552 int
2553 as2proc(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2554 {
2555 	if (!(flags & DCMD_ADDRSPEC) || argc != 0)
2556 		return (DCMD_USAGE);
2557 
2558 	if (mdb_walk("proc", (mdb_walk_cb_t)as2proc_walk, &addr) == -1) {
2559 		mdb_warn("failed to walk proc");
2560 		return (DCMD_ERR);
2561 	}
2562 
2563 	return (DCMD_OK);
2564 }
2565 
2566 /*ARGSUSED*/
2567 int
2568 ptree_walk(uintptr_t addr, const proc_t *p, void *ignored)
2569 {
2570 	proc_t parent;
2571 	int ident = 0;
2572 	uintptr_t paddr;
2573 
2574 	for (paddr = (uintptr_t)p->p_parent; paddr != NULL; ident += 5) {
2575 		mdb_vread(&parent, sizeof (parent), paddr);
2576 		paddr = (uintptr_t)parent.p_parent;
2577 	}
2578 
2579 	mdb_inc_indent(ident);
2580 	mdb_printf("%0?p  %s\n", addr, p->p_user.u_comm);
2581 	mdb_dec_indent(ident);
2582 
2583 	return (WALK_NEXT);
2584 }
2585 
2586 void
2587 ptree_ancestors(uintptr_t addr, uintptr_t start)
2588 {
2589 	proc_t p;
2590 
2591 	if (mdb_vread(&p, sizeof (p), addr) == -1) {
2592 		mdb_warn("couldn't read ancestor at %p", addr);
2593 		return;
2594 	}
2595 
2596 	if (p.p_parent != NULL)
2597 		ptree_ancestors((uintptr_t)p.p_parent, start);
2598 
2599 	if (addr != start)
2600 		(void) ptree_walk(addr, &p, NULL);
2601 }
2602 
2603 /*ARGSUSED*/
2604 int
2605 ptree(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2606 {
2607 	if (!(flags & DCMD_ADDRSPEC))
2608 		addr = NULL;
2609 	else
2610 		ptree_ancestors(addr, addr);
2611 
2612 	if (mdb_pwalk("proc", (mdb_walk_cb_t)ptree_walk, NULL, addr) == -1) {
2613 		mdb_warn("couldn't walk 'proc'");
2614 		return (DCMD_ERR);
2615 	}
2616 
2617 	return (DCMD_OK);
2618 }
2619 
2620 /*ARGSUSED*/
2621 static int
2622 fd(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2623 {
2624 	int fdnum;
2625 	const mdb_arg_t *argp = &argv[0];
2626 	proc_t p;
2627 	uf_entry_t uf;
2628 
2629 	if ((flags & DCMD_ADDRSPEC) == 0) {
2630 		mdb_warn("fd doesn't give global information\n");
2631 		return (DCMD_ERR);
2632 	}
2633 	if (argc != 1)
2634 		return (DCMD_USAGE);
2635 
2636 	if (argp->a_type == MDB_TYPE_IMMEDIATE)
2637 		fdnum = argp->a_un.a_val;
2638 	else
2639 		fdnum = mdb_strtoull(argp->a_un.a_str);
2640 
2641 	if (mdb_vread(&p, sizeof (struct proc), addr) == -1) {
2642 		mdb_warn("couldn't read proc_t at %p", addr);
2643 		return (DCMD_ERR);
2644 	}
2645 	if (fdnum > p.p_user.u_finfo.fi_nfiles) {
2646 		mdb_warn("process %p only has %d files open.\n",
2647 		    addr, p.p_user.u_finfo.fi_nfiles);
2648 		return (DCMD_ERR);
2649 	}
2650 	if (mdb_vread(&uf, sizeof (uf_entry_t),
2651 	    (uintptr_t)&p.p_user.u_finfo.fi_list[fdnum]) == -1) {
2652 		mdb_warn("couldn't read uf_entry_t at %p",
2653 		    &p.p_user.u_finfo.fi_list[fdnum]);
2654 		return (DCMD_ERR);
2655 	}
2656 
2657 	mdb_printf("%p\n", uf.uf_file);
2658 	return (DCMD_OK);
2659 }
2660 
2661 /*ARGSUSED*/
2662 static int
2663 pid2proc(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2664 {
2665 	pid_t pid = (pid_t)addr;
2666 
2667 	if (argc != 0)
2668 		return (DCMD_USAGE);
2669 
2670 	if ((addr = mdb_pid2proc(pid, NULL)) == NULL) {
2671 		mdb_warn("PID 0t%d not found\n", pid);
2672 		return (DCMD_ERR);
2673 	}
2674 
2675 	mdb_printf("%p\n", addr);
2676 	return (DCMD_OK);
2677 }
2678 
2679 static char *sysfile_cmd[] = {
2680 	"exclude:",
2681 	"include:",
2682 	"forceload:",
2683 	"rootdev:",
2684 	"rootfs:",
2685 	"swapdev:",
2686 	"swapfs:",
2687 	"moddir:",
2688 	"set",
2689 	"unknown",
2690 };
2691 
2692 static char *sysfile_ops[] = { "", "=", "&", "|" };
2693 
2694 /*ARGSUSED*/
2695 static int
2696 sysfile_vmem_seg(uintptr_t addr, const vmem_seg_t *vsp, void **target)
2697 {
2698 	if (vsp->vs_type == VMEM_ALLOC && (void *)vsp->vs_start == *target) {
2699 		*target = NULL;
2700 		return (WALK_DONE);
2701 	}
2702 	return (WALK_NEXT);
2703 }
2704 
2705 /*ARGSUSED*/
2706 static int
2707 sysfile(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2708 {
2709 	struct sysparam *sysp, sys;
2710 	char var[256];
2711 	char modname[256];
2712 	char val[256];
2713 	char strval[256];
2714 	vmem_t *mod_sysfile_arena;
2715 	void *straddr;
2716 
2717 	if (mdb_readvar(&sysp, "sysparam_hd") == -1) {
2718 		mdb_warn("failed to read sysparam_hd");
2719 		return (DCMD_ERR);
2720 	}
2721 
2722 	if (mdb_readvar(&mod_sysfile_arena, "mod_sysfile_arena") == -1) {
2723 		mdb_warn("failed to read mod_sysfile_arena");
2724 		return (DCMD_ERR);
2725 	}
2726 
2727 	while (sysp != NULL) {
2728 		var[0] = '\0';
2729 		val[0] = '\0';
2730 		modname[0] = '\0';
2731 		if (mdb_vread(&sys, sizeof (sys), (uintptr_t)sysp) == -1) {
2732 			mdb_warn("couldn't read sysparam %p", sysp);
2733 			return (DCMD_ERR);
2734 		}
2735 		if (sys.sys_modnam != NULL &&
2736 		    mdb_readstr(modname, 256,
2737 		    (uintptr_t)sys.sys_modnam) == -1) {
2738 			mdb_warn("couldn't read modname in %p", sysp);
2739 			return (DCMD_ERR);
2740 		}
2741 		if (sys.sys_ptr != NULL &&
2742 		    mdb_readstr(var, 256, (uintptr_t)sys.sys_ptr) == -1) {
2743 			mdb_warn("couldn't read ptr in %p", sysp);
2744 			return (DCMD_ERR);
2745 		}
2746 		if (sys.sys_op != SETOP_NONE) {
2747 			/*
2748 			 * Is this an int or a string?  We determine this
2749 			 * by checking whether straddr is contained in
2750 			 * mod_sysfile_arena.  If so, the walker will set
2751 			 * straddr to NULL.
2752 			 */
2753 			straddr = (void *)(uintptr_t)sys.sys_info;
2754 			if (sys.sys_op == SETOP_ASSIGN &&
2755 			    sys.sys_info != 0 &&
2756 			    mdb_pwalk("vmem_seg",
2757 			    (mdb_walk_cb_t)sysfile_vmem_seg, &straddr,
2758 			    (uintptr_t)mod_sysfile_arena) == 0 &&
2759 			    straddr == NULL &&
2760 			    mdb_readstr(strval, 256,
2761 			    (uintptr_t)sys.sys_info) != -1) {
2762 				(void) mdb_snprintf(val, sizeof (val), "\"%s\"",
2763 				    strval);
2764 			} else {
2765 				(void) mdb_snprintf(val, sizeof (val),
2766 				    "0x%llx [0t%llu]", sys.sys_info,
2767 				    sys.sys_info);
2768 			}
2769 		}
2770 		mdb_printf("%s %s%s%s%s%s\n", sysfile_cmd[sys.sys_type],
2771 		    modname, modname[0] == '\0' ? "" : ":",
2772 		    var, sysfile_ops[sys.sys_op], val);
2773 
2774 		sysp = sys.sys_next;
2775 	}
2776 
2777 	return (DCMD_OK);
2778 }
2779 
2780 /*
2781  * Dump a taskq_ent_t given its address.
2782  */
2783 /*ARGSUSED*/
2784 int
2785 taskq_ent(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2786 {
2787 	taskq_ent_t	taskq_ent;
2788 	GElf_Sym	sym;
2789 	char		buf[MDB_SYM_NAMLEN+1];
2790 
2791 
2792 	if (!(flags & DCMD_ADDRSPEC)) {
2793 		mdb_warn("expected explicit taskq_ent_t address before ::\n");
2794 		return (DCMD_USAGE);
2795 	}
2796 
2797 	if (mdb_vread(&taskq_ent, sizeof (taskq_ent_t), addr) == -1) {
2798 		mdb_warn("failed to read taskq_ent_t at %p", addr);
2799 		return (DCMD_ERR);
2800 	}
2801 
2802 	if (DCMD_HDRSPEC(flags)) {
2803 		mdb_printf("%<u>%-?s    %-?s    %-s%</u>\n",
2804 		"ENTRY", "ARG", "FUNCTION");
2805 	}
2806 
2807 	if (mdb_lookup_by_addr((uintptr_t)taskq_ent.tqent_func, MDB_SYM_EXACT,
2808 	    buf, sizeof (buf), &sym) == -1) {
2809 		(void) strcpy(buf, "????");
2810 	}
2811 
2812 	mdb_printf("%-?p    %-?p    %s\n", addr, taskq_ent.tqent_arg, buf);
2813 
2814 	return (DCMD_OK);
2815 }
2816 
2817 /*
2818  * Given the address of the (taskq_t) task queue head, walk the queue listing
2819  * the address of every taskq_ent_t.
2820  */
2821 int
2822 taskq_walk_init(mdb_walk_state_t *wsp)
2823 {
2824 	taskq_t	tq_head;
2825 
2826 
2827 	if (wsp->walk_addr == NULL) {
2828 		mdb_warn("start address required\n");
2829 		return (WALK_ERR);
2830 	}
2831 
2832 
2833 	/*
2834 	 * Save the address of the list head entry.  This terminates the list.
2835 	 */
2836 	wsp->walk_data = (void *)
2837 	    ((size_t)wsp->walk_addr + offsetof(taskq_t, tq_task));
2838 
2839 
2840 	/*
2841 	 * Read in taskq head, set walk_addr to point to first taskq_ent_t.
2842 	 */
2843 	if (mdb_vread((void *)&tq_head, sizeof (taskq_t), wsp->walk_addr) ==
2844 	    -1) {
2845 		mdb_warn("failed to read taskq list head at %p",
2846 		    wsp->walk_addr);
2847 	}
2848 	wsp->walk_addr = (uintptr_t)tq_head.tq_task.tqent_next;
2849 
2850 
2851 	/*
2852 	 * Check for null list (next=head)
2853 	 */
2854 	if (wsp->walk_addr == (uintptr_t)wsp->walk_data) {
2855 		return (WALK_DONE);
2856 	}
2857 
2858 	return (WALK_NEXT);
2859 }
2860 
2861 
2862 int
2863 taskq_walk_step(mdb_walk_state_t *wsp)
2864 {
2865 	taskq_ent_t	tq_ent;
2866 	int		status;
2867 
2868 
2869 	if (mdb_vread((void *)&tq_ent, sizeof (taskq_ent_t), wsp->walk_addr) ==
2870 	    -1) {
2871 		mdb_warn("failed to read taskq_ent_t at %p", wsp->walk_addr);
2872 		return (DCMD_ERR);
2873 	}
2874 
2875 	status = wsp->walk_callback(wsp->walk_addr, (void *)&tq_ent,
2876 	    wsp->walk_cbdata);
2877 
2878 	wsp->walk_addr = (uintptr_t)tq_ent.tqent_next;
2879 
2880 
2881 	/* Check if we're at the last element (next=head) */
2882 	if (wsp->walk_addr == (uintptr_t)wsp->walk_data) {
2883 		return (WALK_DONE);
2884 	}
2885 
2886 	return (status);
2887 }
2888 
2889 int
2890 didmatch(uintptr_t addr, const kthread_t *thr, kt_did_t *didp)
2891 {
2892 
2893 	if (*didp == thr->t_did) {
2894 		mdb_printf("%p\n", addr);
2895 		return (WALK_DONE);
2896 	} else
2897 		return (WALK_NEXT);
2898 }
2899 
2900 /*ARGSUSED*/
2901 int
2902 did2thread(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2903 {
2904 	const mdb_arg_t *argp = &argv[0];
2905 	kt_did_t	did;
2906 
2907 	if (argc != 1)
2908 		return (DCMD_USAGE);
2909 
2910 	did = (kt_did_t)mdb_strtoull(argp->a_un.a_str);
2911 
2912 	if (mdb_walk("thread", (mdb_walk_cb_t)didmatch, (void *)&did) == -1) {
2913 		mdb_warn("failed to walk thread");
2914 		return (DCMD_ERR);
2915 
2916 	}
2917 	return (DCMD_OK);
2918 
2919 }
2920 
2921 static int
2922 errorq_walk_init(mdb_walk_state_t *wsp)
2923 {
2924 	if (wsp->walk_addr == NULL &&
2925 	    mdb_readvar(&wsp->walk_addr, "errorq_list") == -1) {
2926 		mdb_warn("failed to read errorq_list");
2927 		return (WALK_ERR);
2928 	}
2929 
2930 	return (WALK_NEXT);
2931 }
2932 
2933 static int
2934 errorq_walk_step(mdb_walk_state_t *wsp)
2935 {
2936 	uintptr_t addr = wsp->walk_addr;
2937 	errorq_t eq;
2938 
2939 	if (addr == NULL)
2940 		return (WALK_DONE);
2941 
2942 	if (mdb_vread(&eq, sizeof (eq), addr) == -1) {
2943 		mdb_warn("failed to read errorq at %p", addr);
2944 		return (WALK_ERR);
2945 	}
2946 
2947 	wsp->walk_addr = (uintptr_t)eq.eq_next;
2948 	return (wsp->walk_callback(addr, &eq, wsp->walk_cbdata));
2949 }
2950 
2951 typedef struct eqd_walk_data {
2952 	uintptr_t *eqd_stack;
2953 	void *eqd_buf;
2954 	ulong_t eqd_qpos;
2955 	ulong_t eqd_qlen;
2956 	size_t eqd_size;
2957 } eqd_walk_data_t;
2958 
2959 /*
2960  * In order to walk the list of pending error queue elements, we push the
2961  * addresses of the corresponding data buffers in to the eqd_stack array.
2962  * The error lists are in reverse chronological order when iterating using
2963  * eqe_prev, so we then pop things off the top in eqd_walk_step so that the
2964  * walker client gets addresses in order from oldest error to newest error.
2965  */
2966 static void
2967 eqd_push_list(eqd_walk_data_t *eqdp, uintptr_t addr)
2968 {
2969 	errorq_elem_t eqe;
2970 
2971 	while (addr != NULL) {
2972 		if (mdb_vread(&eqe, sizeof (eqe), addr) != sizeof (eqe)) {
2973 			mdb_warn("failed to read errorq element at %p", addr);
2974 			break;
2975 		}
2976 
2977 		if (eqdp->eqd_qpos == eqdp->eqd_qlen) {
2978 			mdb_warn("errorq is overfull -- more than %lu "
2979 			    "elems found\n", eqdp->eqd_qlen);
2980 			break;
2981 		}
2982 
2983 		eqdp->eqd_stack[eqdp->eqd_qpos++] = (uintptr_t)eqe.eqe_data;
2984 		addr = (uintptr_t)eqe.eqe_prev;
2985 	}
2986 }
2987 
2988 static int
2989 eqd_walk_init(mdb_walk_state_t *wsp)
2990 {
2991 	eqd_walk_data_t *eqdp;
2992 	errorq_elem_t eqe, *addr;
2993 	errorq_t eq;
2994 	ulong_t i;
2995 
2996 	if (mdb_vread(&eq, sizeof (eq), wsp->walk_addr) == -1) {
2997 		mdb_warn("failed to read errorq at %p", wsp->walk_addr);
2998 		return (WALK_ERR);
2999 	}
3000 
3001 	if (eq.eq_ptail != NULL &&
3002 	    mdb_vread(&eqe, sizeof (eqe), (uintptr_t)eq.eq_ptail) == -1) {
3003 		mdb_warn("failed to read errorq element at %p", eq.eq_ptail);
3004 		return (WALK_ERR);
3005 	}
3006 
3007 	eqdp = mdb_alloc(sizeof (eqd_walk_data_t), UM_SLEEP);
3008 	wsp->walk_data = eqdp;
3009 
3010 	eqdp->eqd_stack = mdb_zalloc(sizeof (uintptr_t) * eq.eq_qlen, UM_SLEEP);
3011 	eqdp->eqd_buf = mdb_alloc(eq.eq_size, UM_SLEEP);
3012 	eqdp->eqd_qlen = eq.eq_qlen;
3013 	eqdp->eqd_qpos = 0;
3014 	eqdp->eqd_size = eq.eq_size;
3015 
3016 	/*
3017 	 * The newest elements in the queue are on the pending list, so we
3018 	 * push those on to our stack first.
3019 	 */
3020 	eqd_push_list(eqdp, (uintptr_t)eq.eq_pend);
3021 
3022 	/*
3023 	 * If eq_ptail is set, it may point to a subset of the errors on the
3024 	 * pending list in the event a casptr() failed; if ptail's data is
3025 	 * already in our stack, NULL out eq_ptail and ignore it.
3026 	 */
3027 	if (eq.eq_ptail != NULL) {
3028 		for (i = 0; i < eqdp->eqd_qpos; i++) {
3029 			if (eqdp->eqd_stack[i] == (uintptr_t)eqe.eqe_data) {
3030 				eq.eq_ptail = NULL;
3031 				break;
3032 			}
3033 		}
3034 	}
3035 
3036 	/*
3037 	 * If eq_phead is set, it has the processing list in order from oldest
3038 	 * to newest.  Use this to recompute eq_ptail as best we can and then
3039 	 * we nicely fall into eqd_push_list() of eq_ptail below.
3040 	 */
3041 	for (addr = eq.eq_phead; addr != NULL && mdb_vread(&eqe, sizeof (eqe),
3042 	    (uintptr_t)addr) == sizeof (eqe); addr = eqe.eqe_next)
3043 		eq.eq_ptail = addr;
3044 
3045 	/*
3046 	 * The oldest elements in the queue are on the processing list, subject
3047 	 * to machinations in the if-clauses above.  Push any such elements.
3048 	 */
3049 	eqd_push_list(eqdp, (uintptr_t)eq.eq_ptail);
3050 	return (WALK_NEXT);
3051 }
3052 
3053 static int
3054 eqd_walk_step(mdb_walk_state_t *wsp)
3055 {
3056 	eqd_walk_data_t *eqdp = wsp->walk_data;
3057 	uintptr_t addr;
3058 
3059 	if (eqdp->eqd_qpos == 0)
3060 		return (WALK_DONE);
3061 
3062 	addr = eqdp->eqd_stack[--eqdp->eqd_qpos];
3063 
3064 	if (mdb_vread(eqdp->eqd_buf, eqdp->eqd_size, addr) != eqdp->eqd_size) {
3065 		mdb_warn("failed to read errorq data at %p", addr);
3066 		return (WALK_ERR);
3067 	}
3068 
3069 	return (wsp->walk_callback(addr, eqdp->eqd_buf, wsp->walk_cbdata));
3070 }
3071 
3072 static void
3073 eqd_walk_fini(mdb_walk_state_t *wsp)
3074 {
3075 	eqd_walk_data_t *eqdp = wsp->walk_data;
3076 
3077 	mdb_free(eqdp->eqd_stack, sizeof (uintptr_t) * eqdp->eqd_qlen);
3078 	mdb_free(eqdp->eqd_buf, eqdp->eqd_size);
3079 	mdb_free(eqdp, sizeof (eqd_walk_data_t));
3080 }
3081 
3082 #define	EQKSVAL(eqv, what) (eqv.eq_kstat.what.value.ui64)
3083 
3084 static int
3085 errorq(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
3086 {
3087 	int i;
3088 	errorq_t eq;
3089 	uint_t opt_v = FALSE;
3090 
3091 	if (!(flags & DCMD_ADDRSPEC)) {
3092 		if (mdb_walk_dcmd("errorq", "errorq", argc, argv) == -1) {
3093 			mdb_warn("can't walk 'errorq'");
3094 			return (DCMD_ERR);
3095 		}
3096 		return (DCMD_OK);
3097 	}
3098 
3099 	i = mdb_getopts(argc, argv, 'v', MDB_OPT_SETBITS, TRUE, &opt_v, NULL);
3100 	argc -= i;
3101 	argv += i;
3102 
3103 	if (argc != 0)
3104 		return (DCMD_USAGE);
3105 
3106 	if (opt_v || DCMD_HDRSPEC(flags)) {
3107 		mdb_printf("%<u>%-11s %-16s %1s %1s %1s ",
3108 		    "ADDR", "NAME", "S", "V", "N");
3109 		if (!opt_v) {
3110 			mdb_printf("%7s %7s %7s%</u>\n",
3111 			    "ACCEPT", "DROP", "LOG");
3112 		} else {
3113 			mdb_printf("%5s %6s %6s %3s %16s%</u>\n",
3114 			    "KSTAT", "QLEN", "SIZE", "IPL", "FUNC");
3115 		}
3116 	}
3117 
3118 	if (mdb_vread(&eq, sizeof (eq), addr) != sizeof (eq)) {
3119 		mdb_warn("failed to read errorq at %p", addr);
3120 		return (DCMD_ERR);
3121 	}
3122 
3123 	mdb_printf("%-11p %-16s %c %c %c ", addr, eq.eq_name,
3124 	    (eq.eq_flags & ERRORQ_ACTIVE) ? '+' : '-',
3125 	    (eq.eq_flags & ERRORQ_VITAL) ? '!' : ' ',
3126 	    (eq.eq_flags & ERRORQ_NVLIST) ? '*' : ' ');
3127 
3128 	if (!opt_v) {
3129 		mdb_printf("%7llu %7llu %7llu\n",
3130 		    EQKSVAL(eq, eqk_dispatched) + EQKSVAL(eq, eqk_committed),
3131 		    EQKSVAL(eq, eqk_dropped) + EQKSVAL(eq, eqk_reserve_fail) +
3132 		    EQKSVAL(eq, eqk_commit_fail), EQKSVAL(eq, eqk_logged));
3133 	} else {
3134 		mdb_printf("%5s %6lu %6lu %3u %a\n",
3135 		    "  |  ", eq.eq_qlen, eq.eq_size, eq.eq_ipl, eq.eq_func);
3136 		mdb_printf("%38s\n%41s"
3137 		    "%12s %llu\n"
3138 		    "%53s %llu\n"
3139 		    "%53s %llu\n"
3140 		    "%53s %llu\n"
3141 		    "%53s %llu\n"
3142 		    "%53s %llu\n"
3143 		    "%53s %llu\n"
3144 		    "%53s %llu\n\n",
3145 		    "|", "+-> ",
3146 		    "DISPATCHED",	EQKSVAL(eq, eqk_dispatched),
3147 		    "DROPPED",		EQKSVAL(eq, eqk_dropped),
3148 		    "LOGGED",		EQKSVAL(eq, eqk_logged),
3149 		    "RESERVED",		EQKSVAL(eq, eqk_reserved),
3150 		    "RESERVE FAIL",	EQKSVAL(eq, eqk_reserve_fail),
3151 		    "COMMITTED",	EQKSVAL(eq, eqk_committed),
3152 		    "COMMIT FAIL",	EQKSVAL(eq, eqk_commit_fail),
3153 		    "CANCELLED",	EQKSVAL(eq, eqk_cancelled));
3154 	}
3155 
3156 	return (DCMD_OK);
3157 }
3158 
3159 /*ARGSUSED*/
3160 static int
3161 panicinfo(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
3162 {
3163 	cpu_t panic_cpu;
3164 	kthread_t *panic_thread;
3165 	void *panicbuf;
3166 	panic_data_t *pd;
3167 	int i, n;
3168 
3169 	if (!mdb_prop_postmortem) {
3170 		mdb_warn("panicinfo can only be run on a system "
3171 		    "dump; see dumpadm(1M)\n");
3172 		return (DCMD_ERR);
3173 	}
3174 
3175 	if (flags & DCMD_ADDRSPEC || argc != 0)
3176 		return (DCMD_USAGE);
3177 
3178 	if (mdb_readsym(&panic_cpu, sizeof (cpu_t), "panic_cpu") == -1)
3179 		mdb_warn("failed to read 'panic_cpu'");
3180 	else
3181 		mdb_printf("%16s %?d\n", "cpu", panic_cpu.cpu_id);
3182 
3183 	if (mdb_readvar(&panic_thread, "panic_thread") == -1)
3184 		mdb_warn("failed to read 'panic_thread'");
3185 	else
3186 		mdb_printf("%16s %?p\n", "thread", panic_thread);
3187 
3188 	panicbuf = mdb_alloc(PANICBUFSIZE, UM_SLEEP);
3189 	pd = (panic_data_t *)panicbuf;
3190 
3191 	if (mdb_readsym(panicbuf, PANICBUFSIZE, "panicbuf") == -1 ||
3192 	    pd->pd_version != PANICBUFVERS) {
3193 		mdb_warn("failed to read 'panicbuf'");
3194 		mdb_free(panicbuf, PANICBUFSIZE);
3195 		return (DCMD_ERR);
3196 	}
3197 
3198 	mdb_printf("%16s %s\n", "message",  (char *)panicbuf + pd->pd_msgoff);
3199 
3200 	n = (pd->pd_msgoff - (sizeof (panic_data_t) -
3201 	    sizeof (panic_nv_t))) / sizeof (panic_nv_t);
3202 
3203 	for (i = 0; i < n; i++)
3204 		mdb_printf("%16s %?llx\n",
3205 		    pd->pd_nvdata[i].pnv_name, pd->pd_nvdata[i].pnv_value);
3206 
3207 	mdb_free(panicbuf, PANICBUFSIZE);
3208 	return (DCMD_OK);
3209 }
3210 
3211 static const mdb_dcmd_t dcmds[] = {
3212 
3213 	/* from genunix.c */
3214 	{ "addr2smap", ":[offset]", "translate address to smap", addr2smap },
3215 	{ "as2proc", ":", "convert as to proc_t address", as2proc },
3216 	{ "binding_hash_entry", ":", "print driver names hash table entry",
3217 		binding_hash_entry },
3218 	{ "callout", NULL, "print callout table", callout },
3219 	{ "class", NULL, "print process scheduler classes", class },
3220 	{ "cpuinfo", "?[-v]", "print CPUs and runnable threads", cpuinfo },
3221 	{ "did2thread", "? kt_did", "find kernel thread for this id",
3222 		did2thread },
3223 	{ "errorq", "?[-v]", "display kernel error queues", errorq },
3224 	{ "fd", ":[fd num]", "get a file pointer from an fd", fd },
3225 	{ "flipone", ":", "the vik_rev_level 2 special", flipone },
3226 	{ "lminfo", NULL, "print lock manager information", lminfo },
3227 	{ "ndi_event_hdl", "?", "print ndi_event_hdl", ndi_event_hdl },
3228 	{ "panicinfo", NULL, "print panic information", panicinfo },
3229 	{ "pid2proc", "?", "convert PID to proc_t address", pid2proc },
3230 	{ "pmap", ":[-q]", "print process memory map", pmap },
3231 	{ "project", NULL, "display kernel project(s)", project },
3232 	{ "ps", "[-fltzTP]", "list processes (and associated thr,lwp)", ps },
3233 	{ "pgrep", "[-x] [-n | -o] pattern",
3234 		"pattern match against all processes", pgrep },
3235 	{ "ptree", NULL, "print process tree", ptree },
3236 	{ "seg", ":", "print address space segment", seg },
3237 	{ "sysevent", "?[-sv]", "print sysevent pending or sent queue",
3238 		sysevent},
3239 	{ "sysevent_channel", "?", "print sysevent channel database",
3240 		sysevent_channel},
3241 	{ "sysevent_class_list", ":", "print sysevent class list",
3242 		sysevent_class_list},
3243 	{ "sysevent_subclass_list", ":",
3244 		"print sysevent subclass list", sysevent_subclass_list},
3245 	{ "system", NULL, "print contents of /etc/system file", sysfile },
3246 	{ "task", NULL, "display kernel task(s)", task },
3247 	{ "taskq_entry", ":", "display a taskq_ent_t", taskq_ent },
3248 	{ "vnode2path", ":[-F]", "vnode address to pathname", vnode2path },
3249 	{ "vnode2smap", ":[offset]", "translate vnode to smap", vnode2smap },
3250 	{ "whereopen", ":", "given a vnode, dumps procs which have it open",
3251 	    whereopen },
3252 
3253 	/* from zone.c */
3254 	{ "zone", "?", "display kernel zone(s)", zoneprt },
3255 	{ "zsd", ":[zsd key]", "lookup zsd value from a key", zsd },
3256 
3257 	/* from bio.c */
3258 	{ "bufpagefind", ":addr", "find page_t on buf_t list", bufpagefind },
3259 
3260 	/* from contract.c */
3261 	{ "contract", "?", "display a contract", cmd_contract },
3262 	{ "ctevent", ":", "display a contract event", cmd_ctevent },
3263 	{ "ctid", ":", "convert id to a contract pointer", cmd_ctid },
3264 
3265 	/* from cpupart.c */
3266 	{ "cpupart", "?[-v]", "print cpu partition info", cpupart },
3267 
3268 	/* from cyclic.c */
3269 	{ "cyccover", NULL, "dump cyclic coverage information", cyccover },
3270 	{ "cycid", "?", "dump a cyclic id", cycid },
3271 	{ "cycinfo", "?", "dump cyc_cpu info", cycinfo },
3272 	{ "cyclic", ":", "developer information", cyclic },
3273 	{ "cyctrace", "?", "dump cyclic trace buffer", cyctrace },
3274 
3275 	/* from devinfo.c */
3276 	{ "devbindings", "?[-qs] [device-name | major-num]",
3277 	    "print devinfo nodes bound to device-name or major-num",
3278 	    devbindings, devinfo_help },
3279 	{ "devinfo", ":[-qs]", "detailed devinfo of one node", devinfo,
3280 	    devinfo_help },
3281 	{ "devinfo_audit", ":[-v]", "devinfo configuration audit record",
3282 	    devinfo_audit },
3283 	{ "devinfo_audit_log", "?[-v]", "system wide devinfo configuration log",
3284 	    devinfo_audit_log },
3285 	{ "devinfo_audit_node", ":[-v]", "devinfo node configuration history",
3286 	    devinfo_audit_node },
3287 	{ "devinfo2driver", ":", "find driver name for this devinfo node",
3288 	    devinfo2driver },
3289 	{ "devnames", "?[-vm] [num]", "print devnames array", devnames },
3290 	{ "dev2major", "?<dev_t>", "convert dev_t to a major number",
3291 	    dev2major },
3292 	{ "dev2minor", "?<dev_t>", "convert dev_t to a minor number",
3293 	    dev2minor },
3294 	{ "devt", "?<dev_t>", "display a dev_t's major and minor numbers",
3295 	    devt },
3296 	{ "major2name", "?<major-num>", "convert major number to dev name",
3297 	    major2name },
3298 	{ "minornodes", ":", "given a devinfo node, print its minor nodes",
3299 	    minornodes },
3300 	{ "modctl2devinfo", ":", "given a modctl, list its devinfos",
3301 	    modctl2devinfo },
3302 	{ "name2major", "<dev-name>", "convert dev name to major number",
3303 	    name2major },
3304 	{ "prtconf", "?[-vpc]", "print devinfo tree", prtconf, prtconf_help },
3305 	{ "softstate", ":<instance>", "retrieve soft-state pointer",
3306 	    softstate },
3307 	{ "devinfo_fm", ":", "devinfo fault managment configuration",
3308 	    devinfo_fm },
3309 	{ "devinfo_fmce", ":", "devinfo fault managment cache entry",
3310 	    devinfo_fmce},
3311 
3312 	/* from findstack.c */
3313 	{ "findstack", ":[-v]", "find kernel thread stack", findstack },
3314 	{ "findstack_debug", NULL, "toggle findstack debugging",
3315 		findstack_debug },
3316 
3317 	/* from kgrep.c + genunix.c */
3318 	{ "kgrep", KGREP_USAGE, "search kernel as for a pointer", kgrep,
3319 		kgrep_help },
3320 
3321 	/* from kmem.c */
3322 	{ "allocdby", ":", "given a thread, print its allocated buffers",
3323 		allocdby },
3324 	{ "bufctl", ":[-vh] [-a addr] [-c caller] [-e earliest] [-l latest] "
3325 		"[-t thd]", "print or filter a bufctl", bufctl, bufctl_help },
3326 	{ "freedby", ":", "given a thread, print its freed buffers", freedby },
3327 	{ "kmalog", "?[ fail | slab ]",
3328 	    "display kmem transaction log and stack traces", kmalog },
3329 	{ "kmastat", NULL, "kernel memory allocator stats", kmastat },
3330 	{ "kmausers", "?[-ef] [cache ...]", "current medium and large users "
3331 		"of the kmem allocator", kmausers, kmausers_help },
3332 	{ "kmem_cache", "?", "print kernel memory caches", kmem_cache },
3333 	{ "kmem_debug", NULL, "toggle kmem dcmd/walk debugging", kmem_debug },
3334 	{ "kmem_log", "?[-b]", "dump kmem transaction log", kmem_log },
3335 	{ "kmem_verify", "?", "check integrity of kmem-managed memory",
3336 		kmem_verify },
3337 	{ "vmem", "?", "print a vmem_t", vmem },
3338 	{ "vmem_seg", ":[-sv] [-c caller] [-e earliest] [-l latest] "
3339 		"[-m minsize] [-M maxsize] [-t thread] [-T type]",
3340 		"print or filter a vmem_seg", vmem_seg, vmem_seg_help },
3341 	{ "whatis", ":[-abiv]", "given an address, return information", whatis,
3342 		whatis_help },
3343 	{ "whatthread", ":[-v]", "print threads whose stack contains the "
3344 		"given address", whatthread },
3345 
3346 	/* from ldi.c */
3347 	{ "ldi_handle", "?[-i]", "display a layered driver handle",
3348 	    ldi_handle, ldi_handle_help },
3349 	{ "ldi_ident", NULL, "display a layered driver identifier",
3350 	    ldi_ident, ldi_ident_help },
3351 
3352 	/* from leaky.c + leaky_subr.c */
3353 	{ "findleaks", FINDLEAKS_USAGE,
3354 	    "search for potential kernel memory leaks", findleaks,
3355 	    findleaks_help },
3356 
3357 	/* from lgrp.c */
3358 	{ "lgrp", "?[-q] [-p | -Pih]", "display an lgrp", lgrp},
3359 	{ "lgrp_set", "", "display bitmask of lgroups as a list", lgrp_set},
3360 
3361 	/* from log.c */
3362 	{ "msgbuf", "?[-v]", "print most recent console messages", msgbuf },
3363 
3364 	/* from memory.c */
3365 	{ "page", "?", "display a summarized page_t", page },
3366 	{ "memstat", NULL, "display memory usage summary", memstat },
3367 	{ "memlist", "?[-iav]", "display a struct memlist", memlist },
3368 	{ "swapinfo", "?", "display a struct swapinfo", swapinfof },
3369 
3370 	/* from mmd.c */
3371 	{ "multidata", ":[-sv]", "display a summarized multidata_t",
3372 		multidata },
3373 	{ "pattbl", ":", "display a summarized multidata attribute table",
3374 		pattbl },
3375 	{ "pattr2multidata", ":", "print multidata pointer from pattr_t",
3376 		pattr2multidata },
3377 	{ "pdesc2slab", ":", "print pdesc slab pointer from pdesc_t",
3378 		pdesc2slab },
3379 	{ "pdesc_verify", ":", "verify integrity of a pdesc_t", pdesc_verify },
3380 	{ "slab2multidata", ":", "print multidata pointer from pdesc_slab_t",
3381 		slab2multidata },
3382 
3383 	/* from modhash.c */
3384 	{ "modhash", "?[-ceht] [-k key] [-v val] [-i index]",
3385 		"display information about one or all mod_hash structures",
3386 		modhash, modhash_help },
3387 	{ "modent", ":[-k | -v | -t type]",
3388 		"display information about a mod_hash_entry", modent,
3389 		modent_help },
3390 
3391 	/* from net.c */
3392 	{ "mi", ":[-p] [-d | -m]", "filter and display MI object or payload",
3393 		mi },
3394 	{ "netstat", "[-arv] [-f inet | inet6 | unix] [-P tcp | udp]",
3395 		"show network statistics", netstat },
3396 	{ "sonode", "?[-f inet | inet6 | unix | #] "
3397 		"[-t stream | dgram | raw | #] [-p #]",
3398 		"filter and display sonode", sonode },
3399 
3400 	/* from nvpair.c */
3401 	{ NVPAIR_DCMD_NAME, NVPAIR_DCMD_USAGE, NVPAIR_DCMD_DESCR,
3402 		nvpair_print },
3403 	{ NVLIST_DCMD_NAME, NVLIST_DCMD_USAGE, NVLIST_DCMD_DESCR,
3404 		nvlist_print },
3405 
3406 	/* from rctl.c */
3407 	{ "rctl_dict", "?", "print systemwide default rctl definitions",
3408 		rctl_dict },
3409 	{ "rctl_list", ":[handle]", "print rctls for the given proc",
3410 		rctl_list },
3411 	{ "rctl", ":[handle]", "print a rctl_t, only if it matches the handle",
3412 		rctl },
3413 	{ "rctl_validate", ":[-v] [-n #]", "test resource control value "
3414 		"sequence", rctl_validate },
3415 
3416 	/* from sobj.c */
3417 	{ "rwlock", ":", "dump out a readers/writer lock", rwlock },
3418 	{ "mutex", ":[-f]", "dump out an adaptive or spin mutex", mutex,
3419 		mutex_help },
3420 	{ "sobj2ts", ":", "perform turnstile lookup on synch object", sobj2ts },
3421 	{ "wchaninfo", "?[-v]", "dump condition variable", wchaninfo },
3422 	{ "turnstile", "?", "display a turnstile", turnstile },
3423 
3424 	/* from stream.c */
3425 	{ "mblk", ":[-q|v] [-f|F flag] [-t|T type] [-l|L|B len] [-d dbaddr]",
3426 		"print an mblk", mblk_prt, mblk_help },
3427 	{ "mblk_verify", "?", "verify integrity of an mblk", mblk_verify },
3428 	{ "mblk2dblk", ":", "convert mblk_t address to dblk_t address",
3429 		mblk2dblk },
3430 	{ "q2otherq", ":", "print peer queue for a given queue", q2otherq },
3431 	{ "q2rdq", ":", "print read queue for a given queue", q2rdq },
3432 	{ "q2syncq", ":", "print syncq for a given queue", q2syncq },
3433 	{ "q2stream", ":", "print stream pointer for a given queue", q2stream },
3434 	{ "q2wrq", ":", "print write queue for a given queue", q2wrq },
3435 	{ "queue", ":[-q|v] [-m mod] [-f flag] [-F flag] [-s syncq_addr]",
3436 		"filter and display STREAM queue", queue, queue_help },
3437 	{ "stdata", ":[-q|v] [-f flag] [-F flag]",
3438 		"filter and display STREAM head", stdata, stdata_help },
3439 	{ "str2mate", ":", "print mate of this stream", str2mate },
3440 	{ "str2wrq", ":", "print write queue of this stream", str2wrq },
3441 	{ "stream", ":", "display STREAM", stream },
3442 	{ "strftevent", ":", "print STREAMS flow trace event", strftevent },
3443 	{ "syncq", ":[-q|v] [-f flag] [-F flag] [-t type] [-T type]",
3444 		"filter and display STREAM sync queue", syncq, syncq_help },
3445 	{ "syncq2q", ":", "print queue for a given syncq", syncq2q },
3446 
3447 	/* from thread.c */
3448 	{ "thread", "?[-bdfimps]", "display a summarized kthread_t", thread,
3449 		thread_help },
3450 	{ "threadlist", "?[-v [count]]",
3451 		"display threads and associated C stack traces", threadlist,
3452 		threadlist_help },
3453 
3454 	/* from tsd.c */
3455 	{ "tsd", ":-k key", "print tsd[key-1] for this thread", ttotsd },
3456 	{ "tsdtot", ":", "find thread with this tsd", tsdtot },
3457 
3458 	/*
3459 	 * typegraph does not work under kmdb, as it requires too much memory
3460 	 * for its internal data structures.
3461 	 */
3462 #ifndef _KMDB
3463 	/* from typegraph.c */
3464 	{ "findlocks", ":", "find locks held by specified thread", findlocks },
3465 	{ "findfalse", "?[-v]", "find potentially falsely shared structures",
3466 		findfalse },
3467 	{ "typegraph", NULL, "build type graph", typegraph },
3468 	{ "istype", ":type", "manually set object type", istype },
3469 	{ "notype", ":", "manually clear object type", notype },
3470 	{ "whattype", ":", "determine object type", whattype },
3471 #endif
3472 
3473 	/* from vfs.c */
3474 	{ "fsinfo", "?[-v]", "print mounted filesystems", fsinfo },
3475 	{ "pfiles", ":[-fp]", "print process file information", pfiles,
3476 		pfiles_help },
3477 
3478 	/* from mdi.c */
3479 	{ "mdipi", NULL, "given a path, dump mdi_pathinfo "
3480 		"and detailed pi_prop list", mdipi },
3481 	{ "mdiprops", NULL, "given a pi_prop, dump the pi_prop list",
3482 		mdiprops },
3483 	{ "mdiphci", NULL, "given a phci, dump mdi_phci and "
3484 		"list all paths", mdiphci },
3485 	{ "mdivhci", NULL, "given a vhci, dump mdi_vhci and list "
3486 		"all phcis", mdivhci },
3487 	{ "mdiclient_paths", NULL, "given a path, walk mdi_pathinfo "
3488 		"client links", mdiclient_paths },
3489 	{ "mdiphci_paths", NULL, "given a path, walk through mdi_pathinfo "
3490 		"phci links", mdiphci_paths },
3491 	{ "mdiphcis", NULL, "given a phci, walk through mdi_phci ph_next links",
3492 		mdiphcis },
3493 
3494 	{ NULL }
3495 };
3496 
3497 static const mdb_walker_t walkers[] = {
3498 
3499 	/* from genunix.c */
3500 	{ "anon", "given an amp, list of anon structures",
3501 		anon_walk_init, anon_walk_step, anon_walk_fini },
3502 	{ "cpu", "walk cpu structures", cpu_walk_init, cpu_walk_step },
3503 	{ "errorq", "walk list of system error queues",
3504 		errorq_walk_init, errorq_walk_step, NULL },
3505 	{ "errorq_data", "walk pending error queue data buffers",
3506 		eqd_walk_init, eqd_walk_step, eqd_walk_fini },
3507 	{ "allfile", "given a proc pointer, list all file pointers",
3508 		file_walk_init, allfile_walk_step, file_walk_fini },
3509 	{ "file", "given a proc pointer, list of open file pointers",
3510 		file_walk_init, file_walk_step, file_walk_fini },
3511 	{ "lock_descriptor", "walk lock_descriptor_t structures",
3512 		ld_walk_init, ld_walk_step, NULL },
3513 	{ "lock_graph", "walk lock graph",
3514 		lg_walk_init, lg_walk_step, NULL },
3515 	{ "port", "given a proc pointer, list of created event ports",
3516 		port_walk_init, port_walk_step, NULL },
3517 	{ "portev", "given a port pointer, list of events in the queue",
3518 		portev_walk_init, portev_walk_step, portev_walk_fini },
3519 	{ "proc", "list of active proc_t structures",
3520 		proc_walk_init, proc_walk_step, proc_walk_fini },
3521 	{ "projects", "walk a list of kernel projects",
3522 		project_walk_init, project_walk_step, NULL },
3523 	{ "seg", "given an as, list of segments",
3524 		seg_walk_init, avl_walk_step, avl_walk_fini },
3525 	{ "sysevent_pend", "walk sysevent pending queue",
3526 		sysevent_pend_walk_init, sysevent_walk_step,
3527 		sysevent_walk_fini},
3528 	{ "sysevent_sent", "walk sysevent sent queue", sysevent_sent_walk_init,
3529 		sysevent_walk_step, sysevent_walk_fini},
3530 	{ "sysevent_channel", "walk sysevent channel subscriptions",
3531 		sysevent_channel_walk_init, sysevent_channel_walk_step,
3532 		sysevent_channel_walk_fini},
3533 	{ "sysevent_class_list", "walk sysevent subscription's class list",
3534 		sysevent_class_list_walk_init, sysevent_class_list_walk_step,
3535 		sysevent_class_list_walk_fini},
3536 	{ "sysevent_subclass_list",
3537 		"walk sysevent subscription's subclass list",
3538 		sysevent_subclass_list_walk_init,
3539 		sysevent_subclass_list_walk_step,
3540 		sysevent_subclass_list_walk_fini},
3541 	{ "task", "given a task pointer, walk its processes",
3542 		task_walk_init, task_walk_step, NULL },
3543 	{ "taskq_entry", "given a taskq_t*, list all taskq_ent_t in the list",
3544 		taskq_walk_init, taskq_walk_step, NULL, NULL },
3545 
3546 	/* from avl.c */
3547 	{ AVL_WALK_NAME, AVL_WALK_DESC,
3548 		avl_walk_init, avl_walk_step, avl_walk_fini },
3549 
3550 	/* from zone.c */
3551 	{ "zone", "walk a list of kernel zones",
3552 		zone_walk_init, zone_walk_step, NULL },
3553 	{ "zsd", "walk list of zsd entries for a zone",
3554 		zsd_walk_init, zsd_walk_step, NULL },
3555 
3556 	/* from bio.c */
3557 	{ "buf", "walk the bio buf hash",
3558 		buf_walk_init, buf_walk_step, buf_walk_fini },
3559 
3560 	/* from contract.c */
3561 	{ "contract", "walk all contracts, or those of the specified type",
3562 		ct_walk_init, generic_walk_step, NULL },
3563 	{ "ct_event", "walk events on a contract event queue",
3564 		ct_event_walk_init, generic_walk_step, NULL },
3565 	{ "ct_listener", "walk contract event queue listeners",
3566 		ct_listener_walk_init, generic_walk_step, NULL },
3567 
3568 	/* from cpupart.c */
3569 	{ "cpupart_cpulist", "given an cpupart_t, walk cpus in partition",
3570 		cpupart_cpulist_walk_init, cpupart_cpulist_walk_step,
3571 		NULL },
3572 	{ "cpupart_walk", "walk the set of cpu partitions",
3573 		cpupart_walk_init, cpupart_walk_step, NULL },
3574 
3575 	/* from ctxop.c */
3576 	{ "ctxop", "walk list of context ops on a thread",
3577 		ctxop_walk_init, ctxop_walk_step, ctxop_walk_fini },
3578 
3579 	/* from cyclic.c */
3580 	{ "cyccpu", "walk per-CPU cyc_cpu structures",
3581 		cyccpu_walk_init, cyccpu_walk_step, NULL },
3582 	{ "cycomni", "for an omnipresent cyclic, walk cyc_omni_cpu list",
3583 		cycomni_walk_init, cycomni_walk_step, NULL },
3584 	{ "cyctrace", "walk cyclic trace buffer",
3585 		cyctrace_walk_init, cyctrace_walk_step, cyctrace_walk_fini },
3586 
3587 	/* from devinfo.c */
3588 	{ "binding_hash", "walk all entries in binding hash table",
3589 		binding_hash_walk_init, binding_hash_walk_step, NULL },
3590 	{ "devinfo", "walk devinfo tree or subtree",
3591 		devinfo_walk_init, devinfo_walk_step, devinfo_walk_fini },
3592 	{ "devinfo_audit_log", "walk devinfo audit system-wide log",
3593 		devinfo_audit_log_walk_init, devinfo_audit_log_walk_step,
3594 		devinfo_audit_log_walk_fini},
3595 	{ "devinfo_audit_node", "walk per-devinfo audit history",
3596 		devinfo_audit_node_walk_init, devinfo_audit_node_walk_step,
3597 		devinfo_audit_node_walk_fini},
3598 	{ "devinfo_children", "walk children of devinfo node",
3599 		devinfo_children_walk_init, devinfo_children_walk_step,
3600 		devinfo_children_walk_fini },
3601 	{ "devinfo_parents", "walk ancestors of devinfo node",
3602 		devinfo_parents_walk_init, devinfo_parents_walk_step,
3603 		devinfo_parents_walk_fini },
3604 	{ "devinfo_siblings", "walk siblings of devinfo node",
3605 		devinfo_siblings_walk_init, devinfo_siblings_walk_step, NULL },
3606 	{ "devi_next", "walk devinfo list",
3607 		NULL, devi_next_walk_step, NULL },
3608 	{ "devnames", "walk devnames array",
3609 		devnames_walk_init, devnames_walk_step, devnames_walk_fini },
3610 	{ "minornode", "given a devinfo node, walk minor nodes",
3611 		minornode_walk_init, minornode_walk_step, NULL },
3612 	{ "softstate",
3613 		"given an i_ddi_soft_state*, list all in-use driver stateps",
3614 		soft_state_walk_init, soft_state_walk_step,
3615 		NULL, NULL },
3616 	{ "softstate_all",
3617 		"given an i_ddi_soft_state*, list all driver stateps",
3618 		soft_state_walk_init, soft_state_all_walk_step,
3619 		NULL, NULL },
3620 	{ "devinfo_fmc",
3621 		"walk a fault management handle cache active list",
3622 		devinfo_fmc_walk_init, devinfo_fmc_walk_step, NULL },
3623 
3624 	/* from kmem.c */
3625 	{ "allocdby", "given a thread, walk its allocated bufctls",
3626 		allocdby_walk_init, allocdby_walk_step, allocdby_walk_fini },
3627 	{ "bufctl", "walk a kmem cache's bufctls",
3628 		bufctl_walk_init, kmem_walk_step, kmem_walk_fini },
3629 	{ "bufctl_history", "walk the available history of a bufctl",
3630 		bufctl_history_walk_init, bufctl_history_walk_step,
3631 		bufctl_history_walk_fini },
3632 	{ "freedby", "given a thread, walk its freed bufctls",
3633 		freedby_walk_init, allocdby_walk_step, allocdby_walk_fini },
3634 	{ "freectl", "walk a kmem cache's free bufctls",
3635 		freectl_walk_init, kmem_walk_step, kmem_walk_fini },
3636 	{ "freectl_constructed", "walk a kmem cache's constructed free bufctls",
3637 		freectl_constructed_walk_init, kmem_walk_step, kmem_walk_fini },
3638 	{ "freemem", "walk a kmem cache's free memory",
3639 		freemem_walk_init, kmem_walk_step, kmem_walk_fini },
3640 	{ "freemem_constructed", "walk a kmem cache's constructed free memory",
3641 		freemem_constructed_walk_init, kmem_walk_step, kmem_walk_fini },
3642 	{ "kmem", "walk a kmem cache",
3643 		kmem_walk_init, kmem_walk_step, kmem_walk_fini },
3644 	{ "kmem_cpu_cache", "given a kmem cache, walk its per-CPU caches",
3645 		kmem_cpu_cache_walk_init, kmem_cpu_cache_walk_step, NULL },
3646 	{ "kmem_hash", "given a kmem cache, walk its allocated hash table",
3647 		kmem_hash_walk_init, kmem_hash_walk_step, kmem_hash_walk_fini },
3648 	{ "kmem_log", "walk the kmem transaction log",
3649 		kmem_log_walk_init, kmem_log_walk_step, kmem_log_walk_fini },
3650 	{ "kmem_slab", "given a kmem cache, walk its slabs",
3651 		kmem_slab_walk_init, kmem_slab_walk_step, NULL },
3652 	{ "kmem_slab_partial",
3653 	    "given a kmem cache, walk its partially allocated slabs (min 1)",
3654 		kmem_slab_walk_partial_init, kmem_slab_walk_step, NULL },
3655 	{ "vmem", "walk vmem structures in pre-fix, depth-first order",
3656 		vmem_walk_init, vmem_walk_step, vmem_walk_fini },
3657 	{ "vmem_alloc", "given a vmem_t, walk its allocated vmem_segs",
3658 		vmem_alloc_walk_init, vmem_seg_walk_step, vmem_seg_walk_fini },
3659 	{ "vmem_free", "given a vmem_t, walk its free vmem_segs",
3660 		vmem_free_walk_init, vmem_seg_walk_step, vmem_seg_walk_fini },
3661 	{ "vmem_postfix", "walk vmem structures in post-fix, depth-first order",
3662 		vmem_walk_init, vmem_postfix_walk_step, vmem_walk_fini },
3663 	{ "vmem_seg", "given a vmem_t, walk all of its vmem_segs",
3664 		vmem_seg_walk_init, vmem_seg_walk_step, vmem_seg_walk_fini },
3665 	{ "vmem_span", "given a vmem_t, walk its spanning vmem_segs",
3666 		vmem_span_walk_init, vmem_seg_walk_step, vmem_seg_walk_fini },
3667 
3668 	/* from ldi.c */
3669 	{ "ldi_handle", "walk the layered driver handle hash",
3670 		ldi_handle_walk_init, ldi_handle_walk_step, NULL },
3671 	{ "ldi_ident", "walk the layered driver identifier hash",
3672 		ldi_ident_walk_init, ldi_ident_walk_step, NULL },
3673 
3674 	/* from leaky.c + leaky_subr.c */
3675 	{ "leak", "given a leaked bufctl or vmem_seg, find leaks w/ same "
3676 	    "stack trace",
3677 		leaky_walk_init, leaky_walk_step, leaky_walk_fini },
3678 	{ "leakbuf", "given a leaked bufctl or vmem_seg, walk buffers for "
3679 	    "leaks w/ same stack trace",
3680 		leaky_walk_init, leaky_buf_walk_step, leaky_walk_fini },
3681 
3682 	/* from lgrp.c */
3683 	{ "lgrp_cpulist", "walk CPUs in a given lgroup",
3684 		lgrp_cpulist_walk_init, lgrp_cpulist_walk_step, NULL },
3685 	{ "lgrptbl", "walk lgroup table",
3686 		lgrp_walk_init, lgrp_walk_step, NULL },
3687 	{ "lgrp_parents", "walk up lgroup lineage from given lgroup",
3688 		lgrp_parents_walk_init, lgrp_parents_walk_step, NULL },
3689 	{ "lgrp_rsrc_mem", "walk lgroup memory resources of given lgroup",
3690 		lgrp_rsrc_mem_walk_init, lgrp_set_walk_step, NULL },
3691 	{ "lgrp_rsrc_cpu", "walk lgroup CPU resources of given lgroup",
3692 		lgrp_rsrc_cpu_walk_init, lgrp_set_walk_step, NULL },
3693 
3694 	/* from list.c */
3695 	{ LIST_WALK_NAME, LIST_WALK_DESC,
3696 		list_walk_init, list_walk_step, list_walk_fini },
3697 
3698 	/* from memory.c */
3699 	{ "page", "walk all pages, or those from the specified vnode",
3700 		page_walk_init, page_walk_step, page_walk_fini },
3701 	{ "memlist", "walk specified memlist",
3702 		NULL, memlist_walk_step, NULL },
3703 	{ "swapinfo", "walk swapinfo structures",
3704 		swap_walk_init, swap_walk_step, NULL },
3705 
3706 	/* from mmd.c */
3707 	{ "pattr", "walk pattr_t structures", pattr_walk_init,
3708 		mmdq_walk_step, mmdq_walk_fini },
3709 	{ "pdesc", "walk pdesc_t structures",
3710 		pdesc_walk_init, mmdq_walk_step, mmdq_walk_fini },
3711 	{ "pdesc_slab", "walk pdesc_slab_t structures",
3712 		pdesc_slab_walk_init, mmdq_walk_step, mmdq_walk_fini },
3713 
3714 	/* from modhash.c */
3715 	{ "modhash", "walk list of mod_hash structures", modhash_walk_init,
3716 		modhash_walk_step, NULL },
3717 	{ "modent", "walk list of entries in a given mod_hash",
3718 		modent_walk_init, modent_walk_step, modent_walk_fini },
3719 	{ "modchain", "walk list of entries in a given mod_hash_entry",
3720 		NULL, modchain_walk_step, NULL },
3721 
3722 	/* from net.c */
3723 	{ "ar", "walk ar_t structures using MI",
3724 		mi_payload_walk_init, mi_payload_walk_step,
3725 		mi_payload_walk_fini, &mi_ar_arg },
3726 	{ "icmp", "walk ICMP control structures using MI",
3727 		mi_payload_walk_init, mi_payload_walk_step,
3728 		mi_payload_walk_fini, &mi_icmp_arg },
3729 	{ "ill", "walk ill_t structures using MI",
3730 		mi_payload_walk_init, mi_payload_walk_step,
3731 		mi_payload_walk_fini, &mi_ill_arg },
3732 	{ "mi", "given a MI_O, walk the MI",
3733 		mi_walk_init, mi_walk_step, mi_walk_fini, NULL },
3734 	{ "sonode", "given a sonode, walk its children",
3735 		sonode_walk_init, sonode_walk_step, sonode_walk_fini, NULL },
3736 
3737 	/* from nvpair.c */
3738 	{ NVPAIR_WALKER_NAME, NVPAIR_WALKER_DESCR,
3739 		nvpair_walk_init, nvpair_walk_step, NULL },
3740 
3741 	/* from rctl.c */
3742 	{ "rctl_dict_list", "walk all rctl_dict_entry_t's from rctl_lists",
3743 		rctl_dict_walk_init, rctl_dict_walk_step, NULL },
3744 	{ "rctl_set", "given a rctl_set, walk all rctls", rctl_set_walk_init,
3745 		rctl_set_walk_step, NULL },
3746 	{ "rctl_val", "given a rctl_t, walk all rctl_val entries associated",
3747 		rctl_val_walk_init, rctl_val_walk_step },
3748 
3749 	/* from sobj.c */
3750 	{ "blocked", "walk threads blocked on a given sobj",
3751 		blocked_walk_init, blocked_walk_step, NULL },
3752 	{ "wchan", "given a wchan, list of blocked threads",
3753 		wchan_walk_init, wchan_walk_step, wchan_walk_fini },
3754 
3755 	/* from stream.c */
3756 	{ "b_cont", "walk mblk_t list using b_cont",
3757 		mblk_walk_init, b_cont_step, mblk_walk_fini },
3758 	{ "b_next", "walk mblk_t list using b_next",
3759 		mblk_walk_init, b_next_step, mblk_walk_fini },
3760 	{ "qlink", "walk queue_t list using q_link",
3761 		queue_walk_init, queue_link_step, queue_walk_fini },
3762 	{ "qnext", "walk queue_t list using q_next",
3763 		queue_walk_init, queue_next_step, queue_walk_fini },
3764 	{ "strftblk", "given a dblk_t, walk STREAMS flow trace event list",
3765 		strftblk_walk_init, strftblk_step, strftblk_walk_fini },
3766 	{ "readq", "walk read queue side of stdata",
3767 		str_walk_init, strr_walk_step, str_walk_fini },
3768 	{ "writeq", "walk write queue side of stdata",
3769 		str_walk_init, strw_walk_step, str_walk_fini },
3770 
3771 	/* from thread.c */
3772 	{ "deathrow", "walk threads on both lwp_ and thread_deathrow",
3773 		deathrow_walk_init, deathrow_walk_step, NULL },
3774 	{ "cpu_dispq", "given a cpu_t, walk threads in dispatcher queues",
3775 		cpu_dispq_walk_init, dispq_walk_step, dispq_walk_fini },
3776 	{ "cpupart_dispq",
3777 		"given a cpupart_t, walk threads in dispatcher queues",
3778 		cpupart_dispq_walk_init, dispq_walk_step, dispq_walk_fini },
3779 	{ "lwp_deathrow", "walk lwp_deathrow",
3780 		lwp_deathrow_walk_init, deathrow_walk_step, NULL },
3781 	{ "thread", "global or per-process kthread_t structures",
3782 		thread_walk_init, thread_walk_step, thread_walk_fini },
3783 	{ "thread_deathrow", "walk threads on thread_deathrow",
3784 		thread_deathrow_walk_init, deathrow_walk_step, NULL },
3785 
3786 	/* from tsd.c */
3787 	{ "tsd", "walk list of thread-specific data",
3788 		tsd_walk_init, tsd_walk_step, tsd_walk_fini },
3789 
3790 	/* from tsol.c */
3791 	{ "tnrh", "walk remote host cache structures",
3792 	    tnrh_walk_init, tnrh_walk_step, tnrh_walk_fini },
3793 	{ "tnrhtp", "walk remote host template structures",
3794 	    tnrhtp_walk_init, tnrhtp_walk_step, tnrhtp_walk_fini },
3795 
3796 	/*
3797 	 * typegraph does not work under kmdb, as it requires too much memory
3798 	 * for its internal data structures.
3799 	 */
3800 #ifndef _KMDB
3801 	/* from typegraph.c */
3802 	{ "typeconflict", "walk buffers with conflicting type inferences",
3803 		typegraph_walk_init, typeconflict_walk_step },
3804 	{ "typeunknown", "walk buffers with unknown types",
3805 		typegraph_walk_init, typeunknown_walk_step },
3806 #endif
3807 
3808 	/* from vfs.c */
3809 	{ "vfs", "walk file system list",
3810 		vfs_walk_init, vfs_walk_step },
3811 
3812 	/* from mdi.c */
3813 	{ "mdipi_client_list", "Walker for mdi_pathinfo pi_client_link",
3814 		mdi_pi_client_link_walk_init,
3815 		mdi_pi_client_link_walk_step,
3816 		mdi_pi_client_link_walk_fini },
3817 
3818 	{ "mdipi_phci_list", "Walker for mdi_pathinfo pi_phci_link",
3819 		mdi_pi_phci_link_walk_init,
3820 		mdi_pi_phci_link_walk_step,
3821 		mdi_pi_phci_link_walk_fini },
3822 
3823 	{ "mdiphci_list", "Walker for mdi_phci ph_next link",
3824 		mdi_phci_ph_next_walk_init,
3825 		mdi_phci_ph_next_walk_step,
3826 		mdi_phci_ph_next_walk_fini },
3827 
3828 	{ NULL }
3829 };
3830 
3831 static const mdb_modinfo_t modinfo = { MDB_API_VERSION, dcmds, walkers };
3832 
3833 const mdb_modinfo_t *
3834 _mdb_init(void)
3835 {
3836 	if (mdb_readvar(&devinfo_root, "top_devinfo") == -1) {
3837 		mdb_warn("failed to read 'top_devinfo'");
3838 		return (NULL);
3839 	}
3840 
3841 	if (findstack_init() != DCMD_OK)
3842 		return (NULL);
3843 
3844 	kmem_init();
3845 
3846 	return (&modinfo);
3847 }
3848 
3849 void
3850 _mdb_fini(void)
3851 {
3852 	/*
3853 	 * Force ::findleaks to let go any cached memory
3854 	 */
3855 	leaky_cleanup(1);
3856 }
3857