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/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;
 218       }
 219       Link *link = new Link(file, line);
 220       if (head == NULL) {
 221         head = tail = link;
 222       } else {
 223         tail->next = link;
 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);
 298   outputStream* st = output();
 299   if (found != NULL) {
 300     for (SourceFileInfo::Link *link = found->head; link; link = link->next) {
 301       const char* file = link->file;
 302       int line = link->line;
 303       if (_cached_src == NULL || strcmp(_cached_src, file) != 0) {
 304         FILE* fp;
 305 
 306         // _cached_src_lines is a single cache of the lines of a source file, and we refill this cache
 307         // every time we need to print a line from a different source file. It's not the fastest,
 308         // but seems bearable.
 309         if (_cached_src_lines != NULL) {
 310           for (int i=0; i<_cached_src_lines->length(); i++) {
 311             os::free((void*)_cached_src_lines->at(i));
 312           }
 313           _cached_src_lines->clear();
 314         } else {
 315           _cached_src_lines = new (ResourceObj::C_HEAP, mtCode)GrowableArray<const char*>(0, true);
 316         }
 317 
 318         if ((fp = fopen(file, "r")) == NULL) {
 319           _cached_src = NULL;
 320           return;
 321         }
 322         _cached_src = file;
 323 
 324         char line[500]; // don't write lines that are too long in your source files!
 325         while (fgets(line, sizeof(line), fp) != NULL) {
 326           size_t len = strlen(line);
 327           if (len > 0 && line[len-1] == '\n') {
 328             line[len-1] = '\0';
 329           }
 330           _cached_src_lines->append(os::strdup(line));
 331         }
 332         fclose(fp);
 333         _print_file_name = true;
 334       }
 335 
 336       if (_print_file_name) {
 337         // We print the file name whenever we switch to a new file, or when
 338         // Disassembler::decode is called to disassemble a new block of code.
 339         _print_file_name = false;
 340         if (newline) {
 341           st->cr();
 342         }
 343         st->move_to(COMMENT_COLUMN);
 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) {
 456         st->print("Stub::%s", desc->name());
 457         if (desc->begin() != adr) {
 458           st->print(INTX_FORMAT_W(+) " " PTR_FORMAT, adr - desc->begin(), p2i(adr));
 459         } else if (WizardMode) {
 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 }