xref: /onnv-gate/usr/src/cmd/mdb/common/modules/dtrace/dtrace.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate  * explicitly define DTRACE_ERRDEBUG to pull in definition of dtrace_errhash_t
31*0Sstevel@tonic-gate  * explicitly define _STDARG_H to avoid stdarg.h/varargs.h u/k defn conflict
32*0Sstevel@tonic-gate  */
33*0Sstevel@tonic-gate #define	DTRACE_ERRDEBUG
34*0Sstevel@tonic-gate #define	_STDARG_H
35*0Sstevel@tonic-gate 
36*0Sstevel@tonic-gate #include <mdb/mdb_param.h>
37*0Sstevel@tonic-gate #include <mdb/mdb_modapi.h>
38*0Sstevel@tonic-gate #include <mdb/mdb_ks.h>
39*0Sstevel@tonic-gate #include <sys/dtrace_impl.h>
40*0Sstevel@tonic-gate #include <sys/vmem_impl.h>
41*0Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
42*0Sstevel@tonic-gate #include <sys/sysmacros.h>
43*0Sstevel@tonic-gate #include <sys/kobj.h>
44*0Sstevel@tonic-gate #include <dtrace.h>
45*0Sstevel@tonic-gate #include <alloca.h>
46*0Sstevel@tonic-gate #include <ctype.h>
47*0Sstevel@tonic-gate #include <errno.h>
48*0Sstevel@tonic-gate #include <math.h>
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate #ifdef _LP64
51*0Sstevel@tonic-gate #define	DIFO_ADDRWIDTH		11
52*0Sstevel@tonic-gate #else
53*0Sstevel@tonic-gate #define	DIFO_ADDRWIDTH		8
54*0Sstevel@tonic-gate #endif
55*0Sstevel@tonic-gate 
56*0Sstevel@tonic-gate int dof_sec(uintptr_t, uint_t, int, const mdb_arg_t *);
57*0Sstevel@tonic-gate 
58*0Sstevel@tonic-gate /*ARGSUSED*/
59*0Sstevel@tonic-gate static void
60*0Sstevel@tonic-gate dis_log(const dtrace_difo_t *dp, const char *name, dif_instr_t instr)
61*0Sstevel@tonic-gate {
62*0Sstevel@tonic-gate 	mdb_printf("%-4s %%r%u, %%r%u, %%r%u", name,
63*0Sstevel@tonic-gate 	    DIF_INSTR_R1(instr), DIF_INSTR_R2(instr), DIF_INSTR_RD(instr));
64*0Sstevel@tonic-gate }
65*0Sstevel@tonic-gate 
66*0Sstevel@tonic-gate /*ARGSUSED*/
67*0Sstevel@tonic-gate static void
68*0Sstevel@tonic-gate dis_branch(const dtrace_difo_t *dp, const char *name, dif_instr_t instr)
69*0Sstevel@tonic-gate {
70*0Sstevel@tonic-gate 	mdb_printf("%-4s %u", name, DIF_INSTR_LABEL(instr));
71*0Sstevel@tonic-gate }
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate /*ARGSUSED*/
74*0Sstevel@tonic-gate static void
75*0Sstevel@tonic-gate dis_load(const dtrace_difo_t *dp, const char *name, dif_instr_t instr)
76*0Sstevel@tonic-gate {
77*0Sstevel@tonic-gate 	mdb_printf("%-4s [%%r%u], %%r%u", name,
78*0Sstevel@tonic-gate 	    DIF_INSTR_R1(instr), DIF_INSTR_RD(instr));
79*0Sstevel@tonic-gate }
80*0Sstevel@tonic-gate 
81*0Sstevel@tonic-gate /*ARGSUSED*/
82*0Sstevel@tonic-gate static void
83*0Sstevel@tonic-gate dis_store(const dtrace_difo_t *dp, const char *name, dif_instr_t instr)
84*0Sstevel@tonic-gate {
85*0Sstevel@tonic-gate 	mdb_printf("%-4s %%r%u, [%%r%u]", name,
86*0Sstevel@tonic-gate 	    DIF_INSTR_R1(instr), DIF_INSTR_RD(instr));
87*0Sstevel@tonic-gate }
88*0Sstevel@tonic-gate 
89*0Sstevel@tonic-gate /*ARGSUSED*/
90*0Sstevel@tonic-gate static void
91*0Sstevel@tonic-gate dis_str(const dtrace_difo_t *dp, const char *name, dif_instr_t instr)
92*0Sstevel@tonic-gate {
93*0Sstevel@tonic-gate 	mdb_printf("%s", name);
94*0Sstevel@tonic-gate }
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate /*ARGSUSED*/
97*0Sstevel@tonic-gate static void
98*0Sstevel@tonic-gate dis_r1rd(const dtrace_difo_t *dp, const char *name, dif_instr_t instr)
99*0Sstevel@tonic-gate {
100*0Sstevel@tonic-gate 	mdb_printf("%-4s %%r%u, %%r%u", name,
101*0Sstevel@tonic-gate 	    DIF_INSTR_R1(instr), DIF_INSTR_RD(instr));
102*0Sstevel@tonic-gate }
103*0Sstevel@tonic-gate 
104*0Sstevel@tonic-gate /*ARGSUSED*/
105*0Sstevel@tonic-gate static void
106*0Sstevel@tonic-gate dis_cmp(const dtrace_difo_t *dp, const char *name, dif_instr_t instr)
107*0Sstevel@tonic-gate {
108*0Sstevel@tonic-gate 	mdb_printf("%-4s %%r%u, %%r%u", name,
109*0Sstevel@tonic-gate 	    DIF_INSTR_R1(instr), DIF_INSTR_R2(instr));
110*0Sstevel@tonic-gate }
111*0Sstevel@tonic-gate 
112*0Sstevel@tonic-gate /*ARGSUSED*/
113*0Sstevel@tonic-gate static void
114*0Sstevel@tonic-gate dis_tst(const dtrace_difo_t *dp, const char *name, dif_instr_t instr)
115*0Sstevel@tonic-gate {
116*0Sstevel@tonic-gate 	mdb_printf("%-4s %%r%u", name, DIF_INSTR_R1(instr));
117*0Sstevel@tonic-gate }
118*0Sstevel@tonic-gate 
119*0Sstevel@tonic-gate static const char *
120*0Sstevel@tonic-gate dis_varname(const dtrace_difo_t *dp, uint_t id, uint_t scope)
121*0Sstevel@tonic-gate {
122*0Sstevel@tonic-gate 	dtrace_difv_t *dvp;
123*0Sstevel@tonic-gate 	size_t varsize;
124*0Sstevel@tonic-gate 	caddr_t addr = NULL, str;
125*0Sstevel@tonic-gate 	uint_t i;
126*0Sstevel@tonic-gate 
127*0Sstevel@tonic-gate 	if (dp == NULL)
128*0Sstevel@tonic-gate 		return (NULL);
129*0Sstevel@tonic-gate 
130*0Sstevel@tonic-gate 	varsize = sizeof (dtrace_difv_t) * dp->dtdo_varlen;
131*0Sstevel@tonic-gate 	dvp = mdb_alloc(varsize, UM_SLEEP);
132*0Sstevel@tonic-gate 
133*0Sstevel@tonic-gate 	if (mdb_vread(dvp, varsize, (uintptr_t)dp->dtdo_vartab) == -1) {
134*0Sstevel@tonic-gate 		mdb_free(dvp, varsize);
135*0Sstevel@tonic-gate 		return ("<unreadable>");
136*0Sstevel@tonic-gate 	}
137*0Sstevel@tonic-gate 
138*0Sstevel@tonic-gate 	for (i = 0; i < dp->dtdo_varlen; i++) {
139*0Sstevel@tonic-gate 		if (dvp[i].dtdv_id == id && dvp[i].dtdv_scope == scope) {
140*0Sstevel@tonic-gate 			if (dvp[i].dtdv_name < dp->dtdo_strlen)
141*0Sstevel@tonic-gate 				addr = dp->dtdo_strtab + dvp[i].dtdv_name;
142*0Sstevel@tonic-gate 			break;
143*0Sstevel@tonic-gate 		}
144*0Sstevel@tonic-gate 	}
145*0Sstevel@tonic-gate 
146*0Sstevel@tonic-gate 	mdb_free(dvp, varsize);
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate 	if (addr == NULL)
149*0Sstevel@tonic-gate 		return (NULL);
150*0Sstevel@tonic-gate 
151*0Sstevel@tonic-gate 	str = mdb_zalloc(dp->dtdo_strlen + 1, UM_SLEEP | UM_GC);
152*0Sstevel@tonic-gate 
153*0Sstevel@tonic-gate 	for (i = 0; i == 0 || str[i - 1] != '\0'; i++, addr++) {
154*0Sstevel@tonic-gate 		if (mdb_vread(&str[i], sizeof (char), (uintptr_t)addr) == -1)
155*0Sstevel@tonic-gate 			return ("<unreadable>");
156*0Sstevel@tonic-gate 	}
157*0Sstevel@tonic-gate 
158*0Sstevel@tonic-gate 	return (str);
159*0Sstevel@tonic-gate }
160*0Sstevel@tonic-gate 
161*0Sstevel@tonic-gate static uint_t
162*0Sstevel@tonic-gate dis_scope(const char *name)
163*0Sstevel@tonic-gate {
164*0Sstevel@tonic-gate 	switch (name[2]) {
165*0Sstevel@tonic-gate 	case 'l': return (DIFV_SCOPE_LOCAL);
166*0Sstevel@tonic-gate 	case 't': return (DIFV_SCOPE_THREAD);
167*0Sstevel@tonic-gate 	case 'g': return (DIFV_SCOPE_GLOBAL);
168*0Sstevel@tonic-gate 	default: return (-1u);
169*0Sstevel@tonic-gate 	}
170*0Sstevel@tonic-gate }
171*0Sstevel@tonic-gate 
172*0Sstevel@tonic-gate static void
173*0Sstevel@tonic-gate dis_lda(const dtrace_difo_t *dp, const char *name, dif_instr_t instr)
174*0Sstevel@tonic-gate {
175*0Sstevel@tonic-gate 	uint_t var = DIF_INSTR_R1(instr);
176*0Sstevel@tonic-gate 	const char *vname;
177*0Sstevel@tonic-gate 
178*0Sstevel@tonic-gate 	mdb_printf("%-4s DIF_VAR(%x), %%r%u, %%r%u", name,
179*0Sstevel@tonic-gate 	    var, DIF_INSTR_R2(instr), DIF_INSTR_RD(instr));
180*0Sstevel@tonic-gate 
181*0Sstevel@tonic-gate 	if ((vname = dis_varname(dp, var, dis_scope(name))) != NULL)
182*0Sstevel@tonic-gate 		mdb_printf("\t\t! %s", vname);
183*0Sstevel@tonic-gate }
184*0Sstevel@tonic-gate 
185*0Sstevel@tonic-gate static void
186*0Sstevel@tonic-gate dis_ldv(const dtrace_difo_t *dp, const char *name, dif_instr_t instr)
187*0Sstevel@tonic-gate {
188*0Sstevel@tonic-gate 	uint_t var = DIF_INSTR_VAR(instr);
189*0Sstevel@tonic-gate 	const char *vname;
190*0Sstevel@tonic-gate 
191*0Sstevel@tonic-gate 	mdb_printf("%-4s DIF_VAR(%x), %%r%u", name, var, DIF_INSTR_RD(instr));
192*0Sstevel@tonic-gate 
193*0Sstevel@tonic-gate 	if ((vname = dis_varname(dp, var, dis_scope(name))) != NULL)
194*0Sstevel@tonic-gate 		mdb_printf("\t\t! %s", vname);
195*0Sstevel@tonic-gate }
196*0Sstevel@tonic-gate 
197*0Sstevel@tonic-gate static void
198*0Sstevel@tonic-gate dis_stv(const dtrace_difo_t *dp, const char *name, dif_instr_t instr)
199*0Sstevel@tonic-gate {
200*0Sstevel@tonic-gate 	uint_t var = DIF_INSTR_VAR(instr);
201*0Sstevel@tonic-gate 	const char *vname;
202*0Sstevel@tonic-gate 
203*0Sstevel@tonic-gate 	mdb_printf("%-4s %%r%u, DIF_VAR(%x)", name, DIF_INSTR_RS(instr), var);
204*0Sstevel@tonic-gate 
205*0Sstevel@tonic-gate 	if ((vname = dis_varname(dp, var, dis_scope(name))) != NULL)
206*0Sstevel@tonic-gate 		mdb_printf("\t\t! %s", vname);
207*0Sstevel@tonic-gate }
208*0Sstevel@tonic-gate 
209*0Sstevel@tonic-gate static void
210*0Sstevel@tonic-gate dis_setx(const dtrace_difo_t *dp, const char *name, dif_instr_t instr)
211*0Sstevel@tonic-gate {
212*0Sstevel@tonic-gate 	uint_t intptr = DIF_INSTR_INTEGER(instr);
213*0Sstevel@tonic-gate 
214*0Sstevel@tonic-gate 	mdb_printf("%-4s DIF_INTEGER[%u], %%r%u", name,
215*0Sstevel@tonic-gate 	    intptr, DIF_INSTR_RD(instr));
216*0Sstevel@tonic-gate 
217*0Sstevel@tonic-gate 	if (dp != NULL && intptr < dp->dtdo_intlen) {
218*0Sstevel@tonic-gate 		uint64_t *ip = mdb_alloc(dp->dtdo_intlen *
219*0Sstevel@tonic-gate 		    sizeof (uint64_t), UM_SLEEP | UM_GC);
220*0Sstevel@tonic-gate 
221*0Sstevel@tonic-gate 		if (mdb_vread(ip, dp->dtdo_intlen * sizeof (uint64_t),
222*0Sstevel@tonic-gate 		    (uintptr_t)dp->dtdo_inttab) == -1)
223*0Sstevel@tonic-gate 			mdb_warn("failed to read data at %p", dp->dtdo_inttab);
224*0Sstevel@tonic-gate 		else
225*0Sstevel@tonic-gate 			mdb_printf("\t\t! 0x%llx", ip[intptr]);
226*0Sstevel@tonic-gate 	}
227*0Sstevel@tonic-gate }
228*0Sstevel@tonic-gate 
229*0Sstevel@tonic-gate static void
230*0Sstevel@tonic-gate dis_sets(const dtrace_difo_t *dp, const char *name, dif_instr_t instr)
231*0Sstevel@tonic-gate {
232*0Sstevel@tonic-gate 	uint_t strptr = DIF_INSTR_STRING(instr);
233*0Sstevel@tonic-gate 
234*0Sstevel@tonic-gate 	mdb_printf("%-4s DIF_STRING[%u], %%r%u", name,
235*0Sstevel@tonic-gate 	    strptr, DIF_INSTR_RD(instr));
236*0Sstevel@tonic-gate 
237*0Sstevel@tonic-gate 	if (dp != NULL && strptr < dp->dtdo_strlen) {
238*0Sstevel@tonic-gate 		char *str = mdb_alloc(dp->dtdo_strlen, UM_SLEEP | UM_GC);
239*0Sstevel@tonic-gate 
240*0Sstevel@tonic-gate 		if (mdb_vread(str, dp->dtdo_strlen,
241*0Sstevel@tonic-gate 		    (uintptr_t)dp->dtdo_strtab) == -1)
242*0Sstevel@tonic-gate 			mdb_warn("failed to read data at %p", dp->dtdo_strtab);
243*0Sstevel@tonic-gate 		else
244*0Sstevel@tonic-gate 			mdb_printf("\t\t! \"%s\"", str + strptr);
245*0Sstevel@tonic-gate 	}
246*0Sstevel@tonic-gate }
247*0Sstevel@tonic-gate 
248*0Sstevel@tonic-gate /*ARGSUSED*/
249*0Sstevel@tonic-gate static void
250*0Sstevel@tonic-gate dis_ret(const dtrace_difo_t *dp, const char *name, dif_instr_t instr)
251*0Sstevel@tonic-gate {
252*0Sstevel@tonic-gate 	mdb_printf("%-4s %%r%u", name, DIF_INSTR_RD(instr));
253*0Sstevel@tonic-gate }
254*0Sstevel@tonic-gate 
255*0Sstevel@tonic-gate /*ARGSUSED*/
256*0Sstevel@tonic-gate static void
257*0Sstevel@tonic-gate dis_call(const dtrace_difo_t *dp, const char *name, dif_instr_t instr)
258*0Sstevel@tonic-gate {
259*0Sstevel@tonic-gate 	static struct {
260*0Sstevel@tonic-gate 		const char *name;
261*0Sstevel@tonic-gate 		int subr;
262*0Sstevel@tonic-gate 	} snames[] = {
263*0Sstevel@tonic-gate 		{ "rand",			DIF_SUBR_RAND },
264*0Sstevel@tonic-gate 		{ "mutex_owned",		DIF_SUBR_MUTEX_OWNED },
265*0Sstevel@tonic-gate 		{ "mutex_owner",		DIF_SUBR_MUTEX_OWNER },
266*0Sstevel@tonic-gate 		{ "mutex_type_adaptive",	DIF_SUBR_MUTEX_TYPE_ADAPTIVE },
267*0Sstevel@tonic-gate 		{ "mutex_type_spin",		DIF_SUBR_MUTEX_TYPE_SPIN },
268*0Sstevel@tonic-gate 		{ "rw_read_held",		DIF_SUBR_RW_READ_HELD },
269*0Sstevel@tonic-gate 		{ "rw_write_held",		DIF_SUBR_RW_WRITE_HELD },
270*0Sstevel@tonic-gate 		{ "rw_iswriter",		DIF_SUBR_RW_ISWRITER },
271*0Sstevel@tonic-gate 		{ "copyin",			DIF_SUBR_COPYIN },
272*0Sstevel@tonic-gate 		{ "copyinstr",			DIF_SUBR_COPYINSTR },
273*0Sstevel@tonic-gate 		{ "speculation",		DIF_SUBR_SPECULATION },
274*0Sstevel@tonic-gate 		{ "progenyof",			DIF_SUBR_PROGENYOF },
275*0Sstevel@tonic-gate 		{ "strlen",			DIF_SUBR_STRLEN },
276*0Sstevel@tonic-gate 		{ "copyout",			DIF_SUBR_COPYOUT },
277*0Sstevel@tonic-gate 		{ "copyoutstr",			DIF_SUBR_COPYOUTSTR },
278*0Sstevel@tonic-gate 		{ "alloca",			DIF_SUBR_ALLOCA },
279*0Sstevel@tonic-gate 		{ "bcopy",			DIF_SUBR_BCOPY },
280*0Sstevel@tonic-gate 		{ "copyinto",			DIF_SUBR_COPYINTO },
281*0Sstevel@tonic-gate 		{ "msgdsize",			DIF_SUBR_MSGDSIZE },
282*0Sstevel@tonic-gate 		{ "msgsize",			DIF_SUBR_MSGSIZE },
283*0Sstevel@tonic-gate 		{ "getmajor",			DIF_SUBR_GETMAJOR },
284*0Sstevel@tonic-gate 		{ "getminor",			DIF_SUBR_GETMINOR },
285*0Sstevel@tonic-gate 		{ "ddi_pathname",		DIF_SUBR_DDI_PATHNAME },
286*0Sstevel@tonic-gate 		{ "strjoin",			DIF_SUBR_STRJOIN },
287*0Sstevel@tonic-gate 		{ "lltostr",			DIF_SUBR_LLTOSTR },
288*0Sstevel@tonic-gate 		{ "basename",			DIF_SUBR_BASENAME },
289*0Sstevel@tonic-gate 		{ "dirname",			DIF_SUBR_DIRNAME },
290*0Sstevel@tonic-gate 		{ "cleanpath",			DIF_SUBR_CLEANPATH },
291*0Sstevel@tonic-gate 		{ "strchr",			DIF_SUBR_STRCHR },
292*0Sstevel@tonic-gate 		{ "strrchr",			DIF_SUBR_STRRCHR },
293*0Sstevel@tonic-gate 		{ "strstr",			DIF_SUBR_STRSTR },
294*0Sstevel@tonic-gate 		{ "strtok",			DIF_SUBR_STRTOK },
295*0Sstevel@tonic-gate 		{ "substr",			DIF_SUBR_SUBSTR },
296*0Sstevel@tonic-gate 		{ "index",			DIF_SUBR_INDEX },
297*0Sstevel@tonic-gate 		{ "rindex",			DIF_SUBR_RINDEX },
298*0Sstevel@tonic-gate 		{ NULL, 0 }
299*0Sstevel@tonic-gate 	};
300*0Sstevel@tonic-gate 
301*0Sstevel@tonic-gate 	uint_t subr = DIF_INSTR_SUBR(instr), i;
302*0Sstevel@tonic-gate 
303*0Sstevel@tonic-gate 	mdb_printf("%-4s DIF_SUBR(%u), %%r%u", name, subr, DIF_INSTR_RD(instr));
304*0Sstevel@tonic-gate 
305*0Sstevel@tonic-gate 	for (i = 0; snames[i].name != NULL; i++) {
306*0Sstevel@tonic-gate 		if (subr == snames[i].subr) {
307*0Sstevel@tonic-gate 			mdb_printf("\t\t! %s", snames[i].name);
308*0Sstevel@tonic-gate 			return;
309*0Sstevel@tonic-gate 		}
310*0Sstevel@tonic-gate 	}
311*0Sstevel@tonic-gate }
312*0Sstevel@tonic-gate 
313*0Sstevel@tonic-gate /*ARGSUSED*/
314*0Sstevel@tonic-gate static void
315*0Sstevel@tonic-gate dis_pushts(const dtrace_difo_t *dp, const char *name, dif_instr_t instr)
316*0Sstevel@tonic-gate {
317*0Sstevel@tonic-gate 	static const char *const tnames[] = { "TYPE_CTF", "TYPE_STRING" };
318*0Sstevel@tonic-gate 	uint_t type = DIF_INSTR_TYPE(instr);
319*0Sstevel@tonic-gate 
320*0Sstevel@tonic-gate 	mdb_printf("%-4s DIF_TYPE(%u), %%r%u, %%r%u",
321*0Sstevel@tonic-gate 	    name, type, DIF_INSTR_R2(instr), DIF_INSTR_RS(instr));
322*0Sstevel@tonic-gate 
323*0Sstevel@tonic-gate 	if (type < sizeof (tnames) / sizeof (tnames[0]))
324*0Sstevel@tonic-gate 		mdb_printf("\t! %s", tnames[type]);
325*0Sstevel@tonic-gate }
326*0Sstevel@tonic-gate 
327*0Sstevel@tonic-gate static char *
328*0Sstevel@tonic-gate dis_typestr(const dtrace_diftype_t *t, char *buf, size_t len)
329*0Sstevel@tonic-gate {
330*0Sstevel@tonic-gate 	char kind[8];
331*0Sstevel@tonic-gate 
332*0Sstevel@tonic-gate 	switch (t->dtdt_kind) {
333*0Sstevel@tonic-gate 	case DIF_TYPE_CTF:
334*0Sstevel@tonic-gate 		(void) strcpy(kind, "D type");
335*0Sstevel@tonic-gate 		break;
336*0Sstevel@tonic-gate 	case DIF_TYPE_STRING:
337*0Sstevel@tonic-gate 		(void) strcpy(kind, "string");
338*0Sstevel@tonic-gate 		break;
339*0Sstevel@tonic-gate 	default:
340*0Sstevel@tonic-gate 		(void) mdb_snprintf(kind, sizeof (kind), "0x%x", t->dtdt_kind);
341*0Sstevel@tonic-gate 	}
342*0Sstevel@tonic-gate 
343*0Sstevel@tonic-gate 	if (t->dtdt_flags & DIF_TF_BYREF) {
344*0Sstevel@tonic-gate 		(void) mdb_snprintf(buf, len,
345*0Sstevel@tonic-gate 		    "%s by ref (size %lu)",
346*0Sstevel@tonic-gate 		    kind, (ulong_t)t->dtdt_size);
347*0Sstevel@tonic-gate 	} else {
348*0Sstevel@tonic-gate 		(void) mdb_snprintf(buf, len, "%s (size %lu)",
349*0Sstevel@tonic-gate 		    kind, (ulong_t)t->dtdt_size);
350*0Sstevel@tonic-gate 	}
351*0Sstevel@tonic-gate 
352*0Sstevel@tonic-gate 	return (buf);
353*0Sstevel@tonic-gate }
354*0Sstevel@tonic-gate 
355*0Sstevel@tonic-gate static int
356*0Sstevel@tonic-gate dis(uintptr_t addr, dtrace_difo_t *dp)
357*0Sstevel@tonic-gate {
358*0Sstevel@tonic-gate 	static const struct opent {
359*0Sstevel@tonic-gate 		const char *op_name;
360*0Sstevel@tonic-gate 		void (*op_func)(const dtrace_difo_t *,
361*0Sstevel@tonic-gate 		    const char *, dif_instr_t);
362*0Sstevel@tonic-gate 	} optab[] = {
363*0Sstevel@tonic-gate 		{ "(illegal opcode)", dis_str },
364*0Sstevel@tonic-gate 		{ "or", dis_log },		/* DIF_OP_OR */
365*0Sstevel@tonic-gate 		{ "xor", dis_log },		/* DIF_OP_XOR */
366*0Sstevel@tonic-gate 		{ "and", dis_log },		/* DIF_OP_AND */
367*0Sstevel@tonic-gate 		{ "sll", dis_log },		/* DIF_OP_SLL */
368*0Sstevel@tonic-gate 		{ "srl", dis_log },		/* DIF_OP_SRL */
369*0Sstevel@tonic-gate 		{ "sub", dis_log },		/* DIF_OP_SUB */
370*0Sstevel@tonic-gate 		{ "add", dis_log },		/* DIF_OP_ADD */
371*0Sstevel@tonic-gate 		{ "mul", dis_log },		/* DIF_OP_MUL */
372*0Sstevel@tonic-gate 		{ "sdiv", dis_log },		/* DIF_OP_SDIV */
373*0Sstevel@tonic-gate 		{ "udiv", dis_log },		/* DIF_OP_UDIV */
374*0Sstevel@tonic-gate 		{ "srem", dis_log },		/* DIF_OP_SREM */
375*0Sstevel@tonic-gate 		{ "urem", dis_log },		/* DIF_OP_UREM */
376*0Sstevel@tonic-gate 		{ "not", dis_r1rd },		/* DIF_OP_NOT */
377*0Sstevel@tonic-gate 		{ "mov", dis_r1rd },		/* DIF_OP_MOV */
378*0Sstevel@tonic-gate 		{ "cmp", dis_cmp },		/* DIF_OP_CMP */
379*0Sstevel@tonic-gate 		{ "tst", dis_tst },		/* DIF_OP_TST */
380*0Sstevel@tonic-gate 		{ "ba", dis_branch },		/* DIF_OP_BA */
381*0Sstevel@tonic-gate 		{ "be", dis_branch },		/* DIF_OP_BE */
382*0Sstevel@tonic-gate 		{ "bne", dis_branch },		/* DIF_OP_BNE */
383*0Sstevel@tonic-gate 		{ "bg", dis_branch },		/* DIF_OP_BG */
384*0Sstevel@tonic-gate 		{ "bgu", dis_branch },		/* DIF_OP_BGU */
385*0Sstevel@tonic-gate 		{ "bge", dis_branch },		/* DIF_OP_BGE */
386*0Sstevel@tonic-gate 		{ "bgeu", dis_branch },		/* DIF_OP_BGEU */
387*0Sstevel@tonic-gate 		{ "bl", dis_branch },		/* DIF_OP_BL */
388*0Sstevel@tonic-gate 		{ "blu", dis_branch },		/* DIF_OP_BLU */
389*0Sstevel@tonic-gate 		{ "ble", dis_branch },		/* DIF_OP_BLE */
390*0Sstevel@tonic-gate 		{ "bleu", dis_branch },		/* DIF_OP_BLEU */
391*0Sstevel@tonic-gate 		{ "ldsb", dis_load },		/* DIF_OP_LDSB */
392*0Sstevel@tonic-gate 		{ "ldsh", dis_load },		/* DIF_OP_LDSH */
393*0Sstevel@tonic-gate 		{ "ldsw", dis_load },		/* DIF_OP_LDSW */
394*0Sstevel@tonic-gate 		{ "ldub", dis_load },		/* DIF_OP_LDUB */
395*0Sstevel@tonic-gate 		{ "lduh", dis_load },		/* DIF_OP_LDUH */
396*0Sstevel@tonic-gate 		{ "lduw", dis_load },		/* DIF_OP_LDUW */
397*0Sstevel@tonic-gate 		{ "ldx", dis_load },		/* DIF_OP_LDX */
398*0Sstevel@tonic-gate 		{ "ret", dis_ret },		/* DIF_OP_RET */
399*0Sstevel@tonic-gate 		{ "nop", dis_str },		/* DIF_OP_NOP */
400*0Sstevel@tonic-gate 		{ "setx", dis_setx },		/* DIF_OP_SETX */
401*0Sstevel@tonic-gate 		{ "sets", dis_sets },		/* DIF_OP_SETS */
402*0Sstevel@tonic-gate 		{ "scmp", dis_cmp },		/* DIF_OP_SCMP */
403*0Sstevel@tonic-gate 		{ "ldga", dis_lda },		/* DIF_OP_LDGA */
404*0Sstevel@tonic-gate 		{ "ldgs", dis_ldv },		/* DIF_OP_LDGS */
405*0Sstevel@tonic-gate 		{ "stgs", dis_stv },		/* DIF_OP_STGS */
406*0Sstevel@tonic-gate 		{ "ldta", dis_lda },		/* DIF_OP_LDTA */
407*0Sstevel@tonic-gate 		{ "ldts", dis_ldv },		/* DIF_OP_LDTS */
408*0Sstevel@tonic-gate 		{ "stts", dis_stv },		/* DIF_OP_STTS */
409*0Sstevel@tonic-gate 		{ "sra", dis_log },		/* DIF_OP_SRA */
410*0Sstevel@tonic-gate 		{ "call", dis_call },		/* DIF_OP_CALL */
411*0Sstevel@tonic-gate 		{ "pushtr", dis_pushts },	/* DIF_OP_PUSHTR */
412*0Sstevel@tonic-gate 		{ "pushtv", dis_pushts },	/* DIF_OP_PUSHTV */
413*0Sstevel@tonic-gate 		{ "popts", dis_str },		/* DIF_OP_POPTS */
414*0Sstevel@tonic-gate 		{ "flushts", dis_str },		/* DIF_OP_FLUSHTS */
415*0Sstevel@tonic-gate 		{ "ldgaa", dis_ldv },		/* DIF_OP_LDGAA */
416*0Sstevel@tonic-gate 		{ "ldtaa", dis_ldv },		/* DIF_OP_LDTAA */
417*0Sstevel@tonic-gate 		{ "stgaa", dis_stv },		/* DIF_OP_STGAA */
418*0Sstevel@tonic-gate 		{ "sttaa", dis_stv },		/* DIF_OP_STTAA */
419*0Sstevel@tonic-gate 		{ "ldls", dis_ldv },		/* DIF_OP_LDLS */
420*0Sstevel@tonic-gate 		{ "stls", dis_stv },		/* DIF_OP_STLS */
421*0Sstevel@tonic-gate 		{ "allocs", dis_r1rd },		/* DIF_OP_ALLOCS */
422*0Sstevel@tonic-gate 		{ "copys", dis_log },		/* DIF_OP_COPYS */
423*0Sstevel@tonic-gate 		{ "stb", dis_store },		/* DIF_OP_STB */
424*0Sstevel@tonic-gate 		{ "sth", dis_store },		/* DIF_OP_STH */
425*0Sstevel@tonic-gate 		{ "stw", dis_store },		/* DIF_OP_STW */
426*0Sstevel@tonic-gate 		{ "stx", dis_store },		/* DIF_OP_STX */
427*0Sstevel@tonic-gate 		{ "uldsb", dis_load },		/* DIF_OP_ULDSB */
428*0Sstevel@tonic-gate 		{ "uldsh", dis_load },		/* DIF_OP_ULDSH */
429*0Sstevel@tonic-gate 		{ "uldsw", dis_load },		/* DIF_OP_ULDSW */
430*0Sstevel@tonic-gate 		{ "uldub", dis_load },		/* DIF_OP_ULDUB */
431*0Sstevel@tonic-gate 		{ "ulduh", dis_load },		/* DIF_OP_ULDUH */
432*0Sstevel@tonic-gate 		{ "ulduw", dis_load },		/* DIF_OP_ULDUW */
433*0Sstevel@tonic-gate 		{ "uldx", dis_load },		/* DIF_OP_ULDX */
434*0Sstevel@tonic-gate 		{ "rldsb", dis_load },		/* DIF_OP_RLDSB */
435*0Sstevel@tonic-gate 		{ "rldsh", dis_load },		/* DIF_OP_RLDSH */
436*0Sstevel@tonic-gate 		{ "rldsw", dis_load },		/* DIF_OP_RLDSW */
437*0Sstevel@tonic-gate 		{ "rldub", dis_load },		/* DIF_OP_RLDUB */
438*0Sstevel@tonic-gate 		{ "rlduh", dis_load },		/* DIF_OP_RLDUH */
439*0Sstevel@tonic-gate 		{ "rlduw", dis_load },		/* DIF_OP_RLDUW */
440*0Sstevel@tonic-gate 		{ "rldx", dis_load },		/* DIF_OP_RLDX */
441*0Sstevel@tonic-gate 	};
442*0Sstevel@tonic-gate 
443*0Sstevel@tonic-gate 	dif_instr_t instr, opcode;
444*0Sstevel@tonic-gate 	const struct opent *op;
445*0Sstevel@tonic-gate 
446*0Sstevel@tonic-gate 	if (mdb_vread(&instr, sizeof (dif_instr_t), addr) == -1) {
447*0Sstevel@tonic-gate 		mdb_warn("failed to read DIF instruction at %p", addr);
448*0Sstevel@tonic-gate 		return (DCMD_ERR);
449*0Sstevel@tonic-gate 	}
450*0Sstevel@tonic-gate 
451*0Sstevel@tonic-gate 	opcode = DIF_INSTR_OP(instr);
452*0Sstevel@tonic-gate 
453*0Sstevel@tonic-gate 	if (opcode >= sizeof (optab) / sizeof (optab[0]))
454*0Sstevel@tonic-gate 		opcode = 0; /* force invalid opcode message */
455*0Sstevel@tonic-gate 
456*0Sstevel@tonic-gate 	op = &optab[opcode];
457*0Sstevel@tonic-gate 	mdb_printf("%0*p %08x ", DIFO_ADDRWIDTH, addr, instr);
458*0Sstevel@tonic-gate 	op->op_func(dp, op->op_name, instr);
459*0Sstevel@tonic-gate 	mdb_printf("\n");
460*0Sstevel@tonic-gate 	mdb_set_dot(addr + sizeof (dif_instr_t));
461*0Sstevel@tonic-gate 
462*0Sstevel@tonic-gate 	return (DCMD_OK);
463*0Sstevel@tonic-gate }
464*0Sstevel@tonic-gate 
465*0Sstevel@tonic-gate /*ARGSUSED*/
466*0Sstevel@tonic-gate int
467*0Sstevel@tonic-gate difo(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
468*0Sstevel@tonic-gate {
469*0Sstevel@tonic-gate 	dtrace_difo_t difo, *dp = &difo;
470*0Sstevel@tonic-gate 	uintptr_t instr, limit;
471*0Sstevel@tonic-gate 	dtrace_difv_t *dvp;
472*0Sstevel@tonic-gate 	size_t varsize;
473*0Sstevel@tonic-gate 	ulong_t i;
474*0Sstevel@tonic-gate 	char type[64];
475*0Sstevel@tonic-gate 	char *str;
476*0Sstevel@tonic-gate 
477*0Sstevel@tonic-gate 	if (!(flags & DCMD_ADDRSPEC))
478*0Sstevel@tonic-gate 		return (DCMD_USAGE);
479*0Sstevel@tonic-gate 
480*0Sstevel@tonic-gate 	if (mdb_vread(dp, sizeof (dtrace_difo_t), addr) == -1) {
481*0Sstevel@tonic-gate 		mdb_warn("couldn't read dtrace_difo_t at %p", addr);
482*0Sstevel@tonic-gate 		return (DCMD_ERR);
483*0Sstevel@tonic-gate 	}
484*0Sstevel@tonic-gate 
485*0Sstevel@tonic-gate 	mdb_printf("%<u>DIF Object 0x%p%</u> (refcnt=%d)\n\n",
486*0Sstevel@tonic-gate 	    addr, dp->dtdo_refcnt);
487*0Sstevel@tonic-gate 	mdb_printf("%<b>%-*s %-8s %s%</b>\n", DIFO_ADDRWIDTH, "ADDR",
488*0Sstevel@tonic-gate 	    "OPCODE", "INSTRUCTION");
489*0Sstevel@tonic-gate 
490*0Sstevel@tonic-gate 	mdb_set_dot((uintmax_t)(uintptr_t)dp->dtdo_buf);
491*0Sstevel@tonic-gate 	limit = (uintptr_t)dp->dtdo_buf + dp->dtdo_len * sizeof (dif_instr_t);
492*0Sstevel@tonic-gate 
493*0Sstevel@tonic-gate 	while ((instr = mdb_get_dot()) < limit)
494*0Sstevel@tonic-gate 		dis(instr, dp);
495*0Sstevel@tonic-gate 
496*0Sstevel@tonic-gate 	if (dp->dtdo_varlen != 0) {
497*0Sstevel@tonic-gate 		mdb_printf("\n%<b>%-16s %-4s %-3s %-3s %-4s %s%</b>\n",
498*0Sstevel@tonic-gate 		    "NAME", "ID", "KND", "SCP", "FLAG", "TYPE");
499*0Sstevel@tonic-gate 	}
500*0Sstevel@tonic-gate 
501*0Sstevel@tonic-gate 	varsize = sizeof (dtrace_difv_t) * dp->dtdo_varlen;
502*0Sstevel@tonic-gate 	dvp = mdb_alloc(varsize, UM_SLEEP | UM_GC);
503*0Sstevel@tonic-gate 
504*0Sstevel@tonic-gate 	if (mdb_vread(dvp, varsize, (uintptr_t)dp->dtdo_vartab) == -1) {
505*0Sstevel@tonic-gate 		mdb_warn("couldn't read dtdo_vartab");
506*0Sstevel@tonic-gate 		return (DCMD_ERR);
507*0Sstevel@tonic-gate 	}
508*0Sstevel@tonic-gate 
509*0Sstevel@tonic-gate 	str = mdb_alloc(dp->dtdo_strlen, UM_SLEEP | UM_GC);
510*0Sstevel@tonic-gate 
511*0Sstevel@tonic-gate 	if (mdb_vread(str, dp->dtdo_strlen, (uintptr_t)dp->dtdo_strtab) == -1) {
512*0Sstevel@tonic-gate 		mdb_warn("couldn't read dtdo_strtab");
513*0Sstevel@tonic-gate 		return (DCMD_ERR);
514*0Sstevel@tonic-gate 	}
515*0Sstevel@tonic-gate 
516*0Sstevel@tonic-gate 	for (i = 0; i < dp->dtdo_varlen; i++) {
517*0Sstevel@tonic-gate 		dtrace_difv_t *v = &dvp[i];
518*0Sstevel@tonic-gate 		char kind[4], scope[4], flags[16] = { 0 };
519*0Sstevel@tonic-gate 
520*0Sstevel@tonic-gate 		switch (v->dtdv_kind) {
521*0Sstevel@tonic-gate 		case DIFV_KIND_ARRAY:
522*0Sstevel@tonic-gate 			(void) strcpy(kind, "arr");
523*0Sstevel@tonic-gate 			break;
524*0Sstevel@tonic-gate 		case DIFV_KIND_SCALAR:
525*0Sstevel@tonic-gate 			(void) strcpy(kind, "scl");
526*0Sstevel@tonic-gate 			break;
527*0Sstevel@tonic-gate 		default:
528*0Sstevel@tonic-gate 			(void) mdb_snprintf(kind, sizeof (kind),
529*0Sstevel@tonic-gate 			    "%u", v->dtdv_kind);
530*0Sstevel@tonic-gate 		}
531*0Sstevel@tonic-gate 
532*0Sstevel@tonic-gate 		switch (v->dtdv_scope) {
533*0Sstevel@tonic-gate 		case DIFV_SCOPE_GLOBAL:
534*0Sstevel@tonic-gate 			(void) strcpy(scope, "glb");
535*0Sstevel@tonic-gate 			break;
536*0Sstevel@tonic-gate 		case DIFV_SCOPE_THREAD:
537*0Sstevel@tonic-gate 			(void) strcpy(scope, "tls");
538*0Sstevel@tonic-gate 			break;
539*0Sstevel@tonic-gate 		case DIFV_SCOPE_LOCAL:
540*0Sstevel@tonic-gate 			(void) strcpy(scope, "loc");
541*0Sstevel@tonic-gate 			break;
542*0Sstevel@tonic-gate 		default:
543*0Sstevel@tonic-gate 			(void) mdb_snprintf(scope, sizeof (scope),
544*0Sstevel@tonic-gate 			    "%u", v->dtdv_scope);
545*0Sstevel@tonic-gate 		}
546*0Sstevel@tonic-gate 
547*0Sstevel@tonic-gate 		if (v->dtdv_flags & ~(DIFV_F_REF | DIFV_F_MOD)) {
548*0Sstevel@tonic-gate 			(void) mdb_snprintf(flags, sizeof (flags), "/0x%x",
549*0Sstevel@tonic-gate 			    v->dtdv_flags & ~(DIFV_F_REF | DIFV_F_MOD));
550*0Sstevel@tonic-gate 		}
551*0Sstevel@tonic-gate 
552*0Sstevel@tonic-gate 		if (v->dtdv_flags & DIFV_F_REF)
553*0Sstevel@tonic-gate 			(void) strcat(flags, "/r");
554*0Sstevel@tonic-gate 		if (v->dtdv_flags & DIFV_F_MOD)
555*0Sstevel@tonic-gate 			(void) strcat(flags, "/w");
556*0Sstevel@tonic-gate 
557*0Sstevel@tonic-gate 		mdb_printf("%-16s %-4x %-3s %-3s %-4s %s\n",
558*0Sstevel@tonic-gate 		    &str[v->dtdv_name],
559*0Sstevel@tonic-gate 		    v->dtdv_id, kind, scope, flags + 1,
560*0Sstevel@tonic-gate 		    dis_typestr(&v->dtdv_type, type, sizeof (type)));
561*0Sstevel@tonic-gate 	}
562*0Sstevel@tonic-gate 
563*0Sstevel@tonic-gate 	mdb_printf("\n%<b>RETURN%</b>\n%s\n\n",
564*0Sstevel@tonic-gate 	    dis_typestr(&dp->dtdo_rtype, type, sizeof (type)));
565*0Sstevel@tonic-gate 
566*0Sstevel@tonic-gate 	return (DCMD_OK);
567*0Sstevel@tonic-gate }
568*0Sstevel@tonic-gate 
569*0Sstevel@tonic-gate /*ARGSUSED*/
570*0Sstevel@tonic-gate int
571*0Sstevel@tonic-gate difinstr(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
572*0Sstevel@tonic-gate {
573*0Sstevel@tonic-gate 	if (!(flags & DCMD_ADDRSPEC))
574*0Sstevel@tonic-gate 		return (DCMD_USAGE);
575*0Sstevel@tonic-gate 
576*0Sstevel@tonic-gate 	return (dis(addr, NULL));
577*0Sstevel@tonic-gate }
578*0Sstevel@tonic-gate 
579*0Sstevel@tonic-gate /*ARGSUSED*/
580*0Sstevel@tonic-gate int
581*0Sstevel@tonic-gate id2probe(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
582*0Sstevel@tonic-gate {
583*0Sstevel@tonic-gate 	uintptr_t probe = NULL;
584*0Sstevel@tonic-gate 	uintptr_t probes;
585*0Sstevel@tonic-gate 
586*0Sstevel@tonic-gate 	if (!(flags & DCMD_ADDRSPEC))
587*0Sstevel@tonic-gate 		return (DCMD_USAGE);
588*0Sstevel@tonic-gate 
589*0Sstevel@tonic-gate 	if (addr == DTRACE_IDNONE || addr > UINT32_MAX)
590*0Sstevel@tonic-gate 		goto out;
591*0Sstevel@tonic-gate 
592*0Sstevel@tonic-gate 	if (mdb_readvar(&probes, "dtrace_probes") == -1) {
593*0Sstevel@tonic-gate 		mdb_warn("failed to read 'dtrace_probes'");
594*0Sstevel@tonic-gate 		return (DCMD_ERR);
595*0Sstevel@tonic-gate 	}
596*0Sstevel@tonic-gate 
597*0Sstevel@tonic-gate 	probes += (addr - 1) * sizeof (dtrace_probe_t *);
598*0Sstevel@tonic-gate 
599*0Sstevel@tonic-gate 	if (mdb_vread(&probe, sizeof (uintptr_t), probes) == -1) {
600*0Sstevel@tonic-gate 		mdb_warn("failed to read dtrace_probes[%d]", addr - 1);
601*0Sstevel@tonic-gate 		return (DCMD_ERR);
602*0Sstevel@tonic-gate 	}
603*0Sstevel@tonic-gate 
604*0Sstevel@tonic-gate out:
605*0Sstevel@tonic-gate 	mdb_printf("%p\n", probe);
606*0Sstevel@tonic-gate 	return (DCMD_OK);
607*0Sstevel@tonic-gate }
608*0Sstevel@tonic-gate 
609*0Sstevel@tonic-gate /*ARGSUSED*/
610*0Sstevel@tonic-gate int
611*0Sstevel@tonic-gate dof_hdr(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
612*0Sstevel@tonic-gate {
613*0Sstevel@tonic-gate 	dof_hdr_t h;
614*0Sstevel@tonic-gate 
615*0Sstevel@tonic-gate 	if (argc != 0)
616*0Sstevel@tonic-gate 		return (DCMD_USAGE);
617*0Sstevel@tonic-gate 
618*0Sstevel@tonic-gate 	if (!(flags & DCMD_ADDRSPEC))
619*0Sstevel@tonic-gate 		addr = 0; /* assume base of file in file target */
620*0Sstevel@tonic-gate 
621*0Sstevel@tonic-gate 	if (mdb_vread(&h, sizeof (h), addr) != sizeof (h)) {
622*0Sstevel@tonic-gate 		mdb_warn("failed to read header at %p", addr);
623*0Sstevel@tonic-gate 		return (DCMD_ERR);
624*0Sstevel@tonic-gate 	}
625*0Sstevel@tonic-gate 
626*0Sstevel@tonic-gate 	mdb_printf("dofh_ident.id_magic = 0x%x, %c, %c, %c\n",
627*0Sstevel@tonic-gate 	    h.dofh_ident[DOF_ID_MAG0], h.dofh_ident[DOF_ID_MAG1],
628*0Sstevel@tonic-gate 	    h.dofh_ident[DOF_ID_MAG2], h.dofh_ident[DOF_ID_MAG3]);
629*0Sstevel@tonic-gate 
630*0Sstevel@tonic-gate 	switch (h.dofh_ident[DOF_ID_MODEL]) {
631*0Sstevel@tonic-gate 	case DOF_MODEL_ILP32:
632*0Sstevel@tonic-gate 		mdb_printf("dofh_ident.id_model = ILP32\n");
633*0Sstevel@tonic-gate 		break;
634*0Sstevel@tonic-gate 	case DOF_MODEL_LP64:
635*0Sstevel@tonic-gate 		mdb_printf("dofh_ident.id_model = LP64\n");
636*0Sstevel@tonic-gate 		break;
637*0Sstevel@tonic-gate 	default:
638*0Sstevel@tonic-gate 		mdb_printf("dofh_ident.id_model = 0x%x\n",
639*0Sstevel@tonic-gate 		    h.dofh_ident[DOF_ID_MODEL]);
640*0Sstevel@tonic-gate 	}
641*0Sstevel@tonic-gate 
642*0Sstevel@tonic-gate 	switch (h.dofh_ident[DOF_ID_ENCODING]) {
643*0Sstevel@tonic-gate 	case DOF_ENCODE_LSB:
644*0Sstevel@tonic-gate 		mdb_printf("dofh_ident.id_encoding = LSB\n");
645*0Sstevel@tonic-gate 		break;
646*0Sstevel@tonic-gate 	case DOF_ENCODE_MSB:
647*0Sstevel@tonic-gate 		mdb_printf("dofh_ident.id_encoding = MSB\n");
648*0Sstevel@tonic-gate 		break;
649*0Sstevel@tonic-gate 	default:
650*0Sstevel@tonic-gate 		mdb_printf("dofh_ident.id_encoding = 0x%x\n",
651*0Sstevel@tonic-gate 		    h.dofh_ident[DOF_ID_ENCODING]);
652*0Sstevel@tonic-gate 	}
653*0Sstevel@tonic-gate 
654*0Sstevel@tonic-gate 	mdb_printf("dofh_ident.id_version = %u\n",
655*0Sstevel@tonic-gate 	    h.dofh_ident[DOF_ID_VERSION]);
656*0Sstevel@tonic-gate 	mdb_printf("dofh_ident.id_difvers = %u\n",
657*0Sstevel@tonic-gate 	    h.dofh_ident[DOF_ID_DIFVERS]);
658*0Sstevel@tonic-gate 	mdb_printf("dofh_ident.id_difireg = %u\n",
659*0Sstevel@tonic-gate 	    h.dofh_ident[DOF_ID_DIFIREG]);
660*0Sstevel@tonic-gate 	mdb_printf("dofh_ident.id_diftreg = %u\n",
661*0Sstevel@tonic-gate 	    h.dofh_ident[DOF_ID_DIFTREG]);
662*0Sstevel@tonic-gate 
663*0Sstevel@tonic-gate 	mdb_printf("dofh_flags = 0x%x\n", h.dofh_flags);
664*0Sstevel@tonic-gate 	mdb_printf("dofh_hdrsize = %u\n", h.dofh_hdrsize);
665*0Sstevel@tonic-gate 	mdb_printf("dofh_secsize = %u\n", h.dofh_secsize);
666*0Sstevel@tonic-gate 	mdb_printf("dofh_secnum = %u\n", h.dofh_secnum);
667*0Sstevel@tonic-gate 	mdb_printf("dofh_secoff = %llu\n", h.dofh_secoff);
668*0Sstevel@tonic-gate 	mdb_printf("dofh_loadsz = %llu\n", h.dofh_loadsz);
669*0Sstevel@tonic-gate 	mdb_printf("dofh_filesz = %llu\n", h.dofh_filesz);
670*0Sstevel@tonic-gate 
671*0Sstevel@tonic-gate 	return (DCMD_OK);
672*0Sstevel@tonic-gate }
673*0Sstevel@tonic-gate 
674*0Sstevel@tonic-gate /*ARGSUSED*/
675*0Sstevel@tonic-gate static int
676*0Sstevel@tonic-gate dof_sec_walk(uintptr_t addr, void *ignored, int *sec)
677*0Sstevel@tonic-gate {
678*0Sstevel@tonic-gate 	mdb_printf("%3d ", (*sec)++);
679*0Sstevel@tonic-gate 	(void) dof_sec(addr, DCMD_ADDRSPEC | DCMD_LOOP, 0, NULL);
680*0Sstevel@tonic-gate 	return (WALK_NEXT);
681*0Sstevel@tonic-gate }
682*0Sstevel@tonic-gate 
683*0Sstevel@tonic-gate static const char *
684*0Sstevel@tonic-gate dof_sec_typename(uint32_t type)
685*0Sstevel@tonic-gate {
686*0Sstevel@tonic-gate 	static const char *const types[] = {
687*0Sstevel@tonic-gate 		"none", "comments", "source", "ecbdesc", "probedesc", "actdesc",
688*0Sstevel@tonic-gate 		"difohdr", "dif", "strtab", "vartab", "reltab", "typtab",
689*0Sstevel@tonic-gate 		"urelhdr", "krelhdr", "optdesc", "provider", "probes",
690*0Sstevel@tonic-gate 		"prargs", "proffs", "inttab", "utsname"
691*0Sstevel@tonic-gate 	};
692*0Sstevel@tonic-gate 	static char buf[32];
693*0Sstevel@tonic-gate 
694*0Sstevel@tonic-gate 	if (type < sizeof (types) / sizeof (types[0]))
695*0Sstevel@tonic-gate 		return (types[type]);
696*0Sstevel@tonic-gate 
697*0Sstevel@tonic-gate 	mdb_snprintf(buf, sizeof (buf), "%u", type);
698*0Sstevel@tonic-gate 	return (buf);
699*0Sstevel@tonic-gate }
700*0Sstevel@tonic-gate 
701*0Sstevel@tonic-gate /*ARGSUSED*/
702*0Sstevel@tonic-gate int
703*0Sstevel@tonic-gate dof_sec(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
704*0Sstevel@tonic-gate {
705*0Sstevel@tonic-gate 	dof_sec_t s;
706*0Sstevel@tonic-gate 
707*0Sstevel@tonic-gate 	if (!(flags & DCMD_ADDRSPEC))
708*0Sstevel@tonic-gate 		mdb_printf("%<u>%-3s ", "NDX");
709*0Sstevel@tonic-gate 
710*0Sstevel@tonic-gate 	if (!(flags & DCMD_ADDRSPEC) || DCMD_HDRSPEC(flags)) {
711*0Sstevel@tonic-gate 		mdb_printf("%<u>%?s %-10s %-5s %-5s %-5s %-6s %-5s%</u>\n",
712*0Sstevel@tonic-gate 		    "ADDR", "TYPE", "ALIGN", "FLAGS", "ENTSZ", "OFFSET",
713*0Sstevel@tonic-gate 		    "SIZE");
714*0Sstevel@tonic-gate 	}
715*0Sstevel@tonic-gate 
716*0Sstevel@tonic-gate 	if (!(flags & DCMD_ADDRSPEC)) {
717*0Sstevel@tonic-gate 		int sec = 0;
718*0Sstevel@tonic-gate 
719*0Sstevel@tonic-gate 		if (mdb_walk("dof_sec",
720*0Sstevel@tonic-gate 		    (mdb_walk_cb_t)dof_sec_walk, &sec) == -1) {
721*0Sstevel@tonic-gate 			mdb_warn("failed to walk dof_sec");
722*0Sstevel@tonic-gate 			return (DCMD_ERR);
723*0Sstevel@tonic-gate 		}
724*0Sstevel@tonic-gate 		return (DCMD_OK);
725*0Sstevel@tonic-gate 	}
726*0Sstevel@tonic-gate 
727*0Sstevel@tonic-gate 	if (argc != 0)
728*0Sstevel@tonic-gate 		return (DCMD_USAGE);
729*0Sstevel@tonic-gate 
730*0Sstevel@tonic-gate 	if (mdb_vread(&s, sizeof (s), addr) != sizeof (s)) {
731*0Sstevel@tonic-gate 		mdb_warn("failed to read section header at %p", addr);
732*0Sstevel@tonic-gate 		return (DCMD_ERR);
733*0Sstevel@tonic-gate 	}
734*0Sstevel@tonic-gate 
735*0Sstevel@tonic-gate 	mdb_printf("%?p ", addr);
736*0Sstevel@tonic-gate 	mdb_printf("%-10s ", dof_sec_typename(s.dofs_type));
737*0Sstevel@tonic-gate 
738*0Sstevel@tonic-gate 	mdb_printf("%-5u %-#5x %-#5x %-6llx %-#5llx\n", s.dofs_align,
739*0Sstevel@tonic-gate 	    s.dofs_flags, s.dofs_entsize, s.dofs_offset, s.dofs_size);
740*0Sstevel@tonic-gate 
741*0Sstevel@tonic-gate 	return (DCMD_OK);
742*0Sstevel@tonic-gate }
743*0Sstevel@tonic-gate 
744*0Sstevel@tonic-gate int
745*0Sstevel@tonic-gate dof_sec_walk_init(mdb_walk_state_t *wsp)
746*0Sstevel@tonic-gate {
747*0Sstevel@tonic-gate 	dof_hdr_t h, *hp;
748*0Sstevel@tonic-gate 	size_t size;
749*0Sstevel@tonic-gate 
750*0Sstevel@tonic-gate 	if (mdb_vread(&h, sizeof (h), wsp->walk_addr) != sizeof (h)) {
751*0Sstevel@tonic-gate 		mdb_warn("failed to read DOF header at %p", wsp->walk_addr);
752*0Sstevel@tonic-gate 		return (WALK_ERR);
753*0Sstevel@tonic-gate 	}
754*0Sstevel@tonic-gate 
755*0Sstevel@tonic-gate 	size = sizeof (dof_hdr_t) + sizeof (dof_sec_t) * h.dofh_secnum;
756*0Sstevel@tonic-gate 	hp = mdb_alloc(size, UM_SLEEP);
757*0Sstevel@tonic-gate 
758*0Sstevel@tonic-gate 	if (mdb_vread(hp, size, wsp->walk_addr) != size) {
759*0Sstevel@tonic-gate 		mdb_warn("failed to read DOF sections at %p", wsp->walk_addr);
760*0Sstevel@tonic-gate 		mdb_free(hp, size);
761*0Sstevel@tonic-gate 		return (WALK_ERR);
762*0Sstevel@tonic-gate 	}
763*0Sstevel@tonic-gate 
764*0Sstevel@tonic-gate 	wsp->walk_arg = (void *)0;
765*0Sstevel@tonic-gate 	wsp->walk_data = hp;
766*0Sstevel@tonic-gate 
767*0Sstevel@tonic-gate 	return (WALK_NEXT);
768*0Sstevel@tonic-gate }
769*0Sstevel@tonic-gate 
770*0Sstevel@tonic-gate int
771*0Sstevel@tonic-gate dof_sec_walk_step(mdb_walk_state_t *wsp)
772*0Sstevel@tonic-gate {
773*0Sstevel@tonic-gate 	uint_t i = (uintptr_t)wsp->walk_arg;
774*0Sstevel@tonic-gate 	size_t off = sizeof (dof_hdr_t) + sizeof (dof_sec_t) * i;
775*0Sstevel@tonic-gate 	dof_hdr_t *hp = wsp->walk_data;
776*0Sstevel@tonic-gate 	dof_sec_t *sp = (dof_sec_t *)((uintptr_t)hp + off);
777*0Sstevel@tonic-gate 
778*0Sstevel@tonic-gate 	if (i >= hp->dofh_secnum)
779*0Sstevel@tonic-gate 		return (WALK_DONE);
780*0Sstevel@tonic-gate 
781*0Sstevel@tonic-gate 	wsp->walk_arg = (void *)(uintptr_t)(i + 1);
782*0Sstevel@tonic-gate 	return (wsp->walk_callback(wsp->walk_addr + off, sp, wsp->walk_cbdata));
783*0Sstevel@tonic-gate }
784*0Sstevel@tonic-gate 
785*0Sstevel@tonic-gate void
786*0Sstevel@tonic-gate dof_sec_walk_fini(mdb_walk_state_t *wsp)
787*0Sstevel@tonic-gate {
788*0Sstevel@tonic-gate 	dof_hdr_t *hp = wsp->walk_data;
789*0Sstevel@tonic-gate 	mdb_free(hp, sizeof (dof_hdr_t) + sizeof (dof_sec_t) * hp->dofh_secnum);
790*0Sstevel@tonic-gate }
791*0Sstevel@tonic-gate 
792*0Sstevel@tonic-gate /*ARGSUSED*/
793*0Sstevel@tonic-gate int
794*0Sstevel@tonic-gate dof_ecbdesc(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
795*0Sstevel@tonic-gate {
796*0Sstevel@tonic-gate 	dof_ecbdesc_t e;
797*0Sstevel@tonic-gate 
798*0Sstevel@tonic-gate 	if (argc != 0 || !(flags & DCMD_ADDRSPEC))
799*0Sstevel@tonic-gate 		return (DCMD_USAGE);
800*0Sstevel@tonic-gate 
801*0Sstevel@tonic-gate 	if (mdb_vread(&e, sizeof (e), addr) != sizeof (e)) {
802*0Sstevel@tonic-gate 		mdb_warn("failed to read ecbdesc at %p", addr);
803*0Sstevel@tonic-gate 		return (DCMD_ERR);
804*0Sstevel@tonic-gate 	}
805*0Sstevel@tonic-gate 
806*0Sstevel@tonic-gate 	mdb_printf("dofe_probes = %d\n", e.dofe_probes);
807*0Sstevel@tonic-gate 	mdb_printf("dofe_actions = %d\n", e.dofe_actions);
808*0Sstevel@tonic-gate 	mdb_printf("dofe_pred = %d\n", e.dofe_pred);
809*0Sstevel@tonic-gate 	mdb_printf("dofe_uarg = 0x%llx\n", e.dofe_uarg);
810*0Sstevel@tonic-gate 
811*0Sstevel@tonic-gate 	return (DCMD_OK);
812*0Sstevel@tonic-gate }
813*0Sstevel@tonic-gate 
814*0Sstevel@tonic-gate /*ARGSUSED*/
815*0Sstevel@tonic-gate int
816*0Sstevel@tonic-gate dof_probedesc(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
817*0Sstevel@tonic-gate {
818*0Sstevel@tonic-gate 	dof_probedesc_t p;
819*0Sstevel@tonic-gate 
820*0Sstevel@tonic-gate 	if (argc != 0 || !(flags & DCMD_ADDRSPEC))
821*0Sstevel@tonic-gate 		return (DCMD_USAGE);
822*0Sstevel@tonic-gate 
823*0Sstevel@tonic-gate 	if (mdb_vread(&p, sizeof (p), addr) != sizeof (p)) {
824*0Sstevel@tonic-gate 		mdb_warn("failed to read probedesc at %p", addr);
825*0Sstevel@tonic-gate 		return (DCMD_ERR);
826*0Sstevel@tonic-gate 	}
827*0Sstevel@tonic-gate 
828*0Sstevel@tonic-gate 	mdb_printf("dofp_strtab = %d\n", p.dofp_strtab);
829*0Sstevel@tonic-gate 	mdb_printf("dofp_provider = %u\n", p.dofp_provider);
830*0Sstevel@tonic-gate 	mdb_printf("dofp_mod = %u\n", p.dofp_mod);
831*0Sstevel@tonic-gate 	mdb_printf("dofp_func = %u\n", p.dofp_func);
832*0Sstevel@tonic-gate 	mdb_printf("dofp_name = %u\n", p.dofp_name);
833*0Sstevel@tonic-gate 	mdb_printf("dofp_id = %u\n", p.dofp_id);
834*0Sstevel@tonic-gate 
835*0Sstevel@tonic-gate 	return (DCMD_OK);
836*0Sstevel@tonic-gate }
837*0Sstevel@tonic-gate 
838*0Sstevel@tonic-gate /*ARGSUSED*/
839*0Sstevel@tonic-gate int
840*0Sstevel@tonic-gate dof_actdesc(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
841*0Sstevel@tonic-gate {
842*0Sstevel@tonic-gate 	dof_actdesc_t a;
843*0Sstevel@tonic-gate 
844*0Sstevel@tonic-gate 	if (argc != 0 || !(flags & DCMD_ADDRSPEC))
845*0Sstevel@tonic-gate 		return (DCMD_USAGE);
846*0Sstevel@tonic-gate 
847*0Sstevel@tonic-gate 	if (mdb_vread(&a, sizeof (a), addr) != sizeof (a)) {
848*0Sstevel@tonic-gate 		mdb_warn("failed to read actdesc at %p", addr);
849*0Sstevel@tonic-gate 		return (DCMD_ERR);
850*0Sstevel@tonic-gate 	}
851*0Sstevel@tonic-gate 
852*0Sstevel@tonic-gate 	mdb_printf("dofa_difo = %d\n", a.dofa_difo);
853*0Sstevel@tonic-gate 	mdb_printf("dofa_strtab = %d\n", a.dofa_strtab);
854*0Sstevel@tonic-gate 	mdb_printf("dofa_kind = %u\n", a.dofa_kind);
855*0Sstevel@tonic-gate 	mdb_printf("dofa_ntuple = %u\n", a.dofa_ntuple);
856*0Sstevel@tonic-gate 	mdb_printf("dofa_arg = 0x%llx\n", a.dofa_arg);
857*0Sstevel@tonic-gate 	mdb_printf("dofa_uarg = 0x%llx\n", a.dofa_uarg);
858*0Sstevel@tonic-gate 
859*0Sstevel@tonic-gate 	return (DCMD_OK);
860*0Sstevel@tonic-gate }
861*0Sstevel@tonic-gate 
862*0Sstevel@tonic-gate /*ARGSUSED*/
863*0Sstevel@tonic-gate int
864*0Sstevel@tonic-gate dof_relohdr(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
865*0Sstevel@tonic-gate {
866*0Sstevel@tonic-gate 	dof_relohdr_t r;
867*0Sstevel@tonic-gate 
868*0Sstevel@tonic-gate 	if (argc != 0 || !(flags & DCMD_ADDRSPEC))
869*0Sstevel@tonic-gate 		return (DCMD_USAGE);
870*0Sstevel@tonic-gate 
871*0Sstevel@tonic-gate 	if (mdb_vread(&r, sizeof (r), addr) != sizeof (r)) {
872*0Sstevel@tonic-gate 		mdb_warn("failed to read relohdr at %p", addr);
873*0Sstevel@tonic-gate 		return (DCMD_ERR);
874*0Sstevel@tonic-gate 	}
875*0Sstevel@tonic-gate 
876*0Sstevel@tonic-gate 	mdb_printf("dofr_strtab = %d\n", r.dofr_strtab);
877*0Sstevel@tonic-gate 	mdb_printf("dofr_relsec = %d\n", r.dofr_relsec);
878*0Sstevel@tonic-gate 	mdb_printf("dofr_tgtsec = %d\n", r.dofr_tgtsec);
879*0Sstevel@tonic-gate 
880*0Sstevel@tonic-gate 	return (DCMD_OK);
881*0Sstevel@tonic-gate }
882*0Sstevel@tonic-gate 
883*0Sstevel@tonic-gate /*ARGSUSED*/
884*0Sstevel@tonic-gate int
885*0Sstevel@tonic-gate dof_relodesc(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
886*0Sstevel@tonic-gate {
887*0Sstevel@tonic-gate 	dof_relodesc_t r;
888*0Sstevel@tonic-gate 
889*0Sstevel@tonic-gate 	if (argc != 0 || !(flags & DCMD_ADDRSPEC))
890*0Sstevel@tonic-gate 		return (DCMD_USAGE);
891*0Sstevel@tonic-gate 
892*0Sstevel@tonic-gate 	if (mdb_vread(&r, sizeof (r), addr) != sizeof (r)) {
893*0Sstevel@tonic-gate 		mdb_warn("failed to read relodesc at %p", addr);
894*0Sstevel@tonic-gate 		return (DCMD_ERR);
895*0Sstevel@tonic-gate 	}
896*0Sstevel@tonic-gate 
897*0Sstevel@tonic-gate 	mdb_printf("dofr_name = %u\n", r.dofr_name);
898*0Sstevel@tonic-gate 	mdb_printf("dofr_type = %u\n", r.dofr_type);
899*0Sstevel@tonic-gate 	mdb_printf("dofr_offset = 0x%llx\n", r.dofr_offset);
900*0Sstevel@tonic-gate 	mdb_printf("dofr_data = 0x%llx\n", r.dofr_data);
901*0Sstevel@tonic-gate 
902*0Sstevel@tonic-gate 	return (DCMD_OK);
903*0Sstevel@tonic-gate }
904*0Sstevel@tonic-gate 
905*0Sstevel@tonic-gate void
906*0Sstevel@tonic-gate dtrace_help(void)
907*0Sstevel@tonic-gate {
908*0Sstevel@tonic-gate 
909*0Sstevel@tonic-gate 	mdb_printf("Given a dtrace_state_t structure that represents a "
910*0Sstevel@tonic-gate 	    "DTrace consumer, prints\n"
911*0Sstevel@tonic-gate 	    "dtrace(1M)-like output for in-kernel DTrace data.  (The "
912*0Sstevel@tonic-gate 	    "dtrace_state_t\n"
913*0Sstevel@tonic-gate 	    "structures for all DTrace consumers may be obtained by running "
914*0Sstevel@tonic-gate 	    "the \n"
915*0Sstevel@tonic-gate 	    "::dtrace_state dcmd.)   When data is present on multiple CPUs, "
916*0Sstevel@tonic-gate 	    "data are\n"
917*0Sstevel@tonic-gate 	    "presented in CPU order, with records within each CPU ordered "
918*0Sstevel@tonic-gate 	    "oldest to \n"
919*0Sstevel@tonic-gate 	    "youngest.  Options:\n\n"
920*0Sstevel@tonic-gate 	    "-c cpu     Only provide output for specified CPU.\n");
921*0Sstevel@tonic-gate }
922*0Sstevel@tonic-gate 
923*0Sstevel@tonic-gate static int
924*0Sstevel@tonic-gate dtracemdb_eprobe(dtrace_state_t *state, dtrace_eprobedesc_t *epd)
925*0Sstevel@tonic-gate {
926*0Sstevel@tonic-gate 	dtrace_epid_t epid = epd->dtepd_epid;
927*0Sstevel@tonic-gate 	dtrace_probe_t probe;
928*0Sstevel@tonic-gate 	dtrace_ecb_t ecb;
929*0Sstevel@tonic-gate 	uintptr_t addr, paddr, ap;
930*0Sstevel@tonic-gate 	dtrace_action_t act;
931*0Sstevel@tonic-gate 	int nactions, nrecs;
932*0Sstevel@tonic-gate 
933*0Sstevel@tonic-gate 	addr = (uintptr_t)state->dts_ecbs +
934*0Sstevel@tonic-gate 	    (epid - 1) * sizeof (dtrace_ecb_t *);
935*0Sstevel@tonic-gate 
936*0Sstevel@tonic-gate 	if (mdb_vread(&addr, sizeof (addr), addr) == -1) {
937*0Sstevel@tonic-gate 		mdb_warn("failed to read ecb for epid %d", epid);
938*0Sstevel@tonic-gate 		return (-1);
939*0Sstevel@tonic-gate 	}
940*0Sstevel@tonic-gate 
941*0Sstevel@tonic-gate 	if (addr == NULL) {
942*0Sstevel@tonic-gate 		mdb_warn("epid %d doesn't match an ecb\n", epid);
943*0Sstevel@tonic-gate 		return (-1);
944*0Sstevel@tonic-gate 	}
945*0Sstevel@tonic-gate 
946*0Sstevel@tonic-gate 	if (mdb_vread(&ecb, sizeof (ecb), addr) == -1) {
947*0Sstevel@tonic-gate 		mdb_warn("failed to read ecb at %p", addr);
948*0Sstevel@tonic-gate 		return (-1);
949*0Sstevel@tonic-gate 	}
950*0Sstevel@tonic-gate 
951*0Sstevel@tonic-gate 	paddr = (uintptr_t)ecb.dte_probe;
952*0Sstevel@tonic-gate 
953*0Sstevel@tonic-gate 	if (mdb_vread(&probe, sizeof (probe), paddr) == -1) {
954*0Sstevel@tonic-gate 		mdb_warn("failed to read probe for ecb %p", addr);
955*0Sstevel@tonic-gate 		return (-1);
956*0Sstevel@tonic-gate 	}
957*0Sstevel@tonic-gate 
958*0Sstevel@tonic-gate 	/*
959*0Sstevel@tonic-gate 	 * This is a little painful:  in order to find the number of actions,
960*0Sstevel@tonic-gate 	 * we need to first walk through them.
961*0Sstevel@tonic-gate 	 */
962*0Sstevel@tonic-gate 	for (ap = (uintptr_t)ecb.dte_action, nactions = 0; ap != NULL; ) {
963*0Sstevel@tonic-gate 		if (mdb_vread(&act, sizeof (act), ap) == -1) {
964*0Sstevel@tonic-gate 			mdb_warn("failed to read action %p on ecb %p",
965*0Sstevel@tonic-gate 			    ap, addr);
966*0Sstevel@tonic-gate 			return (-1);
967*0Sstevel@tonic-gate 		}
968*0Sstevel@tonic-gate 
969*0Sstevel@tonic-gate 		if (!DTRACEACT_ISAGG(act.dta_kind) && !act.dta_intuple)
970*0Sstevel@tonic-gate 			nactions++;
971*0Sstevel@tonic-gate 
972*0Sstevel@tonic-gate 		ap = (uintptr_t)act.dta_next;
973*0Sstevel@tonic-gate 	}
974*0Sstevel@tonic-gate 
975*0Sstevel@tonic-gate 	nrecs = epd->dtepd_nrecs;
976*0Sstevel@tonic-gate 	epd->dtepd_nrecs = nactions;
977*0Sstevel@tonic-gate 	epd->dtepd_probeid = probe.dtpr_id;
978*0Sstevel@tonic-gate 	epd->dtepd_uarg = ecb.dte_uarg;
979*0Sstevel@tonic-gate 	epd->dtepd_size = ecb.dte_size;
980*0Sstevel@tonic-gate 
981*0Sstevel@tonic-gate 	for (ap = (uintptr_t)ecb.dte_action, nactions = 0; ap != NULL; ) {
982*0Sstevel@tonic-gate 		if (mdb_vread(&act, sizeof (act), ap) == -1) {
983*0Sstevel@tonic-gate 			mdb_warn("failed to read action %p on ecb %p",
984*0Sstevel@tonic-gate 			    ap, addr);
985*0Sstevel@tonic-gate 			return (-1);
986*0Sstevel@tonic-gate 		}
987*0Sstevel@tonic-gate 
988*0Sstevel@tonic-gate 		if (!DTRACEACT_ISAGG(act.dta_kind) && !act.dta_intuple) {
989*0Sstevel@tonic-gate 			if (nrecs-- == 0)
990*0Sstevel@tonic-gate 				break;
991*0Sstevel@tonic-gate 
992*0Sstevel@tonic-gate 			epd->dtepd_rec[nactions++] = act.dta_rec;
993*0Sstevel@tonic-gate 		}
994*0Sstevel@tonic-gate 
995*0Sstevel@tonic-gate 		ap = (uintptr_t)act.dta_next;
996*0Sstevel@tonic-gate 	}
997*0Sstevel@tonic-gate 
998*0Sstevel@tonic-gate 	return (0);
999*0Sstevel@tonic-gate }
1000*0Sstevel@tonic-gate 
1001*0Sstevel@tonic-gate /*ARGSUSED*/
1002*0Sstevel@tonic-gate static int
1003*0Sstevel@tonic-gate dtracemdb_probe(dtrace_state_t *state, dtrace_probedesc_t *pd)
1004*0Sstevel@tonic-gate {
1005*0Sstevel@tonic-gate 	uintptr_t base, addr, paddr, praddr;
1006*0Sstevel@tonic-gate 	int nprobes, i;
1007*0Sstevel@tonic-gate 	dtrace_probe_t probe;
1008*0Sstevel@tonic-gate 	dtrace_provider_t prov;
1009*0Sstevel@tonic-gate 
1010*0Sstevel@tonic-gate 	if (pd->dtpd_id == DTRACE_IDNONE)
1011*0Sstevel@tonic-gate 		pd->dtpd_id++;
1012*0Sstevel@tonic-gate 
1013*0Sstevel@tonic-gate 	if (mdb_readvar(&base, "dtrace_probes") == -1) {
1014*0Sstevel@tonic-gate 		mdb_warn("failed to read 'dtrace_probes'");
1015*0Sstevel@tonic-gate 		return (-1);
1016*0Sstevel@tonic-gate 	}
1017*0Sstevel@tonic-gate 
1018*0Sstevel@tonic-gate 	if (mdb_readvar(&nprobes, "dtrace_nprobes") == -1) {
1019*0Sstevel@tonic-gate 		mdb_warn("failed to read 'dtrace_nprobes'");
1020*0Sstevel@tonic-gate 		return (-1);
1021*0Sstevel@tonic-gate 	}
1022*0Sstevel@tonic-gate 
1023*0Sstevel@tonic-gate 	for (i = pd->dtpd_id; i <= nprobes; i++) {
1024*0Sstevel@tonic-gate 		addr = base + (i - 1) * sizeof (dtrace_probe_t *);
1025*0Sstevel@tonic-gate 
1026*0Sstevel@tonic-gate 		if (mdb_vread(&paddr, sizeof (paddr), addr) == -1) {
1027*0Sstevel@tonic-gate 			mdb_warn("couldn't read probe pointer at %p", addr);
1028*0Sstevel@tonic-gate 			return (-1);
1029*0Sstevel@tonic-gate 		}
1030*0Sstevel@tonic-gate 
1031*0Sstevel@tonic-gate 		if (paddr != NULL)
1032*0Sstevel@tonic-gate 			break;
1033*0Sstevel@tonic-gate 	}
1034*0Sstevel@tonic-gate 
1035*0Sstevel@tonic-gate 	if (paddr == NULL) {
1036*0Sstevel@tonic-gate 		errno = ESRCH;
1037*0Sstevel@tonic-gate 		return (-1);
1038*0Sstevel@tonic-gate 	}
1039*0Sstevel@tonic-gate 
1040*0Sstevel@tonic-gate 	if (mdb_vread(&probe, sizeof (probe), paddr) == -1) {
1041*0Sstevel@tonic-gate 		mdb_warn("couldn't read probe at %p", paddr);
1042*0Sstevel@tonic-gate 		return (-1);
1043*0Sstevel@tonic-gate 	}
1044*0Sstevel@tonic-gate 
1045*0Sstevel@tonic-gate 	pd->dtpd_id = probe.dtpr_id;
1046*0Sstevel@tonic-gate 
1047*0Sstevel@tonic-gate 	if (mdb_vread(pd->dtpd_name, DTRACE_NAMELEN,
1048*0Sstevel@tonic-gate 	    (uintptr_t)probe.dtpr_name) == -1) {
1049*0Sstevel@tonic-gate 		mdb_warn("failed to read probe name for probe %p", paddr);
1050*0Sstevel@tonic-gate 		return (-1);
1051*0Sstevel@tonic-gate 	}
1052*0Sstevel@tonic-gate 
1053*0Sstevel@tonic-gate 	if (mdb_vread(pd->dtpd_func, DTRACE_FUNCNAMELEN,
1054*0Sstevel@tonic-gate 	    (uintptr_t)probe.dtpr_func) == -1) {
1055*0Sstevel@tonic-gate 		mdb_warn("failed to read function name for probe %p", paddr);
1056*0Sstevel@tonic-gate 		return (-1);
1057*0Sstevel@tonic-gate 	}
1058*0Sstevel@tonic-gate 
1059*0Sstevel@tonic-gate 	if (mdb_vread(pd->dtpd_mod, DTRACE_MODNAMELEN,
1060*0Sstevel@tonic-gate 	    (uintptr_t)probe.dtpr_mod) == -1) {
1061*0Sstevel@tonic-gate 		mdb_warn("failed to read module name for probe %p", paddr);
1062*0Sstevel@tonic-gate 		return (-1);
1063*0Sstevel@tonic-gate 	}
1064*0Sstevel@tonic-gate 
1065*0Sstevel@tonic-gate 	praddr = (uintptr_t)probe.dtpr_provider;
1066*0Sstevel@tonic-gate 
1067*0Sstevel@tonic-gate 	if (mdb_vread(&prov, sizeof (prov), praddr) == -1) {
1068*0Sstevel@tonic-gate 		mdb_warn("failed to read provider for probe %p", paddr);
1069*0Sstevel@tonic-gate 		return (-1);
1070*0Sstevel@tonic-gate 	}
1071*0Sstevel@tonic-gate 
1072*0Sstevel@tonic-gate 	if (mdb_vread(pd->dtpd_provider, DTRACE_PROVNAMELEN,
1073*0Sstevel@tonic-gate 	    (uintptr_t)prov.dtpv_name) == -1) {
1074*0Sstevel@tonic-gate 		mdb_warn("failed to read provider name for probe %p", paddr);
1075*0Sstevel@tonic-gate 		return (-1);
1076*0Sstevel@tonic-gate 	}
1077*0Sstevel@tonic-gate 
1078*0Sstevel@tonic-gate 	return (0);
1079*0Sstevel@tonic-gate }
1080*0Sstevel@tonic-gate 
1081*0Sstevel@tonic-gate /*ARGSUSED*/
1082*0Sstevel@tonic-gate static int
1083*0Sstevel@tonic-gate dtracemdb_aggdesc(dtrace_state_t *state, dtrace_aggdesc_t *agd)
1084*0Sstevel@tonic-gate {
1085*0Sstevel@tonic-gate 	dtrace_aggid_t aggid = agd->dtagd_id;
1086*0Sstevel@tonic-gate 	dtrace_aggregation_t agg;
1087*0Sstevel@tonic-gate 	dtrace_ecb_t ecb;
1088*0Sstevel@tonic-gate 	uintptr_t addr, eaddr, ap, last;
1089*0Sstevel@tonic-gate 	dtrace_action_t act;
1090*0Sstevel@tonic-gate 	dtrace_recdesc_t *lrec;
1091*0Sstevel@tonic-gate 	int nactions, nrecs;
1092*0Sstevel@tonic-gate 
1093*0Sstevel@tonic-gate 	addr = (uintptr_t)state->dts_aggregations +
1094*0Sstevel@tonic-gate 	    (aggid - 1) * sizeof (dtrace_aggregation_t *);
1095*0Sstevel@tonic-gate 
1096*0Sstevel@tonic-gate 	if (mdb_vread(&addr, sizeof (addr), addr) == -1) {
1097*0Sstevel@tonic-gate 		mdb_warn("failed to read aggregation for aggid %d", aggid);
1098*0Sstevel@tonic-gate 		return (-1);
1099*0Sstevel@tonic-gate 	}
1100*0Sstevel@tonic-gate 
1101*0Sstevel@tonic-gate 	if (addr == NULL) {
1102*0Sstevel@tonic-gate 		mdb_warn("aggid %d doesn't match an aggregation\n", aggid);
1103*0Sstevel@tonic-gate 		return (-1);
1104*0Sstevel@tonic-gate 	}
1105*0Sstevel@tonic-gate 
1106*0Sstevel@tonic-gate 	if (mdb_vread(&agg, sizeof (agg), addr) == -1) {
1107*0Sstevel@tonic-gate 		mdb_warn("failed to read aggregation at %p", addr);
1108*0Sstevel@tonic-gate 		return (-1);
1109*0Sstevel@tonic-gate 	}
1110*0Sstevel@tonic-gate 
1111*0Sstevel@tonic-gate 	eaddr = (uintptr_t)agg.dtag_ecb;
1112*0Sstevel@tonic-gate 
1113*0Sstevel@tonic-gate 	if (mdb_vread(&ecb, sizeof (ecb), eaddr) == -1) {
1114*0Sstevel@tonic-gate 		mdb_warn("failed to read ecb for aggregation %p", addr);
1115*0Sstevel@tonic-gate 		return (-1);
1116*0Sstevel@tonic-gate 	}
1117*0Sstevel@tonic-gate 
1118*0Sstevel@tonic-gate 	last = (uintptr_t)addr + offsetof(dtrace_aggregation_t, dtag_action);
1119*0Sstevel@tonic-gate 
1120*0Sstevel@tonic-gate 	/*
1121*0Sstevel@tonic-gate 	 * This is a little painful:  in order to find the number of actions,
1122*0Sstevel@tonic-gate 	 * we need to first walk through them.
1123*0Sstevel@tonic-gate 	 */
1124*0Sstevel@tonic-gate 	ap = (uintptr_t)agg.dtag_first;
1125*0Sstevel@tonic-gate 	nactions = 0;
1126*0Sstevel@tonic-gate 
1127*0Sstevel@tonic-gate 	for (;;) {
1128*0Sstevel@tonic-gate 		if (mdb_vread(&act, sizeof (act), ap) == -1) {
1129*0Sstevel@tonic-gate 			mdb_warn("failed to read action %p on aggregation %p",
1130*0Sstevel@tonic-gate 			    ap, addr);
1131*0Sstevel@tonic-gate 			return (-1);
1132*0Sstevel@tonic-gate 		}
1133*0Sstevel@tonic-gate 
1134*0Sstevel@tonic-gate 		nactions++;
1135*0Sstevel@tonic-gate 
1136*0Sstevel@tonic-gate 		if (ap == last)
1137*0Sstevel@tonic-gate 			break;
1138*0Sstevel@tonic-gate 
1139*0Sstevel@tonic-gate 		ap = (uintptr_t)act.dta_next;
1140*0Sstevel@tonic-gate 	}
1141*0Sstevel@tonic-gate 
1142*0Sstevel@tonic-gate 	lrec = &act.dta_rec;
1143*0Sstevel@tonic-gate 	agd->dtagd_size = lrec->dtrd_offset + lrec->dtrd_size - agg.dtag_base;
1144*0Sstevel@tonic-gate 
1145*0Sstevel@tonic-gate 	nrecs = agd->dtagd_nrecs;
1146*0Sstevel@tonic-gate 	agd->dtagd_nrecs = nactions;
1147*0Sstevel@tonic-gate 	agd->dtagd_epid = ecb.dte_epid;
1148*0Sstevel@tonic-gate 
1149*0Sstevel@tonic-gate 	ap = (uintptr_t)agg.dtag_first;
1150*0Sstevel@tonic-gate 	nactions = 0;
1151*0Sstevel@tonic-gate 
1152*0Sstevel@tonic-gate 	for (;;) {
1153*0Sstevel@tonic-gate 		dtrace_recdesc_t rec;
1154*0Sstevel@tonic-gate 
1155*0Sstevel@tonic-gate 		if (mdb_vread(&act, sizeof (act), ap) == -1) {
1156*0Sstevel@tonic-gate 			mdb_warn("failed to read action %p on aggregation %p",
1157*0Sstevel@tonic-gate 			    ap, addr);
1158*0Sstevel@tonic-gate 			return (-1);
1159*0Sstevel@tonic-gate 		}
1160*0Sstevel@tonic-gate 
1161*0Sstevel@tonic-gate 		if (nrecs-- == 0)
1162*0Sstevel@tonic-gate 			break;
1163*0Sstevel@tonic-gate 
1164*0Sstevel@tonic-gate 		rec = act.dta_rec;
1165*0Sstevel@tonic-gate 		rec.dtrd_offset -= agg.dtag_base;
1166*0Sstevel@tonic-gate 		rec.dtrd_uarg = 0;
1167*0Sstevel@tonic-gate 		agd->dtagd_rec[nactions++] = rec;
1168*0Sstevel@tonic-gate 
1169*0Sstevel@tonic-gate 		if (ap == last)
1170*0Sstevel@tonic-gate 			break;
1171*0Sstevel@tonic-gate 
1172*0Sstevel@tonic-gate 		ap = (uintptr_t)act.dta_next;
1173*0Sstevel@tonic-gate 	}
1174*0Sstevel@tonic-gate 
1175*0Sstevel@tonic-gate 	return (0);
1176*0Sstevel@tonic-gate }
1177*0Sstevel@tonic-gate 
1178*0Sstevel@tonic-gate static int
1179*0Sstevel@tonic-gate dtracemdb_bufsnap(dtrace_buffer_t *which, dtrace_bufdesc_t *desc)
1180*0Sstevel@tonic-gate {
1181*0Sstevel@tonic-gate 	uintptr_t addr;
1182*0Sstevel@tonic-gate 	size_t bufsize;
1183*0Sstevel@tonic-gate 	dtrace_buffer_t buf;
1184*0Sstevel@tonic-gate 	caddr_t data = desc->dtbd_data;
1185*0Sstevel@tonic-gate 	processorid_t max_cpuid, cpu = desc->dtbd_cpu;
1186*0Sstevel@tonic-gate 
1187*0Sstevel@tonic-gate 	if (mdb_readvar(&max_cpuid, "max_cpuid") == -1) {
1188*0Sstevel@tonic-gate 		mdb_warn("failed to read 'max_cpuid'");
1189*0Sstevel@tonic-gate 		errno = EIO;
1190*0Sstevel@tonic-gate 		return (-1);
1191*0Sstevel@tonic-gate 	}
1192*0Sstevel@tonic-gate 
1193*0Sstevel@tonic-gate 	if (cpu < 0 || cpu > max_cpuid) {
1194*0Sstevel@tonic-gate 		errno = EINVAL;
1195*0Sstevel@tonic-gate 		return (-1);
1196*0Sstevel@tonic-gate 	}
1197*0Sstevel@tonic-gate 
1198*0Sstevel@tonic-gate 	addr = (uintptr_t)which + cpu * sizeof (dtrace_buffer_t);
1199*0Sstevel@tonic-gate 
1200*0Sstevel@tonic-gate 	if (mdb_vread(&buf, sizeof (buf), addr) == -1) {
1201*0Sstevel@tonic-gate 		mdb_warn("failed to read buffer description at %p", addr);
1202*0Sstevel@tonic-gate 		errno = EIO;
1203*0Sstevel@tonic-gate 		return (-1);
1204*0Sstevel@tonic-gate 	}
1205*0Sstevel@tonic-gate 
1206*0Sstevel@tonic-gate 	if (buf.dtb_tomax == NULL) {
1207*0Sstevel@tonic-gate 		errno = ENOENT;
1208*0Sstevel@tonic-gate 		return (-1);
1209*0Sstevel@tonic-gate 	}
1210*0Sstevel@tonic-gate 
1211*0Sstevel@tonic-gate 	if (buf.dtb_flags & DTRACEBUF_WRAPPED) {
1212*0Sstevel@tonic-gate 		bufsize = buf.dtb_size;
1213*0Sstevel@tonic-gate 	} else {
1214*0Sstevel@tonic-gate 		bufsize = buf.dtb_offset;
1215*0Sstevel@tonic-gate 	}
1216*0Sstevel@tonic-gate 
1217*0Sstevel@tonic-gate 	if (mdb_vread(data, bufsize, (uintptr_t)buf.dtb_tomax) == -1) {
1218*0Sstevel@tonic-gate 		mdb_warn("couldn't read buffer for CPU %d", cpu);
1219*0Sstevel@tonic-gate 		errno = EIO;
1220*0Sstevel@tonic-gate 		return (-1);
1221*0Sstevel@tonic-gate 	}
1222*0Sstevel@tonic-gate 
1223*0Sstevel@tonic-gate 	if (buf.dtb_offset > buf.dtb_size) {
1224*0Sstevel@tonic-gate 		mdb_warn("buffer for CPU %d has corrupt offset\n", cpu);
1225*0Sstevel@tonic-gate 		errno = EIO;
1226*0Sstevel@tonic-gate 		return (-1);
1227*0Sstevel@tonic-gate 	}
1228*0Sstevel@tonic-gate 
1229*0Sstevel@tonic-gate 	if (buf.dtb_flags & DTRACEBUF_WRAPPED) {
1230*0Sstevel@tonic-gate 		if (buf.dtb_xamot_offset > buf.dtb_size) {
1231*0Sstevel@tonic-gate 			mdb_warn("ringbuffer for CPU %d has corrupt "
1232*0Sstevel@tonic-gate 			    "wrapped offset\n", cpu);
1233*0Sstevel@tonic-gate 			errno = EIO;
1234*0Sstevel@tonic-gate 			return (-1);
1235*0Sstevel@tonic-gate 		}
1236*0Sstevel@tonic-gate 
1237*0Sstevel@tonic-gate 		/*
1238*0Sstevel@tonic-gate 		 * If the ring buffer has wrapped, it needs to be polished.
1239*0Sstevel@tonic-gate 		 * See the comment in dtrace_buffer_polish() for details.
1240*0Sstevel@tonic-gate 		 */
1241*0Sstevel@tonic-gate 		if (buf.dtb_offset < buf.dtb_xamot_offset) {
1242*0Sstevel@tonic-gate 			bzero(data + buf.dtb_offset,
1243*0Sstevel@tonic-gate 			    buf.dtb_xamot_offset - buf.dtb_offset);
1244*0Sstevel@tonic-gate 		}
1245*0Sstevel@tonic-gate 
1246*0Sstevel@tonic-gate 		if (buf.dtb_offset > buf.dtb_xamot_offset) {
1247*0Sstevel@tonic-gate 			bzero(data + buf.dtb_offset,
1248*0Sstevel@tonic-gate 			    buf.dtb_size - buf.dtb_offset);
1249*0Sstevel@tonic-gate 			bzero(data, buf.dtb_xamot_offset);
1250*0Sstevel@tonic-gate 		}
1251*0Sstevel@tonic-gate 
1252*0Sstevel@tonic-gate 		desc->dtbd_oldest = buf.dtb_xamot_offset;
1253*0Sstevel@tonic-gate 	} else {
1254*0Sstevel@tonic-gate 		desc->dtbd_oldest = 0;
1255*0Sstevel@tonic-gate 	}
1256*0Sstevel@tonic-gate 
1257*0Sstevel@tonic-gate 	desc->dtbd_size = bufsize;
1258*0Sstevel@tonic-gate 	desc->dtbd_drops = buf.dtb_drops;
1259*0Sstevel@tonic-gate 	desc->dtbd_errors = buf.dtb_errors;
1260*0Sstevel@tonic-gate 
1261*0Sstevel@tonic-gate 	return (0);
1262*0Sstevel@tonic-gate }
1263*0Sstevel@tonic-gate 
1264*0Sstevel@tonic-gate /*
1265*0Sstevel@tonic-gate  * This is essentially identical to its cousin in the kernel.
1266*0Sstevel@tonic-gate  */
1267*0Sstevel@tonic-gate static dof_hdr_t *
1268*0Sstevel@tonic-gate dtracemdb_dof_create(dtrace_state_t *state)
1269*0Sstevel@tonic-gate {
1270*0Sstevel@tonic-gate 	dof_hdr_t *dof;
1271*0Sstevel@tonic-gate 	dof_sec_t *sec;
1272*0Sstevel@tonic-gate 	dof_optdesc_t *opt;
1273*0Sstevel@tonic-gate 	int i, len = sizeof (dof_hdr_t) +
1274*0Sstevel@tonic-gate 	    roundup(sizeof (dof_sec_t), sizeof (uint64_t)) +
1275*0Sstevel@tonic-gate 	    sizeof (dof_optdesc_t) * DTRACEOPT_MAX;
1276*0Sstevel@tonic-gate 
1277*0Sstevel@tonic-gate 	dof = mdb_zalloc(len, UM_SLEEP);
1278*0Sstevel@tonic-gate 	dof->dofh_ident[DOF_ID_MAG0] = DOF_MAG_MAG0;
1279*0Sstevel@tonic-gate 	dof->dofh_ident[DOF_ID_MAG1] = DOF_MAG_MAG1;
1280*0Sstevel@tonic-gate 	dof->dofh_ident[DOF_ID_MAG2] = DOF_MAG_MAG2;
1281*0Sstevel@tonic-gate 	dof->dofh_ident[DOF_ID_MAG3] = DOF_MAG_MAG3;
1282*0Sstevel@tonic-gate 
1283*0Sstevel@tonic-gate 	dof->dofh_ident[DOF_ID_MODEL] = DOF_MODEL_NATIVE;
1284*0Sstevel@tonic-gate 	dof->dofh_ident[DOF_ID_ENCODING] = DOF_ENCODE_NATIVE;
1285*0Sstevel@tonic-gate 	dof->dofh_ident[DOF_ID_VERSION] = DOF_VERSION_1;
1286*0Sstevel@tonic-gate 	dof->dofh_ident[DOF_ID_DIFVERS] = DIF_VERSION;
1287*0Sstevel@tonic-gate 	dof->dofh_ident[DOF_ID_DIFIREG] = DIF_DIR_NREGS;
1288*0Sstevel@tonic-gate 	dof->dofh_ident[DOF_ID_DIFTREG] = DIF_DTR_NREGS;
1289*0Sstevel@tonic-gate 
1290*0Sstevel@tonic-gate 	dof->dofh_flags = 0;
1291*0Sstevel@tonic-gate 	dof->dofh_hdrsize = sizeof (dof_hdr_t);
1292*0Sstevel@tonic-gate 	dof->dofh_secsize = sizeof (dof_sec_t);
1293*0Sstevel@tonic-gate 	dof->dofh_secnum = 1;	/* only DOF_SECT_OPTDESC */
1294*0Sstevel@tonic-gate 	dof->dofh_secoff = sizeof (dof_hdr_t);
1295*0Sstevel@tonic-gate 	dof->dofh_loadsz = len;
1296*0Sstevel@tonic-gate 	dof->dofh_filesz = len;
1297*0Sstevel@tonic-gate 	dof->dofh_pad = 0;
1298*0Sstevel@tonic-gate 
1299*0Sstevel@tonic-gate 	/*
1300*0Sstevel@tonic-gate 	 * Fill in the option section header...
1301*0Sstevel@tonic-gate 	 */
1302*0Sstevel@tonic-gate 	sec = (dof_sec_t *)((uintptr_t)dof + sizeof (dof_hdr_t));
1303*0Sstevel@tonic-gate 	sec->dofs_type = DOF_SECT_OPTDESC;
1304*0Sstevel@tonic-gate 	sec->dofs_align = sizeof (uint64_t);
1305*0Sstevel@tonic-gate 	sec->dofs_flags = DOF_SECF_LOAD;
1306*0Sstevel@tonic-gate 	sec->dofs_entsize = sizeof (dof_optdesc_t);
1307*0Sstevel@tonic-gate 
1308*0Sstevel@tonic-gate 	opt = (dof_optdesc_t *)((uintptr_t)sec +
1309*0Sstevel@tonic-gate 	    roundup(sizeof (dof_sec_t), sizeof (uint64_t)));
1310*0Sstevel@tonic-gate 
1311*0Sstevel@tonic-gate 	sec->dofs_offset = (uintptr_t)opt - (uintptr_t)dof;
1312*0Sstevel@tonic-gate 	sec->dofs_size = sizeof (dof_optdesc_t) * DTRACEOPT_MAX;
1313*0Sstevel@tonic-gate 
1314*0Sstevel@tonic-gate 	for (i = 0; i < DTRACEOPT_MAX; i++) {
1315*0Sstevel@tonic-gate 		opt[i].dofo_option = i;
1316*0Sstevel@tonic-gate 		opt[i].dofo_strtab = DOF_SECIDX_NONE;
1317*0Sstevel@tonic-gate 		opt[i].dofo_value = state->dts_options[i];
1318*0Sstevel@tonic-gate 	}
1319*0Sstevel@tonic-gate 
1320*0Sstevel@tonic-gate 	return (dof);
1321*0Sstevel@tonic-gate }
1322*0Sstevel@tonic-gate 
1323*0Sstevel@tonic-gate static int
1324*0Sstevel@tonic-gate dtracemdb_format(dtrace_state_t *state, dtrace_fmtdesc_t *desc)
1325*0Sstevel@tonic-gate {
1326*0Sstevel@tonic-gate 	uintptr_t addr, faddr;
1327*0Sstevel@tonic-gate 	char c;
1328*0Sstevel@tonic-gate 	int len = 0;
1329*0Sstevel@tonic-gate 
1330*0Sstevel@tonic-gate 	if (desc->dtfd_format == 0 || desc->dtfd_format > state->dts_nformats) {
1331*0Sstevel@tonic-gate 		errno = EINVAL;
1332*0Sstevel@tonic-gate 		return (-1);
1333*0Sstevel@tonic-gate 	}
1334*0Sstevel@tonic-gate 
1335*0Sstevel@tonic-gate 	faddr = (uintptr_t)state->dts_formats +
1336*0Sstevel@tonic-gate 	    (desc->dtfd_format - 1) * sizeof (char *);
1337*0Sstevel@tonic-gate 
1338*0Sstevel@tonic-gate 	if (mdb_vread(&addr, sizeof (addr), faddr) == -1) {
1339*0Sstevel@tonic-gate 		mdb_warn("failed to read format string pointer at %p", faddr);
1340*0Sstevel@tonic-gate 		return (-1);
1341*0Sstevel@tonic-gate 	}
1342*0Sstevel@tonic-gate 
1343*0Sstevel@tonic-gate 	do {
1344*0Sstevel@tonic-gate 		if (mdb_vread(&c, sizeof (c), addr + len++) == -1) {
1345*0Sstevel@tonic-gate 			mdb_warn("failed to read format string at %p", addr);
1346*0Sstevel@tonic-gate 			return (-1);
1347*0Sstevel@tonic-gate 		}
1348*0Sstevel@tonic-gate 	} while (c != '\0');
1349*0Sstevel@tonic-gate 
1350*0Sstevel@tonic-gate 	if (len > desc->dtfd_length) {
1351*0Sstevel@tonic-gate 		desc->dtfd_length = len;
1352*0Sstevel@tonic-gate 		return (0);
1353*0Sstevel@tonic-gate 	}
1354*0Sstevel@tonic-gate 
1355*0Sstevel@tonic-gate 	if (mdb_vread(desc->dtfd_string, len, addr) == -1) {
1356*0Sstevel@tonic-gate 		mdb_warn("failed to reread format string at %p", addr);
1357*0Sstevel@tonic-gate 		return (-1);
1358*0Sstevel@tonic-gate 	}
1359*0Sstevel@tonic-gate 
1360*0Sstevel@tonic-gate 	return (0);
1361*0Sstevel@tonic-gate }
1362*0Sstevel@tonic-gate 
1363*0Sstevel@tonic-gate static int
1364*0Sstevel@tonic-gate dtracemdb_status(dtrace_state_t *state, dtrace_status_t *status)
1365*0Sstevel@tonic-gate {
1366*0Sstevel@tonic-gate 	dtrace_dstate_t *dstate;
1367*0Sstevel@tonic-gate 	int i, j;
1368*0Sstevel@tonic-gate 	uint64_t nerrs;
1369*0Sstevel@tonic-gate 	uintptr_t addr;
1370*0Sstevel@tonic-gate 	int ncpu;
1371*0Sstevel@tonic-gate 
1372*0Sstevel@tonic-gate 	if (mdb_readvar(&ncpu, "_ncpu") == -1) {
1373*0Sstevel@tonic-gate 		mdb_warn("failed to read '_ncpu'");
1374*0Sstevel@tonic-gate 		return (DCMD_ERR);
1375*0Sstevel@tonic-gate 	}
1376*0Sstevel@tonic-gate 
1377*0Sstevel@tonic-gate 	bzero(status, sizeof (dtrace_status_t));
1378*0Sstevel@tonic-gate 
1379*0Sstevel@tonic-gate 	if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE) {
1380*0Sstevel@tonic-gate 		errno = ENOENT;
1381*0Sstevel@tonic-gate 		return (-1);
1382*0Sstevel@tonic-gate 	}
1383*0Sstevel@tonic-gate 
1384*0Sstevel@tonic-gate 	/*
1385*0Sstevel@tonic-gate 	 * For the MDB backend, we never set dtst_exiting or dtst_filled.  This
1386*0Sstevel@tonic-gate 	 * is by design:  we don't want the library to try to stop tracing,
1387*0Sstevel@tonic-gate 	 * because it doesn't particularly mean anything.
1388*0Sstevel@tonic-gate 	 */
1389*0Sstevel@tonic-gate 	nerrs = state->dts_errors;
1390*0Sstevel@tonic-gate 	dstate = &state->dts_vstate.dtvs_dynvars;
1391*0Sstevel@tonic-gate 
1392*0Sstevel@tonic-gate 	for (i = 0; i < ncpu; i++) {
1393*0Sstevel@tonic-gate 		dtrace_dstate_percpu_t dcpu;
1394*0Sstevel@tonic-gate 		dtrace_buffer_t buf;
1395*0Sstevel@tonic-gate 
1396*0Sstevel@tonic-gate 		addr = (uintptr_t)&dstate->dtds_percpu[i];
1397*0Sstevel@tonic-gate 
1398*0Sstevel@tonic-gate 		if (mdb_vread(&dcpu, sizeof (dcpu), addr) == -1) {
1399*0Sstevel@tonic-gate 			mdb_warn("failed to read per-CPU dstate at %p", addr);
1400*0Sstevel@tonic-gate 			return (-1);
1401*0Sstevel@tonic-gate 		}
1402*0Sstevel@tonic-gate 
1403*0Sstevel@tonic-gate 		status->dtst_dyndrops += dcpu.dtdsc_drops;
1404*0Sstevel@tonic-gate 		status->dtst_dyndrops_dirty += dcpu.dtdsc_dirty_drops;
1405*0Sstevel@tonic-gate 		status->dtst_dyndrops_rinsing += dcpu.dtdsc_rinsing_drops;
1406*0Sstevel@tonic-gate 
1407*0Sstevel@tonic-gate 		addr = (uintptr_t)&state->dts_buffer[i];
1408*0Sstevel@tonic-gate 
1409*0Sstevel@tonic-gate 		if (mdb_vread(&buf, sizeof (buf), addr) == -1) {
1410*0Sstevel@tonic-gate 			mdb_warn("failed to read per-CPU buffer at %p", addr);
1411*0Sstevel@tonic-gate 			return (-1);
1412*0Sstevel@tonic-gate 		}
1413*0Sstevel@tonic-gate 
1414*0Sstevel@tonic-gate 		nerrs += buf.dtb_errors;
1415*0Sstevel@tonic-gate 
1416*0Sstevel@tonic-gate 		for (j = 0; j < state->dts_nspeculations; j++) {
1417*0Sstevel@tonic-gate 			dtrace_speculation_t spec;
1418*0Sstevel@tonic-gate 
1419*0Sstevel@tonic-gate 			addr = (uintptr_t)&state->dts_speculations[j];
1420*0Sstevel@tonic-gate 
1421*0Sstevel@tonic-gate 			if (mdb_vread(&spec, sizeof (spec), addr) == -1) {
1422*0Sstevel@tonic-gate 				mdb_warn("failed to read "
1423*0Sstevel@tonic-gate 				    "speculation at %p", addr);
1424*0Sstevel@tonic-gate 				return (-1);
1425*0Sstevel@tonic-gate 			}
1426*0Sstevel@tonic-gate 
1427*0Sstevel@tonic-gate 			addr = (uintptr_t)&spec.dtsp_buffer[i];
1428*0Sstevel@tonic-gate 
1429*0Sstevel@tonic-gate 			if (mdb_vread(&buf, sizeof (buf), addr) == -1) {
1430*0Sstevel@tonic-gate 				mdb_warn("failed to read "
1431*0Sstevel@tonic-gate 				    "speculative buffer at %p", addr);
1432*0Sstevel@tonic-gate 				return (-1);
1433*0Sstevel@tonic-gate 			}
1434*0Sstevel@tonic-gate 
1435*0Sstevel@tonic-gate 			status->dtst_specdrops += buf.dtb_xamot_drops;
1436*0Sstevel@tonic-gate 		}
1437*0Sstevel@tonic-gate 	}
1438*0Sstevel@tonic-gate 
1439*0Sstevel@tonic-gate 	status->dtst_specdrops_busy = state->dts_speculations_busy;
1440*0Sstevel@tonic-gate 	status->dtst_specdrops_unavail = state->dts_speculations_unavail;
1441*0Sstevel@tonic-gate 	status->dtst_errors = nerrs;
1442*0Sstevel@tonic-gate 
1443*0Sstevel@tonic-gate 	return (0);
1444*0Sstevel@tonic-gate }
1445*0Sstevel@tonic-gate 
1446*0Sstevel@tonic-gate typedef struct dtracemdb_data {
1447*0Sstevel@tonic-gate 	dtrace_state_t *dtmd_state;
1448*0Sstevel@tonic-gate 	char *dtmd_symstr;
1449*0Sstevel@tonic-gate 	char *dtmd_modstr;
1450*0Sstevel@tonic-gate 	uintptr_t dtmd_addr;
1451*0Sstevel@tonic-gate } dtracemdb_data_t;
1452*0Sstevel@tonic-gate 
1453*0Sstevel@tonic-gate static int
1454*0Sstevel@tonic-gate dtracemdb_ioctl(void *varg, int cmd, void *arg)
1455*0Sstevel@tonic-gate {
1456*0Sstevel@tonic-gate 	dtracemdb_data_t *data = varg;
1457*0Sstevel@tonic-gate 	dtrace_state_t *state = data->dtmd_state;
1458*0Sstevel@tonic-gate 
1459*0Sstevel@tonic-gate 	switch (cmd) {
1460*0Sstevel@tonic-gate 	case DTRACEIOC_CONF: {
1461*0Sstevel@tonic-gate 		dtrace_conf_t *conf = arg;
1462*0Sstevel@tonic-gate 
1463*0Sstevel@tonic-gate 		bzero(conf, sizeof (conf));
1464*0Sstevel@tonic-gate 		conf->dtc_difversion = DIF_VERSION;
1465*0Sstevel@tonic-gate 		conf->dtc_difintregs = DIF_DIR_NREGS;
1466*0Sstevel@tonic-gate 		conf->dtc_diftupregs = DIF_DTR_NREGS;
1467*0Sstevel@tonic-gate 		conf->dtc_ctfmodel = CTF_MODEL_NATIVE;
1468*0Sstevel@tonic-gate 
1469*0Sstevel@tonic-gate 		return (0);
1470*0Sstevel@tonic-gate 	}
1471*0Sstevel@tonic-gate 
1472*0Sstevel@tonic-gate 	case DTRACEIOC_DOFGET: {
1473*0Sstevel@tonic-gate 		dof_hdr_t *hdr = arg, *dof;
1474*0Sstevel@tonic-gate 
1475*0Sstevel@tonic-gate 		dof = dtracemdb_dof_create(state);
1476*0Sstevel@tonic-gate 		bcopy(dof, hdr, MIN(hdr->dofh_loadsz, dof->dofh_loadsz));
1477*0Sstevel@tonic-gate 		mdb_free(dof, dof->dofh_loadsz);
1478*0Sstevel@tonic-gate 
1479*0Sstevel@tonic-gate 		return (0);
1480*0Sstevel@tonic-gate 	}
1481*0Sstevel@tonic-gate 
1482*0Sstevel@tonic-gate 	case DTRACEIOC_BUFSNAP:
1483*0Sstevel@tonic-gate 		return (dtracemdb_bufsnap(state->dts_buffer, arg));
1484*0Sstevel@tonic-gate 
1485*0Sstevel@tonic-gate 	case DTRACEIOC_AGGSNAP:
1486*0Sstevel@tonic-gate 		return (dtracemdb_bufsnap(state->dts_aggbuffer, arg));
1487*0Sstevel@tonic-gate 
1488*0Sstevel@tonic-gate 	case DTRACEIOC_AGGDESC:
1489*0Sstevel@tonic-gate 		return (dtracemdb_aggdesc(state, arg));
1490*0Sstevel@tonic-gate 
1491*0Sstevel@tonic-gate 	case DTRACEIOC_EPROBE:
1492*0Sstevel@tonic-gate 		return (dtracemdb_eprobe(state, arg));
1493*0Sstevel@tonic-gate 
1494*0Sstevel@tonic-gate 	case DTRACEIOC_PROBES:
1495*0Sstevel@tonic-gate 		return (dtracemdb_probe(state, arg));
1496*0Sstevel@tonic-gate 
1497*0Sstevel@tonic-gate 	case DTRACEIOC_FORMAT:
1498*0Sstevel@tonic-gate 		return (dtracemdb_format(state, arg));
1499*0Sstevel@tonic-gate 
1500*0Sstevel@tonic-gate 	case DTRACEIOC_STATUS:
1501*0Sstevel@tonic-gate 		return (dtracemdb_status(state, arg));
1502*0Sstevel@tonic-gate 
1503*0Sstevel@tonic-gate 	case DTRACEIOC_GO:
1504*0Sstevel@tonic-gate 		*(processorid_t *)arg = -1;
1505*0Sstevel@tonic-gate 		return (0);
1506*0Sstevel@tonic-gate 
1507*0Sstevel@tonic-gate 	case DTRACEIOC_ENABLE:
1508*0Sstevel@tonic-gate 		errno = ENOTTY; /* see dt_open.c:dtrace_go() */
1509*0Sstevel@tonic-gate 		return (-1);
1510*0Sstevel@tonic-gate 
1511*0Sstevel@tonic-gate 	case DTRACEIOC_PROVIDER:
1512*0Sstevel@tonic-gate 	case DTRACEIOC_PROBEMATCH:
1513*0Sstevel@tonic-gate 		errno = ESRCH;
1514*0Sstevel@tonic-gate 		return (-1);
1515*0Sstevel@tonic-gate 
1516*0Sstevel@tonic-gate 	default:
1517*0Sstevel@tonic-gate 		mdb_warn("unexpected ioctl 0x%x (%s)\n", cmd,
1518*0Sstevel@tonic-gate 		    cmd == DTRACEIOC_PROVIDER	? "DTRACEIOC_PROVIDER" :
1519*0Sstevel@tonic-gate 		    cmd == DTRACEIOC_PROBES	? "DTRACEIOC_PROBES" :
1520*0Sstevel@tonic-gate 		    cmd == DTRACEIOC_BUFSNAP	? "DTRACEIOC_BUFSNAP" :
1521*0Sstevel@tonic-gate 		    cmd == DTRACEIOC_PROBEMATCH	? "DTRACEIOC_PROBEMATCH" :
1522*0Sstevel@tonic-gate 		    cmd == DTRACEIOC_ENABLE	? "DTRACEIOC_ENABLE" :
1523*0Sstevel@tonic-gate 		    cmd == DTRACEIOC_AGGSNAP	? "DTRACEIOC_AGGSNAP" :
1524*0Sstevel@tonic-gate 		    cmd == DTRACEIOC_EPROBE	? "DTRACEIOC_EPROBE" :
1525*0Sstevel@tonic-gate 		    cmd == DTRACEIOC_PROBEARG	? "DTRACEIOC_PROBEARG" :
1526*0Sstevel@tonic-gate 		    cmd == DTRACEIOC_CONF	? "DTRACEIOC_CONF" :
1527*0Sstevel@tonic-gate 		    cmd == DTRACEIOC_STATUS	? "DTRACEIOC_STATUS" :
1528*0Sstevel@tonic-gate 		    cmd == DTRACEIOC_GO		? "DTRACEIOC_GO" :
1529*0Sstevel@tonic-gate 		    cmd == DTRACEIOC_STOP	? "DTRACEIOC_STOP" :
1530*0Sstevel@tonic-gate 		    cmd == DTRACEIOC_AGGDESC	? "DTRACEIOC_AGGDESC" :
1531*0Sstevel@tonic-gate 		    cmd == DTRACEIOC_FORMAT	? "DTRACEIOC_FORMAT" :
1532*0Sstevel@tonic-gate 		    cmd == DTRACEIOC_DOFGET	? "DTRACEIOC_DOFGET" :
1533*0Sstevel@tonic-gate 		    cmd == DTRACEIOC_REPLICATE	? "DTRACEIOC_REPLICATE" :
1534*0Sstevel@tonic-gate 		    "???");
1535*0Sstevel@tonic-gate 		errno = ENXIO;
1536*0Sstevel@tonic-gate 		return (-1);
1537*0Sstevel@tonic-gate 	}
1538*0Sstevel@tonic-gate }
1539*0Sstevel@tonic-gate 
1540*0Sstevel@tonic-gate static int
1541*0Sstevel@tonic-gate dtracemdb_modctl(uintptr_t addr, const struct modctl *m, dtracemdb_data_t *data)
1542*0Sstevel@tonic-gate {
1543*0Sstevel@tonic-gate 	struct module mod;
1544*0Sstevel@tonic-gate 
1545*0Sstevel@tonic-gate 	if (m->mod_mp == NULL)
1546*0Sstevel@tonic-gate 		return (WALK_NEXT);
1547*0Sstevel@tonic-gate 
1548*0Sstevel@tonic-gate 	if (mdb_vread(&mod, sizeof (mod), (uintptr_t)m->mod_mp) == -1) {
1549*0Sstevel@tonic-gate 		mdb_warn("couldn't read modctl %p's module", addr);
1550*0Sstevel@tonic-gate 		return (WALK_NEXT);
1551*0Sstevel@tonic-gate 	}
1552*0Sstevel@tonic-gate 
1553*0Sstevel@tonic-gate 	if ((uintptr_t)mod.text > data->dtmd_addr)
1554*0Sstevel@tonic-gate 		return (WALK_NEXT);
1555*0Sstevel@tonic-gate 
1556*0Sstevel@tonic-gate 	if ((uintptr_t)mod.text + mod.text_size <= data->dtmd_addr)
1557*0Sstevel@tonic-gate 		return (WALK_NEXT);
1558*0Sstevel@tonic-gate 
1559*0Sstevel@tonic-gate 	if (mdb_readstr(data->dtmd_modstr, MDB_SYM_NAMLEN,
1560*0Sstevel@tonic-gate 	    (uintptr_t)m->mod_modname) == -1)
1561*0Sstevel@tonic-gate 		return (WALK_ERR);
1562*0Sstevel@tonic-gate 
1563*0Sstevel@tonic-gate 	return (WALK_DONE);
1564*0Sstevel@tonic-gate }
1565*0Sstevel@tonic-gate 
1566*0Sstevel@tonic-gate static int
1567*0Sstevel@tonic-gate dtracemdb_lookup_by_addr(void *varg, GElf_Addr addr, GElf_Sym *symp,
1568*0Sstevel@tonic-gate     dtrace_syminfo_t *sip)
1569*0Sstevel@tonic-gate {
1570*0Sstevel@tonic-gate 	dtracemdb_data_t *data = varg;
1571*0Sstevel@tonic-gate 
1572*0Sstevel@tonic-gate 	if (data->dtmd_symstr == NULL) {
1573*0Sstevel@tonic-gate 		data->dtmd_symstr = mdb_zalloc(MDB_SYM_NAMLEN,
1574*0Sstevel@tonic-gate 		    UM_SLEEP | UM_GC);
1575*0Sstevel@tonic-gate 	}
1576*0Sstevel@tonic-gate 
1577*0Sstevel@tonic-gate 	if (data->dtmd_modstr == NULL) {
1578*0Sstevel@tonic-gate 		data->dtmd_modstr = mdb_zalloc(MDB_SYM_NAMLEN,
1579*0Sstevel@tonic-gate 		    UM_SLEEP | UM_GC);
1580*0Sstevel@tonic-gate 	}
1581*0Sstevel@tonic-gate 
1582*0Sstevel@tonic-gate 	if (symp != NULL) {
1583*0Sstevel@tonic-gate 		if (mdb_lookup_by_addr(addr, MDB_SYM_FUZZY, data->dtmd_symstr,
1584*0Sstevel@tonic-gate 		    MDB_SYM_NAMLEN, symp) == -1)
1585*0Sstevel@tonic-gate 			return (-1);
1586*0Sstevel@tonic-gate 	}
1587*0Sstevel@tonic-gate 
1588*0Sstevel@tonic-gate 	if (sip != NULL) {
1589*0Sstevel@tonic-gate 		data->dtmd_addr = addr;
1590*0Sstevel@tonic-gate 
1591*0Sstevel@tonic-gate 		(void) strcpy(data->dtmd_modstr, "???");
1592*0Sstevel@tonic-gate 
1593*0Sstevel@tonic-gate 		if (mdb_walk("modctl",
1594*0Sstevel@tonic-gate 		    (mdb_walk_cb_t)dtracemdb_modctl, varg) == -1) {
1595*0Sstevel@tonic-gate 			mdb_warn("couldn't walk 'modctl'");
1596*0Sstevel@tonic-gate 			return (-1);
1597*0Sstevel@tonic-gate 		}
1598*0Sstevel@tonic-gate 
1599*0Sstevel@tonic-gate 		sip->dts_object = data->dtmd_modstr;
1600*0Sstevel@tonic-gate 		sip->dts_id = 0;
1601*0Sstevel@tonic-gate 		sip->dts_name = symp != NULL ? data->dtmd_symstr : NULL;
1602*0Sstevel@tonic-gate 	}
1603*0Sstevel@tonic-gate 
1604*0Sstevel@tonic-gate 	return (0);
1605*0Sstevel@tonic-gate }
1606*0Sstevel@tonic-gate 
1607*0Sstevel@tonic-gate /*ARGSUSED*/
1608*0Sstevel@tonic-gate static int
1609*0Sstevel@tonic-gate dtracemdb_stat(void *varg, processorid_t cpu)
1610*0Sstevel@tonic-gate {
1611*0Sstevel@tonic-gate 	GElf_Sym sym;
1612*0Sstevel@tonic-gate 	cpu_t c;
1613*0Sstevel@tonic-gate 	uintptr_t caddr, addr;
1614*0Sstevel@tonic-gate 
1615*0Sstevel@tonic-gate 	if (mdb_lookup_by_name("cpu", &sym) == -1) {
1616*0Sstevel@tonic-gate 		mdb_warn("failed to find symbol for 'cpu'");
1617*0Sstevel@tonic-gate 		return (-1);
1618*0Sstevel@tonic-gate 	}
1619*0Sstevel@tonic-gate 
1620*0Sstevel@tonic-gate 	if (cpu * sizeof (uintptr_t) > sym.st_size)
1621*0Sstevel@tonic-gate 		return (-1);
1622*0Sstevel@tonic-gate 
1623*0Sstevel@tonic-gate 	addr = (uintptr_t)sym.st_value + cpu * sizeof (uintptr_t);
1624*0Sstevel@tonic-gate 
1625*0Sstevel@tonic-gate 	if (mdb_vread(&caddr, sizeof (caddr), addr) == -1) {
1626*0Sstevel@tonic-gate 		mdb_warn("failed to read cpu[%d]", cpu);
1627*0Sstevel@tonic-gate 		return (-1);
1628*0Sstevel@tonic-gate 	}
1629*0Sstevel@tonic-gate 
1630*0Sstevel@tonic-gate 	if (caddr == NULL)
1631*0Sstevel@tonic-gate 		return (-1);
1632*0Sstevel@tonic-gate 
1633*0Sstevel@tonic-gate 	if (mdb_vread(&c, sizeof (c), caddr) == -1) {
1634*0Sstevel@tonic-gate 		mdb_warn("failed to read cpu at %p", caddr);
1635*0Sstevel@tonic-gate 		return (-1);
1636*0Sstevel@tonic-gate 	}
1637*0Sstevel@tonic-gate 
1638*0Sstevel@tonic-gate 	if (c.cpu_flags & CPU_POWEROFF) {
1639*0Sstevel@tonic-gate 		return (P_POWEROFF);
1640*0Sstevel@tonic-gate 	} else if (c.cpu_flags & CPU_SPARE) {
1641*0Sstevel@tonic-gate 		return (P_SPARE);
1642*0Sstevel@tonic-gate 	} else if (c.cpu_flags & CPU_FAULTED) {
1643*0Sstevel@tonic-gate 		return (P_FAULTED);
1644*0Sstevel@tonic-gate 	} else if ((c.cpu_flags & (CPU_READY | CPU_OFFLINE)) != CPU_READY) {
1645*0Sstevel@tonic-gate 		return (P_OFFLINE);
1646*0Sstevel@tonic-gate 	} else if (c.cpu_flags & CPU_ENABLE) {
1647*0Sstevel@tonic-gate 		return (P_ONLINE);
1648*0Sstevel@tonic-gate 	} else {
1649*0Sstevel@tonic-gate 		return (P_NOINTR);
1650*0Sstevel@tonic-gate 	}
1651*0Sstevel@tonic-gate }
1652*0Sstevel@tonic-gate 
1653*0Sstevel@tonic-gate /*ARGSUSED*/
1654*0Sstevel@tonic-gate static long
1655*0Sstevel@tonic-gate dtracemdb_sysconf(void *varg, int name)
1656*0Sstevel@tonic-gate {
1657*0Sstevel@tonic-gate 	int max_ncpus;
1658*0Sstevel@tonic-gate 	processorid_t max_cpuid;
1659*0Sstevel@tonic-gate 
1660*0Sstevel@tonic-gate 	switch (name) {
1661*0Sstevel@tonic-gate 	case _SC_CPUID_MAX:
1662*0Sstevel@tonic-gate 		if (mdb_readvar(&max_cpuid, "max_cpuid") == -1) {
1663*0Sstevel@tonic-gate 			mdb_warn("failed to read 'max_cpuid'");
1664*0Sstevel@tonic-gate 			return (-1);
1665*0Sstevel@tonic-gate 		}
1666*0Sstevel@tonic-gate 
1667*0Sstevel@tonic-gate 		return (max_cpuid);
1668*0Sstevel@tonic-gate 
1669*0Sstevel@tonic-gate 	case _SC_NPROCESSORS_MAX:
1670*0Sstevel@tonic-gate 		if (mdb_readvar(&max_ncpus, "max_ncpus") == -1) {
1671*0Sstevel@tonic-gate 			mdb_warn("failed to read 'max_ncpus'");
1672*0Sstevel@tonic-gate 			return (-1);
1673*0Sstevel@tonic-gate 		}
1674*0Sstevel@tonic-gate 
1675*0Sstevel@tonic-gate 		return (max_ncpus);
1676*0Sstevel@tonic-gate 
1677*0Sstevel@tonic-gate 	default:
1678*0Sstevel@tonic-gate 		mdb_warn("unexpected sysconf code %d\n", name);
1679*0Sstevel@tonic-gate 		return (-1);
1680*0Sstevel@tonic-gate 	}
1681*0Sstevel@tonic-gate }
1682*0Sstevel@tonic-gate 
1683*0Sstevel@tonic-gate const dtrace_vector_t dtrace_mdbops = {
1684*0Sstevel@tonic-gate 	dtracemdb_ioctl,
1685*0Sstevel@tonic-gate 	dtracemdb_lookup_by_addr,
1686*0Sstevel@tonic-gate 	dtracemdb_stat,
1687*0Sstevel@tonic-gate 	dtracemdb_sysconf
1688*0Sstevel@tonic-gate };
1689*0Sstevel@tonic-gate 
1690*0Sstevel@tonic-gate typedef struct dtrace_dcmddata {
1691*0Sstevel@tonic-gate 	dtrace_hdl_t *dtdd_dtp;
1692*0Sstevel@tonic-gate 	int dtdd_cpu;
1693*0Sstevel@tonic-gate 	int dtdd_quiet;
1694*0Sstevel@tonic-gate 	int dtdd_flowindent;
1695*0Sstevel@tonic-gate 	int dtdd_heading;
1696*0Sstevel@tonic-gate } dtrace_dcmddata_t;
1697*0Sstevel@tonic-gate 
1698*0Sstevel@tonic-gate /*ARGSUSED*/
1699*0Sstevel@tonic-gate static int
1700*0Sstevel@tonic-gate dtrace_dcmdrec(const dtrace_probedata_t *data,
1701*0Sstevel@tonic-gate     const dtrace_recdesc_t *rec, void *arg)
1702*0Sstevel@tonic-gate {
1703*0Sstevel@tonic-gate 	dtrace_dcmddata_t *dd = arg;
1704*0Sstevel@tonic-gate 
1705*0Sstevel@tonic-gate 	if (rec == NULL) {
1706*0Sstevel@tonic-gate 		/*
1707*0Sstevel@tonic-gate 		 * We have processed the final record; output the newline if
1708*0Sstevel@tonic-gate 		 * we're not in quiet mode.
1709*0Sstevel@tonic-gate 		 */
1710*0Sstevel@tonic-gate 		if (!dd->dtdd_quiet)
1711*0Sstevel@tonic-gate 			mdb_printf("\n");
1712*0Sstevel@tonic-gate 
1713*0Sstevel@tonic-gate 		return (DTRACE_CONSUME_NEXT);
1714*0Sstevel@tonic-gate 	}
1715*0Sstevel@tonic-gate 
1716*0Sstevel@tonic-gate 	return (DTRACE_CONSUME_THIS);
1717*0Sstevel@tonic-gate }
1718*0Sstevel@tonic-gate 
1719*0Sstevel@tonic-gate /*ARGSUSED*/
1720*0Sstevel@tonic-gate static int
1721*0Sstevel@tonic-gate dtrace_dcmdprobe(const dtrace_probedata_t *data, void *arg)
1722*0Sstevel@tonic-gate {
1723*0Sstevel@tonic-gate 	dtrace_probedesc_t *pd = data->dtpda_pdesc;
1724*0Sstevel@tonic-gate 	processorid_t cpu = data->dtpda_cpu;
1725*0Sstevel@tonic-gate 	dtrace_dcmddata_t *dd = arg;
1726*0Sstevel@tonic-gate 	char name[DTRACE_FUNCNAMELEN + DTRACE_NAMELEN + 2];
1727*0Sstevel@tonic-gate 
1728*0Sstevel@tonic-gate 	if (dd->dtdd_cpu != -1UL && dd->dtdd_cpu != cpu)
1729*0Sstevel@tonic-gate 		return (DTRACE_CONSUME_NEXT);
1730*0Sstevel@tonic-gate 
1731*0Sstevel@tonic-gate 	if (dd->dtdd_heading == 0) {
1732*0Sstevel@tonic-gate 		if (!dd->dtdd_flowindent) {
1733*0Sstevel@tonic-gate 			if (!dd->dtdd_quiet) {
1734*0Sstevel@tonic-gate 				mdb_printf("%3s %6s %32s\n",
1735*0Sstevel@tonic-gate 				    "CPU", "ID", "FUNCTION:NAME");
1736*0Sstevel@tonic-gate 			}
1737*0Sstevel@tonic-gate 		} else {
1738*0Sstevel@tonic-gate 			mdb_printf("%3s %-41s\n", "CPU", "FUNCTION");
1739*0Sstevel@tonic-gate 		}
1740*0Sstevel@tonic-gate 		dd->dtdd_heading = 1;
1741*0Sstevel@tonic-gate 	}
1742*0Sstevel@tonic-gate 
1743*0Sstevel@tonic-gate 	if (!dd->dtdd_flowindent) {
1744*0Sstevel@tonic-gate 		if (!dd->dtdd_quiet) {
1745*0Sstevel@tonic-gate 			(void) mdb_snprintf(name, sizeof (name), "%s:%s",
1746*0Sstevel@tonic-gate 			    pd->dtpd_func, pd->dtpd_name);
1747*0Sstevel@tonic-gate 
1748*0Sstevel@tonic-gate 			mdb_printf("%3d %6d %32s ", cpu, pd->dtpd_id, name);
1749*0Sstevel@tonic-gate 		}
1750*0Sstevel@tonic-gate 	} else {
1751*0Sstevel@tonic-gate 		int indent = data->dtpda_indent;
1752*0Sstevel@tonic-gate 
1753*0Sstevel@tonic-gate 		if (data->dtpda_flow == DTRACEFLOW_NONE) {
1754*0Sstevel@tonic-gate 			(void) mdb_snprintf(name, sizeof (name), "%*s%s%s:%s",
1755*0Sstevel@tonic-gate 			    indent, "", data->dtpda_prefix, pd->dtpd_func,
1756*0Sstevel@tonic-gate 			    pd->dtpd_name);
1757*0Sstevel@tonic-gate 		} else {
1758*0Sstevel@tonic-gate 			(void) mdb_snprintf(name, sizeof (name), "%*s%s%s",
1759*0Sstevel@tonic-gate 			    indent, "", data->dtpda_prefix, pd->dtpd_func);
1760*0Sstevel@tonic-gate 		}
1761*0Sstevel@tonic-gate 
1762*0Sstevel@tonic-gate 		mdb_printf("%3d %-41s ", cpu, name);
1763*0Sstevel@tonic-gate 	}
1764*0Sstevel@tonic-gate 
1765*0Sstevel@tonic-gate 	return (DTRACE_CONSUME_THIS);
1766*0Sstevel@tonic-gate }
1767*0Sstevel@tonic-gate 
1768*0Sstevel@tonic-gate /*ARGSUSED*/
1769*0Sstevel@tonic-gate static int
1770*0Sstevel@tonic-gate dtrace_dcmderr(dtrace_errdata_t *data, void *arg)
1771*0Sstevel@tonic-gate {
1772*0Sstevel@tonic-gate 	mdb_warn(data->dteda_msg);
1773*0Sstevel@tonic-gate 	return (DTRACE_HANDLE_OK);
1774*0Sstevel@tonic-gate }
1775*0Sstevel@tonic-gate 
1776*0Sstevel@tonic-gate /*ARGSUSED*/
1777*0Sstevel@tonic-gate static int
1778*0Sstevel@tonic-gate dtrace_dcmddrop(dtrace_dropdata_t *data, void *arg)
1779*0Sstevel@tonic-gate {
1780*0Sstevel@tonic-gate 	mdb_warn(data->dtdda_msg);
1781*0Sstevel@tonic-gate 	return (DTRACE_HANDLE_OK);
1782*0Sstevel@tonic-gate }
1783*0Sstevel@tonic-gate 
1784*0Sstevel@tonic-gate /*ARGSUSED*/
1785*0Sstevel@tonic-gate static int
1786*0Sstevel@tonic-gate dtrace_dcmdbuffered(dtrace_bufdata_t *bufdata, void *arg)
1787*0Sstevel@tonic-gate {
1788*0Sstevel@tonic-gate 	mdb_printf("%s", bufdata->dtbda_buffered);
1789*0Sstevel@tonic-gate 	return (DTRACE_HANDLE_OK);
1790*0Sstevel@tonic-gate }
1791*0Sstevel@tonic-gate 
1792*0Sstevel@tonic-gate /*ARGSUSED*/
1793*0Sstevel@tonic-gate int
1794*0Sstevel@tonic-gate dtrace(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1795*0Sstevel@tonic-gate {
1796*0Sstevel@tonic-gate 	dtrace_state_t state;
1797*0Sstevel@tonic-gate 	dtrace_hdl_t *dtp;
1798*0Sstevel@tonic-gate 	int ncpu, err;
1799*0Sstevel@tonic-gate 	uintptr_t c = -1UL;
1800*0Sstevel@tonic-gate 	dtrace_dcmddata_t dd;
1801*0Sstevel@tonic-gate 	dtrace_optval_t val;
1802*0Sstevel@tonic-gate 	dtracemdb_data_t md;
1803*0Sstevel@tonic-gate 	int rval = DCMD_ERR;
1804*0Sstevel@tonic-gate 
1805*0Sstevel@tonic-gate 	if (!(flags & DCMD_ADDRSPEC))
1806*0Sstevel@tonic-gate 		return (DCMD_USAGE);
1807*0Sstevel@tonic-gate 
1808*0Sstevel@tonic-gate 	if (mdb_getopts(argc, argv, 'c', MDB_OPT_UINTPTR, &c, NULL) != argc)
1809*0Sstevel@tonic-gate 		return (DCMD_USAGE);
1810*0Sstevel@tonic-gate 
1811*0Sstevel@tonic-gate 	if (mdb_readvar(&ncpu, "_ncpu") == -1) {
1812*0Sstevel@tonic-gate 		mdb_warn("failed to read '_ncpu'");
1813*0Sstevel@tonic-gate 		return (DCMD_ERR);
1814*0Sstevel@tonic-gate 	}
1815*0Sstevel@tonic-gate 
1816*0Sstevel@tonic-gate 	if (mdb_vread(&state, sizeof (state), addr) == -1) {
1817*0Sstevel@tonic-gate 		mdb_warn("couldn't read dtrace_state_t at %p", addr);
1818*0Sstevel@tonic-gate 		return (DCMD_ERR);
1819*0Sstevel@tonic-gate 	}
1820*0Sstevel@tonic-gate 
1821*0Sstevel@tonic-gate 	bzero(&md, sizeof (md));
1822*0Sstevel@tonic-gate 	md.dtmd_state = &state;
1823*0Sstevel@tonic-gate 
1824*0Sstevel@tonic-gate 	if ((dtp = dtrace_vopen(DTRACE_VERSION, DTRACE_O_NOSYS, &err,
1825*0Sstevel@tonic-gate 	    &dtrace_mdbops, &md)) == NULL) {
1826*0Sstevel@tonic-gate 		mdb_warn("failed to initialize dtrace: %s\n",
1827*0Sstevel@tonic-gate 		    dtrace_errmsg(NULL, err));
1828*0Sstevel@tonic-gate 		return (DCMD_ERR);
1829*0Sstevel@tonic-gate 	}
1830*0Sstevel@tonic-gate 
1831*0Sstevel@tonic-gate 	if (dtrace_go(dtp) != 0) {
1832*0Sstevel@tonic-gate 		mdb_warn("failed to initialize dtrace: %s\n",
1833*0Sstevel@tonic-gate 		    dtrace_errmsg(dtp, dtrace_errno(dtp)));
1834*0Sstevel@tonic-gate 		goto err;
1835*0Sstevel@tonic-gate 	}
1836*0Sstevel@tonic-gate 
1837*0Sstevel@tonic-gate 	bzero(&dd, sizeof (dd));
1838*0Sstevel@tonic-gate 	dd.dtdd_dtp = dtp;
1839*0Sstevel@tonic-gate 	dd.dtdd_cpu = c;
1840*0Sstevel@tonic-gate 
1841*0Sstevel@tonic-gate 	if (dtrace_getopt(dtp, "flowindent", &val) == -1) {
1842*0Sstevel@tonic-gate 		mdb_warn("couldn't get 'flowindent' option: %s\n",
1843*0Sstevel@tonic-gate 		    dtrace_errmsg(dtp, dtrace_errno(dtp)));
1844*0Sstevel@tonic-gate 		goto err;
1845*0Sstevel@tonic-gate 	}
1846*0Sstevel@tonic-gate 
1847*0Sstevel@tonic-gate 	dd.dtdd_flowindent = (val != DTRACEOPT_UNSET);
1848*0Sstevel@tonic-gate 
1849*0Sstevel@tonic-gate 	if (dtrace_getopt(dtp, "quiet", &val) == -1) {
1850*0Sstevel@tonic-gate 		mdb_warn("couldn't get 'quiet' option: %s\n",
1851*0Sstevel@tonic-gate 		    dtrace_errmsg(dtp, dtrace_errno(dtp)));
1852*0Sstevel@tonic-gate 		goto err;
1853*0Sstevel@tonic-gate 	}
1854*0Sstevel@tonic-gate 
1855*0Sstevel@tonic-gate 	dd.dtdd_quiet = (val != DTRACEOPT_UNSET);
1856*0Sstevel@tonic-gate 
1857*0Sstevel@tonic-gate 	if (dtrace_handle_err(dtp, dtrace_dcmderr, NULL) == -1) {
1858*0Sstevel@tonic-gate 		mdb_warn("couldn't add err handler: %s\n",
1859*0Sstevel@tonic-gate 		    dtrace_errmsg(dtp, dtrace_errno(dtp)));
1860*0Sstevel@tonic-gate 		goto err;
1861*0Sstevel@tonic-gate 	}
1862*0Sstevel@tonic-gate 
1863*0Sstevel@tonic-gate 	if (dtrace_handle_drop(dtp, dtrace_dcmddrop, NULL) == -1) {
1864*0Sstevel@tonic-gate 		mdb_warn("couldn't add drop handler: %s\n",
1865*0Sstevel@tonic-gate 		    dtrace_errmsg(dtp, dtrace_errno(dtp)));
1866*0Sstevel@tonic-gate 		goto err;
1867*0Sstevel@tonic-gate 	}
1868*0Sstevel@tonic-gate 
1869*0Sstevel@tonic-gate 	if (dtrace_handle_buffered(dtp, dtrace_dcmdbuffered, NULL) == -1) {
1870*0Sstevel@tonic-gate 		mdb_warn("couldn't add buffered handler: %s\n",
1871*0Sstevel@tonic-gate 		    dtrace_errmsg(dtp, dtrace_errno(dtp)));
1872*0Sstevel@tonic-gate 		goto err;
1873*0Sstevel@tonic-gate 	}
1874*0Sstevel@tonic-gate 
1875*0Sstevel@tonic-gate 	if (dtrace_status(dtp) == -1) {
1876*0Sstevel@tonic-gate 		mdb_warn("couldn't get status: %s\n",
1877*0Sstevel@tonic-gate 		    dtrace_errmsg(dtp, dtrace_errno(dtp)));
1878*0Sstevel@tonic-gate 		goto err;
1879*0Sstevel@tonic-gate 	}
1880*0Sstevel@tonic-gate 
1881*0Sstevel@tonic-gate 	if (dtrace_aggregate_snap(dtp) == -1) {
1882*0Sstevel@tonic-gate 		mdb_warn("couldn't snapshot aggregation: %s\n",
1883*0Sstevel@tonic-gate 		    dtrace_errmsg(dtp, dtrace_errno(dtp)));
1884*0Sstevel@tonic-gate 		goto err;
1885*0Sstevel@tonic-gate 	}
1886*0Sstevel@tonic-gate 
1887*0Sstevel@tonic-gate 	if (dtrace_consume(dtp, NULL,
1888*0Sstevel@tonic-gate 	    dtrace_dcmdprobe, dtrace_dcmdrec, &dd) == -1) {
1889*0Sstevel@tonic-gate 		mdb_warn("couldn't consume DTrace buffers: %s\n",
1890*0Sstevel@tonic-gate 		    dtrace_errmsg(dtp, dtrace_errno(dtp)));
1891*0Sstevel@tonic-gate 	}
1892*0Sstevel@tonic-gate 
1893*0Sstevel@tonic-gate 	if (dtrace_aggregate_print(dtp, NULL, NULL) == -1) {
1894*0Sstevel@tonic-gate 		mdb_warn("couldn't print aggregation: %s\n",
1895*0Sstevel@tonic-gate 		    dtrace_errmsg(dtp, dtrace_errno(dtp)));
1896*0Sstevel@tonic-gate 		goto err;
1897*0Sstevel@tonic-gate 	}
1898*0Sstevel@tonic-gate 
1899*0Sstevel@tonic-gate 	rval = DCMD_OK;
1900*0Sstevel@tonic-gate err:
1901*0Sstevel@tonic-gate 	dtrace_close(dtp);
1902*0Sstevel@tonic-gate 	return (rval);
1903*0Sstevel@tonic-gate }
1904*0Sstevel@tonic-gate 
1905*0Sstevel@tonic-gate static int
1906*0Sstevel@tonic-gate dtrace_errhash_cmp(const void *l, const void *r)
1907*0Sstevel@tonic-gate {
1908*0Sstevel@tonic-gate 	uintptr_t lhs = *((uintptr_t *)l);
1909*0Sstevel@tonic-gate 	uintptr_t rhs = *((uintptr_t *)r);
1910*0Sstevel@tonic-gate 	dtrace_errhash_t lerr, rerr;
1911*0Sstevel@tonic-gate 	char lmsg[256], rmsg[256];
1912*0Sstevel@tonic-gate 
1913*0Sstevel@tonic-gate 	(void) mdb_vread(&lerr, sizeof (lerr), lhs);
1914*0Sstevel@tonic-gate 	(void) mdb_vread(&rerr, sizeof (rerr), rhs);
1915*0Sstevel@tonic-gate 
1916*0Sstevel@tonic-gate 	if (lerr.dter_msg == NULL)
1917*0Sstevel@tonic-gate 		return (-1);
1918*0Sstevel@tonic-gate 
1919*0Sstevel@tonic-gate 	if (rerr.dter_msg == NULL)
1920*0Sstevel@tonic-gate 		return (1);
1921*0Sstevel@tonic-gate 
1922*0Sstevel@tonic-gate 	(void) mdb_readstr(lmsg, sizeof (lmsg), (uintptr_t)lerr.dter_msg);
1923*0Sstevel@tonic-gate 	(void) mdb_readstr(rmsg, sizeof (rmsg), (uintptr_t)rerr.dter_msg);
1924*0Sstevel@tonic-gate 
1925*0Sstevel@tonic-gate 	return (strcmp(lmsg, rmsg));
1926*0Sstevel@tonic-gate }
1927*0Sstevel@tonic-gate 
1928*0Sstevel@tonic-gate int
1929*0Sstevel@tonic-gate dtrace_errhash_init(mdb_walk_state_t *wsp)
1930*0Sstevel@tonic-gate {
1931*0Sstevel@tonic-gate 	GElf_Sym sym;
1932*0Sstevel@tonic-gate 	uintptr_t *hash, addr;
1933*0Sstevel@tonic-gate 	int i;
1934*0Sstevel@tonic-gate 
1935*0Sstevel@tonic-gate 	if (wsp->walk_addr != NULL) {
1936*0Sstevel@tonic-gate 		mdb_warn("dtrace_errhash walk only supports global walks\n");
1937*0Sstevel@tonic-gate 		return (WALK_ERR);
1938*0Sstevel@tonic-gate 	}
1939*0Sstevel@tonic-gate 
1940*0Sstevel@tonic-gate 	if (mdb_lookup_by_name("dtrace_errhash", &sym) == -1) {
1941*0Sstevel@tonic-gate 		mdb_warn("couldn't find 'dtrace_errhash' (non-DEBUG kernel?)");
1942*0Sstevel@tonic-gate 		return (WALK_ERR);
1943*0Sstevel@tonic-gate 	}
1944*0Sstevel@tonic-gate 
1945*0Sstevel@tonic-gate 	addr = (uintptr_t)sym.st_value;
1946*0Sstevel@tonic-gate 	hash = mdb_alloc(DTRACE_ERRHASHSZ * sizeof (uintptr_t),
1947*0Sstevel@tonic-gate 	    UM_SLEEP | UM_GC);
1948*0Sstevel@tonic-gate 
1949*0Sstevel@tonic-gate 	for (i = 0; i < DTRACE_ERRHASHSZ; i++)
1950*0Sstevel@tonic-gate 		hash[i] = addr + i * sizeof (dtrace_errhash_t);
1951*0Sstevel@tonic-gate 
1952*0Sstevel@tonic-gate 	qsort(hash, DTRACE_ERRHASHSZ, sizeof (uintptr_t), dtrace_errhash_cmp);
1953*0Sstevel@tonic-gate 
1954*0Sstevel@tonic-gate 	wsp->walk_addr = 0;
1955*0Sstevel@tonic-gate 	wsp->walk_data = hash;
1956*0Sstevel@tonic-gate 
1957*0Sstevel@tonic-gate 	return (WALK_NEXT);
1958*0Sstevel@tonic-gate }
1959*0Sstevel@tonic-gate 
1960*0Sstevel@tonic-gate int
1961*0Sstevel@tonic-gate dtrace_errhash_step(mdb_walk_state_t *wsp)
1962*0Sstevel@tonic-gate {
1963*0Sstevel@tonic-gate 	int ndx = (int)wsp->walk_addr;
1964*0Sstevel@tonic-gate 	uintptr_t *hash = wsp->walk_data;
1965*0Sstevel@tonic-gate 	dtrace_errhash_t err;
1966*0Sstevel@tonic-gate 	uintptr_t addr;
1967*0Sstevel@tonic-gate 
1968*0Sstevel@tonic-gate 	if (ndx >= DTRACE_ERRHASHSZ)
1969*0Sstevel@tonic-gate 		return (WALK_DONE);
1970*0Sstevel@tonic-gate 
1971*0Sstevel@tonic-gate 	wsp->walk_addr = ndx + 1;
1972*0Sstevel@tonic-gate 	addr = hash[ndx];
1973*0Sstevel@tonic-gate 
1974*0Sstevel@tonic-gate 	if (mdb_vread(&err, sizeof (err), addr) == -1) {
1975*0Sstevel@tonic-gate 		mdb_warn("failed to read dtrace_errhash_t at %p", addr);
1976*0Sstevel@tonic-gate 		return (WALK_DONE);
1977*0Sstevel@tonic-gate 	}
1978*0Sstevel@tonic-gate 
1979*0Sstevel@tonic-gate 	if (err.dter_msg == NULL)
1980*0Sstevel@tonic-gate 		return (WALK_NEXT);
1981*0Sstevel@tonic-gate 
1982*0Sstevel@tonic-gate 	return (wsp->walk_callback(addr, &err, wsp->walk_cbdata));
1983*0Sstevel@tonic-gate }
1984*0Sstevel@tonic-gate 
1985*0Sstevel@tonic-gate /*ARGSUSED*/
1986*0Sstevel@tonic-gate int
1987*0Sstevel@tonic-gate dtrace_errhash(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1988*0Sstevel@tonic-gate {
1989*0Sstevel@tonic-gate 	dtrace_errhash_t err;
1990*0Sstevel@tonic-gate 	char msg[256];
1991*0Sstevel@tonic-gate 
1992*0Sstevel@tonic-gate 	if (!(flags & DCMD_ADDRSPEC)) {
1993*0Sstevel@tonic-gate 		if (mdb_walk_dcmd("dtrace_errhash", "dtrace_errhash",
1994*0Sstevel@tonic-gate 		    argc, argv) == -1) {
1995*0Sstevel@tonic-gate 			mdb_warn("can't walk 'dtrace_errhash'");
1996*0Sstevel@tonic-gate 			return (DCMD_ERR);
1997*0Sstevel@tonic-gate 		}
1998*0Sstevel@tonic-gate 
1999*0Sstevel@tonic-gate 		return (DCMD_OK);
2000*0Sstevel@tonic-gate 	}
2001*0Sstevel@tonic-gate 
2002*0Sstevel@tonic-gate 	if (DCMD_HDRSPEC(flags))
2003*0Sstevel@tonic-gate 		mdb_printf("%8s %s\n", "COUNT", "ERROR");
2004*0Sstevel@tonic-gate 
2005*0Sstevel@tonic-gate 	if (mdb_vread(&err, sizeof (err), addr) == -1) {
2006*0Sstevel@tonic-gate 		mdb_warn("failed to read dtrace_errhash_t at %p", addr);
2007*0Sstevel@tonic-gate 		return (DCMD_ERR);
2008*0Sstevel@tonic-gate 	}
2009*0Sstevel@tonic-gate 
2010*0Sstevel@tonic-gate 	addr = (uintptr_t)err.dter_msg;
2011*0Sstevel@tonic-gate 
2012*0Sstevel@tonic-gate 	if (mdb_readstr(msg, sizeof (msg), addr) == -1) {
2013*0Sstevel@tonic-gate 		mdb_warn("failed to read error msg at %p", addr);
2014*0Sstevel@tonic-gate 		return (DCMD_ERR);
2015*0Sstevel@tonic-gate 	}
2016*0Sstevel@tonic-gate 
2017*0Sstevel@tonic-gate 	mdb_printf("%8d %s", err.dter_count, msg);
2018*0Sstevel@tonic-gate 
2019*0Sstevel@tonic-gate 	/*
2020*0Sstevel@tonic-gate 	 * Some error messages include a newline -- only print the newline
2021*0Sstevel@tonic-gate 	 * if the message doesn't have one.
2022*0Sstevel@tonic-gate 	 */
2023*0Sstevel@tonic-gate 	if (msg[strlen(msg) - 1] != '\n')
2024*0Sstevel@tonic-gate 		mdb_printf("\n");
2025*0Sstevel@tonic-gate 
2026*0Sstevel@tonic-gate 	return (DCMD_OK);
2027*0Sstevel@tonic-gate }
2028*0Sstevel@tonic-gate 
2029*0Sstevel@tonic-gate int
2030*0Sstevel@tonic-gate dtrace_helptrace_init(mdb_walk_state_t *wsp)
2031*0Sstevel@tonic-gate {
2032*0Sstevel@tonic-gate 	uint32_t next;
2033*0Sstevel@tonic-gate 	int enabled;
2034*0Sstevel@tonic-gate 
2035*0Sstevel@tonic-gate 	if (wsp->walk_addr != NULL) {
2036*0Sstevel@tonic-gate 		mdb_warn("dtrace_helptrace only supports global walks\n");
2037*0Sstevel@tonic-gate 		return (WALK_ERR);
2038*0Sstevel@tonic-gate 	}
2039*0Sstevel@tonic-gate 
2040*0Sstevel@tonic-gate 	if (mdb_readvar(&enabled, "dtrace_helptrace_enabled") == -1) {
2041*0Sstevel@tonic-gate 		mdb_warn("couldn't read 'dtrace_helptrace_enabled'");
2042*0Sstevel@tonic-gate 		return (WALK_ERR);
2043*0Sstevel@tonic-gate 	}
2044*0Sstevel@tonic-gate 
2045*0Sstevel@tonic-gate 	if (!enabled) {
2046*0Sstevel@tonic-gate 		mdb_warn("helper tracing is not enabled\n");
2047*0Sstevel@tonic-gate 		return (WALK_ERR);
2048*0Sstevel@tonic-gate 	}
2049*0Sstevel@tonic-gate 
2050*0Sstevel@tonic-gate 	if (mdb_readvar(&next, "dtrace_helptrace_next") == -1) {
2051*0Sstevel@tonic-gate 		mdb_warn("couldn't read 'dtrace_helptrace_next'");
2052*0Sstevel@tonic-gate 		return (WALK_ERR);
2053*0Sstevel@tonic-gate 	}
2054*0Sstevel@tonic-gate 
2055*0Sstevel@tonic-gate 	wsp->walk_addr = next;
2056*0Sstevel@tonic-gate 
2057*0Sstevel@tonic-gate 	return (WALK_NEXT);
2058*0Sstevel@tonic-gate }
2059*0Sstevel@tonic-gate 
2060*0Sstevel@tonic-gate int
2061*0Sstevel@tonic-gate dtrace_helptrace_step(mdb_walk_state_t *wsp)
2062*0Sstevel@tonic-gate {
2063*0Sstevel@tonic-gate 	uint32_t next, size, nlocals, bufsize;
2064*0Sstevel@tonic-gate 	uintptr_t buffer, addr;
2065*0Sstevel@tonic-gate 	dtrace_helptrace_t *ht;
2066*0Sstevel@tonic-gate 	int rval;
2067*0Sstevel@tonic-gate 
2068*0Sstevel@tonic-gate 	if (mdb_readvar(&next, "dtrace_helptrace_next") == -1) {
2069*0Sstevel@tonic-gate 		mdb_warn("couldn't read 'dtrace_helptrace_next'");
2070*0Sstevel@tonic-gate 		return (WALK_ERR);
2071*0Sstevel@tonic-gate 	}
2072*0Sstevel@tonic-gate 
2073*0Sstevel@tonic-gate 	if (mdb_readvar(&bufsize, "dtrace_helptrace_bufsize") == -1) {
2074*0Sstevel@tonic-gate 		mdb_warn("couldn't read 'dtrace_helptrace_bufsize'");
2075*0Sstevel@tonic-gate 		return (WALK_ERR);
2076*0Sstevel@tonic-gate 	}
2077*0Sstevel@tonic-gate 
2078*0Sstevel@tonic-gate 	if (mdb_readvar(&buffer, "dtrace_helptrace_buffer") == -1) {
2079*0Sstevel@tonic-gate 		mdb_warn("couldn't read 'dtrace_helptrace_buffer'");
2080*0Sstevel@tonic-gate 		return (WALK_ERR);
2081*0Sstevel@tonic-gate 	}
2082*0Sstevel@tonic-gate 
2083*0Sstevel@tonic-gate 	if (mdb_readvar(&nlocals, "dtrace_helptrace_nlocals") == -1) {
2084*0Sstevel@tonic-gate 		mdb_warn("couldn't read 'dtrace_helptrace_nlocals'");
2085*0Sstevel@tonic-gate 		return (WALK_ERR);
2086*0Sstevel@tonic-gate 	}
2087*0Sstevel@tonic-gate 
2088*0Sstevel@tonic-gate 	size = sizeof (dtrace_helptrace_t) +
2089*0Sstevel@tonic-gate 	    nlocals * sizeof (uint64_t) - sizeof (uint64_t);
2090*0Sstevel@tonic-gate 
2091*0Sstevel@tonic-gate 	if (wsp->walk_addr + size > bufsize) {
2092*0Sstevel@tonic-gate 		if (next == 0)
2093*0Sstevel@tonic-gate 			return (WALK_DONE);
2094*0Sstevel@tonic-gate 
2095*0Sstevel@tonic-gate 		wsp->walk_addr = 0;
2096*0Sstevel@tonic-gate 	}
2097*0Sstevel@tonic-gate 
2098*0Sstevel@tonic-gate 	addr = buffer + wsp->walk_addr;
2099*0Sstevel@tonic-gate 	ht = alloca(size);
2100*0Sstevel@tonic-gate 
2101*0Sstevel@tonic-gate 	if (mdb_vread(ht, size, addr) == -1) {
2102*0Sstevel@tonic-gate 		mdb_warn("couldn't read entry at %p", addr);
2103*0Sstevel@tonic-gate 		return (WALK_ERR);
2104*0Sstevel@tonic-gate 	}
2105*0Sstevel@tonic-gate 
2106*0Sstevel@tonic-gate 	if (ht->dtht_helper != NULL) {
2107*0Sstevel@tonic-gate 		rval = wsp->walk_callback(addr, ht, wsp->walk_cbdata);
2108*0Sstevel@tonic-gate 
2109*0Sstevel@tonic-gate 		if (rval != WALK_NEXT)
2110*0Sstevel@tonic-gate 			return (rval);
2111*0Sstevel@tonic-gate 	}
2112*0Sstevel@tonic-gate 
2113*0Sstevel@tonic-gate 	if (wsp->walk_addr < next && wsp->walk_addr + size >= next)
2114*0Sstevel@tonic-gate 		return (WALK_DONE);
2115*0Sstevel@tonic-gate 
2116*0Sstevel@tonic-gate 	wsp->walk_addr += size;
2117*0Sstevel@tonic-gate 	return (WALK_NEXT);
2118*0Sstevel@tonic-gate }
2119*0Sstevel@tonic-gate 
2120*0Sstevel@tonic-gate int
2121*0Sstevel@tonic-gate dtrace_helptrace(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2122*0Sstevel@tonic-gate {
2123*0Sstevel@tonic-gate 	dtrace_helptrace_t help;
2124*0Sstevel@tonic-gate 	dtrace_helper_action_t helper;
2125*0Sstevel@tonic-gate 	char where[30];
2126*0Sstevel@tonic-gate 	uint_t opt_v = FALSE;
2127*0Sstevel@tonic-gate 	uintptr_t haddr;
2128*0Sstevel@tonic-gate 
2129*0Sstevel@tonic-gate 	if (!(flags & DCMD_ADDRSPEC)) {
2130*0Sstevel@tonic-gate 		if (mdb_walk_dcmd("dtrace_helptrace", "dtrace_helptrace",
2131*0Sstevel@tonic-gate 		    argc, argv) == -1) {
2132*0Sstevel@tonic-gate 			mdb_warn("can't walk 'dtrace_helptrace'");
2133*0Sstevel@tonic-gate 			return (DCMD_ERR);
2134*0Sstevel@tonic-gate 		}
2135*0Sstevel@tonic-gate 
2136*0Sstevel@tonic-gate 		return (DCMD_OK);
2137*0Sstevel@tonic-gate 	}
2138*0Sstevel@tonic-gate 
2139*0Sstevel@tonic-gate 	if (mdb_getopts(argc, argv, 'v',
2140*0Sstevel@tonic-gate 	    MDB_OPT_SETBITS, TRUE, &opt_v, NULL) != argc)
2141*0Sstevel@tonic-gate 		return (DCMD_USAGE);
2142*0Sstevel@tonic-gate 
2143*0Sstevel@tonic-gate 	if (DCMD_HDRSPEC(flags)) {
2144*0Sstevel@tonic-gate 		mdb_printf(" %?s %?s %12s %s\n",
2145*0Sstevel@tonic-gate 		    "ADDR", "HELPER", "WHERE", "DIFO");
2146*0Sstevel@tonic-gate 	}
2147*0Sstevel@tonic-gate 
2148*0Sstevel@tonic-gate 	if (mdb_vread(&help, sizeof (help), addr) == -1) {
2149*0Sstevel@tonic-gate 		mdb_warn("failed to read dtrace_helptrace_t at %p", addr);
2150*0Sstevel@tonic-gate 		return (DCMD_ERR);
2151*0Sstevel@tonic-gate 	}
2152*0Sstevel@tonic-gate 
2153*0Sstevel@tonic-gate 	switch (help.dtht_where) {
2154*0Sstevel@tonic-gate 	case 0:
2155*0Sstevel@tonic-gate 		(void) mdb_snprintf(where, sizeof (where), "predicate");
2156*0Sstevel@tonic-gate 		break;
2157*0Sstevel@tonic-gate 
2158*0Sstevel@tonic-gate 	case DTRACE_HELPTRACE_NEXT:
2159*0Sstevel@tonic-gate 		(void) mdb_snprintf(where, sizeof (where), "next");
2160*0Sstevel@tonic-gate 		break;
2161*0Sstevel@tonic-gate 
2162*0Sstevel@tonic-gate 	case DTRACE_HELPTRACE_DONE:
2163*0Sstevel@tonic-gate 		(void) mdb_snprintf(where, sizeof (where), "done");
2164*0Sstevel@tonic-gate 		break;
2165*0Sstevel@tonic-gate 
2166*0Sstevel@tonic-gate 	case DTRACE_HELPTRACE_ERR:
2167*0Sstevel@tonic-gate 		(void) mdb_snprintf(where, sizeof (where), "err");
2168*0Sstevel@tonic-gate 		break;
2169*0Sstevel@tonic-gate 
2170*0Sstevel@tonic-gate 	default:
2171*0Sstevel@tonic-gate 		(void) mdb_snprintf(where, sizeof (where),
2172*0Sstevel@tonic-gate 		    "action #%d", help.dtht_where);
2173*0Sstevel@tonic-gate 		break;
2174*0Sstevel@tonic-gate 	}
2175*0Sstevel@tonic-gate 
2176*0Sstevel@tonic-gate 	mdb_printf(" %?p %?p %12s ", addr, help.dtht_helper, where);
2177*0Sstevel@tonic-gate 
2178*0Sstevel@tonic-gate 	haddr = (uintptr_t)help.dtht_helper;
2179*0Sstevel@tonic-gate 
2180*0Sstevel@tonic-gate 	if (mdb_vread(&helper, sizeof (helper), haddr) == -1) {
2181*0Sstevel@tonic-gate 		/*
2182*0Sstevel@tonic-gate 		 * We're not going to warn in this case -- we're just not going
2183*0Sstevel@tonic-gate 		 * to print anything exciting.
2184*0Sstevel@tonic-gate 		 */
2185*0Sstevel@tonic-gate 		mdb_printf("???\n");
2186*0Sstevel@tonic-gate 	} else {
2187*0Sstevel@tonic-gate 		switch (help.dtht_where) {
2188*0Sstevel@tonic-gate 		case 0:
2189*0Sstevel@tonic-gate 			mdb_printf("%p\n", helper.dthp_predicate);
2190*0Sstevel@tonic-gate 			break;
2191*0Sstevel@tonic-gate 
2192*0Sstevel@tonic-gate 		case DTRACE_HELPTRACE_NEXT:
2193*0Sstevel@tonic-gate 		case DTRACE_HELPTRACE_DONE:
2194*0Sstevel@tonic-gate 		case DTRACE_HELPTRACE_ERR:
2195*0Sstevel@tonic-gate 			mdb_printf("-\n");
2196*0Sstevel@tonic-gate 			break;
2197*0Sstevel@tonic-gate 
2198*0Sstevel@tonic-gate 		default:
2199*0Sstevel@tonic-gate 			haddr = (uintptr_t)helper.dthp_actions +
2200*0Sstevel@tonic-gate 			    (help.dtht_where - 1) * sizeof (uintptr_t);
2201*0Sstevel@tonic-gate 
2202*0Sstevel@tonic-gate 			if (mdb_vread(&haddr, sizeof (haddr), haddr) == -1) {
2203*0Sstevel@tonic-gate 				mdb_printf("???\n");
2204*0Sstevel@tonic-gate 			} else {
2205*0Sstevel@tonic-gate 				mdb_printf("%p\n", haddr);
2206*0Sstevel@tonic-gate 			}
2207*0Sstevel@tonic-gate 		}
2208*0Sstevel@tonic-gate 	}
2209*0Sstevel@tonic-gate 
2210*0Sstevel@tonic-gate 	if (opt_v) {
2211*0Sstevel@tonic-gate 		int i;
2212*0Sstevel@tonic-gate 
2213*0Sstevel@tonic-gate 		mdb_printf("%?s|\n%?s+--> %?s %4s %s\n", "", "",
2214*0Sstevel@tonic-gate 		    "ADDR", "NDX", "VALUE");
2215*0Sstevel@tonic-gate 		addr += sizeof (help) - sizeof (uint64_t);
2216*0Sstevel@tonic-gate 
2217*0Sstevel@tonic-gate 		for (i = 0; i < help.dtht_nlocals; i++) {
2218*0Sstevel@tonic-gate 			uint64_t val;
2219*0Sstevel@tonic-gate 
2220*0Sstevel@tonic-gate 			if (mdb_vread(&val, sizeof (val), addr) == -1) {
2221*0Sstevel@tonic-gate 				mdb_warn("couldn't read local at %p", addr);
2222*0Sstevel@tonic-gate 				continue;
2223*0Sstevel@tonic-gate 			}
2224*0Sstevel@tonic-gate 
2225*0Sstevel@tonic-gate 			mdb_printf("%?s     %?p %4d %p\n", "", addr, i, val);
2226*0Sstevel@tonic-gate 			addr += sizeof (uint64_t);
2227*0Sstevel@tonic-gate 		}
2228*0Sstevel@tonic-gate 
2229*0Sstevel@tonic-gate 		mdb_printf("\n");
2230*0Sstevel@tonic-gate 	}
2231*0Sstevel@tonic-gate 
2232*0Sstevel@tonic-gate 	return (DCMD_OK);
2233*0Sstevel@tonic-gate }
2234*0Sstevel@tonic-gate 
2235*0Sstevel@tonic-gate /*ARGSUSED*/
2236*0Sstevel@tonic-gate static int
2237*0Sstevel@tonic-gate dtrace_state_walk(uintptr_t addr, const vmem_seg_t *seg, minor_t *highest)
2238*0Sstevel@tonic-gate {
2239*0Sstevel@tonic-gate 	if (seg->vs_end > *highest)
2240*0Sstevel@tonic-gate 		*highest = seg->vs_end;
2241*0Sstevel@tonic-gate 
2242*0Sstevel@tonic-gate 	return (WALK_NEXT);
2243*0Sstevel@tonic-gate }
2244*0Sstevel@tonic-gate 
2245*0Sstevel@tonic-gate typedef struct dtrace_state_walk {
2246*0Sstevel@tonic-gate 	uintptr_t dtsw_softstate;
2247*0Sstevel@tonic-gate 	minor_t dtsw_max;
2248*0Sstevel@tonic-gate 	minor_t dtsw_current;
2249*0Sstevel@tonic-gate } dtrace_state_walk_t;
2250*0Sstevel@tonic-gate 
2251*0Sstevel@tonic-gate int
2252*0Sstevel@tonic-gate dtrace_state_init(mdb_walk_state_t *wsp)
2253*0Sstevel@tonic-gate {
2254*0Sstevel@tonic-gate 	uintptr_t dtrace_minor;
2255*0Sstevel@tonic-gate 	minor_t max = 0;
2256*0Sstevel@tonic-gate 	dtrace_state_walk_t *dw;
2257*0Sstevel@tonic-gate 
2258*0Sstevel@tonic-gate 	if (wsp->walk_addr != NULL) {
2259*0Sstevel@tonic-gate 		mdb_warn("dtrace_state only supports global walks\n");
2260*0Sstevel@tonic-gate 		return (WALK_ERR);
2261*0Sstevel@tonic-gate 	}
2262*0Sstevel@tonic-gate 
2263*0Sstevel@tonic-gate 	/*
2264*0Sstevel@tonic-gate 	 * Find the dtrace_minor vmem arena and walk it to get the maximum
2265*0Sstevel@tonic-gate 	 * minor number.
2266*0Sstevel@tonic-gate 	 */
2267*0Sstevel@tonic-gate 	if (mdb_readvar(&dtrace_minor, "dtrace_minor") == -1) {
2268*0Sstevel@tonic-gate 		mdb_warn("failed to read 'dtrace_minor'");
2269*0Sstevel@tonic-gate 		return (WALK_ERR);
2270*0Sstevel@tonic-gate 	}
2271*0Sstevel@tonic-gate 
2272*0Sstevel@tonic-gate 	if (mdb_pwalk("vmem_alloc", (mdb_walk_cb_t)dtrace_state_walk,
2273*0Sstevel@tonic-gate 	    &max, dtrace_minor) == -1) {
2274*0Sstevel@tonic-gate 		mdb_warn("couldn't walk 'vmem_alloc'");
2275*0Sstevel@tonic-gate 		return (WALK_ERR);
2276*0Sstevel@tonic-gate 	}
2277*0Sstevel@tonic-gate 
2278*0Sstevel@tonic-gate 	dw = mdb_zalloc(sizeof (dtrace_state_walk_t), UM_SLEEP | UM_GC);
2279*0Sstevel@tonic-gate 	dw->dtsw_current = 0;
2280*0Sstevel@tonic-gate 	dw->dtsw_max = max;
2281*0Sstevel@tonic-gate 
2282*0Sstevel@tonic-gate 	if (mdb_readvar(&dw->dtsw_softstate, "dtrace_softstate") == -1) {
2283*0Sstevel@tonic-gate 		mdb_warn("failed to read 'dtrace_softstate'");
2284*0Sstevel@tonic-gate 		return (DCMD_ERR);
2285*0Sstevel@tonic-gate 	}
2286*0Sstevel@tonic-gate 
2287*0Sstevel@tonic-gate 	wsp->walk_data = dw;
2288*0Sstevel@tonic-gate 
2289*0Sstevel@tonic-gate 	return (WALK_NEXT);
2290*0Sstevel@tonic-gate }
2291*0Sstevel@tonic-gate 
2292*0Sstevel@tonic-gate int
2293*0Sstevel@tonic-gate dtrace_state_step(mdb_walk_state_t *wsp)
2294*0Sstevel@tonic-gate {
2295*0Sstevel@tonic-gate 	dtrace_state_walk_t *dw = wsp->walk_data;
2296*0Sstevel@tonic-gate 	uintptr_t statep;
2297*0Sstevel@tonic-gate 	dtrace_state_t state;
2298*0Sstevel@tonic-gate 	int rval;
2299*0Sstevel@tonic-gate 
2300*0Sstevel@tonic-gate 	while (mdb_get_soft_state_byaddr(dw->dtsw_softstate, dw->dtsw_current,
2301*0Sstevel@tonic-gate 	    &statep, NULL, 0) == -1) {
2302*0Sstevel@tonic-gate 		if (dw->dtsw_current >= dw->dtsw_max)
2303*0Sstevel@tonic-gate 			return (WALK_DONE);
2304*0Sstevel@tonic-gate 
2305*0Sstevel@tonic-gate 		dw->dtsw_current++;
2306*0Sstevel@tonic-gate 	}
2307*0Sstevel@tonic-gate 
2308*0Sstevel@tonic-gate 	if (mdb_vread(&state, sizeof (state), statep) == -1) {
2309*0Sstevel@tonic-gate 		mdb_warn("couldn't read dtrace_state_t at %p", statep);
2310*0Sstevel@tonic-gate 		return (WALK_NEXT);
2311*0Sstevel@tonic-gate 	}
2312*0Sstevel@tonic-gate 
2313*0Sstevel@tonic-gate 	rval = wsp->walk_callback(statep, &state, wsp->walk_cbdata);
2314*0Sstevel@tonic-gate 	dw->dtsw_current++;
2315*0Sstevel@tonic-gate 
2316*0Sstevel@tonic-gate 	return (rval);
2317*0Sstevel@tonic-gate }
2318*0Sstevel@tonic-gate 
2319*0Sstevel@tonic-gate typedef struct dtrace_state_data {
2320*0Sstevel@tonic-gate 	int dtsd_major;
2321*0Sstevel@tonic-gate 	uintptr_t dtsd_proc;
2322*0Sstevel@tonic-gate 	uintptr_t dtsd_softstate;
2323*0Sstevel@tonic-gate 	uintptr_t dtsd_state;
2324*0Sstevel@tonic-gate } dtrace_state_data_t;
2325*0Sstevel@tonic-gate 
2326*0Sstevel@tonic-gate static int
2327*0Sstevel@tonic-gate dtrace_state_file(uintptr_t addr, struct file *f, dtrace_state_data_t *data)
2328*0Sstevel@tonic-gate {
2329*0Sstevel@tonic-gate 	vnode_t vnode;
2330*0Sstevel@tonic-gate 	proc_t proc;
2331*0Sstevel@tonic-gate 	minor_t minor;
2332*0Sstevel@tonic-gate 	uintptr_t statep;
2333*0Sstevel@tonic-gate 
2334*0Sstevel@tonic-gate 	if (mdb_vread(&vnode, sizeof (vnode), (uintptr_t)f->f_vnode) == -1) {
2335*0Sstevel@tonic-gate 		mdb_warn("couldn't read vnode at %p", (uintptr_t)f->f_vnode);
2336*0Sstevel@tonic-gate 		return (WALK_NEXT);
2337*0Sstevel@tonic-gate 	}
2338*0Sstevel@tonic-gate 
2339*0Sstevel@tonic-gate 	if (getmajor(vnode.v_rdev) != data->dtsd_major)
2340*0Sstevel@tonic-gate 		return (WALK_NEXT);
2341*0Sstevel@tonic-gate 
2342*0Sstevel@tonic-gate 	minor = getminor(vnode.v_rdev);
2343*0Sstevel@tonic-gate 
2344*0Sstevel@tonic-gate 	if (mdb_vread(&proc, sizeof (proc), data->dtsd_proc) == -1) {
2345*0Sstevel@tonic-gate 		mdb_warn("failed to read proc at %p", data->dtsd_proc);
2346*0Sstevel@tonic-gate 		return (WALK_NEXT);
2347*0Sstevel@tonic-gate 	}
2348*0Sstevel@tonic-gate 
2349*0Sstevel@tonic-gate 	if (mdb_get_soft_state_byaddr(data->dtsd_softstate, minor,
2350*0Sstevel@tonic-gate 	    &statep, NULL, 0) == -1) {
2351*0Sstevel@tonic-gate 		mdb_warn("failed to read softstate for minor %d", minor);
2352*0Sstevel@tonic-gate 		return (WALK_NEXT);
2353*0Sstevel@tonic-gate 	}
2354*0Sstevel@tonic-gate 
2355*0Sstevel@tonic-gate 	if (statep != data->dtsd_state)
2356*0Sstevel@tonic-gate 		return (WALK_NEXT);
2357*0Sstevel@tonic-gate 
2358*0Sstevel@tonic-gate 	mdb_printf("%?p %5d %?p %-*s %?p\n", statep, minor,
2359*0Sstevel@tonic-gate 	    data->dtsd_proc, MAXCOMLEN, proc.p_user.u_comm, addr);
2360*0Sstevel@tonic-gate 
2361*0Sstevel@tonic-gate 	return (WALK_NEXT);
2362*0Sstevel@tonic-gate }
2363*0Sstevel@tonic-gate 
2364*0Sstevel@tonic-gate /*ARGSUSED*/
2365*0Sstevel@tonic-gate static int
2366*0Sstevel@tonic-gate dtrace_state_proc(uintptr_t addr, void *ignored, dtrace_state_data_t *data)
2367*0Sstevel@tonic-gate {
2368*0Sstevel@tonic-gate 	data->dtsd_proc = addr;
2369*0Sstevel@tonic-gate 
2370*0Sstevel@tonic-gate 	if (mdb_pwalk("file",
2371*0Sstevel@tonic-gate 	    (mdb_walk_cb_t)dtrace_state_file, data, addr) == -1) {
2372*0Sstevel@tonic-gate 		mdb_warn("couldn't walk 'file' for proc %p", addr);
2373*0Sstevel@tonic-gate 		return (WALK_ERR);
2374*0Sstevel@tonic-gate 	}
2375*0Sstevel@tonic-gate 
2376*0Sstevel@tonic-gate 	return (WALK_NEXT);
2377*0Sstevel@tonic-gate }
2378*0Sstevel@tonic-gate 
2379*0Sstevel@tonic-gate void
2380*0Sstevel@tonic-gate dtrace_state_help(void)
2381*0Sstevel@tonic-gate {
2382*0Sstevel@tonic-gate 	mdb_printf("Given a dtrace_state_t structure, displays all "
2383*0Sstevel@tonic-gate 	    /*CSTYLED*/
2384*0Sstevel@tonic-gate 	    "consumers, or \"<anonymous>\"\nif the consumer is anonymous.  If "
2385*0Sstevel@tonic-gate 	    "no state structure is provided, iterates\nover all state "
2386*0Sstevel@tonic-gate 	    "structures.\n\n"
2387*0Sstevel@tonic-gate 	    "Addresses in ADDR column may be provided to ::dtrace to obtain\n"
2388*0Sstevel@tonic-gate 	    "dtrace(1M)-like output for in-kernel DTrace data.\n");
2389*0Sstevel@tonic-gate }
2390*0Sstevel@tonic-gate 
2391*0Sstevel@tonic-gate int
2392*0Sstevel@tonic-gate dtrace_state(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2393*0Sstevel@tonic-gate {
2394*0Sstevel@tonic-gate 	uintptr_t devi;
2395*0Sstevel@tonic-gate 	struct dev_info info;
2396*0Sstevel@tonic-gate 	dtrace_state_data_t data;
2397*0Sstevel@tonic-gate 	dtrace_anon_t anon;
2398*0Sstevel@tonic-gate 	dtrace_state_t state;
2399*0Sstevel@tonic-gate 
2400*0Sstevel@tonic-gate 	if (!(flags & DCMD_ADDRSPEC)) {
2401*0Sstevel@tonic-gate 		if (mdb_walk_dcmd("dtrace_state",
2402*0Sstevel@tonic-gate 		    "dtrace_state", argc, argv) == -1) {
2403*0Sstevel@tonic-gate 			mdb_warn("can't walk dtrace_state");
2404*0Sstevel@tonic-gate 			return (DCMD_ERR);
2405*0Sstevel@tonic-gate 		}
2406*0Sstevel@tonic-gate 		return (DCMD_OK);
2407*0Sstevel@tonic-gate 	}
2408*0Sstevel@tonic-gate 
2409*0Sstevel@tonic-gate 	if (DCMD_HDRSPEC(flags)) {
2410*0Sstevel@tonic-gate 		mdb_printf("%?s %5s %?s %-*s %?s\n", "ADDR", "MINOR", "PROC",
2411*0Sstevel@tonic-gate 		    MAXCOMLEN, "NAME", "FILE");
2412*0Sstevel@tonic-gate 	}
2413*0Sstevel@tonic-gate 
2414*0Sstevel@tonic-gate 	/*
2415*0Sstevel@tonic-gate 	 * First determine if this is anonymous state.
2416*0Sstevel@tonic-gate 	 */
2417*0Sstevel@tonic-gate 	if (mdb_readvar(&anon, "dtrace_anon") == -1) {
2418*0Sstevel@tonic-gate 		mdb_warn("failed to read 'dtrace_anon'");
2419*0Sstevel@tonic-gate 		return (DCMD_ERR);
2420*0Sstevel@tonic-gate 	}
2421*0Sstevel@tonic-gate 
2422*0Sstevel@tonic-gate 	if ((uintptr_t)anon.dta_state == addr) {
2423*0Sstevel@tonic-gate 		if (mdb_vread(&state, sizeof (state), addr) == -1) {
2424*0Sstevel@tonic-gate 			mdb_warn("failed to read anon at %p", addr);
2425*0Sstevel@tonic-gate 			return (DCMD_ERR);
2426*0Sstevel@tonic-gate 		}
2427*0Sstevel@tonic-gate 
2428*0Sstevel@tonic-gate 		mdb_printf("%?p %5d %?s %-*s %?s\n", addr,
2429*0Sstevel@tonic-gate 		    getminor(state.dts_dev), "-", MAXCOMLEN,
2430*0Sstevel@tonic-gate 		    "<anonymous>", "-");
2431*0Sstevel@tonic-gate 
2432*0Sstevel@tonic-gate 		return (DCMD_OK);
2433*0Sstevel@tonic-gate 	}
2434*0Sstevel@tonic-gate 
2435*0Sstevel@tonic-gate 	if (mdb_readvar(&devi, "dtrace_devi") == -1) {
2436*0Sstevel@tonic-gate 		mdb_warn("failed to read 'dtrace_devi'");
2437*0Sstevel@tonic-gate 		return (DCMD_ERR);
2438*0Sstevel@tonic-gate 	}
2439*0Sstevel@tonic-gate 
2440*0Sstevel@tonic-gate 	if (mdb_vread(&info, sizeof (struct dev_info), devi) == -1) {
2441*0Sstevel@tonic-gate 		mdb_warn("failed to read 'dev_info'");
2442*0Sstevel@tonic-gate 		return (DCMD_ERR);
2443*0Sstevel@tonic-gate 	}
2444*0Sstevel@tonic-gate 
2445*0Sstevel@tonic-gate 	data.dtsd_major = info.devi_major;
2446*0Sstevel@tonic-gate 
2447*0Sstevel@tonic-gate 	if (mdb_readvar(&data.dtsd_softstate, "dtrace_softstate") == -1) {
2448*0Sstevel@tonic-gate 		mdb_warn("failed to read 'dtrace_softstate'");
2449*0Sstevel@tonic-gate 		return (DCMD_ERR);
2450*0Sstevel@tonic-gate 	}
2451*0Sstevel@tonic-gate 
2452*0Sstevel@tonic-gate 	data.dtsd_state = addr;
2453*0Sstevel@tonic-gate 
2454*0Sstevel@tonic-gate 	/*
2455*0Sstevel@tonic-gate 	 * Walk through all processes and all open files looking for this
2456*0Sstevel@tonic-gate 	 * state.  It must be open somewhere...
2457*0Sstevel@tonic-gate 	 */
2458*0Sstevel@tonic-gate 	if (mdb_walk("proc", (mdb_walk_cb_t)dtrace_state_proc, &data) == -1) {
2459*0Sstevel@tonic-gate 		mdb_warn("couldn't walk 'proc'");
2460*0Sstevel@tonic-gate 		return (DCMD_ERR);
2461*0Sstevel@tonic-gate 	}
2462*0Sstevel@tonic-gate 
2463*0Sstevel@tonic-gate 	return (DCMD_OK);
2464*0Sstevel@tonic-gate }
2465*0Sstevel@tonic-gate 
2466*0Sstevel@tonic-gate typedef struct dtrace_aggkey_data {
2467*0Sstevel@tonic-gate 	uintptr_t *dtakd_hash;
2468*0Sstevel@tonic-gate 	uintptr_t dtakd_hashsize;
2469*0Sstevel@tonic-gate 	uintptr_t dtakd_next;
2470*0Sstevel@tonic-gate 	uintptr_t dtakd_ndx;
2471*0Sstevel@tonic-gate } dtrace_aggkey_data_t;
2472*0Sstevel@tonic-gate 
2473*0Sstevel@tonic-gate int
2474*0Sstevel@tonic-gate dtrace_aggkey_init(mdb_walk_state_t *wsp)
2475*0Sstevel@tonic-gate {
2476*0Sstevel@tonic-gate 	dtrace_buffer_t buf;
2477*0Sstevel@tonic-gate 	uintptr_t addr;
2478*0Sstevel@tonic-gate 	dtrace_aggbuffer_t agb;
2479*0Sstevel@tonic-gate 	dtrace_aggkey_data_t *data;
2480*0Sstevel@tonic-gate 	size_t hsize;
2481*0Sstevel@tonic-gate 
2482*0Sstevel@tonic-gate 	if ((addr = wsp->walk_addr) == NULL) {
2483*0Sstevel@tonic-gate 		mdb_warn("dtrace_aggkey walk needs aggregation buffer\n");
2484*0Sstevel@tonic-gate 		return (WALK_ERR);
2485*0Sstevel@tonic-gate 	}
2486*0Sstevel@tonic-gate 
2487*0Sstevel@tonic-gate 	if (mdb_vread(&buf, sizeof (buf), addr) == -1) {
2488*0Sstevel@tonic-gate 		mdb_warn("failed to read aggregation buffer at %p", addr);
2489*0Sstevel@tonic-gate 		return (WALK_ERR);
2490*0Sstevel@tonic-gate 	}
2491*0Sstevel@tonic-gate 
2492*0Sstevel@tonic-gate 	addr = (uintptr_t)buf.dtb_tomax +
2493*0Sstevel@tonic-gate 	    buf.dtb_size - sizeof (dtrace_aggbuffer_t);
2494*0Sstevel@tonic-gate 
2495*0Sstevel@tonic-gate 	if (mdb_vread(&agb, sizeof (agb), addr) == -1) {
2496*0Sstevel@tonic-gate 		mdb_warn("failed to read dtrace_aggbuffer_t at %p", addr);
2497*0Sstevel@tonic-gate 		return (WALK_ERR);
2498*0Sstevel@tonic-gate 	}
2499*0Sstevel@tonic-gate 
2500*0Sstevel@tonic-gate 	data = mdb_zalloc(sizeof (dtrace_aggkey_data_t), UM_SLEEP);
2501*0Sstevel@tonic-gate 
2502*0Sstevel@tonic-gate 	data->dtakd_hashsize = agb.dtagb_hashsize;
2503*0Sstevel@tonic-gate 	hsize = agb.dtagb_hashsize * sizeof (dtrace_aggkey_t *);
2504*0Sstevel@tonic-gate 	data->dtakd_hash = mdb_alloc(hsize, UM_SLEEP);
2505*0Sstevel@tonic-gate 
2506*0Sstevel@tonic-gate 	if (mdb_vread(data->dtakd_hash, hsize,
2507*0Sstevel@tonic-gate 	    (uintptr_t)agb.dtagb_hash) == -1) {
2508*0Sstevel@tonic-gate 		mdb_warn("failed to read hash at %p",
2509*0Sstevel@tonic-gate 		    (uintptr_t)agb.dtagb_hash);
2510*0Sstevel@tonic-gate 		mdb_free(data->dtakd_hash, hsize);
2511*0Sstevel@tonic-gate 		mdb_free(data, sizeof (dtrace_aggkey_data_t));
2512*0Sstevel@tonic-gate 		return (WALK_ERR);
2513*0Sstevel@tonic-gate 	}
2514*0Sstevel@tonic-gate 
2515*0Sstevel@tonic-gate 	wsp->walk_data = data;
2516*0Sstevel@tonic-gate 	return (WALK_NEXT);
2517*0Sstevel@tonic-gate }
2518*0Sstevel@tonic-gate 
2519*0Sstevel@tonic-gate int
2520*0Sstevel@tonic-gate dtrace_aggkey_step(mdb_walk_state_t *wsp)
2521*0Sstevel@tonic-gate {
2522*0Sstevel@tonic-gate 	dtrace_aggkey_data_t *data = wsp->walk_data;
2523*0Sstevel@tonic-gate 	dtrace_aggkey_t key;
2524*0Sstevel@tonic-gate 	uintptr_t addr;
2525*0Sstevel@tonic-gate 
2526*0Sstevel@tonic-gate 	while ((addr = data->dtakd_next) == NULL) {
2527*0Sstevel@tonic-gate 		if (data->dtakd_ndx == data->dtakd_hashsize)
2528*0Sstevel@tonic-gate 			return (WALK_DONE);
2529*0Sstevel@tonic-gate 
2530*0Sstevel@tonic-gate 		data->dtakd_next = data->dtakd_hash[data->dtakd_ndx++];
2531*0Sstevel@tonic-gate 	}
2532*0Sstevel@tonic-gate 
2533*0Sstevel@tonic-gate 	if (mdb_vread(&key, sizeof (key), addr) == -1) {
2534*0Sstevel@tonic-gate 		mdb_warn("failed to read dtrace_aggkey_t at %p", addr);
2535*0Sstevel@tonic-gate 		return (WALK_ERR);
2536*0Sstevel@tonic-gate 	}
2537*0Sstevel@tonic-gate 
2538*0Sstevel@tonic-gate 	data->dtakd_next = (uintptr_t)key.dtak_next;
2539*0Sstevel@tonic-gate 
2540*0Sstevel@tonic-gate 	return (wsp->walk_callback(addr, &key, wsp->walk_cbdata));
2541*0Sstevel@tonic-gate }
2542*0Sstevel@tonic-gate 
2543*0Sstevel@tonic-gate void
2544*0Sstevel@tonic-gate dtrace_aggkey_fini(mdb_walk_state_t *wsp)
2545*0Sstevel@tonic-gate {
2546*0Sstevel@tonic-gate 	dtrace_aggkey_data_t *data = wsp->walk_data;
2547*0Sstevel@tonic-gate 	size_t hsize;
2548*0Sstevel@tonic-gate 
2549*0Sstevel@tonic-gate 	hsize = data->dtakd_hashsize * sizeof (dtrace_aggkey_t *);
2550*0Sstevel@tonic-gate 	mdb_free(data->dtakd_hash, hsize);
2551*0Sstevel@tonic-gate 	mdb_free(data, sizeof (dtrace_aggkey_data_t));
2552*0Sstevel@tonic-gate }
2553*0Sstevel@tonic-gate 
2554*0Sstevel@tonic-gate typedef struct dtrace_dynvar_data {
2555*0Sstevel@tonic-gate 	dtrace_dynhash_t *dtdvd_hash;
2556*0Sstevel@tonic-gate 	uintptr_t dtdvd_hashsize;
2557*0Sstevel@tonic-gate 	uintptr_t dtdvd_next;
2558*0Sstevel@tonic-gate 	uintptr_t dtdvd_ndx;
2559*0Sstevel@tonic-gate } dtrace_dynvar_data_t;
2560*0Sstevel@tonic-gate 
2561*0Sstevel@tonic-gate int
2562*0Sstevel@tonic-gate dtrace_dynvar_init(mdb_walk_state_t *wsp)
2563*0Sstevel@tonic-gate {
2564*0Sstevel@tonic-gate 	uintptr_t addr;
2565*0Sstevel@tonic-gate 	dtrace_dstate_t dstate;
2566*0Sstevel@tonic-gate 	dtrace_dynvar_data_t *data;
2567*0Sstevel@tonic-gate 	size_t hsize;
2568*0Sstevel@tonic-gate 
2569*0Sstevel@tonic-gate 	if ((addr = wsp->walk_addr) == NULL) {
2570*0Sstevel@tonic-gate 		mdb_warn("dtrace_dynvar walk needs dtrace_dstate_t\n");
2571*0Sstevel@tonic-gate 		return (WALK_ERR);
2572*0Sstevel@tonic-gate 	}
2573*0Sstevel@tonic-gate 
2574*0Sstevel@tonic-gate 	if (mdb_vread(&dstate, sizeof (dstate), addr) == -1) {
2575*0Sstevel@tonic-gate 		mdb_warn("failed to read dynamic state at %p", addr);
2576*0Sstevel@tonic-gate 		return (WALK_ERR);
2577*0Sstevel@tonic-gate 	}
2578*0Sstevel@tonic-gate 
2579*0Sstevel@tonic-gate 	data = mdb_zalloc(sizeof (dtrace_dynvar_data_t), UM_SLEEP);
2580*0Sstevel@tonic-gate 
2581*0Sstevel@tonic-gate 	data->dtdvd_hashsize = dstate.dtds_hashsize;
2582*0Sstevel@tonic-gate 	hsize = dstate.dtds_hashsize * sizeof (dtrace_dynhash_t);
2583*0Sstevel@tonic-gate 	data->dtdvd_hash = mdb_alloc(hsize, UM_SLEEP);
2584*0Sstevel@tonic-gate 
2585*0Sstevel@tonic-gate 	if (mdb_vread(data->dtdvd_hash, hsize,
2586*0Sstevel@tonic-gate 	    (uintptr_t)dstate.dtds_hash) == -1) {
2587*0Sstevel@tonic-gate 		mdb_warn("failed to read hash at %p",
2588*0Sstevel@tonic-gate 		    (uintptr_t)dstate.dtds_hash);
2589*0Sstevel@tonic-gate 		mdb_free(data->dtdvd_hash, hsize);
2590*0Sstevel@tonic-gate 		mdb_free(data, sizeof (dtrace_dynvar_data_t));
2591*0Sstevel@tonic-gate 		return (WALK_ERR);
2592*0Sstevel@tonic-gate 	}
2593*0Sstevel@tonic-gate 
2594*0Sstevel@tonic-gate 	wsp->walk_data = data;
2595*0Sstevel@tonic-gate 	return (WALK_NEXT);
2596*0Sstevel@tonic-gate }
2597*0Sstevel@tonic-gate 
2598*0Sstevel@tonic-gate int
2599*0Sstevel@tonic-gate dtrace_dynvar_step(mdb_walk_state_t *wsp)
2600*0Sstevel@tonic-gate {
2601*0Sstevel@tonic-gate 	dtrace_dynvar_data_t *data = wsp->walk_data;
2602*0Sstevel@tonic-gate 	dtrace_dynvar_t dynvar, *dvar;
2603*0Sstevel@tonic-gate 	size_t dvarsize;
2604*0Sstevel@tonic-gate 	uintptr_t addr;
2605*0Sstevel@tonic-gate 	int nkeys;
2606*0Sstevel@tonic-gate 
2607*0Sstevel@tonic-gate 	while ((addr = data->dtdvd_next) == NULL) {
2608*0Sstevel@tonic-gate 		if (data->dtdvd_ndx == data->dtdvd_hashsize)
2609*0Sstevel@tonic-gate 			return (WALK_DONE);
2610*0Sstevel@tonic-gate 
2611*0Sstevel@tonic-gate 		data->dtdvd_next =
2612*0Sstevel@tonic-gate 		    (uintptr_t)data->dtdvd_hash[data->dtdvd_ndx++].dtdh_chain;
2613*0Sstevel@tonic-gate 	}
2614*0Sstevel@tonic-gate 
2615*0Sstevel@tonic-gate 	if (mdb_vread(&dynvar, sizeof (dynvar), addr) == -1) {
2616*0Sstevel@tonic-gate 		mdb_warn("failed to read dtrace_dynvar_t at %p", addr);
2617*0Sstevel@tonic-gate 		return (WALK_ERR);
2618*0Sstevel@tonic-gate 	}
2619*0Sstevel@tonic-gate 
2620*0Sstevel@tonic-gate 	/*
2621*0Sstevel@tonic-gate 	 * Now we need to allocate the correct size.
2622*0Sstevel@tonic-gate 	 */
2623*0Sstevel@tonic-gate 	nkeys = dynvar.dtdv_tuple.dtt_nkeys;
2624*0Sstevel@tonic-gate 	dvarsize = (uintptr_t)&dynvar.dtdv_tuple.dtt_key[nkeys] -
2625*0Sstevel@tonic-gate 	    (uintptr_t)&dynvar;
2626*0Sstevel@tonic-gate 
2627*0Sstevel@tonic-gate 	dvar = alloca(dvarsize);
2628*0Sstevel@tonic-gate 
2629*0Sstevel@tonic-gate 	if (mdb_vread(dvar, dvarsize, addr) == -1) {
2630*0Sstevel@tonic-gate 		mdb_warn("failed to read dtrace_dynvar_t at %p", addr);
2631*0Sstevel@tonic-gate 		return (WALK_ERR);
2632*0Sstevel@tonic-gate 	}
2633*0Sstevel@tonic-gate 
2634*0Sstevel@tonic-gate 	data->dtdvd_next = (uintptr_t)dynvar.dtdv_next;
2635*0Sstevel@tonic-gate 
2636*0Sstevel@tonic-gate 	return (wsp->walk_callback(addr, dvar, wsp->walk_cbdata));
2637*0Sstevel@tonic-gate }
2638*0Sstevel@tonic-gate 
2639*0Sstevel@tonic-gate void
2640*0Sstevel@tonic-gate dtrace_dynvar_fini(mdb_walk_state_t *wsp)
2641*0Sstevel@tonic-gate {
2642*0Sstevel@tonic-gate 	dtrace_dynvar_data_t *data = wsp->walk_data;
2643*0Sstevel@tonic-gate 	size_t hsize;
2644*0Sstevel@tonic-gate 
2645*0Sstevel@tonic-gate 	hsize = data->dtdvd_hashsize * sizeof (dtrace_dynvar_t *);
2646*0Sstevel@tonic-gate 	mdb_free(data->dtdvd_hash, hsize);
2647*0Sstevel@tonic-gate 	mdb_free(data, sizeof (dtrace_dynvar_data_t));
2648*0Sstevel@tonic-gate }
2649*0Sstevel@tonic-gate 
2650*0Sstevel@tonic-gate typedef struct dtrace_hashstat_data {
2651*0Sstevel@tonic-gate 	size_t *dthsd_counts;
2652*0Sstevel@tonic-gate 	size_t dthsd_hashsize;
2653*0Sstevel@tonic-gate 	char *dthsd_data;
2654*0Sstevel@tonic-gate 	size_t dthsd_size;
2655*0Sstevel@tonic-gate 	int dthsd_header;
2656*0Sstevel@tonic-gate } dtrace_hashstat_data_t;
2657*0Sstevel@tonic-gate 
2658*0Sstevel@tonic-gate typedef void (*dtrace_hashstat_func_t)(dtrace_hashstat_data_t *);
2659*0Sstevel@tonic-gate 
2660*0Sstevel@tonic-gate static void
2661*0Sstevel@tonic-gate dtrace_hashstat_additive(dtrace_hashstat_data_t *data)
2662*0Sstevel@tonic-gate {
2663*0Sstevel@tonic-gate 	int i;
2664*0Sstevel@tonic-gate 	int hval = 0;
2665*0Sstevel@tonic-gate 
2666*0Sstevel@tonic-gate 	for (i = 0; i < data->dthsd_size; i++)
2667*0Sstevel@tonic-gate 		hval += data->dthsd_data[i];
2668*0Sstevel@tonic-gate 
2669*0Sstevel@tonic-gate 	data->dthsd_counts[hval % data->dthsd_hashsize]++;
2670*0Sstevel@tonic-gate }
2671*0Sstevel@tonic-gate 
2672*0Sstevel@tonic-gate static void
2673*0Sstevel@tonic-gate dtrace_hashstat_shifty(dtrace_hashstat_data_t *data)
2674*0Sstevel@tonic-gate {
2675*0Sstevel@tonic-gate 	uint64_t hval = 0;
2676*0Sstevel@tonic-gate 	int i;
2677*0Sstevel@tonic-gate 
2678*0Sstevel@tonic-gate 	if (data->dthsd_size < sizeof (uint64_t)) {
2679*0Sstevel@tonic-gate 		dtrace_hashstat_additive(data);
2680*0Sstevel@tonic-gate 		return;
2681*0Sstevel@tonic-gate 	}
2682*0Sstevel@tonic-gate 
2683*0Sstevel@tonic-gate 	for (i = 0; i < data->dthsd_size; i += sizeof (uint64_t)) {
2684*0Sstevel@tonic-gate 		/* LINTED - alignment */
2685*0Sstevel@tonic-gate 		uint64_t val = *((uint64_t *)&data->dthsd_data[i]);
2686*0Sstevel@tonic-gate 
2687*0Sstevel@tonic-gate 		hval += (val & ((1 << NBBY) - 1)) +
2688*0Sstevel@tonic-gate 		    ((val >> NBBY) & ((1 << NBBY) - 1)) +
2689*0Sstevel@tonic-gate 		    ((val >> (NBBY << 1)) & ((1 << NBBY) - 1)) +
2690*0Sstevel@tonic-gate 		    ((val >> (NBBY << 2)) & ((1 << NBBY) - 1)) +
2691*0Sstevel@tonic-gate 		    (val & USHRT_MAX) + (val >> (NBBY << 1) & USHRT_MAX);
2692*0Sstevel@tonic-gate 	}
2693*0Sstevel@tonic-gate 
2694*0Sstevel@tonic-gate 	data->dthsd_counts[hval % data->dthsd_hashsize]++;
2695*0Sstevel@tonic-gate }
2696*0Sstevel@tonic-gate 
2697*0Sstevel@tonic-gate static void
2698*0Sstevel@tonic-gate dtrace_hashstat_knuth(dtrace_hashstat_data_t *data)
2699*0Sstevel@tonic-gate {
2700*0Sstevel@tonic-gate 	int i;
2701*0Sstevel@tonic-gate 	int hval = data->dthsd_size;
2702*0Sstevel@tonic-gate 
2703*0Sstevel@tonic-gate 	for (i = 0; i < data->dthsd_size; i++)
2704*0Sstevel@tonic-gate 		hval = (hval << 4) ^ (hval >> 28) ^ data->dthsd_data[i];
2705*0Sstevel@tonic-gate 
2706*0Sstevel@tonic-gate 	data->dthsd_counts[hval % data->dthsd_hashsize]++;
2707*0Sstevel@tonic-gate }
2708*0Sstevel@tonic-gate 
2709*0Sstevel@tonic-gate static void
2710*0Sstevel@tonic-gate dtrace_hashstat_oneatatime(dtrace_hashstat_data_t *data)
2711*0Sstevel@tonic-gate {
2712*0Sstevel@tonic-gate 	int i;
2713*0Sstevel@tonic-gate 	uint32_t hval = 0;
2714*0Sstevel@tonic-gate 
2715*0Sstevel@tonic-gate 	for (i = 0; i < data->dthsd_size; i++) {
2716*0Sstevel@tonic-gate 		hval += data->dthsd_data[i];
2717*0Sstevel@tonic-gate 		hval += (hval << 10);
2718*0Sstevel@tonic-gate 		hval ^= (hval >> 6);
2719*0Sstevel@tonic-gate 	}
2720*0Sstevel@tonic-gate 
2721*0Sstevel@tonic-gate 	hval += (hval << 3);
2722*0Sstevel@tonic-gate 	hval ^= (hval >> 11);
2723*0Sstevel@tonic-gate 	hval += (hval << 15);
2724*0Sstevel@tonic-gate 
2725*0Sstevel@tonic-gate 	data->dthsd_counts[hval % data->dthsd_hashsize]++;
2726*0Sstevel@tonic-gate }
2727*0Sstevel@tonic-gate 
2728*0Sstevel@tonic-gate static void
2729*0Sstevel@tonic-gate dtrace_hashstat_fnv(dtrace_hashstat_data_t *data)
2730*0Sstevel@tonic-gate {
2731*0Sstevel@tonic-gate 	static const uint32_t prime = 0x01000193;
2732*0Sstevel@tonic-gate 	uint32_t hval = 0;
2733*0Sstevel@tonic-gate 	int i;
2734*0Sstevel@tonic-gate 
2735*0Sstevel@tonic-gate 	for (i = 0; i < data->dthsd_size; i++) {
2736*0Sstevel@tonic-gate 		hval *= prime;
2737*0Sstevel@tonic-gate 		hval ^= data->dthsd_data[i];
2738*0Sstevel@tonic-gate 	}
2739*0Sstevel@tonic-gate 
2740*0Sstevel@tonic-gate 	data->dthsd_counts[hval % data->dthsd_hashsize]++;
2741*0Sstevel@tonic-gate }
2742*0Sstevel@tonic-gate 
2743*0Sstevel@tonic-gate static void
2744*0Sstevel@tonic-gate dtrace_hashstat_stats(char *name, dtrace_hashstat_data_t *data)
2745*0Sstevel@tonic-gate {
2746*0Sstevel@tonic-gate 	size_t nz = 0, i;
2747*0Sstevel@tonic-gate 	int longest = 0;
2748*0Sstevel@tonic-gate 	size_t ttl = 0;
2749*0Sstevel@tonic-gate 	double sum = 0.0;
2750*0Sstevel@tonic-gate 	double avg;
2751*0Sstevel@tonic-gate 	uint_t util, stddev;
2752*0Sstevel@tonic-gate 
2753*0Sstevel@tonic-gate 	if (!data->dthsd_header) {
2754*0Sstevel@tonic-gate 		mdb_printf("%15s %11s %11s %11s %11s %11s\n", "NAME",
2755*0Sstevel@tonic-gate 		    "HASHSIZE", "%UTIL", "LONGEST", "AVERAGE", "STDDEV");
2756*0Sstevel@tonic-gate 		data->dthsd_header = 1;
2757*0Sstevel@tonic-gate 	}
2758*0Sstevel@tonic-gate 
2759*0Sstevel@tonic-gate 	for (i = 0; i < data->dthsd_hashsize; i++) {
2760*0Sstevel@tonic-gate 		if (data->dthsd_counts[i] != 0) {
2761*0Sstevel@tonic-gate 			nz++;
2762*0Sstevel@tonic-gate 
2763*0Sstevel@tonic-gate 			if (data->dthsd_counts[i] > longest)
2764*0Sstevel@tonic-gate 				longest = data->dthsd_counts[i];
2765*0Sstevel@tonic-gate 
2766*0Sstevel@tonic-gate 			ttl += data->dthsd_counts[i];
2767*0Sstevel@tonic-gate 		}
2768*0Sstevel@tonic-gate 	}
2769*0Sstevel@tonic-gate 
2770*0Sstevel@tonic-gate 	if (nz == 0) {
2771*0Sstevel@tonic-gate 		mdb_printf("%15s %11d %11s %11s %11s %11s\n", name,
2772*0Sstevel@tonic-gate 		    data->dthsd_hashsize, "-", "-", "-", "-");
2773*0Sstevel@tonic-gate 		return;
2774*0Sstevel@tonic-gate 	}
2775*0Sstevel@tonic-gate 
2776*0Sstevel@tonic-gate 	avg = (double)ttl / (double)nz;
2777*0Sstevel@tonic-gate 
2778*0Sstevel@tonic-gate 	for (i = 0; i < data->dthsd_hashsize; i++) {
2779*0Sstevel@tonic-gate 		double delta = (double)data->dthsd_counts[i] - avg;
2780*0Sstevel@tonic-gate 
2781*0Sstevel@tonic-gate 		if (data->dthsd_counts[i] == 0)
2782*0Sstevel@tonic-gate 			continue;
2783*0Sstevel@tonic-gate 
2784*0Sstevel@tonic-gate 		sum += delta * delta;
2785*0Sstevel@tonic-gate 	}
2786*0Sstevel@tonic-gate 
2787*0Sstevel@tonic-gate 	util = (nz * 1000) / data->dthsd_hashsize;
2788*0Sstevel@tonic-gate 	stddev = (uint_t)sqrt(sum / (double)nz) * 10;
2789*0Sstevel@tonic-gate 
2790*0Sstevel@tonic-gate 	mdb_printf("%15s %11d %9u.%1u %11d %11d %9u.%1u\n", name,
2791*0Sstevel@tonic-gate 	    data->dthsd_hashsize, util / 10, util % 10, longest, ttl / nz,
2792*0Sstevel@tonic-gate 	    stddev / 10, stddev % 10);
2793*0Sstevel@tonic-gate }
2794*0Sstevel@tonic-gate 
2795*0Sstevel@tonic-gate static struct dtrace_hashstat {
2796*0Sstevel@tonic-gate 	char *dths_name;
2797*0Sstevel@tonic-gate 	dtrace_hashstat_func_t dths_func;
2798*0Sstevel@tonic-gate } _dtrace_hashstat[] = {
2799*0Sstevel@tonic-gate 	{ "<actual>", NULL },
2800*0Sstevel@tonic-gate 	{ "additive", dtrace_hashstat_additive },
2801*0Sstevel@tonic-gate 	{ "shifty", dtrace_hashstat_shifty },
2802*0Sstevel@tonic-gate 	{ "knuth", dtrace_hashstat_knuth },
2803*0Sstevel@tonic-gate 	{ "one-at-a-time", dtrace_hashstat_oneatatime },
2804*0Sstevel@tonic-gate 	{ "fnv", dtrace_hashstat_fnv },
2805*0Sstevel@tonic-gate 	{ NULL, 0 }
2806*0Sstevel@tonic-gate };
2807*0Sstevel@tonic-gate 
2808*0Sstevel@tonic-gate typedef struct dtrace_aggstat_data {
2809*0Sstevel@tonic-gate 	dtrace_hashstat_data_t dtagsd_hash;
2810*0Sstevel@tonic-gate 	dtrace_hashstat_func_t dtagsd_func;
2811*0Sstevel@tonic-gate } dtrace_aggstat_data_t;
2812*0Sstevel@tonic-gate 
2813*0Sstevel@tonic-gate static int
2814*0Sstevel@tonic-gate dtrace_aggstat_walk(uintptr_t addr, dtrace_aggkey_t *key,
2815*0Sstevel@tonic-gate     dtrace_aggstat_data_t *data)
2816*0Sstevel@tonic-gate {
2817*0Sstevel@tonic-gate 	dtrace_hashstat_data_t *hdata = &data->dtagsd_hash;
2818*0Sstevel@tonic-gate 	size_t size;
2819*0Sstevel@tonic-gate 
2820*0Sstevel@tonic-gate 	if (data->dtagsd_func == NULL) {
2821*0Sstevel@tonic-gate 		size_t bucket = key->dtak_hashval % hdata->dthsd_hashsize;
2822*0Sstevel@tonic-gate 
2823*0Sstevel@tonic-gate 		hdata->dthsd_counts[bucket]++;
2824*0Sstevel@tonic-gate 		return (WALK_NEXT);
2825*0Sstevel@tonic-gate 	}
2826*0Sstevel@tonic-gate 
2827*0Sstevel@tonic-gate 	/*
2828*0Sstevel@tonic-gate 	 * We need to read the data.
2829*0Sstevel@tonic-gate 	 */
2830*0Sstevel@tonic-gate 	size = key->dtak_size - sizeof (dtrace_aggid_t);
2831*0Sstevel@tonic-gate 	addr = (uintptr_t)key->dtak_data + sizeof (dtrace_aggid_t);
2832*0Sstevel@tonic-gate 	hdata->dthsd_data = alloca(size);
2833*0Sstevel@tonic-gate 	hdata->dthsd_size = size;
2834*0Sstevel@tonic-gate 
2835*0Sstevel@tonic-gate 	if (mdb_vread(hdata->dthsd_data, size, addr) == -1) {
2836*0Sstevel@tonic-gate 		mdb_warn("couldn't read data at %p", addr);
2837*0Sstevel@tonic-gate 		return (WALK_ERR);
2838*0Sstevel@tonic-gate 	}
2839*0Sstevel@tonic-gate 
2840*0Sstevel@tonic-gate 	data->dtagsd_func(hdata);
2841*0Sstevel@tonic-gate 
2842*0Sstevel@tonic-gate 	return (WALK_NEXT);
2843*0Sstevel@tonic-gate }
2844*0Sstevel@tonic-gate 
2845*0Sstevel@tonic-gate /*ARGSUSED*/
2846*0Sstevel@tonic-gate int
2847*0Sstevel@tonic-gate dtrace_aggstat(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2848*0Sstevel@tonic-gate {
2849*0Sstevel@tonic-gate 	dtrace_buffer_t buf;
2850*0Sstevel@tonic-gate 	uintptr_t aaddr;
2851*0Sstevel@tonic-gate 	dtrace_aggbuffer_t agb;
2852*0Sstevel@tonic-gate 	size_t hsize, i, actual, prime, evenpow;
2853*0Sstevel@tonic-gate 	dtrace_aggstat_data_t data;
2854*0Sstevel@tonic-gate 	dtrace_hashstat_data_t *hdata = &data.dtagsd_hash;
2855*0Sstevel@tonic-gate 
2856*0Sstevel@tonic-gate 	bzero(&data, sizeof (data));
2857*0Sstevel@tonic-gate 
2858*0Sstevel@tonic-gate 	if (!(flags & DCMD_ADDRSPEC))
2859*0Sstevel@tonic-gate 		return (DCMD_USAGE);
2860*0Sstevel@tonic-gate 
2861*0Sstevel@tonic-gate 	if (mdb_vread(&buf, sizeof (buf), addr) == -1) {
2862*0Sstevel@tonic-gate 		mdb_warn("failed to read aggregation buffer at %p", addr);
2863*0Sstevel@tonic-gate 		return (DCMD_ERR);
2864*0Sstevel@tonic-gate 	}
2865*0Sstevel@tonic-gate 
2866*0Sstevel@tonic-gate 	aaddr = (uintptr_t)buf.dtb_tomax +
2867*0Sstevel@tonic-gate 	    buf.dtb_size - sizeof (dtrace_aggbuffer_t);
2868*0Sstevel@tonic-gate 
2869*0Sstevel@tonic-gate 	if (mdb_vread(&agb, sizeof (agb), aaddr) == -1) {
2870*0Sstevel@tonic-gate 		mdb_warn("failed to read dtrace_aggbuffer_t at %p", aaddr);
2871*0Sstevel@tonic-gate 		return (DCMD_ERR);
2872*0Sstevel@tonic-gate 	}
2873*0Sstevel@tonic-gate 
2874*0Sstevel@tonic-gate 	hsize = (actual = agb.dtagb_hashsize) * sizeof (size_t);
2875*0Sstevel@tonic-gate 	hdata->dthsd_counts = mdb_alloc(hsize, UM_SLEEP | UM_GC);
2876*0Sstevel@tonic-gate 
2877*0Sstevel@tonic-gate 	/*
2878*0Sstevel@tonic-gate 	 * Now pick the largest prime smaller than the hash size.  (If the
2879*0Sstevel@tonic-gate 	 * existing size is prime, we'll pick a smaller prime just for the
2880*0Sstevel@tonic-gate 	 * hell of it.)
2881*0Sstevel@tonic-gate 	 */
2882*0Sstevel@tonic-gate 	for (prime = agb.dtagb_hashsize - 1; prime > 7; prime--) {
2883*0Sstevel@tonic-gate 		size_t limit = prime / 7;
2884*0Sstevel@tonic-gate 
2885*0Sstevel@tonic-gate 		for (i = 2; i < limit; i++) {
2886*0Sstevel@tonic-gate 			if ((prime % i) == 0)
2887*0Sstevel@tonic-gate 				break;
2888*0Sstevel@tonic-gate 		}
2889*0Sstevel@tonic-gate 
2890*0Sstevel@tonic-gate 		if (i == limit)
2891*0Sstevel@tonic-gate 			break;
2892*0Sstevel@tonic-gate 	}
2893*0Sstevel@tonic-gate 
2894*0Sstevel@tonic-gate 	/*
2895*0Sstevel@tonic-gate 	 * And now we want to pick the largest power of two smaller than the
2896*0Sstevel@tonic-gate 	 * hashsize.
2897*0Sstevel@tonic-gate 	 */
2898*0Sstevel@tonic-gate 	for (i = 0; (1 << i) < agb.dtagb_hashsize; i++)
2899*0Sstevel@tonic-gate 		continue;
2900*0Sstevel@tonic-gate 
2901*0Sstevel@tonic-gate 	evenpow = (1 << (i - 1));
2902*0Sstevel@tonic-gate 
2903*0Sstevel@tonic-gate 	for (i = 0; _dtrace_hashstat[i].dths_name != NULL; i++) {
2904*0Sstevel@tonic-gate 		data.dtagsd_func = _dtrace_hashstat[i].dths_func;
2905*0Sstevel@tonic-gate 
2906*0Sstevel@tonic-gate 		hdata->dthsd_hashsize = actual;
2907*0Sstevel@tonic-gate 		hsize = hdata->dthsd_hashsize * sizeof (size_t);
2908*0Sstevel@tonic-gate 		bzero(hdata->dthsd_counts, hsize);
2909*0Sstevel@tonic-gate 
2910*0Sstevel@tonic-gate 		if (mdb_pwalk("dtrace_aggkey",
2911*0Sstevel@tonic-gate 		    (mdb_walk_cb_t)dtrace_aggstat_walk, &data, addr) == -1) {
2912*0Sstevel@tonic-gate 			mdb_warn("failed to walk dtrace_aggkey at %p", addr);
2913*0Sstevel@tonic-gate 			return (DCMD_ERR);
2914*0Sstevel@tonic-gate 		}
2915*0Sstevel@tonic-gate 
2916*0Sstevel@tonic-gate 		dtrace_hashstat_stats(_dtrace_hashstat[i].dths_name, hdata);
2917*0Sstevel@tonic-gate 
2918*0Sstevel@tonic-gate 		/*
2919*0Sstevel@tonic-gate 		 * If we were just printing the actual value, we won't try
2920*0Sstevel@tonic-gate 		 * any of the sizing experiments.
2921*0Sstevel@tonic-gate 		 */
2922*0Sstevel@tonic-gate 		if (data.dtagsd_func == NULL)
2923*0Sstevel@tonic-gate 			continue;
2924*0Sstevel@tonic-gate 
2925*0Sstevel@tonic-gate 		hdata->dthsd_hashsize = prime;
2926*0Sstevel@tonic-gate 		hsize = hdata->dthsd_hashsize * sizeof (size_t);
2927*0Sstevel@tonic-gate 		bzero(hdata->dthsd_counts, hsize);
2928*0Sstevel@tonic-gate 
2929*0Sstevel@tonic-gate 		if (mdb_pwalk("dtrace_aggkey",
2930*0Sstevel@tonic-gate 		    (mdb_walk_cb_t)dtrace_aggstat_walk, &data, addr) == -1) {
2931*0Sstevel@tonic-gate 			mdb_warn("failed to walk dtrace_aggkey at %p", addr);
2932*0Sstevel@tonic-gate 			return (DCMD_ERR);
2933*0Sstevel@tonic-gate 		}
2934*0Sstevel@tonic-gate 
2935*0Sstevel@tonic-gate 		dtrace_hashstat_stats(_dtrace_hashstat[i].dths_name, hdata);
2936*0Sstevel@tonic-gate 
2937*0Sstevel@tonic-gate 		hdata->dthsd_hashsize = evenpow;
2938*0Sstevel@tonic-gate 		hsize = hdata->dthsd_hashsize * sizeof (size_t);
2939*0Sstevel@tonic-gate 		bzero(hdata->dthsd_counts, hsize);
2940*0Sstevel@tonic-gate 
2941*0Sstevel@tonic-gate 		if (mdb_pwalk("dtrace_aggkey",
2942*0Sstevel@tonic-gate 		    (mdb_walk_cb_t)dtrace_aggstat_walk, &data, addr) == -1) {
2943*0Sstevel@tonic-gate 			mdb_warn("failed to walk dtrace_aggkey at %p", addr);
2944*0Sstevel@tonic-gate 			return (DCMD_ERR);
2945*0Sstevel@tonic-gate 		}
2946*0Sstevel@tonic-gate 
2947*0Sstevel@tonic-gate 		dtrace_hashstat_stats(_dtrace_hashstat[i].dths_name, hdata);
2948*0Sstevel@tonic-gate 	}
2949*0Sstevel@tonic-gate 
2950*0Sstevel@tonic-gate 	return (DCMD_OK);
2951*0Sstevel@tonic-gate }
2952*0Sstevel@tonic-gate 
2953*0Sstevel@tonic-gate /*ARGSUSED*/
2954*0Sstevel@tonic-gate static int
2955*0Sstevel@tonic-gate dtrace_dynstat_walk(uintptr_t addr, dtrace_dynvar_t *dynvar,
2956*0Sstevel@tonic-gate     dtrace_aggstat_data_t *data)
2957*0Sstevel@tonic-gate {
2958*0Sstevel@tonic-gate 	dtrace_hashstat_data_t *hdata = &data->dtagsd_hash;
2959*0Sstevel@tonic-gate 	dtrace_tuple_t *tuple = &dynvar->dtdv_tuple;
2960*0Sstevel@tonic-gate 	dtrace_key_t *key = tuple->dtt_key;
2961*0Sstevel@tonic-gate 	size_t size = 0, offs = 0;
2962*0Sstevel@tonic-gate 	int i, nkeys = tuple->dtt_nkeys;
2963*0Sstevel@tonic-gate 	char *buf;
2964*0Sstevel@tonic-gate 
2965*0Sstevel@tonic-gate 	if (data->dtagsd_func == NULL) {
2966*0Sstevel@tonic-gate 		size_t bucket = dynvar->dtdv_hashval % hdata->dthsd_hashsize;
2967*0Sstevel@tonic-gate 
2968*0Sstevel@tonic-gate 		hdata->dthsd_counts[bucket]++;
2969*0Sstevel@tonic-gate 		return (WALK_NEXT);
2970*0Sstevel@tonic-gate 	}
2971*0Sstevel@tonic-gate 
2972*0Sstevel@tonic-gate 	/*
2973*0Sstevel@tonic-gate 	 * We want to hand the hashing algorithm a contiguous buffer.  First
2974*0Sstevel@tonic-gate 	 * run through the tuple and determine the size.
2975*0Sstevel@tonic-gate 	 */
2976*0Sstevel@tonic-gate 	for (i = 0; i < nkeys; i++) {
2977*0Sstevel@tonic-gate 		if (key[i].dttk_size == 0) {
2978*0Sstevel@tonic-gate 			size += sizeof (uint64_t);
2979*0Sstevel@tonic-gate 		} else {
2980*0Sstevel@tonic-gate 			size += key[i].dttk_size;
2981*0Sstevel@tonic-gate 		}
2982*0Sstevel@tonic-gate 	}
2983*0Sstevel@tonic-gate 
2984*0Sstevel@tonic-gate 	buf = alloca(size);
2985*0Sstevel@tonic-gate 
2986*0Sstevel@tonic-gate 	/*
2987*0Sstevel@tonic-gate 	 * Now go back through the tuple and copy the data into the buffer.
2988*0Sstevel@tonic-gate 	 */
2989*0Sstevel@tonic-gate 	for (i = 0; i < nkeys; i++) {
2990*0Sstevel@tonic-gate 		if (key[i].dttk_size == 0) {
2991*0Sstevel@tonic-gate 			bcopy(&key[i].dttk_value, &buf[offs],
2992*0Sstevel@tonic-gate 			    sizeof (uint64_t));
2993*0Sstevel@tonic-gate 			offs += sizeof (uint64_t);
2994*0Sstevel@tonic-gate 		} else {
2995*0Sstevel@tonic-gate 			if (mdb_vread(&buf[offs], key[i].dttk_size,
2996*0Sstevel@tonic-gate 			    key[i].dttk_value) == -1) {
2997*0Sstevel@tonic-gate 				mdb_warn("couldn't read tuple data at %p",
2998*0Sstevel@tonic-gate 				    key[i].dttk_value);
2999*0Sstevel@tonic-gate 				return (WALK_ERR);
3000*0Sstevel@tonic-gate 			}
3001*0Sstevel@tonic-gate 
3002*0Sstevel@tonic-gate 			offs += key[i].dttk_size;
3003*0Sstevel@tonic-gate 		}
3004*0Sstevel@tonic-gate 	}
3005*0Sstevel@tonic-gate 
3006*0Sstevel@tonic-gate 	hdata->dthsd_data = buf;
3007*0Sstevel@tonic-gate 	hdata->dthsd_size = size;
3008*0Sstevel@tonic-gate 
3009*0Sstevel@tonic-gate 	data->dtagsd_func(hdata);
3010*0Sstevel@tonic-gate 
3011*0Sstevel@tonic-gate 	return (WALK_NEXT);
3012*0Sstevel@tonic-gate }
3013*0Sstevel@tonic-gate 
3014*0Sstevel@tonic-gate /*ARGSUSED*/
3015*0Sstevel@tonic-gate int
3016*0Sstevel@tonic-gate dtrace_dynstat(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
3017*0Sstevel@tonic-gate {
3018*0Sstevel@tonic-gate 	dtrace_dstate_t dstate;
3019*0Sstevel@tonic-gate 	size_t hsize, i, actual, prime;
3020*0Sstevel@tonic-gate 	dtrace_aggstat_data_t data;
3021*0Sstevel@tonic-gate 	dtrace_hashstat_data_t *hdata = &data.dtagsd_hash;
3022*0Sstevel@tonic-gate 
3023*0Sstevel@tonic-gate 	bzero(&data, sizeof (data));
3024*0Sstevel@tonic-gate 
3025*0Sstevel@tonic-gate 	if (!(flags & DCMD_ADDRSPEC))
3026*0Sstevel@tonic-gate 		return (DCMD_USAGE);
3027*0Sstevel@tonic-gate 
3028*0Sstevel@tonic-gate 	if (mdb_vread(&dstate, sizeof (dstate), addr) == -1) {
3029*0Sstevel@tonic-gate 		mdb_warn("failed to read dynamic variable state at %p", addr);
3030*0Sstevel@tonic-gate 		return (DCMD_ERR);
3031*0Sstevel@tonic-gate 	}
3032*0Sstevel@tonic-gate 
3033*0Sstevel@tonic-gate 	hsize = (actual = dstate.dtds_hashsize) * sizeof (size_t);
3034*0Sstevel@tonic-gate 	hdata->dthsd_counts = mdb_alloc(hsize, UM_SLEEP | UM_GC);
3035*0Sstevel@tonic-gate 
3036*0Sstevel@tonic-gate 	/*
3037*0Sstevel@tonic-gate 	 * Now pick the largest prime smaller than the hash size.  (If the
3038*0Sstevel@tonic-gate 	 * existing size is prime, we'll pick a smaller prime just for the
3039*0Sstevel@tonic-gate 	 * hell of it.)
3040*0Sstevel@tonic-gate 	 */
3041*0Sstevel@tonic-gate 	for (prime = dstate.dtds_hashsize - 1; prime > 7; prime--) {
3042*0Sstevel@tonic-gate 		size_t limit = prime / 7;
3043*0Sstevel@tonic-gate 
3044*0Sstevel@tonic-gate 		for (i = 2; i < limit; i++) {
3045*0Sstevel@tonic-gate 			if ((prime % i) == 0)
3046*0Sstevel@tonic-gate 				break;
3047*0Sstevel@tonic-gate 		}
3048*0Sstevel@tonic-gate 
3049*0Sstevel@tonic-gate 		if (i == limit)
3050*0Sstevel@tonic-gate 			break;
3051*0Sstevel@tonic-gate 	}
3052*0Sstevel@tonic-gate 
3053*0Sstevel@tonic-gate 	for (i = 0; _dtrace_hashstat[i].dths_name != NULL; i++) {
3054*0Sstevel@tonic-gate 		data.dtagsd_func = _dtrace_hashstat[i].dths_func;
3055*0Sstevel@tonic-gate 
3056*0Sstevel@tonic-gate 		hdata->dthsd_hashsize = actual;
3057*0Sstevel@tonic-gate 		hsize = hdata->dthsd_hashsize * sizeof (size_t);
3058*0Sstevel@tonic-gate 		bzero(hdata->dthsd_counts, hsize);
3059*0Sstevel@tonic-gate 
3060*0Sstevel@tonic-gate 		if (mdb_pwalk("dtrace_dynvar",
3061*0Sstevel@tonic-gate 		    (mdb_walk_cb_t)dtrace_dynstat_walk, &data, addr) == -1) {
3062*0Sstevel@tonic-gate 			mdb_warn("failed to walk dtrace_dynvar at %p", addr);
3063*0Sstevel@tonic-gate 			return (DCMD_ERR);
3064*0Sstevel@tonic-gate 		}
3065*0Sstevel@tonic-gate 
3066*0Sstevel@tonic-gate 		dtrace_hashstat_stats(_dtrace_hashstat[i].dths_name, hdata);
3067*0Sstevel@tonic-gate 
3068*0Sstevel@tonic-gate 		/*
3069*0Sstevel@tonic-gate 		 * If we were just printing the actual value, we won't try
3070*0Sstevel@tonic-gate 		 * any of the sizing experiments.
3071*0Sstevel@tonic-gate 		 */
3072*0Sstevel@tonic-gate 		if (data.dtagsd_func == NULL)
3073*0Sstevel@tonic-gate 			continue;
3074*0Sstevel@tonic-gate 
3075*0Sstevel@tonic-gate 		hdata->dthsd_hashsize = prime;
3076*0Sstevel@tonic-gate 		hsize = hdata->dthsd_hashsize * sizeof (size_t);
3077*0Sstevel@tonic-gate 		bzero(hdata->dthsd_counts, hsize);
3078*0Sstevel@tonic-gate 
3079*0Sstevel@tonic-gate 		if (mdb_pwalk("dtrace_dynvar",
3080*0Sstevel@tonic-gate 		    (mdb_walk_cb_t)dtrace_dynstat_walk, &data, addr) == -1) {
3081*0Sstevel@tonic-gate 			mdb_warn("failed to walk dtrace_aggkey at %p", addr);
3082*0Sstevel@tonic-gate 			return (DCMD_ERR);
3083*0Sstevel@tonic-gate 		}
3084*0Sstevel@tonic-gate 
3085*0Sstevel@tonic-gate 		dtrace_hashstat_stats(_dtrace_hashstat[i].dths_name, hdata);
3086*0Sstevel@tonic-gate 	}
3087*0Sstevel@tonic-gate 
3088*0Sstevel@tonic-gate 	return (DCMD_OK);
3089*0Sstevel@tonic-gate }
3090*0Sstevel@tonic-gate 
3091*0Sstevel@tonic-gate static int
3092*0Sstevel@tonic-gate dof_sect_strtab(uintptr_t addr, dof_sec_t *sec)
3093*0Sstevel@tonic-gate {
3094*0Sstevel@tonic-gate 	char *strtab;
3095*0Sstevel@tonic-gate 	size_t sz, i;
3096*0Sstevel@tonic-gate 
3097*0Sstevel@tonic-gate 	sz = (size_t)sec->dofs_size;
3098*0Sstevel@tonic-gate 	strtab = mdb_alloc(sz, UM_SLEEP | UM_GC);
3099*0Sstevel@tonic-gate 	if (mdb_vread(strtab, sz, addr + sec->dofs_offset) != sz) {
3100*0Sstevel@tonic-gate 		mdb_warn("failed to read string table");
3101*0Sstevel@tonic-gate 		return (1);
3102*0Sstevel@tonic-gate 	}
3103*0Sstevel@tonic-gate 
3104*0Sstevel@tonic-gate 	mdb_printf("size = %lx\n", sz);
3105*0Sstevel@tonic-gate 
3106*0Sstevel@tonic-gate 	for (i = 0; i < sz; i++) {
3107*0Sstevel@tonic-gate 		if (strtab[i] == '\0')
3108*0Sstevel@tonic-gate 			mdb_printf("\\0");
3109*0Sstevel@tonic-gate 		else
3110*0Sstevel@tonic-gate 			mdb_printf("%c", strtab[i]);
3111*0Sstevel@tonic-gate 	}
3112*0Sstevel@tonic-gate 
3113*0Sstevel@tonic-gate 	mdb_printf("\n");
3114*0Sstevel@tonic-gate 
3115*0Sstevel@tonic-gate 	return (0);
3116*0Sstevel@tonic-gate }
3117*0Sstevel@tonic-gate 
3118*0Sstevel@tonic-gate static int
3119*0Sstevel@tonic-gate dof_sect_provider(uintptr_t addr, dof_sec_t *sec, dof_sec_t *dofs)
3120*0Sstevel@tonic-gate {
3121*0Sstevel@tonic-gate 	dof_provider_t pv;
3122*0Sstevel@tonic-gate 	dof_probe_t *pb;
3123*0Sstevel@tonic-gate 	char *strtab;
3124*0Sstevel@tonic-gate 	uint32_t *offs;
3125*0Sstevel@tonic-gate 	uint8_t *args = NULL;
3126*0Sstevel@tonic-gate 	size_t sz;
3127*0Sstevel@tonic-gate 	int i, j;
3128*0Sstevel@tonic-gate 	dof_stridx_t narg, xarg;
3129*0Sstevel@tonic-gate 
3130*0Sstevel@tonic-gate 	if (mdb_vread(&pv, sizeof (dof_provider_t),
3131*0Sstevel@tonic-gate 	    addr + sec->dofs_offset) != sizeof (dof_provider_t)) {
3132*0Sstevel@tonic-gate 		mdb_warn("failed to read DOF provider");
3133*0Sstevel@tonic-gate 		return (-1);
3134*0Sstevel@tonic-gate 	}
3135*0Sstevel@tonic-gate 
3136*0Sstevel@tonic-gate 	sz = dofs[pv.dofpv_strtab].dofs_size;
3137*0Sstevel@tonic-gate 	strtab = mdb_alloc(sz, UM_SLEEP | UM_GC);
3138*0Sstevel@tonic-gate 	if (mdb_vread(strtab, sz, addr +
3139*0Sstevel@tonic-gate 	    dofs[pv.dofpv_strtab].dofs_offset) != sz) {
3140*0Sstevel@tonic-gate 		mdb_warn("failed to read string table");
3141*0Sstevel@tonic-gate 		return (-1);
3142*0Sstevel@tonic-gate 	}
3143*0Sstevel@tonic-gate 
3144*0Sstevel@tonic-gate 	mdb_printf("%lx provider %s {\n", (ulong_t)(addr + sec->dofs_offset),
3145*0Sstevel@tonic-gate 	    strtab + pv.dofpv_name);
3146*0Sstevel@tonic-gate 
3147*0Sstevel@tonic-gate 	sz = dofs[pv.dofpv_prargs].dofs_size;
3148*0Sstevel@tonic-gate 	if (sz != 0) {
3149*0Sstevel@tonic-gate 		args = mdb_alloc(sz, UM_SLEEP | UM_GC);
3150*0Sstevel@tonic-gate 		if (mdb_vread(args, sz, addr +
3151*0Sstevel@tonic-gate 		    dofs[pv.dofpv_prargs].dofs_offset) != sz) {
3152*0Sstevel@tonic-gate 			mdb_warn("failed to read args");
3153*0Sstevel@tonic-gate 			return (-1);
3154*0Sstevel@tonic-gate 		}
3155*0Sstevel@tonic-gate 	}
3156*0Sstevel@tonic-gate 
3157*0Sstevel@tonic-gate 	sz = dofs[pv.dofpv_proffs].dofs_size;
3158*0Sstevel@tonic-gate 	offs = mdb_alloc(sz, UM_SLEEP | UM_GC);
3159*0Sstevel@tonic-gate 	if (mdb_vread(offs, sz, addr + dofs[pv.dofpv_proffs].dofs_offset)
3160*0Sstevel@tonic-gate 	    != sz) {
3161*0Sstevel@tonic-gate 		mdb_warn("failed to read offs");
3162*0Sstevel@tonic-gate 		return (-1);
3163*0Sstevel@tonic-gate 	}
3164*0Sstevel@tonic-gate 
3165*0Sstevel@tonic-gate 	sz = dofs[pv.dofpv_probes].dofs_size;
3166*0Sstevel@tonic-gate 	pb = mdb_alloc(sz, UM_SLEEP | UM_GC);
3167*0Sstevel@tonic-gate 	if (mdb_vread(pb, sz, addr + dofs[pv.dofpv_probes].dofs_offset) != sz) {
3168*0Sstevel@tonic-gate 		mdb_warn("failed to read probes");
3169*0Sstevel@tonic-gate 		return (-1);
3170*0Sstevel@tonic-gate 	}
3171*0Sstevel@tonic-gate 
3172*0Sstevel@tonic-gate 	(void) mdb_inc_indent(2);
3173*0Sstevel@tonic-gate 
3174*0Sstevel@tonic-gate 	for (i = 0; i < sz / dofs[pv.dofpv_probes].dofs_entsize; i++) {
3175*0Sstevel@tonic-gate 		mdb_printf("%lx probe %s:%s {\n", (ulong_t)(addr +
3176*0Sstevel@tonic-gate 		    dofs[pv.dofpv_probes].dofs_offset +
3177*0Sstevel@tonic-gate 		    i * dofs[pv.dofpv_probes].dofs_entsize),
3178*0Sstevel@tonic-gate 		    strtab + pb[i].dofpr_func,
3179*0Sstevel@tonic-gate 		    strtab + pb[i].dofpr_name);
3180*0Sstevel@tonic-gate 
3181*0Sstevel@tonic-gate 		(void) mdb_inc_indent(2);
3182*0Sstevel@tonic-gate 		mdb_printf("addr: %p\n", (ulong_t)pb[i].dofpr_addr);
3183*0Sstevel@tonic-gate 		mdb_printf("offs: ");
3184*0Sstevel@tonic-gate 		for (j = 0; j < pb[i].dofpr_noffs; j++) {
3185*0Sstevel@tonic-gate 			mdb_printf("%s %x", "," + (j == 0),
3186*0Sstevel@tonic-gate 			    offs[pb[i].dofpr_offidx + j]);
3187*0Sstevel@tonic-gate 		}
3188*0Sstevel@tonic-gate 		mdb_printf("\n");
3189*0Sstevel@tonic-gate 
3190*0Sstevel@tonic-gate 		mdb_printf("nargs:");
3191*0Sstevel@tonic-gate 		narg = pb[i].dofpr_nargv;
3192*0Sstevel@tonic-gate 		for (j = 0; j < pb[i].dofpr_nargc; j++) {
3193*0Sstevel@tonic-gate 			mdb_printf("%s %s", "," + (j == 0), strtab + narg);
3194*0Sstevel@tonic-gate 			narg += strlen(strtab + narg) + 1;
3195*0Sstevel@tonic-gate 		}
3196*0Sstevel@tonic-gate 		mdb_printf("\n");
3197*0Sstevel@tonic-gate 		mdb_printf("xargs:");
3198*0Sstevel@tonic-gate 		xarg = pb[i].dofpr_xargv;
3199*0Sstevel@tonic-gate 		for (j = 0; j < pb[i].dofpr_xargc; j++) {
3200*0Sstevel@tonic-gate 			mdb_printf("%s %s", "," + (j == 0), strtab + xarg);
3201*0Sstevel@tonic-gate 			xarg += strlen(strtab + xarg) + 1;
3202*0Sstevel@tonic-gate 		}
3203*0Sstevel@tonic-gate 		mdb_printf("\n");
3204*0Sstevel@tonic-gate 		mdb_printf("map:  ");
3205*0Sstevel@tonic-gate 		for (j = 0; j < pb[i].dofpr_xargc; j++) {
3206*0Sstevel@tonic-gate 			mdb_printf("%s %d->%d", "," + (j == 0),
3207*0Sstevel@tonic-gate 			    args[pb[i].dofpr_argidx + j], j);
3208*0Sstevel@tonic-gate 		}
3209*0Sstevel@tonic-gate 
3210*0Sstevel@tonic-gate 		(void) mdb_dec_indent(2);
3211*0Sstevel@tonic-gate 		mdb_printf("\n}\n");
3212*0Sstevel@tonic-gate 	}
3213*0Sstevel@tonic-gate 
3214*0Sstevel@tonic-gate 	(void) mdb_dec_indent(2);
3215*0Sstevel@tonic-gate 	mdb_printf("}\n");
3216*0Sstevel@tonic-gate 
3217*0Sstevel@tonic-gate 	return (0);
3218*0Sstevel@tonic-gate }
3219*0Sstevel@tonic-gate 
3220*0Sstevel@tonic-gate static int
3221*0Sstevel@tonic-gate dof_sect_prargs(uintptr_t addr, dof_sec_t *sec)
3222*0Sstevel@tonic-gate {
3223*0Sstevel@tonic-gate 	int i;
3224*0Sstevel@tonic-gate 	uint8_t arg;
3225*0Sstevel@tonic-gate 
3226*0Sstevel@tonic-gate 	for (i = 0; i < sec->dofs_size; i++) {
3227*0Sstevel@tonic-gate 		if (mdb_vread(&arg, sizeof (arg),
3228*0Sstevel@tonic-gate 		    addr + sec->dofs_offset + i) != sizeof (arg)) {
3229*0Sstevel@tonic-gate 			mdb_warn("failed to read argument");
3230*0Sstevel@tonic-gate 			return (1);
3231*0Sstevel@tonic-gate 		}
3232*0Sstevel@tonic-gate 
3233*0Sstevel@tonic-gate 		mdb_printf("%d ", arg);
3234*0Sstevel@tonic-gate 
3235*0Sstevel@tonic-gate 		if (i % 20 == 19)
3236*0Sstevel@tonic-gate 			mdb_printf("\n");
3237*0Sstevel@tonic-gate 	}
3238*0Sstevel@tonic-gate 
3239*0Sstevel@tonic-gate 	mdb_printf("\n");
3240*0Sstevel@tonic-gate 
3241*0Sstevel@tonic-gate 	return (0);
3242*0Sstevel@tonic-gate }
3243*0Sstevel@tonic-gate 
3244*0Sstevel@tonic-gate /*ARGSUSED*/
3245*0Sstevel@tonic-gate static int
3246*0Sstevel@tonic-gate dofdump(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
3247*0Sstevel@tonic-gate {
3248*0Sstevel@tonic-gate 	dof_hdr_t dofh;
3249*0Sstevel@tonic-gate 	dof_sec_t *dofs;
3250*0Sstevel@tonic-gate 	int i;
3251*0Sstevel@tonic-gate 
3252*0Sstevel@tonic-gate 	if (mdb_vread(&dofh, sizeof (dof_hdr_t), addr) != sizeof (dof_hdr_t)) {
3253*0Sstevel@tonic-gate 		mdb_warn("failed to read DOF header");
3254*0Sstevel@tonic-gate 		return (DCMD_ERR);
3255*0Sstevel@tonic-gate 	}
3256*0Sstevel@tonic-gate 
3257*0Sstevel@tonic-gate 	dofs = mdb_alloc(sizeof (dof_sec_t) * dofh.dofh_secnum,
3258*0Sstevel@tonic-gate 	    UM_SLEEP | UM_GC);
3259*0Sstevel@tonic-gate 
3260*0Sstevel@tonic-gate 	for (i = 0; i < dofh.dofh_secnum; i++) {
3261*0Sstevel@tonic-gate 		if (mdb_vread(&dofs[i], sizeof (dof_sec_t), dofh.dofh_secoff +
3262*0Sstevel@tonic-gate 		    addr + i * dofh.dofh_secsize) != sizeof (dof_sec_t)) {
3263*0Sstevel@tonic-gate 			mdb_warn("failed to read DOF sections");
3264*0Sstevel@tonic-gate 			return (DCMD_ERR);
3265*0Sstevel@tonic-gate 		}
3266*0Sstevel@tonic-gate 	}
3267*0Sstevel@tonic-gate 
3268*0Sstevel@tonic-gate 	for (i = 0; i < dofh.dofh_secnum; i++) {
3269*0Sstevel@tonic-gate 		mdb_printf("%lx Section %d: %s\n", (ulong_t)(dofh.dofh_secoff +
3270*0Sstevel@tonic-gate 		    addr + i * dofh.dofh_secsize), i,
3271*0Sstevel@tonic-gate 		    dof_sec_typename(dofs[i].dofs_type));
3272*0Sstevel@tonic-gate 
3273*0Sstevel@tonic-gate 		(void) mdb_inc_indent(2);
3274*0Sstevel@tonic-gate 		switch (dofs[i].dofs_type) {
3275*0Sstevel@tonic-gate 		case DOF_SECT_PROVIDER:
3276*0Sstevel@tonic-gate 			(void) dof_sect_provider(addr, &dofs[i], dofs);
3277*0Sstevel@tonic-gate 			break;
3278*0Sstevel@tonic-gate 		case DOF_SECT_STRTAB:
3279*0Sstevel@tonic-gate 			(void) dof_sect_strtab(addr, &dofs[i]);
3280*0Sstevel@tonic-gate 			break;
3281*0Sstevel@tonic-gate 		case DOF_SECT_PRARGS:
3282*0Sstevel@tonic-gate 			(void) dof_sect_prargs(addr, &dofs[i]);
3283*0Sstevel@tonic-gate 			break;
3284*0Sstevel@tonic-gate 		}
3285*0Sstevel@tonic-gate 		(void) mdb_dec_indent(2);
3286*0Sstevel@tonic-gate 
3287*0Sstevel@tonic-gate 		mdb_printf("\n");
3288*0Sstevel@tonic-gate 	}
3289*0Sstevel@tonic-gate 
3290*0Sstevel@tonic-gate 	return (DCMD_OK);
3291*0Sstevel@tonic-gate }
3292*0Sstevel@tonic-gate 
3293*0Sstevel@tonic-gate static const mdb_dcmd_t dcmds[] = {
3294*0Sstevel@tonic-gate 	{ "id2probe", ":", "translate a dtrace_id_t to a dtrace_probe_t",
3295*0Sstevel@tonic-gate 	    id2probe },
3296*0Sstevel@tonic-gate 	{ "difinstr", ":", "disassemble a DIF instruction", difinstr },
3297*0Sstevel@tonic-gate 	{ "difo", ":", "print a DIF object", difo },
3298*0Sstevel@tonic-gate 	{ "dof_hdr", "?", "print a DOF header", dof_hdr },
3299*0Sstevel@tonic-gate 	{ "dof_sec", ":", "print a DOF section header", dof_sec },
3300*0Sstevel@tonic-gate 	{ "dof_ecbdesc", ":", "print a DOF ecbdesc", dof_ecbdesc },
3301*0Sstevel@tonic-gate 	{ "dof_probedesc", ":", "print a DOF probedesc", dof_probedesc },
3302*0Sstevel@tonic-gate 	{ "dof_actdesc", ":", "print a DOF actdesc", dof_actdesc },
3303*0Sstevel@tonic-gate 	{ "dof_relohdr", ":", "print a DOF relocation header", dof_relohdr },
3304*0Sstevel@tonic-gate 	{ "dof_relodesc", ":", "print a DOF relodesc", dof_relodesc },
3305*0Sstevel@tonic-gate 	{ "dofdump", ":", "dump DOF", dofdump },
3306*0Sstevel@tonic-gate 	{ "dtrace", ":[-c cpu]", "print dtrace(1M)-like output",
3307*0Sstevel@tonic-gate 	    dtrace, dtrace_help },
3308*0Sstevel@tonic-gate 	{ "dtrace_errhash", ":", "print DTrace error hash", dtrace_errhash },
3309*0Sstevel@tonic-gate 	{ "dtrace_helptrace", ":", "print DTrace helper trace",
3310*0Sstevel@tonic-gate 	    dtrace_helptrace },
3311*0Sstevel@tonic-gate 	{ "dtrace_state", ":", "print active DTrace consumers", dtrace_state,
3312*0Sstevel@tonic-gate 	    dtrace_state_help },
3313*0Sstevel@tonic-gate 	{ "dtrace_aggstat", ":",
3314*0Sstevel@tonic-gate 	    "print DTrace aggregation hash statistics", dtrace_aggstat },
3315*0Sstevel@tonic-gate 	{ "dtrace_dynstat", ":",
3316*0Sstevel@tonic-gate 	    "print DTrace dynamic variable hash statistics", dtrace_dynstat },
3317*0Sstevel@tonic-gate 	{ NULL }
3318*0Sstevel@tonic-gate };
3319*0Sstevel@tonic-gate 
3320*0Sstevel@tonic-gate static const mdb_walker_t walkers[] = {
3321*0Sstevel@tonic-gate 	{ "dof_sec", "walk DOF section header table given header address",
3322*0Sstevel@tonic-gate 		dof_sec_walk_init, dof_sec_walk_step, dof_sec_walk_fini },
3323*0Sstevel@tonic-gate 	{ "dtrace_errhash", "walk hash of DTrace error messasges",
3324*0Sstevel@tonic-gate 		dtrace_errhash_init, dtrace_errhash_step },
3325*0Sstevel@tonic-gate 	{ "dtrace_helptrace", "walk DTrace helper trace entries",
3326*0Sstevel@tonic-gate 		dtrace_helptrace_init, dtrace_helptrace_step },
3327*0Sstevel@tonic-gate 	{ "dtrace_state", "walk DTrace per-consumer softstate",
3328*0Sstevel@tonic-gate 		dtrace_state_init, dtrace_state_step },
3329*0Sstevel@tonic-gate 	{ "dtrace_aggkey", "walk DTrace aggregation keys",
3330*0Sstevel@tonic-gate 		dtrace_aggkey_init, dtrace_aggkey_step, dtrace_aggkey_fini },
3331*0Sstevel@tonic-gate 	{ "dtrace_dynvar", "walk DTrace dynamic variables",
3332*0Sstevel@tonic-gate 		dtrace_dynvar_init, dtrace_dynvar_step, dtrace_dynvar_fini },
3333*0Sstevel@tonic-gate 	{ NULL }
3334*0Sstevel@tonic-gate };
3335*0Sstevel@tonic-gate 
3336*0Sstevel@tonic-gate static const mdb_modinfo_t modinfo = {
3337*0Sstevel@tonic-gate 	MDB_API_VERSION, dcmds, walkers
3338*0Sstevel@tonic-gate };
3339*0Sstevel@tonic-gate 
3340*0Sstevel@tonic-gate const mdb_modinfo_t *
3341*0Sstevel@tonic-gate _mdb_init(void)
3342*0Sstevel@tonic-gate {
3343*0Sstevel@tonic-gate 	return (&modinfo);
3344*0Sstevel@tonic-gate }
3345