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