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 "runtime/fieldDescriptor.hpp"
33 #include "runtime/frame.hpp"
34 #include "runtime/handles.inline.hpp"
35 #include "runtime/orderAccess.hpp"
36 #include "runtime/thread.hpp"
37 #include "runtime/vm_operations.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 
get_phase()77   static jvmtiPhase  get_phase()                    { return _phase; }
set_phase(jvmtiPhase phase)78   static void  set_phase(jvmtiPhase phase)          { _phase = phase; }
is_vm_live()79   static bool is_vm_live()                          { return _phase == JVMTI_PHASE_LIVE; }
80 
entering_dying_thread_env_iteration()81   static void entering_dying_thread_env_iteration() { ++_dying_thread_env_iteration_count; }
leaving_dying_thread_env_iteration()82   static void leaving_dying_thread_env_iteration()  { --_dying_thread_env_iteration_count; }
is_inside_dying_thread_env_iteration()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 
set_env_local_storage(const void * data)116   void set_env_local_storage(const void* data)     { _env_local_storage = data; }
get_env_local_storage()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 
get_native_method_prefixes()122   char** get_native_method_prefixes()              { return _native_method_prefixes; }
get_native_method_prefix_count()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;
next_environment()134   JvmtiEnv* next_environment()                     { return (JvmtiEnv*)_next; }
set_next_environment(JvmtiEnvBase * env)135   void set_next_environment(JvmtiEnvBase* env)     { _next = env; }
head_environment()136   static JvmtiEnv* head_environment()              {
137     JVMTI_ONLY(return (JvmtiEnv*)_head_environment);
138     NOT_JVMTI(return NULL);
139   }
140 
141  public:
142 
143   bool is_valid();
144 
145   bool use_version_1_0_semantics();  // agent asked for version 1.0
146   bool use_version_1_1_semantics();  // agent asked for version 1.1
147   bool use_version_1_2_semantics();  // agent asked for version 1.2
148 
is_retransformable()149   bool is_retransformable()                        { return _is_retransformable; }
150 
jvmti_external_offset()151   static ByteSize jvmti_external_offset() {
152     return byte_offset_of(JvmtiEnvBase, _jvmti_external);
153   };
154 
JvmtiEnv_from_jvmti_env(jvmtiEnv * env)155   static JvmtiEnv* JvmtiEnv_from_jvmti_env(jvmtiEnv *env) {
156     return (JvmtiEnv*)((intptr_t)env - in_bytes(jvmti_external_offset()));
157   };
158 
get_capabilities()159   jvmtiCapabilities *get_capabilities()             { return &_current_capabilities; }
160 
get_prohibited_capabilities()161   jvmtiCapabilities *get_prohibited_capabilities()  { return &_prohibited_capabilities; }
162 
163   static char** get_all_native_method_prefixes(int* count_ptr);
164 
165   // This test will answer true when all environments have been disposed and some have
166   // not yet been deallocated.  As a result, this test should only be used as an
167   // optimization for the no environment case.
environments_might_exist()168   static bool environments_might_exist() {
169     return head_environment() != NULL;
170   }
171 
172   static void check_for_periodic_clean_up();
173 
env_event_enable()174   JvmtiEnvEventEnable *env_event_enable() {
175     return &_env_event_enable;
176   }
177 
allocate(jlong size,unsigned char ** mem_ptr)178   jvmtiError allocate(jlong size, unsigned char** mem_ptr) {
179     if (size < 0) {
180       return JVMTI_ERROR_ILLEGAL_ARGUMENT;
181     }
182     if (size == 0) {
183       *mem_ptr = NULL;
184     } else {
185       *mem_ptr = (unsigned char *)os::malloc((size_t)size, mtInternal);
186       if (*mem_ptr == NULL) {
187         return JVMTI_ERROR_OUT_OF_MEMORY;
188       }
189     }
190     return JVMTI_ERROR_NONE;
191   }
192 
deallocate(unsigned char * mem)193   jvmtiError deallocate(unsigned char* mem) {
194     if (mem != NULL) {
195       os::free(mem, mtInternal);
196     }
197     return JVMTI_ERROR_NONE;
198   }
199 
200 
201   // Memory functions
202   unsigned char* jvmtiMalloc(jlong size);  // don't use this - call allocate
203 
204   // method to create a local handle
jni_reference(Handle hndl)205   jobject jni_reference(Handle hndl) {
206     return JNIHandles::make_local(hndl());
207   }
208 
209   // method to create a local handle.
210   // This function allows caller to specify which
211   // threads local handle table to use.
jni_reference(JavaThread * thread,Handle hndl)212   jobject jni_reference(JavaThread *thread, Handle hndl) {
213     return JNIHandles::make_local(thread, hndl());
214   }
215 
216   // method to destroy a local handle
destroy_jni_reference(jobject jobj)217   void destroy_jni_reference(jobject jobj) {
218     JNIHandles::destroy_local(jobj);
219   }
220 
221   // method to destroy a local handle.
222   // This function allows caller to specify which
223   // threads local handle table to use although currently it is
224   // not used.
destroy_jni_reference(JavaThread * thread,jobject jobj)225   void destroy_jni_reference(JavaThread *thread, jobject jobj) {
226     destroy_jni_reference(jobj);
227   }
228 
jvmti_external()229   jvmtiEnv* jvmti_external() { return &_jvmti_external; };
230 
231 // Event Dispatch
232 
has_callback(jvmtiEvent event_type)233   bool has_callback(jvmtiEvent event_type) {
234     assert(event_type >= JVMTI_MIN_EVENT_TYPE_VAL &&
235            event_type <= JVMTI_MAX_EVENT_TYPE_VAL, "checking");
236     return ((void**)&_event_callbacks)[event_type-JVMTI_MIN_EVENT_TYPE_VAL] != NULL;
237   }
238 
callbacks()239   jvmtiEventCallbacks* callbacks() {
240     return &_event_callbacks;
241   }
242 
ext_callbacks()243   jvmtiExtEventCallbacks* ext_callbacks() {
244     return &_ext_event_callbacks;
245   }
246 
set_tag_map(JvmtiTagMap * tag_map)247   void set_tag_map(JvmtiTagMap* tag_map) {
248     _tag_map = tag_map;
249   }
250 
tag_map()251   JvmtiTagMap* tag_map() {
252     return _tag_map;
253   }
254 
tag_map_acquire()255   JvmtiTagMap* tag_map_acquire() {
256     return (JvmtiTagMap*)OrderAccess::load_ptr_acquire(&_tag_map);
257   }
258 
release_set_tag_map(JvmtiTagMap * tag_map)259   void release_set_tag_map(JvmtiTagMap* tag_map) {
260     OrderAccess::release_store_ptr(&_tag_map, tag_map);
261   }
262 
263   // return true if event is enabled globally or for any thread
264   // True only if there is a callback for it.
is_enabled(jvmtiEvent event_type)265   bool is_enabled(jvmtiEvent event_type) {
266     return _env_event_enable.is_enabled(event_type);
267   }
268 
269 // Random Utilities
270 
271  protected:
272   // helper methods for creating arrays of global JNI Handles from local Handles
273   // allocated into environment specific storage
274   jobject * new_jobjectArray(int length, Handle *handles);
275   jthread * new_jthreadArray(int length, Handle *handles);
276   jthreadGroup * new_jthreadGroupArray(int length, Handle *handles);
277 
278   // convert from JNIHandle to JavaThread *
279   JavaThread  * get_JavaThread(jthread jni_thread);
280 
281   // convert to a jni jclass from a non-null Klass*
282   jclass get_jni_class_non_null(Klass* k);
283 
284   jint count_locked_objects(JavaThread *java_thread, Handle hobj);
285   jvmtiError get_locked_objects_in_frame(JavaThread *calling_thread,
286                                    JavaThread* java_thread,
287                                    javaVFrame *jvf,
288                                    GrowableArray<jvmtiMonitorStackDepthInfo*>* owned_monitors_list,
289                                    jint depth);
290   vframe* vframeFor(JavaThread* java_thread, jint depth);
291 
292  public:
293   // get a field descriptor for the specified class and field
294   static bool get_field_descriptor(Klass* k, jfieldID field, fieldDescriptor* fd);
295   // test for suspend - most (all?) of these should go away
296   static bool is_thread_fully_suspended(JavaThread *thread,
297                                         bool wait_for_suspend,
298                                         uint32_t *bits);
299 
300 
301   // JVMTI API helper functions which are called at safepoint or thread is suspended.
302   jvmtiError get_frame_count(JvmtiThreadState *state, jint *count_ptr);
303   jvmtiError get_frame_location(JavaThread* java_thread, jint depth,
304                                               jmethodID* method_ptr, jlocation* location_ptr);
305   jvmtiError get_object_monitor_usage(JavaThread *calling_thread,
306                                                     jobject object, jvmtiMonitorUsage* info_ptr);
307   jvmtiError get_stack_trace(JavaThread *java_thread,
308                                            jint stack_depth, jint max_count,
309                                            jvmtiFrameInfo* frame_buffer, jint* count_ptr);
310   jvmtiError get_current_contended_monitor(JavaThread *calling_thread,
311                                                          JavaThread *java_thread,
312                                                          jobject *monitor_ptr);
313   jvmtiError get_owned_monitors(JavaThread *calling_thread, JavaThread* java_thread,
314                           GrowableArray<jvmtiMonitorStackDepthInfo*> *owned_monitors_list);
315   jvmtiError check_top_frame(JavaThread* current_thread, JavaThread* java_thread,
316                              jvalue value, TosState tos, Handle* ret_ob_h);
317   jvmtiError force_early_return(JavaThread* java_thread, jvalue value, TosState tos);
318 };
319 
320 // This class is the only safe means of iterating through environments.
321 // Note that this iteratation includes invalid environments pending
322 // deallocation -- in fact, some uses depend on this behavior.
323 
324 class JvmtiEnvIterator : public StackObj {
325  private:
326   bool _entry_was_marked;
327  public:
JvmtiEnvIterator()328   JvmtiEnvIterator() {
329     if (Threads::number_of_threads() == 0) {
330       _entry_was_marked = false; // we are single-threaded, no need
331     } else {
332       Thread::current()->entering_jvmti_env_iteration();
333       _entry_was_marked = true;
334     }
335   }
~JvmtiEnvIterator()336   ~JvmtiEnvIterator() {
337     if (_entry_was_marked) {
338       Thread::current()->leaving_jvmti_env_iteration();
339     }
340   }
first()341   JvmtiEnv* first()                 { return JvmtiEnvBase::head_environment(); }
next(JvmtiEnvBase * env)342   JvmtiEnv* next(JvmtiEnvBase* env) { return env->next_environment(); }
343 };
344 
345 
346 // VM operation to get monitor information with stack depth.
347 class VM_GetOwnedMonitorInfo : public VM_Operation {
348 private:
349   JvmtiEnv *_env;
350   JavaThread* _calling_thread;
351   JavaThread *_java_thread;
352   jvmtiError _result;
353   GrowableArray<jvmtiMonitorStackDepthInfo*> *_owned_monitors_list;
354 
355 public:
VM_GetOwnedMonitorInfo(JvmtiEnv * env,JavaThread * calling_thread,JavaThread * java_thread,GrowableArray<jvmtiMonitorStackDepthInfo * > * owned_monitor_list)356   VM_GetOwnedMonitorInfo(JvmtiEnv* env, JavaThread* calling_thread,
357                                    JavaThread* java_thread,
358                                    GrowableArray<jvmtiMonitorStackDepthInfo*>* owned_monitor_list) {
359     _env = env;
360     _calling_thread = calling_thread;
361     _java_thread = java_thread;
362     _owned_monitors_list = owned_monitor_list;
363     _result = JVMTI_ERROR_NONE;
364   }
type() const365   VMOp_Type type() const { return VMOp_GetOwnedMonitorInfo; }
doit()366   void doit() {
367     _result = JVMTI_ERROR_THREAD_NOT_ALIVE;
368     if (Threads::includes(_java_thread) && !_java_thread->is_exiting()
369                                         && _java_thread->threadObj() != NULL) {
370       _result = ((JvmtiEnvBase *)_env)->get_owned_monitors(_calling_thread, _java_thread,
371                                                             _owned_monitors_list);
372     }
373   }
result()374   jvmtiError result() { return _result; }
375 };
376 
377 
378 // VM operation to get object monitor usage.
379 class VM_GetObjectMonitorUsage : public VM_Operation {
380 private:
381   JvmtiEnv *_env;
382   jobject _object;
383   JavaThread* _calling_thread;
384   jvmtiMonitorUsage* _info_ptr;
385   jvmtiError _result;
386 
387 public:
VM_GetObjectMonitorUsage(JvmtiEnv * env,JavaThread * calling_thread,jobject object,jvmtiMonitorUsage * info_ptr)388   VM_GetObjectMonitorUsage(JvmtiEnv *env, JavaThread* calling_thread, jobject object, jvmtiMonitorUsage* info_ptr) {
389     _env = env;
390     _object = object;
391     _calling_thread = calling_thread;
392     _info_ptr = info_ptr;
393   }
type() const394   VMOp_Type type() const { return VMOp_GetObjectMonitorUsage; }
result()395   jvmtiError result() { return _result; }
doit()396   void doit() {
397     _result = ((JvmtiEnvBase*) _env)->get_object_monitor_usage(_calling_thread, _object, _info_ptr);
398   }
399 
400 };
401 
402 // VM operation to get current contended monitor.
403 class VM_GetCurrentContendedMonitor : public VM_Operation {
404 private:
405   JvmtiEnv *_env;
406   JavaThread *_calling_thread;
407   JavaThread *_java_thread;
408   jobject *_owned_monitor_ptr;
409   jvmtiError _result;
410 
411 public:
VM_GetCurrentContendedMonitor(JvmtiEnv * env,JavaThread * calling_thread,JavaThread * java_thread,jobject * mon_ptr)412   VM_GetCurrentContendedMonitor(JvmtiEnv *env, JavaThread *calling_thread, JavaThread *java_thread, jobject *mon_ptr) {
413     _env = env;
414     _calling_thread = calling_thread;
415     _java_thread = java_thread;
416     _owned_monitor_ptr = mon_ptr;
417   }
type() const418   VMOp_Type type() const { return VMOp_GetCurrentContendedMonitor; }
result()419   jvmtiError result() { return _result; }
doit()420   void doit() {
421     _result = JVMTI_ERROR_THREAD_NOT_ALIVE;
422     if (Threads::includes(_java_thread) && !_java_thread->is_exiting() &&
423         _java_thread->threadObj() != NULL) {
424       _result = ((JvmtiEnvBase *)_env)->get_current_contended_monitor(_calling_thread,_java_thread,_owned_monitor_ptr);
425     }
426   }
427 };
428 
429 // VM operation to get stack trace at safepoint.
430 class VM_GetStackTrace : public VM_Operation {
431 private:
432   JvmtiEnv *_env;
433   JavaThread *_java_thread;
434   jint _start_depth;
435   jint _max_count;
436   jvmtiFrameInfo *_frame_buffer;
437   jint *_count_ptr;
438   jvmtiError _result;
439 
440 public:
VM_GetStackTrace(JvmtiEnv * env,JavaThread * java_thread,jint start_depth,jint max_count,jvmtiFrameInfo * frame_buffer,jint * count_ptr)441   VM_GetStackTrace(JvmtiEnv *env, JavaThread *java_thread,
442                    jint start_depth, jint max_count,
443                    jvmtiFrameInfo* frame_buffer, jint* count_ptr) {
444     _env = env;
445     _java_thread = java_thread;
446     _start_depth = start_depth;
447     _max_count = max_count;
448     _frame_buffer = frame_buffer;
449     _count_ptr = count_ptr;
450   }
result()451   jvmtiError result() { return _result; }
type() const452   VMOp_Type type() const { return VMOp_GetStackTrace; }
doit()453   void doit() {
454     _result = JVMTI_ERROR_THREAD_NOT_ALIVE;
455     if (Threads::includes(_java_thread) && !_java_thread->is_exiting()
456                                         && _java_thread->threadObj() != NULL) {
457       _result = ((JvmtiEnvBase *)_env)->get_stack_trace(_java_thread,
458                                                         _start_depth, _max_count,
459                                                         _frame_buffer, _count_ptr);
460     }
461   }
462 };
463 
464 // forward declaration
465 struct StackInfoNode;
466 
467 // VM operation to get stack trace at safepoint.
468 class VM_GetMultipleStackTraces : public VM_Operation {
469 private:
470   JvmtiEnv *_env;
471   jint _max_frame_count;
472   jvmtiStackInfo *_stack_info;
473   jvmtiError _result;
474   int _frame_count_total;
475   struct StackInfoNode *_head;
476 
env()477   JvmtiEnvBase *env()                 { return (JvmtiEnvBase *)_env; }
max_frame_count()478   jint max_frame_count()              { return _max_frame_count; }
head()479   struct StackInfoNode *head()        { return _head; }
set_head(StackInfoNode * head)480   void set_head(StackInfoNode *head)  { _head = head; }
481 
482 protected:
set_result(jvmtiError result)483   void set_result(jvmtiError result)  { _result = result; }
484   void fill_frames(jthread jt, JavaThread *thr, oop thread_oop);
485   void allocate_and_fill_stacks(jint thread_count);
486 
487 public:
VM_GetMultipleStackTraces(JvmtiEnv * env,jint max_frame_count)488   VM_GetMultipleStackTraces(JvmtiEnv *env, jint max_frame_count) {
489     _env = env;
490     _max_frame_count = max_frame_count;
491     _frame_count_total = 0;
492     _head = NULL;
493     _result = JVMTI_ERROR_NONE;
494   }
type() const495   VMOp_Type type() const             { return VMOp_GetMultipleStackTraces; }
stack_info()496   jvmtiStackInfo *stack_info()       { return _stack_info; }
result()497   jvmtiError result()                { return _result; }
498 };
499 
500 
501 // VM operation to get stack trace at safepoint.
502 class VM_GetAllStackTraces : public VM_GetMultipleStackTraces {
503 private:
504   JavaThread *_calling_thread;
505   jint _final_thread_count;
506 
507 public:
VM_GetAllStackTraces(JvmtiEnv * env,JavaThread * calling_thread,jint max_frame_count)508   VM_GetAllStackTraces(JvmtiEnv *env, JavaThread *calling_thread,
509                        jint max_frame_count)
510       : VM_GetMultipleStackTraces(env, max_frame_count) {
511     _calling_thread = calling_thread;
512   }
type() const513   VMOp_Type type() const          { return VMOp_GetAllStackTraces; }
514   void doit();
final_thread_count()515   jint final_thread_count()       { return _final_thread_count; }
516 };
517 
518 // VM operation to get stack trace at safepoint.
519 class VM_GetThreadListStackTraces : public VM_GetMultipleStackTraces {
520 private:
521   jint _thread_count;
522   const jthread* _thread_list;
523 
524 public:
VM_GetThreadListStackTraces(JvmtiEnv * env,jint thread_count,const jthread * thread_list,jint max_frame_count)525   VM_GetThreadListStackTraces(JvmtiEnv *env, jint thread_count, const jthread* thread_list, jint max_frame_count)
526       : VM_GetMultipleStackTraces(env, max_frame_count) {
527     _thread_count = thread_count;
528     _thread_list = thread_list;
529   }
type() const530   VMOp_Type type() const { return VMOp_GetThreadListStackTraces; }
531   void doit();
532 };
533 
534 
535 // VM operation to count stack frames at safepoint.
536 class VM_GetFrameCount : public VM_Operation {
537 private:
538   JvmtiEnv *_env;
539   JvmtiThreadState *_state;
540   jint *_count_ptr;
541   jvmtiError _result;
542 
543 public:
VM_GetFrameCount(JvmtiEnv * env,JvmtiThreadState * state,jint * count_ptr)544   VM_GetFrameCount(JvmtiEnv *env, JvmtiThreadState *state, jint *count_ptr) {
545     _env = env;
546     _state = state;
547     _count_ptr = count_ptr;
548   }
type() const549   VMOp_Type type() const { return VMOp_GetFrameCount; }
result()550   jvmtiError result()    { return _result; }
doit()551   void doit() {
552     _result = ((JvmtiEnvBase*)_env)->get_frame_count(_state, _count_ptr);
553   }
554 };
555 
556 // VM operation to frame location at safepoint.
557 class VM_GetFrameLocation : public VM_Operation {
558 private:
559   JvmtiEnv *_env;
560   JavaThread* _java_thread;
561   jint _depth;
562   jmethodID* _method_ptr;
563   jlocation* _location_ptr;
564   jvmtiError _result;
565 
566 public:
VM_GetFrameLocation(JvmtiEnv * env,JavaThread * java_thread,jint depth,jmethodID * method_ptr,jlocation * location_ptr)567   VM_GetFrameLocation(JvmtiEnv *env, JavaThread* java_thread, jint depth,
568                       jmethodID* method_ptr, jlocation* location_ptr) {
569     _env = env;
570     _java_thread = java_thread;
571     _depth = depth;
572     _method_ptr = method_ptr;
573     _location_ptr = location_ptr;
574   }
type() const575   VMOp_Type type() const { return VMOp_GetFrameLocation; }
result()576   jvmtiError result()    { return _result; }
doit()577   void doit() {
578     _result = ((JvmtiEnvBase*)_env)->get_frame_location(_java_thread, _depth,
579                                                         _method_ptr, _location_ptr);
580   }
581 };
582 
583 
584 // ResourceTracker
585 //
586 // ResourceTracker works a little like a ResourceMark. All allocates
587 // using the resource tracker are recorded. If an allocate using the
588 // resource tracker fails the destructor will free any resources
589 // that were allocated using the tracker.
590 // The motive for this class is to avoid messy error recovery code
591 // in situations where multiple allocations are done in sequence. If
592 // the second or subsequent allocation fails it avoids any code to
593 // release memory allocated in the previous calls.
594 //
595 // Usage :-
596 //   ResourceTracker rt(env);
597 //   :
598 //   err = rt.allocate(1024, &ptr);
599 
600 class ResourceTracker : public StackObj {
601  private:
602   JvmtiEnv* _env;
603   GrowableArray<unsigned char*> *_allocations;
604   bool _failed;
605  public:
606   ResourceTracker(JvmtiEnv* env);
607   ~ResourceTracker();
608   jvmtiError allocate(jlong size, unsigned char** mem_ptr);
609   unsigned char* allocate(jlong size);
610   char* strdup(const char* str);
611 };
612 
613 
614 // Jvmti monitor closure to collect off stack monitors.
615 class JvmtiMonitorClosure: public MonitorClosure {
616  private:
617   JavaThread *_java_thread;
618   JavaThread *_calling_thread;
619   GrowableArray<jvmtiMonitorStackDepthInfo*> *_owned_monitors_list;
620   jvmtiError _error;
621   JvmtiEnvBase *_env;
622 
623  public:
JvmtiMonitorClosure(JavaThread * thread,JavaThread * calling_thread,GrowableArray<jvmtiMonitorStackDepthInfo * > * owned_monitors,JvmtiEnvBase * env)624   JvmtiMonitorClosure(JavaThread* thread, JavaThread *calling_thread,
625                       GrowableArray<jvmtiMonitorStackDepthInfo*> *owned_monitors,
626                       JvmtiEnvBase *env) {
627     _java_thread = thread;
628     _calling_thread = calling_thread;
629     _owned_monitors_list = owned_monitors;
630     _error = JVMTI_ERROR_NONE;
631     _env = env;
632   }
633   void do_monitor(ObjectMonitor* mon);
error()634   jvmtiError error() { return _error;}
635 };
636 
637 #endif // SHARE_VM_PRIMS_JVMTIENVBASE_HPP
638