1 /*
   2  * Copyright (c) 2003, 2019, 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 "classfile/classLoader.hpp"
  29 #include "prims/jvmtiEnvThreadState.hpp"
  30 #include "prims/jvmtiEventController.hpp"
  31 #include "prims/jvmtiThreadState.hpp"
  32 #include "oops/oopHandle.hpp"
  33 #include "runtime/fieldDescriptor.hpp"
  34 #include "runtime/frame.hpp"
  35 #include "runtime/orderAccess.hpp"
  36 #include "runtime/thread.hpp"
  37 #include "runtime/vmOperations.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  private:
  87 
  88   enum {
  89       JVMTI_MAGIC    = 0x71EE,
  90       DISPOSED_MAGIC = 0xDEFC,
  91       BAD_MAGIC      = 0xDEAD
  92   };
  93 
  94   jvmtiEnv _jvmti_external;
  95   jint _magic;
  96   jint _version;  // version value passed to JNI GetEnv()
  97   JvmtiEnvBase* _next;
  98   bool _is_retransformable;
  99   const void *_env_local_storage;     // per env agent allocated data.
 100   jvmtiEventCallbacks _event_callbacks;
 101   jvmtiExtEventCallbacks _ext_event_callbacks;
 102   JvmtiTagMap* volatile _tag_map;
 103   JvmtiEnvEventEnable _env_event_enable;
 104   jvmtiCapabilities _current_capabilities;
 105   jvmtiCapabilities _prohibited_capabilities;
 106   volatile bool _class_file_load_hook_ever_enabled;
 107   static volatile bool _needs_clean_up;
 108   char** _native_method_prefixes;
 109   int    _native_method_prefix_count;
 110 
 111  protected:
 112   JvmtiEnvBase(jint version);
 113   ~JvmtiEnvBase();
 114   void dispose();
 115   void env_dispose();
 116 
 117   void set_env_local_storage(const void* data)     { _env_local_storage = data; }
 118   const void* get_env_local_storage()              { return _env_local_storage; }
 119 
 120   void record_class_file_load_hook_enabled();
 121   void record_first_time_class_file_load_hook_enabled();
 122 
 123   char** get_native_method_prefixes()              { return _native_method_prefixes; }
 124   int    get_native_method_prefix_count()          { return _native_method_prefix_count; }
 125   jvmtiError set_native_method_prefixes(jint prefix_count, char** prefixes);
 126 
 127  private:
 128   friend class JvmtiEventControllerPrivate;
 129   void initialize();
 130   void set_event_callbacks(const jvmtiEventCallbacks* callbacks, jint size_of_callbacks);
 131   static void globally_initialize();
 132   static void periodic_clean_up();
 133 
 134   friend class JvmtiEnvIterator;
 135   JvmtiEnv* next_environment()                     { return (JvmtiEnv*)_next; }
 136   void set_next_environment(JvmtiEnvBase* env)     { _next = env; }
 137   static JvmtiEnv* head_environment()              {
 138     JVMTI_ONLY(return (JvmtiEnv*)_head_environment);
 139     NOT_JVMTI(return NULL);
 140   }
 141 
 142  public:
 143 
 144   jvmtiPhase  phase();
 145   bool is_valid();
 146 
 147   bool use_version_1_0_semantics();  // agent asked for version 1.0
 148   bool use_version_1_1_semantics();  // agent asked for version 1.1
 149   bool use_version_1_2_semantics();  // agent asked for version 1.2
 150 
 151   bool is_retransformable()                        { return _is_retransformable; }
 152 
 153   static ByteSize jvmti_external_offset() {
 154     return byte_offset_of(JvmtiEnvBase, _jvmti_external);
 155   };
 156 
 157   static JvmtiEnv* JvmtiEnv_from_jvmti_env(jvmtiEnv *env) {
 158     return (JvmtiEnv*)((intptr_t)env - in_bytes(jvmti_external_offset()));
 159   };
 160 
 161   jvmtiCapabilities *get_capabilities()             { return &_current_capabilities; }
 162 
 163   jvmtiCapabilities *get_prohibited_capabilities()  { return &_prohibited_capabilities; }
 164 
 165   bool early_class_hook_env() {
 166     return get_capabilities()->can_generate_early_class_hook_events != 0
 167         && get_capabilities()->can_generate_all_class_hook_events != 0;
 168   }
 169 
 170   bool early_vmstart_env() {
 171     return get_capabilities()->can_generate_early_vmstart != 0;
 172   }
 173 
 174   static char** get_all_native_method_prefixes(int* count_ptr);
 175 
 176   // This test will answer true when all environments have been disposed and some have
 177   // not yet been deallocated.  As a result, this test should only be used as an
 178   // optimization for the no environment case.
 179   static bool environments_might_exist() {
 180     return head_environment() != NULL;
 181   }
 182 
 183   static void check_for_periodic_clean_up();
 184 
 185   JvmtiEnvEventEnable *env_event_enable() {
 186     return &_env_event_enable;
 187   }
 188 
 189   jvmtiError allocate(jlong size, unsigned char** mem_ptr) {
 190     if (size < 0) {
 191       return JVMTI_ERROR_ILLEGAL_ARGUMENT;
 192     }
 193     if (size == 0) {
 194       *mem_ptr = NULL;
 195     } else {
 196       *mem_ptr = (unsigned char *)os::malloc((size_t)size, mtInternal);
 197       if (*mem_ptr == NULL) {
 198         return JVMTI_ERROR_OUT_OF_MEMORY;
 199       }
 200     }
 201     return JVMTI_ERROR_NONE;
 202   }
 203 
 204   jvmtiError deallocate(unsigned char* mem) {
 205     if (mem != NULL) {
 206       os::free(mem);
 207     }
 208     return JVMTI_ERROR_NONE;
 209   }
 210 
 211 
 212   // Memory functions
 213   unsigned char* jvmtiMalloc(jlong size);  // don't use this - call allocate
 214 
 215   // method to create a local handle
 216   jobject jni_reference(Handle hndl);
 217 
 218   // method to create a local handle.
 219   // This function allows caller to specify which
 220   // threads local handle table to use.
 221   jobject jni_reference(JavaThread *thread, Handle hndl);
 222 
 223   // method to destroy a local handle
 224   void destroy_jni_reference(jobject jobj);
 225 
 226   // method to destroy a local handle.
 227   // This function allows caller to specify which
 228   // threads local handle table to use.
 229   void destroy_jni_reference(JavaThread *thread, jobject jobj);
 230 
 231   jvmtiEnv* jvmti_external() { return &_jvmti_external; };
 232 
 233 // Event Dispatch
 234 
 235   bool has_callback(jvmtiEvent event_type) {
 236     assert(event_type >= JVMTI_MIN_EVENT_TYPE_VAL &&
 237            event_type <= JVMTI_MAX_EVENT_TYPE_VAL, "checking");
 238     return ((void**)&_event_callbacks)[event_type-JVMTI_MIN_EVENT_TYPE_VAL] != NULL;
 239   }
 240 
 241   jvmtiEventCallbacks* callbacks() {
 242     return &_event_callbacks;
 243   }
 244 
 245   jvmtiExtEventCallbacks* ext_callbacks() {
 246     return &_ext_event_callbacks;
 247   }
 248 
 249   void set_tag_map(JvmtiTagMap* tag_map) {
 250     _tag_map = tag_map;
 251   }
 252 
 253   JvmtiTagMap* tag_map() {
 254     return _tag_map;
 255   }
 256 
 257   JvmtiTagMap* tag_map_acquire() {
 258     return OrderAccess::load_acquire(&_tag_map);
 259   }
 260 
 261   void release_set_tag_map(JvmtiTagMap* tag_map) {
 262     OrderAccess::release_store(&_tag_map, tag_map);
 263   }
 264 
 265   // return true if event is enabled globally or for any thread
 266   // True only if there is a callback for it.
 267   bool is_enabled(jvmtiEvent event_type) {
 268     return _env_event_enable.is_enabled(event_type);
 269   }
 270 
 271 // Random Utilities
 272 
 273  protected:
 274   // helper methods for creating arrays of global JNI Handles from local Handles
 275   // allocated into environment specific storage
 276   jobject * new_jobjectArray(int length, Handle *handles);
 277   jthread * new_jthreadArray(int length, Handle *handles);
 278   jthreadGroup * new_jthreadGroupArray(int length, Handle *handles);
 279 
 280   // convert to a jni jclass from a non-null Klass*
 281   jclass get_jni_class_non_null(Klass* k);
 282 
 283   jint count_locked_objects(JavaThread *java_thread, Handle hobj);
 284   jvmtiError get_locked_objects_in_frame(JavaThread *calling_thread,
 285                                    JavaThread* java_thread,
 286                                    javaVFrame *jvf,
 287                                    GrowableArray<jvmtiMonitorStackDepthInfo*>* owned_monitors_list,
 288                                    jint depth);
 289   vframe* vframeFor(JavaThread* java_thread, jint depth);
 290 
 291  public:
 292   // get a field descriptor for the specified class and field
 293   static bool get_field_descriptor(Klass* k, jfieldID field, fieldDescriptor* fd);
 294 
 295   // JVMTI API helper functions which are called at safepoint or thread is suspended.
 296   jvmtiError get_frame_count(JvmtiThreadState *state, jint *count_ptr);
 297   jvmtiError get_frame_location(JavaThread* java_thread, jint depth,
 298                                               jmethodID* method_ptr, jlocation* location_ptr);
 299   jvmtiError get_object_monitor_usage(JavaThread *calling_thread,
 300                                                     jobject object, jvmtiMonitorUsage* info_ptr);
 301   jvmtiError get_stack_trace(javaVFrame *jvf,
 302                              jint stack_depth, jint max_count,
 303                              jvmtiFrameInfo* frame_buffer, jint* count_ptr);
 304   jvmtiError get_stack_trace(JavaThread *java_thread,
 305                                            jint stack_depth, jint max_count,
 306                                            jvmtiFrameInfo* frame_buffer, jint* count_ptr);
 307   jvmtiError get_current_contended_monitor(JavaThread *calling_thread,
 308                                                          JavaThread *java_thread,
 309                                                          jobject *monitor_ptr);
 310   jvmtiError get_owned_monitors(JavaThread *calling_thread, JavaThread* java_thread,
 311                           GrowableArray<jvmtiMonitorStackDepthInfo*> *owned_monitors_list);
 312   jvmtiError check_top_frame(JavaThread* current_thread, JavaThread* java_thread,
 313                              jvalue value, TosState tos, Handle* ret_ob_h);
 314   jvmtiError force_early_return(JavaThread* java_thread, jvalue value, TosState tos);
 315 };
 316 
 317 // This class is the only safe means of iterating through environments.
 318 // Note that this iteratation includes invalid environments pending
 319 // deallocation -- in fact, some uses depend on this behavior.
 320 
 321 class JvmtiEnvIterator : public StackObj {
 322  private:
 323   bool _entry_was_marked;
 324  public:
 325   JvmtiEnvIterator() {
 326     if (Threads::number_of_threads() == 0) {
 327       _entry_was_marked = false; // we are single-threaded, no need
 328     } else {
 329       Thread::current()->entering_jvmti_env_iteration();
 330       _entry_was_marked = true;
 331     }
 332   }
 333   ~JvmtiEnvIterator() {
 334     if (_entry_was_marked) {
 335       Thread::current()->leaving_jvmti_env_iteration();
 336     }
 337   }
 338   JvmtiEnv* first()                 { return JvmtiEnvBase::head_environment(); }
 339   JvmtiEnv* next(JvmtiEnvBase* env) { return env->next_environment(); }
 340 };
 341 
 342 // VM operation to update for pop top frame.
 343 class VM_UpdateForPopTopFrame : public VM_Operation {
 344 private:
 345   JvmtiThreadState* _state;
 346   jvmtiError _result;
 347 
 348 public:
 349   VM_UpdateForPopTopFrame(JvmtiThreadState* state) {
 350     _state = state;
 351     _result = JVMTI_ERROR_NONE;
 352   }
 353   VMOp_Type type() const { return VMOp_UpdateForPopTopFrame; }
 354   jvmtiError result() { return _result; }
 355   void doit();
 356 };
 357 
 358 // VM operation to set frame pop.
 359 class VM_SetFramePop : public VM_Operation {
 360 private:
 361   JvmtiEnv *_env;
 362   JvmtiThreadState* _state;
 363   jint _depth;
 364   jvmtiError _result;
 365 
 366 public:
 367   VM_SetFramePop(JvmtiEnv *env, JvmtiThreadState* state, jint depth) {
 368     _env = env;
 369     _state = state;
 370     _depth = depth;
 371     _result = JVMTI_ERROR_NONE;
 372   }
 373   // Nested operation must be allowed for the VM_EnterInterpOnlyMode that is
 374   // called from the JvmtiEventControllerPrivate::recompute_thread_enabled.
 375   bool allow_nested_vm_operations() const { return true; }
 376   VMOp_Type type() const { return VMOp_SetFramePop; }
 377   jvmtiError result() { return _result; }
 378   void doit();
 379 };
 380 
 381 
 382 // VM operation to get monitor information with stack depth.
 383 class VM_GetOwnedMonitorInfo : public VM_Operation {
 384 private:
 385   JvmtiEnv *_env;
 386   JavaThread* _calling_thread;
 387   JavaThread *_java_thread;
 388   jvmtiError _result;
 389   GrowableArray<jvmtiMonitorStackDepthInfo*> *_owned_monitors_list;
 390 
 391 public:
 392   VM_GetOwnedMonitorInfo(JvmtiEnv* env, JavaThread* calling_thread,
 393                                    JavaThread* java_thread,
 394                                    GrowableArray<jvmtiMonitorStackDepthInfo*>* owned_monitor_list) {
 395     _env = env;
 396     _calling_thread = calling_thread;
 397     _java_thread = java_thread;
 398     _owned_monitors_list = owned_monitor_list;
 399     _result = JVMTI_ERROR_NONE;
 400   }
 401   VMOp_Type type() const { return VMOp_GetOwnedMonitorInfo; }
 402   void doit();
 403   jvmtiError result() { return _result; }
 404 };
 405 
 406 
 407 // VM operation to get object monitor usage.
 408 class VM_GetObjectMonitorUsage : public VM_Operation {
 409 private:
 410   JvmtiEnv *_env;
 411   jobject _object;
 412   JavaThread* _calling_thread;
 413   jvmtiMonitorUsage* _info_ptr;
 414   jvmtiError _result;
 415 
 416 public:
 417   VM_GetObjectMonitorUsage(JvmtiEnv *env, JavaThread* calling_thread, jobject object, jvmtiMonitorUsage* info_ptr) {
 418     _env = env;
 419     _object = object;
 420     _calling_thread = calling_thread;
 421     _info_ptr = info_ptr;
 422   }
 423   VMOp_Type type() const { return VMOp_GetObjectMonitorUsage; }
 424   jvmtiError result() { return _result; }
 425   void doit() {
 426     _result = ((JvmtiEnvBase*) _env)->get_object_monitor_usage(_calling_thread, _object, _info_ptr);
 427   }
 428 
 429 };
 430 
 431 // VM operation to get current contended monitor.
 432 class VM_GetCurrentContendedMonitor : public VM_Operation {
 433 private:
 434   JvmtiEnv *_env;
 435   JavaThread *_calling_thread;
 436   JavaThread *_java_thread;
 437   jobject *_owned_monitor_ptr;
 438   jvmtiError _result;
 439 
 440 public:
 441   VM_GetCurrentContendedMonitor(JvmtiEnv *env, JavaThread *calling_thread, JavaThread *java_thread, jobject *mon_ptr) {
 442     _env = env;
 443     _calling_thread = calling_thread;
 444     _java_thread = java_thread;
 445     _owned_monitor_ptr = mon_ptr;
 446   }
 447   VMOp_Type type() const { return VMOp_GetCurrentContendedMonitor; }
 448   jvmtiError result() { return _result; }
 449   void doit();
 450 };
 451 
 452 // VM operation to get stack trace at safepoint.
 453 class VM_GetStackTrace : public VM_Operation {
 454 private:
 455   JvmtiEnv *_env;
 456   JavaThread *_java_thread;
 457   jint _start_depth;
 458   jint _max_count;
 459   jvmtiFrameInfo *_frame_buffer;
 460   jint *_count_ptr;
 461   jvmtiError _result;
 462 
 463 public:
 464   VM_GetStackTrace(JvmtiEnv *env, JavaThread *java_thread,
 465                    jint start_depth, jint max_count,
 466                    jvmtiFrameInfo* frame_buffer, jint* count_ptr) {
 467     _env = env;
 468     _java_thread = java_thread;
 469     _start_depth = start_depth;
 470     _max_count = max_count;
 471     _frame_buffer = frame_buffer;
 472     _count_ptr = count_ptr;
 473   }
 474   jvmtiError result() { return _result; }
 475   VMOp_Type type() const { return VMOp_GetStackTrace; }
 476   void doit();
 477 };
 478 
 479 // forward declaration
 480 struct StackInfoNode;
 481 
 482 // VM operation to get stack trace at safepoint.
 483 class VM_GetMultipleStackTraces : public VM_Operation {
 484 private:
 485   JvmtiEnv *_env;
 486   jint _max_frame_count;
 487   jvmtiStackInfo *_stack_info;
 488   jvmtiError _result;
 489   int _frame_count_total;
 490   struct StackInfoNode *_head;
 491 
 492   JvmtiEnvBase *env()                 { return (JvmtiEnvBase *)_env; }
 493   jint max_frame_count()              { return _max_frame_count; }
 494   struct StackInfoNode *head()        { return _head; }
 495   void set_head(StackInfoNode *head)  { _head = head; }
 496 
 497 protected:
 498   void set_result(jvmtiError result)  { _result = result; }
 499   void fill_frames(jthread jt, JavaThread *thr, oop thread_oop);
 500   void allocate_and_fill_stacks(jint thread_count);
 501 
 502 public:
 503   VM_GetMultipleStackTraces(JvmtiEnv *env, jint max_frame_count) {
 504     _env = env;
 505     _max_frame_count = max_frame_count;
 506     _frame_count_total = 0;
 507     _head = NULL;
 508     _result = JVMTI_ERROR_NONE;
 509   }
 510   VMOp_Type type() const             { return VMOp_GetMultipleStackTraces; }
 511   jvmtiStackInfo *stack_info()       { return _stack_info; }
 512   jvmtiError result()                { return _result; }
 513 };
 514 
 515 
 516 // VM operation to get stack trace at safepoint.
 517 class VM_GetAllStackTraces : public VM_GetMultipleStackTraces {
 518 private:
 519   JavaThread *_calling_thread;
 520   jint _final_thread_count;
 521 
 522 public:
 523   VM_GetAllStackTraces(JvmtiEnv *env, JavaThread *calling_thread,
 524                        jint max_frame_count)
 525       : VM_GetMultipleStackTraces(env, max_frame_count) {
 526     _calling_thread = calling_thread;
 527   }
 528   VMOp_Type type() const          { return VMOp_GetAllStackTraces; }
 529   void doit();
 530   jint final_thread_count()       { return _final_thread_count; }
 531 };
 532 
 533 // VM operation to get stack trace at safepoint.
 534 class VM_GetThreadListStackTraces : public VM_GetMultipleStackTraces {
 535 private:
 536   jint _thread_count;
 537   const jthread* _thread_list;
 538 
 539 public:
 540   VM_GetThreadListStackTraces(JvmtiEnv *env, jint thread_count, const jthread* thread_list, jint max_frame_count)
 541       : VM_GetMultipleStackTraces(env, max_frame_count) {
 542     _thread_count = thread_count;
 543     _thread_list = thread_list;
 544   }
 545   VMOp_Type type() const { return VMOp_GetThreadListStackTraces; }
 546   void doit();
 547 };
 548 
 549 
 550 // VM operation to count stack frames at safepoint.
 551 class VM_GetFrameCount : public VM_Operation {
 552 private:
 553   JvmtiEnv *_env;
 554   JvmtiThreadState *_state;
 555   jint *_count_ptr;
 556   jvmtiError _result;
 557 
 558 public:
 559   VM_GetFrameCount(JvmtiEnv *env, JvmtiThreadState *state, jint *count_ptr) {
 560     _env = env;
 561     _state = state;
 562     _count_ptr = count_ptr;
 563   }
 564   VMOp_Type type() const { return VMOp_GetFrameCount; }
 565   jvmtiError result()    { return _result; }
 566   void doit();
 567 };
 568 
 569 // VM operation get to frame location at safepoint.
 570 class VM_GetFrameLocation : public VM_Operation {
 571 private:
 572   JvmtiEnv *_env;
 573   JavaThread* _java_thread;
 574   jint _depth;
 575   jmethodID* _method_ptr;
 576   jlocation* _location_ptr;
 577   jvmtiError _result;
 578 
 579 public:
 580   VM_GetFrameLocation(JvmtiEnv *env, JavaThread* java_thread, jint depth,
 581                       jmethodID* method_ptr, jlocation* location_ptr) {
 582     _env = env;
 583     _java_thread = java_thread;
 584     _depth = depth;
 585     _method_ptr = method_ptr;
 586     _location_ptr = location_ptr;
 587   }
 588   VMOp_Type type() const { return VMOp_GetFrameLocation; }
 589   jvmtiError result()    { return _result; }
 590   void doit();
 591 };
 592 
 593 // VM operation get to get fiber thread at safepoint.
 594 class VM_GetFiberThread : public VM_Operation {
 595 private:
 596   JavaThread* _current_thread;
 597   Handle _fiber_h;
 598   jthread* _carrier_thread_ptr;
 599   jvmtiError _result;
 600 
 601 public:
 602   VM_GetFiberThread(JavaThread* current_thread, Handle fiber_h, jthread* carrier_thread_ptr) {
 603     _current_thread = current_thread;
 604     _fiber_h = fiber_h;
 605     _carrier_thread_ptr = carrier_thread_ptr;
 606     _result = JVMTI_ERROR_NONE;
 607   }
 608   VMOp_Type type() const { return VMOp_GetFiberThread; }
 609   jvmtiError result()    { return _result; }
 610   void doit();
 611 };
 612 
 613 // VM operation to get fiber stack trace at safepoint.
 614 class VM_GetFiberStackTrace : public VM_Operation {
 615 private:
 616   JvmtiEnv *_env;
 617   Handle _fiber_h;
 618   jint _start_depth;
 619   jint _max_count;
 620   jvmtiFrameInfo *_frame_buffer;
 621   jint *_count_ptr;
 622   jvmtiError _result;
 623 
 624 public:
 625   VM_GetFiberStackTrace(JvmtiEnv *env, Handle fiber_h,
 626                         jint start_depth, jint max_count,
 627                         jvmtiFrameInfo* frame_buffer, jint* count_ptr) {
 628     _env = env;
 629     _fiber_h = fiber_h;
 630     _start_depth = start_depth;
 631     _max_count = max_count;
 632     _frame_buffer = frame_buffer;
 633     _count_ptr = count_ptr;
 634     _result = JVMTI_ERROR_NONE;
 635   }
 636   jvmtiError result() { return _result; }
 637   VMOp_Type type() const { return VMOp_GetFiberStackTrace; }
 638   void doit();
 639 };
 640 
 641 // VM operation to count fiber stack frames at safepoint.
 642 class VM_GetFiberFrameCount : public VM_Operation {
 643 private:
 644   JvmtiEnv *_env;
 645   Handle _fiber_h;
 646   jint *_count_ptr;
 647   jvmtiError _result;
 648 
 649 public:
 650   VM_GetFiberFrameCount(JvmtiEnv *env, Handle fiber_h, jint *count_ptr) {
 651     _env = env;
 652     _fiber_h = fiber_h;
 653     _count_ptr = count_ptr;
 654     _result = JVMTI_ERROR_NONE;
 655   }
 656   VMOp_Type type() const { return VMOp_GetFiberFrameCount; }
 657   jvmtiError result()    { return _result; }
 658   void doit();
 659 };
 660 
 661 // VM operation get to fiber frame location at safepoint.
 662 class VM_GetFiberFrameLocation : public VM_Operation {
 663 private:
 664   JvmtiEnv *_env;
 665   Handle _fiber_h;
 666   jint _depth;
 667   jmethodID* _method_ptr;
 668   jlocation* _location_ptr;
 669   jvmtiError _result;
 670 
 671 public:
 672   VM_GetFiberFrameLocation(JvmtiEnv *env, Handle fiber_h, jint depth,
 673                            jmethodID* method_ptr, jlocation* location_ptr) {
 674     _env = env;
 675     _fiber_h = fiber_h;
 676     _depth = depth;
 677     _method_ptr = method_ptr;
 678     _location_ptr = location_ptr;
 679     _result = JVMTI_ERROR_NONE;
 680   }
 681   VMOp_Type type() const { return VMOp_GetFiberFrameLocation; }
 682   jvmtiError result()    { return _result; }
 683   void doit();
 684 };
 685 
 686 
 687 // ResourceTracker
 688 //
 689 // ResourceTracker works a little like a ResourceMark. All allocates
 690 // using the resource tracker are recorded. If an allocate using the
 691 // resource tracker fails the destructor will free any resources
 692 // that were allocated using the tracker.
 693 // The motive for this class is to avoid messy error recovery code
 694 // in situations where multiple allocations are done in sequence. If
 695 // the second or subsequent allocation fails it avoids any code to
 696 // release memory allocated in the previous calls.
 697 //
 698 // Usage :-
 699 //   ResourceTracker rt(env);
 700 //   :
 701 //   err = rt.allocate(1024, &ptr);
 702 
 703 class ResourceTracker : public StackObj {
 704  private:
 705   JvmtiEnv* _env;
 706   GrowableArray<unsigned char*> *_allocations;
 707   bool _failed;
 708  public:
 709   ResourceTracker(JvmtiEnv* env);
 710   ~ResourceTracker();
 711   jvmtiError allocate(jlong size, unsigned char** mem_ptr);
 712   unsigned char* allocate(jlong size);
 713   char* strdup(const char* str);
 714 };
 715 
 716 
 717 // Jvmti monitor closure to collect off stack monitors.
 718 class JvmtiMonitorClosure: public MonitorClosure {
 719  private:
 720   JavaThread *_java_thread;
 721   JavaThread *_calling_thread;
 722   GrowableArray<jvmtiMonitorStackDepthInfo*> *_owned_monitors_list;
 723   jvmtiError _error;
 724   JvmtiEnvBase *_env;
 725 
 726  public:
 727   JvmtiMonitorClosure(JavaThread* thread, JavaThread *calling_thread,
 728                       GrowableArray<jvmtiMonitorStackDepthInfo*> *owned_monitors,
 729                       JvmtiEnvBase *env) {
 730     _java_thread = thread;
 731     _calling_thread = calling_thread;
 732     _owned_monitors_list = owned_monitors;
 733     _error = JVMTI_ERROR_NONE;
 734     _env = env;
 735   }
 736   void do_monitor(ObjectMonitor* mon);
 737   jvmtiError error() { return _error;}
 738 };
 739 
 740 
 741 // Jvmti module closure to collect all modules loaded to the system.
 742 class JvmtiModuleClosure : public StackObj {
 743 private:
 744   static GrowableArray<OopHandle> *_tbl; // Protected with Module_lock
 745 
 746   static void do_module(ModuleEntry* entry);
 747 public:
 748   jvmtiError get_all_modules(JvmtiEnv* env, jint* module_count_ptr, jobject** modules_ptr);
 749 };
 750 
 751 #endif // SHARE_PRIMS_JVMTIENVBASE_HPP