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_class_hook_env() {
 166     return get_capabilities()->can_generate_early_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
 176   // not yet been deallocated.  As a result, this test should only be used as an
 177   // optimization for the no environment case.
 178   static bool environments_might_exist() {
 179     return head_environment() != NULL;
 180   }
 181 
 182   static void check_for_periodic_clean_up();
 183 
 184   JvmtiEnvEventEnable *env_event_enable() {
 185     return &_env_event_enable;
 186   }
 187 
 188   jvmtiError allocate(jlong size, unsigned char** mem_ptr) {
 189     if (size < 0) {
 190       return JVMTI_ERROR_ILLEGAL_ARGUMENT;
 191     }
 192     if (size == 0) {
 193       *mem_ptr = NULL;
 194     } else {
 195       *mem_ptr = (unsigned char *)os::malloc((size_t)size, mtInternal);
 196       if (*mem_ptr == NULL) {
 197         return JVMTI_ERROR_OUT_OF_MEMORY;
 198       }
 199     }
 200     return JVMTI_ERROR_NONE;
 201   }
 202 
 203   jvmtiError deallocate(unsigned char* mem) {
 204     if (mem != NULL) {
 205       os::free(mem);
 206     }
 207     return JVMTI_ERROR_NONE;
 208   }
 209 
 210 
 211   // Memory functions
 212   unsigned char* jvmtiMalloc(jlong size);  // don't use this - call allocate
 213 
 214   // method to create a local handle
 215   jobject jni_reference(Handle hndl) {
 216     return JNIHandles::make_local(hndl());
 217   }
 218 
 219   // method to create a local handle.
 220   // This function allows caller to specify which
 221   // threads local handle table to use.
 222   jobject jni_reference(JavaThread *thread, Handle hndl) {
 223     return JNIHandles::make_local(thread, hndl());
 224   }
 225 
 226   // method to destroy a local handle
 227   void destroy_jni_reference(jobject jobj) {
 228     JNIHandles::destroy_local(jobj);
 229   }
 230 
 231   // method to destroy a local handle.
 232   // This function allows caller to specify which
 233   // threads local handle table to use although currently it is
 234   // not used.
 235   void destroy_jni_reference(JavaThread *thread, jobject jobj) {
 236     destroy_jni_reference(jobj);
 237   }
 238 
 239   jvmtiEnv* jvmti_external() { return &_jvmti_external; };
 240 
 241 // Event Dispatch
 242 
 243   bool has_callback(jvmtiEvent event_type) {
 244     assert(event_type >= JVMTI_MIN_EVENT_TYPE_VAL &&
 245            event_type <= JVMTI_MAX_EVENT_TYPE_VAL, "checking");
 246     return ((void**)&_event_callbacks)[event_type-JVMTI_MIN_EVENT_TYPE_VAL] != NULL;
 247   }
 248 
 249   jvmtiEventCallbacks* callbacks() {
 250     return &_event_callbacks;
 251   }
 252 
 253   jvmtiExtEventCallbacks* ext_callbacks() {
 254     return &_ext_event_callbacks;
 255   }
 256 
 257   void set_tag_map(JvmtiTagMap* tag_map) {
 258     _tag_map = tag_map;
 259   }
 260 
 261   JvmtiTagMap* tag_map() {
 262     return _tag_map;
 263   }
 264 
 265 
 266   // return true if event is enabled globally or for any thread
 267   // True only if there is a callback for it.
 268   bool is_enabled(jvmtiEvent event_type) {
 269     return _env_event_enable.is_enabled(event_type);
 270   }
 271 
 272 // Random Utilities
 273 
 274  protected:
 275   // helper methods for creating arrays of global JNI Handles from local Handles
 276   // allocated into environment specific storage
 277   jobject * new_jobjectArray(int length, Handle *handles);
 278   jthread * new_jthreadArray(int length, Handle *handles);
 279   jthreadGroup * new_jthreadGroupArray(int length, Handle *handles);
 280 
 281   // convert from JNIHandle to JavaThread *
 282   JavaThread  * get_JavaThread(jthread jni_thread);
 283 
 284   // convert to a jni jclass from a non-null Klass*
 285   jclass get_jni_class_non_null(Klass* k);
 286 
 287   jint count_locked_objects(JavaThread *java_thread, Handle hobj);
 288   jvmtiError get_locked_objects_in_frame(JavaThread *calling_thread,
 289                                    JavaThread* java_thread,
 290                                    javaVFrame *jvf,
 291                                    GrowableArray<jvmtiMonitorStackDepthInfo*>* owned_monitors_list,
 292                                    jint depth);
 293   vframe* vframeFor(JavaThread* java_thread, jint depth);
 294 
 295  public:
 296   // get a field descriptor for the specified class and field
 297   static bool get_field_descriptor(Klass* k, jfieldID field, fieldDescriptor* fd);
 298   // test for suspend - most (all?) of these should go away
 299   static bool is_thread_fully_suspended(JavaThread *thread,
 300                                         bool wait_for_suspend,
 301                                         uint32_t *bits);
 302 
 303 
 304   // JVMTI API helper functions which are called at safepoint or thread is suspended.
 305   jvmtiError get_frame_count(JvmtiThreadState *state, jint *count_ptr);
 306   jvmtiError get_frame_location(JavaThread* java_thread, jint depth,
 307                                               jmethodID* method_ptr, jlocation* location_ptr);
 308   jvmtiError get_object_monitor_usage(JavaThread *calling_thread,
 309                                                     jobject object, jvmtiMonitorUsage* info_ptr);
 310   jvmtiError get_stack_trace(JavaThread *java_thread,
 311                                            jint stack_depth, jint max_count,
 312                                            jvmtiFrameInfo* frame_buffer, jint* count_ptr);
 313   jvmtiError get_current_contended_monitor(JavaThread *calling_thread,
 314                                                          JavaThread *java_thread,
 315                                                          jobject *monitor_ptr);
 316   jvmtiError get_owned_monitors(JavaThread *calling_thread, JavaThread* java_thread,
 317                           GrowableArray<jvmtiMonitorStackDepthInfo*> *owned_monitors_list);
 318   jvmtiError check_top_frame(JavaThread* current_thread, JavaThread* java_thread,
 319                              jvalue value, TosState tos, Handle* ret_ob_h);
 320   jvmtiError force_early_return(JavaThread* java_thread, jvalue value, TosState tos);
 321 };
 322 
 323 // This class is the only safe means of iterating through environments.
 324 // Note that this iteratation includes invalid environments pending
 325 // deallocation -- in fact, some uses depend on this behavior.
 326 
 327 class JvmtiEnvIterator : public StackObj {
 328  private:
 329   bool _entry_was_marked;
 330  public:
 331   JvmtiEnvIterator() {
 332     if (Threads::number_of_threads() == 0) {
 333       _entry_was_marked = false; // we are single-threaded, no need
 334     } else {
 335       Thread::current()->entering_jvmti_env_iteration();
 336       _entry_was_marked = true;
 337     }
 338   }
 339   ~JvmtiEnvIterator() {
 340     if (_entry_was_marked) {
 341       Thread::current()->leaving_jvmti_env_iteration();
 342     }
 343   }
 344   JvmtiEnv* first()                 { return JvmtiEnvBase::head_environment(); }
 345   JvmtiEnv* next(JvmtiEnvBase* env) { return env->next_environment(); }
 346 };
 347 
 348 // VM operation to update for pop top frame.
 349 class VM_UpdateForPopTopFrame : public VM_Operation {
 350 private:
 351   JvmtiThreadState* _state;
 352   jvmtiError _result;
 353 
 354 public:
 355   VM_UpdateForPopTopFrame(JvmtiThreadState* state) {
 356     _state = state;
 357     _result = JVMTI_ERROR_NONE;
 358   }
 359   VMOp_Type type() const { return VMOp_UpdateForPopTopFrame; }
 360   jvmtiError result() { return _result; }
 361   void doit() {
 362     JavaThread* jt = _state->get_thread();
 363     if (Threads::includes(jt) && !jt->is_exiting() && jt->threadObj() != NULL) {
 364       _state->update_for_pop_top_frame();
 365     } else {
 366       _result = JVMTI_ERROR_THREAD_NOT_ALIVE;
 367     }
 368   }
 369 };
 370 
 371 // VM operation to set frame pop.
 372 class VM_SetFramePop : public VM_Operation {
 373 private:
 374   JvmtiEnv *_env;
 375   JvmtiThreadState* _state;
 376   jint _depth;
 377   jvmtiError _result;
 378 
 379 public:
 380   VM_SetFramePop(JvmtiEnv *env, JvmtiThreadState* state, jint depth) {
 381     _env = env;
 382     _state = state;
 383     _depth = depth;
 384     _result = JVMTI_ERROR_NONE;
 385   }
 386   // Nested operation must be allowed for the VM_EnterInterpOnlyMode that is
 387   // called from the JvmtiEventControllerPrivate::recompute_thread_enabled.
 388   bool allow_nested_vm_operations() const { return true; }
 389   VMOp_Type type() const { return VMOp_SetFramePop; }
 390   jvmtiError result() { return _result; }
 391   void doit() {
 392     JavaThread* jt = _state->get_thread();
 393     if (Threads::includes(jt) && !jt->is_exiting() && jt->threadObj() != NULL) {
 394       int frame_number = _state->count_frames() - _depth;
 395       _state->env_thread_state((JvmtiEnvBase*)_env)->set_frame_pop(frame_number);
 396     } else {
 397       _result = JVMTI_ERROR_THREAD_NOT_ALIVE;
 398     }
 399   }
 400 };
 401 
 402 
 403 // VM operation to get monitor information with stack depth.
 404 class VM_GetOwnedMonitorInfo : public VM_Operation {
 405 private:
 406   JvmtiEnv *_env;
 407   JavaThread* _calling_thread;
 408   JavaThread *_java_thread;
 409   jvmtiError _result;
 410   GrowableArray<jvmtiMonitorStackDepthInfo*> *_owned_monitors_list;
 411 
 412 public:
 413   VM_GetOwnedMonitorInfo(JvmtiEnv* env, JavaThread* calling_thread,
 414                                    JavaThread* java_thread,
 415                                    GrowableArray<jvmtiMonitorStackDepthInfo*>* owned_monitor_list) {
 416     _env = env;
 417     _calling_thread = calling_thread;
 418     _java_thread = java_thread;
 419     _owned_monitors_list = owned_monitor_list;
 420     _result = JVMTI_ERROR_NONE;
 421   }
 422   VMOp_Type type() const { return VMOp_GetOwnedMonitorInfo; }
 423   void doit() {
 424     _result = JVMTI_ERROR_THREAD_NOT_ALIVE;
 425     if (Threads::includes(_java_thread) && !_java_thread->is_exiting()
 426                                         && _java_thread->threadObj() != NULL) {
 427       _result = ((JvmtiEnvBase *)_env)->get_owned_monitors(_calling_thread, _java_thread,
 428                                                             _owned_monitors_list);
 429     }
 430   }
 431   jvmtiError result() { return _result; }
 432 };
 433 
 434 
 435 // VM operation to get object monitor usage.
 436 class VM_GetObjectMonitorUsage : public VM_Operation {
 437 private:
 438   JvmtiEnv *_env;
 439   jobject _object;
 440   JavaThread* _calling_thread;
 441   jvmtiMonitorUsage* _info_ptr;
 442   jvmtiError _result;
 443 
 444 public:
 445   VM_GetObjectMonitorUsage(JvmtiEnv *env, JavaThread* calling_thread, jobject object, jvmtiMonitorUsage* info_ptr) {
 446     _env = env;
 447     _object = object;
 448     _calling_thread = calling_thread;
 449     _info_ptr = info_ptr;
 450   }
 451   VMOp_Type type() const { return VMOp_GetObjectMonitorUsage; }
 452   jvmtiError result() { return _result; }
 453   void doit() {
 454     _result = ((JvmtiEnvBase*) _env)->get_object_monitor_usage(_calling_thread, _object, _info_ptr);
 455   }
 456 
 457 };
 458 
 459 // VM operation to get current contended monitor.
 460 class VM_GetCurrentContendedMonitor : public VM_Operation {
 461 private:
 462   JvmtiEnv *_env;
 463   JavaThread *_calling_thread;
 464   JavaThread *_java_thread;
 465   jobject *_owned_monitor_ptr;
 466   jvmtiError _result;
 467 
 468 public:
 469   VM_GetCurrentContendedMonitor(JvmtiEnv *env, JavaThread *calling_thread, JavaThread *java_thread, jobject *mon_ptr) {
 470     _env = env;
 471     _calling_thread = calling_thread;
 472     _java_thread = java_thread;
 473     _owned_monitor_ptr = mon_ptr;
 474   }
 475   VMOp_Type type() const { return VMOp_GetCurrentContendedMonitor; }
 476   jvmtiError result() { return _result; }
 477   void doit() {
 478     _result = JVMTI_ERROR_THREAD_NOT_ALIVE;
 479     if (Threads::includes(_java_thread) && !_java_thread->is_exiting() &&
 480         _java_thread->threadObj() != NULL) {
 481       _result = ((JvmtiEnvBase *)_env)->get_current_contended_monitor(_calling_thread,_java_thread,_owned_monitor_ptr);
 482     }
 483   }
 484 };
 485 
 486 // VM operation to get stack trace at safepoint.
 487 class VM_GetStackTrace : public VM_Operation {
 488 private:
 489   JvmtiEnv *_env;
 490   JavaThread *_java_thread;
 491   jint _start_depth;
 492   jint _max_count;
 493   jvmtiFrameInfo *_frame_buffer;
 494   jint *_count_ptr;
 495   jvmtiError _result;
 496 
 497 public:
 498   VM_GetStackTrace(JvmtiEnv *env, JavaThread *java_thread,
 499                    jint start_depth, jint max_count,
 500                    jvmtiFrameInfo* frame_buffer, jint* count_ptr) {
 501     _env = env;
 502     _java_thread = java_thread;
 503     _start_depth = start_depth;
 504     _max_count = max_count;
 505     _frame_buffer = frame_buffer;
 506     _count_ptr = count_ptr;
 507   }
 508   jvmtiError result() { return _result; }
 509   VMOp_Type type() const { return VMOp_GetStackTrace; }
 510   void doit() {
 511     _result = JVMTI_ERROR_THREAD_NOT_ALIVE;
 512     if (Threads::includes(_java_thread) && !_java_thread->is_exiting()
 513                                         && _java_thread->threadObj() != NULL) {
 514       _result = ((JvmtiEnvBase *)_env)->get_stack_trace(_java_thread,
 515                                                         _start_depth, _max_count,
 516                                                         _frame_buffer, _count_ptr);
 517     }
 518   }
 519 };
 520 
 521 // forward declaration
 522 struct StackInfoNode;
 523 
 524 // VM operation to get stack trace at safepoint.
 525 class VM_GetMultipleStackTraces : public VM_Operation {
 526 private:
 527   JvmtiEnv *_env;
 528   jint _max_frame_count;
 529   jvmtiStackInfo *_stack_info;
 530   jvmtiError _result;
 531   int _frame_count_total;
 532   struct StackInfoNode *_head;
 533 
 534   JvmtiEnvBase *env()                 { return (JvmtiEnvBase *)_env; }
 535   jint max_frame_count()              { return _max_frame_count; }
 536   struct StackInfoNode *head()        { return _head; }
 537   void set_head(StackInfoNode *head)  { _head = head; }
 538 
 539 protected:
 540   void set_result(jvmtiError result)  { _result = result; }
 541   void fill_frames(jthread jt, JavaThread *thr, oop thread_oop);
 542   void allocate_and_fill_stacks(jint thread_count);
 543 
 544 public:
 545   VM_GetMultipleStackTraces(JvmtiEnv *env, jint max_frame_count) {
 546     _env = env;
 547     _max_frame_count = max_frame_count;
 548     _frame_count_total = 0;
 549     _head = NULL;
 550     _result = JVMTI_ERROR_NONE;
 551   }
 552   VMOp_Type type() const             { return VMOp_GetMultipleStackTraces; }
 553   jvmtiStackInfo *stack_info()       { return _stack_info; }
 554   jvmtiError result()                { return _result; }
 555 };
 556 
 557 
 558 // VM operation to get stack trace at safepoint.
 559 class VM_GetAllStackTraces : public VM_GetMultipleStackTraces {
 560 private:
 561   JavaThread *_calling_thread;
 562   jint _final_thread_count;
 563 
 564 public:
 565   VM_GetAllStackTraces(JvmtiEnv *env, JavaThread *calling_thread,
 566                        jint max_frame_count)
 567       : VM_GetMultipleStackTraces(env, max_frame_count) {
 568     _calling_thread = calling_thread;
 569   }
 570   VMOp_Type type() const          { return VMOp_GetAllStackTraces; }
 571   void doit();
 572   jint final_thread_count()       { return _final_thread_count; }
 573 };
 574 
 575 // VM operation to get stack trace at safepoint.
 576 class VM_GetThreadListStackTraces : public VM_GetMultipleStackTraces {
 577 private:
 578   jint _thread_count;
 579   const jthread* _thread_list;
 580 
 581 public:
 582   VM_GetThreadListStackTraces(JvmtiEnv *env, jint thread_count, const jthread* thread_list, jint max_frame_count)
 583       : VM_GetMultipleStackTraces(env, max_frame_count) {
 584     _thread_count = thread_count;
 585     _thread_list = thread_list;
 586   }
 587   VMOp_Type type() const { return VMOp_GetThreadListStackTraces; }
 588   void doit();
 589 };
 590 
 591 
 592 // VM operation to count stack frames at safepoint.
 593 class VM_GetFrameCount : public VM_Operation {
 594 private:
 595   JvmtiEnv *_env;
 596   JvmtiThreadState *_state;
 597   jint *_count_ptr;
 598   jvmtiError _result;
 599 
 600 public:
 601   VM_GetFrameCount(JvmtiEnv *env, JvmtiThreadState *state, jint *count_ptr) {
 602     _env = env;
 603     _state = state;
 604     _count_ptr = count_ptr;
 605   }
 606   VMOp_Type type() const { return VMOp_GetFrameCount; }
 607   jvmtiError result()    { return _result; }
 608   void doit() {
 609     _result = JVMTI_ERROR_THREAD_NOT_ALIVE;
 610     JavaThread* jt = _state->get_thread();
 611     if (Threads::includes(jt) && !jt->is_exiting() && jt->threadObj() != NULL) {
 612       _result = ((JvmtiEnvBase*)_env)->get_frame_count(_state, _count_ptr);
 613     }
 614   }
 615 };
 616 
 617 // VM operation to frame location at safepoint.
 618 class VM_GetFrameLocation : public VM_Operation {
 619 private:
 620   JvmtiEnv *_env;
 621   JavaThread* _java_thread;
 622   jint _depth;
 623   jmethodID* _method_ptr;
 624   jlocation* _location_ptr;
 625   jvmtiError _result;
 626 
 627 public:
 628   VM_GetFrameLocation(JvmtiEnv *env, JavaThread* java_thread, jint depth,
 629                       jmethodID* method_ptr, jlocation* location_ptr) {
 630     _env = env;
 631     _java_thread = java_thread;
 632     _depth = depth;
 633     _method_ptr = method_ptr;
 634     _location_ptr = location_ptr;
 635   }
 636   VMOp_Type type() const { return VMOp_GetFrameLocation; }
 637   jvmtiError result()    { return _result; }
 638   void doit() {
 639     _result = JVMTI_ERROR_THREAD_NOT_ALIVE;
 640     if (Threads::includes(_java_thread) && !_java_thread->is_exiting() &&
 641         _java_thread->threadObj() != NULL) {
 642       _result = ((JvmtiEnvBase*)_env)->get_frame_location(_java_thread, _depth,
 643                                                           _method_ptr, _location_ptr);
 644     }
 645   }
 646 };
 647 
 648 
 649 // ResourceTracker
 650 //
 651 // ResourceTracker works a little like a ResourceMark. All allocates
 652 // using the resource tracker are recorded. If an allocate using the
 653 // resource tracker fails the destructor will free any resources
 654 // that were allocated using the tracker.
 655 // The motive for this class is to avoid messy error recovery code
 656 // in situations where multiple allocations are done in sequence. If
 657 // the second or subsequent allocation fails it avoids any code to
 658 // release memory allocated in the previous calls.
 659 //
 660 // Usage :-
 661 //   ResourceTracker rt(env);
 662 //   :
 663 //   err = rt.allocate(1024, &ptr);
 664 
 665 class ResourceTracker : public StackObj {
 666  private:
 667   JvmtiEnv* _env;
 668   GrowableArray<unsigned char*> *_allocations;
 669   bool _failed;
 670  public:
 671   ResourceTracker(JvmtiEnv* env);
 672   ~ResourceTracker();
 673   jvmtiError allocate(jlong size, unsigned char** mem_ptr);
 674   unsigned char* allocate(jlong size);
 675   char* strdup(const char* str);
 676 };
 677 
 678 
 679 // Jvmti monitor closure to collect off stack monitors.
 680 class JvmtiMonitorClosure: public MonitorClosure {
 681  private:
 682   JavaThread *_java_thread;
 683   JavaThread *_calling_thread;
 684   GrowableArray<jvmtiMonitorStackDepthInfo*> *_owned_monitors_list;
 685   jvmtiError _error;
 686   JvmtiEnvBase *_env;
 687 
 688  public:
 689   JvmtiMonitorClosure(JavaThread* thread, JavaThread *calling_thread,
 690                       GrowableArray<jvmtiMonitorStackDepthInfo*> *owned_monitors,
 691                       JvmtiEnvBase *env) {
 692     _java_thread = thread;
 693     _calling_thread = calling_thread;
 694     _owned_monitors_list = owned_monitors;
 695     _error = JVMTI_ERROR_NONE;
 696     _env = env;
 697   }
 698   void do_monitor(ObjectMonitor* mon);
 699   jvmtiError error() { return _error;}
 700 };
 701 
 702 
 703 // Jvmti module closure to collect all modules loaded to the system.
 704 class JvmtiModuleClosure : public StackObj {
 705 private:
 706   static GrowableArray<jobject> *_tbl; // Protected with Module_lock
 707 
 708   static void do_module(ModuleEntry* entry) {
 709     assert_locked_or_safepoint(Module_lock);
 710     jobject module = entry->module();
 711     guarantee(module != NULL, "module object is NULL");
 712     _tbl->push(module);
 713   }
 714 
 715 public:
 716   jvmtiError get_all_modules(JvmtiEnv* env, jint* module_count_ptr, jobject** modules_ptr);
 717 };
 718 
 719 #endif // SHARE_VM_PRIMS_JVMTIENVBASE_HPP