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