blob: c73215f07b445926f1e9b2373faf6a49e0211042 [file] [log] [blame]
Andreas Gampe77708d92016-10-07 11:48:21 -07001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Andreas Gampe06c42a52017-07-26 14:17:14 -070017#ifndef ART_OPENJDKJVMTI_EVENTS_H_
18#define ART_OPENJDKJVMTI_EVENTS_H_
Andreas Gampe77708d92016-10-07 11:48:21 -070019
20#include <bitset>
21#include <vector>
22
23#include "base/logging.h"
24#include "jvmti.h"
25#include "thread.h"
26
27namespace openjdkjvmti {
28
29struct ArtJvmTiEnv;
Andreas Gampe27fa96c2016-10-07 15:05:24 -070030class JvmtiAllocationListener;
Alex Light8c2b9292017-11-09 13:21:01 -080031class JvmtiDdmChunkListener;
Andreas Gampe9b8c5882016-10-21 15:27:46 -070032class JvmtiGcPauseListener;
Alex Lightb7edcda2017-04-27 13:20:31 -070033class JvmtiMethodTraceListener;
Alex Light77fee872017-09-05 14:51:49 -070034class JvmtiMonitorListener;
Andreas Gampe77708d92016-10-07 11:48:21 -070035
Alex Light73afd322017-01-18 11:17:47 -080036// an enum for ArtEvents. This differs from the JVMTI events only in that we distinguish between
37// retransformation capable and incapable loading
Alex Light8c2b9292017-11-09 13:21:01 -080038enum class ArtJvmtiEvent : jint {
Alex Light40d87f42017-01-18 10:27:06 -080039 kMinEventTypeVal = JVMTI_MIN_EVENT_TYPE_VAL,
40 kVmInit = JVMTI_EVENT_VM_INIT,
41 kVmDeath = JVMTI_EVENT_VM_DEATH,
42 kThreadStart = JVMTI_EVENT_THREAD_START,
43 kThreadEnd = JVMTI_EVENT_THREAD_END,
Alex Light73afd322017-01-18 11:17:47 -080044 kClassFileLoadHookNonRetransformable = JVMTI_EVENT_CLASS_FILE_LOAD_HOOK,
Alex Light40d87f42017-01-18 10:27:06 -080045 kClassLoad = JVMTI_EVENT_CLASS_LOAD,
46 kClassPrepare = JVMTI_EVENT_CLASS_PREPARE,
47 kVmStart = JVMTI_EVENT_VM_START,
48 kException = JVMTI_EVENT_EXCEPTION,
49 kExceptionCatch = JVMTI_EVENT_EXCEPTION_CATCH,
50 kSingleStep = JVMTI_EVENT_SINGLE_STEP,
51 kFramePop = JVMTI_EVENT_FRAME_POP,
52 kBreakpoint = JVMTI_EVENT_BREAKPOINT,
53 kFieldAccess = JVMTI_EVENT_FIELD_ACCESS,
54 kFieldModification = JVMTI_EVENT_FIELD_MODIFICATION,
55 kMethodEntry = JVMTI_EVENT_METHOD_ENTRY,
56 kMethodExit = JVMTI_EVENT_METHOD_EXIT,
57 kNativeMethodBind = JVMTI_EVENT_NATIVE_METHOD_BIND,
58 kCompiledMethodLoad = JVMTI_EVENT_COMPILED_METHOD_LOAD,
59 kCompiledMethodUnload = JVMTI_EVENT_COMPILED_METHOD_UNLOAD,
60 kDynamicCodeGenerated = JVMTI_EVENT_DYNAMIC_CODE_GENERATED,
61 kDataDumpRequest = JVMTI_EVENT_DATA_DUMP_REQUEST,
62 kMonitorWait = JVMTI_EVENT_MONITOR_WAIT,
63 kMonitorWaited = JVMTI_EVENT_MONITOR_WAITED,
64 kMonitorContendedEnter = JVMTI_EVENT_MONITOR_CONTENDED_ENTER,
65 kMonitorContendedEntered = JVMTI_EVENT_MONITOR_CONTENDED_ENTERED,
66 kResourceExhausted = JVMTI_EVENT_RESOURCE_EXHAUSTED,
67 kGarbageCollectionStart = JVMTI_EVENT_GARBAGE_COLLECTION_START,
68 kGarbageCollectionFinish = JVMTI_EVENT_GARBAGE_COLLECTION_FINISH,
69 kObjectFree = JVMTI_EVENT_OBJECT_FREE,
70 kVmObjectAlloc = JVMTI_EVENT_VM_OBJECT_ALLOC,
Alex Light73afd322017-01-18 11:17:47 -080071 kClassFileLoadHookRetransformable = JVMTI_MAX_EVENT_TYPE_VAL + 1,
Alex Light8c2b9292017-11-09 13:21:01 -080072 kDdmPublishChunk = JVMTI_MAX_EVENT_TYPE_VAL + 2,
73 kMaxEventTypeVal = kDdmPublishChunk,
Alex Light40d87f42017-01-18 10:27:06 -080074};
75
Alex Light8c2b9292017-11-09 13:21:01 -080076using ArtJvmtiEventDdmPublishChunk = void (*)(jvmtiEnv *jvmti_env,
77 JNIEnv* jni_env,
78 jint data_type,
79 jint data_len,
80 const jbyte* data);
81
82struct ArtJvmtiEventCallbacks : jvmtiEventCallbacks {
83 ArtJvmtiEventCallbacks() : DdmPublishChunk(nullptr) {
84 memset(this, 0, sizeof(jvmtiEventCallbacks));
85 }
86
87 // Copies extension functions from other callback struct if it exists. There must not have been
88 // any modifications to this struct when it is called.
89 void CopyExtensionsFrom(const ArtJvmtiEventCallbacks* cb);
90
91 jvmtiError Set(jint index, jvmtiExtensionEvent cb);
92
93 ArtJvmtiEventDdmPublishChunk DdmPublishChunk;
94};
95
96bool IsExtensionEvent(jint e);
97bool IsExtensionEvent(ArtJvmtiEvent e);
98
Alex Light40d87f42017-01-18 10:27:06 -080099// Convert a jvmtiEvent into a ArtJvmtiEvent
100ALWAYS_INLINE static inline ArtJvmtiEvent GetArtJvmtiEvent(ArtJvmTiEnv* env, jvmtiEvent e);
101
Alex Light73afd322017-01-18 11:17:47 -0800102static inline jvmtiEvent GetJvmtiEvent(ArtJvmtiEvent e) {
103 if (UNLIKELY(e == ArtJvmtiEvent::kClassFileLoadHookRetransformable)) {
104 return JVMTI_EVENT_CLASS_FILE_LOAD_HOOK;
105 } else {
106 return static_cast<jvmtiEvent>(e);
107 }
Alex Light40d87f42017-01-18 10:27:06 -0800108}
109
Andreas Gampe77708d92016-10-07 11:48:21 -0700110struct EventMask {
Alex Light40d87f42017-01-18 10:27:06 -0800111 static constexpr size_t kEventsSize =
112 static_cast<size_t>(ArtJvmtiEvent::kMaxEventTypeVal) -
113 static_cast<size_t>(ArtJvmtiEvent::kMinEventTypeVal) + 1;
Andreas Gampe77708d92016-10-07 11:48:21 -0700114 std::bitset<kEventsSize> bit_set;
115
Alex Light40d87f42017-01-18 10:27:06 -0800116 static bool EventIsInRange(ArtJvmtiEvent event) {
117 return event >= ArtJvmtiEvent::kMinEventTypeVal && event <= ArtJvmtiEvent::kMaxEventTypeVal;
Andreas Gampe77708d92016-10-07 11:48:21 -0700118 }
119
Alex Light40d87f42017-01-18 10:27:06 -0800120 void Set(ArtJvmtiEvent event, bool value = true) {
Andreas Gampe77708d92016-10-07 11:48:21 -0700121 DCHECK(EventIsInRange(event));
Alex Light40d87f42017-01-18 10:27:06 -0800122 bit_set.set(static_cast<size_t>(event) - static_cast<size_t>(ArtJvmtiEvent::kMinEventTypeVal),
123 value);
Andreas Gampe77708d92016-10-07 11:48:21 -0700124 }
125
Alex Light40d87f42017-01-18 10:27:06 -0800126 bool Test(ArtJvmtiEvent event) const {
Andreas Gampe77708d92016-10-07 11:48:21 -0700127 DCHECK(EventIsInRange(event));
Alex Light40d87f42017-01-18 10:27:06 -0800128 return bit_set.test(
129 static_cast<size_t>(event) - static_cast<size_t>(ArtJvmtiEvent::kMinEventTypeVal));
Andreas Gampe77708d92016-10-07 11:48:21 -0700130 }
131};
132
133struct EventMasks {
134 // The globally enabled events.
135 EventMask global_event_mask;
136
137 // The per-thread enabled events.
138
139 // It is not enough to store a Thread pointer, as these may be reused. Use the pointer and the
140 // thread id.
141 // Note: We could just use the tid like tracing does.
142 using UniqueThread = std::pair<art::Thread*, uint32_t>;
143 // TODO: Native thread objects are immovable, so we can use them as keys in an (unordered) map,
144 // if necessary.
145 std::vector<std::pair<UniqueThread, EventMask>> thread_event_masks;
146
147 // A union of the per-thread events, for fast-pathing.
148 EventMask unioned_thread_event_mask;
149
150 EventMask& GetEventMask(art::Thread* thread);
151 EventMask* GetEventMaskOrNull(art::Thread* thread);
Alex Light40d87f42017-01-18 10:27:06 -0800152 void EnableEvent(art::Thread* thread, ArtJvmtiEvent event);
153 void DisableEvent(art::Thread* thread, ArtJvmtiEvent event);
Alex Light73afd322017-01-18 11:17:47 -0800154 bool IsEnabledAnywhere(ArtJvmtiEvent event);
155 // Make any changes to event masks needed for the given capability changes. If caps_added is true
156 // then caps is all the newly set capabilities of the jvmtiEnv. If it is false then caps is the
157 // set of all capabilities that were removed from the jvmtiEnv.
158 void HandleChangedCapabilities(const jvmtiCapabilities& caps, bool caps_added);
Andreas Gampe77708d92016-10-07 11:48:21 -0700159};
160
Alex Lightb284f8d2017-11-21 00:00:48 +0000161namespace impl {
162template <ArtJvmtiEvent kEvent> struct EventHandlerFunc { };
163} // namespace impl
164
Andreas Gampe77708d92016-10-07 11:48:21 -0700165// Helper class for event handling.
Andreas Gampe27fa96c2016-10-07 15:05:24 -0700166class EventHandler {
167 public:
168 EventHandler();
169 ~EventHandler();
Andreas Gampe77708d92016-10-07 11:48:21 -0700170
Alex Lightb7edcda2017-04-27 13:20:31 -0700171 // do cleanup for the event handler.
172 void Shutdown();
173
Andreas Gampe77708d92016-10-07 11:48:21 -0700174 // Register an env. It is assumed that this happens on env creation, that is, no events are
175 // enabled, yet.
Alex Lightb284f8d2017-11-21 00:00:48 +0000176 void RegisterArtJvmTiEnv(ArtJvmTiEnv* env) REQUIRES(!envs_lock_);
Andreas Gampe77708d92016-10-07 11:48:21 -0700177
Andreas Gampe3a7eb142017-01-19 21:59:22 -0800178 // Remove an env.
Alex Lightb284f8d2017-11-21 00:00:48 +0000179 void RemoveArtJvmTiEnv(ArtJvmTiEnv* env) REQUIRES(!envs_lock_);
Andreas Gampe3a7eb142017-01-19 21:59:22 -0800180
Alex Light40d87f42017-01-18 10:27:06 -0800181 bool IsEventEnabledAnywhere(ArtJvmtiEvent event) const {
Andreas Gampe77708d92016-10-07 11:48:21 -0700182 if (!EventMask::EventIsInRange(event)) {
183 return false;
184 }
185 return global_mask.Test(event);
186 }
187
Alex Light40d87f42017-01-18 10:27:06 -0800188 jvmtiError SetEvent(ArtJvmTiEnv* env,
189 art::Thread* thread,
190 ArtJvmtiEvent event,
Alex Lightb284f8d2017-11-21 00:00:48 +0000191 jvmtiEventMode mode)
192 REQUIRES(!envs_lock_);
Andreas Gampe77708d92016-10-07 11:48:21 -0700193
Alex Light9df79b72017-09-12 08:57:31 -0700194 // Dispatch event to all registered environments. Since this one doesn't have a JNIEnv* it doesn't
195 // matter if it has the mutator_lock.
Andreas Gampe983c1752017-01-23 19:46:56 -0800196 template <ArtJvmtiEvent kEvent, typename ...Args>
Alex Light40d87f42017-01-18 10:27:06 -0800197 ALWAYS_INLINE
Alex Lightb284f8d2017-11-21 00:00:48 +0000198 inline void DispatchEvent(art::Thread* thread, Args... args) const
199 REQUIRES(!envs_lock_);
Alex Light9df79b72017-09-12 08:57:31 -0700200
Alex Lightb7edcda2017-04-27 13:20:31 -0700201 // Dispatch event to all registered environments stashing exceptions as needed. This works since
202 // JNIEnv* is always the second argument if it is passed to an event. Needed since C++ does not
203 // allow partial template function specialization.
Alex Light9df79b72017-09-12 08:57:31 -0700204 //
205 // We need both of these since we want to make sure to push a stack frame when it is possible for
206 // the event to allocate local references.
Alex Lightb7edcda2017-04-27 13:20:31 -0700207 template <ArtJvmtiEvent kEvent, typename ...Args>
208 ALWAYS_INLINE
Alex Lightb284f8d2017-11-21 00:00:48 +0000209 inline void DispatchEvent(art::Thread* thread, JNIEnv* jnienv, Args... args) const
210 REQUIRES(!envs_lock_);
Andreas Gampe27fa96c2016-10-07 15:05:24 -0700211
Alex Light73afd322017-01-18 11:17:47 -0800212 // Tell the event handler capabilities were added/lost so it can adjust the sent events.If
213 // caps_added is true then caps is all the newly set capabilities of the jvmtiEnv. If it is false
214 // then caps is the set of all capabilities that were removed from the jvmtiEnv.
215 ALWAYS_INLINE
216 inline void HandleChangedCapabilities(ArtJvmTiEnv* env,
217 const jvmtiCapabilities& caps,
Alex Lightb284f8d2017-11-21 00:00:48 +0000218 bool added)
219 REQUIRES(!envs_lock_);
Alex Light73afd322017-01-18 11:17:47 -0800220
Alex Light9df79b72017-09-12 08:57:31 -0700221 // Dispatch event to the given environment, only.
222 template <ArtJvmtiEvent kEvent, typename ...Args>
223 ALWAYS_INLINE
Alex Lightb284f8d2017-11-21 00:00:48 +0000224 inline void DispatchEventOnEnv(ArtJvmTiEnv* env,
225 art::Thread* thread,
226 JNIEnv* jnienv,
227 Args... args) const
228 REQUIRES(!envs_lock_);
Alex Light9df79b72017-09-12 08:57:31 -0700229
230 // Dispatch event to the given environment, only.
231 template <ArtJvmtiEvent kEvent, typename ...Args>
232 ALWAYS_INLINE
Alex Lightb284f8d2017-11-21 00:00:48 +0000233 inline void DispatchEventOnEnv(ArtJvmTiEnv* env, art::Thread* thread, Args... args) const
234 REQUIRES(!envs_lock_);
Alex Light9df79b72017-09-12 08:57:31 -0700235
Andreas Gampe27fa96c2016-10-07 15:05:24 -0700236 private:
Alex Lightb284f8d2017-11-21 00:00:48 +0000237 template <ArtJvmtiEvent kEvent, typename ...Args>
238 ALWAYS_INLINE
239 inline std::vector<impl::EventHandlerFunc<kEvent>> CollectEvents(art::Thread* thread,
240 Args... args) const
241 REQUIRES(!envs_lock_);
242
Andreas Gampe983c1752017-01-23 19:46:56 -0800243 template <ArtJvmtiEvent kEvent>
Alex Light40d87f42017-01-18 10:27:06 -0800244 ALWAYS_INLINE
Alex Lightb284f8d2017-11-21 00:00:48 +0000245 inline bool ShouldDispatchOnThread(ArtJvmTiEnv* env, art::Thread* thread) const;
Alex Light9df79b72017-09-12 08:57:31 -0700246
247 template <ArtJvmtiEvent kEvent, typename ...Args>
248 ALWAYS_INLINE
Alex Lightb284f8d2017-11-21 00:00:48 +0000249 static inline void ExecuteCallback(impl::EventHandlerFunc<kEvent> handler,
250 JNIEnv* env,
251 Args... args)
252 REQUIRES(!envs_lock_);
Alex Light9df79b72017-09-12 08:57:31 -0700253
254 template <ArtJvmtiEvent kEvent, typename ...Args>
255 ALWAYS_INLINE
Alex Lightb284f8d2017-11-21 00:00:48 +0000256 static inline void ExecuteCallback(impl::EventHandlerFunc<kEvent> handler, Args... args)
257 REQUIRES(!envs_lock_);
258
259 // Public for use to collect dispatches
260 template <ArtJvmtiEvent kEvent, typename ...Args>
261 ALWAYS_INLINE
Alex Light9df79b72017-09-12 08:57:31 -0700262 inline bool ShouldDispatch(ArtJvmTiEnv* env, art::Thread* thread, Args... args) const;
Alex Light40d87f42017-01-18 10:27:06 -0800263
Alex Light73afd322017-01-18 11:17:47 -0800264 ALWAYS_INLINE
265 inline bool NeedsEventUpdate(ArtJvmTiEnv* env,
266 const jvmtiCapabilities& caps,
267 bool added);
268
269 // Recalculates the event mask for the given event.
270 ALWAYS_INLINE
Alex Lightb284f8d2017-11-21 00:00:48 +0000271 inline void RecalculateGlobalEventMask(ArtJvmtiEvent event) REQUIRES(!envs_lock_);
272 ALWAYS_INLINE
273 inline void RecalculateGlobalEventMaskLocked(ArtJvmtiEvent event) REQUIRES(envs_lock_);
Alex Light73afd322017-01-18 11:17:47 -0800274
Andreas Gampe983c1752017-01-23 19:46:56 -0800275 template <ArtJvmtiEvent kEvent>
Alex Light6ac57502017-01-19 15:05:06 -0800276 ALWAYS_INLINE inline void DispatchClassFileLoadHookEvent(art::Thread* thread,
Andreas Gampe983c1752017-01-23 19:46:56 -0800277 JNIEnv* jnienv,
278 jclass class_being_redefined,
279 jobject loader,
280 const char* name,
281 jobject protection_domain,
282 jint class_data_len,
283 const unsigned char* class_data,
284 jint* new_class_data_len,
Alex Lightb284f8d2017-11-21 00:00:48 +0000285 unsigned char** new_class_data) const
286 REQUIRES(!envs_lock_);
Alex Light6ac57502017-01-19 15:05:06 -0800287
Alex Light40d87f42017-01-18 10:27:06 -0800288 void HandleEventType(ArtJvmtiEvent event, bool enable);
Alex Lightbebd7bd2017-07-25 14:05:52 -0700289 void HandleLocalAccessCapabilityAdded();
Alex Light0fa17862017-10-24 13:43:05 -0700290 void HandleBreakpointEventsChanged(bool enable);
Andreas Gampe27fa96c2016-10-07 15:05:24 -0700291
Alex Light77fee872017-09-05 14:51:49 -0700292 bool OtherMonitorEventsEnabledAnywhere(ArtJvmtiEvent event);
293
Alex Lightb284f8d2017-11-21 00:00:48 +0000294 // List of all JvmTiEnv objects that have been created, in their creation order. It is a std::list
295 // since we mostly access it by iterating over the entire thing, only ever append to the end, and
296 // need to be able to remove arbitrary elements from it.
297 std::list<ArtJvmTiEnv*> envs GUARDED_BY(envs_lock_);
298
299 // Top level lock. Nothing at all should be held when we lock this.
300 mutable art::Mutex envs_lock_ ACQUIRED_BEFORE(art::Locks::instrument_entrypoints_lock_);
Andreas Gampe27fa96c2016-10-07 15:05:24 -0700301
302 // A union of all enabled events, anywhere.
303 EventMask global_mask;
304
305 std::unique_ptr<JvmtiAllocationListener> alloc_listener_;
Alex Light8c2b9292017-11-09 13:21:01 -0800306 std::unique_ptr<JvmtiDdmChunkListener> ddm_listener_;
Andreas Gampe9b8c5882016-10-21 15:27:46 -0700307 std::unique_ptr<JvmtiGcPauseListener> gc_pause_listener_;
Alex Lightb7edcda2017-04-27 13:20:31 -0700308 std::unique_ptr<JvmtiMethodTraceListener> method_trace_listener_;
Alex Light77fee872017-09-05 14:51:49 -0700309 std::unique_ptr<JvmtiMonitorListener> monitor_listener_;
Alex Lighte814f9d2017-07-31 16:14:39 -0700310
311 // True if frame pop has ever been enabled. Since we store pointers to stack frames we need to
312 // continue to listen to this event even if it has been disabled.
313 // TODO We could remove the listeners once all jvmtiEnvs have drained their shadow-frame vectors.
314 bool frame_pop_enabled;
Andreas Gampe77708d92016-10-07 11:48:21 -0700315};
316
317} // namespace openjdkjvmti
318
Andreas Gampe06c42a52017-07-26 14:17:14 -0700319#endif // ART_OPENJDKJVMTI_EVENTS_H_