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