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