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_interface/collectedHeap.hpp" 30 #include "memory/cardTableModRefBS.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 SHARK 53 #include "shark/sharkEntry.hpp" 54 #endif 55 56 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC 57 58 void* Disassembler::_library = NULL; 59 bool Disassembler::_tried_to_load_library = false; 60 61 // This routine is in the shared library: 62 Disassembler::decode_func_virtual Disassembler::_decode_instructions_virtual = NULL; 63 Disassembler::decode_func Disassembler::_decode_instructions = NULL; 64 65 static const char hsdis_library_name[] = "hsdis-"HOTSPOT_LIB_ARCH; 66 static const char decode_instructions_virtual_name[] = "decode_instructions_virtual"; 67 static const char decode_instructions_name[] = "decode_instructions"; 68 static bool use_new_version = true; 69 #define COMMENT_COLUMN 40 LP64_ONLY(+8) /*could be an option*/ 70 #define BYTES_COMMENT ";..." /* funky byte display comment */ 71 72 bool Disassembler::load_library() { 73 if (_decode_instructions_virtual != NULL || _decode_instructions != NULL) { 74 // Already succeeded. 75 return true; 76 } 77 if (_tried_to_load_library) { 78 // Do not try twice. 79 // To force retry in debugger: assign _tried_to_load_library=0 80 return false; 81 } 82 // Try to load it. 83 char ebuf[1024]; 84 char buf[JVM_MAXPATHLEN]; 85 os::jvm_path(buf, sizeof(buf)); 86 int jvm_offset = -1; 87 int lib_offset = -1; 88 { 89 // Match "jvm[^/]*" in jvm_path. 90 const char* base = buf; 91 const char* p = strrchr(buf, *os::file_separator()); 92 if (p != NULL) lib_offset = p - base + 1; 93 p = strstr(p ? p : base, "jvm"); 94 if (p != NULL) jvm_offset = p - base; 95 } 96 // Find the disassembler shared library. 97 // Search for several paths derived from libjvm, in this order: 98 // 1. <home>/jre/lib/<arch>/<vm>/libhsdis-<arch>.so (for compatibility) 99 // 2. <home>/jre/lib/<arch>/<vm>/hsdis-<arch>.so 100 // 3. <home>/jre/lib/<arch>/hsdis-<arch>.so 101 // 4. hsdis-<arch>.so (using LD_LIBRARY_PATH) 102 if (jvm_offset >= 0) { 103 // 1. <home>/jre/lib/<arch>/<vm>/libhsdis-<arch>.so 104 strcpy(&buf[jvm_offset], hsdis_library_name); 105 strcat(&buf[jvm_offset], os::dll_file_extension()); 106 _library = os::dll_load(buf, ebuf, sizeof ebuf); 107 if (_library == NULL) { 108 // 2. <home>/jre/lib/<arch>/<vm>/hsdis-<arch>.so 109 strcpy(&buf[lib_offset], hsdis_library_name); 110 strcat(&buf[lib_offset], os::dll_file_extension()); 111 _library = os::dll_load(buf, ebuf, sizeof ebuf); 112 } 113 if (_library == NULL) { 114 // 3. <home>/jre/lib/<arch>/hsdis-<arch>.so 115 buf[lib_offset - 1] = '\0'; 116 const char* p = strrchr(buf, *os::file_separator()); 117 if (p != NULL) { 118 lib_offset = p - buf + 1; 119 strcpy(&buf[lib_offset], hsdis_library_name); 120 strcat(&buf[lib_offset], os::dll_file_extension()); 121 _library = os::dll_load(buf, ebuf, sizeof ebuf); 122 } 123 } 124 } 125 if (_library == NULL) { 126 // 4. hsdis-<arch>.so (using LD_LIBRARY_PATH) 127 strcpy(&buf[0], hsdis_library_name); 128 strcat(&buf[0], os::dll_file_extension()); 129 _library = os::dll_load(buf, ebuf, sizeof ebuf); 130 } 131 if (_library != NULL) { 132 _decode_instructions_virtual = CAST_TO_FN_PTR(Disassembler::decode_func_virtual, 133 os::dll_lookup(_library, decode_instructions_virtual_name)); 134 } 135 if (_decode_instructions_virtual == NULL) { 136 // could not spot in new version, try old version 137 _decode_instructions = CAST_TO_FN_PTR(Disassembler::decode_func, 138 os::dll_lookup(_library, decode_instructions_name)); 139 use_new_version = false; 140 } else { 141 use_new_version = true; 142 } 143 _tried_to_load_library = true; 144 if (_decode_instructions_virtual == NULL && _decode_instructions == NULL) { 145 tty->print_cr("Could not load %s; %s; %s", buf, 146 ((_library != NULL) 147 ? "entry point is missing" 148 : (WizardMode || PrintMiscellaneous) 149 ? (const char*)ebuf 150 : "library not loadable"), 151 "PrintAssembly is disabled"); 152 return false; 153 } 154 155 // Success. 156 tty->print_cr("Loaded disassembler from %s", buf); 157 return true; 158 } 159 160 161 class decode_env { 162 private: 163 nmethod* _nm; 164 CodeBlob* _code; 165 CodeStrings _strings; 166 outputStream* _output; 167 address _start, _end; 168 169 char _option_buf[512]; 170 char _print_raw; 171 bool _print_pc; 172 bool _print_bytes; 173 address _cur_insn; 174 int _total_ticks; 175 int _bytes_per_line; // arch-specific formatting option 176 177 static bool match(const char* event, const char* tag) { 178 size_t taglen = strlen(tag); 179 if (strncmp(event, tag, taglen) != 0) 180 return false; 181 char delim = event[taglen]; 182 return delim == '\0' || delim == ' ' || delim == '/' || delim == '='; 183 } 184 185 void collect_options(const char* p) { 186 if (p == NULL || p[0] == '\0') return; 187 size_t opt_so_far = strlen(_option_buf); 188 if (opt_so_far + 1 + strlen(p) + 1 > sizeof(_option_buf)) return; 189 char* fillp = &_option_buf[opt_so_far]; 190 if (opt_so_far > 0) *fillp++ = ','; 191 strcat(fillp, p); 192 // replace white space by commas: 193 char* q = fillp; 194 while ((q = strpbrk(q, " \t\n")) != NULL) 195 *q++ = ','; 196 // Note that multiple PrintAssemblyOptions flags accumulate with \n, 197 // which we want to be changed to a comma... 198 } 199 200 void print_insn_labels(); 201 void print_insn_bytes(address pc0, address pc); 202 void print_address(address value); 203 204 public: 205 decode_env(CodeBlob* code, outputStream* output, CodeStrings c = CodeStrings()); 206 207 address decode_instructions(address start, address end); 208 209 void start_insn(address pc) { 210 _cur_insn = pc; 211 output()->bol(); 212 print_insn_labels(); 213 } 214 215 void end_insn(address pc) { 216 address pc0 = cur_insn(); 217 outputStream* st = output(); 218 if (_print_bytes && pc > pc0) 219 print_insn_bytes(pc0, pc); 220 if (_nm != NULL) { 221 _nm->print_code_comment_on(st, COMMENT_COLUMN, pc0, pc); 222 // this calls reloc_string_for which calls oop::print_value_on 223 } 224 225 // Output pc bucket ticks if we have any 226 if (total_ticks() != 0) { 227 address bucket_pc = FlatProfiler::bucket_start_for(pc); 228 if (bucket_pc != NULL && bucket_pc > pc0 && bucket_pc <= pc) { 229 int bucket_count = FlatProfiler::bucket_count_for(pc0); 230 if (bucket_count != 0) { 231 st->bol(); 232 st->print_cr("%3.1f%% [%d]", bucket_count*100.0/total_ticks(), bucket_count); 233 } 234 } 235 } 236 // follow each complete insn by a nice newline 237 st->cr(); 238 } 239 240 address handle_event(const char* event, address arg); 241 242 outputStream* output() { return _output; } 243 address cur_insn() { return _cur_insn; } 244 int total_ticks() { return _total_ticks; } 245 void set_total_ticks(int n) { _total_ticks = n; } 246 const char* options() { return _option_buf; } 247 }; 248 249 decode_env::decode_env(CodeBlob* code, outputStream* output, CodeStrings c) { 250 memset(this, 0, sizeof(*this)); // Beware, this zeroes bits of fields. 251 _output = output ? output : tty; 252 _code = code; 253 if (code != NULL && code->is_nmethod()) 254 _nm = (nmethod*) code; 255 _strings.copy(c); 256 257 // by default, output pc but not bytes: 258 _print_pc = true; 259 _print_bytes = false; 260 _bytes_per_line = Disassembler::pd_instruction_alignment(); 261 262 // parse the global option string: 263 collect_options(Disassembler::pd_cpu_opts()); 264 collect_options(PrintAssemblyOptions); 265 266 if (strstr(options(), "hsdis-")) { 267 if (strstr(options(), "hsdis-print-raw")) 268 _print_raw = (strstr(options(), "xml") ? 2 : 1); 269 if (strstr(options(), "hsdis-print-pc")) 270 _print_pc = !_print_pc; 271 if (strstr(options(), "hsdis-print-bytes")) 272 _print_bytes = !_print_bytes; 273 } 274 if (strstr(options(), "help")) { 275 tty->print_cr("PrintAssemblyOptions help:"); 276 tty->print_cr(" hsdis-print-raw test plugin by requesting raw output"); 277 tty->print_cr(" hsdis-print-raw-xml test plugin by requesting raw xml"); 278 tty->print_cr(" hsdis-print-pc turn off PC printing (on by default)"); 279 tty->print_cr(" hsdis-print-bytes turn on instruction byte output"); 280 tty->print_cr("combined options: %s", options()); 281 } 282 } 283 284 address decode_env::handle_event(const char* event, address arg) { 285 if (match(event, "insn")) { 286 start_insn(arg); 287 } else if (match(event, "/insn")) { 288 end_insn(arg); 289 } else if (match(event, "addr")) { 290 if (arg != NULL) { 291 print_address(arg); 292 return arg; 293 } 294 } else if (match(event, "mach")) { 295 static char buffer[32] = { 0, }; 296 if (strcmp(buffer, (const char*)arg) != 0 || 297 strlen((const char*)arg) > sizeof(buffer) - 1) { 298 // Only print this when the mach changes 299 strncpy(buffer, (const char*)arg, sizeof(buffer) - 1); 300 output()->print_cr("[Disassembling for mach='%s']", arg); 301 } 302 } else if (match(event, "format bytes-per-line")) { 303 _bytes_per_line = (int) (intptr_t) arg; 304 } else { 305 // ignore unrecognized markup 306 } 307 return NULL; 308 } 309 310 // called by the disassembler to print out jump targets and data addresses 311 void decode_env::print_address(address adr) { 312 outputStream* st = _output; 313 314 if (adr == NULL) { 315 st->print("NULL"); 316 return; 317 } 318 319 int small_num = (int)(intptr_t)adr; 320 if ((intptr_t)adr == (intptr_t)small_num 321 && -1 <= small_num && small_num <= 9) { 322 st->print("%d", small_num); 323 return; 324 } 325 326 if (Universe::is_fully_initialized()) { 327 if (StubRoutines::contains(adr)) { 328 StubCodeDesc* desc = StubCodeDesc::desc_for(adr); 329 if (desc == NULL) 330 desc = StubCodeDesc::desc_for(adr + frame::pc_return_offset); 331 if (desc != NULL) { 332 st->print("Stub::%s", desc->name()); 333 if (desc->begin() != adr) 334 st->print("%+d 0x%p",adr - desc->begin(), adr); 335 else if (WizardMode) st->print(" " PTR_FORMAT, adr); 336 return; 337 } 338 st->print("Stub::<unknown> " PTR_FORMAT, adr); 339 return; 340 } 341 342 BarrierSet* bs = Universe::heap()->barrier_set(); 343 if (bs->is_a(BarrierSet::CardTableModRef) && 344 adr == (address)(barrier_set_cast<CardTableModRefBS>(bs)->byte_map_base)) { 345 st->print("word_map_base"); 346 if (WizardMode) st->print(" " INTPTR_FORMAT, (intptr_t)adr); 347 return; 348 } 349 350 oop obj; 351 if (_nm != NULL 352 && (obj = _nm->embeddedOop_at(cur_insn())) != NULL 353 && (address) obj == adr 354 && Universe::heap()->is_in(obj) 355 && Universe::heap()->is_in(obj->klass())) { 356 julong c = st->count(); 357 obj->print_value_on(st); 358 if (st->count() == c) { 359 // No output. (Can happen in product builds.) 360 st->print("(a %s)", obj->klass()->external_name()); 361 } 362 return; 363 } 364 } 365 366 // Fall through to a simple (hexadecimal) numeral. 367 st->print(PTR_FORMAT, adr); 368 } 369 370 void decode_env::print_insn_labels() { 371 address p = cur_insn(); 372 outputStream* st = output(); 373 CodeBlob* cb = _code; 374 if (cb != NULL) { 375 cb->print_block_comment(st, p); 376 } 377 _strings.print_block_comment(st, (intptr_t)(p - _start)); 378 if (_print_pc) { 379 st->print(" " PTR_FORMAT ": ", p); 380 } 381 } 382 383 void decode_env::print_insn_bytes(address pc, address pc_limit) { 384 outputStream* st = output(); 385 size_t incr = 1; 386 size_t perline = _bytes_per_line; 387 if ((size_t) Disassembler::pd_instruction_alignment() >= sizeof(int) 388 && !((uintptr_t)pc % sizeof(int)) 389 && !((uintptr_t)pc_limit % sizeof(int))) { 390 incr = sizeof(int); 391 if (perline % incr) perline += incr - (perline % incr); 392 } 393 while (pc < pc_limit) { 394 // tab to the desired column: 395 st->move_to(COMMENT_COLUMN); 396 address pc0 = pc; 397 address pc1 = pc + perline; 398 if (pc1 > pc_limit) pc1 = pc_limit; 399 for (; pc < pc1; pc += incr) { 400 if (pc == pc0) 401 st->print(BYTES_COMMENT); 402 else if ((uint)(pc - pc0) % sizeof(int) == 0) 403 st->print(" "); // put out a space on word boundaries 404 if (incr == sizeof(int)) 405 st->print("%08lx", *(int*)pc); 406 else st->print("%02x", (*pc)&0xFF); 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 if (!load_library()) return; 501 decode_env env(cb, st); 502 env.output()->print_cr("Decoding CodeBlob " PTR_FORMAT, cb); 503 env.decode_instructions(cb->code_begin(), cb->code_end()); 504 } 505 506 void Disassembler::decode(address start, address end, outputStream* st, CodeStrings c) { 507 if (!load_library()) return; 508 decode_env env(CodeCache::find_blob_unsafe(start), st, c); 509 env.decode_instructions(start, end); 510 } 511 512 void Disassembler::decode(nmethod* nm, outputStream* st) { 513 if (!load_library()) return; 514 decode_env env(nm, st); 515 env.output()->print_cr("Decoding compiled method " PTR_FORMAT ":", nm); 516 env.output()->print_cr("Code:"); 517 518 #ifdef SHARK 519 SharkEntry* entry = (SharkEntry *) nm->code_begin(); 520 unsigned char* p = entry->code_start(); 521 unsigned char* end = entry->code_limit(); 522 #else 523 unsigned char* p = nm->code_begin(); 524 unsigned char* end = nm->code_end(); 525 #endif // SHARK 526 527 // If there has been profiling, print the buckets. 528 if (FlatProfiler::bucket_start_for(p) != NULL) { 529 unsigned char* p1 = p; 530 int total_bucket_count = 0; 531 while (p1 < end) { 532 unsigned char* p0 = p1; 533 p1 += pd_instruction_alignment(); 534 address bucket_pc = FlatProfiler::bucket_start_for(p1); 535 if (bucket_pc != NULL && bucket_pc > p0 && bucket_pc <= p1) 536 total_bucket_count += FlatProfiler::bucket_count_for(p0); 537 } 538 env.set_total_ticks(total_bucket_count); 539 } 540 541 // Print constant table. 542 if (nm->consts_size() > 0) { 543 nm->print_nmethod_labels(env.output(), nm->consts_begin()); 544 int offset = 0; 545 for (address p = nm->consts_begin(); p < nm->consts_end(); p += 4, offset += 4) { 546 if ((offset % 8) == 0) { 547 env.output()->print_cr(" " PTR_FORMAT " (offset: %4d): " PTR32_FORMAT " " PTR64_FORMAT, p, offset, *((int32_t*) p), *((int64_t*) p)); 548 } else { 549 env.output()->print_cr(" " PTR_FORMAT " (offset: %4d): " PTR32_FORMAT, p, offset, *((int32_t*) p)); 550 } 551 } 552 } 553 554 env.decode_instructions(p, end); 555 }