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