1 /*
2  * Copyright (c) 1998, 2020, 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_PRIMS_JVMTIEXPORT_HPP
26 #define SHARE_PRIMS_JVMTIEXPORT_HPP
27 
28 #include "jvmtifiles/jvmti.h"
29 #include "memory/allocation.hpp"
30 #include "memory/iterator.hpp"
31 #include "oops/oop.hpp"
32 #include "oops/oopHandle.hpp"
33 #include "oops/oopsHierarchy.hpp"
34 #include "runtime/frame.hpp"
35 #include "runtime/handles.hpp"
36 #include "utilities/globalDefinitions.hpp"
37 #include "utilities/growableArray.hpp"
38 #include "utilities/macros.hpp"
39 
40 // Must be included after jvmti.h.
41 #include "jvmticmlr.h"
42 
43 // Forward declarations
44 
45 class JvmtiEventControllerPrivate;
46 class JvmtiManageCapabilities;
47 class JvmtiEnv;
48 class JvmtiThreadState;
49 
50 class OopStorage;
51 
52 #define JVMTI_SUPPORT_FLAG(key)                                           \
53   private:                                                                \
54   static bool  _##key;                                                    \
55   public:                                                                 \
56   inline static void set_##key(bool on) {                                 \
57     JVMTI_ONLY(_##key = (on != 0));                                       \
58     NOT_JVMTI(report_unsupported(on));                                    \
59   }                                                                       \
60   inline static bool key() {                                              \
61     JVMTI_ONLY(return _##key);                                            \
62     NOT_JVMTI(return false);                                              \
63   }
64 
65 
66 // This class contains the JVMTI interface for the rest of hotspot.
67 //
68 class JvmtiExport : public AllStatic {
69   friend class VMStructs;
70   friend class CompileReplay;
71 
72  private:
73 
74 #if INCLUDE_JVMTI
75   static int         _field_access_count;
76   static int         _field_modification_count;
77 
78   static bool        _can_access_local_variables;
79   static bool        _can_hotswap_or_post_breakpoint;
80   static bool        _can_modify_any_class;
81   static bool        _can_walk_any_space;
82 #endif // INCLUDE_JVMTI
83 
84   JVMTI_SUPPORT_FLAG(can_get_source_debug_extension)
85   JVMTI_SUPPORT_FLAG(can_maintain_original_method_order)
86   JVMTI_SUPPORT_FLAG(can_post_interpreter_events)
87   JVMTI_SUPPORT_FLAG(can_post_on_exceptions)
88   JVMTI_SUPPORT_FLAG(can_post_breakpoint)
89   JVMTI_SUPPORT_FLAG(can_post_field_access)
90   JVMTI_SUPPORT_FLAG(can_post_field_modification)
91   JVMTI_SUPPORT_FLAG(can_post_method_entry)
92   JVMTI_SUPPORT_FLAG(can_post_method_exit)
93   JVMTI_SUPPORT_FLAG(can_pop_frame)
94   JVMTI_SUPPORT_FLAG(can_force_early_return)
95 
96   JVMTI_SUPPORT_FLAG(early_vmstart_recorded)
97   JVMTI_SUPPORT_FLAG(can_get_owned_monitor_info) // includes can_get_owned_monitor_stack_depth_info
98 
99   friend class JvmtiEventControllerPrivate;  // should only modify these flags
100   JVMTI_SUPPORT_FLAG(should_post_single_step)
101   JVMTI_SUPPORT_FLAG(should_post_field_access)
102   JVMTI_SUPPORT_FLAG(should_post_field_modification)
103   JVMTI_SUPPORT_FLAG(should_post_class_load)
104   JVMTI_SUPPORT_FLAG(should_post_class_prepare)
105   JVMTI_SUPPORT_FLAG(should_post_class_unload)
106   JVMTI_SUPPORT_FLAG(should_post_native_method_bind)
107   JVMTI_SUPPORT_FLAG(should_post_compiled_method_load)
108   JVMTI_SUPPORT_FLAG(should_post_compiled_method_unload)
109   JVMTI_SUPPORT_FLAG(should_post_dynamic_code_generated)
110   JVMTI_SUPPORT_FLAG(should_post_monitor_contended_enter)
111   JVMTI_SUPPORT_FLAG(should_post_monitor_contended_entered)
112   JVMTI_SUPPORT_FLAG(should_post_monitor_wait)
113   JVMTI_SUPPORT_FLAG(should_post_monitor_waited)
114   JVMTI_SUPPORT_FLAG(should_post_data_dump)
115   JVMTI_SUPPORT_FLAG(should_post_garbage_collection_start)
116   JVMTI_SUPPORT_FLAG(should_post_garbage_collection_finish)
117   JVMTI_SUPPORT_FLAG(should_post_on_exceptions)
118 
119   // ------ the below maybe don't have to be (but are for now)
120   // fixed conditions here ------------
121   // any events can be enabled
122   JVMTI_SUPPORT_FLAG(should_post_thread_life)
123   JVMTI_SUPPORT_FLAG(should_post_object_free)
124   JVMTI_SUPPORT_FLAG(should_post_resource_exhausted)
125 
126   // we are holding objects on the heap - need to talk to GC - e.g.
127   // breakpoint info
128   JVMTI_SUPPORT_FLAG(should_clean_up_heap_objects)
129   JVMTI_SUPPORT_FLAG(should_post_vm_object_alloc)
130   JVMTI_SUPPORT_FLAG(should_post_sampled_object_alloc)
131 
132   // If flag cannot be implemented, give an error if on=true
133   static void report_unsupported(bool on);
134 
135   // these should only be called by the friend class
136   friend class JvmtiManageCapabilities;
set_can_modify_any_class(bool on)137   inline static void set_can_modify_any_class(bool on) {
138     JVMTI_ONLY(_can_modify_any_class = (on != 0);)
139   }
set_can_access_local_variables(bool on)140   inline static void set_can_access_local_variables(bool on) {
141     JVMTI_ONLY(_can_access_local_variables = (on != 0);)
142   }
set_can_hotswap_or_post_breakpoint(bool on)143   inline static void set_can_hotswap_or_post_breakpoint(bool on) {
144     JVMTI_ONLY(_can_hotswap_or_post_breakpoint = (on != 0);)
145   }
set_can_walk_any_space(bool on)146   inline static void set_can_walk_any_space(bool on) {
147     JVMTI_ONLY(_can_walk_any_space = (on != 0);)
148   }
149 
150   enum {
151     JVMTI_VERSION_MASK   = 0x70000000,
152     JVMTI_VERSION_VALUE  = 0x30000000,
153     JVMDI_VERSION_VALUE  = 0x20000000
154   };
155 
156   static void post_field_modification(JavaThread *thread, Method* method, address location,
157                                       Klass* field_klass, Handle object, jfieldID field,
158                                       char sig_type, jvalue *value);
159 
160 
161   // posts a DynamicCodeGenerated event (internal/private implementation).
162   // The public post_dynamic_code_generated* functions make use of the
163   // internal implementation.  Also called from JvmtiDeferredEvent::post()
164   static void post_dynamic_code_generated_internal(const char *name, const void *code_begin, const void *code_end) NOT_JVMTI_RETURN;
165 
166   static void post_class_unload_internal(const char *name) NOT_JVMTI_RETURN;
167 
168   static void initialize_oop_storage() NOT_JVMTI_RETURN;
169   static OopStorage* jvmti_oop_storage();
170   static OopStorage* weak_tag_storage();
171  private:
172 
173   // GenerateEvents support to allow posting of CompiledMethodLoad and
174   // DynamicCodeGenerated events for a given environment.
175   friend class JvmtiCodeBlobEvents;
176 
177   static void post_dynamic_code_generated(JvmtiEnv* env, const char *name, const void *code_begin,
178                                           const void *code_end) NOT_JVMTI_RETURN;
179 
180   // This flag indicates whether RedefineClasses() has ever redefined
181   // one or more classes during the lifetime of the VM. The flag should
182   // only be set by the friend class and can be queried by other sub
183   // systems as needed to relax invariant checks.
184   static uint64_t _redefinition_count;
185   friend class VM_RedefineClasses;
increment_redefinition_count()186   inline static void increment_redefinition_count() {
187     JVMTI_ONLY(_redefinition_count++;)
188   }
189   // Flag to indicate if the compiler has recorded all dependencies. When the
190   // can_redefine_classes capability is enabled in the OnLoad phase then the compiler
191   // records all dependencies from startup. However if the capability is first
192   // enabled some time later then the dependencies recorded by the compiler
193   // are incomplete. This flag is used by RedefineClasses to know if the
194   // dependency information is complete or not.
195   static bool _all_dependencies_are_recorded;
196 
197   static void post_method_exit_inner(JavaThread* thread,
198                                      methodHandle& mh,
199                                      JvmtiThreadState *state,
200                                      bool exception_exit,
201                                      frame current_frame,
202                                      jvalue& value);
203 
204  public:
has_redefined_a_class()205   inline static bool has_redefined_a_class() {
206     JVMTI_ONLY(return _redefinition_count != 0);
207     NOT_JVMTI(return false);
208   }
209 
210   // Only set in safepoint, so no memory ordering needed.
redefinition_count()211   inline static uint64_t redefinition_count() {
212     JVMTI_ONLY(return _redefinition_count);
213     NOT_JVMTI(return 0);
214   }
215 
all_dependencies_are_recorded()216   inline static bool all_dependencies_are_recorded() {
217     return _all_dependencies_are_recorded;
218   }
219 
set_all_dependencies_are_recorded(bool on)220   inline static void set_all_dependencies_are_recorded(bool on) {
221     _all_dependencies_are_recorded = (on != 0);
222   }
223 
224   // Add read edges to the unnamed modules of the bootstrap and app class loaders
225   static void add_default_read_edges(Handle h_module, TRAPS) NOT_JVMTI_RETURN;
226 
227   // Add a read edge to the module
228   static jvmtiError add_module_reads(Handle module, Handle to_module, TRAPS);
229 
230   // Updates a module to export a package
231   static jvmtiError add_module_exports(Handle module, Handle pkg_name, Handle to_module, TRAPS);
232 
233   // Updates a module to open a package
234   static jvmtiError add_module_opens(Handle module, Handle pkg_name, Handle to_module, TRAPS);
235 
236   // Add a used service to the module
237   static jvmtiError add_module_uses(Handle module, Handle service, TRAPS);
238 
239   // Add a service provider to the module
240   static jvmtiError add_module_provides(Handle module, Handle service, Handle impl_class, TRAPS);
241 
242   // let JVMTI know that the JVM_OnLoad code is running
243   static void enter_onload_phase() NOT_JVMTI_RETURN;
244 
245   // let JVMTI know that the VM isn't up yet (and JVM_OnLoad code isn't running)
246   static void enter_primordial_phase() NOT_JVMTI_RETURN;
247 
248   // let JVMTI know that the VM isn't up yet but JNI is live
249   static void enter_early_start_phase() NOT_JVMTI_RETURN;
250   static void enter_start_phase() NOT_JVMTI_RETURN;
251 
252   // let JVMTI know that the VM is fully up and running now
253   static void enter_live_phase() NOT_JVMTI_RETURN;
254 
255   // ------ can_* conditions (below) are set at OnLoad and never changed ------------
can_modify_any_class()256   inline static bool can_modify_any_class()                       {
257     JVMTI_ONLY(return _can_modify_any_class);
258     NOT_JVMTI(return false);
259   }
can_access_local_variables()260   inline static bool can_access_local_variables()                 {
261     JVMTI_ONLY(return _can_access_local_variables);
262     NOT_JVMTI(return false);
263   }
can_hotswap_or_post_breakpoint()264   inline static bool can_hotswap_or_post_breakpoint()             {
265     JVMTI_ONLY(return _can_hotswap_or_post_breakpoint);
266     NOT_JVMTI(return false);
267   }
can_walk_any_space()268   inline static bool can_walk_any_space()                         {
269     JVMTI_ONLY(return _can_walk_any_space);
270     NOT_JVMTI(return false);
271   }
272 
273   // field access management
274   static address  get_field_access_count_addr() NOT_JVMTI_RETURN_(0);
275 
276   // field modification management
277   static address  get_field_modification_count_addr() NOT_JVMTI_RETURN_(0);
278 
279   // -----------------
280 
is_jvmti_version(jint version)281   static bool is_jvmti_version(jint version)                      {
282     JVMTI_ONLY(return (version & JVMTI_VERSION_MASK) == JVMTI_VERSION_VALUE);
283     NOT_JVMTI(return false);
284   }
is_jvmdi_version(jint version)285   static bool is_jvmdi_version(jint version)                      {
286     JVMTI_ONLY(return (version & JVMTI_VERSION_MASK) == JVMDI_VERSION_VALUE);
287     NOT_JVMTI(return false);
288   }
289   static jint get_jvmti_interface(JavaVM *jvm, void **penv, jint version) NOT_JVMTI_RETURN_(0);
290   static void decode_version_values(jint version, int * major, int * minor,
291                                     int * micro) NOT_JVMTI_RETURN;
292 
293   // single stepping management methods
294   static void at_single_stepping_point(JavaThread *thread, Method* method, address location) NOT_JVMTI_RETURN;
295   static void expose_single_stepping(JavaThread *thread) NOT_JVMTI_RETURN;
296   static bool hide_single_stepping(JavaThread *thread) NOT_JVMTI_RETURN_(false);
297 
298   // Methods that notify the debugger that something interesting has happened in the VM.
299   static void post_early_vm_start        () NOT_JVMTI_RETURN;
300   static void post_vm_start              () NOT_JVMTI_RETURN;
301   static void post_vm_initialized        () NOT_JVMTI_RETURN;
302   static void post_vm_death              () NOT_JVMTI_RETURN;
303 
304   static void post_single_step           (JavaThread *thread, Method* method, address location) NOT_JVMTI_RETURN;
305   static void post_raw_breakpoint        (JavaThread *thread, Method* method, address location) NOT_JVMTI_RETURN;
306 
307   static void post_exception_throw       (JavaThread *thread, Method* method, address location, oop exception) NOT_JVMTI_RETURN;
308   static void notice_unwind_due_to_exception (JavaThread *thread, Method* method, address location, oop exception, bool in_handler_frame) NOT_JVMTI_RETURN;
309 
310   static oop jni_GetField_probe          (JavaThread *thread, jobject jobj,
311     oop obj, Klass* klass, jfieldID fieldID, bool is_static)
312     NOT_JVMTI_RETURN_(NULL);
313   static oop jni_GetField_probe_nh       (JavaThread *thread, jobject jobj,
314     oop obj, Klass* klass, jfieldID fieldID, bool is_static)
315     NOT_JVMTI_RETURN_(NULL);
316   static void post_field_access_by_jni   (JavaThread *thread, oop obj,
317     Klass* klass, jfieldID fieldID, bool is_static) NOT_JVMTI_RETURN;
318   static void post_field_access          (JavaThread *thread, Method* method,
319     address location, Klass* field_klass, Handle object, jfieldID field) NOT_JVMTI_RETURN;
320   static oop jni_SetField_probe          (JavaThread *thread, jobject jobj,
321     oop obj, Klass* klass, jfieldID fieldID, bool is_static, char sig_type,
322     jvalue *value) NOT_JVMTI_RETURN_(NULL);
323   static oop jni_SetField_probe_nh       (JavaThread *thread, jobject jobj,
324     oop obj, Klass* klass, jfieldID fieldID, bool is_static, char sig_type,
325     jvalue *value) NOT_JVMTI_RETURN_(NULL);
326   static void post_field_modification_by_jni(JavaThread *thread, oop obj,
327     Klass* klass, jfieldID fieldID, bool is_static, char sig_type,
328     jvalue *value);
329   static void post_raw_field_modification(JavaThread *thread, Method* method,
330     address location, Klass* field_klass, Handle object, jfieldID field,
331     char sig_type, jvalue *value) NOT_JVMTI_RETURN;
332 
333   static void post_method_entry          (JavaThread *thread, Method* method, frame current_frame) NOT_JVMTI_RETURN;
334   static void post_method_exit           (JavaThread *thread, Method* method, frame current_frame) NOT_JVMTI_RETURN;
335 
336   static void post_class_load            (JavaThread *thread, Klass* klass) NOT_JVMTI_RETURN;
337   static void post_class_unload          (Klass* klass) NOT_JVMTI_RETURN;
338   static void post_class_prepare         (JavaThread *thread, Klass* klass) NOT_JVMTI_RETURN;
339 
340   static void post_thread_start          (JavaThread *thread) NOT_JVMTI_RETURN;
341   static void post_thread_end            (JavaThread *thread) NOT_JVMTI_RETURN;
342 
343   // Support for java.lang.instrument agent loading.
344   static bool _should_post_class_file_load_hook;
set_should_post_class_file_load_hook(bool on)345   inline static void set_should_post_class_file_load_hook(bool on)     { _should_post_class_file_load_hook = on;  }
should_post_class_file_load_hook()346   inline static bool should_post_class_file_load_hook()           {
347     JVMTI_ONLY(return _should_post_class_file_load_hook);
348     NOT_JVMTI(return false;)
349   }
350   static bool is_early_phase() NOT_JVMTI_RETURN_(false);
351   static bool has_early_class_hook_env() NOT_JVMTI_RETURN_(false);
352   // Return true if the class was modified by the hook.
353   static bool post_class_file_load_hook(Symbol* h_name, Handle class_loader,
354                                         Handle h_protection_domain,
355                                         unsigned char **data_ptr, unsigned char **end_ptr,
356                                         JvmtiCachedClassFileData **cache_ptr) NOT_JVMTI_RETURN_(false);
357   static void post_native_method_bind(Method* method, address* function_ptr) NOT_JVMTI_RETURN;
358   static void post_compiled_method_load(JvmtiEnv* env, nmethod *nm) NOT_JVMTI_RETURN;
359   static void post_compiled_method_load(nmethod *nm) NOT_JVMTI_RETURN;
360   static void post_dynamic_code_generated(const char *name, const void *code_begin, const void *code_end) NOT_JVMTI_RETURN;
361 
362   // used to post a CompiledMethodUnload event
363   static void post_compiled_method_unload(jmethodID mid, const void *code_begin) NOT_JVMTI_RETURN;
364 
365   // similiar to post_dynamic_code_generated except that it can be used to
366   // post a DynamicCodeGenerated event while holding locks in the VM. Any event
367   // posted using this function is recorded by the enclosing event collector
368   // -- JvmtiDynamicCodeEventCollector.
369   static void post_dynamic_code_generated_while_holding_locks(const char* name, address code_begin, address code_end) NOT_JVMTI_RETURN;
370 
371   static void post_garbage_collection_finish() NOT_JVMTI_RETURN;
372   static void post_garbage_collection_start() NOT_JVMTI_RETURN;
373   static void post_data_dump() NOT_JVMTI_RETURN;
374   static void post_monitor_contended_enter(JavaThread *thread, ObjectMonitor *obj_mntr) NOT_JVMTI_RETURN;
375   static void post_monitor_contended_entered(JavaThread *thread, ObjectMonitor *obj_mntr) NOT_JVMTI_RETURN;
376   static void post_monitor_wait(JavaThread *thread, oop obj, jlong timeout) NOT_JVMTI_RETURN;
377   static void post_monitor_waited(JavaThread *thread, ObjectMonitor *obj_mntr, jboolean timed_out) NOT_JVMTI_RETURN;
378   static void post_object_free(JvmtiEnv* env, jlong tag) NOT_JVMTI_RETURN;
379   static void post_resource_exhausted(jint resource_exhausted_flags, const char* detail) NOT_JVMTI_RETURN;
380   static void record_vm_internal_object_allocation(oop object) NOT_JVMTI_RETURN;
381   // Post objects collected by vm_object_alloc_event_collector.
382   static void post_vm_object_alloc(JavaThread *thread, oop object) NOT_JVMTI_RETURN;
383   // Collects vm internal objects for later event posting.
vm_object_alloc_event_collector(oop object)384   inline static void vm_object_alloc_event_collector(oop object) {
385     if (should_post_vm_object_alloc()) {
386       record_vm_internal_object_allocation(object);
387     }
388   }
389 
390   static void record_sampled_internal_object_allocation(oop object) NOT_JVMTI_RETURN;
391   // Post objects collected by sampled_object_alloc_event_collector.
392   static void post_sampled_object_alloc(JavaThread *thread, oop object) NOT_JVMTI_RETURN;
393 
394   // Collects vm internal objects for later event posting.
sampled_object_alloc_event_collector(oop object)395   inline static void sampled_object_alloc_event_collector(oop object) {
396     if (should_post_sampled_object_alloc()) {
397       record_sampled_internal_object_allocation(object);
398     }
399   }
400 
post_array_size_exhausted()401   inline static void post_array_size_exhausted() {
402     if (should_post_resource_exhausted()) {
403       post_resource_exhausted(JVMTI_RESOURCE_EXHAUSTED_OOM_ERROR,
404                               "Requested array size exceeds VM limit");
405     }
406   }
407 
408   static void cleanup_thread             (JavaThread* thread) NOT_JVMTI_RETURN;
409   static void clear_detected_exception   (JavaThread* thread) NOT_JVMTI_RETURN;
410 
411   static void transition_pending_onload_raw_monitors() NOT_JVMTI_RETURN;
412 
413 #if INCLUDE_SERVICES
414   // attach support
415   static jint load_agent_library(const char *agent, const char *absParam, const char *options, outputStream* out) NOT_JVMTI_RETURN_(JNI_ERR);
416 #endif
417 
418   // SetNativeMethodPrefix support
419   static char** get_all_native_method_prefixes(int* count_ptr) NOT_JVMTI_RETURN_(NULL);
420 
421   // JavaThread lifecycle support:
422   static jvmtiError cv_external_thread_to_JavaThread(ThreadsList * t_list,
423                                                      jthread thread,
424                                                      JavaThread ** jt_pp,
425                                                      oop * thread_oop_p);
426   static jvmtiError cv_oop_to_JavaThread(ThreadsList * t_list, oop thread_oop,
427                                          JavaThread ** jt_pp);
428 };
429 
430 // Support class used by JvmtiDynamicCodeEventCollector and others. It
431 // describes a single code blob by name and address range.
432 class JvmtiCodeBlobDesc : public CHeapObj<mtInternal> {
433  private:
434   char _name[64];
435   address _code_begin;
436   address _code_end;
437 
438  public:
JvmtiCodeBlobDesc(const char * name,address code_begin,address code_end)439   JvmtiCodeBlobDesc(const char *name, address code_begin, address code_end) {
440     assert(name != NULL, "all code blobs must be named");
441     strncpy(_name, name, sizeof(_name) - 1);
442     _name[sizeof(_name)-1] = '\0';
443     _code_begin = code_begin;
444     _code_end = code_end;
445   }
name()446   char* name()                  { return _name; }
code_begin()447   address code_begin()          { return _code_begin; }
code_end()448   address code_end()            { return _code_end; }
449 };
450 
451 // JvmtiEventCollector is a helper class to setup thread for
452 // event collection.
453 class JvmtiEventCollector : public StackObj {
454  private:
455   JvmtiEventCollector* _prev;  // Save previous one to support nested event collector.
456   bool _unset_jvmti_thread_state;
457 
458  public:
JvmtiEventCollector()459   JvmtiEventCollector() : _prev(NULL), _unset_jvmti_thread_state(false) {}
460 
461   void setup_jvmti_thread_state(); // Set this collector in current thread, returns if success.
462   void unset_jvmti_thread_state(); // Reset previous collector in current thread.
is_dynamic_code_event()463   virtual bool is_dynamic_code_event()   { return false; }
is_vm_object_alloc_event()464   virtual bool is_vm_object_alloc_event(){ return false; }
is_sampled_object_alloc_event()465   virtual bool is_sampled_object_alloc_event(){ return false; }
get_prev()466   JvmtiEventCollector *get_prev()        { return _prev; }
467 };
468 
469 // A JvmtiDynamicCodeEventCollector is a helper class for the JvmtiExport
470 // interface. It collects "dynamic code generated" events that are posted
471 // while holding locks. When the event collector goes out of scope the
472 // events will be posted.
473 //
474 // Usage :-
475 //
476 // {
477 //   JvmtiDynamicCodeEventCollector event_collector;
478 //   :
479 //   { MutexLocker ml(...)
480 //     :
481 //     JvmtiExport::post_dynamic_code_generated_while_holding_locks(...)
482 //   }
483 //   // event collector goes out of scope => post events to profiler.
484 // }
485 
486 class JvmtiDynamicCodeEventCollector : public JvmtiEventCollector {
487  private:
488   GrowableArray<JvmtiCodeBlobDesc*>* _code_blobs;           // collected code blob events
489 
490   friend class JvmtiExport;
491   void register_stub(const char* name, address start, address end);
492 
493  public:
494   JvmtiDynamicCodeEventCollector()  NOT_JVMTI_RETURN;
495   ~JvmtiDynamicCodeEventCollector() NOT_JVMTI_RETURN;
is_dynamic_code_event()496   bool is_dynamic_code_event()   { return true; }
497 
498 };
499 
500 // Used as a base class for object allocation collection and then posting
501 // the allocations to any event notification callbacks.
502 //
503 class JvmtiObjectAllocEventCollector : public JvmtiEventCollector {
504  protected:
505   GrowableArray<OopHandle>* _allocated;      // field to record collected allocated object oop.
506   bool _enable;                   // This flag is enabled in constructor if set up in the thread state
507                                   // and disabled in destructor before posting event. To avoid
508                                   // collection of objects allocated while running java code inside
509                                   // agent post_X_object_alloc() event handler.
510   void (*_post_callback)(JavaThread*, oop); // what callback to use when destroying the collector.
511 
512   friend class JvmtiExport;
513 
514   // Record allocated object oop.
515   inline void record_allocation(oop obj);
516 
517  public:
518   JvmtiObjectAllocEventCollector()  NOT_JVMTI_RETURN;
519 
520   void generate_call_for_allocated();
521 
is_enabled()522   bool is_enabled()                 { return _enable; }
set_enabled(bool on)523   void set_enabled(bool on)         { _enable = on; }
524 };
525 
526 // Used to record vm internally allocated object oops and post
527 // vm object alloc event for objects visible to java world.
528 // Constructor enables JvmtiThreadState flag and all vm allocated
529 // objects are recorded in a growable array. When destructor is
530 // called the vm object alloc event is posted for each object
531 // visible to java world.
532 // See jvm.cpp file for its usage.
533 //
534 class JvmtiVMObjectAllocEventCollector : public JvmtiObjectAllocEventCollector {
535  public:
536   JvmtiVMObjectAllocEventCollector()  NOT_JVMTI_RETURN;
537   ~JvmtiVMObjectAllocEventCollector()  NOT_JVMTI_RETURN;
is_vm_object_alloc_event()538   virtual bool is_vm_object_alloc_event()   { return true; }
539 };
540 
541 // Used to record sampled allocated object oops and post
542 // sampled object alloc event.
543 // Constructor enables JvmtiThreadState flag and all sampled allocated
544 // objects are recorded in a growable array. When destructor is
545 // called the sampled object alloc event is posted for each sampled object.
546 // See jvm.cpp file for its usage.
547 //
548 class JvmtiSampledObjectAllocEventCollector : public JvmtiObjectAllocEventCollector {
549  public:
550   JvmtiSampledObjectAllocEventCollector()  NOT_JVMTI_RETURN;
551   ~JvmtiSampledObjectAllocEventCollector()  NOT_JVMTI_RETURN;
is_sampled_object_alloc_event()552   bool is_sampled_object_alloc_event()    { return true; }
553   static bool object_alloc_is_safe_to_sample() NOT_JVMTI_RETURN_(false);
554 };
555 
556 // Marker class to disable the posting of VMObjectAlloc events
557 // within its scope.
558 //
559 // Usage :-
560 //
561 // {
562 //   NoJvmtiVMObjectAllocMark njm;
563 //   :
564 //   // VMObjAlloc event will not be posted
565 //   JvmtiExport::vm_object_alloc_event_collector(obj);
566 //   :
567 // }
568 
569 class NoJvmtiVMObjectAllocMark : public StackObj {
570  private:
571   // enclosing collector if enabled, NULL otherwise
572   JvmtiVMObjectAllocEventCollector *_collector;
573 
was_enabled()574   bool was_enabled()    { return _collector != NULL; }
575 
576  public:
577   NoJvmtiVMObjectAllocMark() NOT_JVMTI_RETURN;
578   ~NoJvmtiVMObjectAllocMark() NOT_JVMTI_RETURN;
579 };
580 
581 
582 // Base class for reporting GC events to JVMTI.
583 class JvmtiGCMarker : public StackObj {
584  public:
585   JvmtiGCMarker() NOT_JVMTI_RETURN;
586   ~JvmtiGCMarker() NOT_JVMTI_RETURN;
587 };
588 
589 // JvmtiHideSingleStepping is a helper class for hiding
590 // internal single step events.
591 class JvmtiHideSingleStepping : public StackObj {
592  private:
593   bool         _single_step_hidden;
594   JavaThread * _thread;
595 
596  public:
JvmtiHideSingleStepping(JavaThread * thread)597   JvmtiHideSingleStepping(JavaThread * thread) {
598     assert(thread != NULL, "sanity check");
599 
600     _single_step_hidden = false;
601     _thread = thread;
602     if (JvmtiExport::should_post_single_step()) {
603       _single_step_hidden = JvmtiExport::hide_single_stepping(_thread);
604     }
605   }
606 
~JvmtiHideSingleStepping()607   ~JvmtiHideSingleStepping() {
608     if (_single_step_hidden) {
609       JvmtiExport::expose_single_stepping(_thread);
610     }
611   }
612 };
613 
614 #endif // SHARE_PRIMS_JVMTIEXPORT_HPP
615