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