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