1 /*
2 * Copyright (c) 2008, 2016, 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 */
24
25 #include "precompiled.hpp"
26 #include "classfile/javaClasses.hpp"
27 #include "code/codeCache.hpp"
28 #include "compiler/disassembler.hpp"
29 #include "gc/shared/cardTableModRefBS.hpp"
30 #include "gc/shared/collectedHeap.hpp"
31 #include "memory/resourceArea.hpp"
32 #include "oops/oop.inline.hpp"
33 #include "runtime/fprofiler.hpp"
34 #include "runtime/handles.inline.hpp"
35 #include "runtime/os.hpp"
36 #include "runtime/stubCodeGenerator.hpp"
37 #include "runtime/stubRoutines.hpp"
38 #include CPU_HEADER(depChecker)
39 #ifdef SHARK
40 #include "shark/sharkEntry.hpp"
41 #endif
42
43 void* Disassembler::_library = NULL;
44 bool Disassembler::_tried_to_load_library = false;
45
46 // This routine is in the shared library:
47 Disassembler::decode_func_virtual Disassembler::_decode_instructions_virtual = NULL;
48 Disassembler::decode_func Disassembler::_decode_instructions = NULL;
49
50 static const char hsdis_library_name[] = "hsdis-" HOTSPOT_LIB_ARCH;
51 static const char decode_instructions_virtual_name[] = "decode_instructions_virtual";
52 static const char decode_instructions_name[] = "decode_instructions";
53 static bool use_new_version = true;
146
147 // Success.
148 tty->print_cr("Loaded disassembler from %s", buf);
149 return true;
150 }
151
152
153 class decode_env {
154 private:
155 nmethod* _nm;
156 CodeBlob* _code;
157 CodeStrings _strings;
158 outputStream* _output;
159 address _start, _end;
160
161 char _option_buf[512];
162 char _print_raw;
163 bool _print_pc;
164 bool _print_bytes;
165 address _cur_insn;
166 int _total_ticks;
167 int _bytes_per_line; // arch-specific formatting option
168
169 static bool match(const char* event, const char* tag) {
170 size_t taglen = strlen(tag);
171 if (strncmp(event, tag, taglen) != 0)
172 return false;
173 char delim = event[taglen];
174 return delim == '\0' || delim == ' ' || delim == '/' || delim == '=';
175 }
176
177 void collect_options(const char* p) {
178 if (p == NULL || p[0] == '\0') return;
179 size_t opt_so_far = strlen(_option_buf);
180 if (opt_so_far + 1 + strlen(p) + 1 > sizeof(_option_buf)) return;
181 char* fillp = &_option_buf[opt_so_far];
182 if (opt_so_far > 0) *fillp++ = ',';
183 strcat(fillp, p);
184 // replace white space by commas:
185 char* q = fillp;
186 while ((q = strpbrk(q, " \t\n")) != NULL)
196 public:
197 decode_env(CodeBlob* code, outputStream* output, CodeStrings c = CodeStrings());
198
199 address decode_instructions(address start, address end);
200
201 void start_insn(address pc) {
202 _cur_insn = pc;
203 output()->bol();
204 print_insn_labels();
205 }
206
207 void end_insn(address pc) {
208 address pc0 = cur_insn();
209 outputStream* st = output();
210 if (_print_bytes && pc > pc0)
211 print_insn_bytes(pc0, pc);
212 if (_nm != NULL) {
213 _nm->print_code_comment_on(st, COMMENT_COLUMN, pc0, pc);
214 // this calls reloc_string_for which calls oop::print_value_on
215 }
216
217 // Output pc bucket ticks if we have any
218 if (total_ticks() != 0) {
219 address bucket_pc = FlatProfiler::bucket_start_for(pc);
220 if (bucket_pc != NULL && bucket_pc > pc0 && bucket_pc <= pc) {
221 int bucket_count = FlatProfiler::bucket_count_for(pc0);
222 if (bucket_count != 0) {
223 st->bol();
224 st->print_cr("%3.1f%% [%d]", bucket_count*100.0/total_ticks(), bucket_count);
225 }
226 }
227 }
228 // follow each complete insn by a nice newline
229 st->cr();
230 }
231
232 address handle_event(const char* event, address arg);
233
234 outputStream* output() { return _output; }
235 address cur_insn() { return _cur_insn; }
236 int total_ticks() { return _total_ticks; }
237 void set_total_ticks(int n) { _total_ticks = n; }
238 const char* options() { return _option_buf; }
239 };
240
241 decode_env::decode_env(CodeBlob* code, outputStream* output, CodeStrings c) {
242 memset(this, 0, sizeof(*this)); // Beware, this zeroes bits of fields.
243 _output = output ? output : tty;
244 _code = code;
245 if (code != NULL && code->is_nmethod())
246 _nm = (nmethod*) code;
247 _strings.copy(c);
248
249 // by default, output pc but not bytes:
250 _print_pc = true;
251 _print_bytes = false;
252 _bytes_per_line = Disassembler::pd_instruction_alignment();
253
254 // parse the global option string:
255 collect_options(Disassembler::pd_cpu_opts());
256 collect_options(PrintAssemblyOptions);
257
544 #else
545 unsigned char* p = nm->code_begin();
546 unsigned char* end = nm->code_end();
547 #endif // SHARK
548
549 nm->method()->method_holder()->name()->print_symbol_on(env.output());
550 env.output()->print(".");
551 nm->method()->name()->print_symbol_on(env.output());
552 nm->method()->signature()->print_symbol_on(env.output());
553 #if INCLUDE_JVMCI
554 {
555 char buffer[O_BUFLEN];
556 char* jvmciName = nm->jvmci_installed_code_name(buffer, O_BUFLEN);
557 if (jvmciName != NULL) {
558 env.output()->print(" (%s)", jvmciName);
559 }
560 }
561 #endif
562 env.output()->print_cr(" [" PTR_FORMAT ", " PTR_FORMAT "] " JLONG_FORMAT " bytes", p2i(p), p2i(end), ((jlong)(end - p)));
563
564 // If there has been profiling, print the buckets.
565 if (FlatProfiler::bucket_start_for(p) != NULL) {
566 unsigned char* p1 = p;
567 int total_bucket_count = 0;
568 while (p1 < end) {
569 unsigned char* p0 = p1;
570 p1 += pd_instruction_alignment();
571 address bucket_pc = FlatProfiler::bucket_start_for(p1);
572 if (bucket_pc != NULL && bucket_pc > p0 && bucket_pc <= p1)
573 total_bucket_count += FlatProfiler::bucket_count_for(p0);
574 }
575 env.set_total_ticks(total_bucket_count);
576 }
577
578 // Print constant table.
579 if (nm->consts_size() > 0) {
580 nm->print_nmethod_labels(env.output(), nm->consts_begin());
581 int offset = 0;
582 for (address p = nm->consts_begin(); p < nm->consts_end(); p += 4, offset += 4) {
583 if ((offset % 8) == 0) {
584 env.output()->print_cr(" " PTR_FORMAT " (offset: %4d): " PTR32_FORMAT " " PTR64_FORMAT, p2i(p), offset, *((int32_t*) p), *((int64_t*) p));
585 } else {
586 env.output()->print_cr(" " PTR_FORMAT " (offset: %4d): " PTR32_FORMAT, p2i(p), offset, *((int32_t*) p));
587 }
588 }
589 }
590
591 env.decode_instructions(p, end);
592 }
|
1 /*
2 * Copyright (c) 2008, 2017, 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 */
24
25 #include "precompiled.hpp"
26 #include "classfile/javaClasses.hpp"
27 #include "code/codeCache.hpp"
28 #include "compiler/disassembler.hpp"
29 #include "gc/shared/cardTableModRefBS.hpp"
30 #include "gc/shared/collectedHeap.hpp"
31 #include "memory/resourceArea.hpp"
32 #include "oops/oop.inline.hpp"
33 #include "runtime/handles.inline.hpp"
34 #include "runtime/os.hpp"
35 #include "runtime/stubCodeGenerator.hpp"
36 #include "runtime/stubRoutines.hpp"
37 #include CPU_HEADER(depChecker)
38 #ifdef SHARK
39 #include "shark/sharkEntry.hpp"
40 #endif
41
42 void* Disassembler::_library = NULL;
43 bool Disassembler::_tried_to_load_library = false;
44
45 // This routine is in the shared library:
46 Disassembler::decode_func_virtual Disassembler::_decode_instructions_virtual = NULL;
47 Disassembler::decode_func Disassembler::_decode_instructions = NULL;
48
49 static const char hsdis_library_name[] = "hsdis-" HOTSPOT_LIB_ARCH;
50 static const char decode_instructions_virtual_name[] = "decode_instructions_virtual";
51 static const char decode_instructions_name[] = "decode_instructions";
52 static bool use_new_version = true;
145
146 // Success.
147 tty->print_cr("Loaded disassembler from %s", buf);
148 return true;
149 }
150
151
152 class decode_env {
153 private:
154 nmethod* _nm;
155 CodeBlob* _code;
156 CodeStrings _strings;
157 outputStream* _output;
158 address _start, _end;
159
160 char _option_buf[512];
161 char _print_raw;
162 bool _print_pc;
163 bool _print_bytes;
164 address _cur_insn;
165 int _bytes_per_line; // arch-specific formatting option
166
167 static bool match(const char* event, const char* tag) {
168 size_t taglen = strlen(tag);
169 if (strncmp(event, tag, taglen) != 0)
170 return false;
171 char delim = event[taglen];
172 return delim == '\0' || delim == ' ' || delim == '/' || delim == '=';
173 }
174
175 void collect_options(const char* p) {
176 if (p == NULL || p[0] == '\0') return;
177 size_t opt_so_far = strlen(_option_buf);
178 if (opt_so_far + 1 + strlen(p) + 1 > sizeof(_option_buf)) return;
179 char* fillp = &_option_buf[opt_so_far];
180 if (opt_so_far > 0) *fillp++ = ',';
181 strcat(fillp, p);
182 // replace white space by commas:
183 char* q = fillp;
184 while ((q = strpbrk(q, " \t\n")) != NULL)
194 public:
195 decode_env(CodeBlob* code, outputStream* output, CodeStrings c = CodeStrings());
196
197 address decode_instructions(address start, address end);
198
199 void start_insn(address pc) {
200 _cur_insn = pc;
201 output()->bol();
202 print_insn_labels();
203 }
204
205 void end_insn(address pc) {
206 address pc0 = cur_insn();
207 outputStream* st = output();
208 if (_print_bytes && pc > pc0)
209 print_insn_bytes(pc0, pc);
210 if (_nm != NULL) {
211 _nm->print_code_comment_on(st, COMMENT_COLUMN, pc0, pc);
212 // this calls reloc_string_for which calls oop::print_value_on
213 }
214 // follow each complete insn by a nice newline
215 st->cr();
216 }
217
218 address handle_event(const char* event, address arg);
219
220 outputStream* output() { return _output; }
221 address cur_insn() { return _cur_insn; }
222 const char* options() { return _option_buf; }
223 };
224
225 decode_env::decode_env(CodeBlob* code, outputStream* output, CodeStrings c) {
226 memset(this, 0, sizeof(*this)); // Beware, this zeroes bits of fields.
227 _output = output ? output : tty;
228 _code = code;
229 if (code != NULL && code->is_nmethod())
230 _nm = (nmethod*) code;
231 _strings.copy(c);
232
233 // by default, output pc but not bytes:
234 _print_pc = true;
235 _print_bytes = false;
236 _bytes_per_line = Disassembler::pd_instruction_alignment();
237
238 // parse the global option string:
239 collect_options(Disassembler::pd_cpu_opts());
240 collect_options(PrintAssemblyOptions);
241
528 #else
529 unsigned char* p = nm->code_begin();
530 unsigned char* end = nm->code_end();
531 #endif // SHARK
532
533 nm->method()->method_holder()->name()->print_symbol_on(env.output());
534 env.output()->print(".");
535 nm->method()->name()->print_symbol_on(env.output());
536 nm->method()->signature()->print_symbol_on(env.output());
537 #if INCLUDE_JVMCI
538 {
539 char buffer[O_BUFLEN];
540 char* jvmciName = nm->jvmci_installed_code_name(buffer, O_BUFLEN);
541 if (jvmciName != NULL) {
542 env.output()->print(" (%s)", jvmciName);
543 }
544 }
545 #endif
546 env.output()->print_cr(" [" PTR_FORMAT ", " PTR_FORMAT "] " JLONG_FORMAT " bytes", p2i(p), p2i(end), ((jlong)(end - p)));
547
548 // Print constant table.
549 if (nm->consts_size() > 0) {
550 nm->print_nmethod_labels(env.output(), nm->consts_begin());
551 int offset = 0;
552 for (address p = nm->consts_begin(); p < nm->consts_end(); p += 4, offset += 4) {
553 if ((offset % 8) == 0) {
554 env.output()->print_cr(" " PTR_FORMAT " (offset: %4d): " PTR32_FORMAT " " PTR64_FORMAT, p2i(p), offset, *((int32_t*) p), *((int64_t*) p));
555 } else {
556 env.output()->print_cr(" " PTR_FORMAT " (offset: %4d): " PTR32_FORMAT, p2i(p), offset, *((int32_t*) p));
557 }
558 }
559 }
560
561 env.decode_instructions(p, end);
562 }
|