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