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