xref: /onnv-gate/usr/src/cmd/mdb/common/modules/genunix/genunix.c (revision 0:68f95e015346)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <mdb/mdb_param.h>
30 #include <mdb/mdb_modapi.h>
31 #include <mdb/mdb_ks.h>
32 #include <mdb/mdb_ctf.h>
33 
34 #include <sys/types.h>
35 #include <sys/thread.h>
36 #include <sys/session.h>
37 #include <sys/user.h>
38 #include <sys/proc.h>
39 #include <sys/var.h>
40 #include <sys/t_lock.h>
41 #include <sys/callo.h>
42 #include <sys/priocntl.h>
43 #include <sys/class.h>
44 #include <sys/regset.h>
45 #include <sys/stack.h>
46 #include <sys/cpuvar.h>
47 #include <sys/vnode.h>
48 #include <sys/vfs.h>
49 #include <sys/flock_impl.h>
50 #include <sys/kmem_impl.h>
51 #include <sys/vmem_impl.h>
52 #include <sys/kstat.h>
53 #include <vm/seg_vn.h>
54 #include <vm/anon.h>
55 #include <vm/as.h>
56 #include <vm/seg_map.h>
57 #include <sys/dditypes.h>
58 #include <sys/ddi_impldefs.h>
59 #include <sys/sysmacros.h>
60 #include <sys/sysconf.h>
61 #include <sys/task.h>
62 #include <sys/project.h>
63 #include <sys/taskq.h>
64 #include <sys/taskq_impl.h>
65 #include <sys/errorq_impl.h>
66 #include <sys/cred_impl.h>
67 #include <sys/zone.h>
68 #include <sys/panic.h>
69 #include <regex.h>
70 #include <sys/port_impl.h>
71 
72 #include "contract.h"
73 #include "cpupart_mdb.h"
74 #include "devinfo.h"
75 #include "leaky.h"
76 #include "lgrp.h"
77 #include "list.h"
78 #include "log.h"
79 #include "kgrep.h"
80 #include "kmem.h"
81 #include "bio.h"
82 #include "streams.h"
83 #include "cyclic.h"
84 #include "findstack.h"
85 #include "ndievents.h"
86 #include "mmd.h"
87 #include "net.h"
88 #include "nvpair.h"
89 #include "ctxop.h"
90 #include "tsd.h"
91 #include "thread.h"
92 #include "memory.h"
93 #include "sobj.h"
94 #include "sysevent.h"
95 #include "rctl.h"
96 #include "typegraph.h"
97 #include "ldi.h"
98 #include "vfs.h"
99 #include "zone.h"
100 #include "modhash.h"
101 
102 /*
103  * Surely this is defined somewhere...
104  */
105 #define	NINTR		16
106 
107 #ifndef STACK_BIAS
108 #define	STACK_BIAS	0
109 #endif
110 
111 static char
112 pstat2ch(uchar_t state)
113 {
114 	switch (state) {
115 		case SSLEEP: return ('S');
116 		case SRUN: return ('R');
117 		case SZOMB: return ('Z');
118 		case SIDL: return ('I');
119 		case SONPROC: return ('O');
120 		case SSTOP: return ('T');
121 		default: return ('?');
122 	}
123 }
124 
125 #define	PS_PRTTHREADS	0x1
126 #define	PS_PRTLWPS	0x2
127 #define	PS_PSARGS	0x4
128 #define	PS_TASKS	0x8
129 #define	PS_PROJECTS	0x10
130 #define	PS_ZONES	0x20
131 
132 static int
133 ps_threadprint(uintptr_t addr, const void *data, void *private)
134 {
135 	const kthread_t *t = (const kthread_t *)data;
136 	uint_t prt_flags = *((uint_t *)private);
137 
138 	static const mdb_bitmask_t t_state_bits[] = {
139 		{ "TS_FREE",	UINT_MAX,	TS_FREE		},
140 		{ "TS_SLEEP",	TS_SLEEP,	TS_SLEEP	},
141 		{ "TS_RUN",	TS_RUN,		TS_RUN		},
142 		{ "TS_ONPROC",	TS_ONPROC,	TS_ONPROC	},
143 		{ "TS_ZOMB",	TS_ZOMB,	TS_ZOMB		},
144 		{ "TS_STOPPED",	TS_STOPPED,	TS_STOPPED	},
145 		{ NULL,		0,		0		}
146 	};
147 
148 	if (prt_flags & PS_PRTTHREADS)
149 		mdb_printf("\tT  %?a <%b>\n", addr, t->t_state, t_state_bits);
150 
151 	if (prt_flags & PS_PRTLWPS)
152 		mdb_printf("\tL  %?a ID: %u\n", t->t_lwp, t->t_tid);
153 
154 	return (WALK_NEXT);
155 }
156 
157 int
158 ps(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
159 {
160 	uint_t prt_flags = 0;
161 	proc_t pr;
162 	struct pid pid, pgid, sid;
163 	sess_t session;
164 	cred_t cred;
165 	task_t tk;
166 	kproject_t pj;
167 	zone_t zn;
168 
169 	if (!(flags & DCMD_ADDRSPEC)) {
170 		if (mdb_walk_dcmd("proc", "ps", argc, argv) == -1) {
171 			mdb_warn("can't walk 'proc'");
172 			return (DCMD_ERR);
173 		}
174 		return (DCMD_OK);
175 	}
176 
177 	if (mdb_getopts(argc, argv,
178 	    'f', MDB_OPT_SETBITS, PS_PSARGS, &prt_flags,
179 	    'l', MDB_OPT_SETBITS, PS_PRTLWPS, &prt_flags,
180 	    'T', MDB_OPT_SETBITS, PS_TASKS, &prt_flags,
181 	    'P', MDB_OPT_SETBITS, PS_PROJECTS, &prt_flags,
182 	    'z', MDB_OPT_SETBITS, PS_ZONES, &prt_flags,
183 	    't', MDB_OPT_SETBITS, PS_PRTTHREADS, &prt_flags, NULL) != argc)
184 		return (DCMD_USAGE);
185 
186 	if (DCMD_HDRSPEC(flags)) {
187 		mdb_printf("%<u>%1s %6s %6s %6s %6s ",
188 		    "S", "PID", "PPID", "PGID", "SID");
189 		if (prt_flags & PS_TASKS)
190 			mdb_printf("%5s ", "TASK");
191 		if (prt_flags & PS_PROJECTS)
192 			mdb_printf("%5s ", "PROJ");
193 		if (prt_flags & PS_ZONES)
194 			mdb_printf("%5s ", "ZONE");
195 		mdb_printf("%6s %10s %?s %s%</u>\n",
196 		    "UID", "FLAGS", "ADDR", "NAME");
197 	}
198 
199 	mdb_vread(&pr, sizeof (pr), addr);
200 	mdb_vread(&pid, sizeof (pid), (uintptr_t)pr.p_pidp);
201 	mdb_vread(&pgid, sizeof (pgid), (uintptr_t)pr.p_pgidp);
202 	mdb_vread(&cred, sizeof (cred), (uintptr_t)pr.p_cred);
203 	mdb_vread(&session, sizeof (session), (uintptr_t)pr.p_sessp);
204 	mdb_vread(&sid, sizeof (sid), (uintptr_t)session.s_sidp);
205 	if (prt_flags & (PS_TASKS | PS_PROJECTS))
206 		mdb_vread(&tk, sizeof (tk), (uintptr_t)pr.p_task);
207 	if (prt_flags & PS_PROJECTS)
208 		mdb_vread(&pj, sizeof (pj), (uintptr_t)tk.tk_proj);
209 	if (prt_flags & PS_ZONES)
210 		mdb_vread(&zn, sizeof (zone_t), (uintptr_t)pr.p_zone);
211 
212 	mdb_printf("%c %6d %6d %6d %6d ",
213 	    pstat2ch(pr.p_stat), pid.pid_id, pr.p_ppid, pgid.pid_id,
214 	    sid.pid_id);
215 	if (prt_flags & PS_TASKS)
216 		mdb_printf("%5d ", tk.tk_tkid);
217 	if (prt_flags & PS_PROJECTS)
218 		mdb_printf("%5d ", pj.kpj_id);
219 	if (prt_flags & PS_ZONES)
220 		mdb_printf("%5d ", zn.zone_id);
221 	mdb_printf("%6d 0x%08x %0?p %s\n",
222 	    cred.cr_uid, pr.p_flag, addr,
223 	    (prt_flags & PS_PSARGS) ? pr.p_user.u_psargs : pr.p_user.u_comm);
224 
225 	if (prt_flags & ~PS_PSARGS)
226 		(void) mdb_pwalk("thread", ps_threadprint, &prt_flags, addr);
227 
228 	return (DCMD_OK);
229 }
230 
231 #define	PG_NEWEST	0x0001
232 #define	PG_OLDEST	0x0002
233 #define	PG_PIPE_OUT	0x0004
234 
235 typedef struct pgrep_data {
236 	uint_t pg_flags;
237 	uint_t pg_psflags;
238 	uintptr_t pg_xaddr;
239 	hrtime_t pg_xstart;
240 	const char *pg_pat;
241 #ifndef _KMDB
242 	regex_t pg_reg;
243 #endif
244 } pgrep_data_t;
245 
246 /*ARGSUSED*/
247 static int
248 pgrep_cb(uintptr_t addr, const void *pdata, void *data)
249 {
250 	const proc_t *prp = pdata;
251 	pgrep_data_t *pgp = data;
252 #ifndef _KMDB
253 	regmatch_t pmatch;
254 #endif
255 
256 	/*
257 	 * kmdb doesn't have access to the reg* functions, so we fall back
258 	 * to strstr.
259 	 */
260 #ifdef _KMDB
261 	if (strstr(prp->p_user.u_comm, pgp->pg_pat) == NULL)
262 		return (WALK_NEXT);
263 #else
264 	if (regexec(&pgp->pg_reg, prp->p_user.u_comm, 1, &pmatch, 0) != 0)
265 		return (WALK_NEXT);
266 #endif
267 
268 	if (pgp->pg_flags & (PG_NEWEST | PG_OLDEST)) {
269 		hrtime_t start;
270 
271 		start = (hrtime_t)prp->p_user.u_start.tv_sec * NANOSEC +
272 		    prp->p_user.u_start.tv_nsec;
273 
274 		if (pgp->pg_flags & PG_NEWEST) {
275 			if (pgp->pg_xaddr == NULL || start > pgp->pg_xstart) {
276 				pgp->pg_xaddr = addr;
277 				pgp->pg_xstart = start;
278 			}
279 		} else {
280 			if (pgp->pg_xaddr == NULL || start < pgp->pg_xstart) {
281 				pgp->pg_xaddr = addr;
282 				pgp->pg_xstart = start;
283 			}
284 		}
285 
286 	} else if (pgp->pg_flags & PG_PIPE_OUT) {
287 		mdb_printf("%p\n", addr);
288 
289 	} else {
290 		if (mdb_call_dcmd("ps", addr, pgp->pg_psflags, 0, NULL) != 0) {
291 			mdb_warn("can't invoke 'ps'");
292 			return (WALK_DONE);
293 		}
294 		pgp->pg_psflags &= ~DCMD_LOOPFIRST;
295 	}
296 
297 	return (WALK_NEXT);
298 }
299 
300 /*ARGSUSED*/
301 int
302 pgrep(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
303 {
304 	pgrep_data_t pg;
305 	int i;
306 #ifndef _KMDB
307 	int err;
308 #endif
309 
310 	if (flags & DCMD_ADDRSPEC)
311 		return (DCMD_USAGE);
312 
313 	pg.pg_flags = 0;
314 	pg.pg_xaddr = 0;
315 
316 	i = mdb_getopts(argc, argv,
317 	    'n', MDB_OPT_SETBITS, PG_NEWEST, &pg.pg_flags,
318 	    'o', MDB_OPT_SETBITS, PG_OLDEST, &pg.pg_flags,
319 	    NULL);
320 
321 	argc -= i;
322 	argv += i;
323 
324 	if (argc != 1)
325 		return (DCMD_USAGE);
326 
327 	/*
328 	 * -n and -o are mutually exclusive.
329 	 */
330 	if ((pg.pg_flags & PG_NEWEST) && (pg.pg_flags & PG_OLDEST))
331 		return (DCMD_USAGE);
332 
333 	if (argv->a_type != MDB_TYPE_STRING)
334 		return (DCMD_USAGE);
335 
336 	if (flags & DCMD_PIPE_OUT)
337 		pg.pg_flags |= PG_PIPE_OUT;
338 
339 	pg.pg_pat = argv->a_un.a_str;
340 	if (DCMD_HDRSPEC(flags))
341 		pg.pg_psflags = DCMD_ADDRSPEC | DCMD_LOOP | DCMD_LOOPFIRST;
342 	else
343 		pg.pg_psflags = DCMD_ADDRSPEC | DCMD_LOOP;
344 
345 #ifndef _KMDB
346 	if ((err = regcomp(&pg.pg_reg, pg.pg_pat, REG_EXTENDED)) != 0) {
347 		size_t nbytes;
348 		char *buf;
349 
350 		nbytes = regerror(err, &pg.pg_reg, NULL, 0);
351 		buf = mdb_alloc(nbytes + 1, UM_SLEEP | UM_GC);
352 		(void) regerror(err, &pg.pg_reg, buf, nbytes);
353 		mdb_warn("%s\n", buf);
354 
355 		return (DCMD_ERR);
356 	}
357 #endif
358 
359 	if (mdb_walk("proc", pgrep_cb, &pg) != 0) {
360 		mdb_warn("can't walk 'proc'");
361 		return (DCMD_ERR);
362 	}
363 
364 	if (pg.pg_xaddr != 0 && (pg.pg_flags & (PG_NEWEST | PG_OLDEST))) {
365 		if (pg.pg_flags & PG_PIPE_OUT) {
366 			mdb_printf("%p\n", pg.pg_xaddr);
367 		} else {
368 			if (mdb_call_dcmd("ps", pg.pg_xaddr, pg.pg_psflags,
369 			    0, NULL) != 0) {
370 				mdb_warn("can't invoke 'ps'");
371 				return (DCMD_ERR);
372 			}
373 		}
374 	}
375 
376 	return (DCMD_OK);
377 }
378 
379 int
380 task(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
381 {
382 	task_t tk;
383 	kproject_t pj;
384 
385 	if (!(flags & DCMD_ADDRSPEC)) {
386 		if (mdb_walk_dcmd("task_cache", "task", argc, argv) == -1) {
387 			mdb_warn("can't walk task_cache");
388 			return (DCMD_ERR);
389 		}
390 		return (DCMD_OK);
391 	}
392 	if (DCMD_HDRSPEC(flags)) {
393 		mdb_printf("%<u>%?s %6s %6s %6s %6s %10s%</u>\n",
394 		    "ADDR", "TASKID", "PROJID", "ZONEID", "REFCNT", "FLAGS");
395 	}
396 	if (mdb_vread(&tk, sizeof (task_t), addr) == -1) {
397 		mdb_warn("can't read task_t structure at %p", addr);
398 		return (DCMD_ERR);
399 	}
400 	if (mdb_vread(&pj, sizeof (kproject_t), (uintptr_t)tk.tk_proj) == -1) {
401 		mdb_warn("can't read project_t structure at %p", addr);
402 		return (DCMD_ERR);
403 	}
404 	mdb_printf("%0?p %6d %6d %6d %6u 0x%08x\n",
405 	    addr, tk.tk_tkid, pj.kpj_id, pj.kpj_zoneid, tk.tk_hold_count,
406 	    tk.tk_flags);
407 	return (DCMD_OK);
408 }
409 
410 int
411 project(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
412 {
413 	kproject_t pj;
414 
415 	if (!(flags & DCMD_ADDRSPEC)) {
416 		if (mdb_walk_dcmd("projects", "project", argc, argv) == -1) {
417 			mdb_warn("can't walk projects");
418 			return (DCMD_ERR);
419 		}
420 		return (DCMD_OK);
421 	}
422 	if (DCMD_HDRSPEC(flags)) {
423 		mdb_printf("%<u>%?s %6s %6s %6s%</u>\n",
424 		    "ADDR", "PROJID", "ZONEID", "REFCNT");
425 	}
426 	if (mdb_vread(&pj, sizeof (kproject_t), addr) == -1) {
427 		mdb_warn("can't read kproject_t structure at %p", addr);
428 		return (DCMD_ERR);
429 	}
430 	mdb_printf("%0?p %6d %6d %6u\n", addr, pj.kpj_id, pj.kpj_zoneid,
431 	    pj.kpj_count);
432 	return (DCMD_OK);
433 }
434 
435 /*ARGSUSED*/
436 int
437 callout(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
438 {
439 	callout_table_t	*co_ktable[CALLOUT_TABLES];
440 	int co_kfanout;
441 	callout_table_t co_table;
442 	callout_t co_callout;
443 	callout_t *co_ptr;
444 	int co_id;
445 	clock_t lbolt;
446 	int i, j, k;
447 	const char *lbolt_sym;
448 
449 	if ((flags & DCMD_ADDRSPEC) || argc != 0)
450 		return (DCMD_USAGE);
451 
452 	if (mdb_prop_postmortem)
453 		lbolt_sym = "panic_lbolt";
454 	else
455 		lbolt_sym = "lbolt";
456 
457 	if (mdb_readvar(&lbolt, lbolt_sym) == -1) {
458 		mdb_warn("failed to read '%s'", lbolt_sym);
459 		return (DCMD_ERR);
460 	}
461 
462 	if (mdb_readvar(&co_kfanout, "callout_fanout") == -1) {
463 		mdb_warn("failed to read callout_fanout");
464 		return (DCMD_ERR);
465 	}
466 
467 	if (mdb_readvar(&co_ktable, "callout_table") == -1) {
468 		mdb_warn("failed to read callout_table");
469 		return (DCMD_ERR);
470 	}
471 
472 	mdb_printf("%<u>%-24s %-?s %-?s %-?s%</u>\n",
473 	    "FUNCTION", "ARGUMENT", "ID", "TIME");
474 
475 	for (i = 0; i < CALLOUT_NTYPES; i++) {
476 		for (j = 0; j < co_kfanout; j++) {
477 
478 			co_id = CALLOUT_TABLE(i, j);
479 
480 			if (mdb_vread(&co_table, sizeof (co_table),
481 			    (uintptr_t)co_ktable[co_id]) == -1) {
482 				mdb_warn("failed to read table at %p",
483 				    (uintptr_t)co_ktable[co_id]);
484 				continue;
485 			}
486 
487 			for (k = 0; k < CALLOUT_BUCKETS; k++) {
488 				co_ptr = co_table.ct_idhash[k];
489 
490 				while (co_ptr != NULL) {
491 					mdb_vread(&co_callout,
492 					    sizeof (co_callout),
493 					    (uintptr_t)co_ptr);
494 
495 					mdb_printf("%-24a %0?p %0?lx %?lx "
496 					    "(T%+ld)\n", co_callout.c_func,
497 					    co_callout.c_arg, co_callout.c_xid,
498 					    co_callout.c_runtime,
499 					    co_callout.c_runtime - lbolt);
500 
501 					co_ptr = co_callout.c_idnext;
502 				}
503 			}
504 		}
505 	}
506 
507 	return (DCMD_OK);
508 }
509 
510 /*ARGSUSED*/
511 int
512 class(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
513 {
514 	long num_classes, i;
515 	sclass_t *class_tbl;
516 	GElf_Sym g_sclass;
517 	char class_name[PC_CLNMSZ];
518 	size_t tbl_size;
519 
520 	if (mdb_lookup_by_name("sclass", &g_sclass) == -1) {
521 		mdb_warn("failed to find symbol sclass\n");
522 		return (DCMD_ERR);
523 	}
524 
525 	tbl_size = (size_t)g_sclass.st_size;
526 	num_classes = tbl_size / (sizeof (sclass_t));
527 	class_tbl = mdb_alloc(tbl_size, UM_SLEEP | UM_GC);
528 
529 	if (mdb_readsym(class_tbl, tbl_size, "sclass") == -1) {
530 		mdb_warn("failed to read sclass");
531 		return (DCMD_ERR);
532 	}
533 
534 	mdb_printf("%<u>%4s %-10s %-24s %-24s%</u>\n", "SLOT", "NAME",
535 	    "INIT FCN", "CLASS FCN");
536 
537 	for (i = 0; i < num_classes; i++) {
538 		if (mdb_vread(class_name, sizeof (class_name),
539 		    (uintptr_t)class_tbl[i].cl_name) == -1)
540 			(void) strcpy(class_name, "???");
541 
542 		mdb_printf("%4ld %-10s %-24a %-24a\n", i, class_name,
543 		    class_tbl[i].cl_init, class_tbl[i].cl_funcs);
544 	}
545 
546 	return (DCMD_OK);
547 }
548 
549 #define	FSNAMELEN	32	/* Max len of FS name we read from vnodeops */
550 
551 int
552 vnode2path(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
553 {
554 	uintptr_t rootdir;
555 	vnode_t vn;
556 	char buf[MAXPATHLEN];
557 
558 	uint_t opt_F = FALSE;
559 
560 	if (mdb_getopts(argc, argv,
561 	    'F', MDB_OPT_SETBITS, TRUE, &opt_F, NULL) != argc)
562 		return (DCMD_USAGE);
563 
564 	if (!(flags & DCMD_ADDRSPEC)) {
565 		mdb_warn("expected explicit vnode_t address before ::\n");
566 		return (DCMD_USAGE);
567 	}
568 
569 	if (mdb_readvar(&rootdir, "rootdir") == -1) {
570 		mdb_warn("failed to read rootdir");
571 		return (DCMD_ERR);
572 	}
573 
574 	if (mdb_vnode2path(addr, buf, sizeof (buf)) == -1)
575 		return (DCMD_ERR);
576 
577 	if (*buf == '\0') {
578 		mdb_printf("??\n");
579 		return (DCMD_OK);
580 	}
581 
582 	mdb_printf("%s", buf);
583 	if (opt_F && buf[strlen(buf)-1] != '/' &&
584 	    mdb_vread(&vn, sizeof (vn), addr) == sizeof (vn))
585 		mdb_printf("%c", mdb_vtype2chr(vn.v_type, 0));
586 	mdb_printf("\n");
587 
588 	return (DCMD_OK);
589 }
590 
591 int
592 ld_walk_init(mdb_walk_state_t *wsp)
593 {
594 	wsp->walk_data = (void *)wsp->walk_addr;
595 	return (WALK_NEXT);
596 }
597 
598 int
599 ld_walk_step(mdb_walk_state_t *wsp)
600 {
601 	int status;
602 	lock_descriptor_t ld;
603 
604 	if (mdb_vread(&ld, sizeof (lock_descriptor_t), wsp->walk_addr) == -1) {
605 		mdb_warn("couldn't read lock_descriptor_t at %p\n",
606 		    wsp->walk_addr);
607 		return (WALK_ERR);
608 	}
609 
610 	status = wsp->walk_callback(wsp->walk_addr, &ld, wsp->walk_cbdata);
611 	if (status == WALK_ERR)
612 		return (WALK_ERR);
613 
614 	wsp->walk_addr = (uintptr_t)ld.l_next;
615 	if (wsp->walk_addr == (uintptr_t)wsp->walk_data)
616 		return (WALK_DONE);
617 
618 	return (status);
619 }
620 
621 int
622 lg_walk_init(mdb_walk_state_t *wsp)
623 {
624 	GElf_Sym sym;
625 
626 	if (mdb_lookup_by_name("lock_graph", &sym) == -1) {
627 		mdb_warn("failed to find symbol 'lock_graph'\n");
628 		return (WALK_ERR);
629 	}
630 
631 	wsp->walk_addr = (uintptr_t)sym.st_value;
632 	wsp->walk_data = (void *)(sym.st_value + sym.st_size);
633 
634 	return (WALK_NEXT);
635 }
636 
637 typedef struct lg_walk_data {
638 	uintptr_t startaddr;
639 	mdb_walk_cb_t callback;
640 	void *data;
641 } lg_walk_data_t;
642 
643 /*
644  * We can't use ::walk lock_descriptor directly, because the head of each graph
645  * is really a dummy lock.  Rather than trying to dynamically determine if this
646  * is a dummy node or not, we just filter out the initial element of the
647  * list.
648  */
649 static int
650 lg_walk_cb(uintptr_t addr, const void *data, void *priv)
651 {
652 	lg_walk_data_t *lw = priv;
653 
654 	if (addr != lw->startaddr)
655 		return (lw->callback(addr, data, lw->data));
656 
657 	return (WALK_NEXT);
658 }
659 
660 int
661 lg_walk_step(mdb_walk_state_t *wsp)
662 {
663 	graph_t *graph;
664 	lg_walk_data_t lw;
665 
666 	if (wsp->walk_addr >= (uintptr_t)wsp->walk_data)
667 		return (WALK_DONE);
668 
669 	if (mdb_vread(&graph, sizeof (graph), wsp->walk_addr) == -1) {
670 		mdb_warn("failed to read graph_t at %p", wsp->walk_addr);
671 		return (WALK_ERR);
672 	}
673 
674 	wsp->walk_addr += sizeof (graph);
675 
676 	if (graph == NULL)
677 		return (WALK_NEXT);
678 
679 	lw.callback = wsp->walk_callback;
680 	lw.data = wsp->walk_cbdata;
681 
682 	lw.startaddr = (uintptr_t)&(graph->active_locks);
683 	if (mdb_pwalk("lock_descriptor", lg_walk_cb, &lw, lw.startaddr)) {
684 		mdb_warn("couldn't walk lock_descriptor at %p\n", lw.startaddr);
685 		return (WALK_ERR);
686 	}
687 
688 	lw.startaddr = (uintptr_t)&(graph->sleeping_locks);
689 	if (mdb_pwalk("lock_descriptor", lg_walk_cb, &lw, lw.startaddr)) {
690 		mdb_warn("couldn't walk lock_descriptor at %p\n", lw.startaddr);
691 		return (WALK_ERR);
692 	}
693 
694 	return (WALK_NEXT);
695 }
696 
697 /*
698  * The space available for the path corresponding to the locked vnode depends
699  * on whether we are printing 32- or 64-bit addresses.
700  */
701 #ifdef _LP64
702 #define	LM_VNPATHLEN	20
703 #else
704 #define	LM_VNPATHLEN	30
705 #endif
706 
707 /*ARGSUSED*/
708 static int
709 lminfo_cb(uintptr_t addr, const void *data, void *priv)
710 {
711 	const lock_descriptor_t *ld = data;
712 	char buf[LM_VNPATHLEN];
713 	proc_t p;
714 
715 	mdb_printf("%-?p %2s %04x %6d %-16s %-?p ",
716 	    addr, ld->l_type == F_RDLCK ? "RD" :
717 	    ld->l_type == F_WRLCK ? "WR" : "??",
718 	    ld->l_state, ld->l_flock.l_pid,
719 	    ld->l_flock.l_pid == 0 ? "<kernel>" :
720 	    mdb_pid2proc(ld->l_flock.l_pid, &p) == NULL ?
721 	    "<defunct>" : p.p_user.u_comm,
722 	    ld->l_vnode);
723 
724 	mdb_vnode2path((uintptr_t)ld->l_vnode, buf,
725 	    sizeof (buf));
726 	mdb_printf("%s\n", buf);
727 
728 	return (WALK_NEXT);
729 }
730 
731 /*ARGSUSED*/
732 int
733 lminfo(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
734 {
735 	if (DCMD_HDRSPEC(flags))
736 		mdb_printf("%<u>%-?s %2s %4s %6s %-16s %-?s %s%</u>\n",
737 		    "ADDR", "TP", "FLAG", "PID", "COMM", "VNODE", "PATH");
738 
739 	return (mdb_pwalk("lock_graph", lminfo_cb, NULL, NULL));
740 }
741 
742 /*ARGSUSED*/
743 int
744 seg(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
745 {
746 	struct seg s;
747 
748 	if (argc != 0)
749 		return (DCMD_USAGE);
750 
751 	if ((flags & DCMD_LOOPFIRST) || !(flags & DCMD_LOOP)) {
752 		mdb_printf("%<u>%?s %?s %?s %?s %s%</u>\n",
753 		    "SEG", "BASE", "SIZE", "DATA", "OPS");
754 	}
755 
756 	if (mdb_vread(&s, sizeof (s), addr) == -1) {
757 		mdb_warn("failed to read seg at %p", addr);
758 		return (DCMD_ERR);
759 	}
760 
761 	mdb_printf("%?p %?p %?lx %?p %a\n",
762 	    addr, s.s_base, s.s_size, s.s_data, s.s_ops);
763 
764 	return (DCMD_OK);
765 }
766 
767 /*ARGSUSED*/
768 static int
769 pmap_walk_anon(uintptr_t addr, const struct anon *anon, int *nres)
770 {
771 	uintptr_t pp =
772 	    mdb_vnode2page((uintptr_t)anon->an_vp, (uintptr_t)anon->an_off);
773 
774 	if (pp != NULL)
775 		(*nres)++;
776 
777 	return (WALK_NEXT);
778 }
779 
780 static int
781 pmap_walk_seg(uintptr_t addr, const struct seg *seg, uintptr_t segvn)
782 {
783 
784 	mdb_printf("%0?p %0?p %7dk", addr, seg->s_base, seg->s_size / 1024);
785 
786 	if (segvn == (uintptr_t)seg->s_ops) {
787 		struct segvn_data svn;
788 		int nres = 0;
789 
790 		(void) mdb_vread(&svn, sizeof (svn), (uintptr_t)seg->s_data);
791 
792 		if (svn.amp == NULL) {
793 			mdb_printf(" %8s", "");
794 			goto drive_on;
795 		}
796 
797 		/*
798 		 * We've got an amp for this segment; walk through
799 		 * the amp, and determine mappings.
800 		 */
801 		if (mdb_pwalk("anon", (mdb_walk_cb_t)pmap_walk_anon,
802 		    &nres, (uintptr_t)svn.amp) == -1)
803 			mdb_warn("failed to walk anon (amp=%p)", svn.amp);
804 
805 		mdb_printf(" %7dk", (nres * PAGESIZE) / 1024);
806 drive_on:
807 
808 		if (svn.vp != NULL) {
809 			char buf[29];
810 
811 			mdb_vnode2path((uintptr_t)svn.vp, buf, sizeof (buf));
812 			mdb_printf(" %s", buf);
813 		} else
814 			mdb_printf(" [ anon ]");
815 	}
816 
817 	mdb_printf("\n");
818 	return (WALK_NEXT);
819 }
820 
821 static int
822 pmap_walk_seg_quick(uintptr_t addr, const struct seg *seg, uintptr_t segvn)
823 {
824 	mdb_printf("%0?p %0?p %7dk", addr, seg->s_base, seg->s_size / 1024);
825 
826 	if (segvn == (uintptr_t)seg->s_ops) {
827 		struct segvn_data svn;
828 
829 		(void) mdb_vread(&svn, sizeof (svn), (uintptr_t)seg->s_data);
830 
831 		if (svn.vp != NULL) {
832 			mdb_printf(" %0?p", svn.vp);
833 		} else {
834 			mdb_printf(" [ anon ]");
835 		}
836 	}
837 
838 	mdb_printf("\n");
839 	return (WALK_NEXT);
840 }
841 
842 /*ARGSUSED*/
843 int
844 pmap(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
845 {
846 	uintptr_t segvn;
847 	proc_t proc;
848 	uint_t quick = FALSE;
849 	mdb_walk_cb_t cb = (mdb_walk_cb_t)pmap_walk_seg;
850 
851 	GElf_Sym sym;
852 
853 	if (!(flags & DCMD_ADDRSPEC))
854 		return (DCMD_USAGE);
855 
856 	if (mdb_getopts(argc, argv,
857 	    'q', MDB_OPT_SETBITS, TRUE, &quick, NULL) != argc)
858 		return (DCMD_USAGE);
859 
860 	if (mdb_vread(&proc, sizeof (proc), addr) == -1) {
861 		mdb_warn("failed to read proc at %p", addr);
862 		return (DCMD_ERR);
863 	}
864 
865 	if (mdb_lookup_by_name("segvn_ops", &sym) == 0)
866 		segvn = (uintptr_t)sym.st_value;
867 	else
868 		segvn = NULL;
869 
870 	mdb_printf("%?s %?s %8s ", "SEG", "BASE", "SIZE");
871 
872 	if (quick) {
873 		mdb_printf("VNODE\n");
874 		cb = (mdb_walk_cb_t)pmap_walk_seg_quick;
875 	} else {
876 		mdb_printf("%8s %s\n", "RES", "PATH");
877 	}
878 
879 	if (mdb_pwalk("seg", cb, (void *)segvn, (uintptr_t)proc.p_as) == -1) {
880 		mdb_warn("failed to walk segments of as %p", proc.p_as);
881 		return (DCMD_ERR);
882 	}
883 
884 	return (DCMD_OK);
885 }
886 
887 typedef struct anon_walk_data {
888 	uintptr_t *aw_levone;
889 	uintptr_t *aw_levtwo;
890 	int aw_nlevone;
891 	int aw_levone_ndx;
892 	int aw_levtwo_ndx;
893 	struct anon_map aw_amp;
894 	struct anon_hdr aw_ahp;
895 } anon_walk_data_t;
896 
897 int
898 anon_walk_init(mdb_walk_state_t *wsp)
899 {
900 	anon_walk_data_t *aw;
901 
902 	if (wsp->walk_addr == NULL) {
903 		mdb_warn("anon walk doesn't support global walks\n");
904 		return (WALK_ERR);
905 	}
906 
907 	aw = mdb_alloc(sizeof (anon_walk_data_t), UM_SLEEP);
908 
909 	if (mdb_vread(&aw->aw_amp, sizeof (aw->aw_amp), wsp->walk_addr) == -1) {
910 		mdb_warn("failed to read anon map at %p", wsp->walk_addr);
911 		mdb_free(aw, sizeof (anon_walk_data_t));
912 		return (WALK_ERR);
913 	}
914 
915 	if (mdb_vread(&aw->aw_ahp, sizeof (aw->aw_ahp),
916 	    (uintptr_t)(aw->aw_amp.ahp)) == -1) {
917 		mdb_warn("failed to read anon hdr ptr at %p", aw->aw_amp.ahp);
918 		mdb_free(aw, sizeof (anon_walk_data_t));
919 		return (WALK_ERR);
920 	}
921 
922 	if (aw->aw_ahp.size <= ANON_CHUNK_SIZE ||
923 	    (aw->aw_ahp.flags & ANON_ALLOC_FORCE)) {
924 		aw->aw_nlevone = aw->aw_ahp.size;
925 		aw->aw_levtwo = NULL;
926 	} else {
927 		aw->aw_nlevone =
928 		    (aw->aw_ahp.size + ANON_CHUNK_OFF) >> ANON_CHUNK_SHIFT;
929 		aw->aw_levtwo =
930 		    mdb_zalloc(ANON_CHUNK_SIZE * sizeof (uintptr_t), UM_SLEEP);
931 	}
932 
933 	aw->aw_levone =
934 	    mdb_alloc(aw->aw_nlevone * sizeof (uintptr_t), UM_SLEEP);
935 
936 	aw->aw_levone_ndx = 0;
937 	aw->aw_levtwo_ndx = 0;
938 
939 	mdb_vread(aw->aw_levone, aw->aw_nlevone * sizeof (uintptr_t),
940 	    (uintptr_t)aw->aw_ahp.array_chunk);
941 
942 	if (aw->aw_levtwo != NULL) {
943 		while (aw->aw_levone[aw->aw_levone_ndx] == NULL) {
944 			aw->aw_levone_ndx++;
945 			if (aw->aw_levone_ndx == aw->aw_nlevone) {
946 				mdb_warn("corrupt anon; couldn't"
947 				    "find ptr to lev two map");
948 				goto out;
949 			}
950 		}
951 
952 		mdb_vread(aw->aw_levtwo, ANON_CHUNK_SIZE * sizeof (uintptr_t),
953 		    aw->aw_levone[aw->aw_levone_ndx]);
954 	}
955 
956 out:
957 	wsp->walk_data = aw;
958 	return (0);
959 }
960 
961 int
962 anon_walk_step(mdb_walk_state_t *wsp)
963 {
964 	int status;
965 	anon_walk_data_t *aw = (anon_walk_data_t *)wsp->walk_data;
966 	struct anon anon;
967 	uintptr_t anonptr;
968 
969 again:
970 	/*
971 	 * Once we've walked through level one, we're done.
972 	 */
973 	if (aw->aw_levone_ndx == aw->aw_nlevone)
974 		return (WALK_DONE);
975 
976 	if (aw->aw_levtwo == NULL) {
977 		anonptr = aw->aw_levone[aw->aw_levone_ndx];
978 		aw->aw_levone_ndx++;
979 	} else {
980 		anonptr = aw->aw_levtwo[aw->aw_levtwo_ndx];
981 		aw->aw_levtwo_ndx++;
982 
983 		if (aw->aw_levtwo_ndx == ANON_CHUNK_SIZE) {
984 			aw->aw_levtwo_ndx = 0;
985 
986 			do {
987 				aw->aw_levone_ndx++;
988 
989 				if (aw->aw_levone_ndx == aw->aw_nlevone)
990 					return (WALK_DONE);
991 			} while (aw->aw_levone[aw->aw_levone_ndx] == NULL);
992 
993 			mdb_vread(aw->aw_levtwo, ANON_CHUNK_SIZE *
994 			    sizeof (uintptr_t),
995 			    aw->aw_levone[aw->aw_levone_ndx]);
996 		}
997 	}
998 
999 	if (anonptr != NULL) {
1000 		mdb_vread(&anon, sizeof (anon), anonptr);
1001 		status = wsp->walk_callback(anonptr, &anon, wsp->walk_cbdata);
1002 	} else
1003 		goto again;
1004 
1005 	return (status);
1006 }
1007 
1008 void
1009 anon_walk_fini(mdb_walk_state_t *wsp)
1010 {
1011 	anon_walk_data_t *aw = (anon_walk_data_t *)wsp->walk_data;
1012 
1013 	if (aw->aw_levtwo != NULL)
1014 		mdb_free(aw->aw_levtwo, ANON_CHUNK_SIZE * sizeof (uintptr_t));
1015 
1016 	mdb_free(aw->aw_levone, aw->aw_nlevone * sizeof (uintptr_t));
1017 	mdb_free(aw, sizeof (anon_walk_data_t));
1018 }
1019 
1020 /*ARGSUSED*/
1021 int
1022 whereopen_fwalk(uintptr_t addr, struct file *f, uintptr_t *target)
1023 {
1024 	if ((uintptr_t)f->f_vnode == *target) {
1025 		mdb_printf("file %p\n", addr);
1026 		*target = NULL;
1027 	}
1028 
1029 	return (WALK_NEXT);
1030 }
1031 
1032 /*ARGSUSED*/
1033 int
1034 whereopen_pwalk(uintptr_t addr, void *ignored, uintptr_t *target)
1035 {
1036 	uintptr_t t = *target;
1037 
1038 	if (mdb_pwalk("file", (mdb_walk_cb_t)whereopen_fwalk, &t, addr) == -1) {
1039 		mdb_warn("couldn't file walk proc %p", addr);
1040 		return (WALK_ERR);
1041 	}
1042 
1043 	if (t == NULL)
1044 		mdb_printf("%p\n", addr);
1045 
1046 	return (WALK_NEXT);
1047 }
1048 
1049 /*ARGSUSED*/
1050 int
1051 whereopen(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1052 {
1053 	uintptr_t target = addr;
1054 
1055 	if (!(flags & DCMD_ADDRSPEC) || addr == NULL)
1056 		return (DCMD_USAGE);
1057 
1058 	if (mdb_walk("proc", (mdb_walk_cb_t)whereopen_pwalk, &target) == -1) {
1059 		mdb_warn("can't proc walk");
1060 		return (DCMD_ERR);
1061 	}
1062 
1063 	return (DCMD_OK);
1064 }
1065 
1066 typedef struct datafmt {
1067 	char	*hdr1;
1068 	char	*hdr2;
1069 	char	*dashes;
1070 	char	*fmt;
1071 } datafmt_t;
1072 
1073 static datafmt_t kmemfmt[] = {
1074 	{ "cache                    ", "name                     ",
1075 	"-------------------------", "%-25s "				},
1076 	{ "   buf",	"  size",	"------",	"%6u "		},
1077 	{ "   buf",	"in use",	"------",	"%6u "		},
1078 	{ "   buf",	" total",	"------",	"%6u "		},
1079 	{ "   memory",	"   in use",	"---------",	"%9u "		},
1080 	{ "    alloc",	"  succeed",	"---------",	"%9u "		},
1081 	{ "alloc",	" fail",	"-----",	"%5u "		},
1082 	{ NULL,		NULL,		NULL,		NULL		}
1083 };
1084 
1085 static datafmt_t vmemfmt[] = {
1086 	{ "vmem                     ", "name                     ",
1087 	"-------------------------", "%-*s "				},
1088 	{ "   memory",	"   in use",	"---------",	"%9llu "	},
1089 	{ "    memory",	"     total",	"----------",	"%10llu "	},
1090 	{ "   memory",	"   import",	"---------",	"%9llu "	},
1091 	{ "    alloc",	"  succeed",	"---------",	"%9llu "	},
1092 	{ "alloc",	" fail",	"-----",	"%5llu "	},
1093 	{ NULL,		NULL,		NULL,		NULL		}
1094 };
1095 
1096 /*ARGSUSED*/
1097 static int
1098 kmastat_cpu_avail(uintptr_t addr, const kmem_cpu_cache_t *ccp, int *avail)
1099 {
1100 	if (ccp->cc_rounds > 0)
1101 		*avail += ccp->cc_rounds;
1102 	if (ccp->cc_prounds > 0)
1103 		*avail += ccp->cc_prounds;
1104 
1105 	return (WALK_NEXT);
1106 }
1107 
1108 /*ARGSUSED*/
1109 static int
1110 kmastat_cpu_alloc(uintptr_t addr, const kmem_cpu_cache_t *ccp, int *alloc)
1111 {
1112 	*alloc += ccp->cc_alloc;
1113 
1114 	return (WALK_NEXT);
1115 }
1116 
1117 /*ARGSUSED*/
1118 static int
1119 kmastat_slab_avail(uintptr_t addr, const kmem_slab_t *sp, int *avail)
1120 {
1121 	*avail += sp->slab_chunks - sp->slab_refcnt;
1122 
1123 	return (WALK_NEXT);
1124 }
1125 
1126 typedef struct kmastat_vmem {
1127 	uintptr_t kv_addr;
1128 	struct kmastat_vmem *kv_next;
1129 	int kv_meminuse;
1130 	int kv_alloc;
1131 	int kv_fail;
1132 } kmastat_vmem_t;
1133 
1134 static int
1135 kmastat_cache(uintptr_t addr, const kmem_cache_t *cp, kmastat_vmem_t **kvp)
1136 {
1137 	kmastat_vmem_t *kv;
1138 	datafmt_t *dfp = kmemfmt;
1139 	int magsize;
1140 
1141 	int avail, alloc, total;
1142 	size_t meminuse = (cp->cache_slab_create - cp->cache_slab_destroy) *
1143 	    cp->cache_slabsize;
1144 
1145 	mdb_walk_cb_t cpu_avail = (mdb_walk_cb_t)kmastat_cpu_avail;
1146 	mdb_walk_cb_t cpu_alloc = (mdb_walk_cb_t)kmastat_cpu_alloc;
1147 	mdb_walk_cb_t slab_avail = (mdb_walk_cb_t)kmastat_slab_avail;
1148 
1149 	magsize = kmem_get_magsize(cp);
1150 
1151 	alloc = cp->cache_slab_alloc + cp->cache_full.ml_alloc;
1152 	avail = cp->cache_full.ml_total * magsize;
1153 	total = cp->cache_buftotal;
1154 
1155 	(void) mdb_pwalk("kmem_cpu_cache", cpu_alloc, &alloc, addr);
1156 	(void) mdb_pwalk("kmem_cpu_cache", cpu_avail, &avail, addr);
1157 	(void) mdb_pwalk("kmem_slab_partial", slab_avail, &avail, addr);
1158 
1159 	for (kv = *kvp; kv != NULL; kv = kv->kv_next) {
1160 		if (kv->kv_addr == (uintptr_t)cp->cache_arena)
1161 			goto out;
1162 	}
1163 
1164 	kv = mdb_zalloc(sizeof (kmastat_vmem_t), UM_SLEEP | UM_GC);
1165 	kv->kv_next = *kvp;
1166 	kv->kv_addr = (uintptr_t)cp->cache_arena;
1167 	*kvp = kv;
1168 out:
1169 	kv->kv_meminuse += meminuse;
1170 	kv->kv_alloc += alloc;
1171 	kv->kv_fail += cp->cache_alloc_fail;
1172 
1173 	mdb_printf((dfp++)->fmt, cp->cache_name);
1174 	mdb_printf((dfp++)->fmt, cp->cache_bufsize);
1175 	mdb_printf((dfp++)->fmt, total - avail);
1176 	mdb_printf((dfp++)->fmt, total);
1177 	mdb_printf((dfp++)->fmt, meminuse);
1178 	mdb_printf((dfp++)->fmt, alloc);
1179 	mdb_printf((dfp++)->fmt, cp->cache_alloc_fail);
1180 	mdb_printf("\n");
1181 
1182 	return (WALK_NEXT);
1183 }
1184 
1185 static int
1186 kmastat_vmem_totals(uintptr_t addr, const vmem_t *v, kmastat_vmem_t *kv)
1187 {
1188 	size_t len;
1189 
1190 	while (kv != NULL && kv->kv_addr != addr)
1191 		kv = kv->kv_next;
1192 
1193 	if (kv == NULL || kv->kv_alloc == 0)
1194 		return (WALK_NEXT);
1195 
1196 	len = MIN(17, strlen(v->vm_name));
1197 
1198 	mdb_printf("Total [%s]%*s %6s %6s %6s %9u %9u %5u\n", v->vm_name,
1199 	    17 - len, "", "", "", "",
1200 	    kv->kv_meminuse, kv->kv_alloc, kv->kv_fail);
1201 
1202 	return (WALK_NEXT);
1203 }
1204 
1205 /*ARGSUSED*/
1206 static int
1207 kmastat_vmem(uintptr_t addr, const vmem_t *v, void *ignored)
1208 {
1209 	datafmt_t *dfp = vmemfmt;
1210 	const vmem_kstat_t *vkp = &v->vm_kstat;
1211 	uintptr_t paddr;
1212 	vmem_t parent;
1213 	int ident = 0;
1214 
1215 	for (paddr = (uintptr_t)v->vm_source; paddr != NULL; ident += 4) {
1216 		if (mdb_vread(&parent, sizeof (parent), paddr) == -1) {
1217 			mdb_warn("couldn't trace %p's ancestry", addr);
1218 			ident = 0;
1219 			break;
1220 		}
1221 		paddr = (uintptr_t)parent.vm_source;
1222 	}
1223 
1224 	mdb_printf("%*s", ident, "");
1225 	mdb_printf((dfp++)->fmt, 25 - ident, v->vm_name);
1226 	mdb_printf((dfp++)->fmt, vkp->vk_mem_inuse.value.ui64);
1227 	mdb_printf((dfp++)->fmt, vkp->vk_mem_total.value.ui64);
1228 	mdb_printf((dfp++)->fmt, vkp->vk_mem_import.value.ui64);
1229 	mdb_printf((dfp++)->fmt, vkp->vk_alloc.value.ui64);
1230 	mdb_printf((dfp++)->fmt, vkp->vk_fail.value.ui64);
1231 
1232 	mdb_printf("\n");
1233 
1234 	return (WALK_NEXT);
1235 }
1236 
1237 /*ARGSUSED*/
1238 int
1239 kmastat(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1240 {
1241 	kmastat_vmem_t *kv = NULL;
1242 	datafmt_t *dfp;
1243 
1244 	if (argc != 0)
1245 		return (DCMD_USAGE);
1246 
1247 	for (dfp = kmemfmt; dfp->hdr1 != NULL; dfp++)
1248 		mdb_printf("%s ", dfp->hdr1);
1249 	mdb_printf("\n");
1250 
1251 	for (dfp = kmemfmt; dfp->hdr1 != NULL; dfp++)
1252 		mdb_printf("%s ", dfp->hdr2);
1253 	mdb_printf("\n");
1254 
1255 	for (dfp = kmemfmt; dfp->hdr1 != NULL; dfp++)
1256 		mdb_printf("%s ", dfp->dashes);
1257 	mdb_printf("\n");
1258 
1259 	if (mdb_walk("kmem_cache", (mdb_walk_cb_t)kmastat_cache, &kv) == -1) {
1260 		mdb_warn("can't walk 'kmem_cache'");
1261 		return (DCMD_ERR);
1262 	}
1263 
1264 	for (dfp = kmemfmt; dfp->hdr1 != NULL; dfp++)
1265 		mdb_printf("%s ", dfp->dashes);
1266 	mdb_printf("\n");
1267 
1268 	if (mdb_walk("vmem", (mdb_walk_cb_t)kmastat_vmem_totals, kv) == -1) {
1269 		mdb_warn("can't walk 'vmem'");
1270 		return (DCMD_ERR);
1271 	}
1272 
1273 	for (dfp = kmemfmt; dfp->hdr1 != NULL; dfp++)
1274 		mdb_printf("%s ", dfp->dashes);
1275 	mdb_printf("\n");
1276 
1277 	mdb_printf("\n");
1278 
1279 	for (dfp = vmemfmt; dfp->hdr1 != NULL; dfp++)
1280 		mdb_printf("%s ", dfp->hdr1);
1281 	mdb_printf("\n");
1282 
1283 	for (dfp = vmemfmt; dfp->hdr1 != NULL; dfp++)
1284 		mdb_printf("%s ", dfp->hdr2);
1285 	mdb_printf("\n");
1286 
1287 	for (dfp = vmemfmt; dfp->hdr1 != NULL; dfp++)
1288 		mdb_printf("%s ", dfp->dashes);
1289 	mdb_printf("\n");
1290 
1291 	if (mdb_walk("vmem", (mdb_walk_cb_t)kmastat_vmem, NULL) == -1) {
1292 		mdb_warn("can't walk 'vmem'");
1293 		return (DCMD_ERR);
1294 	}
1295 
1296 	for (dfp = vmemfmt; dfp->hdr1 != NULL; dfp++)
1297 		mdb_printf("%s ", dfp->dashes);
1298 	mdb_printf("\n");
1299 	return (DCMD_OK);
1300 }
1301 
1302 /*
1303  * Our ::kgrep callback scans the entire kernel VA space (kas).  kas is made
1304  * up of a set of 'struct seg's.  We could just scan each seg en masse, but
1305  * unfortunately, a few of the segs are both large and sparse, so we could
1306  * spend quite a bit of time scanning VAs which have no backing pages.
1307  *
1308  * So for the few very sparse segs, we skip the segment itself, and scan
1309  * the allocated vmem_segs in the vmem arena which manages that part of kas.
1310  * Currently, we do this for:
1311  *
1312  *	SEG		VMEM ARENA
1313  *	kvseg		heap_arena
1314  *	kvseg32		heap32_arena
1315  *	kvseg_core	heap_core_arena
1316  *
1317  * In addition, we skip the segkpm segment in its entirety, since it is very
1318  * sparse, and contains no new kernel data.
1319  */
1320 typedef struct kgrep_walk_data {
1321 	kgrep_cb_func *kg_cb;
1322 	void *kg_cbdata;
1323 	uintptr_t kg_kvseg;
1324 	uintptr_t kg_kvseg32;
1325 	uintptr_t kg_kvseg_core;
1326 	uintptr_t kg_segkpm;
1327 } kgrep_walk_data_t;
1328 
1329 static int
1330 kgrep_walk_seg(uintptr_t addr, const struct seg *seg, kgrep_walk_data_t *kg)
1331 {
1332 	uintptr_t base = (uintptr_t)seg->s_base;
1333 
1334 	if (addr == kg->kg_kvseg || addr == kg->kg_kvseg32 ||
1335 	    addr == kg->kg_kvseg_core)
1336 		return (WALK_NEXT);
1337 
1338 	if ((uintptr_t)seg->s_ops == kg->kg_segkpm)
1339 		return (WALK_NEXT);
1340 
1341 	return (kg->kg_cb(base, base + seg->s_size, kg->kg_cbdata));
1342 }
1343 
1344 /*ARGSUSED*/
1345 static int
1346 kgrep_walk_vseg(uintptr_t addr, const vmem_seg_t *seg, kgrep_walk_data_t *kg)
1347 {
1348 	return (kg->kg_cb(seg->vs_start, seg->vs_end, kg->kg_cbdata));
1349 }
1350 
1351 static int
1352 kgrep_walk_vmem(uintptr_t addr, const vmem_t *vmem, kgrep_walk_data_t *kg)
1353 {
1354 	if (strcmp(vmem->vm_name, "heap") != 0 &&
1355 	    strcmp(vmem->vm_name, "heap32") != 0 &&
1356 	    strcmp(vmem->vm_name, "heap_core") != 0)
1357 		return (WALK_NEXT);
1358 
1359 	if (mdb_pwalk("vmem_alloc",
1360 	    (mdb_walk_cb_t)kgrep_walk_vseg, kg, addr) == -1) {
1361 		mdb_warn("couldn't walk vmem_alloc for vmem %p", addr);
1362 		return (WALK_ERR);
1363 	}
1364 
1365 	return (WALK_NEXT);
1366 }
1367 
1368 int
1369 kgrep_subr(kgrep_cb_func *cb, void *cbdata)
1370 {
1371 	GElf_Sym kas, kvseg, kvseg32, kvseg_core, segkpm;
1372 	kgrep_walk_data_t kg;
1373 
1374 	if (mdb_get_state() == MDB_STATE_RUNNING) {
1375 		mdb_warn("kgrep can only be run on a system "
1376 		    "dump or under kmdb; see dumpadm(1M)\n");
1377 		return (DCMD_ERR);
1378 	}
1379 
1380 	if (mdb_lookup_by_name("kas", &kas) == -1) {
1381 		mdb_warn("failed to locate 'kas' symbol\n");
1382 		return (DCMD_ERR);
1383 	}
1384 
1385 	if (mdb_lookup_by_name("kvseg", &kvseg) == -1) {
1386 		mdb_warn("failed to locate 'kvseg' symbol\n");
1387 		return (DCMD_ERR);
1388 	}
1389 
1390 	if (mdb_lookup_by_name("kvseg32", &kvseg32) == -1) {
1391 		mdb_warn("failed to locate 'kvseg32' symbol\n");
1392 		return (DCMD_ERR);
1393 	}
1394 
1395 	if (mdb_lookup_by_name("kvseg_core", &kvseg_core) == -1) {
1396 		mdb_warn("failed to locate 'kvseg_core' symbol\n");
1397 		return (DCMD_ERR);
1398 	}
1399 
1400 	if (mdb_lookup_by_name("segkpm_ops", &segkpm) == -1) {
1401 		mdb_warn("failed to locate 'segkpm_ops' symbol\n");
1402 		return (DCMD_ERR);
1403 	}
1404 
1405 	kg.kg_cb = cb;
1406 	kg.kg_cbdata = cbdata;
1407 	kg.kg_kvseg = (uintptr_t)kvseg.st_value;
1408 	kg.kg_kvseg32 = (uintptr_t)kvseg32.st_value;
1409 	kg.kg_kvseg_core = (uintptr_t)kvseg_core.st_value;
1410 	kg.kg_segkpm = (uintptr_t)segkpm.st_value;
1411 
1412 	if (mdb_pwalk("seg", (mdb_walk_cb_t)kgrep_walk_seg,
1413 	    &kg, kas.st_value) == -1) {
1414 		mdb_warn("failed to walk kas segments");
1415 		return (DCMD_ERR);
1416 	}
1417 
1418 	if (mdb_walk("vmem", (mdb_walk_cb_t)kgrep_walk_vmem, &kg) == -1) {
1419 		mdb_warn("failed to walk heap/heap32 vmem arenas");
1420 		return (DCMD_ERR);
1421 	}
1422 
1423 	return (DCMD_OK);
1424 }
1425 
1426 size_t
1427 kgrep_subr_pagesize(void)
1428 {
1429 	return (PAGESIZE);
1430 }
1431 
1432 typedef struct file_walk_data {
1433 	struct uf_entry *fw_flist;
1434 	int fw_flistsz;
1435 	int fw_ndx;
1436 	int fw_nofiles;
1437 } file_walk_data_t;
1438 
1439 int
1440 file_walk_init(mdb_walk_state_t *wsp)
1441 {
1442 	file_walk_data_t *fw;
1443 	proc_t p;
1444 
1445 	if (wsp->walk_addr == NULL) {
1446 		mdb_warn("file walk doesn't support global walks\n");
1447 		return (WALK_ERR);
1448 	}
1449 
1450 	fw = mdb_alloc(sizeof (file_walk_data_t), UM_SLEEP);
1451 
1452 	if (mdb_vread(&p, sizeof (p), wsp->walk_addr) == -1) {
1453 		mdb_free(fw, sizeof (file_walk_data_t));
1454 		mdb_warn("failed to read proc structure at %p", wsp->walk_addr);
1455 		return (WALK_ERR);
1456 	}
1457 
1458 	if (p.p_user.u_finfo.fi_nfiles == 0) {
1459 		mdb_free(fw, sizeof (file_walk_data_t));
1460 		return (WALK_DONE);
1461 	}
1462 
1463 	fw->fw_nofiles = p.p_user.u_finfo.fi_nfiles;
1464 	fw->fw_flistsz = sizeof (struct uf_entry) * fw->fw_nofiles;
1465 	fw->fw_flist = mdb_alloc(fw->fw_flistsz, UM_SLEEP);
1466 
1467 	if (mdb_vread(fw->fw_flist, fw->fw_flistsz,
1468 	    (uintptr_t)p.p_user.u_finfo.fi_list) == -1) {
1469 		mdb_warn("failed to read file array at %p",
1470 		    p.p_user.u_finfo.fi_list);
1471 		mdb_free(fw->fw_flist, fw->fw_flistsz);
1472 		mdb_free(fw, sizeof (file_walk_data_t));
1473 		return (WALK_ERR);
1474 	}
1475 
1476 	fw->fw_ndx = 0;
1477 	wsp->walk_data = fw;
1478 
1479 	return (WALK_NEXT);
1480 }
1481 
1482 int
1483 file_walk_step(mdb_walk_state_t *wsp)
1484 {
1485 	file_walk_data_t *fw = (file_walk_data_t *)wsp->walk_data;
1486 	struct file file;
1487 	uintptr_t fp;
1488 
1489 again:
1490 	if (fw->fw_ndx == fw->fw_nofiles)
1491 		return (WALK_DONE);
1492 
1493 	if ((fp = (uintptr_t)fw->fw_flist[fw->fw_ndx++].uf_file) == NULL)
1494 		goto again;
1495 
1496 	(void) mdb_vread(&file, sizeof (file), (uintptr_t)fp);
1497 	return (wsp->walk_callback(fp, &file, wsp->walk_cbdata));
1498 }
1499 
1500 int
1501 allfile_walk_step(mdb_walk_state_t *wsp)
1502 {
1503 	file_walk_data_t *fw = (file_walk_data_t *)wsp->walk_data;
1504 	struct file file;
1505 	uintptr_t fp;
1506 
1507 	if (fw->fw_ndx == fw->fw_nofiles)
1508 		return (WALK_DONE);
1509 
1510 	if ((fp = (uintptr_t)fw->fw_flist[fw->fw_ndx++].uf_file) != NULL)
1511 		(void) mdb_vread(&file, sizeof (file), (uintptr_t)fp);
1512 	else
1513 		bzero(&file, sizeof (file));
1514 
1515 	return (wsp->walk_callback(fp, &file, wsp->walk_cbdata));
1516 }
1517 
1518 void
1519 file_walk_fini(mdb_walk_state_t *wsp)
1520 {
1521 	file_walk_data_t *fw = (file_walk_data_t *)wsp->walk_data;
1522 
1523 	mdb_free(fw->fw_flist, fw->fw_flistsz);
1524 	mdb_free(fw, sizeof (file_walk_data_t));
1525 }
1526 
1527 int
1528 port_walk_init(mdb_walk_state_t *wsp)
1529 {
1530 	if (wsp->walk_addr == NULL) {
1531 		mdb_warn("port walk doesn't support global walks\n");
1532 		return (WALK_ERR);
1533 	}
1534 
1535 	if (mdb_layered_walk("file", wsp) == -1) {
1536 		mdb_warn("couldn't walk 'file'");
1537 		return (WALK_ERR);
1538 	}
1539 	return (WALK_NEXT);
1540 }
1541 
1542 int
1543 port_walk_step(mdb_walk_state_t *wsp)
1544 {
1545 	struct vnode	vn;
1546 	uintptr_t	vp;
1547 	uintptr_t	pp;
1548 	struct port	port;
1549 
1550 	vp = (uintptr_t)((struct file *)wsp->walk_layer)->f_vnode;
1551 	if (mdb_vread(&vn, sizeof (vn), vp) == -1) {
1552 		mdb_warn("failed to read vnode_t at %p", vp);
1553 		return (WALK_ERR);
1554 	}
1555 	if (vn.v_type != VPORT)
1556 		return (WALK_NEXT);
1557 
1558 	pp = (uintptr_t)vn.v_data;
1559 	if (mdb_vread(&port, sizeof (port), pp) == -1) {
1560 		mdb_warn("failed to read port_t at %p", pp);
1561 		return (WALK_ERR);
1562 	}
1563 	return (wsp->walk_callback(pp, &port, wsp->walk_cbdata));
1564 }
1565 
1566 typedef struct portev_walk_data {
1567 	list_node_t	*pev_node;
1568 	list_node_t	*pev_last;
1569 	size_t		pev_offset;
1570 } portev_walk_data_t;
1571 
1572 int
1573 portev_walk_init(mdb_walk_state_t *wsp)
1574 {
1575 	portev_walk_data_t *pevd;
1576 	struct port	port;
1577 	struct vnode	vn;
1578 	struct list	*list;
1579 	uintptr_t	vp;
1580 
1581 	if (wsp->walk_addr == NULL) {
1582 		mdb_warn("portev walk doesn't support global walks\n");
1583 		return (WALK_ERR);
1584 	}
1585 
1586 	pevd = mdb_alloc(sizeof (portev_walk_data_t), UM_SLEEP);
1587 
1588 	if (mdb_vread(&port, sizeof (port), wsp->walk_addr) == -1) {
1589 		mdb_free(pevd, sizeof (portev_walk_data_t));
1590 		mdb_warn("failed to read port structure at %p", wsp->walk_addr);
1591 		return (WALK_ERR);
1592 	}
1593 
1594 	vp = (uintptr_t)port.port_vnode;
1595 	if (mdb_vread(&vn, sizeof (vn), vp) == -1) {
1596 		mdb_free(pevd, sizeof (portev_walk_data_t));
1597 		mdb_warn("failed to read vnode_t at %p", vp);
1598 		return (WALK_ERR);
1599 	}
1600 
1601 	if (vn.v_type != VPORT) {
1602 		mdb_free(pevd, sizeof (portev_walk_data_t));
1603 		mdb_warn("input address (%p) does not point to an event port",
1604 		    wsp->walk_addr);
1605 		return (WALK_ERR);
1606 	}
1607 
1608 	if (port.port_queue.portq_nent == 0) {
1609 		mdb_free(pevd, sizeof (portev_walk_data_t));
1610 		return (WALK_DONE);
1611 	}
1612 	list = &port.port_queue.portq_list;
1613 	pevd->pev_offset = list->list_offset;
1614 	pevd->pev_last = list->list_head.list_prev;
1615 	pevd->pev_node = list->list_head.list_next;
1616 	wsp->walk_data = pevd;
1617 	return (WALK_NEXT);
1618 }
1619 
1620 int
1621 portev_walk_step(mdb_walk_state_t *wsp)
1622 {
1623 	portev_walk_data_t	*pevd;
1624 	struct port_kevent	ev;
1625 	uintptr_t		evp;
1626 
1627 	pevd = (portev_walk_data_t *)wsp->walk_data;
1628 
1629 	if (pevd->pev_last == NULL)
1630 		return (WALK_DONE);
1631 	if (pevd->pev_node == pevd->pev_last)
1632 		pevd->pev_last = NULL;		/* last round */
1633 
1634 	evp = ((uintptr_t)(((char *)pevd->pev_node) - pevd->pev_offset));
1635 	if (mdb_vread(&ev, sizeof (ev), evp) == -1) {
1636 		mdb_warn("failed to read port_kevent at %p", evp);
1637 		return (WALK_DONE);
1638 	}
1639 	pevd->pev_node = ev.portkev_node.list_next;
1640 	return (wsp->walk_callback(evp, &ev, wsp->walk_cbdata));
1641 }
1642 
1643 void
1644 portev_walk_fini(mdb_walk_state_t *wsp)
1645 {
1646 	portev_walk_data_t *pevd = (portev_walk_data_t *)wsp->walk_data;
1647 
1648 	if (pevd != NULL)
1649 		mdb_free(pevd, sizeof (portev_walk_data_t));
1650 }
1651 
1652 typedef struct proc_walk_data {
1653 	uintptr_t *pw_stack;
1654 	int pw_depth;
1655 	int pw_max;
1656 } proc_walk_data_t;
1657 
1658 int
1659 proc_walk_init(mdb_walk_state_t *wsp)
1660 {
1661 	GElf_Sym sym;
1662 	proc_walk_data_t *pw;
1663 
1664 	if (wsp->walk_addr == NULL) {
1665 		if (mdb_lookup_by_name("p0", &sym) == -1) {
1666 			mdb_warn("failed to read 'practive'");
1667 			return (WALK_ERR);
1668 		}
1669 		wsp->walk_addr = (uintptr_t)sym.st_value;
1670 	}
1671 
1672 	pw = mdb_zalloc(sizeof (proc_walk_data_t), UM_SLEEP);
1673 
1674 	if (mdb_readvar(&pw->pw_max, "nproc") == -1) {
1675 		mdb_warn("failed to read 'nproc'");
1676 		mdb_free(pw, sizeof (pw));
1677 		return (WALK_ERR);
1678 	}
1679 
1680 	pw->pw_stack = mdb_alloc(pw->pw_max * sizeof (uintptr_t), UM_SLEEP);
1681 	wsp->walk_data = pw;
1682 
1683 	return (WALK_NEXT);
1684 }
1685 
1686 int
1687 proc_walk_step(mdb_walk_state_t *wsp)
1688 {
1689 	proc_walk_data_t *pw = wsp->walk_data;
1690 	uintptr_t addr = wsp->walk_addr;
1691 	uintptr_t cld, sib;
1692 
1693 	int status;
1694 	proc_t pr;
1695 
1696 	if (mdb_vread(&pr, sizeof (proc_t), addr) == -1) {
1697 		mdb_warn("failed to read proc at %p", addr);
1698 		return (WALK_DONE);
1699 	}
1700 
1701 	cld = (uintptr_t)pr.p_child;
1702 	sib = (uintptr_t)pr.p_sibling;
1703 
1704 	if (pw->pw_depth > 0 && addr == pw->pw_stack[pw->pw_depth - 1]) {
1705 		pw->pw_depth--;
1706 		goto sib;
1707 	}
1708 
1709 	status = wsp->walk_callback(addr, &pr, wsp->walk_cbdata);
1710 
1711 	if (status != WALK_NEXT)
1712 		return (status);
1713 
1714 	if ((wsp->walk_addr = cld) != NULL) {
1715 		if (mdb_vread(&pr, sizeof (proc_t), cld) == -1) {
1716 			mdb_warn("proc %p has invalid p_child %p; skipping\n",
1717 			    addr, cld);
1718 			goto sib;
1719 		}
1720 
1721 		pw->pw_stack[pw->pw_depth++] = addr;
1722 
1723 		if (pw->pw_depth == pw->pw_max) {
1724 			mdb_warn("depth %d exceeds max depth; try again\n",
1725 			    pw->pw_depth);
1726 			return (WALK_DONE);
1727 		}
1728 		return (WALK_NEXT);
1729 	}
1730 
1731 sib:
1732 	/*
1733 	 * We know that p0 has no siblings, and if another starting proc
1734 	 * was given, we don't want to walk its siblings anyway.
1735 	 */
1736 	if (pw->pw_depth == 0)
1737 		return (WALK_DONE);
1738 
1739 	if (sib != NULL && mdb_vread(&pr, sizeof (proc_t), sib) == -1) {
1740 		mdb_warn("proc %p has invalid p_sibling %p; skipping\n",
1741 		    addr, sib);
1742 		sib = NULL;
1743 	}
1744 
1745 	if ((wsp->walk_addr = sib) == NULL) {
1746 		if (pw->pw_depth > 0) {
1747 			wsp->walk_addr = pw->pw_stack[pw->pw_depth - 1];
1748 			return (WALK_NEXT);
1749 		}
1750 		return (WALK_DONE);
1751 	}
1752 
1753 	return (WALK_NEXT);
1754 }
1755 
1756 void
1757 proc_walk_fini(mdb_walk_state_t *wsp)
1758 {
1759 	proc_walk_data_t *pw = wsp->walk_data;
1760 
1761 	mdb_free(pw->pw_stack, pw->pw_max * sizeof (uintptr_t));
1762 	mdb_free(pw, sizeof (proc_walk_data_t));
1763 }
1764 
1765 int
1766 task_walk_init(mdb_walk_state_t *wsp)
1767 {
1768 	task_t task;
1769 
1770 	if (mdb_vread(&task, sizeof (task_t), wsp->walk_addr) == -1) {
1771 		mdb_warn("failed to read task at %p", wsp->walk_addr);
1772 		return (WALK_ERR);
1773 	}
1774 	wsp->walk_addr = (uintptr_t)task.tk_memb_list;
1775 	wsp->walk_data = task.tk_memb_list;
1776 	return (WALK_NEXT);
1777 }
1778 
1779 int
1780 task_walk_step(mdb_walk_state_t *wsp)
1781 {
1782 	proc_t proc;
1783 	int status;
1784 
1785 	if (mdb_vread(&proc, sizeof (proc_t), wsp->walk_addr) == -1) {
1786 		mdb_warn("failed to read proc at %p", wsp->walk_addr);
1787 		return (WALK_DONE);
1788 	}
1789 
1790 	status = wsp->walk_callback(wsp->walk_addr, NULL, wsp->walk_cbdata);
1791 
1792 	if (proc.p_tasknext == wsp->walk_data)
1793 		return (WALK_DONE);
1794 
1795 	wsp->walk_addr = (uintptr_t)proc.p_tasknext;
1796 	return (status);
1797 }
1798 
1799 int
1800 project_walk_init(mdb_walk_state_t *wsp)
1801 {
1802 	if (wsp->walk_addr == NULL) {
1803 		if (mdb_readvar(&wsp->walk_addr, "proj0p") == -1) {
1804 			mdb_warn("failed to read 'proj0p'");
1805 			return (WALK_ERR);
1806 		}
1807 	}
1808 	wsp->walk_data = (void *)wsp->walk_addr;
1809 	return (WALK_NEXT);
1810 }
1811 
1812 int
1813 project_walk_step(mdb_walk_state_t *wsp)
1814 {
1815 	uintptr_t addr = wsp->walk_addr;
1816 	kproject_t pj;
1817 	int status;
1818 
1819 	if (mdb_vread(&pj, sizeof (kproject_t), addr) == -1) {
1820 		mdb_warn("failed to read project at %p", addr);
1821 		return (WALK_DONE);
1822 	}
1823 	status = wsp->walk_callback(addr, &pj, wsp->walk_cbdata);
1824 	if (status != WALK_NEXT)
1825 		return (status);
1826 	wsp->walk_addr = (uintptr_t)pj.kpj_next;
1827 	if ((void *)wsp->walk_addr == wsp->walk_data)
1828 		return (WALK_DONE);
1829 	return (WALK_NEXT);
1830 }
1831 
1832 static int
1833 generic_walk_step(mdb_walk_state_t *wsp)
1834 {
1835 	return (wsp->walk_callback(wsp->walk_addr, wsp->walk_layer,
1836 	    wsp->walk_cbdata));
1837 }
1838 
1839 struct aw_info {
1840 	void *aw_buff;		/* buffer to hold the tree's data structure */
1841 	avl_tree_t aw_tree;	/* copy of avl_tree_t being walked */
1842 };
1843 
1844 /*
1845  * common code used to find the addr of the the leftmost child below
1846  * an AVL node
1847  */
1848 static uintptr_t
1849 avl_leftmostchild(uintptr_t addr, void * buff, size_t offset, size_t size)
1850 {
1851 	avl_node_t *node = (avl_node_t *)((uintptr_t)buff + offset);
1852 
1853 	for (;;) {
1854 		addr -= offset;
1855 		if (mdb_vread(buff, size, addr) == -1) {
1856 			mdb_warn("read of avl_node_t failed: %p", addr);
1857 			return ((uintptr_t)-1L);
1858 		}
1859 		if (node->avl_child[0] == NULL)
1860 			break;
1861 		addr = (uintptr_t)node->avl_child[0];
1862 	}
1863 	return (addr);
1864 }
1865 
1866 /*
1867  * initialize a forward walk thru an avl tree.
1868  */
1869 int
1870 avl_walk_init(mdb_walk_state_t *wsp)
1871 {
1872 	struct aw_info *aw;
1873 	avl_tree_t *tree;
1874 	uintptr_t addr;
1875 
1876 	/*
1877 	 * allocate the AVL walk data
1878 	 */
1879 	wsp->walk_data = aw = mdb_zalloc(sizeof (struct aw_info), UM_SLEEP);
1880 
1881 	/*
1882 	 * get an mdb copy of the avl_tree_t being walked
1883 	 */
1884 	tree = &aw->aw_tree;
1885 	if (mdb_vread(tree, sizeof (avl_tree_t), wsp->walk_addr) == -1) {
1886 		mdb_warn("read of avl_tree_t failed: %p", wsp->walk_addr);
1887 		goto error;
1888 	}
1889 	if (tree->avl_size < tree->avl_offset + sizeof (avl_node_t)) {
1890 		mdb_warn("invalid avl_tree_t at %p, avl_size:%d, avl_offset:%d",
1891 		    wsp->walk_addr, tree->avl_size, tree->avl_offset);
1892 		goto error;
1893 	}
1894 
1895 	/*
1896 	 * allocate a buffer to hold the mdb copy of tree's structs
1897 	 * "node" always points at the avl_node_t field inside the struct
1898 	 */
1899 	aw->aw_buff = mdb_zalloc(tree->avl_size, UM_SLEEP);
1900 
1901 	/*
1902 	 * get the first avl_node_t address, use same algorithm
1903 	 * as avl_start() -- leftmost child in tree from root
1904 	 */
1905 	addr = (uintptr_t)tree->avl_root;
1906 	if (addr == NULL) {
1907 		wsp->walk_addr = NULL;
1908 		return (WALK_NEXT);
1909 	}
1910 	addr = avl_leftmostchild(addr, aw->aw_buff, tree->avl_offset,
1911 	    tree->avl_size);
1912 	if (addr == (uintptr_t)-1L)
1913 		goto error;
1914 
1915 	wsp->walk_addr = addr;
1916 	return (WALK_NEXT);
1917 
1918 error:
1919 	if (aw->aw_buff != NULL)
1920 		mdb_free(aw->aw_buff, sizeof (tree->avl_size));
1921 	mdb_free(aw, sizeof (struct aw_info));
1922 	return (WALK_ERR);
1923 }
1924 
1925 /*
1926  * At each step, visit (callback) the current node, then move to the next
1927  * in the AVL tree.  Uses the same algorithm as avl_walk().
1928  */
1929 int
1930 avl_walk_step(mdb_walk_state_t *wsp)
1931 {
1932 	struct aw_info *aw;
1933 	size_t offset;
1934 	size_t size;
1935 	uintptr_t addr;
1936 	avl_node_t *node;
1937 	int status;
1938 	int was_child;
1939 
1940 	/*
1941 	 * don't walk past the end of the tree!
1942 	 */
1943 	addr = wsp->walk_addr;
1944 	if (addr == NULL)
1945 		return (WALK_DONE);
1946 
1947 	aw = (struct aw_info *)wsp->walk_data;
1948 	size = aw->aw_tree.avl_size;
1949 	offset = aw->aw_tree.avl_offset;
1950 	node = (avl_node_t *)((uintptr_t)aw->aw_buff + offset);
1951 
1952 	/*
1953 	 * must read the current node for the call back to use
1954 	 */
1955 	if (mdb_vread(aw->aw_buff, size, addr) == -1) {
1956 		mdb_warn("read of avl_node_t failed: %p", addr);
1957 		return (WALK_ERR);
1958 	}
1959 
1960 	/*
1961 	 * do the call back
1962 	 */
1963 	status = wsp->walk_callback(addr, aw->aw_buff, wsp->walk_cbdata);
1964 	if (status != WALK_NEXT)
1965 		return (status);
1966 
1967 	/*
1968 	 * move to the next node....
1969 	 * note we read in new nodes, so the pointer to the buffer is fixed
1970 	 */
1971 
1972 	/*
1973 	 * if the node has a right child then go to it and then all the way
1974 	 * thru as many left children as possible
1975 	 */
1976 	addr = (uintptr_t)node->avl_child[1];
1977 	if (addr != NULL) {
1978 		addr = avl_leftmostchild(addr, aw->aw_buff, offset, size);
1979 		if (addr == (uintptr_t)-1L)
1980 			return (WALK_ERR);
1981 
1982 	/*
1983 	 * othewise return to parent nodes, stopping if we ever return from
1984 	 * a left child
1985 	 */
1986 	} else {
1987 		for (;;) {
1988 			was_child = AVL_XCHILD(node);
1989 			addr = (uintptr_t)AVL_XPARENT(node);
1990 			if (addr == NULL)
1991 				break;
1992 			addr -= offset;
1993 			if (was_child == 0) /* stop on return from left child */
1994 				break;
1995 			if (mdb_vread(aw->aw_buff, size, addr) == -1) {
1996 				mdb_warn("read of avl_node_t failed: %p", addr);
1997 				return (WALK_ERR);
1998 			}
1999 		}
2000 	}
2001 
2002 	wsp->walk_addr = addr;
2003 	return (WALK_NEXT);
2004 }
2005 
2006 /*
2007  * Release the memory allocated for the walk
2008  */
2009 void
2010 avl_walk_fini(mdb_walk_state_t *wsp)
2011 {
2012 	struct aw_info *aw;
2013 
2014 	aw = (struct aw_info *)wsp->walk_data;
2015 
2016 	if (aw == NULL)
2017 		return;
2018 
2019 	if (aw->aw_buff != NULL)
2020 		mdb_free(aw->aw_buff, aw->aw_tree.avl_size);
2021 
2022 	mdb_free(aw, sizeof (struct aw_info));
2023 }
2024 
2025 
2026 int
2027 seg_walk_init(mdb_walk_state_t *wsp)
2028 {
2029 	if (wsp->walk_addr == NULL) {
2030 		mdb_warn("seg walk must begin at struct as *\n");
2031 		return (WALK_ERR);
2032 	}
2033 
2034 	/*
2035 	 * this is really just a wrapper to AVL tree walk
2036 	 */
2037 	wsp->walk_addr = (uintptr_t)&((struct as *)wsp->walk_addr)->a_segtree;
2038 	return (avl_walk_init(wsp));
2039 }
2040 
2041 static int
2042 cpu_walk_cmp(const void *l, const void *r)
2043 {
2044 	uintptr_t lhs = *((uintptr_t *)l);
2045 	uintptr_t rhs = *((uintptr_t *)r);
2046 	cpu_t lcpu, rcpu;
2047 
2048 	(void) mdb_vread(&lcpu, sizeof (lcpu), lhs);
2049 	(void) mdb_vread(&rcpu, sizeof (rcpu), rhs);
2050 
2051 	if (lcpu.cpu_id < rcpu.cpu_id)
2052 		return (-1);
2053 
2054 	if (lcpu.cpu_id > rcpu.cpu_id)
2055 		return (1);
2056 
2057 	return (0);
2058 }
2059 
2060 typedef struct cpu_walk {
2061 	uintptr_t *cw_array;
2062 	int cw_ndx;
2063 } cpu_walk_t;
2064 
2065 int
2066 cpu_walk_init(mdb_walk_state_t *wsp)
2067 {
2068 	cpu_walk_t *cw;
2069 	int max_ncpus, i = 0;
2070 	uintptr_t current, first;
2071 	cpu_t cpu, panic_cpu;
2072 	uintptr_t panicstr, addr;
2073 	GElf_Sym sym;
2074 
2075 	cw = mdb_zalloc(sizeof (cpu_walk_t), UM_SLEEP | UM_GC);
2076 
2077 	if (mdb_readvar(&max_ncpus, "max_ncpus") == -1) {
2078 		mdb_warn("failed to read 'max_ncpus'");
2079 		return (WALK_ERR);
2080 	}
2081 
2082 	if (mdb_readvar(&panicstr, "panicstr") == -1) {
2083 		mdb_warn("failed to read 'panicstr'");
2084 		return (WALK_ERR);
2085 	}
2086 
2087 	if (panicstr != NULL) {
2088 		if (mdb_lookup_by_name("panic_cpu", &sym) == -1) {
2089 			mdb_warn("failed to find 'panic_cpu'");
2090 			return (WALK_ERR);
2091 		}
2092 
2093 		addr = (uintptr_t)sym.st_value;
2094 
2095 		if (mdb_vread(&panic_cpu, sizeof (cpu_t), addr) == -1) {
2096 			mdb_warn("failed to read 'panic_cpu'");
2097 			return (WALK_ERR);
2098 		}
2099 	}
2100 
2101 	/*
2102 	 * Unfortunately, there is no platform-independent way to walk
2103 	 * CPUs in ID order.  We therefore loop through in cpu_next order,
2104 	 * building an array of CPU pointers which will subsequently be
2105 	 * sorted.
2106 	 */
2107 	cw->cw_array =
2108 	    mdb_zalloc((max_ncpus + 1) * sizeof (uintptr_t), UM_SLEEP | UM_GC);
2109 
2110 	if (mdb_readvar(&first, "cpu_list") == -1) {
2111 		mdb_warn("failed to read 'cpu_list'");
2112 		return (WALK_ERR);
2113 	}
2114 
2115 	current = first;
2116 	do {
2117 		if (mdb_vread(&cpu, sizeof (cpu), current) == -1) {
2118 			mdb_warn("failed to read cpu at %p", current);
2119 			return (WALK_ERR);
2120 		}
2121 
2122 		if (panicstr != NULL && panic_cpu.cpu_id == cpu.cpu_id) {
2123 			cw->cw_array[i++] = addr;
2124 		} else {
2125 			cw->cw_array[i++] = current;
2126 		}
2127 	} while ((current = (uintptr_t)cpu.cpu_next) != first);
2128 
2129 	qsort(cw->cw_array, i, sizeof (uintptr_t), cpu_walk_cmp);
2130 	wsp->walk_data = cw;
2131 
2132 	return (WALK_NEXT);
2133 }
2134 
2135 int
2136 cpu_walk_step(mdb_walk_state_t *wsp)
2137 {
2138 	cpu_walk_t *cw = wsp->walk_data;
2139 	cpu_t cpu;
2140 	uintptr_t addr = cw->cw_array[cw->cw_ndx++];
2141 
2142 	if (addr == NULL)
2143 		return (WALK_DONE);
2144 
2145 	if (mdb_vread(&cpu, sizeof (cpu), addr) == -1) {
2146 		mdb_warn("failed to read cpu at %p", addr);
2147 		return (WALK_DONE);
2148 	}
2149 
2150 	return (wsp->walk_callback(addr, &cpu, wsp->walk_cbdata));
2151 }
2152 
2153 typedef struct cpuinfo_data {
2154 	intptr_t cid_cpu;
2155 	uintptr_t cid_lbolt;
2156 	uintptr_t **cid_ithr;
2157 	char	cid_print_head;
2158 	char	cid_print_thr;
2159 	char	cid_print_ithr;
2160 	char	cid_print_flags;
2161 } cpuinfo_data_t;
2162 
2163 int
2164 cpuinfo_walk_ithread(uintptr_t addr, const kthread_t *thr, cpuinfo_data_t *cid)
2165 {
2166 	cpu_t c;
2167 	int id;
2168 	uint8_t pil;
2169 
2170 	if (!(thr->t_flag & T_INTR_THREAD) || thr->t_state == TS_FREE)
2171 		return (WALK_NEXT);
2172 
2173 	if (thr->t_bound_cpu == NULL) {
2174 		mdb_warn("thr %p is intr thread w/out a CPU\n", addr);
2175 		return (WALK_NEXT);
2176 	}
2177 
2178 	(void) mdb_vread(&c, sizeof (c), (uintptr_t)thr->t_bound_cpu);
2179 
2180 	if ((id = c.cpu_id) >= NCPU) {
2181 		mdb_warn("CPU %p has id (%d) greater than NCPU (%d)\n",
2182 		    thr->t_bound_cpu, id, NCPU);
2183 		return (WALK_NEXT);
2184 	}
2185 
2186 	if ((pil = thr->t_pil) >= NINTR) {
2187 		mdb_warn("thread %p has pil (%d) greater than %d\n",
2188 		    addr, pil, NINTR);
2189 		return (WALK_NEXT);
2190 	}
2191 
2192 	if (cid->cid_ithr[id][pil] != NULL) {
2193 		mdb_warn("CPU %d has multiple threads at pil %d (at least "
2194 		    "%p and %p)\n", id, pil, addr, cid->cid_ithr[id][pil]);
2195 		return (WALK_NEXT);
2196 	}
2197 
2198 	cid->cid_ithr[id][pil] = addr;
2199 
2200 	return (WALK_NEXT);
2201 }
2202 
2203 #define	CPUINFO_IDWIDTH		3
2204 #define	CPUINFO_FLAGWIDTH	9
2205 
2206 #ifdef _LP64
2207 #define	CPUINFO_CPUWIDTH	11
2208 #define	CPUINFO_TWIDTH		11
2209 #else
2210 #define	CPUINFO_CPUWIDTH	8
2211 #define	CPUINFO_TWIDTH		8
2212 #endif
2213 
2214 #define	CPUINFO_THRDELT		(CPUINFO_IDWIDTH + CPUINFO_CPUWIDTH + 9)
2215 #define	CPUINFO_FLAGDELT	(CPUINFO_IDWIDTH + CPUINFO_CPUWIDTH + 4)
2216 #define	CPUINFO_ITHRDELT	4
2217 
2218 #define	CPUINFO_INDENT	mdb_printf("%*s", CPUINFO_THRDELT, \
2219     flagline < nflaglines ? flagbuf[flagline++] : "")
2220 
2221 int
2222 cpuinfo_walk_cpu(uintptr_t addr, const cpu_t *cpu, cpuinfo_data_t *cid)
2223 {
2224 	kthread_t t;
2225 	disp_t disp;
2226 	proc_t p;
2227 	uintptr_t pinned;
2228 	char **flagbuf;
2229 	int nflaglines = 0, flagline = 0, bspl, rval = WALK_NEXT;
2230 
2231 	const char *flags[] = {
2232 	    "RUNNING", "READY", "QUIESCED", "EXISTS",
2233 	    "ENABLE", "OFFLINE", "POWEROFF", "FROZEN",
2234 	    "SPARE", "FAULTED", NULL
2235 	};
2236 
2237 	if (cid->cid_cpu != -1) {
2238 		if (addr != cid->cid_cpu && cpu->cpu_id != cid->cid_cpu)
2239 			return (WALK_NEXT);
2240 
2241 		/*
2242 		 * Set cid_cpu to -1 to indicate that we found a matching CPU.
2243 		 */
2244 		cid->cid_cpu = -1;
2245 		rval = WALK_DONE;
2246 	}
2247 
2248 	if (cid->cid_print_head) {
2249 		mdb_printf("%3s %-*s %3s %4s %4s %3s %4s %5s %-6s %-*s %s\n",
2250 		    "ID", CPUINFO_CPUWIDTH, "ADDR", "FLG", "NRUN", "BSPL",
2251 		    "PRI", "RNRN", "KRNRN", "SWITCH", CPUINFO_TWIDTH, "THREAD",
2252 		    "PROC");
2253 		cid->cid_print_head = FALSE;
2254 	}
2255 
2256 	bspl = cpu->cpu_base_spl;
2257 
2258 	if (mdb_vread(&disp, sizeof (disp_t), (uintptr_t)cpu->cpu_disp) == -1) {
2259 		mdb_warn("failed to read disp_t at %p", cpu->cpu_disp);
2260 		return (WALK_ERR);
2261 	}
2262 
2263 	mdb_printf("%3d %0*p %3x %4d %4d ",
2264 	    cpu->cpu_id, CPUINFO_CPUWIDTH, addr, cpu->cpu_flags,
2265 	    disp.disp_nrunnable, bspl);
2266 
2267 	if (mdb_vread(&t, sizeof (t), (uintptr_t)cpu->cpu_thread) != -1) {
2268 		mdb_printf("%3d ", t.t_pri);
2269 	} else {
2270 		mdb_printf("%3s ", "-");
2271 	}
2272 
2273 	mdb_printf("%4s %5s ", cpu->cpu_runrun ? "yes" : "no",
2274 	    cpu->cpu_kprunrun ? "yes" : "no");
2275 
2276 	if (cpu->cpu_last_swtch) {
2277 		clock_t lbolt;
2278 
2279 		if (mdb_vread(&lbolt, sizeof (lbolt), cid->cid_lbolt) == -1) {
2280 			mdb_warn("failed to read lbolt at %p", cid->cid_lbolt);
2281 			return (WALK_ERR);
2282 		}
2283 		mdb_printf("t-%-4d ", lbolt - cpu->cpu_last_swtch);
2284 	} else {
2285 		mdb_printf("%-6s ", "-");
2286 	}
2287 
2288 	mdb_printf("%0*p", CPUINFO_TWIDTH, cpu->cpu_thread);
2289 
2290 	if (cpu->cpu_thread == cpu->cpu_idle_thread)
2291 		mdb_printf(" (idle)\n");
2292 	else if (cpu->cpu_thread == NULL)
2293 		mdb_printf(" -\n");
2294 	else {
2295 		if (mdb_vread(&p, sizeof (p), (uintptr_t)t.t_procp) != -1) {
2296 			mdb_printf(" %s\n", p.p_user.u_comm);
2297 		} else {
2298 			mdb_printf(" ?\n");
2299 		}
2300 	}
2301 
2302 	flagbuf = mdb_zalloc(sizeof (flags), UM_SLEEP | UM_GC);
2303 
2304 	if (cid->cid_print_flags) {
2305 		int first = 1, i, j, k;
2306 		char *s;
2307 
2308 		cid->cid_print_head = TRUE;
2309 
2310 		for (i = 1, j = 0; flags[j] != NULL; i <<= 1, j++) {
2311 			if (!(cpu->cpu_flags & i))
2312 				continue;
2313 
2314 			if (first) {
2315 				s = mdb_alloc(CPUINFO_THRDELT + 1,
2316 				    UM_GC | UM_SLEEP);
2317 
2318 				(void) mdb_snprintf(s, CPUINFO_THRDELT + 1,
2319 				    "%*s|%*s", CPUINFO_FLAGDELT, "",
2320 				    CPUINFO_THRDELT - 1 - CPUINFO_FLAGDELT, "");
2321 				flagbuf[nflaglines++] = s;
2322 			}
2323 
2324 			s = mdb_alloc(CPUINFO_THRDELT + 1, UM_GC | UM_SLEEP);
2325 			(void) mdb_snprintf(s, CPUINFO_THRDELT + 1, "%*s%*s %s",
2326 			    CPUINFO_IDWIDTH + CPUINFO_CPUWIDTH -
2327 			    CPUINFO_FLAGWIDTH, "", CPUINFO_FLAGWIDTH, flags[j],
2328 			    first ? "<--+" : "");
2329 
2330 			for (k = strlen(s); k < CPUINFO_THRDELT; k++)
2331 				s[k] = ' ';
2332 			s[k] = '\0';
2333 
2334 			flagbuf[nflaglines++] = s;
2335 			first = 0;
2336 		}
2337 	}
2338 
2339 	if (cid->cid_print_ithr) {
2340 		int i, found_one = FALSE;
2341 		int print_thr = disp.disp_nrunnable && cid->cid_print_thr;
2342 
2343 		for (i = NINTR - 1; i >= 0; i--) {
2344 			uintptr_t iaddr = cid->cid_ithr[cpu->cpu_id][i];
2345 
2346 			if (iaddr == NULL)
2347 				continue;
2348 
2349 			if (!found_one) {
2350 				found_one = TRUE;
2351 
2352 				CPUINFO_INDENT;
2353 				mdb_printf("%c%*s|\n", print_thr ? '|' : ' ',
2354 				    CPUINFO_ITHRDELT, "");
2355 
2356 				CPUINFO_INDENT;
2357 				mdb_printf("%c%*s+--> %3s %s\n",
2358 				    print_thr ? '|' : ' ', CPUINFO_ITHRDELT,
2359 				    "", "PIL", "THREAD");
2360 			}
2361 
2362 			if (mdb_vread(&t, sizeof (t), iaddr) == -1) {
2363 				mdb_warn("failed to read kthread_t at %p",
2364 				    iaddr);
2365 				return (WALK_ERR);
2366 			}
2367 
2368 			CPUINFO_INDENT;
2369 			mdb_printf("%c%*s     %3d %0*p\n",
2370 			    print_thr ? '|' : ' ', CPUINFO_ITHRDELT, "",
2371 			    t.t_pil, CPUINFO_TWIDTH, iaddr);
2372 
2373 			pinned = (uintptr_t)t.t_intr;
2374 		}
2375 
2376 		if (found_one && pinned != NULL) {
2377 			cid->cid_print_head = TRUE;
2378 			(void) strcpy(p.p_user.u_comm, "?");
2379 
2380 			if (mdb_vread(&t, sizeof (t),
2381 			    (uintptr_t)pinned) == -1) {
2382 				mdb_warn("failed to read kthread_t at %p",
2383 				    pinned);
2384 				return (WALK_ERR);
2385 			}
2386 			if (mdb_vread(&p, sizeof (p),
2387 			    (uintptr_t)t.t_procp) == -1) {
2388 				mdb_warn("failed to read proc_t at %p",
2389 				    t.t_procp);
2390 				return (WALK_ERR);
2391 			}
2392 
2393 			CPUINFO_INDENT;
2394 			mdb_printf("%c%*s     %3s %0*p %s\n",
2395 			    print_thr ? '|' : ' ', CPUINFO_ITHRDELT, "", "-",
2396 			    CPUINFO_TWIDTH, pinned,
2397 			    pinned == (uintptr_t)cpu->cpu_idle_thread ?
2398 			    "(idle)" : p.p_user.u_comm);
2399 		}
2400 	}
2401 
2402 	if (disp.disp_nrunnable && cid->cid_print_thr) {
2403 		dispq_t *dq;
2404 
2405 		int i, npri = disp.disp_npri;
2406 
2407 		dq = mdb_alloc(sizeof (dispq_t) * npri, UM_SLEEP | UM_GC);
2408 
2409 		if (mdb_vread(dq, sizeof (dispq_t) * npri,
2410 		    (uintptr_t)disp.disp_q) == -1) {
2411 			mdb_warn("failed to read dispq_t at %p", disp.disp_q);
2412 			return (WALK_ERR);
2413 		}
2414 
2415 		CPUINFO_INDENT;
2416 		mdb_printf("|\n");
2417 
2418 		CPUINFO_INDENT;
2419 		mdb_printf("+-->  %3s %-*s %s\n", "PRI",
2420 		    CPUINFO_TWIDTH, "THREAD", "PROC");
2421 
2422 		for (i = npri - 1; i >= 0; i--) {
2423 			uintptr_t taddr = (uintptr_t)dq[i].dq_first;
2424 
2425 			while (taddr != NULL) {
2426 				if (mdb_vread(&t, sizeof (t), taddr) == -1) {
2427 					mdb_warn("failed to read kthread_t "
2428 					    "at %p", taddr);
2429 					return (WALK_ERR);
2430 				}
2431 				if (mdb_vread(&p, sizeof (p),
2432 				    (uintptr_t)t.t_procp) == -1) {
2433 					mdb_warn("failed to read proc_t at %p",
2434 					    t.t_procp);
2435 					return (WALK_ERR);
2436 				}
2437 
2438 				CPUINFO_INDENT;
2439 				mdb_printf("      %3d %0*p %s\n", t.t_pri,
2440 				    CPUINFO_TWIDTH, taddr, p.p_user.u_comm);
2441 
2442 				taddr = (uintptr_t)t.t_link;
2443 			}
2444 		}
2445 		cid->cid_print_head = TRUE;
2446 	}
2447 
2448 	while (flagline < nflaglines)
2449 		mdb_printf("%s\n", flagbuf[flagline++]);
2450 
2451 	if (cid->cid_print_head)
2452 		mdb_printf("\n");
2453 
2454 	return (rval);
2455 }
2456 
2457 int
2458 cpuinfo(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2459 {
2460 	uint_t verbose = FALSE;
2461 	cpuinfo_data_t cid;
2462 	GElf_Sym sym;
2463 	clock_t lbolt;
2464 
2465 	cid.cid_print_ithr = FALSE;
2466 	cid.cid_print_thr = FALSE;
2467 	cid.cid_print_flags = FALSE;
2468 	cid.cid_print_head = DCMD_HDRSPEC(flags) ? TRUE : FALSE;
2469 	cid.cid_cpu = -1;
2470 
2471 	if (flags & DCMD_ADDRSPEC)
2472 		cid.cid_cpu = addr;
2473 
2474 	if (mdb_getopts(argc, argv,
2475 	    'v', MDB_OPT_SETBITS, TRUE, &verbose, NULL) != argc)
2476 		return (DCMD_USAGE);
2477 
2478 	if (verbose) {
2479 		cid.cid_print_ithr = TRUE;
2480 		cid.cid_print_thr = TRUE;
2481 		cid.cid_print_flags = TRUE;
2482 		cid.cid_print_head = TRUE;
2483 	}
2484 
2485 	if (cid.cid_print_ithr) {
2486 		int i;
2487 
2488 		cid.cid_ithr = mdb_alloc(sizeof (uintptr_t **)
2489 		    * NCPU, UM_SLEEP | UM_GC);
2490 
2491 		for (i = 0; i < NCPU; i++)
2492 			cid.cid_ithr[i] = mdb_zalloc(sizeof (uintptr_t *) *
2493 			    NINTR, UM_SLEEP | UM_GC);
2494 
2495 		if (mdb_walk("thread", (mdb_walk_cb_t)cpuinfo_walk_ithread,
2496 		    &cid) == -1) {
2497 			mdb_warn("couldn't walk thread");
2498 			return (DCMD_ERR);
2499 		}
2500 	}
2501 
2502 	if (mdb_lookup_by_name("panic_lbolt", &sym) == -1) {
2503 		mdb_warn("failed to find panic_lbolt");
2504 		return (DCMD_ERR);
2505 	}
2506 
2507 	cid.cid_lbolt = (uintptr_t)sym.st_value;
2508 
2509 	if (mdb_vread(&lbolt, sizeof (lbolt), cid.cid_lbolt) == -1) {
2510 		mdb_warn("failed to read panic_lbolt");
2511 		return (DCMD_ERR);
2512 	}
2513 
2514 	if (lbolt == 0) {
2515 		if (mdb_lookup_by_name("lbolt", &sym) == -1) {
2516 			mdb_warn("failed to find lbolt");
2517 			return (DCMD_ERR);
2518 		}
2519 		cid.cid_lbolt = (uintptr_t)sym.st_value;
2520 	}
2521 
2522 	if (mdb_walk("cpu", (mdb_walk_cb_t)cpuinfo_walk_cpu, &cid) == -1) {
2523 		mdb_warn("can't walk cpus");
2524 		return (DCMD_ERR);
2525 	}
2526 
2527 	if (cid.cid_cpu != -1) {
2528 		/*
2529 		 * We didn't find this CPU when we walked through the CPUs
2530 		 * (i.e. the address specified doesn't show up in the "cpu"
2531 		 * walk).  However, the specified address may still correspond
2532 		 * to a valid cpu_t (for example, if the specified address is
2533 		 * the actual panicking cpu_t and not the cached panic_cpu).
2534 		 * Point is:  even if we didn't find it, we still want to try
2535 		 * to print the specified address as a cpu_t.
2536 		 */
2537 		cpu_t cpu;
2538 
2539 		if (mdb_vread(&cpu, sizeof (cpu), cid.cid_cpu) == -1) {
2540 			mdb_warn("%p is neither a valid CPU ID nor a "
2541 			    "valid cpu_t address\n", cid.cid_cpu);
2542 			return (DCMD_ERR);
2543 		}
2544 
2545 		(void) cpuinfo_walk_cpu(cid.cid_cpu, &cpu, &cid);
2546 	}
2547 
2548 	return (DCMD_OK);
2549 }
2550 
2551 /*ARGSUSED*/
2552 int
2553 flipone(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2554 {
2555 	int i;
2556 
2557 	if (!(flags & DCMD_ADDRSPEC))
2558 		return (DCMD_USAGE);
2559 
2560 	for (i = 0; i < sizeof (addr) * NBBY; i++)
2561 		mdb_printf("%p\n", addr ^ (1UL << i));
2562 
2563 	return (DCMD_OK);
2564 }
2565 
2566 /*
2567  * Grumble, grumble.
2568  */
2569 #define	SMAP_HASHFUNC(vp, off)	\
2570 	((((uintptr_t)(vp) >> 6) + ((uintptr_t)(vp) >> 3) + \
2571 	((off) >> MAXBSHIFT)) & smd_hashmsk)
2572 
2573 int
2574 vnode2smap(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2575 {
2576 	long smd_hashmsk;
2577 	int hash;
2578 	uintptr_t offset = 0;
2579 	struct smap smp;
2580 	uintptr_t saddr, kaddr;
2581 	uintptr_t smd_hash, smd_smap;
2582 	struct seg seg;
2583 
2584 	if (!(flags & DCMD_ADDRSPEC))
2585 		return (DCMD_USAGE);
2586 
2587 	if (mdb_readvar(&smd_hashmsk, "smd_hashmsk") == -1) {
2588 		mdb_warn("failed to read smd_hashmsk");
2589 		return (DCMD_ERR);
2590 	}
2591 
2592 	if (mdb_readvar(&smd_hash, "smd_hash") == -1) {
2593 		mdb_warn("failed to read smd_hash");
2594 		return (DCMD_ERR);
2595 	}
2596 
2597 	if (mdb_readvar(&smd_smap, "smd_smap") == -1) {
2598 		mdb_warn("failed to read smd_hash");
2599 		return (DCMD_ERR);
2600 	}
2601 
2602 	if (mdb_readvar(&kaddr, "segkmap") == -1) {
2603 		mdb_warn("failed to read segkmap");
2604 		return (DCMD_ERR);
2605 	}
2606 
2607 	if (mdb_vread(&seg, sizeof (seg), kaddr) == -1) {
2608 		mdb_warn("failed to read segkmap at %p", kaddr);
2609 		return (DCMD_ERR);
2610 	}
2611 
2612 	if (argc != 0) {
2613 		const mdb_arg_t *arg = &argv[0];
2614 
2615 		if (arg->a_type == MDB_TYPE_IMMEDIATE)
2616 			offset = arg->a_un.a_val;
2617 		else
2618 			offset = (uintptr_t)mdb_strtoull(arg->a_un.a_str);
2619 	}
2620 
2621 	hash = SMAP_HASHFUNC(addr, offset);
2622 
2623 	if (mdb_vread(&saddr, sizeof (saddr),
2624 	    smd_hash + hash * sizeof (uintptr_t)) == -1) {
2625 		mdb_warn("couldn't read smap at %p",
2626 		    smd_hash + hash * sizeof (uintptr_t));
2627 		return (DCMD_ERR);
2628 	}
2629 
2630 	do {
2631 		if (mdb_vread(&smp, sizeof (smp), saddr) == -1) {
2632 			mdb_warn("couldn't read smap at %p", saddr);
2633 			return (DCMD_ERR);
2634 		}
2635 
2636 		if ((uintptr_t)smp.sm_vp == addr && smp.sm_off == offset) {
2637 			mdb_printf("vnode %p, offs %p is smap %p, vaddr %p\n",
2638 			    addr, offset, saddr, ((saddr - smd_smap) /
2639 			    sizeof (smp)) * MAXBSIZE + seg.s_base);
2640 			return (DCMD_OK);
2641 		}
2642 
2643 		saddr = (uintptr_t)smp.sm_hash;
2644 	} while (saddr != NULL);
2645 
2646 	mdb_printf("no smap for vnode %p, offs %p\n", addr, offset);
2647 	return (DCMD_OK);
2648 }
2649 
2650 /*ARGSUSED*/
2651 int
2652 addr2smap(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2653 {
2654 	uintptr_t kaddr;
2655 	struct seg seg;
2656 	struct segmap_data sd;
2657 
2658 	if (!(flags & DCMD_ADDRSPEC))
2659 		return (DCMD_USAGE);
2660 
2661 	if (mdb_readvar(&kaddr, "segkmap") == -1) {
2662 		mdb_warn("failed to read segkmap");
2663 		return (DCMD_ERR);
2664 	}
2665 
2666 	if (mdb_vread(&seg, sizeof (seg), kaddr) == -1) {
2667 		mdb_warn("failed to read segkmap at %p", kaddr);
2668 		return (DCMD_ERR);
2669 	}
2670 
2671 	if (mdb_vread(&sd, sizeof (sd), (uintptr_t)seg.s_data) == -1) {
2672 		mdb_warn("failed to read segmap_data at %p", seg.s_data);
2673 		return (DCMD_ERR);
2674 	}
2675 
2676 	mdb_printf("%p is smap %p\n", addr,
2677 	    ((addr - (uintptr_t)seg.s_base) >> MAXBSHIFT) *
2678 	    sizeof (struct smap) + (uintptr_t)sd.smd_sm);
2679 
2680 	return (DCMD_OK);
2681 }
2682 
2683 int
2684 as2proc_walk(uintptr_t addr, const proc_t *p, struct as **asp)
2685 {
2686 	if (p->p_as == *asp)
2687 		mdb_printf("%p\n", addr);
2688 	return (WALK_NEXT);
2689 }
2690 
2691 /*ARGSUSED*/
2692 int
2693 as2proc(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2694 {
2695 	if (!(flags & DCMD_ADDRSPEC) || argc != 0)
2696 		return (DCMD_USAGE);
2697 
2698 	if (mdb_walk("proc", (mdb_walk_cb_t)as2proc_walk, &addr) == -1) {
2699 		mdb_warn("failed to walk proc");
2700 		return (DCMD_ERR);
2701 	}
2702 
2703 	return (DCMD_OK);
2704 }
2705 
2706 /*ARGSUSED*/
2707 int
2708 ptree_walk(uintptr_t addr, const proc_t *p, void *ignored)
2709 {
2710 	proc_t parent;
2711 	int ident = 0;
2712 	uintptr_t paddr;
2713 
2714 	for (paddr = (uintptr_t)p->p_parent; paddr != NULL; ident += 5) {
2715 		mdb_vread(&parent, sizeof (parent), paddr);
2716 		paddr = (uintptr_t)parent.p_parent;
2717 	}
2718 
2719 	mdb_inc_indent(ident);
2720 	mdb_printf("%0?p  %s\n", addr, p->p_user.u_comm);
2721 	mdb_dec_indent(ident);
2722 
2723 	return (WALK_NEXT);
2724 }
2725 
2726 void
2727 ptree_ancestors(uintptr_t addr, uintptr_t start)
2728 {
2729 	proc_t p;
2730 
2731 	if (mdb_vread(&p, sizeof (p), addr) == -1) {
2732 		mdb_warn("couldn't read ancestor at %p", addr);
2733 		return;
2734 	}
2735 
2736 	if (p.p_parent != NULL)
2737 		ptree_ancestors((uintptr_t)p.p_parent, start);
2738 
2739 	if (addr != start)
2740 		(void) ptree_walk(addr, &p, NULL);
2741 }
2742 
2743 /*ARGSUSED*/
2744 int
2745 ptree(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2746 {
2747 	if (!(flags & DCMD_ADDRSPEC))
2748 		addr = NULL;
2749 	else
2750 		ptree_ancestors(addr, addr);
2751 
2752 	if (mdb_pwalk("proc", (mdb_walk_cb_t)ptree_walk, NULL, addr) == -1) {
2753 		mdb_warn("couldn't walk 'proc'");
2754 		return (DCMD_ERR);
2755 	}
2756 
2757 	return (DCMD_OK);
2758 }
2759 
2760 /*ARGSUSED*/
2761 static int
2762 fd(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2763 {
2764 	int fdnum;
2765 	const mdb_arg_t *argp = &argv[0];
2766 	proc_t p;
2767 	uf_entry_t uf;
2768 
2769 	if ((flags & DCMD_ADDRSPEC) == 0) {
2770 		mdb_warn("fd doesn't give global information\n");
2771 		return (DCMD_ERR);
2772 	}
2773 	if (argc != 1)
2774 		return (DCMD_USAGE);
2775 
2776 	if (argp->a_type == MDB_TYPE_IMMEDIATE)
2777 		fdnum = argp->a_un.a_val;
2778 	else
2779 		fdnum = mdb_strtoull(argp->a_un.a_str);
2780 
2781 	if (mdb_vread(&p, sizeof (struct proc), addr) == -1) {
2782 		mdb_warn("couldn't read proc_t at %p", addr);
2783 		return (DCMD_ERR);
2784 	}
2785 	if (fdnum > p.p_user.u_finfo.fi_nfiles) {
2786 		mdb_warn("process %p only has %d files open.\n",
2787 		    addr, p.p_user.u_finfo.fi_nfiles);
2788 		return (DCMD_ERR);
2789 	}
2790 	if (mdb_vread(&uf, sizeof (uf_entry_t),
2791 	    (uintptr_t)&p.p_user.u_finfo.fi_list[fdnum]) == -1) {
2792 		mdb_warn("couldn't read uf_entry_t at %p",
2793 		    &p.p_user.u_finfo.fi_list[fdnum]);
2794 		return (DCMD_ERR);
2795 	}
2796 
2797 	mdb_printf("%p\n", uf.uf_file);
2798 	return (DCMD_OK);
2799 }
2800 
2801 /*ARGSUSED*/
2802 static int
2803 pid2proc(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2804 {
2805 	pid_t pid = (pid_t)addr;
2806 
2807 	if (argc != 0)
2808 		return (DCMD_USAGE);
2809 
2810 	if ((addr = mdb_pid2proc(pid, NULL)) == NULL) {
2811 		mdb_warn("PID 0t%d not found\n", pid);
2812 		return (DCMD_ERR);
2813 	}
2814 
2815 	mdb_printf("%p\n", addr);
2816 	return (DCMD_OK);
2817 }
2818 
2819 static char *sysfile_cmd[] = {
2820 	"exclude:",
2821 	"include:",
2822 	"forceload:",
2823 	"rootdev:",
2824 	"rootfs:",
2825 	"swapdev:",
2826 	"swapfs:",
2827 	"moddir:",
2828 	"set",
2829 	"unknown",
2830 };
2831 
2832 static char *sysfile_ops[] = { "", "=", "&", "|" };
2833 
2834 /*ARGSUSED*/
2835 static int
2836 sysfile_vmem_seg(uintptr_t addr, const vmem_seg_t *vsp, void **target)
2837 {
2838 	if (vsp->vs_type == VMEM_ALLOC && (void *)vsp->vs_start == *target) {
2839 		*target = NULL;
2840 		return (WALK_DONE);
2841 	}
2842 	return (WALK_NEXT);
2843 }
2844 
2845 /*ARGSUSED*/
2846 static int
2847 sysfile(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2848 {
2849 	struct sysparam *sysp, sys;
2850 	char var[256];
2851 	char modname[256];
2852 	char val[256];
2853 	char strval[256];
2854 	vmem_t *mod_sysfile_arena;
2855 	void *straddr;
2856 
2857 	if (mdb_readvar(&sysp, "sysparam_hd") == -1) {
2858 		mdb_warn("failed to read sysparam_hd");
2859 		return (DCMD_ERR);
2860 	}
2861 
2862 	if (mdb_readvar(&mod_sysfile_arena, "mod_sysfile_arena") == -1) {
2863 		mdb_warn("failed to read mod_sysfile_arena");
2864 		return (DCMD_ERR);
2865 	}
2866 
2867 	while (sysp != NULL) {
2868 		var[0] = '\0';
2869 		val[0] = '\0';
2870 		modname[0] = '\0';
2871 		if (mdb_vread(&sys, sizeof (sys), (uintptr_t)sysp) == -1) {
2872 			mdb_warn("couldn't read sysparam %p", sysp);
2873 			return (DCMD_ERR);
2874 		}
2875 		if (sys.sys_modnam != NULL &&
2876 		    mdb_readstr(modname, 256,
2877 		    (uintptr_t)sys.sys_modnam) == -1) {
2878 			mdb_warn("couldn't read modname in %p", sysp);
2879 			return (DCMD_ERR);
2880 		}
2881 		if (sys.sys_ptr != NULL &&
2882 		    mdb_readstr(var, 256, (uintptr_t)sys.sys_ptr) == -1) {
2883 			mdb_warn("couldn't read ptr in %p", sysp);
2884 			return (DCMD_ERR);
2885 		}
2886 		if (sys.sys_op != SETOP_NONE) {
2887 			/*
2888 			 * Is this an int or a string?  We determine this
2889 			 * by checking whether straddr is contained in
2890 			 * mod_sysfile_arena.  If so, the walker will set
2891 			 * straddr to NULL.
2892 			 */
2893 			straddr = (void *)(uintptr_t)sys.sys_info;
2894 			if (sys.sys_op == SETOP_ASSIGN &&
2895 			    sys.sys_info != 0 &&
2896 			    mdb_pwalk("vmem_seg",
2897 			    (mdb_walk_cb_t)sysfile_vmem_seg, &straddr,
2898 			    (uintptr_t)mod_sysfile_arena) == 0 &&
2899 			    straddr == NULL &&
2900 			    mdb_readstr(strval, 256,
2901 			    (uintptr_t)sys.sys_info) != -1) {
2902 				(void) mdb_snprintf(val, sizeof (val), "\"%s\"",
2903 				    strval);
2904 			} else {
2905 				(void) mdb_snprintf(val, sizeof (val),
2906 				    "0x%llx [0t%llu]", sys.sys_info,
2907 				    sys.sys_info);
2908 			}
2909 		}
2910 		mdb_printf("%s %s%s%s%s%s\n", sysfile_cmd[sys.sys_type],
2911 		    modname, modname[0] == '\0' ? "" : ":",
2912 		    var, sysfile_ops[sys.sys_op], val);
2913 
2914 		sysp = sys.sys_next;
2915 	}
2916 
2917 	return (DCMD_OK);
2918 }
2919 
2920 /*
2921  * Dump a taskq_ent_t given its address.
2922  */
2923 /*ARGSUSED*/
2924 int
2925 taskq_ent(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2926 {
2927 	taskq_ent_t	taskq_ent;
2928 	GElf_Sym	sym;
2929 	char		buf[MDB_SYM_NAMLEN+1];
2930 
2931 
2932 	if (!(flags & DCMD_ADDRSPEC)) {
2933 		mdb_warn("expected explicit taskq_ent_t address before ::\n");
2934 		return (DCMD_USAGE);
2935 	}
2936 
2937 	if (mdb_vread(&taskq_ent, sizeof (taskq_ent_t), addr) == -1) {
2938 		mdb_warn("failed to read taskq_ent_t at %p", addr);
2939 		return (DCMD_ERR);
2940 	}
2941 
2942 	if (DCMD_HDRSPEC(flags)) {
2943 		mdb_printf("%<u>%-?s    %-?s    %-s%</u>\n",
2944 		"ENTRY", "ARG", "FUNCTION");
2945 	}
2946 
2947 	if (mdb_lookup_by_addr((uintptr_t)taskq_ent.tqent_func, MDB_SYM_EXACT,
2948 	    buf, sizeof (buf), &sym) == -1) {
2949 		(void) strcpy(buf, "????");
2950 	}
2951 
2952 	mdb_printf("%-?p    %-?p    %s\n", addr, taskq_ent.tqent_arg, buf);
2953 
2954 	return (DCMD_OK);
2955 }
2956 
2957 /*
2958  * Given the address of the (taskq_t) task queue head, walk the queue listing
2959  * the address of every taskq_ent_t.
2960  */
2961 int
2962 taskq_walk_init(mdb_walk_state_t *wsp)
2963 {
2964 	taskq_t	tq_head;
2965 
2966 
2967 	if (wsp->walk_addr == NULL) {
2968 		mdb_warn("start address required\n");
2969 		return (WALK_ERR);
2970 	}
2971 
2972 
2973 	/*
2974 	 * Save the address of the list head entry.  This terminates the list.
2975 	 */
2976 	wsp->walk_data = (void *)
2977 	    ((size_t)wsp->walk_addr + offsetof(taskq_t, tq_task));
2978 
2979 
2980 	/*
2981 	 * Read in taskq head, set walk_addr to point to first taskq_ent_t.
2982 	 */
2983 	if (mdb_vread((void *)&tq_head, sizeof (taskq_t), wsp->walk_addr) ==
2984 	    -1) {
2985 		mdb_warn("failed to read taskq list head at %p",
2986 		    wsp->walk_addr);
2987 	}
2988 	wsp->walk_addr = (uintptr_t)tq_head.tq_task.tqent_next;
2989 
2990 
2991 	/*
2992 	 * Check for null list (next=head)
2993 	 */
2994 	if (wsp->walk_addr == (uintptr_t)wsp->walk_data) {
2995 		return (WALK_DONE);
2996 	}
2997 
2998 	return (WALK_NEXT);
2999 }
3000 
3001 
3002 int
3003 taskq_walk_step(mdb_walk_state_t *wsp)
3004 {
3005 	taskq_ent_t	tq_ent;
3006 	int		status;
3007 
3008 
3009 	if (mdb_vread((void *)&tq_ent, sizeof (taskq_ent_t), wsp->walk_addr) ==
3010 	    -1) {
3011 		mdb_warn("failed to read taskq_ent_t at %p", wsp->walk_addr);
3012 		return (DCMD_ERR);
3013 	}
3014 
3015 	status = wsp->walk_callback(wsp->walk_addr, (void *)&tq_ent,
3016 	    wsp->walk_cbdata);
3017 
3018 	wsp->walk_addr = (uintptr_t)tq_ent.tqent_next;
3019 
3020 
3021 	/* Check if we're at the last element (next=head) */
3022 	if (wsp->walk_addr == (uintptr_t)wsp->walk_data) {
3023 		return (WALK_DONE);
3024 	}
3025 
3026 	return (status);
3027 }
3028 
3029 int
3030 didmatch(uintptr_t addr, const kthread_t *thr, kt_did_t *didp)
3031 {
3032 
3033 	if (*didp == thr->t_did) {
3034 		mdb_printf("%p\n", addr);
3035 		return (WALK_DONE);
3036 	} else
3037 		return (WALK_NEXT);
3038 }
3039 
3040 /*ARGSUSED*/
3041 int
3042 did2thread(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
3043 {
3044 	const mdb_arg_t *argp = &argv[0];
3045 	kt_did_t	did;
3046 
3047 	if (argc != 1)
3048 		return (DCMD_USAGE);
3049 
3050 	did = (kt_did_t)mdb_strtoull(argp->a_un.a_str);
3051 
3052 	if (mdb_walk("thread", (mdb_walk_cb_t)didmatch, (void *)&did) == -1) {
3053 		mdb_warn("failed to walk thread");
3054 		return (DCMD_ERR);
3055 
3056 	}
3057 	return (DCMD_OK);
3058 
3059 }
3060 
3061 static int
3062 errorq_walk_init(mdb_walk_state_t *wsp)
3063 {
3064 	if (wsp->walk_addr == NULL &&
3065 	    mdb_readvar(&wsp->walk_addr, "errorq_list") == -1) {
3066 		mdb_warn("failed to read errorq_list");
3067 		return (WALK_ERR);
3068 	}
3069 
3070 	return (WALK_NEXT);
3071 }
3072 
3073 static int
3074 errorq_walk_step(mdb_walk_state_t *wsp)
3075 {
3076 	uintptr_t addr = wsp->walk_addr;
3077 	errorq_t eq;
3078 
3079 	if (addr == NULL)
3080 		return (WALK_DONE);
3081 
3082 	if (mdb_vread(&eq, sizeof (eq), addr) == -1) {
3083 		mdb_warn("failed to read errorq at %p", addr);
3084 		return (WALK_ERR);
3085 	}
3086 
3087 	wsp->walk_addr = (uintptr_t)eq.eq_next;
3088 	return (wsp->walk_callback(addr, &eq, wsp->walk_cbdata));
3089 }
3090 
3091 typedef struct eqd_walk_data {
3092 	uintptr_t *eqd_stack;
3093 	void *eqd_buf;
3094 	ulong_t eqd_qpos;
3095 	ulong_t eqd_qlen;
3096 	size_t eqd_size;
3097 } eqd_walk_data_t;
3098 
3099 /*
3100  * In order to walk the list of pending error queue elements, we push the
3101  * addresses of the corresponding data buffers in to the eqd_stack array.
3102  * The error lists are in reverse chronological order when iterating using
3103  * eqe_prev, so we then pop things off the top in eqd_walk_step so that the
3104  * walker client gets addresses in order from oldest error to newest error.
3105  */
3106 static void
3107 eqd_push_list(eqd_walk_data_t *eqdp, uintptr_t addr)
3108 {
3109 	errorq_elem_t eqe;
3110 
3111 	while (addr != NULL) {
3112 		if (mdb_vread(&eqe, sizeof (eqe), addr) != sizeof (eqe)) {
3113 			mdb_warn("failed to read errorq element at %p", addr);
3114 			break;
3115 		}
3116 
3117 		if (eqdp->eqd_qpos == eqdp->eqd_qlen) {
3118 			mdb_warn("errorq is overfull -- more than %lu "
3119 			    "elems found\n", eqdp->eqd_qlen);
3120 			break;
3121 		}
3122 
3123 		eqdp->eqd_stack[eqdp->eqd_qpos++] = (uintptr_t)eqe.eqe_data;
3124 		addr = (uintptr_t)eqe.eqe_prev;
3125 	}
3126 }
3127 
3128 static int
3129 eqd_walk_init(mdb_walk_state_t *wsp)
3130 {
3131 	eqd_walk_data_t *eqdp;
3132 	errorq_elem_t eqe, *addr;
3133 	errorq_t eq;
3134 	ulong_t i;
3135 
3136 	if (mdb_vread(&eq, sizeof (eq), wsp->walk_addr) == -1) {
3137 		mdb_warn("failed to read errorq at %p", wsp->walk_addr);
3138 		return (WALK_ERR);
3139 	}
3140 
3141 	if (eq.eq_ptail != NULL &&
3142 	    mdb_vread(&eqe, sizeof (eqe), (uintptr_t)eq.eq_ptail) == -1) {
3143 		mdb_warn("failed to read errorq element at %p", eq.eq_ptail);
3144 		return (WALK_ERR);
3145 	}
3146 
3147 	eqdp = mdb_alloc(sizeof (eqd_walk_data_t), UM_SLEEP);
3148 	wsp->walk_data = eqdp;
3149 
3150 	eqdp->eqd_stack = mdb_zalloc(sizeof (uintptr_t) * eq.eq_qlen, UM_SLEEP);
3151 	eqdp->eqd_buf = mdb_alloc(eq.eq_size, UM_SLEEP);
3152 	eqdp->eqd_qlen = eq.eq_qlen;
3153 	eqdp->eqd_qpos = 0;
3154 	eqdp->eqd_size = eq.eq_size;
3155 
3156 	/*
3157 	 * The newest elements in the queue are on the pending list, so we
3158 	 * push those on to our stack first.
3159 	 */
3160 	eqd_push_list(eqdp, (uintptr_t)eq.eq_pend);
3161 
3162 	/*
3163 	 * If eq_ptail is set, it may point to a subset of the errors on the
3164 	 * pending list in the event a casptr() failed; if ptail's data is
3165 	 * already in our stack, NULL out eq_ptail and ignore it.
3166 	 */
3167 	if (eq.eq_ptail != NULL) {
3168 		for (i = 0; i < eqdp->eqd_qpos; i++) {
3169 			if (eqdp->eqd_stack[i] == (uintptr_t)eqe.eqe_data) {
3170 				eq.eq_ptail = NULL;
3171 				break;
3172 			}
3173 		}
3174 	}
3175 
3176 	/*
3177 	 * If eq_phead is set, it has the processing list in order from oldest
3178 	 * to newest.  Use this to recompute eq_ptail as best we can and then
3179 	 * we nicely fall into eqd_push_list() of eq_ptail below.
3180 	 */
3181 	for (addr = eq.eq_phead; addr != NULL && mdb_vread(&eqe, sizeof (eqe),
3182 	    (uintptr_t)addr) == sizeof (eqe); addr = eqe.eqe_next)
3183 		eq.eq_ptail = addr;
3184 
3185 	/*
3186 	 * The oldest elements in the queue are on the processing list, subject
3187 	 * to machinations in the if-clauses above.  Push any such elements.
3188 	 */
3189 	eqd_push_list(eqdp, (uintptr_t)eq.eq_ptail);
3190 	return (WALK_NEXT);
3191 }
3192 
3193 static int
3194 eqd_walk_step(mdb_walk_state_t *wsp)
3195 {
3196 	eqd_walk_data_t *eqdp = wsp->walk_data;
3197 	uintptr_t addr;
3198 
3199 	if (eqdp->eqd_qpos == 0)
3200 		return (WALK_DONE);
3201 
3202 	addr = eqdp->eqd_stack[--eqdp->eqd_qpos];
3203 
3204 	if (mdb_vread(eqdp->eqd_buf, eqdp->eqd_size, addr) != eqdp->eqd_size) {
3205 		mdb_warn("failed to read errorq data at %p", addr);
3206 		return (WALK_ERR);
3207 	}
3208 
3209 	return (wsp->walk_callback(addr, eqdp->eqd_buf, wsp->walk_cbdata));
3210 }
3211 
3212 static void
3213 eqd_walk_fini(mdb_walk_state_t *wsp)
3214 {
3215 	eqd_walk_data_t *eqdp = wsp->walk_data;
3216 
3217 	mdb_free(eqdp->eqd_stack, sizeof (uintptr_t) * eqdp->eqd_qlen);
3218 	mdb_free(eqdp->eqd_buf, eqdp->eqd_size);
3219 	mdb_free(eqdp, sizeof (eqd_walk_data_t));
3220 }
3221 
3222 #define	EQKSVAL(eqv, what) (eqv.eq_kstat.what.value.ui64)
3223 
3224 static int
3225 errorq(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
3226 {
3227 	int i;
3228 	errorq_t eq;
3229 	uint_t opt_v = FALSE;
3230 
3231 	if (!(flags & DCMD_ADDRSPEC)) {
3232 		if (mdb_walk_dcmd("errorq", "errorq", argc, argv) == -1) {
3233 			mdb_warn("can't walk 'errorq'");
3234 			return (DCMD_ERR);
3235 		}
3236 		return (DCMD_OK);
3237 	}
3238 
3239 	i = mdb_getopts(argc, argv, 'v', MDB_OPT_SETBITS, TRUE, &opt_v, NULL);
3240 	argc -= i;
3241 	argv += i;
3242 
3243 	if (argc != 0)
3244 		return (DCMD_USAGE);
3245 
3246 	if (opt_v || DCMD_HDRSPEC(flags)) {
3247 		mdb_printf("%<u>%-11s %-16s %1s %1s %1s ",
3248 		    "ADDR", "NAME", "S", "V", "N");
3249 		if (!opt_v) {
3250 			mdb_printf("%7s %7s %7s%</u>\n",
3251 			    "ACCEPT", "DROP", "LOG");
3252 		} else {
3253 			mdb_printf("%5s %6s %6s %3s %16s%</u>\n",
3254 			    "KSTAT", "QLEN", "SIZE", "IPL", "FUNC");
3255 		}
3256 	}
3257 
3258 	if (mdb_vread(&eq, sizeof (eq), addr) != sizeof (eq)) {
3259 		mdb_warn("failed to read errorq at %p", addr);
3260 		return (DCMD_ERR);
3261 	}
3262 
3263 	mdb_printf("%-11p %-16s %c %c %c ", addr, eq.eq_name,
3264 	    (eq.eq_flags & ERRORQ_ACTIVE) ? '+' : '-',
3265 	    (eq.eq_flags & ERRORQ_VITAL) ? '!' : ' ',
3266 	    (eq.eq_flags & ERRORQ_NVLIST) ? '*' : ' ');
3267 
3268 	if (!opt_v) {
3269 		mdb_printf("%7llu %7llu %7llu\n",
3270 		    EQKSVAL(eq, eqk_dispatched) + EQKSVAL(eq, eqk_committed),
3271 		    EQKSVAL(eq, eqk_dropped) + EQKSVAL(eq, eqk_reserve_fail) +
3272 		    EQKSVAL(eq, eqk_commit_fail), EQKSVAL(eq, eqk_logged));
3273 	} else {
3274 		mdb_printf("%5s %6lu %6lu %3u %a\n",
3275 		    "  |  ", eq.eq_qlen, eq.eq_size, eq.eq_ipl, eq.eq_func);
3276 		mdb_printf("%38s\n%41s"
3277 		    "%12s %llu\n"
3278 		    "%53s %llu\n"
3279 		    "%53s %llu\n"
3280 		    "%53s %llu\n"
3281 		    "%53s %llu\n"
3282 		    "%53s %llu\n"
3283 		    "%53s %llu\n"
3284 		    "%53s %llu\n\n",
3285 		    "|", "+-> ",
3286 		    "DISPATCHED",	EQKSVAL(eq, eqk_dispatched),
3287 		    "DROPPED",		EQKSVAL(eq, eqk_dropped),
3288 		    "LOGGED",		EQKSVAL(eq, eqk_logged),
3289 		    "RESERVED",		EQKSVAL(eq, eqk_reserved),
3290 		    "RESERVE FAIL",	EQKSVAL(eq, eqk_reserve_fail),
3291 		    "COMMITTED",	EQKSVAL(eq, eqk_committed),
3292 		    "COMMIT FAIL",	EQKSVAL(eq, eqk_commit_fail),
3293 		    "CANCELLED",	EQKSVAL(eq, eqk_cancelled));
3294 	}
3295 
3296 	return (DCMD_OK);
3297 }
3298 
3299 /*ARGSUSED*/
3300 static int
3301 panicinfo(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
3302 {
3303 	cpu_t panic_cpu;
3304 	kthread_t *panic_thread;
3305 	void *panicbuf;
3306 	panic_data_t *pd;
3307 	int i, n;
3308 
3309 	if (!mdb_prop_postmortem) {
3310 		mdb_warn("panicinfo can only be run on a system "
3311 		    "dump; see dumpadm(1M)\n");
3312 		return (DCMD_ERR);
3313 	}
3314 
3315 	if (flags & DCMD_ADDRSPEC || argc != 0)
3316 		return (DCMD_USAGE);
3317 
3318 	if (mdb_readsym(&panic_cpu, sizeof (cpu_t), "panic_cpu") == -1)
3319 		mdb_warn("failed to read 'panic_cpu'");
3320 	else
3321 		mdb_printf("%16s %?d\n", "cpu", panic_cpu.cpu_id);
3322 
3323 	if (mdb_readvar(&panic_thread, "panic_thread") == -1)
3324 		mdb_warn("failed to read 'panic_thread'");
3325 	else
3326 		mdb_printf("%16s %?p\n", "thread", panic_thread);
3327 
3328 	panicbuf = mdb_alloc(PANICBUFSIZE, UM_SLEEP);
3329 	pd = (panic_data_t *)panicbuf;
3330 
3331 	if (mdb_readsym(panicbuf, PANICBUFSIZE, "panicbuf") == -1 ||
3332 	    pd->pd_version != PANICBUFVERS) {
3333 		mdb_warn("failed to read 'panicbuf'");
3334 		mdb_free(panicbuf, PANICBUFSIZE);
3335 		return (DCMD_ERR);
3336 	}
3337 
3338 	mdb_printf("%16s %s\n", "message",  (char *)panicbuf + pd->pd_msgoff);
3339 
3340 	n = (pd->pd_msgoff - (sizeof (panic_data_t) -
3341 	    sizeof (panic_nv_t))) / sizeof (panic_nv_t);
3342 
3343 	for (i = 0; i < n; i++)
3344 		mdb_printf("%16s %?llx\n",
3345 		    pd->pd_nvdata[i].pnv_name, pd->pd_nvdata[i].pnv_value);
3346 
3347 	mdb_free(panicbuf, PANICBUFSIZE);
3348 	return (DCMD_OK);
3349 }
3350 
3351 static const mdb_dcmd_t dcmds[] = {
3352 
3353 	/* from genunix.c */
3354 	{ "addr2smap", ":[offset]", "translate address to smap", addr2smap },
3355 	{ "as2proc", ":", "convert as to proc_t address", as2proc },
3356 	{ "binding_hash_entry", ":", "print driver names hash table entry",
3357 		binding_hash_entry },
3358 	{ "callout", NULL, "print callout table", callout },
3359 	{ "class", NULL, "print process scheduler classes", class },
3360 	{ "cpuinfo", "?[-v]", "print CPUs and runnable threads", cpuinfo },
3361 	{ "did2thread", "? kt_did", "find kernel thread for this id",
3362 		did2thread },
3363 	{ "errorq", "?[-v]", "display kernel error queues", errorq },
3364 	{ "fd", ":[fd num]", "get a file pointer from an fd", fd },
3365 	{ "flipone", ":", "the vik_rev_level 2 special", flipone },
3366 	{ "lminfo", NULL, "print lock manager information", lminfo },
3367 	{ "ndi_event_hdl", "?", "print ndi_event_hdl", ndi_event_hdl },
3368 	{ "panicinfo", NULL, "print panic information", panicinfo },
3369 	{ "pid2proc", "?", "convert PID to proc_t address", pid2proc },
3370 	{ "pmap", ":[-q]", "print process memory map", pmap },
3371 	{ "project", NULL, "display kernel project(s)", project },
3372 	{ "ps", "[-fltzTP]", "list processes (and associated thr,lwp)", ps },
3373 	{ "pgrep", "[-n | -o] pattern", "pattern match against all processes",
3374 		pgrep },
3375 	{ "ptree", NULL, "print process tree", ptree },
3376 	{ "seg", ":", "print address space segment", seg },
3377 	{ "sysevent", "?[-sv]", "print sysevent pending or sent queue",
3378 		sysevent},
3379 	{ "sysevent_channel", "?", "print sysevent channel database",
3380 		sysevent_channel},
3381 	{ "sysevent_class_list", ":", "print sysevent class list",
3382 		sysevent_class_list},
3383 	{ "sysevent_subclass_list", ":",
3384 		"print sysevent subclass list", sysevent_subclass_list},
3385 	{ "system", NULL, "print contents of /etc/system file", sysfile },
3386 	{ "task", NULL, "display kernel task(s)", task },
3387 	{ "taskq_entry", ":", "display a taskq_ent_t", taskq_ent },
3388 	{ "vnode2path", ":[-F]", "vnode address to pathname", vnode2path },
3389 	{ "vnode2smap", ":[offset]", "translate vnode to smap", vnode2smap },
3390 	{ "whereopen", ":", "given a vnode, dumps procs which have it open",
3391 	    whereopen },
3392 
3393 	/* from zone.c */
3394 	{ "zone", "?", "display kernel zone(s)", zoneprt },
3395 	{ "zsd", ":[zsd key]", "lookup zsd value from a key", zsd },
3396 
3397 	/* from bio.c */
3398 	{ "bufpagefind", ":addr", "find page_t on buf_t list", bufpagefind },
3399 
3400 	/* from contract.c */
3401 	{ "contract", "?", "display a contract", cmd_contract },
3402 	{ "ctevent", ":", "display a contract event", cmd_ctevent },
3403 	{ "ctid", ":", "convert id to a contract pointer", cmd_ctid },
3404 
3405 	/* from cpupart.c */
3406 	{ "cpupart", "?", "print cpu partition info", cpupart },
3407 
3408 	/* from cyclic.c */
3409 	{ "cyccover", NULL, "dump cyclic coverage information", cyccover },
3410 	{ "cycid", "?", "dump a cyclic id", cycid },
3411 	{ "cycinfo", "?", "dump cyc_cpu info", cycinfo },
3412 	{ "cyclic", ":", "developer information", cyclic },
3413 	{ "cyctrace", "?", "dump cyclic trace buffer", cyctrace },
3414 
3415 	/* from devinfo.c */
3416 	{ "devbindings", "?[-qs] [device-name | major-num]",
3417 	    "print devinfo nodes bound to device-name or major-num",
3418 	    devbindings, devinfo_help },
3419 	{ "devinfo", ":[-qs]", "detailed devinfo of one node", devinfo,
3420 	    devinfo_help },
3421 	{ "devinfo_audit", ":[-v]", "devinfo configuration audit record",
3422 	    devinfo_audit },
3423 	{ "devinfo_audit_log", "?[-v]", "system wide devinfo configuration log",
3424 	    devinfo_audit_log },
3425 	{ "devinfo_audit_node", ":[-v]", "devinfo node configuration history",
3426 	    devinfo_audit_node },
3427 	{ "devinfo2driver", ":", "find driver name for this devinfo node",
3428 	    devinfo2driver },
3429 	{ "devnames", "?[-vm] [num]", "print devnames array", devnames },
3430 	{ "dev2major", "?<dev_t>", "convert dev_t to a major number",
3431 	    dev2major },
3432 	{ "dev2minor", "?<dev_t>", "convert dev_t to a minor number",
3433 	    dev2minor },
3434 	{ "devt", "?<dev_t>", "display a dev_t's major and minor numbers",
3435 	    devt },
3436 	{ "major2name", "?<major-num>", "convert major number to dev name",
3437 	    major2name },
3438 	{ "minornodes", ":", "given a devinfo node, print its minor nodes",
3439 	    minornodes },
3440 	{ "modctl2devinfo", ":", "given a modctl, list its devinfos",
3441 	    modctl2devinfo },
3442 	{ "name2major", "<dev-name>", "convert dev name to major number",
3443 	    name2major },
3444 	{ "prtconf", "?[-vpc]", "print devinfo tree", prtconf, prtconf_help },
3445 	{ "softstate", ":<instance>", "retrieve soft-state pointer",
3446 	    softstate },
3447 	{ "devinfo_fm", ":", "devinfo fault managment configuration",
3448 	    devinfo_fm },
3449 	{ "devinfo_fmce", ":", "devinfo fault managment cache entry",
3450 	    devinfo_fmce},
3451 
3452 	/* from findstack.c */
3453 	{ "findstack", ":[-v]", "find kernel thread stack", findstack },
3454 	{ "findstack_debug", NULL, "toggle findstack debugging",
3455 		findstack_debug },
3456 
3457 	/* from kgrep.c + genunix.c */
3458 	{ "kgrep", KGREP_USAGE, "search kernel as for a pointer", kgrep },
3459 
3460 	/* from kmem.c */
3461 	{ "allocdby", ":", "given a thread, print its allocated buffers",
3462 		allocdby },
3463 	{ "bufctl", ":[-vh] [-a addr] [-c caller] [-e earliest] [-l latest] "
3464 		"[-t thd]", "print or filter a bufctl", bufctl, bufctl_help },
3465 	{ "freedby", ":", "given a thread, print its freed buffers", freedby },
3466 	{ "kmalog", "?[ fail | slab ]",
3467 	    "display kmem transaction log and stack traces", kmalog },
3468 	{ "kmastat", NULL, "kernel memory allocator stats", kmastat },
3469 	{ "kmausers", "?[-ef] [cache ...]", "current medium and large users "
3470 		"of the kmem allocator", kmausers, kmausers_help },
3471 	{ "kmem_cache", "?", "print kernel memory caches", kmem_cache },
3472 	{ "kmem_debug", NULL, "toggle kmem dcmd/walk debugging", kmem_debug },
3473 	{ "kmem_log", "?[-b]", "dump kmem transaction log", kmem_log },
3474 	{ "kmem_verify", "?", "check integrity of kmem-managed memory",
3475 		kmem_verify },
3476 	{ "vmem", "?", "print a vmem_t", vmem },
3477 	{ "vmem_seg", ":[-sv] [-c caller] [-e earliest] [-l latest] "
3478 		"[-m minsize] [-M maxsize] [-t thread] [-T type]",
3479 		"print or filter a vmem_seg", vmem_seg, vmem_seg_help },
3480 	{ "whatis", ":[-abiv]", "given an address, return information", whatis,
3481 		whatis_help },
3482 	{ "whatthread", ":[-v]", "print threads whose stack contains the "
3483 		"given address", whatthread },
3484 
3485 	/* from ldi.c */
3486 	{ "ldi_handle", "?[-i]", "display a layered driver handle",
3487 	    ldi_handle, ldi_handle_help },
3488 	{ "ldi_ident", NULL, "display a layered driver identifier",
3489 	    ldi_ident, ldi_ident_help },
3490 
3491 	/* from leaky.c + leaky_subr.c */
3492 	{ "findleaks", FINDLEAKS_USAGE,
3493 	    "search for potential kernel memory leaks", findleaks,
3494 	    findleaks_help },
3495 
3496 	/* from lgrp.c */
3497 	{ "lgrp", "?[-q] [-p | -Pih]", "display an lgrp", lgrp},
3498 
3499 	/* from log.c */
3500 	{ "msgbuf", "?[-v]", "print most recent console messages", msgbuf },
3501 
3502 	/* from memory.c */
3503 	{ "page", "?", "display a summarized page_t", page },
3504 	{ "memstat", NULL, "display memory usage summary", memstat },
3505 	{ "memlist", "?[-iav]", "display a struct memlist", memlist },
3506 	{ "swapinfo", "?", "display a struct swapinfo", swapinfof },
3507 
3508 	/* from mmd.c */
3509 	{ "multidata", ":[-sv]", "display a summarized multidata_t",
3510 		multidata },
3511 	{ "pattbl", ":", "display a summarized multidata attribute table",
3512 		pattbl },
3513 	{ "pattr2multidata", ":", "print multidata pointer from pattr_t",
3514 		pattr2multidata },
3515 	{ "pdesc2slab", ":", "print pdesc slab pointer from pdesc_t",
3516 		pdesc2slab },
3517 	{ "pdesc_verify", ":", "verify integrity of a pdesc_t", pdesc_verify },
3518 	{ "slab2multidata", ":", "print multidata pointer from pdesc_slab_t",
3519 		slab2multidata },
3520 
3521 	/* from modhash.c */
3522 	{ "modhash", "?[-ceht] [-k key] [-v val] [-i index]",
3523 		"display information about one or all mod_hash structures",
3524 		modhash, modhash_help },
3525 	{ "modent", ":[-k | -v | -t type]",
3526 		"display information about a mod_hash_entry", modent,
3527 		modent_help },
3528 
3529 	/* from net.c */
3530 	{ "mi", ":[-p] [-d | -m]", "filter and display MI object or payload",
3531 		mi },
3532 	{ "netstat", "[-av] [-f inet | inet6 | unix] [-P tcp | udp]",
3533 		"show network statistics", netstat },
3534 	{ "sonode", "?[-f inet | inet6 | unix | #] "
3535 		"[-t stream | dgram | raw | #] [-p #]",
3536 		"filter and display sonode", sonode },
3537 
3538 	/* from nvpair.c */
3539 	{ NVPAIR_DCMD_NAME, NVPAIR_DCMD_USAGE, NVPAIR_DCMD_DESCR,
3540 		nvpair_print },
3541 
3542 	/* from rctl.c */
3543 	{ "rctl_dict", "?", "print systemwide default rctl definitions",
3544 		rctl_dict },
3545 	{ "rctl_list", ":[handle]", "print rctls for the given proc",
3546 		rctl_list },
3547 	{ "rctl", ":[handle]", "print a rctl_t, only if it matches the handle",
3548 		rctl },
3549 	{ "rctl_validate", ":[-v] [-n #]", "test resource control value "
3550 		"sequence", rctl_validate },
3551 
3552 	/* from sobj.c */
3553 	{ "rwlock", ":", "dump out a readers/writer lock", rwlock },
3554 	{ "mutex", ":[-f]", "dump out an adaptive or spin mutex", mutex,
3555 		mutex_help },
3556 	{ "sobj2ts", ":", "perform turnstile lookup on synch object", sobj2ts },
3557 	{ "wchaninfo", "?[-v]", "dump condition variable", wchaninfo },
3558 	{ "turnstile", "?", "display a turnstile", turnstile },
3559 
3560 	/* from stream.c */
3561 	{ "mblk", ":[-q|v] [-f|F flag] [-t|T type] [-l|L|B len] [-d dbaddr]",
3562 		"print an mblk", mblk_prt, mblk_help },
3563 	{ "mblk_verify", "?", "verify integrity of an mblk", mblk_verify },
3564 	{ "mblk2dblk", ":", "convert mblk_t address to dblk_t address",
3565 		mblk2dblk },
3566 	{ "q2otherq", ":", "print peer queue for a given queue", q2otherq },
3567 	{ "q2rdq", ":", "print read queue for a given queue", q2rdq },
3568 	{ "q2syncq", ":", "print syncq for a given queue", q2syncq },
3569 	{ "q2stream", ":", "print stream pointer for a given queue", q2stream },
3570 	{ "q2wrq", ":", "print write queue for a given queue", q2wrq },
3571 	{ "queue", ":[-q|v] [-m mod] [-f flag] [-F flag] [-s syncq_addr]",
3572 		"filter and display STREAM queue", queue, queue_help },
3573 	{ "stdata", ":[-q|v] [-f flag] [-F flag]",
3574 		"filter and display STREAM head", stdata, stdata_help },
3575 	{ "str2mate", ":", "print mate of this stream", str2mate },
3576 	{ "str2wrq", ":", "print write queue of this stream", str2wrq },
3577 	{ "stream", ":", "display STREAM", stream },
3578 	{ "strftevent", ":", "print STREAMS flow trace event", strftevent },
3579 	{ "syncq", ":[-q|v] [-f flag] [-F flag] [-t type] [-T type]",
3580 		"filter and display STREAM sync queue", syncq, syncq_help },
3581 	{ "syncq2q", ":", "print queue for a given syncq", syncq2q },
3582 
3583 	/* from thread.c */
3584 	{ "thread", "?[-bdfimps]", "display a summarized kthread_t", thread,
3585 		thread_help },
3586 	{ "threadlist", "?[-v [count]]",
3587 		"display threads and associated C stack traces", threadlist,
3588 		threadlist_help },
3589 
3590 	/* from tsd.c */
3591 	{ "tsd", ":-k key", "print tsd[key-1] for this thread", ttotsd },
3592 	{ "tsdtot", ":", "find thread with this tsd", tsdtot },
3593 
3594 	/*
3595 	 * typegraph does not work under kmdb, as it requires too much memory
3596 	 * for its internal data structures.
3597 	 */
3598 #ifndef _KMDB
3599 	/* from typegraph.c */
3600 	{ "findlocks", ":", "find locks held by specified thread", findlocks },
3601 	{ "findfalse", "?[-v]", "find potentially falsely shared structures",
3602 		findfalse },
3603 	{ "typegraph", NULL, "build type graph", typegraph },
3604 	{ "istype", ":type", "manually set object type", istype },
3605 	{ "notype", ":", "manually clear object type", notype },
3606 	{ "whattype", ":", "determine object type", whattype },
3607 #endif
3608 
3609 	/* from vfs.c */
3610 	{ "fsinfo", "?[-v]", "print mounted filesystems", fsinfo },
3611 	{ "pfiles", ":[-fp]", "print process file information", pfiles,
3612 		pfiles_help },
3613 
3614 	{ NULL }
3615 };
3616 
3617 static const mdb_walker_t walkers[] = {
3618 
3619 	/* from genunix.c */
3620 	{ "avl", "given any avl_tree_t *, forward walk all entries in tree",
3621 		avl_walk_init, avl_walk_step, avl_walk_fini },
3622 	{ "anon", "given an amp, list of anon structures",
3623 		anon_walk_init, anon_walk_step, anon_walk_fini },
3624 	{ "cpu", "walk cpu structures", cpu_walk_init, cpu_walk_step },
3625 	{ "errorq", "walk list of system error queues",
3626 		errorq_walk_init, errorq_walk_step, NULL },
3627 	{ "errorq_data", "walk pending error queue data buffers",
3628 		eqd_walk_init, eqd_walk_step, eqd_walk_fini },
3629 	{ "allfile", "given a proc pointer, list all file pointers",
3630 		file_walk_init, allfile_walk_step, file_walk_fini },
3631 	{ "file", "given a proc pointer, list of open file pointers",
3632 		file_walk_init, file_walk_step, file_walk_fini },
3633 	{ "lock_descriptor", "walk lock_descriptor_t structures",
3634 		ld_walk_init, ld_walk_step, NULL },
3635 	{ "lock_graph", "walk lock graph",
3636 		lg_walk_init, lg_walk_step, NULL },
3637 	{ "port", "given a proc pointer, list of created event ports",
3638 		port_walk_init, port_walk_step, NULL },
3639 	{ "portev", "given a port pointer, list of events in the queue",
3640 		portev_walk_init, portev_walk_step, portev_walk_fini },
3641 	{ "proc", "list of active proc_t structures",
3642 		proc_walk_init, proc_walk_step, proc_walk_fini },
3643 	{ "projects", "walk a list of kernel projects",
3644 		project_walk_init, project_walk_step, NULL },
3645 	{ "seg", "given an as, list of segments",
3646 		seg_walk_init, avl_walk_step, avl_walk_fini },
3647 	{ "sysevent_pend", "walk sysevent pending queue",
3648 		sysevent_pend_walk_init, sysevent_walk_step,
3649 		sysevent_walk_fini},
3650 	{ "sysevent_sent", "walk sysevent sent queue", sysevent_sent_walk_init,
3651 		sysevent_walk_step, sysevent_walk_fini},
3652 	{ "sysevent_channel", "walk sysevent channel subscriptions",
3653 		sysevent_channel_walk_init, sysevent_channel_walk_step,
3654 		sysevent_channel_walk_fini},
3655 	{ "sysevent_class_list", "walk sysevent subscription's class list",
3656 		sysevent_class_list_walk_init, sysevent_class_list_walk_step,
3657 		sysevent_class_list_walk_fini},
3658 	{ "sysevent_subclass_list",
3659 		"walk sysevent subscription's subclass list",
3660 		sysevent_subclass_list_walk_init,
3661 		sysevent_subclass_list_walk_step,
3662 		sysevent_subclass_list_walk_fini},
3663 	{ "task", "given a task pointer, walk its processes",
3664 		task_walk_init, task_walk_step, NULL },
3665 	{ "taskq_entry", "given a taskq_t*, list all taskq_ent_t in the list",
3666 		taskq_walk_init, taskq_walk_step, NULL, NULL },
3667 
3668 	/* from zone.c */
3669 	{ "zone", "walk a list of kernel zones",
3670 		zone_walk_init, zone_walk_step, NULL },
3671 	{ "zsd", "walk list of zsd entries for a zone",
3672 		zsd_walk_init, zsd_walk_step, NULL },
3673 
3674 	/* from bio.c */
3675 	{ "buf", "walk the bio buf hash",
3676 		buf_walk_init, buf_walk_step, buf_walk_fini },
3677 
3678 	/* from contract.c */
3679 	{ "contract", "walk all contracts, or those of the specified type",
3680 		ct_walk_init, generic_walk_step, NULL },
3681 	{ "ct_event", "walk events on a contract event queue",
3682 		ct_event_walk_init, generic_walk_step, NULL },
3683 	{ "ct_listener", "walk contract event queue listeners",
3684 		ct_listener_walk_init, generic_walk_step, NULL },
3685 
3686 	/* from cpupart.c */
3687 	{ "cpupart_cpulist", "given an cpupart_t, walk cpus in partition",
3688 		cpupart_cpulist_walk_init, cpupart_cpulist_walk_step,
3689 		NULL },
3690 	{ "cpupart_walk", "walk the set of cpu partitions",
3691 		cpupart_walk_init, cpupart_walk_step, NULL },
3692 
3693 	/* from ctxop.c */
3694 	{ "ctxop", "walk list of context ops on a thread",
3695 		ctxop_walk_init, ctxop_walk_step, ctxop_walk_fini },
3696 
3697 	/* from cyclic.c */
3698 	{ "cyccpu", "walk per-CPU cyc_cpu structures",
3699 		cyccpu_walk_init, cyccpu_walk_step, NULL },
3700 	{ "cycomni", "for an omnipresent cyclic, walk cyc_omni_cpu list",
3701 		cycomni_walk_init, cycomni_walk_step, NULL },
3702 	{ "cyctrace", "walk cyclic trace buffer",
3703 		cyctrace_walk_init, cyctrace_walk_step, cyctrace_walk_fini },
3704 
3705 	/* from devinfo.c */
3706 	{ "binding_hash", "walk all entries in binding hash table",
3707 		binding_hash_walk_init, binding_hash_walk_step, NULL },
3708 	{ "devinfo", "walk devinfo tree or subtree",
3709 		devinfo_walk_init, devinfo_walk_step, devinfo_walk_fini },
3710 	{ "devinfo_audit_log", "walk devinfo audit system-wide log",
3711 		devinfo_audit_log_walk_init, devinfo_audit_log_walk_step,
3712 		devinfo_audit_log_walk_fini},
3713 	{ "devinfo_audit_node", "walk per-devinfo audit history",
3714 		devinfo_audit_node_walk_init, devinfo_audit_node_walk_step,
3715 		devinfo_audit_node_walk_fini},
3716 	{ "devinfo_children", "walk children of devinfo node",
3717 		devinfo_children_walk_init, devinfo_children_walk_step,
3718 		devinfo_children_walk_fini },
3719 	{ "devinfo_parents", "walk ancestors of devinfo node",
3720 		devinfo_parents_walk_init, devinfo_parents_walk_step,
3721 		devinfo_parents_walk_fini },
3722 	{ "devinfo_siblings", "walk siblings of devinfo node",
3723 		devinfo_siblings_walk_init, devinfo_siblings_walk_step, NULL },
3724 	{ "devi_next", "walk devinfo list",
3725 		NULL, devi_next_walk_step, NULL },
3726 	{ "devnames", "walk devnames array",
3727 		devnames_walk_init, devnames_walk_step, devnames_walk_fini },
3728 	{ "minornode", "given a devinfo node, walk minor nodes",
3729 		minornode_walk_init, minornode_walk_step, NULL },
3730 	{ "softstate",
3731 		"given an i_ddi_soft_state*, list all in-use driver stateps",
3732 		soft_state_walk_init, soft_state_walk_step,
3733 		NULL, NULL },
3734 	{ "softstate_all",
3735 		"given an i_ddi_soft_state*, list all driver stateps",
3736 		soft_state_walk_init, soft_state_all_walk_step,
3737 		NULL, NULL },
3738 	{ "devinfo_fmc",
3739 		"walk a fault management handle cache active list",
3740 		devinfo_fmc_walk_init, devinfo_fmc_walk_step, NULL },
3741 
3742 	/* from kmem.c */
3743 	{ "allocdby", "given a thread, walk its allocated bufctls",
3744 		allocdby_walk_init, allocdby_walk_step, allocdby_walk_fini },
3745 	{ "bufctl", "walk a kmem cache's bufctls",
3746 		bufctl_walk_init, kmem_walk_step, kmem_walk_fini },
3747 	{ "bufctl_history", "walk the available history of a bufctl",
3748 		bufctl_history_walk_init, bufctl_history_walk_step,
3749 		bufctl_history_walk_fini },
3750 	{ "freedby", "given a thread, walk its freed bufctls",
3751 		freedby_walk_init, allocdby_walk_step, allocdby_walk_fini },
3752 	{ "freectl", "walk a kmem cache's free bufctls",
3753 		freectl_walk_init, kmem_walk_step, kmem_walk_fini },
3754 	{ "freectl_constructed", "walk a kmem cache's constructed free bufctls",
3755 		freectl_constructed_walk_init, kmem_walk_step, kmem_walk_fini },
3756 	{ "freemem", "walk a kmem cache's free memory",
3757 		freemem_walk_init, kmem_walk_step, kmem_walk_fini },
3758 	{ "freemem_constructed", "walk a kmem cache's constructed free memory",
3759 		freemem_constructed_walk_init, kmem_walk_step, kmem_walk_fini },
3760 	{ "kmem", "walk a kmem cache",
3761 		kmem_walk_init, kmem_walk_step, kmem_walk_fini },
3762 	{ "kmem_cpu_cache", "given a kmem cache, walk its per-CPU caches",
3763 		kmem_cpu_cache_walk_init, kmem_cpu_cache_walk_step, NULL },
3764 	{ "kmem_hash", "given a kmem cache, walk its allocated hash table",
3765 		kmem_hash_walk_init, kmem_hash_walk_step, kmem_hash_walk_fini },
3766 	{ "kmem_log", "walk the kmem transaction log",
3767 		kmem_log_walk_init, kmem_log_walk_step, kmem_log_walk_fini },
3768 	{ "kmem_slab", "given a kmem cache, walk its slabs",
3769 		kmem_slab_walk_init, kmem_slab_walk_step, NULL },
3770 	{ "kmem_slab_partial",
3771 	    "given a kmem cache, walk its partially allocated slabs (min 1)",
3772 		kmem_slab_walk_partial_init, kmem_slab_walk_step, NULL },
3773 	{ "vmem", "walk vmem structures in pre-fix, depth-first order",
3774 		vmem_walk_init, vmem_walk_step, vmem_walk_fini },
3775 	{ "vmem_alloc", "given a vmem_t, walk its allocated vmem_segs",
3776 		vmem_alloc_walk_init, vmem_seg_walk_step, vmem_seg_walk_fini },
3777 	{ "vmem_free", "given a vmem_t, walk its free vmem_segs",
3778 		vmem_free_walk_init, vmem_seg_walk_step, vmem_seg_walk_fini },
3779 	{ "vmem_postfix", "walk vmem structures in post-fix, depth-first order",
3780 		vmem_walk_init, vmem_postfix_walk_step, vmem_walk_fini },
3781 	{ "vmem_seg", "given a vmem_t, walk all of its vmem_segs",
3782 		vmem_seg_walk_init, vmem_seg_walk_step, vmem_seg_walk_fini },
3783 	{ "vmem_span", "given a vmem_t, walk its spanning vmem_segs",
3784 		vmem_span_walk_init, vmem_seg_walk_step, vmem_seg_walk_fini },
3785 
3786 	/* from ldi.c */
3787 	{ "ldi_handle", "walk the layered driver handle hash",
3788 		ldi_handle_walk_init, ldi_handle_walk_step, NULL },
3789 	{ "ldi_ident", "walk the layered driver identifier hash",
3790 		ldi_ident_walk_init, ldi_ident_walk_step, NULL },
3791 
3792 	/* from leaky.c + leaky_subr.c */
3793 	{ "leak", "given a leaked bufctl or vmem_seg, find leaks w/ same "
3794 	    "stack trace",
3795 		leaky_walk_init, leaky_walk_step, leaky_walk_fini },
3796 	{ "leakbuf", "given a leaked bufctl or vmem_seg, walk buffers for "
3797 	    "leaks w/ same stack trace",
3798 		leaky_walk_init, leaky_buf_walk_step, leaky_walk_fini },
3799 
3800 	/* from lgrp.c */
3801 	{ "lgrp_cpulist", "given an lgrp, walk cpus",
3802 		lgrp_cpulist_walk_init, lgrp_cpulist_walk_step,
3803 		NULL },
3804 	{ "lgrptbl", "walk the lgrp table",
3805 		lgrp_walk_init, lgrp_walk_step, NULL },
3806 
3807 	/* from list.c */
3808 	{ "list", "walk a linked list",
3809 		list_walk_init, list_walk_step, list_walk_fini },
3810 
3811 	/* from memory.c */
3812 	{ "page", "walk all pages, or those from the specified vnode",
3813 		page_walk_init, page_walk_step, page_walk_fini },
3814 	{ "memlist", "walk specified memlist",
3815 		NULL, memlist_walk_step, NULL },
3816 	{ "swapinfo", "walk swapinfo structures",
3817 		swap_walk_init, swap_walk_step, NULL },
3818 
3819 	/* from mmd.c */
3820 	{ "pattr", "walk pattr_t structures", pattr_walk_init,
3821 		mmdq_walk_step, mmdq_walk_fini },
3822 	{ "pdesc", "walk pdesc_t structures",
3823 		pdesc_walk_init, mmdq_walk_step, mmdq_walk_fini },
3824 	{ "pdesc_slab", "walk pdesc_slab_t structures",
3825 		pdesc_slab_walk_init, mmdq_walk_step, mmdq_walk_fini },
3826 
3827 	/* from modhash.c */
3828 	{ "modhash", "walk list of mod_hash structures", modhash_walk_init,
3829 		modhash_walk_step, NULL },
3830 	{ "modent", "walk list of entries in a given mod_hash",
3831 		modent_walk_init, modent_walk_step, modent_walk_fini },
3832 	{ "modchain", "walk list of entries in a given mod_hash_entry",
3833 		NULL, modchain_walk_step, NULL },
3834 
3835 	/* from net.c */
3836 	{ "ar", "walk ar_t structures using MI",
3837 		mi_payload_walk_init, mi_payload_walk_step,
3838 		mi_payload_walk_fini, &mi_ar_arg },
3839 	{ "icmp", "walk ICMP control structures using MI",
3840 		mi_payload_walk_init, mi_payload_walk_step,
3841 		mi_payload_walk_fini, &mi_icmp_arg },
3842 	{ "ill", "walk ill_t structures using MI",
3843 		mi_payload_walk_init, mi_payload_walk_step,
3844 		mi_payload_walk_fini, &mi_ill_arg },
3845 	{ "mi", "given a MI_O, walk the MI",
3846 		mi_walk_init, mi_walk_step, mi_walk_fini, NULL },
3847 	{ "sonode", "given a sonode, walk its children",
3848 		sonode_walk_init, sonode_walk_step, sonode_walk_fini, NULL },
3849 	{ "udp", "walk UDP connections using MI",
3850 		mi_payload_walk_init, mi_payload_walk_step,
3851 		mi_payload_walk_fini, &mi_udp_arg },
3852 
3853 	/* from nvpair.c */
3854 	{ NVPAIR_WALKER_NAME, NVPAIR_WALKER_DESCR,
3855 		nvpair_walk_init, nvpair_walk_step, NULL },
3856 
3857 	/* from rctl.c */
3858 	{ "rctl_dict_list", "walk all rctl_dict_entry_t's from rctl_lists",
3859 		rctl_dict_walk_init, rctl_dict_walk_step, NULL },
3860 	{ "rctl_set", "given a rctl_set, walk all rctls", rctl_set_walk_init,
3861 		rctl_set_walk_step, NULL },
3862 	{ "rctl_val", "given a rctl_t, walk all rctl_val entries associated",
3863 		rctl_val_walk_init, rctl_val_walk_step },
3864 
3865 	/* from sobj.c */
3866 	{ "blocked", "walk threads blocked on a given sobj",
3867 		blocked_walk_init, blocked_walk_step, NULL },
3868 	{ "wchan", "given a wchan, list of blocked threads",
3869 		wchan_walk_init, wchan_walk_step, wchan_walk_fini },
3870 
3871 	/* from stream.c */
3872 	{ "b_cont", "walk mblk_t list using b_cont",
3873 		mblk_walk_init, b_cont_step, mblk_walk_fini },
3874 	{ "b_next", "walk mblk_t list using b_next",
3875 		mblk_walk_init, b_next_step, mblk_walk_fini },
3876 	{ "qlink", "walk queue_t list using q_link",
3877 		queue_walk_init, queue_link_step, queue_walk_fini },
3878 	{ "qnext", "walk queue_t list using q_next",
3879 		queue_walk_init, queue_next_step, queue_walk_fini },
3880 	{ "strftblk", "given a dblk_t, walk STREAMS flow trace event list",
3881 		strftblk_walk_init, strftblk_step, strftblk_walk_fini },
3882 	{ "readq", "walk read queue side of stdata",
3883 		str_walk_init, strr_walk_step, str_walk_fini },
3884 	{ "writeq", "walk write queue side of stdata",
3885 		str_walk_init, strw_walk_step, str_walk_fini },
3886 
3887 	/* from thread.c */
3888 	{ "deathrow", "walk threads on both lwp_ and thread_deathrow",
3889 		deathrow_walk_init, deathrow_walk_step, NULL },
3890 	{ "cpu_dispq", "given a cpu_t, walk threads in dispatcher queues",
3891 		cpu_dispq_walk_init, dispq_walk_step, dispq_walk_fini },
3892 	{ "cpupart_dispq",
3893 		"given a cpupart_t, walk threads in dispatcher queues",
3894 		cpupart_dispq_walk_init, dispq_walk_step, dispq_walk_fini },
3895 	{ "lwp_deathrow", "walk lwp_deathrow",
3896 		lwp_deathrow_walk_init, deathrow_walk_step, NULL },
3897 	{ "thread", "global or per-process kthread_t structures",
3898 		thread_walk_init, thread_walk_step, thread_walk_fini },
3899 	{ "thread_deathrow", "walk threads on thread_deathrow",
3900 		thread_deathrow_walk_init, deathrow_walk_step, NULL },
3901 
3902 	/* from tsd.c */
3903 	{ "tsd", "walk list of thread-specific data",
3904 		tsd_walk_init, tsd_walk_step, tsd_walk_fini },
3905 
3906 	/*
3907 	 * typegraph does not work under kmdb, as it requires too much memory
3908 	 * for its internal data structures.
3909 	 */
3910 #ifndef _KMDB
3911 	/* from typegraph.c */
3912 	{ "typeconflict", "walk buffers with conflicting type inferences",
3913 		typegraph_walk_init, typeconflict_walk_step },
3914 	{ "typeunknown", "walk buffers with unknown types",
3915 		typegraph_walk_init, typeunknown_walk_step },
3916 #endif
3917 
3918 	/* from vfs.c */
3919 	{ "vfs", "walk file system list",
3920 		vfs_walk_init, vfs_walk_step },
3921 	{ NULL }
3922 };
3923 
3924 static const mdb_modinfo_t modinfo = { MDB_API_VERSION, dcmds, walkers };
3925 
3926 const mdb_modinfo_t *
3927 _mdb_init(void)
3928 {
3929 	if (mdb_readvar(&devinfo_root, "top_devinfo") == -1) {
3930 		mdb_warn("failed to read 'top_devinfo'");
3931 		return (NULL);
3932 	}
3933 
3934 	if (findstack_init() != DCMD_OK)
3935 		return (NULL);
3936 
3937 	kmem_init();
3938 
3939 	return (&modinfo);
3940 }
3941 
3942 void
3943 _mdb_fini(void)
3944 {
3945 	/*
3946 	 * Force ::findleaks to let go any cached memory
3947 	 */
3948 	leaky_cleanup(1);
3949 }
3950