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