blob: bf12cb191e4522834ed9f97c3017505cd07f3bd2 [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
Andreas Gampe57943812017-12-06 21:39:13 -080023#include <android-base/logging.h>
24
25#include "base/macros.h"
26#include "base/mutex.h"
Andreas Gampe77708d92016-10-07 11:48:21 -070027#include "jvmti.h"
28#include "thread.h"
29
30namespace openjdkjvmti {
31
32struct ArtJvmTiEnv;
Andreas Gampe27fa96c2016-10-07 15:05:24 -070033class JvmtiAllocationListener;
Alex Light8c2b9292017-11-09 13:21:01 -080034class JvmtiDdmChunkListener;
Andreas Gampe9b8c5882016-10-21 15:27:46 -070035class JvmtiGcPauseListener;
Alex Lightb7edcda2017-04-27 13:20:31 -070036class JvmtiMethodTraceListener;
Alex Light77fee872017-09-05 14:51:49 -070037class JvmtiMonitorListener;
Andreas Gampe77708d92016-10-07 11:48:21 -070038
Alex Light73afd322017-01-18 11:17:47 -080039// an enum for ArtEvents. This differs from the JVMTI events only in that we distinguish between
40// retransformation capable and incapable loading
Alex Light8c2b9292017-11-09 13:21:01 -080041enum class ArtJvmtiEvent : jint {
Alex Light40d87f42017-01-18 10:27:06 -080042 kMinEventTypeVal = JVMTI_MIN_EVENT_TYPE_VAL,
43 kVmInit = JVMTI_EVENT_VM_INIT,
44 kVmDeath = JVMTI_EVENT_VM_DEATH,
45 kThreadStart = JVMTI_EVENT_THREAD_START,
46 kThreadEnd = JVMTI_EVENT_THREAD_END,
Alex Light73afd322017-01-18 11:17:47 -080047 kClassFileLoadHookNonRetransformable = JVMTI_EVENT_CLASS_FILE_LOAD_HOOK,
Alex Light40d87f42017-01-18 10:27:06 -080048 kClassLoad = JVMTI_EVENT_CLASS_LOAD,
49 kClassPrepare = JVMTI_EVENT_CLASS_PREPARE,
50 kVmStart = JVMTI_EVENT_VM_START,
51 kException = JVMTI_EVENT_EXCEPTION,
52 kExceptionCatch = JVMTI_EVENT_EXCEPTION_CATCH,
53 kSingleStep = JVMTI_EVENT_SINGLE_STEP,
54 kFramePop = JVMTI_EVENT_FRAME_POP,
55 kBreakpoint = JVMTI_EVENT_BREAKPOINT,
56 kFieldAccess = JVMTI_EVENT_FIELD_ACCESS,
57 kFieldModification = JVMTI_EVENT_FIELD_MODIFICATION,
58 kMethodEntry = JVMTI_EVENT_METHOD_ENTRY,
59 kMethodExit = JVMTI_EVENT_METHOD_EXIT,
60 kNativeMethodBind = JVMTI_EVENT_NATIVE_METHOD_BIND,
61 kCompiledMethodLoad = JVMTI_EVENT_COMPILED_METHOD_LOAD,
62 kCompiledMethodUnload = JVMTI_EVENT_COMPILED_METHOD_UNLOAD,
63 kDynamicCodeGenerated = JVMTI_EVENT_DYNAMIC_CODE_GENERATED,
64 kDataDumpRequest = JVMTI_EVENT_DATA_DUMP_REQUEST,
65 kMonitorWait = JVMTI_EVENT_MONITOR_WAIT,
66 kMonitorWaited = JVMTI_EVENT_MONITOR_WAITED,
67 kMonitorContendedEnter = JVMTI_EVENT_MONITOR_CONTENDED_ENTER,
68 kMonitorContendedEntered = JVMTI_EVENT_MONITOR_CONTENDED_ENTERED,
69 kResourceExhausted = JVMTI_EVENT_RESOURCE_EXHAUSTED,
70 kGarbageCollectionStart = JVMTI_EVENT_GARBAGE_COLLECTION_START,
71 kGarbageCollectionFinish = JVMTI_EVENT_GARBAGE_COLLECTION_FINISH,
72 kObjectFree = JVMTI_EVENT_OBJECT_FREE,
73 kVmObjectAlloc = JVMTI_EVENT_VM_OBJECT_ALLOC,
Alex Light73afd322017-01-18 11:17:47 -080074 kClassFileLoadHookRetransformable = JVMTI_MAX_EVENT_TYPE_VAL + 1,
Alex Light8c2b9292017-11-09 13:21:01 -080075 kDdmPublishChunk = JVMTI_MAX_EVENT_TYPE_VAL + 2,
76 kMaxEventTypeVal = kDdmPublishChunk,
Alex Light40d87f42017-01-18 10:27:06 -080077};
78
Alex Light8c2b9292017-11-09 13:21:01 -080079using ArtJvmtiEventDdmPublishChunk = void (*)(jvmtiEnv *jvmti_env,
80 JNIEnv* jni_env,
81 jint data_type,
82 jint data_len,
83 const jbyte* data);
84
85struct ArtJvmtiEventCallbacks : jvmtiEventCallbacks {
86 ArtJvmtiEventCallbacks() : DdmPublishChunk(nullptr) {
87 memset(this, 0, sizeof(jvmtiEventCallbacks));
88 }
89
90 // Copies extension functions from other callback struct if it exists. There must not have been
91 // any modifications to this struct when it is called.
92 void CopyExtensionsFrom(const ArtJvmtiEventCallbacks* cb);
93
94 jvmtiError Set(jint index, jvmtiExtensionEvent cb);
95
96 ArtJvmtiEventDdmPublishChunk DdmPublishChunk;
97};
98
99bool IsExtensionEvent(jint e);
100bool IsExtensionEvent(ArtJvmtiEvent e);
101
Alex Light40d87f42017-01-18 10:27:06 -0800102// Convert a jvmtiEvent into a ArtJvmtiEvent
103ALWAYS_INLINE static inline ArtJvmtiEvent GetArtJvmtiEvent(ArtJvmTiEnv* env, jvmtiEvent e);
104
Alex Light73afd322017-01-18 11:17:47 -0800105static inline jvmtiEvent GetJvmtiEvent(ArtJvmtiEvent e) {
106 if (UNLIKELY(e == ArtJvmtiEvent::kClassFileLoadHookRetransformable)) {
107 return JVMTI_EVENT_CLASS_FILE_LOAD_HOOK;
108 } else {
109 return static_cast<jvmtiEvent>(e);
110 }
Alex Light40d87f42017-01-18 10:27:06 -0800111}
112
Andreas Gampe77708d92016-10-07 11:48:21 -0700113struct EventMask {
Alex Light40d87f42017-01-18 10:27:06 -0800114 static constexpr size_t kEventsSize =
115 static_cast<size_t>(ArtJvmtiEvent::kMaxEventTypeVal) -
116 static_cast<size_t>(ArtJvmtiEvent::kMinEventTypeVal) + 1;
Andreas Gampe77708d92016-10-07 11:48:21 -0700117 std::bitset<kEventsSize> bit_set;
118
Alex Light40d87f42017-01-18 10:27:06 -0800119 static bool EventIsInRange(ArtJvmtiEvent event) {
120 return event >= ArtJvmtiEvent::kMinEventTypeVal && event <= ArtJvmtiEvent::kMaxEventTypeVal;
Andreas Gampe77708d92016-10-07 11:48:21 -0700121 }
122
Alex Light40d87f42017-01-18 10:27:06 -0800123 void Set(ArtJvmtiEvent event, bool value = true) {
Andreas Gampe77708d92016-10-07 11:48:21 -0700124 DCHECK(EventIsInRange(event));
Alex Light40d87f42017-01-18 10:27:06 -0800125 bit_set.set(static_cast<size_t>(event) - static_cast<size_t>(ArtJvmtiEvent::kMinEventTypeVal),
126 value);
Andreas Gampe77708d92016-10-07 11:48:21 -0700127 }
128
Alex Light40d87f42017-01-18 10:27:06 -0800129 bool Test(ArtJvmtiEvent event) const {
Andreas Gampe77708d92016-10-07 11:48:21 -0700130 DCHECK(EventIsInRange(event));
Alex Light40d87f42017-01-18 10:27:06 -0800131 return bit_set.test(
132 static_cast<size_t>(event) - static_cast<size_t>(ArtJvmtiEvent::kMinEventTypeVal));
Andreas Gampe77708d92016-10-07 11:48:21 -0700133 }
134};
135
136struct EventMasks {
137 // The globally enabled events.
138 EventMask global_event_mask;
139
140 // The per-thread enabled events.
141
142 // It is not enough to store a Thread pointer, as these may be reused. Use the pointer and the
143 // thread id.
144 // Note: We could just use the tid like tracing does.
145 using UniqueThread = std::pair<art::Thread*, uint32_t>;
146 // TODO: Native thread objects are immovable, so we can use them as keys in an (unordered) map,
147 // if necessary.
148 std::vector<std::pair<UniqueThread, EventMask>> thread_event_masks;
149
150 // A union of the per-thread events, for fast-pathing.
151 EventMask unioned_thread_event_mask;
152
153 EventMask& GetEventMask(art::Thread* thread);
154 EventMask* GetEventMaskOrNull(art::Thread* thread);
Alex Light74c84402017-11-29 15:26:38 -0800155 // Circular dependencies mean we cannot see the definition of ArtJvmTiEnv so the mutex is simply
156 // asserted in the function.
157 // Note that the 'env' passed in must be the same env this EventMasks is associated with.
158 void EnableEvent(ArtJvmTiEnv* env, art::Thread* thread, ArtJvmtiEvent event);
159 // REQUIRES(env->event_info_mutex_);
160 // Circular dependencies mean we cannot see the definition of ArtJvmTiEnv so the mutex is simply
161 // asserted in the function.
162 // Note that the 'env' passed in must be the same env this EventMasks is associated with.
163 void DisableEvent(ArtJvmTiEnv* env, art::Thread* thread, ArtJvmtiEvent event);
164 // REQUIRES(env->event_info_mutex_);
Alex Light73afd322017-01-18 11:17:47 -0800165 bool IsEnabledAnywhere(ArtJvmtiEvent event);
166 // Make any changes to event masks needed for the given capability changes. If caps_added is true
167 // then caps is all the newly set capabilities of the jvmtiEnv. If it is false then caps is the
168 // set of all capabilities that were removed from the jvmtiEnv.
169 void HandleChangedCapabilities(const jvmtiCapabilities& caps, bool caps_added);
Andreas Gampe77708d92016-10-07 11:48:21 -0700170};
171
Alex Lightb284f8d2017-11-21 00:00:48 +0000172namespace impl {
173template <ArtJvmtiEvent kEvent> struct EventHandlerFunc { };
174} // namespace impl
175
Andreas Gampe77708d92016-10-07 11:48:21 -0700176// Helper class for event handling.
Andreas Gampe27fa96c2016-10-07 15:05:24 -0700177class EventHandler {
178 public:
179 EventHandler();
180 ~EventHandler();
Andreas Gampe77708d92016-10-07 11:48:21 -0700181
Alex Lightb7edcda2017-04-27 13:20:31 -0700182 // do cleanup for the event handler.
183 void Shutdown();
184
Andreas Gampe77708d92016-10-07 11:48:21 -0700185 // Register an env. It is assumed that this happens on env creation, that is, no events are
186 // enabled, yet.
Alex Lightb284f8d2017-11-21 00:00:48 +0000187 void RegisterArtJvmTiEnv(ArtJvmTiEnv* env) REQUIRES(!envs_lock_);
Andreas Gampe77708d92016-10-07 11:48:21 -0700188
Andreas Gampe3a7eb142017-01-19 21:59:22 -0800189 // Remove an env.
Alex Lightb284f8d2017-11-21 00:00:48 +0000190 void RemoveArtJvmTiEnv(ArtJvmTiEnv* env) REQUIRES(!envs_lock_);
Andreas Gampe3a7eb142017-01-19 21:59:22 -0800191
Alex Light40d87f42017-01-18 10:27:06 -0800192 bool IsEventEnabledAnywhere(ArtJvmtiEvent event) const {
Andreas Gampe77708d92016-10-07 11:48:21 -0700193 if (!EventMask::EventIsInRange(event)) {
194 return false;
195 }
196 return global_mask.Test(event);
197 }
198
Alex Light40d87f42017-01-18 10:27:06 -0800199 jvmtiError SetEvent(ArtJvmTiEnv* env,
200 art::Thread* thread,
201 ArtJvmtiEvent event,
Alex Lightb284f8d2017-11-21 00:00:48 +0000202 jvmtiEventMode mode)
203 REQUIRES(!envs_lock_);
Andreas Gampe77708d92016-10-07 11:48:21 -0700204
Alex Light9df79b72017-09-12 08:57:31 -0700205 // Dispatch event to all registered environments. Since this one doesn't have a JNIEnv* it doesn't
206 // matter if it has the mutator_lock.
Andreas Gampe983c1752017-01-23 19:46:56 -0800207 template <ArtJvmtiEvent kEvent, typename ...Args>
Alex Light40d87f42017-01-18 10:27:06 -0800208 ALWAYS_INLINE
Alex Lightb284f8d2017-11-21 00:00:48 +0000209 inline void DispatchEvent(art::Thread* thread, Args... args) const
210 REQUIRES(!envs_lock_);
Alex Light9df79b72017-09-12 08:57:31 -0700211
Alex Lightb7edcda2017-04-27 13:20:31 -0700212 // Dispatch event to all registered environments stashing exceptions as needed. This works since
213 // JNIEnv* is always the second argument if it is passed to an event. Needed since C++ does not
214 // allow partial template function specialization.
Alex Light9df79b72017-09-12 08:57:31 -0700215 //
216 // We need both of these since we want to make sure to push a stack frame when it is possible for
217 // the event to allocate local references.
Alex Lightb7edcda2017-04-27 13:20:31 -0700218 template <ArtJvmtiEvent kEvent, typename ...Args>
219 ALWAYS_INLINE
Alex Lightb284f8d2017-11-21 00:00:48 +0000220 inline void DispatchEvent(art::Thread* thread, JNIEnv* jnienv, Args... args) const
221 REQUIRES(!envs_lock_);
Andreas Gampe27fa96c2016-10-07 15:05:24 -0700222
Alex Light73afd322017-01-18 11:17:47 -0800223 // Tell the event handler capabilities were added/lost so it can adjust the sent events.If
224 // caps_added is true then caps is all the newly set capabilities of the jvmtiEnv. If it is false
225 // then caps is the set of all capabilities that were removed from the jvmtiEnv.
226 ALWAYS_INLINE
227 inline void HandleChangedCapabilities(ArtJvmTiEnv* env,
228 const jvmtiCapabilities& caps,
Alex Lightb284f8d2017-11-21 00:00:48 +0000229 bool added)
230 REQUIRES(!envs_lock_);
Alex Light73afd322017-01-18 11:17:47 -0800231
Alex Light9df79b72017-09-12 08:57:31 -0700232 // Dispatch event to the given environment, only.
233 template <ArtJvmtiEvent kEvent, typename ...Args>
234 ALWAYS_INLINE
Alex Lightb284f8d2017-11-21 00:00:48 +0000235 inline void DispatchEventOnEnv(ArtJvmTiEnv* env,
236 art::Thread* thread,
237 JNIEnv* jnienv,
238 Args... args) const
239 REQUIRES(!envs_lock_);
Alex Light9df79b72017-09-12 08:57:31 -0700240
241 // Dispatch event to the given environment, only.
242 template <ArtJvmtiEvent kEvent, typename ...Args>
243 ALWAYS_INLINE
Alex Lightb284f8d2017-11-21 00:00:48 +0000244 inline void DispatchEventOnEnv(ArtJvmTiEnv* env, art::Thread* thread, Args... args) const
245 REQUIRES(!envs_lock_);
Alex Light9df79b72017-09-12 08:57:31 -0700246
Andreas Gampe27fa96c2016-10-07 15:05:24 -0700247 private:
Alex Lightf6df1b52017-11-29 14:46:53 -0800248 void SetupTraceListener(JvmtiMethodTraceListener* listener, ArtJvmtiEvent event, bool enable);
249
Alex Lightf5d5eb12018-03-06 15:13:59 -0800250 // Specifically handle the FramePop event which it might not always be possible to turn off.
251 void SetupFramePopTraceListener(bool enable);
252
Alex Lightb284f8d2017-11-21 00:00:48 +0000253 template <ArtJvmtiEvent kEvent, typename ...Args>
254 ALWAYS_INLINE
255 inline std::vector<impl::EventHandlerFunc<kEvent>> CollectEvents(art::Thread* thread,
256 Args... args) const
257 REQUIRES(!envs_lock_);
258
Andreas Gampe983c1752017-01-23 19:46:56 -0800259 template <ArtJvmtiEvent kEvent>
Alex Light40d87f42017-01-18 10:27:06 -0800260 ALWAYS_INLINE
Alex Lightb284f8d2017-11-21 00:00:48 +0000261 inline bool ShouldDispatchOnThread(ArtJvmTiEnv* env, art::Thread* thread) const;
Alex Light9df79b72017-09-12 08:57:31 -0700262
263 template <ArtJvmtiEvent kEvent, typename ...Args>
264 ALWAYS_INLINE
Alex Lightb284f8d2017-11-21 00:00:48 +0000265 static inline void ExecuteCallback(impl::EventHandlerFunc<kEvent> handler,
266 JNIEnv* env,
267 Args... args)
268 REQUIRES(!envs_lock_);
Alex Light9df79b72017-09-12 08:57:31 -0700269
270 template <ArtJvmtiEvent kEvent, typename ...Args>
271 ALWAYS_INLINE
Alex Lightb284f8d2017-11-21 00:00:48 +0000272 static inline void ExecuteCallback(impl::EventHandlerFunc<kEvent> handler, Args... args)
273 REQUIRES(!envs_lock_);
274
275 // Public for use to collect dispatches
276 template <ArtJvmtiEvent kEvent, typename ...Args>
277 ALWAYS_INLINE
Alex Light9df79b72017-09-12 08:57:31 -0700278 inline bool ShouldDispatch(ArtJvmTiEnv* env, art::Thread* thread, Args... args) const;
Alex Light40d87f42017-01-18 10:27:06 -0800279
Alex Light73afd322017-01-18 11:17:47 -0800280 ALWAYS_INLINE
281 inline bool NeedsEventUpdate(ArtJvmTiEnv* env,
282 const jvmtiCapabilities& caps,
283 bool added);
284
285 // Recalculates the event mask for the given event.
286 ALWAYS_INLINE
Alex Lightb284f8d2017-11-21 00:00:48 +0000287 inline void RecalculateGlobalEventMask(ArtJvmtiEvent event) REQUIRES(!envs_lock_);
288 ALWAYS_INLINE
Alex Light2a96fe82018-01-22 17:45:02 -0800289 inline void RecalculateGlobalEventMaskLocked(ArtJvmtiEvent event) REQUIRES_SHARED(envs_lock_);
Alex Light73afd322017-01-18 11:17:47 -0800290
Andreas Gampe983c1752017-01-23 19:46:56 -0800291 template <ArtJvmtiEvent kEvent>
Alex Light6ac57502017-01-19 15:05:06 -0800292 ALWAYS_INLINE inline void DispatchClassFileLoadHookEvent(art::Thread* thread,
Andreas Gampe983c1752017-01-23 19:46:56 -0800293 JNIEnv* jnienv,
294 jclass class_being_redefined,
295 jobject loader,
296 const char* name,
297 jobject protection_domain,
298 jint class_data_len,
299 const unsigned char* class_data,
300 jint* new_class_data_len,
Alex Lightb284f8d2017-11-21 00:00:48 +0000301 unsigned char** new_class_data) const
302 REQUIRES(!envs_lock_);
Alex Light6ac57502017-01-19 15:05:06 -0800303
Alex Light40d87f42017-01-18 10:27:06 -0800304 void HandleEventType(ArtJvmtiEvent event, bool enable);
Alex Lightbebd7bd2017-07-25 14:05:52 -0700305 void HandleLocalAccessCapabilityAdded();
Alex Light0fa17862017-10-24 13:43:05 -0700306 void HandleBreakpointEventsChanged(bool enable);
Andreas Gampe27fa96c2016-10-07 15:05:24 -0700307
Alex Light77fee872017-09-05 14:51:49 -0700308 bool OtherMonitorEventsEnabledAnywhere(ArtJvmtiEvent event);
309
Alex Lightb284f8d2017-11-21 00:00:48 +0000310 // List of all JvmTiEnv objects that have been created, in their creation order. It is a std::list
311 // since we mostly access it by iterating over the entire thing, only ever append to the end, and
312 // need to be able to remove arbitrary elements from it.
313 std::list<ArtJvmTiEnv*> envs GUARDED_BY(envs_lock_);
314
315 // Top level lock. Nothing at all should be held when we lock this.
Alex Light2a96fe82018-01-22 17:45:02 -0800316 mutable art::ReaderWriterMutex envs_lock_
317 ACQUIRED_BEFORE(art::Locks::instrument_entrypoints_lock_);
Andreas Gampe27fa96c2016-10-07 15:05:24 -0700318
319 // A union of all enabled events, anywhere.
320 EventMask global_mask;
321
322 std::unique_ptr<JvmtiAllocationListener> alloc_listener_;
Alex Light8c2b9292017-11-09 13:21:01 -0800323 std::unique_ptr<JvmtiDdmChunkListener> ddm_listener_;
Andreas Gampe9b8c5882016-10-21 15:27:46 -0700324 std::unique_ptr<JvmtiGcPauseListener> gc_pause_listener_;
Alex Lightb7edcda2017-04-27 13:20:31 -0700325 std::unique_ptr<JvmtiMethodTraceListener> method_trace_listener_;
Alex Light77fee872017-09-05 14:51:49 -0700326 std::unique_ptr<JvmtiMonitorListener> monitor_listener_;
Alex Lighte814f9d2017-07-31 16:14:39 -0700327
328 // True if frame pop has ever been enabled. Since we store pointers to stack frames we need to
329 // continue to listen to this event even if it has been disabled.
330 // TODO We could remove the listeners once all jvmtiEnvs have drained their shadow-frame vectors.
331 bool frame_pop_enabled;
Andreas Gampe77708d92016-10-07 11:48:21 -0700332};
333
334} // namespace openjdkjvmti
335
Andreas Gampe06c42a52017-07-26 14:17:14 -0700336#endif // ART_OPENJDKJVMTI_EVENTS_H_