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