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   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 
 91   // check if thread_oop represents a passive carrier thread
 92   static bool is_passive_carrier_thread(JavaThread* java_thread, oop thread_oop) {
 93      return java_thread != NULL && java_thread->mounted_vthread() != NULL
 94                                 && java_thread->threadObj() == thread_oop
 95                                 && java_thread->vthread() != thread_oop;
 96   }
 97 
 98  private:
 99 
100   enum {
101       JVMTI_MAGIC    = 0x71EE,
102       DISPOSED_MAGIC = 0xDEFC,
103       BAD_MAGIC      = 0xDEAD
104   };
105 
106   jvmtiEnv _jvmti_external;
107   jint _magic;
108   jint _version;  // version value passed to JNI GetEnv()
109   JvmtiEnvBase* _next;
110   bool _is_retransformable;
111   const void *_env_local_storage;     // per env agent allocated data.
112   jvmtiEventCallbacks _event_callbacks;
113   jvmtiExtEventCallbacks _ext_event_callbacks;
114   JvmtiTagMap* volatile _tag_map;
115   JvmtiEnvEventEnable _env_event_enable;
116   jvmtiCapabilities _current_capabilities;
117   jvmtiCapabilities _prohibited_capabilities;
118   volatile bool _class_file_load_hook_ever_enabled;
119   static volatile bool _needs_clean_up;
120   char** _native_method_prefixes;
121   int    _native_method_prefix_count;
122 
123  protected:
124   JvmtiEnvBase(jint version);
125   ~JvmtiEnvBase();
126   void dispose();
127   void env_dispose();
128 
129   void set_env_local_storage(const void* data)     { _env_local_storage = data; }
130   const void* get_env_local_storage()              { return _env_local_storage; }
131 
132   void record_class_file_load_hook_enabled();
133   void record_first_time_class_file_load_hook_enabled();
134 
135   char** get_native_method_prefixes()              { return _native_method_prefixes; }
136   int    get_native_method_prefix_count()          { return _native_method_prefix_count; }
137   jvmtiError set_native_method_prefixes(jint prefix_count, char** prefixes);
138 
139  private:
140   friend class JvmtiEventControllerPrivate;
141   void initialize();
142   void set_event_callbacks(const jvmtiEventCallbacks* callbacks, jint size_of_callbacks);
143   static void globally_initialize();
144   static void periodic_clean_up();
145 
146   friend class JvmtiEnvIterator;
147   JvmtiEnv* next_environment()                     { return (JvmtiEnv*)_next; }
148   void set_next_environment(JvmtiEnvBase* env)     { _next = env; }
149   static JvmtiEnv* head_environment()              {
150     JVMTI_ONLY(return (JvmtiEnv*)_head_environment);
151     NOT_JVMTI(return NULL);
152   }
153 
154  public:
155 
156   jvmtiPhase  phase();
157   bool is_valid();
158 
159   bool use_version_1_0_semantics();  // agent asked for version 1.0
160   bool use_version_1_1_semantics();  // agent asked for version 1.1
161   bool use_version_1_2_semantics();  // agent asked for version 1.2
162 
163   bool is_retransformable()                        { return _is_retransformable; }
164 
165   static ByteSize jvmti_external_offset() {
166     return byte_offset_of(JvmtiEnvBase, _jvmti_external);
167   };
168 
169   static jvmtiError get_JavaThread(ThreadsList* tlist, jthread thread, JavaThread** jt_pp) {
170     jvmtiError err = JVMTI_ERROR_NONE;
171     if (thread == NULL) {
172       *jt_pp = JavaThread::current();
173     } else {
174       err = JvmtiExport::cv_external_thread_to_JavaThread(tlist, thread, jt_pp, NULL);
175     }
176     return err;
177   }
178 
179   // If there is a virtual thread mounted to the JavaThread* then
180   // return virtual thread oop. Otherwise, return thread oop.
181   static oop get_vthread_or_thread_oop(JavaThread* jt) {
182     oop result = jt->threadObj();
183     if (jt->mounted_vthread() != NULL) {
184       result = jt->mounted_vthread();
185     }
186     return result;
187   }
188 
189   static jvmtiError get_threadOop_and_JavaThread(ThreadsList* t_list, jthread thread,
190                                                  JavaThread** jt_pp, oop* thread_oop_p);
191 
192   // Return true if java thread is a carrier thread with a mounted virtual thread.
193   static bool cthread_with_mounted_vthread(JavaThread* jt);
194   static bool cthread_with_continuation(JavaThread* jt);
195 
196   static JvmtiEnv* JvmtiEnv_from_jvmti_env(jvmtiEnv *env) {
197     return (JvmtiEnv*)((intptr_t)env - in_bytes(jvmti_external_offset()));
198   };
199 
200   jvmtiCapabilities *get_capabilities()             { return &_current_capabilities; }
201 
202   jvmtiCapabilities *get_prohibited_capabilities()  { return &_prohibited_capabilities; }
203 
204   bool early_class_hook_env() {
205     return get_capabilities()->can_generate_early_class_hook_events != 0
206         && get_capabilities()->can_generate_all_class_hook_events != 0;
207   }
208 
209   bool early_vmstart_env() {
210     return get_capabilities()->can_generate_early_vmstart != 0;
211   }
212 
213   static char** get_all_native_method_prefixes(int* count_ptr);
214 
215   // This test will answer true when all environments have been disposed and some have
216   // not yet been deallocated.  As a result, this test should only be used as an
217   // optimization for the no environment case.
218   static bool environments_might_exist() {
219     return head_environment() != NULL;
220   }
221 
222   static void check_for_periodic_clean_up();
223 
224   JvmtiEnvEventEnable *env_event_enable() {
225     return &_env_event_enable;
226   }
227 
228   jvmtiError allocate(jlong size, unsigned char** mem_ptr) {
229     if (size < 0) {
230       return JVMTI_ERROR_ILLEGAL_ARGUMENT;
231     }
232     if (size == 0) {
233       *mem_ptr = NULL;
234     } else {
235       *mem_ptr = (unsigned char *)os::malloc((size_t)size, mtInternal);
236       if (*mem_ptr == NULL) {
237         return JVMTI_ERROR_OUT_OF_MEMORY;
238       }
239     }
240     return JVMTI_ERROR_NONE;
241   }
242 
243   jvmtiError deallocate(unsigned char* mem) {
244     if (mem != NULL) {
245       os::free(mem);
246     }
247     return JVMTI_ERROR_NONE;
248   }
249 
250 
251   // Memory functions
252   unsigned char* jvmtiMalloc(jlong size);  // don't use this - call allocate
253 
254   // method to create a local handle
255   jobject jni_reference(Handle hndl);
256 
257   // method to create a local handle.
258   // This function allows caller to specify which
259   // threads local handle table to use.
260   jobject jni_reference(JavaThread *thread, Handle hndl);
261 
262   // method to destroy a local handle
263   void destroy_jni_reference(jobject jobj);
264 
265   // method to destroy a local handle.
266   // This function allows caller to specify which
267   // threads local handle table to use.
268   void destroy_jni_reference(JavaThread *thread, jobject jobj);
269 
270   jvmtiEnv* jvmti_external() { return &_jvmti_external; };
271 
272 // Event Dispatch
273 
274   bool has_callback(jvmtiEvent event_type) {
275     assert(event_type >= JVMTI_MIN_EVENT_TYPE_VAL &&
276            event_type <= JVMTI_MAX_EVENT_TYPE_VAL, "checking");
277     return ((void**)&_event_callbacks)[event_type-JVMTI_MIN_EVENT_TYPE_VAL] != NULL;
278   }
279 
280   jvmtiEventCallbacks* callbacks() {
281     return &_event_callbacks;
282   }
283 
284   jvmtiExtEventCallbacks* ext_callbacks() {
285     return &_ext_event_callbacks;
286   }
287 
288   void set_tag_map(JvmtiTagMap* tag_map) {
289     _tag_map = tag_map;
290   }
291 
292   JvmtiTagMap* tag_map() {
293     return _tag_map;
294   }
295 
296   JvmtiTagMap* tag_map_acquire() {
297     return Atomic::load_acquire(&_tag_map);
298   }
299 
300   void release_set_tag_map(JvmtiTagMap* tag_map) {
301     Atomic::release_store(&_tag_map, tag_map);
302   }
303 
304   // return true if event is enabled globally or for any thread
305   // True only if there is a callback for it.
306   bool is_enabled(jvmtiEvent event_type) {
307     return _env_event_enable.is_enabled(event_type);
308   }
309 
310 // Random Utilities
311 
312  protected:
313   // helper methods for creating arrays of global JNI Handles from local Handles
314   // allocated into environment specific storage
315   jobject * new_jobjectArray(int length, Handle *handles);
316   jthread * new_jthreadArray(int length, Handle *handles);
317   jthreadGroup * new_jthreadGroupArray(int length, Handle *handles);
318 
319   // convert to a jni jclass from a non-null Klass*
320   jclass get_jni_class_non_null(Klass* k);
321 
322   jint count_locked_objects(JavaThread *java_thread, Handle hobj);
323   jvmtiError get_locked_objects_in_frame(JavaThread *calling_thread,
324                                    JavaThread* java_thread,
325                                    javaVFrame *jvf,
326                                    GrowableArray<jvmtiMonitorStackDepthInfo*>* owned_monitors_list,
327                                    jint depth);
328  public:
329   static vframe* vframeForNoProcess(JavaThread* java_thread, jint depth, bool for_cont = false);
330 
331   // get a field descriptor for the specified class and field
332   static bool get_field_descriptor(Klass* k, jfieldID field, fieldDescriptor* fd);
333 
334   // check and skip frames hidden in mount/unmount transitions
335   static javaVFrame* check_and_skip_hidden_frames(bool is_in_VTMT, javaVFrame* jvf);
336   static javaVFrame* check_and_skip_hidden_frames(JavaThread* jt, javaVFrame* jvf);
337   static javaVFrame* check_and_skip_hidden_frames(oop vthread, javaVFrame* jvf);
338 
339   // check if virtual thread is not terminated (alive)
340   static bool is_vthread_alive(oop vt);
341 
342   // get virtual thread last java vframe
343   static javaVFrame* get_vthread_jvf(oop vthread);
344 
345   // get carrier thread last java vframe
346   static javaVFrame* get_last_java_vframe(JavaThread* jt, RegisterMap* reg_map);
347 
348   // get ordinary thread thread state
349   static jint get_thread_state(oop thread_oop, JavaThread* jt);
350 
351   // get virtual thread thread state
352   static jint get_vthread_state(oop thread_oop);
353 
354   // enumerates the live threads in the given thread group
355   static int get_live_threads(JavaThread* current_thread, Handle group_hdl, Handle **thread_objs_p);
356 
357   // enumerates the subgroups in the given thread group
358   static int get_subgroups(JavaThread* current_thread, Handle group_hdl, Handle **group_objs_p);
359 
360   // JVMTI API helper functions which are called when target thread is suspended
361   // or at safepoint / thread local handshake.
362   static jint get_frame_count(javaVFrame* jvf);
363   jvmtiError get_frame_count(JavaThread* java_thread, jint *count_ptr);
364   jvmtiError get_frame_count(oop frame_oop, jint *count_ptr);
365   jvmtiError get_frame_location(JavaThread* java_thread, jint depth,
366                                 jmethodID* method_ptr, jlocation* location_ptr);
367   jvmtiError get_frame_location(oop vthread_oop, jint depth,
368                                 jmethodID* method_ptr, jlocation* location_ptr);
369   jvmtiError get_object_monitor_usage(JavaThread *calling_thread,
370                                                     jobject object, jvmtiMonitorUsage* info_ptr);
371   jvmtiError get_stack_trace(javaVFrame *jvf,
372                              jint stack_depth, jint max_count,
373                              jvmtiFrameInfo* frame_buffer, jint* count_ptr);
374   jvmtiError get_stack_trace(JavaThread *java_thread,
375                                            jint stack_depth, jint max_count,
376                                            jvmtiFrameInfo* frame_buffer, jint* count_ptr);
377   jvmtiError get_current_contended_monitor(JavaThread* calling_thread, JavaThread *java_thread,
378                                            jobject *monitor_ptr, bool is_virtual);
379   jvmtiError get_owned_monitors(JavaThread* calling_thread, JavaThread* java_thread,
380                                 GrowableArray<jvmtiMonitorStackDepthInfo*> *owned_monitors_list);
381   jvmtiError get_owned_monitors(JavaThread *calling_thread, JavaThread* java_thread, javaVFrame* jvf,
382                           GrowableArray<jvmtiMonitorStackDepthInfo*> *owned_monitors_list);
383   static jvmtiError check_top_frame(Thread* current_thread, JavaThread* java_thread,
384                                     jvalue value, TosState tos, Handle* ret_ob_h);
385   jvmtiError force_early_return(JavaThread* java_thread, jvalue value, TosState tos);
386 };
387 
388 // This class is the only safe means of iterating through environments.
389 // Note that this iteratation includes invalid environments pending
390 // deallocation -- in fact, some uses depend on this behavior.
391 
392 class JvmtiEnvIterator : public StackObj {
393  private:
394   bool _entry_was_marked;
395  public:
396   JvmtiEnvIterator() {
397     if (Threads::number_of_threads() == 0) {
398       _entry_was_marked = false; // we are single-threaded, no need
399     } else {
400       Thread::current()->entering_jvmti_env_iteration();
401       _entry_was_marked = true;
402     }
403   }
404   ~JvmtiEnvIterator() {
405     if (_entry_was_marked) {
406       Thread::current()->leaving_jvmti_env_iteration();
407     }
408   }
409   JvmtiEnv* first()                 { return JvmtiEnvBase::head_environment(); }
410   JvmtiEnv* next(JvmtiEnvBase* env) { return env->next_environment(); }
411 };
412 
413 class JvmtiHandshakeClosure : public HandshakeClosure {
414  protected:
415   jvmtiError _result;
416  public:
417   JvmtiHandshakeClosure(const char* name)
418     : HandshakeClosure(name),
419       _result(JVMTI_ERROR_THREAD_NOT_ALIVE) {}
420   jvmtiError result() { return _result; }
421 };
422 
423 class SetForceEarlyReturn : public JvmtiHandshakeClosure {
424 private:
425   JvmtiThreadState* _state;
426   jvalue _value;
427   TosState _tos;
428 public:
429   SetForceEarlyReturn(JvmtiThreadState* state, jvalue value, TosState tos)
430     : JvmtiHandshakeClosure("SetForceEarlyReturn"),
431      _state(state),
432      _value(value),
433      _tos(tos) {}
434   void do_thread(Thread *target) {
435     doit(target, false /* self */);
436   }
437   void doit(Thread *target, bool self);
438 };
439 
440 // HandshakeClosure to update for pop top frame.
441 class UpdateForPopTopFrameClosure : public JvmtiHandshakeClosure {
442 private:
443   JvmtiThreadState* _state;
444 
445 public:
446   UpdateForPopTopFrameClosure(JvmtiThreadState* state)
447     : JvmtiHandshakeClosure("UpdateForPopTopFrame"),
448      _state(state) {}
449   void do_thread(Thread *target) {
450     doit(target, false /* self */);
451   }
452   void doit(Thread *target, bool self);
453 };
454 
455 // HandshakeClosure to set frame pop.
456 class SetFramePopClosure : public JvmtiHandshakeClosure {
457 private:
458   JvmtiEnv *_env;
459   JvmtiThreadState* _state;
460   jint _depth;
461 
462 public:
463   SetFramePopClosure(JvmtiEnv *env, JvmtiThreadState* state, jint depth)
464     : JvmtiHandshakeClosure("SetFramePop"),
465       _env(env),
466       _state(state),
467       _depth(depth) {}
468   void do_thread(Thread *target) {
469     doit(target, false /* self */);
470   }
471   void doit(Thread *target, bool self);
472 };
473 
474 // HandshakeClosure to get monitor information with stack depth.
475 class GetOwnedMonitorInfoClosure : public JvmtiHandshakeClosure {
476 private:
477   JavaThread* _calling_thread;
478   JvmtiEnv *_env;
479   GrowableArray<jvmtiMonitorStackDepthInfo*> *_owned_monitors_list;
480 
481 public:
482   GetOwnedMonitorInfoClosure(JavaThread* calling_thread, JvmtiEnv* env,
483                              GrowableArray<jvmtiMonitorStackDepthInfo*>* owned_monitor_list)
484     : JvmtiHandshakeClosure("GetOwnedMonitorInfo"),
485       _calling_thread(calling_thread),
486       _env(env),
487       _owned_monitors_list(owned_monitor_list) {}
488   void do_thread(Thread *target);
489 };
490 

491 // VM operation to get object monitor usage.
492 class VM_GetObjectMonitorUsage : public VM_Operation {
493 private:
494   JvmtiEnv *_env;
495   jobject _object;
496   JavaThread* _calling_thread;
497   jvmtiMonitorUsage* _info_ptr;
498   jvmtiError _result;
499 
500 public:
501   VM_GetObjectMonitorUsage(JvmtiEnv *env, JavaThread* calling_thread, jobject object, jvmtiMonitorUsage* info_ptr) {
502     _env = env;
503     _object = object;
504     _calling_thread = calling_thread;
505     _info_ptr = info_ptr;
506   }
507   VMOp_Type type() const { return VMOp_GetObjectMonitorUsage; }
508   jvmtiError result() { return _result; }
509   void doit() {
510     _result = ((JvmtiEnvBase*) _env)->get_object_monitor_usage(_calling_thread, _object, _info_ptr);
511   }
512 
513 };
514 
515 // HandshakeClosure to get current contended monitor.
516 class GetCurrentContendedMonitorClosure : public JvmtiHandshakeClosure {
517 private:
518   JavaThread *_calling_thread;
519   JvmtiEnv *_env;
520   jobject *_owned_monitor_ptr;
521 
522 public:
523   GetCurrentContendedMonitorClosure(JavaThread* calling_thread, JvmtiEnv *env, jobject *mon_ptr)
524     : JvmtiHandshakeClosure("GetCurrentContendedMonitor"),
525       _calling_thread(calling_thread),
526       _env(env),
527       _owned_monitor_ptr(mon_ptr) {}
528   void do_thread(Thread *target);
529 };
530 
531 // HandshakeClosure to get stack trace.
532 class GetStackTraceClosure : public JvmtiHandshakeClosure {
533 private:
534   JvmtiEnv *_env;
535   jint _start_depth;
536   jint _max_count;
537   jvmtiFrameInfo *_frame_buffer;
538   jint *_count_ptr;
539 
540 public:
541   GetStackTraceClosure(JvmtiEnv *env, jint start_depth, jint max_count,
542                        jvmtiFrameInfo* frame_buffer, jint* count_ptr)
543     : JvmtiHandshakeClosure("GetStackTrace"),
544       _env(env),
545       _start_depth(start_depth),
546       _max_count(max_count),
547       _frame_buffer(frame_buffer),
548       _count_ptr(count_ptr) {}
549   void do_thread(Thread *target);
550 };
551 
552 #ifdef ASSERT
553 // HandshakeClosure to print stack trace in JvmtiVTMTDisabler error handling
554 class PrintStackTraceClosure : public HandshakeClosure {
555  public:
556   static void do_thread_impl(Thread *target);
557 
558   PrintStackTraceClosure()
559       : HandshakeClosure("PrintStackTraceClosure") {}
560   void do_thread(Thread *target);
561 };
562 #endif
563 
564 // forward declaration
565 struct StackInfoNode;
566 
567 // Get stack trace at safepoint or at direct handshake.
568 class MultipleStackTracesCollector {
569 private:
570   friend class VM_GetThreadListStackTraces;
571   JvmtiEnv *_env;
572   jint _max_frame_count;
573   jvmtiStackInfo *_stack_info;
574   jvmtiError _result;
575   int _frame_count_total;
576   struct StackInfoNode *_head;
577 
578   JvmtiEnvBase *env()                 { return (JvmtiEnvBase *)_env; }
579   jint max_frame_count()              { return _max_frame_count; }
580   struct StackInfoNode *head()        { return _head; }
581   void set_head(StackInfoNode *head)  { _head = head; }
582 
583 public:
584   MultipleStackTracesCollector(JvmtiEnv *env, jint max_frame_count)
585     : _env(env),
586       _max_frame_count(max_frame_count),
587       _stack_info(NULL),
588       _result(JVMTI_ERROR_NONE),
589       _frame_count_total(0),
590       _head(NULL) {
591   }
592   void set_result(jvmtiError result)  { _result = result; }
593   void fill_frames(jthread jt, JavaThread *thr, oop thread_oop);
594   void allocate_and_fill_stacks(jint thread_count);
595   jvmtiStackInfo *stack_info()       { return _stack_info; }
596   jvmtiError result()                { return _result; }
597 };
598 
599 
600 // VM operation to get stack trace at safepoint.
601 class VM_GetAllStackTraces : public VM_Operation {
602 private:
603   JavaThread *_calling_thread;
604   jint _final_thread_count;
605   MultipleStackTracesCollector _collector;
606 
607 public:
608   VM_GetAllStackTraces(JvmtiEnv *env, JavaThread *calling_thread,
609                        jint max_frame_count)
610       : _calling_thread(calling_thread),
611         _final_thread_count(0),
612         _collector(env, max_frame_count) {
613   }
614   VMOp_Type type() const          { return VMOp_GetAllStackTraces; }
615   void doit();
616   jint final_thread_count()       { return _final_thread_count; }
617   jvmtiStackInfo *stack_info()    { return _collector.stack_info(); }
618   jvmtiError result()             { return _collector.result(); }
619 };
620 
621 // VM operation to get stack trace at safepoint.
622 class VM_GetThreadListStackTraces : public VM_Operation {
623 private:
624   jint _thread_count;
625   const jthread* _thread_list;
626   MultipleStackTracesCollector _collector;
627 
628   JvmtiEnvBase *env() { return _collector.env(); }
629 
630 public:
631   VM_GetThreadListStackTraces(JvmtiEnv *env, jint thread_count, const jthread* thread_list, jint max_frame_count)
632       : _thread_count(thread_count),
633         _thread_list(thread_list),
634         _collector(env, max_frame_count) {
635   }
636   VMOp_Type type() const { return VMOp_GetThreadListStackTraces; }
637   void doit();
638   jvmtiStackInfo *stack_info()    { return _collector.stack_info(); }
639   jvmtiError result()             { return _collector.result(); }
640 };
641 
642 class VM_VThreadGetStackTrace : public VM_Operation {
643 private:
644   JvmtiEnv *_env;
645   Handle _vthread_h;
646   jint _start_depth;
647   jint _max_count;
648   jvmtiFrameInfo* _frame_buffer;
649   jint* _count_ptr;
650   jvmtiError _result;
651 
652 public:
653   VM_VThreadGetStackTrace(JvmtiEnv *env, Handle vthread_h,
654                           jint start_depth, jint max_count,
655                           jvmtiFrameInfo* frame_buffer, jint* count_ptr)
656     : _vthread_h(vthread_h),
657       _start_depth(start_depth),
658       _max_count(max_count),
659       _frame_buffer(frame_buffer),
660       _count_ptr(count_ptr),
661       _result(JVMTI_ERROR_NONE)
662   {}
663 
664   VMOp_Type type() const { return VMOp_VThreadGetStackTrace; }
665   void doit();
666   jvmtiError result() { return _result; }
667 };
668 
669 class VM_VThreadGetFrameCount : public VM_Operation {
670 private:
671   JvmtiEnv *_env;
672   Handle _vthread_h;
673   jint* _count_ptr;
674   jvmtiError _result;
675 
676 public:
677   VM_VThreadGetFrameCount(JvmtiEnv *env, Handle vthread_h, jint* count_ptr)
678     : _vthread_h(vthread_h),
679       _count_ptr(count_ptr),
680       _result(JVMTI_ERROR_NONE)
681   {}
682 
683   VMOp_Type type() const { return VMOp_VThreadGetFrameCount; }
684   void doit();
685   jvmtiError result() { return _result; }
686 };
687 
688 // HandshakeClosure to get single stack trace.
689 class GetSingleStackTraceClosure : public HandshakeClosure {
690 private:
691   JavaThread *_calling_thread;
692   jthread _jthread;
693   MultipleStackTracesCollector _collector;
694 
695 public:
696   GetSingleStackTraceClosure(JvmtiEnv *env, JavaThread *calling_thread,
697                              jthread thread, jint max_frame_count)
698     : HandshakeClosure("GetSingleStackTrace"),
699       _calling_thread(calling_thread),
700       _jthread(thread),
701       _collector(env, max_frame_count) {
702   }
703   void do_thread(Thread *target);
704   jvmtiStackInfo *stack_info()    { return _collector.stack_info(); }
705   jvmtiError result()             { return _collector.result(); }
706 };
707 
708 // HandshakeClosure to count stack frames.
709 class GetFrameCountClosure : public JvmtiHandshakeClosure {
710 private:
711   JvmtiEnv *_env;

712   jint *_count_ptr;
713 
714 public:
715   GetFrameCountClosure(JvmtiEnv *env, jint *count_ptr)
716     : JvmtiHandshakeClosure("GetFrameCount"),
717       _env(env),

718       _count_ptr(count_ptr) {}
719   void do_thread(Thread *target);
720 };
721 
722 // HandshakeClosure to get frame location.
723 class GetFrameLocationClosure : public JvmtiHandshakeClosure {
724 private:
725   JvmtiEnv *_env;
726   jint _depth;
727   jmethodID* _method_ptr;
728   jlocation* _location_ptr;
729 
730 public:
731   GetFrameLocationClosure(JvmtiEnv *env, jint depth,
732                           jmethodID* method_ptr, jlocation* location_ptr)
733     : JvmtiHandshakeClosure("GetFrameLocation"),
734       _env(env),
735       _depth(depth),
736       _method_ptr(method_ptr),
737       _location_ptr(location_ptr) {}
738   void do_thread(Thread *target);
739 };
740 
741 // HandshakeClosure to get virtual thread monitor information with stack depth.
742 class VThreadGetOwnedMonitorInfoClosure : public HandshakeClosure {
743 private:
744   JvmtiEnv *_env;
745   Handle _vthread_h;
746   GrowableArray<jvmtiMonitorStackDepthInfo*> *_owned_monitors_list;
747   jvmtiError _result;
748 
749 public:
750   VThreadGetOwnedMonitorInfoClosure(JvmtiEnv* env,
751                             Handle vthread_h,
752                             GrowableArray<jvmtiMonitorStackDepthInfo*>* owned_monitors_list)
753     : HandshakeClosure("VThreadGetOwnedMonitorInfo"),
754       _env(env),
755       _vthread_h(vthread_h),
756       _owned_monitors_list(owned_monitors_list),
757       _result(JVMTI_ERROR_THREAD_NOT_ALIVE) {}
758 
759   void do_thread(Thread *target);
760   jvmtiError result() { return _result; }
761 };
762 
763 // HandshakeClosure to get virtual thread current contended monitor.
764 class VThreadGetCurrentContendedMonitorClosure : public HandshakeClosure {
765 private:
766   JvmtiEnv *_env;
767   Handle _vthread_h;
768   jobject *_owned_monitor_ptr;
769   jvmtiError _result;
770 
771 public:
772   VThreadGetCurrentContendedMonitorClosure(JvmtiEnv *env, Handle vthread_h, jobject *mon_ptr)
773     : HandshakeClosure("VThreadGetCurrentContendedMonitor"),
774       _env(env),
775       _vthread_h(vthread_h),
776       _owned_monitor_ptr(mon_ptr),
777       _result(JVMTI_ERROR_THREAD_NOT_ALIVE) {}
778   jvmtiError result() { return _result; }
779   void do_thread(Thread *target);
780 };
781 
782 // HandshakeClosure to get virtual thread thread at safepoint.
783 class VThreadGetThreadClosure : public HandshakeClosure {
784 private:
785   Handle _vthread_h;
786   jthread* _carrier_thread_ptr;
787   jvmtiError _result;
788 
789 public:
790   VThreadGetThreadClosure(Handle vthread_h, jthread* carrier_thread_ptr)
791     : HandshakeClosure("VThreadGetThread"),
792       _vthread_h(vthread_h),
793       _carrier_thread_ptr(carrier_thread_ptr),
794       _result(JVMTI_ERROR_NONE) {}
795 
796   void do_thread(Thread *target);
797   jvmtiError result() { return _result; }
798 };
799 
800 // HandshakeClosure to get virtual thread stack trace at safepoint.
801 class VThreadGetStackTraceClosure : public HandshakeClosure {
802 private:
803   JvmtiEnv *_env;
804   Handle _vthread_h;
805   jint _start_depth;
806   jint _max_count;
807   jvmtiFrameInfo *_frame_buffer;
808   jint *_count_ptr;
809   jvmtiError _result;
810 
811 public:
812   VThreadGetStackTraceClosure(JvmtiEnv *env, Handle vthread_h,
813                               jint start_depth, jint max_count,
814                               jvmtiFrameInfo* frame_buffer, jint* count_ptr)
815     : HandshakeClosure("VThreadGetStackTrace"),
816       _env(env),
817       _vthread_h(vthread_h),
818        _start_depth(start_depth),
819       _max_count(max_count),
820       _frame_buffer(frame_buffer),
821       _count_ptr(count_ptr),
822       _result(JVMTI_ERROR_NONE) {}
823 
824   void do_thread(Thread *target);
825   jvmtiError result() { return _result; }
826 };
827 
828 // HandshakeClosure to count virtual thread stack frames at safepoint.
829 class VThreadGetFrameCountClosure : public HandshakeClosure {
830 private:
831   JvmtiEnv *_env;
832   Handle _vthread_h;
833   jint *_count_ptr;
834   jvmtiError _result;
835 
836 public:
837   VThreadGetFrameCountClosure(JvmtiEnv *env, Handle vthread_h, jint *count_ptr)
838     : HandshakeClosure("VThreadGetFrameCount"),
839       _env(env), _vthread_h(vthread_h), _count_ptr(count_ptr),
840       _result(JVMTI_ERROR_NONE) {}
841 
842   void do_thread(Thread *target);
843   jvmtiError result() { return _result; }
844 };
845 
846 // HandshakeClosure get to virtual thread frame location at safepoint.
847 class VThreadGetFrameLocationClosure : public HandshakeClosure {
848 private:
849   JvmtiEnv *_env;
850   Handle _vthread_h;
851   jint _depth;
852   jmethodID* _method_ptr;
853   jlocation* _location_ptr;
854   jvmtiError _result;
855 
856 public:
857   VThreadGetFrameLocationClosure(JvmtiEnv *env, Handle vthread_h, jint depth,
858                                  jmethodID* method_ptr, jlocation* location_ptr)
859     : HandshakeClosure("VThreadGetFrameLocation"),
860       _env(env),
861       _vthread_h(vthread_h),
862       _depth(depth),
863       _method_ptr(method_ptr),
864       _location_ptr(location_ptr),
865       _result(JVMTI_ERROR_NONE) {}
866 
867   void do_thread(Thread *target);
868   jvmtiError result() { return _result; }
869 };
870 
871 // HandshakeClosure to get virtual thread state at safepoint.
872 class VThreadGetThreadStateClosure : public HandshakeClosure {
873 private:
874   Handle _vthread_h;
875   jint *_state_ptr;
876   jvmtiError _result;
877 
878 public:
879   VThreadGetThreadStateClosure(Handle vthread_h, jint *state_ptr)
880     : HandshakeClosure("VThreadGetThreadState"),
881       _vthread_h(vthread_h),
882       _state_ptr(state_ptr),
883       _result(JVMTI_ERROR_NONE) {}
884 
885   void do_thread(Thread *target);
886   jvmtiError result() { return _result; }
887 };
888 
889 // ResourceTracker
890 //
891 // ResourceTracker works a little like a ResourceMark. All allocates
892 // using the resource tracker are recorded. If an allocate using the
893 // resource tracker fails the destructor will free any resources
894 // that were allocated using the tracker.
895 // The motive for this class is to avoid messy error recovery code
896 // in situations where multiple allocations are done in sequence. If
897 // the second or subsequent allocation fails it avoids any code to
898 // release memory allocated in the previous calls.
899 //
900 // Usage :-
901 //   ResourceTracker rt(env);
902 //   :
903 //   err = rt.allocate(1024, &ptr);
904 
905 class ResourceTracker : public StackObj {
906  private:
907   JvmtiEnv* _env;
908   GrowableArray<unsigned char*> *_allocations;
909   bool _failed;
910  public:
911   ResourceTracker(JvmtiEnv* env);
912   ~ResourceTracker();
913   jvmtiError allocate(jlong size, unsigned char** mem_ptr);
914   unsigned char* allocate(jlong size);
915   char* strdup(const char* str);
916 };
917 
918 
919 // Jvmti monitor closure to collect off stack monitors.
920 class JvmtiMonitorClosure: public MonitorClosure {
921  private:
922   JavaThread *_calling_thread;
923   GrowableArray<jvmtiMonitorStackDepthInfo*> *_owned_monitors_list;
924   jvmtiError _error;
925   JvmtiEnvBase *_env;
926 
927  public:
928   JvmtiMonitorClosure(JavaThread *calling_thread,
929                       GrowableArray<jvmtiMonitorStackDepthInfo*> *owned_monitors,
930                       JvmtiEnvBase *env) {
931     _calling_thread = calling_thread;
932     _owned_monitors_list = owned_monitors;
933     _error = JVMTI_ERROR_NONE;
934     _env = env;
935   }
936   void do_monitor(ObjectMonitor* mon);
937   jvmtiError error() { return _error;}
938 };
939 
940 
941 // Jvmti module closure to collect all modules loaded to the system.
942 class JvmtiModuleClosure : public StackObj {
943 private:
944   static GrowableArray<OopHandle> *_tbl; // Protected with Module_lock
945 
946   static void do_module(ModuleEntry* entry);
947 public:
948   jvmtiError get_all_modules(JvmtiEnv* env, jint* module_count_ptr, jobject** modules_ptr);
949 };
950 
951 #endif // SHARE_PRIMS_JVMTIENVBASE_HPP
--- EOF ---