Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/interpreter/bytecodeTracer.cpp
32285 views
/*1* Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "interpreter/bytecodeHistogram.hpp"26#include "interpreter/bytecodeTracer.hpp"27#include "interpreter/bytecodes.hpp"28#include "interpreter/interpreter.hpp"29#include "interpreter/interpreterRuntime.hpp"30#include "memory/resourceArea.hpp"31#include "oops/methodData.hpp"32#include "oops/method.hpp"33#include "runtime/mutexLocker.hpp"34#include "runtime/timer.hpp"353637#ifndef PRODUCT3839// Standard closure for BytecodeTracer: prints the current bytecode40// and its attributes using bytecode-specific information.4142class BytecodePrinter: public BytecodeClosure {43private:44// %%% This field is not GC-ed, and so can contain garbage45// between critical sections. Use only pointer-comparison46// operations on the pointer, except within a critical section.47// (Also, ensure that occasional false positives are benign.)48Method* _current_method;49bool _is_wide;50Bytecodes::Code _code;51address _next_pc; // current decoding position5253void align() { _next_pc = (address)round_to((intptr_t)_next_pc, sizeof(jint)); }54int get_byte() { return *(jbyte*) _next_pc++; } // signed55short get_short() { short i=Bytes::get_Java_u2(_next_pc); _next_pc+=2; return i; }56int get_int() { int i=Bytes::get_Java_u4(_next_pc); _next_pc+=4; return i; }5758int get_index_u1() { return *(address)_next_pc++; }59int get_index_u2() { int i=Bytes::get_Java_u2(_next_pc); _next_pc+=2; return i; }60int get_index_u1_cpcache() { return get_index_u1() + ConstantPool::CPCACHE_INDEX_TAG; }61int get_index_u2_cpcache() { int i=Bytes::get_native_u2(_next_pc); _next_pc+=2; return i + ConstantPool::CPCACHE_INDEX_TAG; }62int get_index_u4() { int i=Bytes::get_native_u4(_next_pc); _next_pc+=4; return i; }63int get_index_special() { return (is_wide()) ? get_index_u2() : get_index_u1(); }64Method* method() { return _current_method; }65bool is_wide() { return _is_wide; }66Bytecodes::Code raw_code() { return Bytecodes::Code(_code); }676869bool check_index(int i, int& cp_index, outputStream* st = tty);70bool check_cp_cache_index(int i, int& cp_index, outputStream* st = tty);71bool check_obj_index(int i, int& cp_index, outputStream* st = tty);72bool check_invokedynamic_index(int i, int& cp_index, outputStream* st = tty);73void print_constant(int i, outputStream* st = tty);74void print_field_or_method(int i, outputStream* st = tty);75void print_field_or_method(int orig_i, int i, outputStream* st = tty);76void print_attributes(int bci, outputStream* st = tty);77void bytecode_epilog(int bci, outputStream* st = tty);7879public:80BytecodePrinter() {81_is_wide = false;82_code = Bytecodes::_illegal;83}8485// This method is called while executing the raw bytecodes, so none of86// the adjustments that BytecodeStream performs applies.87void trace(methodHandle method, address bcp, uintptr_t tos, uintptr_t tos2, outputStream* st) {88ResourceMark rm;89if (_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 garbage93// _current_method pointer happens to have the same bits as94// the incoming method. We could lose a line of trace output.95// This is acceptable in a debug-only feature.96st->cr();97st->print("[%ld] ", (long) Thread::current()->osthread()->thread_id());98method->print_name(st);99st->cr();100_current_method = method();101}102Bytecodes::Code code;103if (is_wide()) {104// bcp wasn't advanced if previous bytecode was _wide.105code = Bytecodes::code_at(method(), bcp+1);106} else {107code = Bytecodes::code_at(method(), bcp);108}109_code = code;110int bci = bcp - method->code_base();111st->print("[%ld] ", (long) Thread::current()->osthread()->thread_id());112if (Verbose) {113st->print("%8d %4d " INTPTR_FORMAT " " INTPTR_FORMAT " %s",114BytecodeCounter::counter_value(), bci, tos, tos2, Bytecodes::name(code));115} else {116st->print("%8d %4d %s",117BytecodeCounter::counter_value(), bci, Bytecodes::name(code));118}119_next_pc = is_wide() ? bcp+2 : bcp+1;120print_attributes(bci);121// Set is_wide for the next one, since the caller of this doesn't skip122// the next bytecode.123_is_wide = (code == Bytecodes::_wide);124_code = Bytecodes::_illegal;125}126127// Used for Method*::print_codes(). The input bcp comes from128// BytecodeStream, which will skip wide bytecodes.129void trace(methodHandle method, address bcp, outputStream* st) {130_current_method = method();131ResourceMark rm;132Bytecodes::Code code = Bytecodes::code_at(method(), bcp);133// Set is_wide134_is_wide = (code == Bytecodes::_wide);135if (is_wide()) {136code = Bytecodes::code_at(method(), bcp+1);137}138_code = code;139int bci = bcp - method->code_base();140// Print bytecode index and name141if (is_wide()) {142st->print("%d %s_w", bci, Bytecodes::name(code));143} else {144st->print("%d %s", bci, Bytecodes::name(code));145}146_next_pc = is_wide() ? bcp+2 : bcp+1;147print_attributes(bci, st);148bytecode_epilog(bci, st);149}150};151152153// Implementation of BytecodeTracer154155// %%% This set_closure thing seems overly general, given that156// nobody uses it. Also, if BytecodePrinter weren't hidden157// then Method* could use instances of it directly and it158// would be easier to remove races on _current_method and bcp.159// Since this is not product functionality, we can defer cleanup.160161BytecodeClosure* BytecodeTracer::_closure = NULL;162163static BytecodePrinter std_closure;164BytecodeClosure* BytecodeTracer::std_closure() {165return &::std_closure;166}167168169void BytecodeTracer::trace(methodHandle method, address bcp, uintptr_t tos, uintptr_t tos2, outputStream* st) {170if (TraceBytecodes && BytecodeCounter::counter_value() >= TraceBytecodesAt) {171ttyLocker ttyl; // 5065316: keep the following output coherent172// The ttyLocker also prevents races between two threads173// trying to use the single instance of BytecodePrinter.174// Using the ttyLocker prevents the system from coming to175// 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 will179// work just as well, as long as the printing operations never block.180//181// We put the locker on the static trace method, not the182// virtual one, because the clients of this module go through183// the static method.184_closure->trace(method, bcp, tos, tos2, st);185}186}187188void BytecodeTracer::trace(methodHandle method, address bcp, outputStream* st) {189ttyLocker ttyl; // 5065316: keep the following output coherent190_closure->trace(method, bcp, st);191}192193void print_symbol(Symbol* sym, outputStream* st) {194char buf[40];195int len = sym->utf8_length();196if (len >= (int)sizeof(buf)) {197st->print_cr(" %s...[%d]", sym->as_C_string(buf, sizeof(buf)), len);198} else {199st->print(" ");200sym->print_on(st); st->cr();201}202}203204void print_oop(oop value, outputStream* st) {205if (value == NULL) {206st->print_cr(" NULL");207} else if (java_lang_String::is_instance(value)) {208char buf[40];209int len = java_lang_String::utf8_length(value);210java_lang_String::as_utf8_string(value, buf, sizeof(buf));211if (len >= (int)sizeof(buf)) {212st->print_cr(" %s...[%d]", buf, len);213} else {214st->print_cr(" %s", buf);215}216} else {217st->print_cr(" " PTR_FORMAT, p2i((void *)value));218}219}220221bool BytecodePrinter::check_index(int i, int& cp_index, outputStream* st) {222ConstantPool* constants = method()->constants();223int ilimit = constants->length();224Bytecodes::Code code = raw_code();225226ConstantPoolCache* cache = NULL;227if (Bytecodes::uses_cp_cache(code)) {228bool okay = true;229switch (code) {230case Bytecodes::_fast_aldc:231case Bytecodes::_fast_aldc_w:232okay = check_obj_index(i, cp_index, st);233break;234case Bytecodes::_invokedynamic:235okay = check_invokedynamic_index(i, cp_index, st);236break;237default:238okay = check_cp_cache_index(i, cp_index, st);239break;240}241if (!okay) return false;242}243244245// check cp index246if (cp_index >= 0 && cp_index < ilimit) {247if (WizardMode) st->print(" cp[%d]", cp_index);248return true;249}250251st->print_cr(" CP[%d] not in CP", cp_index);252return false;253}254255bool BytecodePrinter::check_cp_cache_index(int i, int& cp_index, outputStream* st) {256ConstantPool* constants = method()->constants();257int ilimit = constants->length(), climit = 0;258Bytecodes::Code code = raw_code();259260ConstantPoolCache* cache = constants->cache();261// If rewriter hasn't run, the index is the cp_index262if (cache == NULL) {263cp_index = i;264return true;265}266//climit = cache->length(); // %%% private!267size_t size = cache->size() * HeapWordSize;268size -= sizeof(ConstantPoolCache);269size /= sizeof(ConstantPoolCacheEntry);270climit = (int) size;271272#ifdef ASSERT273{274const int CPCACHE_INDEX_TAG = ConstantPool::CPCACHE_INDEX_TAG;275if (i >= CPCACHE_INDEX_TAG && i < climit + CPCACHE_INDEX_TAG) {276i -= CPCACHE_INDEX_TAG;277} else {278st->print_cr(" CP[%d] missing bias?", i);279return false;280}281}282#endif //ASSERT283if (i >= 0 && i < climit) {284cp_index = cache->entry_at(i)->constant_pool_index();285} else {286st->print_cr("%d not in CP[*]?", i);287return false;288}289return true;290}291292293bool BytecodePrinter::check_obj_index(int i, int& cp_index, outputStream* st) {294ConstantPool* constants = method()->constants();295i -= ConstantPool::CPCACHE_INDEX_TAG;296297if (i >= 0 && i < constants->resolved_references()->length()) {298cp_index = constants->object_to_cp_index(i);299return true;300} else {301st->print_cr("%d not in OBJ[*]?", i);302return false;303}304}305306307bool BytecodePrinter::check_invokedynamic_index(int i, int& cp_index, outputStream* st) {308ConstantPool* constants = method()->constants();309assert(ConstantPool::is_invokedynamic_index(i), "not secondary index?");310i = ConstantPool::decode_invokedynamic_index(i) + ConstantPool::CPCACHE_INDEX_TAG;311312return check_cp_cache_index(i, cp_index, st);313}314315void BytecodePrinter::print_constant(int i, outputStream* st) {316int orig_i = i;317if (!check_index(orig_i, i, st)) return;318319ConstantPool* constants = method()->constants();320constantTag tag = constants->tag_at(i);321322if (tag.is_int()) {323st->print_cr(" " INT32_FORMAT, constants->int_at(i));324} else if (tag.is_long()) {325st->print_cr(" " INT64_FORMAT, (int64_t)(constants->long_at(i)));326} else if (tag.is_float()) {327st->print_cr(" %f", constants->float_at(i));328} else if (tag.is_double()) {329st->print_cr(" %f", constants->double_at(i));330} else if (tag.is_string()) {331const char* string = constants->string_at_noresolve(i);332st->print_cr(" %s", string);333} else if (tag.is_klass()) {334st->print_cr(" %s", constants->resolved_klass_at(i)->external_name());335} else if (tag.is_unresolved_klass()) {336st->print_cr(" <unresolved klass at %d>", i);337} else if (tag.is_method_type()) {338int i2 = constants->method_type_index_at(i);339st->print(" <MethodType> %d", i2);340print_symbol(constants->symbol_at(i2), st);341} else if (tag.is_method_handle()) {342int kind = constants->method_handle_ref_kind_at(i);343int i2 = constants->method_handle_index_at(i);344st->print(" <MethodHandle of kind %d index at %d>", kind, i2);345print_field_or_method(-i, i2, st);346} else {347st->print_cr(" bad tag=%d at %d", tag.value(), i);348}349}350351void BytecodePrinter::print_field_or_method(int i, outputStream* st) {352int orig_i = i;353if (!check_index(orig_i, i, st)) return;354print_field_or_method(orig_i, i, st);355}356357void BytecodePrinter::print_field_or_method(int orig_i, int i, outputStream* st) {358ConstantPool* constants = method()->constants();359constantTag tag = constants->tag_at(i);360361bool has_klass = true;362363switch (tag.value()) {364case JVM_CONSTANT_InterfaceMethodref:365case JVM_CONSTANT_Methodref:366case JVM_CONSTANT_Fieldref:367break;368case JVM_CONSTANT_NameAndType:369case JVM_CONSTANT_InvokeDynamic:370has_klass = false;371break;372default:373st->print_cr(" bad tag=%d at %d", tag.value(), i);374return;375}376377Symbol* name = constants->uncached_name_ref_at(i);378Symbol* signature = constants->uncached_signature_ref_at(i);379const char* sep = (tag.is_field() ? "/" : "");380if (has_klass) {381Symbol* klass = constants->klass_name_at(constants->uncached_klass_ref_index_at(i));382st->print_cr(" %d <%s.%s%s%s> ", i, klass->as_C_string(), name->as_C_string(), sep, signature->as_C_string());383} else {384if (tag.is_invoke_dynamic()) {385int bsm = constants->invoke_dynamic_bootstrap_method_ref_index_at(i);386st->print(" bsm=%d", bsm);387}388st->print_cr(" %d <%s%s%s>", i, name->as_C_string(), sep, signature->as_C_string());389}390}391392393PRAGMA_FORMAT_NONLITERAL_IGNORED_EXTERNAL394void BytecodePrinter::print_attributes(int bci, outputStream* st) {395// Show attributes of pre-rewritten codes396Bytecodes::Code code = Bytecodes::java_code(raw_code());397// If the code doesn't have any fields there's nothing to print.398// note this is ==1 because the tableswitch and lookupswitch are399// zero size (for some reason) and we want to print stuff out for them.400if (Bytecodes::length_for(code) == 1) {401st->cr();402return;403}404405switch(code) {406// Java specific bytecodes only matter.407case Bytecodes::_bipush:408st->print_cr(" " INT32_FORMAT, get_byte());409break;410case Bytecodes::_sipush:411st->print_cr(" " INT32_FORMAT, get_short());412break;413case Bytecodes::_ldc:414if (Bytecodes::uses_cp_cache(raw_code())) {415print_constant(get_index_u1_cpcache(), st);416} else {417print_constant(get_index_u1(), st);418}419break;420421case Bytecodes::_ldc_w:422case Bytecodes::_ldc2_w:423if (Bytecodes::uses_cp_cache(raw_code())) {424print_constant(get_index_u2_cpcache(), st);425} else {426print_constant(get_index_u2(), st);427}428break;429430case Bytecodes::_iload:431case Bytecodes::_lload:432case Bytecodes::_fload:433case Bytecodes::_dload:434case Bytecodes::_aload:435case Bytecodes::_istore:436case Bytecodes::_lstore:437case Bytecodes::_fstore:438case Bytecodes::_dstore:439case Bytecodes::_astore:440st->print_cr(" #%d", get_index_special());441break;442443case Bytecodes::_iinc:444{ int index = get_index_special();445jint offset = is_wide() ? get_short(): get_byte();446st->print_cr(" #%d " INT32_FORMAT, index, offset);447}448break;449450case Bytecodes::_newarray: {451BasicType atype = (BasicType)get_index_u1();452const char* str = type2name(atype);453if (str == NULL || atype == T_OBJECT || atype == T_ARRAY) {454assert(false, "Unidentified basic type");455}456st->print_cr(" %s", str);457}458break;459case Bytecodes::_anewarray: {460int klass_index = get_index_u2();461ConstantPool* constants = method()->constants();462Symbol* name = constants->klass_name_at(klass_index);463st->print_cr(" %s ", name->as_C_string());464}465break;466case Bytecodes::_multianewarray: {467int klass_index = get_index_u2();468int nof_dims = get_index_u1();469ConstantPool* constants = method()->constants();470Symbol* name = constants->klass_name_at(klass_index);471st->print_cr(" %s %d", name->as_C_string(), nof_dims);472}473break;474475case Bytecodes::_ifeq:476case Bytecodes::_ifnull:477case Bytecodes::_iflt:478case Bytecodes::_ifle:479case Bytecodes::_ifne:480case Bytecodes::_ifnonnull:481case Bytecodes::_ifgt:482case Bytecodes::_ifge:483case Bytecodes::_if_icmpeq:484case Bytecodes::_if_icmpne:485case Bytecodes::_if_icmplt:486case Bytecodes::_if_icmpgt:487case Bytecodes::_if_icmple:488case Bytecodes::_if_icmpge:489case Bytecodes::_if_acmpeq:490case Bytecodes::_if_acmpne:491case Bytecodes::_goto:492case Bytecodes::_jsr:493st->print_cr(" %d", bci + get_short());494break;495496case Bytecodes::_goto_w:497case Bytecodes::_jsr_w:498st->print_cr(" %d", bci + get_int());499break;500501case Bytecodes::_ret: st->print_cr(" %d", get_index_special()); break;502503case Bytecodes::_tableswitch:504{ align();505int default_dest = bci + get_int();506int lo = get_int();507int hi = get_int();508int len = hi - lo + 1;509jint* dest = NEW_RESOURCE_ARRAY(jint, len);510for (int i = 0; i < len; i++) {511dest[i] = bci + get_int();512}513st->print(" %d " INT32_FORMAT " " INT32_FORMAT " ",514default_dest, lo, hi);515int first = true;516for (int ll = lo; ll <= hi; ll++, first = false) {517int idx = ll - lo;518const char *format = first ? " %d:" INT32_FORMAT " (delta: %d)" :519", %d:" INT32_FORMAT " (delta: %d)";520PRAGMA_DIAG_PUSH521PRAGMA_FORMAT_NONLITERAL_IGNORED_INTERNAL522st->print(format, ll, dest[idx], dest[idx]-bci);523PRAGMA_DIAG_POP524}525st->cr();526}527break;528case Bytecodes::_lookupswitch:529{ align();530int default_dest = bci + get_int();531int len = get_int();532jint* key = NEW_RESOURCE_ARRAY(jint, len);533jint* dest = NEW_RESOURCE_ARRAY(jint, len);534for (int i = 0; i < len; i++) {535key [i] = get_int();536dest[i] = bci + get_int();537};538st->print(" %d %d ", default_dest, len);539bool first = true;540for (int ll = 0; ll < len; ll++, first = false) {541const char *format = first ? " " INT32_FORMAT ":" INT32_FORMAT :542", " INT32_FORMAT ":" INT32_FORMAT ;543PRAGMA_DIAG_PUSH544PRAGMA_FORMAT_NONLITERAL_IGNORED_INTERNAL545st->print(format, key[ll], dest[ll]);546PRAGMA_DIAG_POP547}548st->cr();549}550break;551552case Bytecodes::_putstatic:553case Bytecodes::_getstatic:554case Bytecodes::_putfield:555case Bytecodes::_getfield:556print_field_or_method(get_index_u2_cpcache(), st);557break;558559case Bytecodes::_invokevirtual:560case Bytecodes::_invokespecial:561case Bytecodes::_invokestatic:562print_field_or_method(get_index_u2_cpcache(), st);563break;564565case Bytecodes::_invokeinterface:566{ int i = get_index_u2_cpcache();567int n = get_index_u1();568get_byte(); // ignore zero byte569print_field_or_method(i, st);570}571break;572573case Bytecodes::_invokedynamic:574print_field_or_method(get_index_u4(), st);575break;576577case Bytecodes::_new:578case Bytecodes::_checkcast:579case Bytecodes::_instanceof:580{ int i = get_index_u2();581ConstantPool* constants = method()->constants();582Symbol* name = constants->klass_name_at(i);583st->print_cr(" %d <%s>", i, name->as_C_string());584}585break;586587case Bytecodes::_wide:588// length is zero not one, but printed with no more info.589break;590591default:592ShouldNotReachHere();593break;594}595}596597598void BytecodePrinter::bytecode_epilog(int bci, outputStream* st) {599MethodData* mdo = method()->method_data();600if (mdo != NULL) {601ProfileData* data = mdo->bci_to_data(bci);602if (data != NULL) {603st->print(" %d", mdo->dp_to_di(data->dp()));604st->fill_to(6);605data->print_data_on(st, mdo);606}607}608}609#endif // PRODUCT610611612