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