Path: blob/master/src/hotspot/share/runtime/frame.cpp
40951 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/moduleEntry.hpp"26#include "code/codeCache.hpp"27#include "code/vmreg.inline.hpp"28#include "compiler/abstractCompiler.hpp"29#include "compiler/disassembler.hpp"30#include "compiler/oopMap.hpp"31#include "gc/shared/collectedHeap.inline.hpp"32#include "interpreter/interpreter.hpp"33#include "interpreter/oopMapCache.hpp"34#include "memory/resourceArea.hpp"35#include "memory/universe.hpp"36#include "oops/markWord.hpp"37#include "oops/method.hpp"38#include "oops/methodData.hpp"39#include "oops/oop.inline.hpp"40#include "oops/verifyOopClosure.hpp"41#include "prims/methodHandles.hpp"42#include "runtime/frame.inline.hpp"43#include "runtime/handles.inline.hpp"44#include "runtime/javaCalls.hpp"45#include "runtime/monitorChunk.hpp"46#include "runtime/os.hpp"47#include "runtime/sharedRuntime.hpp"48#include "runtime/signature.hpp"49#include "runtime/stubCodeGenerator.hpp"50#include "runtime/stubRoutines.hpp"51#include "runtime/thread.inline.hpp"52#include "utilities/debug.hpp"53#include "utilities/decoder.hpp"54#include "utilities/formatBuffer.hpp"5556RegisterMap::RegisterMap(JavaThread *thread, bool update_map, bool process_frames) {57_thread = thread;58_update_map = update_map;59_process_frames = process_frames;60clear();61debug_only(_update_for_id = NULL;)62#ifndef PRODUCT63for (int i = 0; i < reg_count ; i++ ) _location[i] = NULL;64#endif /* PRODUCT */65}6667RegisterMap::RegisterMap(const RegisterMap* map) {68assert(map != this, "bad initialization parameter");69assert(map != NULL, "RegisterMap must be present");70_thread = map->thread();71_update_map = map->update_map();72_process_frames = map->process_frames();73_include_argument_oops = map->include_argument_oops();74debug_only(_update_for_id = map->_update_for_id;)75pd_initialize_from(map);76if (update_map()) {77for(int i = 0; i < location_valid_size; i++) {78LocationValidType bits = !update_map() ? 0 : map->_location_valid[i];79_location_valid[i] = bits;80// for whichever bits are set, pull in the corresponding map->_location81int j = i*location_valid_type_size;82while (bits != 0) {83if ((bits & 1) != 0) {84assert(0 <= j && j < reg_count, "range check");85_location[j] = map->_location[j];86}87bits >>= 1;88j += 1;89}90}91}92}9394void RegisterMap::clear() {95set_include_argument_oops(true);96if (_update_map) {97for(int i = 0; i < location_valid_size; i++) {98_location_valid[i] = 0;99}100pd_clear();101} else {102pd_initialize();103}104}105106#ifndef PRODUCT107108void RegisterMap::print_on(outputStream* st) const {109st->print_cr("Register map");110for(int i = 0; i < reg_count; i++) {111112VMReg r = VMRegImpl::as_VMReg(i);113intptr_t* src = (intptr_t*) location(r);114if (src != NULL) {115116r->print_on(st);117st->print(" [" INTPTR_FORMAT "] = ", p2i(src));118if (((uintptr_t)src & (sizeof(*src)-1)) != 0) {119st->print_cr("<misaligned>");120} else {121st->print_cr(INTPTR_FORMAT, *src);122}123}124}125}126127void RegisterMap::print() const {128print_on(tty);129}130131#endif132// This returns the pc that if you were in the debugger you'd see. Not133// the idealized value in the frame object. This undoes the magic conversion134// that happens for deoptimized frames. In addition it makes the value the135// hardware would want to see in the native frame. The only user (at this point)136// is deoptimization. It likely no one else should ever use it.137138address frame::raw_pc() const {139if (is_deoptimized_frame()) {140CompiledMethod* cm = cb()->as_compiled_method_or_null();141if (cm->is_method_handle_return(pc()))142return cm->deopt_mh_handler_begin() - pc_return_offset;143else144return cm->deopt_handler_begin() - pc_return_offset;145} else {146return (pc() - pc_return_offset);147}148}149150// Change the pc in a frame object. This does not change the actual pc in151// actual frame. To do that use patch_pc.152//153void frame::set_pc(address newpc ) {154#ifdef ASSERT155if (_cb != NULL && _cb->is_nmethod()) {156assert(!((nmethod*)_cb)->is_deopt_pc(_pc), "invariant violation");157}158#endif // ASSERT159160// Unsafe to use the is_deoptimized tester after changing pc161_deopt_state = unknown;162_pc = newpc;163_cb = CodeCache::find_blob_unsafe(_pc);164165}166167// type testers168bool frame::is_ignored_frame() const {169return false; // FIXME: some LambdaForm frames should be ignored170}171bool frame::is_deoptimized_frame() const {172assert(_deopt_state != unknown, "not answerable");173return _deopt_state == is_deoptimized;174}175176bool frame::is_native_frame() const {177return (_cb != NULL &&178_cb->is_nmethod() &&179((nmethod*)_cb)->is_native_method());180}181182bool frame::is_java_frame() const {183if (is_interpreted_frame()) return true;184if (is_compiled_frame()) return true;185return false;186}187188189bool frame::is_compiled_frame() const {190if (_cb != NULL &&191_cb->is_compiled() &&192((CompiledMethod*)_cb)->is_java_method()) {193return true;194}195return false;196}197198199bool frame::is_runtime_frame() const {200return (_cb != NULL && _cb->is_runtime_stub());201}202203bool frame::is_safepoint_blob_frame() const {204return (_cb != NULL && _cb->is_safepoint_stub());205}206207// testers208209bool frame::is_first_java_frame() const {210RegisterMap map(JavaThread::current(), false); // No update211frame s;212for (s = sender(&map); !(s.is_java_frame() || s.is_first_frame()); s = s.sender(&map));213return s.is_first_frame();214}215216217bool frame::entry_frame_is_first() const {218return entry_frame_call_wrapper()->is_first_frame();219}220221JavaCallWrapper* frame::entry_frame_call_wrapper_if_safe(JavaThread* thread) const {222JavaCallWrapper** jcw = entry_frame_call_wrapper_addr();223address addr = (address) jcw;224225// addr must be within the usable part of the stack226if (thread->is_in_usable_stack(addr)) {227return *jcw;228}229230return NULL;231}232233bool frame::is_entry_frame_valid(JavaThread* thread) const {234// Validate the JavaCallWrapper an entry frame must have235address jcw = (address)entry_frame_call_wrapper();236if (!thread->is_in_stack_range_excl(jcw, (address)fp())) {237return false;238}239240// Validate sp saved in the java frame anchor241JavaFrameAnchor* jfa = entry_frame_call_wrapper()->anchor();242return (jfa->last_Java_sp() > sp());243}244245bool frame::should_be_deoptimized() const {246if (_deopt_state == is_deoptimized ||247!is_compiled_frame() ) return false;248assert(_cb != NULL && _cb->is_compiled(), "must be an nmethod");249CompiledMethod* nm = (CompiledMethod *)_cb;250if (TraceDependencies) {251tty->print("checking (%s) ", nm->is_marked_for_deoptimization() ? "true" : "false");252nm->print_value_on(tty);253tty->cr();254}255256if( !nm->is_marked_for_deoptimization() )257return false;258259// If at the return point, then the frame has already been popped, and260// only the return needs to be executed. Don't deoptimize here.261return !nm->is_at_poll_return(pc());262}263264bool frame::can_be_deoptimized() const {265if (!is_compiled_frame()) return false;266CompiledMethod* nm = (CompiledMethod*)_cb;267268if( !nm->can_be_deoptimized() )269return false;270271return !nm->is_at_poll_return(pc());272}273274void frame::deoptimize(JavaThread* thread) {275assert(thread->frame_anchor()->has_last_Java_frame() &&276thread->frame_anchor()->walkable(), "must be");277// Schedule deoptimization of an nmethod activation with this frame.278assert(_cb != NULL && _cb->is_compiled(), "must be");279280// If the call site is a MethodHandle call site use the MH deopt281// handler.282CompiledMethod* cm = (CompiledMethod*) _cb;283address deopt = cm->is_method_handle_return(pc()) ?284cm->deopt_mh_handler_begin() :285cm->deopt_handler_begin();286287// Save the original pc before we patch in the new one288cm->set_original_pc(this, pc());289patch_pc(thread, deopt);290291#ifdef ASSERT292{293RegisterMap map(thread, false);294frame check = thread->last_frame();295while (id() != check.id()) {296check = check.sender(&map);297}298assert(check.is_deoptimized_frame(), "missed deopt");299}300#endif // ASSERT301}302303frame frame::java_sender() const {304RegisterMap map(JavaThread::current(), false);305frame s;306for (s = sender(&map); !(s.is_java_frame() || s.is_first_frame()); s = s.sender(&map)) ;307guarantee(s.is_java_frame(), "tried to get caller of first java frame");308return s;309}310311frame frame::real_sender(RegisterMap* map) const {312frame result = sender(map);313while (result.is_runtime_frame() ||314result.is_ignored_frame()) {315result = result.sender(map);316}317return result;318}319320// Interpreter frames321322323void frame::interpreter_frame_set_locals(intptr_t* locs) {324assert(is_interpreted_frame(), "Not an interpreted frame");325*interpreter_frame_locals_addr() = locs;326}327328Method* frame::interpreter_frame_method() const {329assert(is_interpreted_frame(), "interpreted frame expected");330Method* m = *interpreter_frame_method_addr();331assert(m->is_method(), "not a Method*");332return m;333}334335void frame::interpreter_frame_set_method(Method* method) {336assert(is_interpreted_frame(), "interpreted frame expected");337*interpreter_frame_method_addr() = method;338}339340void frame::interpreter_frame_set_mirror(oop mirror) {341assert(is_interpreted_frame(), "interpreted frame expected");342*interpreter_frame_mirror_addr() = mirror;343}344345jint frame::interpreter_frame_bci() const {346assert(is_interpreted_frame(), "interpreted frame expected");347address bcp = interpreter_frame_bcp();348return interpreter_frame_method()->bci_from(bcp);349}350351address frame::interpreter_frame_bcp() const {352assert(is_interpreted_frame(), "interpreted frame expected");353address bcp = (address)*interpreter_frame_bcp_addr();354return interpreter_frame_method()->bcp_from(bcp);355}356357void frame::interpreter_frame_set_bcp(address bcp) {358assert(is_interpreted_frame(), "interpreted frame expected");359*interpreter_frame_bcp_addr() = (intptr_t)bcp;360}361362address frame::interpreter_frame_mdp() const {363assert(ProfileInterpreter, "must be profiling interpreter");364assert(is_interpreted_frame(), "interpreted frame expected");365return (address)*interpreter_frame_mdp_addr();366}367368void frame::interpreter_frame_set_mdp(address mdp) {369assert(is_interpreted_frame(), "interpreted frame expected");370assert(ProfileInterpreter, "must be profiling interpreter");371*interpreter_frame_mdp_addr() = (intptr_t)mdp;372}373374BasicObjectLock* frame::next_monitor_in_interpreter_frame(BasicObjectLock* current) const {375assert(is_interpreted_frame(), "Not an interpreted frame");376#ifdef ASSERT377interpreter_frame_verify_monitor(current);378#endif379BasicObjectLock* next = (BasicObjectLock*) (((intptr_t*) current) + interpreter_frame_monitor_size());380return next;381}382383BasicObjectLock* frame::previous_monitor_in_interpreter_frame(BasicObjectLock* current) const {384assert(is_interpreted_frame(), "Not an interpreted frame");385#ifdef ASSERT386// // This verification needs to be checked before being enabled387// interpreter_frame_verify_monitor(current);388#endif389BasicObjectLock* previous = (BasicObjectLock*) (((intptr_t*) current) - interpreter_frame_monitor_size());390return previous;391}392393// Interpreter locals and expression stack locations.394395intptr_t* frame::interpreter_frame_local_at(int index) const {396const int n = Interpreter::local_offset_in_bytes(index)/wordSize;397return &((*interpreter_frame_locals_addr())[n]);398}399400intptr_t* frame::interpreter_frame_expression_stack_at(jint offset) const {401const int i = offset * interpreter_frame_expression_stack_direction();402const int n = i * Interpreter::stackElementWords;403return &(interpreter_frame_expression_stack()[n]);404}405406jint frame::interpreter_frame_expression_stack_size() const {407// Number of elements on the interpreter expression stack408// Callers should span by stackElementWords409int element_size = Interpreter::stackElementWords;410size_t stack_size = 0;411if (frame::interpreter_frame_expression_stack_direction() < 0) {412stack_size = (interpreter_frame_expression_stack() -413interpreter_frame_tos_address() + 1)/element_size;414} else {415stack_size = (interpreter_frame_tos_address() -416interpreter_frame_expression_stack() + 1)/element_size;417}418assert( stack_size <= (size_t)max_jint, "stack size too big");419return ((jint)stack_size);420}421422423// (frame::interpreter_frame_sender_sp accessor is in frame_<arch>.cpp)424425const char* frame::print_name() const {426if (is_native_frame()) return "Native";427if (is_interpreted_frame()) return "Interpreted";428if (is_compiled_frame()) {429if (is_deoptimized_frame()) return "Deoptimized";430return "Compiled";431}432if (sp() == NULL) return "Empty";433return "C";434}435436void frame::print_value_on(outputStream* st, JavaThread *thread) const {437NOT_PRODUCT(address begin = pc()-40;)438NOT_PRODUCT(address end = NULL;)439440st->print("%s frame (sp=" INTPTR_FORMAT " unextended sp=" INTPTR_FORMAT, print_name(), p2i(sp()), p2i(unextended_sp()));441if (sp() != NULL)442st->print(", fp=" INTPTR_FORMAT ", real_fp=" INTPTR_FORMAT ", pc=" INTPTR_FORMAT,443p2i(fp()), p2i(real_fp()), p2i(pc()));444445if (StubRoutines::contains(pc())) {446st->print_cr(")");447st->print("(");448StubCodeDesc* desc = StubCodeDesc::desc_for(pc());449st->print("~Stub::%s", desc->name());450NOT_PRODUCT(begin = desc->begin(); end = desc->end();)451} else if (Interpreter::contains(pc())) {452st->print_cr(")");453st->print("(");454InterpreterCodelet* desc = Interpreter::codelet_containing(pc());455if (desc != NULL) {456st->print("~");457desc->print_on(st);458NOT_PRODUCT(begin = desc->code_begin(); end = desc->code_end();)459} else {460st->print("~interpreter");461}462}463st->print_cr(")");464465if (_cb != NULL) {466st->print(" ");467_cb->print_value_on(st);468st->cr();469#ifndef PRODUCT470if (end == NULL) {471begin = _cb->code_begin();472end = _cb->code_end();473}474#endif475}476NOT_PRODUCT(if (WizardMode && Verbose) Disassembler::decode(begin, end);)477}478479480void frame::print_on(outputStream* st) const {481print_value_on(st,NULL);482if (is_interpreted_frame()) {483interpreter_frame_print_on(st);484}485}486487488void frame::interpreter_frame_print_on(outputStream* st) const {489#ifndef PRODUCT490assert(is_interpreted_frame(), "Not an interpreted frame");491jint i;492for (i = 0; i < interpreter_frame_method()->max_locals(); i++ ) {493intptr_t x = *interpreter_frame_local_at(i);494st->print(" - local [" INTPTR_FORMAT "]", x);495st->fill_to(23);496st->print_cr("; #%d", i);497}498for (i = interpreter_frame_expression_stack_size() - 1; i >= 0; --i ) {499intptr_t x = *interpreter_frame_expression_stack_at(i);500st->print(" - stack [" INTPTR_FORMAT "]", x);501st->fill_to(23);502st->print_cr("; #%d", i);503}504// locks for synchronization505for (BasicObjectLock* current = interpreter_frame_monitor_end();506current < interpreter_frame_monitor_begin();507current = next_monitor_in_interpreter_frame(current)) {508st->print(" - obj [");509current->obj()->print_value_on(st);510st->print_cr("]");511st->print(" - lock [");512current->lock()->print_on(st, current->obj());513st->print_cr("]");514}515// monitor516st->print_cr(" - monitor[" INTPTR_FORMAT "]", p2i(interpreter_frame_monitor_begin()));517// bcp518st->print(" - bcp [" INTPTR_FORMAT "]", p2i(interpreter_frame_bcp()));519st->fill_to(23);520st->print_cr("; @%d", interpreter_frame_bci());521// locals522st->print_cr(" - locals [" INTPTR_FORMAT "]", p2i(interpreter_frame_local_at(0)));523// method524st->print(" - method [" INTPTR_FORMAT "]", p2i(interpreter_frame_method()));525st->fill_to(23);526st->print("; ");527interpreter_frame_method()->print_name(st);528st->cr();529#endif530}531532// Print whether the frame is in the VM or OS indicating a HotSpot problem.533// Otherwise, it's likely a bug in the native library that the Java code calls,534// hopefully indicating where to submit bugs.535void frame::print_C_frame(outputStream* st, char* buf, int buflen, address pc) {536// C/C++ frame537bool in_vm = os::address_is_in_vm(pc);538st->print(in_vm ? "V" : "C");539540int offset;541bool found;542543if (buf == NULL || buflen < 1) return;544// libname545buf[0] = '\0';546found = os::dll_address_to_library_name(pc, buf, buflen, &offset);547if (found && buf[0] != '\0') {548// skip directory names549const char *p1, *p2;550p1 = buf;551int len = (int)strlen(os::file_separator());552while ((p2 = strstr(p1, os::file_separator())) != NULL) p1 = p2 + len;553st->print(" [%s+0x%x]", p1, offset);554} else {555st->print(" " PTR_FORMAT, p2i(pc));556}557558found = os::dll_address_to_function_name(pc, buf, buflen, &offset);559if (found) {560st->print(" %s+0x%x", buf, offset);561}562}563564// frame::print_on_error() is called by fatal error handler. Notice that we may565// crash inside this function if stack frame is corrupted. The fatal error566// handler can catch and handle the crash. Here we assume the frame is valid.567//568// First letter indicates type of the frame:569// J: Java frame (compiled)570// j: Java frame (interpreted)571// V: VM frame (C/C++)572// v: Other frames running VM generated code (e.g. stubs, adapters, etc.)573// C: C/C++ frame574//575// We don't need detailed frame type as that in frame::print_name(). "C"576// suggests the problem is in user lib; everything else is likely a VM bug.577578void frame::print_on_error(outputStream* st, char* buf, int buflen, bool verbose) const {579if (_cb != NULL) {580if (Interpreter::contains(pc())) {581Method* m = this->interpreter_frame_method();582if (m != NULL) {583m->name_and_sig_as_C_string(buf, buflen);584st->print("j %s", buf);585st->print("+%d", this->interpreter_frame_bci());586ModuleEntry* module = m->method_holder()->module();587if (module->is_named()) {588module->name()->as_C_string(buf, buflen);589st->print(" %s", buf);590if (module->version() != NULL) {591module->version()->as_C_string(buf, buflen);592st->print("@%s", buf);593}594}595} else {596st->print("j " PTR_FORMAT, p2i(pc()));597}598} else if (StubRoutines::contains(pc())) {599StubCodeDesc* desc = StubCodeDesc::desc_for(pc());600if (desc != NULL) {601st->print("v ~StubRoutines::%s", desc->name());602} else {603st->print("v ~StubRoutines::" PTR_FORMAT, p2i(pc()));604}605} else if (_cb->is_buffer_blob()) {606st->print("v ~BufferBlob::%s", ((BufferBlob *)_cb)->name());607} else if (_cb->is_compiled()) {608CompiledMethod* cm = (CompiledMethod*)_cb;609Method* m = cm->method();610if (m != NULL) {611if (cm->is_nmethod()) {612nmethod* nm = cm->as_nmethod();613st->print("J %d%s", nm->compile_id(), (nm->is_osr_method() ? "%" : ""));614st->print(" %s", nm->compiler_name());615}616m->name_and_sig_as_C_string(buf, buflen);617st->print(" %s", buf);618ModuleEntry* module = m->method_holder()->module();619if (module->is_named()) {620module->name()->as_C_string(buf, buflen);621st->print(" %s", buf);622if (module->version() != NULL) {623module->version()->as_C_string(buf, buflen);624st->print("@%s", buf);625}626}627st->print(" (%d bytes) @ " PTR_FORMAT " [" PTR_FORMAT "+" INTPTR_FORMAT "]",628m->code_size(), p2i(_pc), p2i(_cb->code_begin()), _pc - _cb->code_begin());629#if INCLUDE_JVMCI630if (cm->is_nmethod()) {631nmethod* nm = cm->as_nmethod();632const char* jvmciName = nm->jvmci_name();633if (jvmciName != NULL) {634st->print(" (%s)", jvmciName);635}636}637#endif638} else {639st->print("J " PTR_FORMAT, p2i(pc()));640}641} else if (_cb->is_runtime_stub()) {642st->print("v ~RuntimeStub::%s", ((RuntimeStub *)_cb)->name());643} else if (_cb->is_deoptimization_stub()) {644st->print("v ~DeoptimizationBlob");645} else if (_cb->is_exception_stub()) {646st->print("v ~ExceptionBlob");647} else if (_cb->is_safepoint_stub()) {648st->print("v ~SafepointBlob");649} else if (_cb->is_adapter_blob()) {650st->print("v ~AdapterBlob");651} else if (_cb->is_vtable_blob()) {652st->print("v ~VtableBlob");653} else if (_cb->is_method_handles_adapter_blob()) {654st->print("v ~MethodHandlesAdapterBlob");655} else if (_cb->is_uncommon_trap_stub()) {656st->print("v ~UncommonTrapBlob");657} else {658st->print("v blob " PTR_FORMAT, p2i(pc()));659}660} else {661print_C_frame(st, buf, buflen, pc());662}663}664665666/*667The interpreter_frame_expression_stack_at method in the case of SPARC needs the668max_stack value of the method in order to compute the expression stack address.669It uses the Method* in order to get the max_stack value but during GC this670Method* value saved on the frame is changed by reverse_and_push and hence cannot671be used. So we save the max_stack value in the FrameClosure object and pass it672down to the interpreter_frame_expression_stack_at method673*/674class InterpreterFrameClosure : public OffsetClosure {675private:676const frame* _fr;677OopClosure* _f;678int _max_locals;679int _max_stack;680681public:682InterpreterFrameClosure(const frame* fr, int max_locals, int max_stack,683OopClosure* f) {684_fr = fr;685_max_locals = max_locals;686_max_stack = max_stack;687_f = f;688}689690void offset_do(int offset) {691oop* addr;692if (offset < _max_locals) {693addr = (oop*) _fr->interpreter_frame_local_at(offset);694assert((intptr_t*)addr >= _fr->sp(), "must be inside the frame");695_f->do_oop(addr);696} else {697addr = (oop*) _fr->interpreter_frame_expression_stack_at((offset - _max_locals));698// In case of exceptions, the expression stack is invalid and the esp will be reset to express699// this condition. Therefore, we call f only if addr is 'inside' the stack (i.e., addr >= esp for Intel).700bool in_stack;701if (frame::interpreter_frame_expression_stack_direction() > 0) {702in_stack = (intptr_t*)addr <= _fr->interpreter_frame_tos_address();703} else {704in_stack = (intptr_t*)addr >= _fr->interpreter_frame_tos_address();705}706if (in_stack) {707_f->do_oop(addr);708}709}710}711712int max_locals() { return _max_locals; }713};714715716class InterpretedArgumentOopFinder: public SignatureIterator {717private:718OopClosure* _f; // Closure to invoke719int _offset; // TOS-relative offset, decremented with each argument720bool _has_receiver; // true if the callee has a receiver721const frame* _fr;722723friend class SignatureIterator; // so do_parameters_on can call do_type724void do_type(BasicType type) {725_offset -= parameter_type_word_count(type);726if (is_reference_type(type)) oop_offset_do();727}728729void oop_offset_do() {730oop* addr;731addr = (oop*)_fr->interpreter_frame_tos_at(_offset);732_f->do_oop(addr);733}734735public:736InterpretedArgumentOopFinder(Symbol* signature, bool has_receiver, const frame* fr, OopClosure* f) : SignatureIterator(signature), _has_receiver(has_receiver) {737// compute size of arguments738int args_size = ArgumentSizeComputer(signature).size() + (has_receiver ? 1 : 0);739assert(!fr->is_interpreted_frame() ||740args_size <= fr->interpreter_frame_expression_stack_size(),741"args cannot be on stack anymore");742// initialize InterpretedArgumentOopFinder743_f = f;744_fr = fr;745_offset = args_size;746}747748void oops_do() {749if (_has_receiver) {750--_offset;751oop_offset_do();752}753do_parameters_on(this);754}755};756757758// Entry frame has following form (n arguments)759// +-----------+760// sp -> | last arg |761// +-----------+762// : ::: :763// +-----------+764// (sp+n)->| first arg|765// +-----------+766767768769// visits and GC's all the arguments in entry frame770class EntryFrameOopFinder: public SignatureIterator {771private:772bool _is_static;773int _offset;774const frame* _fr;775OopClosure* _f;776777friend class SignatureIterator; // so do_parameters_on can call do_type778void do_type(BasicType type) {779// decrement offset before processing the type780_offset -= parameter_type_word_count(type);781assert (_offset >= 0, "illegal offset");782if (is_reference_type(type)) oop_at_offset_do(_offset);783}784785void oop_at_offset_do(int offset) {786assert (offset >= 0, "illegal offset");787oop* addr = (oop*) _fr->entry_frame_argument_at(offset);788_f->do_oop(addr);789}790791public:792EntryFrameOopFinder(const frame* frame, Symbol* signature, bool is_static) : SignatureIterator(signature) {793_f = NULL; // will be set later794_fr = frame;795_is_static = is_static;796_offset = ArgumentSizeComputer(signature).size(); // pre-decremented down to zero797}798799void arguments_do(OopClosure* f) {800_f = f;801if (!_is_static) oop_at_offset_do(_offset); // do the receiver802do_parameters_on(this);803}804805};806807oop* frame::interpreter_callee_receiver_addr(Symbol* signature) {808ArgumentSizeComputer asc(signature);809int size = asc.size();810return (oop *)interpreter_frame_tos_at(size);811}812813814void frame::oops_interpreted_do(OopClosure* f, const RegisterMap* map, bool query_oop_map_cache) const {815assert(is_interpreted_frame(), "Not an interpreted frame");816assert(map != NULL, "map must be set");817Thread *thread = Thread::current();818methodHandle m (thread, interpreter_frame_method());819jint bci = interpreter_frame_bci();820821assert(!Universe::heap()->is_in(m()),822"must be valid oop");823assert(m->is_method(), "checking frame value");824assert((m->is_native() && bci == 0) ||825(!m->is_native() && bci >= 0 && bci < m->code_size()),826"invalid bci value");827828// Handle the monitor elements in the activation829for (830BasicObjectLock* current = interpreter_frame_monitor_end();831current < interpreter_frame_monitor_begin();832current = next_monitor_in_interpreter_frame(current)833) {834#ifdef ASSERT835interpreter_frame_verify_monitor(current);836#endif837current->oops_do(f);838}839840if (m->is_native()) {841f->do_oop(interpreter_frame_temp_oop_addr());842}843844// The method pointer in the frame might be the only path to the method's845// klass, and the klass needs to be kept alive while executing. The GCs846// don't trace through method pointers, so the mirror of the method's klass847// is installed as a GC root.848f->do_oop(interpreter_frame_mirror_addr());849850int max_locals = m->is_native() ? m->size_of_parameters() : m->max_locals();851852Symbol* signature = NULL;853bool has_receiver = false;854855// Process a callee's arguments if we are at a call site856// (i.e., if we are at an invoke bytecode)857// This is used sometimes for calling into the VM, not for another858// interpreted or compiled frame.859if (!m->is_native()) {860Bytecode_invoke call = Bytecode_invoke_check(m, bci);861if (call.is_valid()) {862signature = call.signature();863has_receiver = call.has_receiver();864if (map->include_argument_oops() &&865interpreter_frame_expression_stack_size() > 0) {866ResourceMark rm(thread); // is this right ???867// we are at a call site & the expression stack is not empty868// => process callee's arguments869//870// Note: The expression stack can be empty if an exception871// occurred during method resolution/execution. In all872// cases we empty the expression stack completely be-873// fore handling the exception (the exception handling874// code in the interpreter calls a blocking runtime875// routine which can cause this code to be executed).876// (was bug gri 7/27/98)877oops_interpreted_arguments_do(signature, has_receiver, f);878}879}880}881882InterpreterFrameClosure blk(this, max_locals, m->max_stack(), f);883884// process locals & expression stack885InterpreterOopMap mask;886if (query_oop_map_cache) {887m->mask_for(bci, &mask);888} else {889OopMapCache::compute_one_oop_map(m, bci, &mask);890}891mask.iterate_oop(&blk);892}893894895void frame::oops_interpreted_arguments_do(Symbol* signature, bool has_receiver, OopClosure* f) const {896InterpretedArgumentOopFinder finder(signature, has_receiver, this, f);897finder.oops_do();898}899900void frame::oops_code_blob_do(OopClosure* f, CodeBlobClosure* cf, const RegisterMap* reg_map,901DerivedPointerIterationMode derived_mode) const {902assert(_cb != NULL, "sanity check");903if (_cb->oop_maps() != NULL) {904OopMapSet::oops_do(this, reg_map, f, derived_mode);905906// Preserve potential arguments for a callee. We handle this by dispatching907// on the codeblob. For c2i, we do908if (reg_map->include_argument_oops()) {909_cb->preserve_callee_argument_oops(*this, reg_map, f);910}911}912// In cases where perm gen is collected, GC will want to mark913// oops referenced from nmethods active on thread stacks so as to914// prevent them from being collected. However, this visit should be915// restricted to certain phases of the collection only. The916// closure decides how it wants nmethods to be traced.917if (cf != NULL)918cf->do_code_blob(_cb);919}920921class CompiledArgumentOopFinder: public SignatureIterator {922protected:923OopClosure* _f;924int _offset; // the current offset, incremented with each argument925bool _has_receiver; // true if the callee has a receiver926bool _has_appendix; // true if the call has an appendix927frame _fr;928RegisterMap* _reg_map;929int _arg_size;930VMRegPair* _regs; // VMReg list of arguments931932friend class SignatureIterator; // so do_parameters_on can call do_type933void do_type(BasicType type) {934if (is_reference_type(type)) handle_oop_offset();935_offset += parameter_type_word_count(type);936}937938virtual void handle_oop_offset() {939// Extract low order register number from register array.940// In LP64-land, the high-order bits are valid but unhelpful.941VMReg reg = _regs[_offset].first();942oop *loc = _fr.oopmapreg_to_oop_location(reg, _reg_map);943assert(loc != NULL, "missing register map entry");944_f->do_oop(loc);945}946947public:948CompiledArgumentOopFinder(Symbol* signature, bool has_receiver, bool has_appendix, OopClosure* f, frame fr, const RegisterMap* reg_map)949: SignatureIterator(signature) {950951// initialize CompiledArgumentOopFinder952_f = f;953_offset = 0;954_has_receiver = has_receiver;955_has_appendix = has_appendix;956_fr = fr;957_reg_map = (RegisterMap*)reg_map;958_arg_size = ArgumentSizeComputer(signature).size() + (has_receiver ? 1 : 0) + (has_appendix ? 1 : 0);959960int arg_size;961_regs = SharedRuntime::find_callee_arguments(signature, has_receiver, has_appendix, &arg_size);962assert(arg_size == _arg_size, "wrong arg size");963}964965void oops_do() {966if (_has_receiver) {967handle_oop_offset();968_offset++;969}970do_parameters_on(this);971if (_has_appendix) {972handle_oop_offset();973_offset++;974}975}976};977978void frame::oops_compiled_arguments_do(Symbol* signature, bool has_receiver, bool has_appendix,979const RegisterMap* reg_map, OopClosure* f) const {980ResourceMark rm;981CompiledArgumentOopFinder finder(signature, has_receiver, has_appendix, f, *this, reg_map);982finder.oops_do();983}984985986// Get receiver out of callers frame, i.e. find parameter 0 in callers987// frame. Consult ADLC for where parameter 0 is to be found. Then988// check local reg_map for it being a callee-save register or argument989// register, both of which are saved in the local frame. If not found990// there, it must be an in-stack argument of the caller.991// Note: caller.sp() points to callee-arguments992oop frame::retrieve_receiver(RegisterMap* reg_map) {993frame caller = *this;994995// First consult the ADLC on where it puts parameter 0 for this signature.996VMReg reg = SharedRuntime::name_for_receiver();997oop* oop_adr = caller.oopmapreg_to_oop_location(reg, reg_map);998if (oop_adr == NULL) {999guarantee(oop_adr != NULL, "bad register save location");1000return NULL;1001}1002oop r = *oop_adr;1003assert(Universe::heap()->is_in_or_null(r), "bad receiver: " INTPTR_FORMAT " (" INTX_FORMAT ")", p2i(r), p2i(r));1004return r;1005}100610071008BasicLock* frame::get_native_monitor() {1009nmethod* nm = (nmethod*)_cb;1010assert(_cb != NULL && _cb->is_nmethod() && nm->method()->is_native(),1011"Should not call this unless it's a native nmethod");1012int byte_offset = in_bytes(nm->native_basic_lock_sp_offset());1013assert(byte_offset >= 0, "should not see invalid offset");1014return (BasicLock*) &sp()[byte_offset / wordSize];1015}10161017oop frame::get_native_receiver() {1018nmethod* nm = (nmethod*)_cb;1019assert(_cb != NULL && _cb->is_nmethod() && nm->method()->is_native(),1020"Should not call this unless it's a native nmethod");1021int byte_offset = in_bytes(nm->native_receiver_sp_offset());1022assert(byte_offset >= 0, "should not see invalid offset");1023oop owner = ((oop*) sp())[byte_offset / wordSize];1024assert( Universe::heap()->is_in(owner), "bad receiver" );1025return owner;1026}10271028void frame::oops_entry_do(OopClosure* f, const RegisterMap* map) const {1029assert(map != NULL, "map must be set");1030if (map->include_argument_oops()) {1031// must collect argument oops, as nobody else is doing it1032Thread *thread = Thread::current();1033methodHandle m (thread, entry_frame_call_wrapper()->callee_method());1034EntryFrameOopFinder finder(this, m->signature(), m->is_static());1035finder.arguments_do(f);1036}1037// Traverse the Handle Block saved in the entry frame1038entry_frame_call_wrapper()->oops_do(f);1039}10401041void frame::oops_do(OopClosure* f, CodeBlobClosure* cf, const RegisterMap* map,1042DerivedPointerIterationMode derived_mode) const {1043oops_do_internal(f, cf, map, true, derived_mode);1044}10451046void frame::oops_do(OopClosure* f, CodeBlobClosure* cf, const RegisterMap* map) const {1047#if COMPILER2_OR_JVMCI1048oops_do_internal(f, cf, map, true, DerivedPointerTable::is_active() ?1049DerivedPointerIterationMode::_with_table :1050DerivedPointerIterationMode::_ignore);1051#else1052oops_do_internal(f, cf, map, true, DerivedPointerIterationMode::_ignore);1053#endif1054}10551056void frame::oops_do_internal(OopClosure* f, CodeBlobClosure* cf, const RegisterMap* map,1057bool use_interpreter_oop_map_cache, DerivedPointerIterationMode derived_mode) const {1058#ifndef PRODUCT1059// simulate GC crash here to dump java thread in error report1060if (CrashGCForDumpingJavaThread) {1061char *t = NULL;1062*t = 'c';1063}1064#endif1065if (is_interpreted_frame()) {1066oops_interpreted_do(f, map, use_interpreter_oop_map_cache);1067} else if (is_entry_frame()) {1068oops_entry_do(f, map);1069} else if (is_optimized_entry_frame()) {1070// Nothing to do1071// receiver is a global ref1072// handle block is for JNI1073} else if (CodeCache::contains(pc())) {1074oops_code_blob_do(f, cf, map, derived_mode);1075} else {1076ShouldNotReachHere();1077}1078}10791080void frame::nmethods_do(CodeBlobClosure* cf) const {1081if (_cb != NULL && _cb->is_nmethod()) {1082cf->do_code_blob(_cb);1083}1084}108510861087// Call f closure on the interpreted Method*s in the stack.1088void frame::metadata_do(MetadataClosure* f) const {1089ResourceMark rm;1090if (is_interpreted_frame()) {1091Method* m = this->interpreter_frame_method();1092assert(m != NULL, "expecting a method in this frame");1093f->do_metadata(m);1094}1095}10961097void frame::verify(const RegisterMap* map) const {1098// for now make sure receiver type is correct1099if (is_interpreted_frame()) {1100Method* method = interpreter_frame_method();1101guarantee(method->is_method(), "method is wrong in frame::verify");1102if (!method->is_static()) {1103// fetch the receiver1104oop* p = (oop*) interpreter_frame_local_at(0);1105// make sure we have the right receiver type1106}1107}1108#if COMPILER2_OR_JVMCI1109assert(DerivedPointerTable::is_empty(), "must be empty before verify");1110#endif1111if (map->update_map()) { // The map has to be up-to-date for the current frame1112oops_do_internal(&VerifyOopClosure::verify_oop, NULL, map, false, DerivedPointerIterationMode::_ignore);1113}1114}111511161117#ifdef ASSERT1118bool frame::verify_return_pc(address x) {1119if (StubRoutines::returns_to_call_stub(x)) {1120return true;1121}1122if (CodeCache::contains(x)) {1123return true;1124}1125if (Interpreter::contains(x)) {1126return true;1127}1128return false;1129}1130#endif11311132#ifdef ASSERT1133void frame::interpreter_frame_verify_monitor(BasicObjectLock* value) const {1134assert(is_interpreted_frame(), "Not an interpreted frame");1135// verify that the value is in the right part of the frame1136address low_mark = (address) interpreter_frame_monitor_end();1137address high_mark = (address) interpreter_frame_monitor_begin();1138address current = (address) value;11391140const int monitor_size = frame::interpreter_frame_monitor_size();1141guarantee((high_mark - current) % monitor_size == 0 , "Misaligned top of BasicObjectLock*");1142guarantee( high_mark > current , "Current BasicObjectLock* higher than high_mark");11431144guarantee((current - low_mark) % monitor_size == 0 , "Misaligned bottom of BasicObjectLock*");1145guarantee( current >= low_mark , "Current BasicObjectLock* below than low_mark");1146}1147#endif11481149#ifndef PRODUCT1150// callers need a ResourceMark because of name_and_sig_as_C_string() usage,1151// RA allocated string is returned to the caller1152void frame::describe(FrameValues& values, int frame_no) {1153// boundaries: sp and the 'real' frame pointer1154values.describe(-1, sp(), err_msg("sp for #%d", frame_no), 1);1155intptr_t* frame_pointer = real_fp(); // Note: may differ from fp()11561157// print frame info at the highest boundary1158intptr_t* info_address = MAX2(sp(), frame_pointer);11591160if (info_address != frame_pointer) {1161// print frame_pointer explicitly if not marked by the frame info1162values.describe(-1, frame_pointer, err_msg("frame pointer for #%d", frame_no), 1);1163}11641165if (is_entry_frame() || is_compiled_frame() || is_interpreted_frame() || is_native_frame()) {1166// Label values common to most frames1167values.describe(-1, unextended_sp(), err_msg("unextended_sp for #%d", frame_no));1168}11691170if (is_interpreted_frame()) {1171Method* m = interpreter_frame_method();1172int bci = interpreter_frame_bci();11731174// Label the method and current bci1175values.describe(-1, info_address,1176FormatBuffer<1024>("#%d method %s @ %d", frame_no, m->name_and_sig_as_C_string(), bci), 2);1177values.describe(-1, info_address,1178err_msg("- %d locals %d max stack", m->max_locals(), m->max_stack()), 1);1179if (m->max_locals() > 0) {1180intptr_t* l0 = interpreter_frame_local_at(0);1181intptr_t* ln = interpreter_frame_local_at(m->max_locals() - 1);1182values.describe(-1, MAX2(l0, ln), err_msg("locals for #%d", frame_no), 1);1183// Report each local and mark as owned by this frame1184for (int l = 0; l < m->max_locals(); l++) {1185intptr_t* l0 = interpreter_frame_local_at(l);1186values.describe(frame_no, l0, err_msg("local %d", l));1187}1188}11891190// Compute the actual expression stack size1191InterpreterOopMap mask;1192OopMapCache::compute_one_oop_map(methodHandle(Thread::current(), m), bci, &mask);1193intptr_t* tos = NULL;1194// Report each stack element and mark as owned by this frame1195for (int e = 0; e < mask.expression_stack_size(); e++) {1196tos = MAX2(tos, interpreter_frame_expression_stack_at(e));1197values.describe(frame_no, interpreter_frame_expression_stack_at(e),1198err_msg("stack %d", e));1199}1200if (tos != NULL) {1201values.describe(-1, tos, err_msg("expression stack for #%d", frame_no), 1);1202}1203if (interpreter_frame_monitor_begin() != interpreter_frame_monitor_end()) {1204values.describe(frame_no, (intptr_t*)interpreter_frame_monitor_begin(), "monitors begin");1205values.describe(frame_no, (intptr_t*)interpreter_frame_monitor_end(), "monitors end");1206}1207} else if (is_entry_frame()) {1208// For now just label the frame1209values.describe(-1, info_address, err_msg("#%d entry frame", frame_no), 2);1210} else if (is_compiled_frame()) {1211// For now just label the frame1212CompiledMethod* cm = (CompiledMethod*)cb();1213values.describe(-1, info_address,1214FormatBuffer<1024>("#%d nmethod " INTPTR_FORMAT " for method J %s%s", frame_no,1215p2i(cm),1216cm->method()->name_and_sig_as_C_string(),1217(_deopt_state == is_deoptimized) ?1218" (deoptimized)" :1219((_deopt_state == unknown) ? " (state unknown)" : "")),12202);1221} else if (is_native_frame()) {1222// For now just label the frame1223nmethod* nm = cb()->as_nmethod_or_null();1224values.describe(-1, info_address,1225FormatBuffer<1024>("#%d nmethod " INTPTR_FORMAT " for native method %s", frame_no,1226p2i(nm), nm->method()->name_and_sig_as_C_string()), 2);1227} else {1228// provide default info if not handled before1229char *info = (char *) "special frame";1230if ((_cb != NULL) &&1231(_cb->name() != NULL)) {1232info = (char *)_cb->name();1233}1234values.describe(-1, info_address, err_msg("#%d <%s>", frame_no, info), 2);1235}12361237// platform dependent additional data1238describe_pd(values, frame_no);1239}12401241#endif12421243#ifndef PRODUCT12441245void FrameValues::describe(int owner, intptr_t* location, const char* description, int priority) {1246FrameValue fv;1247fv.location = location;1248fv.owner = owner;1249fv.priority = priority;1250fv.description = NEW_RESOURCE_ARRAY(char, strlen(description) + 1);1251strcpy(fv.description, description);1252_values.append(fv);1253}125412551256#ifdef ASSERT1257void FrameValues::validate() {1258_values.sort(compare);1259bool error = false;1260FrameValue prev;1261prev.owner = -1;1262for (int i = _values.length() - 1; i >= 0; i--) {1263FrameValue fv = _values.at(i);1264if (fv.owner == -1) continue;1265if (prev.owner == -1) {1266prev = fv;1267continue;1268}1269if (prev.location == fv.location) {1270if (fv.owner != prev.owner) {1271tty->print_cr("overlapping storage");1272tty->print_cr(" " INTPTR_FORMAT ": " INTPTR_FORMAT " %s", p2i(prev.location), *prev.location, prev.description);1273tty->print_cr(" " INTPTR_FORMAT ": " INTPTR_FORMAT " %s", p2i(fv.location), *fv.location, fv.description);1274error = true;1275}1276} else {1277prev = fv;1278}1279}1280assert(!error, "invalid layout");1281}1282#endif // ASSERT12831284void FrameValues::print_on(JavaThread* thread, outputStream* st) {1285_values.sort(compare);12861287// Sometimes values like the fp can be invalid values if the1288// register map wasn't updated during the walk. Trim out values1289// that aren't actually in the stack of the thread.1290int min_index = 0;1291int max_index = _values.length() - 1;1292intptr_t* v0 = _values.at(min_index).location;1293intptr_t* v1 = _values.at(max_index).location;12941295if (thread == Thread::current()) {1296while (!thread->is_in_live_stack((address)v0)) {1297v0 = _values.at(++min_index).location;1298}1299while (!thread->is_in_live_stack((address)v1)) {1300v1 = _values.at(--max_index).location;1301}1302} else {1303while (!thread->is_in_full_stack((address)v0)) {1304v0 = _values.at(++min_index).location;1305}1306while (!thread->is_in_full_stack((address)v1)) {1307v1 = _values.at(--max_index).location;1308}1309}1310intptr_t* min = MIN2(v0, v1);1311intptr_t* max = MAX2(v0, v1);1312intptr_t* cur = max;1313intptr_t* last = NULL;1314for (int i = max_index; i >= min_index; i--) {1315FrameValue fv = _values.at(i);1316while (cur > fv.location) {1317st->print_cr(" " INTPTR_FORMAT ": " INTPTR_FORMAT, p2i(cur), *cur);1318cur--;1319}1320if (last == fv.location) {1321const char* spacer = " " LP64_ONLY(" ");1322st->print_cr(" %s %s %s", spacer, spacer, fv.description);1323} else {1324st->print_cr(" " INTPTR_FORMAT ": " INTPTR_FORMAT " %s", p2i(fv.location), *fv.location, fv.description);1325last = fv.location;1326cur--;1327}1328}1329}13301331#endif // ndef PRODUCT133213331334