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