bytecodeTracer.cpp revision 2880
0N/A/*
6323N/A * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation.
0N/A *
2362N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
0N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
2362N/A *
2362N/A */
0N/A
0N/A#include "precompiled.hpp"
0N/A#include "interpreter/bytecodeHistogram.hpp"
0N/A#include "interpreter/bytecodeTracer.hpp"
0N/A#include "interpreter/bytecodes.hpp"
0N/A#include "interpreter/interpreter.hpp"
4632N/A#include "interpreter/interpreterRuntime.hpp"
4632N/A#include "memory/resourceArea.hpp"
4632N/A#include "oops/methodDataOop.hpp"
4632N/A#include "oops/methodOop.hpp"
4632N/A#include "runtime/mutexLocker.hpp"
0N/A#include "runtime/timer.hpp"
4632N/A
0N/A
0N/A#ifndef PRODUCT
0N/A
0N/A// Standard closure for BytecodeTracer: prints the current bytecode
0N/A// and its attributes using bytecode-specific information.
6323N/A
0N/Aclass BytecodePrinter: public BytecodeClosure {
0N/A private:
0N/A // %%% This field is not GC-ed, and so can contain garbage
0N/A // between critical sections. Use only pointer-comparison
0N/A // operations on the pointer, except within a critical section.
0N/A // (Also, ensure that occasional false positives are benign.)
0N/A methodOop _current_method;
0N/A bool _is_wide;
0N/A Bytecodes::Code _code;
0N/A address _next_pc; // current decoding position
0N/A
0N/A void align() { _next_pc = (address)round_to((intptr_t)_next_pc, sizeof(jint)); }
0N/A int get_byte() { return *(jbyte*) _next_pc++; } // signed
2370N/A short get_short() { short i=Bytes::get_Java_u2(_next_pc); _next_pc+=2; return i; }
2370N/A int get_int() { int i=Bytes::get_Java_u4(_next_pc); _next_pc+=4; return i; }
2370N/A
2370N/A int get_index_u1() { return *(address)_next_pc++; }
2370N/A int get_index_u2() { int i=Bytes::get_Java_u2(_next_pc); _next_pc+=2; return i; }
0N/A int get_index_u1_cpcache() { return get_index_u1() + constantPoolOopDesc::CPCACHE_INDEX_TAG; }
0N/A int get_index_u2_cpcache() { int i=Bytes::get_native_u2(_next_pc); _next_pc+=2; return i + constantPoolOopDesc::CPCACHE_INDEX_TAG; }
0N/A int get_index_u4() { int i=Bytes::get_native_u4(_next_pc); _next_pc+=4; return i; }
0N/A int get_index_special() { return (is_wide()) ? get_index_u2() : get_index_u1(); }
0N/A methodOop method() { return _current_method; }
4632N/A bool is_wide() { return _is_wide; }
4632N/A Bytecodes::Code raw_code() { return Bytecodes::Code(_code); }
4632N/A
4632N/A
4632N/A bool check_index(int i, int& cp_index, outputStream* st = tty);
0N/A void print_constant(int i, outputStream* st = tty);
0N/A void print_field_or_method(int i, outputStream* st = tty);
0N/A void print_field_or_method(int orig_i, int i, outputStream* st = tty);
0N/A void print_attributes(int bci, outputStream* st = tty);
0N/A void bytecode_epilog(int bci, outputStream* st = tty);
0N/A
0N/A public:
0N/A BytecodePrinter() {
0N/A _is_wide = false;
0N/A _code = Bytecodes::_illegal;
0N/A }
0N/A
0N/A // This method is called while executing the raw bytecodes, so none of
0N/A // the adjustments that BytecodeStream performs applies.
0N/A void trace(methodHandle method, address bcp, uintptr_t tos, uintptr_t tos2, outputStream* st) {
0N/A ResourceMark rm;
0N/A if (_current_method != method()) {
0N/A // Note 1: This code will not work as expected with true MT/MP.
0N/A // Need an explicit lock or a different solution.
0N/A // It is possible for this block to be skipped, if a garbage
0N/A // _current_method pointer happens to have the same bits as
0N/A // the incoming method. We could lose a line of trace output.
0N/A // This is acceptable in a debug-only feature.
0N/A st->cr();
0N/A st->print("[%ld] ", (long) Thread::current()->osthread()->thread_id());
0N/A method->print_name(st);
1066N/A st->cr();
0N/A _current_method = method();
0N/A }
0N/A Bytecodes::Code code;
0N/A if (is_wide()) {
0N/A // bcp wasn't advanced if previous bytecode was _wide.
0N/A code = Bytecodes::code_at(method(), bcp+1);
0N/A } else {
4632N/A code = Bytecodes::code_at(method(), bcp);
0N/A }
0N/A _code = code;
0N/A int bci = bcp - method->code_base();
0N/A st->print("[%ld] ", (long) Thread::current()->osthread()->thread_id());
0N/A if (Verbose) {
0N/A st->print("%8d %4d " INTPTR_FORMAT " " INTPTR_FORMAT " %s",
0N/A BytecodeCounter::counter_value(), bci, tos, tos2, Bytecodes::name(code));
0N/A } else {
0N/A st->print("%8d %4d %s",
0N/A BytecodeCounter::counter_value(), bci, Bytecodes::name(code));
0N/A }
0N/A _next_pc = is_wide() ? bcp+2 : bcp+1;
0N/A print_attributes(bci);
0N/A // Set is_wide for the next one, since the caller of this doesn't skip
0N/A // the next bytecode.
0N/A _is_wide = (code == Bytecodes::_wide);
0N/A _code = Bytecodes::_illegal;
0N/A }
0N/A
0N/A // Used for methodOop::print_codes(). The input bcp comes from
0N/A // BytecodeStream, which will skip wide bytecodes.
0N/A void trace(methodHandle method, address bcp, outputStream* st) {
0N/A _current_method = method();
0N/A ResourceMark rm;
0N/A Bytecodes::Code code = Bytecodes::code_at(method(), bcp);
0N/A // Set is_wide
0N/A _is_wide = (code == Bytecodes::_wide);
0N/A if (is_wide()) {
0N/A code = Bytecodes::code_at(method(), bcp+1);
0N/A }
0N/A _code = code;
0N/A int bci = bcp - method->code_base();
0N/A // Print bytecode index and name
2370N/A if (is_wide()) {
0N/A st->print("%d %s_w", bci, Bytecodes::name(code));
0N/A } else {
3876N/A st->print("%d %s", bci, Bytecodes::name(code));
3876N/A }
3876N/A _next_pc = is_wide() ? bcp+2 : bcp+1;
3876N/A print_attributes(bci, st);
3876N/A bytecode_epilog(bci, st);
3876N/A }
3876N/A};
3876N/A
3876N/A
3876N/A// Implementation of BytecodeTracer
3876N/A
0N/A// %%% This set_closure thing seems overly general, given that
0N/A// nobody uses it. Also, if BytecodePrinter weren't hidden
3344N/A// then methodOop could use instances of it directly and it
3344N/A// would be easier to remove races on _current_method and bcp.
3344N/A// Since this is not product functionality, we can defer cleanup.
3344N/A
3344N/ABytecodeClosure* BytecodeTracer::_closure = NULL;
3344N/A
3344N/Astatic BytecodePrinter std_closure;
3344N/ABytecodeClosure* BytecodeTracer::std_closure() {
3344N/A return &::std_closure;
0N/A}
0N/A
3344N/A
3344N/Avoid BytecodeTracer::trace(methodHandle method, address bcp, uintptr_t tos, uintptr_t tos2, outputStream* st) {
3344N/A if (TraceBytecodes && BytecodeCounter::counter_value() >= TraceBytecodesAt) {
3344N/A ttyLocker ttyl; // 5065316: keep the following output coherent
3344N/A // The ttyLocker also prevents races between two threads
3344N/A // trying to use the single instance of BytecodePrinter.
3344N/A // Using the ttyLocker prevents the system from coming to
3344N/A // a safepoint within this code, which is sensitive to methodOop
0N/A // movement.
0N/A //
4632N/A // There used to be a leaf mutex here, but the ttyLocker will
4632N/A // work just as well, as long as the printing operations never block.
4632N/A //
4632N/A // We put the locker on the static trace method, not the
0N/A // virtual one, because the clients of this module go through
0N/A // the static method.
0N/A _closure->trace(method, bcp, tos, tos2, st);
0N/A }
0N/A}
0N/A
0N/Avoid BytecodeTracer::trace(methodHandle method, address bcp, outputStream* st) {
0N/A ttyLocker ttyl; // 5065316: keep the following output coherent
0N/A _closure->trace(method, bcp, st);
0N/A}
0N/A
0N/Avoid print_symbol(Symbol* sym, outputStream* st) {
0N/A char buf[40];
0N/A int len = sym->utf8_length();
0N/A if (len >= (int)sizeof(buf)) {
0N/A st->print_cr(" %s...[%d]", sym->as_C_string(buf, sizeof(buf)), len);
0N/A } else {
0N/A st->print(" ");
0N/A sym->print_on(st); st->cr();
0N/A }
0N/A}
0N/A
0N/Avoid print_oop(oop value, outputStream* st) {
0N/A if (value == NULL) {
0N/A st->print_cr(" NULL");
0N/A } else if (java_lang_String::is_instance(value)) {
0N/A char buf[40];
0N/A int len = java_lang_String::utf8_length(value);
0N/A java_lang_String::as_utf8_string(value, buf, sizeof(buf));
0N/A if (len >= (int)sizeof(buf)) {
0N/A st->print_cr(" %s...[%d]", buf, len);
0N/A } else {
0N/A st->print_cr(" %s", buf);
0N/A }
0N/A } else {
0N/A st->print_cr(" " PTR_FORMAT, (intptr_t) value);
0N/A }
0N/A}
0N/A
0N/Abool BytecodePrinter::check_index(int i, int& cp_index, outputStream* st) {
0N/A constantPoolOop constants = method()->constants();
0N/A int ilimit = constants->length(), climit = 0;
0N/A Bytecodes::Code code = raw_code();
0N/A
4632N/A constantPoolCacheOop cache = NULL;
4632N/A if (Bytecodes::uses_cp_cache(code)) {
4632N/A cache = constants->cache();
4632N/A if (cache != NULL) {
4632N/A //climit = cache->length(); // %%% private!
4632N/A size_t size = cache->size() * HeapWordSize;
4632N/A size -= sizeof(constantPoolCacheOopDesc);
0N/A size /= sizeof(ConstantPoolCacheEntry);
0N/A climit = (int) size;
0N/A }
0N/A }
5048N/A
5048N/A if (cache != NULL && constantPoolCacheOopDesc::is_secondary_index(i)) {
5048N/A i = constantPoolCacheOopDesc::decode_secondary_index(i);
0N/A st->print(" secondary cache[%d] of", i);
0N/A if (i >= 0 && i < climit) {
0N/A if (!cache->entry_at(i)->is_secondary_entry()) {
0N/A st->print_cr(" not secondary entry?", i);
5048N/A return false;
5048N/A }
5048N/A i = cache->entry_at(i)->main_entry_index() + constantPoolOopDesc::CPCACHE_INDEX_TAG;
0N/A goto check_cache_index;
4632N/A } else {
0N/A st->print_cr(" not in cache[*]?", i);
0N/A return false;
0N/A }
0N/A }
0N/A
0N/A if (cache != NULL) {
0N/A goto check_cache_index;
0N/A }
0N/A
0N/A check_cp_index:
0N/A if (i >= 0 && i < ilimit) {
0N/A if (WizardMode) st->print(" cp[%d]", i);
0N/A cp_index = i;
0N/A return true;
0N/A }
0N/A
0N/A st->print_cr(" CP[%d] not in CP", i);
0N/A return false;
0N/A
0N/A check_cache_index:
0N/A#ifdef ASSERT
0N/A {
0N/A const int CPCACHE_INDEX_TAG = constantPoolOopDesc::CPCACHE_INDEX_TAG;
0N/A if (i >= CPCACHE_INDEX_TAG && i < climit + CPCACHE_INDEX_TAG) {
0N/A i -= CPCACHE_INDEX_TAG;
0N/A } else {
0N/A st->print_cr(" CP[%d] missing bias?", i);
0N/A return false;
0N/A }
0N/A }
0N/A#endif //ASSERT
0N/A if (i >= 0 && i < climit) {
0N/A if (cache->entry_at(i)->is_secondary_entry()) {
5048N/A st->print_cr(" secondary entry?");
5048N/A return false;
5048N/A }
5048N/A i = cache->entry_at(i)->constant_pool_index();
5048N/A goto check_cp_index;
5048N/A }
0N/A st->print_cr(" not in CP[*]?", i);
0N/A return false;
5048N/A}
0N/A
0N/Avoid BytecodePrinter::print_constant(int i, outputStream* st) {
0N/A int orig_i = i;
0N/A if (!check_index(orig_i, i, st)) return;
0N/A
0N/A constantPoolOop constants = method()->constants();
0N/A constantTag tag = constants->tag_at(i);
0N/A
0N/A if (tag.is_int()) {
0N/A st->print_cr(" " INT32_FORMAT, constants->int_at(i));
0N/A } else if (tag.is_long()) {
0N/A st->print_cr(" " INT64_FORMAT, constants->long_at(i));
0N/A } else if (tag.is_float()) {
0N/A st->print_cr(" %f", constants->float_at(i));
0N/A } else if (tag.is_double()) {
0N/A st->print_cr(" %f", constants->double_at(i));
0N/A } else if (tag.is_string()) {
0N/A oop string = constants->pseudo_string_at(i);
0N/A print_oop(string, st);
0N/A } else if (tag.is_unresolved_string()) {
0N/A const char* string = constants->string_at_noresolve(i);
0N/A st->print_cr(" %s", string);
0N/A } else if (tag.is_klass()) {
110N/A st->print_cr(" %s", constants->resolved_klass_at(i)->klass_part()->external_name());
0N/A } else if (tag.is_unresolved_klass()) {
0N/A st->print_cr(" <unresolved klass at %d>", i);
0N/A } else if (tag.is_object()) {
0N/A st->print(" <Object>");
0N/A print_oop(constants->object_at(i), st);
0N/A } else if (tag.is_method_type()) {
0N/A int i2 = constants->method_type_index_at(i);
0N/A st->print(" <MethodType> %d", i2);
0N/A print_symbol(constants->symbol_at(i2), st);
110N/A } else if (tag.is_method_handle()) {
0N/A int kind = constants->method_handle_ref_kind_at(i);
0N/A int i2 = constants->method_handle_index_at(i);
0N/A st->print(" <MethodHandle of kind %d>", kind, i2);
0N/A print_field_or_method(-i, i2, st);
0N/A } else {
0N/A st->print_cr(" bad tag=%d at %d", tag.value(), i);
0N/A }
0N/A}
0N/A
void BytecodePrinter::print_field_or_method(int i, outputStream* st) {
int orig_i = i;
if (!check_index(orig_i, i, st)) return;
print_field_or_method(orig_i, i, st);
}
void BytecodePrinter::print_field_or_method(int orig_i, int i, outputStream* st) {
constantPoolOop constants = method()->constants();
constantTag tag = constants->tag_at(i);
bool has_klass = true;
switch (tag.value()) {
case JVM_CONSTANT_InterfaceMethodref:
case JVM_CONSTANT_Methodref:
case JVM_CONSTANT_Fieldref:
break;
case JVM_CONSTANT_NameAndType:
case JVM_CONSTANT_InvokeDynamic:
has_klass = false;
break;
default:
st->print_cr(" bad tag=%d at %d", tag.value(), i);
return;
}
Symbol* name = constants->uncached_name_ref_at(i);
Symbol* signature = constants->uncached_signature_ref_at(i);
const char* sep = (tag.is_field() ? "/" : "");
if (has_klass) {
Symbol* klass = constants->klass_name_at(constants->uncached_klass_ref_index_at(i));
st->print_cr(" %d <%s.%s%s%s> ", i, klass->as_C_string(), name->as_C_string(), sep, signature->as_C_string());
} else {
if (tag.is_invoke_dynamic()) {
int bsm = constants->invoke_dynamic_bootstrap_method_ref_index_at(i);
st->print(" bsm=%d", bsm);
}
st->print_cr(" %d <%s%s%s>", i, name->as_C_string(), sep, signature->as_C_string());
}
}
void BytecodePrinter::print_attributes(int bci, outputStream* st) {
// Show attributes of pre-rewritten codes
Bytecodes::Code code = Bytecodes::java_code(raw_code());
// If the code doesn't have any fields there's nothing to print.
// note this is ==1 because the tableswitch and lookupswitch are
// zero size (for some reason) and we want to print stuff out for them.
if (Bytecodes::length_for(code) == 1) {
st->cr();
return;
}
switch(code) {
// Java specific bytecodes only matter.
case Bytecodes::_bipush:
st->print_cr(" " INT32_FORMAT, get_byte());
break;
case Bytecodes::_sipush:
st->print_cr(" " INT32_FORMAT, get_short());
break;
case Bytecodes::_ldc:
if (Bytecodes::uses_cp_cache(raw_code())) {
print_constant(get_index_u1_cpcache(), st);
} else {
print_constant(get_index_u1(), st);
}
break;
case Bytecodes::_ldc_w:
case Bytecodes::_ldc2_w:
if (Bytecodes::uses_cp_cache(raw_code())) {
print_constant(get_index_u2_cpcache(), st);
} else {
print_constant(get_index_u2(), st);
}
break;
case Bytecodes::_iload:
case Bytecodes::_lload:
case Bytecodes::_fload:
case Bytecodes::_dload:
case Bytecodes::_aload:
case Bytecodes::_istore:
case Bytecodes::_lstore:
case Bytecodes::_fstore:
case Bytecodes::_dstore:
case Bytecodes::_astore:
st->print_cr(" #%d", get_index_special());
break;
case Bytecodes::_iinc:
{ int index = get_index_special();
jint offset = is_wide() ? get_short(): get_byte();
st->print_cr(" #%d " INT32_FORMAT, index, offset);
}
break;
case Bytecodes::_newarray: {
BasicType atype = (BasicType)get_index_u1();
const char* str = type2name(atype);
if (str == NULL || atype == T_OBJECT || atype == T_ARRAY) {
assert(false, "Unidentified basic type");
}
st->print_cr(" %s", str);
}
break;
case Bytecodes::_anewarray: {
int klass_index = get_index_u2();
constantPoolOop constants = method()->constants();
Symbol* name = constants->klass_name_at(klass_index);
st->print_cr(" %s ", name->as_C_string());
}
break;
case Bytecodes::_multianewarray: {
int klass_index = get_index_u2();
int nof_dims = get_index_u1();
constantPoolOop constants = method()->constants();
Symbol* name = constants->klass_name_at(klass_index);
st->print_cr(" %s %d", name->as_C_string(), nof_dims);
}
break;
case Bytecodes::_ifeq:
case Bytecodes::_ifnull:
case Bytecodes::_iflt:
case Bytecodes::_ifle:
case Bytecodes::_ifne:
case Bytecodes::_ifnonnull:
case Bytecodes::_ifgt:
case Bytecodes::_ifge:
case Bytecodes::_if_icmpeq:
case Bytecodes::_if_icmpne:
case Bytecodes::_if_icmplt:
case Bytecodes::_if_icmpgt:
case Bytecodes::_if_icmple:
case Bytecodes::_if_icmpge:
case Bytecodes::_if_acmpeq:
case Bytecodes::_if_acmpne:
case Bytecodes::_goto:
case Bytecodes::_jsr:
st->print_cr(" %d", bci + get_short());
break;
case Bytecodes::_goto_w:
case Bytecodes::_jsr_w:
st->print_cr(" %d", bci + get_int());
break;
case Bytecodes::_ret: st->print_cr(" %d", get_index_special()); break;
case Bytecodes::_tableswitch:
{ align();
int default_dest = bci + get_int();
int lo = get_int();
int hi = get_int();
int len = hi - lo + 1;
jint* dest = NEW_RESOURCE_ARRAY(jint, len);
for (int i = 0; i < len; i++) {
dest[i] = bci + get_int();
}
st->print(" %d " INT32_FORMAT " " INT32_FORMAT " ",
default_dest, lo, hi);
int first = true;
for (int ll = lo; ll <= hi; ll++, first = false) {
int idx = ll - lo;
const char *format = first ? " %d:" INT32_FORMAT " (delta: %d)" :
", %d:" INT32_FORMAT " (delta: %d)";
st->print(format, ll, dest[idx], dest[idx]-bci);
}
st->cr();
}
break;
case Bytecodes::_lookupswitch:
{ align();
int default_dest = bci + get_int();
int len = get_int();
jint* key = NEW_RESOURCE_ARRAY(jint, len);
jint* dest = NEW_RESOURCE_ARRAY(jint, len);
for (int i = 0; i < len; i++) {
key [i] = get_int();
dest[i] = bci + get_int();
};
st->print(" %d %d ", default_dest, len);
bool first = true;
for (int ll = 0; ll < len; ll++, first = false) {
const char *format = first ? " " INT32_FORMAT ":" INT32_FORMAT :
", " INT32_FORMAT ":" INT32_FORMAT ;
st->print(format, key[ll], dest[ll]);
}
st->cr();
}
break;
case Bytecodes::_putstatic:
case Bytecodes::_getstatic:
case Bytecodes::_putfield:
case Bytecodes::_getfield:
print_field_or_method(get_index_u2_cpcache(), st);
break;
case Bytecodes::_invokevirtual:
case Bytecodes::_invokespecial:
case Bytecodes::_invokestatic:
print_field_or_method(get_index_u2_cpcache(), st);
break;
case Bytecodes::_invokeinterface:
{ int i = get_index_u2_cpcache();
int n = get_index_u1();
get_byte(); // ignore zero byte
print_field_or_method(i, st);
}
break;
case Bytecodes::_invokedynamic:
print_field_or_method(get_index_u4(), st);
break;
case Bytecodes::_new:
case Bytecodes::_checkcast:
case Bytecodes::_instanceof:
{ int i = get_index_u2();
constantPoolOop constants = method()->constants();
Symbol* name = constants->klass_name_at(i);
st->print_cr(" %d <%s>", i, name->as_C_string());
}
break;
case Bytecodes::_wide:
// length is zero not one, but printed with no more info.
break;
default:
ShouldNotReachHere();
break;
}
}
void BytecodePrinter::bytecode_epilog(int bci, outputStream* st) {
methodDataOop mdo = method()->method_data();
if (mdo != NULL) {
ProfileData* data = mdo->bci_to_data(bci);
if (data != NULL) {
st->print(" %d", mdo->dp_to_di(data->dp()));
st->fill_to(6);
data->print_data_on(st);
}
}
}
#endif // PRODUCT