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