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