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