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