Path: blob/master/src/hotspot/share/prims/jvmtiEnvBase.hpp
41144 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#ifndef SHARE_PRIMS_JVMTIENVBASE_HPP25#define SHARE_PRIMS_JVMTIENVBASE_HPP2627#include "prims/jvmtiEnvThreadState.hpp"28#include "prims/jvmtiEventController.hpp"29#include "prims/jvmtiThreadState.hpp"30#include "oops/oopHandle.hpp"31#include "runtime/atomic.hpp"32#include "runtime/fieldDescriptor.hpp"33#include "runtime/frame.hpp"34#include "runtime/thread.hpp"35#include "runtime/vmOperation.hpp"36#include "utilities/growableArray.hpp"37#include "utilities/macros.hpp"3839//40// Forward Declarations41//4243class JvmtiEnv;44class JvmtiThreadState;45class JvmtiRawMonitor; // for jvmtiEnv.hpp46class JvmtiEventControllerPrivate;47class JvmtiTagMap;48495051// One JvmtiEnv object is created per jvmti attachment;52// done via JNI GetEnv() call. Multiple attachments are53// allowed in jvmti.5455class JvmtiEnvBase : public CHeapObj<mtInternal> {5657private:5859#if INCLUDE_JVMTI60static JvmtiEnvBase* _head_environment; // head of environment list61#endif // INCLUDE_JVMTI6263static bool _globally_initialized;64static jvmtiPhase _phase;65static volatile int _dying_thread_env_iteration_count;6667public:6869enum {70JDK15_JVMTI_VERSION = JVMTI_VERSION_1_0 + 33, /* version: 1.0.33 */71JDK16_JVMTI_VERSION = JVMTI_VERSION_1_1 + 102, /* version: 1.1.102 */72JDK17_JVMTI_VERSION = JVMTI_VERSION_1_2 + 2 /* version: 1.2.2 */73};7475static jvmtiPhase get_phase() { return _phase; }76static jvmtiPhase get_phase(jvmtiEnv* env) { return ((JvmtiEnvBase*)JvmtiEnv_from_jvmti_env(env))->phase(); }77static void set_phase(jvmtiPhase phase) { _phase = phase; }78static bool is_vm_live() { return _phase == JVMTI_PHASE_LIVE; }7980static void entering_dying_thread_env_iteration() { ++_dying_thread_env_iteration_count; }81static void leaving_dying_thread_env_iteration() { --_dying_thread_env_iteration_count; }82static bool is_inside_dying_thread_env_iteration(){ return _dying_thread_env_iteration_count > 0; }8384private:8586enum {87JVMTI_MAGIC = 0x71EE,88DISPOSED_MAGIC = 0xDEFC,89BAD_MAGIC = 0xDEAD90};9192jvmtiEnv _jvmti_external;93jint _magic;94jint _version; // version value passed to JNI GetEnv()95JvmtiEnvBase* _next;96bool _is_retransformable;97const void *_env_local_storage; // per env agent allocated data.98jvmtiEventCallbacks _event_callbacks;99jvmtiExtEventCallbacks _ext_event_callbacks;100JvmtiTagMap* volatile _tag_map;101JvmtiEnvEventEnable _env_event_enable;102jvmtiCapabilities _current_capabilities;103jvmtiCapabilities _prohibited_capabilities;104volatile bool _class_file_load_hook_ever_enabled;105static volatile bool _needs_clean_up;106char** _native_method_prefixes;107int _native_method_prefix_count;108109protected:110JvmtiEnvBase(jint version);111~JvmtiEnvBase();112void dispose();113void env_dispose();114115void set_env_local_storage(const void* data) { _env_local_storage = data; }116const void* get_env_local_storage() { return _env_local_storage; }117118void record_class_file_load_hook_enabled();119void record_first_time_class_file_load_hook_enabled();120121char** get_native_method_prefixes() { return _native_method_prefixes; }122int get_native_method_prefix_count() { return _native_method_prefix_count; }123jvmtiError set_native_method_prefixes(jint prefix_count, char** prefixes);124125private:126friend class JvmtiEventControllerPrivate;127void initialize();128void set_event_callbacks(const jvmtiEventCallbacks* callbacks, jint size_of_callbacks);129static void globally_initialize();130static void periodic_clean_up();131132friend class JvmtiEnvIterator;133JvmtiEnv* next_environment() { return (JvmtiEnv*)_next; }134void set_next_environment(JvmtiEnvBase* env) { _next = env; }135static JvmtiEnv* head_environment() {136JVMTI_ONLY(return (JvmtiEnv*)_head_environment);137NOT_JVMTI(return NULL);138}139140public:141142jvmtiPhase phase();143bool is_valid();144145bool use_version_1_0_semantics(); // agent asked for version 1.0146bool use_version_1_1_semantics(); // agent asked for version 1.1147bool use_version_1_2_semantics(); // agent asked for version 1.2148149bool is_retransformable() { return _is_retransformable; }150151static ByteSize jvmti_external_offset() {152return byte_offset_of(JvmtiEnvBase, _jvmti_external);153};154155static JvmtiEnv* JvmtiEnv_from_jvmti_env(jvmtiEnv *env) {156return (JvmtiEnv*)((intptr_t)env - in_bytes(jvmti_external_offset()));157};158159jvmtiCapabilities *get_capabilities() { return &_current_capabilities; }160161jvmtiCapabilities *get_prohibited_capabilities() { return &_prohibited_capabilities; }162163bool early_class_hook_env() {164return get_capabilities()->can_generate_early_class_hook_events != 0165&& get_capabilities()->can_generate_all_class_hook_events != 0;166}167168bool early_vmstart_env() {169return get_capabilities()->can_generate_early_vmstart != 0;170}171172static char** get_all_native_method_prefixes(int* count_ptr);173174// This test will answer true when all environments have been disposed and some have175// not yet been deallocated. As a result, this test should only be used as an176// optimization for the no environment case.177static bool environments_might_exist() {178return head_environment() != NULL;179}180181static void check_for_periodic_clean_up();182183JvmtiEnvEventEnable *env_event_enable() {184return &_env_event_enable;185}186187jvmtiError allocate(jlong size, unsigned char** mem_ptr) {188if (size < 0) {189return JVMTI_ERROR_ILLEGAL_ARGUMENT;190}191if (size == 0) {192*mem_ptr = NULL;193} else {194*mem_ptr = (unsigned char *)os::malloc((size_t)size, mtInternal);195if (*mem_ptr == NULL) {196return JVMTI_ERROR_OUT_OF_MEMORY;197}198}199return JVMTI_ERROR_NONE;200}201202jvmtiError deallocate(unsigned char* mem) {203if (mem != NULL) {204os::free(mem);205}206return JVMTI_ERROR_NONE;207}208209210// Memory functions211unsigned char* jvmtiMalloc(jlong size); // don't use this - call allocate212213// method to create a local handle214jobject jni_reference(Handle hndl);215216// method to create a local handle.217// This function allows caller to specify which218// threads local handle table to use.219jobject jni_reference(JavaThread *thread, Handle hndl);220221// method to destroy a local handle222void destroy_jni_reference(jobject jobj);223224// method to destroy a local handle.225// This function allows caller to specify which226// threads local handle table to use.227void destroy_jni_reference(JavaThread *thread, jobject jobj);228229jvmtiEnv* jvmti_external() { return &_jvmti_external; };230231// Event Dispatch232233bool has_callback(jvmtiEvent event_type) {234assert(event_type >= JVMTI_MIN_EVENT_TYPE_VAL &&235event_type <= JVMTI_MAX_EVENT_TYPE_VAL, "checking");236return ((void**)&_event_callbacks)[event_type-JVMTI_MIN_EVENT_TYPE_VAL] != NULL;237}238239jvmtiEventCallbacks* callbacks() {240return &_event_callbacks;241}242243jvmtiExtEventCallbacks* ext_callbacks() {244return &_ext_event_callbacks;245}246247void set_tag_map(JvmtiTagMap* tag_map) {248_tag_map = tag_map;249}250251JvmtiTagMap* tag_map() {252return _tag_map;253}254255JvmtiTagMap* tag_map_acquire() {256return Atomic::load_acquire(&_tag_map);257}258259void release_set_tag_map(JvmtiTagMap* tag_map) {260Atomic::release_store(&_tag_map, tag_map);261}262263// return true if event is enabled globally or for any thread264// True only if there is a callback for it.265bool is_enabled(jvmtiEvent event_type) {266return _env_event_enable.is_enabled(event_type);267}268269// Random Utilities270271protected:272// helper methods for creating arrays of global JNI Handles from local Handles273// allocated into environment specific storage274jobject * new_jobjectArray(int length, Handle *handles);275jthread * new_jthreadArray(int length, Handle *handles);276jthreadGroup * new_jthreadGroupArray(int length, Handle *handles);277278// convert to a jni jclass from a non-null Klass*279jclass get_jni_class_non_null(Klass* k);280281jint count_locked_objects(JavaThread *java_thread, Handle hobj);282jvmtiError get_locked_objects_in_frame(JavaThread *calling_thread,283JavaThread* java_thread,284javaVFrame *jvf,285GrowableArray<jvmtiMonitorStackDepthInfo*>* owned_monitors_list,286jint depth);287public:288static vframe* vframeForNoProcess(JavaThread* java_thread, jint depth);289290// get a field descriptor for the specified class and field291static bool get_field_descriptor(Klass* k, jfieldID field, fieldDescriptor* fd);292293// JVMTI API helper functions which are called when target thread is suspended294// or at safepoint / thread local handshake.295jvmtiError get_frame_count(JvmtiThreadState *state, jint *count_ptr);296jvmtiError get_frame_location(JavaThread* java_thread, jint depth,297jmethodID* method_ptr, jlocation* location_ptr);298jvmtiError get_object_monitor_usage(JavaThread *calling_thread,299jobject object, jvmtiMonitorUsage* info_ptr);300jvmtiError get_stack_trace(JavaThread *java_thread,301jint stack_depth, jint max_count,302jvmtiFrameInfo* frame_buffer, jint* count_ptr);303jvmtiError get_current_contended_monitor(JavaThread *calling_thread, JavaThread *java_thread,304jobject *monitor_ptr);305jvmtiError get_owned_monitors(JavaThread *calling_thread, JavaThread* java_thread,306GrowableArray<jvmtiMonitorStackDepthInfo*> *owned_monitors_list);307static jvmtiError check_top_frame(Thread* current_thread, JavaThread* java_thread,308jvalue value, TosState tos, Handle* ret_ob_h);309jvmtiError force_early_return(JavaThread* java_thread, jvalue value, TosState tos);310};311312// This class is the only safe means of iterating through environments.313// Note that this iteratation includes invalid environments pending314// deallocation -- in fact, some uses depend on this behavior.315316class JvmtiEnvIterator : public StackObj {317private:318bool _entry_was_marked;319public:320JvmtiEnvIterator() {321if (Threads::number_of_threads() == 0) {322_entry_was_marked = false; // we are single-threaded, no need323} else {324Thread::current()->entering_jvmti_env_iteration();325_entry_was_marked = true;326}327}328~JvmtiEnvIterator() {329if (_entry_was_marked) {330Thread::current()->leaving_jvmti_env_iteration();331}332}333JvmtiEnv* first() { return JvmtiEnvBase::head_environment(); }334JvmtiEnv* next(JvmtiEnvBase* env) { return env->next_environment(); }335};336337class JvmtiHandshakeClosure : public HandshakeClosure {338protected:339jvmtiError _result;340public:341JvmtiHandshakeClosure(const char* name)342: HandshakeClosure(name),343_result(JVMTI_ERROR_THREAD_NOT_ALIVE) {}344jvmtiError result() { return _result; }345};346347class SetForceEarlyReturn : public JvmtiHandshakeClosure {348private:349JvmtiThreadState* _state;350jvalue _value;351TosState _tos;352public:353SetForceEarlyReturn(JvmtiThreadState* state, jvalue value, TosState tos)354: JvmtiHandshakeClosure("SetForceEarlyReturn"),355_state(state),356_value(value),357_tos(tos) {}358void do_thread(Thread *target) {359doit(target, false /* self */);360}361void doit(Thread *target, bool self);362};363364// HandshakeClosure to update for pop top frame.365class UpdateForPopTopFrameClosure : public JvmtiHandshakeClosure {366private:367JvmtiThreadState* _state;368369public:370UpdateForPopTopFrameClosure(JvmtiThreadState* state)371: JvmtiHandshakeClosure("UpdateForPopTopFrame"),372_state(state) {}373void do_thread(Thread *target) {374doit(target, false /* self */);375}376void doit(Thread *target, bool self);377};378379// HandshakeClosure to set frame pop.380class SetFramePopClosure : public JvmtiHandshakeClosure {381private:382JvmtiEnv *_env;383JvmtiThreadState* _state;384jint _depth;385386public:387SetFramePopClosure(JvmtiEnv *env, JvmtiThreadState* state, jint depth)388: JvmtiHandshakeClosure("SetFramePop"),389_env(env),390_state(state),391_depth(depth) {}392void do_thread(Thread *target) {393doit(target, false /* self */);394}395void doit(Thread *target, bool self);396};397398// HandshakeClosure to get monitor information with stack depth.399class GetOwnedMonitorInfoClosure : public JvmtiHandshakeClosure {400private:401JavaThread* _calling_thread;402JvmtiEnv *_env;403GrowableArray<jvmtiMonitorStackDepthInfo*> *_owned_monitors_list;404405public:406GetOwnedMonitorInfoClosure(JavaThread* calling_thread, JvmtiEnv* env,407GrowableArray<jvmtiMonitorStackDepthInfo*>* owned_monitor_list)408: JvmtiHandshakeClosure("GetOwnedMonitorInfo"),409_calling_thread(calling_thread),410_env(env),411_owned_monitors_list(owned_monitor_list) {}412void do_thread(Thread *target);413};414415416// VM operation to get object monitor usage.417class VM_GetObjectMonitorUsage : public VM_Operation {418private:419JvmtiEnv *_env;420jobject _object;421JavaThread* _calling_thread;422jvmtiMonitorUsage* _info_ptr;423jvmtiError _result;424425public:426VM_GetObjectMonitorUsage(JvmtiEnv *env, JavaThread* calling_thread, jobject object, jvmtiMonitorUsage* info_ptr) {427_env = env;428_object = object;429_calling_thread = calling_thread;430_info_ptr = info_ptr;431}432VMOp_Type type() const { return VMOp_GetObjectMonitorUsage; }433jvmtiError result() { return _result; }434void doit() {435_result = ((JvmtiEnvBase*) _env)->get_object_monitor_usage(_calling_thread, _object, _info_ptr);436}437438};439440// HandshakeClosure to get current contended monitor.441class GetCurrentContendedMonitorClosure : public JvmtiHandshakeClosure {442private:443JavaThread *_calling_thread;444JvmtiEnv *_env;445jobject *_owned_monitor_ptr;446447public:448GetCurrentContendedMonitorClosure(JavaThread* calling_thread, JvmtiEnv *env, jobject *mon_ptr)449: JvmtiHandshakeClosure("GetCurrentContendedMonitor"),450_calling_thread(calling_thread),451_env(env),452_owned_monitor_ptr(mon_ptr) {}453void do_thread(Thread *target);454};455456// HandshakeClosure to get stack trace.457class GetStackTraceClosure : public JvmtiHandshakeClosure {458private:459JvmtiEnv *_env;460jint _start_depth;461jint _max_count;462jvmtiFrameInfo *_frame_buffer;463jint *_count_ptr;464465public:466GetStackTraceClosure(JvmtiEnv *env, jint start_depth, jint max_count,467jvmtiFrameInfo* frame_buffer, jint* count_ptr)468: JvmtiHandshakeClosure("GetStackTrace"),469_env(env),470_start_depth(start_depth),471_max_count(max_count),472_frame_buffer(frame_buffer),473_count_ptr(count_ptr) {}474void do_thread(Thread *target);475};476477// forward declaration478struct StackInfoNode;479480// Get stack trace at safepoint or at direct handshake.481class MultipleStackTracesCollector {482private:483JvmtiEnv *_env;484jint _max_frame_count;485jvmtiStackInfo *_stack_info;486jvmtiError _result;487int _frame_count_total;488struct StackInfoNode *_head;489490JvmtiEnvBase *env() { return (JvmtiEnvBase *)_env; }491jint max_frame_count() { return _max_frame_count; }492struct StackInfoNode *head() { return _head; }493void set_head(StackInfoNode *head) { _head = head; }494495public:496MultipleStackTracesCollector(JvmtiEnv *env, jint max_frame_count)497: _env(env),498_max_frame_count(max_frame_count),499_stack_info(NULL),500_result(JVMTI_ERROR_NONE),501_frame_count_total(0),502_head(NULL) {503}504void set_result(jvmtiError result) { _result = result; }505void fill_frames(jthread jt, JavaThread *thr, oop thread_oop);506void allocate_and_fill_stacks(jint thread_count);507jvmtiStackInfo *stack_info() { return _stack_info; }508jvmtiError result() { return _result; }509};510511512// VM operation to get stack trace at safepoint.513class VM_GetAllStackTraces : public VM_Operation {514private:515JavaThread *_calling_thread;516jint _final_thread_count;517MultipleStackTracesCollector _collector;518519public:520VM_GetAllStackTraces(JvmtiEnv *env, JavaThread *calling_thread,521jint max_frame_count)522: _calling_thread(calling_thread),523_final_thread_count(0),524_collector(env, max_frame_count) {525}526VMOp_Type type() const { return VMOp_GetAllStackTraces; }527void doit();528jint final_thread_count() { return _final_thread_count; }529jvmtiStackInfo *stack_info() { return _collector.stack_info(); }530jvmtiError result() { return _collector.result(); }531};532533// VM operation to get stack trace at safepoint.534class VM_GetThreadListStackTraces : public VM_Operation {535private:536jint _thread_count;537const jthread* _thread_list;538MultipleStackTracesCollector _collector;539540public:541VM_GetThreadListStackTraces(JvmtiEnv *env, jint thread_count, const jthread* thread_list, jint max_frame_count)542: _thread_count(thread_count),543_thread_list(thread_list),544_collector(env, max_frame_count) {545}546VMOp_Type type() const { return VMOp_GetThreadListStackTraces; }547void doit();548jvmtiStackInfo *stack_info() { return _collector.stack_info(); }549jvmtiError result() { return _collector.result(); }550};551552// HandshakeClosure to get single stack trace.553class GetSingleStackTraceClosure : public HandshakeClosure {554private:555JavaThread *_calling_thread;556jthread _jthread;557MultipleStackTracesCollector _collector;558559public:560GetSingleStackTraceClosure(JvmtiEnv *env, JavaThread *calling_thread,561jthread thread, jint max_frame_count)562: HandshakeClosure("GetSingleStackTrace"),563_calling_thread(calling_thread),564_jthread(thread),565_collector(env, max_frame_count) {566}567void do_thread(Thread *target);568jvmtiStackInfo *stack_info() { return _collector.stack_info(); }569jvmtiError result() { return _collector.result(); }570};571572// HandshakeClosure to count stack frames.573class GetFrameCountClosure : public JvmtiHandshakeClosure {574private:575JvmtiEnv *_env;576JvmtiThreadState *_state;577jint *_count_ptr;578579public:580GetFrameCountClosure(JvmtiEnv *env, JvmtiThreadState *state, jint *count_ptr)581: JvmtiHandshakeClosure("GetFrameCount"),582_env(env),583_state(state),584_count_ptr(count_ptr) {}585void do_thread(Thread *target);586};587588// HandshakeClosure to get frame location.589class GetFrameLocationClosure : public JvmtiHandshakeClosure {590private:591JvmtiEnv *_env;592jint _depth;593jmethodID* _method_ptr;594jlocation* _location_ptr;595596public:597GetFrameLocationClosure(JvmtiEnv *env, jint depth,598jmethodID* method_ptr, jlocation* location_ptr)599: JvmtiHandshakeClosure("GetFrameLocation"),600_env(env),601_depth(depth),602_method_ptr(method_ptr),603_location_ptr(location_ptr) {}604void do_thread(Thread *target);605};606607608// ResourceTracker609//610// ResourceTracker works a little like a ResourceMark. All allocates611// using the resource tracker are recorded. If an allocate using the612// resource tracker fails the destructor will free any resources613// that were allocated using the tracker.614// The motive for this class is to avoid messy error recovery code615// in situations where multiple allocations are done in sequence. If616// the second or subsequent allocation fails it avoids any code to617// release memory allocated in the previous calls.618//619// Usage :-620// ResourceTracker rt(env);621// :622// err = rt.allocate(1024, &ptr);623624class ResourceTracker : public StackObj {625private:626JvmtiEnv* _env;627GrowableArray<unsigned char*> *_allocations;628bool _failed;629public:630ResourceTracker(JvmtiEnv* env);631~ResourceTracker();632jvmtiError allocate(jlong size, unsigned char** mem_ptr);633unsigned char* allocate(jlong size);634char* strdup(const char* str);635};636637638// Jvmti monitor closure to collect off stack monitors.639class JvmtiMonitorClosure: public MonitorClosure {640private:641JavaThread *_java_thread;642JavaThread *_calling_thread;643GrowableArray<jvmtiMonitorStackDepthInfo*> *_owned_monitors_list;644jvmtiError _error;645JvmtiEnvBase *_env;646647public:648JvmtiMonitorClosure(JavaThread* thread, JavaThread *calling_thread,649GrowableArray<jvmtiMonitorStackDepthInfo*> *owned_monitors,650JvmtiEnvBase *env) {651_java_thread = thread;652_calling_thread = calling_thread;653_owned_monitors_list = owned_monitors;654_error = JVMTI_ERROR_NONE;655_env = env;656}657void do_monitor(ObjectMonitor* mon);658jvmtiError error() { return _error;}659};660661662// Jvmti module closure to collect all modules loaded to the system.663class JvmtiModuleClosure : public StackObj {664private:665static GrowableArray<OopHandle> *_tbl; // Protected with Module_lock666667static void do_module(ModuleEntry* entry);668public:669jvmtiError get_all_modules(JvmtiEnv* env, jint* module_count_ptr, jobject** modules_ptr);670};671672#endif // SHARE_PRIMS_JVMTIENVBASE_HPP673674675