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