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