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