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