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