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 "oops/oop.inline.hpp"
  36 #include "runtime/handles.inline.hpp"
  37 #include "runtime/os.inline.hpp"
  38 #include "runtime/stubCodeGenerator.hpp"
  39 #include "runtime/stubRoutines.hpp"
  40 #include "utilities/resourceHash.hpp"
  41 #include CPU_HEADER(depChecker)
  42 
  43 void*       Disassembler::_library               = NULL;
  44 bool        Disassembler::_tried_to_load_library = false;
  45 
  46 // This routine is in the shared library:
  47 Disassembler::decode_func_virtual Disassembler::_decode_instructions_virtual = NULL;
  48 Disassembler::decode_func Disassembler::_decode_instructions = NULL;
  49 
  50 static const char hsdis_library_name[] = "hsdis-" HOTSPOT_LIB_ARCH;
  51 static const char decode_instructions_virtual_name[] = "decode_instructions_virtual";
  52 static const char decode_instructions_name[] = "decode_instructions";
  53 static bool use_new_version = true;
  54 #define COMMENT_COLUMN  52 LP64_ONLY(+8) /*could be an option*/
  55 #define BYTES_COMMENT   ";..."  /* funky byte display comment */
  56 
  57 bool Disassembler::load_library() {
  58   if (_decode_instructions_virtual != NULL || _decode_instructions != NULL) {
  59     // Already succeeded.
  60     return true;
  61   }
  62   if (_tried_to_load_library) {
  63     // Do not try twice.
  64     // To force retry in debugger: assign _tried_to_load_library=0
  65     return false;
  66   }
  67   // Try to load it.
  68   char ebuf[1024];
  69   char buf[JVM_MAXPATHLEN];
  70   os::jvm_path(buf, sizeof(buf));
  71   int jvm_offset = -1;
  72   int lib_offset = -1;
  73 #ifdef STATIC_BUILD
  74   char* p = strrchr(buf, '/');
  75   *p = '\0';
  76   strcat(p, "/lib/");
  77   lib_offset = jvm_offset = strlen(buf);
  78 #else
  79   {
  80     // Match "jvm[^/]*" in jvm_path.
  81     const char* base = buf;
  82     const char* p = strrchr(buf, *os::file_separator());
  83     if (p != NULL) lib_offset = p - base + 1;
  84     p = strstr(p ? p : base, "jvm");
  85     if (p != NULL) jvm_offset = p - base;
  86   }
  87 #endif
  88   // Find the disassembler shared library.
  89   // Search for several paths derived from libjvm, in this order:
  90   // 1. <home>/jre/lib/<arch>/<vm>/libhsdis-<arch>.so  (for compatibility)
  91   // 2. <home>/jre/lib/<arch>/<vm>/hsdis-<arch>.so
  92   // 3. <home>/jre/lib/<arch>/hsdis-<arch>.so
  93   // 4. hsdis-<arch>.so  (using LD_LIBRARY_PATH)
  94   if (jvm_offset >= 0) {
  95     // 1. <home>/jre/lib/<arch>/<vm>/libhsdis-<arch>.so
  96     strcpy(&buf[jvm_offset], hsdis_library_name);
  97     strcat(&buf[jvm_offset], os::dll_file_extension());
  98     _library = os::dll_load(buf, ebuf, sizeof ebuf);
  99     if (_library == NULL && lib_offset >= 0) {
 100       // 2. <home>/jre/lib/<arch>/<vm>/hsdis-<arch>.so
 101       strcpy(&buf[lib_offset], hsdis_library_name);
 102       strcat(&buf[lib_offset], os::dll_file_extension());
 103       _library = os::dll_load(buf, ebuf, sizeof ebuf);
 104     }
 105     if (_library == NULL && lib_offset > 0) {
 106       // 3. <home>/jre/lib/<arch>/hsdis-<arch>.so
 107       buf[lib_offset - 1] = '\0';
 108       const char* p = strrchr(buf, *os::file_separator());
 109       if (p != NULL) {
 110         lib_offset = p - buf + 1;
 111         strcpy(&buf[lib_offset], hsdis_library_name);
 112         strcat(&buf[lib_offset], os::dll_file_extension());
 113         _library = os::dll_load(buf, ebuf, sizeof ebuf);
 114       }
 115     }
 116   }
 117   if (_library == NULL) {
 118     // 4. hsdis-<arch>.so  (using LD_LIBRARY_PATH)
 119     strcpy(&buf[0], hsdis_library_name);
 120     strcat(&buf[0], os::dll_file_extension());
 121     _library = os::dll_load(buf, ebuf, sizeof ebuf);
 122   }
 123   if (_library != NULL) {
 124     _decode_instructions_virtual = CAST_TO_FN_PTR(Disassembler::decode_func_virtual,
 125                                           os::dll_lookup(_library, decode_instructions_virtual_name));
 126   }
 127   if (_decode_instructions_virtual == NULL && _library != NULL) {
 128     // could not spot in new version, try old version
 129     _decode_instructions = CAST_TO_FN_PTR(Disassembler::decode_func,
 130                                           os::dll_lookup(_library, decode_instructions_name));
 131     use_new_version = false;
 132   } else {
 133     use_new_version = true;
 134   }
 135   _tried_to_load_library = true;
 136   if (_decode_instructions_virtual == NULL && _decode_instructions == NULL) {
 137     tty->print_cr("Could not load %s; %s; %s", buf,
 138                   ((_library != NULL)
 139                    ? "entry point is missing"
 140                    : (WizardMode || PrintMiscellaneous)
 141                    ? (const char*)ebuf
 142                    : "library not loadable"),
 143                   "PrintAssembly is disabled");
 144     return false;
 145   }
 146 
 147   // Success.
 148   tty->print_cr("Loaded disassembler from %s", buf);
 149   return true;
 150 }
 151 
 152 
 153 class decode_env {
 154  private:
 155   nmethod*      _nm;
 156   CodeBlob*     _code;
 157   CodeStrings   _strings;
 158   outputStream* _output;
 159   address       _start, _end;
 160   ptrdiff_t     _offset;
 161 
 162   char          _option_buf[512];
 163   char          _print_raw;
 164   bool          _print_pc;
 165   bool          _print_bytes;
 166   address       _cur_insn;
 167   int           _bytes_per_line; // arch-specific formatting option
 168   bool          _print_file_name;
 169 
 170   static bool match(const char* event, const char* tag) {
 171     size_t taglen = strlen(tag);
 172     if (strncmp(event, tag, taglen) != 0)
 173       return false;
 174     char delim = event[taglen];
 175     return delim == '\0' || delim == ' ' || delim == '/' || delim == '=';
 176   }
 177 
 178   void collect_options(const char* p) {
 179     if (p == NULL || p[0] == '\0')  return;
 180     size_t opt_so_far = strlen(_option_buf);
 181     if (opt_so_far + 1 + strlen(p) + 1 > sizeof(_option_buf))  return;
 182     char* fillp = &_option_buf[opt_so_far];
 183     if (opt_so_far > 0) *fillp++ = ',';
 184     strcat(fillp, p);
 185     // replace white space by commas:
 186     char* q = fillp;
 187     while ((q = strpbrk(q, " \t\n")) != NULL)
 188       *q++ = ',';
 189     // Note that multiple PrintAssemblyOptions flags accumulate with \n,
 190     // which we want to be changed to a comma...
 191   }
 192 
 193   void print_insn_labels();
 194   void print_insn_bytes(address pc0, address pc);
 195   void print_address(address value);
 196 
 197   struct SourceFileInfo {
 198     struct Link : public CHeapObj<mtCode> {
 199       const char* file;
 200       int line;
 201       Link* next;
 202       Link(const char* f, int l) : file(f), line(l), next(NULL) {}
 203     };
 204     Link *head, *tail;
 205 
 206     static unsigned hash(const address& a) {
 207       return primitive_hash<address>(a);
 208     }
 209     static bool equals(const address& a0, const address& a1) {
 210       return primitive_equals<address>(a0, a1);
 211     }
 212     void append(const char* file, int line) {
 213       if (tail != NULL && tail->file == file && tail->line == line) {
 214         // Don't print duplicated lines at the same address. This could happen with C
 215         // macros that end up having multiple "__" tokens on the same __LINE__.
 216         return;
 217       }
 218       Link *link = new Link(file, line);
 219       if (head == NULL) {
 220         head = tail = link;
 221       } else {
 222         tail->next = link;
 223         tail = link;
 224       }
 225     }
 226     SourceFileInfo(const char* file, int line) : head(NULL), tail(NULL) {
 227       append(file, line);
 228     }
 229   };
 230 
 231   typedef ResourceHashtable<
 232       address, SourceFileInfo,
 233       SourceFileInfo::hash,
 234       SourceFileInfo::equals,
 235       15889,      // prime number
 236       ResourceObj::C_HEAP> SourceFileInfoTable;
 237 
 238   static SourceFileInfoTable _src_table;
 239   static const char* _cached_src;
 240   static GrowableArray<const char*>* _cached_src_lines;
 241 
 242  public:
 243   decode_env(CodeBlob* code, outputStream* output,
 244              CodeStrings c = CodeStrings(), ptrdiff_t offset = 0);
 245 
 246   address decode_instructions(address start, address end);
 247 
 248   void start_insn(address pc) {
 249     _cur_insn = pc;
 250     output()->bol();
 251     print_insn_labels();
 252   }
 253 
 254   void end_insn(address pc) {
 255     address pc0 = cur_insn();
 256     outputStream* st = output();
 257     if (_print_bytes && pc > pc0)
 258       print_insn_bytes(pc0, pc);
 259     if (_nm != NULL) {
 260       _nm->print_code_comment_on(st, COMMENT_COLUMN, pc0, pc);
 261       // this calls reloc_string_for which calls oop::print_value_on
 262     }
 263     print_hook_comments(pc0, _nm != NULL);
 264     // follow each complete insn by a nice newline
 265     st->cr();
 266   }
 267 
 268   address handle_event(const char* event, address arg);
 269 
 270   outputStream* output() { return _output; }
 271   address cur_insn() { return _cur_insn; }
 272   const char* options() { return _option_buf; }
 273   static void hook(const char* file, int line, address pc);
 274   void print_hook_comments(address pc, bool newline);
 275 };
 276 
 277 decode_env::SourceFileInfoTable decode_env::_src_table;
 278 const char* decode_env::_cached_src = NULL;
 279 GrowableArray<const char*>* decode_env::_cached_src_lines = NULL;
 280 
 281 void decode_env::hook(const char* file, int line, address pc) {
 282   // For simplication, we never free from this table. It's really not
 283   // necessary as we add to the table only when PrintInterpreter is true,
 284   // which means we are debugging the VM and a little bit of extra
 285   // memory usage doesn't matter.
 286   SourceFileInfo* found = _src_table.get(pc);
 287   if (found != NULL) {
 288     found->append(file, line);
 289   } else {
 290     SourceFileInfo sfi(file, line);
 291     _src_table.put(pc, sfi); // sfi is copied by value
 292   }
 293 }
 294 
 295 void decode_env::print_hook_comments(address pc, bool newline) {
 296   SourceFileInfo* found = _src_table.get(pc);
 297   outputStream* st = output();
 298   if (found != NULL) {
 299     for (SourceFileInfo::Link *link = found->head; link; link = link->next) {
 300       const char* file = link->file;
 301       int line = link->line;
 302       if (_cached_src == NULL || strcmp(_cached_src, file) != 0) {
 303         FILE* fp;
 304 
 305         // _cached_src_lines is a single cache of the lines of a source file, and we refill this cache
 306         // every time we need to print a line from a different source file. It's not the fastest,
 307         // but seems bearable.
 308         if (_cached_src_lines != NULL) {
 309           for (int i=0; i<_cached_src_lines->length(); i++) {
 310             os::free((void*)_cached_src_lines->at(i));
 311           }
 312           _cached_src_lines->clear();
 313         } else {
 314           _cached_src_lines = new (ResourceObj::C_HEAP, mtCode)GrowableArray<const char*>(0, true);
 315         }
 316 
 317         if ((fp = fopen(file, "r")) == NULL) {
 318           _cached_src = NULL;
 319           return;
 320         }
 321         _cached_src = file;
 322 
 323         char line[500]; // don't write lines that are too long in your source files!
 324         while (fgets(line, sizeof(line), fp) != NULL) {
 325           size_t len = strlen(line);
 326           if (len > 0 && line[len-1] == '\n') {
 327             line[len-1] = '\0';
 328           }
 329           _cached_src_lines->append(os::strdup(line));
 330         }
 331         fclose(fp);
 332         _print_file_name = true;
 333       }
 334 
 335       if (_print_file_name) {
 336         // We print the file name whenever we switch to a new file, or when
 337         // Disassembler::decode is called to disassemble a new block of code.
 338         _print_file_name = false;
 339         if (newline) {
 340           st->cr();
 341         }
 342         st->move_to(COMMENT_COLUMN);
 343         st->print(";;@FILE: %s", file);
 344         newline = true;
 345       }
 346 
 347       int index = line - 1; // 1-based line number -> 0-based index.
 348       if (index >= _cached_src_lines->length()) {
 349         // This could happen if source file is mismatched.
 350       } else {
 351         const char* source_line = _cached_src_lines->at(index);
 352         if (newline) {
 353           st->cr();
 354         }
 355         st->move_to(COMMENT_COLUMN);
 356         st->print(";;%5d: %s", line, source_line);
 357         newline = true;
 358       }
 359     }
 360   }
 361 }
 362 
 363 decode_env::decode_env(CodeBlob* code, outputStream* output, CodeStrings c,
 364                        ptrdiff_t offset) : _nm(NULL),
 365                                            _start(NULL),
 366                                            _end(NULL),
 367                                            _option_buf(),
 368                                            _print_raw('\0'),
 369                                            _cur_insn(NULL) {
 370   _output = output ? output : tty;
 371   _code = code;
 372   if (code != NULL && code->is_nmethod())
 373     _nm = (nmethod*) code;
 374   _strings.copy(c);
 375   _offset = offset;
 376 
 377   // by default, output pc but not bytes:
 378   _print_pc       = true;
 379   _print_bytes    = false;
 380   _bytes_per_line = Disassembler::pd_instruction_alignment();
 381   _print_file_name= true;
 382 
 383   // parse the global option string:
 384   collect_options(Disassembler::pd_cpu_opts());
 385   collect_options(PrintAssemblyOptions);
 386 
 387   if (strstr(options(), "hsdis-")) {
 388     if (strstr(options(), "hsdis-print-raw"))
 389       _print_raw = (strstr(options(), "xml") ? 2 : 1);
 390     if (strstr(options(), "hsdis-print-pc"))
 391       _print_pc = !_print_pc;
 392     if (strstr(options(), "hsdis-print-bytes"))
 393       _print_bytes = !_print_bytes;
 394   }
 395   if (strstr(options(), "help")) {
 396     tty->print_cr("PrintAssemblyOptions help:");
 397     tty->print_cr("  hsdis-print-raw       test plugin by requesting raw output");
 398     tty->print_cr("  hsdis-print-raw-xml   test plugin by requesting raw xml");
 399     tty->print_cr("  hsdis-print-pc        turn off PC printing (on by default)");
 400     tty->print_cr("  hsdis-print-bytes     turn on instruction byte output");
 401     tty->print_cr("combined options: %s", options());
 402   }
 403 }
 404 
 405 address decode_env::handle_event(const char* event, address arg) {
 406   if (match(event, "insn")) {
 407     start_insn(arg);
 408   } else if (match(event, "/insn")) {
 409     end_insn(arg);
 410   } else if (match(event, "addr")) {
 411     if (arg != NULL) {
 412       print_address(arg);
 413       return arg;
 414     }
 415   } else if (match(event, "mach")) {
 416     static char buffer[32] = { 0, };
 417     if (strcmp(buffer, (const char*)arg) != 0 ||
 418         strlen((const char*)arg) > sizeof(buffer) - 1) {
 419       // Only print this when the mach changes
 420       strncpy(buffer, (const char*)arg, sizeof(buffer) - 1);
 421       buffer[sizeof(buffer) - 1] = '\0';
 422       output()->print_cr("[Disassembling for mach='%s']", arg);
 423     }
 424   } else if (match(event, "format bytes-per-line")) {
 425     _bytes_per_line = (int) (intptr_t) arg;
 426   } else {
 427     // ignore unrecognized markup
 428   }
 429   return NULL;
 430 }
 431 
 432 // called by the disassembler to print out jump targets and data addresses
 433 void decode_env::print_address(address adr) {
 434   outputStream* st = _output;
 435 
 436   if (adr == NULL) {
 437     st->print("NULL");
 438     return;
 439   }
 440 
 441   int small_num = (int)(intptr_t)adr;
 442   if ((intptr_t)adr == (intptr_t)small_num
 443       && -1 <= small_num && small_num <= 9) {
 444     st->print("%d", small_num);
 445     return;
 446   }
 447 
 448   if (Universe::is_fully_initialized()) {
 449     if (StubRoutines::contains(adr)) {
 450       StubCodeDesc* desc = StubCodeDesc::desc_for(adr);
 451       if (desc == NULL) {
 452         desc = StubCodeDesc::desc_for(adr + frame::pc_return_offset);
 453       }
 454       if (desc != NULL) {
 455         st->print("Stub::%s", desc->name());
 456         if (desc->begin() != adr) {
 457           st->print(INTX_FORMAT_W(+) " " PTR_FORMAT, adr - desc->begin(), p2i(adr));
 458         } else if (WizardMode) {
 459           st->print(" " PTR_FORMAT, p2i(adr));
 460         }
 461         return;
 462       }
 463       st->print("Stub::<unknown> " PTR_FORMAT, p2i(adr));
 464       return;
 465     }
 466 
 467     BarrierSet* bs = BarrierSet::barrier_set();
 468     if (bs->is_a(BarrierSet::CardTableBarrierSet) &&
 469         adr == ci_card_table_address_as<address>()) {
 470       st->print("word_map_base");
 471       if (WizardMode) st->print(" " INTPTR_FORMAT, p2i(adr));
 472       return;
 473     }
 474   }
 475 
 476   if (_nm == NULL) {
 477     // Don't do this for native methods, as the function name will be printed in
 478     // nmethod::reloc_string_for().
 479     ResourceMark rm;
 480     const int buflen = 1024;
 481     char* buf = NEW_RESOURCE_ARRAY(char, buflen);
 482     int offset;
 483     if (os::dll_address_to_function_name(adr, buf, buflen, &offset)) {
 484       st->print(PTR_FORMAT " = %s",  p2i(adr), buf);
 485       if (offset != 0) {
 486         st->print("+%d", offset);
 487       }
 488       return;
 489     }
 490   }
 491 
 492   // Fall through to a simple (hexadecimal) numeral.
 493   st->print(PTR_FORMAT, p2i(adr));
 494 }
 495 
 496 void decode_env::print_insn_labels() {
 497   address p = cur_insn();
 498   outputStream* st = output();
 499   CodeBlob* cb = _code;
 500   if (cb != NULL) {
 501     cb->print_block_comment(st, p);
 502   }
 503   _strings.print_block_comment(st, (intptr_t)(p - _start + _offset));
 504   if (_print_pc) {
 505     st->print("  " PTR_FORMAT ": ", p2i(p));
 506   }
 507 }
 508 
 509 void decode_env::print_insn_bytes(address pc, address pc_limit) {
 510   outputStream* st = output();
 511   size_t incr = 1;
 512   size_t perline = _bytes_per_line;
 513   if ((size_t) Disassembler::pd_instruction_alignment() >= sizeof(int)
 514       && !((uintptr_t)pc % sizeof(int))
 515       && !((uintptr_t)pc_limit % sizeof(int))) {
 516     incr = sizeof(int);
 517     if (perline % incr)  perline += incr - (perline % incr);
 518   }
 519   while (pc < pc_limit) {
 520     // tab to the desired column:
 521     st->move_to(COMMENT_COLUMN);
 522     address pc0 = pc;
 523     address pc1 = pc + perline;
 524     if (pc1 > pc_limit)  pc1 = pc_limit;
 525     for (; pc < pc1; pc += incr) {
 526       if (pc == pc0) {
 527         st->print(BYTES_COMMENT);
 528       } else if ((uint)(pc - pc0) % sizeof(int) == 0) {
 529         st->print(" ");         // put out a space on word boundaries
 530       }
 531       if (incr == sizeof(int)) {
 532         st->print("%08x", *(int*)pc);
 533       } else {
 534         st->print("%02x", (*pc)&0xFF);
 535       }
 536     }
 537     st->cr();
 538   }
 539 }
 540 
 541 
 542 static void* event_to_env(void* env_pv, const char* event, void* arg) {
 543   decode_env* env = (decode_env*) env_pv;
 544   return env->handle_event(event, (address) arg);
 545 }
 546 
 547 ATTRIBUTE_PRINTF(2, 3)
 548 static int printf_to_env(void* env_pv, const char* format, ...) {
 549   decode_env* env = (decode_env*) env_pv;
 550   outputStream* st = env->output();
 551   size_t flen = strlen(format);
 552   const char* raw = NULL;
 553   if (flen == 0)  return 0;
 554   if (flen == 1 && format[0] == '\n') { st->bol(); return 1; }
 555   if (flen < 2 ||
 556       strchr(format, '%') == NULL) {
 557     raw = format;
 558   } else if (format[0] == '%' && format[1] == '%' &&
 559              strchr(format+2, '%') == NULL) {
 560     // happens a lot on machines with names like %foo
 561     flen--;
 562     raw = format+1;
 563   }
 564   if (raw != NULL) {
 565     st->print_raw(raw, (int) flen);
 566     return (int) flen;
 567   }
 568   va_list ap;
 569   va_start(ap, format);
 570   julong cnt0 = st->count();
 571   st->vprint(format, ap);
 572   julong cnt1 = st->count();
 573   va_end(ap);
 574   return (int)(cnt1 - cnt0);
 575 }
 576 
 577 address decode_env::decode_instructions(address start, address end) {
 578   _start = start; _end = end;
 579 
 580   assert(((((intptr_t)start | (intptr_t)end) % Disassembler::pd_instruction_alignment()) == 0), "misaligned insn addr");
 581 
 582   const int show_bytes = false; // for disassembler debugging
 583 
 584   //_version = Disassembler::pd_cpu_version();
 585 
 586   if (!Disassembler::can_decode()) {
 587     return NULL;
 588   }
 589 
 590   // decode a series of instructions and return the end of the last instruction
 591 
 592   if (_print_raw) {
 593     // Print whatever the library wants to print, w/o fancy callbacks.
 594     // This is mainly for debugging the library itself.
 595     FILE* out = stdout;
 596     FILE* xmlout = (_print_raw > 1 ? out : NULL);
 597     return use_new_version ?
 598       (address)
 599       (*Disassembler::_decode_instructions_virtual)((uintptr_t)start, (uintptr_t)end,
 600                                                     start, end - start,
 601                                                     NULL, (void*) xmlout,
 602                                                     NULL, (void*) out,
 603                                                     options(), 0/*nice new line*/)
 604       :
 605       (address)
 606       (*Disassembler::_decode_instructions)(start, end,
 607                                             NULL, (void*) xmlout,
 608                                             NULL, (void*) out,
 609                                             options());
 610   }
 611 
 612   return use_new_version ?
 613     (address)
 614     (*Disassembler::_decode_instructions_virtual)((uintptr_t)start, (uintptr_t)end,
 615                                                   start, end - start,
 616                                                   &event_to_env,  (void*) this,
 617                                                   &printf_to_env, (void*) this,
 618                                                   options(), 0/*nice new line*/)
 619     :
 620     (address)
 621     (*Disassembler::_decode_instructions)(start, end,
 622                                           &event_to_env,  (void*) this,
 623                                           &printf_to_env, (void*) this,
 624                                           options());
 625 }
 626 
 627 
 628 void Disassembler::decode(CodeBlob* cb, outputStream* st) {
 629   ttyLocker ttyl;
 630   if (!load_library())  return;
 631   if (cb->is_nmethod()) {
 632     decode((nmethod*)cb, st);
 633     return;
 634   }
 635   decode_env env(cb, st);
 636   env.output()->print_cr("----------------------------------------------------------------------");
 637   if (cb->is_aot()) {
 638     env.output()->print("A ");
 639     if (cb->is_compiled()) {
 640       CompiledMethod* cm = (CompiledMethod*)cb;
 641       env.output()->print("%d ",cm->compile_id());
 642       cm->method()->method_holder()->name()->print_symbol_on(env.output());
 643       env.output()->print(".");
 644       cm->method()->name()->print_symbol_on(env.output());
 645       cm->method()->signature()->print_symbol_on(env.output());
 646     } else {
 647       env.output()->print_cr("%s", cb->name());
 648     }
 649   } else {
 650     env.output()->print_cr("%s", cb->name());
 651   }
 652   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*));
 653   env.decode_instructions(cb->code_begin(), cb->code_end());
 654 }
 655 
 656 void Disassembler::decode(address start, address end, outputStream* st, CodeStrings c,
 657                           ptrdiff_t offset) {
 658   ttyLocker ttyl;
 659   if (!load_library())  return;
 660   decode_env env(CodeCache::find_blob_unsafe(start), st, c, offset);
 661   env.decode_instructions(start, end);
 662 }
 663 
 664 void Disassembler::decode(nmethod* nm, outputStream* st) {
 665   ttyLocker ttyl;
 666   if (!load_library())  return;
 667   decode_env env(nm, st);
 668   env.output()->print_cr("----------------------------------------------------------------------");
 669 
 670   unsigned char* p   = nm->code_begin();
 671   unsigned char* end = nm->code_end();
 672 
 673   nm->method()->method_holder()->name()->print_symbol_on(env.output());
 674   env.output()->print(".");
 675   nm->method()->name()->print_symbol_on(env.output());
 676   nm->method()->signature()->print_symbol_on(env.output());
 677 #if INCLUDE_JVMCI
 678   {
 679     const char* jvmciName = nm->jvmci_name();
 680     if (jvmciName != NULL) {
 681       env.output()->print(" (%s)", jvmciName);
 682     }
 683   }
 684 #endif
 685   env.output()->print_cr("  [" PTR_FORMAT ", " PTR_FORMAT "]  " JLONG_FORMAT " bytes", p2i(p), p2i(end), ((jlong)(end - p)));
 686 
 687   // Print constant table.
 688   if (nm->consts_size() > 0) {
 689     nm->print_nmethod_labels(env.output(), nm->consts_begin());
 690     int offset = 0;
 691     for (address p = nm->consts_begin(); p < nm->consts_end(); p += 4, offset += 4) {
 692       if ((offset % 8) == 0) {
 693         env.output()->print_cr("  " PTR_FORMAT " (offset: %4d): " PTR32_FORMAT "   " PTR64_FORMAT, p2i(p), offset, *((int32_t*) p), *((int64_t*) p));
 694       } else {
 695         env.output()->print_cr("  " PTR_FORMAT " (offset: %4d): " PTR32_FORMAT,                    p2i(p), offset, *((int32_t*) p));
 696       }
 697     }
 698   }
 699 
 700   env.decode_instructions(p, end);
 701 }
 702 
 703 // To prevent excessive code expansion in the interpreter generator, we
 704 // do not inline this function into Disassembler::hook().
 705 void Disassembler::_hook(const char* file, int line, MacroAssembler* masm) {
 706   decode_env::hook(file, line, masm->code_section()->end());
 707 }