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