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