Path: blob/master/src/hotspot/share/interpreter/bytecodeTracer.cpp
40949 views
/*1* Copyright (c) 1997, 2021, 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 "classfile/javaClasses.inline.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/constantPool.inline.hpp"33#include "oops/methodData.hpp"34#include "oops/method.hpp"35#include "runtime/mutexLocker.hpp"36#include "runtime/osThread.hpp"37#include "runtime/timer.hpp"38#include "utilities/align.hpp"394041// Standard closure for BytecodeTracer: prints the current bytecode42// and its attributes using bytecode-specific information.4344class BytecodePrinter: public BytecodeClosure {45private:46// %%% This field is not GC-ed, and so can contain garbage47// between critical sections. Use only pointer-comparison48// operations on the pointer, except within a critical section.49// (Also, ensure that occasional false positives are benign.)50Method* _current_method;51bool _is_wide;52Bytecodes::Code _code;53address _next_pc; // current decoding position5455void align() { _next_pc = align_up(_next_pc, sizeof(jint)); }56int get_byte() { return *(jbyte*) _next_pc++; } // signed57short get_short() { short i=Bytes::get_Java_u2(_next_pc); _next_pc+=2; return i; }58int get_int() { int i=Bytes::get_Java_u4(_next_pc); _next_pc+=4; return i; }5960int get_index_u1() { return *(address)_next_pc++; }61int get_index_u2() { int i=Bytes::get_Java_u2(_next_pc); _next_pc+=2; return i; }62int get_index_u1_cpcache() { return get_index_u1() + ConstantPool::CPCACHE_INDEX_TAG; }63int get_index_u2_cpcache() { int i=Bytes::get_native_u2(_next_pc); _next_pc+=2; return i + ConstantPool::CPCACHE_INDEX_TAG; }64int get_index_u4() { int i=Bytes::get_native_u4(_next_pc); _next_pc+=4; return i; }65int get_index_special() { return (is_wide()) ? get_index_u2() : get_index_u1(); }66Method* method() { return _current_method; }67bool is_wide() { return _is_wide; }68Bytecodes::Code raw_code() { return Bytecodes::Code(_code); }697071bool check_index(int i, int& cp_index, outputStream* st = tty);72bool check_cp_cache_index(int i, int& cp_index, outputStream* st = tty);73bool check_obj_index(int i, int& cp_index, outputStream* st = tty);74bool check_invokedynamic_index(int i, int& cp_index, outputStream* st = tty);75void print_constant(int i, outputStream* st = tty);76void print_field_or_method(int i, outputStream* st = tty);77void print_field_or_method(int orig_i, int i, outputStream* st = tty);78void print_attributes(int bci, outputStream* st = tty);79void bytecode_epilog(int bci, outputStream* st = tty);8081public:82BytecodePrinter() {83_is_wide = false;84_code = Bytecodes::_illegal;85}8687// This method is called while executing the raw bytecodes, so none of88// the adjustments that BytecodeStream performs applies.89void trace(const methodHandle& method, address bcp, uintptr_t tos, uintptr_t tos2, outputStream* st) {90ResourceMark rm;91if (_current_method != method()) {92// Note 1: This code will not work as expected with true MT/MP.93// Need an explicit lock or a different solution.94// It is possible for this block to be skipped, if a garbage95// _current_method pointer happens to have the same bits as96// the incoming method. We could lose a line of trace output.97// This is acceptable in a debug-only feature.98st->cr();99st->print("[%ld] ", (long) Thread::current()->osthread()->thread_id());100method->print_name(st);101st->cr();102_current_method = method();103}104Bytecodes::Code code;105if (is_wide()) {106// bcp wasn't advanced if previous bytecode was _wide.107code = Bytecodes::code_at(method(), bcp+1);108} else {109code = Bytecodes::code_at(method(), bcp);110}111_code = code;112int bci = bcp - method->code_base();113st->print("[%ld] ", (long) Thread::current()->osthread()->thread_id());114if (Verbose) {115st->print("%8d %4d " INTPTR_FORMAT " " INTPTR_FORMAT " %s",116BytecodeCounter::counter_value(), bci, tos, tos2, Bytecodes::name(code));117} else {118st->print("%8d %4d %s",119BytecodeCounter::counter_value(), bci, Bytecodes::name(code));120}121_next_pc = is_wide() ? bcp+2 : bcp+1;122print_attributes(bci);123// Set is_wide for the next one, since the caller of this doesn't skip124// the next bytecode.125_is_wide = (code == Bytecodes::_wide);126_code = Bytecodes::_illegal;127}128129// Used for Method*::print_codes(). The input bcp comes from130// BytecodeStream, which will skip wide bytecodes.131void trace(const methodHandle& method, address bcp, outputStream* st) {132_current_method = method();133ResourceMark rm;134Bytecodes::Code code = Bytecodes::code_at(method(), bcp);135// Set is_wide136_is_wide = (code == Bytecodes::_wide);137if (is_wide()) {138code = Bytecodes::code_at(method(), bcp+1);139}140_code = code;141int bci = bcp - method->code_base();142// Print bytecode index and name143if (is_wide()) {144st->print("%d %s_w", bci, Bytecodes::name(code));145} else {146st->print("%d %s", bci, Bytecodes::name(code));147}148_next_pc = is_wide() ? bcp+2 : bcp+1;149print_attributes(bci, st);150bytecode_epilog(bci, st);151}152};153154155// Implementation of BytecodeTracer156157// %%% This set_closure thing seems overly general, given that158// nobody uses it. Also, if BytecodePrinter weren't hidden159// then Method* could use instances of it directly and it160// would be easier to remove races on _current_method and bcp.161// Since this is not product functionality, we can defer cleanup.162163BytecodeClosure* BytecodeTracer::_closure = NULL;164165static BytecodePrinter std_closure;166BytecodeClosure* BytecodeTracer::std_closure() {167return &::std_closure;168}169170171void BytecodeTracer::trace(const methodHandle& method, address bcp, uintptr_t tos, uintptr_t tos2, outputStream* st) {172if (TraceBytecodes && BytecodeCounter::counter_value() >= TraceBytecodesAt) {173ttyLocker ttyl; // 5065316: keep the following output coherent174// The ttyLocker also prevents races between two threads175// trying to use the single instance of BytecodePrinter.176// Using the ttyLocker prevents the system from coming to177// a safepoint within this code, which is sensitive to Method*178// movement.179//180// There used to be a leaf mutex here, but the ttyLocker will181// work just as well, as long as the printing operations never block.182//183// We put the locker on the static trace method, not the184// virtual one, because the clients of this module go through185// the static method.186_closure->trace(method, bcp, tos, tos2, st);187}188}189190void BytecodeTracer::trace(const methodHandle& method, address bcp, outputStream* st) {191ttyLocker ttyl; // 5065316: keep the following output coherent192_closure->trace(method, bcp, st);193}194195void print_symbol(Symbol* sym, outputStream* st) {196char buf[40];197int len = sym->utf8_length();198if (len >= (int)sizeof(buf)) {199st->print_cr(" %s...[%d]", sym->as_C_string(buf, sizeof(buf)), len);200} else {201st->print(" ");202sym->print_on(st); st->cr();203}204}205206void print_oop(oop value, outputStream* st) {207if (value == NULL) {208st->print_cr(" NULL");209} else if (java_lang_String::is_instance(value)) {210char buf[40];211int len = java_lang_String::utf8_length(value);212java_lang_String::as_utf8_string(value, buf, sizeof(buf));213if (len >= (int)sizeof(buf)) {214st->print_cr(" %s...[%d]", buf, len);215} else {216st->print_cr(" %s", buf);217}218} else {219st->print_cr(" " INTPTR_FORMAT, p2i((void *)value));220}221}222223bool BytecodePrinter::check_index(int i, int& cp_index, outputStream* st) {224ConstantPool* constants = method()->constants();225int ilimit = constants->length();226Bytecodes::Code code = raw_code();227228if (Bytecodes::uses_cp_cache(code)) {229bool okay = true;230switch (code) {231case Bytecodes::_fast_aldc:232case Bytecodes::_fast_aldc_w:233okay = check_obj_index(i, cp_index, st);234break;235case Bytecodes::_invokedynamic:236okay = check_invokedynamic_index(i, cp_index, st);237break;238default:239okay = check_cp_cache_index(i, cp_index, st);240break;241}242if (!okay) return false;243}244245246// check cp index247if (cp_index >= 0 && cp_index < ilimit) {248if (WizardMode) st->print(" cp[%d]", cp_index);249return true;250}251252st->print_cr(" CP[%d] not in CP", cp_index);253return false;254}255256bool BytecodePrinter::check_cp_cache_index(int i, int& cp_index, outputStream* st) {257ConstantPool* constants = method()->constants();258int climit = 0;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() * wordSize;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) {308assert(ConstantPool::is_invokedynamic_index(i), "not secondary index?");309i = ConstantPool::decode_invokedynamic_index(i) + ConstantPool::CPCACHE_INDEX_TAG;310311return check_cp_cache_index(i, cp_index, st);312}313314void BytecodePrinter::print_constant(int i, outputStream* st) {315int orig_i = i;316if (!check_index(orig_i, i, st)) return;317318ConstantPool* constants = method()->constants();319constantTag tag = constants->tag_at(i);320321if (tag.is_int()) {322st->print_cr(" " INT32_FORMAT, constants->int_at(i));323} else if (tag.is_long()) {324st->print_cr(" " INT64_FORMAT, (int64_t)(constants->long_at(i)));325} else if (tag.is_float()) {326st->print_cr(" %f", constants->float_at(i));327} else if (tag.is_double()) {328st->print_cr(" %f", constants->double_at(i));329} else if (tag.is_string()) {330const char* string = constants->string_at_noresolve(i);331st->print_cr(" %s", string);332} else if (tag.is_klass()) {333st->print_cr(" %s", constants->resolved_klass_at(i)->external_name());334} else if (tag.is_unresolved_klass()) {335st->print_cr(" <unresolved klass at %d>", i);336} else if (tag.is_method_type()) {337int i2 = constants->method_type_index_at(i);338st->print(" <MethodType> %d", i2);339print_symbol(constants->symbol_at(i2), st);340} else if (tag.is_method_handle()) {341int kind = constants->method_handle_ref_kind_at(i);342int i2 = constants->method_handle_index_at(i);343st->print(" <MethodHandle of kind %d index at %d>", kind, i2);344print_field_or_method(-i, i2, st);345} else {346st->print_cr(" bad tag=%d at %d", tag.value(), i);347}348}349350void BytecodePrinter::print_field_or_method(int i, outputStream* st) {351int orig_i = i;352if (!check_index(orig_i, i, st)) return;353print_field_or_method(orig_i, i, st);354}355356void BytecodePrinter::print_field_or_method(int orig_i, int i, outputStream* st) {357ConstantPool* constants = method()->constants();358constantTag tag = constants->tag_at(i);359360bool has_klass = true;361362switch (tag.value()) {363case JVM_CONSTANT_InterfaceMethodref:364case JVM_CONSTANT_Methodref:365case JVM_CONSTANT_Fieldref:366break;367case JVM_CONSTANT_NameAndType:368case JVM_CONSTANT_Dynamic: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_dynamic_constant() || tag.is_invoke_dynamic()) {385int bsm = constants->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}391392393void BytecodePrinter::print_attributes(int bci, outputStream* st) {394// Show attributes of pre-rewritten codes395Bytecodes::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 are398// zero size (for some reason) and we want to print stuff out for them.399if (Bytecodes::length_for(code) == 1) {400st->cr();401return;402}403404switch(code) {405// Java specific bytecodes only matter.406case Bytecodes::_bipush:407st->print_cr(" " INT32_FORMAT, get_byte());408break;409case Bytecodes::_sipush:410st->print_cr(" " INT32_FORMAT, get_short());411break;412case Bytecodes::_ldc:413if (Bytecodes::uses_cp_cache(raw_code())) {414print_constant(get_index_u1_cpcache(), st);415} else {416print_constant(get_index_u1(), st);417}418break;419420case Bytecodes::_ldc_w:421case Bytecodes::_ldc2_w:422if (Bytecodes::uses_cp_cache(raw_code())) {423print_constant(get_index_u2_cpcache(), st);424} else {425print_constant(get_index_u2(), st);426}427break;428429case Bytecodes::_iload:430case Bytecodes::_lload:431case Bytecodes::_fload:432case Bytecodes::_dload:433case Bytecodes::_aload:434case Bytecodes::_istore:435case Bytecodes::_lstore:436case Bytecodes::_fstore:437case Bytecodes::_dstore:438case Bytecodes::_astore:439st->print_cr(" #%d", get_index_special());440break;441442case Bytecodes::_iinc:443{ int index = get_index_special();444jint offset = is_wide() ? get_short(): get_byte();445st->print_cr(" #%d " INT32_FORMAT, index, offset);446}447break;448449case Bytecodes::_newarray: {450BasicType atype = (BasicType)get_index_u1();451const char* str = type2name(atype);452if (str == NULL || is_reference_type(atype)) {453assert(false, "Unidentified basic type");454}455st->print_cr(" %s", str);456}457break;458case Bytecodes::_anewarray: {459int klass_index = get_index_u2();460ConstantPool* constants = method()->constants();461Symbol* name = constants->klass_name_at(klass_index);462st->print_cr(" %s ", name->as_C_string());463}464break;465case Bytecodes::_multianewarray: {466int klass_index = get_index_u2();467int nof_dims = get_index_u1();468ConstantPool* constants = method()->constants();469Symbol* name = constants->klass_name_at(klass_index);470st->print_cr(" %s %d", name->as_C_string(), nof_dims);471}472break;473474case Bytecodes::_ifeq:475case Bytecodes::_ifnull:476case Bytecodes::_iflt:477case Bytecodes::_ifle:478case Bytecodes::_ifne:479case Bytecodes::_ifnonnull:480case Bytecodes::_ifgt:481case Bytecodes::_ifge:482case Bytecodes::_if_icmpeq:483case Bytecodes::_if_icmpne:484case Bytecodes::_if_icmplt:485case Bytecodes::_if_icmpgt:486case Bytecodes::_if_icmple:487case Bytecodes::_if_icmpge:488case Bytecodes::_if_acmpeq:489case Bytecodes::_if_acmpne:490case Bytecodes::_goto:491case Bytecodes::_jsr:492st->print_cr(" %d", bci + get_short());493break;494495case Bytecodes::_goto_w:496case Bytecodes::_jsr_w:497st->print_cr(" %d", bci + get_int());498break;499500case Bytecodes::_ret: st->print_cr(" %d", get_index_special()); break;501502case Bytecodes::_tableswitch:503{ align();504int default_dest = bci + get_int();505int lo = get_int();506int hi = get_int();507int len = hi - lo + 1;508jint* dest = NEW_RESOURCE_ARRAY(jint, len);509for (int i = 0; i < len; i++) {510dest[i] = bci + get_int();511}512st->print(" %d " INT32_FORMAT " " INT32_FORMAT " ",513default_dest, lo, hi);514const char *comma = "";515for (int ll = lo; ll <= hi; ll++) {516int idx = ll - lo;517st->print("%s %d:" INT32_FORMAT " (delta: %d)", comma, ll, dest[idx], dest[idx]-bci);518comma = ",";519}520st->cr();521}522break;523case Bytecodes::_lookupswitch:524{ align();525int default_dest = bci + get_int();526int len = get_int();527jint* key = NEW_RESOURCE_ARRAY(jint, len);528jint* dest = NEW_RESOURCE_ARRAY(jint, len);529for (int i = 0; i < len; i++) {530key [i] = get_int();531dest[i] = bci + get_int();532};533st->print(" %d %d ", default_dest, len);534const char *comma = "";535for (int ll = 0; ll < len; ll++) {536st->print("%s " INT32_FORMAT ":" INT32_FORMAT, comma, key[ll], dest[ll]);537comma = ",";538}539st->cr();540}541break;542543case Bytecodes::_putstatic:544case Bytecodes::_getstatic:545case Bytecodes::_putfield:546case Bytecodes::_getfield:547print_field_or_method(get_index_u2_cpcache(), st);548break;549550case Bytecodes::_invokevirtual:551case Bytecodes::_invokespecial:552case Bytecodes::_invokestatic:553print_field_or_method(get_index_u2_cpcache(), st);554break;555556case Bytecodes::_invokeinterface:557{ int i = get_index_u2_cpcache();558int n = get_index_u1();559get_byte(); // ignore zero byte560print_field_or_method(i, st);561}562break;563564case Bytecodes::_invokedynamic:565print_field_or_method(get_index_u4(), st);566break;567568case Bytecodes::_new:569case Bytecodes::_checkcast:570case Bytecodes::_instanceof:571{ int i = get_index_u2();572ConstantPool* constants = method()->constants();573Symbol* name = constants->klass_name_at(i);574st->print_cr(" %d <%s>", i, name->as_C_string());575}576break;577578case Bytecodes::_wide:579// length is zero not one, but printed with no more info.580break;581582default:583ShouldNotReachHere();584break;585}586}587588589void BytecodePrinter::bytecode_epilog(int bci, outputStream* st) {590MethodData* mdo = method()->method_data();591if (mdo != NULL) {592ProfileData* data = mdo->bci_to_data(bci);593if (data != NULL) {594st->print(" %d", mdo->dp_to_di(data->dp()));595st->fill_to(6);596data->print_data_on(st, mdo);597}598}599}600601602