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