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