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