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