Mon, 24 Sep 2012 10:30:14 -0700
7200163: add CodeComments functionality to assember stubs
Summary: Pass the codeBuffer to the Stub constructor, and adapts the disassembler to print the comments.
Reviewed-by: jrose, kvn, twisti
Contributed-by: goetz.lindenmaier@sap.com
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 int lib_offset = -1;
82 {
83 // Match "jvm[^/]*" in jvm_path.
84 const char* base = buf;
85 const char* p = strrchr(buf, '/');
86 if (p != NULL) lib_offset = p - base + 1;
87 p = strstr(p ? p : base, "jvm");
88 if (p != NULL) jvm_offset = p - base;
89 }
90 // Find the disassembler shared library.
91 // Search for several paths derived from libjvm, in this order:
92 // 1. <home>/jre/lib/<arch>/<vm>/libhsdis-<arch>.so (for compatibility)
93 // 2. <home>/jre/lib/<arch>/<vm>/hsdis-<arch>.so
94 // 3. <home>/jre/lib/<arch>/hsdis-<arch>.so
95 // 4. hsdis-<arch>.so (using LD_LIBRARY_PATH)
96 if (jvm_offset >= 0) {
97 // 1. <home>/jre/lib/<arch>/<vm>/libhsdis-<arch>.so
98 strcpy(&buf[jvm_offset], hsdis_library_name);
99 strcat(&buf[jvm_offset], os::dll_file_extension());
100 _library = os::dll_load(buf, ebuf, sizeof ebuf);
101 if (_library == NULL) {
102 // 2. <home>/jre/lib/<arch>/<vm>/hsdis-<arch>.so
103 strcpy(&buf[lib_offset], hsdis_library_name);
104 strcat(&buf[lib_offset], os::dll_file_extension());
105 _library = os::dll_load(buf, ebuf, sizeof ebuf);
106 }
107 if (_library == NULL) {
108 // 3. <home>/jre/lib/<arch>/hsdis-<arch>.so
109 buf[lib_offset - 1] = '\0';
110 const char* p = strrchr(buf, '/');
111 if (p != NULL) {
112 lib_offset = p - buf + 1;
113 strcpy(&buf[lib_offset], hsdis_library_name);
114 strcat(&buf[lib_offset], os::dll_file_extension());
115 _library = os::dll_load(buf, ebuf, sizeof ebuf);
116 }
117 }
118 }
119 if (_library == NULL) {
120 // 4. hsdis-<arch>.so (using LD_LIBRARY_PATH)
121 strcpy(&buf[0], hsdis_library_name);
122 strcat(&buf[0], os::dll_file_extension());
123 _library = os::dll_load(buf, ebuf, sizeof ebuf);
124 }
125 if (_library != NULL) {
126 _decode_instructions = CAST_TO_FN_PTR(Disassembler::decode_func,
127 os::dll_lookup(_library, decode_instructions_name));
128 }
129 _tried_to_load_library = true;
130 if (_decode_instructions == NULL) {
131 tty->print_cr("Could not load %s; %s; %s", buf,
132 ((_library != NULL)
133 ? "entry point is missing"
134 : (WizardMode || PrintMiscellaneous)
135 ? (const char*)ebuf
136 : "library not loadable"),
137 "PrintAssembly is disabled");
138 return false;
139 }
141 // Success.
142 tty->print_cr("Loaded disassembler from %s", buf);
143 return true;
144 }
147 class decode_env {
148 private:
149 nmethod* _nm;
150 CodeBlob* _code;
151 CodeComments _comments;
152 outputStream* _output;
153 address _start, _end;
155 char _option_buf[512];
156 char _print_raw;
157 bool _print_pc;
158 bool _print_bytes;
159 address _cur_insn;
160 int _total_ticks;
161 int _bytes_per_line; // arch-specific formatting option
163 static bool match(const char* event, const char* tag) {
164 size_t taglen = strlen(tag);
165 if (strncmp(event, tag, taglen) != 0)
166 return false;
167 char delim = event[taglen];
168 return delim == '\0' || delim == ' ' || delim == '/' || delim == '=';
169 }
171 void collect_options(const char* p) {
172 if (p == NULL || p[0] == '\0') return;
173 size_t opt_so_far = strlen(_option_buf);
174 if (opt_so_far + 1 + strlen(p) + 1 > sizeof(_option_buf)) return;
175 char* fillp = &_option_buf[opt_so_far];
176 if (opt_so_far > 0) *fillp++ = ',';
177 strcat(fillp, p);
178 // replace white space by commas:
179 char* q = fillp;
180 while ((q = strpbrk(q, " \t\n")) != NULL)
181 *q++ = ',';
182 // Note that multiple PrintAssemblyOptions flags accumulate with \n,
183 // which we want to be changed to a comma...
184 }
186 void print_insn_labels();
187 void print_insn_bytes(address pc0, address pc);
188 void print_address(address value);
190 public:
191 decode_env(CodeBlob* code, outputStream* output, CodeComments c = CodeComments());
193 address decode_instructions(address start, address end);
195 void start_insn(address pc) {
196 _cur_insn = pc;
197 output()->bol();
198 print_insn_labels();
199 }
201 void end_insn(address pc) {
202 address pc0 = cur_insn();
203 outputStream* st = output();
204 if (_print_bytes && pc > pc0)
205 print_insn_bytes(pc0, pc);
206 if (_nm != NULL) {
207 _nm->print_code_comment_on(st, COMMENT_COLUMN, pc0, pc);
208 // this calls reloc_string_for which calls oop::print_value_on
209 }
211 // Output pc bucket ticks if we have any
212 if (total_ticks() != 0) {
213 address bucket_pc = FlatProfiler::bucket_start_for(pc);
214 if (bucket_pc != NULL && bucket_pc > pc0 && bucket_pc <= pc) {
215 int bucket_count = FlatProfiler::bucket_count_for(pc0);
216 if (bucket_count != 0) {
217 st->bol();
218 st->print_cr("%3.1f%% [%d]", bucket_count*100.0/total_ticks(), bucket_count);
219 }
220 }
221 }
222 }
224 address handle_event(const char* event, address arg);
226 outputStream* output() { return _output; }
227 address cur_insn() { return _cur_insn; }
228 int total_ticks() { return _total_ticks; }
229 void set_total_ticks(int n) { _total_ticks = n; }
230 const char* options() { return _option_buf; }
231 };
233 decode_env::decode_env(CodeBlob* code, outputStream* output, CodeComments c) {
234 memset(this, 0, sizeof(*this));
235 _output = output ? output : tty;
236 _code = code;
237 if (code != NULL && code->is_nmethod())
238 _nm = (nmethod*) code;
239 _comments.assign(c);
241 // by default, output pc but not bytes:
242 _print_pc = true;
243 _print_bytes = false;
244 _bytes_per_line = Disassembler::pd_instruction_alignment();
246 // parse the global option string:
247 collect_options(Disassembler::pd_cpu_opts());
248 collect_options(PrintAssemblyOptions);
250 if (strstr(options(), "hsdis-")) {
251 if (strstr(options(), "hsdis-print-raw"))
252 _print_raw = (strstr(options(), "xml") ? 2 : 1);
253 if (strstr(options(), "hsdis-print-pc"))
254 _print_pc = !_print_pc;
255 if (strstr(options(), "hsdis-print-bytes"))
256 _print_bytes = !_print_bytes;
257 }
258 if (strstr(options(), "help")) {
259 tty->print_cr("PrintAssemblyOptions help:");
260 tty->print_cr(" hsdis-print-raw test plugin by requesting raw output");
261 tty->print_cr(" hsdis-print-raw-xml test plugin by requesting raw xml");
262 tty->print_cr(" hsdis-print-pc turn off PC printing (on by default)");
263 tty->print_cr(" hsdis-print-bytes turn on instruction byte output");
264 tty->print_cr("combined options: %s", options());
265 }
266 }
268 address decode_env::handle_event(const char* event, address arg) {
269 if (match(event, "insn")) {
270 start_insn(arg);
271 } else if (match(event, "/insn")) {
272 end_insn(arg);
273 } else if (match(event, "addr")) {
274 if (arg != NULL) {
275 print_address(arg);
276 return arg;
277 }
278 } else if (match(event, "mach")) {
279 static char buffer[32] = { 0, };
280 if (strcmp(buffer, (const char*)arg) != 0 ||
281 strlen((const char*)arg) > sizeof(buffer) - 1) {
282 // Only print this when the mach changes
283 strncpy(buffer, (const char*)arg, sizeof(buffer) - 1);
284 output()->print_cr("[Disassembling for mach='%s']", arg);
285 }
286 } else if (match(event, "format bytes-per-line")) {
287 _bytes_per_line = (int) (intptr_t) arg;
288 } else {
289 // ignore unrecognized markup
290 }
291 return NULL;
292 }
294 // called by the disassembler to print out jump targets and data addresses
295 void decode_env::print_address(address adr) {
296 outputStream* st = _output;
298 if (adr == NULL) {
299 st->print("NULL");
300 return;
301 }
303 int small_num = (int)(intptr_t)adr;
304 if ((intptr_t)adr == (intptr_t)small_num
305 && -1 <= small_num && small_num <= 9) {
306 st->print("%d", small_num);
307 return;
308 }
310 if (Universe::is_fully_initialized()) {
311 if (StubRoutines::contains(adr)) {
312 StubCodeDesc* desc = StubCodeDesc::desc_for(adr);
313 if (desc == NULL)
314 desc = StubCodeDesc::desc_for(adr + frame::pc_return_offset);
315 if (desc != NULL) {
316 st->print("Stub::%s", desc->name());
317 if (desc->begin() != adr)
318 st->print("%+d 0x%p",adr - desc->begin(), adr);
319 else if (WizardMode) st->print(" " PTR_FORMAT, adr);
320 return;
321 }
322 st->print("Stub::<unknown> " PTR_FORMAT, adr);
323 return;
324 }
326 BarrierSet* bs = Universe::heap()->barrier_set();
327 if (bs->kind() == BarrierSet::CardTableModRef &&
328 adr == (address)((CardTableModRefBS*)(bs))->byte_map_base) {
329 st->print("word_map_base");
330 if (WizardMode) st->print(" " INTPTR_FORMAT, (intptr_t)adr);
331 return;
332 }
334 oop obj;
335 if (_nm != NULL
336 && (obj = _nm->embeddedOop_at(cur_insn())) != NULL
337 && (address) obj == adr
338 && Universe::heap()->is_in(obj)
339 && Universe::heap()->is_in(obj->klass())) {
340 julong c = st->count();
341 obj->print_value_on(st);
342 if (st->count() == c) {
343 // No output. (Can happen in product builds.)
344 st->print("(a %s)", Klass::cast(obj->klass())->external_name());
345 }
346 return;
347 }
348 }
350 // Fall through to a simple (hexadecimal) numeral.
351 st->print(PTR_FORMAT, adr);
352 }
354 void decode_env::print_insn_labels() {
355 address p = cur_insn();
356 outputStream* st = output();
357 CodeBlob* cb = _code;
358 if (cb != NULL) {
359 cb->print_block_comment(st, p);
360 }
361 _comments.print_block_comment(st, (intptr_t)(p - _start));
362 if (_print_pc) {
363 st->print(" " PTR_FORMAT ": ", p);
364 }
365 }
367 void decode_env::print_insn_bytes(address pc, address pc_limit) {
368 outputStream* st = output();
369 size_t incr = 1;
370 size_t perline = _bytes_per_line;
371 if ((size_t) Disassembler::pd_instruction_alignment() >= sizeof(int)
372 && !((uintptr_t)pc % sizeof(int))
373 && !((uintptr_t)pc_limit % sizeof(int))) {
374 incr = sizeof(int);
375 if (perline % incr) perline += incr - (perline % incr);
376 }
377 while (pc < pc_limit) {
378 // tab to the desired column:
379 st->move_to(COMMENT_COLUMN);
380 address pc0 = pc;
381 address pc1 = pc + perline;
382 if (pc1 > pc_limit) pc1 = pc_limit;
383 for (; pc < pc1; pc += incr) {
384 if (pc == pc0)
385 st->print(BYTES_COMMENT);
386 else if ((uint)(pc - pc0) % sizeof(int) == 0)
387 st->print(" "); // put out a space on word boundaries
388 if (incr == sizeof(int))
389 st->print("%08lx", *(int*)pc);
390 else st->print("%02x", (*pc)&0xFF);
391 }
392 st->cr();
393 }
394 }
397 static void* event_to_env(void* env_pv, const char* event, void* arg) {
398 decode_env* env = (decode_env*) env_pv;
399 return env->handle_event(event, (address) arg);
400 }
402 static int printf_to_env(void* env_pv, const char* format, ...) {
403 decode_env* env = (decode_env*) env_pv;
404 outputStream* st = env->output();
405 size_t flen = strlen(format);
406 const char* raw = NULL;
407 if (flen == 0) return 0;
408 if (flen == 1 && format[0] == '\n') { st->bol(); return 1; }
409 if (flen < 2 ||
410 strchr(format, '%') == NULL) {
411 raw = format;
412 } else if (format[0] == '%' && format[1] == '%' &&
413 strchr(format+2, '%') == NULL) {
414 // happens a lot on machines with names like %foo
415 flen--;
416 raw = format+1;
417 }
418 if (raw != NULL) {
419 st->print_raw(raw, (int) flen);
420 return (int) flen;
421 }
422 va_list ap;
423 va_start(ap, format);
424 julong cnt0 = st->count();
425 st->vprint(format, ap);
426 julong cnt1 = st->count();
427 va_end(ap);
428 return (int)(cnt1 - cnt0);
429 }
431 address decode_env::decode_instructions(address start, address end) {
432 _start = start; _end = end;
434 assert(((((intptr_t)start | (intptr_t)end) % Disassembler::pd_instruction_alignment()) == 0), "misaligned insn addr");
436 const int show_bytes = false; // for disassembler debugging
438 //_version = Disassembler::pd_cpu_version();
440 if (!Disassembler::can_decode()) {
441 return NULL;
442 }
444 // decode a series of instructions and return the end of the last instruction
446 if (_print_raw) {
447 // Print whatever the library wants to print, w/o fancy callbacks.
448 // This is mainly for debugging the library itself.
449 FILE* out = stdout;
450 FILE* xmlout = (_print_raw > 1 ? out : NULL);
451 return (address)
452 (*Disassembler::_decode_instructions)(start, end,
453 NULL, (void*) xmlout,
454 NULL, (void*) out,
455 options());
456 }
458 return (address)
459 (*Disassembler::_decode_instructions)(start, end,
460 &event_to_env, (void*) this,
461 &printf_to_env, (void*) this,
462 options());
463 }
466 void Disassembler::decode(CodeBlob* cb, outputStream* st) {
467 if (!load_library()) return;
468 decode_env env(cb, st);
469 env.output()->print_cr("Decoding CodeBlob " PTR_FORMAT, cb);
470 env.decode_instructions(cb->code_begin(), cb->code_end());
471 }
473 void Disassembler::decode(address start, address end, outputStream* st, CodeComments c) {
474 if (!load_library()) return;
475 decode_env env(CodeCache::find_blob_unsafe(start), st, c);
476 env.decode_instructions(start, end);
477 }
479 void Disassembler::decode(nmethod* nm, outputStream* st) {
480 if (!load_library()) return;
481 decode_env env(nm, st);
482 env.output()->print_cr("Decoding compiled method " PTR_FORMAT ":", nm);
483 env.output()->print_cr("Code:");
485 #ifdef SHARK
486 SharkEntry* entry = (SharkEntry *) nm->code_begin();
487 unsigned char* p = entry->code_start();
488 unsigned char* end = entry->code_limit();
489 #else
490 unsigned char* p = nm->code_begin();
491 unsigned char* end = nm->code_end();
492 #endif // SHARK
494 // If there has been profiling, print the buckets.
495 if (FlatProfiler::bucket_start_for(p) != NULL) {
496 unsigned char* p1 = p;
497 int total_bucket_count = 0;
498 while (p1 < end) {
499 unsigned char* p0 = p1;
500 p1 += pd_instruction_alignment();
501 address bucket_pc = FlatProfiler::bucket_start_for(p1);
502 if (bucket_pc != NULL && bucket_pc > p0 && bucket_pc <= p1)
503 total_bucket_count += FlatProfiler::bucket_count_for(p0);
504 }
505 env.set_total_ticks(total_bucket_count);
506 }
508 // Print constant table.
509 if (nm->consts_size() > 0) {
510 nm->print_nmethod_labels(env.output(), nm->consts_begin());
511 int offset = 0;
512 for (address p = nm->consts_begin(); p < nm->consts_end(); p += 4, offset += 4) {
513 if ((offset % 8) == 0) {
514 env.output()->print_cr(" " PTR_FORMAT " (offset: %4d): " PTR32_FORMAT " " PTR64_FORMAT, p, offset, *((int32_t*) p), *((int64_t*) p));
515 } else {
516 env.output()->print_cr(" " PTR_FORMAT " (offset: %4d): " PTR32_FORMAT, p, offset, *((int32_t*) p));
517 }
518 }
519 }
521 env.decode_instructions(p, end);
522 }