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