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