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