Wed, 03 Jul 2019 20:42:37 +0800
Merge
1 /*
2 * Copyright (c) 1997, 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 #include "precompiled.hpp"
26 #include "interpreter/bytecodeHistogram.hpp"
27 #include "interpreter/bytecodeTracer.hpp"
28 #include "interpreter/bytecodes.hpp"
29 #include "interpreter/interpreter.hpp"
30 #include "interpreter/interpreterRuntime.hpp"
31 #include "memory/resourceArea.hpp"
32 #include "oops/methodData.hpp"
33 #include "oops/method.hpp"
34 #include "runtime/mutexLocker.hpp"
35 #include "runtime/timer.hpp"
38 #ifndef PRODUCT
40 // Standard closure for BytecodeTracer: prints the current bytecode
41 // and its attributes using bytecode-specific information.
43 class BytecodePrinter: public BytecodeClosure {
44 private:
45 // %%% This field is not GC-ed, and so can contain garbage
46 // between critical sections. Use only pointer-comparison
47 // operations on the pointer, except within a critical section.
48 // (Also, ensure that occasional false positives are benign.)
49 Method* _current_method;
50 bool _is_wide;
51 Bytecodes::Code _code;
52 address _next_pc; // current decoding position
54 void align() { _next_pc = (address)round_to((intptr_t)_next_pc, sizeof(jint)); }
55 int get_byte() { return *(jbyte*) _next_pc++; } // signed
56 short get_short() { short i=Bytes::get_Java_u2(_next_pc); _next_pc+=2; return i; }
57 int get_int() { int i=Bytes::get_Java_u4(_next_pc); _next_pc+=4; return i; }
59 int get_index_u1() { return *(address)_next_pc++; }
60 int get_index_u2() { int i=Bytes::get_Java_u2(_next_pc); _next_pc+=2; return i; }
61 int get_index_u1_cpcache() { return get_index_u1() + ConstantPool::CPCACHE_INDEX_TAG; }
62 int get_index_u2_cpcache() { int i=Bytes::get_native_u2(_next_pc); _next_pc+=2; return i + ConstantPool::CPCACHE_INDEX_TAG; }
63 int get_index_u4() { int i=Bytes::get_native_u4(_next_pc); _next_pc+=4; return i; }
64 int get_index_special() { return (is_wide()) ? get_index_u2() : get_index_u1(); }
65 Method* method() { return _current_method; }
66 bool is_wide() { return _is_wide; }
67 Bytecodes::Code raw_code() { return Bytecodes::Code(_code); }
70 bool check_index(int i, int& cp_index, outputStream* st = tty);
71 bool check_cp_cache_index(int i, int& cp_index, outputStream* st = tty);
72 bool check_obj_index(int i, int& cp_index, outputStream* st = tty);
73 bool check_invokedynamic_index(int i, int& cp_index, outputStream* st = tty);
74 void print_constant(int i, outputStream* st = tty);
75 void print_field_or_method(int i, outputStream* st = tty);
76 void print_field_or_method(int orig_i, int i, outputStream* st = tty);
77 void print_attributes(int bci, outputStream* st = tty);
78 void bytecode_epilog(int bci, outputStream* st = tty);
80 public:
81 BytecodePrinter() {
82 _is_wide = false;
83 _code = Bytecodes::_illegal;
84 }
86 // This method is called while executing the raw bytecodes, so none of
87 // the adjustments that BytecodeStream performs applies.
88 void trace(methodHandle method, address bcp, uintptr_t tos, uintptr_t tos2, outputStream* st) {
89 ResourceMark rm;
90 if (_current_method != method()) {
91 // Note 1: This code will not work as expected with true MT/MP.
92 // Need an explicit lock or a different solution.
93 // It is possible for this block to be skipped, if a garbage
94 // _current_method pointer happens to have the same bits as
95 // the incoming method. We could lose a line of trace output.
96 // This is acceptable in a debug-only feature.
97 st->cr();
98 st->print("[%ld] ", (long) Thread::current()->osthread()->thread_id());
99 method->print_name(st);
100 st->cr();
101 _current_method = method();
102 }
103 Bytecodes::Code code;
104 if (is_wide()) {
105 // bcp wasn't advanced if previous bytecode was _wide.
106 code = Bytecodes::code_at(method(), bcp+1);
107 } else {
108 code = Bytecodes::code_at(method(), bcp);
109 }
110 _code = code;
111 int bci = bcp - method->code_base();
112 st->print("[%ld] ", (long) Thread::current()->osthread()->thread_id());
113 if (Verbose) {
114 st->print("%8d %4d " INTPTR_FORMAT " " INTPTR_FORMAT " %s",
115 BytecodeCounter::counter_value(), bci, tos, tos2, Bytecodes::name(code));
116 } else {
117 st->print("%8d %4d %s",
118 BytecodeCounter::counter_value(), bci, Bytecodes::name(code));
119 }
120 _next_pc = is_wide() ? bcp+2 : bcp+1;
121 print_attributes(bci);
122 // Set is_wide for the next one, since the caller of this doesn't skip
123 // the next bytecode.
124 _is_wide = (code == Bytecodes::_wide);
125 _code = Bytecodes::_illegal;
126 }
128 // Used for Method*::print_codes(). The input bcp comes from
129 // BytecodeStream, which will skip wide bytecodes.
130 void trace(methodHandle method, address bcp, outputStream* st) {
131 _current_method = method();
132 ResourceMark rm;
133 Bytecodes::Code code = Bytecodes::code_at(method(), bcp);
134 // Set is_wide
135 _is_wide = (code == Bytecodes::_wide);
136 if (is_wide()) {
137 code = Bytecodes::code_at(method(), bcp+1);
138 }
139 _code = code;
140 int bci = bcp - method->code_base();
141 // Print bytecode index and name
142 if (is_wide()) {
143 st->print("%d %s_w", bci, Bytecodes::name(code));
144 } else {
145 st->print("%d %s", bci, Bytecodes::name(code));
146 }
147 _next_pc = is_wide() ? bcp+2 : bcp+1;
148 print_attributes(bci, st);
149 bytecode_epilog(bci, st);
150 }
151 };
154 // Implementation of BytecodeTracer
156 // %%% This set_closure thing seems overly general, given that
157 // nobody uses it. Also, if BytecodePrinter weren't hidden
158 // then Method* could use instances of it directly and it
159 // would be easier to remove races on _current_method and bcp.
160 // Since this is not product functionality, we can defer cleanup.
162 BytecodeClosure* BytecodeTracer::_closure = NULL;
164 static BytecodePrinter std_closure;
165 BytecodeClosure* BytecodeTracer::std_closure() {
166 return &::std_closure;
167 }
170 void BytecodeTracer::trace(methodHandle method, address bcp, uintptr_t tos, uintptr_t tos2, outputStream* st) {
171 if (TraceBytecodes && BytecodeCounter::counter_value() >= TraceBytecodesAt) {
172 ttyLocker ttyl; // 5065316: keep the following output coherent
173 // The ttyLocker also prevents races between two threads
174 // trying to use the single instance of BytecodePrinter.
175 // Using the ttyLocker prevents the system from coming to
176 // a safepoint within this code, which is sensitive to Method*
177 // movement.
178 //
179 // There used to be a leaf mutex here, but the ttyLocker will
180 // work just as well, as long as the printing operations never block.
181 //
182 // We put the locker on the static trace method, not the
183 // virtual one, because the clients of this module go through
184 // the static method.
185 _closure->trace(method, bcp, tos, tos2, st);
186 }
187 }
189 void BytecodeTracer::trace(methodHandle method, address bcp, outputStream* st) {
190 ttyLocker ttyl; // 5065316: keep the following output coherent
191 _closure->trace(method, bcp, st);
192 }
194 void print_symbol(Symbol* sym, outputStream* st) {
195 char buf[40];
196 int len = sym->utf8_length();
197 if (len >= (int)sizeof(buf)) {
198 st->print_cr(" %s...[%d]", sym->as_C_string(buf, sizeof(buf)), len);
199 } else {
200 st->print(" ");
201 sym->print_on(st); st->cr();
202 }
203 }
205 void print_oop(oop value, outputStream* st) {
206 if (value == NULL) {
207 st->print_cr(" NULL");
208 } else if (java_lang_String::is_instance(value)) {
209 char buf[40];
210 int len = java_lang_String::utf8_length(value);
211 java_lang_String::as_utf8_string(value, buf, sizeof(buf));
212 if (len >= (int)sizeof(buf)) {
213 st->print_cr(" %s...[%d]", buf, len);
214 } else {
215 st->print_cr(" %s", buf);
216 }
217 } else {
218 st->print_cr(" " PTR_FORMAT, p2i((void *)value));
219 }
220 }
222 bool BytecodePrinter::check_index(int i, int& cp_index, outputStream* st) {
223 ConstantPool* constants = method()->constants();
224 int ilimit = constants->length();
225 Bytecodes::Code code = raw_code();
227 ConstantPoolCache* cache = NULL;
228 if (Bytecodes::uses_cp_cache(code)) {
229 bool okay = true;
230 switch (code) {
231 case Bytecodes::_fast_aldc:
232 case Bytecodes::_fast_aldc_w:
233 okay = check_obj_index(i, cp_index, st);
234 break;
235 case Bytecodes::_invokedynamic:
236 okay = check_invokedynamic_index(i, cp_index, st);
237 break;
238 default:
239 okay = check_cp_cache_index(i, cp_index, st);
240 break;
241 }
242 if (!okay) return false;
243 }
246 // check cp index
247 if (cp_index >= 0 && cp_index < ilimit) {
248 if (WizardMode) st->print(" cp[%d]", cp_index);
249 return true;
250 }
252 st->print_cr(" CP[%d] not in CP", cp_index);
253 return false;
254 }
256 bool BytecodePrinter::check_cp_cache_index(int i, int& cp_index, outputStream* st) {
257 ConstantPool* constants = method()->constants();
258 int ilimit = constants->length(), climit = 0;
259 Bytecodes::Code code = raw_code();
261 ConstantPoolCache* cache = constants->cache();
262 // If rewriter hasn't run, the index is the cp_index
263 if (cache == NULL) {
264 cp_index = i;
265 return true;
266 }
267 //climit = cache->length(); // %%% private!
268 size_t size = cache->size() * HeapWordSize;
269 size -= sizeof(ConstantPoolCache);
270 size /= sizeof(ConstantPoolCacheEntry);
271 climit = (int) size;
273 #ifdef ASSERT
274 {
275 const int CPCACHE_INDEX_TAG = ConstantPool::CPCACHE_INDEX_TAG;
276 if (i >= CPCACHE_INDEX_TAG && i < climit + CPCACHE_INDEX_TAG) {
277 i -= CPCACHE_INDEX_TAG;
278 } else {
279 st->print_cr(" CP[%d] missing bias?", i);
280 return false;
281 }
282 }
283 #endif //ASSERT
284 if (i >= 0 && i < climit) {
285 cp_index = cache->entry_at(i)->constant_pool_index();
286 } else {
287 st->print_cr("%d not in CP[*]?", i);
288 return false;
289 }
290 return true;
291 }
294 bool BytecodePrinter::check_obj_index(int i, int& cp_index, outputStream* st) {
295 ConstantPool* constants = method()->constants();
296 i -= ConstantPool::CPCACHE_INDEX_TAG;
298 if (i >= 0 && i < constants->resolved_references()->length()) {
299 cp_index = constants->object_to_cp_index(i);
300 return true;
301 } else {
302 st->print_cr("%d not in OBJ[*]?", i);
303 return false;
304 }
305 }
308 bool BytecodePrinter::check_invokedynamic_index(int i, int& cp_index, outputStream* st) {
309 ConstantPool* constants = method()->constants();
310 assert(ConstantPool::is_invokedynamic_index(i), "not secondary index?");
311 i = ConstantPool::decode_invokedynamic_index(i) + ConstantPool::CPCACHE_INDEX_TAG;
313 return check_cp_cache_index(i, cp_index, st);
314 }
316 void BytecodePrinter::print_constant(int i, outputStream* st) {
317 int orig_i = i;
318 if (!check_index(orig_i, i, st)) return;
320 ConstantPool* constants = method()->constants();
321 constantTag tag = constants->tag_at(i);
323 if (tag.is_int()) {
324 st->print_cr(" " INT32_FORMAT, constants->int_at(i));
325 } else if (tag.is_long()) {
326 st->print_cr(" " INT64_FORMAT, (int64_t)(constants->long_at(i)));
327 } else if (tag.is_float()) {
328 st->print_cr(" %f", constants->float_at(i));
329 } else if (tag.is_double()) {
330 st->print_cr(" %f", constants->double_at(i));
331 } else if (tag.is_string()) {
332 const char* string = constants->string_at_noresolve(i);
333 st->print_cr(" %s", string);
334 } else if (tag.is_klass()) {
335 st->print_cr(" %s", constants->resolved_klass_at(i)->external_name());
336 } else if (tag.is_unresolved_klass()) {
337 st->print_cr(" <unresolved klass at %d>", i);
338 } else if (tag.is_method_type()) {
339 int i2 = constants->method_type_index_at(i);
340 st->print(" <MethodType> %d", i2);
341 print_symbol(constants->symbol_at(i2), st);
342 } else if (tag.is_method_handle()) {
343 int kind = constants->method_handle_ref_kind_at(i);
344 int i2 = constants->method_handle_index_at(i);
345 st->print(" <MethodHandle of kind %d index at %d>", kind, i2);
346 print_field_or_method(-i, i2, st);
347 } else {
348 st->print_cr(" bad tag=%d at %d", tag.value(), i);
349 }
350 }
352 void BytecodePrinter::print_field_or_method(int i, outputStream* st) {
353 int orig_i = i;
354 if (!check_index(orig_i, i, st)) return;
355 print_field_or_method(orig_i, i, st);
356 }
358 void BytecodePrinter::print_field_or_method(int orig_i, int i, outputStream* st) {
359 ConstantPool* constants = method()->constants();
360 constantTag tag = constants->tag_at(i);
362 bool has_klass = true;
364 switch (tag.value()) {
365 case JVM_CONSTANT_InterfaceMethodref:
366 case JVM_CONSTANT_Methodref:
367 case JVM_CONSTANT_Fieldref:
368 break;
369 case JVM_CONSTANT_NameAndType:
370 case JVM_CONSTANT_InvokeDynamic:
371 has_klass = false;
372 break;
373 default:
374 st->print_cr(" bad tag=%d at %d", tag.value(), i);
375 return;
376 }
378 Symbol* name = constants->uncached_name_ref_at(i);
379 Symbol* signature = constants->uncached_signature_ref_at(i);
380 const char* sep = (tag.is_field() ? "/" : "");
381 if (has_klass) {
382 Symbol* klass = constants->klass_name_at(constants->uncached_klass_ref_index_at(i));
383 st->print_cr(" %d <%s.%s%s%s> ", i, klass->as_C_string(), name->as_C_string(), sep, signature->as_C_string());
384 } else {
385 if (tag.is_invoke_dynamic()) {
386 int bsm = constants->invoke_dynamic_bootstrap_method_ref_index_at(i);
387 st->print(" bsm=%d", bsm);
388 }
389 st->print_cr(" %d <%s%s%s>", i, name->as_C_string(), sep, signature->as_C_string());
390 }
391 }
394 PRAGMA_FORMAT_NONLITERAL_IGNORED_EXTERNAL
395 void BytecodePrinter::print_attributes(int bci, outputStream* st) {
396 // Show attributes of pre-rewritten codes
397 Bytecodes::Code code = Bytecodes::java_code(raw_code());
398 // If the code doesn't have any fields there's nothing to print.
399 // note this is ==1 because the tableswitch and lookupswitch are
400 // zero size (for some reason) and we want to print stuff out for them.
401 if (Bytecodes::length_for(code) == 1) {
402 st->cr();
403 return;
404 }
406 switch(code) {
407 // Java specific bytecodes only matter.
408 case Bytecodes::_bipush:
409 st->print_cr(" " INT32_FORMAT, get_byte());
410 break;
411 case Bytecodes::_sipush:
412 st->print_cr(" " INT32_FORMAT, get_short());
413 break;
414 case Bytecodes::_ldc:
415 if (Bytecodes::uses_cp_cache(raw_code())) {
416 print_constant(get_index_u1_cpcache(), st);
417 } else {
418 print_constant(get_index_u1(), st);
419 }
420 break;
422 case Bytecodes::_ldc_w:
423 case Bytecodes::_ldc2_w:
424 if (Bytecodes::uses_cp_cache(raw_code())) {
425 print_constant(get_index_u2_cpcache(), st);
426 } else {
427 print_constant(get_index_u2(), st);
428 }
429 break;
431 case Bytecodes::_iload:
432 case Bytecodes::_lload:
433 case Bytecodes::_fload:
434 case Bytecodes::_dload:
435 case Bytecodes::_aload:
436 case Bytecodes::_istore:
437 case Bytecodes::_lstore:
438 case Bytecodes::_fstore:
439 case Bytecodes::_dstore:
440 case Bytecodes::_astore:
441 st->print_cr(" #%d", get_index_special());
442 break;
444 case Bytecodes::_iinc:
445 { int index = get_index_special();
446 jint offset = is_wide() ? get_short(): get_byte();
447 st->print_cr(" #%d " INT32_FORMAT, index, offset);
448 }
449 break;
451 case Bytecodes::_newarray: {
452 BasicType atype = (BasicType)get_index_u1();
453 const char* str = type2name(atype);
454 if (str == NULL || atype == T_OBJECT || atype == T_ARRAY) {
455 assert(false, "Unidentified basic type");
456 }
457 st->print_cr(" %s", str);
458 }
459 break;
460 case Bytecodes::_anewarray: {
461 int klass_index = get_index_u2();
462 ConstantPool* constants = method()->constants();
463 Symbol* name = constants->klass_name_at(klass_index);
464 st->print_cr(" %s ", name->as_C_string());
465 }
466 break;
467 case Bytecodes::_multianewarray: {
468 int klass_index = get_index_u2();
469 int nof_dims = get_index_u1();
470 ConstantPool* constants = method()->constants();
471 Symbol* name = constants->klass_name_at(klass_index);
472 st->print_cr(" %s %d", name->as_C_string(), nof_dims);
473 }
474 break;
476 case Bytecodes::_ifeq:
477 case Bytecodes::_ifnull:
478 case Bytecodes::_iflt:
479 case Bytecodes::_ifle:
480 case Bytecodes::_ifne:
481 case Bytecodes::_ifnonnull:
482 case Bytecodes::_ifgt:
483 case Bytecodes::_ifge:
484 case Bytecodes::_if_icmpeq:
485 case Bytecodes::_if_icmpne:
486 case Bytecodes::_if_icmplt:
487 case Bytecodes::_if_icmpgt:
488 case Bytecodes::_if_icmple:
489 case Bytecodes::_if_icmpge:
490 case Bytecodes::_if_acmpeq:
491 case Bytecodes::_if_acmpne:
492 case Bytecodes::_goto:
493 case Bytecodes::_jsr:
494 st->print_cr(" %d", bci + get_short());
495 break;
497 case Bytecodes::_goto_w:
498 case Bytecodes::_jsr_w:
499 st->print_cr(" %d", bci + get_int());
500 break;
502 case Bytecodes::_ret: st->print_cr(" %d", get_index_special()); break;
504 case Bytecodes::_tableswitch:
505 { align();
506 int default_dest = bci + get_int();
507 int lo = get_int();
508 int hi = get_int();
509 int len = hi - lo + 1;
510 jint* dest = NEW_RESOURCE_ARRAY(jint, len);
511 for (int i = 0; i < len; i++) {
512 dest[i] = bci + get_int();
513 }
514 st->print(" %d " INT32_FORMAT " " INT32_FORMAT " ",
515 default_dest, lo, hi);
516 int first = true;
517 for (int ll = lo; ll <= hi; ll++, first = false) {
518 int idx = ll - lo;
519 const char *format = first ? " %d:" INT32_FORMAT " (delta: %d)" :
520 ", %d:" INT32_FORMAT " (delta: %d)";
521 PRAGMA_DIAG_PUSH
522 PRAGMA_FORMAT_NONLITERAL_IGNORED_INTERNAL
523 st->print(format, ll, dest[idx], dest[idx]-bci);
524 PRAGMA_DIAG_POP
525 }
526 st->cr();
527 }
528 break;
529 case Bytecodes::_lookupswitch:
530 { align();
531 int default_dest = bci + get_int();
532 int len = get_int();
533 jint* key = NEW_RESOURCE_ARRAY(jint, len);
534 jint* dest = NEW_RESOURCE_ARRAY(jint, len);
535 for (int i = 0; i < len; i++) {
536 key [i] = get_int();
537 dest[i] = bci + get_int();
538 };
539 st->print(" %d %d ", default_dest, len);
540 bool first = true;
541 for (int ll = 0; ll < len; ll++, first = false) {
542 const char *format = first ? " " INT32_FORMAT ":" INT32_FORMAT :
543 ", " INT32_FORMAT ":" INT32_FORMAT ;
544 PRAGMA_DIAG_PUSH
545 PRAGMA_FORMAT_NONLITERAL_IGNORED_INTERNAL
546 st->print(format, key[ll], dest[ll]);
547 PRAGMA_DIAG_POP
548 }
549 st->cr();
550 }
551 break;
553 case Bytecodes::_putstatic:
554 case Bytecodes::_getstatic:
555 case Bytecodes::_putfield:
556 case Bytecodes::_getfield:
557 print_field_or_method(get_index_u2_cpcache(), st);
558 break;
560 case Bytecodes::_invokevirtual:
561 case Bytecodes::_invokespecial:
562 case Bytecodes::_invokestatic:
563 print_field_or_method(get_index_u2_cpcache(), st);
564 break;
566 case Bytecodes::_invokeinterface:
567 { int i = get_index_u2_cpcache();
568 int n = get_index_u1();
569 get_byte(); // ignore zero byte
570 print_field_or_method(i, st);
571 }
572 break;
574 case Bytecodes::_invokedynamic:
575 print_field_or_method(get_index_u4(), st);
576 break;
578 case Bytecodes::_new:
579 case Bytecodes::_checkcast:
580 case Bytecodes::_instanceof:
581 { int i = get_index_u2();
582 ConstantPool* constants = method()->constants();
583 Symbol* name = constants->klass_name_at(i);
584 st->print_cr(" %d <%s>", i, name->as_C_string());
585 }
586 break;
588 case Bytecodes::_wide:
589 // length is zero not one, but printed with no more info.
590 break;
592 default:
593 ShouldNotReachHere();
594 break;
595 }
596 }
599 void BytecodePrinter::bytecode_epilog(int bci, outputStream* st) {
600 MethodData* mdo = method()->method_data();
601 if (mdo != NULL) {
602 ProfileData* data = mdo->bci_to_data(bci);
603 if (data != NULL) {
604 st->print(" %d", mdo->dp_to_di(data->dp()));
605 st->fill_to(6);
606 data->print_data_on(st, mdo);
607 }
608 }
609 }
610 #endif // PRODUCT