xref: /onnv-gate/usr/src/lib/libdtrace/common/dt_dis.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 2004 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 #include <strings.h>
30*0Sstevel@tonic-gate #include <stdio.h>
31*0Sstevel@tonic-gate #include <dt_ident.h>
32*0Sstevel@tonic-gate #include <dt_impl.h>
33*0Sstevel@tonic-gate 
34*0Sstevel@tonic-gate /*ARGSUSED*/
35*0Sstevel@tonic-gate static void
36*0Sstevel@tonic-gate dt_dis_log(const dtrace_difo_t *dp, const char *name, dif_instr_t in, FILE *fp)
37*0Sstevel@tonic-gate {
38*0Sstevel@tonic-gate 	(void) fprintf(fp, "%-4s %%r%u, %%r%u, %%r%u", name,
39*0Sstevel@tonic-gate 	    DIF_INSTR_R1(in), DIF_INSTR_R2(in), DIF_INSTR_RD(in));
40*0Sstevel@tonic-gate }
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate /*ARGSUSED*/
43*0Sstevel@tonic-gate static void
44*0Sstevel@tonic-gate dt_dis_branch(const dtrace_difo_t *dp, const char *name,
45*0Sstevel@tonic-gate 	dif_instr_t in, FILE *fp)
46*0Sstevel@tonic-gate {
47*0Sstevel@tonic-gate 	(void) fprintf(fp, "%-4s %u", name, DIF_INSTR_LABEL(in));
48*0Sstevel@tonic-gate }
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate /*ARGSUSED*/
51*0Sstevel@tonic-gate static void
52*0Sstevel@tonic-gate dt_dis_load(const dtrace_difo_t *dp, const char *name, dif_instr_t in, FILE *fp)
53*0Sstevel@tonic-gate {
54*0Sstevel@tonic-gate 	(void) fprintf(fp, "%-4s [%%r%u], %%r%u", name,
55*0Sstevel@tonic-gate 	    DIF_INSTR_R1(in), DIF_INSTR_RD(in));
56*0Sstevel@tonic-gate }
57*0Sstevel@tonic-gate 
58*0Sstevel@tonic-gate /*ARGSUSED*/
59*0Sstevel@tonic-gate static void
60*0Sstevel@tonic-gate dt_dis_store(const dtrace_difo_t *dp, const char *name,
61*0Sstevel@tonic-gate 	dif_instr_t in, FILE *fp)
62*0Sstevel@tonic-gate {
63*0Sstevel@tonic-gate 	(void) fprintf(fp, "%-4s %%r%u, [%%r%u]", name,
64*0Sstevel@tonic-gate 	    DIF_INSTR_R1(in), DIF_INSTR_RD(in));
65*0Sstevel@tonic-gate }
66*0Sstevel@tonic-gate 
67*0Sstevel@tonic-gate /*ARGSUSED*/
68*0Sstevel@tonic-gate static void
69*0Sstevel@tonic-gate dt_dis_str(const dtrace_difo_t *dp, const char *name, dif_instr_t in, FILE *fp)
70*0Sstevel@tonic-gate {
71*0Sstevel@tonic-gate 	(void) fprintf(fp, "%s", name);
72*0Sstevel@tonic-gate }
73*0Sstevel@tonic-gate 
74*0Sstevel@tonic-gate /*ARGSUSED*/
75*0Sstevel@tonic-gate static void
76*0Sstevel@tonic-gate dt_dis_r1rd(const dtrace_difo_t *dp, const char *name, dif_instr_t in, FILE *fp)
77*0Sstevel@tonic-gate {
78*0Sstevel@tonic-gate 	(void) fprintf(fp, "%-4s %%r%u, %%r%u", name,
79*0Sstevel@tonic-gate 	    DIF_INSTR_R1(in), DIF_INSTR_RD(in));
80*0Sstevel@tonic-gate }
81*0Sstevel@tonic-gate 
82*0Sstevel@tonic-gate /*ARGSUSED*/
83*0Sstevel@tonic-gate static void
84*0Sstevel@tonic-gate dt_dis_cmp(const dtrace_difo_t *dp, const char *name, dif_instr_t in, FILE *fp)
85*0Sstevel@tonic-gate {
86*0Sstevel@tonic-gate 	(void) fprintf(fp, "%-4s %%r%u, %%r%u", name,
87*0Sstevel@tonic-gate 	    DIF_INSTR_R1(in), DIF_INSTR_R2(in));
88*0Sstevel@tonic-gate }
89*0Sstevel@tonic-gate 
90*0Sstevel@tonic-gate /*ARGSUSED*/
91*0Sstevel@tonic-gate static void
92*0Sstevel@tonic-gate dt_dis_tst(const dtrace_difo_t *dp, const char *name, dif_instr_t in, FILE *fp)
93*0Sstevel@tonic-gate {
94*0Sstevel@tonic-gate 	(void) fprintf(fp, "%-4s %%r%u", name, DIF_INSTR_R1(in));
95*0Sstevel@tonic-gate }
96*0Sstevel@tonic-gate 
97*0Sstevel@tonic-gate static const char *
98*0Sstevel@tonic-gate dt_dis_varname(const dtrace_difo_t *dp, uint_t id, uint_t scope)
99*0Sstevel@tonic-gate {
100*0Sstevel@tonic-gate 	const dtrace_difv_t *dvp = dp->dtdo_vartab;
101*0Sstevel@tonic-gate 	uint_t i;
102*0Sstevel@tonic-gate 
103*0Sstevel@tonic-gate 	for (i = 0; i < dp->dtdo_varlen; i++, dvp++) {
104*0Sstevel@tonic-gate 		if (dvp->dtdv_id == id && dvp->dtdv_scope == scope) {
105*0Sstevel@tonic-gate 			if (dvp->dtdv_name < dp->dtdo_strlen)
106*0Sstevel@tonic-gate 				return (dp->dtdo_strtab + dvp->dtdv_name);
107*0Sstevel@tonic-gate 			break;
108*0Sstevel@tonic-gate 		}
109*0Sstevel@tonic-gate 	}
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate 	return (NULL);
112*0Sstevel@tonic-gate }
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate static uint_t
115*0Sstevel@tonic-gate dt_dis_scope(const char *name)
116*0Sstevel@tonic-gate {
117*0Sstevel@tonic-gate 	switch (name[2]) {
118*0Sstevel@tonic-gate 	case 'l': return (DIFV_SCOPE_LOCAL);
119*0Sstevel@tonic-gate 	case 't': return (DIFV_SCOPE_THREAD);
120*0Sstevel@tonic-gate 	case 'g': return (DIFV_SCOPE_GLOBAL);
121*0Sstevel@tonic-gate 	default: return (-1u);
122*0Sstevel@tonic-gate 	}
123*0Sstevel@tonic-gate }
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate static void
126*0Sstevel@tonic-gate dt_dis_lda(const dtrace_difo_t *dp, const char *name, dif_instr_t in, FILE *fp)
127*0Sstevel@tonic-gate {
128*0Sstevel@tonic-gate 	uint_t var = DIF_INSTR_R1(in);
129*0Sstevel@tonic-gate 	const char *vname;
130*0Sstevel@tonic-gate 
131*0Sstevel@tonic-gate 	(void) fprintf(fp, "%-4s DT_VAR(%u), %%r%u, %%r%u", name,
132*0Sstevel@tonic-gate 	    var, DIF_INSTR_R2(in), DIF_INSTR_RD(in));
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate 	if ((vname = dt_dis_varname(dp, var, dt_dis_scope(name))) != NULL)
135*0Sstevel@tonic-gate 		(void) fprintf(fp, "\t\t! DT_VAR(%u) = \"%s\"", var, vname);
136*0Sstevel@tonic-gate }
137*0Sstevel@tonic-gate 
138*0Sstevel@tonic-gate static void
139*0Sstevel@tonic-gate dt_dis_ldv(const dtrace_difo_t *dp, const char *name, dif_instr_t in, FILE *fp)
140*0Sstevel@tonic-gate {
141*0Sstevel@tonic-gate 	uint_t var = DIF_INSTR_VAR(in);
142*0Sstevel@tonic-gate 	const char *vname;
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate 	(void) fprintf(fp, "%-4s DT_VAR(%u), %%r%u",
145*0Sstevel@tonic-gate 	    name, var, DIF_INSTR_RD(in));
146*0Sstevel@tonic-gate 
147*0Sstevel@tonic-gate 	if ((vname = dt_dis_varname(dp, var, dt_dis_scope(name))) != NULL)
148*0Sstevel@tonic-gate 		(void) fprintf(fp, "\t\t! DT_VAR(%u) = \"%s\"", var, vname);
149*0Sstevel@tonic-gate }
150*0Sstevel@tonic-gate 
151*0Sstevel@tonic-gate static void
152*0Sstevel@tonic-gate dt_dis_stv(const dtrace_difo_t *dp, const char *name, dif_instr_t in, FILE *fp)
153*0Sstevel@tonic-gate {
154*0Sstevel@tonic-gate 	uint_t var = DIF_INSTR_VAR(in);
155*0Sstevel@tonic-gate 	const char *vname;
156*0Sstevel@tonic-gate 
157*0Sstevel@tonic-gate 	(void) fprintf(fp, "%-4s %%r%u, DT_VAR(%u)",
158*0Sstevel@tonic-gate 	    name, DIF_INSTR_RS(in), var);
159*0Sstevel@tonic-gate 
160*0Sstevel@tonic-gate 	if ((vname = dt_dis_varname(dp, var, dt_dis_scope(name))) != NULL)
161*0Sstevel@tonic-gate 		(void) fprintf(fp, "\t\t! DT_VAR(%u) = \"%s\"", var, vname);
162*0Sstevel@tonic-gate }
163*0Sstevel@tonic-gate 
164*0Sstevel@tonic-gate static void
165*0Sstevel@tonic-gate dt_dis_setx(const dtrace_difo_t *dp, const char *name, dif_instr_t in, FILE *fp)
166*0Sstevel@tonic-gate {
167*0Sstevel@tonic-gate 	uint_t intptr = DIF_INSTR_INTEGER(in);
168*0Sstevel@tonic-gate 
169*0Sstevel@tonic-gate 	(void) fprintf(fp, "%-4s DT_INTEGER[%u], %%r%u", name,
170*0Sstevel@tonic-gate 	    intptr, DIF_INSTR_RD(in));
171*0Sstevel@tonic-gate 
172*0Sstevel@tonic-gate 	if (intptr < dp->dtdo_intlen) {
173*0Sstevel@tonic-gate 		(void) fprintf(fp, "\t\t! 0x%llx",
174*0Sstevel@tonic-gate 		    (u_longlong_t)dp->dtdo_inttab[intptr]);
175*0Sstevel@tonic-gate 	}
176*0Sstevel@tonic-gate }
177*0Sstevel@tonic-gate 
178*0Sstevel@tonic-gate static void
179*0Sstevel@tonic-gate dt_dis_sets(const dtrace_difo_t *dp, const char *name, dif_instr_t in, FILE *fp)
180*0Sstevel@tonic-gate {
181*0Sstevel@tonic-gate 	uint_t strptr = DIF_INSTR_STRING(in);
182*0Sstevel@tonic-gate 
183*0Sstevel@tonic-gate 	(void) fprintf(fp, "%-4s DT_STRING[%u], %%r%u", name,
184*0Sstevel@tonic-gate 	    strptr, DIF_INSTR_RD(in));
185*0Sstevel@tonic-gate 
186*0Sstevel@tonic-gate 	if (strptr < dp->dtdo_strlen)
187*0Sstevel@tonic-gate 		(void) fprintf(fp, "\t\t! \"%s\"", dp->dtdo_strtab + strptr);
188*0Sstevel@tonic-gate }
189*0Sstevel@tonic-gate 
190*0Sstevel@tonic-gate /*ARGSUSED*/
191*0Sstevel@tonic-gate static void
192*0Sstevel@tonic-gate dt_dis_ret(const dtrace_difo_t *dp, const char *name, dif_instr_t in, FILE *fp)
193*0Sstevel@tonic-gate {
194*0Sstevel@tonic-gate 	(void) fprintf(fp, "%-4s %%r%u", name, DIF_INSTR_RD(in));
195*0Sstevel@tonic-gate }
196*0Sstevel@tonic-gate 
197*0Sstevel@tonic-gate /*ARGSUSED*/
198*0Sstevel@tonic-gate static void
199*0Sstevel@tonic-gate dt_dis_call(const dtrace_difo_t *dp, const char *name, dif_instr_t in, FILE *fp)
200*0Sstevel@tonic-gate {
201*0Sstevel@tonic-gate 	static const struct {
202*0Sstevel@tonic-gate 		const char *name;
203*0Sstevel@tonic-gate 		int subr;
204*0Sstevel@tonic-gate 	} snames[] = {
205*0Sstevel@tonic-gate 		{ "rand",			DIF_SUBR_RAND },
206*0Sstevel@tonic-gate 		{ "mutex_owned",		DIF_SUBR_MUTEX_OWNED },
207*0Sstevel@tonic-gate 		{ "mutex_owner",		DIF_SUBR_MUTEX_OWNER },
208*0Sstevel@tonic-gate 		{ "mutex_type_adaptive",	DIF_SUBR_MUTEX_TYPE_ADAPTIVE },
209*0Sstevel@tonic-gate 		{ "mutex_type_spin",		DIF_SUBR_MUTEX_TYPE_SPIN },
210*0Sstevel@tonic-gate 		{ "rw_read_held",		DIF_SUBR_RW_READ_HELD },
211*0Sstevel@tonic-gate 		{ "rw_write_held",		DIF_SUBR_RW_WRITE_HELD },
212*0Sstevel@tonic-gate 		{ "rw_iswriter",		DIF_SUBR_RW_ISWRITER },
213*0Sstevel@tonic-gate 		{ "copyin",			DIF_SUBR_COPYIN },
214*0Sstevel@tonic-gate 		{ "copyinstr",			DIF_SUBR_COPYINSTR },
215*0Sstevel@tonic-gate 		{ "speculation",		DIF_SUBR_SPECULATION },
216*0Sstevel@tonic-gate 		{ "progenyof",			DIF_SUBR_PROGENYOF },
217*0Sstevel@tonic-gate 		{ "strlen",			DIF_SUBR_STRLEN },
218*0Sstevel@tonic-gate 		{ "copyout",			DIF_SUBR_COPYOUT },
219*0Sstevel@tonic-gate 		{ "copyoutstr",			DIF_SUBR_COPYOUTSTR },
220*0Sstevel@tonic-gate 		{ "alloca",			DIF_SUBR_ALLOCA },
221*0Sstevel@tonic-gate 		{ "bcopy",			DIF_SUBR_BCOPY },
222*0Sstevel@tonic-gate 		{ "copyinto",			DIF_SUBR_COPYINTO },
223*0Sstevel@tonic-gate 		{ "msgdsize",			DIF_SUBR_MSGDSIZE },
224*0Sstevel@tonic-gate 		{ "msgsize",			DIF_SUBR_MSGSIZE },
225*0Sstevel@tonic-gate 		{ NULL, 0 }
226*0Sstevel@tonic-gate 	};
227*0Sstevel@tonic-gate 
228*0Sstevel@tonic-gate 	uint_t subr = DIF_INSTR_SUBR(in), i;
229*0Sstevel@tonic-gate 
230*0Sstevel@tonic-gate 	(void) fprintf(fp, "%-4s DIF_SUBR(%u), %%r%u",
231*0Sstevel@tonic-gate 	    name, subr, DIF_INSTR_RD(in));
232*0Sstevel@tonic-gate 
233*0Sstevel@tonic-gate 	for (i = 0; snames[i].name != NULL; i++) {
234*0Sstevel@tonic-gate 		if (subr == snames[i].subr) {
235*0Sstevel@tonic-gate 			(void) fprintf(fp, "\t\t! %s", snames[i].name);
236*0Sstevel@tonic-gate 			return;
237*0Sstevel@tonic-gate 		}
238*0Sstevel@tonic-gate 	}
239*0Sstevel@tonic-gate }
240*0Sstevel@tonic-gate 
241*0Sstevel@tonic-gate /*ARGSUSED*/
242*0Sstevel@tonic-gate static void
243*0Sstevel@tonic-gate dt_dis_pushts(const dtrace_difo_t *dp, const char *name,
244*0Sstevel@tonic-gate 	dif_instr_t in, FILE *fp)
245*0Sstevel@tonic-gate {
246*0Sstevel@tonic-gate 	static const char *const tnames[] = { "D type", "string" };
247*0Sstevel@tonic-gate 	uint_t type = DIF_INSTR_TYPE(in);
248*0Sstevel@tonic-gate 
249*0Sstevel@tonic-gate 	(void) fprintf(fp, "%-4s DT_TYPE(%u), %%r%u, %%r%u",
250*0Sstevel@tonic-gate 	    name, type, DIF_INSTR_R2(in), DIF_INSTR_RS(in));
251*0Sstevel@tonic-gate 
252*0Sstevel@tonic-gate 	if (type < sizeof (tnames) / sizeof (tnames[0]))
253*0Sstevel@tonic-gate 		(void) fprintf(fp, "\t! DT_TYPE(%u) = %s", type, tnames[type]);
254*0Sstevel@tonic-gate }
255*0Sstevel@tonic-gate 
256*0Sstevel@tonic-gate static char *
257*0Sstevel@tonic-gate dt_dis_typestr(const dtrace_diftype_t *t, char *buf, size_t len)
258*0Sstevel@tonic-gate {
259*0Sstevel@tonic-gate 	char kind[16], ckind[16];
260*0Sstevel@tonic-gate 
261*0Sstevel@tonic-gate 	switch (t->dtdt_kind) {
262*0Sstevel@tonic-gate 	case DIF_TYPE_CTF:
263*0Sstevel@tonic-gate 		(void) strcpy(kind, "D type");
264*0Sstevel@tonic-gate 		break;
265*0Sstevel@tonic-gate 	case DIF_TYPE_STRING:
266*0Sstevel@tonic-gate 		(void) strcpy(kind, "string");
267*0Sstevel@tonic-gate 		break;
268*0Sstevel@tonic-gate 	default:
269*0Sstevel@tonic-gate 		(void) snprintf(kind, sizeof (kind), "0x%x", t->dtdt_kind);
270*0Sstevel@tonic-gate 	}
271*0Sstevel@tonic-gate 
272*0Sstevel@tonic-gate 	switch (t->dtdt_ckind) {
273*0Sstevel@tonic-gate 	case CTF_K_UNKNOWN:
274*0Sstevel@tonic-gate 		(void) strcpy(ckind, "unknown");
275*0Sstevel@tonic-gate 		break;
276*0Sstevel@tonic-gate 	case CTF_K_INTEGER:
277*0Sstevel@tonic-gate 		(void) strcpy(ckind, "integer");
278*0Sstevel@tonic-gate 		break;
279*0Sstevel@tonic-gate 	case CTF_K_FLOAT:
280*0Sstevel@tonic-gate 		(void) strcpy(ckind, "float");
281*0Sstevel@tonic-gate 		break;
282*0Sstevel@tonic-gate 	case CTF_K_POINTER:
283*0Sstevel@tonic-gate 		(void) strcpy(ckind, "pointer");
284*0Sstevel@tonic-gate 		break;
285*0Sstevel@tonic-gate 	case CTF_K_ARRAY:
286*0Sstevel@tonic-gate 		(void) strcpy(ckind, "array");
287*0Sstevel@tonic-gate 		break;
288*0Sstevel@tonic-gate 	case CTF_K_FUNCTION:
289*0Sstevel@tonic-gate 		(void) strcpy(ckind, "function");
290*0Sstevel@tonic-gate 		break;
291*0Sstevel@tonic-gate 	case CTF_K_STRUCT:
292*0Sstevel@tonic-gate 		(void) strcpy(ckind, "struct");
293*0Sstevel@tonic-gate 		break;
294*0Sstevel@tonic-gate 	case CTF_K_UNION:
295*0Sstevel@tonic-gate 		(void) strcpy(ckind, "union");
296*0Sstevel@tonic-gate 		break;
297*0Sstevel@tonic-gate 	case CTF_K_ENUM:
298*0Sstevel@tonic-gate 		(void) strcpy(ckind, "enum");
299*0Sstevel@tonic-gate 		break;
300*0Sstevel@tonic-gate 	case CTF_K_FORWARD:
301*0Sstevel@tonic-gate 		(void) strcpy(ckind, "forward");
302*0Sstevel@tonic-gate 		break;
303*0Sstevel@tonic-gate 	case CTF_K_TYPEDEF:
304*0Sstevel@tonic-gate 		(void) strcpy(ckind, "typedef");
305*0Sstevel@tonic-gate 		break;
306*0Sstevel@tonic-gate 	case CTF_K_VOLATILE:
307*0Sstevel@tonic-gate 		(void) strcpy(ckind, "volatile");
308*0Sstevel@tonic-gate 		break;
309*0Sstevel@tonic-gate 	case CTF_K_CONST:
310*0Sstevel@tonic-gate 		(void) strcpy(ckind, "const");
311*0Sstevel@tonic-gate 		break;
312*0Sstevel@tonic-gate 	case CTF_K_RESTRICT:
313*0Sstevel@tonic-gate 		(void) strcpy(ckind, "restrict");
314*0Sstevel@tonic-gate 		break;
315*0Sstevel@tonic-gate 	default:
316*0Sstevel@tonic-gate 		(void) snprintf(ckind, sizeof (ckind), "0x%x", t->dtdt_ckind);
317*0Sstevel@tonic-gate 	}
318*0Sstevel@tonic-gate 
319*0Sstevel@tonic-gate 	if (t->dtdt_flags & DIF_TF_BYREF) {
320*0Sstevel@tonic-gate 		(void) snprintf(buf, len, "%s (%s) by ref (size %lu)",
321*0Sstevel@tonic-gate 		    kind, ckind, (ulong_t)t->dtdt_size);
322*0Sstevel@tonic-gate 	} else {
323*0Sstevel@tonic-gate 		(void) snprintf(buf, len, "%s (%s) (size %lu)",
324*0Sstevel@tonic-gate 		    kind, ckind, (ulong_t)t->dtdt_size);
325*0Sstevel@tonic-gate 	}
326*0Sstevel@tonic-gate 
327*0Sstevel@tonic-gate 	return (buf);
328*0Sstevel@tonic-gate }
329*0Sstevel@tonic-gate 
330*0Sstevel@tonic-gate static void
331*0Sstevel@tonic-gate dt_dis_rtab(const char *rtag, const dtrace_difo_t *dp, FILE *fp,
332*0Sstevel@tonic-gate     const dof_relodesc_t *rp, uint32_t len)
333*0Sstevel@tonic-gate {
334*0Sstevel@tonic-gate 	(void) fprintf(fp, "\n%-4s %-8s %-8s %s\n",
335*0Sstevel@tonic-gate 	    rtag, "OFFSET", "DATA", "NAME");
336*0Sstevel@tonic-gate 
337*0Sstevel@tonic-gate 	for (; len != 0; len--, rp++) {
338*0Sstevel@tonic-gate 		(void) fprintf(fp, "%-4u %-8llu %-8llu %s\n",
339*0Sstevel@tonic-gate 		    rp->dofr_type, (u_longlong_t)rp->dofr_offset,
340*0Sstevel@tonic-gate 		    (u_longlong_t)rp->dofr_data,
341*0Sstevel@tonic-gate 		    &dp->dtdo_strtab[rp->dofr_name]);
342*0Sstevel@tonic-gate 	}
343*0Sstevel@tonic-gate }
344*0Sstevel@tonic-gate 
345*0Sstevel@tonic-gate void
346*0Sstevel@tonic-gate dtrace_difo_print(const dtrace_difo_t *dp, FILE *fp)
347*0Sstevel@tonic-gate {
348*0Sstevel@tonic-gate 	static const struct opent {
349*0Sstevel@tonic-gate 		const char *op_name;
350*0Sstevel@tonic-gate 		void (*op_func)(const dtrace_difo_t *, const char *,
351*0Sstevel@tonic-gate 		    dif_instr_t, FILE *);
352*0Sstevel@tonic-gate 	} optab[] = {
353*0Sstevel@tonic-gate 		{ "(illegal opcode)", dt_dis_str },
354*0Sstevel@tonic-gate 		{ "or", dt_dis_log },		/* DIF_OP_OR */
355*0Sstevel@tonic-gate 		{ "xor", dt_dis_log },		/* DIF_OP_XOR */
356*0Sstevel@tonic-gate 		{ "and", dt_dis_log },		/* DIF_OP_AND */
357*0Sstevel@tonic-gate 		{ "sll", dt_dis_log },		/* DIF_OP_SLL */
358*0Sstevel@tonic-gate 		{ "srl", dt_dis_log },		/* DIF_OP_SRL */
359*0Sstevel@tonic-gate 		{ "sub", dt_dis_log },		/* DIF_OP_SUB */
360*0Sstevel@tonic-gate 		{ "add", dt_dis_log },		/* DIF_OP_ADD */
361*0Sstevel@tonic-gate 		{ "mul", dt_dis_log },		/* DIF_OP_MUL */
362*0Sstevel@tonic-gate 		{ "sdiv", dt_dis_log },		/* DIF_OP_SDIV */
363*0Sstevel@tonic-gate 		{ "udiv", dt_dis_log },		/* DIF_OP_UDIV */
364*0Sstevel@tonic-gate 		{ "srem", dt_dis_log },		/* DIF_OP_SREM */
365*0Sstevel@tonic-gate 		{ "urem", dt_dis_log },		/* DIF_OP_UREM */
366*0Sstevel@tonic-gate 		{ "not", dt_dis_r1rd },		/* DIF_OP_NOT */
367*0Sstevel@tonic-gate 		{ "mov", dt_dis_r1rd },		/* DIF_OP_MOV */
368*0Sstevel@tonic-gate 		{ "cmp", dt_dis_cmp },		/* DIF_OP_CMP */
369*0Sstevel@tonic-gate 		{ "tst", dt_dis_tst },		/* DIF_OP_TST */
370*0Sstevel@tonic-gate 		{ "ba", dt_dis_branch },	/* DIF_OP_BA */
371*0Sstevel@tonic-gate 		{ "be", dt_dis_branch },	/* DIF_OP_BE */
372*0Sstevel@tonic-gate 		{ "bne", dt_dis_branch },	/* DIF_OP_BNE */
373*0Sstevel@tonic-gate 		{ "bg", dt_dis_branch },	/* DIF_OP_BG */
374*0Sstevel@tonic-gate 		{ "bgu", dt_dis_branch },	/* DIF_OP_BGU */
375*0Sstevel@tonic-gate 		{ "bge", dt_dis_branch },	/* DIF_OP_BGE */
376*0Sstevel@tonic-gate 		{ "bgeu", dt_dis_branch },	/* DIF_OP_BGEU */
377*0Sstevel@tonic-gate 		{ "bl", dt_dis_branch },	/* DIF_OP_BL */
378*0Sstevel@tonic-gate 		{ "blu", dt_dis_branch },	/* DIF_OP_BLU */
379*0Sstevel@tonic-gate 		{ "ble", dt_dis_branch },	/* DIF_OP_BLE */
380*0Sstevel@tonic-gate 		{ "bleu", dt_dis_branch },	/* DIF_OP_BLEU */
381*0Sstevel@tonic-gate 		{ "ldsb", dt_dis_load },	/* DIF_OP_LDSB */
382*0Sstevel@tonic-gate 		{ "ldsh", dt_dis_load },	/* DIF_OP_LDSH */
383*0Sstevel@tonic-gate 		{ "ldsw", dt_dis_load },	/* DIF_OP_LDSW */
384*0Sstevel@tonic-gate 		{ "ldub", dt_dis_load },	/* DIF_OP_LDUB */
385*0Sstevel@tonic-gate 		{ "lduh", dt_dis_load },	/* DIF_OP_LDUH */
386*0Sstevel@tonic-gate 		{ "lduw", dt_dis_load },	/* DIF_OP_LDUW */
387*0Sstevel@tonic-gate 		{ "ldx", dt_dis_load },		/* DIF_OP_LDX */
388*0Sstevel@tonic-gate 		{ "ret", dt_dis_ret },		/* DIF_OP_RET */
389*0Sstevel@tonic-gate 		{ "nop", dt_dis_str },		/* DIF_OP_NOP */
390*0Sstevel@tonic-gate 		{ "setx", dt_dis_setx },	/* DIF_OP_SETX */
391*0Sstevel@tonic-gate 		{ "sets", dt_dis_sets },	/* DIF_OP_SETS */
392*0Sstevel@tonic-gate 		{ "scmp", dt_dis_cmp },		/* DIF_OP_SCMP */
393*0Sstevel@tonic-gate 		{ "ldga", dt_dis_lda },		/* DIF_OP_LDGA */
394*0Sstevel@tonic-gate 		{ "ldgs", dt_dis_ldv },		/* DIF_OP_LDGS */
395*0Sstevel@tonic-gate 		{ "stgs", dt_dis_stv },		/* DIF_OP_STGS */
396*0Sstevel@tonic-gate 		{ "ldta", dt_dis_lda },		/* DIF_OP_LDTA */
397*0Sstevel@tonic-gate 		{ "ldts", dt_dis_ldv },		/* DIF_OP_LDTS */
398*0Sstevel@tonic-gate 		{ "stts", dt_dis_stv },		/* DIF_OP_STTS */
399*0Sstevel@tonic-gate 		{ "sra", dt_dis_log },		/* DIF_OP_SRA */
400*0Sstevel@tonic-gate 		{ "call", dt_dis_call },	/* DIF_OP_CALL */
401*0Sstevel@tonic-gate 		{ "pushtr", dt_dis_pushts },	/* DIF_OP_PUSHTR */
402*0Sstevel@tonic-gate 		{ "pushtv", dt_dis_pushts },	/* DIF_OP_PUSHTV */
403*0Sstevel@tonic-gate 		{ "popts", dt_dis_str },	/* DIF_OP_POPTS */
404*0Sstevel@tonic-gate 		{ "flushts", dt_dis_str },	/* DIF_OP_FLUSHTS */
405*0Sstevel@tonic-gate 		{ "ldgaa", dt_dis_ldv },	/* DIF_OP_LDGAA */
406*0Sstevel@tonic-gate 		{ "ldtaa", dt_dis_ldv },	/* DIF_OP_LDTAA */
407*0Sstevel@tonic-gate 		{ "stgaa", dt_dis_stv },	/* DIF_OP_STGAA */
408*0Sstevel@tonic-gate 		{ "sttaa", dt_dis_stv },	/* DIF_OP_STTAA */
409*0Sstevel@tonic-gate 		{ "ldls", dt_dis_ldv },		/* DIF_OP_LDLS */
410*0Sstevel@tonic-gate 		{ "stls", dt_dis_stv },		/* DIF_OP_STLS */
411*0Sstevel@tonic-gate 		{ "allocs", dt_dis_r1rd },	/* DIF_OP_ALLOCS */
412*0Sstevel@tonic-gate 		{ "copys", dt_dis_log },	/* DIF_OP_COPYS */
413*0Sstevel@tonic-gate 		{ "stb", dt_dis_store },	/* DIF_OP_STB */
414*0Sstevel@tonic-gate 		{ "sth", dt_dis_store },	/* DIF_OP_STH */
415*0Sstevel@tonic-gate 		{ "stw", dt_dis_store },	/* DIF_OP_STW */
416*0Sstevel@tonic-gate 		{ "stx", dt_dis_store },	/* DIF_OP_STX */
417*0Sstevel@tonic-gate 		{ "uldsb", dt_dis_load },	/* DIF_OP_ULDSB */
418*0Sstevel@tonic-gate 		{ "uldsh", dt_dis_load },	/* DIF_OP_ULDSH */
419*0Sstevel@tonic-gate 		{ "uldsw", dt_dis_load },	/* DIF_OP_ULDSW */
420*0Sstevel@tonic-gate 		{ "uldub", dt_dis_load },	/* DIF_OP_ULDUB */
421*0Sstevel@tonic-gate 		{ "ulduh", dt_dis_load },	/* DIF_OP_ULDUH */
422*0Sstevel@tonic-gate 		{ "ulduw", dt_dis_load },	/* DIF_OP_ULDUW */
423*0Sstevel@tonic-gate 		{ "uldx", dt_dis_load },	/* DIF_OP_ULDX */
424*0Sstevel@tonic-gate 		{ "rldsb", dt_dis_load },	/* DIF_OP_RLDSB */
425*0Sstevel@tonic-gate 		{ "rldsh", dt_dis_load },	/* DIF_OP_RLDSH */
426*0Sstevel@tonic-gate 		{ "rldsw", dt_dis_load },	/* DIF_OP_RLDSW */
427*0Sstevel@tonic-gate 		{ "rldub", dt_dis_load },	/* DIF_OP_RLDUB */
428*0Sstevel@tonic-gate 		{ "rlduh", dt_dis_load },	/* DIF_OP_RLDUH */
429*0Sstevel@tonic-gate 		{ "rlduw", dt_dis_load },	/* DIF_OP_RLDUW */
430*0Sstevel@tonic-gate 		{ "rldx", dt_dis_load },	/* DIF_OP_RLDX */
431*0Sstevel@tonic-gate 	};
432*0Sstevel@tonic-gate 
433*0Sstevel@tonic-gate 	const struct opent *op;
434*0Sstevel@tonic-gate 	ulong_t i = 0;
435*0Sstevel@tonic-gate 	char type[64];
436*0Sstevel@tonic-gate 
437*0Sstevel@tonic-gate 	(void) fprintf(fp, "DIFO 0x%p refcnt=%u returns %s\n",
438*0Sstevel@tonic-gate 	    (void *)dp, dp->dtdo_refcnt,
439*0Sstevel@tonic-gate 	    dt_dis_typestr(&dp->dtdo_rtype, type, sizeof (type)));
440*0Sstevel@tonic-gate 
441*0Sstevel@tonic-gate 	(void) fprintf(fp, "%-3s %-8s    %s\n",
442*0Sstevel@tonic-gate 	    "OFF", "OPCODE", "INSTRUCTION");
443*0Sstevel@tonic-gate 
444*0Sstevel@tonic-gate 	for (i = 0; i < dp->dtdo_len; i++) {
445*0Sstevel@tonic-gate 		dif_instr_t instr = dp->dtdo_buf[i];
446*0Sstevel@tonic-gate 		dif_instr_t opcode = DIF_INSTR_OP(instr);
447*0Sstevel@tonic-gate 
448*0Sstevel@tonic-gate 		if (opcode >= sizeof (optab) / sizeof (optab[0]))
449*0Sstevel@tonic-gate 			opcode = 0; /* force invalid opcode message */
450*0Sstevel@tonic-gate 
451*0Sstevel@tonic-gate 		op = &optab[opcode];
452*0Sstevel@tonic-gate 		(void) fprintf(fp, "%02lu: %08x    ", i, instr);
453*0Sstevel@tonic-gate 		op->op_func(dp, op->op_name, instr, fp);
454*0Sstevel@tonic-gate 		(void) fprintf(fp, "\n");
455*0Sstevel@tonic-gate 	}
456*0Sstevel@tonic-gate 
457*0Sstevel@tonic-gate 	if (dp->dtdo_varlen != 0) {
458*0Sstevel@tonic-gate 		(void) fprintf(fp, "\n%-16s %-4s %-3s %-3s %-4s %s\n",
459*0Sstevel@tonic-gate 		    "NAME", "ID", "KND", "SCP", "FLAG", "TYPE");
460*0Sstevel@tonic-gate 	}
461*0Sstevel@tonic-gate 
462*0Sstevel@tonic-gate 	for (i = 0; i < dp->dtdo_varlen; i++) {
463*0Sstevel@tonic-gate 		dtrace_difv_t *v = &dp->dtdo_vartab[i];
464*0Sstevel@tonic-gate 		char kind[4], scope[4], flags[16] = { 0 };
465*0Sstevel@tonic-gate 
466*0Sstevel@tonic-gate 		switch (v->dtdv_kind) {
467*0Sstevel@tonic-gate 		case DIFV_KIND_ARRAY:
468*0Sstevel@tonic-gate 			(void) strcpy(kind, "arr");
469*0Sstevel@tonic-gate 			break;
470*0Sstevel@tonic-gate 		case DIFV_KIND_SCALAR:
471*0Sstevel@tonic-gate 			(void) strcpy(kind, "scl");
472*0Sstevel@tonic-gate 			break;
473*0Sstevel@tonic-gate 		default:
474*0Sstevel@tonic-gate 			(void) snprintf(kind, sizeof (kind),
475*0Sstevel@tonic-gate 			    "%u", v->dtdv_kind);
476*0Sstevel@tonic-gate 		}
477*0Sstevel@tonic-gate 
478*0Sstevel@tonic-gate 		switch (v->dtdv_scope) {
479*0Sstevel@tonic-gate 		case DIFV_SCOPE_GLOBAL:
480*0Sstevel@tonic-gate 			(void) strcpy(scope, "glb");
481*0Sstevel@tonic-gate 			break;
482*0Sstevel@tonic-gate 		case DIFV_SCOPE_THREAD:
483*0Sstevel@tonic-gate 			(void) strcpy(scope, "tls");
484*0Sstevel@tonic-gate 			break;
485*0Sstevel@tonic-gate 		case DIFV_SCOPE_LOCAL:
486*0Sstevel@tonic-gate 			(void) strcpy(scope, "loc");
487*0Sstevel@tonic-gate 			break;
488*0Sstevel@tonic-gate 		default:
489*0Sstevel@tonic-gate 			(void) snprintf(scope, sizeof (scope),
490*0Sstevel@tonic-gate 			    "%u", v->dtdv_scope);
491*0Sstevel@tonic-gate 		}
492*0Sstevel@tonic-gate 
493*0Sstevel@tonic-gate 		if (v->dtdv_flags & ~(DIFV_F_REF | DIFV_F_MOD)) {
494*0Sstevel@tonic-gate 			(void) snprintf(flags, sizeof (flags), "/0x%x",
495*0Sstevel@tonic-gate 			    v->dtdv_flags & ~(DIFV_F_REF | DIFV_F_MOD));
496*0Sstevel@tonic-gate 		}
497*0Sstevel@tonic-gate 
498*0Sstevel@tonic-gate 		if (v->dtdv_flags & DIFV_F_REF)
499*0Sstevel@tonic-gate 			(void) strcat(flags, "/r");
500*0Sstevel@tonic-gate 		if (v->dtdv_flags & DIFV_F_MOD)
501*0Sstevel@tonic-gate 			(void) strcat(flags, "/w");
502*0Sstevel@tonic-gate 
503*0Sstevel@tonic-gate 		(void) fprintf(fp, "%-16s %-4x %-3s %-3s %-4s %s\n",
504*0Sstevel@tonic-gate 		    &dp->dtdo_strtab[v->dtdv_name],
505*0Sstevel@tonic-gate 		    v->dtdv_id, kind, scope, flags + 1,
506*0Sstevel@tonic-gate 		    dt_dis_typestr(&v->dtdv_type, type, sizeof (type)));
507*0Sstevel@tonic-gate 	}
508*0Sstevel@tonic-gate 
509*0Sstevel@tonic-gate 	if (dp->dtdo_krelen != 0)
510*0Sstevel@tonic-gate 		dt_dis_rtab("KREL", dp, fp, dp->dtdo_kreltab, dp->dtdo_krelen);
511*0Sstevel@tonic-gate 
512*0Sstevel@tonic-gate 	if (dp->dtdo_urelen != 0)
513*0Sstevel@tonic-gate 		dt_dis_rtab("UREL", dp, fp, dp->dtdo_ureltab, dp->dtdo_urelen);
514*0Sstevel@tonic-gate }
515