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