Path: blob/main/cddl/contrib/opensolaris/lib/libdtrace/common/dt_dis.c
39562 views
/*1* CDDL HEADER START2*3* The contents of this file are subject to the terms of the4* Common Development and Distribution License, Version 1.0 only5* (the "License"). You may not use this file except in compliance6* with the License.7*8* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE9* or http://www.opensolaris.org/os/licensing.10* See the License for the specific language governing permissions11* and limitations under the License.12*13* When distributing Covered Code, include this CDDL HEADER in each14* file and include the License file at usr/src/OPENSOLARIS.LICENSE.15* If applicable, add the following below this CDDL HEADER, with the16* fields enclosed by brackets "[]" replaced with your own identifying17* information: Portions Copyright [yyyy] [name of copyright owner]18*19* CDDL HEADER END20*/2122/*23* Copyright 2005 Sun Microsystems, Inc. All rights reserved.24* Use is subject to license terms.25*/2627/*28* Copyright (c) 2013 by Delphix. All rights reserved.29* Copyright (c) 2013 Joyent, Inc. All rights reserved.30*/3132#include <strings.h>33#include <stdio.h>3435#include <dt_impl.h>36#include <dt_ident.h>3738/*ARGSUSED*/39static void40dt_dis_log(const dtrace_difo_t *dp, const char *name, dif_instr_t in, FILE *fp)41{42(void) fprintf(fp, "%-4s %%r%u, %%r%u, %%r%u", name,43DIF_INSTR_R1(in), DIF_INSTR_R2(in), DIF_INSTR_RD(in));44}4546/*ARGSUSED*/47static void48dt_dis_branch(const dtrace_difo_t *dp, const char *name,49dif_instr_t in, FILE *fp)50{51(void) fprintf(fp, "%-4s %u", name, DIF_INSTR_LABEL(in));52}5354/*ARGSUSED*/55static void56dt_dis_load(const dtrace_difo_t *dp, const char *name, dif_instr_t in, FILE *fp)57{58(void) fprintf(fp, "%-4s [%%r%u], %%r%u", name,59DIF_INSTR_R1(in), DIF_INSTR_RD(in));60}6162/*ARGSUSED*/63static void64dt_dis_store(const dtrace_difo_t *dp, const char *name,65dif_instr_t in, FILE *fp)66{67(void) fprintf(fp, "%-4s %%r%u, [%%r%u]", name,68DIF_INSTR_R1(in), DIF_INSTR_RD(in));69}7071/*ARGSUSED*/72static void73dt_dis_str(const dtrace_difo_t *dp, const char *name, dif_instr_t in, FILE *fp)74{75(void) fprintf(fp, "%s", name);76}7778/*ARGSUSED*/79static void80dt_dis_r1rd(const dtrace_difo_t *dp, const char *name, dif_instr_t in, FILE *fp)81{82(void) fprintf(fp, "%-4s %%r%u, %%r%u", name,83DIF_INSTR_R1(in), DIF_INSTR_RD(in));84}8586/*ARGSUSED*/87static void88dt_dis_cmp(const dtrace_difo_t *dp, const char *name, dif_instr_t in, FILE *fp)89{90(void) fprintf(fp, "%-4s %%r%u, %%r%u", name,91DIF_INSTR_R1(in), DIF_INSTR_R2(in));92}9394/*ARGSUSED*/95static void96dt_dis_tst(const dtrace_difo_t *dp, const char *name, dif_instr_t in, FILE *fp)97{98(void) fprintf(fp, "%-4s %%r%u", name, DIF_INSTR_R1(in));99}100101static const char *102dt_dis_varname(const dtrace_difo_t *dp, uint_t id, uint_t scope)103{104const dtrace_difv_t *dvp = dp->dtdo_vartab;105uint_t i;106107for (i = 0; i < dp->dtdo_varlen; i++, dvp++) {108if (dvp->dtdv_id == id && dvp->dtdv_scope == scope) {109if (dvp->dtdv_name < dp->dtdo_strlen)110return (dp->dtdo_strtab + dvp->dtdv_name);111break;112}113}114115return (NULL);116}117118static uint_t119dt_dis_scope(const char *name)120{121switch (name[2]) {122case 'l': return (DIFV_SCOPE_LOCAL);123case 't': return (DIFV_SCOPE_THREAD);124case 'g': return (DIFV_SCOPE_GLOBAL);125default: return (-1u);126}127}128129static void130dt_dis_lda(const dtrace_difo_t *dp, const char *name, dif_instr_t in, FILE *fp)131{132uint_t var = DIF_INSTR_R1(in);133const char *vname;134135(void) fprintf(fp, "%-4s DT_VAR(%u), %%r%u, %%r%u", name,136var, DIF_INSTR_R2(in), DIF_INSTR_RD(in));137138if ((vname = dt_dis_varname(dp, var, dt_dis_scope(name))) != NULL)139(void) fprintf(fp, "\t\t! DT_VAR(%u) = \"%s\"", var, vname);140}141142static void143dt_dis_ldv(const dtrace_difo_t *dp, const char *name, dif_instr_t in, FILE *fp)144{145uint_t var = DIF_INSTR_VAR(in);146const char *vname;147148(void) fprintf(fp, "%-4s DT_VAR(%u), %%r%u",149name, var, DIF_INSTR_RD(in));150151if ((vname = dt_dis_varname(dp, var, dt_dis_scope(name))) != NULL)152(void) fprintf(fp, "\t\t! DT_VAR(%u) = \"%s\"", var, vname);153}154155static void156dt_dis_stv(const dtrace_difo_t *dp, const char *name, dif_instr_t in, FILE *fp)157{158uint_t var = DIF_INSTR_VAR(in);159const char *vname;160161(void) fprintf(fp, "%-4s %%r%u, DT_VAR(%u)",162name, DIF_INSTR_RS(in), var);163164if ((vname = dt_dis_varname(dp, var, dt_dis_scope(name))) != NULL)165(void) fprintf(fp, "\t\t! DT_VAR(%u) = \"%s\"", var, vname);166}167168static void169dt_dis_setx(const dtrace_difo_t *dp, const char *name, dif_instr_t in, FILE *fp)170{171uint_t intptr = DIF_INSTR_INTEGER(in);172173(void) fprintf(fp, "%-4s DT_INTEGER[%u], %%r%u", name,174intptr, DIF_INSTR_RD(in));175176if (intptr < dp->dtdo_intlen) {177(void) fprintf(fp, "\t\t! 0x%llx",178(u_longlong_t)dp->dtdo_inttab[intptr]);179}180}181182static void183dt_dis_sets(const dtrace_difo_t *dp, const char *name, dif_instr_t in, FILE *fp)184{185uint_t strptr = DIF_INSTR_STRING(in);186187(void) fprintf(fp, "%-4s DT_STRING[%u], %%r%u", name,188strptr, DIF_INSTR_RD(in));189190if (strptr < dp->dtdo_strlen)191(void) fprintf(fp, "\t\t! \"%s\"", dp->dtdo_strtab + strptr);192}193194/*ARGSUSED*/195static void196dt_dis_ret(const dtrace_difo_t *dp, const char *name, dif_instr_t in, FILE *fp)197{198(void) fprintf(fp, "%-4s %%r%u", name, DIF_INSTR_RD(in));199}200201/*ARGSUSED*/202static void203dt_dis_call(const dtrace_difo_t *dp, const char *name, dif_instr_t in, FILE *fp)204{205uint_t subr = DIF_INSTR_SUBR(in);206207(void) fprintf(fp, "%-4s DIF_SUBR(%u), %%r%u\t\t! %s",208name, subr, DIF_INSTR_RD(in), dtrace_subrstr(NULL, subr));209}210211/*ARGSUSED*/212static void213dt_dis_pushts(const dtrace_difo_t *dp,214const char *name, dif_instr_t in, FILE *fp)215{216static const char *const tnames[] = { "D type", "string" };217uint_t type = DIF_INSTR_TYPE(in);218const char *pad;219220if (DIF_INSTR_OP(in) == DIF_OP_PUSHTV) {221(void) fprintf(fp, "%-4s DT_TYPE(%u), %%r%u",222name, type, DIF_INSTR_RS(in));223pad = "\t\t";224} else {225(void) fprintf(fp, "%-4s DT_TYPE(%u), %%r%u, %%r%u",226name, type, DIF_INSTR_R2(in), DIF_INSTR_RS(in));227pad = "\t";228}229230if (type < sizeof (tnames) / sizeof (tnames[0])) {231(void) fprintf(fp, "%s! DT_TYPE(%u) = %s", pad,232type, tnames[type]);233}234}235236static void237dt_dis_xlate(const dtrace_difo_t *dp,238const char *name, dif_instr_t in, FILE *fp)239{240uint_t xlr = DIF_INSTR_XLREF(in);241242(void) fprintf(fp, "%-4s DT_XLREF[%u], %%r%u",243name, xlr, DIF_INSTR_RD(in));244245if (xlr < dp->dtdo_xlmlen) {246(void) fprintf(fp, "\t\t! DT_XLREF[%u] = %u.%s", xlr,247(uint_t)dp->dtdo_xlmtab[xlr]->dn_membexpr->dn_xlator->dx_id,248dp->dtdo_xlmtab[xlr]->dn_membname);249}250}251252static char *253dt_dis_typestr(const dtrace_diftype_t *t, char *buf, size_t len)254{255char kind[16], ckind[16];256257switch (t->dtdt_kind) {258case DIF_TYPE_CTF:259(void) strcpy(kind, "D type");260break;261case DIF_TYPE_STRING:262(void) strcpy(kind, "string");263break;264default:265(void) snprintf(kind, sizeof (kind), "0x%x", t->dtdt_kind);266}267268switch (t->dtdt_ckind) {269case CTF_K_UNKNOWN:270(void) strcpy(ckind, "unknown");271break;272case CTF_K_INTEGER:273(void) strcpy(ckind, "integer");274break;275case CTF_K_FLOAT:276(void) strcpy(ckind, "float");277break;278case CTF_K_POINTER:279(void) strcpy(ckind, "pointer");280break;281case CTF_K_ARRAY:282(void) strcpy(ckind, "array");283break;284case CTF_K_FUNCTION:285(void) strcpy(ckind, "function");286break;287case CTF_K_STRUCT:288(void) strcpy(ckind, "struct");289break;290case CTF_K_UNION:291(void) strcpy(ckind, "union");292break;293case CTF_K_ENUM:294(void) strcpy(ckind, "enum");295break;296case CTF_K_FORWARD:297(void) strcpy(ckind, "forward");298break;299case CTF_K_TYPEDEF:300(void) strcpy(ckind, "typedef");301break;302case CTF_K_VOLATILE:303(void) strcpy(ckind, "volatile");304break;305case CTF_K_CONST:306(void) strcpy(ckind, "const");307break;308case CTF_K_RESTRICT:309(void) strcpy(ckind, "restrict");310break;311default:312(void) snprintf(ckind, sizeof (ckind), "0x%x", t->dtdt_ckind);313}314315if (t->dtdt_flags & (DIF_TF_BYREF | DIF_TF_BYUREF)) {316(void) snprintf(buf, len, "%s (%s) by %sref (size %lu)",317kind, ckind, (t->dtdt_flags & DIF_TF_BYUREF) ? "user " : "",318(ulong_t)t->dtdt_size);319} else {320(void) snprintf(buf, len, "%s (%s) (size %lu)",321kind, ckind, (ulong_t)t->dtdt_size);322}323324return (buf);325}326327static void328dt_dis_rtab(const char *rtag, const dtrace_difo_t *dp, FILE *fp,329const dof_relodesc_t *rp, uint32_t len)330{331(void) fprintf(fp, "\n%-4s %-8s %-8s %s\n",332rtag, "OFFSET", "DATA", "NAME");333334for (; len != 0; len--, rp++) {335(void) fprintf(fp, "%-4u %-8llu %-8llu %s\n",336rp->dofr_type, (u_longlong_t)rp->dofr_offset,337(u_longlong_t)rp->dofr_data,338&dp->dtdo_strtab[rp->dofr_name]);339}340}341342void343dt_dis(const dtrace_difo_t *dp, FILE *fp)344{345static const struct opent {346const char *op_name;347void (*op_func)(const dtrace_difo_t *, const char *,348dif_instr_t, FILE *);349} optab[] = {350[0] = { "(illegal opcode)", dt_dis_str },351[DIF_OP_OR] = { "or", dt_dis_log },352[DIF_OP_XOR] = { "xor", dt_dis_log },353[DIF_OP_AND] = { "and", dt_dis_log },354[DIF_OP_SLL] = { "sll", dt_dis_log },355[DIF_OP_SRL] = { "srl", dt_dis_log },356[DIF_OP_SUB] = { "sub", dt_dis_log },357[DIF_OP_ADD] = { "add", dt_dis_log },358[DIF_OP_MUL] = { "mul", dt_dis_log },359[DIF_OP_SDIV] = { "sdiv", dt_dis_log },360[DIF_OP_UDIV] = { "udiv", dt_dis_log },361[DIF_OP_SREM] = { "srem", dt_dis_log },362[DIF_OP_UREM] = { "urem", dt_dis_log },363[DIF_OP_NOT] = { "not", dt_dis_r1rd },364[DIF_OP_MOV] = { "mov", dt_dis_r1rd },365[DIF_OP_CMP] = { "cmp", dt_dis_cmp },366[DIF_OP_TST] = { "tst", dt_dis_tst },367[DIF_OP_BA] = { "ba", dt_dis_branch },368[DIF_OP_BE] = { "be", dt_dis_branch },369[DIF_OP_BNE] = { "bne", dt_dis_branch },370[DIF_OP_BG] = { "bg", dt_dis_branch },371[DIF_OP_BGU] = { "bgu", dt_dis_branch },372[DIF_OP_BGE] = { "bge", dt_dis_branch },373[DIF_OP_BGEU] = { "bgeu", dt_dis_branch },374[DIF_OP_BL] = { "bl", dt_dis_branch },375[DIF_OP_BLU] = { "blu", dt_dis_branch },376[DIF_OP_BLE] = { "ble", dt_dis_branch },377[DIF_OP_BLEU] = { "bleu", dt_dis_branch },378[DIF_OP_LDSB] = { "ldsb", dt_dis_load },379[DIF_OP_LDSH] = { "ldsh", dt_dis_load },380[DIF_OP_LDSW] = { "ldsw", dt_dis_load },381[DIF_OP_LDUB] = { "ldub", dt_dis_load },382[DIF_OP_LDUH] = { "lduh", dt_dis_load },383[DIF_OP_LDUW] = { "lduw", dt_dis_load },384[DIF_OP_LDX] = { "ldx", dt_dis_load },385[DIF_OP_RET] = { "ret", dt_dis_ret },386[DIF_OP_NOP] = { "nop", dt_dis_str },387[DIF_OP_SETX] = { "setx", dt_dis_setx },388[DIF_OP_SETS] = { "sets", dt_dis_sets },389[DIF_OP_SCMP] = { "scmp", dt_dis_cmp },390[DIF_OP_LDGA] = { "ldga", dt_dis_lda },391[DIF_OP_LDGS] = { "ldgs", dt_dis_ldv },392[DIF_OP_STGS] = { "stgs", dt_dis_stv },393[DIF_OP_LDTA] = { "ldta", dt_dis_lda },394[DIF_OP_LDTS] = { "ldts", dt_dis_ldv },395[DIF_OP_STTS] = { "stts", dt_dis_stv },396[DIF_OP_SRA] = { "sra", dt_dis_log },397[DIF_OP_CALL] = { "call", dt_dis_call },398[DIF_OP_PUSHTR] = { "pushtr", dt_dis_pushts },399[DIF_OP_PUSHTV] = { "pushtv", dt_dis_pushts },400[DIF_OP_POPTS] = { "popts", dt_dis_str },401[DIF_OP_FLUSHTS] = { "flushts", dt_dis_str },402[DIF_OP_LDGAA] = { "ldgaa", dt_dis_ldv },403[DIF_OP_LDTAA] = { "ldtaa", dt_dis_ldv },404[DIF_OP_STGAA] = { "stgaa", dt_dis_stv },405[DIF_OP_STTAA] = { "sttaa", dt_dis_stv },406[DIF_OP_LDLS] = { "ldls", dt_dis_ldv },407[DIF_OP_STLS] = { "stls", dt_dis_stv },408[DIF_OP_ALLOCS] = { "allocs", dt_dis_r1rd },409[DIF_OP_COPYS] = { "copys", dt_dis_log },410[DIF_OP_STB] = { "stb", dt_dis_store },411[DIF_OP_STH] = { "sth", dt_dis_store },412[DIF_OP_STW] = { "stw", dt_dis_store },413[DIF_OP_STX] = { "stx", dt_dis_store },414[DIF_OP_ULDSB] = { "uldsb", dt_dis_load },415[DIF_OP_ULDSH] = { "uldsh", dt_dis_load },416[DIF_OP_ULDSW] = { "uldsw", dt_dis_load },417[DIF_OP_ULDUB] = { "uldub", dt_dis_load },418[DIF_OP_ULDUH] = { "ulduh", dt_dis_load },419[DIF_OP_ULDUW] = { "ulduw", dt_dis_load },420[DIF_OP_ULDX] = { "uldx", dt_dis_load },421[DIF_OP_RLDSB] = { "rldsb", dt_dis_load },422[DIF_OP_RLDSH] = { "rldsh", dt_dis_load },423[DIF_OP_RLDSW] = { "rldsw", dt_dis_load },424[DIF_OP_RLDUB] = { "rldub", dt_dis_load },425[DIF_OP_RLDUH] = { "rlduh", dt_dis_load },426[DIF_OP_RLDUW] = { "rlduw", dt_dis_load },427[DIF_OP_RLDX] = { "rldx", dt_dis_load },428[DIF_OP_XLATE] = { "xlate", dt_dis_xlate },429[DIF_OP_XLARG] = { "xlarg", dt_dis_xlate },430};431432const struct opent *op;433ulong_t i = 0;434char type[DT_TYPE_NAMELEN];435436(void) fprintf(fp, "\nDIFO %p returns %s\n", (void *)dp,437dt_dis_typestr(&dp->dtdo_rtype, type, sizeof (type)));438439(void) fprintf(fp, "%-3s %-8s %s\n",440"OFF", "OPCODE", "INSTRUCTION");441442for (i = 0; i < dp->dtdo_len; i++) {443dif_instr_t instr = dp->dtdo_buf[i];444dif_instr_t opcode = DIF_INSTR_OP(instr);445446if (opcode >= sizeof (optab) / sizeof (optab[0]))447opcode = 0; /* force invalid opcode message */448449op = &optab[opcode];450(void) fprintf(fp, "%02lu: %08x ", i, instr);451op->op_func(dp, op->op_name, instr, fp);452(void) fprintf(fp, "\n");453}454455if (dp->dtdo_varlen != 0) {456(void) fprintf(fp, "\n%-16s %-4s %-3s %-3s %-4s %s\n",457"NAME", "ID", "KND", "SCP", "FLAG", "TYPE");458}459460for (i = 0; i < dp->dtdo_varlen; i++) {461dtrace_difv_t *v = &dp->dtdo_vartab[i];462char kind[4], scope[4], flags[16] = { 0 };463464switch (v->dtdv_kind) {465case DIFV_KIND_ARRAY:466(void) strcpy(kind, "arr");467break;468case DIFV_KIND_SCALAR:469(void) strcpy(kind, "scl");470break;471default:472(void) snprintf(kind, sizeof (kind),473"%u", v->dtdv_kind);474}475476switch (v->dtdv_scope) {477case DIFV_SCOPE_GLOBAL:478(void) strcpy(scope, "glb");479break;480case DIFV_SCOPE_THREAD:481(void) strcpy(scope, "tls");482break;483case DIFV_SCOPE_LOCAL:484(void) strcpy(scope, "loc");485break;486default:487(void) snprintf(scope, sizeof (scope),488"%u", v->dtdv_scope);489}490491if (v->dtdv_flags & ~(DIFV_F_REF | DIFV_F_MOD)) {492(void) snprintf(flags, sizeof (flags), "/0x%x",493v->dtdv_flags & ~(DIFV_F_REF | DIFV_F_MOD));494}495496if (v->dtdv_flags & DIFV_F_REF)497(void) strcat(flags, "/r");498if (v->dtdv_flags & DIFV_F_MOD)499(void) strcat(flags, "/w");500501(void) fprintf(fp, "%-16s %-4u %-3s %-3s %-4s %s\n",502&dp->dtdo_strtab[v->dtdv_name],503v->dtdv_id, kind, scope, flags + 1,504dt_dis_typestr(&v->dtdv_type, type, sizeof (type)));505}506507if (dp->dtdo_xlmlen != 0) {508(void) fprintf(fp, "\n%-4s %-3s %-12s %s\n",509"XLID", "ARG", "MEMBER", "TYPE");510}511512for (i = 0; i < dp->dtdo_xlmlen; i++) {513dt_node_t *dnp = dp->dtdo_xlmtab[i];514dt_xlator_t *dxp = dnp->dn_membexpr->dn_xlator;515(void) fprintf(fp, "%-4u %-3d %-12s %s\n",516(uint_t)dxp->dx_id, dxp->dx_arg, dnp->dn_membname,517dt_node_type_name(dnp, type, sizeof (type)));518}519520if (dp->dtdo_krelen != 0)521dt_dis_rtab("KREL", dp, fp, dp->dtdo_kreltab, dp->dtdo_krelen);522523if (dp->dtdo_urelen != 0)524dt_dis_rtab("UREL", dp, fp, dp->dtdo_ureltab, dp->dtdo_urelen);525}526527528