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