1 /* 2 * Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #ifndef SHARE_PRIMS_JVMTIENVBASE_HPP 26 #define SHARE_PRIMS_JVMTIENVBASE_HPP 27 28 #include "prims/jvmtiEnvThreadState.hpp" 29 #include "prims/jvmtiEventController.hpp" 30 #include "prims/jvmtiThreadState.hpp" 31 #include "oops/oopHandle.hpp" 32 #include "runtime/atomic.hpp" 33 #include "runtime/fieldDescriptor.hpp" 34 #include "runtime/frame.hpp" 35 #include "runtime/javaThread.hpp" 36 #include "runtime/threads.hpp" 37 #include "runtime/vmOperation.hpp" 38 #include "utilities/growableArray.hpp" 39 #include "utilities/macros.hpp" 40 41 // 42 // Forward Declarations 43 // 44 45 class JvmtiEnv; 46 class JvmtiThreadState; 47 class JvmtiRawMonitor; // for jvmtiEnv.hpp 48 class JvmtiEventControllerPrivate; 49 class JvmtiTagMap; 50 51 52 53 // One JvmtiEnv object is created per jvmti attachment; 54 // done via JNI GetEnv() call. Multiple attachments are 55 // allowed in jvmti. 56 57 class JvmtiEnvBase : public CHeapObj<mtInternal> { 58 59 private: 60 61 #if INCLUDE_JVMTI 62 static JvmtiEnvBase* _head_environment; // head of environment list 63 #endif // INCLUDE_JVMTI 64 65 static bool _globally_initialized; 66 static jvmtiPhase _phase; 67 static volatile int _dying_thread_env_iteration_count; 68 69 public: 70 71 enum { 72 JDK15_JVMTI_VERSION = JVMTI_VERSION_1_0 + 33, /* version: 1.0.33 */ 73 JDK16_JVMTI_VERSION = JVMTI_VERSION_1_1 + 102, /* version: 1.1.102 */ 74 JDK17_JVMTI_VERSION = JVMTI_VERSION_1_2 + 2 /* version: 1.2.2 */ 75 }; 76 77 static jvmtiPhase get_phase() { return _phase; } 78 static jvmtiPhase get_phase(jvmtiEnv* env) { return ((JvmtiEnvBase*)JvmtiEnv_from_jvmti_env(env))->phase(); } 79 static void set_phase(jvmtiPhase phase) { _phase = phase; } 80 static bool is_vm_live() { return _phase == JVMTI_PHASE_LIVE; } 81 82 static void entering_dying_thread_env_iteration() { ++_dying_thread_env_iteration_count; } 83 static void leaving_dying_thread_env_iteration() { --_dying_thread_env_iteration_count; } 84 static bool is_inside_dying_thread_env_iteration(){ return _dying_thread_env_iteration_count > 0; } 85 86 // This function is to support agents loaded into running VM. 87 static bool enable_virtual_threads_notify_jvmti(); 88 89 // This function is used in WhiteBox, only needed to test the function above. 90 // It is unsafe to use this function when virtual threads are executed. 91 static bool disable_virtual_threads_notify_jvmti(); 92 93 static jvmtiError suspend_thread(oop thread_oop, JavaThread* java_thread, bool single_suspend, 94 int* need_safepoint_p); 95 static jvmtiError resume_thread(oop thread_oop, JavaThread* java_thread, bool single_resume); 96 static jvmtiError check_thread_list(jint count, const jthread* list); 97 static bool is_in_thread_list(jint count, const jthread* list, oop jt_oop); 98 99 // check if thread_oop represents a thread carrying a virtual thread 100 static bool is_thread_carrying_vthread(JavaThread* java_thread, oop thread_oop) { 101 return java_thread != nullptr && java_thread->jvmti_vthread() != nullptr 102 && java_thread->jvmti_vthread() != thread_oop 103 && java_thread->threadObj() == thread_oop; 104 } 105 106 private: 107 108 enum { 109 JVMTI_MAGIC = 0x71EE, 110 DISPOSED_MAGIC = 0xDEFC, 111 BAD_MAGIC = 0xDEAD 112 }; 113 114 jvmtiEnv _jvmti_external; 115 jint _magic; 116 jint _version; // version value passed to JNI GetEnv() 117 JvmtiEnvBase* _next; 118 bool _is_retransformable; 119 const void *_env_local_storage; // per env agent allocated data. 120 jvmtiEventCallbacks _event_callbacks; 121 jvmtiExtEventCallbacks _ext_event_callbacks; 122 JvmtiTagMap* volatile _tag_map; 123 JvmtiEnvEventEnable _env_event_enable; 124 jvmtiCapabilities _current_capabilities; 125 jvmtiCapabilities _prohibited_capabilities; 126 volatile bool _class_file_load_hook_ever_enabled; 127 static volatile bool _needs_clean_up; 128 char** _native_method_prefixes; 129 int _native_method_prefix_count; 130 131 protected: 132 JvmtiEnvBase(jint version); 133 ~JvmtiEnvBase(); 134 void dispose(); 135 void env_dispose(); 136 137 void set_env_local_storage(const void* data) { _env_local_storage = data; } 138 139 void record_class_file_load_hook_enabled(); 140 void record_first_time_class_file_load_hook_enabled(); 141 142 char** get_native_method_prefixes() { return _native_method_prefixes; } 143 int get_native_method_prefix_count() { return _native_method_prefix_count; } 144 jvmtiError set_native_method_prefixes(jint prefix_count, char** prefixes); 145 146 private: 147 friend class JvmtiEventControllerPrivate; 148 void initialize(); 149 void set_event_callbacks(const jvmtiEventCallbacks* callbacks, jint size_of_callbacks); 150 static void globally_initialize(); 151 static void periodic_clean_up(); 152 153 friend class JvmtiEnvIterator; 154 JvmtiEnv* next_environment() { return (JvmtiEnv*)_next; } 155 void set_next_environment(JvmtiEnvBase* env) { _next = env; } 156 static JvmtiEnv* head_environment() { 157 JVMTI_ONLY(return (JvmtiEnv*)_head_environment); 158 NOT_JVMTI(return nullptr); 159 } 160 161 public: 162 163 jvmtiPhase phase(); 164 bool is_valid(); 165 166 bool use_version_1_0_semantics(); // agent asked for version 1.0 167 bool use_version_1_1_semantics(); // agent asked for version 1.1 168 bool use_version_1_2_semantics(); // agent asked for version 1.2 169 170 bool is_retransformable() { return _is_retransformable; } 171 172 const void* get_env_local_storage() { return _env_local_storage; } 173 174 static ByteSize jvmti_external_offset() { 175 return byte_offset_of(JvmtiEnvBase, _jvmti_external); 176 }; 177 178 // If (thread == nullptr) then return current thread object. 179 // Otherwise return JNIHandles::resolve_external_guard(thread). 180 static oop current_thread_obj_or_resolve_external_guard(jthread thread); 181 182 // Return true if the thread identified with a pair <jt,thr_obj> is current. 183 // A thread carrying a virtual thread is not treated as current. 184 static bool is_JavaThread_current(JavaThread* jt, oop thr_obj) { 185 JavaThread* current = JavaThread::current(); 186 // jt can be null in case of a virtual thread 187 if (jt == nullptr || jt != current) { 188 return false; 189 } 190 oop cur_obj = current->jvmti_vthread(); 191 192 // cur_obj == nullptr is true for normal platform threads only 193 // otherwise it can be virtual or carrier thread. 194 return cur_obj == nullptr || cur_obj == thr_obj; 195 } 196 197 static jvmtiError get_JavaThread(ThreadsList* tlist, jthread thread, JavaThread** jt_pp) { 198 jvmtiError err = JVMTI_ERROR_NONE; 199 if (thread == nullptr) { 200 *jt_pp = JavaThread::current(); 201 } else { 202 err = JvmtiExport::cv_external_thread_to_JavaThread(tlist, thread, jt_pp, nullptr); 203 } 204 return err; 205 } 206 207 // If there is a virtual thread mounted on the JavaThread* then 208 // return virtual thread oop. Otherwise, return thread oop. 209 static oop get_vthread_or_thread_oop(JavaThread* jt) { 210 oop result = jt->threadObj(); 211 if (jt->jvmti_vthread() != nullptr) { 212 result = jt->jvmti_vthread(); 213 } 214 return result; 215 } 216 217 static jvmtiError get_threadOop_and_JavaThread(ThreadsList* t_list, jthread thread, JavaThread* cur_thread, 218 JavaThread** jt_pp, oop* thread_oop_p); 219 220 // Return true if java thread is a carrier thread with a mounted virtual thread. 221 static bool is_cthread_with_mounted_vthread(JavaThread* jt); 222 static bool is_cthread_with_continuation(JavaThread* jt); 223 224 // Check if VirtualThread or BoundVirtualThread is suspended. 225 static bool is_vthread_suspended(oop vt_oop, JavaThread* jt); 226 227 // Check for JVMTI_ERROR_NOT_SUSPENDED and JVMTI_ERROR_OPAQUE_FRAME errors. 228 // Used in PopFrame and ForceEarlyReturn implementations. 229 static jvmtiError check_non_suspended_or_opaque_frame(JavaThread* jt, oop thr_obj, bool self); 230 231 static JvmtiEnv* JvmtiEnv_from_jvmti_env(jvmtiEnv *env) { 232 return (JvmtiEnv*)((intptr_t)env - in_bytes(jvmti_external_offset())); 233 }; 234 235 jvmtiCapabilities *get_capabilities() { return &_current_capabilities; } 236 237 jvmtiCapabilities *get_prohibited_capabilities() { return &_prohibited_capabilities; } 238 239 bool early_class_hook_env() { 240 return get_capabilities()->can_generate_early_class_hook_events != 0 241 && get_capabilities()->can_generate_all_class_hook_events != 0; 242 } 243 244 bool early_vmstart_env() { 245 return get_capabilities()->can_generate_early_vmstart != 0; 246 } 247 248 static char** get_all_native_method_prefixes(int* count_ptr); 249 250 // This test will answer true when all environments have been disposed and some have 251 // not yet been deallocated. As a result, this test should only be used as an 252 // optimization for the no environment case. 253 static bool environments_might_exist() { 254 return head_environment() != nullptr; 255 } 256 257 static void check_for_periodic_clean_up(); 258 259 JvmtiEnvEventEnable *env_event_enable() { 260 return &_env_event_enable; 261 } 262 263 jvmtiError allocate(jlong size, unsigned char** mem_ptr) { 264 if (size < 0) { 265 return JVMTI_ERROR_ILLEGAL_ARGUMENT; 266 } 267 if (size == 0) { 268 *mem_ptr = nullptr; 269 } else { 270 *mem_ptr = (unsigned char *)os::malloc((size_t)size, mtInternal); 271 if (*mem_ptr == nullptr) { 272 return JVMTI_ERROR_OUT_OF_MEMORY; 273 } 274 } 275 return JVMTI_ERROR_NONE; 276 } 277 278 jvmtiError deallocate(unsigned char* mem) { 279 if (mem != nullptr) { 280 os::free(mem); 281 } 282 return JVMTI_ERROR_NONE; 283 } 284 285 286 // Memory functions 287 unsigned char* jvmtiMalloc(jlong size); // don't use this - call allocate 288 289 // method to create a local handle 290 jobject jni_reference(Handle hndl); 291 292 // method to create a local handle. 293 // This function allows caller to specify which 294 // threads local handle table to use. 295 jobject jni_reference(JavaThread *thread, Handle hndl); 296 297 // method to destroy a local handle 298 void destroy_jni_reference(jobject jobj); 299 300 // method to destroy a local handle. 301 // This function allows caller to specify which 302 // threads local handle table to use. 303 void destroy_jni_reference(JavaThread *thread, jobject jobj); 304 305 jvmtiEnv* jvmti_external() { return &_jvmti_external; }; 306 307 // Event Dispatch 308 309 bool has_callback(jvmtiEvent event_type) { 310 assert(event_type >= JVMTI_MIN_EVENT_TYPE_VAL && 311 event_type <= JVMTI_MAX_EVENT_TYPE_VAL, "checking"); 312 return ((void**)&_event_callbacks)[event_type-JVMTI_MIN_EVENT_TYPE_VAL] != nullptr; 313 } 314 315 jvmtiEventCallbacks* callbacks() { 316 return &_event_callbacks; 317 } 318 319 jvmtiExtEventCallbacks* ext_callbacks() { 320 return &_ext_event_callbacks; 321 } 322 323 void set_tag_map(JvmtiTagMap* tag_map) { 324 _tag_map = tag_map; 325 } 326 327 JvmtiTagMap* tag_map() { 328 return _tag_map; 329 } 330 331 JvmtiTagMap* tag_map_acquire() { 332 return Atomic::load_acquire(&_tag_map); 333 } 334 335 void release_set_tag_map(JvmtiTagMap* tag_map) { 336 Atomic::release_store(&_tag_map, tag_map); 337 } 338 339 // return true if event is enabled globally or for any thread 340 // True only if there is a callback for it. 341 bool is_enabled(jvmtiEvent event_type) { 342 return _env_event_enable.is_enabled(event_type); 343 } 344 345 // Random Utilities 346 347 protected: 348 // helper methods for creating arrays of global JNI Handles from local Handles 349 // allocated into environment specific storage 350 jthread * new_jthreadArray(int length, Handle *handles); 351 jthreadGroup * new_jthreadGroupArray(int length, objArrayHandle groups); 352 353 // convert to a jni jclass from a non-null Klass* 354 jclass get_jni_class_non_null(Klass* k); 355 356 jint count_locked_objects(JavaThread *java_thread, Handle hobj); 357 jvmtiError get_locked_objects_in_frame(JavaThread *calling_thread, 358 JavaThread* java_thread, 359 javaVFrame *jvf, 360 GrowableArray<jvmtiMonitorStackDepthInfo*>* owned_monitors_list, 361 jint depth, oop vthread = nullptr); 362 public: 363 static javaVFrame* jvf_for_thread_and_depth(JavaThread* java_thread, jint depth); 364 365 // get a field descriptor for the specified class and field 366 static bool get_field_descriptor(Klass* k, jfieldID field, fieldDescriptor* fd); 367 368 // check and skip frames hidden in mount/unmount transitions 369 static javaVFrame* check_and_skip_hidden_frames(bool is_in_VTMS_transition, javaVFrame* jvf); 370 static javaVFrame* check_and_skip_hidden_frames(JavaThread* jt, javaVFrame* jvf); 371 static javaVFrame* check_and_skip_hidden_frames(oop vthread, javaVFrame* jvf); 372 373 // check if virtual thread is not terminated (alive) 374 static bool is_vthread_alive(oop vt); 375 376 // return JavaThread if virtual thread is mounted, null otherwise 377 static JavaThread* get_JavaThread_or_null(oop vthread); 378 379 // get virtual thread last java vframe 380 static javaVFrame* get_vthread_jvf(oop vthread); 381 382 // get carrier thread last java vframe 383 static javaVFrame* get_cthread_last_java_vframe(JavaThread* jt, RegisterMap* reg_map); 384 385 // get platform thread state 386 static jint get_thread_state_base(oop thread_oop, JavaThread* jt); 387 static jint get_thread_state(oop thread_oop, JavaThread* jt); 388 389 // get virtual thread state 390 static jint get_vthread_state(oop thread_oop, JavaThread* jt); 391 392 // get platform or virtual thread state 393 static jint get_thread_or_vthread_state(oop thread_oop, JavaThread* jt); 394 395 // enumerates the live threads in the given thread group 396 static jvmtiError get_live_threads(JavaThread* current_thread, Handle group_hdl, jint *count_ptr, Handle **thread_objs_p); 397 398 // enumerates the subgroups in the given thread group 399 static jvmtiError get_subgroups(JavaThread* current_thread, Handle group_hdl, jint *count_ptr, objArrayHandle *group_objs_p); 400 401 // JVMTI API helper functions which are called when target thread is suspended 402 // or at safepoint / thread local handshake. 403 static jint get_frame_count(javaVFrame* jvf); 404 jvmtiError get_frame_count(JavaThread* java_thread, jint *count_ptr); 405 jvmtiError get_frame_count(oop frame_oop, jint *count_ptr); 406 jvmtiError get_frame_location(javaVFrame* jvf, jint depth, 407 jmethodID* method_ptr, jlocation* location_ptr); 408 jvmtiError get_frame_location(JavaThread* java_thread, jint depth, 409 jmethodID* method_ptr, jlocation* location_ptr); 410 jvmtiError get_frame_location(oop vthread_oop, jint depth, 411 jmethodID* method_ptr, jlocation* location_ptr); 412 jvmtiError set_frame_pop(JvmtiThreadState* state, javaVFrame* jvf, jint depth); 413 jvmtiError get_object_monitor_usage(JavaThread* calling_thread, 414 jobject object, jvmtiMonitorUsage* info_ptr); 415 jvmtiError get_stack_trace(javaVFrame* jvf, 416 jint stack_depth, jint max_count, 417 jvmtiFrameInfo* frame_buffer, jint* count_ptr); 418 jvmtiError get_stack_trace(JavaThread* java_thread, 419 jint stack_depth, jint max_count, 420 jvmtiFrameInfo* frame_buffer, jint* count_ptr); 421 jvmtiError get_current_contended_monitor(JavaThread* calling_thread, JavaThread* java_thread, 422 jobject* monitor_ptr, bool is_virtual); 423 jvmtiError get_owned_monitors(JavaThread* calling_thread, JavaThread* java_thread, 424 GrowableArray<jvmtiMonitorStackDepthInfo*> *owned_monitors_list); 425 jvmtiError get_owned_monitors(JavaThread* calling_thread, JavaThread* carrier, javaVFrame* jvf, 426 GrowableArray<jvmtiMonitorStackDepthInfo*> *owned_monitors_list, oop vthread); 427 static jvmtiError check_top_frame(Thread* current_thread, JavaThread* java_thread, 428 jvalue value, TosState tos, Handle* ret_ob_h); 429 jvmtiError force_early_return(jthread thread, jvalue value, TosState tos); 430 }; 431 432 // This class is the only safe means of iterating through environments. 433 // Note that this iteratation includes invalid environments pending 434 // deallocation -- in fact, some uses depend on this behavior. 435 436 class JvmtiEnvIterator : public StackObj { 437 private: 438 bool _entry_was_marked; 439 public: 440 JvmtiEnvIterator() { 441 if (Threads::number_of_threads() == 0) { 442 _entry_was_marked = false; // we are single-threaded, no need 443 } else { 444 Thread::current()->entering_jvmti_env_iteration(); 445 _entry_was_marked = true; 446 } 447 } 448 ~JvmtiEnvIterator() { 449 if (_entry_was_marked) { 450 Thread::current()->leaving_jvmti_env_iteration(); 451 } 452 } 453 JvmtiEnv* first() { return JvmtiEnvBase::head_environment(); } 454 JvmtiEnv* next(JvmtiEnvBase* env) { return env->next_environment(); } 455 }; 456 457 // Used in combination with the JvmtiHandshake class. 458 // It is intended to support both platform and virtual threads. 459 class JvmtiUnitedHandshakeClosure : public HandshakeClosure { 460 protected: 461 jvmtiError _result; 462 // the fields below are set by the JvmtiHandshake::execute 463 JavaThread* _target_jt; 464 bool _is_virtual; 465 bool _self; 466 public: 467 JvmtiUnitedHandshakeClosure(const char* name) 468 : HandshakeClosure(name), 469 _result(JVMTI_ERROR_THREAD_NOT_ALIVE), 470 _target_jt(nullptr), 471 _is_virtual(false), 472 _self(false) {} 473 474 void set_result(jvmtiError err) { _result = err; } 475 void set_target_jt(JavaThread* target_jt) { _target_jt = target_jt; } 476 void set_is_virtual(bool val) { _is_virtual = val; } 477 void set_self(bool val) { _self = val; } 478 jvmtiError result() { return _result; } 479 virtual void do_vthread(Handle target_h) = 0; 480 }; 481 482 // The JvmtiHandshake supports virtual threads. 483 class JvmtiHandshake : public Handshake { 484 public: 485 static void execute(JvmtiUnitedHandshakeClosure* hs_cl, ThreadsListHandle* tlh, 486 JavaThread* target_jt, Handle target_h); 487 static void execute(JvmtiUnitedHandshakeClosure* hs_cl, jthread target); 488 }; 489 490 class SetForceEarlyReturn : public JvmtiUnitedHandshakeClosure { 491 private: 492 JvmtiThreadState* _state; 493 jvalue _value; 494 TosState _tos; 495 public: 496 SetForceEarlyReturn(JvmtiThreadState* state, jvalue value, TosState tos) 497 : JvmtiUnitedHandshakeClosure("SetForceEarlyReturn"), 498 _state(state), 499 _value(value), 500 _tos(tos) {} 501 void doit(Thread *target); 502 void do_thread(Thread *target) { 503 doit(target); 504 } 505 void do_vthread(Handle target_h) { 506 assert(_target_jt != nullptr, "sanity check"); 507 // Use jvmti_vthread() instead of vthread() as target could have temporarily changed 508 // identity to carrier thread (see VirtualThread.switchToCarrierThread). 509 assert(_target_jt->jvmti_vthread() == target_h(), "sanity check"); 510 doit(_target_jt); // mounted virtual thread 511 } 512 }; 513 514 // HandshakeClosure to update for pop top frame. 515 class UpdateForPopTopFrameClosure : public JvmtiUnitedHandshakeClosure { 516 private: 517 JvmtiThreadState* _state; 518 519 public: 520 UpdateForPopTopFrameClosure(JvmtiThreadState* state) 521 : JvmtiUnitedHandshakeClosure("UpdateForPopTopFrame"), 522 _state(state) {} 523 void doit(Thread *target); 524 void do_thread(Thread *target) { 525 doit(target); 526 } 527 void do_vthread(Handle target_h) { 528 assert(_target_jt != nullptr, "sanity check"); 529 // Use jvmti_vthread() instead of vthread() as target could have temporarily changed 530 // identity to carrier thread (see VirtualThread.switchToCarrierThread). 531 assert(_target_jt->jvmti_vthread() == target_h(), "sanity check"); 532 doit(_target_jt); // mounted virtual thread 533 } 534 }; 535 536 // HandshakeClosure to set frame pop. 537 class SetFramePopClosure : public JvmtiUnitedHandshakeClosure { 538 private: 539 JvmtiEnv *_env; 540 JvmtiThreadState* _state; 541 jint _depth; 542 543 public: 544 SetFramePopClosure(JvmtiEnv *env, JvmtiThreadState* state, jint depth) 545 : JvmtiUnitedHandshakeClosure("SetFramePopClosure"), 546 _env(env), 547 _state(state), 548 _depth(depth) {} 549 void do_thread(Thread *target); 550 void do_vthread(Handle target_h); 551 }; 552 553 // HandshakeClosure to get monitor information with stack depth. 554 class GetOwnedMonitorInfoClosure : public JvmtiUnitedHandshakeClosure { 555 private: 556 JvmtiEnv *_env; 557 JavaThread* _calling_thread; 558 GrowableArray<jvmtiMonitorStackDepthInfo*> *_owned_monitors_list; 559 560 public: 561 GetOwnedMonitorInfoClosure(JvmtiEnv* env, 562 JavaThread* calling_thread, 563 GrowableArray<jvmtiMonitorStackDepthInfo*>* owned_monitors_list) 564 : JvmtiUnitedHandshakeClosure("GetOwnedMonitorInfo"), 565 _env(env), 566 _calling_thread(calling_thread), 567 _owned_monitors_list(owned_monitors_list) {} 568 569 void do_thread(Thread *target); 570 void do_vthread(Handle target_h); 571 }; 572 573 // VM operation to get object monitor usage. 574 class VM_GetObjectMonitorUsage : public VM_Operation { 575 private: 576 JvmtiEnv *_env; 577 jobject _object; 578 JavaThread* _calling_thread; 579 jvmtiMonitorUsage* _info_ptr; 580 jvmtiError _result; 581 582 public: 583 VM_GetObjectMonitorUsage(JvmtiEnv *env, JavaThread* calling_thread, jobject object, jvmtiMonitorUsage* info_ptr) { 584 _env = env; 585 _object = object; 586 _calling_thread = calling_thread; 587 _info_ptr = info_ptr; 588 } 589 VMOp_Type type() const { return VMOp_GetObjectMonitorUsage; } 590 jvmtiError result() { return _result; } 591 void doit() { 592 _result = ((JvmtiEnvBase*) _env)->get_object_monitor_usage(_calling_thread, _object, _info_ptr); 593 } 594 595 }; 596 597 // HandshakeClosure to get current contended monitor. It is used for both platform and virtual threads. 598 class GetCurrentContendedMonitorClosure : public JvmtiUnitedHandshakeClosure { 599 private: 600 JvmtiEnv *_env; 601 JavaThread *_calling_thread; 602 jobject *_owned_monitor_ptr; 603 604 public: 605 GetCurrentContendedMonitorClosure(JvmtiEnv *env, 606 JavaThread* calling_thread, 607 jobject *owned_monitor_ptr) 608 : JvmtiUnitedHandshakeClosure("GetCurrentContendedMonitor"), 609 _env(env), 610 _calling_thread(calling_thread), 611 _owned_monitor_ptr(owned_monitor_ptr) {} 612 void do_thread(Thread *target); 613 void do_vthread(Handle target_h); 614 }; 615 616 // HandshakeClosure to get stack trace. 617 class GetStackTraceClosure : public JvmtiUnitedHandshakeClosure { 618 private: 619 JvmtiEnv *_env; 620 jint _start_depth; 621 jint _max_count; 622 jvmtiFrameInfo *_frame_buffer; 623 jint *_count_ptr; 624 625 public: 626 GetStackTraceClosure(JvmtiEnv *env, jint start_depth, jint max_count, 627 jvmtiFrameInfo* frame_buffer, jint* count_ptr) 628 : JvmtiUnitedHandshakeClosure("GetStackTrace"), 629 _env(env), 630 _start_depth(start_depth), 631 _max_count(max_count), 632 _frame_buffer(frame_buffer), 633 _count_ptr(count_ptr) {} 634 void do_thread(Thread *target); 635 void do_vthread(Handle target_h); 636 }; 637 638 #ifdef ASSERT 639 // HandshakeClosure to print stack trace in JvmtiVTMSTransitionDisabler error handling. 640 class PrintStackTraceClosure : public HandshakeClosure { 641 public: 642 static void do_thread_impl(Thread *target); 643 644 PrintStackTraceClosure() 645 : HandshakeClosure("PrintStackTraceClosure") {} 646 void do_thread(Thread *target); 647 }; 648 #endif 649 650 // Forward declaration. 651 struct StackInfoNode; 652 653 // Get stack trace at safepoint or at direct handshake. 654 class MultipleStackTracesCollector { 655 private: 656 friend class VM_GetThreadListStackTraces; 657 JvmtiEnv *_env; 658 jint _max_frame_count; 659 jvmtiStackInfo *_stack_info; 660 jvmtiError _result; 661 int _frame_count_total; 662 struct StackInfoNode *_head; 663 664 JvmtiEnvBase *env() { return (JvmtiEnvBase *)_env; } 665 jint max_frame_count() { return _max_frame_count; } 666 struct StackInfoNode *head() { return _head; } 667 void set_head(StackInfoNode *head) { _head = head; } 668 669 public: 670 MultipleStackTracesCollector(JvmtiEnv *env, jint max_frame_count) 671 : _env(env), 672 _max_frame_count(max_frame_count), 673 _stack_info(nullptr), 674 _result(JVMTI_ERROR_NONE), 675 _frame_count_total(0), 676 _head(nullptr) { 677 } 678 void set_result(jvmtiError result) { _result = result; } 679 void fill_frames(jthread jt, JavaThread *thr, oop thread_oop); 680 void allocate_and_fill_stacks(jint thread_count); 681 jvmtiStackInfo *stack_info() { return _stack_info; } 682 jvmtiError result() { return _result; } 683 }; 684 685 686 // VM operation to get stack trace at safepoint. 687 class VM_GetAllStackTraces : public VM_Operation { 688 private: 689 JavaThread *_calling_thread; 690 jint _final_thread_count; 691 MultipleStackTracesCollector _collector; 692 693 public: 694 VM_GetAllStackTraces(JvmtiEnv *env, JavaThread *calling_thread, 695 jint max_frame_count) 696 : _calling_thread(calling_thread), 697 _final_thread_count(0), 698 _collector(env, max_frame_count) { 699 } 700 VMOp_Type type() const { return VMOp_GetAllStackTraces; } 701 void doit(); 702 jint final_thread_count() { return _final_thread_count; } 703 jvmtiStackInfo *stack_info() { return _collector.stack_info(); } 704 jvmtiError result() { return _collector.result(); } 705 }; 706 707 // VM operation to get stack trace at safepoint. 708 class VM_GetThreadListStackTraces : public VM_Operation { 709 private: 710 jint _thread_count; 711 const jthread* _thread_list; 712 MultipleStackTracesCollector _collector; 713 714 JvmtiEnvBase *env() { return _collector.env(); } 715 716 public: 717 VM_GetThreadListStackTraces(JvmtiEnv *env, jint thread_count, const jthread* thread_list, jint max_frame_count) 718 : _thread_count(thread_count), 719 _thread_list(thread_list), 720 _collector(env, max_frame_count) { 721 } 722 VMOp_Type type() const { return VMOp_GetThreadListStackTraces; } 723 void doit(); 724 jvmtiStackInfo *stack_info() { return _collector.stack_info(); } 725 jvmtiError result() { return _collector.result(); } 726 }; 727 728 // HandshakeClosure to get single stack trace. 729 class GetSingleStackTraceClosure : public JvmtiUnitedHandshakeClosure { 730 private: 731 JavaThread *_calling_thread; 732 jthread _jthread; 733 MultipleStackTracesCollector _collector; 734 735 public: 736 GetSingleStackTraceClosure(JvmtiEnv *env, JavaThread *calling_thread, 737 jthread thread, jint max_frame_count) 738 : JvmtiUnitedHandshakeClosure("GetSingleStackTrace"), 739 _calling_thread(calling_thread), 740 _jthread(thread), 741 _collector(env, max_frame_count) { 742 } 743 void do_thread(Thread *target); 744 void do_vthread(Handle target_h); 745 void doit(); 746 jvmtiStackInfo *stack_info() { return _collector.stack_info(); } 747 jvmtiError result() { return _result; } 748 }; 749 750 // HandshakeClosure to count stack frames. 751 class GetFrameCountClosure : public JvmtiUnitedHandshakeClosure { 752 private: 753 JvmtiEnv *_env; 754 jint *_count_ptr; 755 756 public: 757 GetFrameCountClosure(JvmtiEnv *env, jint *count_ptr) 758 : JvmtiUnitedHandshakeClosure("GetFrameCount"), 759 _env(env), 760 _count_ptr(count_ptr) {} 761 void do_thread(Thread *target); 762 void do_vthread(Handle target_h); 763 }; 764 765 // HandshakeClosure to get frame location. 766 class GetFrameLocationClosure : public JvmtiUnitedHandshakeClosure { 767 private: 768 JvmtiEnv *_env; 769 jint _depth; 770 jmethodID* _method_ptr; 771 jlocation* _location_ptr; 772 773 public: 774 GetFrameLocationClosure(JvmtiEnv *env, jint depth, 775 jmethodID* method_ptr, jlocation* location_ptr) 776 : JvmtiUnitedHandshakeClosure("GetFrameLocation"), 777 _env(env), 778 _depth(depth), 779 _method_ptr(method_ptr), 780 _location_ptr(location_ptr) {} 781 void do_thread(Thread *target); 782 void do_vthread(Handle target_h); 783 }; 784 785 // ResourceTracker 786 // 787 // ResourceTracker works a little like a ResourceMark. All allocates 788 // using the resource tracker are recorded. If an allocate using the 789 // resource tracker fails the destructor will free any resources 790 // that were allocated using the tracker. 791 // The motive for this class is to avoid messy error recovery code 792 // in situations where multiple allocations are done in sequence. If 793 // the second or subsequent allocation fails it avoids any code to 794 // release memory allocated in the previous calls. 795 // 796 // Usage :- 797 // ResourceTracker rt(env); 798 // : 799 // err = rt.allocate(1024, &ptr); 800 801 class ResourceTracker : public StackObj { 802 private: 803 JvmtiEnv* _env; 804 GrowableArray<unsigned char*> *_allocations; 805 bool _failed; 806 public: 807 ResourceTracker(JvmtiEnv* env); 808 ~ResourceTracker(); 809 jvmtiError allocate(jlong size, unsigned char** mem_ptr); 810 unsigned char* allocate(jlong size); 811 char* strdup(const char* str); 812 }; 813 814 815 // Jvmti monitor closure to collect off stack monitors. 816 class JvmtiMonitorClosure: public MonitorClosure { 817 private: 818 JavaThread *_calling_thread; 819 GrowableArray<jvmtiMonitorStackDepthInfo*> *_owned_monitors_list; 820 jvmtiError _error; 821 JvmtiEnvBase *_env; 822 823 public: 824 JvmtiMonitorClosure(JavaThread *calling_thread, 825 GrowableArray<jvmtiMonitorStackDepthInfo*> *owned_monitors, 826 JvmtiEnvBase *env) { 827 _calling_thread = calling_thread; 828 _owned_monitors_list = owned_monitors; 829 _error = JVMTI_ERROR_NONE; 830 _env = env; 831 } 832 void do_monitor(ObjectMonitor* mon); 833 jvmtiError error() { return _error;} 834 }; 835 836 837 // Jvmti module closure to collect all modules loaded to the system. 838 class JvmtiModuleClosure : public StackObj { 839 private: 840 static GrowableArray<OopHandle> *_tbl; // Protected with Module_lock 841 842 static void do_module(ModuleEntry* entry); 843 public: 844 jvmtiError get_all_modules(JvmtiEnv* env, jint* module_count_ptr, jobject** modules_ptr); 845 }; 846 847 #endif // SHARE_PRIMS_JVMTIENVBASE_HPP