Path: blob/master/src/hotspot/share/prims/jvmtiImpl.cpp
41145 views
/*1* Copyright (c) 2003, 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.hpp"26#include "classfile/symbolTable.hpp"27#include "code/nmethod.hpp"28#include "interpreter/interpreter.hpp"29#include "interpreter/oopMapCache.hpp"30#include "jvmtifiles/jvmtiEnv.hpp"31#include "logging/log.hpp"32#include "logging/logStream.hpp"33#include "memory/allocation.inline.hpp"34#include "memory/resourceArea.hpp"35#include "oops/instanceKlass.hpp"36#include "oops/klass.inline.hpp"37#include "oops/oop.inline.hpp"38#include "oops/oopHandle.inline.hpp"39#include "prims/jvmtiAgentThread.hpp"40#include "prims/jvmtiEventController.inline.hpp"41#include "prims/jvmtiImpl.hpp"42#include "prims/jvmtiRedefineClasses.hpp"43#include "runtime/deoptimization.hpp"44#include "runtime/frame.inline.hpp"45#include "runtime/handles.inline.hpp"46#include "runtime/interfaceSupport.inline.hpp"47#include "runtime/javaCalls.hpp"48#include "runtime/jniHandles.hpp"49#include "runtime/os.hpp"50#include "runtime/serviceThread.hpp"51#include "runtime/signature.hpp"52#include "runtime/thread.inline.hpp"53#include "runtime/threadSMR.hpp"54#include "runtime/vframe.hpp"55#include "runtime/vframe_hp.hpp"56#include "runtime/vmOperations.hpp"57#include "utilities/exceptions.hpp"5859//60// class JvmtiAgentThread61//62// JavaThread used to wrap a thread started by an agent63// using the JVMTI method RunAgentThread.64//6566JvmtiAgentThread::JvmtiAgentThread(JvmtiEnv* env, jvmtiStartFunction start_fn, const void *start_arg)67: JavaThread(start_function_wrapper) {68_env = env;69_start_fn = start_fn;70_start_arg = start_arg;71}7273void74JvmtiAgentThread::start_function_wrapper(JavaThread *thread, TRAPS) {75// It is expected that any Agent threads will be created as76// Java Threads. If this is the case, notification of the creation77// of the thread is given in JavaThread::thread_main().78assert(thread == JavaThread::current(), "sanity check");7980JvmtiAgentThread *dthread = (JvmtiAgentThread *)thread;81dthread->call_start_function();82}8384void85JvmtiAgentThread::call_start_function() {86ThreadToNativeFromVM transition(this);87_start_fn(_env->jvmti_external(), jni_environment(), (void*)_start_arg);88}899091//92// class GrowableCache - private methods93//9495void GrowableCache::recache() {96int len = _elements->length();9798FREE_C_HEAP_ARRAY(address, _cache);99_cache = NEW_C_HEAP_ARRAY(address,len+1, mtInternal);100101for (int i=0; i<len; i++) {102_cache[i] = _elements->at(i)->getCacheValue();103//104// The cache entry has gone bad. Without a valid frame pointer105// value, the entry is useless so we simply delete it in product106// mode. The call to remove() will rebuild the cache again107// without the bad entry.108//109if (_cache[i] == NULL) {110assert(false, "cannot recache NULL elements");111remove(i);112return;113}114}115_cache[len] = NULL;116117_listener_fun(_this_obj,_cache);118}119120bool GrowableCache::equals(void* v, GrowableElement *e2) {121GrowableElement *e1 = (GrowableElement *) v;122assert(e1 != NULL, "e1 != NULL");123assert(e2 != NULL, "e2 != NULL");124125return e1->equals(e2);126}127128//129// class GrowableCache - public methods130//131132GrowableCache::GrowableCache() {133_this_obj = NULL;134_listener_fun = NULL;135_elements = NULL;136_cache = NULL;137}138139GrowableCache::~GrowableCache() {140clear();141delete _elements;142FREE_C_HEAP_ARRAY(address, _cache);143}144145void GrowableCache::initialize(void *this_obj, void listener_fun(void *, address*) ) {146_this_obj = this_obj;147_listener_fun = listener_fun;148_elements = new (ResourceObj::C_HEAP, mtServiceability) GrowableArray<GrowableElement*>(5, mtServiceability);149recache();150}151152// number of elements in the collection153int GrowableCache::length() {154return _elements->length();155}156157// get the value of the index element in the collection158GrowableElement* GrowableCache::at(int index) {159GrowableElement *e = (GrowableElement *) _elements->at(index);160assert(e != NULL, "e != NULL");161return e;162}163164int GrowableCache::find(GrowableElement* e) {165return _elements->find(e, GrowableCache::equals);166}167168// append a copy of the element to the end of the collection169void GrowableCache::append(GrowableElement* e) {170GrowableElement *new_e = e->clone();171_elements->append(new_e);172recache();173}174175// remove the element at index176void GrowableCache::remove (int index) {177GrowableElement *e = _elements->at(index);178assert(e != NULL, "e != NULL");179_elements->remove(e);180delete e;181recache();182}183184// clear out all elements, release all heap space and185// let our listener know that things have changed.186void GrowableCache::clear() {187int len = _elements->length();188for (int i=0; i<len; i++) {189delete _elements->at(i);190}191_elements->clear();192recache();193}194195//196// class JvmtiBreakpoint197//198199JvmtiBreakpoint::JvmtiBreakpoint(Method* m_method, jlocation location)200: _method(m_method), _bci((int)location) {201assert(_method != NULL, "No method for breakpoint.");202assert(_bci >= 0, "Negative bci for breakpoint.");203oop class_holder_oop = _method->method_holder()->klass_holder();204_class_holder = OopHandle(JvmtiExport::jvmti_oop_storage(), class_holder_oop);205}206207JvmtiBreakpoint::~JvmtiBreakpoint() {208if (_class_holder.peek() != NULL) {209_class_holder.release(JvmtiExport::jvmti_oop_storage());210}211}212213void JvmtiBreakpoint::copy(JvmtiBreakpoint& bp) {214_method = bp._method;215_bci = bp._bci;216_class_holder = OopHandle(JvmtiExport::jvmti_oop_storage(), bp._class_holder.resolve());217}218219bool JvmtiBreakpoint::equals(JvmtiBreakpoint& bp) {220return _method == bp._method221&& _bci == bp._bci;222}223224address JvmtiBreakpoint::getBcp() const {225return _method->bcp_from(_bci);226}227228void JvmtiBreakpoint::each_method_version_do(method_action meth_act) {229((Method*)_method->*meth_act)(_bci);230231// add/remove breakpoint to/from versions of the method that are EMCP.232Thread *thread = Thread::current();233InstanceKlass* ik = _method->method_holder();234Symbol* m_name = _method->name();235Symbol* m_signature = _method->signature();236237// search previous versions if they exist238for (InstanceKlass* pv_node = ik->previous_versions();239pv_node != NULL;240pv_node = pv_node->previous_versions()) {241Array<Method*>* methods = pv_node->methods();242243for (int i = methods->length() - 1; i >= 0; i--) {244Method* method = methods->at(i);245// Only set breakpoints in EMCP methods.246// EMCP methods are old but not obsolete. Equivalent247// Modulo Constant Pool means the method is equivalent except248// the constant pool and instructions that access the constant249// pool might be different.250// If a breakpoint is set in a redefined method, its EMCP methods251// must have a breakpoint also.252// None of the methods are deleted until none are running.253// This code could set a breakpoint in a method that254// is never reached, but this won't be noticeable to the programmer.255if (!method->is_obsolete() &&256method->name() == m_name &&257method->signature() == m_signature) {258ResourceMark rm;259log_debug(redefine, class, breakpoint)260("%sing breakpoint in %s(%s)", meth_act == &Method::set_breakpoint ? "sett" : "clear",261method->name()->as_C_string(), method->signature()->as_C_string());262(method->*meth_act)(_bci);263break;264}265}266}267}268269void JvmtiBreakpoint::set() {270each_method_version_do(&Method::set_breakpoint);271}272273void JvmtiBreakpoint::clear() {274each_method_version_do(&Method::clear_breakpoint);275}276277void JvmtiBreakpoint::print_on(outputStream* out) const {278#ifndef PRODUCT279ResourceMark rm;280const char *class_name = (_method == NULL) ? "NULL" : _method->klass_name()->as_C_string();281const char *method_name = (_method == NULL) ? "NULL" : _method->name()->as_C_string();282out->print("Breakpoint(%s,%s,%d,%p)", class_name, method_name, _bci, getBcp());283#endif284}285286287//288// class VM_ChangeBreakpoints289//290// Modify the Breakpoints data structure at a safepoint291//292293void VM_ChangeBreakpoints::doit() {294switch (_operation) {295case SET_BREAKPOINT:296_breakpoints->set_at_safepoint(*_bp);297break;298case CLEAR_BREAKPOINT:299_breakpoints->clear_at_safepoint(*_bp);300break;301default:302assert(false, "Unknown operation");303}304}305306//307// class JvmtiBreakpoints308//309// a JVMTI internal collection of JvmtiBreakpoint310//311312JvmtiBreakpoints::JvmtiBreakpoints(void listener_fun(void *,address *)) {313_bps.initialize(this,listener_fun);314}315316JvmtiBreakpoints:: ~JvmtiBreakpoints() {}317318void JvmtiBreakpoints::print() {319#ifndef PRODUCT320LogTarget(Trace, jvmti) log;321LogStream log_stream(log);322323int n = _bps.length();324for (int i=0; i<n; i++) {325JvmtiBreakpoint& bp = _bps.at(i);326log_stream.print("%d: ", i);327bp.print_on(&log_stream);328log_stream.cr();329}330#endif331}332333334void JvmtiBreakpoints::set_at_safepoint(JvmtiBreakpoint& bp) {335assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");336337int i = _bps.find(bp);338if (i == -1) {339_bps.append(bp);340bp.set();341}342}343344void JvmtiBreakpoints::clear_at_safepoint(JvmtiBreakpoint& bp) {345assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");346347int i = _bps.find(bp);348if (i != -1) {349_bps.remove(i);350bp.clear();351}352}353354int JvmtiBreakpoints::length() { return _bps.length(); }355356int JvmtiBreakpoints::set(JvmtiBreakpoint& bp) {357if ( _bps.find(bp) != -1) {358return JVMTI_ERROR_DUPLICATE;359}360VM_ChangeBreakpoints set_breakpoint(VM_ChangeBreakpoints::SET_BREAKPOINT, &bp);361VMThread::execute(&set_breakpoint);362return JVMTI_ERROR_NONE;363}364365int JvmtiBreakpoints::clear(JvmtiBreakpoint& bp) {366if ( _bps.find(bp) == -1) {367return JVMTI_ERROR_NOT_FOUND;368}369370VM_ChangeBreakpoints clear_breakpoint(VM_ChangeBreakpoints::CLEAR_BREAKPOINT, &bp);371VMThread::execute(&clear_breakpoint);372return JVMTI_ERROR_NONE;373}374375void JvmtiBreakpoints::clearall_in_class_at_safepoint(Klass* klass) {376bool changed = true;377// We are going to run thru the list of bkpts378// and delete some. This deletion probably alters379// the list in some implementation defined way such380// that when we delete entry i, the next entry might381// no longer be at i+1. To be safe, each time we delete382// an entry, we'll just start again from the beginning.383// We'll stop when we make a pass thru the whole list without384// deleting anything.385while (changed) {386int len = _bps.length();387changed = false;388for (int i = 0; i < len; i++) {389JvmtiBreakpoint& bp = _bps.at(i);390if (bp.method()->method_holder() == klass) {391bp.clear();392_bps.remove(i);393// This changed 'i' so we have to start over.394changed = true;395break;396}397}398}399}400401//402// class JvmtiCurrentBreakpoints403//404405JvmtiBreakpoints *JvmtiCurrentBreakpoints::_jvmti_breakpoints = NULL;406address * JvmtiCurrentBreakpoints::_breakpoint_list = NULL;407408409JvmtiBreakpoints& JvmtiCurrentBreakpoints::get_jvmti_breakpoints() {410if (_jvmti_breakpoints != NULL) return (*_jvmti_breakpoints);411_jvmti_breakpoints = new JvmtiBreakpoints(listener_fun);412assert(_jvmti_breakpoints != NULL, "_jvmti_breakpoints != NULL");413return (*_jvmti_breakpoints);414}415416void JvmtiCurrentBreakpoints::listener_fun(void *this_obj, address *cache) {417JvmtiBreakpoints *this_jvmti = (JvmtiBreakpoints *) this_obj;418assert(this_jvmti != NULL, "this_jvmti != NULL");419420debug_only(int n = this_jvmti->length(););421assert(cache[n] == NULL, "cache must be NULL terminated");422423set_breakpoint_list(cache);424}425426///////////////////////////////////////////////////////////////427//428// class VM_GetOrSetLocal429//430431// Constructor for non-object getter432VM_GetOrSetLocal::VM_GetOrSetLocal(JavaThread* thread, jint depth, jint index, BasicType type)433: _thread(thread)434, _calling_thread(NULL)435, _depth(depth)436, _index(index)437, _type(type)438, _jvf(NULL)439, _set(false)440, _eb(false, NULL, NULL)441, _result(JVMTI_ERROR_NONE)442{443}444445// Constructor for object or non-object setter446VM_GetOrSetLocal::VM_GetOrSetLocal(JavaThread* thread, jint depth, jint index, BasicType type, jvalue value)447: _thread(thread)448, _calling_thread(NULL)449, _depth(depth)450, _index(index)451, _type(type)452, _value(value)453, _jvf(NULL)454, _set(true)455, _eb(type == T_OBJECT, JavaThread::current(), thread)456, _result(JVMTI_ERROR_NONE)457{458}459460// Constructor for object getter461VM_GetOrSetLocal::VM_GetOrSetLocal(JavaThread* thread, JavaThread* calling_thread, jint depth, int index)462: _thread(thread)463, _calling_thread(calling_thread)464, _depth(depth)465, _index(index)466, _type(T_OBJECT)467, _jvf(NULL)468, _set(false)469, _eb(true, calling_thread, thread)470, _result(JVMTI_ERROR_NONE)471{472}473474vframe *VM_GetOrSetLocal::get_vframe() {475if (!_thread->has_last_Java_frame()) {476return NULL;477}478RegisterMap reg_map(_thread);479vframe *vf = _thread->last_java_vframe(®_map);480int d = 0;481while ((vf != NULL) && (d < _depth)) {482vf = vf->java_sender();483d++;484}485return vf;486}487488javaVFrame *VM_GetOrSetLocal::get_java_vframe() {489vframe* vf = get_vframe();490if (vf == NULL) {491_result = JVMTI_ERROR_NO_MORE_FRAMES;492return NULL;493}494javaVFrame *jvf = (javaVFrame*)vf;495496if (!vf->is_java_frame()) {497_result = JVMTI_ERROR_OPAQUE_FRAME;498return NULL;499}500return jvf;501}502503// Check that the klass is assignable to a type with the given signature.504// Another solution could be to use the function Klass::is_subtype_of(type).505// But the type class can be forced to load/initialize eagerly in such a case.506// This may cause unexpected consequences like CFLH or class-init JVMTI events.507// It is better to avoid such a behavior.508bool VM_GetOrSetLocal::is_assignable(const char* ty_sign, Klass* klass, Thread* thread) {509assert(ty_sign != NULL, "type signature must not be NULL");510assert(thread != NULL, "thread must not be NULL");511assert(klass != NULL, "klass must not be NULL");512513int len = (int) strlen(ty_sign);514if (ty_sign[0] == JVM_SIGNATURE_CLASS &&515ty_sign[len-1] == JVM_SIGNATURE_ENDCLASS) { // Need pure class/interface name516ty_sign++;517len -= 2;518}519TempNewSymbol ty_sym = SymbolTable::new_symbol(ty_sign, len);520if (klass->name() == ty_sym) {521return true;522}523// Compare primary supers524int super_depth = klass->super_depth();525int idx;526for (idx = 0; idx < super_depth; idx++) {527if (klass->primary_super_of_depth(idx)->name() == ty_sym) {528return true;529}530}531// Compare secondary supers532const Array<Klass*>* sec_supers = klass->secondary_supers();533for (idx = 0; idx < sec_supers->length(); idx++) {534if (((Klass*) sec_supers->at(idx))->name() == ty_sym) {535return true;536}537}538return false;539}540541// Checks error conditions:542// JVMTI_ERROR_INVALID_SLOT543// JVMTI_ERROR_TYPE_MISMATCH544// Returns: 'true' - everything is Ok, 'false' - error code545546bool VM_GetOrSetLocal::check_slot_type_lvt(javaVFrame* jvf) {547Method* method = jvf->method();548jint num_entries = method->localvariable_table_length();549if (num_entries == 0) {550_result = JVMTI_ERROR_INVALID_SLOT;551return false; // There are no slots552}553int signature_idx = -1;554int vf_bci = jvf->bci();555LocalVariableTableElement* table = method->localvariable_table_start();556for (int i = 0; i < num_entries; i++) {557int start_bci = table[i].start_bci;558int end_bci = start_bci + table[i].length;559560// Here we assume that locations of LVT entries561// with the same slot number cannot be overlapped562if (_index == (jint) table[i].slot && start_bci <= vf_bci && vf_bci <= end_bci) {563signature_idx = (int) table[i].descriptor_cp_index;564break;565}566}567if (signature_idx == -1) {568_result = JVMTI_ERROR_INVALID_SLOT;569return false; // Incorrect slot index570}571Symbol* sign_sym = method->constants()->symbol_at(signature_idx);572BasicType slot_type = Signature::basic_type(sign_sym);573574switch (slot_type) {575case T_BYTE:576case T_SHORT:577case T_CHAR:578case T_BOOLEAN:579slot_type = T_INT;580break;581case T_ARRAY:582slot_type = T_OBJECT;583break;584default:585break;586};587if (_type != slot_type) {588_result = JVMTI_ERROR_TYPE_MISMATCH;589return false;590}591592jobject jobj = _value.l;593if (_set && slot_type == T_OBJECT && jobj != NULL) { // NULL reference is allowed594// Check that the jobject class matches the return type signature.595oop obj = JNIHandles::resolve_external_guard(jobj);596NULL_CHECK(obj, (_result = JVMTI_ERROR_INVALID_OBJECT, false));597Klass* ob_k = obj->klass();598NULL_CHECK(ob_k, (_result = JVMTI_ERROR_INVALID_OBJECT, false));599600const char* signature = (const char *) sign_sym->as_utf8();601if (!is_assignable(signature, ob_k, VMThread::vm_thread())) {602_result = JVMTI_ERROR_TYPE_MISMATCH;603return false;604}605}606return true;607}608609bool VM_GetOrSetLocal::check_slot_type_no_lvt(javaVFrame* jvf) {610Method* method = jvf->method();611jint extra_slot = (_type == T_LONG || _type == T_DOUBLE) ? 1 : 0;612613if (_index < 0 || _index + extra_slot >= method->max_locals()) {614_result = JVMTI_ERROR_INVALID_SLOT;615return false;616}617StackValueCollection *locals = _jvf->locals();618BasicType slot_type = locals->at(_index)->type();619620if (slot_type == T_CONFLICT) {621_result = JVMTI_ERROR_INVALID_SLOT;622return false;623}624if (extra_slot) {625BasicType extra_slot_type = locals->at(_index + 1)->type();626if (extra_slot_type != T_INT) {627_result = JVMTI_ERROR_INVALID_SLOT;628return false;629}630}631if (_type != slot_type && (_type == T_OBJECT || slot_type != T_INT)) {632_result = JVMTI_ERROR_TYPE_MISMATCH;633return false;634}635return true;636}637638static bool can_be_deoptimized(vframe* vf) {639return (vf->is_compiled_frame() && vf->fr().can_be_deoptimized());640}641642bool VM_GetOrSetLocal::doit_prologue() {643if (!_eb.deoptimize_objects(_depth, _depth)) {644// The target frame is affected by a reallocation failure.645_result = JVMTI_ERROR_OUT_OF_MEMORY;646return false;647}648649return true;650}651652void VM_GetOrSetLocal::doit() {653_jvf = _jvf == NULL ? get_java_vframe() : _jvf;654if (_jvf == NULL) {655return;656};657658Method* method = _jvf->method();659if (getting_receiver()) {660if (method->is_static()) {661_result = JVMTI_ERROR_INVALID_SLOT;662return;663}664} else {665if (method->is_native()) {666_result = JVMTI_ERROR_OPAQUE_FRAME;667return;668}669670if (!check_slot_type_no_lvt(_jvf)) {671return;672}673if (method->has_localvariable_table() &&674!check_slot_type_lvt(_jvf)) {675return;676}677}678679InterpreterOopMap oop_mask;680_jvf->method()->mask_for(_jvf->bci(), &oop_mask);681if (oop_mask.is_dead(_index)) {682// The local can be invalid and uninitialized in the scope of current bci683_result = JVMTI_ERROR_INVALID_SLOT;684return;685}686if (_set) {687// Force deoptimization of frame if compiled because it's688// possible the compiler emitted some locals as constant values,689// meaning they are not mutable.690if (can_be_deoptimized(_jvf)) {691692// Schedule deoptimization so that eventually the local693// update will be written to an interpreter frame.694Deoptimization::deoptimize_frame(_jvf->thread(), _jvf->fr().id());695696// Now store a new value for the local which will be applied697// once deoptimization occurs. Note however that while this698// write is deferred until deoptimization actually happens699// can vframe created after this point will have its locals700// reflecting this update so as far as anyone can see the701// write has already taken place.702703// If we are updating an oop then get the oop from the handle704// since the handle will be long gone by the time the deopt705// happens. The oop stored in the deferred local will be706// gc'd on its own.707if (_type == T_OBJECT) {708_value.l = cast_from_oop<jobject>(JNIHandles::resolve_external_guard(_value.l));709}710// Re-read the vframe so we can see that it is deoptimized711// [ Only need because of assert in update_local() ]712_jvf = get_java_vframe();713((compiledVFrame*)_jvf)->update_local(_type, _index, _value);714return;715}716StackValueCollection *locals = _jvf->locals();717Thread* current_thread = VMThread::vm_thread();718HandleMark hm(current_thread);719720switch (_type) {721case T_INT: locals->set_int_at (_index, _value.i); break;722case T_LONG: locals->set_long_at (_index, _value.j); break;723case T_FLOAT: locals->set_float_at (_index, _value.f); break;724case T_DOUBLE: locals->set_double_at(_index, _value.d); break;725case T_OBJECT: {726Handle ob_h(current_thread, JNIHandles::resolve_external_guard(_value.l));727locals->set_obj_at (_index, ob_h);728break;729}730default: ShouldNotReachHere();731}732_jvf->set_locals(locals);733} else {734if (_jvf->method()->is_native() && _jvf->is_compiled_frame()) {735assert(getting_receiver(), "Can only get here when getting receiver");736oop receiver = _jvf->fr().get_native_receiver();737_value.l = JNIHandles::make_local(_calling_thread, receiver);738} else {739StackValueCollection *locals = _jvf->locals();740741switch (_type) {742case T_INT: _value.i = locals->int_at (_index); break;743case T_LONG: _value.j = locals->long_at (_index); break;744case T_FLOAT: _value.f = locals->float_at (_index); break;745case T_DOUBLE: _value.d = locals->double_at(_index); break;746case T_OBJECT: {747// Wrap the oop to be returned in a local JNI handle since748// oops_do() no longer applies after doit() is finished.749oop obj = locals->obj_at(_index)();750_value.l = JNIHandles::make_local(_calling_thread, obj);751break;752}753default: ShouldNotReachHere();754}755}756}757}758759760bool VM_GetOrSetLocal::allow_nested_vm_operations() const {761return true; // May need to deoptimize762}763764765VM_GetReceiver::VM_GetReceiver(766JavaThread* thread, JavaThread* caller_thread, jint depth)767: VM_GetOrSetLocal(thread, caller_thread, depth, 0) {}768769/////////////////////////////////////////////////////////////////////////////////////////770771//772// class JvmtiSuspendControl - see comments in jvmtiImpl.hpp773//774775bool JvmtiSuspendControl::suspend(JavaThread *java_thread) {776return java_thread->java_suspend();777}778779bool JvmtiSuspendControl::resume(JavaThread *java_thread) {780return java_thread->java_resume();781}782783void JvmtiSuspendControl::print() {784#ifndef PRODUCT785ResourceMark rm;786LogStreamHandle(Trace, jvmti) log_stream;787log_stream.print("Suspended Threads: [");788for (JavaThreadIteratorWithHandle jtiwh; JavaThread *thread = jtiwh.next(); ) {789#ifdef JVMTI_TRACE790const char *name = JvmtiTrace::safe_get_thread_name(thread);791#else792const char *name = "";793#endif /*JVMTI_TRACE */794log_stream.print("%s(%c ", name, thread->is_suspended() ? 'S' : '_');795if (!thread->has_last_Java_frame()) {796log_stream.print("no stack");797}798log_stream.print(") ");799}800log_stream.print_cr("]");801#endif802}803804JvmtiDeferredEvent JvmtiDeferredEvent::compiled_method_load_event(805nmethod* nm) {806JvmtiDeferredEvent event = JvmtiDeferredEvent(TYPE_COMPILED_METHOD_LOAD);807event._event_data.compiled_method_load = nm;808return event;809}810811JvmtiDeferredEvent JvmtiDeferredEvent::compiled_method_unload_event(812jmethodID id, const void* code) {813JvmtiDeferredEvent event = JvmtiDeferredEvent(TYPE_COMPILED_METHOD_UNLOAD);814event._event_data.compiled_method_unload.method_id = id;815event._event_data.compiled_method_unload.code_begin = code;816return event;817}818819JvmtiDeferredEvent JvmtiDeferredEvent::dynamic_code_generated_event(820const char* name, const void* code_begin, const void* code_end) {821JvmtiDeferredEvent event = JvmtiDeferredEvent(TYPE_DYNAMIC_CODE_GENERATED);822// Need to make a copy of the name since we don't know how long823// the event poster will keep it around after we enqueue the824// deferred event and return. strdup() failure is handled in825// the post() routine below.826event._event_data.dynamic_code_generated.name = os::strdup(name);827event._event_data.dynamic_code_generated.code_begin = code_begin;828event._event_data.dynamic_code_generated.code_end = code_end;829return event;830}831832JvmtiDeferredEvent JvmtiDeferredEvent::class_unload_event(const char* name) {833JvmtiDeferredEvent event = JvmtiDeferredEvent(TYPE_CLASS_UNLOAD);834// Need to make a copy of the name since we don't know how long835// the event poster will keep it around after we enqueue the836// deferred event and return. strdup() failure is handled in837// the post() routine below.838event._event_data.class_unload.name = os::strdup(name);839return event;840}841842void JvmtiDeferredEvent::post() {843assert(Thread::current()->is_service_thread(),844"Service thread must post enqueued events");845switch(_type) {846case TYPE_COMPILED_METHOD_LOAD: {847nmethod* nm = _event_data.compiled_method_load;848JvmtiExport::post_compiled_method_load(nm);849break;850}851case TYPE_COMPILED_METHOD_UNLOAD: {852JvmtiExport::post_compiled_method_unload(853_event_data.compiled_method_unload.method_id,854_event_data.compiled_method_unload.code_begin);855break;856}857case TYPE_DYNAMIC_CODE_GENERATED: {858JvmtiExport::post_dynamic_code_generated_internal(859// if strdup failed give the event a default name860(_event_data.dynamic_code_generated.name == NULL)861? "unknown_code" : _event_data.dynamic_code_generated.name,862_event_data.dynamic_code_generated.code_begin,863_event_data.dynamic_code_generated.code_end);864if (_event_data.dynamic_code_generated.name != NULL) {865// release our copy866os::free((void *)_event_data.dynamic_code_generated.name);867}868break;869}870case TYPE_CLASS_UNLOAD: {871JvmtiExport::post_class_unload_internal(872// if strdup failed give the event a default name873(_event_data.class_unload.name == NULL)874? "unknown_class" : _event_data.class_unload.name);875if (_event_data.class_unload.name != NULL) {876// release our copy877os::free((void *)_event_data.class_unload.name);878}879break;880}881default:882ShouldNotReachHere();883}884}885886void JvmtiDeferredEvent::post_compiled_method_load_event(JvmtiEnv* env) {887assert(_type == TYPE_COMPILED_METHOD_LOAD, "only user of this method");888nmethod* nm = _event_data.compiled_method_load;889JvmtiExport::post_compiled_method_load(env, nm);890}891892void JvmtiDeferredEvent::run_nmethod_entry_barriers() {893if (_type == TYPE_COMPILED_METHOD_LOAD) {894_event_data.compiled_method_load->run_nmethod_entry_barrier();895}896}897898899// Keep the nmethod for compiled_method_load from being unloaded.900void JvmtiDeferredEvent::oops_do(OopClosure* f, CodeBlobClosure* cf) {901if (cf != NULL && _type == TYPE_COMPILED_METHOD_LOAD) {902cf->do_code_blob(_event_data.compiled_method_load);903}904}905906// The sweeper calls this and marks the nmethods here on the stack so that907// they cannot be turned into zombies while in the queue.908void JvmtiDeferredEvent::nmethods_do(CodeBlobClosure* cf) {909if (cf != NULL && _type == TYPE_COMPILED_METHOD_LOAD) {910cf->do_code_blob(_event_data.compiled_method_load);911}912}913914915bool JvmtiDeferredEventQueue::has_events() {916// We save the queued events before the live phase and post them when it starts.917// This code could skip saving the events on the queue before the live918// phase and ignore them, but this would change how we do things now.919// Starting the service thread earlier causes this to be called before the live phase begins.920// The events on the queue should all be posted after the live phase so this is an921// ok check. Before the live phase, DynamicCodeGenerated events are posted directly.922// If we add other types of events to the deferred queue, this could get ugly.923return JvmtiEnvBase::get_phase() == JVMTI_PHASE_LIVE && _queue_head != NULL;924}925926void JvmtiDeferredEventQueue::enqueue(JvmtiDeferredEvent event) {927// Events get added to the end of the queue (and are pulled off the front).928QueueNode* node = new QueueNode(event);929if (_queue_tail == NULL) {930_queue_tail = _queue_head = node;931} else {932assert(_queue_tail->next() == NULL, "Must be the last element in the list");933_queue_tail->set_next(node);934_queue_tail = node;935}936937assert((_queue_head == NULL) == (_queue_tail == NULL),938"Inconsistent queue markers");939}940941JvmtiDeferredEvent JvmtiDeferredEventQueue::dequeue() {942assert(_queue_head != NULL, "Nothing to dequeue");943944if (_queue_head == NULL) {945// Just in case this happens in product; it shouldn't but let's not crash946return JvmtiDeferredEvent();947}948949QueueNode* node = _queue_head;950_queue_head = _queue_head->next();951if (_queue_head == NULL) {952_queue_tail = NULL;953}954955assert((_queue_head == NULL) == (_queue_tail == NULL),956"Inconsistent queue markers");957958JvmtiDeferredEvent event = node->event();959delete node;960return event;961}962963void JvmtiDeferredEventQueue::post(JvmtiEnv* env) {964// Post and destroy queue nodes965while (_queue_head != NULL) {966JvmtiDeferredEvent event = dequeue();967event.post_compiled_method_load_event(env);968}969}970971void JvmtiDeferredEventQueue::run_nmethod_entry_barriers() {972for(QueueNode* node = _queue_head; node != NULL; node = node->next()) {973node->event().run_nmethod_entry_barriers();974}975}976977978void JvmtiDeferredEventQueue::oops_do(OopClosure* f, CodeBlobClosure* cf) {979for(QueueNode* node = _queue_head; node != NULL; node = node->next()) {980node->event().oops_do(f, cf);981}982}983984void JvmtiDeferredEventQueue::nmethods_do(CodeBlobClosure* cf) {985for(QueueNode* node = _queue_head; node != NULL; node = node->next()) {986node->event().nmethods_do(cf);987}988}989990991