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