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