< prev index next >

src/hotspot/share/compiler/disassembler.cpp

Print this page
rev 54960 : 8213084: Rework and enhance Print[Opto]Assembly output
Reviewed-by: kvn, thartmann


   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/macroAssembler.hpp"
  27 #include "ci/ciUtilities.hpp"
  28 #include "classfile/javaClasses.hpp"
  29 #include "code/codeCache.hpp"
  30 #include "compiler/disassembler.hpp"
  31 #include "gc/shared/cardTable.hpp"
  32 #include "gc/shared/cardTableBarrierSet.hpp"
  33 #include "gc/shared/collectedHeap.hpp"
  34 #include "memory/resourceArea.hpp"
  35 #include "memory/universe.hpp"
  36 #include "oops/oop.inline.hpp"
  37 #include "runtime/handles.inline.hpp"
  38 #include "runtime/os.inline.hpp"
  39 #include "runtime/stubCodeGenerator.hpp"
  40 #include "runtime/stubRoutines.hpp"
  41 #include "utilities/resourceHash.hpp"
  42 #include CPU_HEADER(depChecker)
  43 
  44 void*       Disassembler::_library               = NULL;
  45 bool        Disassembler::_tried_to_load_library = false;

  46 
  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 bool Disassembler::load_library() {
  59   if (_decode_instructions_virtual != NULL || _decode_instructions != NULL) {
  60     // Already succeeded.
  61     return true;
  62   }
  63   if (_tried_to_load_library) {
  64     // Do not try twice.
  65     // To force retry in debugger: assign _tried_to_load_library=0
  66     return false;
  67   }
  68   // Try to load it.
  69   char ebuf[1024];
  70   char buf[JVM_MAXPATHLEN];
  71   os::jvm_path(buf, sizeof(buf));
  72   int jvm_offset = -1;
  73   int lib_offset = -1;
  74 #ifdef STATIC_BUILD
  75   char* p = strrchr(buf, '/');
  76   *p = '\0';
  77   strcat(p, "/lib/");
  78   lib_offset = jvm_offset = strlen(buf);
  79 #else
  80   {
  81     // Match "jvm[^/]*" in jvm_path.
  82     const char* base = buf;
  83     const char* p = strrchr(buf, *os::file_separator());
  84     if (p != NULL) lib_offset = p - base + 1;
  85     p = strstr(p ? p : base, "jvm");
  86     if (p != NULL) jvm_offset = p - base;
  87   }
  88 #endif
  89   // Find the disassembler shared library.
  90   // Search for several paths derived from libjvm, in this order:
  91   // 1. <home>/jre/lib/<arch>/<vm>/libhsdis-<arch>.so  (for compatibility)
  92   // 2. <home>/jre/lib/<arch>/<vm>/hsdis-<arch>.so
  93   // 3. <home>/jre/lib/<arch>/hsdis-<arch>.so
  94   // 4. hsdis-<arch>.so  (using LD_LIBRARY_PATH)
  95   if (jvm_offset >= 0) {
  96     // 1. <home>/jre/lib/<arch>/<vm>/libhsdis-<arch>.so
  97     strcpy(&buf[jvm_offset], hsdis_library_name);
  98     strcat(&buf[jvm_offset], os::dll_file_extension());
  99     _library = os::dll_load(buf, ebuf, sizeof ebuf);
 100     if (_library == NULL && lib_offset >= 0) {
 101       // 2. <home>/jre/lib/<arch>/<vm>/hsdis-<arch>.so
 102       strcpy(&buf[lib_offset], hsdis_library_name);
 103       strcat(&buf[lib_offset], os::dll_file_extension());
 104       _library = os::dll_load(buf, ebuf, sizeof ebuf);
 105     }
 106     if (_library == NULL && lib_offset > 0) {
 107       // 3. <home>/jre/lib/<arch>/hsdis-<arch>.so
 108       buf[lib_offset - 1] = '\0';
 109       const char* p = strrchr(buf, *os::file_separator());
 110       if (p != NULL) {
 111         lib_offset = p - buf + 1;
 112         strcpy(&buf[lib_offset], hsdis_library_name);
 113         strcat(&buf[lib_offset], os::dll_file_extension());
 114         _library = os::dll_load(buf, ebuf, sizeof ebuf);
 115       }
 116     }
 117   }
 118   if (_library == NULL) {
 119     // 4. hsdis-<arch>.so  (using LD_LIBRARY_PATH)
 120     strcpy(&buf[0], hsdis_library_name);
 121     strcat(&buf[0], os::dll_file_extension());
 122     _library = os::dll_load(buf, ebuf, sizeof ebuf);
 123   }
 124   if (_library != NULL) {
 125     _decode_instructions_virtual = CAST_TO_FN_PTR(Disassembler::decode_func_virtual,
 126                                           os::dll_lookup(_library, decode_instructions_virtual_name));
 127   }
 128   if (_decode_instructions_virtual == NULL && _library != NULL) {
 129     // could not spot in new version, try old version
 130     _decode_instructions = CAST_TO_FN_PTR(Disassembler::decode_func,
 131                                           os::dll_lookup(_library, decode_instructions_name));
 132     use_new_version = false;
 133   } else {
 134     use_new_version = true;
 135   }
 136   _tried_to_load_library = true;
 137   if (_decode_instructions_virtual == NULL && _decode_instructions == NULL) {
 138     tty->print_cr("Could not load %s; %s; %s", buf,
 139                   ((_library != NULL)
 140                    ? "entry point is missing"
 141                    : (WizardMode || PrintMiscellaneous)
 142                    ? (const char*)ebuf
 143                    : "library not loadable"),
 144                   "PrintAssembly is disabled");
 145     return false;
 146   }
 147 
 148   // Success.
 149   tty->print_cr("Loaded disassembler from %s", buf);
 150   return true;
 151 }
 152 
 153 
 154 class decode_env {
 155  private:
 156   nmethod*      _nm;
 157   CodeBlob*     _code;


 158   CodeStrings   _strings;
 159   outputStream* _output;
 160   address       _start, _end;
 161   ptrdiff_t     _offset;
 162 
 163   char          _option_buf[512];
 164   char          _print_raw;
 165   bool          _print_pc;
 166   bool          _print_bytes;
 167   address       _cur_insn;
 168   int           _bytes_per_line; // arch-specific formatting option


 169   bool          _print_file_name;







 170 



 171   static bool match(const char* event, const char* tag) {

 172     size_t taglen = strlen(tag);
 173     if (strncmp(event, tag, taglen) != 0)


 174       return false;
 175     char delim = event[taglen];
 176     return delim == '\0' || delim == ' ' || delim == '/' || delim == '=';
 177   }
 178 

 179   void collect_options(const char* p) {
 180     if (p == NULL || p[0] == '\0')  return;
 181     size_t opt_so_far = strlen(_option_buf);
 182     if (opt_so_far + 1 + strlen(p) + 1 > sizeof(_option_buf))  return;
 183     char* fillp = &_option_buf[opt_so_far];
 184     if (opt_so_far > 0) *fillp++ = ',';
 185     strcat(fillp, p);
 186     // replace white space by commas:
 187     char* q = fillp;
 188     while ((q = strpbrk(q, " \t\n")) != NULL)
 189       *q++ = ',';
 190     // Note that multiple PrintAssemblyOptions flags accumulate with \n,
 191     // which we want to be changed to a comma...
 192   }
 193 


 194   void print_insn_labels();
 195   void print_insn_bytes(address pc0, address pc);
 196   void print_address(address value);
 197 










































 198   struct SourceFileInfo {
 199     struct Link : public CHeapObj<mtCode> {
 200       const char* file;
 201       int line;
 202       Link* next;
 203       Link(const char* f, int l) : file(f), line(l), next(NULL) {}
 204     };
 205     Link *head, *tail;
 206 
 207     static unsigned hash(const address& a) {
 208       return primitive_hash<address>(a);
 209     }
 210     static bool equals(const address& a0, const address& a1) {
 211       return primitive_equals<address>(a0, a1);
 212     }
 213     void append(const char* file, int line) {
 214       if (tail != NULL && tail->file == file && tail->line == line) {
 215         // Don't print duplicated lines at the same address. This could happen with C
 216         // macros that end up having multiple "__" tokens on the same __LINE__.
 217         return;


 224         tail = link;
 225       }
 226     }
 227     SourceFileInfo(const char* file, int line) : head(NULL), tail(NULL) {
 228       append(file, line);
 229     }
 230   };
 231 
 232   typedef ResourceHashtable<
 233       address, SourceFileInfo,
 234       SourceFileInfo::hash,
 235       SourceFileInfo::equals,
 236       15889,      // prime number
 237       ResourceObj::C_HEAP> SourceFileInfoTable;
 238 
 239   static SourceFileInfoTable _src_table;
 240   static const char* _cached_src;
 241   static GrowableArray<const char*>* _cached_src_lines;
 242 
 243  public:
 244   decode_env(CodeBlob* code, outputStream* output,
 245              CodeStrings c = CodeStrings(), ptrdiff_t offset = 0);
 246 
 247   address decode_instructions(address start, address end);
 248 
 249   void start_insn(address pc) {
 250     _cur_insn = pc;
 251     output()->bol();
 252     print_insn_labels();
 253   }
 254 
 255   void end_insn(address pc) {
 256     address pc0 = cur_insn();
 257     outputStream* st = output();
 258     if (_print_bytes && pc > pc0)
 259       print_insn_bytes(pc0, pc);
 260     if (_nm != NULL) {
 261       _nm->print_code_comment_on(st, COMMENT_COLUMN, pc0, pc);
 262       // this calls reloc_string_for which calls oop::print_value_on
 263     }
 264     print_hook_comments(pc0, _nm != NULL);
 265     // follow each complete insn by a nice newline
 266     st->cr();
 267   }
 268 
 269   address handle_event(const char* event, address arg);
 270 
 271   outputStream* output() { return _output; }
 272   address cur_insn() { return _cur_insn; }
 273   const char* options() { return _option_buf; }
 274   static void hook(const char* file, int line, address pc);
 275   void print_hook_comments(address pc, bool newline);
 276 };
 277 


 278 decode_env::SourceFileInfoTable decode_env::_src_table;
 279 const char* decode_env::_cached_src = NULL;
 280 GrowableArray<const char*>* decode_env::_cached_src_lines = NULL;
 281 
 282 void decode_env::hook(const char* file, int line, address pc) {
 283   // For simplication, we never free from this table. It's really not
 284   // necessary as we add to the table only when PrintInterpreter is true,
 285   // which means we are debugging the VM and a little bit of extra
 286   // memory usage doesn't matter.
 287   SourceFileInfo* found = _src_table.get(pc);
 288   if (found != NULL) {
 289     found->append(file, line);
 290   } else {
 291     SourceFileInfo sfi(file, line);
 292     _src_table.put(pc, sfi); // sfi is copied by value
 293   }
 294 }
 295 
 296 void decode_env::print_hook_comments(address pc, bool newline) {
 297   SourceFileInfo* found = _src_table.get(pc);


 344         st->print(";;@FILE: %s", file);
 345         newline = true;
 346       }
 347 
 348       int index = line - 1; // 1-based line number -> 0-based index.
 349       if (index >= _cached_src_lines->length()) {
 350         // This could happen if source file is mismatched.
 351       } else {
 352         const char* source_line = _cached_src_lines->at(index);
 353         if (newline) {
 354           st->cr();
 355         }
 356         st->move_to(COMMENT_COLUMN);
 357         st->print(";;%5d: %s", line, source_line);
 358         newline = true;
 359       }
 360     }
 361   }
 362 }
 363 
 364 decode_env::decode_env(CodeBlob* code, outputStream* output, CodeStrings c,
 365                        ptrdiff_t offset) : _nm(NULL),
 366                                            _start(NULL),
 367                                            _end(NULL),
 368                                            _option_buf(),
 369                                            _print_raw('\0'),
 370                                            _cur_insn(NULL) {





 371   _output = output ? output : tty;
 372   _code = code;
 373   if (code != NULL && code->is_nmethod())


 374     _nm = (nmethod*) code;

 375   _strings.copy(c);
 376   _offset = offset;
 377 

































 378   // by default, output pc but not bytes:
 379   _print_pc       = true;
 380   _print_bytes    = false;
 381   _bytes_per_line = Disassembler::pd_instruction_alignment();
 382   _print_file_name= true;


 383 
 384   // parse the global option string:
 385   collect_options(Disassembler::pd_cpu_opts());
 386   collect_options(PrintAssemblyOptions);
 387 
 388   if (strstr(options(), "hsdis-")) {
 389     if (strstr(options(), "hsdis-print-raw"))
 390       _print_raw = (strstr(options(), "xml") ? 2 : 1);
 391     if (strstr(options(), "hsdis-print-pc"))
 392       _print_pc = !_print_pc;
 393     if (strstr(options(), "hsdis-print-bytes"))
 394       _print_bytes = !_print_bytes;
 395   }

 396   if (strstr(options(), "help")) {
 397     tty->print_cr("PrintAssemblyOptions help:");
 398     tty->print_cr("  hsdis-print-raw       test plugin by requesting raw output");
 399     tty->print_cr("  hsdis-print-raw-xml   test plugin by requesting raw xml");
 400     tty->print_cr("  hsdis-print-pc        turn off PC printing (on by default)");
 401     tty->print_cr("  hsdis-print-bytes     turn on instruction byte output");
 402     tty->print_cr("combined options: %s", options());
 403   }
 404 }
 405 
















































































 406 address decode_env::handle_event(const char* event, address arg) {
 407   if (match(event, "insn")) {























 408     start_insn(arg);
 409   } else if (match(event, "/insn")) {
 410     end_insn(arg);
 411   } else if (match(event, "addr")) {
 412     if (arg != NULL) {
 413       print_address(arg);
 414       return arg;
 415     }
 416   } else if (match(event, "mach")) {
 417     static char buffer[32] = { 0, };
 418     if (strcmp(buffer, (const char*)arg) != 0 ||
 419         strlen((const char*)arg) > sizeof(buffer) - 1) {
























 420       // Only print this when the mach changes
 421       strncpy(buffer, (const char*)arg, sizeof(buffer) - 1);
 422       buffer[sizeof(buffer) - 1] = '\0';
 423       output()->print_cr("[Disassembling for mach='%s']", arg);


 424     }
 425   } else if (match(event, "format bytes-per-line")) {


 426     _bytes_per_line = (int) (intptr_t) arg;
 427   } else {
 428     // ignore unrecognized markup
 429   }

 430   return NULL;
 431 }
 432 





 433 // called by the disassembler to print out jump targets and data addresses
 434 void decode_env::print_address(address adr) {
 435   outputStream* st = _output;
 436 
 437   if (adr == NULL) {
 438     st->print("NULL");
 439     return;
 440   }
 441 
 442   int small_num = (int)(intptr_t)adr;
 443   if ((intptr_t)adr == (intptr_t)small_num
 444       && -1 <= small_num && small_num <= 9) {
 445     st->print("%d", small_num);
 446     return;
 447   }
 448 
 449   if (Universe::is_fully_initialized()) {
 450     if (StubRoutines::contains(adr)) {
 451       StubCodeDesc* desc = StubCodeDesc::desc_for(adr);
 452       if (desc == NULL) {
 453         desc = StubCodeDesc::desc_for(adr + frame::pc_return_offset);
 454       }
 455       if (desc != NULL) {


 460           st->print(" " PTR_FORMAT, p2i(adr));
 461         }
 462         return;
 463       }
 464       st->print("Stub::<unknown> " PTR_FORMAT, p2i(adr));
 465       return;
 466     }
 467 
 468     BarrierSet* bs = BarrierSet::barrier_set();
 469     if (bs->is_a(BarrierSet::CardTableBarrierSet) &&
 470         adr == ci_card_table_address_as<address>()) {
 471       st->print("word_map_base");
 472       if (WizardMode) st->print(" " INTPTR_FORMAT, p2i(adr));
 473       return;
 474     }
 475   }
 476 
 477   if (_nm == NULL) {
 478     // Don't do this for native methods, as the function name will be printed in
 479     // nmethod::reloc_string_for().
 480     ResourceMark rm;


 481     const int buflen = 1024;
 482     char* buf = NEW_RESOURCE_ARRAY(char, buflen);
 483     int offset;
 484     if (os::dll_address_to_function_name(adr, buf, buflen, &offset)) {
 485       st->print(PTR_FORMAT " = %s",  p2i(adr), buf);
 486       if (offset != 0) {
 487         st->print("+%d", offset);
 488       }
 489       return;
 490     }
 491   }
 492 
 493   // Fall through to a simple (hexadecimal) numeral.
 494   st->print(PTR_FORMAT, p2i(adr));
 495 }
 496 
 497 void decode_env::print_insn_labels() {

 498   address p = cur_insn();
 499   outputStream* st = output();
 500   CodeBlob* cb = _code;
 501   if (cb != NULL) {
 502     cb->print_block_comment(st, p);
 503   }
 504   _strings.print_block_comment(st, (intptr_t)(p - _start + _offset));
 505   if (_print_pc) {
 506     st->print("  " PTR_FORMAT ": ", p2i(p));
 507   }
 508 }
 509 
 510 void decode_env::print_insn_bytes(address pc, address pc_limit) {
 511   outputStream* st = output();
 512   size_t incr = 1;
 513   size_t perline = _bytes_per_line;
 514   if ((size_t) Disassembler::pd_instruction_alignment() >= sizeof(int)
 515       && !((uintptr_t)pc % sizeof(int))
 516       && !((uintptr_t)pc_limit % sizeof(int))) {
 517     incr = sizeof(int);
 518     if (perline % incr)  perline += incr - (perline % incr);
 519   }
 520   while (pc < pc_limit) {
 521     // tab to the desired column:
 522     st->move_to(COMMENT_COLUMN);
 523     address pc0 = pc;
 524     address pc1 = pc + perline;
 525     if (pc1 > pc_limit)  pc1 = pc_limit;
 526     for (; pc < pc1; pc += incr) {
 527       if (pc == pc0) {
 528         st->print(BYTES_COMMENT);
 529       } else if ((uint)(pc - pc0) % sizeof(int) == 0) {
 530         st->print(" ");         // put out a space on word boundaries
 531       }
 532       if (incr == sizeof(int)) {
 533         st->print("%08x", *(int*)pc);
 534       } else {
 535         st->print("%02x", (*pc)&0xFF);
 536       }


 537     }
 538     st->cr();
 539   }
 540 }
 541 
 542 
 543 static void* event_to_env(void* env_pv, const char* event, void* arg) {
 544   decode_env* env = (decode_env*) env_pv;
 545   return env->handle_event(event, (address) arg);

 546 }
 547 
 548 ATTRIBUTE_PRINTF(2, 3)
 549 static int printf_to_env(void* env_pv, const char* format, ...) {
 550   decode_env* env = (decode_env*) env_pv;
 551   outputStream* st = env->output();
 552   size_t flen = strlen(format);
 553   const char* raw = NULL;
 554   if (flen == 0)  return 0;
 555   if (flen == 1 && format[0] == '\n') { st->bol(); return 1; }
 556   if (flen < 2 ||
 557       strchr(format, '%') == NULL) {
 558     raw = format;
 559   } else if (format[0] == '%' && format[1] == '%' &&
 560              strchr(format+2, '%') == NULL) {
 561     // happens a lot on machines with names like %foo
 562     flen--;
 563     raw = format+1;
 564   }
 565   if (raw != NULL) {
 566     st->print_raw(raw, (int) flen);
 567     return (int) flen;
 568   }
 569   va_list ap;
 570   va_start(ap, format);
 571   julong cnt0 = st->count();
 572   st->vprint(format, ap);
 573   julong cnt1 = st->count();
 574   va_end(ap);
 575   return (int)(cnt1 - cnt0);
 576 }
 577 
 578 address decode_env::decode_instructions(address start, address end) {
 579   _start = start; _end = end;
 580 
 581   assert(((((intptr_t)start | (intptr_t)end) % Disassembler::pd_instruction_alignment()) == 0), "misaligned insn addr");
 582 
 583   const int show_bytes = false; // for disassembler debugging
 584 
 585   //_version = Disassembler::pd_cpu_version();
 586 
 587   if (!Disassembler::can_decode()) {















 588     return NULL;
 589   }
 590 
 591   // decode a series of instructions and return the end of the last instruction
 592 
 593   if (_print_raw) {
 594     // Print whatever the library wants to print, w/o fancy callbacks.
 595     // This is mainly for debugging the library itself.
 596     FILE* out = stdout;
 597     FILE* xmlout = (_print_raw > 1 ? out : NULL);
 598     return use_new_version ?
 599       (address)
 600       (*Disassembler::_decode_instructions_virtual)((uintptr_t)start, (uintptr_t)end,
 601                                                     start, end - start,
 602                                                     NULL, (void*) xmlout,
 603                                                     NULL, (void*) out,
 604                                                     options(), 0/*nice new line*/)
 605       :
 606       (address)
 607       (*Disassembler::_decode_instructions)(start, end,
 608                                             NULL, (void*) xmlout,
 609                                             NULL, (void*) out,
 610                                             options());
 611   }
 612 
 613   return use_new_version ?
 614     (address)
 615     (*Disassembler::_decode_instructions_virtual)((uintptr_t)start, (uintptr_t)end,
 616                                                   start, end - start,
 617                                                   &event_to_env,  (void*) this,
 618                                                   &printf_to_env, (void*) this,
 619                                                   options(), 0/*nice new line*/)
 620     :
 621     (address)
 622     (*Disassembler::_decode_instructions)(start, end,
 623                                           &event_to_env,  (void*) this,
 624                                           &printf_to_env, (void*) this,
 625                                           options());
 626 }
 627 





 628 
 629 void Disassembler::decode(CodeBlob* cb, outputStream* st) {
 630   ttyLocker ttyl;
 631   if (!load_library())  return;

























































































































































 632   if (cb->is_nmethod()) {
 633     decode((nmethod*)cb, st);


 634     return;
 635   }

 636   decode_env env(cb, st);
 637   env.output()->print_cr("----------------------------------------------------------------------");
 638   if (cb->is_aot()) {
 639     env.output()->print("A ");
 640     if (cb->is_compiled()) {
 641       CompiledMethod* cm = (CompiledMethod*)cb;
 642       env.output()->print("%d ",cm->compile_id());
 643       cm->method()->method_holder()->name()->print_symbol_on(env.output());
 644       env.output()->print(".");
 645       cm->method()->name()->print_symbol_on(env.output());
 646       cm->method()->signature()->print_symbol_on(env.output());
 647     } else {
 648       env.output()->print_cr("%s", cb->name());
 649     }
 650   } else {
 651     env.output()->print_cr("%s", cb->name());


 652   }
 653   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())) * sizeof(unsigned char*));





 654   env.decode_instructions(cb->code_begin(), cb->code_end());



 655 }
 656 
 657 void Disassembler::decode(address start, address end, outputStream* st, CodeStrings c,
 658                           ptrdiff_t offset) {



 659   ttyLocker ttyl;
 660   if (!load_library())  return;
 661   decode_env env(CodeCache::find_blob_unsafe(start), st, c, offset);
 662   env.decode_instructions(start, end);
 663 }
 664 
 665 void Disassembler::decode(nmethod* nm, outputStream* st) {
 666   ttyLocker ttyl;
 667   if (!load_library())  return;
 668   decode_env env(nm, st);
 669   env.output()->print_cr("----------------------------------------------------------------------");
 670 
 671   unsigned char* p   = nm->code_begin();
 672   unsigned char* end = nm->code_end();
 673 
 674   nm->method()->method_holder()->name()->print_symbol_on(env.output());
 675   env.output()->print(".");
 676   nm->method()->name()->print_symbol_on(env.output());
 677   nm->method()->signature()->print_symbol_on(env.output());
 678 #if INCLUDE_JVMCI
 679   {
 680     const char* jvmciName = nm->jvmci_name();
 681     if (jvmciName != NULL) {
 682       env.output()->print(" (%s)", jvmciName);
 683     }
 684   }

 685 #endif
 686   env.output()->print_cr("  [" PTR_FORMAT ", " PTR_FORMAT "]  " JLONG_FORMAT " bytes", p2i(p), p2i(end), ((jlong)(end - p)));
 687 
 688   // Print constant table.
 689   if (nm->consts_size() > 0) {
 690     nm->print_nmethod_labels(env.output(), nm->consts_begin());
 691     int offset = 0;
 692     for (address p = nm->consts_begin(); p < nm->consts_end(); p += 4, offset += 4) {
 693       if ((offset % 8) == 0) {
 694         env.output()->print_cr("  " PTR_FORMAT " (offset: %4d): " PTR32_FORMAT "   " PTR64_FORMAT, p2i(p), offset, *((int32_t*) p), *((int64_t*) p));
 695       } else {
 696         env.output()->print_cr("  " PTR_FORMAT " (offset: %4d): " PTR32_FORMAT,                    p2i(p), offset, *((int32_t*) p));
 697       }

 698     }




 699   }
 700 
 701   env.decode_instructions(p, end);

















 702 }
 703 
 704 // To prevent excessive code expansion in the interpreter generator, we
 705 // do not inline this function into Disassembler::hook().
 706 void Disassembler::_hook(const char* file, int line, MacroAssembler* masm) {
 707   decode_env::hook(file, line, masm->code_section()->end());
 708 }


   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "asm/assembler.inline.hpp"
  27 #include "ci/ciUtilities.hpp"
  28 #include "classfile/javaClasses.hpp"
  29 #include "code/codeCache.hpp"
  30 #include "compiler/disassembler.hpp"
  31 #include "gc/shared/cardTable.hpp"
  32 #include "gc/shared/cardTableBarrierSet.hpp"
  33 #include "gc/shared/collectedHeap.hpp"
  34 #include "memory/resourceArea.hpp"
  35 #include "memory/universe.hpp"
  36 #include "oops/oop.inline.hpp"
  37 #include "runtime/handles.inline.hpp"
  38 #include "runtime/os.inline.hpp"
  39 #include "runtime/stubCodeGenerator.hpp"
  40 #include "runtime/stubRoutines.hpp"
  41 #include "utilities/resourceHash.hpp"
  42 #include CPU_HEADER(depChecker)
  43 
  44 void*       Disassembler::_library               = NULL;
  45 bool        Disassembler::_tried_to_load_library = false;
  46 bool        Disassembler::_library_usable        = false;
  47 
  48 // This routine is in the shared library:
  49 Disassembler::decode_func_virtual Disassembler::_decode_instructions_virtual = NULL;
  50 Disassembler::decode_func Disassembler::_decode_instructions = NULL;
  51 
  52 static const char hsdis_library_name[] = "hsdis-" HOTSPOT_LIB_ARCH;
  53 static const char decode_instructions_virtual_name[] = "decode_instructions_virtual";
  54 static const char decode_instructions_name[] = "decode_instructions";
  55 static bool use_new_version = true;
  56 #define COMMENT_COLUMN  52 LP64_ONLY(+8) /*could be an option*/
  57 #define BYTES_COMMENT   ";..."  /* funky byte display comment */
  58 
































































































  59 class decode_env {
  60  private:
  61   outputStream* _output;      // where the disassembly is directed to
  62   CodeBuffer*   _codeBuffer;  // != NULL only when decoding a CodeBuffer
  63   CodeBlob*     _codeBlob;    // != NULL only when decoding a CodeBlob
  64   nmethod*      _nm;          // != NULL only when decoding a nmethod
  65   CodeStrings   _strings;
  66   address       _start;       // != NULL when decoding a range of unknown type
  67   address       _end;         // != NULL when decoding a range of unknown type

  68 
  69   char          _option_buf[512];
  70   char          _print_raw;
  71   address       _cur_insn;        // address of instruction currently being decoded


  72   int           _bytes_per_line;  // arch-specific formatting option
  73   int           _pre_decode_alignment;
  74   int           _post_decode_alignment;
  75   bool          _print_file_name;
  76   bool          _print_help;
  77   bool          _helpPrinted;
  78   static bool   _optionsParsed;
  79 
  80   enum {
  81     tabspacing = 8
  82   };
  83 
  84   // Check if the event matches the expected tag
  85   // The tag must be a substring of the event, and
  86   // the tag must be a token in the event, i.e. separated by delimiters
  87   static bool match(const char* event, const char* tag) {
  88     size_t eventlen = strlen(event);
  89     size_t taglen   = strlen(tag);
  90     if (eventlen < taglen)  // size mismatch
  91       return false;
  92     if (strncmp(event, tag, taglen) != 0)  // string mismatch
  93       return false;
  94     char delim = event[taglen];
  95     return delim == '\0' || delim == ' ' || delim == '/' || delim == '=';
  96   }
  97 
  98   // Merge new option string with previously recorded options
  99   void collect_options(const char* p) {
 100     if (p == NULL || p[0] == '\0')  return;
 101     size_t opt_so_far = strlen(_option_buf);
 102     if (opt_so_far + 1 + strlen(p) + 1 > sizeof(_option_buf))  return;
 103     char* fillp = &_option_buf[opt_so_far];
 104     if (opt_so_far > 0) *fillp++ = ',';
 105     strcat(fillp, p);
 106     // replace white space by commas:
 107     char* q = fillp;
 108     while ((q = strpbrk(q, " \t\n")) != NULL)
 109       *q++ = ',';


 110   }
 111 
 112   void process_options(outputStream* ost);
 113 
 114   void print_insn_labels();
 115   void print_insn_prefix();
 116   void print_address(address value);
 117 
 118   // Properly initializes _start/_end. Overwritten too often if
 119   // printing of instructions is called for each instruction.
 120   void set_start(address s)   { _start = s; }
 121   void set_end  (address e)   { _end = e; }
 122   void set_nm   (nmethod* nm) { _nm = nm; }
 123   void set_output(outputStream* st) { _output = st; }
 124 
 125 #if defined(SUPPORT_ASSEMBLY) || defined(SUPPORT_ABSTRACT_ASSEMBLY)
 126   // The disassembler library (sometimes) uses tabs to nicely align the instruction operands.
 127   // Depending on the mnemonic length and the column position where the
 128   // mnemonic is printed, alignment may turn out to be not so nice.
 129   // To improve, we assume 8-character tab spacing and left-align the mnemonic on a tab position.
 130   // Instruction comments are aligned 4 tab positions to the right of the mnemonic.
 131   void calculate_alignment() {
 132     _pre_decode_alignment  = ((output()->position()+tabspacing-1)/tabspacing)*tabspacing;
 133     _post_decode_alignment = _pre_decode_alignment + 4*tabspacing;
 134   }
 135 
 136   void start_insn(address pc) {
 137     _cur_insn = pc;
 138     output()->bol();
 139     print_insn_labels();
 140     print_insn_prefix();
 141   }
 142 
 143   void end_insn(address pc) {
 144     address pc0 = cur_insn();
 145     outputStream* st = output();
 146 
 147     if (AbstractDisassembler::show_comment()) {
 148       if ((_nm != NULL) && _nm->has_code_comment(pc0, pc)) {
 149         _nm->print_code_comment_on(st, _post_decode_alignment, pc0, pc);
 150         // this calls reloc_string_for which calls oop::print_value_on
 151       }
 152       print_hook_comments(pc0, _nm != NULL);
 153     }
 154     Disassembler::annotate(pc0, output());
 155     // follow each complete insn by a nice newline
 156     st->bol();
 157   }
 158 #endif
 159 
 160   struct SourceFileInfo {
 161     struct Link : public CHeapObj<mtCode> {
 162       const char* file;
 163       int line;
 164       Link* next;
 165       Link(const char* f, int l) : file(f), line(l), next(NULL) {}
 166     };
 167     Link *head, *tail;
 168 
 169     static unsigned hash(const address& a) {
 170       return primitive_hash<address>(a);
 171     }
 172     static bool equals(const address& a0, const address& a1) {
 173       return primitive_equals<address>(a0, a1);
 174     }
 175     void append(const char* file, int line) {
 176       if (tail != NULL && tail->file == file && tail->line == line) {
 177         // Don't print duplicated lines at the same address. This could happen with C
 178         // macros that end up having multiple "__" tokens on the same __LINE__.
 179         return;


 186         tail = link;
 187       }
 188     }
 189     SourceFileInfo(const char* file, int line) : head(NULL), tail(NULL) {
 190       append(file, line);
 191     }
 192   };
 193 
 194   typedef ResourceHashtable<
 195       address, SourceFileInfo,
 196       SourceFileInfo::hash,
 197       SourceFileInfo::equals,
 198       15889,      // prime number
 199       ResourceObj::C_HEAP> SourceFileInfoTable;
 200 
 201   static SourceFileInfoTable _src_table;
 202   static const char* _cached_src;
 203   static GrowableArray<const char*>* _cached_src_lines;
 204 
 205  public:
 206   decode_env(CodeBuffer* code, outputStream* output);
 207   decode_env(CodeBlob*   code, outputStream* output, CodeStrings c = CodeStrings() /* , ptrdiff_t offset */);
 208   decode_env(nmethod*    code, outputStream* output, CodeStrings c = CodeStrings());
 209   // Constructor for a 'decode_env' to decode an arbitrary
 210   // piece of memory, hopefully containing code.
 211   decode_env(address start, address end, outputStream* output);
 212 
 213   // Add 'original_start' argument which is the the original address
 214   // the instructions were located at (if this is not equal to 'start').
 215   address decode_instructions(address start, address end, address original_start = NULL);














 216 
 217   address handle_event(const char* event, address arg);
 218 
 219   outputStream* output()   { return _output; }
 220   address       cur_insn() { return _cur_insn; }
 221   const char*   options()  { return _option_buf; }
 222   static void   hook(const char* file, int line, address pc);
 223   void print_hook_comments(address pc, bool newline);
 224 };
 225 
 226 bool decode_env::_optionsParsed = false;
 227 
 228 decode_env::SourceFileInfoTable decode_env::_src_table;
 229 const char* decode_env::_cached_src = NULL;
 230 GrowableArray<const char*>* decode_env::_cached_src_lines = NULL;
 231 
 232 void decode_env::hook(const char* file, int line, address pc) {
 233   // For simplication, we never free from this table. It's really not
 234   // necessary as we add to the table only when PrintInterpreter is true,
 235   // which means we are debugging the VM and a little bit of extra
 236   // memory usage doesn't matter.
 237   SourceFileInfo* found = _src_table.get(pc);
 238   if (found != NULL) {
 239     found->append(file, line);
 240   } else {
 241     SourceFileInfo sfi(file, line);
 242     _src_table.put(pc, sfi); // sfi is copied by value
 243   }
 244 }
 245 
 246 void decode_env::print_hook_comments(address pc, bool newline) {
 247   SourceFileInfo* found = _src_table.get(pc);


 294         st->print(";;@FILE: %s", file);
 295         newline = true;
 296       }
 297 
 298       int index = line - 1; // 1-based line number -> 0-based index.
 299       if (index >= _cached_src_lines->length()) {
 300         // This could happen if source file is mismatched.
 301       } else {
 302         const char* source_line = _cached_src_lines->at(index);
 303         if (newline) {
 304           st->cr();
 305         }
 306         st->move_to(COMMENT_COLUMN);
 307         st->print(";;%5d: %s", line, source_line);
 308         newline = true;
 309       }
 310     }
 311   }
 312 }
 313 
 314 decode_env::decode_env(CodeBuffer* code, outputStream* output) {
 315   memset(this, 0, sizeof(*this));
 316   _output = output ? output : tty;
 317   _codeBlob    = NULL;
 318   _codeBuffer  = code;
 319   _helpPrinted = false;
 320 
 321   process_options(_output);
 322 }
 323 
 324 decode_env::decode_env(CodeBlob* code, outputStream* output, CodeStrings c) {
 325    memset(this, 0, sizeof(*this)); // Beware, this zeroes bits of fields.
 326    _output = output ? output : tty;
 327   _codeBlob    = code;
 328   _codeBuffer  = NULL;
 329   _helpPrinted = false;
 330   if (_codeBlob != NULL && _codeBlob->is_nmethod()) {
 331     _nm = (nmethod*) code;
 332   }
 333   _strings.copy(c);

 334 
 335   process_options(_output);
 336 }
 337 
 338 decode_env::decode_env(nmethod* code, outputStream* output, CodeStrings c) {
 339   memset(this, 0, sizeof(*this)); // Beware, this zeroes bits of fields.
 340   _output = output ? output : tty;
 341   _codeBlob    = NULL;
 342   _codeBuffer  = NULL;
 343   _nm          = code;
 344   _start       = _nm->code_begin();
 345   _end         = _nm->code_end();
 346   _helpPrinted = false;
 347   _strings.copy(c);
 348 
 349   process_options(_output);
 350 }
 351 
 352 // Constructor for a 'decode_env' to decode a memory range [start, end)
 353 // of unknown origin, assuming it contains code.
 354 decode_env::decode_env(address start, address end, outputStream* output) {
 355   assert(start < end, "Range must have a positive size, [" PTR_FORMAT ".." PTR_FORMAT ").", p2i(start), p2i(end));
 356   memset(this, 0, sizeof(*this));
 357   _output = output ? output : tty;
 358   _codeBlob    = NULL;
 359   _codeBuffer  = NULL;
 360   _start       = start;
 361   _end         = end;
 362   _helpPrinted = false;
 363 
 364   process_options(_output);
 365 }
 366 
 367 void decode_env::process_options(outputStream* ost) {
 368   // by default, output pc but not bytes:
 369   _print_help      = false;

 370   _bytes_per_line  = Disassembler::pd_instruction_alignment();
 371   _print_file_name = true;
 372 
 373   if (_optionsParsed) return;  // parse only once
 374 
 375   // parse the global option string:
 376   collect_options(Disassembler::pd_cpu_opts());
 377   collect_options(PrintAssemblyOptions);
 378 
 379   if (strstr(options(), "print-raw")) {

 380     _print_raw = (strstr(options(), "xml") ? 2 : 1);




 381   }
 382 
 383   if (strstr(options(), "help")) {
 384     _print_help = true;





 385   }
 386   if (strstr(options(), "align-instr")) {
 387     AbstractDisassembler::toggle_align_instr();
 388   }
 389   if (strstr(options(), "show-pc")) {
 390     AbstractDisassembler::toggle_show_pc();
 391   }
 392   if (strstr(options(), "show-offset")) {
 393     AbstractDisassembler::toggle_show_offset();
 394   }
 395   if (strstr(options(), "show-bytes")) {
 396     AbstractDisassembler::toggle_show_bytes();
 397   }
 398   if (strstr(options(), "show-data-hex")) {
 399     AbstractDisassembler::toggle_show_data_hex();
 400   }
 401   if (strstr(options(), "show-data-int")) {
 402     AbstractDisassembler::toggle_show_data_int();
 403   }
 404   if (strstr(options(), "show-data-float")) {
 405     AbstractDisassembler::toggle_show_data_float();
 406   }
 407   if (strstr(options(), "show-structs")) {
 408     AbstractDisassembler::toggle_show_structs();
 409   }
 410   if (strstr(options(), "show-comment")) {
 411     AbstractDisassembler::toggle_show_comment();
 412   }
 413   if (strstr(options(), "show-block-comment")) {
 414     AbstractDisassembler::toggle_show_block_comment();
 415   }
 416   _optionsParsed = true;
 417 
 418   if (_print_help && ! _helpPrinted) {
 419     _helpPrinted = true;
 420     ost->print_cr("PrintAssemblyOptions help:");
 421     ost->print_cr("  print-raw       test plugin by requesting raw output");
 422     ost->print_cr("  print-raw-xml   test plugin by requesting raw xml");
 423     ost->cr();
 424     ost->print_cr("  show-pc            toggle printing current pc,        currently %s", AbstractDisassembler::show_pc()            ? "ON" : "OFF");
 425     ost->print_cr("  show-offset        toggle printing current offset,    currently %s", AbstractDisassembler::show_offset()        ? "ON" : "OFF");
 426     ost->print_cr("  show-bytes         toggle printing instruction bytes, currently %s", AbstractDisassembler::show_bytes()         ? "ON" : "OFF");
 427     ost->print_cr("  show-data-hex      toggle formatting data as hex,     currently %s", AbstractDisassembler::show_data_hex()      ? "ON" : "OFF");
 428     ost->print_cr("  show-data-int      toggle formatting data as int,     currently %s", AbstractDisassembler::show_data_int()      ? "ON" : "OFF");
 429     ost->print_cr("  show-data-float    toggle formatting data as float,   currently %s", AbstractDisassembler::show_data_float()    ? "ON" : "OFF");
 430     ost->print_cr("  show-structs       toggle compiler data structures,   currently %s", AbstractDisassembler::show_structs()       ? "ON" : "OFF");
 431     ost->print_cr("  show-comment       toggle instruction comments,       currently %s", AbstractDisassembler::show_comment()       ? "ON" : "OFF");
 432     ost->print_cr("  show-block-comment toggle block comments,             currently %s", AbstractDisassembler::show_block_comment() ? "ON" : "OFF");
 433     ost->print_cr("  align-instr        toggle instruction alignment,      currently %s", AbstractDisassembler::align_instr()        ? "ON" : "OFF");
 434     ost->print_cr("combined options: %s", options());
 435   }
 436 }
 437 
 438 // Disassembly Event Handler.
 439 // This method receives events from the disassembler library hsdis
 440 // via event_to_env for each decoding step (installed by
 441 // Disassembler::decode_instructions(), replacing the default
 442 // callback method). This enables dumping additional info
 443 // and custom line formatting.
 444 // In a future extension, calling a custom decode method will be
 445 // supported. We can use such a method to decode instructions the
 446 // binutils decoder does not handle to our liking (suboptimal
 447 // formatting, incomplete information, ...).
 448 // Returns:
 449 // - NULL for all standard invocations. The function result is not
 450 //        examined (as of now, 20190409) by the hsdis decoder loop.
 451 // - next for 'insn0' invocations.
 452 //        next == arg: the custom decoder didn't do anything.
 453 //        next >  arg: the custom decoder did decode the instruction.
 454 //                     next points to the next undecoded instruction
 455 //                     (continuation point for decoder loop).
 456 //
 457 // "Normal" sequence of events:
 458 //  insns   - start of instruction stream decoding
 459 //  mach    - display architecture
 460 //  format  - display bytes-per-line
 461 //  for each instruction:
 462 //    insn    - start of instruction decoding
 463 //    insn0   - custom decoder invocation (if any)
 464 //    addr    - print address value
 465 //    /insn   - end of instruction decoding
 466 //  /insns  - premature end of instruction stream due to no progress
 467 //
 468 address decode_env::handle_event(const char* event, address arg) {
 469 
 470 #if defined(SUPPORT_ASSEMBLY) || defined(SUPPORT_ABSTRACT_ASSEMBLY)
 471 
 472   //---<  Event: end decoding loop (error, no progress)  >---
 473   if (decode_env::match(event, "/insns")) {
 474     // Nothing to be done here.
 475     return NULL;
 476   }
 477 
 478   //---<  Event: start decoding loop  >---
 479   if (decode_env::match(event, "insns")) {
 480     // Nothing to be done here.
 481     return NULL;
 482   }
 483 
 484   //---<  Event: finish decoding an instruction  >---
 485   if (decode_env::match(event, "/insn")) {
 486     output()->fill_to(_post_decode_alignment);
 487     end_insn(arg);
 488     return NULL;
 489   }
 490 
 491   //---<  Event: start decoding an instruction  >---
 492   if (decode_env::match(event, "insn")) {
 493     start_insn(arg);
 494   } else if (match(event, "/insn")) {
 495     end_insn(arg);
 496   } else if (match(event, "addr")) {
 497     if (arg != NULL) {
 498       print_address(arg);
 499       return arg;
 500     }
 501     calculate_alignment();
 502     output()->fill_to(_pre_decode_alignment);
 503     return NULL;
 504   }
 505 
 506   //---<  Event: call custom decoder (platform specific)  >---
 507   if (decode_env::match(event, "insn0")) {
 508     return Disassembler::decode_instruction0(arg, output(), arg);
 509   }
 510 
 511   //---<  Event: Print address  >---
 512   if (decode_env::match(event, "addr")) {
 513     print_address(arg);
 514     return arg;
 515   }
 516 
 517   //---<  Event: mach (inform about machine architecture)  >---
 518   // This event is problematic because it messes up the output.
 519   // The event is fired after the instruction address has already
 520   // been printed. The decoded instruction (event "insn") is
 521   // printed afterwards. That doesn't look nice.
 522   if (decode_env::match(event, "mach")) {
 523     guarantee(arg != NULL, "event_to_env - arg must not be NULL for event 'mach'");
 524     static char buffer[64] = { 0, };
 525     // Output suppressed because it messes up disassembly.
 526     // Only print this when the mach changes.
 527     if (false && (strcmp(buffer, (const char*)arg) != 0 ||
 528                   strlen((const char*)arg) > sizeof(buffer) - 1)) {
 529       // Only print this when the mach changes
 530       strncpy(buffer, (const char*)arg, sizeof(buffer) - 1);
 531       buffer[sizeof(buffer) - 1] = '\0';
 532       output()->print_cr("[Disassembling for mach='%s']", (const char*)arg);
 533     }
 534     return NULL;
 535   }
 536 
 537   //---<  Event: format bytes-per-line  >---
 538   if (decode_env::match(event, "format bytes-per-line")) {
 539     _bytes_per_line = (int) (intptr_t) arg;
 540     return NULL;

 541   }
 542 #endif
 543   return NULL;
 544 }
 545 
 546 static void* event_to_env(void* env_pv, const char* event, void* arg) {
 547   decode_env* env = (decode_env*) env_pv;
 548   return env->handle_event(event, (address) arg);
 549 }
 550 
 551 // called by the disassembler to print out jump targets and data addresses
 552 void decode_env::print_address(address adr) {
 553   outputStream* st = output();
 554 
 555   if (adr == NULL) {
 556     st->print("NULL");
 557     return;
 558   }
 559 
 560   int small_num = (int)(intptr_t)adr;
 561   if ((intptr_t)adr == (intptr_t)small_num
 562       && -1 <= small_num && small_num <= 9) {
 563     st->print("%d", small_num);
 564     return;
 565   }
 566 
 567   if (Universe::is_fully_initialized()) {
 568     if (StubRoutines::contains(adr)) {
 569       StubCodeDesc* desc = StubCodeDesc::desc_for(adr);
 570       if (desc == NULL) {
 571         desc = StubCodeDesc::desc_for(adr + frame::pc_return_offset);
 572       }
 573       if (desc != NULL) {


 578           st->print(" " PTR_FORMAT, p2i(adr));
 579         }
 580         return;
 581       }
 582       st->print("Stub::<unknown> " PTR_FORMAT, p2i(adr));
 583       return;
 584     }
 585 
 586     BarrierSet* bs = BarrierSet::barrier_set();
 587     if (bs->is_a(BarrierSet::CardTableBarrierSet) &&
 588         adr == ci_card_table_address_as<address>()) {
 589       st->print("word_map_base");
 590       if (WizardMode) st->print(" " INTPTR_FORMAT, p2i(adr));
 591       return;
 592     }
 593   }
 594 
 595   if (_nm == NULL) {
 596     // Don't do this for native methods, as the function name will be printed in
 597     // nmethod::reloc_string_for().
 598     // Allocate the buffer on the stack instead of as RESOURCE array.
 599     // In case we do DecodeErrorFile, Thread will not be initialized,
 600     // causing a "assert(current != __null) failed" failure.
 601     const int buflen = 1024;
 602     char buf[buflen];
 603     int offset;
 604     if (os::dll_address_to_function_name(adr, buf, buflen, &offset)) {
 605       st->print(PTR_FORMAT " = %s",  p2i(adr), buf);
 606       if (offset != 0) {
 607         st->print("+%d", offset);
 608       }
 609       return;
 610     }
 611   }
 612 
 613   // Fall through to a simple (hexadecimal) numeral.
 614   st->print(PTR_FORMAT, p2i(adr));
 615 }
 616 
 617 void decode_env::print_insn_labels() {
 618   if (AbstractDisassembler::show_block_comment()) {
 619     address       p  = cur_insn();
 620     outputStream* st = output();









 621 
 622     //---<  Block comments for nmethod  >---
 623     // Outputs a bol() before and a cr() after, but only if a comment is printed.
 624     // Prints nmethod_section_label as well.
 625     if (_nm != NULL) {
 626       _nm->print_block_comment(st, p);
















 627     }
 628     if (_codeBlob != NULL) {
 629       _codeBlob->print_block_comment(st, p);


 630     }
 631     if (_codeBuffer != NULL) {
 632       _codeBuffer->print_block_comment(st, p);
 633     }
 634     _strings.print_block_comment(st, (intptr_t)(p - _start));
 635   }
 636 }
 637 
 638 void decode_env::print_insn_prefix() {
 639   address       p  = cur_insn();
 640   outputStream* st = output();
 641   AbstractDisassembler::print_location(p, _start, _end, st, false, false);
 642   AbstractDisassembler::print_instruction(p, Assembler::instr_len(p), Assembler::instr_maxlen(), st, true, false);
 643 }
 644 
 645 ATTRIBUTE_PRINTF(2, 3)
 646 static int printf_to_env(void* env_pv, const char* format, ...) {
 647   decode_env* env = (decode_env*) env_pv;
 648   outputStream* st = env->output();
 649   size_t flen = strlen(format);
 650   const char* raw = NULL;
 651   if (flen == 0)  return 0;
 652   if (flen == 1 && format[0] == '\n') { st->bol(); return 1; }
 653   if (flen < 2 ||
 654       strchr(format, '%') == NULL) {
 655     raw = format;
 656   } else if (format[0] == '%' && format[1] == '%' &&
 657              strchr(format+2, '%') == NULL) {
 658     // happens a lot on machines with names like %foo
 659     flen--;
 660     raw = format+1;
 661   }
 662   if (raw != NULL) {
 663     st->print_raw(raw, (int) flen);
 664     return (int) flen;
 665   }
 666   va_list ap;
 667   va_start(ap, format);
 668   julong cnt0 = st->count();
 669   st->vprint(format, ap);
 670   julong cnt1 = st->count();
 671   va_end(ap);
 672   return (int)(cnt1 - cnt0);
 673 }
 674 
 675 // The 'original_start' argument holds the the original address where
 676 // the instructions were located in the originating system. If zero (NULL)
 677 // is passed in, there is no original address.
 678 address decode_env::decode_instructions(address start, address end, address original_start /* = 0*/) {
 679   // CodeComment in Stubs.
 680   // Properly initialize _start/_end. Overwritten too often if
 681   // printing of instructions is called for each instruction.
 682   assert((_start == NULL) || (start == NULL) || (_start == start), "don't overwrite CTOR values");
 683   assert((_end   == NULL) || (end   == NULL) || (_end   == end  ), "don't overwrite CTOR values");
 684   if (start != NULL) set_start(start);
 685   if (end   != NULL) set_end(end);
 686   if (original_start == NULL) {
 687     original_start = start;
 688   }
 689 
 690   //---<  Check (and correct) alignment  >---
 691   // Don't check alignment of end, it is not aligned.
 692   if (((uint64_t)start & ((uint64_t)Disassembler::pd_instruction_alignment() - 1)) != 0) {
 693     output()->print_cr("Decode range start:" PTR_FORMAT ": ... (unaligned)", p2i(start));
 694     start = (address)((uint64_t)start & ~((uint64_t)Disassembler::pd_instruction_alignment() - 1));
 695   }
 696 
 697   // Trying to decode instructions doesn't make sense if we
 698   // couldn't load the disassembler library.
 699   if (Disassembler::is_abstract()) {
 700     return NULL;
 701   }
 702 
 703   // decode a series of instructions and return the end of the last instruction
 704 
 705   if (_print_raw) {
 706     // Print whatever the library wants to print, w/o fancy callbacks.
 707     // This is mainly for debugging the library itself.
 708     FILE* out = stdout;
 709     FILE* xmlout = (_print_raw > 1 ? out : NULL);
 710     return use_new_version ?
 711       (address)
 712       (*Disassembler::_decode_instructions_virtual)((uintptr_t)start, (uintptr_t)end,
 713                                                     start, end - start,
 714                                                     NULL, (void*) xmlout,
 715                                                     NULL, (void*) out,
 716                                                     options(), 0/*nice new line*/)
 717       :
 718       (address)
 719       (*Disassembler::_decode_instructions)(start, end,
 720                                             NULL, (void*) xmlout,
 721                                             NULL, (void*) out,
 722                                             options());
 723   }
 724 
 725   return use_new_version ?
 726     (address)
 727     (*Disassembler::_decode_instructions_virtual)((uintptr_t)start, (uintptr_t)end,
 728                                                   start, end - start,
 729                                                   &event_to_env,  (void*) this,
 730                                                   &printf_to_env, (void*) this,
 731                                                   options(), 0/*nice new line*/)
 732     :
 733     (address)
 734     (*Disassembler::_decode_instructions)(start, end,
 735                                           &event_to_env,  (void*) this,
 736                                           &printf_to_env, (void*) this,
 737                                           options());
 738 }
 739 
 740 // ----------------------------------------------------------------------------
 741 // Disassembler
 742 // Used as a static wrapper for decode_env.
 743 // Each method will create a decode_env before decoding.
 744 // You can call the decode_env methods directly if you already have one.
 745 
 746 
 747 bool Disassembler::load_library(outputStream* st) {
 748   // Do not try to load multiple times. Failed once -> fails always.
 749   // To force retry in debugger: assign _tried_to_load_library=0
 750   if (_tried_to_load_library) {
 751     return _library_usable;
 752   }
 753 
 754 #if defined(SUPPORT_ASSEMBLY) || defined(SUPPORT_ABSTRACT_ASSEMBLY)
 755   // Print to given stream, if any.
 756   // Print to tty if Verbose is on and no stream given.
 757   st = ((st == NULL) && Verbose) ? tty : st;
 758 
 759   // Compute fully qualified library name.
 760   char ebuf[1024];
 761   char buf[JVM_MAXPATHLEN];
 762   os::jvm_path(buf, sizeof(buf));
 763   int jvm_offset = -1;
 764   int lib_offset = -1;
 765 #ifdef STATIC_BUILD
 766   char* p = strrchr(buf, '/');
 767   *p = '\0';
 768   strcat(p, "/lib/");
 769   lib_offset = jvm_offset = strlen(buf);
 770 #else
 771   {
 772     // Match "libjvm" instead of "jvm" on *nix platforms. Creates better matches.
 773     // Match "[lib]jvm[^/]*" in jvm_path.
 774     const char* base = buf;
 775     const char* p = strrchr(buf, *os::file_separator());
 776 #ifdef _WIN32
 777     p = strstr(p ? p : base, "jvm");
 778 #else
 779     p = strstr(p ? p : base, "libjvm");
 780 #endif
 781     if (p != NULL) lib_offset = p - base + 1;
 782     if (p != NULL) jvm_offset = p - base;
 783   }
 784 #endif
 785 
 786   // Find the disassembler shared library.
 787   // Search for several paths derived from libjvm, in this order:
 788   // 1. <home>/jre/lib/<arch>/<vm>/libhsdis-<arch>.so  (for compatibility)
 789   // 2. <home>/jre/lib/<arch>/<vm>/hsdis-<arch>.so
 790   // 3. <home>/jre/lib/<arch>/hsdis-<arch>.so
 791   // 4. hsdis-<arch>.so  (using LD_LIBRARY_PATH)
 792   if (jvm_offset >= 0) {
 793     // 1. <home>/jre/lib/<arch>/<vm>/libhsdis-<arch>.so
 794     strcpy(&buf[jvm_offset], hsdis_library_name);
 795     strcat(&buf[jvm_offset], os::dll_file_extension());
 796     _library = os::dll_load(buf, ebuf, sizeof ebuf);
 797     if (_library == NULL && lib_offset >= 0) {
 798       // 2. <home>/jre/lib/<arch>/<vm>/hsdis-<arch>.so
 799       strcpy(&buf[lib_offset], hsdis_library_name);
 800       strcat(&buf[lib_offset], os::dll_file_extension());
 801       _library = os::dll_load(buf, ebuf, sizeof ebuf);
 802     }
 803     if (_library == NULL && lib_offset > 0) {
 804       // 3. <home>/jre/lib/<arch>/hsdis-<arch>.so
 805       buf[lib_offset - 1] = '\0';
 806       const char* p = strrchr(buf, *os::file_separator());
 807       if (p != NULL) {
 808         lib_offset = p - buf + 1;
 809         strcpy(&buf[lib_offset], hsdis_library_name);
 810         strcat(&buf[lib_offset], os::dll_file_extension());
 811         _library = os::dll_load(buf, ebuf, sizeof ebuf);
 812       }
 813     }
 814   }
 815   if (_library == NULL) {
 816     // 4. hsdis-<arch>.so  (using LD_LIBRARY_PATH)
 817     strcpy(&buf[0], hsdis_library_name);
 818     strcat(&buf[0], os::dll_file_extension());
 819     _library = os::dll_load(buf, ebuf, sizeof ebuf);
 820   }
 821 
 822   // load the decoder function to use (new or old version).
 823   if (_library != NULL) {
 824     _decode_instructions_virtual = CAST_TO_FN_PTR(Disassembler::decode_func_virtual,
 825                                           os::dll_lookup(_library, decode_instructions_virtual_name));
 826   }
 827   if (_decode_instructions_virtual == NULL && _library != NULL) {
 828     // could not spot in new version, try old version
 829     _decode_instructions = CAST_TO_FN_PTR(Disassembler::decode_func,
 830                                           os::dll_lookup(_library, decode_instructions_name));
 831     use_new_version = false;
 832   } else {
 833     use_new_version = true;
 834   }
 835   _tried_to_load_library = true;
 836   _library_usable        = _decode_instructions_virtual != NULL || _decode_instructions != NULL;
 837 
 838   // Create a dummy environment to initialize PrintAssemblyOptions.
 839   // The PrintAssemblyOptions must be known for abstract disassemblies as well.
 840   decode_env dummy((unsigned char*)(&buf[0]), (unsigned char*)(&buf[1]), st);
 841 
 842   // Report problems during dll_load or dll_lookup, if any.
 843   if (st != NULL) {
 844     // Success.
 845     if (_library_usable) {
 846       st->print_cr("Loaded disassembler from %s", buf);
 847     } else {
 848       st->print_cr("Could not load %s; %s; %s",
 849                    buf,
 850                    ((_library != NULL)
 851                     ? "entry point is missing"
 852                     : ((WizardMode || PrintMiscellaneous)
 853                        ? (const char*)ebuf
 854                        : "library not loadable")),
 855                    "PrintAssembly defaults to abstract disassembly.");
 856     }
 857   }
 858 #endif
 859   return _library_usable;
 860 }
 861 
 862 
 863 // Directly disassemble code buffer.
 864 void Disassembler::decode(CodeBuffer* cb, address start, address end, outputStream* st) {
 865 #if defined(SUPPORT_ASSEMBLY) || defined(SUPPORT_ABSTRACT_ASSEMBLY)
 866   //---<  Test memory before decoding  >---
 867   if (!(cb->contains(start) && cb->contains(end))) {
 868     //---<  Allow output suppression, but prevent writing to a NULL stream. Could happen with +PrintStubCode.  >---
 869     if (st != NULL) {
 870       st->print("Memory range [" PTR_FORMAT ".." PTR_FORMAT "] not contained in CodeBuffer", p2i(start), p2i(end));
 871     }
 872     return;
 873   }
 874   if (!os::is_readable_range(start, end)) {
 875     //---<  Allow output suppression, but prevent writing to a NULL stream. Could happen with +PrintStubCode.  >---
 876     if (st != NULL) {
 877       st->print("Memory range [" PTR_FORMAT ".." PTR_FORMAT "] not readable", p2i(start), p2i(end));
 878     }
 879     return;
 880   }
 881 
 882   decode_env env(cb, st);
 883   env.output()->print_cr("--------------------------------------------------------------------------------");
 884   env.output()->print("Decoding CodeBuffer (" PTR_FORMAT ")", p2i(cb));
 885   if (cb->name() != NULL) {
 886     env.output()->print(", name: %s,", cb->name());
 887   }
 888   env.output()->print_cr(" at  [" PTR_FORMAT ", " PTR_FORMAT "]  " JLONG_FORMAT " bytes", p2i(start), p2i(end), ((jlong)(end - start)));
 889 
 890   if (is_abstract()) {
 891     AbstractDisassembler::decode_abstract(start, end, env.output(), Assembler::instr_maxlen());
 892   } else {
 893     env.decode_instructions(start, end);
 894   }
 895   env.output()->print_cr("--------------------------------------------------------------------------------");
 896 #endif
 897 }
 898 
 899 // Directly disassemble code blob.
 900 void Disassembler::decode(CodeBlob* cb, outputStream* st, CodeStrings c) {
 901 #if defined(SUPPORT_ASSEMBLY) || defined(SUPPORT_ABSTRACT_ASSEMBLY)
 902   if (cb->is_nmethod()) {
 903     // If we  have an nmethod at hand,
 904     // call the specialized decoder directly.
 905     decode((nmethod*)cb, st, c);
 906     return;
 907   }
 908 
 909   decode_env env(cb, st);
 910   env.output()->print_cr("--------------------------------------------------------------------------------");
 911   if (cb->is_aot()) {
 912     env.output()->print("A ");
 913     if (cb->is_compiled()) {
 914       CompiledMethod* cm = (CompiledMethod*)cb;
 915       env.output()->print("%d ",cm->compile_id());
 916       cm->method()->method_holder()->name()->print_symbol_on(env.output());
 917       env.output()->print(".");
 918       cm->method()->name()->print_symbol_on(env.output());
 919       cm->method()->signature()->print_symbol_on(env.output());
 920     } else {
 921       env.output()->print_cr("%s", cb->name());
 922     }
 923   } else {
 924     env.output()->print("Decoding CodeBlob");
 925     if (cb->name() != NULL) {
 926       env.output()->print(", name: %s,", cb->name());
 927     }
 928   }
 929   env.output()->print_cr(" at  [" PTR_FORMAT ", " PTR_FORMAT "]  " JLONG_FORMAT " bytes", p2i(cb->code_begin()), p2i(cb->code_end()), ((jlong)(cb->code_end() - cb->code_begin())));
 930 
 931   if (is_abstract()) {
 932     AbstractDisassembler::decode_abstract(cb->code_begin(), cb->code_end(), env.output(), Assembler::instr_maxlen());
 933   } else {
 934     env.decode_instructions(cb->code_begin(), cb->code_end());
 935   }
 936   env.output()->print_cr("--------------------------------------------------------------------------------");
 937 #endif
 938 }
 939 
 940 // Decode a nmethod.
 941 // This includes printing the constant pool and all code segments.
 942 // The nmethod data structures (oop maps, relocations and the like) are not printed.
 943 void Disassembler::decode(nmethod* nm, outputStream* st, CodeStrings c) {
 944 #if defined(SUPPORT_ASSEMBLY) || defined(SUPPORT_ABSTRACT_ASSEMBLY)
 945   ttyLocker ttyl;




 946 



 947   decode_env env(nm, st);
 948   env.output()->print_cr("--------------------------------------------------------------------------------");
 949   nm->print_constant_pool(env.output());
 950   env.output()->print_cr("--------------------------------------------------------------------------------");
 951   env.output()->cr();
 952   if (is_abstract()) {
 953     AbstractDisassembler::decode_abstract(nm->code_begin(), nm->code_end(), env.output(), Assembler::instr_maxlen());
 954   } else {
 955     env.decode_instructions(nm->code_begin(), nm->code_end());







 956   }
 957   env.output()->print_cr("--------------------------------------------------------------------------------");
 958 #endif
 959 }
 960 
 961 // Decode a range, given as [start address, end address)
 962 void Disassembler::decode(address start, address end, outputStream* st, CodeStrings c /*, ptrdiff_t offset */) {
 963 #if defined(SUPPORT_ASSEMBLY) || defined(SUPPORT_ABSTRACT_ASSEMBLY)
 964   //---<  Test memory before decoding  >---
 965   if (!os::is_readable_range(start, end)) {
 966     //---<  Allow output suppression, but prevent writing to a NULL stream. Could happen with +PrintStubCode.  >---
 967     if (st != NULL) {
 968       st->print("Memory range [" PTR_FORMAT ".." PTR_FORMAT "] not readable", p2i(start), p2i(end));

 969     }
 970     return;
 971   }
 972 
 973   if (is_abstract()) {
 974     AbstractDisassembler::decode_abstract(start, end, st, Assembler::instr_maxlen());
 975     return;
 976   }
 977 
 978 // Don't do that fancy stuff. If we just have two addresses, live with it
 979 // and treat the memory contents as "amorphic" piece of code.
 980 #if 0
 981   CodeBlob* cb = CodeCache::find_blob_unsafe(start);
 982   if (cb != NULL) {
 983     // If we  have an CodeBlob at hand,
 984     // call the specialized decoder directly.
 985     decode(cb, st, c);
 986   } else
 987 #endif
 988   {
 989     // This seems to be just a chunk of memory.
 990     decode_env env(start, end, st);
 991     env.output()->print_cr("--------------------------------------------------------------------------------");
 992     env.decode_instructions(start, end);
 993     env.output()->print_cr("--------------------------------------------------------------------------------");
 994   }
 995 #endif
 996 }
 997 
 998 // To prevent excessive code expansion in the interpreter generator, we
 999 // do not inline this function into Disassembler::hook().
1000 void Disassembler::_hook(const char* file, int line, MacroAssembler* masm) {
1001   decode_env::hook(file, line, masm->code_section()->end());
1002 }
< prev index next >