1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/compiler/disassembler.cpp Wed Apr 02 12:09:59 2008 -0700 1.3 @@ -0,0 +1,443 @@ 1.4 +/* 1.5 + * Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or 1.24 + * have any questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +# include "incls/_precompiled.incl" 1.29 +# include "incls/_disassembler.cpp.incl" 1.30 + 1.31 +void* Disassembler::_library = NULL; 1.32 +bool Disassembler::_tried_to_load_library = false; 1.33 + 1.34 +// This routine is in the shared library: 1.35 +Disassembler::decode_func Disassembler::_decode_instructions = NULL; 1.36 + 1.37 +static const char hsdis_library_name[] = "hsdis-"HOTSPOT_LIB_ARCH; 1.38 +static const char decode_instructions_name[] = "decode_instructions"; 1.39 + 1.40 +#define COMMENT_COLUMN 40 LP64_ONLY(+8) /*could be an option*/ 1.41 +#define BYTES_COMMENT ";..." /* funky byte display comment */ 1.42 + 1.43 +bool Disassembler::load_library() { 1.44 + if (_decode_instructions != NULL) { 1.45 + // Already succeeded. 1.46 + return true; 1.47 + } 1.48 + if (_tried_to_load_library) { 1.49 + // Do not try twice. 1.50 + // To force retry in debugger: assign _tried_to_load_library=0 1.51 + return false; 1.52 + } 1.53 + // Try to load it. 1.54 + char ebuf[1024]; 1.55 + char buf[JVM_MAXPATHLEN]; 1.56 + os::jvm_path(buf, sizeof(buf)); 1.57 + int jvm_offset = -1; 1.58 + { 1.59 + // Match "jvm[^/]*" in jvm_path. 1.60 + const char* base = buf; 1.61 + const char* p = strrchr(buf, '/'); 1.62 + p = strstr(p ? p : base, "jvm"); 1.63 + if (p != NULL) jvm_offset = p - base; 1.64 + } 1.65 + if (jvm_offset >= 0) { 1.66 + // Find the disassembler next to libjvm.so. 1.67 + strcpy(&buf[jvm_offset], hsdis_library_name); 1.68 + strcat(&buf[jvm_offset], os::dll_file_extension()); 1.69 + _library = hpi::dll_load(buf, ebuf, sizeof ebuf); 1.70 + } 1.71 + if (_library == NULL) { 1.72 + // Try a free-floating lookup. 1.73 + strcpy(&buf[0], hsdis_library_name); 1.74 + strcat(&buf[0], os::dll_file_extension()); 1.75 + _library = hpi::dll_load(buf, ebuf, sizeof ebuf); 1.76 + } 1.77 + if (_library != NULL) { 1.78 + _decode_instructions = CAST_TO_FN_PTR(Disassembler::decode_func, 1.79 + hpi::dll_lookup(_library, decode_instructions_name)); 1.80 + } 1.81 + _tried_to_load_library = true; 1.82 + if (_decode_instructions == NULL) { 1.83 + tty->print_cr("Could not load %s; %s; %s", buf, 1.84 + ((_library != NULL) 1.85 + ? "entry point is missing" 1.86 + : (WizardMode || PrintMiscellaneous) 1.87 + ? (const char*)ebuf 1.88 + : "library not loadable"), 1.89 + "PrintAssembly is disabled"); 1.90 + return false; 1.91 + } 1.92 + 1.93 + // Success. 1.94 + tty->print_cr("Loaded disassembler from %s", buf); 1.95 + return true; 1.96 +} 1.97 + 1.98 + 1.99 +class decode_env { 1.100 + private: 1.101 + nmethod* _nm; 1.102 + CodeBlob* _code; 1.103 + outputStream* _output; 1.104 + address _start, _end; 1.105 + 1.106 + char _option_buf[512]; 1.107 + char _print_raw; 1.108 + bool _print_pc; 1.109 + bool _print_bytes; 1.110 + address _cur_insn; 1.111 + int _total_ticks; 1.112 + int _bytes_per_line; // arch-specific formatting option 1.113 + 1.114 + static bool match(const char* event, const char* tag) { 1.115 + size_t taglen = strlen(tag); 1.116 + if (strncmp(event, tag, taglen) != 0) 1.117 + return false; 1.118 + char delim = event[taglen]; 1.119 + return delim == '\0' || delim == ' ' || delim == '/' || delim == '='; 1.120 + } 1.121 + 1.122 + void collect_options(const char* p) { 1.123 + if (p == NULL || p[0] == '\0') return; 1.124 + size_t opt_so_far = strlen(_option_buf); 1.125 + if (opt_so_far + 1 + strlen(p) + 1 > sizeof(_option_buf)) return; 1.126 + char* fillp = &_option_buf[opt_so_far]; 1.127 + if (opt_so_far > 0) *fillp++ = ','; 1.128 + strcat(fillp, p); 1.129 + // replace white space by commas: 1.130 + char* q = fillp; 1.131 + while ((q = strpbrk(q, " \t\n")) != NULL) 1.132 + *q++ = ','; 1.133 + // Note that multiple PrintAssemblyOptions flags accumulate with \n, 1.134 + // which we want to be changed to a comma... 1.135 + } 1.136 + 1.137 + void print_insn_labels(); 1.138 + void print_insn_bytes(address pc0, address pc); 1.139 + void print_address(address value); 1.140 + 1.141 + public: 1.142 + decode_env(CodeBlob* code, outputStream* output); 1.143 + 1.144 + address decode_instructions(address start, address end); 1.145 + 1.146 + void start_insn(address pc) { 1.147 + _cur_insn = pc; 1.148 + output()->bol(); 1.149 + print_insn_labels(); 1.150 + } 1.151 + 1.152 + void end_insn(address pc) { 1.153 + address pc0 = cur_insn(); 1.154 + outputStream* st = output(); 1.155 + if (_print_bytes && pc > pc0) 1.156 + print_insn_bytes(pc0, pc); 1.157 + if (_nm != NULL) 1.158 + _nm->print_code_comment_on(st, COMMENT_COLUMN, pc0, pc); 1.159 + 1.160 + // Output pc bucket ticks if we have any 1.161 + if (total_ticks() != 0) { 1.162 + address bucket_pc = FlatProfiler::bucket_start_for(pc); 1.163 + if (bucket_pc != NULL && bucket_pc > pc0 && bucket_pc <= pc) { 1.164 + int bucket_count = FlatProfiler::bucket_count_for(pc0); 1.165 + if (bucket_count != 0) { 1.166 + st->bol(); 1.167 + st->print_cr("%3.1f%% [%d]", bucket_count*100.0/total_ticks(), bucket_count); 1.168 + } 1.169 + } 1.170 + } 1.171 + } 1.172 + 1.173 + address handle_event(const char* event, address arg); 1.174 + 1.175 + outputStream* output() { return _output; } 1.176 + address cur_insn() { return _cur_insn; } 1.177 + int total_ticks() { return _total_ticks; } 1.178 + void set_total_ticks(int n) { _total_ticks = n; } 1.179 + const char* options() { return _option_buf; } 1.180 +}; 1.181 + 1.182 +decode_env::decode_env(CodeBlob* code, outputStream* output) { 1.183 + memset(this, 0, sizeof(*this)); 1.184 + _output = output ? output : tty; 1.185 + _code = code; 1.186 + if (code != NULL && code->is_nmethod()) 1.187 + _nm = (nmethod*) code; 1.188 + 1.189 + // by default, output pc but not bytes: 1.190 + _print_pc = true; 1.191 + _print_bytes = false; 1.192 + _bytes_per_line = Disassembler::pd_instruction_alignment(); 1.193 + 1.194 + // parse the global option string: 1.195 + collect_options(Disassembler::pd_cpu_opts()); 1.196 + collect_options(PrintAssemblyOptions); 1.197 + 1.198 + if (strstr(options(), "hsdis-")) { 1.199 + if (strstr(options(), "hsdis-print-raw")) 1.200 + _print_raw = (strstr(options(), "xml") ? 2 : 1); 1.201 + if (strstr(options(), "hsdis-print-pc")) 1.202 + _print_pc = !_print_pc; 1.203 + if (strstr(options(), "hsdis-print-bytes")) 1.204 + _print_bytes = !_print_bytes; 1.205 + } 1.206 + if (strstr(options(), "help")) { 1.207 + tty->print_cr("PrintAssemblyOptions help:"); 1.208 + tty->print_cr(" hsdis-print-raw test plugin by requesting raw output"); 1.209 + tty->print_cr(" hsdis-print-raw-xml test plugin by requesting raw xml"); 1.210 + tty->print_cr(" hsdis-print-pc turn off PC printing (on by default)"); 1.211 + tty->print_cr(" hsdis-print-bytes turn on instruction byte output"); 1.212 + tty->print_cr("combined options: %s", options()); 1.213 + } 1.214 +} 1.215 + 1.216 +address decode_env::handle_event(const char* event, address arg) { 1.217 + if (match(event, "insn")) { 1.218 + start_insn(arg); 1.219 + } else if (match(event, "/insn")) { 1.220 + end_insn(arg); 1.221 + } else if (match(event, "addr")) { 1.222 + if (arg != NULL) { 1.223 + print_address(arg); 1.224 + return arg; 1.225 + } 1.226 + } else if (match(event, "mach")) { 1.227 + output()->print_cr("[Disassembling for mach='%s']", arg); 1.228 + } else if (match(event, "format bytes-per-line")) { 1.229 + _bytes_per_line = (int) (intptr_t) arg; 1.230 + } else { 1.231 + // ignore unrecognized markup 1.232 + } 1.233 + return NULL; 1.234 +} 1.235 + 1.236 +// called by the disassembler to print out jump targets and data addresses 1.237 +void decode_env::print_address(address adr) { 1.238 + outputStream* st = _output; 1.239 + 1.240 + if (adr == NULL) { 1.241 + st->print("NULL"); 1.242 + return; 1.243 + } 1.244 + 1.245 + int small_num = (int)(intptr_t)adr; 1.246 + if ((intptr_t)adr == (intptr_t)small_num 1.247 + && -1 <= small_num && small_num <= 9) { 1.248 + st->print("%d", small_num); 1.249 + return; 1.250 + } 1.251 + 1.252 + if (Universe::is_fully_initialized()) { 1.253 + if (StubRoutines::contains(adr)) { 1.254 + StubCodeDesc* desc = StubCodeDesc::desc_for(adr); 1.255 + if (desc == NULL) 1.256 + desc = StubCodeDesc::desc_for(adr + frame::pc_return_offset); 1.257 + if (desc != NULL) { 1.258 + st->print("Stub::%s", desc->name()); 1.259 + if (desc->begin() != adr) 1.260 + st->print("%+d 0x%p",adr - desc->begin(), adr); 1.261 + else if (WizardMode) st->print(" " INTPTR_FORMAT, adr); 1.262 + return; 1.263 + } 1.264 + st->print("Stub::<unknown> " INTPTR_FORMAT, adr); 1.265 + return; 1.266 + } 1.267 + 1.268 + BarrierSet* bs = Universe::heap()->barrier_set(); 1.269 + if (bs->kind() == BarrierSet::CardTableModRef && 1.270 + adr == (address)((CardTableModRefBS*)(bs))->byte_map_base) { 1.271 + st->print("word_map_base"); 1.272 + if (WizardMode) st->print(" " INTPTR_FORMAT, (intptr_t)adr); 1.273 + return; 1.274 + } 1.275 + 1.276 + oop obj; 1.277 + if (_nm != NULL 1.278 + && (obj = _nm->embeddedOop_at(cur_insn())) != NULL 1.279 + && (address) obj == adr) { 1.280 + obj->print_value_on(st); 1.281 + return; 1.282 + } 1.283 + } 1.284 + 1.285 + // Fall through to a simple numeral. 1.286 + st->print(INTPTR_FORMAT, (intptr_t)adr); 1.287 +} 1.288 + 1.289 +void decode_env::print_insn_labels() { 1.290 + address p = cur_insn(); 1.291 + outputStream* st = output(); 1.292 + nmethod* nm = _nm; 1.293 + if (nm != NULL) { 1.294 + if (p == nm->entry_point()) st->print_cr("[Entry Point]"); 1.295 + if (p == nm->verified_entry_point()) st->print_cr("[Verified Entry Point]"); 1.296 + if (p == nm->exception_begin()) st->print_cr("[Exception Handler]"); 1.297 + if (p == nm->stub_begin()) st->print_cr("[Stub Code]"); 1.298 + if (p == nm->consts_begin()) st->print_cr("[Constants]"); 1.299 + } 1.300 + CodeBlob* cb = _code; 1.301 + if (cb != NULL) { 1.302 + cb->print_block_comment(st, (intptr_t)(p - cb->instructions_begin())); 1.303 + } 1.304 + if (_print_pc) { 1.305 + st->print(" " INTPTR_FORMAT ": ", (intptr_t) p); 1.306 + } 1.307 +} 1.308 + 1.309 +void decode_env::print_insn_bytes(address pc, address pc_limit) { 1.310 + outputStream* st = output(); 1.311 + size_t incr = 1; 1.312 + size_t perline = _bytes_per_line; 1.313 + if ((size_t) Disassembler::pd_instruction_alignment() >= sizeof(int) 1.314 + && !((uintptr_t)pc % sizeof(int)) 1.315 + && !((uintptr_t)pc_limit % sizeof(int))) { 1.316 + incr = sizeof(int); 1.317 + if (perline % incr) perline += incr - (perline % incr); 1.318 + } 1.319 + while (pc < pc_limit) { 1.320 + // tab to the desired column: 1.321 + st->move_to(COMMENT_COLUMN); 1.322 + address pc0 = pc; 1.323 + address pc1 = pc + perline; 1.324 + if (pc1 > pc_limit) pc1 = pc_limit; 1.325 + for (; pc < pc1; pc += incr) { 1.326 + if (pc == pc0) 1.327 + st->print(BYTES_COMMENT); 1.328 + else if ((uint)(pc - pc0) % sizeof(int) == 0) 1.329 + st->print(" "); // put out a space on word boundaries 1.330 + if (incr == sizeof(int)) 1.331 + st->print("%08lx", *(int*)pc); 1.332 + else st->print("%02x", (*pc)&0xFF); 1.333 + } 1.334 + st->cr(); 1.335 + } 1.336 +} 1.337 + 1.338 + 1.339 +static void* event_to_env(void* env_pv, const char* event, void* arg) { 1.340 + decode_env* env = (decode_env*) env_pv; 1.341 + return env->handle_event(event, (address) arg); 1.342 +} 1.343 + 1.344 +static int printf_to_env(void* env_pv, const char* format, ...) { 1.345 + decode_env* env = (decode_env*) env_pv; 1.346 + outputStream* st = env->output(); 1.347 + size_t flen = strlen(format); 1.348 + const char* raw = NULL; 1.349 + if (flen == 0) return 0; 1.350 + if (flen == 1 && format[0] == '\n') { st->bol(); return 1; } 1.351 + if (flen < 2 || 1.352 + strchr(format, '%') == NULL) { 1.353 + raw = format; 1.354 + } else if (format[0] == '%' && format[1] == '%' && 1.355 + strchr(format+2, '%') == NULL) { 1.356 + // happens a lot on machines with names like %foo 1.357 + flen--; 1.358 + raw = format+1; 1.359 + } 1.360 + if (raw != NULL) { 1.361 + st->print_raw(raw, (int) flen); 1.362 + return (int) flen; 1.363 + } 1.364 + va_list ap; 1.365 + va_start(ap, format); 1.366 + julong cnt0 = st->count(); 1.367 + st->vprint(format, ap); 1.368 + julong cnt1 = st->count(); 1.369 + va_end(ap); 1.370 + return (int)(cnt1 - cnt0); 1.371 +} 1.372 + 1.373 +address decode_env::decode_instructions(address start, address end) { 1.374 + _start = start; _end = end; 1.375 + 1.376 + assert((((intptr_t)start | (intptr_t)end) % Disassembler::pd_instruction_alignment() == 0), "misaligned insn addr"); 1.377 + 1.378 + const int show_bytes = false; // for disassembler debugging 1.379 + 1.380 + //_version = Disassembler::pd_cpu_version(); 1.381 + 1.382 + if (!Disassembler::can_decode()) { 1.383 + return NULL; 1.384 + } 1.385 + 1.386 + // decode a series of instructions and return the end of the last instruction 1.387 + 1.388 + if (_print_raw) { 1.389 + // Print whatever the library wants to print, w/o fancy callbacks. 1.390 + // This is mainly for debugging the library itself. 1.391 + FILE* out = stdout; 1.392 + FILE* xmlout = (_print_raw > 1 ? out : NULL); 1.393 + return (address) 1.394 + (*Disassembler::_decode_instructions)(start, end, 1.395 + NULL, (void*) xmlout, 1.396 + NULL, (void*) out, 1.397 + options()); 1.398 + } 1.399 + 1.400 + return (address) 1.401 + (*Disassembler::_decode_instructions)(start, end, 1.402 + &event_to_env, (void*) this, 1.403 + &printf_to_env, (void*) this, 1.404 + options()); 1.405 +} 1.406 + 1.407 + 1.408 +void Disassembler::decode(CodeBlob* cb, outputStream* st) { 1.409 + if (!load_library()) return; 1.410 + decode_env env(cb, st); 1.411 + env.output()->print_cr("Decoding CodeBlob " INTPTR_FORMAT, cb); 1.412 + env.decode_instructions(cb->instructions_begin(), cb->instructions_end()); 1.413 +} 1.414 + 1.415 + 1.416 +void Disassembler::decode(address start, address end, outputStream* st) { 1.417 + if (!load_library()) return; 1.418 + decode_env env(CodeCache::find_blob_unsafe(start), st); 1.419 + env.decode_instructions(start, end); 1.420 +} 1.421 + 1.422 +void Disassembler::decode(nmethod* nm, outputStream* st) { 1.423 + if (!load_library()) return; 1.424 + decode_env env(nm, st); 1.425 + env.output()->print_cr("Decoding compiled method " INTPTR_FORMAT ":", nm); 1.426 + env.output()->print_cr("Code:"); 1.427 + 1.428 + unsigned char* p = nm->instructions_begin(); 1.429 + unsigned char* end = nm->instructions_end(); 1.430 + 1.431 + // If there has been profiling, print the buckets. 1.432 + if (FlatProfiler::bucket_start_for(p) != NULL) { 1.433 + unsigned char* p1 = p; 1.434 + int total_bucket_count = 0; 1.435 + while (p1 < end) { 1.436 + unsigned char* p0 = p1; 1.437 + p1 += pd_instruction_alignment(); 1.438 + address bucket_pc = FlatProfiler::bucket_start_for(p1); 1.439 + if (bucket_pc != NULL && bucket_pc > p0 && bucket_pc <= p1) 1.440 + total_bucket_count += FlatProfiler::bucket_count_for(p0); 1.441 + } 1.442 + env.set_total_ticks(total_bucket_count); 1.443 + } 1.444 + 1.445 + env.decode_instructions(p, end); 1.446 +}