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