Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/compiler/disassembler.cpp
32285 views
/*1* Copyright (c) 2008, 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "classfile/javaClasses.hpp"26#include "code/codeCache.hpp"27#include "compiler/disassembler.hpp"28#include "gc_interface/collectedHeap.hpp"29#include "memory/cardTableModRefBS.hpp"30#include "runtime/fprofiler.hpp"31#include "runtime/handles.inline.hpp"32#include "runtime/stubCodeGenerator.hpp"33#include "runtime/stubRoutines.hpp"34#ifdef TARGET_ARCH_x8635# include "depChecker_x86.hpp"36#endif37#ifdef TARGET_ARCH_aarch3238# include "depChecker_aarch32.hpp"39#endif40#ifdef TARGET_ARCH_aarch6441# include "depChecker_aarch64.hpp"42#endif43#ifdef TARGET_ARCH_sparc44# include "depChecker_sparc.hpp"45#endif46#ifdef TARGET_ARCH_zero47# include "depChecker_zero.hpp"48#endif49#ifdef TARGET_ARCH_arm50# include "depChecker_arm.hpp"51#endif52#ifdef TARGET_ARCH_ppc53# include "depChecker_ppc.hpp"54#endif55#ifdef SHARK56#include "shark/sharkEntry.hpp"57#endif5859PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC6061void* Disassembler::_library = NULL;62bool Disassembler::_tried_to_load_library = false;6364// This routine is in the shared library:65Disassembler::decode_func_virtual Disassembler::_decode_instructions_virtual = NULL;66Disassembler::decode_func Disassembler::_decode_instructions = NULL;6768static const char hsdis_library_name[] = "hsdis-" HOTSPOT_LIB_ARCH;69static const char decode_instructions_virtual_name[] = "decode_instructions_virtual";70static const char decode_instructions_name[] = "decode_instructions";71static bool use_new_version = true;72#define COMMENT_COLUMN 40 LP64_ONLY(+8) /*could be an option*/73#define BYTES_COMMENT ";..." /* funky byte display comment */7475bool Disassembler::load_library() {76if (_decode_instructions_virtual != NULL || _decode_instructions != NULL) {77// Already succeeded.78return true;79}80if (_tried_to_load_library) {81// Do not try twice.82// To force retry in debugger: assign _tried_to_load_library=083return false;84}85// Try to load it.86char ebuf[1024];87char buf[JVM_MAXPATHLEN];88os::jvm_path(buf, sizeof(buf));89int jvm_offset = -1;90int lib_offset = -1;91{92// Match "jvm[^/]*" in jvm_path.93const char* base = buf;94const char* p = strrchr(buf, '/');95if (p != NULL) lib_offset = p - base + 1;96p = strstr(p ? p : base, "jvm");97if (p != NULL) jvm_offset = p - base;98}99// Find the disassembler shared library.100// Search for several paths derived from libjvm, in this order:101// 1. <home>/jre/lib/<arch>/<vm>/libhsdis-<arch>.so (for compatibility)102// 2. <home>/jre/lib/<arch>/<vm>/hsdis-<arch>.so103// 3. <home>/jre/lib/<arch>/hsdis-<arch>.so104// 4. hsdis-<arch>.so (using LD_LIBRARY_PATH)105if (jvm_offset >= 0) {106// 1. <home>/jre/lib/<arch>/<vm>/libhsdis-<arch>.so107strcpy(&buf[jvm_offset], hsdis_library_name);108strcat(&buf[jvm_offset], os::dll_file_extension());109_library = os::dll_load(buf, ebuf, sizeof ebuf);110if (_library == NULL) {111// 2. <home>/jre/lib/<arch>/<vm>/hsdis-<arch>.so112strcpy(&buf[lib_offset], hsdis_library_name);113strcat(&buf[lib_offset], os::dll_file_extension());114_library = os::dll_load(buf, ebuf, sizeof ebuf);115}116if (_library == NULL) {117// 3. <home>/jre/lib/<arch>/hsdis-<arch>.so118buf[lib_offset - 1] = '\0';119const char* p = strrchr(buf, '/');120if (p != NULL) {121lib_offset = p - buf + 1;122strcpy(&buf[lib_offset], hsdis_library_name);123strcat(&buf[lib_offset], os::dll_file_extension());124_library = os::dll_load(buf, ebuf, sizeof ebuf);125}126}127}128if (_library == NULL) {129// 4. hsdis-<arch>.so (using LD_LIBRARY_PATH)130strcpy(&buf[0], hsdis_library_name);131strcat(&buf[0], os::dll_file_extension());132_library = os::dll_load(buf, ebuf, sizeof ebuf);133}134if (_library != NULL) {135_decode_instructions_virtual = CAST_TO_FN_PTR(Disassembler::decode_func_virtual,136os::dll_lookup(_library, decode_instructions_virtual_name));137}138if (_decode_instructions_virtual == NULL) {139// could not spot in new version, try old version140_decode_instructions = CAST_TO_FN_PTR(Disassembler::decode_func,141os::dll_lookup(_library, decode_instructions_name));142use_new_version = false;143} else {144use_new_version = true;145}146_tried_to_load_library = true;147if (_decode_instructions_virtual == NULL && _decode_instructions == NULL) {148tty->print_cr("Could not load %s; %s; %s", buf,149((_library != NULL)150? "entry point is missing"151: (WizardMode || PrintMiscellaneous)152? (const char*)ebuf153: "library not loadable"),154"PrintAssembly is disabled");155return false;156}157158// Success.159tty->print_cr("Loaded disassembler from %s", buf);160return true;161}162163164class decode_env {165private:166nmethod* _nm;167CodeBlob* _code;168CodeStrings _strings;169outputStream* _output;170address _start, _end;171172char _option_buf[512];173char _print_raw;174bool _print_pc;175bool _print_bytes;176address _cur_insn;177int _total_ticks;178int _bytes_per_line; // arch-specific formatting option179180static bool match(const char* event, const char* tag) {181size_t taglen = strlen(tag);182if (strncmp(event, tag, taglen) != 0)183return false;184char delim = event[taglen];185return delim == '\0' || delim == ' ' || delim == '/' || delim == '=';186}187188void collect_options(const char* p) {189if (p == NULL || p[0] == '\0') return;190size_t opt_so_far = strlen(_option_buf);191if (opt_so_far + 1 + strlen(p) + 1 > sizeof(_option_buf)) return;192char* fillp = &_option_buf[opt_so_far];193if (opt_so_far > 0) *fillp++ = ',';194strcat(fillp, p);195// replace white space by commas:196char* q = fillp;197while ((q = strpbrk(q, " \t\n")) != NULL)198*q++ = ',';199// Note that multiple PrintAssemblyOptions flags accumulate with \n,200// which we want to be changed to a comma...201}202203void print_insn_labels();204void print_insn_bytes(address pc0, address pc);205void print_address(address value);206207public:208decode_env(CodeBlob* code, outputStream* output, CodeStrings c = CodeStrings());209210address decode_instructions(address start, address end);211212void start_insn(address pc) {213_cur_insn = pc;214output()->bol();215print_insn_labels();216}217218void end_insn(address pc) {219address pc0 = cur_insn();220outputStream* st = output();221if (_print_bytes && pc > pc0)222print_insn_bytes(pc0, pc);223if (_nm != NULL) {224_nm->print_code_comment_on(st, COMMENT_COLUMN, pc0, pc);225// this calls reloc_string_for which calls oop::print_value_on226}227228// Output pc bucket ticks if we have any229if (total_ticks() != 0) {230address bucket_pc = FlatProfiler::bucket_start_for(pc);231if (bucket_pc != NULL && bucket_pc > pc0 && bucket_pc <= pc) {232int bucket_count = FlatProfiler::bucket_count_for(pc0);233if (bucket_count != 0) {234st->bol();235st->print_cr("%3.1f%% [%d]", bucket_count*100.0/total_ticks(), bucket_count);236}237}238}239// follow each complete insn by a nice newline240st->cr();241}242243address handle_event(const char* event, address arg);244245outputStream* output() { return _output; }246address cur_insn() { return _cur_insn; }247int total_ticks() { return _total_ticks; }248void set_total_ticks(int n) { _total_ticks = n; }249const char* options() { return _option_buf; }250};251252decode_env::decode_env(CodeBlob* code, outputStream* output, CodeStrings c) :253_nm((code != NULL && code->is_nmethod()) ? (nmethod*)code : NULL),254_code(code),255_strings(),256_output(output ? output : tty),257_start(NULL),258_end(NULL),259_print_raw(0),260// by default, output pc but not bytes:261_print_pc(true),262_print_bytes(false),263_cur_insn(NULL),264_total_ticks(0),265_bytes_per_line(Disassembler::pd_instruction_alignment())266{267memset(_option_buf, 0, sizeof(_option_buf));268_strings.copy(c);269270// parse the global option string:271collect_options(Disassembler::pd_cpu_opts());272collect_options(PrintAssemblyOptions);273274if (strstr(options(), "hsdis-")) {275if (strstr(options(), "hsdis-print-raw"))276_print_raw = (strstr(options(), "xml") ? 2 : 1);277if (strstr(options(), "hsdis-print-pc"))278_print_pc = !_print_pc;279if (strstr(options(), "hsdis-print-bytes"))280_print_bytes = !_print_bytes;281}282if (strstr(options(), "help")) {283tty->print_cr("PrintAssemblyOptions help:");284tty->print_cr(" hsdis-print-raw test plugin by requesting raw output");285tty->print_cr(" hsdis-print-raw-xml test plugin by requesting raw xml");286tty->print_cr(" hsdis-print-pc turn off PC printing (on by default)");287tty->print_cr(" hsdis-print-bytes turn on instruction byte output");288tty->print_cr("combined options: %s", options());289}290}291292address decode_env::handle_event(const char* event, address arg) {293if (match(event, "insn")) {294start_insn(arg);295} else if (match(event, "/insn")) {296end_insn(arg);297} else if (match(event, "addr")) {298if (arg != NULL) {299print_address(arg);300return arg;301}302} else if (match(event, "mach")) {303static char buffer[32] = { 0, };304if (strcmp(buffer, (const char*)arg) != 0 ||305strlen((const char*)arg) > sizeof(buffer) - 1) {306// Only print this when the mach changes307strncpy(buffer, (const char*)arg, sizeof(buffer) - 1);308buffer[sizeof(buffer) - 1] = '\0';309output()->print_cr("[Disassembling for mach='%s']", arg);310}311} else if (match(event, "format bytes-per-line")) {312_bytes_per_line = (int) (intptr_t) arg;313} else {314// ignore unrecognized markup315}316return NULL;317}318319// called by the disassembler to print out jump targets and data addresses320void decode_env::print_address(address adr) {321outputStream* st = _output;322323if (adr == NULL) {324st->print("NULL");325return;326}327328int small_num = (int)(intptr_t)adr;329if ((intptr_t)adr == (intptr_t)small_num330&& -1 <= small_num && small_num <= 9) {331st->print("%d", small_num);332return;333}334335if (Universe::is_fully_initialized()) {336if (StubRoutines::contains(adr)) {337StubCodeDesc* desc = StubCodeDesc::desc_for(adr);338if (desc == NULL)339desc = StubCodeDesc::desc_for(adr + frame::pc_return_offset);340if (desc != NULL) {341st->print("Stub::%s", desc->name());342if (desc->begin() != adr)343st->print("%+d 0x%p",adr - desc->begin(), adr);344else if (WizardMode) st->print(" " PTR_FORMAT, adr);345return;346}347st->print("Stub::<unknown> " PTR_FORMAT, adr);348return;349}350351BarrierSet* bs = Universe::heap()->barrier_set();352if (bs->kind() == BarrierSet::CardTableModRef &&353adr == (address)((CardTableModRefBS*)(bs))->byte_map_base) {354st->print("word_map_base");355if (WizardMode) st->print(" " INTPTR_FORMAT, (intptr_t)adr);356return;357}358359oop obj;360if (_nm != NULL361&& (obj = _nm->embeddedOop_at(cur_insn())) != NULL362&& (address) obj == adr363&& Universe::heap()->is_in(obj)364&& Universe::heap()->is_in(obj->klass())) {365julong c = st->count();366obj->print_value_on(st);367if (st->count() == c) {368// No output. (Can happen in product builds.)369st->print("(a %s)", obj->klass()->external_name());370}371return;372}373}374375// Fall through to a simple (hexadecimal) numeral.376st->print(PTR_FORMAT, adr);377}378379void decode_env::print_insn_labels() {380address p = cur_insn();381outputStream* st = output();382CodeBlob* cb = _code;383if (cb != NULL) {384cb->print_block_comment(st, p);385}386_strings.print_block_comment(st, (intptr_t)(p - _start));387if (_print_pc) {388st->print(" " PTR_FORMAT ": ", p);389}390}391392void decode_env::print_insn_bytes(address pc, address pc_limit) {393outputStream* st = output();394size_t incr = 1;395size_t perline = _bytes_per_line;396if ((size_t) Disassembler::pd_instruction_alignment() >= sizeof(int)397&& !((uintptr_t)pc % sizeof(int))398&& !((uintptr_t)pc_limit % sizeof(int))) {399incr = sizeof(int);400if (perline % incr) perline += incr - (perline % incr);401}402while (pc < pc_limit) {403// tab to the desired column:404st->move_to(COMMENT_COLUMN);405address pc0 = pc;406address pc1 = pc + perline;407if (pc1 > pc_limit) pc1 = pc_limit;408for (; pc < pc1; pc += incr) {409if (pc == pc0)410st->print(BYTES_COMMENT);411else if ((uint)(pc - pc0) % sizeof(int) == 0)412st->print(" "); // put out a space on word boundaries413if (incr == sizeof(int))414st->print("%08lx", *(int*)pc);415else st->print("%02x", (*pc)&0xFF);416}417st->cr();418}419}420421422static void* event_to_env(void* env_pv, const char* event, void* arg) {423decode_env* env = (decode_env*) env_pv;424return env->handle_event(event, (address) arg);425}426427ATTRIBUTE_PRINTF(2, 3)428static int printf_to_env(void* env_pv, const char* format, ...) {429decode_env* env = (decode_env*) env_pv;430outputStream* st = env->output();431size_t flen = strlen(format);432const char* raw = NULL;433if (flen == 0) return 0;434if (flen == 1 && format[0] == '\n') { st->bol(); return 1; }435if (flen < 2 ||436strchr(format, '%') == NULL) {437raw = format;438} else if (format[0] == '%' && format[1] == '%' &&439strchr(format+2, '%') == NULL) {440// happens a lot on machines with names like %foo441flen--;442raw = format+1;443}444if (raw != NULL) {445st->print_raw(raw, (int) flen);446return (int) flen;447}448va_list ap;449va_start(ap, format);450julong cnt0 = st->count();451st->vprint(format, ap);452julong cnt1 = st->count();453va_end(ap);454return (int)(cnt1 - cnt0);455}456457address decode_env::decode_instructions(address start, address end) {458_start = start; _end = end;459460assert(((((intptr_t)start | (intptr_t)end) % Disassembler::pd_instruction_alignment()) == 0), "misaligned insn addr");461462const int show_bytes = false; // for disassembler debugging463464//_version = Disassembler::pd_cpu_version();465466if (!Disassembler::can_decode()) {467return NULL;468}469470// decode a series of instructions and return the end of the last instruction471472if (_print_raw) {473// Print whatever the library wants to print, w/o fancy callbacks.474// This is mainly for debugging the library itself.475FILE* out = stdout;476FILE* xmlout = (_print_raw > 1 ? out : NULL);477return use_new_version ?478(address)479(*Disassembler::_decode_instructions_virtual)((uintptr_t)start, (uintptr_t)end,480start, end - start,481NULL, (void*) xmlout,482NULL, (void*) out,483options(), 0/*nice new line*/)484:485(address)486(*Disassembler::_decode_instructions)(start, end,487NULL, (void*) xmlout,488NULL, (void*) out,489options());490}491492return use_new_version ?493(address)494(*Disassembler::_decode_instructions_virtual)((uintptr_t)start, (uintptr_t)end,495start, end - start,496&event_to_env, (void*) this,497&printf_to_env, (void*) this,498options(), 0/*nice new line*/)499:500(address)501(*Disassembler::_decode_instructions)(start, end,502&event_to_env, (void*) this,503&printf_to_env, (void*) this,504options());505}506507508void Disassembler::decode(CodeBlob* cb, outputStream* st) {509ttyLocker ttyl;510if (!load_library()) return;511decode_env env(cb, st);512env.output()->print_cr("Decoding CodeBlob " PTR_FORMAT, cb);513env.decode_instructions(cb->code_begin(), cb->code_end());514}515516void Disassembler::decode(address start, address end, outputStream* st, CodeStrings c) {517ttyLocker ttyl;518if (!load_library()) return;519decode_env env(CodeCache::find_blob_unsafe(start), st, c);520env.decode_instructions(start, end);521}522523void Disassembler::decode(nmethod* nm, outputStream* st) {524ttyLocker ttyl;525if (!load_library()) return;526decode_env env(nm, st);527env.output()->print_cr("Decoding compiled method " PTR_FORMAT ":", nm);528env.output()->print_cr("Code:");529530#ifdef SHARK531SharkEntry* entry = (SharkEntry *) nm->code_begin();532unsigned char* p = entry->code_start();533unsigned char* end = entry->code_limit();534#else535unsigned char* p = nm->code_begin();536unsigned char* end = nm->code_end();537#endif // SHARK538539// If there has been profiling, print the buckets.540if (FlatProfiler::bucket_start_for(p) != NULL) {541unsigned char* p1 = p;542int total_bucket_count = 0;543while (p1 < end) {544unsigned char* p0 = p1;545p1 += pd_instruction_alignment();546address bucket_pc = FlatProfiler::bucket_start_for(p1);547if (bucket_pc != NULL && bucket_pc > p0 && bucket_pc <= p1)548total_bucket_count += FlatProfiler::bucket_count_for(p0);549}550env.set_total_ticks(total_bucket_count);551}552553// Print constant table.554if (nm->consts_size() > 0) {555nm->print_nmethod_labels(env.output(), nm->consts_begin());556int offset = 0;557for (address p = nm->consts_begin(); p < nm->consts_end(); p += 4, offset += 4) {558if ((offset % 8) == 0) {559env.output()->print_cr(" " PTR_FORMAT " (offset: %4d): " PTR32_FORMAT " " PTR64_FORMAT, p, offset, *((int32_t*) p), *((int64_t*) p));560} else {561env.output()->print_cr(" " PTR_FORMAT " (offset: %4d): " PTR32_FORMAT, p, offset, *((int32_t*) p));562}563}564}565566env.decode_instructions(p, end);567}568569570