< prev index next >

src/hotspot/share/prims/jvmtiEnvBase.hpp

Print this page

 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;

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

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     }

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);

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) {

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;

 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   static jvmtiError suspend_thread(oop thread_oop, JavaThread* java_thread, bool single_suspend,
 86                                    int* need_safepoint_p);
 87   static jvmtiError resume_thread(oop thread_oop, JavaThread* java_thread, bool single_suspend);
 88   static jvmtiError check_thread_list(jint count, const jthread* list);
 89   static bool is_in_thread_list(jint count, const jthread* list, oop jt_oop);
 90  private:
 91 
 92   enum {
 93       JVMTI_MAGIC    = 0x71EE,
 94       DISPOSED_MAGIC = 0xDEFC,
 95       BAD_MAGIC      = 0xDEAD
 96   };
 97 
 98   jvmtiEnv _jvmti_external;
 99   jint _magic;
100   jint _version;  // version value passed to JNI GetEnv()
101   JvmtiEnvBase* _next;
102   bool _is_retransformable;
103   const void *_env_local_storage;     // per env agent allocated data.
104   jvmtiEventCallbacks _event_callbacks;
105   jvmtiExtEventCallbacks _ext_event_callbacks;
106   JvmtiTagMap* volatile _tag_map;
107   JvmtiEnvEventEnable _env_event_enable;
108   jvmtiCapabilities _current_capabilities;
109   jvmtiCapabilities _prohibited_capabilities;

141   static JvmtiEnv* head_environment()              {
142     JVMTI_ONLY(return (JvmtiEnv*)_head_environment);
143     NOT_JVMTI(return NULL);
144   }
145 
146  public:
147 
148   jvmtiPhase  phase();
149   bool is_valid();
150 
151   bool use_version_1_0_semantics();  // agent asked for version 1.0
152   bool use_version_1_1_semantics();  // agent asked for version 1.1
153   bool use_version_1_2_semantics();  // agent asked for version 1.2
154 
155   bool is_retransformable()                        { return _is_retransformable; }
156 
157   static ByteSize jvmti_external_offset() {
158     return byte_offset_of(JvmtiEnvBase, _jvmti_external);
159   };
160 
161   static jvmtiError get_JavaThread(ThreadsList* tlist, jthread thread, JavaThread** jt_pp) {
162     jvmtiError err = JVMTI_ERROR_NONE;
163     if (thread == NULL) {
164       *jt_pp = JavaThread::current();
165     } else {
166       err = JvmtiExport::cv_external_thread_to_JavaThread(tlist, thread, jt_pp, NULL);
167     }
168     return err;
169   }
170 
171   // If there is a virtual thread mounted to the JavaThread* then
172   // return virtual thread oop. Otherwise, return thread oop.
173   static oop get_vthread_or_thread_oop(JavaThread* jt) {
174     oop result = jt->threadObj();
175     if (jt->mounted_vthread() != NULL) {
176       result = jt->mounted_vthread();
177     }
178     return result;
179   }
180 
181   static jvmtiError get_threadOop_and_JavaThread(ThreadsList* t_list, jthread thread,
182                                                  JavaThread** jt_pp, oop* thread_oop_p);
183 
184   // Return true if java thread is a carrier thread with a mounted virtual thread.
185   static bool cthread_with_mounted_vthread(JavaThread* jt);
186   static bool cthread_with_continuation(JavaThread* jt);
187 
188   static JvmtiEnv* JvmtiEnv_from_jvmti_env(jvmtiEnv *env) {
189     return (JvmtiEnv*)((intptr_t)env - in_bytes(jvmti_external_offset()));
190   };
191 
192   jvmtiCapabilities *get_capabilities()             { return &_current_capabilities; }
193 
194   jvmtiCapabilities *get_prohibited_capabilities()  { return &_prohibited_capabilities; }
195 
196   bool early_class_hook_env() {
197     return get_capabilities()->can_generate_early_class_hook_events != 0
198         && get_capabilities()->can_generate_all_class_hook_events != 0;
199   }
200 
201   bool early_vmstart_env() {
202     return get_capabilities()->can_generate_early_vmstart != 0;
203   }
204 
205   static char** get_all_native_method_prefixes(int* count_ptr);
206 
207   // This test will answer true when all environments have been disposed and some have

301 
302 // Random Utilities
303 
304  protected:
305   // helper methods for creating arrays of global JNI Handles from local Handles
306   // allocated into environment specific storage
307   jobject * new_jobjectArray(int length, Handle *handles);
308   jthread * new_jthreadArray(int length, Handle *handles);
309   jthreadGroup * new_jthreadGroupArray(int length, Handle *handles);
310 
311   // convert to a jni jclass from a non-null Klass*
312   jclass get_jni_class_non_null(Klass* k);
313 
314   jint count_locked_objects(JavaThread *java_thread, Handle hobj);
315   jvmtiError get_locked_objects_in_frame(JavaThread *calling_thread,
316                                    JavaThread* java_thread,
317                                    javaVFrame *jvf,
318                                    GrowableArray<jvmtiMonitorStackDepthInfo*>* owned_monitors_list,
319                                    jint depth);
320  public:
321   static vframe* vframeForNoProcess(JavaThread* java_thread, jint depth, bool for_cont = false);
322 
323   // get a field descriptor for the specified class and field
324   static bool get_field_descriptor(Klass* k, jfieldID field, fieldDescriptor* fd);
325 
326   // check and skip frames hidden in mount/unmount transitions
327   static javaVFrame* check_and_skip_hidden_frames(bool is_in_VTMT, javaVFrame* jvf);
328   static javaVFrame* check_and_skip_hidden_frames(JavaThread* jt, javaVFrame* jvf);
329   static javaVFrame* check_and_skip_hidden_frames(oop vthread, javaVFrame* jvf);
330 
331   // check if virtual thread is not terminated (alive)
332   static bool is_vthread_alive(oop vt);
333 
334   // get virtual thread last java vframe
335   static javaVFrame* get_vthread_jvf(oop vthread);
336 
337   // get carrier thread last java vframe
338   static javaVFrame* get_last_java_vframe(JavaThread* jt, RegisterMap* reg_map);
339 
340   // get ordinary thread thread state
341   static jint get_thread_state(oop thread_oop, JavaThread* jt);
342 
343   // get virtual thread thread state
344   static jint get_vthread_state(oop thread_oop);
345 
346   // enumerates the live threads in the given thread group
347   static int get_live_threads(JavaThread* current_thread, Handle group_hdl, Handle **thread_objs_p);
348 
349   // enumerates the subgroups in the given thread group
350   static int get_subgroups(JavaThread* current_thread, Handle group_hdl, Handle **group_objs_p);
351 
352   // JVMTI API helper functions which are called when target thread is suspended
353   // or at safepoint / thread local handshake.
354   static jint get_frame_count(javaVFrame* jvf);
355   jvmtiError get_frame_count(JavaThread* java_thread, jint *count_ptr);
356   jvmtiError get_frame_count(oop frame_oop, jint *count_ptr);
357   jvmtiError get_frame_location(JavaThread* java_thread, jint depth,
358                                 jmethodID* method_ptr, jlocation* location_ptr);
359   jvmtiError get_frame_location(oop vthread_oop, jint depth,
360                                 jmethodID* method_ptr, jlocation* location_ptr);
361   jvmtiError get_object_monitor_usage(JavaThread *calling_thread,
362                                                     jobject object, jvmtiMonitorUsage* info_ptr);
363   jvmtiError get_stack_trace(javaVFrame *jvf,
364                              jint stack_depth, jint max_count,
365                              jvmtiFrameInfo* frame_buffer, jint* count_ptr);
366   jvmtiError get_stack_trace(JavaThread *java_thread,
367                                            jint stack_depth, jint max_count,
368                                            jvmtiFrameInfo* frame_buffer, jint* count_ptr);
369   jvmtiError get_current_contended_monitor(JavaThread* calling_thread, JavaThread *java_thread,
370                                            jobject *monitor_ptr);
371   jvmtiError get_owned_monitors(JavaThread* calling_thread, JavaThread* java_thread,
372                                 GrowableArray<jvmtiMonitorStackDepthInfo*> *owned_monitors_list);
373   jvmtiError get_owned_monitors(JavaThread *calling_thread, JavaThread* java_thread, javaVFrame* jvf,
374                           GrowableArray<jvmtiMonitorStackDepthInfo*> *owned_monitors_list);
375   static jvmtiError check_top_frame(Thread* current_thread, JavaThread* java_thread,
376                                     jvalue value, TosState tos, Handle* ret_ob_h);
377   jvmtiError force_early_return(JavaThread* java_thread, jvalue value, TosState tos);
378 };
379 
380 // This class is the only safe means of iterating through environments.
381 // Note that this iteratation includes invalid environments pending
382 // deallocation -- in fact, some uses depend on this behavior.
383 
384 class JvmtiEnvIterator : public StackObj {
385  private:
386   bool _entry_was_marked;
387  public:
388   JvmtiEnvIterator() {
389     if (Threads::number_of_threads() == 0) {
390       _entry_was_marked = false; // we are single-threaded, no need
391     } else {
392       Thread::current()->entering_jvmti_env_iteration();
393       _entry_was_marked = true;
394     }

463   void doit(Thread *target, bool self);
464 };
465 
466 // HandshakeClosure to get monitor information with stack depth.
467 class GetOwnedMonitorInfoClosure : public JvmtiHandshakeClosure {
468 private:
469   JavaThread* _calling_thread;
470   JvmtiEnv *_env;
471   GrowableArray<jvmtiMonitorStackDepthInfo*> *_owned_monitors_list;
472 
473 public:
474   GetOwnedMonitorInfoClosure(JavaThread* calling_thread, JvmtiEnv* env,
475                              GrowableArray<jvmtiMonitorStackDepthInfo*>* owned_monitor_list)
476     : JvmtiHandshakeClosure("GetOwnedMonitorInfo"),
477       _calling_thread(calling_thread),
478       _env(env),
479       _owned_monitors_list(owned_monitor_list) {}
480   void do_thread(Thread *target);
481 };
482 

483 // VM operation to get object monitor usage.
484 class VM_GetObjectMonitorUsage : public VM_Operation {
485 private:
486   JvmtiEnv *_env;
487   jobject _object;
488   JavaThread* _calling_thread;
489   jvmtiMonitorUsage* _info_ptr;
490   jvmtiError _result;
491 
492 public:
493   VM_GetObjectMonitorUsage(JvmtiEnv *env, JavaThread* calling_thread, jobject object, jvmtiMonitorUsage* info_ptr) {
494     _env = env;
495     _object = object;
496     _calling_thread = calling_thread;
497     _info_ptr = info_ptr;
498   }
499   VMOp_Type type() const { return VMOp_GetObjectMonitorUsage; }
500   jvmtiError result() { return _result; }
501   void doit() {
502     _result = ((JvmtiEnvBase*) _env)->get_object_monitor_usage(_calling_thread, _object, _info_ptr);

524 class GetStackTraceClosure : public JvmtiHandshakeClosure {
525 private:
526   JvmtiEnv *_env;
527   jint _start_depth;
528   jint _max_count;
529   jvmtiFrameInfo *_frame_buffer;
530   jint *_count_ptr;
531 
532 public:
533   GetStackTraceClosure(JvmtiEnv *env, jint start_depth, jint max_count,
534                        jvmtiFrameInfo* frame_buffer, jint* count_ptr)
535     : JvmtiHandshakeClosure("GetStackTrace"),
536       _env(env),
537       _start_depth(start_depth),
538       _max_count(max_count),
539       _frame_buffer(frame_buffer),
540       _count_ptr(count_ptr) {}
541   void do_thread(Thread *target);
542 };
543 
544 #ifdef ASSERT
545 // HandshakeClosure to print stack trace in JvmtiVTMTDisabler error handling
546 class PrintStackTraceClosure : public HandshakeClosure {
547  public:
548   static void do_thread_impl(Thread *target);
549 
550   PrintStackTraceClosure()
551       : HandshakeClosure("PrintStackTraceClosure") {}
552   void do_thread(Thread *target);
553 };
554 #endif
555 
556 // forward declaration
557 struct StackInfoNode;
558 
559 // Get stack trace at safepoint or at direct handshake.
560 class MultipleStackTracesCollector {
561 private:
562   friend class VM_GetThreadListStackTraces;
563   JvmtiEnv *_env;
564   jint _max_frame_count;
565   jvmtiStackInfo *_stack_info;
566   jvmtiError _result;
567   int _frame_count_total;
568   struct StackInfoNode *_head;
569 
570   JvmtiEnvBase *env()                 { return (JvmtiEnvBase *)_env; }
571   jint max_frame_count()              { return _max_frame_count; }
572   struct StackInfoNode *head()        { return _head; }
573   void set_head(StackInfoNode *head)  { _head = head; }
574 
575 public:
576   MultipleStackTracesCollector(JvmtiEnv *env, jint max_frame_count)
577     : _env(env),
578       _max_frame_count(max_frame_count),
579       _stack_info(NULL),
580       _result(JVMTI_ERROR_NONE),
581       _frame_count_total(0),
582       _head(NULL) {

600   VM_GetAllStackTraces(JvmtiEnv *env, JavaThread *calling_thread,
601                        jint max_frame_count)
602       : _calling_thread(calling_thread),
603         _final_thread_count(0),
604         _collector(env, max_frame_count) {
605   }
606   VMOp_Type type() const          { return VMOp_GetAllStackTraces; }
607   void doit();
608   jint final_thread_count()       { return _final_thread_count; }
609   jvmtiStackInfo *stack_info()    { return _collector.stack_info(); }
610   jvmtiError result()             { return _collector.result(); }
611 };
612 
613 // VM operation to get stack trace at safepoint.
614 class VM_GetThreadListStackTraces : public VM_Operation {
615 private:
616   jint _thread_count;
617   const jthread* _thread_list;
618   MultipleStackTracesCollector _collector;
619 
620   JvmtiEnvBase *env() { return _collector.env(); }
621 
622 public:
623   VM_GetThreadListStackTraces(JvmtiEnv *env, jint thread_count, const jthread* thread_list, jint max_frame_count)
624       : _thread_count(thread_count),
625         _thread_list(thread_list),
626         _collector(env, max_frame_count) {
627   }
628   VMOp_Type type() const { return VMOp_GetThreadListStackTraces; }
629   void doit();
630   jvmtiStackInfo *stack_info()    { return _collector.stack_info(); }
631   jvmtiError result()             { return _collector.result(); }
632 };
633 
634 class VM_VThreadGetStackTrace : public VM_Operation {
635 private:
636   JvmtiEnv *_env;
637   Handle _vthread_h;
638   jint _start_depth;
639   jint _max_count;
640   jvmtiFrameInfo* _frame_buffer;
641   jint* _count_ptr;
642   jvmtiError _result;
643 
644 public:
645   VM_VThreadGetStackTrace(JvmtiEnv *env, Handle vthread_h,
646                           jint start_depth, jint max_count,
647                           jvmtiFrameInfo* frame_buffer, jint* count_ptr)
648     : _vthread_h(vthread_h),
649       _start_depth(start_depth),
650       _max_count(max_count),
651       _frame_buffer(frame_buffer),
652       _count_ptr(count_ptr),
653       _result(JVMTI_ERROR_NONE)
654   {}
655 
656   VMOp_Type type() const { return VMOp_VThreadGetStackTrace; }
657   void doit();
658   jvmtiError result() { return _result; }
659 };
660 
661 class VM_VThreadGetFrameCount : public VM_Operation {
662 private:
663   JvmtiEnv *_env;
664   Handle _vthread_h;
665   jint* _count_ptr;
666   jvmtiError _result;
667 
668 public:
669   VM_VThreadGetFrameCount(JvmtiEnv *env, Handle vthread_h, jint* count_ptr)
670     : _vthread_h(vthread_h),
671       _count_ptr(count_ptr),
672       _result(JVMTI_ERROR_NONE)
673   {}
674 
675   VMOp_Type type() const { return VMOp_VThreadGetFrameCount; }
676   void doit();
677   jvmtiError result() { return _result; }
678 };
679 
680 // HandshakeClosure to get single stack trace.
681 class GetSingleStackTraceClosure : public HandshakeClosure {
682 private:
683   JavaThread *_calling_thread;
684   jthread _jthread;
685   MultipleStackTracesCollector _collector;
686 
687 public:
688   GetSingleStackTraceClosure(JvmtiEnv *env, JavaThread *calling_thread,
689                              jthread thread, jint max_frame_count)
690     : HandshakeClosure("GetSingleStackTrace"),
691       _calling_thread(calling_thread),
692       _jthread(thread),
693       _collector(env, max_frame_count) {
694   }
695   void do_thread(Thread *target);
696   jvmtiStackInfo *stack_info()    { return _collector.stack_info(); }
697   jvmtiError result()             { return _collector.result(); }
698 };
699 
700 // HandshakeClosure to count stack frames.
701 class GetFrameCountClosure : public JvmtiHandshakeClosure {
702 private:
703   JvmtiEnv *_env;

704   jint *_count_ptr;
705 
706 public:
707   GetFrameCountClosure(JvmtiEnv *env, jint *count_ptr)
708     : JvmtiHandshakeClosure("GetFrameCount"),
709       _env(env),

710       _count_ptr(count_ptr) {}
711   void do_thread(Thread *target);
712 };
713 
714 // HandshakeClosure to get frame location.
715 class GetFrameLocationClosure : public JvmtiHandshakeClosure {
716 private:
717   JvmtiEnv *_env;
718   jint _depth;
719   jmethodID* _method_ptr;
720   jlocation* _location_ptr;
721 
722 public:
723   GetFrameLocationClosure(JvmtiEnv *env, jint depth,
724                           jmethodID* method_ptr, jlocation* location_ptr)
725     : JvmtiHandshakeClosure("GetFrameLocation"),
726       _env(env),
727       _depth(depth),
728       _method_ptr(method_ptr),
729       _location_ptr(location_ptr) {}
730   void do_thread(Thread *target);
731 };
732 
733 // HandshakeClosure to get virtual thread monitor information with stack depth.
734 class VThreadGetOwnedMonitorInfoClosure : public HandshakeClosure {
735 private:
736   JvmtiEnv *_env;
737   Handle _vthread_h;
738   GrowableArray<jvmtiMonitorStackDepthInfo*> *_owned_monitors_list;
739   jvmtiError _result;
740 
741 public:
742   VThreadGetOwnedMonitorInfoClosure(JvmtiEnv* env,
743                             Handle vthread_h,
744                             GrowableArray<jvmtiMonitorStackDepthInfo*>* owned_monitors_list)
745     : HandshakeClosure("VThreadGetOwnedMonitorInfo"),
746       _env(env),
747       _vthread_h(vthread_h),
748       _owned_monitors_list(owned_monitors_list),
749       _result(JVMTI_ERROR_THREAD_NOT_ALIVE) {}
750 
751   void do_thread(Thread *target);
752   jvmtiError result() { return _result; }
753 };
754 
755 // HandshakeClosure to get virtual thread current contended monitor.
756 class VThreadGetCurrentContendedMonitorClosure : public HandshakeClosure {
757 private:
758   JvmtiEnv *_env;
759   Handle _vthread_h;
760   jobject *_owned_monitor_ptr;
761   jvmtiError _result;
762 
763 public:
764   VThreadGetCurrentContendedMonitorClosure(JvmtiEnv *env, Handle vthread_h, jobject *mon_ptr)
765     : HandshakeClosure("VThreadGetCurrentContendedMonitor"),
766       _env(env),
767       _vthread_h(vthread_h),
768       _owned_monitor_ptr(mon_ptr),
769       _result(JVMTI_ERROR_THREAD_NOT_ALIVE) {}
770   jvmtiError result() { return _result; }
771   void do_thread(Thread *target);
772 };
773 
774 // HandshakeClosure to get virtual thread thread at safepoint.
775 class VThreadGetThreadClosure : public HandshakeClosure {
776 private:
777   Handle _vthread_h;
778   jthread* _carrier_thread_ptr;
779   jvmtiError _result;
780 
781 public:
782   VThreadGetThreadClosure(Handle vthread_h, jthread* carrier_thread_ptr)
783     : HandshakeClosure("VThreadGetThread"),
784       _vthread_h(vthread_h),
785       _carrier_thread_ptr(carrier_thread_ptr),
786       _result(JVMTI_ERROR_NONE) {}
787 
788   void do_thread(Thread *target);
789   jvmtiError result() { return _result; }
790 };
791 
792 // HandshakeClosure to get virtual thread stack trace at safepoint.
793 class VThreadGetStackTraceClosure : public HandshakeClosure {
794 private:
795   JvmtiEnv *_env;
796   Handle _vthread_h;
797   jint _start_depth;
798   jint _max_count;
799   jvmtiFrameInfo *_frame_buffer;
800   jint *_count_ptr;
801   jvmtiError _result;
802 
803 public:
804   VThreadGetStackTraceClosure(JvmtiEnv *env, Handle vthread_h,
805                               jint start_depth, jint max_count,
806                               jvmtiFrameInfo* frame_buffer, jint* count_ptr)
807     : HandshakeClosure("VThreadGetStackTrace"),
808       _env(env),
809       _vthread_h(vthread_h),
810        _start_depth(start_depth),
811       _max_count(max_count),
812       _frame_buffer(frame_buffer),
813       _count_ptr(count_ptr),
814       _result(JVMTI_ERROR_NONE) {}
815 
816   void do_thread(Thread *target);
817   jvmtiError result() { return _result; }
818 };
819 
820 // HandshakeClosure to count virtual thread stack frames at safepoint.
821 class VThreadGetFrameCountClosure : public HandshakeClosure {
822 private:
823   JvmtiEnv *_env;
824   Handle _vthread_h;
825   jint *_count_ptr;
826   jvmtiError _result;
827 
828 public:
829   VThreadGetFrameCountClosure(JvmtiEnv *env, Handle vthread_h, jint *count_ptr)
830     : HandshakeClosure("VThreadGetFrameCount"),
831       _env(env), _vthread_h(vthread_h), _count_ptr(count_ptr),
832       _result(JVMTI_ERROR_NONE) {}
833 
834   void do_thread(Thread *target);
835   jvmtiError result() { return _result; }
836 };
837 
838 // HandshakeClosure get to virtual thread frame location at safepoint.
839 class VThreadGetFrameLocationClosure : public HandshakeClosure {
840 private:
841   JvmtiEnv *_env;
842   Handle _vthread_h;
843   jint _depth;
844   jmethodID* _method_ptr;
845   jlocation* _location_ptr;
846   jvmtiError _result;
847 
848 public:
849   VThreadGetFrameLocationClosure(JvmtiEnv *env, Handle vthread_h, jint depth,
850                                  jmethodID* method_ptr, jlocation* location_ptr)
851     : HandshakeClosure("VThreadGetFrameLocation"),
852       _env(env),
853       _vthread_h(vthread_h),
854       _depth(depth),
855       _method_ptr(method_ptr),
856       _location_ptr(location_ptr),
857       _result(JVMTI_ERROR_NONE) {}
858 
859   void do_thread(Thread *target);
860   jvmtiError result() { return _result; }
861 };
862 
863 // HandshakeClosure to get virtual thread state at safepoint.
864 class VThreadGetThreadStateClosure : public HandshakeClosure {
865 private:
866   Handle _vthread_h;
867   jint *_state_ptr;
868   jvmtiError _result;
869 
870 public:
871   VThreadGetThreadStateClosure(Handle vthread_h, jint *state_ptr)
872     : HandshakeClosure("VThreadGetThreadState"),
873       _vthread_h(vthread_h),
874       _state_ptr(state_ptr),
875       _result(JVMTI_ERROR_NONE) {}
876 
877   void do_thread(Thread *target);
878   jvmtiError result() { return _result; }
879 };
880 
881 // ResourceTracker
882 //
883 // ResourceTracker works a little like a ResourceMark. All allocates
884 // using the resource tracker are recorded. If an allocate using the
885 // resource tracker fails the destructor will free any resources
886 // that were allocated using the tracker.
887 // The motive for this class is to avoid messy error recovery code
888 // in situations where multiple allocations are done in sequence. If
889 // the second or subsequent allocation fails it avoids any code to
890 // release memory allocated in the previous calls.
891 //
892 // Usage :-
893 //   ResourceTracker rt(env);
894 //   :
895 //   err = rt.allocate(1024, &ptr);
896 
897 class ResourceTracker : public StackObj {
898  private:
899   JvmtiEnv* _env;
< prev index next >