1 /*
   2  * Copyright (c) 2003, 2017, 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     return JNIHandles::make_local(hndl());
 219   }
 220 
 221   // method to create a local handle.
 222   // This function allows caller to specify which
 223   // threads local handle table to use.
 224   jobject jni_reference(JavaThread *thread, Handle hndl) {
 225     return JNIHandles::make_local(thread, hndl());
 226   }
 227 
 228   // method to destroy a local handle
 229   void destroy_jni_reference(jobject jobj) {
 230     JNIHandles::destroy_local(jobj);
 231   }
 232 
 233   // method to destroy a local handle.
 234   // This function allows caller to specify which
 235   // threads local handle table to use although currently it is
 236   // not used.
 237   void destroy_jni_reference(JavaThread *thread, jobject jobj) {
 238     destroy_jni_reference(jobj);
 239   }
 240 
 241   jvmtiEnv* jvmti_external() { return &_jvmti_external; };
 242 
 243 // Event Dispatch
 244 
 245   bool has_callback(jvmtiEvent event_type) {
 246     assert(event_type >= JVMTI_MIN_EVENT_TYPE_VAL &&
 247            event_type <= JVMTI_MAX_EVENT_TYPE_VAL, "checking");
 248     return ((void**)&_event_callbacks)[event_type-JVMTI_MIN_EVENT_TYPE_VAL] != NULL;
 249   }
 250 
 251   jvmtiEventCallbacks* callbacks() {
 252     return &_event_callbacks;
 253   }
 254 
 255   jvmtiExtEventCallbacks* ext_callbacks() {
 256     return &_ext_event_callbacks;
 257   }
 258 
 259   void set_tag_map(JvmtiTagMap* tag_map) {
 260     _tag_map = tag_map;
 261   }
 262 
 263   JvmtiTagMap* tag_map() {
 264     return _tag_map;
 265   }
 266 
 267 
 268   // return true if event is enabled globally or for any thread
 269   // True only if there is a callback for it.
 270   bool is_enabled(jvmtiEvent event_type) {
 271     return _env_event_enable.is_enabled(event_type);
 272   }
 273 
 274 // Random Utilities
 275 
 276  protected:
 277   // helper methods for creating arrays of global JNI Handles from local Handles
 278   // allocated into environment specific storage
 279   jobject * new_jobjectArray(int length, Handle *handles);
 280   jthread * new_jthreadArray(int length, Handle *handles);
 281   jthreadGroup * new_jthreadGroupArray(int length, Handle *handles);
 282 
 283   // convert to a jni jclass from a non-null Klass*
 284   jclass get_jni_class_non_null(Klass* k);
 285 
 286   jint count_locked_objects(JavaThread *java_thread, Handle hobj);
 287   jvmtiError get_locked_objects_in_frame(JavaThread *calling_thread,
 288                                    JavaThread* java_thread,
 289                                    javaVFrame *jvf,
 290                                    GrowableArray<jvmtiMonitorStackDepthInfo*>* owned_monitors_list,
 291                                    jint depth);
 292   vframe* vframeFor(JavaThread* java_thread, jint depth);
 293 
 294  public:
 295   // get a field descriptor for the specified class and field
 296   static bool get_field_descriptor(Klass* k, jfieldID field, fieldDescriptor* fd);
 297 
 298   // JVMTI API helper functions which are called at safepoint or thread is suspended.
 299   jvmtiError get_frame_count(JvmtiThreadState *state, jint *count_ptr);
 300   jvmtiError get_frame_location(JavaThread* java_thread, jint depth,
 301                                               jmethodID* method_ptr, jlocation* location_ptr);
 302   jvmtiError get_object_monitor_usage(JavaThread *calling_thread,
 303                                                     jobject object, jvmtiMonitorUsage* info_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 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 
 594 // ResourceTracker
 595 //
 596 // ResourceTracker works a little like a ResourceMark. All allocates
 597 // using the resource tracker are recorded. If an allocate using the
 598 // resource tracker fails the destructor will free any resources
 599 // that were allocated using the tracker.
 600 // The motive for this class is to avoid messy error recovery code
 601 // in situations where multiple allocations are done in sequence. If
 602 // the second or subsequent allocation fails it avoids any code to
 603 // release memory allocated in the previous calls.
 604 //
 605 // Usage :-
 606 //   ResourceTracker rt(env);
 607 //   :
 608 //   err = rt.allocate(1024, &ptr);
 609 
 610 class ResourceTracker : public StackObj {
 611  private:
 612   JvmtiEnv* _env;
 613   GrowableArray<unsigned char*> *_allocations;
 614   bool _failed;
 615  public:
 616   ResourceTracker(JvmtiEnv* env);
 617   ~ResourceTracker();
 618   jvmtiError allocate(jlong size, unsigned char** mem_ptr);
 619   unsigned char* allocate(jlong size);
 620   char* strdup(const char* str);
 621 };
 622 
 623 
 624 // Jvmti monitor closure to collect off stack monitors.
 625 class JvmtiMonitorClosure: public MonitorClosure {
 626  private:
 627   JavaThread *_java_thread;
 628   JavaThread *_calling_thread;
 629   GrowableArray<jvmtiMonitorStackDepthInfo*> *_owned_monitors_list;
 630   jvmtiError _error;
 631   JvmtiEnvBase *_env;
 632 
 633  public:
 634   JvmtiMonitorClosure(JavaThread* thread, JavaThread *calling_thread,
 635                       GrowableArray<jvmtiMonitorStackDepthInfo*> *owned_monitors,
 636                       JvmtiEnvBase *env) {
 637     _java_thread = thread;
 638     _calling_thread = calling_thread;
 639     _owned_monitors_list = owned_monitors;
 640     _error = JVMTI_ERROR_NONE;
 641     _env = env;
 642   }
 643   void do_monitor(ObjectMonitor* mon);
 644   jvmtiError error() { return _error;}
 645 };
 646 
 647 
 648 // Jvmti module closure to collect all modules loaded to the system.
 649 class JvmtiModuleClosure : public StackObj {
 650 private:
 651   static GrowableArray<OopHandle> *_tbl; // Protected with Module_lock
 652 
 653   static void do_module(ModuleEntry* entry) {
 654     assert_locked_or_safepoint(Module_lock);
 655     OopHandle module = entry->module_handle();
 656     guarantee(module.resolve() != NULL, "module object is NULL");
 657     _tbl->push(module);
 658   }
 659 
 660 public:
 661   jvmtiError get_all_modules(JvmtiEnv* env, jint* module_count_ptr, jobject** modules_ptr);
 662 };
 663 
 664 #endif // SHARE_VM_PRIMS_JVMTIENVBASE_HPP