1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
23  * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright 2019 Joyent, Inc.
25  * Copyright (c) 2013 by Delphix. All rights reserved.
26  */
27 
28 #include <mdb/mdb_param.h>
29 #include <mdb/mdb_modapi.h>
30 #include <mdb/mdb_ks.h>
31 #include <mdb/mdb_ctf.h>
32 
33 #include <sys/types.h>
34 #include <sys/thread.h>
35 #include <sys/session.h>
36 #include <sys/user.h>
37 #include <sys/proc.h>
38 #include <sys/var.h>
39 #include <sys/t_lock.h>
40 #include <sys/callo.h>
41 #include <sys/priocntl.h>
42 #include <sys/class.h>
43 #include <sys/regset.h>
44 #include <sys/stack.h>
45 #include <sys/cpuvar.h>
46 #include <sys/vnode.h>
47 #include <sys/vfs.h>
48 #include <sys/flock_impl.h>
49 #include <sys/kmem_impl.h>
50 #include <sys/vmem_impl.h>
51 #include <sys/kstat.h>
52 #include <sys/dditypes.h>
53 #include <sys/ddi_impldefs.h>
54 #include <sys/sysmacros.h>
55 #include <sys/sysconf.h>
56 #include <sys/task.h>
57 #include <sys/project.h>
58 #include <sys/errorq_impl.h>
59 #include <sys/cred_impl.h>
60 #include <sys/zone.h>
61 #include <sys/panic.h>
62 #include <regex.h>
63 #include <sys/port_impl.h>
64 #include <sys/contract/process_impl.h>
65 
66 #include "avl.h"
67 #include "bio.h"
68 #include "bitset.h"
69 #include "combined.h"
70 #include "contract.h"
71 #include "cpupart_mdb.h"
72 #include "cred.h"
73 #include "ctxop.h"
74 #include "cyclic.h"
75 #include "damap.h"
76 #include "ddi_periodic.h"
77 #include "devinfo.h"
78 #include "dnlc.h"
79 #include "findstack.h"
80 #include "fm.h"
81 #include "gcore.h"
82 #include "group.h"
83 #include "irm.h"
84 #include "kgrep.h"
85 #include "kmem.h"
86 #include "ldi.h"
87 #include "leaky.h"
88 #include "lgrp.h"
89 #include "list.h"
90 #include "log.h"
91 #include "mdi.h"
92 #include "memory.h"
93 #include "mmd.h"
94 #include "modhash.h"
95 #include "ndievents.h"
96 #include "net.h"
97 #include "netstack.h"
98 #include "nvpair.h"
99 #include "pci.h"
100 #include "pg.h"
101 #include "rctl.h"
102 #include "sobj.h"
103 #include "streams.h"
104 #include "sysevent.h"
105 #include "taskq.h"
106 #include "thread.h"
107 #include "tsd.h"
108 #include "tsol.h"
109 #include "typegraph.h"
110 #include "vfs.h"
111 #include "zone.h"
112 #include "hotplug.h"
113 
114 /*
115  * Surely this is defined somewhere...
116  */
117 #define	NINTR		16
118 
119 #define	KILOS		10
120 #define	MEGS		20
121 #define	GIGS		30
122 
123 #ifndef STACK_BIAS
124 #define	STACK_BIAS	0
125 #endif
126 
127 static char
pstat2ch(uchar_t state)128 pstat2ch(uchar_t state)
129 {
130 	switch (state) {
131 		case SSLEEP: return ('S');
132 		case SRUN: return ('R');
133 		case SZOMB: return ('Z');
134 		case SIDL: return ('I');
135 		case SONPROC: return ('O');
136 		case SSTOP: return ('T');
137 		case SWAIT: return ('W');
138 		default: return ('?');
139 	}
140 }
141 
142 #define	PS_PRTTHREADS	0x1
143 #define	PS_PRTLWPS	0x2
144 #define	PS_PSARGS	0x4
145 #define	PS_TASKS	0x8
146 #define	PS_PROJECTS	0x10
147 #define	PS_ZONES	0x20
148 #define	PS_SERVICES	0x40
149 
150 static int
ps_threadprint(uintptr_t addr,const void * data,void * private)151 ps_threadprint(uintptr_t addr, const void *data, void *private)
152 {
153 	const kthread_t *t = (const kthread_t *)data;
154 	uint_t prt_flags = *((uint_t *)private);
155 
156 	static const mdb_bitmask_t t_state_bits[] = {
157 		{ "TS_FREE",	UINT_MAX,	TS_FREE		},
158 		{ "TS_SLEEP",	TS_SLEEP,	TS_SLEEP	},
159 		{ "TS_RUN",	TS_RUN,		TS_RUN		},
160 		{ "TS_ONPROC",	TS_ONPROC,	TS_ONPROC	},
161 		{ "TS_ZOMB",	TS_ZOMB,	TS_ZOMB		},
162 		{ "TS_STOPPED",	TS_STOPPED,	TS_STOPPED	},
163 		{ "TS_WAIT",	TS_WAIT,	TS_WAIT		},
164 		{ NULL,		0,		0		}
165 	};
166 
167 	if (prt_flags & PS_PRTTHREADS)
168 		mdb_printf("\tT  %?a <%b>\n", addr, t->t_state, t_state_bits);
169 
170 	if (prt_flags & PS_PRTLWPS) {
171 		char desc[128] = "";
172 
173 		(void) thread_getdesc(addr, B_FALSE, desc, sizeof (desc));
174 
175 		mdb_printf("\tL  %?a ID: %s\n", t->t_lwp, desc);
176 	}
177 
178 	return (WALK_NEXT);
179 }
180 
181 typedef struct mdb_pflags_proc {
182 	struct pid	*p_pidp;
183 	ushort_t	p_pidflag;
184 	uint_t		p_proc_flag;
185 	uint_t		p_flag;
186 } mdb_pflags_proc_t;
187 
188 static int
pflags(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)189 pflags(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
190 {
191 	mdb_pflags_proc_t pr;
192 	struct pid pid;
193 
194 	static const mdb_bitmask_t p_flag_bits[] = {
195 		{ "SSYS",		SSYS,		SSYS		},
196 		{ "SEXITING",		SEXITING,	SEXITING	},
197 		{ "SITBUSY",		SITBUSY,	SITBUSY		},
198 		{ "SFORKING",		SFORKING,	SFORKING	},
199 		{ "SWATCHOK",		SWATCHOK,	SWATCHOK	},
200 		{ "SKILLED",		SKILLED,	SKILLED		},
201 		{ "SSCONT",		SSCONT,		SSCONT		},
202 		{ "SZONETOP",		SZONETOP,	SZONETOP	},
203 		{ "SEXTKILLED",		SEXTKILLED,	SEXTKILLED	},
204 		{ "SUGID",		SUGID,		SUGID		},
205 		{ "SEXECED",		SEXECED,	SEXECED		},
206 		{ "SJCTL",		SJCTL,		SJCTL		},
207 		{ "SNOWAIT",		SNOWAIT,	SNOWAIT		},
208 		{ "SVFORK",		SVFORK,		SVFORK		},
209 		{ "SVFWAIT",		SVFWAIT,	SVFWAIT		},
210 		{ "SEXITLWPS",		SEXITLWPS,	SEXITLWPS	},
211 		{ "SHOLDFORK",		SHOLDFORK,	SHOLDFORK	},
212 		{ "SHOLDFORK1",		SHOLDFORK1,	SHOLDFORK1	},
213 		{ "SCOREDUMP",		SCOREDUMP,	SCOREDUMP	},
214 		{ "SMSACCT",		SMSACCT,	SMSACCT		},
215 		{ "SLWPWRAP",		SLWPWRAP,	SLWPWRAP	},
216 		{ "SAUTOLPG",		SAUTOLPG,	SAUTOLPG	},
217 		{ "SNOCD",		SNOCD,		SNOCD		},
218 		{ "SHOLDWATCH",		SHOLDWATCH,	SHOLDWATCH	},
219 		{ "SMSFORK",		SMSFORK,	SMSFORK		},
220 		{ "SDOCORE",		SDOCORE,	SDOCORE		},
221 		{ NULL,			0,		0		}
222 	};
223 
224 	static const mdb_bitmask_t p_pidflag_bits[] = {
225 		{ "CLDPEND",		CLDPEND,	CLDPEND		},
226 		{ "CLDCONT",		CLDCONT,	CLDCONT		},
227 		{ "CLDNOSIGCHLD",	CLDNOSIGCHLD,	CLDNOSIGCHLD	},
228 		{ "CLDWAITPID",		CLDWAITPID,	CLDWAITPID	},
229 		{ NULL,			0,		0		}
230 	};
231 
232 	static const mdb_bitmask_t p_proc_flag_bits[] = {
233 		{ "P_PR_TRACE",		P_PR_TRACE,	P_PR_TRACE	},
234 		{ "P_PR_PTRACE",	P_PR_PTRACE,	P_PR_PTRACE	},
235 		{ "P_PR_FORK",		P_PR_FORK,	P_PR_FORK	},
236 		{ "P_PR_LOCK",		P_PR_LOCK,	P_PR_LOCK	},
237 		{ "P_PR_ASYNC",		P_PR_ASYNC,	P_PR_ASYNC	},
238 		{ "P_PR_EXEC",		P_PR_EXEC,	P_PR_EXEC	},
239 		{ "P_PR_BPTADJ",	P_PR_BPTADJ,	P_PR_BPTADJ	},
240 		{ "P_PR_RUNLCL",	P_PR_RUNLCL,	P_PR_RUNLCL	},
241 		{ "P_PR_KILLCL",	P_PR_KILLCL,	P_PR_KILLCL	},
242 		{ NULL,			0,		0		}
243 	};
244 
245 	if (!(flags & DCMD_ADDRSPEC)) {
246 		if (mdb_walk_dcmd("proc", "pflags", argc, argv) == -1) {
247 			mdb_warn("can't walk 'proc'");
248 			return (DCMD_ERR);
249 		}
250 		return (DCMD_OK);
251 	}
252 
253 	if (mdb_ctf_vread(&pr, "proc_t", "mdb_pflags_proc_t", addr, 0) == -1 ||
254 	    mdb_vread(&pid, sizeof (pid), (uintptr_t)pr.p_pidp) == -1) {
255 		mdb_warn("cannot read proc_t or pid");
256 		return (DCMD_ERR);
257 	}
258 
259 	mdb_printf("%p [pid %d]:\n", addr, pid.pid_id);
260 	mdb_printf("\tp_flag:      %08x <%b>\n", pr.p_flag, pr.p_flag,
261 	    p_flag_bits);
262 	mdb_printf("\tp_pidflag:   %08x <%b>\n", pr.p_pidflag, pr.p_pidflag,
263 	    p_pidflag_bits);
264 	mdb_printf("\tp_proc_flag: %08x <%b>\n", pr.p_proc_flag, pr.p_proc_flag,
265 	    p_proc_flag_bits);
266 
267 	return (DCMD_OK);
268 }
269 
270 typedef struct mdb_ps_proc {
271 	char		p_stat;
272 	struct pid	*p_pidp;
273 	struct pid	*p_pgidp;
274 	struct cred	*p_cred;
275 	struct sess	*p_sessp;
276 	struct task	*p_task;
277 	struct zone	*p_zone;
278 	struct cont_process *p_ct_process;
279 	pid_t		p_ppid;
280 	uint_t		p_flag;
281 	struct {
282 		char		u_comm[MAXCOMLEN + 1];
283 		char		u_psargs[PSARGSZ];
284 	} p_user;
285 } mdb_ps_proc_t;
286 
287 /*
288  * A reasonable enough limit. Note that we purposefully let this column over-run
289  * if needed.
290  */
291 #define	FMRI_LEN (128)
292 
293 int
ps(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)294 ps(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
295 {
296 	uint_t prt_flags = 0;
297 	mdb_ps_proc_t pr;
298 	struct pid pid, pgid, sid;
299 	sess_t session;
300 	cred_t cred;
301 	task_t tk;
302 	kproject_t pj;
303 	zone_t zn;
304 	struct cont_process cp;
305 	char fmri[FMRI_LEN] = "";
306 
307 	if (!(flags & DCMD_ADDRSPEC)) {
308 		if (mdb_walk_dcmd("proc", "ps", argc, argv) == -1) {
309 			mdb_warn("can't walk 'proc'");
310 			return (DCMD_ERR);
311 		}
312 		return (DCMD_OK);
313 	}
314 
315 	if (mdb_getopts(argc, argv,
316 	    'f', MDB_OPT_SETBITS, PS_PSARGS, &prt_flags,
317 	    'l', MDB_OPT_SETBITS, PS_PRTLWPS, &prt_flags,
318 	    's', MDB_OPT_SETBITS, PS_SERVICES, &prt_flags,
319 	    'T', MDB_OPT_SETBITS, PS_TASKS, &prt_flags,
320 	    'P', MDB_OPT_SETBITS, PS_PROJECTS, &prt_flags,
321 	    'z', MDB_OPT_SETBITS, PS_ZONES, &prt_flags,
322 	    't', MDB_OPT_SETBITS, PS_PRTTHREADS, &prt_flags, NULL) != argc)
323 		return (DCMD_USAGE);
324 
325 	if (DCMD_HDRSPEC(flags)) {
326 		mdb_printf("%<u>%-1s %-6s %-6s %-6s %-6s ",
327 		    "S", "PID", "PPID", "PGID", "SID");
328 		if (prt_flags & PS_TASKS)
329 			mdb_printf("%-5s ", "TASK");
330 		if (prt_flags & PS_PROJECTS)
331 			mdb_printf("%-5s ", "PROJ");
332 		if (prt_flags & PS_ZONES)
333 			mdb_printf("%-5s ", "ZONE");
334 		if (prt_flags & PS_SERVICES)
335 			mdb_printf("%-40s ", "SERVICE");
336 		mdb_printf("%-6s %-10s %-?s %-s%</u>\n",
337 		    "UID", "FLAGS", "ADDR", "NAME");
338 	}
339 
340 	if (mdb_ctf_vread(&pr, "proc_t", "mdb_ps_proc_t", addr, 0) == -1)
341 		return (DCMD_ERR);
342 
343 	mdb_vread(&pid, sizeof (pid), (uintptr_t)pr.p_pidp);
344 	mdb_vread(&pgid, sizeof (pgid), (uintptr_t)pr.p_pgidp);
345 	mdb_vread(&cred, sizeof (cred), (uintptr_t)pr.p_cred);
346 	mdb_vread(&session, sizeof (session), (uintptr_t)pr.p_sessp);
347 	mdb_vread(&sid, sizeof (sid), (uintptr_t)session.s_sidp);
348 	if (prt_flags & (PS_TASKS | PS_PROJECTS))
349 		mdb_vread(&tk, sizeof (tk), (uintptr_t)pr.p_task);
350 	if (prt_flags & PS_PROJECTS)
351 		mdb_vread(&pj, sizeof (pj), (uintptr_t)tk.tk_proj);
352 	if (prt_flags & PS_ZONES)
353 		mdb_vread(&zn, sizeof (zn), (uintptr_t)pr.p_zone);
354 	if ((prt_flags & PS_SERVICES) && pr.p_ct_process != NULL) {
355 		mdb_vread(&cp, sizeof (cp), (uintptr_t)pr.p_ct_process);
356 
357 		if (mdb_read_refstr((uintptr_t)cp.conp_svc_fmri, fmri,
358 		    sizeof (fmri)) <= 0)
359 			(void) strlcpy(fmri, "?", sizeof (fmri));
360 
361 		/* Strip any standard prefix and suffix. */
362 		if (strncmp(fmri, "svc:/", sizeof ("svc:/") - 1) == 0) {
363 			char *i = fmri;
364 			char *j = fmri + sizeof ("svc:/") - 1;
365 			for (; *j != '\0'; i++, j++) {
366 				if (strcmp(j, ":default") == 0)
367 					break;
368 				*i = *j;
369 			}
370 
371 			*i = '\0';
372 		}
373 	}
374 
375 	mdb_printf("%-c %-6d %-6d %-6d %-6d ",
376 	    pstat2ch(pr.p_stat), pid.pid_id, pr.p_ppid, pgid.pid_id,
377 	    sid.pid_id);
378 	if (prt_flags & PS_TASKS)
379 		mdb_printf("%-5d ", tk.tk_tkid);
380 	if (prt_flags & PS_PROJECTS)
381 		mdb_printf("%-5d ", pj.kpj_id);
382 	if (prt_flags & PS_ZONES)
383 		mdb_printf("%-5d ", zn.zone_id);
384 	if (prt_flags & PS_SERVICES)
385 		mdb_printf("%-40s ", fmri);
386 	mdb_printf("%-6d 0x%08x %0?p %-s\n",
387 	    cred.cr_uid, pr.p_flag, addr,
388 	    (prt_flags & PS_PSARGS) ? pr.p_user.u_psargs : pr.p_user.u_comm);
389 
390 	if (prt_flags & ~PS_PSARGS)
391 		(void) mdb_pwalk("thread", ps_threadprint, &prt_flags, addr);
392 
393 	return (DCMD_OK);
394 }
395 
396 static void
ps_help(void)397 ps_help(void)
398 {
399 	mdb_printf("Display processes.\n\n"
400 	    "Options:\n"
401 	    "    -f\tDisplay command arguments\n"
402 	    "    -l\tDisplay LWPs\n"
403 	    "    -T\tDisplay tasks\n"
404 	    "    -P\tDisplay projects\n"
405 	    "    -s\tDisplay SMF FMRI\n"
406 	    "    -z\tDisplay zones\n"
407 	    "    -t\tDisplay threads\n\n");
408 
409 	mdb_printf("The resulting output is a table of the processes on the "
410 	    "system.  The\n"
411 	    "columns in the output consist of a combination of the "
412 	    "following fields:\n\n");
413 	mdb_printf("S\tProcess state.  Possible states are:\n"
414 	    "\tS\tSleeping (SSLEEP)\n"
415 	    "\tR\tRunnable (SRUN)\n"
416 	    "\tZ\tZombie (SZOMB)\n"
417 	    "\tI\tIdle (SIDL)\n"
418 	    "\tO\tOn Cpu (SONPROC)\n"
419 	    "\tT\tStopped (SSTOP)\n"
420 	    "\tW\tWaiting (SWAIT)\n");
421 
422 	mdb_printf("PID\tProcess id.\n");
423 	mdb_printf("PPID\tParent process id.\n");
424 	mdb_printf("PGID\tProcess group id.\n");
425 	mdb_printf("SID\tProcess id of the session leader.\n");
426 	mdb_printf("TASK\tThe task id of the process.\n");
427 	mdb_printf("PROJ\tThe project id of the process.\n");
428 	mdb_printf("ZONE\tThe zone id of the process.\n");
429 	mdb_printf("SERVICE The SMF service FMRI of the process.\n");
430 	mdb_printf("UID\tThe user id of the process.\n");
431 	mdb_printf("FLAGS\tThe process flags (see ::pflags).\n");
432 	mdb_printf("ADDR\tThe kernel address of the proc_t structure of the "
433 	    "process\n");
434 	mdb_printf("NAME\tThe name (p_user.u_comm field) of the process.  If "
435 	    "the -f flag\n"
436 	    "\tis specified, the arguments of the process are displayed.\n");
437 }
438 
439 #define	PG_NEWEST	0x0001
440 #define	PG_OLDEST	0x0002
441 #define	PG_PIPE_OUT	0x0004
442 #define	PG_EXACT_MATCH	0x0008
443 
444 typedef struct pgrep_data {
445 	uint_t pg_flags;
446 	uint_t pg_psflags;
447 	uintptr_t pg_xaddr;
448 	hrtime_t pg_xstart;
449 	const char *pg_pat;
450 #ifndef _KMDB
451 	regex_t pg_reg;
452 #endif
453 } pgrep_data_t;
454 
455 typedef struct mdb_pgrep_proc {
456 	struct {
457 		timestruc_t	u_start;
458 		char		u_comm[MAXCOMLEN + 1];
459 	} p_user;
460 } mdb_pgrep_proc_t;
461 
462 /*ARGSUSED*/
463 static int
pgrep_cb(uintptr_t addr,const void * ignored,void * data)464 pgrep_cb(uintptr_t addr, const void *ignored, void *data)
465 {
466 	mdb_pgrep_proc_t p;
467 	pgrep_data_t *pgp = data;
468 #ifndef _KMDB
469 	regmatch_t pmatch;
470 #endif
471 
472 	if (mdb_ctf_vread(&p, "proc_t", "mdb_pgrep_proc_t", addr, 0) == -1)
473 		return (WALK_ERR);
474 
475 	/*
476 	 * kmdb doesn't have access to the reg* functions, so we fall back
477 	 * to strstr/strcmp.
478 	 */
479 #ifdef _KMDB
480 	if ((pgp->pg_flags & PG_EXACT_MATCH) ?
481 	    (strcmp(p.p_user.u_comm, pgp->pg_pat) != 0) :
482 	    (strstr(p.p_user.u_comm, pgp->pg_pat) == NULL))
483 		return (WALK_NEXT);
484 #else
485 	if (regexec(&pgp->pg_reg, p.p_user.u_comm, 1, &pmatch, 0) != 0)
486 		return (WALK_NEXT);
487 
488 	if ((pgp->pg_flags & PG_EXACT_MATCH) &&
489 	    (pmatch.rm_so != 0 || p.p_user.u_comm[pmatch.rm_eo] != '\0'))
490 		return (WALK_NEXT);
491 #endif
492 
493 	if (pgp->pg_flags & (PG_NEWEST | PG_OLDEST)) {
494 		hrtime_t start;
495 
496 		start = (hrtime_t)p.p_user.u_start.tv_sec * NANOSEC +
497 		    p.p_user.u_start.tv_nsec;
498 
499 		if (pgp->pg_flags & PG_NEWEST) {
500 			if (pgp->pg_xaddr == 0 || start > pgp->pg_xstart) {
501 				pgp->pg_xaddr = addr;
502 				pgp->pg_xstart = start;
503 			}
504 		} else {
505 			if (pgp->pg_xaddr == 0 || start < pgp->pg_xstart) {
506 				pgp->pg_xaddr = addr;
507 				pgp->pg_xstart = start;
508 			}
509 		}
510 
511 	} else if (pgp->pg_flags & PG_PIPE_OUT) {
512 		mdb_printf("%p\n", addr);
513 
514 	} else {
515 		if (mdb_call_dcmd("ps", addr, pgp->pg_psflags, 0, NULL) != 0) {
516 			mdb_warn("can't invoke 'ps'");
517 			return (WALK_DONE);
518 		}
519 		pgp->pg_psflags &= ~DCMD_LOOPFIRST;
520 	}
521 
522 	return (WALK_NEXT);
523 }
524 
525 /*ARGSUSED*/
526 int
pgrep(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)527 pgrep(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
528 {
529 	pgrep_data_t pg;
530 	int i;
531 #ifndef _KMDB
532 	int err;
533 #endif
534 
535 	if (flags & DCMD_ADDRSPEC)
536 		return (DCMD_USAGE);
537 
538 	pg.pg_flags = 0;
539 	pg.pg_xaddr = 0;
540 
541 	i = mdb_getopts(argc, argv,
542 	    'n', MDB_OPT_SETBITS, PG_NEWEST, &pg.pg_flags,
543 	    'o', MDB_OPT_SETBITS, PG_OLDEST, &pg.pg_flags,
544 	    'x', MDB_OPT_SETBITS, PG_EXACT_MATCH, &pg.pg_flags,
545 	    NULL);
546 
547 	argc -= i;
548 	argv += i;
549 
550 	if (argc != 1)
551 		return (DCMD_USAGE);
552 
553 	/*
554 	 * -n and -o are mutually exclusive.
555 	 */
556 	if ((pg.pg_flags & PG_NEWEST) && (pg.pg_flags & PG_OLDEST))
557 		return (DCMD_USAGE);
558 
559 	if (argv->a_type != MDB_TYPE_STRING)
560 		return (DCMD_USAGE);
561 
562 	if (flags & DCMD_PIPE_OUT)
563 		pg.pg_flags |= PG_PIPE_OUT;
564 
565 	pg.pg_pat = argv->a_un.a_str;
566 	if (DCMD_HDRSPEC(flags))
567 		pg.pg_psflags = DCMD_ADDRSPEC | DCMD_LOOP | DCMD_LOOPFIRST;
568 	else
569 		pg.pg_psflags = DCMD_ADDRSPEC | DCMD_LOOP;
570 
571 #ifndef _KMDB
572 	if ((err = regcomp(&pg.pg_reg, pg.pg_pat, REG_EXTENDED)) != 0) {
573 		size_t nbytes;
574 		char *buf;
575 
576 		nbytes = regerror(err, &pg.pg_reg, NULL, 0);
577 		buf = mdb_alloc(nbytes + 1, UM_SLEEP | UM_GC);
578 		(void) regerror(err, &pg.pg_reg, buf, nbytes);
579 		mdb_warn("%s\n", buf);
580 
581 		return (DCMD_ERR);
582 	}
583 #endif
584 
585 	if (mdb_walk("proc", pgrep_cb, &pg) != 0) {
586 		mdb_warn("can't walk 'proc'");
587 		return (DCMD_ERR);
588 	}
589 
590 	if (pg.pg_xaddr != 0 && (pg.pg_flags & (PG_NEWEST | PG_OLDEST))) {
591 		if (pg.pg_flags & PG_PIPE_OUT) {
592 			mdb_printf("%p\n", pg.pg_xaddr);
593 		} else {
594 			if (mdb_call_dcmd("ps", pg.pg_xaddr, pg.pg_psflags,
595 			    0, NULL) != 0) {
596 				mdb_warn("can't invoke 'ps'");
597 				return (DCMD_ERR);
598 			}
599 		}
600 	}
601 
602 	return (DCMD_OK);
603 }
604 
605 int
task(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)606 task(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
607 {
608 	task_t tk;
609 	kproject_t pj;
610 
611 	if (!(flags & DCMD_ADDRSPEC)) {
612 		if (mdb_walk_dcmd("task_cache", "task", argc, argv) == -1) {
613 			mdb_warn("can't walk task_cache");
614 			return (DCMD_ERR);
615 		}
616 		return (DCMD_OK);
617 	}
618 	if (DCMD_HDRSPEC(flags)) {
619 		mdb_printf("%<u>%?s %6s %6s %6s %6s %10s%</u>\n",
620 		    "ADDR", "TASKID", "PROJID", "ZONEID", "REFCNT", "FLAGS");
621 	}
622 	if (mdb_vread(&tk, sizeof (task_t), addr) == -1) {
623 		mdb_warn("can't read task_t structure at %p", addr);
624 		return (DCMD_ERR);
625 	}
626 	if (mdb_vread(&pj, sizeof (kproject_t), (uintptr_t)tk.tk_proj) == -1) {
627 		mdb_warn("can't read project_t structure at %p", addr);
628 		return (DCMD_ERR);
629 	}
630 	mdb_printf("%0?p %6d %6d %6d %6u 0x%08x\n",
631 	    addr, tk.tk_tkid, pj.kpj_id, pj.kpj_zoneid, tk.tk_hold_count,
632 	    tk.tk_flags);
633 	return (DCMD_OK);
634 }
635 
636 int
project(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)637 project(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
638 {
639 	kproject_t pj;
640 
641 	if (!(flags & DCMD_ADDRSPEC)) {
642 		if (mdb_walk_dcmd("projects", "project", argc, argv) == -1) {
643 			mdb_warn("can't walk projects");
644 			return (DCMD_ERR);
645 		}
646 		return (DCMD_OK);
647 	}
648 	if (DCMD_HDRSPEC(flags)) {
649 		mdb_printf("%<u>%?s %6s %6s %6s%</u>\n",
650 		    "ADDR", "PROJID", "ZONEID", "REFCNT");
651 	}
652 	if (mdb_vread(&pj, sizeof (kproject_t), addr) == -1) {
653 		mdb_warn("can't read kproject_t structure at %p", addr);
654 		return (DCMD_ERR);
655 	}
656 	mdb_printf("%0?p %6d %6d %6u\n", addr, pj.kpj_id, pj.kpj_zoneid,
657 	    pj.kpj_count);
658 	return (DCMD_OK);
659 }
660 
661 /* walk callouts themselves, either by list or id hash. */
662 int
callout_walk_init(mdb_walk_state_t * wsp)663 callout_walk_init(mdb_walk_state_t *wsp)
664 {
665 	if (wsp->walk_addr == 0) {
666 		mdb_warn("callout doesn't support global walk");
667 		return (WALK_ERR);
668 	}
669 	wsp->walk_data = mdb_alloc(sizeof (callout_t), UM_SLEEP);
670 	return (WALK_NEXT);
671 }
672 
673 #define	CALLOUT_WALK_BYLIST	0
674 #define	CALLOUT_WALK_BYID	1
675 
676 /* the walker arg switches between walking by list (0) and walking by id (1). */
677 int
callout_walk_step(mdb_walk_state_t * wsp)678 callout_walk_step(mdb_walk_state_t *wsp)
679 {
680 	int retval;
681 
682 	if (wsp->walk_addr == 0) {
683 		return (WALK_DONE);
684 	}
685 	if (mdb_vread(wsp->walk_data, sizeof (callout_t),
686 	    wsp->walk_addr) == -1) {
687 		mdb_warn("failed to read callout at %p", wsp->walk_addr);
688 		return (WALK_DONE);
689 	}
690 	retval = wsp->walk_callback(wsp->walk_addr, wsp->walk_data,
691 	    wsp->walk_cbdata);
692 
693 	if ((ulong_t)wsp->walk_arg == CALLOUT_WALK_BYID) {
694 		wsp->walk_addr =
695 		    (uintptr_t)(((callout_t *)wsp->walk_data)->c_idnext);
696 	} else {
697 		wsp->walk_addr =
698 		    (uintptr_t)(((callout_t *)wsp->walk_data)->c_clnext);
699 	}
700 
701 	return (retval);
702 }
703 
704 void
callout_walk_fini(mdb_walk_state_t * wsp)705 callout_walk_fini(mdb_walk_state_t *wsp)
706 {
707 	mdb_free(wsp->walk_data, sizeof (callout_t));
708 }
709 
710 /*
711  * walker for callout lists. This is different from hashes and callouts.
712  * Thankfully, it's also simpler.
713  */
714 int
callout_list_walk_init(mdb_walk_state_t * wsp)715 callout_list_walk_init(mdb_walk_state_t *wsp)
716 {
717 	if (wsp->walk_addr == 0) {
718 		mdb_warn("callout list doesn't support global walk");
719 		return (WALK_ERR);
720 	}
721 	wsp->walk_data = mdb_alloc(sizeof (callout_list_t), UM_SLEEP);
722 	return (WALK_NEXT);
723 }
724 
725 int
callout_list_walk_step(mdb_walk_state_t * wsp)726 callout_list_walk_step(mdb_walk_state_t *wsp)
727 {
728 	int retval;
729 
730 	if (wsp->walk_addr == 0) {
731 		return (WALK_DONE);
732 	}
733 	if (mdb_vread(wsp->walk_data, sizeof (callout_list_t),
734 	    wsp->walk_addr) != sizeof (callout_list_t)) {
735 		mdb_warn("failed to read callout_list at %p", wsp->walk_addr);
736 		return (WALK_ERR);
737 	}
738 	retval = wsp->walk_callback(wsp->walk_addr, wsp->walk_data,
739 	    wsp->walk_cbdata);
740 
741 	wsp->walk_addr = (uintptr_t)
742 	    (((callout_list_t *)wsp->walk_data)->cl_next);
743 
744 	return (retval);
745 }
746 
747 void
callout_list_walk_fini(mdb_walk_state_t * wsp)748 callout_list_walk_fini(mdb_walk_state_t *wsp)
749 {
750 	mdb_free(wsp->walk_data, sizeof (callout_list_t));
751 }
752 
753 /* routines/structs to walk callout table(s) */
754 typedef struct cot_data {
755 	callout_table_t *ct0;
756 	callout_table_t ct;
757 	callout_hash_t cot_idhash[CALLOUT_BUCKETS];
758 	callout_hash_t cot_clhash[CALLOUT_BUCKETS];
759 	kstat_named_t ct_kstat_data[CALLOUT_NUM_STATS];
760 	int cotndx;
761 	int cotsize;
762 } cot_data_t;
763 
764 int
callout_table_walk_init(mdb_walk_state_t * wsp)765 callout_table_walk_init(mdb_walk_state_t *wsp)
766 {
767 	int max_ncpus;
768 	cot_data_t *cot_walk_data;
769 
770 	cot_walk_data = mdb_alloc(sizeof (cot_data_t), UM_SLEEP);
771 
772 	if (wsp->walk_addr == 0) {
773 		if (mdb_readvar(&cot_walk_data->ct0, "callout_table") == -1) {
774 			mdb_warn("failed to read 'callout_table'");
775 			return (WALK_ERR);
776 		}
777 		if (mdb_readvar(&max_ncpus, "max_ncpus") == -1) {
778 			mdb_warn("failed to get callout_table array size");
779 			return (WALK_ERR);
780 		}
781 		cot_walk_data->cotsize = CALLOUT_NTYPES * max_ncpus;
782 		wsp->walk_addr = (uintptr_t)cot_walk_data->ct0;
783 	} else {
784 		/* not a global walk */
785 		cot_walk_data->cotsize = 1;
786 	}
787 
788 	cot_walk_data->cotndx = 0;
789 	wsp->walk_data = cot_walk_data;
790 
791 	return (WALK_NEXT);
792 }
793 
794 int
callout_table_walk_step(mdb_walk_state_t * wsp)795 callout_table_walk_step(mdb_walk_state_t *wsp)
796 {
797 	int retval;
798 	cot_data_t *cotwd = (cot_data_t *)wsp->walk_data;
799 	size_t size;
800 
801 	if (cotwd->cotndx >= cotwd->cotsize) {
802 		return (WALK_DONE);
803 	}
804 	if (mdb_vread(&(cotwd->ct), sizeof (callout_table_t),
805 	    wsp->walk_addr) != sizeof (callout_table_t)) {
806 		mdb_warn("failed to read callout_table at %p", wsp->walk_addr);
807 		return (WALK_ERR);
808 	}
809 
810 	size = sizeof (callout_hash_t) * CALLOUT_BUCKETS;
811 	if (cotwd->ct.ct_idhash != NULL) {
812 		if (mdb_vread(cotwd->cot_idhash, size,
813 		    (uintptr_t)(cotwd->ct.ct_idhash)) != size) {
814 			mdb_warn("failed to read id_hash at %p",
815 			    cotwd->ct.ct_idhash);
816 			return (WALK_ERR);
817 		}
818 	}
819 	if (cotwd->ct.ct_clhash != NULL) {
820 		if (mdb_vread(&(cotwd->cot_clhash), size,
821 		    (uintptr_t)cotwd->ct.ct_clhash) == -1) {
822 			mdb_warn("failed to read cl_hash at %p",
823 			    cotwd->ct.ct_clhash);
824 			return (WALK_ERR);
825 		}
826 	}
827 	size = sizeof (kstat_named_t) * CALLOUT_NUM_STATS;
828 	if (cotwd->ct.ct_kstat_data != NULL) {
829 		if (mdb_vread(&(cotwd->ct_kstat_data), size,
830 		    (uintptr_t)cotwd->ct.ct_kstat_data) == -1) {
831 			mdb_warn("failed to read kstats at %p",
832 			    cotwd->ct.ct_kstat_data);
833 			return (WALK_ERR);
834 		}
835 	}
836 	retval = wsp->walk_callback(wsp->walk_addr, (void *)cotwd,
837 	    wsp->walk_cbdata);
838 
839 	cotwd->cotndx++;
840 	if (cotwd->cotndx >= cotwd->cotsize) {
841 		return (WALK_DONE);
842 	}
843 	wsp->walk_addr = (uintptr_t)((char *)wsp->walk_addr +
844 	    sizeof (callout_table_t));
845 
846 	return (retval);
847 }
848 
849 void
callout_table_walk_fini(mdb_walk_state_t * wsp)850 callout_table_walk_fini(mdb_walk_state_t *wsp)
851 {
852 	mdb_free(wsp->walk_data, sizeof (cot_data_t));
853 }
854 
855 static const char *co_typenames[] = { "R", "N" };
856 
857 #define	CO_PLAIN_ID(xid)	((xid) & CALLOUT_ID_MASK)
858 
859 #define	TABLE_TO_SEQID(x)	((x) >> CALLOUT_TYPE_BITS)
860 
861 /* callout flags, in no particular order */
862 #define	COF_REAL	0x00000001
863 #define	COF_NORM	0x00000002
864 #define	COF_LONG	0x00000004
865 #define	COF_SHORT	0x00000008
866 #define	COF_EMPTY	0x00000010
867 #define	COF_TIME	0x00000020
868 #define	COF_BEFORE	0x00000040
869 #define	COF_AFTER	0x00000080
870 #define	COF_SEQID	0x00000100
871 #define	COF_FUNC	0x00000200
872 #define	COF_ADDR	0x00000400
873 #define	COF_EXEC	0x00000800
874 #define	COF_HIRES	0x00001000
875 #define	COF_ABS		0x00002000
876 #define	COF_TABLE	0x00004000
877 #define	COF_BYIDH	0x00008000
878 #define	COF_FREE	0x00010000
879 #define	COF_LIST	0x00020000
880 #define	COF_EXPREL	0x00040000
881 #define	COF_HDR		0x00080000
882 #define	COF_VERBOSE	0x00100000
883 #define	COF_LONGLIST	0x00200000
884 #define	COF_THDR	0x00400000
885 #define	COF_LHDR	0x00800000
886 #define	COF_CHDR	0x01000000
887 #define	COF_PARAM	0x02000000
888 #define	COF_DECODE	0x04000000
889 #define	COF_HEAP	0x08000000
890 #define	COF_QUEUE	0x10000000
891 
892 /* show real and normal, short and long, expired and unexpired. */
893 #define	COF_DEFAULT	(COF_REAL | COF_NORM | COF_LONG | COF_SHORT)
894 
895 #define	COF_LIST_FLAGS	\
896 	(CALLOUT_LIST_FLAG_HRESTIME | CALLOUT_LIST_FLAG_ABSOLUTE)
897 
898 /* private callout data for callback functions */
899 typedef struct callout_data {
900 	uint_t flags;		/* COF_* */
901 	cpu_t *cpu;		/* cpu pointer if given */
902 	int seqid;		/* cpu seqid, or -1 */
903 	hrtime_t time;		/* expiration time value */
904 	hrtime_t atime;		/* expiration before value */
905 	hrtime_t btime;		/* expiration after value */
906 	uintptr_t funcaddr;	/* function address or NULL */
907 	uintptr_t param;	/* parameter to function or NULL */
908 	hrtime_t now;		/* current system time */
909 	int nsec_per_tick;	/* for conversions */
910 	ulong_t ctbits;		/* for decoding xid */
911 	callout_table_t *co_table;	/* top of callout table array */
912 	int ndx;		/* table index. */
913 	int bucket;		/* which list/id bucket are we in */
914 	hrtime_t exp;		/* expire time */
915 	int list_flags;		/* copy of cl_flags */
916 } callout_data_t;
917 
918 /* this callback does the actual callback itself (finally). */
919 /*ARGSUSED*/
920 static int
callouts_cb(uintptr_t addr,const void * data,void * priv)921 callouts_cb(uintptr_t addr, const void *data, void *priv)
922 {
923 	callout_data_t *coargs = (callout_data_t *)priv;
924 	callout_t *co = (callout_t *)data;
925 	int tableid, list_flags;
926 	callout_id_t coid;
927 
928 	if ((coargs == NULL) || (co == NULL)) {
929 		return (WALK_ERR);
930 	}
931 
932 	if ((coargs->flags & COF_FREE) && !(co->c_xid & CALLOUT_ID_FREE)) {
933 		/*
934 		 * The callout must have been reallocated. No point in
935 		 * walking any more.
936 		 */
937 		return (WALK_DONE);
938 	}
939 	if (!(coargs->flags & COF_FREE) && (co->c_xid & CALLOUT_ID_FREE)) {
940 		/*
941 		 * The callout must have been freed. No point in
942 		 * walking any more.
943 		 */
944 		return (WALK_DONE);
945 	}
946 	if ((coargs->flags & COF_FUNC) &&
947 	    (coargs->funcaddr != (uintptr_t)co->c_func)) {
948 		return (WALK_NEXT);
949 	}
950 	if ((coargs->flags & COF_PARAM) &&
951 	    (coargs->param != (uintptr_t)co->c_arg)) {
952 		return (WALK_NEXT);
953 	}
954 	if (!(coargs->flags & COF_LONG) && (co->c_xid & CALLOUT_LONGTERM)) {
955 		return (WALK_NEXT);
956 	}
957 	if (!(coargs->flags & COF_SHORT) && !(co->c_xid & CALLOUT_LONGTERM)) {
958 		return (WALK_NEXT);
959 	}
960 	if ((coargs->flags & COF_EXEC) && !(co->c_xid & CALLOUT_EXECUTING)) {
961 		return (WALK_NEXT);
962 	}
963 	/* it is possible we don't have the exp time or flags */
964 	if (coargs->flags & COF_BYIDH) {
965 		if (!(coargs->flags & COF_FREE)) {
966 			/* we have to fetch the expire time ourselves. */
967 			if (mdb_vread(&coargs->exp, sizeof (hrtime_t),
968 			    (uintptr_t)co->c_list + offsetof(callout_list_t,
969 			    cl_expiration)) == -1) {
970 				mdb_warn("failed to read expiration "
971 				    "time from %p", co->c_list);
972 				coargs->exp = 0;
973 			}
974 			/* and flags. */
975 			if (mdb_vread(&coargs->list_flags, sizeof (int),
976 			    (uintptr_t)co->c_list + offsetof(callout_list_t,
977 			    cl_flags)) == -1) {
978 				mdb_warn("failed to read list flags"
979 				    "from %p", co->c_list);
980 				coargs->list_flags = 0;
981 			}
982 		} else {
983 			/* free callouts can't use list pointer. */
984 			coargs->exp = 0;
985 			coargs->list_flags = 0;
986 		}
987 		if (coargs->exp != 0) {
988 			if ((coargs->flags & COF_TIME) &&
989 			    (coargs->exp != coargs->time)) {
990 				return (WALK_NEXT);
991 			}
992 			if ((coargs->flags & COF_BEFORE) &&
993 			    (coargs->exp > coargs->btime)) {
994 				return (WALK_NEXT);
995 			}
996 			if ((coargs->flags & COF_AFTER) &&
997 			    (coargs->exp < coargs->atime)) {
998 				return (WALK_NEXT);
999 			}
1000 		}
1001 		/* tricky part, since both HIRES and ABS can be set */
1002 		list_flags = coargs->list_flags;
1003 		if ((coargs->flags & COF_HIRES) && (coargs->flags & COF_ABS)) {
1004 			/* both flags are set, only skip "regular" ones */
1005 			if (! (list_flags & COF_LIST_FLAGS)) {
1006 				return (WALK_NEXT);
1007 			}
1008 		} else {
1009 			/* individual flags, or no flags */
1010 			if ((coargs->flags & COF_HIRES) &&
1011 			    !(list_flags & CALLOUT_LIST_FLAG_HRESTIME)) {
1012 				return (WALK_NEXT);
1013 			}
1014 			if ((coargs->flags & COF_ABS) &&
1015 			    !(list_flags & CALLOUT_LIST_FLAG_ABSOLUTE)) {
1016 				return (WALK_NEXT);
1017 			}
1018 		}
1019 		/*
1020 		 * We do the checks for COF_HEAP and COF_QUEUE here only if we
1021 		 * are traversing BYIDH. If the traversal is by callout list,
1022 		 * we do this check in callout_list_cb() to be more
1023 		 * efficient.
1024 		 */
1025 		if ((coargs->flags & COF_HEAP) &&
1026 		    !(list_flags & CALLOUT_LIST_FLAG_HEAPED)) {
1027 			return (WALK_NEXT);
1028 		}
1029 
1030 		if ((coargs->flags & COF_QUEUE) &&
1031 		    !(list_flags & CALLOUT_LIST_FLAG_QUEUED)) {
1032 			return (WALK_NEXT);
1033 		}
1034 	}
1035 
1036 #define	callout_table_mask	((1 << coargs->ctbits) - 1)
1037 	tableid = CALLOUT_ID_TO_TABLE(co->c_xid);
1038 #undef	callout_table_mask
1039 	coid = CO_PLAIN_ID(co->c_xid);
1040 
1041 	if ((coargs->flags & COF_CHDR) && !(coargs->flags & COF_ADDR)) {
1042 		/*
1043 		 * We need to print the headers. If walking by id, then
1044 		 * the list header isn't printed, so we must include
1045 		 * that info here.
1046 		 */
1047 		if (!(coargs->flags & COF_VERBOSE)) {
1048 			mdb_printf("%<u>%3s %-1s %-14s %</u>",
1049 			    "SEQ", "T", "EXP");
1050 		} else if (coargs->flags & COF_BYIDH) {
1051 			mdb_printf("%<u>%-14s %</u>", "EXP");
1052 		}
1053 		mdb_printf("%<u>%-4s %-?s %-20s%</u>",
1054 		    "XHAL", "XID", "FUNC(ARG)");
1055 		if (coargs->flags & COF_LONGLIST) {
1056 			mdb_printf("%<u> %-?s %-?s %-?s %-?s%</u>",
1057 			    "PREVID", "NEXTID", "PREVL", "NEXTL");
1058 			mdb_printf("%<u> %-?s %-4s %-?s%</u>",
1059 			    "DONE", "UTOS", "THREAD");
1060 		}
1061 		mdb_printf("\n");
1062 		coargs->flags &= ~COF_CHDR;
1063 		coargs->flags |= (COF_THDR | COF_LHDR);
1064 	}
1065 
1066 	if (!(coargs->flags & COF_ADDR)) {
1067 		if (!(coargs->flags & COF_VERBOSE)) {
1068 			mdb_printf("%-3d %1s %-14llx ",
1069 			    TABLE_TO_SEQID(tableid),
1070 			    co_typenames[tableid & CALLOUT_TYPE_MASK],
1071 			    (coargs->flags & COF_EXPREL) ?
1072 			    coargs->exp - coargs->now : coargs->exp);
1073 		} else if (coargs->flags & COF_BYIDH) {
1074 			mdb_printf("%-14x ",
1075 			    (coargs->flags & COF_EXPREL) ?
1076 			    coargs->exp - coargs->now : coargs->exp);
1077 		}
1078 		list_flags = coargs->list_flags;
1079 		mdb_printf("%1s%1s%1s%1s %-?llx %a(%p)",
1080 		    (co->c_xid & CALLOUT_EXECUTING) ? "X" : " ",
1081 		    (list_flags & CALLOUT_LIST_FLAG_HRESTIME) ? "H" : " ",
1082 		    (list_flags & CALLOUT_LIST_FLAG_ABSOLUTE) ? "A" : " ",
1083 		    (co->c_xid & CALLOUT_LONGTERM) ? "L" : " ",
1084 		    (long long)coid, co->c_func, co->c_arg);
1085 		if (coargs->flags & COF_LONGLIST) {
1086 			mdb_printf(" %-?p %-?p %-?p %-?p",
1087 			    co->c_idprev, co->c_idnext, co->c_clprev,
1088 			    co->c_clnext);
1089 			mdb_printf(" %-?p %-4d %-0?p",
1090 			    co->c_done, co->c_waiting, co->c_executor);
1091 		}
1092 	} else {
1093 		/* address only */
1094 		mdb_printf("%-0p", addr);
1095 	}
1096 	mdb_printf("\n");
1097 	return (WALK_NEXT);
1098 }
1099 
1100 /* this callback is for callout list handling. idhash is done by callout_t_cb */
1101 /*ARGSUSED*/
1102 static int
callout_list_cb(uintptr_t addr,const void * data,void * priv)1103 callout_list_cb(uintptr_t addr, const void *data, void *priv)
1104 {
1105 	callout_data_t *coargs = (callout_data_t *)priv;
1106 	callout_list_t *cl = (callout_list_t *)data;
1107 	callout_t *coptr;
1108 	int list_flags;
1109 
1110 	if ((coargs == NULL) || (cl == NULL)) {
1111 		return (WALK_ERR);
1112 	}
1113 
1114 	coargs->exp = cl->cl_expiration;
1115 	coargs->list_flags = cl->cl_flags;
1116 	if ((coargs->flags & COF_FREE) &&
1117 	    !(cl->cl_flags & CALLOUT_LIST_FLAG_FREE)) {
1118 		/*
1119 		 * The callout list must have been reallocated. No point in
1120 		 * walking any more.
1121 		 */
1122 		return (WALK_DONE);
1123 	}
1124 	if (!(coargs->flags & COF_FREE) &&
1125 	    (cl->cl_flags & CALLOUT_LIST_FLAG_FREE)) {
1126 		/*
1127 		 * The callout list must have been freed. No point in
1128 		 * walking any more.
1129 		 */
1130 		return (WALK_DONE);
1131 	}
1132 	if ((coargs->flags & COF_TIME) &&
1133 	    (cl->cl_expiration != coargs->time)) {
1134 		return (WALK_NEXT);
1135 	}
1136 	if ((coargs->flags & COF_BEFORE) &&
1137 	    (cl->cl_expiration > coargs->btime)) {
1138 		return (WALK_NEXT);
1139 	}
1140 	if ((coargs->flags & COF_AFTER) &&
1141 	    (cl->cl_expiration < coargs->atime)) {
1142 		return (WALK_NEXT);
1143 	}
1144 	if (!(coargs->flags & COF_EMPTY) &&
1145 	    (cl->cl_callouts.ch_head == NULL)) {
1146 		return (WALK_NEXT);
1147 	}
1148 	/* FOUR cases, each different, !A!B, !AB, A!B, AB */
1149 	if ((coargs->flags & COF_HIRES) && (coargs->flags & COF_ABS)) {
1150 		/* both flags are set, only skip "regular" ones */
1151 		if (! (cl->cl_flags & COF_LIST_FLAGS)) {
1152 			return (WALK_NEXT);
1153 		}
1154 	} else {
1155 		if ((coargs->flags & COF_HIRES) &&
1156 		    !(cl->cl_flags & CALLOUT_LIST_FLAG_HRESTIME)) {
1157 			return (WALK_NEXT);
1158 		}
1159 		if ((coargs->flags & COF_ABS) &&
1160 		    !(cl->cl_flags & CALLOUT_LIST_FLAG_ABSOLUTE)) {
1161 			return (WALK_NEXT);
1162 		}
1163 	}
1164 
1165 	if ((coargs->flags & COF_HEAP) &&
1166 	    !(coargs->list_flags & CALLOUT_LIST_FLAG_HEAPED)) {
1167 		return (WALK_NEXT);
1168 	}
1169 
1170 	if ((coargs->flags & COF_QUEUE) &&
1171 	    !(coargs->list_flags & CALLOUT_LIST_FLAG_QUEUED)) {
1172 		return (WALK_NEXT);
1173 	}
1174 
1175 	if ((coargs->flags & COF_LHDR) && !(coargs->flags & COF_ADDR) &&
1176 	    (coargs->flags & (COF_LIST | COF_VERBOSE))) {
1177 		if (!(coargs->flags & COF_VERBOSE)) {
1178 			/* don't be redundant again */
1179 			mdb_printf("%<u>SEQ T %</u>");
1180 		}
1181 		mdb_printf("%<u>EXP            HA BUCKET "
1182 		    "CALLOUTS         %</u>");
1183 
1184 		if (coargs->flags & COF_LONGLIST) {
1185 			mdb_printf("%<u> %-?s %-?s%</u>",
1186 			    "PREV", "NEXT");
1187 		}
1188 		mdb_printf("\n");
1189 		coargs->flags &= ~COF_LHDR;
1190 		coargs->flags |= (COF_THDR | COF_CHDR);
1191 	}
1192 	if (coargs->flags & (COF_LIST | COF_VERBOSE)) {
1193 		if (!(coargs->flags & COF_ADDR)) {
1194 			if (!(coargs->flags & COF_VERBOSE)) {
1195 				mdb_printf("%3d %1s ",
1196 				    TABLE_TO_SEQID(coargs->ndx),
1197 				    co_typenames[coargs->ndx &
1198 				    CALLOUT_TYPE_MASK]);
1199 			}
1200 
1201 			list_flags = coargs->list_flags;
1202 			mdb_printf("%-14llx %1s%1s %-6d %-0?p ",
1203 			    (coargs->flags & COF_EXPREL) ?
1204 			    coargs->exp - coargs->now : coargs->exp,
1205 			    (list_flags & CALLOUT_LIST_FLAG_HRESTIME) ?
1206 			    "H" : " ",
1207 			    (list_flags & CALLOUT_LIST_FLAG_ABSOLUTE) ?
1208 			    "A" : " ",
1209 			    coargs->bucket, cl->cl_callouts.ch_head);
1210 
1211 			if (coargs->flags & COF_LONGLIST) {
1212 				mdb_printf(" %-?p %-?p",
1213 				    cl->cl_prev, cl->cl_next);
1214 			}
1215 		} else {
1216 			/* address only */
1217 			mdb_printf("%-0p", addr);
1218 		}
1219 		mdb_printf("\n");
1220 		if (coargs->flags & COF_LIST) {
1221 			return (WALK_NEXT);
1222 		}
1223 	}
1224 	/* yet another layer as we walk the actual callouts via list. */
1225 	if (cl->cl_callouts.ch_head == NULL) {
1226 		return (WALK_NEXT);
1227 	}
1228 	/* free list structures do not have valid callouts off of them. */
1229 	if (coargs->flags & COF_FREE) {
1230 		return (WALK_NEXT);
1231 	}
1232 	coptr = (callout_t *)cl->cl_callouts.ch_head;
1233 
1234 	if (coargs->flags & COF_VERBOSE) {
1235 		mdb_inc_indent(4);
1236 	}
1237 	/*
1238 	 * walk callouts using yet another callback routine.
1239 	 * we use callouts_bytime because id hash is handled via
1240 	 * the callout_t_cb callback.
1241 	 */
1242 	if (mdb_pwalk("callouts_bytime", callouts_cb, coargs,
1243 	    (uintptr_t)coptr) == -1) {
1244 		mdb_warn("cannot walk callouts at %p", coptr);
1245 		return (WALK_ERR);
1246 	}
1247 	if (coargs->flags & COF_VERBOSE) {
1248 		mdb_dec_indent(4);
1249 	}
1250 
1251 	return (WALK_NEXT);
1252 }
1253 
1254 /* this callback handles the details of callout table walking. */
1255 static int
callout_t_cb(uintptr_t addr,const void * data,void * priv)1256 callout_t_cb(uintptr_t addr, const void *data, void *priv)
1257 {
1258 	callout_data_t *coargs = (callout_data_t *)priv;
1259 	cot_data_t *cotwd = (cot_data_t *)data;
1260 	callout_table_t *ct = &(cotwd->ct);
1261 	int index, seqid, cotype;
1262 	int i;
1263 	callout_list_t *clptr;
1264 	callout_t *coptr;
1265 
1266 	if ((coargs == NULL) || (ct == NULL) || (coargs->co_table == NULL)) {
1267 		return (WALK_ERR);
1268 	}
1269 
1270 	index =  ((char *)addr - (char *)coargs->co_table) /
1271 	    sizeof (callout_table_t);
1272 	cotype = index & CALLOUT_TYPE_MASK;
1273 	seqid = TABLE_TO_SEQID(index);
1274 
1275 	if ((coargs->flags & COF_SEQID) && (coargs->seqid != seqid)) {
1276 		return (WALK_NEXT);
1277 	}
1278 
1279 	if (!(coargs->flags & COF_REAL) && (cotype == CALLOUT_REALTIME)) {
1280 		return (WALK_NEXT);
1281 	}
1282 
1283 	if (!(coargs->flags & COF_NORM) && (cotype == CALLOUT_NORMAL)) {
1284 		return (WALK_NEXT);
1285 	}
1286 
1287 	if (!(coargs->flags & COF_EMPTY) && (
1288 	    (ct->ct_heap == NULL) || (ct->ct_cyclic == 0))) {
1289 		return (WALK_NEXT);
1290 	}
1291 
1292 	if ((coargs->flags & COF_THDR) && !(coargs->flags & COF_ADDR) &&
1293 	    (coargs->flags & (COF_TABLE | COF_VERBOSE))) {
1294 		/* print table hdr */
1295 		mdb_printf("%<u>%-3s %-1s %-?s %-?s %-?s %-?s%</u>",
1296 		    "SEQ", "T", "FREE", "LFREE", "CYCLIC", "HEAP");
1297 		coargs->flags &= ~COF_THDR;
1298 		coargs->flags |= (COF_LHDR | COF_CHDR);
1299 		if (coargs->flags & COF_LONGLIST) {
1300 			/* more info! */
1301 			mdb_printf("%<u> %-T%-7s %-7s %-?s %-?s %-?s"
1302 			    " %-?s %-?s %-?s%</u>",
1303 			    "HEAPNUM", "HEAPMAX", "TASKQ", "EXPQ", "QUE",
1304 			    "PEND", "FREE", "LOCK");
1305 		}
1306 		mdb_printf("\n");
1307 	}
1308 	if (coargs->flags & (COF_TABLE | COF_VERBOSE)) {
1309 		if (!(coargs->flags & COF_ADDR)) {
1310 			mdb_printf("%-3d %-1s %-0?p %-0?p %-0?p %-?p",
1311 			    seqid, co_typenames[cotype],
1312 			    ct->ct_free, ct->ct_lfree, ct->ct_cyclic,
1313 			    ct->ct_heap);
1314 			if (coargs->flags & COF_LONGLIST)  {
1315 				/* more info! */
1316 				mdb_printf(" %-7d %-7d %-?p %-?p %-?p"
1317 				    " %-?lld %-?lld %-?p",
1318 				    ct->ct_heap_num,  ct->ct_heap_max,
1319 				    ct->ct_taskq, ct->ct_expired.ch_head,
1320 				    ct->ct_queue.ch_head,
1321 				    cotwd->ct_timeouts_pending,
1322 				    cotwd->ct_allocations -
1323 				    cotwd->ct_timeouts_pending,
1324 				    ct->ct_mutex);
1325 			}
1326 		} else {
1327 			/* address only */
1328 			mdb_printf("%-0?p", addr);
1329 		}
1330 		mdb_printf("\n");
1331 		if (coargs->flags & COF_TABLE) {
1332 			return (WALK_NEXT);
1333 		}
1334 	}
1335 
1336 	coargs->ndx = index;
1337 	if (coargs->flags & COF_VERBOSE) {
1338 		mdb_inc_indent(4);
1339 	}
1340 	/* keep digging. */
1341 	if (!(coargs->flags & COF_BYIDH)) {
1342 		/* walk the list hash table */
1343 		if (coargs->flags & COF_FREE) {
1344 			clptr = ct->ct_lfree;
1345 			coargs->bucket = 0;
1346 			if (clptr == NULL) {
1347 				return (WALK_NEXT);
1348 			}
1349 			if (mdb_pwalk("callout_list", callout_list_cb, coargs,
1350 			    (uintptr_t)clptr) == -1) {
1351 				mdb_warn("cannot walk callout free list at %p",
1352 				    clptr);
1353 				return (WALK_ERR);
1354 			}
1355 		} else {
1356 			/* first print the expired list. */
1357 			clptr = (callout_list_t *)ct->ct_expired.ch_head;
1358 			if (clptr != NULL) {
1359 				coargs->bucket = -1;
1360 				if (mdb_pwalk("callout_list", callout_list_cb,
1361 				    coargs, (uintptr_t)clptr) == -1) {
1362 					mdb_warn("cannot walk callout_list"
1363 					    " at %p", clptr);
1364 					return (WALK_ERR);
1365 				}
1366 			}
1367 			/* then, print the callout queue */
1368 			clptr = (callout_list_t *)ct->ct_queue.ch_head;
1369 			if (clptr != NULL) {
1370 				coargs->bucket = -1;
1371 				if (mdb_pwalk("callout_list", callout_list_cb,
1372 				    coargs, (uintptr_t)clptr) == -1) {
1373 					mdb_warn("cannot walk callout_list"
1374 					    " at %p", clptr);
1375 					return (WALK_ERR);
1376 				}
1377 			}
1378 			for (i = 0; i < CALLOUT_BUCKETS; i++) {
1379 				if (ct->ct_clhash == NULL) {
1380 					/* nothing to do */
1381 					break;
1382 				}
1383 				if (cotwd->cot_clhash[i].ch_head == NULL) {
1384 					continue;
1385 				}
1386 				clptr = (callout_list_t *)
1387 				    cotwd->cot_clhash[i].ch_head;
1388 				coargs->bucket = i;
1389 				/* walk list with callback routine. */
1390 				if (mdb_pwalk("callout_list", callout_list_cb,
1391 				    coargs, (uintptr_t)clptr) == -1) {
1392 					mdb_warn("cannot walk callout_list"
1393 					    " at %p", clptr);
1394 					return (WALK_ERR);
1395 				}
1396 			}
1397 		}
1398 	} else {
1399 		/* walk the id hash table. */
1400 		if (coargs->flags & COF_FREE) {
1401 			coptr = ct->ct_free;
1402 			coargs->bucket = 0;
1403 			if (coptr == NULL) {
1404 				return (WALK_NEXT);
1405 			}
1406 			if (mdb_pwalk("callouts_byid", callouts_cb, coargs,
1407 			    (uintptr_t)coptr) == -1) {
1408 				mdb_warn("cannot walk callout id free list"
1409 				    " at %p", coptr);
1410 				return (WALK_ERR);
1411 			}
1412 		} else {
1413 			for (i = 0; i < CALLOUT_BUCKETS; i++) {
1414 				if (ct->ct_idhash == NULL) {
1415 					break;
1416 				}
1417 				coptr = (callout_t *)
1418 				    cotwd->cot_idhash[i].ch_head;
1419 				if (coptr == NULL) {
1420 					continue;
1421 				}
1422 				coargs->bucket = i;
1423 
1424 				/*
1425 				 * walk callouts directly by id. For id
1426 				 * chain, the callout list is just a header,
1427 				 * so there's no need to walk it.
1428 				 */
1429 				if (mdb_pwalk("callouts_byid", callouts_cb,
1430 				    coargs, (uintptr_t)coptr) == -1) {
1431 					mdb_warn("cannot walk callouts at %p",
1432 					    coptr);
1433 					return (WALK_ERR);
1434 				}
1435 			}
1436 		}
1437 	}
1438 	if (coargs->flags & COF_VERBOSE) {
1439 		mdb_dec_indent(4);
1440 	}
1441 	return (WALK_NEXT);
1442 }
1443 
1444 /*
1445  * initialize some common info for both callout dcmds.
1446  */
1447 int
callout_common_init(callout_data_t * coargs)1448 callout_common_init(callout_data_t *coargs)
1449 {
1450 	/* we need a couple of things */
1451 	if (mdb_readvar(&(coargs->co_table), "callout_table") == -1) {
1452 		mdb_warn("failed to read 'callout_table'");
1453 		return (DCMD_ERR);
1454 	}
1455 	/* need to get now in nsecs. Approximate with hrtime vars */
1456 	if (mdb_readsym(&(coargs->now), sizeof (hrtime_t), "hrtime_last") !=
1457 	    sizeof (hrtime_t)) {
1458 		if (mdb_readsym(&(coargs->now), sizeof (hrtime_t),
1459 		    "hrtime_base") != sizeof (hrtime_t)) {
1460 			mdb_warn("Could not determine current system time");
1461 			return (DCMD_ERR);
1462 		}
1463 	}
1464 
1465 	if (mdb_readvar(&(coargs->ctbits), "callout_table_bits") == -1) {
1466 		mdb_warn("failed to read 'callout_table_bits'");
1467 		return (DCMD_ERR);
1468 	}
1469 	if (mdb_readvar(&(coargs->nsec_per_tick), "nsec_per_tick") == -1) {
1470 		mdb_warn("failed to read 'nsec_per_tick'");
1471 		return (DCMD_ERR);
1472 	}
1473 	return (DCMD_OK);
1474 }
1475 
1476 /*
1477  * dcmd to print callouts.  Optional addr limits to specific table.
1478  * Parses lots of options that get passed to callbacks for walkers.
1479  * Has it's own help function.
1480  */
1481 /*ARGSUSED*/
1482 int
callout(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)1483 callout(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1484 {
1485 	callout_data_t coargs;
1486 	/* getopts doesn't help much with stuff like this */
1487 	boolean_t Sflag, Cflag, tflag, aflag, bflag, dflag, kflag;
1488 	char *funcname = NULL;
1489 	char *paramstr = NULL;
1490 	uintptr_t Stmp, Ctmp;	/* for getopt. */
1491 	int retval;
1492 
1493 	coargs.flags = COF_DEFAULT;
1494 	Sflag = Cflag = tflag = bflag = aflag = dflag = kflag = FALSE;
1495 	coargs.seqid = -1;
1496 
1497 	if (mdb_getopts(argc, argv,
1498 	    'r', MDB_OPT_CLRBITS, COF_NORM, &coargs.flags,
1499 	    'n', MDB_OPT_CLRBITS, COF_REAL, &coargs.flags,
1500 	    'l', MDB_OPT_CLRBITS, COF_SHORT, &coargs.flags,
1501 	    's', MDB_OPT_CLRBITS, COF_LONG, &coargs.flags,
1502 	    'x', MDB_OPT_SETBITS, COF_EXEC, &coargs.flags,
1503 	    'h', MDB_OPT_SETBITS, COF_HIRES, &coargs.flags,
1504 	    'B', MDB_OPT_SETBITS, COF_ABS, &coargs.flags,
1505 	    'E', MDB_OPT_SETBITS, COF_EMPTY, &coargs.flags,
1506 	    'd', MDB_OPT_SETBITS, 1, &dflag,
1507 	    'C', MDB_OPT_UINTPTR_SET, &Cflag, &Ctmp,
1508 	    'S', MDB_OPT_UINTPTR_SET, &Sflag, &Stmp,
1509 	    't', MDB_OPT_UINTPTR_SET, &tflag, (uintptr_t *)&coargs.time,
1510 	    'a', MDB_OPT_UINTPTR_SET, &aflag, (uintptr_t *)&coargs.atime,
1511 	    'b', MDB_OPT_UINTPTR_SET, &bflag, (uintptr_t *)&coargs.btime,
1512 	    'k', MDB_OPT_SETBITS, 1, &kflag,
1513 	    'f', MDB_OPT_STR, &funcname,
1514 	    'p', MDB_OPT_STR, &paramstr,
1515 	    'T', MDB_OPT_SETBITS, COF_TABLE, &coargs.flags,
1516 	    'D', MDB_OPT_SETBITS, COF_EXPREL, &coargs.flags,
1517 	    'L', MDB_OPT_SETBITS, COF_LIST, &coargs.flags,
1518 	    'V', MDB_OPT_SETBITS, COF_VERBOSE, &coargs.flags,
1519 	    'v', MDB_OPT_SETBITS, COF_LONGLIST, &coargs.flags,
1520 	    'i', MDB_OPT_SETBITS, COF_BYIDH, &coargs.flags,
1521 	    'F', MDB_OPT_SETBITS, COF_FREE, &coargs.flags,
1522 	    'H', MDB_OPT_SETBITS, COF_HEAP, &coargs.flags,
1523 	    'Q', MDB_OPT_SETBITS, COF_QUEUE, &coargs.flags,
1524 	    'A', MDB_OPT_SETBITS, COF_ADDR, &coargs.flags,
1525 	    NULL) != argc) {
1526 		return (DCMD_USAGE);
1527 	}
1528 
1529 	/* initialize from kernel variables */
1530 	if ((retval = callout_common_init(&coargs)) != DCMD_OK) {
1531 		return (retval);
1532 	}
1533 
1534 	/* do some option post-processing */
1535 	if (kflag) {
1536 		coargs.time *= coargs.nsec_per_tick;
1537 		coargs.atime *= coargs.nsec_per_tick;
1538 		coargs.btime *= coargs.nsec_per_tick;
1539 	}
1540 
1541 	if (dflag) {
1542 		coargs.time += coargs.now;
1543 		coargs.atime += coargs.now;
1544 		coargs.btime += coargs.now;
1545 	}
1546 	if (Sflag) {
1547 		if (flags & DCMD_ADDRSPEC) {
1548 			mdb_printf("-S option conflicts with explicit"
1549 			    " address\n");
1550 			return (DCMD_USAGE);
1551 		}
1552 		coargs.flags |= COF_SEQID;
1553 		coargs.seqid = (int)Stmp;
1554 	}
1555 	if (Cflag) {
1556 		if (flags & DCMD_ADDRSPEC) {
1557 			mdb_printf("-C option conflicts with explicit"
1558 			    " address\n");
1559 			return (DCMD_USAGE);
1560 		}
1561 		if (coargs.flags & COF_SEQID) {
1562 			mdb_printf("-C and -S are mutually exclusive\n");
1563 			return (DCMD_USAGE);
1564 		}
1565 		coargs.cpu = (cpu_t *)Ctmp;
1566 		if (mdb_vread(&coargs.seqid, sizeof (processorid_t),
1567 		    (uintptr_t)&(coargs.cpu->cpu_seqid)) == -1) {
1568 			mdb_warn("failed to read cpu_t at %p", Ctmp);
1569 			return (DCMD_ERR);
1570 		}
1571 		coargs.flags |= COF_SEQID;
1572 	}
1573 	/* avoid null outputs. */
1574 	if (!(coargs.flags & (COF_REAL | COF_NORM))) {
1575 		coargs.flags |= COF_REAL | COF_NORM;
1576 	}
1577 	if (!(coargs.flags & (COF_LONG | COF_SHORT))) {
1578 		coargs.flags |= COF_LONG | COF_SHORT;
1579 	}
1580 	if (tflag) {
1581 		if (aflag || bflag) {
1582 			mdb_printf("-t and -a|b are mutually exclusive\n");
1583 			return (DCMD_USAGE);
1584 		}
1585 		coargs.flags |= COF_TIME;
1586 	}
1587 	if (aflag) {
1588 		coargs.flags |= COF_AFTER;
1589 	}
1590 	if (bflag) {
1591 		coargs.flags |= COF_BEFORE;
1592 	}
1593 	if ((aflag && bflag) && (coargs.btime <= coargs.atime)) {
1594 		mdb_printf("value for -a must be earlier than the value"
1595 		    " for -b.\n");
1596 		return (DCMD_USAGE);
1597 	}
1598 
1599 	if ((coargs.flags & COF_HEAP) && (coargs.flags & COF_QUEUE)) {
1600 		mdb_printf("-H and -Q are mutually exclusive\n");
1601 		return (DCMD_USAGE);
1602 	}
1603 
1604 	if (funcname != NULL) {
1605 		GElf_Sym sym;
1606 
1607 		if (mdb_lookup_by_name(funcname, &sym) != 0) {
1608 			coargs.funcaddr = mdb_strtoull(funcname);
1609 		} else {
1610 			coargs.funcaddr = sym.st_value;
1611 		}
1612 		coargs.flags |= COF_FUNC;
1613 	}
1614 
1615 	if (paramstr != NULL) {
1616 		GElf_Sym sym;
1617 
1618 		if (mdb_lookup_by_name(paramstr, &sym) != 0) {
1619 			coargs.param = mdb_strtoull(paramstr);
1620 		} else {
1621 			coargs.param = sym.st_value;
1622 		}
1623 		coargs.flags |= COF_PARAM;
1624 	}
1625 
1626 	if (!(flags & DCMD_ADDRSPEC)) {
1627 		/* don't pass "dot" if no addr. */
1628 		addr = 0;
1629 	}
1630 	if (addr != 0) {
1631 		/*
1632 		 * a callout table was specified. Ignore -r|n option
1633 		 * to avoid null output.
1634 		 */
1635 		coargs.flags |= (COF_REAL | COF_NORM);
1636 	}
1637 
1638 	if (DCMD_HDRSPEC(flags) || (coargs.flags & COF_VERBOSE)) {
1639 		coargs.flags |= COF_THDR | COF_LHDR | COF_CHDR;
1640 	}
1641 	if (coargs.flags & COF_FREE) {
1642 		coargs.flags |= COF_EMPTY;
1643 		/* -F = free callouts, -FL = free lists */
1644 		if (!(coargs.flags & COF_LIST)) {
1645 			coargs.flags |= COF_BYIDH;
1646 		}
1647 	}
1648 
1649 	/* walk table, using specialized callback routine. */
1650 	if (mdb_pwalk("callout_table", callout_t_cb, &coargs, addr) == -1) {
1651 		mdb_warn("cannot walk callout_table");
1652 		return (DCMD_ERR);
1653 	}
1654 	return (DCMD_OK);
1655 }
1656 
1657 
1658 /*
1659  * Given an extended callout id, dump its information.
1660  */
1661 /*ARGSUSED*/
1662 int
calloutid(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)1663 calloutid(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1664 {
1665 	callout_data_t coargs;
1666 	callout_table_t *ctptr;
1667 	callout_table_t ct;
1668 	callout_id_t coid;
1669 	callout_t *coptr;
1670 	int tableid;
1671 	callout_id_t xid;
1672 	ulong_t idhash;
1673 	int i, retval;
1674 	const mdb_arg_t *arg;
1675 	size_t size;
1676 	callout_hash_t cot_idhash[CALLOUT_BUCKETS];
1677 
1678 	coargs.flags = COF_DEFAULT | COF_BYIDH;
1679 	i = mdb_getopts(argc, argv,
1680 	    'd', MDB_OPT_SETBITS, COF_DECODE, &coargs.flags,
1681 	    'v', MDB_OPT_SETBITS, COF_LONGLIST, &coargs.flags,
1682 	    NULL);
1683 	argc -= i;
1684 	argv += i;
1685 
1686 	if (argc != 1) {
1687 		return (DCMD_USAGE);
1688 	}
1689 	arg = &argv[0];
1690 
1691 	if (arg->a_type == MDB_TYPE_IMMEDIATE) {
1692 		xid = arg->a_un.a_val;
1693 	} else {
1694 		xid = (callout_id_t)mdb_strtoull(arg->a_un.a_str);
1695 	}
1696 
1697 	if (DCMD_HDRSPEC(flags)) {
1698 		coargs.flags |= COF_CHDR;
1699 	}
1700 
1701 
1702 	/* initialize from kernel variables */
1703 	if ((retval = callout_common_init(&coargs)) != DCMD_OK) {
1704 		return (retval);
1705 	}
1706 
1707 	/* we must massage the environment so that the macros will play nice */
1708 #define	callout_table_mask	((1 << coargs.ctbits) - 1)
1709 #define	callout_table_bits	coargs.ctbits
1710 #define	nsec_per_tick		coargs.nsec_per_tick
1711 	tableid = CALLOUT_ID_TO_TABLE(xid);
1712 	idhash = CALLOUT_IDHASH(xid);
1713 #undef	callouts_table_bits
1714 #undef	callout_table_mask
1715 #undef	nsec_per_tick
1716 	coid = CO_PLAIN_ID(xid);
1717 
1718 	if (flags & DCMD_ADDRSPEC) {
1719 		mdb_printf("calloutid does not accept explicit address.\n");
1720 		return (DCMD_USAGE);
1721 	}
1722 
1723 	if (coargs.flags & COF_DECODE) {
1724 		if (DCMD_HDRSPEC(flags)) {
1725 			mdb_printf("%<u>%3s %1s %2s %-?s %-6s %</u>\n",
1726 			    "SEQ", "T", "XL", "XID", "IDHASH");
1727 		}
1728 		mdb_printf("%-3d %1s %1s%1s %-?llx %-6d\n",
1729 		    TABLE_TO_SEQID(tableid),
1730 		    co_typenames[tableid & CALLOUT_TYPE_MASK],
1731 		    (xid & CALLOUT_EXECUTING) ? "X" : " ",
1732 		    (xid & CALLOUT_LONGTERM) ? "L" : " ",
1733 		    (long long)coid, idhash);
1734 		return (DCMD_OK);
1735 	}
1736 
1737 	/* get our table. Note this relies on the types being correct */
1738 	ctptr = coargs.co_table + tableid;
1739 	if (mdb_vread(&ct, sizeof (callout_table_t), (uintptr_t)ctptr) == -1) {
1740 		mdb_warn("failed to read callout_table at %p", ctptr);
1741 		return (DCMD_ERR);
1742 	}
1743 	size = sizeof (callout_hash_t) * CALLOUT_BUCKETS;
1744 	if (ct.ct_idhash != NULL) {
1745 		if (mdb_vread(&(cot_idhash), size,
1746 		    (uintptr_t)ct.ct_idhash) == -1) {
1747 			mdb_warn("failed to read id_hash at %p",
1748 			    ct.ct_idhash);
1749 			return (WALK_ERR);
1750 		}
1751 	}
1752 
1753 	/* callout at beginning of hash chain */
1754 	if (ct.ct_idhash == NULL) {
1755 		mdb_printf("id hash chain for this xid is empty\n");
1756 		return (DCMD_ERR);
1757 	}
1758 	coptr = (callout_t *)cot_idhash[idhash].ch_head;
1759 	if (coptr == NULL) {
1760 		mdb_printf("id hash chain for this xid is empty\n");
1761 		return (DCMD_ERR);
1762 	}
1763 
1764 	coargs.ndx = tableid;
1765 	coargs.bucket = idhash;
1766 
1767 	/* use the walker, luke */
1768 	if (mdb_pwalk("callouts_byid", callouts_cb, &coargs,
1769 	    (uintptr_t)coptr) == -1) {
1770 		mdb_warn("cannot walk callouts at %p", coptr);
1771 		return (WALK_ERR);
1772 	}
1773 
1774 	return (DCMD_OK);
1775 }
1776 
1777 void
callout_help(void)1778 callout_help(void)
1779 {
1780 	mdb_printf("callout: display callouts.\n"
1781 	    "Given a callout table address, display callouts from table.\n"
1782 	    "Without an address, display callouts from all tables.\n"
1783 	    "options:\n"
1784 	    " -r|n : limit display to (r)ealtime or (n)ormal type callouts\n"
1785 	    " -s|l : limit display to (s)hort-term ids or (l)ong-term ids\n"
1786 	    " -x : limit display to callouts which are executing\n"
1787 	    " -h : limit display to callouts based on hrestime\n"
1788 	    " -B : limit display to callouts based on absolute time\n"
1789 	    " -t|a|b nsec: limit display to callouts that expire a(t) time,"
1790 	    " (a)fter time,\n     or (b)efore time. Use -a and -b together "
1791 	    " to specify a range.\n     For \"now\", use -d[t|a|b] 0.\n"
1792 	    " -d : interpret time option to -t|a|b as delta from current time\n"
1793 	    " -k : use ticks instead of nanoseconds as arguments to"
1794 	    " -t|a|b. Note that\n     ticks are less accurate and may not"
1795 	    " match other tick times (ie: lbolt).\n"
1796 	    " -D : display exiration time as delta from current time\n"
1797 	    " -S seqid : limit display to callouts for this cpu sequence id\n"
1798 	    " -C addr :  limit display to callouts for this cpu pointer\n"
1799 	    " -f name|addr : limit display to callouts with this function\n"
1800 	    " -p name|addr : limit display to callouts functions with this"
1801 	    " parameter\n"
1802 	    " -T : display the callout table itself, instead of callouts\n"
1803 	    " -L : display callout lists instead of callouts\n"
1804 	    " -E : with -T or L, display empty data structures.\n"
1805 	    " -i : traverse callouts by id hash instead of list hash\n"
1806 	    " -F : walk free callout list (free list with -i) instead\n"
1807 	    " -v : display more info for each item\n"
1808 	    " -V : show details of each level of info as it is traversed\n"
1809 	    " -H : limit display to callouts in the callout heap\n"
1810 	    " -Q : limit display to callouts in the callout queue\n"
1811 	    " -A : show only addresses. Useful for pipelines.\n");
1812 }
1813 
1814 void
calloutid_help(void)1815 calloutid_help(void)
1816 {
1817 	mdb_printf("calloutid: display callout by id.\n"
1818 	    "Given an extended callout id, display the callout infomation.\n"
1819 	    "options:\n"
1820 	    " -d : do not dereference callout, just decode the id.\n"
1821 	    " -v : verbose display more info about the callout\n");
1822 }
1823 
1824 /*ARGSUSED*/
1825 int
class(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)1826 class(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1827 {
1828 	long num_classes, i;
1829 	sclass_t *class_tbl;
1830 	GElf_Sym g_sclass;
1831 	char class_name[PC_CLNMSZ];
1832 	size_t tbl_size;
1833 
1834 	if (mdb_lookup_by_name("sclass", &g_sclass) == -1) {
1835 		mdb_warn("failed to find symbol sclass\n");
1836 		return (DCMD_ERR);
1837 	}
1838 
1839 	tbl_size = (size_t)g_sclass.st_size;
1840 	num_classes = tbl_size / (sizeof (sclass_t));
1841 	class_tbl = mdb_alloc(tbl_size, UM_SLEEP | UM_GC);
1842 
1843 	if (mdb_readsym(class_tbl, tbl_size, "sclass") == -1) {
1844 		mdb_warn("failed to read sclass");
1845 		return (DCMD_ERR);
1846 	}
1847 
1848 	mdb_printf("%<u>%4s %-10s %-24s %-24s%</u>\n", "SLOT", "NAME",
1849 	    "INIT FCN", "CLASS FCN");
1850 
1851 	for (i = 0; i < num_classes; i++) {
1852 		if (mdb_vread(class_name, sizeof (class_name),
1853 		    (uintptr_t)class_tbl[i].cl_name) == -1)
1854 			(void) strcpy(class_name, "???");
1855 
1856 		mdb_printf("%4ld %-10s %-24a %-24a\n", i, class_name,
1857 		    class_tbl[i].cl_init, class_tbl[i].cl_funcs);
1858 	}
1859 
1860 	return (DCMD_OK);
1861 }
1862 
1863 #define	FSNAMELEN	32	/* Max len of FS name we read from vnodeops */
1864 
1865 int
vnode2path(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)1866 vnode2path(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1867 {
1868 	uintptr_t rootdir;
1869 	vnode_t vn;
1870 	char buf[MAXPATHLEN];
1871 
1872 	uint_t opt_F = FALSE;
1873 
1874 	if (mdb_getopts(argc, argv,
1875 	    'F', MDB_OPT_SETBITS, TRUE, &opt_F, NULL) != argc)
1876 		return (DCMD_USAGE);
1877 
1878 	if (!(flags & DCMD_ADDRSPEC)) {
1879 		mdb_warn("expected explicit vnode_t address before ::\n");
1880 		return (DCMD_USAGE);
1881 	}
1882 
1883 	if (mdb_readvar(&rootdir, "rootdir") == -1) {
1884 		mdb_warn("failed to read rootdir");
1885 		return (DCMD_ERR);
1886 	}
1887 
1888 	if (mdb_vnode2path(addr, buf, sizeof (buf)) == -1)
1889 		return (DCMD_ERR);
1890 
1891 	if (*buf == '\0') {
1892 		mdb_printf("??\n");
1893 		return (DCMD_OK);
1894 	}
1895 
1896 	mdb_printf("%s", buf);
1897 	if (opt_F && buf[strlen(buf)-1] != '/' &&
1898 	    mdb_vread(&vn, sizeof (vn), addr) == sizeof (vn))
1899 		mdb_printf("%c", mdb_vtype2chr(vn.v_type, 0));
1900 	mdb_printf("\n");
1901 
1902 	return (DCMD_OK);
1903 }
1904 
1905 int
ld_walk_init(mdb_walk_state_t * wsp)1906 ld_walk_init(mdb_walk_state_t *wsp)
1907 {
1908 	wsp->walk_data = (void *)wsp->walk_addr;
1909 	return (WALK_NEXT);
1910 }
1911 
1912 int
ld_walk_step(mdb_walk_state_t * wsp)1913 ld_walk_step(mdb_walk_state_t *wsp)
1914 {
1915 	int status;
1916 	lock_descriptor_t ld;
1917 
1918 	if (mdb_vread(&ld, sizeof (lock_descriptor_t), wsp->walk_addr) == -1) {
1919 		mdb_warn("couldn't read lock_descriptor_t at %p\n",
1920 		    wsp->walk_addr);
1921 		return (WALK_ERR);
1922 	}
1923 
1924 	status = wsp->walk_callback(wsp->walk_addr, &ld, wsp->walk_cbdata);
1925 	if (status == WALK_ERR)
1926 		return (WALK_ERR);
1927 
1928 	wsp->walk_addr = (uintptr_t)ld.l_next;
1929 	if (wsp->walk_addr == (uintptr_t)wsp->walk_data)
1930 		return (WALK_DONE);
1931 
1932 	return (status);
1933 }
1934 
1935 int
lg_walk_init(mdb_walk_state_t * wsp)1936 lg_walk_init(mdb_walk_state_t *wsp)
1937 {
1938 	GElf_Sym sym;
1939 
1940 	if (mdb_lookup_by_name("lock_graph", &sym) == -1) {
1941 		mdb_warn("failed to find symbol 'lock_graph'\n");
1942 		return (WALK_ERR);
1943 	}
1944 
1945 	wsp->walk_addr = (uintptr_t)sym.st_value;
1946 	wsp->walk_data = (void *)(uintptr_t)(sym.st_value + sym.st_size);
1947 
1948 	return (WALK_NEXT);
1949 }
1950 
1951 typedef struct lg_walk_data {
1952 	uintptr_t startaddr;
1953 	mdb_walk_cb_t callback;
1954 	void *data;
1955 } lg_walk_data_t;
1956 
1957 /*
1958  * We can't use ::walk lock_descriptor directly, because the head of each graph
1959  * is really a dummy lock.  Rather than trying to dynamically determine if this
1960  * is a dummy node or not, we just filter out the initial element of the
1961  * list.
1962  */
1963 static int
lg_walk_cb(uintptr_t addr,const void * data,void * priv)1964 lg_walk_cb(uintptr_t addr, const void *data, void *priv)
1965 {
1966 	lg_walk_data_t *lw = priv;
1967 
1968 	if (addr != lw->startaddr)
1969 		return (lw->callback(addr, data, lw->data));
1970 
1971 	return (WALK_NEXT);
1972 }
1973 
1974 int
lg_walk_step(mdb_walk_state_t * wsp)1975 lg_walk_step(mdb_walk_state_t *wsp)
1976 {
1977 	graph_t *graph;
1978 	lg_walk_data_t lw;
1979 
1980 	if (wsp->walk_addr >= (uintptr_t)wsp->walk_data)
1981 		return (WALK_DONE);
1982 
1983 	if (mdb_vread(&graph, sizeof (graph), wsp->walk_addr) == -1) {
1984 		mdb_warn("failed to read graph_t at %p", wsp->walk_addr);
1985 		return (WALK_ERR);
1986 	}
1987 
1988 	wsp->walk_addr += sizeof (graph);
1989 
1990 	if (graph == NULL)
1991 		return (WALK_NEXT);
1992 
1993 	lw.callback = wsp->walk_callback;
1994 	lw.data = wsp->walk_cbdata;
1995 
1996 	lw.startaddr = (uintptr_t)&(graph->active_locks);
1997 	if (mdb_pwalk("lock_descriptor", lg_walk_cb, &lw, lw.startaddr)) {
1998 		mdb_warn("couldn't walk lock_descriptor at %p\n", lw.startaddr);
1999 		return (WALK_ERR);
2000 	}
2001 
2002 	lw.startaddr = (uintptr_t)&(graph->sleeping_locks);
2003 	if (mdb_pwalk("lock_descriptor", lg_walk_cb, &lw, lw.startaddr)) {
2004 		mdb_warn("couldn't walk lock_descriptor at %p\n", lw.startaddr);
2005 		return (WALK_ERR);
2006 	}
2007 
2008 	return (WALK_NEXT);
2009 }
2010 
2011 /*
2012  * The space available for the path corresponding to the locked vnode depends
2013  * on whether we are printing 32- or 64-bit addresses.
2014  */
2015 #ifdef _LP64
2016 #define	LM_VNPATHLEN	20
2017 #else
2018 #define	LM_VNPATHLEN	30
2019 #endif
2020 
2021 typedef struct mdb_lminfo_proc {
2022 	struct {
2023 		char		u_comm[MAXCOMLEN + 1];
2024 	} p_user;
2025 } mdb_lminfo_proc_t;
2026 
2027 /*ARGSUSED*/
2028 static int
lminfo_cb(uintptr_t addr,const void * data,void * priv)2029 lminfo_cb(uintptr_t addr, const void *data, void *priv)
2030 {
2031 	const lock_descriptor_t *ld = data;
2032 	char buf[LM_VNPATHLEN];
2033 	mdb_lminfo_proc_t p;
2034 	uintptr_t paddr = 0;
2035 
2036 	if (ld->l_flock.l_pid != 0)
2037 		paddr = mdb_pid2proc(ld->l_flock.l_pid, NULL);
2038 
2039 	if (paddr != 0)
2040 		mdb_ctf_vread(&p, "proc_t", "mdb_lminfo_proc_t", paddr, 0);
2041 
2042 	mdb_printf("%-?p %2s %04x %6d %-16s %-?p ",
2043 	    addr, ld->l_type == F_RDLCK ? "RD" :
2044 	    ld->l_type == F_WRLCK ? "WR" : "??",
2045 	    ld->l_state, ld->l_flock.l_pid,
2046 	    ld->l_flock.l_pid == 0 ? "<kernel>" :
2047 	    paddr == 0 ? "<defunct>" : p.p_user.u_comm, ld->l_vnode);
2048 
2049 	mdb_vnode2path((uintptr_t)ld->l_vnode, buf,
2050 	    sizeof (buf));
2051 	mdb_printf("%s\n", buf);
2052 
2053 	return (WALK_NEXT);
2054 }
2055 
2056 /*ARGSUSED*/
2057 int
lminfo(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)2058 lminfo(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2059 {
2060 	if (DCMD_HDRSPEC(flags))
2061 		mdb_printf("%<u>%-?s %2s %4s %6s %-16s %-?s %s%</u>\n",
2062 		    "ADDR", "TP", "FLAG", "PID", "COMM", "VNODE", "PATH");
2063 
2064 	return (mdb_pwalk("lock_graph", lminfo_cb, NULL, 0));
2065 }
2066 
2067 typedef struct mdb_whereopen {
2068 	uint_t mwo_flags;
2069 	uintptr_t mwo_target;
2070 	boolean_t mwo_found;
2071 } mdb_whereopen_t;
2072 
2073 /*ARGSUSED*/
2074 int
whereopen_fwalk(uintptr_t addr,const void * farg,void * arg)2075 whereopen_fwalk(uintptr_t addr, const void *farg, void *arg)
2076 {
2077 	const struct file *f = farg;
2078 	mdb_whereopen_t *mwo = arg;
2079 
2080 	if ((uintptr_t)f->f_vnode == mwo->mwo_target) {
2081 		if ((mwo->mwo_flags & DCMD_PIPE_OUT) == 0 &&
2082 		    !mwo->mwo_found) {
2083 			mdb_printf("file %p\n", addr);
2084 		}
2085 		mwo->mwo_found = B_TRUE;
2086 	}
2087 
2088 	return (WALK_NEXT);
2089 }
2090 
2091 /*ARGSUSED*/
2092 int
whereopen_pwalk(uintptr_t addr,const void * ignored,void * arg)2093 whereopen_pwalk(uintptr_t addr, const void *ignored, void *arg)
2094 {
2095 	mdb_whereopen_t *mwo = arg;
2096 
2097 	mwo->mwo_found = B_FALSE;
2098 	if (mdb_pwalk("file", whereopen_fwalk, mwo, addr) == -1) {
2099 		mdb_warn("couldn't file walk proc %p", addr);
2100 		return (WALK_ERR);
2101 	}
2102 
2103 	if (mwo->mwo_found) {
2104 		mdb_printf("%p\n", addr);
2105 	}
2106 
2107 	return (WALK_NEXT);
2108 }
2109 
2110 /*ARGSUSED*/
2111 int
whereopen(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)2112 whereopen(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2113 {
2114 	mdb_whereopen_t mwo;
2115 
2116 	if (!(flags & DCMD_ADDRSPEC) || addr == 0)
2117 		return (DCMD_USAGE);
2118 
2119 	mwo.mwo_flags = flags;
2120 	mwo.mwo_target = addr;
2121 	mwo.mwo_found = B_FALSE;
2122 
2123 	if (mdb_walk("proc", whereopen_pwalk, &mwo) == -1) {
2124 		mdb_warn("can't proc walk");
2125 		return (DCMD_ERR);
2126 	}
2127 
2128 	return (DCMD_OK);
2129 }
2130 
2131 typedef struct datafmt {
2132 	char	*hdr1;
2133 	char	*hdr2;
2134 	char	*dashes;
2135 	char	*fmt;
2136 } datafmt_t;
2137 
2138 static datafmt_t kmemfmt[] = {
2139 	{ "cache                    ", "name                     ",
2140 	"-------------------------", "%-25s "				},
2141 	{ "   buf",	"  size",	"------",	"%6u "		},
2142 	{ "   buf",	"in use",	"------",	"%6u "		},
2143 	{ "   buf",	" total",	"------",	"%6u "		},
2144 	{ "   memory",	"   in use",	"----------",	"%10lu%c "	},
2145 	{ "    alloc",	"  succeed",	"---------",	"%9u "		},
2146 	{ "alloc",	" fail",	"-----",	"%5u "		},
2147 	{ NULL,		NULL,		NULL,		NULL		}
2148 };
2149 
2150 static datafmt_t vmemfmt[] = {
2151 	{ "vmem                     ", "name                     ",
2152 	"-------------------------", "%-*s "				},
2153 	{ "   memory",	"   in use",	"----------",	"%9llu%c "	},
2154 	{ "    memory",	"     total",	"-----------",	"%10llu%c "	},
2155 	{ "   memory",	"   import",	"----------",	"%9llu%c "	},
2156 	{ "    alloc",	"  succeed",	"---------",	"%9llu "	},
2157 	{ "alloc",	" fail",	"-----",	"%5llu "	},
2158 	{ NULL,		NULL,		NULL,		NULL		}
2159 };
2160 
2161 /*ARGSUSED*/
2162 static int
kmastat_cpu_avail(uintptr_t addr,const kmem_cpu_cache_t * ccp,int * avail)2163 kmastat_cpu_avail(uintptr_t addr, const kmem_cpu_cache_t *ccp, int *avail)
2164 {
2165 	short rounds, prounds;
2166 
2167 	if (KMEM_DUMPCC(ccp)) {
2168 		rounds = ccp->cc_dump_rounds;
2169 		prounds = ccp->cc_dump_prounds;
2170 	} else {
2171 		rounds = ccp->cc_rounds;
2172 		prounds = ccp->cc_prounds;
2173 	}
2174 	if (rounds > 0)
2175 		*avail += rounds;
2176 	if (prounds > 0)
2177 		*avail += prounds;
2178 
2179 	return (WALK_NEXT);
2180 }
2181 
2182 /*ARGSUSED*/
2183 static int
kmastat_cpu_alloc(uintptr_t addr,const kmem_cpu_cache_t * ccp,int * alloc)2184 kmastat_cpu_alloc(uintptr_t addr, const kmem_cpu_cache_t *ccp, int *alloc)
2185 {
2186 	*alloc += ccp->cc_alloc;
2187 
2188 	return (WALK_NEXT);
2189 }
2190 
2191 /*ARGSUSED*/
2192 static int
kmastat_slab_avail(uintptr_t addr,const kmem_slab_t * sp,int * avail)2193 kmastat_slab_avail(uintptr_t addr, const kmem_slab_t *sp, int *avail)
2194 {
2195 	*avail += sp->slab_chunks - sp->slab_refcnt;
2196 
2197 	return (WALK_NEXT);
2198 }
2199 
2200 typedef struct kmastat_vmem {
2201 	uintptr_t kv_addr;
2202 	struct kmastat_vmem *kv_next;
2203 	size_t kv_meminuse;
2204 	int kv_alloc;
2205 	int kv_fail;
2206 } kmastat_vmem_t;
2207 
2208 typedef struct kmastat_args {
2209 	kmastat_vmem_t **ka_kvpp;
2210 	uint_t ka_shift;
2211 } kmastat_args_t;
2212 
2213 static int
kmastat_cache(uintptr_t addr,const kmem_cache_t * cp,kmastat_args_t * kap)2214 kmastat_cache(uintptr_t addr, const kmem_cache_t *cp, kmastat_args_t *kap)
2215 {
2216 	kmastat_vmem_t **kvpp = kap->ka_kvpp;
2217 	kmastat_vmem_t *kv;
2218 	datafmt_t *dfp = kmemfmt;
2219 	int magsize;
2220 
2221 	int avail, alloc, total;
2222 	size_t meminuse = (cp->cache_slab_create - cp->cache_slab_destroy) *
2223 	    cp->cache_slabsize;
2224 
2225 	mdb_walk_cb_t cpu_avail = (mdb_walk_cb_t)kmastat_cpu_avail;
2226 	mdb_walk_cb_t cpu_alloc = (mdb_walk_cb_t)kmastat_cpu_alloc;
2227 	mdb_walk_cb_t slab_avail = (mdb_walk_cb_t)kmastat_slab_avail;
2228 
2229 	magsize = kmem_get_magsize(cp);
2230 
2231 	alloc = cp->cache_slab_alloc + cp->cache_full.ml_alloc;
2232 	avail = cp->cache_full.ml_total * magsize;
2233 	total = cp->cache_buftotal;
2234 
2235 	(void) mdb_pwalk("kmem_cpu_cache", cpu_alloc, &alloc, addr);
2236 	(void) mdb_pwalk("kmem_cpu_cache", cpu_avail, &avail, addr);
2237 	(void) mdb_pwalk("kmem_slab_partial", slab_avail, &avail, addr);
2238 
2239 	for (kv = *kvpp; kv != NULL; kv = kv->kv_next) {
2240 		if (kv->kv_addr == (uintptr_t)cp->cache_arena)
2241 			goto out;
2242 	}
2243 
2244 	kv = mdb_zalloc(sizeof (kmastat_vmem_t), UM_SLEEP | UM_GC);
2245 	kv->kv_next = *kvpp;
2246 	kv->kv_addr = (uintptr_t)cp->cache_arena;
2247 	*kvpp = kv;
2248 out:
2249 	kv->kv_meminuse += meminuse;
2250 	kv->kv_alloc += alloc;
2251 	kv->kv_fail += cp->cache_alloc_fail;
2252 
2253 	mdb_printf((dfp++)->fmt, cp->cache_name);
2254 	mdb_printf((dfp++)->fmt, cp->cache_bufsize);
2255 	mdb_printf((dfp++)->fmt, total - avail);
2256 	mdb_printf((dfp++)->fmt, total);
2257 	mdb_printf((dfp++)->fmt, meminuse >> kap->ka_shift,
2258 	    kap->ka_shift == GIGS ? 'G' : kap->ka_shift == MEGS ? 'M' :
2259 	    kap->ka_shift == KILOS ? 'K' : 'B');
2260 	mdb_printf((dfp++)->fmt, alloc);
2261 	mdb_printf((dfp++)->fmt, cp->cache_alloc_fail);
2262 	mdb_printf("\n");
2263 
2264 	return (WALK_NEXT);
2265 }
2266 
2267 static int
kmastat_vmem_totals(uintptr_t addr,const vmem_t * v,kmastat_args_t * kap)2268 kmastat_vmem_totals(uintptr_t addr, const vmem_t *v, kmastat_args_t *kap)
2269 {
2270 	kmastat_vmem_t *kv = *kap->ka_kvpp;
2271 	size_t len;
2272 
2273 	while (kv != NULL && kv->kv_addr != addr)
2274 		kv = kv->kv_next;
2275 
2276 	if (kv == NULL || kv->kv_alloc == 0)
2277 		return (WALK_NEXT);
2278 
2279 	len = MIN(17, strlen(v->vm_name));
2280 
2281 	mdb_printf("Total [%s]%*s %6s %6s %6s %10lu%c %9u %5u\n", v->vm_name,
2282 	    17 - len, "", "", "", "",
2283 	    kv->kv_meminuse >> kap->ka_shift,
2284 	    kap->ka_shift == GIGS ? 'G' : kap->ka_shift == MEGS ? 'M' :
2285 	    kap->ka_shift == KILOS ? 'K' : 'B', kv->kv_alloc, kv->kv_fail);
2286 
2287 	return (WALK_NEXT);
2288 }
2289 
2290 /*ARGSUSED*/
2291 static int
kmastat_vmem(uintptr_t addr,const vmem_t * v,const uint_t * shiftp)2292 kmastat_vmem(uintptr_t addr, const vmem_t *v, const uint_t *shiftp)
2293 {
2294 	datafmt_t *dfp = vmemfmt;
2295 	const vmem_kstat_t *vkp = &v->vm_kstat;
2296 	uintptr_t paddr;
2297 	vmem_t parent;
2298 	int ident = 0;
2299 
2300 	for (paddr = (uintptr_t)v->vm_source; paddr != 0; ident += 4) {
2301 		if (mdb_vread(&parent, sizeof (parent), paddr) == -1) {
2302 			mdb_warn("couldn't trace %p's ancestry", addr);
2303 			ident = 0;
2304 			break;
2305 		}
2306 		paddr = (uintptr_t)parent.vm_source;
2307 	}
2308 
2309 	mdb_printf("%*s", ident, "");
2310 	mdb_printf((dfp++)->fmt, 25 - ident, v->vm_name);
2311 	mdb_printf((dfp++)->fmt, vkp->vk_mem_inuse.value.ui64 >> *shiftp,
2312 	    *shiftp == GIGS ? 'G' : *shiftp == MEGS ? 'M' :
2313 	    *shiftp == KILOS ? 'K' : 'B');
2314 	mdb_printf((dfp++)->fmt, vkp->vk_mem_total.value.ui64 >> *shiftp,
2315 	    *shiftp == GIGS ? 'G' : *shiftp == MEGS ? 'M' :
2316 	    *shiftp == KILOS ? 'K' : 'B');
2317 	mdb_printf((dfp++)->fmt, vkp->vk_mem_import.value.ui64 >> *shiftp,
2318 	    *shiftp == GIGS ? 'G' : *shiftp == MEGS ? 'M' :
2319 	    *shiftp == KILOS ? 'K' : 'B');
2320 	mdb_printf((dfp++)->fmt, vkp->vk_alloc.value.ui64);
2321 	mdb_printf((dfp++)->fmt, vkp->vk_fail.value.ui64);
2322 
2323 	mdb_printf("\n");
2324 
2325 	return (WALK_NEXT);
2326 }
2327 
2328 /*ARGSUSED*/
2329 int
kmastat(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)2330 kmastat(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2331 {
2332 	kmastat_vmem_t *kv = NULL;
2333 	datafmt_t *dfp;
2334 	kmastat_args_t ka;
2335 
2336 	ka.ka_shift = 0;
2337 	if (mdb_getopts(argc, argv,
2338 	    'k', MDB_OPT_SETBITS, KILOS, &ka.ka_shift,
2339 	    'm', MDB_OPT_SETBITS, MEGS, &ka.ka_shift,
2340 	    'g', MDB_OPT_SETBITS, GIGS, &ka.ka_shift, NULL) != argc)
2341 		return (DCMD_USAGE);
2342 
2343 	for (dfp = kmemfmt; dfp->hdr1 != NULL; dfp++)
2344 		mdb_printf("%s ", dfp->hdr1);
2345 	mdb_printf("\n");
2346 
2347 	for (dfp = kmemfmt; dfp->hdr1 != NULL; dfp++)
2348 		mdb_printf("%s ", dfp->hdr2);
2349 	mdb_printf("\n");
2350 
2351 	for (dfp = kmemfmt; dfp->hdr1 != NULL; dfp++)
2352 		mdb_printf("%s ", dfp->dashes);
2353 	mdb_printf("\n");
2354 
2355 	ka.ka_kvpp = &kv;
2356 	if (mdb_walk("kmem_cache", (mdb_walk_cb_t)kmastat_cache, &ka) == -1) {
2357 		mdb_warn("can't walk 'kmem_cache'");
2358 		return (DCMD_ERR);
2359 	}
2360 
2361 	for (dfp = kmemfmt; dfp->hdr1 != NULL; dfp++)
2362 		mdb_printf("%s ", dfp->dashes);
2363 	mdb_printf("\n");
2364 
2365 	if (mdb_walk("vmem", (mdb_walk_cb_t)kmastat_vmem_totals, &ka) == -1) {
2366 		mdb_warn("can't walk 'vmem'");
2367 		return (DCMD_ERR);
2368 	}
2369 
2370 	for (dfp = kmemfmt; dfp->hdr1 != NULL; dfp++)
2371 		mdb_printf("%s ", dfp->dashes);
2372 	mdb_printf("\n");
2373 
2374 	mdb_printf("\n");
2375 
2376 	for (dfp = vmemfmt; dfp->hdr1 != NULL; dfp++)
2377 		mdb_printf("%s ", dfp->hdr1);
2378 	mdb_printf("\n");
2379 
2380 	for (dfp = vmemfmt; dfp->hdr1 != NULL; dfp++)
2381 		mdb_printf("%s ", dfp->hdr2);
2382 	mdb_printf("\n");
2383 
2384 	for (dfp = vmemfmt; dfp->hdr1 != NULL; dfp++)
2385 		mdb_printf("%s ", dfp->dashes);
2386 	mdb_printf("\n");
2387 
2388 	if (mdb_walk("vmem", (mdb_walk_cb_t)kmastat_vmem, &ka.ka_shift) == -1) {
2389 		mdb_warn("can't walk 'vmem'");
2390 		return (DCMD_ERR);
2391 	}
2392 
2393 	for (dfp = vmemfmt; dfp->hdr1 != NULL; dfp++)
2394 		mdb_printf("%s ", dfp->dashes);
2395 	mdb_printf("\n");
2396 	return (DCMD_OK);
2397 }
2398 
2399 /*
2400  * Our ::kgrep callback scans the entire kernel VA space (kas).  kas is made
2401  * up of a set of 'struct seg's.  We could just scan each seg en masse, but
2402  * unfortunately, a few of the segs are both large and sparse, so we could
2403  * spend quite a bit of time scanning VAs which have no backing pages.
2404  *
2405  * So for the few very sparse segs, we skip the segment itself, and scan
2406  * the allocated vmem_segs in the vmem arena which manages that part of kas.
2407  * Currently, we do this for:
2408  *
2409  *	SEG		VMEM ARENA
2410  *	kvseg		heap_arena
2411  *	kvseg32		heap32_arena
2412  *	kvseg_core	heap_core_arena
2413  *
2414  * In addition, we skip the segkpm segment in its entirety, since it is very
2415  * sparse, and contains no new kernel data.
2416  */
2417 typedef struct kgrep_walk_data {
2418 	kgrep_cb_func *kg_cb;
2419 	void *kg_cbdata;
2420 	uintptr_t kg_kvseg;
2421 	uintptr_t kg_kvseg32;
2422 	uintptr_t kg_kvseg_core;
2423 	uintptr_t kg_segkpm;
2424 	uintptr_t kg_heap_lp_base;
2425 	uintptr_t kg_heap_lp_end;
2426 } kgrep_walk_data_t;
2427 
2428 static int
kgrep_walk_seg(uintptr_t addr,const struct seg * seg,kgrep_walk_data_t * kg)2429 kgrep_walk_seg(uintptr_t addr, const struct seg *seg, kgrep_walk_data_t *kg)
2430 {
2431 	uintptr_t base = (uintptr_t)seg->s_base;
2432 
2433 	if (addr == kg->kg_kvseg || addr == kg->kg_kvseg32 ||
2434 	    addr == kg->kg_kvseg_core)
2435 		return (WALK_NEXT);
2436 
2437 	if ((uintptr_t)seg->s_ops == kg->kg_segkpm)
2438 		return (WALK_NEXT);
2439 
2440 	return (kg->kg_cb(base, base + seg->s_size, kg->kg_cbdata));
2441 }
2442 
2443 /*ARGSUSED*/
2444 static int
kgrep_walk_vseg(uintptr_t addr,const vmem_seg_t * seg,kgrep_walk_data_t * kg)2445 kgrep_walk_vseg(uintptr_t addr, const vmem_seg_t *seg, kgrep_walk_data_t *kg)
2446 {
2447 	/*
2448 	 * skip large page heap address range - it is scanned by walking
2449 	 * allocated vmem_segs in the heap_lp_arena
2450 	 */
2451 	if (seg->vs_start == kg->kg_heap_lp_base &&
2452 	    seg->vs_end == kg->kg_heap_lp_end)
2453 		return (WALK_NEXT);
2454 
2455 	return (kg->kg_cb(seg->vs_start, seg->vs_end, kg->kg_cbdata));
2456 }
2457 
2458 /*ARGSUSED*/
2459 static int
kgrep_xwalk_vseg(uintptr_t addr,const vmem_seg_t * seg,kgrep_walk_data_t * kg)2460 kgrep_xwalk_vseg(uintptr_t addr, const vmem_seg_t *seg, kgrep_walk_data_t *kg)
2461 {
2462 	return (kg->kg_cb(seg->vs_start, seg->vs_end, kg->kg_cbdata));
2463 }
2464 
2465 static int
kgrep_walk_vmem(uintptr_t addr,const vmem_t * vmem,kgrep_walk_data_t * kg)2466 kgrep_walk_vmem(uintptr_t addr, const vmem_t *vmem, kgrep_walk_data_t *kg)
2467 {
2468 	mdb_walk_cb_t walk_vseg = (mdb_walk_cb_t)kgrep_walk_vseg;
2469 
2470 	if (strcmp(vmem->vm_name, "heap") != 0 &&
2471 	    strcmp(vmem->vm_name, "heap32") != 0 &&
2472 	    strcmp(vmem->vm_name, "heap_core") != 0 &&
2473 	    strcmp(vmem->vm_name, "heap_lp") != 0)
2474 		return (WALK_NEXT);
2475 
2476 	if (strcmp(vmem->vm_name, "heap_lp") == 0)
2477 		walk_vseg = (mdb_walk_cb_t)kgrep_xwalk_vseg;
2478 
2479 	if (mdb_pwalk("vmem_alloc", walk_vseg, kg, addr) == -1) {
2480 		mdb_warn("couldn't walk vmem_alloc for vmem %p", addr);
2481 		return (WALK_ERR);
2482 	}
2483 
2484 	return (WALK_NEXT);
2485 }
2486 
2487 int
kgrep_subr(kgrep_cb_func * cb,void * cbdata)2488 kgrep_subr(kgrep_cb_func *cb, void *cbdata)
2489 {
2490 	GElf_Sym kas, kvseg, kvseg32, kvseg_core, segkpm;
2491 	kgrep_walk_data_t kg;
2492 
2493 	if (mdb_get_state() == MDB_STATE_RUNNING) {
2494 		mdb_warn("kgrep can only be run on a system "
2495 		    "dump or under kmdb; see dumpadm(8)\n");
2496 		return (DCMD_ERR);
2497 	}
2498 
2499 	if (mdb_lookup_by_name("kas", &kas) == -1) {
2500 		mdb_warn("failed to locate 'kas' symbol\n");
2501 		return (DCMD_ERR);
2502 	}
2503 
2504 	if (mdb_lookup_by_name("kvseg", &kvseg) == -1) {
2505 		mdb_warn("failed to locate 'kvseg' symbol\n");
2506 		return (DCMD_ERR);
2507 	}
2508 
2509 	if (mdb_lookup_by_name("kvseg32", &kvseg32) == -1) {
2510 		mdb_warn("failed to locate 'kvseg32' symbol\n");
2511 		return (DCMD_ERR);
2512 	}
2513 
2514 	if (mdb_lookup_by_name("kvseg_core", &kvseg_core) == -1) {
2515 		mdb_warn("failed to locate 'kvseg_core' symbol\n");
2516 		return (DCMD_ERR);
2517 	}
2518 
2519 	if (mdb_lookup_by_name("segkpm_ops", &segkpm) == -1) {
2520 		mdb_warn("failed to locate 'segkpm_ops' symbol\n");
2521 		return (DCMD_ERR);
2522 	}
2523 
2524 	if (mdb_readvar(&kg.kg_heap_lp_base, "heap_lp_base") == -1) {
2525 		mdb_warn("failed to read 'heap_lp_base'\n");
2526 		return (DCMD_ERR);
2527 	}
2528 
2529 	if (mdb_readvar(&kg.kg_heap_lp_end, "heap_lp_end") == -1) {
2530 		mdb_warn("failed to read 'heap_lp_end'\n");
2531 		return (DCMD_ERR);
2532 	}
2533 
2534 	kg.kg_cb = cb;
2535 	kg.kg_cbdata = cbdata;
2536 	kg.kg_kvseg = (uintptr_t)kvseg.st_value;
2537 	kg.kg_kvseg32 = (uintptr_t)kvseg32.st_value;
2538 	kg.kg_kvseg_core = (uintptr_t)kvseg_core.st_value;
2539 	kg.kg_segkpm = (uintptr_t)segkpm.st_value;
2540 
2541 	if (mdb_pwalk("seg", (mdb_walk_cb_t)kgrep_walk_seg,
2542 	    &kg, kas.st_value) == -1) {
2543 		mdb_warn("failed to walk kas segments");
2544 		return (DCMD_ERR);
2545 	}
2546 
2547 	if (mdb_walk("vmem", (mdb_walk_cb_t)kgrep_walk_vmem, &kg) == -1) {
2548 		mdb_warn("failed to walk heap/heap32 vmem arenas");
2549 		return (DCMD_ERR);
2550 	}
2551 
2552 	return (DCMD_OK);
2553 }
2554 
2555 size_t
kgrep_subr_pagesize(void)2556 kgrep_subr_pagesize(void)
2557 {
2558 	return (PAGESIZE);
2559 }
2560 
2561 typedef struct file_walk_data {
2562 	struct uf_entry *fw_flist;
2563 	int fw_flistsz;
2564 	int fw_ndx;
2565 	int fw_nofiles;
2566 } file_walk_data_t;
2567 
2568 typedef struct mdb_file_proc {
2569 	struct {
2570 		struct {
2571 			int			fi_nfiles;
2572 			uf_entry_t *volatile	fi_list;
2573 		} u_finfo;
2574 	} p_user;
2575 } mdb_file_proc_t;
2576 
2577 int
file_walk_init(mdb_walk_state_t * wsp)2578 file_walk_init(mdb_walk_state_t *wsp)
2579 {
2580 	file_walk_data_t *fw;
2581 	mdb_file_proc_t p;
2582 
2583 	if (wsp->walk_addr == 0) {
2584 		mdb_warn("file walk doesn't support global walks\n");
2585 		return (WALK_ERR);
2586 	}
2587 
2588 	fw = mdb_alloc(sizeof (file_walk_data_t), UM_SLEEP);
2589 
2590 	if (mdb_ctf_vread(&p, "proc_t", "mdb_file_proc_t",
2591 	    wsp->walk_addr, 0) == -1) {
2592 		mdb_free(fw, sizeof (file_walk_data_t));
2593 		mdb_warn("failed to read proc structure at %p", wsp->walk_addr);
2594 		return (WALK_ERR);
2595 	}
2596 
2597 	if (p.p_user.u_finfo.fi_nfiles == 0) {
2598 		mdb_free(fw, sizeof (file_walk_data_t));
2599 		return (WALK_DONE);
2600 	}
2601 
2602 	fw->fw_nofiles = p.p_user.u_finfo.fi_nfiles;
2603 	fw->fw_flistsz = sizeof (struct uf_entry) * fw->fw_nofiles;
2604 	fw->fw_flist = mdb_alloc(fw->fw_flistsz, UM_SLEEP);
2605 
2606 	if (mdb_vread(fw->fw_flist, fw->fw_flistsz,
2607 	    (uintptr_t)p.p_user.u_finfo.fi_list) == -1) {
2608 		mdb_warn("failed to read file array at %p",
2609 		    p.p_user.u_finfo.fi_list);
2610 		mdb_free(fw->fw_flist, fw->fw_flistsz);
2611 		mdb_free(fw, sizeof (file_walk_data_t));
2612 		return (WALK_ERR);
2613 	}
2614 
2615 	fw->fw_ndx = 0;
2616 	wsp->walk_data = fw;
2617 
2618 	return (WALK_NEXT);
2619 }
2620 
2621 int
file_walk_step(mdb_walk_state_t * wsp)2622 file_walk_step(mdb_walk_state_t *wsp)
2623 {
2624 	file_walk_data_t *fw = (file_walk_data_t *)wsp->walk_data;
2625 	struct file file;
2626 	uintptr_t fp;
2627 
2628 again:
2629 	if (fw->fw_ndx == fw->fw_nofiles)
2630 		return (WALK_DONE);
2631 
2632 	if ((fp = (uintptr_t)fw->fw_flist[fw->fw_ndx++].uf_file) == 0)
2633 		goto again;
2634 
2635 	(void) mdb_vread(&file, sizeof (file), (uintptr_t)fp);
2636 	return (wsp->walk_callback(fp, &file, wsp->walk_cbdata));
2637 }
2638 
2639 int
allfile_walk_step(mdb_walk_state_t * wsp)2640 allfile_walk_step(mdb_walk_state_t *wsp)
2641 {
2642 	file_walk_data_t *fw = (file_walk_data_t *)wsp->walk_data;
2643 	struct file file;
2644 	uintptr_t fp;
2645 
2646 	if (fw->fw_ndx == fw->fw_nofiles)
2647 		return (WALK_DONE);
2648 
2649 	if ((fp = (uintptr_t)fw->fw_flist[fw->fw_ndx++].uf_file) != 0)
2650 		(void) mdb_vread(&file, sizeof (file), (uintptr_t)fp);
2651 	else
2652 		bzero(&file, sizeof (file));
2653 
2654 	return (wsp->walk_callback(fp, &file, wsp->walk_cbdata));
2655 }
2656 
2657 void
file_walk_fini(mdb_walk_state_t * wsp)2658 file_walk_fini(mdb_walk_state_t *wsp)
2659 {
2660 	file_walk_data_t *fw = (file_walk_data_t *)wsp->walk_data;
2661 
2662 	mdb_free(fw->fw_flist, fw->fw_flistsz);
2663 	mdb_free(fw, sizeof (file_walk_data_t));
2664 }
2665 
2666 int
port_walk_init(mdb_walk_state_t * wsp)2667 port_walk_init(mdb_walk_state_t *wsp)
2668 {
2669 	if (wsp->walk_addr == 0) {
2670 		mdb_warn("port walk doesn't support global walks\n");
2671 		return (WALK_ERR);
2672 	}
2673 
2674 	if (mdb_layered_walk("file", wsp) == -1) {
2675 		mdb_warn("couldn't walk 'file'");
2676 		return (WALK_ERR);
2677 	}
2678 	return (WALK_NEXT);
2679 }
2680 
2681 int
port_walk_step(mdb_walk_state_t * wsp)2682 port_walk_step(mdb_walk_state_t *wsp)
2683 {
2684 	struct vnode	vn;
2685 	uintptr_t	vp;
2686 	uintptr_t	pp;
2687 	struct port	port;
2688 
2689 	vp = (uintptr_t)((struct file *)wsp->walk_layer)->f_vnode;
2690 	if (mdb_vread(&vn, sizeof (vn), vp) == -1) {
2691 		mdb_warn("failed to read vnode_t at %p", vp);
2692 		return (WALK_ERR);
2693 	}
2694 	if (vn.v_type != VPORT)
2695 		return (WALK_NEXT);
2696 
2697 	pp = (uintptr_t)vn.v_data;
2698 	if (mdb_vread(&port, sizeof (port), pp) == -1) {
2699 		mdb_warn("failed to read port_t at %p", pp);
2700 		return (WALK_ERR);
2701 	}
2702 	return (wsp->walk_callback(pp, &port, wsp->walk_cbdata));
2703 }
2704 
2705 typedef struct portev_walk_data {
2706 	list_node_t	*pev_node;
2707 	list_node_t	*pev_last;
2708 	size_t		pev_offset;
2709 } portev_walk_data_t;
2710 
2711 int
portev_walk_init(mdb_walk_state_t * wsp)2712 portev_walk_init(mdb_walk_state_t *wsp)
2713 {
2714 	portev_walk_data_t *pevd;
2715 	struct port	port;
2716 	struct vnode	vn;
2717 	struct list	*list;
2718 	uintptr_t	vp;
2719 
2720 	if (wsp->walk_addr == 0) {
2721 		mdb_warn("portev walk doesn't support global walks\n");
2722 		return (WALK_ERR);
2723 	}
2724 
2725 	pevd = mdb_alloc(sizeof (portev_walk_data_t), UM_SLEEP);
2726 
2727 	if (mdb_vread(&port, sizeof (port), wsp->walk_addr) == -1) {
2728 		mdb_free(pevd, sizeof (portev_walk_data_t));
2729 		mdb_warn("failed to read port structure at %p", wsp->walk_addr);
2730 		return (WALK_ERR);
2731 	}
2732 
2733 	vp = (uintptr_t)port.port_vnode;
2734 	if (mdb_vread(&vn, sizeof (vn), vp) == -1) {
2735 		mdb_free(pevd, sizeof (portev_walk_data_t));
2736 		mdb_warn("failed to read vnode_t at %p", vp);
2737 		return (WALK_ERR);
2738 	}
2739 
2740 	if (vn.v_type != VPORT) {
2741 		mdb_free(pevd, sizeof (portev_walk_data_t));
2742 		mdb_warn("input address (%p) does not point to an event port",
2743 		    wsp->walk_addr);
2744 		return (WALK_ERR);
2745 	}
2746 
2747 	if (port.port_queue.portq_nent == 0) {
2748 		mdb_free(pevd, sizeof (portev_walk_data_t));
2749 		return (WALK_DONE);
2750 	}
2751 	list = &port.port_queue.portq_list;
2752 	pevd->pev_offset = list->list_offset;
2753 	pevd->pev_last = list->list_head.list_prev;
2754 	pevd->pev_node = list->list_head.list_next;
2755 	wsp->walk_data = pevd;
2756 	return (WALK_NEXT);
2757 }
2758 
2759 int
portev_walk_step(mdb_walk_state_t * wsp)2760 portev_walk_step(mdb_walk_state_t *wsp)
2761 {
2762 	portev_walk_data_t	*pevd;
2763 	struct port_kevent	ev;
2764 	uintptr_t		evp;
2765 
2766 	pevd = (portev_walk_data_t *)wsp->walk_data;
2767 
2768 	if (pevd->pev_last == NULL)
2769 		return (WALK_DONE);
2770 	if (pevd->pev_node == pevd->pev_last)
2771 		pevd->pev_last = NULL;		/* last round */
2772 
2773 	evp = ((uintptr_t)(((char *)pevd->pev_node) - pevd->pev_offset));
2774 	if (mdb_vread(&ev, sizeof (ev), evp) == -1) {
2775 		mdb_warn("failed to read port_kevent at %p", evp);
2776 		return (WALK_DONE);
2777 	}
2778 	pevd->pev_node = ev.portkev_node.list_next;
2779 	return (wsp->walk_callback(evp, &ev, wsp->walk_cbdata));
2780 }
2781 
2782 void
portev_walk_fini(mdb_walk_state_t * wsp)2783 portev_walk_fini(mdb_walk_state_t *wsp)
2784 {
2785 	portev_walk_data_t *pevd = (portev_walk_data_t *)wsp->walk_data;
2786 
2787 	if (pevd != NULL)
2788 		mdb_free(pevd, sizeof (portev_walk_data_t));
2789 }
2790 
2791 typedef struct proc_walk_data {
2792 	uintptr_t *pw_stack;
2793 	int pw_depth;
2794 	int pw_max;
2795 } proc_walk_data_t;
2796 
2797 int
proc_walk_init(mdb_walk_state_t * wsp)2798 proc_walk_init(mdb_walk_state_t *wsp)
2799 {
2800 	GElf_Sym sym;
2801 	proc_walk_data_t *pw;
2802 
2803 	if (wsp->walk_addr == 0) {
2804 		if (mdb_lookup_by_name("p0", &sym) == -1) {
2805 			mdb_warn("failed to read 'practive'");
2806 			return (WALK_ERR);
2807 		}
2808 		wsp->walk_addr = (uintptr_t)sym.st_value;
2809 	}
2810 
2811 	pw = mdb_zalloc(sizeof (proc_walk_data_t), UM_SLEEP);
2812 
2813 	if (mdb_readvar(&pw->pw_max, "nproc") == -1) {
2814 		mdb_warn("failed to read 'nproc'");
2815 		mdb_free(pw, sizeof (pw));
2816 		return (WALK_ERR);
2817 	}
2818 
2819 	pw->pw_stack = mdb_alloc(pw->pw_max * sizeof (uintptr_t), UM_SLEEP);
2820 	wsp->walk_data = pw;
2821 
2822 	return (WALK_NEXT);
2823 }
2824 
2825 typedef struct mdb_walk_proc {
2826 	struct proc	*p_child;
2827 	struct proc	*p_sibling;
2828 } mdb_walk_proc_t;
2829 
2830 int
proc_walk_step(mdb_walk_state_t * wsp)2831 proc_walk_step(mdb_walk_state_t *wsp)
2832 {
2833 	proc_walk_data_t *pw = wsp->walk_data;
2834 	uintptr_t addr = wsp->walk_addr;
2835 	uintptr_t cld, sib;
2836 	int status;
2837 	mdb_walk_proc_t pr;
2838 
2839 	if (mdb_ctf_vread(&pr, "proc_t", "mdb_walk_proc_t",
2840 	    addr, 0) == -1) {
2841 		mdb_warn("failed to read proc at %p", addr);
2842 		return (WALK_DONE);
2843 	}
2844 
2845 	cld = (uintptr_t)pr.p_child;
2846 	sib = (uintptr_t)pr.p_sibling;
2847 
2848 	if (pw->pw_depth > 0 && addr == pw->pw_stack[pw->pw_depth - 1]) {
2849 		pw->pw_depth--;
2850 		goto sib;
2851 	}
2852 
2853 	/*
2854 	 * Always pass NULL as the local copy pointer. Consumers
2855 	 * should use mdb_ctf_vread() to read their own minimal
2856 	 * version of proc_t. Thus minimizing the chance of breakage
2857 	 * with older crash dumps.
2858 	 */
2859 	status = wsp->walk_callback(addr, NULL, wsp->walk_cbdata);
2860 
2861 	if (status != WALK_NEXT)
2862 		return (status);
2863 
2864 	if ((wsp->walk_addr = cld) != 0) {
2865 		if (mdb_ctf_vread(&pr, "proc_t", "mdb_walk_proc_t",
2866 		    cld, 0) == -1) {
2867 			mdb_warn("proc %p has invalid p_child %p; skipping\n",
2868 			    addr, cld);
2869 			goto sib;
2870 		}
2871 
2872 		pw->pw_stack[pw->pw_depth++] = addr;
2873 
2874 		if (pw->pw_depth == pw->pw_max) {
2875 			mdb_warn("depth %d exceeds max depth; try again\n",
2876 			    pw->pw_depth);
2877 			return (WALK_DONE);
2878 		}
2879 		return (WALK_NEXT);
2880 	}
2881 
2882 sib:
2883 	/*
2884 	 * We know that p0 has no siblings, and if another starting proc
2885 	 * was given, we don't want to walk its siblings anyway.
2886 	 */
2887 	if (pw->pw_depth == 0)
2888 		return (WALK_DONE);
2889 
2890 	if (sib != 0 && mdb_ctf_vread(&pr, "proc_t", "mdb_walk_proc_t",
2891 	    sib, 0) == -1) {
2892 		mdb_warn("proc %p has invalid p_sibling %p; skipping\n",
2893 		    addr, sib);
2894 		sib = 0;
2895 	}
2896 
2897 	if ((wsp->walk_addr = sib) == 0) {
2898 		if (pw->pw_depth > 0) {
2899 			wsp->walk_addr = pw->pw_stack[pw->pw_depth - 1];
2900 			return (WALK_NEXT);
2901 		}
2902 		return (WALK_DONE);
2903 	}
2904 
2905 	return (WALK_NEXT);
2906 }
2907 
2908 void
proc_walk_fini(mdb_walk_state_t * wsp)2909 proc_walk_fini(mdb_walk_state_t *wsp)
2910 {
2911 	proc_walk_data_t *pw = wsp->walk_data;
2912 
2913 	mdb_free(pw->pw_stack, pw->pw_max * sizeof (uintptr_t));
2914 	mdb_free(pw, sizeof (proc_walk_data_t));
2915 }
2916 
2917 int
task_walk_init(mdb_walk_state_t * wsp)2918 task_walk_init(mdb_walk_state_t *wsp)
2919 {
2920 	task_t task;
2921 
2922 	if (mdb_vread(&task, sizeof (task_t), wsp->walk_addr) == -1) {
2923 		mdb_warn("failed to read task at %p", wsp->walk_addr);
2924 		return (WALK_ERR);
2925 	}
2926 	wsp->walk_addr = (uintptr_t)task.tk_memb_list;
2927 	wsp->walk_data = task.tk_memb_list;
2928 	return (WALK_NEXT);
2929 }
2930 
2931 typedef struct mdb_task_proc {
2932 	struct proc	*p_tasknext;
2933 } mdb_task_proc_t;
2934 
2935 int
task_walk_step(mdb_walk_state_t * wsp)2936 task_walk_step(mdb_walk_state_t *wsp)
2937 {
2938 	mdb_task_proc_t proc;
2939 	int status;
2940 
2941 	if (mdb_ctf_vread(&proc, "proc_t", "mdb_task_proc_t",
2942 	    wsp->walk_addr, 0) == -1) {
2943 		mdb_warn("failed to read proc at %p", wsp->walk_addr);
2944 		return (WALK_DONE);
2945 	}
2946 
2947 	status = wsp->walk_callback(wsp->walk_addr, NULL, wsp->walk_cbdata);
2948 
2949 	if (proc.p_tasknext == wsp->walk_data)
2950 		return (WALK_DONE);
2951 
2952 	wsp->walk_addr = (uintptr_t)proc.p_tasknext;
2953 	return (status);
2954 }
2955 
2956 int
project_walk_init(mdb_walk_state_t * wsp)2957 project_walk_init(mdb_walk_state_t *wsp)
2958 {
2959 	if (wsp->walk_addr == 0) {
2960 		if (mdb_readvar(&wsp->walk_addr, "proj0p") == -1) {
2961 			mdb_warn("failed to read 'proj0p'");
2962 			return (WALK_ERR);
2963 		}
2964 	}
2965 	wsp->walk_data = (void *)wsp->walk_addr;
2966 	return (WALK_NEXT);
2967 }
2968 
2969 int
project_walk_step(mdb_walk_state_t * wsp)2970 project_walk_step(mdb_walk_state_t *wsp)
2971 {
2972 	uintptr_t addr = wsp->walk_addr;
2973 	kproject_t pj;
2974 	int status;
2975 
2976 	if (mdb_vread(&pj, sizeof (kproject_t), addr) == -1) {
2977 		mdb_warn("failed to read project at %p", addr);
2978 		return (WALK_DONE);
2979 	}
2980 	status = wsp->walk_callback(addr, &pj, wsp->walk_cbdata);
2981 	if (status != WALK_NEXT)
2982 		return (status);
2983 	wsp->walk_addr = (uintptr_t)pj.kpj_next;
2984 	if ((void *)wsp->walk_addr == wsp->walk_data)
2985 		return (WALK_DONE);
2986 	return (WALK_NEXT);
2987 }
2988 
2989 static int
generic_walk_step(mdb_walk_state_t * wsp)2990 generic_walk_step(mdb_walk_state_t *wsp)
2991 {
2992 	return (wsp->walk_callback(wsp->walk_addr, wsp->walk_layer,
2993 	    wsp->walk_cbdata));
2994 }
2995 
2996 static int
cpu_walk_cmp(const void * l,const void * r)2997 cpu_walk_cmp(const void *l, const void *r)
2998 {
2999 	uintptr_t lhs = *((uintptr_t *)l);
3000 	uintptr_t rhs = *((uintptr_t *)r);
3001 	cpu_t lcpu, rcpu;
3002 
3003 	(void) mdb_vread(&lcpu, sizeof (lcpu), lhs);
3004 	(void) mdb_vread(&rcpu, sizeof (rcpu), rhs);
3005 
3006 	if (lcpu.cpu_id < rcpu.cpu_id)
3007 		return (-1);
3008 
3009 	if (lcpu.cpu_id > rcpu.cpu_id)
3010 		return (1);
3011 
3012 	return (0);
3013 }
3014 
3015 typedef struct cpu_walk {
3016 	uintptr_t *cw_array;
3017 	int cw_ndx;
3018 } cpu_walk_t;
3019 
3020 int
cpu_walk_init(mdb_walk_state_t * wsp)3021 cpu_walk_init(mdb_walk_state_t *wsp)
3022 {
3023 	cpu_walk_t *cw;
3024 	int max_ncpus, i = 0;
3025 	uintptr_t current, first;
3026 	cpu_t cpu, panic_cpu;
3027 	uintptr_t panicstr, addr = 0;
3028 	GElf_Sym sym;
3029 
3030 	cw = mdb_zalloc(sizeof (cpu_walk_t), UM_SLEEP | UM_GC);
3031 
3032 	if (mdb_readvar(&max_ncpus, "max_ncpus") == -1) {
3033 		mdb_warn("failed to read 'max_ncpus'");
3034 		return (WALK_ERR);
3035 	}
3036 
3037 	if (mdb_readvar(&panicstr, "panicstr") == -1) {
3038 		mdb_warn("failed to read 'panicstr'");
3039 		return (WALK_ERR);
3040 	}
3041 
3042 	if (panicstr != 0) {
3043 		if (mdb_lookup_by_name("panic_cpu", &sym) == -1) {
3044 			mdb_warn("failed to find 'panic_cpu'");
3045 			return (WALK_ERR);
3046 		}
3047 
3048 		addr = (uintptr_t)sym.st_value;
3049 
3050 		if (mdb_vread(&panic_cpu, sizeof (cpu_t), addr) == -1) {
3051 			mdb_warn("failed to read 'panic_cpu'");
3052 			return (WALK_ERR);
3053 		}
3054 	}
3055 
3056 	/*
3057 	 * Unfortunately, there is no platform-independent way to walk
3058 	 * CPUs in ID order.  We therefore loop through in cpu_next order,
3059 	 * building an array of CPU pointers which will subsequently be
3060 	 * sorted.
3061 	 */
3062 	cw->cw_array =
3063 	    mdb_zalloc((max_ncpus + 1) * sizeof (uintptr_t), UM_SLEEP | UM_GC);
3064 
3065 	if (mdb_readvar(&first, "cpu_list") == -1) {
3066 		mdb_warn("failed to read 'cpu_list'");
3067 		return (WALK_ERR);
3068 	}
3069 
3070 	current = first;
3071 	do {
3072 		if (mdb_vread(&cpu, sizeof (cpu), current) == -1) {
3073 			mdb_warn("failed to read cpu at %p", current);
3074 			return (WALK_ERR);
3075 		}
3076 
3077 		if (panicstr != 0 && panic_cpu.cpu_id == cpu.cpu_id) {
3078 			cw->cw_array[i++] = addr;
3079 		} else {
3080 			cw->cw_array[i++] = current;
3081 		}
3082 	} while ((current = (uintptr_t)cpu.cpu_next) != first);
3083 
3084 	qsort(cw->cw_array, i, sizeof (uintptr_t), cpu_walk_cmp);
3085 	wsp->walk_data = cw;
3086 
3087 	return (WALK_NEXT);
3088 }
3089 
3090 int
cpu_walk_step(mdb_walk_state_t * wsp)3091 cpu_walk_step(mdb_walk_state_t *wsp)
3092 {
3093 	cpu_walk_t *cw = wsp->walk_data;
3094 	cpu_t cpu;
3095 	uintptr_t addr = cw->cw_array[cw->cw_ndx++];
3096 
3097 	if (addr == 0)
3098 		return (WALK_DONE);
3099 
3100 	if (mdb_vread(&cpu, sizeof (cpu), addr) == -1) {
3101 		mdb_warn("failed to read cpu at %p", addr);
3102 		return (WALK_DONE);
3103 	}
3104 
3105 	return (wsp->walk_callback(addr, &cpu, wsp->walk_cbdata));
3106 }
3107 
3108 typedef struct cpuinfo_data {
3109 	intptr_t cid_cpu;
3110 	uintptr_t **cid_ithr;
3111 	char	cid_print_head;
3112 	char	cid_print_thr;
3113 	char	cid_print_ithr;
3114 	char	cid_print_flags;
3115 } cpuinfo_data_t;
3116 
3117 int
cpuinfo_walk_ithread(uintptr_t addr,const kthread_t * thr,cpuinfo_data_t * cid)3118 cpuinfo_walk_ithread(uintptr_t addr, const kthread_t *thr, cpuinfo_data_t *cid)
3119 {
3120 	cpu_t c;
3121 	int id;
3122 	uint8_t pil;
3123 
3124 	if (!(thr->t_flag & T_INTR_THREAD) || thr->t_state == TS_FREE)
3125 		return (WALK_NEXT);
3126 
3127 	if (thr->t_bound_cpu == NULL) {
3128 		mdb_warn("thr %p is intr thread w/out a CPU\n", addr);
3129 		return (WALK_NEXT);
3130 	}
3131 
3132 	(void) mdb_vread(&c, sizeof (c), (uintptr_t)thr->t_bound_cpu);
3133 
3134 	if ((id = c.cpu_id) >= NCPU) {
3135 		mdb_warn("CPU %p has id (%d) greater than NCPU (%d)\n",
3136 		    thr->t_bound_cpu, id, NCPU);
3137 		return (WALK_NEXT);
3138 	}
3139 
3140 	if ((pil = thr->t_pil) >= NINTR) {
3141 		mdb_warn("thread %p has pil (%d) greater than %d\n",
3142 		    addr, pil, NINTR);
3143 		return (WALK_NEXT);
3144 	}
3145 
3146 	if (cid->cid_ithr[id][pil] != 0) {
3147 		mdb_warn("CPU %d has multiple threads at pil %d (at least "
3148 		    "%p and %p)\n", id, pil, addr, cid->cid_ithr[id][pil]);
3149 		return (WALK_NEXT);
3150 	}
3151 
3152 	cid->cid_ithr[id][pil] = addr;
3153 
3154 	return (WALK_NEXT);
3155 }
3156 
3157 #define	CPUINFO_IDWIDTH		3
3158 #define	CPUINFO_FLAGWIDTH	9
3159 
3160 #ifdef _LP64
3161 #if defined(__amd64)
3162 #define	CPUINFO_TWIDTH		16
3163 #define	CPUINFO_CPUWIDTH	16
3164 #else
3165 #define	CPUINFO_CPUWIDTH	11
3166 #define	CPUINFO_TWIDTH		11
3167 #endif
3168 #else
3169 #define	CPUINFO_CPUWIDTH	8
3170 #define	CPUINFO_TWIDTH		8
3171 #endif
3172 
3173 #define	CPUINFO_THRDELT		(CPUINFO_IDWIDTH + CPUINFO_CPUWIDTH + 9)
3174 #define	CPUINFO_FLAGDELT	(CPUINFO_IDWIDTH + CPUINFO_CPUWIDTH + 4)
3175 #define	CPUINFO_ITHRDELT	4
3176 
3177 #define	CPUINFO_INDENT	mdb_printf("%*s", CPUINFO_THRDELT, \
3178     flagline < nflaglines ? flagbuf[flagline++] : "")
3179 
3180 typedef struct mdb_cpuinfo_proc {
3181 	struct {
3182 		char		u_comm[MAXCOMLEN + 1];
3183 	} p_user;
3184 } mdb_cpuinfo_proc_t;
3185 
3186 int
cpuinfo_walk_cpu(uintptr_t addr,const cpu_t * cpu,cpuinfo_data_t * cid)3187 cpuinfo_walk_cpu(uintptr_t addr, const cpu_t *cpu, cpuinfo_data_t *cid)
3188 {
3189 	kthread_t t;
3190 	disp_t disp;
3191 	mdb_cpuinfo_proc_t p;
3192 	uintptr_t pinned = 0;
3193 	char **flagbuf;
3194 	int nflaglines = 0,