xref: /titanic_50/usr/src/cmd/mdb/common/modules/genunix/genunix.c (revision e7860364450c5bfee3d12a2a4d9fcde8b0a1c2b6)
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_ithr;
3087 	char	cid_print_head;
3088 	char	cid_print_thr;
3089 	char	cid_print_ithr;
3090 	char	cid_print_flags;
3091 } cpuinfo_data_t;
3092 
3093 int
3094 cpuinfo_walk_ithread(uintptr_t addr, const kthread_t *thr, cpuinfo_data_t *cid)
3095 {
3096 	cpu_t c;
3097 	int id;
3098 	uint8_t pil;
3099 
3100 	if (!(thr->t_flag & T_INTR_THREAD) || thr->t_state == TS_FREE)
3101 		return (WALK_NEXT);
3102 
3103 	if (thr->t_bound_cpu == NULL) {
3104 		mdb_warn("thr %p is intr thread w/out a CPU\n", addr);
3105 		return (WALK_NEXT);
3106 	}
3107 
3108 	(void) mdb_vread(&c, sizeof (c), (uintptr_t)thr->t_bound_cpu);
3109 
3110 	if ((id = c.cpu_id) >= NCPU) {
3111 		mdb_warn("CPU %p has id (%d) greater than NCPU (%d)\n",
3112 		    thr->t_bound_cpu, id, NCPU);
3113 		return (WALK_NEXT);
3114 	}
3115 
3116 	if ((pil = thr->t_pil) >= NINTR) {
3117 		mdb_warn("thread %p has pil (%d) greater than %d\n",
3118 		    addr, pil, NINTR);
3119 		return (WALK_NEXT);
3120 	}
3121 
3122 	if (cid->cid_ithr[id][pil] != NULL) {
3123 		mdb_warn("CPU %d has multiple threads at pil %d (at least "
3124 		    "%p and %p)\n", id, pil, addr, cid->cid_ithr[id][pil]);
3125 		return (WALK_NEXT);
3126 	}
3127 
3128 	cid->cid_ithr[id][pil] = addr;
3129 
3130 	return (WALK_NEXT);
3131 }
3132 
3133 #define	CPUINFO_IDWIDTH		3
3134 #define	CPUINFO_FLAGWIDTH	9
3135 
3136 #ifdef _LP64
3137 #if defined(__amd64)
3138 #define	CPUINFO_TWIDTH		16
3139 #define	CPUINFO_CPUWIDTH	16
3140 #else
3141 #define	CPUINFO_CPUWIDTH	11
3142 #define	CPUINFO_TWIDTH		11
3143 #endif
3144 #else
3145 #define	CPUINFO_CPUWIDTH	8
3146 #define	CPUINFO_TWIDTH		8
3147 #endif
3148 
3149 #define	CPUINFO_THRDELT		(CPUINFO_IDWIDTH + CPUINFO_CPUWIDTH + 9)
3150 #define	CPUINFO_FLAGDELT	(CPUINFO_IDWIDTH + CPUINFO_CPUWIDTH + 4)
3151 #define	CPUINFO_ITHRDELT	4
3152 
3153 #define	CPUINFO_INDENT	mdb_printf("%*s", CPUINFO_THRDELT, \
3154     flagline < nflaglines ? flagbuf[flagline++] : "")
3155 
3156 int
3157 cpuinfo_walk_cpu(uintptr_t addr, const cpu_t *cpu, cpuinfo_data_t *cid)
3158 {
3159 	kthread_t t;
3160 	disp_t disp;
3161 	proc_t p;
3162 	uintptr_t pinned;
3163 	char **flagbuf;
3164 	int nflaglines = 0, flagline = 0, bspl, rval = WALK_NEXT;
3165 
3166 	const char *flags[] = {
3167 	    "RUNNING", "READY", "QUIESCED", "EXISTS",
3168 	    "ENABLE", "OFFLINE", "POWEROFF", "FROZEN",
3169 	    "SPARE", "FAULTED", NULL
3170 	};
3171 
3172 	if (cid->cid_cpu != -1) {
3173 		if (addr != cid->cid_cpu && cpu->cpu_id != cid->cid_cpu)
3174 			return (WALK_NEXT);
3175 
3176 		/*
3177 		 * Set cid_cpu to -1 to indicate that we found a matching CPU.
3178 		 */
3179 		cid->cid_cpu = -1;
3180 		rval = WALK_DONE;
3181 	}
3182 
3183 	if (cid->cid_print_head) {
3184 		mdb_printf("%3s %-*s %3s %4s %4s %3s %4s %5s %-6s %-*s %s\n",
3185 		    "ID", CPUINFO_CPUWIDTH, "ADDR", "FLG", "NRUN", "BSPL",
3186 		    "PRI", "RNRN", "KRNRN", "SWITCH", CPUINFO_TWIDTH, "THREAD",
3187 		    "PROC");
3188 		cid->cid_print_head = FALSE;
3189 	}
3190 
3191 	bspl = cpu->cpu_base_spl;
3192 
3193 	if (mdb_vread(&disp, sizeof (disp_t), (uintptr_t)cpu->cpu_disp) == -1) {
3194 		mdb_warn("failed to read disp_t at %p", cpu->cpu_disp);
3195 		return (WALK_ERR);
3196 	}
3197 
3198 	mdb_printf("%3d %0*p %3x %4d %4d ",
3199 	    cpu->cpu_id, CPUINFO_CPUWIDTH, addr, cpu->cpu_flags,
3200 	    disp.disp_nrunnable, bspl);
3201 
3202 	if (mdb_vread(&t, sizeof (t), (uintptr_t)cpu->cpu_thread) != -1) {
3203 		mdb_printf("%3d ", t.t_pri);
3204 	} else {
3205 		mdb_printf("%3s ", "-");
3206 	}
3207 
3208 	mdb_printf("%4s %5s ", cpu->cpu_runrun ? "yes" : "no",
3209 	    cpu->cpu_kprunrun ? "yes" : "no");
3210 
3211 	if (cpu->cpu_last_swtch) {
3212 		mdb_printf("t-%-4d ",
3213 		    (clock_t)mdb_get_lbolt() - cpu->cpu_last_swtch);
3214 	} else {
3215 		mdb_printf("%-6s ", "-");
3216 	}
3217 
3218 	mdb_printf("%0*p", CPUINFO_TWIDTH, cpu->cpu_thread);
3219 
3220 	if (cpu->cpu_thread == cpu->cpu_idle_thread)
3221 		mdb_printf(" (idle)\n");
3222 	else if (cpu->cpu_thread == NULL)
3223 		mdb_printf(" -\n");
3224 	else {
3225 		if (mdb_vread(&p, sizeof (p), (uintptr_t)t.t_procp) != -1) {
3226 			mdb_printf(" %s\n", p.p_user.u_comm);
3227 		} else {
3228 			mdb_printf(" ?\n");
3229 		}
3230 	}
3231 
3232 	flagbuf = mdb_zalloc(sizeof (flags), UM_SLEEP | UM_GC);
3233 
3234 	if (cid->cid_print_flags) {
3235 		int first = 1, i, j, k;
3236 		char *s;
3237 
3238 		cid->cid_print_head = TRUE;
3239 
3240 		for (i = 1, j = 0; flags[j] != NULL; i <<= 1, j++) {
3241 			if (!(cpu->cpu_flags & i))
3242 				continue;
3243 
3244 			if (first) {
3245 				s = mdb_alloc(CPUINFO_THRDELT + 1,
3246 				    UM_GC | UM_SLEEP);
3247 
3248 				(void) mdb_snprintf(s, CPUINFO_THRDELT + 1,
3249 				    "%*s|%*s", CPUINFO_FLAGDELT, "",
3250 				    CPUINFO_THRDELT - 1 - CPUINFO_FLAGDELT, "");
3251 				flagbuf[nflaglines++] = s;
3252 			}
3253 
3254 			s = mdb_alloc(CPUINFO_THRDELT + 1, UM_GC | UM_SLEEP);
3255 			(void) mdb_snprintf(s, CPUINFO_THRDELT + 1, "%*s%*s %s",
3256 			    CPUINFO_IDWIDTH + CPUINFO_CPUWIDTH -
3257 			    CPUINFO_FLAGWIDTH, "", CPUINFO_FLAGWIDTH, flags[j],
3258 			    first ? "<--+" : "");
3259 
3260 			for (k = strlen(s); k < CPUINFO_THRDELT; k++)
3261 				s[k] = ' ';
3262 			s[k] = '\0';
3263 
3264 			flagbuf[nflaglines++] = s;
3265 			first = 0;
3266 		}
3267 	}
3268 
3269 	if (cid->cid_print_ithr) {
3270 		int i, found_one = FALSE;
3271 		int print_thr = disp.disp_nrunnable && cid->cid_print_thr;
3272 
3273 		for (i = NINTR - 1; i >= 0; i--) {
3274 			uintptr_t iaddr = cid->cid_ithr[cpu->cpu_id][i];
3275 
3276 			if (iaddr == NULL)
3277 				continue;
3278 
3279 			if (!found_one) {
3280 				found_one = TRUE;
3281 
3282 				CPUINFO_INDENT;
3283 				mdb_printf("%c%*s|\n", print_thr ? '|' : ' ',
3284 				    CPUINFO_ITHRDELT, "");
3285 
3286 				CPUINFO_INDENT;
3287 				mdb_printf("%c%*s+--> %3s %s\n",
3288 				    print_thr ? '|' : ' ', CPUINFO_ITHRDELT,
3289 				    "", "PIL", "THREAD");
3290 			}
3291 
3292 			if (mdb_vread(&t, sizeof (t), iaddr) == -1) {
3293 				mdb_warn("failed to read kthread_t at %p",
3294 				    iaddr);
3295 				return (WALK_ERR);
3296 			}
3297 
3298 			CPUINFO_INDENT;
3299 			mdb_printf("%c%*s     %3d %0*p\n",
3300 			    print_thr ? '|' : ' ', CPUINFO_ITHRDELT, "",
3301 			    t.t_pil, CPUINFO_TWIDTH, iaddr);
3302 
3303 			pinned = (uintptr_t)t.t_intr;
3304 		}
3305 
3306 		if (found_one && pinned != NULL) {
3307 			cid->cid_print_head = TRUE;
3308 			(void) strcpy(p.p_user.u_comm, "?");
3309 
3310 			if (mdb_vread(&t, sizeof (t),
3311 			    (uintptr_t)pinned) == -1) {
3312 				mdb_warn("failed to read kthread_t at %p",
3313 				    pinned);
3314 				return (WALK_ERR);
3315 			}
3316 			if (mdb_vread(&p, sizeof (p),
3317 			    (uintptr_t)t.t_procp) == -1) {
3318 				mdb_warn("failed to read proc_t at %p",
3319 				    t.t_procp);
3320 				return (WALK_ERR);
3321 			}
3322 
3323 			CPUINFO_INDENT;
3324 			mdb_printf("%c%*s     %3s %0*p %s\n",
3325 			    print_thr ? '|' : ' ', CPUINFO_ITHRDELT, "", "-",
3326 			    CPUINFO_TWIDTH, pinned,
3327 			    pinned == (uintptr_t)cpu->cpu_idle_thread ?
3328 			    "(idle)" : p.p_user.u_comm);
3329 		}
3330 	}
3331 
3332 	if (disp.disp_nrunnable && cid->cid_print_thr) {
3333 		dispq_t *dq;
3334 
3335 		int i, npri = disp.disp_npri;
3336 
3337 		dq = mdb_alloc(sizeof (dispq_t) * npri, UM_SLEEP | UM_GC);
3338 
3339 		if (mdb_vread(dq, sizeof (dispq_t) * npri,
3340 		    (uintptr_t)disp.disp_q) == -1) {
3341 			mdb_warn("failed to read dispq_t at %p", disp.disp_q);
3342 			return (WALK_ERR);
3343 		}
3344 
3345 		CPUINFO_INDENT;
3346 		mdb_printf("|\n");
3347 
3348 		CPUINFO_INDENT;
3349 		mdb_printf("+-->  %3s %-*s %s\n", "PRI",
3350 		    CPUINFO_TWIDTH, "THREAD", "PROC");
3351 
3352 		for (i = npri - 1; i >= 0; i--) {
3353 			uintptr_t taddr = (uintptr_t)dq[i].dq_first;
3354 
3355 			while (taddr != NULL) {
3356 				if (mdb_vread(&t, sizeof (t), taddr) == -1) {
3357 					mdb_warn("failed to read kthread_t "
3358 					    "at %p", taddr);
3359 					return (WALK_ERR);
3360 				}
3361 				if (mdb_vread(&p, sizeof (p),
3362 				    (uintptr_t)t.t_procp) == -1) {
3363 					mdb_warn("failed to read proc_t at %p",
3364 					    t.t_procp);
3365 					return (WALK_ERR);
3366 				}
3367 
3368 				CPUINFO_INDENT;
3369 				mdb_printf("      %3d %0*p %s\n", t.t_pri,
3370 				    CPUINFO_TWIDTH, taddr, p.p_user.u_comm);
3371 
3372 				taddr = (uintptr_t)t.t_link;
3373 			}
3374 		}
3375 		cid->cid_print_head = TRUE;
3376 	}
3377 
3378 	while (flagline < nflaglines)
3379 		mdb_printf("%s\n", flagbuf[flagline++]);
3380 
3381 	if (cid->cid_print_head)
3382 		mdb_printf("\n");
3383 
3384 	return (rval);
3385 }
3386 
3387 int
3388 cpuinfo(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
3389 {
3390 	uint_t verbose = FALSE;
3391 	cpuinfo_data_t cid;
3392 
3393 	cid.cid_print_ithr = FALSE;
3394 	cid.cid_print_thr = FALSE;
3395 	cid.cid_print_flags = FALSE;
3396 	cid.cid_print_head = DCMD_HDRSPEC(flags) ? TRUE : FALSE;
3397 	cid.cid_cpu = -1;
3398 
3399 	if (flags & DCMD_ADDRSPEC)
3400 		cid.cid_cpu = addr;
3401 
3402 	if (mdb_getopts(argc, argv,
3403 	    'v', MDB_OPT_SETBITS, TRUE, &verbose, NULL) != argc)
3404 		return (DCMD_USAGE);
3405 
3406 	if (verbose) {
3407 		cid.cid_print_ithr = TRUE;
3408 		cid.cid_print_thr = TRUE;
3409 		cid.cid_print_flags = TRUE;
3410 		cid.cid_print_head = TRUE;
3411 	}
3412 
3413 	if (cid.cid_print_ithr) {
3414 		int i;
3415 
3416 		cid.cid_ithr = mdb_alloc(sizeof (uintptr_t **)
3417 		    * NCPU, UM_SLEEP | UM_GC);
3418 
3419 		for (i = 0; i < NCPU; i++)
3420 			cid.cid_ithr[i] = mdb_zalloc(sizeof (uintptr_t *) *
3421 			    NINTR, UM_SLEEP | UM_GC);
3422 
3423 		if (mdb_walk("thread", (mdb_walk_cb_t)cpuinfo_walk_ithread,
3424 		    &cid) == -1) {
3425 			mdb_warn("couldn't walk thread");
3426 			return (DCMD_ERR);
3427 		}
3428 	}
3429 
3430 	if (mdb_walk("cpu", (mdb_walk_cb_t)cpuinfo_walk_cpu, &cid) == -1) {
3431 		mdb_warn("can't walk cpus");
3432 		return (DCMD_ERR);
3433 	}
3434 
3435 	if (cid.cid_cpu != -1) {
3436 		/*
3437 		 * We didn't find this CPU when we walked through the CPUs
3438 		 * (i.e. the address specified doesn't show up in the "cpu"
3439 		 * walk).  However, the specified address may still correspond
3440 		 * to a valid cpu_t (for example, if the specified address is
3441 		 * the actual panicking cpu_t and not the cached panic_cpu).
3442 		 * Point is:  even if we didn't find it, we still want to try
3443 		 * to print the specified address as a cpu_t.
3444 		 */
3445 		cpu_t cpu;
3446 
3447 		if (mdb_vread(&cpu, sizeof (cpu), cid.cid_cpu) == -1) {
3448 			mdb_warn("%p is neither a valid CPU ID nor a "
3449 			    "valid cpu_t address\n", cid.cid_cpu);
3450 			return (DCMD_ERR);
3451 		}
3452 
3453 		(void) cpuinfo_walk_cpu(cid.cid_cpu, &cpu, &cid);
3454 	}
3455 
3456 	return (DCMD_OK);
3457 }
3458 
3459 /*ARGSUSED*/
3460 int
3461 flipone(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
3462 {
3463 	int i;
3464 
3465 	if (!(flags & DCMD_ADDRSPEC))
3466 		return (DCMD_USAGE);
3467 
3468 	for (i = 0; i < sizeof (addr) * NBBY; i++)
3469 		mdb_printf("%p\n", addr ^ (1UL << i));
3470 
3471 	return (DCMD_OK);
3472 }
3473 
3474 /*
3475  * Grumble, grumble.
3476  */
3477 #define	SMAP_HASHFUNC(vp, off)	\
3478 	((((uintptr_t)(vp) >> 6) + ((uintptr_t)(vp) >> 3) + \
3479 	((off) >> MAXBSHIFT)) & smd_hashmsk)
3480 
3481 int
3482 vnode2smap(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
3483 {
3484 	long smd_hashmsk;
3485 	int hash;
3486 	uintptr_t offset = 0;
3487 	struct smap smp;
3488 	uintptr_t saddr, kaddr;
3489 	uintptr_t smd_hash, smd_smap;
3490 	struct seg seg;
3491 
3492 	if (!(flags & DCMD_ADDRSPEC))
3493 		return (DCMD_USAGE);
3494 
3495 	if (mdb_readvar(&smd_hashmsk, "smd_hashmsk") == -1) {
3496 		mdb_warn("failed to read smd_hashmsk");
3497 		return (DCMD_ERR);
3498 	}
3499 
3500 	if (mdb_readvar(&smd_hash, "smd_hash") == -1) {
3501 		mdb_warn("failed to read smd_hash");
3502 		return (DCMD_ERR);
3503 	}
3504 
3505 	if (mdb_readvar(&smd_smap, "smd_smap") == -1) {
3506 		mdb_warn("failed to read smd_hash");
3507 		return (DCMD_ERR);
3508 	}
3509 
3510 	if (mdb_readvar(&kaddr, "segkmap") == -1) {
3511 		mdb_warn("failed to read segkmap");
3512 		return (DCMD_ERR);
3513 	}
3514 
3515 	if (mdb_vread(&seg, sizeof (seg), kaddr) == -1) {
3516 		mdb_warn("failed to read segkmap at %p", kaddr);
3517 		return (DCMD_ERR);
3518 	}
3519 
3520 	if (argc != 0) {
3521 		const mdb_arg_t *arg = &argv[0];
3522 
3523 		if (arg->a_type == MDB_TYPE_IMMEDIATE)
3524 			offset = arg->a_un.a_val;
3525 		else
3526 			offset = (uintptr_t)mdb_strtoull(arg->a_un.a_str);
3527 	}
3528 
3529 	hash = SMAP_HASHFUNC(addr, offset);
3530 
3531 	if (mdb_vread(&saddr, sizeof (saddr),
3532 	    smd_hash + hash * sizeof (uintptr_t)) == -1) {
3533 		mdb_warn("couldn't read smap at %p",
3534 		    smd_hash + hash * sizeof (uintptr_t));
3535 		return (DCMD_ERR);
3536 	}
3537 
3538 	do {
3539 		if (mdb_vread(&smp, sizeof (smp), saddr) == -1) {
3540 			mdb_warn("couldn't read smap at %p", saddr);
3541 			return (DCMD_ERR);
3542 		}
3543 
3544 		if ((uintptr_t)smp.sm_vp == addr && smp.sm_off == offset) {
3545 			mdb_printf("vnode %p, offs %p is smap %p, vaddr %p\n",
3546 			    addr, offset, saddr, ((saddr - smd_smap) /
3547 			    sizeof (smp)) * MAXBSIZE + seg.s_base);
3548 			return (DCMD_OK);
3549 		}
3550 
3551 		saddr = (uintptr_t)smp.sm_hash;
3552 	} while (saddr != NULL);
3553 
3554 	mdb_printf("no smap for vnode %p, offs %p\n", addr, offset);
3555 	return (DCMD_OK);
3556 }
3557 
3558 /*ARGSUSED*/
3559 int
3560 addr2smap(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
3561 {
3562 	uintptr_t kaddr;
3563 	struct seg seg;
3564 	struct segmap_data sd;
3565 
3566 	if (!(flags & DCMD_ADDRSPEC))
3567 		return (DCMD_USAGE);
3568 
3569 	if (mdb_readvar(&kaddr, "segkmap") == -1) {
3570 		mdb_warn("failed to read segkmap");
3571 		return (DCMD_ERR);
3572 	}
3573 
3574 	if (mdb_vread(&seg, sizeof (seg), kaddr) == -1) {
3575 		mdb_warn("failed to read segkmap at %p", kaddr);
3576 		return (DCMD_ERR);
3577 	}
3578 
3579 	if (mdb_vread(&sd, sizeof (sd), (uintptr_t)seg.s_data) == -1) {
3580 		mdb_warn("failed to read segmap_data at %p", seg.s_data);
3581 		return (DCMD_ERR);
3582 	}
3583 
3584 	mdb_printf("%p is smap %p\n", addr,
3585 	    ((addr - (uintptr_t)seg.s_base) >> MAXBSHIFT) *
3586 	    sizeof (struct smap) + (uintptr_t)sd.smd_sm);
3587 
3588 	return (DCMD_OK);
3589 }
3590 
3591 int
3592 as2proc_walk(uintptr_t addr, const proc_t *p, struct as **asp)
3593 {
3594 	if (p->p_as == *asp)
3595 		mdb_printf("%p\n", addr);
3596 	return (WALK_NEXT);
3597 }
3598 
3599 /*ARGSUSED*/
3600 int
3601 as2proc(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
3602 {
3603 	if (!(flags & DCMD_ADDRSPEC) || argc != 0)
3604 		return (DCMD_USAGE);
3605 
3606 	if (mdb_walk("proc", (mdb_walk_cb_t)as2proc_walk, &addr) == -1) {
3607 		mdb_warn("failed to walk proc");
3608 		return (DCMD_ERR);
3609 	}
3610 
3611 	return (DCMD_OK);
3612 }
3613 
3614 /*ARGSUSED*/
3615 int
3616 ptree_walk(uintptr_t addr, const proc_t *p, void *ignored)
3617 {
3618 	proc_t parent;
3619 	int ident = 0;
3620 	uintptr_t paddr;
3621 
3622 	for (paddr = (uintptr_t)p->p_parent; paddr != NULL; ident += 5) {
3623 		mdb_vread(&parent, sizeof (parent), paddr);
3624 		paddr = (uintptr_t)parent.p_parent;
3625 	}
3626 
3627 	mdb_inc_indent(ident);
3628 	mdb_printf("%0?p  %s\n", addr, p->p_user.u_comm);
3629 	mdb_dec_indent(ident);
3630 
3631 	return (WALK_NEXT);
3632 }
3633 
3634 void
3635 ptree_ancestors(uintptr_t addr, uintptr_t start)
3636 {
3637 	proc_t p;
3638 
3639 	if (mdb_vread(&p, sizeof (p), addr) == -1) {
3640 		mdb_warn("couldn't read ancestor at %p", addr);
3641 		return;
3642 	}
3643 
3644 	if (p.p_parent != NULL)
3645 		ptree_ancestors((uintptr_t)p.p_parent, start);
3646 
3647 	if (addr != start)
3648 		(void) ptree_walk(addr, &p, NULL);
3649 }
3650 
3651 /*ARGSUSED*/
3652 int
3653 ptree(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
3654 {
3655 	if (!(flags & DCMD_ADDRSPEC))
3656 		addr = NULL;
3657 	else
3658 		ptree_ancestors(addr, addr);
3659 
3660 	if (mdb_pwalk("proc", (mdb_walk_cb_t)ptree_walk, NULL, addr) == -1) {
3661 		mdb_warn("couldn't walk 'proc'");
3662 		return (DCMD_ERR);
3663 	}
3664 
3665 	return (DCMD_OK);
3666 }
3667 
3668 /*ARGSUSED*/
3669 static int
3670 fd(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
3671 {
3672 	int fdnum;
3673 	const mdb_arg_t *argp = &argv[0];
3674 	proc_t p;
3675 	uf_entry_t uf;
3676 
3677 	if ((flags & DCMD_ADDRSPEC) == 0) {
3678 		mdb_warn("fd doesn't give global information\n");
3679 		return (DCMD_ERR);
3680 	}
3681 	if (argc != 1)
3682 		return (DCMD_USAGE);
3683 
3684 	if (argp->a_type == MDB_TYPE_IMMEDIATE)
3685 		fdnum = argp->a_un.a_val;
3686 	else
3687 		fdnum = mdb_strtoull(argp->a_un.a_str);
3688 
3689 	if (mdb_vread(&p, sizeof (struct proc), addr) == -1) {
3690 		mdb_warn("couldn't read proc_t at %p", addr);
3691 		return (DCMD_ERR);
3692 	}
3693 	if (fdnum > p.p_user.u_finfo.fi_nfiles) {
3694 		mdb_warn("process %p only has %d files open.\n",
3695 		    addr, p.p_user.u_finfo.fi_nfiles);
3696 		return (DCMD_ERR);
3697 	}
3698 	if (mdb_vread(&uf, sizeof (uf_entry_t),
3699 	    (uintptr_t)&p.p_user.u_finfo.fi_list[fdnum]) == -1) {
3700 		mdb_warn("couldn't read uf_entry_t at %p",
3701 		    &p.p_user.u_finfo.fi_list[fdnum]);
3702 		return (DCMD_ERR);
3703 	}
3704 
3705 	mdb_printf("%p\n", uf.uf_file);
3706 	return (DCMD_OK);
3707 }
3708 
3709 /*ARGSUSED*/
3710 static int
3711 pid2proc(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
3712 {
3713 	pid_t pid = (pid_t)addr;
3714 
3715 	if (argc != 0)
3716 		return (DCMD_USAGE);
3717 
3718 	if ((addr = mdb_pid2proc(pid, NULL)) == NULL) {
3719 		mdb_warn("PID 0t%d not found\n", pid);
3720 		return (DCMD_ERR);
3721 	}
3722 
3723 	mdb_printf("%p\n", addr);
3724 	return (DCMD_OK);
3725 }
3726 
3727 static char *sysfile_cmd[] = {
3728 	"exclude:",
3729 	"include:",
3730 	"forceload:",
3731 	"rootdev:",
3732 	"rootfs:",
3733 	"swapdev:",
3734 	"swapfs:",
3735 	"moddir:",
3736 	"set",
3737 	"unknown",
3738 };
3739 
3740 static char *sysfile_ops[] = { "", "=", "&", "|" };
3741 
3742 /*ARGSUSED*/
3743 static int
3744 sysfile_vmem_seg(uintptr_t addr, const vmem_seg_t *vsp, void **target)
3745 {
3746 	if (vsp->vs_type == VMEM_ALLOC && (void *)vsp->vs_start == *target) {
3747 		*target = NULL;
3748 		return (WALK_DONE);
3749 	}
3750 	return (WALK_NEXT);
3751 }
3752 
3753 /*ARGSUSED*/
3754 static int
3755 sysfile(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
3756 {
3757 	struct sysparam *sysp, sys;
3758 	char var[256];
3759 	char modname[256];
3760 	char val[256];
3761 	char strval[256];
3762 	vmem_t *mod_sysfile_arena;
3763 	void *straddr;
3764 
3765 	if (mdb_readvar(&sysp, "sysparam_hd") == -1) {
3766 		mdb_warn("failed to read sysparam_hd");
3767 		return (DCMD_ERR);
3768 	}
3769 
3770 	if (mdb_readvar(&mod_sysfile_arena, "mod_sysfile_arena") == -1) {
3771 		mdb_warn("failed to read mod_sysfile_arena");
3772 		return (DCMD_ERR);
3773 	}
3774 
3775 	while (sysp != NULL) {
3776 		var[0] = '\0';
3777 		val[0] = '\0';
3778 		modname[0] = '\0';
3779 		if (mdb_vread(&sys, sizeof (sys), (uintptr_t)sysp) == -1) {
3780 			mdb_warn("couldn't read sysparam %p", sysp);
3781 			return (DCMD_ERR);
3782 		}
3783 		if (sys.sys_modnam != NULL &&
3784 		    mdb_readstr(modname, 256,
3785 		    (uintptr_t)sys.sys_modnam) == -1) {
3786 			mdb_warn("couldn't read modname in %p", sysp);
3787 			return (DCMD_ERR);
3788 		}
3789 		if (sys.sys_ptr != NULL &&
3790 		    mdb_readstr(var, 256, (uintptr_t)sys.sys_ptr) == -1) {
3791 			mdb_warn("couldn't read ptr in %p", sysp);
3792 			return (DCMD_ERR);
3793 		}
3794 		if (sys.sys_op != SETOP_NONE) {
3795 			/*
3796 			 * Is this an int or a string?  We determine this
3797 			 * by checking whether straddr is contained in
3798 			 * mod_sysfile_arena.  If so, the walker will set
3799 			 * straddr to NULL.
3800 			 */
3801 			straddr = (void *)(uintptr_t)sys.sys_info;
3802 			if (sys.sys_op == SETOP_ASSIGN &&
3803 			    sys.sys_info != 0 &&
3804 			    mdb_pwalk("vmem_seg",
3805 			    (mdb_walk_cb_t)sysfile_vmem_seg, &straddr,
3806 			    (uintptr_t)mod_sysfile_arena) == 0 &&
3807 			    straddr == NULL &&
3808 			    mdb_readstr(strval, 256,
3809 			    (uintptr_t)sys.sys_info) != -1) {
3810 				(void) mdb_snprintf(val, sizeof (val), "\"%s\"",
3811 				    strval);
3812 			} else {
3813 				(void) mdb_snprintf(val, sizeof (val),
3814 				    "0x%llx [0t%llu]", sys.sys_info,
3815 				    sys.sys_info);
3816 			}
3817 		}
3818 		mdb_printf("%s %s%s%s%s%s\n", sysfile_cmd[sys.sys_type],
3819 		    modname, modname[0] == '\0' ? "" : ":",
3820 		    var, sysfile_ops[sys.sys_op], val);
3821 
3822 		sysp = sys.sys_next;
3823 	}
3824 
3825 	return (DCMD_OK);
3826 }
3827 
3828 int
3829 didmatch(uintptr_t addr, const kthread_t *thr, kt_did_t *didp)
3830 {
3831 
3832 	if (*didp == thr->t_did) {
3833 		mdb_printf("%p\n", addr);
3834 		return (WALK_DONE);
3835 	} else
3836 		return (WALK_NEXT);
3837 }
3838 
3839 /*ARGSUSED*/
3840 int
3841 did2thread(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
3842 {
3843 	const mdb_arg_t *argp = &argv[0];
3844 	kt_did_t	did;
3845 
3846 	if (argc != 1)
3847 		return (DCMD_USAGE);
3848 
3849 	did = (kt_did_t)mdb_strtoull(argp->a_un.a_str);
3850 
3851 	if (mdb_walk("thread", (mdb_walk_cb_t)didmatch, (void *)&did) == -1) {
3852 		mdb_warn("failed to walk thread");
3853 		return (DCMD_ERR);
3854 
3855 	}
3856 	return (DCMD_OK);
3857 
3858 }
3859 
3860 static int
3861 errorq_walk_init(mdb_walk_state_t *wsp)
3862 {
3863 	if (wsp->walk_addr == NULL &&
3864 	    mdb_readvar(&wsp->walk_addr, "errorq_list") == -1) {
3865 		mdb_warn("failed to read errorq_list");
3866 		return (WALK_ERR);
3867 	}
3868 
3869 	return (WALK_NEXT);
3870 }
3871 
3872 static int
3873 errorq_walk_step(mdb_walk_state_t *wsp)
3874 {
3875 	uintptr_t addr = wsp->walk_addr;
3876 	errorq_t eq;
3877 
3878 	if (addr == NULL)
3879 		return (WALK_DONE);
3880 
3881 	if (mdb_vread(&eq, sizeof (eq), addr) == -1) {
3882 		mdb_warn("failed to read errorq at %p", addr);
3883 		return (WALK_ERR);
3884 	}
3885 
3886 	wsp->walk_addr = (uintptr_t)eq.eq_next;
3887 	return (wsp->walk_callback(addr, &eq, wsp->walk_cbdata));
3888 }
3889 
3890 typedef struct eqd_walk_data {
3891 	uintptr_t *eqd_stack;
3892 	void *eqd_buf;
3893 	ulong_t eqd_qpos;
3894 	ulong_t eqd_qlen;
3895 	size_t eqd_size;
3896 } eqd_walk_data_t;
3897 
3898 /*
3899  * In order to walk the list of pending error queue elements, we push the
3900  * addresses of the corresponding data buffers in to the eqd_stack array.
3901  * The error lists are in reverse chronological order when iterating using
3902  * eqe_prev, so we then pop things off the top in eqd_walk_step so that the
3903  * walker client gets addresses in order from oldest error to newest error.
3904  */
3905 static void
3906 eqd_push_list(eqd_walk_data_t *eqdp, uintptr_t addr)
3907 {
3908 	errorq_elem_t eqe;
3909 
3910 	while (addr != NULL) {
3911 		if (mdb_vread(&eqe, sizeof (eqe), addr) != sizeof (eqe)) {
3912 			mdb_warn("failed to read errorq element at %p", addr);
3913 			break;
3914 		}
3915 
3916 		if (eqdp->eqd_qpos == eqdp->eqd_qlen) {
3917 			mdb_warn("errorq is overfull -- more than %lu "
3918 			    "elems found\n", eqdp->eqd_qlen);
3919 			break;
3920 		}
3921 
3922 		eqdp->eqd_stack[eqdp->eqd_qpos++] = (uintptr_t)eqe.eqe_data;
3923 		addr = (uintptr_t)eqe.eqe_prev;
3924 	}
3925 }
3926 
3927 static int
3928 eqd_walk_init(mdb_walk_state_t *wsp)
3929 {
3930 	eqd_walk_data_t *eqdp;
3931 	errorq_elem_t eqe, *addr;
3932 	errorq_t eq;
3933 	ulong_t i;
3934 
3935 	if (mdb_vread(&eq, sizeof (eq), wsp->walk_addr) == -1) {
3936 		mdb_warn("failed to read errorq at %p", wsp->walk_addr);
3937 		return (WALK_ERR);
3938 	}
3939 
3940 	if (eq.eq_ptail != NULL &&
3941 	    mdb_vread(&eqe, sizeof (eqe), (uintptr_t)eq.eq_ptail) == -1) {
3942 		mdb_warn("failed to read errorq element at %p", eq.eq_ptail);
3943 		return (WALK_ERR);
3944 	}
3945 
3946 	eqdp = mdb_alloc(sizeof (eqd_walk_data_t), UM_SLEEP);
3947 	wsp->walk_data = eqdp;
3948 
3949 	eqdp->eqd_stack = mdb_zalloc(sizeof (uintptr_t) * eq.eq_qlen, UM_SLEEP);
3950 	eqdp->eqd_buf = mdb_alloc(eq.eq_size, UM_SLEEP);
3951 	eqdp->eqd_qlen = eq.eq_qlen;
3952 	eqdp->eqd_qpos = 0;
3953 	eqdp->eqd_size = eq.eq_size;
3954 
3955 	/*
3956 	 * The newest elements in the queue are on the pending list, so we
3957 	 * push those on to our stack first.
3958 	 */
3959 	eqd_push_list(eqdp, (uintptr_t)eq.eq_pend);
3960 
3961 	/*
3962 	 * If eq_ptail is set, it may point to a subset of the errors on the
3963 	 * pending list in the event a casptr() failed; if ptail's data is
3964 	 * already in our stack, NULL out eq_ptail and ignore it.
3965 	 */
3966 	if (eq.eq_ptail != NULL) {
3967 		for (i = 0; i < eqdp->eqd_qpos; i++) {
3968 			if (eqdp->eqd_stack[i] == (uintptr_t)eqe.eqe_data) {
3969 				eq.eq_ptail = NULL;
3970 				break;
3971 			}
3972 		}
3973 	}
3974 
3975 	/*
3976 	 * If eq_phead is set, it has the processing list in order from oldest
3977 	 * to newest.  Use this to recompute eq_ptail as best we can and then
3978 	 * we nicely fall into eqd_push_list() of eq_ptail below.
3979 	 */
3980 	for (addr = eq.eq_phead; addr != NULL && mdb_vread(&eqe, sizeof (eqe),
3981 	    (uintptr_t)addr) == sizeof (eqe); addr = eqe.eqe_next)
3982 		eq.eq_ptail = addr;
3983 
3984 	/*
3985 	 * The oldest elements in the queue are on the processing list, subject
3986 	 * to machinations in the if-clauses above.  Push any such elements.
3987 	 */
3988 	eqd_push_list(eqdp, (uintptr_t)eq.eq_ptail);
3989 	return (WALK_NEXT);
3990 }
3991 
3992 static int
3993 eqd_walk_step(mdb_walk_state_t *wsp)
3994 {
3995 	eqd_walk_data_t *eqdp = wsp->walk_data;
3996 	uintptr_t addr;
3997 
3998 	if (eqdp->eqd_qpos == 0)
3999 		return (WALK_DONE);
4000 
4001 	addr = eqdp->eqd_stack[--eqdp->eqd_qpos];
4002 
4003 	if (mdb_vread(eqdp->eqd_buf, eqdp->eqd_size, addr) != eqdp->eqd_size) {
4004 		mdb_warn("failed to read errorq data at %p", addr);
4005 		return (WALK_ERR);
4006 	}
4007 
4008 	return (wsp->walk_callback(addr, eqdp->eqd_buf, wsp->walk_cbdata));
4009 }
4010 
4011 static void
4012 eqd_walk_fini(mdb_walk_state_t *wsp)
4013 {
4014 	eqd_walk_data_t *eqdp = wsp->walk_data;
4015 
4016 	mdb_free(eqdp->eqd_stack, sizeof (uintptr_t) * eqdp->eqd_qlen);
4017 	mdb_free(eqdp->eqd_buf, eqdp->eqd_size);
4018 	mdb_free(eqdp, sizeof (eqd_walk_data_t));
4019 }
4020 
4021 #define	EQKSVAL(eqv, what) (eqv.eq_kstat.what.value.ui64)
4022 
4023 static int
4024 errorq(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
4025 {
4026 	int i;
4027 	errorq_t eq;
4028 	uint_t opt_v = FALSE;
4029 
4030 	if (!(flags & DCMD_ADDRSPEC)) {
4031 		if (mdb_walk_dcmd("errorq", "errorq", argc, argv) == -1) {
4032 			mdb_warn("can't walk 'errorq'");
4033 			return (DCMD_ERR);
4034 		}
4035 		return (DCMD_OK);
4036 	}
4037 
4038 	i = mdb_getopts(argc, argv, 'v', MDB_OPT_SETBITS, TRUE, &opt_v, NULL);
4039 	argc -= i;
4040 	argv += i;
4041 
4042 	if (argc != 0)
4043 		return (DCMD_USAGE);
4044 
4045 	if (opt_v || DCMD_HDRSPEC(flags)) {
4046 		mdb_printf("%<u>%-11s %-16s %1s %1s %1s ",
4047 		    "ADDR", "NAME", "S", "V", "N");
4048 		if (!opt_v) {
4049 			mdb_printf("%7s %7s %7s%</u>\n",
4050 			    "ACCEPT", "DROP", "LOG");
4051 		} else {
4052 			mdb_printf("%5s %6s %6s %3s %16s%</u>\n",
4053 			    "KSTAT", "QLEN", "SIZE", "IPL", "FUNC");
4054 		}
4055 	}
4056 
4057 	if (mdb_vread(&eq, sizeof (eq), addr) != sizeof (eq)) {
4058 		mdb_warn("failed to read errorq at %p", addr);
4059 		return (DCMD_ERR);
4060 	}
4061 
4062 	mdb_printf("%-11p %-16s %c %c %c ", addr, eq.eq_name,
4063 	    (eq.eq_flags & ERRORQ_ACTIVE) ? '+' : '-',
4064 	    (eq.eq_flags & ERRORQ_VITAL) ? '!' : ' ',
4065 	    (eq.eq_flags & ERRORQ_NVLIST) ? '*' : ' ');
4066 
4067 	if (!opt_v) {
4068 		mdb_printf("%7llu %7llu %7llu\n",
4069 		    EQKSVAL(eq, eqk_dispatched) + EQKSVAL(eq, eqk_committed),
4070 		    EQKSVAL(eq, eqk_dropped) + EQKSVAL(eq, eqk_reserve_fail) +
4071 		    EQKSVAL(eq, eqk_commit_fail), EQKSVAL(eq, eqk_logged));
4072 	} else {
4073 		mdb_printf("%5s %6lu %6lu %3u %a\n",
4074 		    "  |  ", eq.eq_qlen, eq.eq_size, eq.eq_ipl, eq.eq_func);
4075 		mdb_printf("%38s\n%41s"
4076 		    "%12s %llu\n"
4077 		    "%53s %llu\n"
4078 		    "%53s %llu\n"
4079 		    "%53s %llu\n"
4080 		    "%53s %llu\n"
4081 		    "%53s %llu\n"
4082 		    "%53s %llu\n"
4083 		    "%53s %llu\n\n",
4084 		    "|", "+-> ",
4085 		    "DISPATCHED",	EQKSVAL(eq, eqk_dispatched),
4086 		    "DROPPED",		EQKSVAL(eq, eqk_dropped),
4087 		    "LOGGED",		EQKSVAL(eq, eqk_logged),
4088 		    "RESERVED",		EQKSVAL(eq, eqk_reserved),
4089 		    "RESERVE FAIL",	EQKSVAL(eq, eqk_reserve_fail),
4090 		    "COMMITTED",	EQKSVAL(eq, eqk_committed),
4091 		    "COMMIT FAIL",	EQKSVAL(eq, eqk_commit_fail),
4092 		    "CANCELLED",	EQKSVAL(eq, eqk_cancelled));
4093 	}
4094 
4095 	return (DCMD_OK);
4096 }
4097 
4098 /*ARGSUSED*/
4099 static int
4100 panicinfo(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
4101 {
4102 	cpu_t panic_cpu;
4103 	kthread_t *panic_thread;
4104 	void *buf;
4105 	panic_data_t *pd;
4106 	int i, n;
4107 
4108 	if (!mdb_prop_postmortem) {
4109 		mdb_warn("panicinfo can only be run on a system "
4110 		    "dump; see dumpadm(1M)\n");
4111 		return (DCMD_ERR);
4112 	}
4113 
4114 	if (flags & DCMD_ADDRSPEC || argc != 0)
4115 		return (DCMD_USAGE);
4116 
4117 	if (mdb_readsym(&panic_cpu, sizeof (cpu_t), "panic_cpu") == -1)
4118 		mdb_warn("failed to read 'panic_cpu'");
4119 	else
4120 		mdb_printf("%16s %?d\n", "cpu", panic_cpu.cpu_id);
4121 
4122 	if (mdb_readvar(&panic_thread, "panic_thread") == -1)
4123 		mdb_warn("failed to read 'panic_thread'");
4124 	else
4125 		mdb_printf("%16s %?p\n", "thread", panic_thread);
4126 
4127 	buf = mdb_alloc(PANICBUFSIZE, UM_SLEEP);
4128 	pd = (panic_data_t *)buf;
4129 
4130 	if (mdb_readsym(buf, PANICBUFSIZE, "panicbuf") == -1 ||
4131 	    pd->pd_version != PANICBUFVERS) {
4132 		mdb_warn("failed to read 'panicbuf'");
4133 		mdb_free(buf, PANICBUFSIZE);
4134 		return (DCMD_ERR);
4135 	}
4136 
4137 	mdb_printf("%16s %s\n", "message",  (char *)buf + pd->pd_msgoff);
4138 
4139 	n = (pd->pd_msgoff - (sizeof (panic_data_t) -
4140 	    sizeof (panic_nv_t))) / sizeof (panic_nv_t);
4141 
4142 	for (i = 0; i < n; i++)
4143 		mdb_printf("%16s %?llx\n",
4144 		    pd->pd_nvdata[i].pnv_name, pd->pd_nvdata[i].pnv_value);
4145 
4146 	mdb_free(buf, PANICBUFSIZE);
4147 	return (DCMD_OK);
4148 }
4149 
4150 /*
4151  * ::time dcmd, which will print a hires timestamp of when we entered the
4152  * debugger, or the lbolt value if used with the -l option.
4153  *
4154  */
4155 /*ARGSUSED*/
4156 static int
4157 time(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
4158 {
4159 	uint_t opt_lbolt = FALSE;
4160 
4161 	if (mdb_getopts(argc, argv, 'l', MDB_OPT_SETBITS, TRUE, &opt_lbolt,
4162 	    NULL) != argc)
4163 		return (DCMD_USAGE);
4164 
4165 	if (opt_lbolt)
4166 		mdb_printf("%ld\n", mdb_get_lbolt());
4167 	else
4168 		mdb_printf("%lld\n", mdb_gethrtime());
4169 
4170 	return (DCMD_OK);
4171 }
4172 
4173 void
4174 time_help(void)
4175 {
4176 	mdb_printf("Prints the system time in nanoseconds.\n\n"
4177 	    "::time will return the timestamp at which we dropped into, \n"
4178 	    "if called from, kmdb(1); the core dump's high resolution \n"
4179 	    "time if inspecting one; or the running hires time if we're \n"
4180 	    "looking at a live system.\n\n"
4181 	    "Switches:\n"
4182 	    "  -l   prints the number of clock ticks since system boot\n");
4183 }
4184 
4185 static const mdb_dcmd_t dcmds[] = {
4186 
4187 	/* from genunix.c */
4188 	{ "addr2smap", ":[offset]", "translate address to smap", addr2smap },
4189 	{ "as2proc", ":", "convert as to proc_t address", as2proc },
4190 	{ "binding_hash_entry", ":", "print driver names hash table entry",
4191 		binding_hash_entry },
4192 	{ "callout", "?[-r|n] [-s|l] [-xhB] [-t | -ab nsec [-dkD]]"
4193 	    " [-C addr | -S seqid] [-f name|addr] [-p name| addr] [-T|L [-E]]"
4194 	    " [-FivVA]",
4195 	    "display callouts", callout, callout_help },
4196 	{ "calloutid", "[-d|v] xid", "print callout by extended id",
4197 	    calloutid, calloutid_help },
4198 	{ "class", NULL, "print process scheduler classes", class },
4199 	{ "cpuinfo", "?[-v]", "print CPUs and runnable threads", cpuinfo },
4200 	{ "did2thread", "? kt_did", "find kernel thread for this id",
4201 		did2thread },
4202 	{ "errorq", "?[-v]", "display kernel error queues", errorq },
4203 	{ "fd", ":[fd num]", "get a file pointer from an fd", fd },
4204 	{ "flipone", ":", "the vik_rev_level 2 special", flipone },
4205 	{ "lminfo", NULL, "print lock manager information", lminfo },
4206 	{ "ndi_event_hdl", "?", "print ndi_event_hdl", ndi_event_hdl },
4207 	{ "panicinfo", NULL, "print panic information", panicinfo },
4208 	{ "pid2proc", "?", "convert PID to proc_t address", pid2proc },
4209 	{ "pmap", ":[-q]", "print process memory map", pmap },
4210 	{ "project", NULL, "display kernel project(s)", project },
4211 	{ "ps", "[-fltzTP]", "list processes (and associated thr,lwp)", ps },
4212 	{ "pgrep", "[-x] [-n | -o] pattern",
4213 		"pattern match against all processes", pgrep },
4214 	{ "ptree", NULL, "print process tree", ptree },
4215 	{ "seg", ":", "print address space segment", seg },
4216 	{ "sysevent", "?[-sv]", "print sysevent pending or sent queue",
4217 		sysevent},
4218 	{ "sysevent_channel", "?", "print sysevent channel database",
4219 		sysevent_channel},
4220 	{ "sysevent_class_list", ":", "print sysevent class list",
4221 		sysevent_class_list},
4222 	{ "sysevent_subclass_list", ":",
4223 		"print sysevent subclass list", sysevent_subclass_list},
4224 	{ "system", NULL, "print contents of /etc/system file", sysfile },
4225 	{ "task", NULL, "display kernel task(s)", task },
4226 	{ "time", "[-l]", "display system time", time, time_help },
4227 	{ "vnode2path", ":[-F]", "vnode address to pathname", vnode2path },
4228 	{ "vnode2smap", ":[offset]", "translate vnode to smap", vnode2smap },
4229 	{ "whereopen", ":", "given a vnode, dumps procs which have it open",
4230 	    whereopen },
4231 
4232 	/* from bio.c */
4233 	{ "bufpagefind", ":addr", "find page_t on buf_t list", bufpagefind },
4234 
4235 	/* from bitset.c */
4236 	{ "bitset", ":", "display a bitset", bitset, bitset_help },
4237 
4238 	/* from contract.c */
4239 	{ "contract", "?", "display a contract", cmd_contract },
4240 	{ "ctevent", ":", "display a contract event", cmd_ctevent },
4241 	{ "ctid", ":", "convert id to a contract pointer", cmd_ctid },
4242 
4243 	/* from cpupart.c */
4244 	{ "cpupart", "?[-v]", "print cpu partition info", cpupart },
4245 
4246 	/* from cyclic.c */
4247 	{ "cyccover", NULL, "dump cyclic coverage information", cyccover },
4248 	{ "cycid", "?", "dump a cyclic id", cycid },
4249 	{ "cycinfo", "?", "dump cyc_cpu info", cycinfo },
4250 	{ "cyclic", ":", "developer information", cyclic },
4251 	{ "cyctrace", "?", "dump cyclic trace buffer", cyctrace },
4252 
4253 	/* from damap.c */
4254 	{ "damap", ":", "display a damap_t", damap, damap_help },
4255 
4256 	/* from devinfo.c */
4257 	{ "devbindings", "?[-qs] [device-name | major-num]",
4258 	    "print devinfo nodes bound to device-name or major-num",
4259 	    devbindings, devinfo_help },
4260 	{ "devinfo", ":[-qs]", "detailed devinfo of one node", devinfo,
4261 	    devinfo_help },
4262 	{ "devinfo_audit", ":[-v]", "devinfo configuration audit record",
4263 	    devinfo_audit },
4264 	{ "devinfo_audit_log", "?[-v]", "system wide devinfo configuration log",
4265 	    devinfo_audit_log },
4266 	{ "devinfo_audit_node", ":[-v]", "devinfo node configuration history",
4267 	    devinfo_audit_node },
4268 	{ "devinfo2driver", ":", "find driver name for this devinfo node",
4269 	    devinfo2driver },
4270 	{ "devnames", "?[-vm] [num]", "print devnames array", devnames },
4271 	{ "dev2major", "?<dev_t>", "convert dev_t to a major number",
4272 	    dev2major },
4273 	{ "dev2minor", "?<dev_t>", "convert dev_t to a minor number",
4274 	    dev2minor },
4275 	{ "devt", "?<dev_t>", "display a dev_t's major and minor numbers",
4276 	    devt },
4277 	{ "major2name", "?<major-num>", "convert major number to dev name",
4278 	    major2name },
4279 	{ "minornodes", ":", "given a devinfo node, print its minor nodes",
4280 	    minornodes },
4281 	{ "modctl2devinfo", ":", "given a modctl, list its devinfos",
4282 	    modctl2devinfo },
4283 	{ "name2major", "<dev-name>", "convert dev name to major number",
4284 	    name2major },
4285 	{ "prtconf", "?[-vpc]", "print devinfo tree", prtconf, prtconf_help },
4286 	{ "softstate", ":<instance>", "retrieve soft-state pointer",
4287 	    softstate },
4288 	{ "devinfo_fm", ":", "devinfo fault managment configuration",
4289 	    devinfo_fm },
4290 	{ "devinfo_fmce", ":", "devinfo fault managment cache entry",
4291 	    devinfo_fmce},
4292 
4293 	/* from fm.c */
4294 	{ "ereport", "[-v]", "print ereports logged in dump",
4295 	    ereport },
4296 
4297 	/* from findstack.c */
4298 	{ "findstack", ":[-v]", "find kernel thread stack", findstack },
4299 	{ "findstack_debug", NULL, "toggle findstack debugging",
4300 		findstack_debug },
4301 	{ "stacks", "?[-afiv] [-c func] [-C func] [-m module] [-M module] "
4302 		"[-s sobj | -S sobj] [-t tstate | -T tstate]",
4303 		"print unique kernel thread stacks",
4304 		stacks, stacks_help },
4305 
4306 	/* from group.c */
4307 	{ "group", "?[-q]", "display a group", group},
4308 
4309 	/* from irm.c */
4310 	{ "irmpools", NULL, "display interrupt pools", irmpools_dcmd },
4311 	{ "irmreqs", NULL, "display interrupt requests in an interrupt pool",
4312 	    irmreqs_dcmd },
4313 	{ "irmreq", NULL, "display an interrupt request", irmreq_dcmd },
4314 
4315 	/* from kgrep.c + genunix.c */
4316 	{ "kgrep", KGREP_USAGE, "search kernel as for a pointer", kgrep,
4317 		kgrep_help },
4318 
4319 	/* from kmem.c */
4320 	{ "allocdby", ":", "given a thread, print its allocated buffers",
4321 		allocdby },
4322 	{ "bufctl", ":[-vh] [-a addr] [-c caller] [-e earliest] [-l latest] "
4323 		"[-t thd]", "print or filter a bufctl", bufctl, bufctl_help },
4324 	{ "freedby", ":", "given a thread, print its freed buffers", freedby },
4325 	{ "kmalog", "?[ fail | slab ]",
4326 	    "display kmem transaction log and stack traces", kmalog },
4327 	{ "kmastat", "[-kmg]", "kernel memory allocator stats",
4328 	    kmastat },
4329 	{ "kmausers", "?[-ef] [cache ...]", "current medium and large users "
4330 		"of the kmem allocator", kmausers, kmausers_help },
4331 	{ "kmem_cache", "?[-n name]",
4332 		"print kernel memory caches", kmem_cache, kmem_cache_help},
4333 	{ "kmem_slabs", "?[-v] [-n cache] [-N cache] [-b maxbins] "
4334 		"[-B minbinsize]", "display slab usage per kmem cache",
4335 		kmem_slabs, kmem_slabs_help },
4336 	{ "kmem_debug", NULL, "toggle kmem dcmd/walk debugging", kmem_debug },
4337 	{ "kmem_log", "?[-b]", "dump kmem transaction log", kmem_log },
4338 	{ "kmem_verify", "?", "check integrity of kmem-managed memory",
4339 		kmem_verify },
4340 	{ "vmem", "?", "print a vmem_t", vmem },
4341 	{ "vmem_seg", ":[-sv] [-c caller] [-e earliest] [-l latest] "
4342 		"[-m minsize] [-M maxsize] [-t thread] [-T type]",
4343 		"print or filter a vmem_seg", vmem_seg, vmem_seg_help },
4344 	{ "whatthread", ":[-v]", "print threads whose stack contains the "
4345 		"given address", whatthread },
4346 
4347 	/* from ldi.c */
4348 	{ "ldi_handle", "?[-i]", "display a layered driver handle",
4349 	    ldi_handle, ldi_handle_help },
4350 	{ "ldi_ident", NULL, "display a layered driver identifier",
4351 	    ldi_ident, ldi_ident_help },
4352 
4353 	/* from leaky.c + leaky_subr.c */
4354 	{ "findleaks", FINDLEAKS_USAGE,
4355 	    "search for potential kernel memory leaks", findleaks,
4356 	    findleaks_help },
4357 
4358 	/* from lgrp.c */
4359 	{ "lgrp", "?[-q] [-p | -Pih]", "display an lgrp", lgrp},
4360 	{ "lgrp_set", "", "display bitmask of lgroups as a list", lgrp_set},
4361 
4362 	/* from log.c */
4363 	{ "msgbuf", "?[-v]", "print most recent console messages", msgbuf },
4364 
4365 	/* from mdi.c */
4366 	{ "mdipi", NULL, "given a path, dump mdi_pathinfo "
4367 		"and detailed pi_prop list", mdipi },
4368 	{ "mdiprops", NULL, "given a pi_prop, dump the pi_prop list",
4369 		mdiprops },
4370 	{ "mdiphci", NULL, "given a phci, dump mdi_phci and "
4371 		"list all paths", mdiphci },
4372 	{ "mdivhci", NULL, "given a vhci, dump mdi_vhci and list "
4373 		"all phcis", mdivhci },
4374 	{ "mdiclient_paths", NULL, "given a path, walk mdi_pathinfo "
4375 		"client links", mdiclient_paths },
4376 	{ "mdiphci_paths", NULL, "given a path, walk through mdi_pathinfo "
4377 		"phci links", mdiphci_paths },
4378 	{ "mdiphcis", NULL, "given a phci, walk through mdi_phci ph_next links",
4379 		mdiphcis },
4380 
4381 	/* from memory.c */
4382 	{ "page", "?", "display a summarized page_t", page },
4383 	{ "memstat", NULL, "display memory usage summary", memstat },
4384 	{ "memlist", "?[-iav]", "display a struct memlist", memlist },
4385 	{ "swapinfo", "?", "display a struct swapinfo", swapinfof },
4386 
4387 	/* from mmd.c */
4388 	{ "multidata", ":[-sv]", "display a summarized multidata_t",
4389 		multidata },
4390 	{ "pattbl", ":", "display a summarized multidata attribute table",
4391 		pattbl },
4392 	{ "pattr2multidata", ":", "print multidata pointer from pattr_t",
4393 		pattr2multidata },
4394 	{ "pdesc2slab", ":", "print pdesc slab pointer from pdesc_t",
4395 		pdesc2slab },
4396 	{ "pdesc_verify", ":", "verify integrity of a pdesc_t", pdesc_verify },
4397 	{ "slab2multidata", ":", "print multidata pointer from pdesc_slab_t",
4398 		slab2multidata },
4399 
4400 	/* from modhash.c */
4401 	{ "modhash", "?[-ceht] [-k key] [-v val] [-i index]",
4402 		"display information about one or all mod_hash structures",
4403 		modhash, modhash_help },
4404 	{ "modent", ":[-k | -v | -t type]",
4405 		"display information about a mod_hash_entry", modent,
4406 		modent_help },
4407 
4408 	/* from net.c */
4409 	{ "dladm", "?<sub-command> [flags]", "show data link information",
4410 		dladm, dladm_help },
4411 	{ "mi", ":[-p] [-d | -m]", "filter and display MI object or payload",
4412 		mi },
4413 	{ "netstat", "[-arv] [-f inet | inet6 | unix] [-P tcp | udp | icmp]",
4414 		"show network statistics", netstat },
4415 	{ "sonode", "?[-f inet | inet6 | unix | #] "
4416 		"[-t stream | dgram | raw | #] [-p #]",
4417 		"filter and display sonode", sonode },
4418 
4419 	/* from netstack.c */
4420 	{ "netstack", "", "show stack instances", netstack },
4421 
4422 	/* from nvpair.c */
4423 	{ NVPAIR_DCMD_NAME, NVPAIR_DCMD_USAGE, NVPAIR_DCMD_DESCR,
4424 		nvpair_print },
4425 	{ NVLIST_DCMD_NAME, NVLIST_DCMD_USAGE, NVLIST_DCMD_DESCR,
4426 		print_nvlist },
4427 
4428 	/* from pg.c */
4429 	{ "pg", "?[-q]", "display a pg", pg},
4430 
4431 	/* from rctl.c */
4432 	{ "rctl_dict", "?", "print systemwide default rctl definitions",
4433 		rctl_dict },
4434 	{ "rctl_list", ":[handle]", "print rctls for the given proc",
4435 		rctl_list },
4436 	{ "rctl", ":[handle]", "print a rctl_t, only if it matches the handle",
4437 		rctl },
4438 	{ "rctl_validate", ":[-v] [-n #]", "test resource control value "
4439 		"sequence", rctl_validate },
4440 
4441 	/* from sobj.c */
4442 	{ "rwlock", ":", "dump out a readers/writer lock", rwlock },
4443 	{ "mutex", ":[-f]", "dump out an adaptive or spin mutex", mutex,
4444 		mutex_help },
4445 	{ "sobj2ts", ":", "perform turnstile lookup on synch object", sobj2ts },
4446 	{ "wchaninfo", "?[-v]", "dump condition variable", wchaninfo },
4447 	{ "turnstile", "?", "display a turnstile", turnstile },
4448 
4449 	/* from stream.c */
4450 	{ "mblk", ":[-q|v] [-f|F flag] [-t|T type] [-l|L|B len] [-d dbaddr]",
4451 		"print an mblk", mblk_prt, mblk_help },
4452 	{ "mblk_verify", "?", "verify integrity of an mblk", mblk_verify },
4453 	{ "mblk2dblk", ":", "convert mblk_t address to dblk_t address",
4454 		mblk2dblk },
4455 	{ "q2otherq", ":", "print peer queue for a given queue", q2otherq },
4456 	{ "q2rdq", ":", "print read queue for a given queue", q2rdq },
4457 	{ "q2syncq", ":", "print syncq for a given queue", q2syncq },
4458 	{ "q2stream", ":", "print stream pointer for a given queue", q2stream },
4459 	{ "q2wrq", ":", "print write queue for a given queue", q2wrq },
4460 	{ "queue", ":[-q|v] [-m mod] [-f flag] [-F flag] [-s syncq_addr]",
4461 		"filter and display STREAM queue", queue, queue_help },
4462 	{ "stdata", ":[-q|v] [-f flag] [-F flag]",
4463 		"filter and display STREAM head", stdata, stdata_help },
4464 	{ "str2mate", ":", "print mate of this stream", str2mate },
4465 	{ "str2wrq", ":", "print write queue of this stream", str2wrq },
4466 	{ "stream", ":", "display STREAM", stream },
4467 	{ "strftevent", ":", "print STREAMS flow trace event", strftevent },
4468 	{ "syncq", ":[-q|v] [-f flag] [-F flag] [-t type] [-T type]",
4469 		"filter and display STREAM sync queue", syncq, syncq_help },
4470 	{ "syncq2q", ":", "print queue for a given syncq", syncq2q },
4471 
4472 	/* from taskq.c */
4473 	{ "taskq", ":[-atT] [-m min_maxq] [-n name]",
4474 	    "display a taskq", taskq, taskq_help },
4475 	{ "taskq_entry", ":", "display a taskq_ent_t", taskq_ent },
4476 
4477 	/* from thread.c */
4478 	{ "thread", "?[-bdfimps]", "display a summarized kthread_t", thread,
4479 		thread_help },
4480 	{ "threadlist", "?[-t] [-v [count]]",
4481 		"display threads and associated C stack traces", threadlist,
4482 		threadlist_help },
4483 	{ "stackinfo", "?[-h|-a]", "display kthread_t stack usage", stackinfo,
4484 		stackinfo_help },
4485 
4486 	/* from tsd.c */
4487 	{ "tsd", ":-k key", "print tsd[key-1] for this thread", ttotsd },
4488 	{ "tsdtot", ":", "find thread with this tsd", tsdtot },
4489 
4490 	/*
4491 	 * typegraph does not work under kmdb, as it requires too much memory
4492 	 * for its internal data structures.
4493 	 */
4494 #ifndef _KMDB
4495 	/* from typegraph.c */
4496 	{ "findlocks", ":", "find locks held by specified thread", findlocks },
4497 	{ "findfalse", "?[-v]", "find potentially falsely shared structures",
4498 		findfalse },
4499 	{ "typegraph", NULL, "build type graph", typegraph },
4500 	{ "istype", ":type", "manually set object type", istype },
4501 	{ "notype", ":", "manually clear object type", notype },
4502 	{ "whattype", ":", "determine object type", whattype },
4503 #endif
4504 
4505 	/* from vfs.c */
4506 	{ "fsinfo", "?[-v]", "print mounted filesystems", fsinfo },
4507 	{ "pfiles", ":[-fp]", "print process file information", pfiles,
4508 		pfiles_help },
4509 
4510 	/* from zone.c */
4511 	{ "zone", "?", "display kernel zone(s)", zoneprt },
4512 	{ "zsd", ":[-v] [zsd_key]", "display zone-specific-data entries for "
4513 	    "selected zones", zsd },
4514 
4515 	/* from hotplug.c */
4516 	{ "hotplug", "?[-p]", "display a registered hotplug attachment",
4517 	    hotplug, hotplug_help },
4518 
4519 	{ NULL }
4520 };
4521 
4522 static const mdb_walker_t walkers[] = {
4523 
4524 	/* from genunix.c */
4525 	{ "anon", "given an amp, list of anon structures",
4526 		anon_walk_init, anon_walk_step, anon_walk_fini },
4527 	{ "callouts_bytime", "walk callouts by list chain (expiration time)",
4528 		callout_walk_init, callout_walk_step, callout_walk_fini,
4529 		(void *)CALLOUT_WALK_BYLIST },
4530 	{ "callouts_byid", "walk callouts by id hash chain",
4531 		callout_walk_init, callout_walk_step, callout_walk_fini,
4532 		(void *)CALLOUT_WALK_BYID },
4533 	{ "callout_list", "walk a callout list", callout_list_walk_init,
4534 		callout_list_walk_step, callout_list_walk_fini },
4535 	{ "callout_table", "walk callout table array", callout_table_walk_init,
4536 		callout_table_walk_step, callout_table_walk_fini },
4537 	{ "cpu", "walk cpu structures", cpu_walk_init, cpu_walk_step },
4538 	{ "ereportq_dump", "walk list of ereports in dump error queue",
4539 		ereportq_dump_walk_init, ereportq_dump_walk_step, NULL },
4540 	{ "ereportq_pend", "walk list of ereports in pending error queue",
4541 		ereportq_pend_walk_init, ereportq_pend_walk_step, NULL },
4542 	{ "errorq", "walk list of system error queues",
4543 		errorq_walk_init, errorq_walk_step, NULL },
4544 	{ "errorq_data", "walk pending error queue data buffers",
4545 		eqd_walk_init, eqd_walk_step, eqd_walk_fini },
4546 	{ "allfile", "given a proc pointer, list all file pointers",
4547 		file_walk_init, allfile_walk_step, file_walk_fini },
4548 	{ "file", "given a proc pointer, list of open file pointers",
4549 		file_walk_init, file_walk_step, file_walk_fini },
4550 	{ "lock_descriptor", "walk lock_descriptor_t structures",
4551 		ld_walk_init, ld_walk_step, NULL },
4552 	{ "lock_graph", "walk lock graph",
4553 		lg_walk_init, lg_walk_step, NULL },
4554 	{ "port", "given a proc pointer, list of created event ports",
4555 		port_walk_init, port_walk_step, NULL },
4556 	{ "portev", "given a port pointer, list of events in the queue",
4557 		portev_walk_init, portev_walk_step, portev_walk_fini },
4558 	{ "proc", "list of active proc_t structures",
4559 		proc_walk_init, proc_walk_step, proc_walk_fini },
4560 	{ "projects", "walk a list of kernel projects",
4561 		project_walk_init, project_walk_step, NULL },
4562 	{ "seg", "given an as, list of segments",
4563 		seg_walk_init, avl_walk_step, avl_walk_fini },
4564 	{ "sysevent_pend", "walk sysevent pending queue",
4565 		sysevent_pend_walk_init, sysevent_walk_step,
4566 		sysevent_walk_fini},
4567 	{ "sysevent_sent", "walk sysevent sent queue", sysevent_sent_walk_init,
4568 		sysevent_walk_step, sysevent_walk_fini},
4569 	{ "sysevent_channel", "walk sysevent channel subscriptions",
4570 		sysevent_channel_walk_init, sysevent_channel_walk_step,
4571 		sysevent_channel_walk_fini},
4572 	{ "sysevent_class_list", "walk sysevent subscription's class list",
4573 		sysevent_class_list_walk_init, sysevent_class_list_walk_step,
4574 		sysevent_class_list_walk_fini},
4575 	{ "sysevent_subclass_list",
4576 		"walk sysevent subscription's subclass list",
4577 		sysevent_subclass_list_walk_init,
4578 		sysevent_subclass_list_walk_step,
4579 		sysevent_subclass_list_walk_fini},
4580 	{ "task", "given a task pointer, walk its processes",
4581 		task_walk_init, task_walk_step, NULL },
4582 
4583 	/* from avl.c */
4584 	{ AVL_WALK_NAME, AVL_WALK_DESC,
4585 		avl_walk_init, avl_walk_step, avl_walk_fini },
4586 
4587 	/* from bio.c */
4588 	{ "buf", "walk the bio buf hash",
4589 		buf_walk_init, buf_walk_step, buf_walk_fini },
4590 
4591 	/* from contract.c */
4592 	{ "contract", "walk all contracts, or those of the specified type",
4593 		ct_walk_init, generic_walk_step, NULL },
4594 	{ "ct_event", "walk events on a contract event queue",
4595 		ct_event_walk_init, generic_walk_step, NULL },
4596 	{ "ct_listener", "walk contract event queue listeners",
4597 		ct_listener_walk_init, generic_walk_step, NULL },
4598 
4599 	/* from cpupart.c */
4600 	{ "cpupart_cpulist", "given an cpupart_t, walk cpus in partition",
4601 		cpupart_cpulist_walk_init, cpupart_cpulist_walk_step,
4602 		NULL },
4603 	{ "cpupart_walk", "walk the set of cpu partitions",
4604 		cpupart_walk_init, cpupart_walk_step, NULL },
4605 
4606 	/* from ctxop.c */
4607 	{ "ctxop", "walk list of context ops on a thread",
4608 		ctxop_walk_init, ctxop_walk_step, ctxop_walk_fini },
4609 
4610 	/* from cyclic.c */
4611 	{ "cyccpu", "walk per-CPU cyc_cpu structures",
4612 		cyccpu_walk_init, cyccpu_walk_step, NULL },
4613 	{ "cycomni", "for an omnipresent cyclic, walk cyc_omni_cpu list",
4614 		cycomni_walk_init, cycomni_walk_step, NULL },
4615 	{ "cyctrace", "walk cyclic trace buffer",
4616 		cyctrace_walk_init, cyctrace_walk_step, cyctrace_walk_fini },
4617 
4618 	/* from devinfo.c */
4619 	{ "binding_hash", "walk all entries in binding hash table",
4620 		binding_hash_walk_init, binding_hash_walk_step, NULL },
4621 	{ "devinfo", "walk devinfo tree or subtree",
4622 		devinfo_walk_init, devinfo_walk_step, devinfo_walk_fini },
4623 	{ "devinfo_audit_log", "walk devinfo audit system-wide log",
4624 		devinfo_audit_log_walk_init, devinfo_audit_log_walk_step,
4625 		devinfo_audit_log_walk_fini},
4626 	{ "devinfo_audit_node", "walk per-devinfo audit history",
4627 		devinfo_audit_node_walk_init, devinfo_audit_node_walk_step,
4628 		devinfo_audit_node_walk_fini},
4629 	{ "devinfo_children", "walk children of devinfo node",
4630 		devinfo_children_walk_init, devinfo_children_walk_step,
4631 		devinfo_children_walk_fini },
4632 	{ "devinfo_parents", "walk ancestors of devinfo node",
4633 		devinfo_parents_walk_init, devinfo_parents_walk_step,
4634 		devinfo_parents_walk_fini },
4635 	{ "devinfo_siblings", "walk siblings of devinfo node",
4636 		devinfo_siblings_walk_init, devinfo_siblings_walk_step, NULL },
4637 	{ "devi_next", "walk devinfo list",
4638 		NULL, devi_next_walk_step, NULL },
4639 	{ "devnames", "walk devnames array",
4640 		devnames_walk_init, devnames_walk_step, devnames_walk_fini },
4641 	{ "minornode", "given a devinfo node, walk minor nodes",
4642 		minornode_walk_init, minornode_walk_step, NULL },
4643 	{ "softstate",
4644 		"given an i_ddi_soft_state*, list all in-use driver stateps",
4645 		soft_state_walk_init, soft_state_walk_step,
4646 		NULL, NULL },
4647 	{ "softstate_all",
4648 		"given an i_ddi_soft_state*, list all driver stateps",
4649 		soft_state_walk_init, soft_state_all_walk_step,
4650 		NULL, NULL },
4651 	{ "devinfo_fmc",
4652 		"walk a fault management handle cache active list",
4653 		devinfo_fmc_walk_init, devinfo_fmc_walk_step, NULL },
4654 
4655 	/* from group.c */
4656 	{ "group", "walk all elements of a group",
4657 		group_walk_init, group_walk_step, NULL },
4658 
4659 	/* from irm.c */
4660 	{ "irmpools", "walk global list of interrupt pools",
4661 	    irmpools_walk_init, list_walk_step, list_walk_fini },
4662 	{ "irmreqs", "walk list of interrupt requests in an interrupt pool",
4663 	    irmreqs_walk_init, list_walk_step, list_walk_fini },
4664 
4665 	/* from kmem.c */
4666 	{ "allocdby", "given a thread, walk its allocated bufctls",
4667 		allocdby_walk_init, allocdby_walk_step, allocdby_walk_fini },
4668 	{ "bufctl", "walk a kmem cache's bufctls",
4669 		bufctl_walk_init, kmem_walk_step, kmem_walk_fini },
4670 	{ "bufctl_history", "walk the available history of a bufctl",
4671 		bufctl_history_walk_init, bufctl_history_walk_step,
4672 		bufctl_history_walk_fini },
4673 	{ "freedby", "given a thread, walk its freed bufctls",
4674 		freedby_walk_init, allocdby_walk_step, allocdby_walk_fini },
4675 	{ "freectl", "walk a kmem cache's free bufctls",
4676 		freectl_walk_init, kmem_walk_step, kmem_walk_fini },
4677 	{ "freectl_constructed", "walk a kmem cache's constructed free bufctls",
4678 		freectl_constructed_walk_init, kmem_walk_step, kmem_walk_fini },
4679 	{ "freemem", "walk a kmem cache's free memory",
4680 		freemem_walk_init, kmem_walk_step, kmem_walk_fini },
4681 	{ "freemem_constructed", "walk a kmem cache's constructed free memory",
4682 		freemem_constructed_walk_init, kmem_walk_step, kmem_walk_fini },
4683 	{ "kmem", "walk a kmem cache",
4684 		kmem_walk_init, kmem_walk_step, kmem_walk_fini },
4685 	{ "kmem_cpu_cache", "given a kmem cache, walk its per-CPU caches",
4686 		kmem_cpu_cache_walk_init, kmem_cpu_cache_walk_step, NULL },
4687 	{ "kmem_hash", "given a kmem cache, walk its allocated hash table",
4688 		kmem_hash_walk_init, kmem_hash_walk_step, kmem_hash_walk_fini },
4689 	{ "kmem_log", "walk the kmem transaction log",
4690 		kmem_log_walk_init, kmem_log_walk_step, kmem_log_walk_fini },
4691 	{ "kmem_slab", "given a kmem cache, walk its slabs",
4692 		kmem_slab_walk_init, combined_walk_step, combined_walk_fini },
4693 	{ "kmem_slab_partial",
4694 	    "given a kmem cache, walk its partially allocated slabs (min 1)",
4695 		kmem_slab_walk_partial_init, combined_walk_step,
4696 		combined_walk_fini },
4697 	{ "vmem", "walk vmem structures in pre-fix, depth-first order",
4698 		vmem_walk_init, vmem_walk_step, vmem_walk_fini },
4699 	{ "vmem_alloc", "given a vmem_t, walk its allocated vmem_segs",
4700 		vmem_alloc_walk_init, vmem_seg_walk_step, vmem_seg_walk_fini },
4701 	{ "vmem_free", "given a vmem_t, walk its free vmem_segs",
4702 		vmem_free_walk_init, vmem_seg_walk_step, vmem_seg_walk_fini },
4703 	{ "vmem_postfix", "walk vmem structures in post-fix, depth-first order",
4704 		vmem_walk_init, vmem_postfix_walk_step, vmem_walk_fini },
4705 	{ "vmem_seg", "given a vmem_t, walk all of its vmem_segs",
4706 		vmem_seg_walk_init, vmem_seg_walk_step, vmem_seg_walk_fini },
4707 	{ "vmem_span", "given a vmem_t, walk its spanning vmem_segs",
4708 		vmem_span_walk_init, vmem_seg_walk_step, vmem_seg_walk_fini },
4709 
4710 	/* from ldi.c */
4711 	{ "ldi_handle", "walk the layered driver handle hash",
4712 		ldi_handle_walk_init, ldi_handle_walk_step, NULL },
4713 	{ "ldi_ident", "walk the layered driver identifier hash",
4714 		ldi_ident_walk_init, ldi_ident_walk_step, NULL },
4715 
4716 	/* from leaky.c + leaky_subr.c */
4717 	{ "leak", "given a leaked bufctl or vmem_seg, find leaks w/ same "
4718 	    "stack trace",
4719 		leaky_walk_init, leaky_walk_step, leaky_walk_fini },
4720 	{ "leakbuf", "given a leaked bufctl or vmem_seg, walk buffers for "
4721 	    "leaks w/ same stack trace",
4722 		leaky_walk_init, leaky_buf_walk_step, leaky_walk_fini },
4723 
4724 	/* from lgrp.c */
4725 	{ "lgrp_cpulist", "walk CPUs in a given lgroup",
4726 		lgrp_cpulist_walk_init, lgrp_cpulist_walk_step, NULL },
4727 	{ "lgrptbl", "walk lgroup table",
4728 		lgrp_walk_init, lgrp_walk_step, NULL },
4729 	{ "lgrp_parents", "walk up lgroup lineage from given lgroup",
4730 		lgrp_parents_walk_init, lgrp_parents_walk_step, NULL },
4731 	{ "lgrp_rsrc_mem", "walk lgroup memory resources of given lgroup",
4732 		lgrp_rsrc_mem_walk_init, lgrp_set_walk_step, NULL },
4733 	{ "lgrp_rsrc_cpu", "walk lgroup CPU resources of given lgroup",
4734 		lgrp_rsrc_cpu_walk_init, lgrp_set_walk_step, NULL },
4735 
4736 	/* from list.c */
4737 	{ LIST_WALK_NAME, LIST_WALK_DESC,
4738 		list_walk_init, list_walk_step, list_walk_fini },
4739 
4740 	/* from mdi.c */
4741 	{ "mdipi_client_list", "Walker for mdi_pathinfo pi_client_link",
4742 		mdi_pi_client_link_walk_init,
4743 		mdi_pi_client_link_walk_step,
4744 		mdi_pi_client_link_walk_fini },
4745 	{ "mdipi_phci_list", "Walker for mdi_pathinfo pi_phci_link",
4746 		mdi_pi_phci_link_walk_init,
4747 		mdi_pi_phci_link_walk_step,
4748 		mdi_pi_phci_link_walk_fini },
4749 	{ "mdiphci_list", "Walker for mdi_phci ph_next link",
4750 		mdi_phci_ph_next_walk_init,
4751 		mdi_phci_ph_next_walk_step,
4752 		mdi_phci_ph_next_walk_fini },
4753 
4754 	/* from memory.c */
4755 	{ "page", "walk all pages, or those from the specified vnode",
4756 		page_walk_init, page_walk_step, page_walk_fini },
4757 	{ "allpages", "walk all pages, including free pages",
4758 		allpages_walk_init, allpages_walk_step, allpages_walk_fini },
4759 	{ "memlist", "walk specified memlist",
4760 		NULL, memlist_walk_step, NULL },
4761 	{ "swapinfo", "walk swapinfo structures",
4762 		swap_walk_init, swap_walk_step, NULL },
4763 
4764 	/* from mmd.c */
4765 	{ "pattr", "walk pattr_t structures", pattr_walk_init,
4766 		mmdq_walk_step, mmdq_walk_fini },
4767 	{ "pdesc", "walk pdesc_t structures",
4768 		pdesc_walk_init, mmdq_walk_step, mmdq_walk_fini },
4769 	{ "pdesc_slab", "walk pdesc_slab_t structures",
4770 		pdesc_slab_walk_init, mmdq_walk_step, mmdq_walk_fini },
4771 
4772 	/* from modhash.c */
4773 	{ "modhash", "walk list of mod_hash structures", modhash_walk_init,
4774 		modhash_walk_step, NULL },
4775 	{ "modent", "walk list of entries in a given mod_hash",
4776 		modent_walk_init, modent_walk_step, modent_walk_fini },
4777 	{ "modchain", "walk list of entries in a given mod_hash_entry",
4778 		NULL, modchain_walk_step, NULL },
4779 
4780 	/* from net.c */
4781 	{ "icmp", "walk ICMP control structures using MI for all stacks",
4782 		mi_payload_walk_init, mi_payload_walk_step, NULL,
4783 		&mi_icmp_arg },
4784 	{ "mi", "given a MI_O, walk the MI",
4785 		mi_walk_init, mi_walk_step, mi_walk_fini, NULL },
4786 	{ "sonode", "given a sonode, walk its children",
4787 		sonode_walk_init, sonode_walk_step, sonode_walk_fini, NULL },
4788 	{ "icmp_stacks", "walk all the icmp_stack_t",
4789 		icmp_stacks_walk_init, icmp_stacks_walk_step, NULL },
4790 	{ "tcp_stacks", "walk all the tcp_stack_t",
4791 		tcp_stacks_walk_init, tcp_stacks_walk_step, NULL },
4792 	{ "udp_stacks", "walk all the udp_stack_t",
4793 		udp_stacks_walk_init, udp_stacks_walk_step, NULL },
4794 
4795 	/* from netstack.c */
4796 	{ "netstack", "walk a list of kernel netstacks",
4797 		netstack_walk_init, netstack_walk_step, NULL },
4798 
4799 	/* from nvpair.c */
4800 	{ NVPAIR_WALKER_NAME, NVPAIR_WALKER_DESCR,
4801 		nvpair_walk_init, nvpair_walk_step, NULL },
4802 
4803 	/* from rctl.c */
4804 	{ "rctl_dict_list", "walk all rctl_dict_entry_t's from rctl_lists",
4805 		rctl_dict_walk_init, rctl_dict_walk_step, NULL },
4806 	{ "rctl_set", "given a rctl_set, walk all rctls", rctl_set_walk_init,
4807 		rctl_set_walk_step, NULL },
4808 	{ "rctl_val", "given a rctl_t, walk all rctl_val entries associated",
4809 		rctl_val_walk_init, rctl_val_walk_step },
4810 
4811 	/* from sobj.c */
4812 	{ "blocked", "walk threads blocked on a given sobj",
4813 		blocked_walk_init, blocked_walk_step, NULL },
4814 	{ "wchan", "given a wchan, list of blocked threads",
4815 		wchan_walk_init, wchan_walk_step, wchan_walk_fini },
4816 
4817 	/* from stream.c */
4818 	{ "b_cont", "walk mblk_t list using b_cont",
4819 		mblk_walk_init, b_cont_step, mblk_walk_fini },
4820 	{ "b_next", "walk mblk_t list using b_next",
4821 		mblk_walk_init, b_next_step, mblk_walk_fini },
4822 	{ "qlink", "walk queue_t list using q_link",
4823 		queue_walk_init, queue_link_step, queue_walk_fini },
4824 	{ "qnext", "walk queue_t list using q_next",
4825 		queue_walk_init, queue_next_step, queue_walk_fini },
4826 	{ "strftblk", "given a dblk_t, walk STREAMS flow trace event list",
4827 		strftblk_walk_init, strftblk_step, strftblk_walk_fini },
4828 	{ "readq", "walk read queue side of stdata",
4829 		str_walk_init, strr_walk_step, str_walk_fini },
4830 	{ "writeq", "walk write queue side of stdata",
4831 		str_walk_init, strw_walk_step, str_walk_fini },
4832 
4833 	/* from taskq.c */
4834 	{ "taskq_thread", "given a taskq_t, list all of its threads",
4835 		taskq_thread_walk_init,
4836 		taskq_thread_walk_step,
4837 		taskq_thread_walk_fini },
4838 	{ "taskq_entry", "given a taskq_t*, list all taskq_ent_t in the list",
4839 		taskq_ent_walk_init, taskq_ent_walk_step, NULL },
4840 
4841 	/* from thread.c */
4842 	{ "deathrow", "walk threads on both lwp_ and thread_deathrow",
4843 		deathrow_walk_init, deathrow_walk_step, NULL },
4844 	{ "cpu_dispq", "given a cpu_t, walk threads in dispatcher queues",
4845 		cpu_dispq_walk_init, dispq_walk_step, dispq_walk_fini },
4846 	{ "cpupart_dispq",
4847 		"given a cpupart_t, walk threads in dispatcher queues",
4848 		cpupart_dispq_walk_init, dispq_walk_step, dispq_walk_fini },
4849 	{ "lwp_deathrow", "walk lwp_deathrow",
4850 		lwp_deathrow_walk_init, deathrow_walk_step, NULL },
4851 	{ "thread", "global or per-process kthread_t structures",
4852 		thread_walk_init, thread_walk_step, thread_walk_fini },
4853 	{ "thread_deathrow", "walk threads on thread_deathrow",
4854 		thread_deathrow_walk_init, deathrow_walk_step, NULL },
4855 
4856 	/* from tsd.c */
4857 	{ "tsd", "walk list of thread-specific data",
4858 		tsd_walk_init, tsd_walk_step, tsd_walk_fini },
4859 
4860 	/* from tsol.c */
4861 	{ "tnrh", "walk remote host cache structures",
4862 	    tnrh_walk_init, tnrh_walk_step, tnrh_walk_fini },
4863 	{ "tnrhtp", "walk remote host template structures",
4864 	    tnrhtp_walk_init, tnrhtp_walk_step, tnrhtp_walk_fini },
4865 
4866 	/*
4867 	 * typegraph does not work under kmdb, as it requires too much memory
4868 	 * for its internal data structures.
4869 	 */
4870 #ifndef _KMDB
4871 	/* from typegraph.c */
4872 	{ "typeconflict", "walk buffers with conflicting type inferences",
4873 		typegraph_walk_init, typeconflict_walk_step },
4874 	{ "typeunknown", "walk buffers with unknown types",
4875 		typegraph_walk_init, typeunknown_walk_step },
4876 #endif
4877 
4878 	/* from vfs.c */
4879 	{ "vfs", "walk file system list",
4880 		vfs_walk_init, vfs_walk_step },
4881 
4882 	/* from zone.c */
4883 	{ "zone", "walk a list of kernel zones",
4884 		zone_walk_init, zone_walk_step, NULL },
4885 	{ "zsd", "walk list of zsd entries for a zone",
4886 		zsd_walk_init, zsd_walk_step, NULL },
4887 
4888 	{ NULL }
4889 };
4890 
4891 static const mdb_modinfo_t modinfo = { MDB_API_VERSION, dcmds, walkers };
4892 
4893 /*ARGSUSED*/
4894 static void
4895 genunix_statechange_cb(void *ignored)
4896 {
4897 	/*
4898 	 * Force ::findleaks and ::stacks to let go any cached state.
4899 	 */
4900 	leaky_cleanup(1);
4901 	stacks_cleanup(1);
4902 
4903 	kmem_statechange();	/* notify kmem */
4904 }
4905 
4906 const mdb_modinfo_t *
4907 _mdb_init(void)
4908 {
4909 	kmem_init();
4910 
4911 	(void) mdb_callback_add(MDB_CALLBACK_STCHG,
4912 	    genunix_statechange_cb, NULL);
4913 
4914 	return (&modinfo);
4915 }
4916 
4917 void
4918 _mdb_fini(void)
4919 {
4920 	leaky_cleanup(1);
4921 	stacks_cleanup(1);
4922 }
4923