jrose@535: /* twisti@2047: * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved. jrose@535: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jrose@535: * jrose@535: * This code is free software; you can redistribute it and/or modify it jrose@535: * under the terms of the GNU General Public License version 2 only, as jrose@535: * published by the Free Software Foundation. jrose@535: * jrose@535: * This code is distributed in the hope that it will be useful, but WITHOUT jrose@535: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jrose@535: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jrose@535: * version 2 for more details (a copy is included in the LICENSE file that jrose@535: * accompanied this code). jrose@535: * jrose@535: * You should have received a copy of the GNU General Public License version jrose@535: * 2 along with this work; if not, write to the Free Software Foundation, jrose@535: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jrose@535: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. jrose@535: * jrose@535: */ jrose@535: stefank@2314: #include "precompiled.hpp" stefank@2314: #include "classfile/javaClasses.hpp" stefank@2314: #include "code/codeCache.hpp" stefank@2314: #include "compiler/disassembler.hpp" stefank@2314: #include "gc_interface/collectedHeap.hpp" stefank@2314: #include "memory/cardTableModRefBS.hpp" stefank@2314: #include "runtime/fprofiler.hpp" stefank@2314: #include "runtime/handles.inline.hpp" stefank@2314: #include "runtime/stubCodeGenerator.hpp" stefank@2314: #include "runtime/stubRoutines.hpp" stefank@2314: #ifdef TARGET_ARCH_x86 stefank@2314: # include "depChecker_x86.hpp" stefank@2314: #endif stefank@2314: #ifdef TARGET_ARCH_sparc stefank@2314: # include "depChecker_sparc.hpp" stefank@2314: #endif stefank@2314: #ifdef TARGET_ARCH_zero stefank@2314: # include "depChecker_zero.hpp" stefank@2314: #endif bobv@2508: #ifdef TARGET_ARCH_arm bobv@2508: # include "depChecker_arm.hpp" bobv@2508: #endif bobv@2508: #ifdef TARGET_ARCH_ppc bobv@2508: # include "depChecker_ppc.hpp" bobv@2508: #endif stefank@2314: #ifdef SHARK stefank@2314: #include "shark/sharkEntry.hpp" stefank@2314: #endif jrose@535: jrose@535: void* Disassembler::_library = NULL; jrose@535: bool Disassembler::_tried_to_load_library = false; jrose@535: jrose@535: // This routine is in the shared library: jrose@535: Disassembler::decode_func Disassembler::_decode_instructions = NULL; jrose@535: jrose@535: static const char hsdis_library_name[] = "hsdis-"HOTSPOT_LIB_ARCH; jrose@535: static const char decode_instructions_name[] = "decode_instructions"; jrose@535: jrose@535: #define COMMENT_COLUMN 40 LP64_ONLY(+8) /*could be an option*/ jrose@535: #define BYTES_COMMENT ";..." /* funky byte display comment */ jrose@535: jrose@535: bool Disassembler::load_library() { jrose@535: if (_decode_instructions != NULL) { jrose@535: // Already succeeded. jrose@535: return true; jrose@535: } jrose@535: if (_tried_to_load_library) { jrose@535: // Do not try twice. jrose@535: // To force retry in debugger: assign _tried_to_load_library=0 jrose@535: return false; jrose@535: } jrose@535: // Try to load it. jrose@535: char ebuf[1024]; jrose@535: char buf[JVM_MAXPATHLEN]; jrose@535: os::jvm_path(buf, sizeof(buf)); jrose@535: int jvm_offset = -1; jrose@535: { jrose@535: // Match "jvm[^/]*" in jvm_path. jrose@535: const char* base = buf; jrose@535: const char* p = strrchr(buf, '/'); jrose@535: p = strstr(p ? p : base, "jvm"); jrose@535: if (p != NULL) jvm_offset = p - base; jrose@535: } jrose@535: if (jvm_offset >= 0) { jrose@535: // Find the disassembler next to libjvm.so. jrose@535: strcpy(&buf[jvm_offset], hsdis_library_name); jrose@535: strcat(&buf[jvm_offset], os::dll_file_extension()); ikrylov@2322: _library = os::dll_load(buf, ebuf, sizeof ebuf); jrose@535: } jrose@535: if (_library == NULL) { jrose@535: // Try a free-floating lookup. jrose@535: strcpy(&buf[0], hsdis_library_name); jrose@535: strcat(&buf[0], os::dll_file_extension()); ikrylov@2322: _library = os::dll_load(buf, ebuf, sizeof ebuf); jrose@535: } jrose@535: if (_library != NULL) { jrose@535: _decode_instructions = CAST_TO_FN_PTR(Disassembler::decode_func, ikrylov@2322: os::dll_lookup(_library, decode_instructions_name)); jrose@535: } jrose@535: _tried_to_load_library = true; jrose@535: if (_decode_instructions == NULL) { jrose@535: tty->print_cr("Could not load %s; %s; %s", buf, jrose@535: ((_library != NULL) jrose@535: ? "entry point is missing" jrose@535: : (WizardMode || PrintMiscellaneous) jrose@535: ? (const char*)ebuf jrose@535: : "library not loadable"), jrose@535: "PrintAssembly is disabled"); jrose@535: return false; jrose@535: } jrose@535: jrose@535: // Success. jrose@535: tty->print_cr("Loaded disassembler from %s", buf); jrose@535: return true; jrose@535: } jrose@535: jrose@535: jrose@535: class decode_env { jrose@535: private: jrose@535: nmethod* _nm; jrose@535: CodeBlob* _code; jrose@535: outputStream* _output; jrose@535: address _start, _end; jrose@535: jrose@535: char _option_buf[512]; jrose@535: char _print_raw; jrose@535: bool _print_pc; jrose@535: bool _print_bytes; jrose@535: address _cur_insn; jrose@535: int _total_ticks; jrose@535: int _bytes_per_line; // arch-specific formatting option jrose@535: jrose@535: static bool match(const char* event, const char* tag) { jrose@535: size_t taglen = strlen(tag); jrose@535: if (strncmp(event, tag, taglen) != 0) jrose@535: return false; jrose@535: char delim = event[taglen]; jrose@535: return delim == '\0' || delim == ' ' || delim == '/' || delim == '='; jrose@535: } jrose@535: jrose@535: void collect_options(const char* p) { jrose@535: if (p == NULL || p[0] == '\0') return; jrose@535: size_t opt_so_far = strlen(_option_buf); jrose@535: if (opt_so_far + 1 + strlen(p) + 1 > sizeof(_option_buf)) return; jrose@535: char* fillp = &_option_buf[opt_so_far]; jrose@535: if (opt_so_far > 0) *fillp++ = ','; jrose@535: strcat(fillp, p); jrose@535: // replace white space by commas: jrose@535: char* q = fillp; jrose@535: while ((q = strpbrk(q, " \t\n")) != NULL) jrose@535: *q++ = ','; jrose@535: // Note that multiple PrintAssemblyOptions flags accumulate with \n, jrose@535: // which we want to be changed to a comma... jrose@535: } jrose@535: jrose@535: void print_insn_labels(); jrose@535: void print_insn_bytes(address pc0, address pc); jrose@535: void print_address(address value); jrose@535: jrose@535: public: jrose@535: decode_env(CodeBlob* code, outputStream* output); jrose@535: jrose@535: address decode_instructions(address start, address end); jrose@535: jrose@535: void start_insn(address pc) { jrose@535: _cur_insn = pc; jrose@535: output()->bol(); jrose@535: print_insn_labels(); jrose@535: } jrose@535: jrose@535: void end_insn(address pc) { jrose@535: address pc0 = cur_insn(); jrose@535: outputStream* st = output(); jrose@535: if (_print_bytes && pc > pc0) jrose@535: print_insn_bytes(pc0, pc); jrose@1590: if (_nm != NULL) { jrose@535: _nm->print_code_comment_on(st, COMMENT_COLUMN, pc0, pc); jrose@1590: // this calls reloc_string_for which calls oop::print_value_on jrose@1590: } jrose@535: jrose@535: // Output pc bucket ticks if we have any jrose@535: if (total_ticks() != 0) { jrose@535: address bucket_pc = FlatProfiler::bucket_start_for(pc); jrose@535: if (bucket_pc != NULL && bucket_pc > pc0 && bucket_pc <= pc) { jrose@535: int bucket_count = FlatProfiler::bucket_count_for(pc0); jrose@535: if (bucket_count != 0) { jrose@535: st->bol(); jrose@535: st->print_cr("%3.1f%% [%d]", bucket_count*100.0/total_ticks(), bucket_count); jrose@535: } jrose@535: } jrose@535: } jrose@535: } jrose@535: jrose@535: address handle_event(const char* event, address arg); jrose@535: jrose@535: outputStream* output() { return _output; } jrose@535: address cur_insn() { return _cur_insn; } jrose@535: int total_ticks() { return _total_ticks; } jrose@535: void set_total_ticks(int n) { _total_ticks = n; } jrose@535: const char* options() { return _option_buf; } jrose@535: }; jrose@535: jrose@535: decode_env::decode_env(CodeBlob* code, outputStream* output) { jrose@535: memset(this, 0, sizeof(*this)); jrose@535: _output = output ? output : tty; jrose@535: _code = code; jrose@535: if (code != NULL && code->is_nmethod()) jrose@535: _nm = (nmethod*) code; jrose@535: jrose@535: // by default, output pc but not bytes: jrose@535: _print_pc = true; jrose@535: _print_bytes = false; jrose@535: _bytes_per_line = Disassembler::pd_instruction_alignment(); jrose@535: jrose@535: // parse the global option string: jrose@535: collect_options(Disassembler::pd_cpu_opts()); jrose@535: collect_options(PrintAssemblyOptions); jrose@535: jrose@535: if (strstr(options(), "hsdis-")) { jrose@535: if (strstr(options(), "hsdis-print-raw")) jrose@535: _print_raw = (strstr(options(), "xml") ? 2 : 1); jrose@535: if (strstr(options(), "hsdis-print-pc")) jrose@535: _print_pc = !_print_pc; jrose@535: if (strstr(options(), "hsdis-print-bytes")) jrose@535: _print_bytes = !_print_bytes; jrose@535: } jrose@535: if (strstr(options(), "help")) { jrose@535: tty->print_cr("PrintAssemblyOptions help:"); jrose@535: tty->print_cr(" hsdis-print-raw test plugin by requesting raw output"); jrose@535: tty->print_cr(" hsdis-print-raw-xml test plugin by requesting raw xml"); jrose@535: tty->print_cr(" hsdis-print-pc turn off PC printing (on by default)"); jrose@535: tty->print_cr(" hsdis-print-bytes turn on instruction byte output"); jrose@535: tty->print_cr("combined options: %s", options()); jrose@535: } jrose@535: } jrose@535: jrose@535: address decode_env::handle_event(const char* event, address arg) { jrose@535: if (match(event, "insn")) { jrose@535: start_insn(arg); jrose@535: } else if (match(event, "/insn")) { jrose@535: end_insn(arg); jrose@535: } else if (match(event, "addr")) { jrose@535: if (arg != NULL) { jrose@535: print_address(arg); jrose@535: return arg; jrose@535: } jrose@535: } else if (match(event, "mach")) { jrose@535: output()->print_cr("[Disassembling for mach='%s']", arg); jrose@535: } else if (match(event, "format bytes-per-line")) { jrose@535: _bytes_per_line = (int) (intptr_t) arg; jrose@535: } else { jrose@535: // ignore unrecognized markup jrose@535: } jrose@535: return NULL; jrose@535: } jrose@535: jrose@535: // called by the disassembler to print out jump targets and data addresses jrose@535: void decode_env::print_address(address adr) { jrose@535: outputStream* st = _output; jrose@535: jrose@535: if (adr == NULL) { jrose@535: st->print("NULL"); jrose@535: return; jrose@535: } jrose@535: jrose@535: int small_num = (int)(intptr_t)adr; jrose@535: if ((intptr_t)adr == (intptr_t)small_num jrose@535: && -1 <= small_num && small_num <= 9) { jrose@535: st->print("%d", small_num); jrose@535: return; jrose@535: } jrose@535: jrose@535: if (Universe::is_fully_initialized()) { jrose@535: if (StubRoutines::contains(adr)) { jrose@535: StubCodeDesc* desc = StubCodeDesc::desc_for(adr); jrose@535: if (desc == NULL) jrose@535: desc = StubCodeDesc::desc_for(adr + frame::pc_return_offset); jrose@535: if (desc != NULL) { jrose@535: st->print("Stub::%s", desc->name()); jrose@535: if (desc->begin() != adr) jrose@535: st->print("%+d 0x%p",adr - desc->begin(), adr); jrose@535: else if (WizardMode) st->print(" " INTPTR_FORMAT, adr); jrose@535: return; jrose@535: } jrose@535: st->print("Stub:: " INTPTR_FORMAT, adr); jrose@535: return; jrose@535: } jrose@535: jrose@535: BarrierSet* bs = Universe::heap()->barrier_set(); jrose@535: if (bs->kind() == BarrierSet::CardTableModRef && jrose@535: adr == (address)((CardTableModRefBS*)(bs))->byte_map_base) { jrose@535: st->print("word_map_base"); jrose@535: if (WizardMode) st->print(" " INTPTR_FORMAT, (intptr_t)adr); jrose@535: return; jrose@535: } jrose@535: jrose@535: oop obj; jrose@535: if (_nm != NULL jrose@535: && (obj = _nm->embeddedOop_at(cur_insn())) != NULL jrose@1590: && (address) obj == adr jrose@1590: && Universe::heap()->is_in(obj) jrose@1590: && Universe::heap()->is_in(obj->klass())) { jrose@1590: julong c = st->count(); jrose@535: obj->print_value_on(st); jrose@1590: if (st->count() == c) { jrose@1590: // No output. (Can happen in product builds.) jrose@1590: st->print("(a %s)", Klass::cast(obj->klass())->external_name()); jrose@1590: } jrose@535: return; jrose@535: } jrose@535: } jrose@535: jrose@535: // Fall through to a simple numeral. jrose@535: st->print(INTPTR_FORMAT, (intptr_t)adr); jrose@535: } jrose@535: jrose@535: void decode_env::print_insn_labels() { jrose@535: address p = cur_insn(); jrose@535: outputStream* st = output(); jrose@535: CodeBlob* cb = _code; jrose@535: if (cb != NULL) { jrose@1590: cb->print_block_comment(st, p); jrose@535: } jrose@535: if (_print_pc) { jrose@535: st->print(" " INTPTR_FORMAT ": ", (intptr_t) p); jrose@535: } jrose@535: } jrose@535: jrose@535: void decode_env::print_insn_bytes(address pc, address pc_limit) { jrose@535: outputStream* st = output(); jrose@535: size_t incr = 1; jrose@535: size_t perline = _bytes_per_line; jrose@535: if ((size_t) Disassembler::pd_instruction_alignment() >= sizeof(int) jrose@535: && !((uintptr_t)pc % sizeof(int)) jrose@535: && !((uintptr_t)pc_limit % sizeof(int))) { jrose@535: incr = sizeof(int); jrose@535: if (perline % incr) perline += incr - (perline % incr); jrose@535: } jrose@535: while (pc < pc_limit) { jrose@535: // tab to the desired column: jrose@535: st->move_to(COMMENT_COLUMN); jrose@535: address pc0 = pc; jrose@535: address pc1 = pc + perline; jrose@535: if (pc1 > pc_limit) pc1 = pc_limit; jrose@535: for (; pc < pc1; pc += incr) { jrose@535: if (pc == pc0) jrose@535: st->print(BYTES_COMMENT); jrose@535: else if ((uint)(pc - pc0) % sizeof(int) == 0) jrose@535: st->print(" "); // put out a space on word boundaries jrose@535: if (incr == sizeof(int)) jrose@535: st->print("%08lx", *(int*)pc); jrose@535: else st->print("%02x", (*pc)&0xFF); jrose@535: } jrose@535: st->cr(); jrose@535: } jrose@535: } jrose@535: jrose@535: jrose@535: static void* event_to_env(void* env_pv, const char* event, void* arg) { jrose@535: decode_env* env = (decode_env*) env_pv; jrose@535: return env->handle_event(event, (address) arg); jrose@535: } jrose@535: jrose@535: static int printf_to_env(void* env_pv, const char* format, ...) { jrose@535: decode_env* env = (decode_env*) env_pv; jrose@535: outputStream* st = env->output(); jrose@535: size_t flen = strlen(format); jrose@535: const char* raw = NULL; jrose@535: if (flen == 0) return 0; jrose@535: if (flen == 1 && format[0] == '\n') { st->bol(); return 1; } jrose@535: if (flen < 2 || jrose@535: strchr(format, '%') == NULL) { jrose@535: raw = format; jrose@535: } else if (format[0] == '%' && format[1] == '%' && jrose@535: strchr(format+2, '%') == NULL) { jrose@535: // happens a lot on machines with names like %foo jrose@535: flen--; jrose@535: raw = format+1; jrose@535: } jrose@535: if (raw != NULL) { jrose@535: st->print_raw(raw, (int) flen); jrose@535: return (int) flen; jrose@535: } jrose@535: va_list ap; jrose@535: va_start(ap, format); jrose@535: julong cnt0 = st->count(); jrose@535: st->vprint(format, ap); jrose@535: julong cnt1 = st->count(); jrose@535: va_end(ap); jrose@535: return (int)(cnt1 - cnt0); jrose@535: } jrose@535: jrose@535: address decode_env::decode_instructions(address start, address end) { jrose@535: _start = start; _end = end; jrose@535: bobv@2036: assert(((((intptr_t)start | (intptr_t)end) % Disassembler::pd_instruction_alignment()) == 0), "misaligned insn addr"); jrose@535: jrose@535: const int show_bytes = false; // for disassembler debugging jrose@535: jrose@535: //_version = Disassembler::pd_cpu_version(); jrose@535: jrose@535: if (!Disassembler::can_decode()) { jrose@535: return NULL; jrose@535: } jrose@535: jrose@535: // decode a series of instructions and return the end of the last instruction jrose@535: jrose@535: if (_print_raw) { jrose@535: // Print whatever the library wants to print, w/o fancy callbacks. jrose@535: // This is mainly for debugging the library itself. jrose@535: FILE* out = stdout; jrose@535: FILE* xmlout = (_print_raw > 1 ? out : NULL); jrose@535: return (address) jrose@535: (*Disassembler::_decode_instructions)(start, end, jrose@535: NULL, (void*) xmlout, jrose@535: NULL, (void*) out, jrose@535: options()); jrose@535: } jrose@535: jrose@535: return (address) jrose@535: (*Disassembler::_decode_instructions)(start, end, jrose@535: &event_to_env, (void*) this, jrose@535: &printf_to_env, (void*) this, jrose@535: options()); jrose@535: } jrose@535: jrose@535: jrose@535: void Disassembler::decode(CodeBlob* cb, outputStream* st) { jrose@535: if (!load_library()) return; jrose@535: decode_env env(cb, st); jrose@535: env.output()->print_cr("Decoding CodeBlob " INTPTR_FORMAT, cb); twisti@2103: env.decode_instructions(cb->code_begin(), cb->code_end()); jrose@535: } jrose@535: jrose@535: jrose@535: void Disassembler::decode(address start, address end, outputStream* st) { jrose@535: if (!load_library()) return; jrose@535: decode_env env(CodeCache::find_blob_unsafe(start), st); jrose@535: env.decode_instructions(start, end); jrose@535: } jrose@535: jrose@535: void Disassembler::decode(nmethod* nm, outputStream* st) { jrose@535: if (!load_library()) return; jrose@535: decode_env env(nm, st); jrose@535: env.output()->print_cr("Decoding compiled method " INTPTR_FORMAT ":", nm); jrose@535: env.output()->print_cr("Code:"); jrose@535: twisti@2047: #ifdef SHARK twisti@2103: SharkEntry* entry = (SharkEntry *) nm->code_begin(); twisti@2103: unsigned char* p = entry->code_start(); twisti@2047: unsigned char* end = entry->code_limit(); twisti@2047: #else twisti@2103: unsigned char* p = nm->code_begin(); twisti@2103: unsigned char* end = nm->code_end(); twisti@2047: #endif // SHARK jrose@535: jrose@535: // If there has been profiling, print the buckets. jrose@535: if (FlatProfiler::bucket_start_for(p) != NULL) { jrose@535: unsigned char* p1 = p; jrose@535: int total_bucket_count = 0; jrose@535: while (p1 < end) { jrose@535: unsigned char* p0 = p1; jrose@535: p1 += pd_instruction_alignment(); jrose@535: address bucket_pc = FlatProfiler::bucket_start_for(p1); jrose@535: if (bucket_pc != NULL && bucket_pc > p0 && bucket_pc <= p1) jrose@535: total_bucket_count += FlatProfiler::bucket_count_for(p0); jrose@535: } jrose@535: env.set_total_ticks(total_bucket_count); jrose@535: } jrose@535: twisti@2350: // Print constant table. twisti@2350: if (nm->consts_size() > 0) { twisti@2350: nm->print_nmethod_labels(env.output(), nm->consts_begin()); twisti@2350: int offset = 0; twisti@2350: for (address p = nm->consts_begin(); p < nm->consts_end(); p += 4, offset += 4) { twisti@2350: if ((offset % 8) == 0) { twisti@2350: env.output()->print_cr(" " INTPTR_FORMAT " (offset: %4d): " PTR32_FORMAT " " PTR64_FORMAT, (intptr_t) p, offset, *((int32_t*) p), *((int64_t*) p)); twisti@2350: } else { twisti@2350: env.output()->print_cr(" " INTPTR_FORMAT " (offset: %4d): " PTR32_FORMAT, (intptr_t) p, offset, *((int32_t*) p)); twisti@2350: } twisti@2350: } twisti@2350: } twisti@2350: jrose@535: env.decode_instructions(p, end); jrose@535: }