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