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, '/'); 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, '/'); 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 _nm((code != NULL && code->is_nmethod()) ? (nmethod*)code : NULL), 249 _code(code), 250 _strings(), 251 _output(output ? output : tty), 252 _start(NULL), 253 _end(NULL), 254 _print_raw(0), 255 // by default, output pc but not bytes: 256 _print_pc(true), 257 _print_bytes(false), 258 _cur_insn(NULL), 259 _total_ticks(0), 260 _bytes_per_line(Disassembler::pd_instruction_alignment()) 261 { 262 memset(_option_buf, 0, sizeof(_option_buf)); 263 _strings.copy(c); 264 265 // parse the global option string: 266 collect_options(Disassembler::pd_cpu_opts()); 267 collect_options(PrintAssemblyOptions); 268 269 if (strstr(options(), "hsdis-")) { 270 if (strstr(options(), "hsdis-print-raw")) 271 _print_raw = (strstr(options(), "xml") ? 2 : 1); 272 if (strstr(options(), "hsdis-print-pc")) 273 _print_pc = !_print_pc; 274 if (strstr(options(), "hsdis-print-bytes")) 275 _print_bytes = !_print_bytes; 276 } 277 if (strstr(options(), "help")) { 278 tty->print_cr("PrintAssemblyOptions help:"); 279 tty->print_cr(" hsdis-print-raw test plugin by requesting raw output"); 280 tty->print_cr(" hsdis-print-raw-xml test plugin by requesting raw xml"); 281 tty->print_cr(" hsdis-print-pc turn off PC printing (on by default)"); 282 tty->print_cr(" hsdis-print-bytes turn on instruction byte output"); 283 tty->print_cr("combined options: %s", options()); 284 } 285 } 286 287 address decode_env::handle_event(const char* event, address arg) { 288 if (match(event, "insn")) { 289 start_insn(arg); 290 } else if (match(event, "/insn")) { 291 end_insn(arg); 292 } else if (match(event, "addr")) { 293 if (arg != NULL) { 294 print_address(arg); 295 return arg; 296 } 297 } else if (match(event, "mach")) { 298 static char buffer[32] = { 0, }; 299 if (strcmp(buffer, (const char*)arg) != 0 || 300 strlen((const char*)arg) > sizeof(buffer) - 1) { 301 // Only print this when the mach changes 302 strncpy(buffer, (const char*)arg, sizeof(buffer) - 1); 303 buffer[sizeof(buffer) - 1] = '\0'; 304 output()->print_cr("[Disassembling for mach='%s']", arg); 305 } 306 } else if (match(event, "format bytes-per-line")) { 307 _bytes_per_line = (int) (intptr_t) arg; 308 } else { 309 // ignore unrecognized markup 310 } 311 return NULL; 312 } 313 314 // called by the disassembler to print out jump targets and data addresses 315 void decode_env::print_address(address adr) { 316 outputStream* st = _output; 317 318 if (adr == NULL) { 319 st->print("NULL"); 320 return; 321 } 322 323 int small_num = (int)(intptr_t)adr; 324 if ((intptr_t)adr == (intptr_t)small_num 325 && -1 <= small_num && small_num <= 9) { 326 st->print("%d", small_num); 327 return; 328 } 329 330 if (Universe::is_fully_initialized()) { 331 if (StubRoutines::contains(adr)) { 332 StubCodeDesc* desc = StubCodeDesc::desc_for(adr); 333 if (desc == NULL) 334 desc = StubCodeDesc::desc_for(adr + frame::pc_return_offset); 335 if (desc != NULL) { 336 st->print("Stub::%s", desc->name()); 337 if (desc->begin() != adr) 338 st->print("%+d 0x%p",adr - desc->begin(), adr); 339 else if (WizardMode) st->print(" " PTR_FORMAT, adr); 340 return; 341 } 342 st->print("Stub::<unknown> " PTR_FORMAT, adr); 343 return; 344 } 345 346 BarrierSet* bs = Universe::heap()->barrier_set(); 347 if (bs->kind() == BarrierSet::CardTableModRef && 348 adr == (address)((CardTableModRefBS*)(bs))->byte_map_base) { 349 st->print("word_map_base"); 350 if (WizardMode) st->print(" " INTPTR_FORMAT, (intptr_t)adr); 351 return; 352 } 353 354 oop obj; 355 if (_nm != NULL 356 && (obj = _nm->embeddedOop_at(cur_insn())) != NULL 357 && (address) obj == adr 358 && Universe::heap()->is_in(obj) 359 && Universe::heap()->is_in(obj->klass())) { 360 julong c = st->count(); 361 obj->print_value_on(st); 362 if (st->count() == c) { 363 // No output. (Can happen in product builds.) 364 st->print("(a %s)", obj->klass()->external_name()); 365 } 366 return; 367 } 368 } 369 370 // Fall through to a simple (hexadecimal) numeral. 371 st->print(PTR_FORMAT, adr); 372 } 373 374 void decode_env::print_insn_labels() { 375 address p = cur_insn(); 376 outputStream* st = output(); 377 CodeBlob* cb = _code; 378 if (cb != NULL) { 379 cb->print_block_comment(st, p); 380 } 381 _strings.print_block_comment(st, (intptr_t)(p - _start)); 382 if (_print_pc) { 383 st->print(" " PTR_FORMAT ": ", p); 384 } 385 } 386 387 void decode_env::print_insn_bytes(address pc, address pc_limit) { 388 outputStream* st = output(); 389 size_t incr = 1; 390 size_t perline = _bytes_per_line; 391 if ((size_t) Disassembler::pd_instruction_alignment() >= sizeof(int) 392 && !((uintptr_t)pc % sizeof(int)) 393 && !((uintptr_t)pc_limit % sizeof(int))) { 394 incr = sizeof(int); 395 if (perline % incr) perline += incr - (perline % incr); 396 } 397 while (pc < pc_limit) { 398 // tab to the desired column: 399 st->move_to(COMMENT_COLUMN); 400 address pc0 = pc; 401 address pc1 = pc + perline; 402 if (pc1 > pc_limit) pc1 = pc_limit; 403 for (; pc < pc1; pc += incr) { 404 if (pc == pc0) 405 st->print(BYTES_COMMENT); 406 else if ((uint)(pc - pc0) % sizeof(int) == 0) 407 st->print(" "); // put out a space on word boundaries 408 if (incr == sizeof(int)) 409 st->print("%08lx", *(int*)pc); 410 else st->print("%02x", (*pc)&0xFF); 411 } 412 st->cr(); 413 } 414 } 415 416 417 static void* event_to_env(void* env_pv, const char* event, void* arg) { 418 decode_env* env = (decode_env*) env_pv; 419 return env->handle_event(event, (address) arg); 420 } 421 422 ATTRIBUTE_PRINTF(2, 3) 423 static int printf_to_env(void* env_pv, const char* format, ...) { 424 decode_env* env = (decode_env*) env_pv; 425 outputStream* st = env->output(); 426 size_t flen = strlen(format); 427 const char* raw = NULL; 428 if (flen == 0) return 0; 429 if (flen == 1 && format[0] == '\n') { st->bol(); return 1; } 430 if (flen < 2 || 431 strchr(format, '%') == NULL) { 432 raw = format; 433 } else if (format[0] == '%' && format[1] == '%' && 434 strchr(format+2, '%') == NULL) { 435 // happens a lot on machines with names like %foo 436 flen--; 437 raw = format+1; 438 } 439 if (raw != NULL) { 440 st->print_raw(raw, (int) flen); 441 return (int) flen; 442 } 443 va_list ap; 444 va_start(ap, format); 445 julong cnt0 = st->count(); 446 st->vprint(format, ap); 447 julong cnt1 = st->count(); 448 va_end(ap); 449 return (int)(cnt1 - cnt0); 450 } 451 452 address decode_env::decode_instructions(address start, address end) { 453 _start = start; _end = end; 454 455 assert(((((intptr_t)start | (intptr_t)end) % Disassembler::pd_instruction_alignment()) == 0), "misaligned insn addr"); 456 457 const int show_bytes = false; // for disassembler debugging 458 459 //_version = Disassembler::pd_cpu_version(); 460 461 if (!Disassembler::can_decode()) { 462 return NULL; 463 } 464 465 // decode a series of instructions and return the end of the last instruction 466 467 if (_print_raw) { 468 // Print whatever the library wants to print, w/o fancy callbacks. 469 // This is mainly for debugging the library itself. 470 FILE* out = stdout; 471 FILE* xmlout = (_print_raw > 1 ? out : NULL); 472 return use_new_version ? 473 (address) 474 (*Disassembler::_decode_instructions_virtual)((uintptr_t)start, (uintptr_t)end, 475 start, end - start, 476 NULL, (void*) xmlout, 477 NULL, (void*) out, 478 options(), 0/*nice new line*/) 479 : 480 (address) 481 (*Disassembler::_decode_instructions)(start, end, 482 NULL, (void*) xmlout, 483 NULL, (void*) out, 484 options()); 485 } 486 487 return use_new_version ? 488 (address) 489 (*Disassembler::_decode_instructions_virtual)((uintptr_t)start, (uintptr_t)end, 490 start, end - start, 491 &event_to_env, (void*) this, 492 &printf_to_env, (void*) this, 493 options(), 0/*nice new line*/) 494 : 495 (address) 496 (*Disassembler::_decode_instructions)(start, end, 497 &event_to_env, (void*) this, 498 &printf_to_env, (void*) this, 499 options()); 500 } 501 502 503 void Disassembler::decode(CodeBlob* cb, outputStream* st) { 504 if (!load_library()) return; 505 decode_env env(cb, st); 506 env.output()->print_cr("Decoding CodeBlob " PTR_FORMAT, cb); 507 env.decode_instructions(cb->code_begin(), cb->code_end()); 508 } 509 510 void Disassembler::decode(address start, address end, outputStream* st, CodeStrings c) { 511 if (!load_library()) return; 512 decode_env env(CodeCache::find_blob_unsafe(start), st, c); 513 env.decode_instructions(start, end); 514 } 515 516 void Disassembler::decode(nmethod* nm, outputStream* st) { 517 if (!load_library()) return; 518 decode_env env(nm, st); 519 env.output()->print_cr("Decoding compiled method " PTR_FORMAT ":", nm); 520 env.output()->print_cr("Code:"); 521 522 #ifdef SHARK 523 SharkEntry* entry = (SharkEntry *) nm->code_begin(); 524 unsigned char* p = entry->code_start(); 525 unsigned char* end = entry->code_limit(); 526 #else 527 unsigned char* p = nm->code_begin(); 528 unsigned char* end = nm->code_end(); 529 #endif // SHARK 530 531 // If there has been profiling, print the buckets. 532 if (FlatProfiler::bucket_start_for(p) != NULL) { 533 unsigned char* p1 = p; 534 int total_bucket_count = 0; 535 while (p1 < end) { 536 unsigned char* p0 = p1; 537 p1 += pd_instruction_alignment(); 538 address bucket_pc = FlatProfiler::bucket_start_for(p1); 539 if (bucket_pc != NULL && bucket_pc > p0 && bucket_pc <= p1) 540 total_bucket_count += FlatProfiler::bucket_count_for(p0); 541 } 542 env.set_total_ticks(total_bucket_count); 543 } 544 545 // Print constant table. 546 if (nm->consts_size() > 0) { 547 nm->print_nmethod_labels(env.output(), nm->consts_begin()); 548 int offset = 0; 549 for (address p = nm->consts_begin(); p < nm->consts_end(); p += 4, offset += 4) { 550 if ((offset % 8) == 0) { 551 env.output()->print_cr(" " PTR_FORMAT " (offset: %4d): " PTR32_FORMAT " " PTR64_FORMAT, p, offset, *((int32_t*) p), *((int64_t*) p)); 552 } else { 553 env.output()->print_cr(" " PTR_FORMAT " (offset: %4d): " PTR32_FORMAT, p, offset, *((int32_t*) p)); 554 } 555 } 556 } 557 558 env.decode_instructions(p, end); 559 }