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