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