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