src/share/vm/prims/jvmtiEnvBase.hpp

Sat, 01 Dec 2007 00:00:00 +0000

author
duke
date
Sat, 01 Dec 2007 00:00:00 +0000
changeset 435
a61af66fc99e
child 1046
2f716c0acb64
child 1556
98cd9901c161
permissions
-rw-r--r--

Initial load

     1 /*
     2  * Copyright 2003-2006 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
    21  * have any questions.
    22  *
    23  */
    25 #ifndef _JAVA_JVMTIENVBASE_H_
    26 #define _JAVA_JVMTIENVBASE_H_
    28 //
    29 // Forward Declarations
    30 //
    32 class JvmtiEnv;
    33 class JvmtiThreadState;
    34 class JvmtiRawMonitor; // for jvmtiEnv.hpp
    35 class JvmtiEventControllerPrivate;
    36 class JvmtiTagMap;
    40 // One JvmtiEnv object is created per jvmti attachment;
    41 // done via JNI GetEnv() call. Multiple attachments are
    42 // allowed in jvmti.
    44 class JvmtiEnvBase : public CHeapObj {
    46  private:
    48   static JvmtiEnvBase*     _head_environment;  // head of environment list
    50   static bool              _globally_initialized;
    51   static jvmtiPhase        _phase;
    52   static volatile int      _dying_thread_env_iteration_count;
    54  public:
    56   enum {
    57     JDK15_JVMTI_VERSION = JVMTI_VERSION_1_0 +  33,  /* version: 1.0.33  */
    58     JDK16_JVMTI_VERSION = JVMTI_VERSION_1_1 + 102   /* version: 1.1.102 */
    59   };
    61   static jvmtiPhase  get_phase()                    { return _phase; }
    62   static void  set_phase(jvmtiPhase phase)          { _phase = phase; }
    63   static bool is_vm_live()                          { return _phase == JVMTI_PHASE_LIVE; }
    65   static void entering_dying_thread_env_iteration() { ++_dying_thread_env_iteration_count; }
    66   static void leaving_dying_thread_env_iteration()  { --_dying_thread_env_iteration_count; }
    67   static bool is_inside_dying_thread_env_iteration(){ return _dying_thread_env_iteration_count > 0; }
    69  private:
    71   enum {
    72       JVMTI_MAGIC    = 0x71EE,
    73       DISPOSED_MAGIC = 0xDEFC,
    74       BAD_MAGIC      = 0xDEAD
    75   };
    77   jvmtiEnv _jvmti_external;
    78   jint _magic;
    79   JvmtiEnvBase* _next;
    80   bool _is_retransformable;
    81   const void *_env_local_storage;     // per env agent allocated data.
    82   jvmtiEventCallbacks _event_callbacks;
    83   jvmtiExtEventCallbacks _ext_event_callbacks;
    84   JvmtiTagMap* _tag_map;
    85   JvmtiEnvEventEnable _env_event_enable;
    86   jvmtiCapabilities _current_capabilities;
    87   jvmtiCapabilities _prohibited_capabilities;
    88   volatile bool _class_file_load_hook_ever_enabled;
    89   static volatile bool _needs_clean_up;
    90   char** _native_method_prefixes;
    91   int    _native_method_prefix_count;
    93  protected:
    94   JvmtiEnvBase();
    95   ~JvmtiEnvBase();
    96   void dispose();
    97   void env_dispose();
    99   void set_env_local_storage(const void* data)     { _env_local_storage = data; }
   100   const void* get_env_local_storage()              { return _env_local_storage; }
   102   void record_class_file_load_hook_enabled();
   103   void record_first_time_class_file_load_hook_enabled();
   105   char** get_native_method_prefixes()              { return _native_method_prefixes; }
   106   int    get_native_method_prefix_count()          { return _native_method_prefix_count; }
   107   jvmtiError set_native_method_prefixes(jint prefix_count, char** prefixes);
   109  private:
   110   friend class JvmtiEventControllerPrivate;
   111   void initialize();
   112   void set_event_callbacks(const jvmtiEventCallbacks* callbacks, jint size_of_callbacks);
   113   static void globally_initialize();
   114   static void periodic_clean_up();
   116   friend class JvmtiEnvIterator;
   117   JvmtiEnv* next_environment()                     { return (JvmtiEnv*)_next; }
   118   void set_next_environment(JvmtiEnvBase* env)     { _next = env; }
   119   static JvmtiEnv* head_environment()              { return (JvmtiEnv*)_head_environment; }
   121  public:
   123   bool is_valid()                                  { return _magic == JVMTI_MAGIC; }
   125   bool is_retransformable()                        { return _is_retransformable; }
   127   static ByteSize jvmti_external_offset() {
   128     return byte_offset_of(JvmtiEnvBase, _jvmti_external);
   129   };
   131   static JvmtiEnv* JvmtiEnv_from_jvmti_env(jvmtiEnv *env) {
   132     return (JvmtiEnv*)((intptr_t)env - in_bytes(jvmti_external_offset()));
   133   };
   135   jvmtiCapabilities *get_capabilities()             { return &_current_capabilities; }
   137   jvmtiCapabilities *get_prohibited_capabilities()  { return &_prohibited_capabilities; }
   139   static char** get_all_native_method_prefixes(int* count_ptr);
   141   // This test will answer true when all environments have been disposed and some have
   142   // not yet been deallocated.  As a result, this test should only be used as an
   143   // optimization for the no environment case.
   144   static bool environments_might_exist() {
   145     return head_environment() != NULL;
   146   }
   148   static void check_for_periodic_clean_up();
   150   JvmtiEnvEventEnable *env_event_enable() {
   151     return &_env_event_enable;
   152   }
   154   jvmtiError allocate(jlong size, unsigned char** mem_ptr) {
   155     if (size < 0) {
   156       return JVMTI_ERROR_ILLEGAL_ARGUMENT;
   157     }
   158     if (size == 0) {
   159       *mem_ptr = NULL;
   160     } else {
   161       *mem_ptr = (unsigned char *)os::malloc((size_t)size);
   162       if (*mem_ptr == NULL) {
   163         return JVMTI_ERROR_OUT_OF_MEMORY;
   164       }
   165     }
   166     return JVMTI_ERROR_NONE;
   167   }
   169   jvmtiError deallocate(unsigned char* mem) {
   170     if (mem != NULL) {
   171       os::free(mem);
   172     }
   173     return JVMTI_ERROR_NONE;
   174   }
   177   // Memory functions
   178   unsigned char* jvmtiMalloc(jlong size);  // don't use this - call allocate
   180   // method to create a local handle
   181   jobject jni_reference(Handle hndl) {
   182     return JNIHandles::make_local(hndl());
   183   }
   185   // method to create a local handle.
   186   // This function allows caller to specify which
   187   // threads local handle table to use.
   188   jobject jni_reference(JavaThread *thread, Handle hndl) {
   189     return JNIHandles::make_local(thread, hndl());
   190   }
   192   // method to destroy a local handle
   193   void destroy_jni_reference(jobject jobj) {
   194     JNIHandles::destroy_local(jobj);
   195   }
   197   // method to destroy a local handle.
   198   // This function allows caller to specify which
   199   // threads local handle table to use although currently it is
   200   // not used.
   201   void destroy_jni_reference(JavaThread *thread, jobject jobj) {
   202     destroy_jni_reference(jobj);
   203   }
   205   jvmtiEnv* jvmti_external() { return &_jvmti_external; };
   207 // Event Dispatch
   209   bool has_callback(jvmtiEvent event_type) {
   210     assert(event_type >= JVMTI_MIN_EVENT_TYPE_VAL &&
   211            event_type <= JVMTI_MAX_EVENT_TYPE_VAL, "checking");
   212     return ((void**)&_event_callbacks)[event_type-JVMTI_MIN_EVENT_TYPE_VAL] != NULL;
   213   }
   215   jvmtiEventCallbacks* callbacks() {
   216     return &_event_callbacks;
   217   }
   219   jvmtiExtEventCallbacks* ext_callbacks() {
   220     return &_ext_event_callbacks;
   221   }
   223   void set_tag_map(JvmtiTagMap* tag_map) {
   224     _tag_map = tag_map;
   225   }
   227   JvmtiTagMap* tag_map() {
   228     return _tag_map;
   229   }
   232   // return true if event is enabled globally or for any thread
   233   // True only if there is a callback for it.
   234   bool is_enabled(jvmtiEvent event_type) {
   235     return _env_event_enable.is_enabled(event_type);
   236   }
   238 // Random Utilities
   240  protected:
   241   // helper methods for creating arrays of global JNI Handles from local Handles
   242   // allocated into environment specific storage
   243   jobject * new_jobjectArray(int length, Handle *handles);
   244   jthread * new_jthreadArray(int length, Handle *handles);
   245   jthreadGroup * new_jthreadGroupArray(int length, Handle *handles);
   247   // convert from JNIHandle to JavaThread *
   248   JavaThread  * get_JavaThread(jthread jni_thread);
   250   // convert to a jni jclass from a non-null klassOop
   251   jclass get_jni_class_non_null(klassOop k);
   253   void update_klass_field_access_flag(fieldDescriptor *fd);
   255   jint count_locked_objects(JavaThread *java_thread, Handle hobj);
   256   jvmtiError get_locked_objects_in_frame(JavaThread *calling_thread,
   257                                    JavaThread* java_thread,
   258                                    javaVFrame *jvf,
   259                                    GrowableArray<jvmtiMonitorStackDepthInfo*>* owned_monitors_list,
   260                                    jint depth);
   261   vframe* vframeFor(JavaThread* java_thread, jint depth);
   263  public:
   264   // get a field descriptor for the specified class and field
   265   static bool get_field_descriptor(klassOop k, jfieldID field, fieldDescriptor* fd);
   266   // test for suspend - most (all?) of these should go away
   267   static bool is_thread_fully_suspended(JavaThread *thread,
   268                                         bool wait_for_suspend,
   269                                         uint32_t *bits);
   272   // JVMTI API helper functions which are called at safepoint or thread is suspended.
   273   jvmtiError get_frame_count(JvmtiThreadState *state, jint *count_ptr);
   274   jvmtiError get_frame_location(JavaThread* java_thread, jint depth,
   275                                               jmethodID* method_ptr, jlocation* location_ptr);
   276   jvmtiError get_object_monitor_usage(JavaThread *calling_thread,
   277                                                     jobject object, jvmtiMonitorUsage* info_ptr);
   278   jvmtiError get_stack_trace(JavaThread *java_thread,
   279                                            jint stack_depth, jint max_count,
   280                                            jvmtiFrameInfo* frame_buffer, jint* count_ptr);
   281   jvmtiError get_current_contended_monitor(JavaThread *calling_thread,
   282                                                          JavaThread *java_thread,
   283                                                          jobject *monitor_ptr);
   284   jvmtiError get_owned_monitors(JavaThread *calling_thread, JavaThread* java_thread,
   285                           GrowableArray<jvmtiMonitorStackDepthInfo*> *owned_monitors_list);
   286   jvmtiError check_top_frame(JavaThread* current_thread, JavaThread* java_thread,
   287                              jvalue value, TosState tos, Handle* ret_ob_h);
   288   jvmtiError force_early_return(JavaThread* java_thread, jvalue value, TosState tos);
   289 };
   291 // This class is the only safe means of iterating through environments.
   292 // Note that this iteratation includes invalid environments pending
   293 // deallocation -- in fact, some uses depend on this behavior.
   295 class JvmtiEnvIterator : public StackObj {
   296  private:
   297   bool _entry_was_marked;
   298  public:
   299   JvmtiEnvIterator() {
   300     if (Threads::number_of_threads() == 0) {
   301       _entry_was_marked = false; // we are single-threaded, no need
   302     } else {
   303       Thread::current()->entering_jvmti_env_iteration();
   304       _entry_was_marked = true;
   305     }
   306   }
   307   ~JvmtiEnvIterator() {
   308     if (_entry_was_marked) {
   309       Thread::current()->leaving_jvmti_env_iteration();
   310     }
   311   }
   312   JvmtiEnv* first()                 { return JvmtiEnvBase::head_environment(); }
   313   JvmtiEnv* next(JvmtiEnvBase* env) { return env->next_environment(); }
   314 };
   317 // VM operation to get monitor information with stack depth.
   318 class VM_GetOwnedMonitorInfo : public VM_Operation {
   319 private:
   320   JvmtiEnv *_env;
   321   JavaThread* _calling_thread;
   322   JavaThread *_java_thread;
   323   jvmtiError _result;
   324   GrowableArray<jvmtiMonitorStackDepthInfo*> *_owned_monitors_list;
   326 public:
   327   VM_GetOwnedMonitorInfo(JvmtiEnv* env, JavaThread* calling_thread,
   328                                    JavaThread* java_thread,
   329                                    GrowableArray<jvmtiMonitorStackDepthInfo*>* owned_monitor_list) {
   330     _env = env;
   331     _calling_thread = calling_thread;
   332     _java_thread = java_thread;
   333     _owned_monitors_list = owned_monitor_list;
   334     _result = JVMTI_ERROR_NONE;
   335   }
   336   VMOp_Type type() const { return VMOp_GetOwnedMonitorInfo; }
   337   void doit() {
   338     ((JvmtiEnvBase *)_env)->get_owned_monitors(_calling_thread, _java_thread,
   339                                                          _owned_monitors_list);
   340   }
   341   jvmtiError result() { return _result; }
   342 };
   345 // VM operation to get object monitor usage.
   346 class VM_GetObjectMonitorUsage : public VM_Operation {
   347 private:
   348   JvmtiEnv *_env;
   349   jobject _object;
   350   JavaThread* _calling_thread;
   351   jvmtiMonitorUsage* _info_ptr;
   352   jvmtiError _result;
   354 public:
   355   VM_GetObjectMonitorUsage(JvmtiEnv *env, JavaThread* calling_thread, jobject object, jvmtiMonitorUsage* info_ptr) {
   356     _env = env;
   357     _object = object;
   358     _calling_thread = calling_thread;
   359     _info_ptr = info_ptr;
   360   }
   361   VMOp_Type type() const { return VMOp_GetObjectMonitorUsage; }
   362   jvmtiError result() { return _result; }
   363   void doit() {
   364     _result = ((JvmtiEnvBase*) _env)->get_object_monitor_usage(_calling_thread, _object, _info_ptr);
   365   }
   367 };
   369 // VM operation to get current contended monitor.
   370 class VM_GetCurrentContendedMonitor : public VM_Operation {
   371 private:
   372   JvmtiEnv *_env;
   373   JavaThread *_calling_thread;
   374   JavaThread *_java_thread;
   375   jobject *_owned_monitor_ptr;
   376   jvmtiError _result;
   378 public:
   379   VM_GetCurrentContendedMonitor(JvmtiEnv *env, JavaThread *calling_thread, JavaThread *java_thread, jobject *mon_ptr) {
   380     _env = env;
   381     _calling_thread = calling_thread;
   382     _java_thread = java_thread;
   383     _owned_monitor_ptr = mon_ptr;
   384   }
   385   VMOp_Type type() const { return VMOp_GetCurrentContendedMonitor; }
   386   jvmtiError result() { return _result; }
   387   void doit() {
   388     _result = ((JvmtiEnvBase *)_env)->get_current_contended_monitor(_calling_thread,_java_thread,_owned_monitor_ptr);
   389   }
   390 };
   392 // VM operation to get stack trace at safepoint.
   393 class VM_GetStackTrace : public VM_Operation {
   394 private:
   395   JvmtiEnv *_env;
   396   JavaThread *_java_thread;
   397   jint _start_depth;
   398   jint _max_count;
   399   jvmtiFrameInfo *_frame_buffer;
   400   jint *_count_ptr;
   401   jvmtiError _result;
   403 public:
   404   VM_GetStackTrace(JvmtiEnv *env, JavaThread *java_thread,
   405                    jint start_depth, jint max_count,
   406                    jvmtiFrameInfo* frame_buffer, jint* count_ptr) {
   407     _env = env;
   408     _java_thread = java_thread;
   409     _start_depth = start_depth;
   410     _max_count = max_count;
   411     _frame_buffer = frame_buffer;
   412     _count_ptr = count_ptr;
   413   }
   414   jvmtiError result() { return _result; }
   415   VMOp_Type type() const { return VMOp_GetStackTrace; }
   416   void doit() {
   417     _result = ((JvmtiEnvBase *)_env)->get_stack_trace(_java_thread,
   418                                                       _start_depth, _max_count,
   419                                                       _frame_buffer, _count_ptr);
   420   }
   421 };
   423 // forward declaration
   424 struct StackInfoNode;
   426 // VM operation to get stack trace at safepoint.
   427 class VM_GetMultipleStackTraces : public VM_Operation {
   428 private:
   429   JvmtiEnv *_env;
   430   jint _max_frame_count;
   431   jvmtiStackInfo *_stack_info;
   432   jvmtiError _result;
   433   int _frame_count_total;
   434   struct StackInfoNode *_head;
   436   JvmtiEnvBase *env()                 { return (JvmtiEnvBase *)_env; }
   437   jint max_frame_count()              { return _max_frame_count; }
   438   struct StackInfoNode *head()        { return _head; }
   439   void set_head(StackInfoNode *head)  { _head = head; }
   441 protected:
   442   void set_result(jvmtiError result)  { _result = result; }
   443   void fill_frames(jthread jt, JavaThread *thr, oop thread_oop);
   444   void allocate_and_fill_stacks(jint thread_count);
   446 public:
   447   VM_GetMultipleStackTraces(JvmtiEnv *env, jint max_frame_count) {
   448     _env = env;
   449     _max_frame_count = max_frame_count;
   450     _frame_count_total = 0;
   451     _head = NULL;
   452     _result = JVMTI_ERROR_NONE;
   453   }
   454   VMOp_Type type() const             { return VMOp_GetMultipleStackTraces; }
   455   jvmtiStackInfo *stack_info()       { return _stack_info; }
   456   jvmtiError result()                { return _result; }
   457 };
   460 // VM operation to get stack trace at safepoint.
   461 class VM_GetAllStackTraces : public VM_GetMultipleStackTraces {
   462 private:
   463   JavaThread *_calling_thread;
   464   jint _final_thread_count;
   466 public:
   467   VM_GetAllStackTraces(JvmtiEnv *env, JavaThread *calling_thread,
   468                        jint max_frame_count)
   469       : VM_GetMultipleStackTraces(env, max_frame_count) {
   470     _calling_thread = calling_thread;
   471   }
   472   VMOp_Type type() const          { return VMOp_GetAllStackTraces; }
   473   void doit();
   474   jint final_thread_count()       { return _final_thread_count; }
   475 };
   477 // VM operation to get stack trace at safepoint.
   478 class VM_GetThreadListStackTraces : public VM_GetMultipleStackTraces {
   479 private:
   480   jint _thread_count;
   481   const jthread* _thread_list;
   483 public:
   484   VM_GetThreadListStackTraces(JvmtiEnv *env, jint thread_count, const jthread* thread_list, jint max_frame_count)
   485       : VM_GetMultipleStackTraces(env, max_frame_count) {
   486     _thread_count = thread_count;
   487     _thread_list = thread_list;
   488   }
   489   VMOp_Type type() const { return VMOp_GetThreadListStackTraces; }
   490   void doit();
   491 };
   494 // VM operation to count stack frames at safepoint.
   495 class VM_GetFrameCount : public VM_Operation {
   496 private:
   497   JvmtiEnv *_env;
   498   JvmtiThreadState *_state;
   499   jint *_count_ptr;
   500   jvmtiError _result;
   502 public:
   503   VM_GetFrameCount(JvmtiEnv *env, JvmtiThreadState *state, jint *count_ptr) {
   504     _env = env;
   505     _state = state;
   506     _count_ptr = count_ptr;
   507   }
   508   VMOp_Type type() const { return VMOp_GetFrameCount; }
   509   jvmtiError result()    { return _result; }
   510   void doit() {
   511     _result = ((JvmtiEnvBase*)_env)->get_frame_count(_state, _count_ptr);
   512   }
   513 };
   515 // VM operation to frame location at safepoint.
   516 class VM_GetFrameLocation : public VM_Operation {
   517 private:
   518   JvmtiEnv *_env;
   519   JavaThread* _java_thread;
   520   jint _depth;
   521   jmethodID* _method_ptr;
   522   jlocation* _location_ptr;
   523   jvmtiError _result;
   525 public:
   526   VM_GetFrameLocation(JvmtiEnv *env, JavaThread* java_thread, jint depth,
   527                       jmethodID* method_ptr, jlocation* location_ptr) {
   528     _env = env;
   529     _java_thread = java_thread;
   530     _depth = depth;
   531     _method_ptr = method_ptr;
   532     _location_ptr = location_ptr;
   533   }
   534   VMOp_Type type() const { return VMOp_GetFrameLocation; }
   535   jvmtiError result()    { return _result; }
   536   void doit() {
   537     _result = ((JvmtiEnvBase*)_env)->get_frame_location(_java_thread, _depth,
   538                                                         _method_ptr, _location_ptr);
   539   }
   540 };
   543 // ResourceTracker
   544 //
   545 // ResourceTracker works a little like a ResourceMark. All allocates
   546 // using the resource tracker are recorded. If an allocate using the
   547 // resource tracker fails the destructor will free any resources
   548 // that were allocated using the tracker.
   549 // The motive for this class is to avoid messy error recovery code
   550 // in situations where multiple allocations are done in sequence. If
   551 // the second or subsequent allocation fails it avoids any code to
   552 // release memory allocated in the previous calls.
   553 //
   554 // Usage :-
   555 //   ResourceTracker rt(env);
   556 //   :
   557 //   err = rt.allocate(1024, &ptr);
   559 class ResourceTracker : public StackObj {
   560  private:
   561   JvmtiEnv* _env;
   562   GrowableArray<unsigned char*> *_allocations;
   563   bool _failed;
   564  public:
   565   ResourceTracker(JvmtiEnv* env);
   566   ~ResourceTracker();
   567   jvmtiError allocate(jlong size, unsigned char** mem_ptr);
   568   unsigned char* allocate(jlong size);
   569   char* strdup(const char* str);
   570 };
   573 // Jvmti monitor closure to collect off stack monitors.
   574 class JvmtiMonitorClosure: public MonitorClosure {
   575  private:
   576   JavaThread *_java_thread;
   577   JavaThread *_calling_thread;
   578   GrowableArray<jvmtiMonitorStackDepthInfo*> *_owned_monitors_list;
   579   jvmtiError _error;
   580   JvmtiEnvBase *_env;
   582  public:
   583   JvmtiMonitorClosure(JavaThread* thread, JavaThread *calling_thread,
   584                       GrowableArray<jvmtiMonitorStackDepthInfo*> *owned_monitors,
   585                       JvmtiEnvBase *env) {
   586     _java_thread = thread;
   587     _calling_thread = calling_thread;
   588     _owned_monitors_list = owned_monitors;
   589     _error = JVMTI_ERROR_NONE;
   590     _env = env;
   591   }
   592   void do_monitor(ObjectMonitor* mon);
   593   jvmtiError error() { return _error;}
   594 };
   596 #endif   /* _JAVA_JVMTIENVBASE_H_ */

mercurial