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