1 /* 2 * Copyright (c) 2008, 2019, 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 "asm/assembler.inline.hpp" 27 #include "ci/ciUtilities.hpp" 28 #include "classfile/javaClasses.hpp" 29 #include "code/codeCache.hpp" 30 #include "compiler/disassembler.hpp" 31 #include "gc/shared/cardTable.hpp" 32 #include "gc/shared/cardTableBarrierSet.hpp" 33 #include "gc/shared/collectedHeap.hpp" 34 #include "memory/resourceArea.hpp" 35 #include "memory/universe.hpp" 36 #include "oops/oop.inline.hpp" 37 #include "runtime/handles.inline.hpp" 38 #include "runtime/os.inline.hpp" 39 #include "runtime/stubCodeGenerator.hpp" 40 #include "runtime/stubRoutines.hpp" 41 #include "utilities/resourceHash.hpp" 42 #include CPU_HEADER(depChecker) 43 44 void* Disassembler::_library = NULL; 45 bool Disassembler::_tried_to_load_library = false; 46 bool Disassembler::_library_usable = false; 47 48 // This routine is in the shared library: 49 Disassembler::decode_func_virtual Disassembler::_decode_instructions_virtual = NULL; 50 Disassembler::decode_func Disassembler::_decode_instructions = NULL; 51 52 static const char hsdis_library_name[] = "hsdis-" HOTSPOT_LIB_ARCH; 53 static const char decode_instructions_virtual_name[] = "decode_instructions_virtual"; 54 static const char decode_instructions_name[] = "decode_instructions"; 55 static bool use_new_version = true; 56 #define COMMENT_COLUMN 52 LP64_ONLY(+8) /*could be an option*/ 57 #define BYTES_COMMENT ";..." /* funky byte display comment */ 58 59 class decode_env { 60 private: 61 outputStream* _output; // where the disassembly is directed to 62 CodeBuffer* _codeBuffer; // != NULL only when decoding a CodeBuffer 63 CodeBlob* _codeBlob; // != NULL only when decoding a CodeBlob 64 nmethod* _nm; // != NULL only when decoding a nmethod 65 CodeStrings _strings; 66 address _start; // != NULL when decoding a range of unknown type 67 address _end; // != NULL when decoding a range of unknown type 68 69 char _option_buf[512]; 70 char _print_raw; 71 address _cur_insn; // address of instruction currently being decoded 72 int _bytes_per_line; // arch-specific formatting option 73 int _pre_decode_alignment; 74 int _post_decode_alignment; 75 bool _print_file_name; 76 bool _print_help; 77 bool _helpPrinted; 78 static bool _optionsParsed; 79 80 enum { 81 tabspacing = 8 82 }; 83 84 // Check if the event matches the expected tag 85 // The tag must be a substring of the event, and 86 // the tag must be a token in the event, i.e. separated by delimiters 87 static bool match(const char* event, const char* tag) { 88 size_t eventlen = strlen(event); 89 size_t taglen = strlen(tag); 90 if (eventlen < taglen) // size mismatch 91 return false; 92 if (strncmp(event, tag, taglen) != 0) // string mismatch 93 return false; 94 char delim = event[taglen]; 95 return delim == '\0' || delim == ' ' || delim == '/' || delim == '='; 96 } 97 98 // Merge new option string with previously recorded options 99 void collect_options(const char* p) { 100 if (p == NULL || p[0] == '\0') return; 101 size_t opt_so_far = strlen(_option_buf); 102 if (opt_so_far + 1 + strlen(p) + 1 > sizeof(_option_buf)) return; 103 char* fillp = &_option_buf[opt_so_far]; 104 if (opt_so_far > 0) *fillp++ = ','; 105 strcat(fillp, p); 106 // replace white space by commas: 107 char* q = fillp; 108 while ((q = strpbrk(q, " \t\n")) != NULL) 109 *q++ = ','; 110 } 111 112 void process_options(outputStream* ost); 113 114 void print_insn_labels(); 115 void print_insn_prefix(); 116 void print_address(address value); 117 118 // Properly initializes _start/_end. Overwritten too often if 119 // printing of instructions is called for each instruction. 120 void set_start(address s) { _start = s; } 121 void set_end (address e) { _end = e; } 122 void set_nm (nmethod* nm) { _nm = nm; } 123 void set_output(outputStream* st) { _output = st; } 124 125 #if defined(SUPPORT_ASSEMBLY) || defined(SUPPORT_ABSTRACT_ASSEMBLY) 126 // The disassembler library (sometimes) uses tabs to nicely align the instruction operands. 127 // Depending on the mnemonic length and the column position where the 128 // mnemonic is printed, alignment may turn out to be not so nice. 129 // To improve, we assume 8-character tab spacing and left-align the mnemonic on a tab position. 130 // Instruction comments are aligned 4 tab positions to the right of the mnemonic. 131 void calculate_alignment() { 132 _pre_decode_alignment = ((output()->position()+tabspacing-1)/tabspacing)*tabspacing; 133 _post_decode_alignment = _pre_decode_alignment + 4*tabspacing; 134 } 135 136 void start_insn(address pc) { 137 _cur_insn = pc; 138 output()->bol(); 139 print_insn_labels(); 140 print_insn_prefix(); 141 } 142 143 void end_insn(address pc) { 144 address pc0 = cur_insn(); 145 outputStream* st = output(); 146 147 if (AbstractDisassembler::show_comment()) { 148 if ((_nm != NULL) && _nm->has_code_comment(pc0, pc)) { 149 _nm->print_code_comment_on(st, _post_decode_alignment, pc0, pc); 150 // this calls reloc_string_for which calls oop::print_value_on 151 } 152 print_hook_comments(pc0, _nm != NULL); 153 } 154 Disassembler::annotate(pc0, output()); 155 // follow each complete insn by a nice newline 156 st->bol(); 157 } 158 #endif 159 160 struct SourceFileInfo { 161 struct Link : public CHeapObj<mtCode> { 162 const char* file; 163 int line; 164 Link* next; 165 Link(const char* f, int l) : file(f), line(l), next(NULL) {} 166 }; 167 Link *head, *tail; 168 169 static unsigned hash(const address& a) { 170 return primitive_hash<address>(a); 171 } 172 static bool equals(const address& a0, const address& a1) { 173 return primitive_equals<address>(a0, a1); 174 } 175 void append(const char* file, int line) { 176 if (tail != NULL && tail->file == file && tail->line == line) { 177 // Don't print duplicated lines at the same address. This could happen with C 178 // macros that end up having multiple "__" tokens on the same __LINE__. 179 return; 180 } 181 Link *link = new Link(file, line); 182 if (head == NULL) { 183 head = tail = link; 184 } else { 185 tail->next = link; 186 tail = link; 187 } 188 } 189 SourceFileInfo(const char* file, int line) : head(NULL), tail(NULL) { 190 append(file, line); 191 } 192 }; 193 194 typedef ResourceHashtable< 195 address, SourceFileInfo, 196 SourceFileInfo::hash, 197 SourceFileInfo::equals, 198 15889, // prime number 199 ResourceObj::C_HEAP> SourceFileInfoTable; 200 201 static SourceFileInfoTable _src_table; 202 static const char* _cached_src; 203 static GrowableArray<const char*>* _cached_src_lines; 204 205 public: 206 decode_env(CodeBuffer* code, outputStream* output); 207 decode_env(CodeBlob* code, outputStream* output, CodeStrings c = CodeStrings() /* , ptrdiff_t offset */); 208 decode_env(nmethod* code, outputStream* output, CodeStrings c = CodeStrings()); 209 // Constructor for a 'decode_env' to decode an arbitrary 210 // piece of memory, hopefully containing code. 211 decode_env(address start, address end, outputStream* output); 212 213 // Add 'original_start' argument which is the the original address 214 // the instructions were located at (if this is not equal to 'start'). 215 address decode_instructions(address start, address end, address original_start = NULL); 216 217 address handle_event(const char* event, address arg); 218 219 outputStream* output() { return _output; } 220 address cur_insn() { return _cur_insn; } 221 const char* options() { return _option_buf; } 222 static void hook(const char* file, int line, address pc); 223 void print_hook_comments(address pc, bool newline); 224 }; 225 226 bool decode_env::_optionsParsed = false; 227 228 decode_env::SourceFileInfoTable decode_env::_src_table; 229 const char* decode_env::_cached_src = NULL; 230 GrowableArray<const char*>* decode_env::_cached_src_lines = NULL; 231 232 void decode_env::hook(const char* file, int line, address pc) { 233 // For simplication, we never free from this table. It's really not 234 // necessary as we add to the table only when PrintInterpreter is true, 235 // which means we are debugging the VM and a little bit of extra 236 // memory usage doesn't matter. 237 SourceFileInfo* found = _src_table.get(pc); 238 if (found != NULL) { 239 found->append(file, line); 240 } else { 241 SourceFileInfo sfi(file, line); 242 _src_table.put(pc, sfi); // sfi is copied by value 243 } 244 } 245 246 void decode_env::print_hook_comments(address pc, bool newline) { 247 SourceFileInfo* found = _src_table.get(pc); 248 outputStream* st = output(); 249 if (found != NULL) { 250 for (SourceFileInfo::Link *link = found->head; link; link = link->next) { 251 const char* file = link->file; 252 int line = link->line; 253 if (_cached_src == NULL || strcmp(_cached_src, file) != 0) { 254 FILE* fp; 255 256 // _cached_src_lines is a single cache of the lines of a source file, and we refill this cache 257 // every time we need to print a line from a different source file. It's not the fastest, 258 // but seems bearable. 259 if (_cached_src_lines != NULL) { 260 for (int i=0; i<_cached_src_lines->length(); i++) { 261 os::free((void*)_cached_src_lines->at(i)); 262 } 263 _cached_src_lines->clear(); 264 } else { 265 _cached_src_lines = new (ResourceObj::C_HEAP, mtCode)GrowableArray<const char*>(0, true); 266 } 267 268 if ((fp = fopen(file, "r")) == NULL) { 269 _cached_src = NULL; 270 return; 271 } 272 _cached_src = file; 273 274 char line[500]; // don't write lines that are too long in your source files! 275 while (fgets(line, sizeof(line), fp) != NULL) { 276 size_t len = strlen(line); 277 if (len > 0 && line[len-1] == '\n') { 278 line[len-1] = '\0'; 279 } 280 _cached_src_lines->append(os::strdup(line)); 281 } 282 fclose(fp); 283 _print_file_name = true; 284 } 285 286 if (_print_file_name) { 287 // We print the file name whenever we switch to a new file, or when 288 // Disassembler::decode is called to disassemble a new block of code. 289 _print_file_name = false; 290 if (newline) { 291 st->cr(); 292 } 293 st->move_to(COMMENT_COLUMN); 294 st->print(";;@FILE: %s", file); 295 newline = true; 296 } 297 298 int index = line - 1; // 1-based line number -> 0-based index. 299 if (index >= _cached_src_lines->length()) { 300 // This could happen if source file is mismatched. 301 } else { 302 const char* source_line = _cached_src_lines->at(index); 303 if (newline) { 304 st->cr(); 305 } 306 st->move_to(COMMENT_COLUMN); 307 st->print(";;%5d: %s", line, source_line); 308 newline = true; 309 } 310 } 311 } 312 } 313 314 decode_env::decode_env(CodeBuffer* code, outputStream* output) { 315 memset(this, 0, sizeof(*this)); 316 _output = output ? output : tty; 317 _codeBlob = NULL; 318 _codeBuffer = code; 319 _helpPrinted = false; 320 321 process_options(_output); 322 } 323 324 decode_env::decode_env(CodeBlob* code, outputStream* output, CodeStrings c) { 325 memset(this, 0, sizeof(*this)); // Beware, this zeroes bits of fields. 326 _output = output ? output : tty; 327 _codeBlob = code; 328 _codeBuffer = NULL; 329 _helpPrinted = false; 330 if (_codeBlob != NULL && _codeBlob->is_nmethod()) { 331 _nm = (nmethod*) code; 332 } 333 _strings.copy(c); 334 335 process_options(_output); 336 } 337 338 decode_env::decode_env(nmethod* code, outputStream* output, CodeStrings c) { 339 memset(this, 0, sizeof(*this)); // Beware, this zeroes bits of fields. 340 _output = output ? output : tty; 341 _codeBlob = NULL; 342 _codeBuffer = NULL; 343 _nm = code; 344 _start = _nm->code_begin(); 345 _end = _nm->code_end(); 346 _helpPrinted = false; 347 _strings.copy(c); 348 349 process_options(_output); 350 } 351 352 // Constructor for a 'decode_env' to decode a memory range [start, end) 353 // of unknown origin, assuming it contains code. 354 decode_env::decode_env(address start, address end, outputStream* output) { 355 assert(start < end, "Range must have a positive size, [" PTR_FORMAT ".." PTR_FORMAT ").", p2i(start), p2i(end)); 356 memset(this, 0, sizeof(*this)); 357 _output = output ? output : tty; 358 _codeBlob = NULL; 359 _codeBuffer = NULL; 360 _start = start; 361 _end = end; 362 _helpPrinted = false; 363 364 process_options(_output); 365 } 366 367 void decode_env::process_options(outputStream* ost) { 368 // by default, output pc but not bytes: 369 _print_help = false; 370 _bytes_per_line = Disassembler::pd_instruction_alignment(); 371 _print_file_name = true; 372 373 if (_optionsParsed) return; // parse only once 374 375 // parse the global option string: 376 collect_options(Disassembler::pd_cpu_opts()); 377 collect_options(PrintAssemblyOptions); 378 379 if (strstr(options(), "print-raw")) { 380 _print_raw = (strstr(options(), "xml") ? 2 : 1); 381 } 382 383 if (strstr(options(), "help")) { 384 _print_help = true; 385 } 386 if (strstr(options(), "align-instr")) { 387 AbstractDisassembler::toggle_align_instr(); 388 } 389 if (strstr(options(), "show-pc")) { 390 AbstractDisassembler::toggle_show_pc(); 391 } 392 if (strstr(options(), "show-offset")) { 393 AbstractDisassembler::toggle_show_offset(); 394 } 395 if (strstr(options(), "show-bytes")) { 396 AbstractDisassembler::toggle_show_bytes(); 397 } 398 if (strstr(options(), "show-data-hex")) { 399 AbstractDisassembler::toggle_show_data_hex(); 400 } 401 if (strstr(options(), "show-data-int")) { 402 AbstractDisassembler::toggle_show_data_int(); 403 } 404 if (strstr(options(), "show-data-float")) { 405 AbstractDisassembler::toggle_show_data_float(); 406 } 407 if (strstr(options(), "show-structs")) { 408 AbstractDisassembler::toggle_show_structs(); 409 } 410 if (strstr(options(), "show-comment")) { 411 AbstractDisassembler::toggle_show_comment(); 412 } 413 if (strstr(options(), "show-block-comment")) { 414 AbstractDisassembler::toggle_show_block_comment(); 415 } 416 _optionsParsed = true; 417 418 if (_print_help && ! _helpPrinted) { 419 _helpPrinted = true; 420 ost->print_cr("PrintAssemblyOptions help:"); 421 ost->print_cr(" print-raw test plugin by requesting raw output"); 422 ost->print_cr(" print-raw-xml test plugin by requesting raw xml"); 423 ost->cr(); 424 ost->print_cr(" show-pc toggle printing current pc, currently %s", AbstractDisassembler::show_pc() ? "ON" : "OFF"); 425 ost->print_cr(" show-offset toggle printing current offset, currently %s", AbstractDisassembler::show_offset() ? "ON" : "OFF"); 426 ost->print_cr(" show-bytes toggle printing instruction bytes, currently %s", AbstractDisassembler::show_bytes() ? "ON" : "OFF"); 427 ost->print_cr(" show-data-hex toggle formatting data as hex, currently %s", AbstractDisassembler::show_data_hex() ? "ON" : "OFF"); 428 ost->print_cr(" show-data-int toggle formatting data as int, currently %s", AbstractDisassembler::show_data_int() ? "ON" : "OFF"); 429 ost->print_cr(" show-data-float toggle formatting data as float, currently %s", AbstractDisassembler::show_data_float() ? "ON" : "OFF"); 430 ost->print_cr(" show-structs toggle compiler data structures, currently %s", AbstractDisassembler::show_structs() ? "ON" : "OFF"); 431 ost->print_cr(" show-comment toggle instruction comments, currently %s", AbstractDisassembler::show_comment() ? "ON" : "OFF"); 432 ost->print_cr(" show-block-comment toggle block comments, currently %s", AbstractDisassembler::show_block_comment() ? "ON" : "OFF"); 433 ost->print_cr(" align-instr toggle instruction alignment, currently %s", AbstractDisassembler::align_instr() ? "ON" : "OFF"); 434 ost->print_cr("combined options: %s", options()); 435 } 436 } 437 438 // Disassembly Event Handler. 439 // This method receives events from the disassembler library hsdis 440 // via event_to_env for each decoding step (installed by 441 // Disassembler::decode_instructions(), replacing the default 442 // callback method). This enables dumping additional info 443 // and custom line formatting. 444 // In a future extension, calling a custom decode method will be 445 // supported. We can use such a method to decode instructions the 446 // binutils decoder does not handle to our liking (suboptimal 447 // formatting, incomplete information, ...). 448 // Returns: 449 // - NULL for all standard invocations. The function result is not 450 // examined (as of now, 20190409) by the hsdis decoder loop. 451 // - next for 'insn0' invocations. 452 // next == arg: the custom decoder didn't do anything. 453 // next > arg: the custom decoder did decode the instruction. 454 // next points to the next undecoded instruction 455 // (continuation point for decoder loop). 456 // 457 // "Normal" sequence of events: 458 // insns - start of instruction stream decoding 459 // mach - display architecture 460 // format - display bytes-per-line 461 // for each instruction: 462 // insn - start of instruction decoding 463 // insn0 - custom decoder invocation (if any) 464 // addr - print address value 465 // /insn - end of instruction decoding 466 // /insns - premature end of instruction stream due to no progress 467 // 468 address decode_env::handle_event(const char* event, address arg) { 469 470 #if defined(SUPPORT_ASSEMBLY) || defined(SUPPORT_ABSTRACT_ASSEMBLY) 471 472 //---< Event: end decoding loop (error, no progress) >--- 473 if (decode_env::match(event, "/insns")) { 474 // Nothing to be done here. 475 return NULL; 476 } 477 478 //---< Event: start decoding loop >--- 479 if (decode_env::match(event, "insns")) { 480 // Nothing to be done here. 481 return NULL; 482 } 483 484 //---< Event: finish decoding an instruction >--- 485 if (decode_env::match(event, "/insn")) { 486 output()->fill_to(_post_decode_alignment); 487 end_insn(arg); 488 return NULL; 489 } 490 491 //---< Event: start decoding an instruction >--- 492 if (decode_env::match(event, "insn")) { 493 start_insn(arg); 494 } else if (match(event, "/insn")) { 495 end_insn(arg); 496 } else if (match(event, "addr")) { 497 if (arg != NULL) { 498 print_address(arg); 499 return arg; 500 } 501 calculate_alignment(); 502 output()->fill_to(_pre_decode_alignment); 503 return NULL; 504 } 505 506 //---< Event: call custom decoder (platform specific) >--- 507 if (decode_env::match(event, "insn0")) { 508 return Disassembler::decode_instruction0(arg, output(), arg); 509 } 510 511 //---< Event: Print address >--- 512 if (decode_env::match(event, "addr")) { 513 print_address(arg); 514 return arg; 515 } 516 517 //---< Event: mach (inform about machine architecture) >--- 518 // This event is problematic because it messes up the output. 519 // The event is fired after the instruction address has already 520 // been printed. The decoded instruction (event "insn") is 521 // printed afterwards. That doesn't look nice. 522 if (decode_env::match(event, "mach")) { 523 guarantee(arg != NULL, "event_to_env - arg must not be NULL for event 'mach'"); 524 static char buffer[64] = { 0, }; 525 // Output suppressed because it messes up disassembly. 526 // Only print this when the mach changes. 527 if (false && (strcmp(buffer, (const char*)arg) != 0 || 528 strlen((const char*)arg) > sizeof(buffer) - 1)) { 529 // Only print this when the mach changes 530 strncpy(buffer, (const char*)arg, sizeof(buffer) - 1); 531 buffer[sizeof(buffer) - 1] = '\0'; 532 output()->print_cr("[Disassembling for mach='%s']", (const char*)arg); 533 } 534 return NULL; 535 } 536 537 //---< Event: format bytes-per-line >--- 538 if (decode_env::match(event, "format bytes-per-line")) { 539 _bytes_per_line = (int) (intptr_t) arg; 540 return NULL; 541 } 542 #endif 543 return NULL; 544 } 545 546 static void* event_to_env(void* env_pv, const char* event, void* arg) { 547 decode_env* env = (decode_env*) env_pv; 548 return env->handle_event(event, (address) arg); 549 } 550 551 // called by the disassembler to print out jump targets and data addresses 552 void decode_env::print_address(address adr) { 553 outputStream* st = output(); 554 555 if (adr == NULL) { 556 st->print("NULL"); 557 return; 558 } 559 560 int small_num = (int)(intptr_t)adr; 561 if ((intptr_t)adr == (intptr_t)small_num 562 && -1 <= small_num && small_num <= 9) { 563 st->print("%d", small_num); 564 return; 565 } 566 567 if (Universe::is_fully_initialized()) { 568 if (StubRoutines::contains(adr)) { 569 StubCodeDesc* desc = StubCodeDesc::desc_for(adr); 570 if (desc == NULL) { 571 desc = StubCodeDesc::desc_for(adr + frame::pc_return_offset); 572 } 573 if (desc != NULL) { 574 st->print("Stub::%s", desc->name()); 575 if (desc->begin() != adr) { 576 st->print(INTX_FORMAT_W(+) " " PTR_FORMAT, adr - desc->begin(), p2i(adr)); 577 } else if (WizardMode) { 578 st->print(" " PTR_FORMAT, p2i(adr)); 579 } 580 return; 581 } 582 st->print("Stub::<unknown> " PTR_FORMAT, p2i(adr)); 583 return; 584 } 585 586 BarrierSet* bs = BarrierSet::barrier_set(); 587 if (bs->is_a(BarrierSet::CardTableBarrierSet) && 588 adr == ci_card_table_address_as<address>()) { 589 st->print("word_map_base"); 590 if (WizardMode) st->print(" " INTPTR_FORMAT, p2i(adr)); 591 return; 592 } 593 } 594 595 if (_nm == NULL) { 596 // Don't do this for native methods, as the function name will be printed in 597 // nmethod::reloc_string_for(). 598 // Allocate the buffer on the stack instead of as RESOURCE array. 599 // In case we do DecodeErrorFile, Thread will not be initialized, 600 // causing a "assert(current != __null) failed" failure. 601 const int buflen = 1024; 602 char buf[buflen]; 603 int offset; 604 if (os::dll_address_to_function_name(adr, buf, buflen, &offset)) { 605 st->print(PTR_FORMAT " = %s", p2i(adr), buf); 606 if (offset != 0) { 607 st->print("+%d", offset); 608 } 609 return; 610 } 611 } 612 613 // Fall through to a simple (hexadecimal) numeral. 614 st->print(PTR_FORMAT, p2i(adr)); 615 } 616 617 void decode_env::print_insn_labels() { 618 if (AbstractDisassembler::show_block_comment()) { 619 address p = cur_insn(); 620 outputStream* st = output(); 621 622 //---< Block comments for nmethod >--- 623 // Outputs a bol() before and a cr() after, but only if a comment is printed. 624 // Prints nmethod_section_label as well. 625 if (_nm != NULL) { 626 _nm->print_block_comment(st, p); 627 } 628 if (_codeBlob != NULL) { 629 _codeBlob->print_block_comment(st, p); 630 } 631 if (_codeBuffer != NULL) { 632 _codeBuffer->print_block_comment(st, p); 633 } 634 _strings.print_block_comment(st, (intptr_t)(p - _start)); 635 } 636 } 637 638 void decode_env::print_insn_prefix() { 639 address p = cur_insn(); 640 outputStream* st = output(); 641 AbstractDisassembler::print_location(p, _start, _end, st, false, false); 642 AbstractDisassembler::print_instruction(p, Assembler::instr_len(p), Assembler::instr_maxlen(), st, true, false); 643 } 644 645 ATTRIBUTE_PRINTF(2, 3) 646 static int printf_to_env(void* env_pv, const char* format, ...) { 647 decode_env* env = (decode_env*) env_pv; 648 outputStream* st = env->output(); 649 size_t flen = strlen(format); 650 const char* raw = NULL; 651 if (flen == 0) return 0; 652 if (flen == 1 && format[0] == '\n') { st->bol(); return 1; } 653 if (flen < 2 || 654 strchr(format, '%') == NULL) { 655 raw = format; 656 } else if (format[0] == '%' && format[1] == '%' && 657 strchr(format+2, '%') == NULL) { 658 // happens a lot on machines with names like %foo 659 flen--; 660 raw = format+1; 661 } 662 if (raw != NULL) { 663 st->print_raw(raw, (int) flen); 664 return (int) flen; 665 } 666 va_list ap; 667 va_start(ap, format); 668 julong cnt0 = st->count(); 669 st->vprint(format, ap); 670 julong cnt1 = st->count(); 671 va_end(ap); 672 return (int)(cnt1 - cnt0); 673 } 674 675 // The 'original_start' argument holds the the original address where 676 // the instructions were located in the originating system. If zero (NULL) 677 // is passed in, there is no original address. 678 address decode_env::decode_instructions(address start, address end, address original_start /* = 0*/) { 679 // CodeComment in Stubs. 680 // Properly initialize _start/_end. Overwritten too often if 681 // printing of instructions is called for each instruction. 682 assert((_start == NULL) || (start == NULL) || (_start == start), "don't overwrite CTOR values"); 683 assert((_end == NULL) || (end == NULL) || (_end == end ), "don't overwrite CTOR values"); 684 if (start != NULL) set_start(start); 685 if (end != NULL) set_end(end); 686 if (original_start == NULL) { 687 original_start = start; 688 } 689 690 //---< Check (and correct) alignment >--- 691 // Don't check alignment of end, it is not aligned. 692 if (((uint64_t)start & ((uint64_t)Disassembler::pd_instruction_alignment() - 1)) != 0) { 693 output()->print_cr("Decode range start:" PTR_FORMAT ": ... (unaligned)", p2i(start)); 694 start = (address)((uint64_t)start & ~((uint64_t)Disassembler::pd_instruction_alignment() - 1)); 695 } 696 697 // Trying to decode instructions doesn't make sense if we 698 // couldn't load the disassembler library. 699 if (Disassembler::is_abstract()) { 700 return NULL; 701 } 702 703 // decode a series of instructions and return the end of the last instruction 704 705 if (_print_raw) { 706 // Print whatever the library wants to print, w/o fancy callbacks. 707 // This is mainly for debugging the library itself. 708 FILE* out = stdout; 709 FILE* xmlout = (_print_raw > 1 ? out : NULL); 710 return use_new_version ? 711 (address) 712 (*Disassembler::_decode_instructions_virtual)((uintptr_t)start, (uintptr_t)end, 713 start, end - start, 714 NULL, (void*) xmlout, 715 NULL, (void*) out, 716 options(), 0/*nice new line*/) 717 : 718 (address) 719 (*Disassembler::_decode_instructions)(start, end, 720 NULL, (void*) xmlout, 721 NULL, (void*) out, 722 options()); 723 } 724 725 return use_new_version ? 726 (address) 727 (*Disassembler::_decode_instructions_virtual)((uintptr_t)start, (uintptr_t)end, 728 start, end - start, 729 &event_to_env, (void*) this, 730 &printf_to_env, (void*) this, 731 options(), 0/*nice new line*/) 732 : 733 (address) 734 (*Disassembler::_decode_instructions)(start, end, 735 &event_to_env, (void*) this, 736 &printf_to_env, (void*) this, 737 options()); 738 } 739 740 // ---------------------------------------------------------------------------- 741 // Disassembler 742 // Used as a static wrapper for decode_env. 743 // Each method will create a decode_env before decoding. 744 // You can call the decode_env methods directly if you already have one. 745 746 747 bool Disassembler::load_library(outputStream* st) { 748 // Do not try to load multiple times. Failed once -> fails always. 749 // To force retry in debugger: assign _tried_to_load_library=0 750 if (_tried_to_load_library) { 751 return _library_usable; 752 } 753 754 #if defined(SUPPORT_ASSEMBLY) || defined(SUPPORT_ABSTRACT_ASSEMBLY) 755 // Print to given stream, if any. 756 // Print to tty if Verbose is on and no stream given. 757 st = ((st == NULL) && Verbose) ? tty : st; 758 759 // Compute fully qualified library name. 760 char ebuf[1024]; 761 char buf[JVM_MAXPATHLEN]; 762 os::jvm_path(buf, sizeof(buf)); 763 int jvm_offset = -1; 764 int lib_offset = -1; 765 #ifdef STATIC_BUILD 766 char* p = strrchr(buf, '/'); 767 *p = '\0'; 768 strcat(p, "/lib/"); 769 lib_offset = jvm_offset = strlen(buf); 770 #else 771 { 772 // Match "libjvm" instead of "jvm" on *nix platforms. Creates better matches. 773 // Match "[lib]jvm[^/]*" in jvm_path. 774 const char* base = buf; 775 const char* p = strrchr(buf, *os::file_separator()); 776 #ifdef _WIN32 777 p = strstr(p ? p : base, "jvm"); 778 #else 779 p = strstr(p ? p : base, "libjvm"); 780 #endif 781 if (p != NULL) lib_offset = p - base + 1; 782 if (p != NULL) jvm_offset = p - base; 783 } 784 #endif 785 786 // Find the disassembler shared library. 787 // Search for several paths derived from libjvm, in this order: 788 // 1. <home>/jre/lib/<arch>/<vm>/libhsdis-<arch>.so (for compatibility) 789 // 2. <home>/jre/lib/<arch>/<vm>/hsdis-<arch>.so 790 // 3. <home>/jre/lib/<arch>/hsdis-<arch>.so 791 // 4. hsdis-<arch>.so (using LD_LIBRARY_PATH) 792 if (jvm_offset >= 0) { 793 // 1. <home>/jre/lib/<arch>/<vm>/libhsdis-<arch>.so 794 strcpy(&buf[jvm_offset], hsdis_library_name); 795 strcat(&buf[jvm_offset], os::dll_file_extension()); 796 _library = os::dll_load(buf, ebuf, sizeof ebuf); 797 if (_library == NULL && lib_offset >= 0) { 798 // 2. <home>/jre/lib/<arch>/<vm>/hsdis-<arch>.so 799 strcpy(&buf[lib_offset], hsdis_library_name); 800 strcat(&buf[lib_offset], os::dll_file_extension()); 801 _library = os::dll_load(buf, ebuf, sizeof ebuf); 802 } 803 if (_library == NULL && lib_offset > 0) { 804 // 3. <home>/jre/lib/<arch>/hsdis-<arch>.so 805 buf[lib_offset - 1] = '\0'; 806 const char* p = strrchr(buf, *os::file_separator()); 807 if (p != NULL) { 808 lib_offset = p - buf + 1; 809 strcpy(&buf[lib_offset], hsdis_library_name); 810 strcat(&buf[lib_offset], os::dll_file_extension()); 811 _library = os::dll_load(buf, ebuf, sizeof ebuf); 812 } 813 } 814 } 815 if (_library == NULL) { 816 // 4. hsdis-<arch>.so (using LD_LIBRARY_PATH) 817 strcpy(&buf[0], hsdis_library_name); 818 strcat(&buf[0], os::dll_file_extension()); 819 _library = os::dll_load(buf, ebuf, sizeof ebuf); 820 } 821 822 // load the decoder function to use (new or old version). 823 if (_library != NULL) { 824 _decode_instructions_virtual = CAST_TO_FN_PTR(Disassembler::decode_func_virtual, 825 os::dll_lookup(_library, decode_instructions_virtual_name)); 826 } 827 if (_decode_instructions_virtual == NULL && _library != NULL) { 828 // could not spot in new version, try old version 829 _decode_instructions = CAST_TO_FN_PTR(Disassembler::decode_func, 830 os::dll_lookup(_library, decode_instructions_name)); 831 use_new_version = false; 832 } else { 833 use_new_version = true; 834 } 835 _tried_to_load_library = true; 836 _library_usable = _decode_instructions_virtual != NULL || _decode_instructions != NULL; 837 838 // Create a dummy environment to initialize PrintAssemblyOptions. 839 // The PrintAssemblyOptions must be known for abstract disassemblies as well. 840 decode_env dummy((unsigned char*)(&buf[0]), (unsigned char*)(&buf[1]), st); 841 842 // Report problems during dll_load or dll_lookup, if any. 843 if (st != NULL) { 844 // Success. 845 if (_library_usable) { 846 st->print_cr("Loaded disassembler from %s", buf); 847 } else { 848 st->print_cr("Could not load %s; %s; %s", 849 buf, 850 ((_library != NULL) 851 ? "entry point is missing" 852 : ((WizardMode || PrintMiscellaneous) 853 ? (const char*)ebuf 854 : "library not loadable")), 855 "PrintAssembly defaults to abstract disassembly."); 856 } 857 } 858 #endif 859 return _library_usable; 860 } 861 862 863 // Directly disassemble code buffer. 864 void Disassembler::decode(CodeBuffer* cb, address start, address end, outputStream* st) { 865 #if defined(SUPPORT_ASSEMBLY) || defined(SUPPORT_ABSTRACT_ASSEMBLY) 866 //---< Test memory before decoding >--- 867 if (!(cb->contains(start) && cb->contains(end))) { 868 //---< Allow output suppression, but prevent writing to a NULL stream. Could happen with +PrintStubCode. >--- 869 if (st != NULL) { 870 st->print("Memory range [" PTR_FORMAT ".." PTR_FORMAT "] not contained in CodeBuffer", p2i(start), p2i(end)); 871 } 872 return; 873 } 874 if (!os::is_readable_range(start, end)) { 875 //---< Allow output suppression, but prevent writing to a NULL stream. Could happen with +PrintStubCode. >--- 876 if (st != NULL) { 877 st->print("Memory range [" PTR_FORMAT ".." PTR_FORMAT "] not readable", p2i(start), p2i(end)); 878 } 879 return; 880 } 881 882 decode_env env(cb, st); 883 env.output()->print_cr("--------------------------------------------------------------------------------"); 884 env.output()->print("Decoding CodeBuffer (" PTR_FORMAT ")", p2i(cb)); 885 if (cb->name() != NULL) { 886 env.output()->print(", name: %s,", cb->name()); 887 } 888 env.output()->print_cr(" at [" PTR_FORMAT ", " PTR_FORMAT "] " JLONG_FORMAT " bytes", p2i(start), p2i(end), ((jlong)(end - start))); 889 890 if (is_abstract()) { 891 AbstractDisassembler::decode_abstract(start, end, env.output(), Assembler::instr_maxlen()); 892 } else { 893 env.decode_instructions(start, end); 894 } 895 env.output()->print_cr("--------------------------------------------------------------------------------"); 896 #endif 897 } 898 899 // Directly disassemble code blob. 900 void Disassembler::decode(CodeBlob* cb, outputStream* st, CodeStrings c) { 901 #if defined(SUPPORT_ASSEMBLY) || defined(SUPPORT_ABSTRACT_ASSEMBLY) 902 if (cb->is_nmethod()) { 903 // If we have an nmethod at hand, 904 // call the specialized decoder directly. 905 decode((nmethod*)cb, st, c); 906 return; 907 } 908 909 decode_env env(cb, st); 910 env.output()->print_cr("--------------------------------------------------------------------------------"); 911 if (cb->is_aot()) { 912 env.output()->print("A "); 913 if (cb->is_compiled()) { 914 CompiledMethod* cm = (CompiledMethod*)cb; 915 env.output()->print("%d ",cm->compile_id()); 916 cm->method()->method_holder()->name()->print_symbol_on(env.output()); 917 env.output()->print("."); 918 cm->method()->name()->print_symbol_on(env.output()); 919 cm->method()->signature()->print_symbol_on(env.output()); 920 } else { 921 env.output()->print_cr("%s", cb->name()); 922 } 923 } else { 924 env.output()->print("Decoding CodeBlob"); 925 if (cb->name() != NULL) { 926 env.output()->print(", name: %s,", cb->name()); 927 } 928 } 929 env.output()->print_cr(" at [" PTR_FORMAT ", " PTR_FORMAT "] " JLONG_FORMAT " bytes", p2i(cb->code_begin()), p2i(cb->code_end()), ((jlong)(cb->code_end() - cb->code_begin()))); 930 931 if (is_abstract()) { 932 AbstractDisassembler::decode_abstract(cb->code_begin(), cb->code_end(), env.output(), Assembler::instr_maxlen()); 933 } else { 934 env.decode_instructions(cb->code_begin(), cb->code_end()); 935 } 936 env.output()->print_cr("--------------------------------------------------------------------------------"); 937 #endif 938 } 939 940 // Decode a nmethod. 941 // This includes printing the constant pool and all code segments. 942 // The nmethod data structures (oop maps, relocations and the like) are not printed. 943 void Disassembler::decode(nmethod* nm, outputStream* st, CodeStrings c) { 944 #if defined(SUPPORT_ASSEMBLY) || defined(SUPPORT_ABSTRACT_ASSEMBLY) 945 ttyLocker ttyl; 946 947 decode_env env(nm, st); 948 env.output()->print_cr("--------------------------------------------------------------------------------"); 949 nm->print_constant_pool(env.output()); 950 env.output()->print_cr("--------------------------------------------------------------------------------"); 951 env.output()->cr(); 952 if (is_abstract()) { 953 AbstractDisassembler::decode_abstract(nm->code_begin(), nm->code_end(), env.output(), Assembler::instr_maxlen()); 954 } else { 955 env.decode_instructions(nm->code_begin(), nm->code_end()); 956 } 957 env.output()->print_cr("--------------------------------------------------------------------------------"); 958 #endif 959 } 960 961 // Decode a range, given as [start address, end address) 962 void Disassembler::decode(address start, address end, outputStream* st, CodeStrings c /*, ptrdiff_t offset */) { 963 #if defined(SUPPORT_ASSEMBLY) || defined(SUPPORT_ABSTRACT_ASSEMBLY) 964 //---< Test memory before decoding >--- 965 if (!os::is_readable_range(start, end)) { 966 //---< Allow output suppression, but prevent writing to a NULL stream. Could happen with +PrintStubCode. >--- 967 if (st != NULL) { 968 st->print("Memory range [" PTR_FORMAT ".." PTR_FORMAT "] not readable", p2i(start), p2i(end)); 969 } 970 return; 971 } 972 973 if (is_abstract()) { 974 AbstractDisassembler::decode_abstract(start, end, st, Assembler::instr_maxlen()); 975 return; 976 } 977 978 // Don't do that fancy stuff. If we just have two addresses, live with it 979 // and treat the memory contents as "amorphic" piece of code. 980 #if 0 981 CodeBlob* cb = CodeCache::find_blob_unsafe(start); 982 if (cb != NULL) { 983 // If we have an CodeBlob at hand, 984 // call the specialized decoder directly. 985 decode(cb, st, c); 986 } else 987 #endif 988 { 989 // This seems to be just a chunk of memory. 990 decode_env env(start, end, st); 991 env.output()->print_cr("--------------------------------------------------------------------------------"); 992 env.decode_instructions(start, end); 993 env.output()->print_cr("--------------------------------------------------------------------------------"); 994 } 995 #endif 996 } 997 998 // To prevent excessive code expansion in the interpreter generator, we 999 // do not inline this function into Disassembler::hook(). 1000 void Disassembler::_hook(const char* file, int line, MacroAssembler* masm) { 1001 decode_env::hook(file, line, masm->code_section()->end()); 1002 }