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