1 /* 2 * Copyright (c) 2008, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "classfile/javaClasses.hpp" 27 #include "code/codeCache.hpp" 28 #include "compiler/disassembler.hpp" 29 #include "gc/shared/cardTableModRefBS.hpp" 30 #include "gc/shared/collectedHeap.hpp" 31 #include "memory/resourceArea.hpp" 32 #include "oops/oop.inline.hpp" 33 #include "runtime/fprofiler.hpp" 34 #include "runtime/handles.inline.hpp" 35 #include "runtime/os.hpp" 36 #include "runtime/stubCodeGenerator.hpp" 37 #include "runtime/stubRoutines.hpp" 38 #include CPU_HEADER(depChecker) 39 #ifdef SHARK 40 #include "shark/sharkEntry.hpp" 41 #endif 42 43 void* Disassembler::_library = NULL; 44 bool Disassembler::_tried_to_load_library = false; 45 46 // This routine is in the shared library: 47 Disassembler::decode_func_virtual Disassembler::_decode_instructions_virtual = NULL; 48 Disassembler::decode_func Disassembler::_decode_instructions = NULL; 49 50 static const char hsdis_library_name[] = "hsdis-" HOTSPOT_LIB_ARCH; 51 static const char decode_instructions_virtual_name[] = "decode_instructions_virtual"; 52 static const char decode_instructions_name[] = "decode_instructions"; 53 static bool use_new_version = true; 54 #define COMMENT_COLUMN 40 LP64_ONLY(+8) /*could be an option*/ 55 #define BYTES_COMMENT ";..." /* funky byte display comment */ 56 57 bool Disassembler::load_library() { 58 if (_decode_instructions_virtual != NULL || _decode_instructions != NULL) { 59 // Already succeeded. 60 return true; 61 } 62 if (_tried_to_load_library) { 63 // Do not try twice. 64 // To force retry in debugger: assign _tried_to_load_library=0 65 return false; 66 } 67 // Try to load it. 68 char ebuf[1024]; 69 char buf[JVM_MAXPATHLEN]; 70 os::jvm_path(buf, sizeof(buf)); 71 int jvm_offset = -1; 72 int lib_offset = -1; 73 #ifdef STATIC_BUILD 74 char* p = strrchr(buf, '/'); 75 *p = '\0'; 76 strcat(p, "/lib/"); 77 lib_offset = jvm_offset = strlen(buf); 78 #else 79 { 80 // Match "jvm[^/]*" in jvm_path. 81 const char* base = buf; 82 const char* p = strrchr(buf, *os::file_separator()); 83 if (p != NULL) lib_offset = p - base + 1; 84 p = strstr(p ? p : base, "jvm"); 85 if (p != NULL) jvm_offset = p - base; 86 } 87 #endif 88 // Find the disassembler shared library. 89 // Search for several paths derived from libjvm, in this order: 90 // 1. <home>/jre/lib/<arch>/<vm>/libhsdis-<arch>.so (for compatibility) 91 // 2. <home>/jre/lib/<arch>/<vm>/hsdis-<arch>.so 92 // 3. <home>/jre/lib/<arch>/hsdis-<arch>.so 93 // 4. hsdis-<arch>.so (using LD_LIBRARY_PATH) 94 if (jvm_offset >= 0) { 95 // 1. <home>/jre/lib/<arch>/<vm>/libhsdis-<arch>.so 96 strcpy(&buf[jvm_offset], hsdis_library_name); 97 strcat(&buf[jvm_offset], os::dll_file_extension()); 98 _library = os::dll_load(buf, ebuf, sizeof ebuf); 99 if (_library == NULL && lib_offset >= 0) { 100 // 2. <home>/jre/lib/<arch>/<vm>/hsdis-<arch>.so 101 strcpy(&buf[lib_offset], hsdis_library_name); 102 strcat(&buf[lib_offset], os::dll_file_extension()); 103 _library = os::dll_load(buf, ebuf, sizeof ebuf); 104 } 105 if (_library == NULL && lib_offset > 0) { 106 // 3. <home>/jre/lib/<arch>/hsdis-<arch>.so 107 buf[lib_offset - 1] = '\0'; 108 const char* p = strrchr(buf, *os::file_separator()); 109 if (p != NULL) { 110 lib_offset = p - buf + 1; 111 strcpy(&buf[lib_offset], hsdis_library_name); 112 strcat(&buf[lib_offset], os::dll_file_extension()); 113 _library = os::dll_load(buf, ebuf, sizeof ebuf); 114 } 115 } 116 } 117 if (_library == NULL) { 118 // 4. hsdis-<arch>.so (using LD_LIBRARY_PATH) 119 strcpy(&buf[0], hsdis_library_name); 120 strcat(&buf[0], os::dll_file_extension()); 121 _library = os::dll_load(buf, ebuf, sizeof ebuf); 122 } 123 if (_library != NULL) { 124 _decode_instructions_virtual = CAST_TO_FN_PTR(Disassembler::decode_func_virtual, 125 os::dll_lookup(_library, decode_instructions_virtual_name)); 126 } 127 if (_decode_instructions_virtual == NULL) { 128 // could not spot in new version, try old version 129 _decode_instructions = CAST_TO_FN_PTR(Disassembler::decode_func, 130 os::dll_lookup(_library, decode_instructions_name)); 131 use_new_version = false; 132 } else { 133 use_new_version = true; 134 } 135 _tried_to_load_library = true; 136 if (_decode_instructions_virtual == NULL && _decode_instructions == NULL) { 137 tty->print_cr("Could not load %s; %s; %s", buf, 138 ((_library != NULL) 139 ? "entry point is missing" 140 : (WizardMode || PrintMiscellaneous) 141 ? (const char*)ebuf 142 : "library not loadable"), 143 "PrintAssembly is disabled"); 144 return false; 145 } 146 147 // Success. 148 tty->print_cr("Loaded disassembler from %s", buf); 149 return true; 150 } 151 152 153 class decode_env { 154 private: 155 nmethod* _nm; 156 CodeBlob* _code; 157 CodeStrings _strings; 158 outputStream* _output; 159 address _start, _end; 160 161 char _option_buf[512]; 162 char _print_raw; 163 bool _print_pc; 164 bool _print_bytes; 165 address _cur_insn; 166 int _total_ticks; 167 int _bytes_per_line; // arch-specific formatting option 168 169 static bool match(const char* event, const char* tag) { 170 size_t taglen = strlen(tag); 171 if (strncmp(event, tag, taglen) != 0) 172 return false; 173 char delim = event[taglen]; 174 return delim == '\0' || delim == ' ' || delim == '/' || delim == '='; 175 } 176 177 void collect_options(const char* p) { 178 if (p == NULL || p[0] == '\0') return; 179 size_t opt_so_far = strlen(_option_buf); 180 if (opt_so_far + 1 + strlen(p) + 1 > sizeof(_option_buf)) return; 181 char* fillp = &_option_buf[opt_so_far]; 182 if (opt_so_far > 0) *fillp++ = ','; 183 strcat(fillp, p); 184 // replace white space by commas: 185 char* q = fillp; 186 while ((q = strpbrk(q, " \t\n")) != NULL) 187 *q++ = ','; 188 // Note that multiple PrintAssemblyOptions flags accumulate with \n, 189 // which we want to be changed to a comma... 190 } 191 192 void print_insn_labels(); 193 void print_insn_bytes(address pc0, address pc); 194 void print_address(address value); 195 196 public: 197 decode_env(CodeBlob* code, outputStream* output, CodeStrings c = CodeStrings()); 198 199 address decode_instructions(address start, address end); 200 201 void start_insn(address pc) { 202 _cur_insn = pc; 203 output()->bol(); 204 print_insn_labels(); 205 } 206 207 void end_insn(address pc) { 208 address pc0 = cur_insn(); 209 outputStream* st = output(); 210 if (_print_bytes && pc > pc0) 211 print_insn_bytes(pc0, pc); 212 if (_nm != NULL) { 213 _nm->print_code_comment_on(st, COMMENT_COLUMN, pc0, pc); 214 // this calls reloc_string_for which calls oop::print_value_on 215 } 216 217 // Output pc bucket ticks if we have any 218 if (total_ticks() != 0) { 219 address bucket_pc = FlatProfiler::bucket_start_for(pc); 220 if (bucket_pc != NULL && bucket_pc > pc0 && bucket_pc <= pc) { 221 int bucket_count = FlatProfiler::bucket_count_for(pc0); 222 if (bucket_count != 0) { 223 st->bol(); 224 st->print_cr("%3.1f%% [%d]", bucket_count*100.0/total_ticks(), bucket_count); 225 } 226 } 227 } 228 // follow each complete insn by a nice newline 229 st->cr(); 230 } 231 232 address handle_event(const char* event, address arg); 233 234 outputStream* output() { return _output; } 235 address cur_insn() { return _cur_insn; } 236 int total_ticks() { return _total_ticks; } 237 void set_total_ticks(int n) { _total_ticks = n; } 238 const char* options() { return _option_buf; } 239 }; 240 241 decode_env::decode_env(CodeBlob* code, outputStream* output, CodeStrings c) { 242 memset(this, 0, sizeof(*this)); // Beware, this zeroes bits of fields. 243 _output = output ? output : tty; 244 _code = code; 245 if (code != NULL && code->is_nmethod()) 246 _nm = (nmethod*) code; 247 _strings.copy(c); 248 249 // by default, output pc but not bytes: 250 _print_pc = true; 251 _print_bytes = false; 252 _bytes_per_line = Disassembler::pd_instruction_alignment(); 253 254 // parse the global option string: 255 collect_options(Disassembler::pd_cpu_opts()); 256 collect_options(PrintAssemblyOptions); 257 258 if (strstr(options(), "hsdis-")) { 259 if (strstr(options(), "hsdis-print-raw")) 260 _print_raw = (strstr(options(), "xml") ? 2 : 1); 261 if (strstr(options(), "hsdis-print-pc")) 262 _print_pc = !_print_pc; 263 if (strstr(options(), "hsdis-print-bytes")) 264 _print_bytes = !_print_bytes; 265 } 266 if (strstr(options(), "help")) { 267 tty->print_cr("PrintAssemblyOptions help:"); 268 tty->print_cr(" hsdis-print-raw test plugin by requesting raw output"); 269 tty->print_cr(" hsdis-print-raw-xml test plugin by requesting raw xml"); 270 tty->print_cr(" hsdis-print-pc turn off PC printing (on by default)"); 271 tty->print_cr(" hsdis-print-bytes turn on instruction byte output"); 272 tty->print_cr("combined options: %s", options()); 273 } 274 } 275 276 address decode_env::handle_event(const char* event, address arg) { 277 if (match(event, "insn")) { 278 start_insn(arg); 279 } else if (match(event, "/insn")) { 280 end_insn(arg); 281 } else if (match(event, "addr")) { 282 if (arg != NULL) { 283 print_address(arg); 284 return arg; 285 } 286 } else if (match(event, "mach")) { 287 static char buffer[32] = { 0, }; 288 if (strcmp(buffer, (const char*)arg) != 0 || 289 strlen((const char*)arg) > sizeof(buffer) - 1) { 290 // Only print this when the mach changes 291 strncpy(buffer, (const char*)arg, sizeof(buffer) - 1); 292 buffer[sizeof(buffer) - 1] = '\0'; 293 output()->print_cr("[Disassembling for mach='%s']", arg); 294 } 295 } else if (match(event, "format bytes-per-line")) { 296 _bytes_per_line = (int) (intptr_t) arg; 297 } else { 298 // ignore unrecognized markup 299 } 300 return NULL; 301 } 302 303 // called by the disassembler to print out jump targets and data addresses 304 void decode_env::print_address(address adr) { 305 outputStream* st = _output; 306 307 if (adr == NULL) { 308 st->print("NULL"); 309 return; 310 } 311 312 int small_num = (int)(intptr_t)adr; 313 if ((intptr_t)adr == (intptr_t)small_num 314 && -1 <= small_num && small_num <= 9) { 315 st->print("%d", small_num); 316 return; 317 } 318 319 if (Universe::is_fully_initialized()) { 320 if (StubRoutines::contains(adr)) { 321 StubCodeDesc* desc = StubCodeDesc::desc_for(adr); 322 if (desc == NULL) { 323 desc = StubCodeDesc::desc_for(adr + frame::pc_return_offset); 324 } 325 if (desc != NULL) { 326 st->print("Stub::%s", desc->name()); 327 if (desc->begin() != adr) { 328 st->print(INTX_FORMAT_W(+) " " PTR_FORMAT, adr - desc->begin(), p2i(adr)); 329 } else if (WizardMode) { 330 st->print(" " PTR_FORMAT, p2i(adr)); 331 } 332 return; 333 } 334 st->print("Stub::<unknown> " PTR_FORMAT, p2i(adr)); 335 return; 336 } 337 338 BarrierSet* bs = GC::gc()->heap()->barrier_set(); 339 if (bs->is_a(BarrierSet::CardTableModRef) && 340 adr == (address)(barrier_set_cast<CardTableModRefBS>(bs)->byte_map_base)) { 341 st->print("word_map_base"); 342 if (WizardMode) st->print(" " INTPTR_FORMAT, p2i(adr)); 343 return; 344 } 345 } 346 347 if (_nm == NULL) { 348 // Don't do this for native methods, as the function name will be printed in 349 // nmethod::reloc_string_for(). 350 ResourceMark rm; 351 const int buflen = 1024; 352 char* buf = NEW_RESOURCE_ARRAY(char, buflen); 353 int offset; 354 if (os::dll_address_to_function_name(adr, buf, buflen, &offset)) { 355 st->print(PTR_FORMAT " = %s", p2i(adr), buf); 356 if (offset != 0) { 357 st->print("+%d", offset); 358 } 359 return; 360 } 361 } 362 363 // Fall through to a simple (hexadecimal) numeral. 364 st->print(PTR_FORMAT, p2i(adr)); 365 } 366 367 void decode_env::print_insn_labels() { 368 address p = cur_insn(); 369 outputStream* st = output(); 370 CodeBlob* cb = _code; 371 if (cb != NULL) { 372 cb->print_block_comment(st, p); 373 } 374 _strings.print_block_comment(st, (intptr_t)(p - _start)); 375 if (_print_pc) { 376 st->print(" " PTR_FORMAT ": ", p2i(p)); 377 } 378 } 379 380 void decode_env::print_insn_bytes(address pc, address pc_limit) { 381 outputStream* st = output(); 382 size_t incr = 1; 383 size_t perline = _bytes_per_line; 384 if ((size_t) Disassembler::pd_instruction_alignment() >= sizeof(int) 385 && !((uintptr_t)pc % sizeof(int)) 386 && !((uintptr_t)pc_limit % sizeof(int))) { 387 incr = sizeof(int); 388 if (perline % incr) perline += incr - (perline % incr); 389 } 390 while (pc < pc_limit) { 391 // tab to the desired column: 392 st->move_to(COMMENT_COLUMN); 393 address pc0 = pc; 394 address pc1 = pc + perline; 395 if (pc1 > pc_limit) pc1 = pc_limit; 396 for (; pc < pc1; pc += incr) { 397 if (pc == pc0) { 398 st->print(BYTES_COMMENT); 399 } else if ((uint)(pc - pc0) % sizeof(int) == 0) { 400 st->print(" "); // put out a space on word boundaries 401 } 402 if (incr == sizeof(int)) { 403 st->print("%08x", *(int*)pc); 404 } else { 405 st->print("%02x", (*pc)&0xFF); 406 } 407 } 408 st->cr(); 409 } 410 } 411 412 413 static void* event_to_env(void* env_pv, const char* event, void* arg) { 414 decode_env* env = (decode_env*) env_pv; 415 return env->handle_event(event, (address) arg); 416 } 417 418 ATTRIBUTE_PRINTF(2, 3) 419 static int printf_to_env(void* env_pv, const char* format, ...) { 420 decode_env* env = (decode_env*) env_pv; 421 outputStream* st = env->output(); 422 size_t flen = strlen(format); 423 const char* raw = NULL; 424 if (flen == 0) return 0; 425 if (flen == 1 && format[0] == '\n') { st->bol(); return 1; } 426 if (flen < 2 || 427 strchr(format, '%') == NULL) { 428 raw = format; 429 } else if (format[0] == '%' && format[1] == '%' && 430 strchr(format+2, '%') == NULL) { 431 // happens a lot on machines with names like %foo 432 flen--; 433 raw = format+1; 434 } 435 if (raw != NULL) { 436 st->print_raw(raw, (int) flen); 437 return (int) flen; 438 } 439 va_list ap; 440 va_start(ap, format); 441 julong cnt0 = st->count(); 442 st->vprint(format, ap); 443 julong cnt1 = st->count(); 444 va_end(ap); 445 return (int)(cnt1 - cnt0); 446 } 447 448 address decode_env::decode_instructions(address start, address end) { 449 _start = start; _end = end; 450 451 assert(((((intptr_t)start | (intptr_t)end) % Disassembler::pd_instruction_alignment()) == 0), "misaligned insn addr"); 452 453 const int show_bytes = false; // for disassembler debugging 454 455 //_version = Disassembler::pd_cpu_version(); 456 457 if (!Disassembler::can_decode()) { 458 return NULL; 459 } 460 461 // decode a series of instructions and return the end of the last instruction 462 463 if (_print_raw) { 464 // Print whatever the library wants to print, w/o fancy callbacks. 465 // This is mainly for debugging the library itself. 466 FILE* out = stdout; 467 FILE* xmlout = (_print_raw > 1 ? out : NULL); 468 return use_new_version ? 469 (address) 470 (*Disassembler::_decode_instructions_virtual)((uintptr_t)start, (uintptr_t)end, 471 start, end - start, 472 NULL, (void*) xmlout, 473 NULL, (void*) out, 474 options(), 0/*nice new line*/) 475 : 476 (address) 477 (*Disassembler::_decode_instructions)(start, end, 478 NULL, (void*) xmlout, 479 NULL, (void*) out, 480 options()); 481 } 482 483 return use_new_version ? 484 (address) 485 (*Disassembler::_decode_instructions_virtual)((uintptr_t)start, (uintptr_t)end, 486 start, end - start, 487 &event_to_env, (void*) this, 488 &printf_to_env, (void*) this, 489 options(), 0/*nice new line*/) 490 : 491 (address) 492 (*Disassembler::_decode_instructions)(start, end, 493 &event_to_env, (void*) this, 494 &printf_to_env, (void*) this, 495 options()); 496 } 497 498 499 void Disassembler::decode(CodeBlob* cb, outputStream* st) { 500 ttyLocker ttyl; 501 if (!load_library()) return; 502 if (cb->is_nmethod()) { 503 decode((nmethod*)cb, st); 504 return; 505 } 506 decode_env env(cb, st); 507 env.output()->print_cr("----------------------------------------------------------------------"); 508 env.output()->print_cr("%s", cb->name()); 509 env.output()->print_cr(" at [" PTR_FORMAT ", " PTR_FORMAT "] " JLONG_FORMAT " bytes", p2i(cb->code_begin()), p2i(cb->code_end()), ((jlong)(cb->code_end() - cb->code_begin())) * sizeof(unsigned char*)); 510 env.decode_instructions(cb->code_begin(), cb->code_end()); 511 } 512 513 void Disassembler::decode(address start, address end, outputStream* st, CodeStrings c) { 514 ttyLocker ttyl; 515 if (!load_library()) return; 516 decode_env env(CodeCache::find_blob_unsafe(start), st, c); 517 env.decode_instructions(start, end); 518 } 519 520 void Disassembler::decode(nmethod* nm, outputStream* st) { 521 ttyLocker ttyl; 522 if (!load_library()) return; 523 decode_env env(nm, st); 524 env.output()->print_cr("----------------------------------------------------------------------"); 525 526 #ifdef SHARK 527 SharkEntry* entry = (SharkEntry *) nm->code_begin(); 528 unsigned char* p = entry->code_start(); 529 unsigned char* end = entry->code_limit(); 530 #else 531 unsigned char* p = nm->code_begin(); 532 unsigned char* end = nm->code_end(); 533 #endif // SHARK 534 535 nm->method()->method_holder()->name()->print_symbol_on(env.output()); 536 env.output()->print("."); 537 nm->method()->name()->print_symbol_on(env.output()); 538 nm->method()->signature()->print_symbol_on(env.output()); 539 #if INCLUDE_JVMCI 540 { 541 char buffer[O_BUFLEN]; 542 char* jvmciName = nm->jvmci_installed_code_name(buffer, O_BUFLEN); 543 if (jvmciName != NULL) { 544 env.output()->print(" (%s)", jvmciName); 545 } 546 } 547 #endif 548 env.output()->print_cr(" [" PTR_FORMAT ", " PTR_FORMAT "] " JLONG_FORMAT " bytes", p2i(p), p2i(end), ((jlong)(end - p))); 549 550 // If there has been profiling, print the buckets. 551 if (FlatProfiler::bucket_start_for(p) != NULL) { 552 unsigned char* p1 = p; 553 int total_bucket_count = 0; 554 while (p1 < end) { 555 unsigned char* p0 = p1; 556 p1 += pd_instruction_alignment(); 557 address bucket_pc = FlatProfiler::bucket_start_for(p1); 558 if (bucket_pc != NULL && bucket_pc > p0 && bucket_pc <= p1) 559 total_bucket_count += FlatProfiler::bucket_count_for(p0); 560 } 561 env.set_total_ticks(total_bucket_count); 562 } 563 564 // Print constant table. 565 if (nm->consts_size() > 0) { 566 nm->print_nmethod_labels(env.output(), nm->consts_begin()); 567 int offset = 0; 568 for (address p = nm->consts_begin(); p < nm->consts_end(); p += 4, offset += 4) { 569 if ((offset % 8) == 0) { 570 env.output()->print_cr(" " PTR_FORMAT " (offset: %4d): " PTR32_FORMAT " " PTR64_FORMAT, p2i(p), offset, *((int32_t*) p), *((int64_t*) p)); 571 } else { 572 env.output()->print_cr(" " PTR_FORMAT " (offset: %4d): " PTR32_FORMAT, p2i(p), offset, *((int32_t*) p)); 573 } 574 } 575 } 576 577 env.decode_instructions(p, end); 578 }