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