Path: blob/master/src/hotspot/share/prims/jvmtiImpl.cpp
64440 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() {208_class_holder.release(JvmtiExport::jvmti_oop_storage());209}210211void JvmtiBreakpoint::copy(JvmtiBreakpoint& bp) {212_method = bp._method;213_bci = bp._bci;214_class_holder = OopHandle(JvmtiExport::jvmti_oop_storage(), bp._class_holder.resolve());215}216217bool JvmtiBreakpoint::equals(JvmtiBreakpoint& bp) {218return _method == bp._method219&& _bci == bp._bci;220}221222address JvmtiBreakpoint::getBcp() const {223return _method->bcp_from(_bci);224}225226void JvmtiBreakpoint::each_method_version_do(method_action meth_act) {227((Method*)_method->*meth_act)(_bci);228229// add/remove breakpoint to/from versions of the method that are EMCP.230Thread *thread = Thread::current();231InstanceKlass* ik = _method->method_holder();232Symbol* m_name = _method->name();233Symbol* m_signature = _method->signature();234235// search previous versions if they exist236for (InstanceKlass* pv_node = ik->previous_versions();237pv_node != NULL;238pv_node = pv_node->previous_versions()) {239Array<Method*>* methods = pv_node->methods();240241for (int i = methods->length() - 1; i >= 0; i--) {242Method* method = methods->at(i);243// Only set breakpoints in EMCP methods.244// EMCP methods are old but not obsolete. Equivalent245// Modulo Constant Pool means the method is equivalent except246// the constant pool and instructions that access the constant247// pool might be different.248// If a breakpoint is set in a redefined method, its EMCP methods249// must have a breakpoint also.250// None of the methods are deleted until none are running.251// This code could set a breakpoint in a method that252// is never reached, but this won't be noticeable to the programmer.253if (!method->is_obsolete() &&254method->name() == m_name &&255method->signature() == m_signature) {256ResourceMark rm;257log_debug(redefine, class, breakpoint)258("%sing breakpoint in %s(%s)", meth_act == &Method::set_breakpoint ? "sett" : "clear",259method->name()->as_C_string(), method->signature()->as_C_string());260(method->*meth_act)(_bci);261break;262}263}264}265}266267void JvmtiBreakpoint::set() {268each_method_version_do(&Method::set_breakpoint);269}270271void JvmtiBreakpoint::clear() {272each_method_version_do(&Method::clear_breakpoint);273}274275void JvmtiBreakpoint::print_on(outputStream* out) const {276#ifndef PRODUCT277ResourceMark rm;278const char *class_name = (_method == NULL) ? "NULL" : _method->klass_name()->as_C_string();279const char *method_name = (_method == NULL) ? "NULL" : _method->name()->as_C_string();280out->print("Breakpoint(%s,%s,%d,%p)", class_name, method_name, _bci, getBcp());281#endif282}283284285//286// class VM_ChangeBreakpoints287//288// Modify the Breakpoints data structure at a safepoint289//290291void VM_ChangeBreakpoints::doit() {292switch (_operation) {293case SET_BREAKPOINT:294_breakpoints->set_at_safepoint(*_bp);295break;296case CLEAR_BREAKPOINT:297_breakpoints->clear_at_safepoint(*_bp);298break;299default:300assert(false, "Unknown operation");301}302}303304//305// class JvmtiBreakpoints306//307// a JVMTI internal collection of JvmtiBreakpoint308//309310JvmtiBreakpoints::JvmtiBreakpoints(void listener_fun(void *,address *)) {311_bps.initialize(this,listener_fun);312}313314JvmtiBreakpoints:: ~JvmtiBreakpoints() {}315316void JvmtiBreakpoints::print() {317#ifndef PRODUCT318LogTarget(Trace, jvmti) log;319LogStream log_stream(log);320321int n = _bps.length();322for (int i=0; i<n; i++) {323JvmtiBreakpoint& bp = _bps.at(i);324log_stream.print("%d: ", i);325bp.print_on(&log_stream);326log_stream.cr();327}328#endif329}330331332void JvmtiBreakpoints::set_at_safepoint(JvmtiBreakpoint& bp) {333assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");334335int i = _bps.find(bp);336if (i == -1) {337_bps.append(bp);338bp.set();339}340}341342void JvmtiBreakpoints::clear_at_safepoint(JvmtiBreakpoint& bp) {343assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");344345int i = _bps.find(bp);346if (i != -1) {347_bps.remove(i);348bp.clear();349}350}351352int JvmtiBreakpoints::length() { return _bps.length(); }353354int JvmtiBreakpoints::set(JvmtiBreakpoint& bp) {355if ( _bps.find(bp) != -1) {356return JVMTI_ERROR_DUPLICATE;357}358VM_ChangeBreakpoints set_breakpoint(VM_ChangeBreakpoints::SET_BREAKPOINT, &bp);359VMThread::execute(&set_breakpoint);360return JVMTI_ERROR_NONE;361}362363int JvmtiBreakpoints::clear(JvmtiBreakpoint& bp) {364if ( _bps.find(bp) == -1) {365return JVMTI_ERROR_NOT_FOUND;366}367368VM_ChangeBreakpoints clear_breakpoint(VM_ChangeBreakpoints::CLEAR_BREAKPOINT, &bp);369VMThread::execute(&clear_breakpoint);370return JVMTI_ERROR_NONE;371}372373void JvmtiBreakpoints::clearall_in_class_at_safepoint(Klass* klass) {374bool changed = true;375// We are going to run thru the list of bkpts376// and delete some. This deletion probably alters377// the list in some implementation defined way such378// that when we delete entry i, the next entry might379// no longer be at i+1. To be safe, each time we delete380// an entry, we'll just start again from the beginning.381// We'll stop when we make a pass thru the whole list without382// deleting anything.383while (changed) {384int len = _bps.length();385changed = false;386for (int i = 0; i < len; i++) {387JvmtiBreakpoint& bp = _bps.at(i);388if (bp.method()->method_holder() == klass) {389bp.clear();390_bps.remove(i);391// This changed 'i' so we have to start over.392changed = true;393break;394}395}396}397}398399//400// class JvmtiCurrentBreakpoints401//402403JvmtiBreakpoints *JvmtiCurrentBreakpoints::_jvmti_breakpoints = NULL;404address * JvmtiCurrentBreakpoints::_breakpoint_list = NULL;405406407JvmtiBreakpoints& JvmtiCurrentBreakpoints::get_jvmti_breakpoints() {408if (_jvmti_breakpoints != NULL) return (*_jvmti_breakpoints);409_jvmti_breakpoints = new JvmtiBreakpoints(listener_fun);410assert(_jvmti_breakpoints != NULL, "_jvmti_breakpoints != NULL");411return (*_jvmti_breakpoints);412}413414void JvmtiCurrentBreakpoints::listener_fun(void *this_obj, address *cache) {415JvmtiBreakpoints *this_jvmti = (JvmtiBreakpoints *) this_obj;416assert(this_jvmti != NULL, "this_jvmti != NULL");417418debug_only(int n = this_jvmti->length(););419assert(cache[n] == NULL, "cache must be NULL terminated");420421set_breakpoint_list(cache);422}423424///////////////////////////////////////////////////////////////425//426// class VM_GetOrSetLocal427//428429// Constructor for non-object getter430VM_GetOrSetLocal::VM_GetOrSetLocal(JavaThread* thread, jint depth, jint index, BasicType type)431: _thread(thread)432, _calling_thread(NULL)433, _depth(depth)434, _index(index)435, _type(type)436, _jvf(NULL)437, _set(false)438, _eb(false, NULL, NULL)439, _result(JVMTI_ERROR_NONE)440{441}442443// Constructor for object or non-object setter444VM_GetOrSetLocal::VM_GetOrSetLocal(JavaThread* thread, jint depth, jint index, BasicType type, jvalue value)445: _thread(thread)446, _calling_thread(NULL)447, _depth(depth)448, _index(index)449, _type(type)450, _value(value)451, _jvf(NULL)452, _set(true)453, _eb(type == T_OBJECT, JavaThread::current(), thread)454, _result(JVMTI_ERROR_NONE)455{456}457458// Constructor for object getter459VM_GetOrSetLocal::VM_GetOrSetLocal(JavaThread* thread, JavaThread* calling_thread, jint depth, int index)460: _thread(thread)461, _calling_thread(calling_thread)462, _depth(depth)463, _index(index)464, _type(T_OBJECT)465, _jvf(NULL)466, _set(false)467, _eb(true, calling_thread, thread)468, _result(JVMTI_ERROR_NONE)469{470}471472vframe *VM_GetOrSetLocal::get_vframe() {473if (!_thread->has_last_Java_frame()) {474return NULL;475}476RegisterMap reg_map(_thread);477vframe *vf = _thread->last_java_vframe(®_map);478int d = 0;479while ((vf != NULL) && (d < _depth)) {480vf = vf->java_sender();481d++;482}483return vf;484}485486javaVFrame *VM_GetOrSetLocal::get_java_vframe() {487vframe* vf = get_vframe();488if (vf == NULL) {489_result = JVMTI_ERROR_NO_MORE_FRAMES;490return NULL;491}492javaVFrame *jvf = (javaVFrame*)vf;493494if (!vf->is_java_frame()) {495_result = JVMTI_ERROR_OPAQUE_FRAME;496return NULL;497}498return jvf;499}500501// Check that the klass is assignable to a type with the given signature.502// Another solution could be to use the function Klass::is_subtype_of(type).503// But the type class can be forced to load/initialize eagerly in such a case.504// This may cause unexpected consequences like CFLH or class-init JVMTI events.505// It is better to avoid such a behavior.506bool VM_GetOrSetLocal::is_assignable(const char* ty_sign, Klass* klass, Thread* thread) {507assert(ty_sign != NULL, "type signature must not be NULL");508assert(thread != NULL, "thread must not be NULL");509assert(klass != NULL, "klass must not be NULL");510511int len = (int) strlen(ty_sign);512if (ty_sign[0] == JVM_SIGNATURE_CLASS &&513ty_sign[len-1] == JVM_SIGNATURE_ENDCLASS) { // Need pure class/interface name514ty_sign++;515len -= 2;516}517TempNewSymbol ty_sym = SymbolTable::new_symbol(ty_sign, len);518if (klass->name() == ty_sym) {519return true;520}521// Compare primary supers522int super_depth = klass->super_depth();523int idx;524for (idx = 0; idx < super_depth; idx++) {525if (klass->primary_super_of_depth(idx)->name() == ty_sym) {526return true;527}528}529// Compare secondary supers530const Array<Klass*>* sec_supers = klass->secondary_supers();531for (idx = 0; idx < sec_supers->length(); idx++) {532if (((Klass*) sec_supers->at(idx))->name() == ty_sym) {533return true;534}535}536return false;537}538539// Checks error conditions:540// JVMTI_ERROR_INVALID_SLOT541// JVMTI_ERROR_TYPE_MISMATCH542// Returns: 'true' - everything is Ok, 'false' - error code543544bool VM_GetOrSetLocal::check_slot_type_lvt(javaVFrame* jvf) {545Method* method = jvf->method();546jint num_entries = method->localvariable_table_length();547if (num_entries == 0) {548_result = JVMTI_ERROR_INVALID_SLOT;549return false; // There are no slots550}551int signature_idx = -1;552int vf_bci = jvf->bci();553LocalVariableTableElement* table = method->localvariable_table_start();554for (int i = 0; i < num_entries; i++) {555int start_bci = table[i].start_bci;556int end_bci = start_bci + table[i].length;557558// Here we assume that locations of LVT entries559// with the same slot number cannot be overlapped560if (_index == (jint) table[i].slot && start_bci <= vf_bci && vf_bci <= end_bci) {561signature_idx = (int) table[i].descriptor_cp_index;562break;563}564}565if (signature_idx == -1) {566_result = JVMTI_ERROR_INVALID_SLOT;567return false; // Incorrect slot index568}569Symbol* sign_sym = method->constants()->symbol_at(signature_idx);570BasicType slot_type = Signature::basic_type(sign_sym);571572switch (slot_type) {573case T_BYTE:574case T_SHORT:575case T_CHAR:576case T_BOOLEAN:577slot_type = T_INT;578break;579case T_ARRAY:580slot_type = T_OBJECT;581break;582default:583break;584};585if (_type != slot_type) {586_result = JVMTI_ERROR_TYPE_MISMATCH;587return false;588}589590jobject jobj = _value.l;591if (_set && slot_type == T_OBJECT && jobj != NULL) { // NULL reference is allowed592// Check that the jobject class matches the return type signature.593oop obj = JNIHandles::resolve_external_guard(jobj);594NULL_CHECK(obj, (_result = JVMTI_ERROR_INVALID_OBJECT, false));595Klass* ob_k = obj->klass();596NULL_CHECK(ob_k, (_result = JVMTI_ERROR_INVALID_OBJECT, false));597598const char* signature = (const char *) sign_sym->as_utf8();599if (!is_assignable(signature, ob_k, VMThread::vm_thread())) {600_result = JVMTI_ERROR_TYPE_MISMATCH;601return false;602}603}604return true;605}606607bool VM_GetOrSetLocal::check_slot_type_no_lvt(javaVFrame* jvf) {608Method* method = jvf->method();609jint extra_slot = (_type == T_LONG || _type == T_DOUBLE) ? 1 : 0;610611if (_index < 0 || _index + extra_slot >= method->max_locals()) {612_result = JVMTI_ERROR_INVALID_SLOT;613return false;614}615StackValueCollection *locals = _jvf->locals();616BasicType slot_type = locals->at(_index)->type();617618if (slot_type == T_CONFLICT) {619_result = JVMTI_ERROR_INVALID_SLOT;620return false;621}622if (extra_slot) {623BasicType extra_slot_type = locals->at(_index + 1)->type();624if (extra_slot_type != T_INT) {625_result = JVMTI_ERROR_INVALID_SLOT;626return false;627}628}629if (_type != slot_type && (_type == T_OBJECT || slot_type != T_INT)) {630_result = JVMTI_ERROR_TYPE_MISMATCH;631return false;632}633return true;634}635636static bool can_be_deoptimized(vframe* vf) {637return (vf->is_compiled_frame() && vf->fr().can_be_deoptimized());638}639640bool VM_GetOrSetLocal::doit_prologue() {641if (!_eb.deoptimize_objects(_depth, _depth)) {642// The target frame is affected by a reallocation failure.643_result = JVMTI_ERROR_OUT_OF_MEMORY;644return false;645}646647return true;648}649650void VM_GetOrSetLocal::doit() {651_jvf = _jvf == NULL ? get_java_vframe() : _jvf;652if (_jvf == NULL) {653return;654};655656Method* method = _jvf->method();657if (getting_receiver()) {658if (method->is_static()) {659_result = JVMTI_ERROR_INVALID_SLOT;660return;661}662} else {663if (method->is_native()) {664_result = JVMTI_ERROR_OPAQUE_FRAME;665return;666}667668if (!check_slot_type_no_lvt(_jvf)) {669return;670}671if (method->has_localvariable_table() &&672!check_slot_type_lvt(_jvf)) {673return;674}675}676677InterpreterOopMap oop_mask;678_jvf->method()->mask_for(_jvf->bci(), &oop_mask);679if (oop_mask.is_dead(_index)) {680// The local can be invalid and uninitialized in the scope of current bci681_result = JVMTI_ERROR_INVALID_SLOT;682return;683}684if (_set) {685// Force deoptimization of frame if compiled because it's686// possible the compiler emitted some locals as constant values,687// meaning they are not mutable.688if (can_be_deoptimized(_jvf)) {689690// Schedule deoptimization so that eventually the local691// update will be written to an interpreter frame.692Deoptimization::deoptimize_frame(_jvf->thread(), _jvf->fr().id());693694// Now store a new value for the local which will be applied695// once deoptimization occurs. Note however that while this696// write is deferred until deoptimization actually happens697// can vframe created after this point will have its locals698// reflecting this update so as far as anyone can see the699// write has already taken place.700701// If we are updating an oop then get the oop from the handle702// since the handle will be long gone by the time the deopt703// happens. The oop stored in the deferred local will be704// gc'd on its own.705if (_type == T_OBJECT) {706_value.l = cast_from_oop<jobject>(JNIHandles::resolve_external_guard(_value.l));707}708// Re-read the vframe so we can see that it is deoptimized709// [ Only need because of assert in update_local() ]710_jvf = get_java_vframe();711((compiledVFrame*)_jvf)->update_local(_type, _index, _value);712return;713}714StackValueCollection *locals = _jvf->locals();715Thread* current_thread = VMThread::vm_thread();716HandleMark hm(current_thread);717718switch (_type) {719case T_INT: locals->set_int_at (_index, _value.i); break;720case T_LONG: locals->set_long_at (_index, _value.j); break;721case T_FLOAT: locals->set_float_at (_index, _value.f); break;722case T_DOUBLE: locals->set_double_at(_index, _value.d); break;723case T_OBJECT: {724Handle ob_h(current_thread, JNIHandles::resolve_external_guard(_value.l));725locals->set_obj_at (_index, ob_h);726break;727}728default: ShouldNotReachHere();729}730_jvf->set_locals(locals);731} else {732if (_jvf->method()->is_native() && _jvf->is_compiled_frame()) {733assert(getting_receiver(), "Can only get here when getting receiver");734oop receiver = _jvf->fr().get_native_receiver();735_value.l = JNIHandles::make_local(_calling_thread, receiver);736} else {737StackValueCollection *locals = _jvf->locals();738739switch (_type) {740case T_INT: _value.i = locals->int_at (_index); break;741case T_LONG: _value.j = locals->long_at (_index); break;742case T_FLOAT: _value.f = locals->float_at (_index); break;743case T_DOUBLE: _value.d = locals->double_at(_index); break;744case T_OBJECT: {745// Wrap the oop to be returned in a local JNI handle since746// oops_do() no longer applies after doit() is finished.747oop obj = locals->obj_at(_index)();748_value.l = JNIHandles::make_local(_calling_thread, obj);749break;750}751default: ShouldNotReachHere();752}753}754}755}756757758bool VM_GetOrSetLocal::allow_nested_vm_operations() const {759return true; // May need to deoptimize760}761762763VM_GetReceiver::VM_GetReceiver(764JavaThread* thread, JavaThread* caller_thread, jint depth)765: VM_GetOrSetLocal(thread, caller_thread, depth, 0) {}766767/////////////////////////////////////////////////////////////////////////////////////////768769//770// class JvmtiSuspendControl - see comments in jvmtiImpl.hpp771//772773bool JvmtiSuspendControl::suspend(JavaThread *java_thread) {774return java_thread->java_suspend();775}776777bool JvmtiSuspendControl::resume(JavaThread *java_thread) {778return java_thread->java_resume();779}780781void JvmtiSuspendControl::print() {782#ifndef PRODUCT783ResourceMark rm;784LogStreamHandle(Trace, jvmti) log_stream;785log_stream.print("Suspended Threads: [");786for (JavaThreadIteratorWithHandle jtiwh; JavaThread *thread = jtiwh.next(); ) {787#ifdef JVMTI_TRACE788const char *name = JvmtiTrace::safe_get_thread_name(thread);789#else790const char *name = "";791#endif /*JVMTI_TRACE */792log_stream.print("%s(%c ", name, thread->is_suspended() ? 'S' : '_');793if (!thread->has_last_Java_frame()) {794log_stream.print("no stack");795}796log_stream.print(") ");797}798log_stream.print_cr("]");799#endif800}801802JvmtiDeferredEvent JvmtiDeferredEvent::compiled_method_load_event(803nmethod* nm) {804JvmtiDeferredEvent event = JvmtiDeferredEvent(TYPE_COMPILED_METHOD_LOAD);805event._event_data.compiled_method_load = nm;806return event;807}808809JvmtiDeferredEvent JvmtiDeferredEvent::compiled_method_unload_event(810jmethodID id, const void* code) {811JvmtiDeferredEvent event = JvmtiDeferredEvent(TYPE_COMPILED_METHOD_UNLOAD);812event._event_data.compiled_method_unload.method_id = id;813event._event_data.compiled_method_unload.code_begin = code;814return event;815}816817JvmtiDeferredEvent JvmtiDeferredEvent::dynamic_code_generated_event(818const char* name, const void* code_begin, const void* code_end) {819JvmtiDeferredEvent event = JvmtiDeferredEvent(TYPE_DYNAMIC_CODE_GENERATED);820// Need to make a copy of the name since we don't know how long821// the event poster will keep it around after we enqueue the822// deferred event and return. strdup() failure is handled in823// the post() routine below.824event._event_data.dynamic_code_generated.name = os::strdup(name);825event._event_data.dynamic_code_generated.code_begin = code_begin;826event._event_data.dynamic_code_generated.code_end = code_end;827return event;828}829830JvmtiDeferredEvent JvmtiDeferredEvent::class_unload_event(const char* name) {831JvmtiDeferredEvent event = JvmtiDeferredEvent(TYPE_CLASS_UNLOAD);832// Need to make a copy of the name since we don't know how long833// the event poster will keep it around after we enqueue the834// deferred event and return. strdup() failure is handled in835// the post() routine below.836event._event_data.class_unload.name = os::strdup(name);837return event;838}839840void JvmtiDeferredEvent::post() {841assert(Thread::current()->is_service_thread(),842"Service thread must post enqueued events");843switch(_type) {844case TYPE_COMPILED_METHOD_LOAD: {845nmethod* nm = _event_data.compiled_method_load;846JvmtiExport::post_compiled_method_load(nm);847break;848}849case TYPE_COMPILED_METHOD_UNLOAD: {850JvmtiExport::post_compiled_method_unload(851_event_data.compiled_method_unload.method_id,852_event_data.compiled_method_unload.code_begin);853break;854}855case TYPE_DYNAMIC_CODE_GENERATED: {856JvmtiExport::post_dynamic_code_generated_internal(857// if strdup failed give the event a default name858(_event_data.dynamic_code_generated.name == NULL)859? "unknown_code" : _event_data.dynamic_code_generated.name,860_event_data.dynamic_code_generated.code_begin,861_event_data.dynamic_code_generated.code_end);862if (_event_data.dynamic_code_generated.name != NULL) {863// release our copy864os::free((void *)_event_data.dynamic_code_generated.name);865}866break;867}868case TYPE_CLASS_UNLOAD: {869JvmtiExport::post_class_unload_internal(870// if strdup failed give the event a default name871(_event_data.class_unload.name == NULL)872? "unknown_class" : _event_data.class_unload.name);873if (_event_data.class_unload.name != NULL) {874// release our copy875os::free((void *)_event_data.class_unload.name);876}877break;878}879default:880ShouldNotReachHere();881}882}883884void JvmtiDeferredEvent::post_compiled_method_load_event(JvmtiEnv* env) {885assert(_type == TYPE_COMPILED_METHOD_LOAD, "only user of this method");886nmethod* nm = _event_data.compiled_method_load;887JvmtiExport::post_compiled_method_load(env, nm);888}889890void JvmtiDeferredEvent::run_nmethod_entry_barriers() {891if (_type == TYPE_COMPILED_METHOD_LOAD) {892_event_data.compiled_method_load->run_nmethod_entry_barrier();893}894}895896897// Keep the nmethod for compiled_method_load from being unloaded.898void JvmtiDeferredEvent::oops_do(OopClosure* f, CodeBlobClosure* cf) {899if (cf != NULL && _type == TYPE_COMPILED_METHOD_LOAD) {900cf->do_code_blob(_event_data.compiled_method_load);901}902}903904// The sweeper calls this and marks the nmethods here on the stack so that905// they cannot be turned into zombies while in the queue.906void JvmtiDeferredEvent::nmethods_do(CodeBlobClosure* cf) {907if (cf != NULL && _type == TYPE_COMPILED_METHOD_LOAD) {908cf->do_code_blob(_event_data.compiled_method_load);909}910}911912913bool JvmtiDeferredEventQueue::has_events() {914// We save the queued events before the live phase and post them when it starts.915// This code could skip saving the events on the queue before the live916// phase and ignore them, but this would change how we do things now.917// Starting the service thread earlier causes this to be called before the live phase begins.918// The events on the queue should all be posted after the live phase so this is an919// ok check. Before the live phase, DynamicCodeGenerated events are posted directly.920// If we add other types of events to the deferred queue, this could get ugly.921return JvmtiEnvBase::get_phase() == JVMTI_PHASE_LIVE && _queue_head != NULL;922}923924void JvmtiDeferredEventQueue::enqueue(JvmtiDeferredEvent event) {925// Events get added to the end of the queue (and are pulled off the front).926QueueNode* node = new QueueNode(event);927if (_queue_tail == NULL) {928_queue_tail = _queue_head = node;929} else {930assert(_queue_tail->next() == NULL, "Must be the last element in the list");931_queue_tail->set_next(node);932_queue_tail = node;933}934935assert((_queue_head == NULL) == (_queue_tail == NULL),936"Inconsistent queue markers");937}938939JvmtiDeferredEvent JvmtiDeferredEventQueue::dequeue() {940assert(_queue_head != NULL, "Nothing to dequeue");941942if (_queue_head == NULL) {943// Just in case this happens in product; it shouldn't but let's not crash944return JvmtiDeferredEvent();945}946947QueueNode* node = _queue_head;948_queue_head = _queue_head->next();949if (_queue_head == NULL) {950_queue_tail = NULL;951}952953assert((_queue_head == NULL) == (_queue_tail == NULL),954"Inconsistent queue markers");955956JvmtiDeferredEvent event = node->event();957delete node;958return event;959}960961void JvmtiDeferredEventQueue::post(JvmtiEnv* env) {962// Post and destroy queue nodes963while (_queue_head != NULL) {964JvmtiDeferredEvent event = dequeue();965event.post_compiled_method_load_event(env);966}967}968969void JvmtiDeferredEventQueue::run_nmethod_entry_barriers() {970for(QueueNode* node = _queue_head; node != NULL; node = node->next()) {971node->event().run_nmethod_entry_barriers();972}973}974975976void JvmtiDeferredEventQueue::oops_do(OopClosure* f, CodeBlobClosure* cf) {977for(QueueNode* node = _queue_head; node != NULL; node = node->next()) {978node->event().oops_do(f, cf);979}980}981982void JvmtiDeferredEventQueue::nmethods_do(CodeBlobClosure* cf) {983for(QueueNode* node = _queue_head; node != NULL; node = node->next()) {984node->event().nmethods_do(cf);985}986}987988989