src/share/vm/compiler/disassembler.cpp

Sat, 24 Oct 2020 16:43:47 +0800

author
aoqi
date
Sat, 24 Oct 2020 16:43:47 +0800
changeset 10015
eb7ce841ccec
parent 9931
fd44df5e3bc3
parent 10003
a0eb08e2db5a
permissions
-rw-r--r--

Merge

     1 /*
     2  * Copyright (c) 2008, 2014, 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  */
    25 /*
    26  * This file has been modified by Loongson Technology in 2015. These
    27  * modifications are Copyright (c) 2015 Loongson Technology, and are made
    28  * available on the same license terms set forth above.
    29  */
    31 #include "precompiled.hpp"
    32 #include "classfile/javaClasses.hpp"
    33 #include "code/codeCache.hpp"
    34 #include "compiler/disassembler.hpp"
    35 #include "gc_interface/collectedHeap.hpp"
    36 #include "memory/cardTableModRefBS.hpp"
    37 #include "runtime/fprofiler.hpp"
    38 #include "runtime/handles.inline.hpp"
    39 #include "runtime/stubCodeGenerator.hpp"
    40 #include "runtime/stubRoutines.hpp"
    41 #ifdef TARGET_ARCH_x86
    42 # include "depChecker_x86.hpp"
    43 #endif
    44 #ifdef TARGET_ARCH_sparc
    45 # include "depChecker_sparc.hpp"
    46 #endif
    47 #ifdef TARGET_ARCH_zero
    48 # include "depChecker_zero.hpp"
    49 #endif
    50 #ifdef TARGET_ARCH_arm
    51 # include "depChecker_arm.hpp"
    52 #endif
    53 #ifdef TARGET_ARCH_ppc
    54 # include "depChecker_ppc.hpp"
    55 #endif
    56 #ifdef TARGET_ARCH_mips
    57 # include "depChecker_mips.hpp"
    58 #endif
    59 #ifdef SHARK
    60 #include "shark/sharkEntry.hpp"
    61 #endif
    63 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
    65 void*       Disassembler::_library               = NULL;
    66 bool        Disassembler::_tried_to_load_library = false;
    68 // This routine is in the shared library:
    69 Disassembler::decode_func_virtual Disassembler::_decode_instructions_virtual = NULL;
    70 Disassembler::decode_func Disassembler::_decode_instructions = NULL;
    72 static const char hsdis_library_name[] = "hsdis-" HOTSPOT_LIB_ARCH;
    73 static const char decode_instructions_virtual_name[] = "decode_instructions_virtual";
    74 static const char decode_instructions_name[] = "decode_instructions";
    75 static bool use_new_version = true;
    76 #define COMMENT_COLUMN  40 LP64_ONLY(+8) /*could be an option*/
    77 #define BYTES_COMMENT   ";..."  /* funky byte display comment */
    79 bool Disassembler::load_library() {
    80   if (_decode_instructions_virtual != NULL || _decode_instructions != NULL) {
    81     // Already succeeded.
    82     return true;
    83   }
    84   if (_tried_to_load_library) {
    85     // Do not try twice.
    86     // To force retry in debugger: assign _tried_to_load_library=0
    87     return false;
    88   }
    89   // Try to load it.
    90   char ebuf[1024];
    91   char buf[JVM_MAXPATHLEN];
    92   os::jvm_path(buf, sizeof(buf));
    93   int jvm_offset = -1;
    94   int lib_offset = -1;
    95   {
    96     // Match "jvm[^/]*" in jvm_path.
    97     const char* base = buf;
    98     const char* p = strrchr(buf, '/');
    99     if (p != NULL) lib_offset = p - base + 1;
   100     p = strstr(p ? p : base, "jvm");
   101     if (p != NULL)  jvm_offset = p - base;
   102   }
   103   // Find the disassembler shared library.
   104   // Search for several paths derived from libjvm, in this order:
   105   // 1. <home>/jre/lib/<arch>/<vm>/libhsdis-<arch>.so  (for compatibility)
   106   // 2. <home>/jre/lib/<arch>/<vm>/hsdis-<arch>.so
   107   // 3. <home>/jre/lib/<arch>/hsdis-<arch>.so
   108   // 4. hsdis-<arch>.so  (using LD_LIBRARY_PATH)
   109   if (jvm_offset >= 0) {
   110     // 1. <home>/jre/lib/<arch>/<vm>/libhsdis-<arch>.so
   111     strcpy(&buf[jvm_offset], hsdis_library_name);
   112     strcat(&buf[jvm_offset], os::dll_file_extension());
   113     _library = os::dll_load(buf, ebuf, sizeof ebuf);
   114     if (_library == NULL) {
   115       // 2. <home>/jre/lib/<arch>/<vm>/hsdis-<arch>.so
   116       strcpy(&buf[lib_offset], hsdis_library_name);
   117       strcat(&buf[lib_offset], os::dll_file_extension());
   118       _library = os::dll_load(buf, ebuf, sizeof ebuf);
   119     }
   120     if (_library == NULL) {
   121       // 3. <home>/jre/lib/<arch>/hsdis-<arch>.so
   122       buf[lib_offset - 1] = '\0';
   123       const char* p = strrchr(buf, '/');
   124       if (p != NULL) {
   125         lib_offset = p - buf + 1;
   126         strcpy(&buf[lib_offset], hsdis_library_name);
   127         strcat(&buf[lib_offset], os::dll_file_extension());
   128         _library = os::dll_load(buf, ebuf, sizeof ebuf);
   129       }
   130     }
   131   }
   132   if (_library == NULL) {
   133     // 4. hsdis-<arch>.so  (using LD_LIBRARY_PATH)
   134     strcpy(&buf[0], hsdis_library_name);
   135     strcat(&buf[0], os::dll_file_extension());
   136     _library = os::dll_load(buf, ebuf, sizeof ebuf);
   137   }
   138   if (_library != NULL) {
   139     _decode_instructions_virtual = CAST_TO_FN_PTR(Disassembler::decode_func_virtual,
   140                                           os::dll_lookup(_library, decode_instructions_virtual_name));
   141   }
   142   if (_decode_instructions_virtual == NULL) {
   143     // could not spot in new version, try old version
   144     _decode_instructions = CAST_TO_FN_PTR(Disassembler::decode_func,
   145                                           os::dll_lookup(_library, decode_instructions_name));
   146     use_new_version = false;
   147   } else {
   148     use_new_version = true;
   149   }
   150   _tried_to_load_library = true;
   151   if (_decode_instructions_virtual == NULL && _decode_instructions == NULL) {
   152     tty->print_cr("Could not load %s; %s; %s", buf,
   153                   ((_library != NULL)
   154                    ? "entry point is missing"
   155                    : (WizardMode || PrintMiscellaneous)
   156                    ? (const char*)ebuf
   157                    : "library not loadable"),
   158                   "PrintAssembly is disabled");
   159     return false;
   160   }
   162   // Success.
   163   tty->print_cr("Loaded disassembler from %s", buf);
   164   return true;
   165 }
   168 class decode_env {
   169  private:
   170   nmethod*      _nm;
   171   CodeBlob*     _code;
   172   CodeStrings   _strings;
   173   outputStream* _output;
   174   address       _start, _end;
   176   char          _option_buf[512];
   177   char          _print_raw;
   178   bool          _print_pc;
   179   bool          _print_bytes;
   180   address       _cur_insn;
   181   int           _total_ticks;
   182   int           _bytes_per_line; // arch-specific formatting option
   184   static bool match(const char* event, const char* tag) {
   185     size_t taglen = strlen(tag);
   186     if (strncmp(event, tag, taglen) != 0)
   187       return false;
   188     char delim = event[taglen];
   189     return delim == '\0' || delim == ' ' || delim == '/' || delim == '=';
   190   }
   192   void collect_options(const char* p) {
   193     if (p == NULL || p[0] == '\0')  return;
   194     size_t opt_so_far = strlen(_option_buf);
   195     if (opt_so_far + 1 + strlen(p) + 1 > sizeof(_option_buf))  return;
   196     char* fillp = &_option_buf[opt_so_far];
   197     if (opt_so_far > 0) *fillp++ = ',';
   198     strcat(fillp, p);
   199     // replace white space by commas:
   200     char* q = fillp;
   201     while ((q = strpbrk(q, " \t\n")) != NULL)
   202       *q++ = ',';
   203     // Note that multiple PrintAssemblyOptions flags accumulate with \n,
   204     // which we want to be changed to a comma...
   205   }
   207   void print_insn_labels();
   208   void print_insn_bytes(address pc0, address pc);
   209   void print_address(address value);
   211  public:
   212   decode_env(CodeBlob* code, outputStream* output, CodeStrings c = CodeStrings());
   214   address decode_instructions(address start, address end);
   216   void start_insn(address pc) {
   217     _cur_insn = pc;
   218     output()->bol();
   219     print_insn_labels();
   220   }
   222   void end_insn(address pc) {
   223     address pc0 = cur_insn();
   224     outputStream* st = output();
   225     if (_print_bytes && pc > pc0)
   226       print_insn_bytes(pc0, pc);
   227     if (_nm != NULL) {
   228       _nm->print_code_comment_on(st, COMMENT_COLUMN, pc0, pc);
   229       // this calls reloc_string_for which calls oop::print_value_on
   230     }
   232     // Output pc bucket ticks if we have any
   233     if (total_ticks() != 0) {
   234       address bucket_pc = FlatProfiler::bucket_start_for(pc);
   235       if (bucket_pc != NULL && bucket_pc > pc0 && bucket_pc <= pc) {
   236         int bucket_count = FlatProfiler::bucket_count_for(pc0);
   237         if (bucket_count != 0) {
   238           st->bol();
   239           st->print_cr("%3.1f%% [%d]", bucket_count*100.0/total_ticks(), bucket_count);
   240         }
   241       }
   242     }
   243     // follow each complete insn by a nice newline
   244     st->cr();
   245   }
   247   address handle_event(const char* event, address arg);
   249   outputStream* output() { return _output; }
   250   address cur_insn() { return _cur_insn; }
   251   int total_ticks() { return _total_ticks; }
   252   void set_total_ticks(int n) { _total_ticks = n; }
   253   const char* options() { return _option_buf; }
   254 };
   256 decode_env::decode_env(CodeBlob* code, outputStream* output, CodeStrings c) :
   257   _nm((code != NULL && code->is_nmethod()) ? (nmethod*)code : NULL),
   258   _code(code),
   259   _strings(),
   260   _output(output ? output : tty),
   261   _start(NULL),
   262   _end(NULL),
   263   _print_raw(0),
   264   // by default, output pc but not bytes:
   265   _print_pc(true),
   266   _print_bytes(false),
   267   _cur_insn(NULL),
   268   _total_ticks(0),
   269   _bytes_per_line(Disassembler::pd_instruction_alignment())
   270 {
   271   memset(_option_buf, 0, sizeof(_option_buf));
   272   _strings.copy(c);
   274   // parse the global option string:
   275   collect_options(Disassembler::pd_cpu_opts());
   276   collect_options(PrintAssemblyOptions);
   278   if (strstr(options(), "hsdis-")) {
   279     if (strstr(options(), "hsdis-print-raw"))
   280       _print_raw = (strstr(options(), "xml") ? 2 : 1);
   281     if (strstr(options(), "hsdis-print-pc"))
   282       _print_pc = !_print_pc;
   283     if (strstr(options(), "hsdis-print-bytes"))
   284       _print_bytes = !_print_bytes;
   285   }
   286   if (strstr(options(), "help")) {
   287     tty->print_cr("PrintAssemblyOptions help:");
   288     tty->print_cr("  hsdis-print-raw       test plugin by requesting raw output");
   289     tty->print_cr("  hsdis-print-raw-xml   test plugin by requesting raw xml");
   290     tty->print_cr("  hsdis-print-pc        turn off PC printing (on by default)");
   291     tty->print_cr("  hsdis-print-bytes     turn on instruction byte output");
   292     tty->print_cr("combined options: %s", options());
   293   }
   294 }
   296 address decode_env::handle_event(const char* event, address arg) {
   297   if (match(event, "insn")) {
   298     start_insn(arg);
   299   } else if (match(event, "/insn")) {
   300     end_insn(arg);
   301   } else if (match(event, "addr")) {
   302     if (arg != NULL) {
   303       print_address(arg);
   304       return arg;
   305     }
   306   } else if (match(event, "mach")) {
   307     static char buffer[32] = { 0, };
   308     if (strcmp(buffer, (const char*)arg) != 0 ||
   309         strlen((const char*)arg) > sizeof(buffer) - 1) {
   310       // Only print this when the mach changes
   311       strncpy(buffer, (const char*)arg, sizeof(buffer) - 1);
   312       buffer[sizeof(buffer) - 1] = '\0';
   313       output()->print_cr("[Disassembling for mach='%s']", arg);
   314     }
   315   } else if (match(event, "format bytes-per-line")) {
   316     _bytes_per_line = (int) (intptr_t) arg;
   317   } else {
   318     // ignore unrecognized markup
   319   }
   320   return NULL;
   321 }
   323 // called by the disassembler to print out jump targets and data addresses
   324 void decode_env::print_address(address adr) {
   325   outputStream* st = _output;
   327   if (adr == NULL) {
   328     st->print("NULL");
   329     return;
   330   }
   332   int small_num = (int)(intptr_t)adr;
   333   if ((intptr_t)adr == (intptr_t)small_num
   334       && -1 <= small_num && small_num <= 9) {
   335     st->print("%d", small_num);
   336     return;
   337   }
   339   if (Universe::is_fully_initialized()) {
   340     if (StubRoutines::contains(adr)) {
   341       StubCodeDesc* desc = StubCodeDesc::desc_for(adr);
   342       if (desc == NULL)
   343         desc = StubCodeDesc::desc_for(adr + frame::pc_return_offset);
   344       if (desc != NULL) {
   345         st->print("Stub::%s", desc->name());
   346         if (desc->begin() != adr)
   347           st->print("%+d 0x%p",adr - desc->begin(), adr);
   348         else if (WizardMode) st->print(" " PTR_FORMAT, adr);
   349         return;
   350       }
   351       st->print("Stub::<unknown> " PTR_FORMAT, adr);
   352       return;
   353     }
   355     BarrierSet* bs = Universe::heap()->barrier_set();
   356     if (bs->kind() == BarrierSet::CardTableModRef &&
   357         adr == (address)((CardTableModRefBS*)(bs))->byte_map_base) {
   358       st->print("word_map_base");
   359       if (WizardMode) st->print(" " INTPTR_FORMAT, (intptr_t)adr);
   360       return;
   361     }
   363     oop obj;
   364     if (_nm != NULL
   365         && (obj = _nm->embeddedOop_at(cur_insn())) != NULL
   366         && (address) obj == adr
   367         && Universe::heap()->is_in(obj)
   368         && Universe::heap()->is_in(obj->klass())) {
   369       julong c = st->count();
   370       obj->print_value_on(st);
   371       if (st->count() == c) {
   372         // No output.  (Can happen in product builds.)
   373         st->print("(a %s)", obj->klass()->external_name());
   374       }
   375       return;
   376     }
   377   }
   379   // Fall through to a simple (hexadecimal) numeral.
   380   st->print(PTR_FORMAT, adr);
   381 }
   383 void decode_env::print_insn_labels() {
   384   address p = cur_insn();
   385   outputStream* st = output();
   386   CodeBlob* cb = _code;
   387   if (cb != NULL) {
   388     cb->print_block_comment(st, p);
   389   }
   390   _strings.print_block_comment(st, (intptr_t)(p - _start));
   391   if (_print_pc) {
   392     st->print("  " PTR_FORMAT ": ", p);
   393   }
   394 }
   396 void decode_env::print_insn_bytes(address pc, address pc_limit) {
   397   outputStream* st = output();
   398   size_t incr = 1;
   399   size_t perline = _bytes_per_line;
   400   if ((size_t) Disassembler::pd_instruction_alignment() >= sizeof(int)
   401       && !((uintptr_t)pc % sizeof(int))
   402       && !((uintptr_t)pc_limit % sizeof(int))) {
   403     incr = sizeof(int);
   404     if (perline % incr)  perline += incr - (perline % incr);
   405   }
   406   while (pc < pc_limit) {
   407     // tab to the desired column:
   408     st->move_to(COMMENT_COLUMN);
   409     address pc0 = pc;
   410     address pc1 = pc + perline;
   411     if (pc1 > pc_limit)  pc1 = pc_limit;
   412     for (; pc < pc1; pc += incr) {
   413       if (pc == pc0)
   414         st->print(BYTES_COMMENT);
   415       else if ((uint)(pc - pc0) % sizeof(int) == 0)
   416         st->print(" ");         // put out a space on word boundaries
   417       if (incr == sizeof(int))
   418             st->print("%08lx", *(int*)pc);
   419       else  st->print("%02x",   (*pc)&0xFF);
   420     }
   421     st->cr();
   422   }
   423 }
   426 static void* event_to_env(void* env_pv, const char* event, void* arg) {
   427   decode_env* env = (decode_env*) env_pv;
   428   return env->handle_event(event, (address) arg);
   429 }
   431 ATTRIBUTE_PRINTF(2, 3)
   432 static int printf_to_env(void* env_pv, const char* format, ...) {
   433   decode_env* env = (decode_env*) env_pv;
   434   outputStream* st = env->output();
   435   size_t flen = strlen(format);
   436   const char* raw = NULL;
   437   if (flen == 0)  return 0;
   438   if (flen == 1 && format[0] == '\n') { st->bol(); return 1; }
   439   if (flen < 2 ||
   440       strchr(format, '%') == NULL) {
   441     raw = format;
   442   } else if (format[0] == '%' && format[1] == '%' &&
   443              strchr(format+2, '%') == NULL) {
   444     // happens a lot on machines with names like %foo
   445     flen--;
   446     raw = format+1;
   447   }
   448   if (raw != NULL) {
   449     st->print_raw(raw, (int) flen);
   450     return (int) flen;
   451   }
   452   va_list ap;
   453   va_start(ap, format);
   454   julong cnt0 = st->count();
   455   st->vprint(format, ap);
   456   julong cnt1 = st->count();
   457   va_end(ap);
   458   return (int)(cnt1 - cnt0);
   459 }
   461 address decode_env::decode_instructions(address start, address end) {
   462   _start = start; _end = end;
   464   assert(((((intptr_t)start | (intptr_t)end) % Disassembler::pd_instruction_alignment()) == 0), "misaligned insn addr");
   466   const int show_bytes = false; // for disassembler debugging
   468   //_version = Disassembler::pd_cpu_version();
   470   if (!Disassembler::can_decode()) {
   471     return NULL;
   472   }
   474   // decode a series of instructions and return the end of the last instruction
   476   if (_print_raw) {
   477     // Print whatever the library wants to print, w/o fancy callbacks.
   478     // This is mainly for debugging the library itself.
   479     FILE* out = stdout;
   480     FILE* xmlout = (_print_raw > 1 ? out : NULL);
   481     return use_new_version ?
   482       (address)
   483       (*Disassembler::_decode_instructions_virtual)((uintptr_t)start, (uintptr_t)end,
   484                                                     start, end - start,
   485                                                     NULL, (void*) xmlout,
   486                                                     NULL, (void*) out,
   487                                                     options(), 0/*nice new line*/)
   488       :
   489       (address)
   490       (*Disassembler::_decode_instructions)(start, end,
   491                                             NULL, (void*) xmlout,
   492                                             NULL, (void*) out,
   493                                             options());
   494   }
   496   return use_new_version ?
   497     (address)
   498     (*Disassembler::_decode_instructions_virtual)((uintptr_t)start, (uintptr_t)end,
   499                                                   start, end - start,
   500                                                   &event_to_env,  (void*) this,
   501                                                   &printf_to_env, (void*) this,
   502                                                   options(), 0/*nice new line*/)
   503     :
   504     (address)
   505     (*Disassembler::_decode_instructions)(start, end,
   506                                           &event_to_env,  (void*) this,
   507                                           &printf_to_env, (void*) this,
   508                                           options());
   509 }
   512 void Disassembler::decode(CodeBlob* cb, outputStream* st) {
   513   if (!load_library())  return;
   514   decode_env env(cb, st);
   515   env.output()->print_cr("Decoding CodeBlob " PTR_FORMAT, cb);
   516   env.decode_instructions(cb->code_begin(), cb->code_end());
   517 }
   519 void Disassembler::decode(address start, address end, outputStream* st, CodeStrings c) {
   520   if (!load_library())  return;
   521   decode_env env(CodeCache::find_blob_unsafe(start), st, c);
   522   env.decode_instructions(start, end);
   523 }
   525 void Disassembler::decode(nmethod* nm, outputStream* st) {
   526   if (!load_library())  return;
   527   decode_env env(nm, st);
   528   env.output()->print_cr("Decoding compiled method " PTR_FORMAT ":", nm);
   529   env.output()->print_cr("Code:");
   531 #ifdef SHARK
   532   SharkEntry* entry = (SharkEntry *) nm->code_begin();
   533   unsigned char* p   = entry->code_start();
   534   unsigned char* end = entry->code_limit();
   535 #else
   536   unsigned char* p   = nm->code_begin();
   537   unsigned char* end = nm->code_end();
   538 #endif // SHARK
   540   // If there has been profiling, print the buckets.
   541   if (FlatProfiler::bucket_start_for(p) != NULL) {
   542     unsigned char* p1 = p;
   543     int total_bucket_count = 0;
   544     while (p1 < end) {
   545       unsigned char* p0 = p1;
   546       p1 += pd_instruction_alignment();
   547       address bucket_pc = FlatProfiler::bucket_start_for(p1);
   548       if (bucket_pc != NULL && bucket_pc > p0 && bucket_pc <= p1)
   549         total_bucket_count += FlatProfiler::bucket_count_for(p0);
   550     }
   551     env.set_total_ticks(total_bucket_count);
   552   }
   554   // Print constant table.
   555   if (nm->consts_size() > 0) {
   556     nm->print_nmethod_labels(env.output(), nm->consts_begin());
   557     int offset = 0;
   558     for (address p = nm->consts_begin(); p < nm->consts_end(); p += 4, offset += 4) {
   559       if ((offset % 8) == 0) {
   560         env.output()->print_cr("  " PTR_FORMAT " (offset: %4d): " PTR32_FORMAT "   " PTR64_FORMAT, p, offset, *((int32_t*) p), *((int64_t*) p));
   561       } else {
   562         env.output()->print_cr("  " PTR_FORMAT " (offset: %4d): " PTR32_FORMAT,                    p, offset, *((int32_t*) p));
   563       }
   564     }
   565   }
   567   env.decode_instructions(p, end);
   568 }

mercurial