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