1 /*
  2  * Copyright (c) 2003, 2019, 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_JVMTIEVENTCONTROLLER_HPP
 26 #define SHARE_PRIMS_JVMTIEVENTCONTROLLER_HPP
 27 
 28 #include "jvmtifiles/jvmti.h"
 29 #include "memory/allocation.hpp"
 30 #include "utilities/globalDefinitions.hpp"
 31 
 32 // forward declaration
 33 class JvmtiEventControllerPrivate;
 34 class JvmtiEventController;
 35 class JvmtiEnvThreadState;
 36 class JvmtiFramePop;
 37 class JvmtiEnvBase;
 38 
 39 
 40 // Extension event support
 41 //
 42 // jvmtiExtEvent is the extensions equivalent of jvmtiEvent
 43 // jvmtiExtCallbacks is the extensions equivalent of jvmtiEventCallbacks
 44 
 45 // Extension events start JVMTI_MIN_EVENT_TYPE_VAL-1 and work towards 0.
 46 typedef enum {
 47   EXT_EVENT_VIRTUAL_THREAD_UNMOUNT = JVMTI_MIN_EVENT_TYPE_VAL-3,
 48   EXT_EVENT_VIRTUAL_THREAD_MOUNT = JVMTI_MIN_EVENT_TYPE_VAL-2,
 49   EXT_EVENT_CLASS_UNLOAD = JVMTI_MIN_EVENT_TYPE_VAL-1,
 50   EXT_MIN_EVENT_TYPE_VAL = EXT_EVENT_VIRTUAL_THREAD_UNMOUNT,
 51   EXT_MAX_EVENT_TYPE_VAL = EXT_EVENT_CLASS_UNLOAD
 52 } jvmtiExtEvent;
 53 
 54 typedef struct {
 55   jvmtiExtensionEvent ClassUnload;
 56   jvmtiExtensionEvent VirtualThreadMount;
 57   jvmtiExtensionEvent VirtualThreadUnmount;
 58 } jvmtiExtEventCallbacks;
 59 
 60 
 61 // The complete range of events is EXT_MIN_EVENT_TYPE_VAL to
 62 // JVMTI_MAX_EVENT_TYPE_VAL (inclusive and contiguous).
 63 const int TOTAL_MIN_EVENT_TYPE_VAL = EXT_MIN_EVENT_TYPE_VAL;
 64 const int TOTAL_MAX_EVENT_TYPE_VAL = JVMTI_MAX_EVENT_TYPE_VAL;
 65 
 66 
 67 ///////////////////////////////////////////////////////////////
 68 //
 69 // JvmtiEventEnabled
 70 //
 71 // Utility class
 72 //
 73 // A boolean array indexed by event_type, used as an internal
 74 // data structure to track what JVMTI event types are enabled.
 75 // Used for user set enabling and disabling (globally and on a
 76 // per thread basis), and for computed merges across environments,
 77 // threads and the VM as a whole.
 78 //
 79 // for inlines see jvmtiEventController_inline.hpp
 80 //
 81 
 82 class JvmtiEventEnabled {
 83 private:
 84   friend class JvmtiEventControllerPrivate;
 85   jlong _enabled_bits;
 86 #ifndef PRODUCT
 87   enum {
 88     JEE_INIT_GUARD = 0xEAD0
 89   } _init_guard;
 90 #endif
 91   static jlong bit_for(jvmtiEvent event_type);
 92   jlong get_bits();
 93   void set_bits(jlong bits);
 94 public:
 95   JvmtiEventEnabled();
 96   void clear();
 97   bool is_enabled(jvmtiEvent event_type);
 98   void set_enabled(jvmtiEvent event_type, bool enabled);
 99 };
100 
101 
102 ///////////////////////////////////////////////////////////////
103 //
104 // JvmtiEnvThreadEventEnable
105 //
106 // JvmtiEventController data specific to a particular environment and thread.
107 //
108 // for inlines see jvmtiEventController_inline.hpp
109 //
110 
111 class JvmtiEnvThreadEventEnable {
112 private:
113   friend class JvmtiEventControllerPrivate;
114   JvmtiEventEnabled _event_user_enabled;
115   JvmtiEventEnabled _event_enabled;
116 
117 public:
118   JvmtiEnvThreadEventEnable();
119   ~JvmtiEnvThreadEventEnable();
120   bool is_enabled(jvmtiEvent event_type);
121   void set_user_enabled(jvmtiEvent event_type, bool enabled);
122 };
123 
124 
125 ///////////////////////////////////////////////////////////////
126 //
127 // JvmtiThreadEventEnable
128 //
129 // JvmtiEventController data specific to a particular thread.
130 //
131 // for inlines see jvmtiEventController_inline.hpp
132 //
133 
134 class JvmtiThreadEventEnable {
135 private:
136   friend class JvmtiEventControllerPrivate;
137   JvmtiEventEnabled _event_enabled;
138 
139 public:
140   JvmtiThreadEventEnable();
141   ~JvmtiThreadEventEnable();
142   bool is_enabled(jvmtiEvent event_type);
143 };
144 
145 
146 ///////////////////////////////////////////////////////////////
147 //
148 // JvmtiEnvEventEnable
149 //
150 // JvmtiEventController data specific to a particular environment.
151 //
152 // for inlines see jvmtiEventController_inline.hpp
153 //
154 
155 class JvmtiEnvEventEnable {
156 private:
157   friend class JvmtiEventControllerPrivate;
158 
159   // user set global event enablement indexed by jvmtiEvent
160   JvmtiEventEnabled _event_user_enabled;
161 
162   // this flag indicates the presence (true) or absence (false) of event callbacks
163   // it is indexed by jvmtiEvent
164   JvmtiEventEnabled _event_callback_enabled;
165 
166   // indexed by jvmtiEvent true if enabled globally or on any thread.
167   // True only if there is a callback for it.
168   JvmtiEventEnabled _event_enabled;
169 
170 public:
171   JvmtiEnvEventEnable();
172   ~JvmtiEnvEventEnable();
173   bool is_enabled(jvmtiEvent event_type);
174   void set_user_enabled(jvmtiEvent event_type, bool enabled);
175 };
176 
177 
178 ///////////////////////////////////////////////////////////////
179 //
180 // JvmtiEventController
181 //
182 // The class is the access point for all actions that change
183 // which events are active, this include:
184 //      enabling and disabling events
185 //      changing the callbacks/eventhook (they may be null)
186 //      setting and clearing field watchpoints
187 //      setting frame pops
188 //      encountering frame pops
189 //
190 // for inlines see jvmtiEventController_inline.hpp
191 //
192 
193 class JvmtiEventController : AllStatic {
194 private:
195   friend class JvmtiEventControllerPrivate;
196 
197   // for all environments, global array indexed by jvmtiEvent
198   static JvmtiEventEnabled _universal_global_event_enabled;
199 
200 public:
201   static bool is_enabled(jvmtiEvent event_type);
202 
203   // events that can ONLY be enabled/disabled globally (can't toggle on individual threads).
204   static bool is_global_event(jvmtiEvent event_type);
205 
206   // is the event_type valid?
207   // to do: check against valid event array
208   static bool is_valid_event_type(jvmtiEvent event_type) {
209     return ((int)event_type >= TOTAL_MIN_EVENT_TYPE_VAL)
210         && ((int)event_type <= TOTAL_MAX_EVENT_TYPE_VAL);
211   }
212 
213   // Use (thread == NULL) to enable/disable an event globally.
214   // Use (thread != NULL) to enable/disable an event for a particular thread.
215   // thread is ignored for events that can only be specified globally
216   static void set_user_enabled(JvmtiEnvBase *env, JavaThread *thread, oop thread_oop,
217                                jvmtiEvent event_type, bool enabled);
218 
219   // Setting callbacks changes computed enablement and must be done
220   // at a safepoint otherwise a NULL callback could be attempted
221   static void set_event_callbacks(JvmtiEnvBase *env,
222                                   const jvmtiEventCallbacks* callbacks,
223                                   jint size_of_callbacks);
224 
225   // Sets the callback function for a single extension event and enables
226   // (or disables it).
227   static void set_extension_event_callback(JvmtiEnvBase* env,
228                                            jint extension_event_index,
229                                            jvmtiExtensionEvent callback);
230 
231   static void set_frame_pop(JvmtiEnvThreadState *env_thread, JvmtiFramePop fpop);
232   static void clear_frame_pop(JvmtiEnvThreadState *env_thread, JvmtiFramePop fpop);
233 
234   static void change_field_watch(jvmtiEvent event_type, bool added);
235 
236   static void thread_started(JavaThread *thread);
237   static void thread_ended(JavaThread *thread);
238 
239   static void env_initialize(JvmtiEnvBase *env);
240   static void env_dispose(JvmtiEnvBase *env);
241 
242   static void vm_start();
243   static void vm_init();
244   static void vm_death();
245 };
246 
247 #endif // SHARE_PRIMS_JVMTIEVENTCONTROLLER_HPP