comparison src/share/vm/compiler/disassembler.cpp @ 100:c7c777385a15

6667042: PrintAssembly option does not work without special plugin Summary: remove old private plugin interface, simplify, rework old plugin to use unchanged Gnu sources Reviewed-by: kvn, rasbold
author jrose
date Wed, 02 Apr 2008 12:09:59 -0700
parents
children 4e6abf09f540
comparison
equal deleted inserted replaced
99:8a4ef4e001d3 100:c7c777385a15
1 /*
2 * Copyright 2008 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 *
23 */
24
25 # include "incls/_precompiled.incl"
26 # include "incls/_disassembler.cpp.incl"
27
28 void* Disassembler::_library = NULL;
29 bool Disassembler::_tried_to_load_library = false;
30
31 // This routine is in the shared library:
32 Disassembler::decode_func Disassembler::_decode_instructions = NULL;
33
34 static const char hsdis_library_name[] = "hsdis-"HOTSPOT_LIB_ARCH;
35 static const char decode_instructions_name[] = "decode_instructions";
36
37 #define COMMENT_COLUMN 40 LP64_ONLY(+8) /*could be an option*/
38 #define BYTES_COMMENT ";..." /* funky byte display comment */
39
40 bool Disassembler::load_library() {
41 if (_decode_instructions != NULL) {
42 // Already succeeded.
43 return true;
44 }
45 if (_tried_to_load_library) {
46 // Do not try twice.
47 // To force retry in debugger: assign _tried_to_load_library=0
48 return false;
49 }
50 // Try to load it.
51 char ebuf[1024];
52 char buf[JVM_MAXPATHLEN];
53 os::jvm_path(buf, sizeof(buf));
54 int jvm_offset = -1;
55 {
56 // Match "jvm[^/]*" in jvm_path.
57 const char* base = buf;
58 const char* p = strrchr(buf, '/');
59 p = strstr(p ? p : base, "jvm");
60 if (p != NULL) jvm_offset = p - base;
61 }
62 if (jvm_offset >= 0) {
63 // Find the disassembler next to libjvm.so.
64 strcpy(&buf[jvm_offset], hsdis_library_name);
65 strcat(&buf[jvm_offset], os::dll_file_extension());
66 _library = hpi::dll_load(buf, ebuf, sizeof ebuf);
67 }
68 if (_library == NULL) {
69 // Try a free-floating lookup.
70 strcpy(&buf[0], hsdis_library_name);
71 strcat(&buf[0], os::dll_file_extension());
72 _library = hpi::dll_load(buf, ebuf, sizeof ebuf);
73 }
74 if (_library != NULL) {
75 _decode_instructions = CAST_TO_FN_PTR(Disassembler::decode_func,
76 hpi::dll_lookup(_library, decode_instructions_name));
77 }
78 _tried_to_load_library = true;
79 if (_decode_instructions == NULL) {
80 tty->print_cr("Could not load %s; %s; %s", buf,
81 ((_library != NULL)
82 ? "entry point is missing"
83 : (WizardMode || PrintMiscellaneous)
84 ? (const char*)ebuf
85 : "library not loadable"),
86 "PrintAssembly is disabled");
87 return false;
88 }
89
90 // Success.
91 tty->print_cr("Loaded disassembler from %s", buf);
92 return true;
93 }
94
95
96 class decode_env {
97 private:
98 nmethod* _nm;
99 CodeBlob* _code;
100 outputStream* _output;
101 address _start, _end;
102
103 char _option_buf[512];
104 char _print_raw;
105 bool _print_pc;
106 bool _print_bytes;
107 address _cur_insn;
108 int _total_ticks;
109 int _bytes_per_line; // arch-specific formatting option
110
111 static bool match(const char* event, const char* tag) {
112 size_t taglen = strlen(tag);
113 if (strncmp(event, tag, taglen) != 0)
114 return false;
115 char delim = event[taglen];
116 return delim == '\0' || delim == ' ' || delim == '/' || delim == '=';
117 }
118
119 void collect_options(const char* p) {
120 if (p == NULL || p[0] == '\0') return;
121 size_t opt_so_far = strlen(_option_buf);
122 if (opt_so_far + 1 + strlen(p) + 1 > sizeof(_option_buf)) return;
123 char* fillp = &_option_buf[opt_so_far];
124 if (opt_so_far > 0) *fillp++ = ',';
125 strcat(fillp, p);
126 // replace white space by commas:
127 char* q = fillp;
128 while ((q = strpbrk(q, " \t\n")) != NULL)
129 *q++ = ',';
130 // Note that multiple PrintAssemblyOptions flags accumulate with \n,
131 // which we want to be changed to a comma...
132 }
133
134 void print_insn_labels();
135 void print_insn_bytes(address pc0, address pc);
136 void print_address(address value);
137
138 public:
139 decode_env(CodeBlob* code, outputStream* output);
140
141 address decode_instructions(address start, address end);
142
143 void start_insn(address pc) {
144 _cur_insn = pc;
145 output()->bol();
146 print_insn_labels();
147 }
148
149 void end_insn(address pc) {
150 address pc0 = cur_insn();
151 outputStream* st = output();
152 if (_print_bytes && pc > pc0)
153 print_insn_bytes(pc0, pc);
154 if (_nm != NULL)
155 _nm->print_code_comment_on(st, COMMENT_COLUMN, pc0, pc);
156
157 // Output pc bucket ticks if we have any
158 if (total_ticks() != 0) {
159 address bucket_pc = FlatProfiler::bucket_start_for(pc);
160 if (bucket_pc != NULL && bucket_pc > pc0 && bucket_pc <= pc) {
161 int bucket_count = FlatProfiler::bucket_count_for(pc0);
162 if (bucket_count != 0) {
163 st->bol();
164 st->print_cr("%3.1f%% [%d]", bucket_count*100.0/total_ticks(), bucket_count);
165 }
166 }
167 }
168 }
169
170 address handle_event(const char* event, address arg);
171
172 outputStream* output() { return _output; }
173 address cur_insn() { return _cur_insn; }
174 int total_ticks() { return _total_ticks; }
175 void set_total_ticks(int n) { _total_ticks = n; }
176 const char* options() { return _option_buf; }
177 };
178
179 decode_env::decode_env(CodeBlob* code, outputStream* output) {
180 memset(this, 0, sizeof(*this));
181 _output = output ? output : tty;
182 _code = code;
183 if (code != NULL && code->is_nmethod())
184 _nm = (nmethod*) code;
185
186 // by default, output pc but not bytes:
187 _print_pc = true;
188 _print_bytes = false;
189 _bytes_per_line = Disassembler::pd_instruction_alignment();
190
191 // parse the global option string:
192 collect_options(Disassembler::pd_cpu_opts());
193 collect_options(PrintAssemblyOptions);
194
195 if (strstr(options(), "hsdis-")) {
196 if (strstr(options(), "hsdis-print-raw"))
197 _print_raw = (strstr(options(), "xml") ? 2 : 1);
198 if (strstr(options(), "hsdis-print-pc"))
199 _print_pc = !_print_pc;
200 if (strstr(options(), "hsdis-print-bytes"))
201 _print_bytes = !_print_bytes;
202 }
203 if (strstr(options(), "help")) {
204 tty->print_cr("PrintAssemblyOptions help:");
205 tty->print_cr(" hsdis-print-raw test plugin by requesting raw output");
206 tty->print_cr(" hsdis-print-raw-xml test plugin by requesting raw xml");
207 tty->print_cr(" hsdis-print-pc turn off PC printing (on by default)");
208 tty->print_cr(" hsdis-print-bytes turn on instruction byte output");
209 tty->print_cr("combined options: %s", options());
210 }
211 }
212
213 address decode_env::handle_event(const char* event, address arg) {
214 if (match(event, "insn")) {
215 start_insn(arg);
216 } else if (match(event, "/insn")) {
217 end_insn(arg);
218 } else if (match(event, "addr")) {
219 if (arg != NULL) {
220 print_address(arg);
221 return arg;
222 }
223 } else if (match(event, "mach")) {
224 output()->print_cr("[Disassembling for mach='%s']", arg);
225 } else if (match(event, "format bytes-per-line")) {
226 _bytes_per_line = (int) (intptr_t) arg;
227 } else {
228 // ignore unrecognized markup
229 }
230 return NULL;
231 }
232
233 // called by the disassembler to print out jump targets and data addresses
234 void decode_env::print_address(address adr) {
235 outputStream* st = _output;
236
237 if (adr == NULL) {
238 st->print("NULL");
239 return;
240 }
241
242 int small_num = (int)(intptr_t)adr;
243 if ((intptr_t)adr == (intptr_t)small_num
244 && -1 <= small_num && small_num <= 9) {
245 st->print("%d", small_num);
246 return;
247 }
248
249 if (Universe::is_fully_initialized()) {
250 if (StubRoutines::contains(adr)) {
251 StubCodeDesc* desc = StubCodeDesc::desc_for(adr);
252 if (desc == NULL)
253 desc = StubCodeDesc::desc_for(adr + frame::pc_return_offset);
254 if (desc != NULL) {
255 st->print("Stub::%s", desc->name());
256 if (desc->begin() != adr)
257 st->print("%+d 0x%p",adr - desc->begin(), adr);
258 else if (WizardMode) st->print(" " INTPTR_FORMAT, adr);
259 return;
260 }
261 st->print("Stub::<unknown> " INTPTR_FORMAT, adr);
262 return;
263 }
264
265 BarrierSet* bs = Universe::heap()->barrier_set();
266 if (bs->kind() == BarrierSet::CardTableModRef &&
267 adr == (address)((CardTableModRefBS*)(bs))->byte_map_base) {
268 st->print("word_map_base");
269 if (WizardMode) st->print(" " INTPTR_FORMAT, (intptr_t)adr);
270 return;
271 }
272
273 oop obj;
274 if (_nm != NULL
275 && (obj = _nm->embeddedOop_at(cur_insn())) != NULL
276 && (address) obj == adr) {
277 obj->print_value_on(st);
278 return;
279 }
280 }
281
282 // Fall through to a simple numeral.
283 st->print(INTPTR_FORMAT, (intptr_t)adr);
284 }
285
286 void decode_env::print_insn_labels() {
287 address p = cur_insn();
288 outputStream* st = output();
289 nmethod* nm = _nm;
290 if (nm != NULL) {
291 if (p == nm->entry_point()) st->print_cr("[Entry Point]");
292 if (p == nm->verified_entry_point()) st->print_cr("[Verified Entry Point]");
293 if (p == nm->exception_begin()) st->print_cr("[Exception Handler]");
294 if (p == nm->stub_begin()) st->print_cr("[Stub Code]");
295 if (p == nm->consts_begin()) st->print_cr("[Constants]");
296 }
297 CodeBlob* cb = _code;
298 if (cb != NULL) {
299 cb->print_block_comment(st, (intptr_t)(p - cb->instructions_begin()));
300 }
301 if (_print_pc) {
302 st->print(" " INTPTR_FORMAT ": ", (intptr_t) p);
303 }
304 }
305
306 void decode_env::print_insn_bytes(address pc, address pc_limit) {
307 outputStream* st = output();
308 size_t incr = 1;
309 size_t perline = _bytes_per_line;
310 if ((size_t) Disassembler::pd_instruction_alignment() >= sizeof(int)
311 && !((uintptr_t)pc % sizeof(int))
312 && !((uintptr_t)pc_limit % sizeof(int))) {
313 incr = sizeof(int);
314 if (perline % incr) perline += incr - (perline % incr);
315 }
316 while (pc < pc_limit) {
317 // tab to the desired column:
318 st->move_to(COMMENT_COLUMN);
319 address pc0 = pc;
320 address pc1 = pc + perline;
321 if (pc1 > pc_limit) pc1 = pc_limit;
322 for (; pc < pc1; pc += incr) {
323 if (pc == pc0)
324 st->print(BYTES_COMMENT);
325 else if ((uint)(pc - pc0) % sizeof(int) == 0)
326 st->print(" "); // put out a space on word boundaries
327 if (incr == sizeof(int))
328 st->print("%08lx", *(int*)pc);
329 else st->print("%02x", (*pc)&0xFF);
330 }
331 st->cr();
332 }
333 }
334
335
336 static void* event_to_env(void* env_pv, const char* event, void* arg) {
337 decode_env* env = (decode_env*) env_pv;
338 return env->handle_event(event, (address) arg);
339 }
340
341 static int printf_to_env(void* env_pv, const char* format, ...) {
342 decode_env* env = (decode_env*) env_pv;
343 outputStream* st = env->output();
344 size_t flen = strlen(format);
345 const char* raw = NULL;
346 if (flen == 0) return 0;
347 if (flen == 1 && format[0] == '\n') { st->bol(); return 1; }
348 if (flen < 2 ||
349 strchr(format, '%') == NULL) {
350 raw = format;
351 } else if (format[0] == '%' && format[1] == '%' &&
352 strchr(format+2, '%') == NULL) {
353 // happens a lot on machines with names like %foo
354 flen--;
355 raw = format+1;
356 }
357 if (raw != NULL) {
358 st->print_raw(raw, (int) flen);
359 return (int) flen;
360 }
361 va_list ap;
362 va_start(ap, format);
363 julong cnt0 = st->count();
364 st->vprint(format, ap);
365 julong cnt1 = st->count();
366 va_end(ap);
367 return (int)(cnt1 - cnt0);
368 }
369
370 address decode_env::decode_instructions(address start, address end) {
371 _start = start; _end = end;
372
373 assert((((intptr_t)start | (intptr_t)end) % Disassembler::pd_instruction_alignment() == 0), "misaligned insn addr");
374
375 const int show_bytes = false; // for disassembler debugging
376
377 //_version = Disassembler::pd_cpu_version();
378
379 if (!Disassembler::can_decode()) {
380 return NULL;
381 }
382
383 // decode a series of instructions and return the end of the last instruction
384
385 if (_print_raw) {
386 // Print whatever the library wants to print, w/o fancy callbacks.
387 // This is mainly for debugging the library itself.
388 FILE* out = stdout;
389 FILE* xmlout = (_print_raw > 1 ? out : NULL);
390 return (address)
391 (*Disassembler::_decode_instructions)(start, end,
392 NULL, (void*) xmlout,
393 NULL, (void*) out,
394 options());
395 }
396
397 return (address)
398 (*Disassembler::_decode_instructions)(start, end,
399 &event_to_env, (void*) this,
400 &printf_to_env, (void*) this,
401 options());
402 }
403
404
405 void Disassembler::decode(CodeBlob* cb, outputStream* st) {
406 if (!load_library()) return;
407 decode_env env(cb, st);
408 env.output()->print_cr("Decoding CodeBlob " INTPTR_FORMAT, cb);
409 env.decode_instructions(cb->instructions_begin(), cb->instructions_end());
410 }
411
412
413 void Disassembler::decode(address start, address end, outputStream* st) {
414 if (!load_library()) return;
415 decode_env env(CodeCache::find_blob_unsafe(start), st);
416 env.decode_instructions(start, end);
417 }
418
419 void Disassembler::decode(nmethod* nm, outputStream* st) {
420 if (!load_library()) return;
421 decode_env env(nm, st);
422 env.output()->print_cr("Decoding compiled method " INTPTR_FORMAT ":", nm);
423 env.output()->print_cr("Code:");
424
425 unsigned char* p = nm->instructions_begin();
426 unsigned char* end = nm->instructions_end();
427
428 // If there has been profiling, print the buckets.
429 if (FlatProfiler::bucket_start_for(p) != NULL) {
430 unsigned char* p1 = p;
431 int total_bucket_count = 0;
432 while (p1 < end) {
433 unsigned char* p0 = p1;
434 p1 += pd_instruction_alignment();
435 address bucket_pc = FlatProfiler::bucket_start_for(p1);
436 if (bucket_pc != NULL && bucket_pc > p0 && bucket_pc <= p1)
437 total_bucket_count += FlatProfiler::bucket_count_for(p0);
438 }
439 env.set_total_ticks(total_bucket_count);
440 }
441
442 env.decode_instructions(p, end);
443 }