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