blob: abb15cc3295791fd0ab9ae5fc7ae62f54f58f9b4 [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;
Charles Munger5cc0e752018-11-09 12:30:46 -080038class JvmtiParkListener;
Andreas Gampe77708d92016-10-07 11:48:21 -070039
Alex Light73afd322017-01-18 11:17:47 -080040// an enum for ArtEvents. This differs from the JVMTI events only in that we distinguish between
41// retransformation capable and incapable loading
Alex Light8c2b9292017-11-09 13:21:01 -080042enum class ArtJvmtiEvent : jint {
Alex Light40d87f42017-01-18 10:27:06 -080043 kMinEventTypeVal = JVMTI_MIN_EVENT_TYPE_VAL,
44 kVmInit = JVMTI_EVENT_VM_INIT,
45 kVmDeath = JVMTI_EVENT_VM_DEATH,
46 kThreadStart = JVMTI_EVENT_THREAD_START,
47 kThreadEnd = JVMTI_EVENT_THREAD_END,
Alex Light73afd322017-01-18 11:17:47 -080048 kClassFileLoadHookNonRetransformable = JVMTI_EVENT_CLASS_FILE_LOAD_HOOK,
Alex Light40d87f42017-01-18 10:27:06 -080049 kClassLoad = JVMTI_EVENT_CLASS_LOAD,
50 kClassPrepare = JVMTI_EVENT_CLASS_PREPARE,
51 kVmStart = JVMTI_EVENT_VM_START,
52 kException = JVMTI_EVENT_EXCEPTION,
53 kExceptionCatch = JVMTI_EVENT_EXCEPTION_CATCH,
54 kSingleStep = JVMTI_EVENT_SINGLE_STEP,
55 kFramePop = JVMTI_EVENT_FRAME_POP,
56 kBreakpoint = JVMTI_EVENT_BREAKPOINT,
57 kFieldAccess = JVMTI_EVENT_FIELD_ACCESS,
58 kFieldModification = JVMTI_EVENT_FIELD_MODIFICATION,
59 kMethodEntry = JVMTI_EVENT_METHOD_ENTRY,
60 kMethodExit = JVMTI_EVENT_METHOD_EXIT,
61 kNativeMethodBind = JVMTI_EVENT_NATIVE_METHOD_BIND,
62 kCompiledMethodLoad = JVMTI_EVENT_COMPILED_METHOD_LOAD,
63 kCompiledMethodUnload = JVMTI_EVENT_COMPILED_METHOD_UNLOAD,
64 kDynamicCodeGenerated = JVMTI_EVENT_DYNAMIC_CODE_GENERATED,
65 kDataDumpRequest = JVMTI_EVENT_DATA_DUMP_REQUEST,
66 kMonitorWait = JVMTI_EVENT_MONITOR_WAIT,
67 kMonitorWaited = JVMTI_EVENT_MONITOR_WAITED,
68 kMonitorContendedEnter = JVMTI_EVENT_MONITOR_CONTENDED_ENTER,
69 kMonitorContendedEntered = JVMTI_EVENT_MONITOR_CONTENDED_ENTERED,
70 kResourceExhausted = JVMTI_EVENT_RESOURCE_EXHAUSTED,
71 kGarbageCollectionStart = JVMTI_EVENT_GARBAGE_COLLECTION_START,
72 kGarbageCollectionFinish = JVMTI_EVENT_GARBAGE_COLLECTION_FINISH,
73 kObjectFree = JVMTI_EVENT_OBJECT_FREE,
74 kVmObjectAlloc = JVMTI_EVENT_VM_OBJECT_ALLOC,
Alex Light73afd322017-01-18 11:17:47 -080075 kClassFileLoadHookRetransformable = JVMTI_MAX_EVENT_TYPE_VAL + 1,
Alex Light8c2b9292017-11-09 13:21:01 -080076 kDdmPublishChunk = JVMTI_MAX_EVENT_TYPE_VAL + 2,
77 kMaxEventTypeVal = kDdmPublishChunk,
Alex Light40d87f42017-01-18 10:27:06 -080078};
79
Alex Light8c2b9292017-11-09 13:21:01 -080080using ArtJvmtiEventDdmPublishChunk = void (*)(jvmtiEnv *jvmti_env,
81 JNIEnv* jni_env,
82 jint data_type,
83 jint data_len,
84 const jbyte* data);
85
86struct ArtJvmtiEventCallbacks : jvmtiEventCallbacks {
87 ArtJvmtiEventCallbacks() : DdmPublishChunk(nullptr) {
88 memset(this, 0, sizeof(jvmtiEventCallbacks));
89 }
90
91 // Copies extension functions from other callback struct if it exists. There must not have been
92 // any modifications to this struct when it is called.
93 void CopyExtensionsFrom(const ArtJvmtiEventCallbacks* cb);
94
95 jvmtiError Set(jint index, jvmtiExtensionEvent cb);
96
97 ArtJvmtiEventDdmPublishChunk DdmPublishChunk;
98};
99
100bool IsExtensionEvent(jint e);
101bool IsExtensionEvent(ArtJvmtiEvent e);
102
Alex Light40d87f42017-01-18 10:27:06 -0800103// Convert a jvmtiEvent into a ArtJvmtiEvent
104ALWAYS_INLINE static inline ArtJvmtiEvent GetArtJvmtiEvent(ArtJvmTiEnv* env, jvmtiEvent e);
105
Alex Light73afd322017-01-18 11:17:47 -0800106static inline jvmtiEvent GetJvmtiEvent(ArtJvmtiEvent e) {
107 if (UNLIKELY(e == ArtJvmtiEvent::kClassFileLoadHookRetransformable)) {
108 return JVMTI_EVENT_CLASS_FILE_LOAD_HOOK;
109 } else {
110 return static_cast<jvmtiEvent>(e);
111 }
Alex Light40d87f42017-01-18 10:27:06 -0800112}
113
Andreas Gampe77708d92016-10-07 11:48:21 -0700114struct EventMask {
Alex Light40d87f42017-01-18 10:27:06 -0800115 static constexpr size_t kEventsSize =
116 static_cast<size_t>(ArtJvmtiEvent::kMaxEventTypeVal) -
117 static_cast<size_t>(ArtJvmtiEvent::kMinEventTypeVal) + 1;
Andreas Gampe77708d92016-10-07 11:48:21 -0700118 std::bitset<kEventsSize> bit_set;
119
Alex Light40d87f42017-01-18 10:27:06 -0800120 static bool EventIsInRange(ArtJvmtiEvent event) {
121 return event >= ArtJvmtiEvent::kMinEventTypeVal && event <= ArtJvmtiEvent::kMaxEventTypeVal;
Andreas Gampe77708d92016-10-07 11:48:21 -0700122 }
123
Alex Light40d87f42017-01-18 10:27:06 -0800124 void Set(ArtJvmtiEvent event, bool value = true) {
Andreas Gampe77708d92016-10-07 11:48:21 -0700125 DCHECK(EventIsInRange(event));
Alex Light40d87f42017-01-18 10:27:06 -0800126 bit_set.set(static_cast<size_t>(event) - static_cast<size_t>(ArtJvmtiEvent::kMinEventTypeVal),
127 value);
Andreas Gampe77708d92016-10-07 11:48:21 -0700128 }
129
Alex Light40d87f42017-01-18 10:27:06 -0800130 bool Test(ArtJvmtiEvent event) const {
Andreas Gampe77708d92016-10-07 11:48:21 -0700131 DCHECK(EventIsInRange(event));
Alex Light40d87f42017-01-18 10:27:06 -0800132 return bit_set.test(
133 static_cast<size_t>(event) - static_cast<size_t>(ArtJvmtiEvent::kMinEventTypeVal));
Andreas Gampe77708d92016-10-07 11:48:21 -0700134 }
135};
136
137struct EventMasks {
138 // The globally enabled events.
139 EventMask global_event_mask;
140
141 // The per-thread enabled events.
142
143 // It is not enough to store a Thread pointer, as these may be reused. Use the pointer and the
144 // thread id.
145 // Note: We could just use the tid like tracing does.
146 using UniqueThread = std::pair<art::Thread*, uint32_t>;
147 // TODO: Native thread objects are immovable, so we can use them as keys in an (unordered) map,
148 // if necessary.
149 std::vector<std::pair<UniqueThread, EventMask>> thread_event_masks;
150
151 // A union of the per-thread events, for fast-pathing.
152 EventMask unioned_thread_event_mask;
153
154 EventMask& GetEventMask(art::Thread* thread);
155 EventMask* GetEventMaskOrNull(art::Thread* thread);
Alex Light74c84402017-11-29 15:26:38 -0800156 // Circular dependencies mean we cannot see the definition of ArtJvmTiEnv so the mutex is simply
157 // asserted in the function.
158 // Note that the 'env' passed in must be the same env this EventMasks is associated with.
159 void EnableEvent(ArtJvmTiEnv* env, art::Thread* thread, ArtJvmtiEvent event);
160 // REQUIRES(env->event_info_mutex_);
161 // Circular dependencies mean we cannot see the definition of ArtJvmTiEnv so the mutex is simply
162 // asserted in the function.
163 // Note that the 'env' passed in must be the same env this EventMasks is associated with.
164 void DisableEvent(ArtJvmTiEnv* env, art::Thread* thread, ArtJvmtiEvent event);
165 // REQUIRES(env->event_info_mutex_);
Alex Light73afd322017-01-18 11:17:47 -0800166 bool IsEnabledAnywhere(ArtJvmtiEvent event);
167 // Make any changes to event masks needed for the given capability changes. If caps_added is true
168 // then caps is all the newly set capabilities of the jvmtiEnv. If it is false then caps is the
169 // set of all capabilities that were removed from the jvmtiEnv.
170 void HandleChangedCapabilities(const jvmtiCapabilities& caps, bool caps_added);
Andreas Gampe77708d92016-10-07 11:48:21 -0700171};
172
Alex Lightb284f8d2017-11-21 00:00:48 +0000173namespace impl {
174template <ArtJvmtiEvent kEvent> struct EventHandlerFunc { };
175} // namespace impl
176
Andreas Gampe77708d92016-10-07 11:48:21 -0700177// Helper class for event handling.
Andreas Gampe27fa96c2016-10-07 15:05:24 -0700178class EventHandler {
179 public:
180 EventHandler();
181 ~EventHandler();
Andreas Gampe77708d92016-10-07 11:48:21 -0700182
Alex Lightb7edcda2017-04-27 13:20:31 -0700183 // do cleanup for the event handler.
184 void Shutdown();
185
Andreas Gampe77708d92016-10-07 11:48:21 -0700186 // Register an env. It is assumed that this happens on env creation, that is, no events are
187 // enabled, yet.
Alex Lightb284f8d2017-11-21 00:00:48 +0000188 void RegisterArtJvmTiEnv(ArtJvmTiEnv* env) REQUIRES(!envs_lock_);
Andreas Gampe77708d92016-10-07 11:48:21 -0700189
Andreas Gampe3a7eb142017-01-19 21:59:22 -0800190 // Remove an env.
Alex Lightb284f8d2017-11-21 00:00:48 +0000191 void RemoveArtJvmTiEnv(ArtJvmTiEnv* env) REQUIRES(!envs_lock_);
Andreas Gampe3a7eb142017-01-19 21:59:22 -0800192
Alex Light40d87f42017-01-18 10:27:06 -0800193 bool IsEventEnabledAnywhere(ArtJvmtiEvent event) const {
Andreas Gampe77708d92016-10-07 11:48:21 -0700194 if (!EventMask::EventIsInRange(event)) {
195 return false;
196 }
197 return global_mask.Test(event);
198 }
199
Alex Light40d87f42017-01-18 10:27:06 -0800200 jvmtiError SetEvent(ArtJvmTiEnv* env,
201 art::Thread* thread,
202 ArtJvmtiEvent event,
Alex Lightb284f8d2017-11-21 00:00:48 +0000203 jvmtiEventMode mode)
204 REQUIRES(!envs_lock_);
Andreas Gampe77708d92016-10-07 11:48:21 -0700205
Alex Light9df79b72017-09-12 08:57:31 -0700206 // Dispatch event to all registered environments. Since this one doesn't have a JNIEnv* it doesn't
207 // matter if it has the mutator_lock.
Andreas Gampe983c1752017-01-23 19:46:56 -0800208 template <ArtJvmtiEvent kEvent, typename ...Args>
Alex Light40d87f42017-01-18 10:27:06 -0800209 ALWAYS_INLINE
Alex Lightb284f8d2017-11-21 00:00:48 +0000210 inline void DispatchEvent(art::Thread* thread, Args... args) const
211 REQUIRES(!envs_lock_);
Alex Light9df79b72017-09-12 08:57:31 -0700212
Alex Lightb7edcda2017-04-27 13:20:31 -0700213 // Dispatch event to all registered environments stashing exceptions as needed. This works since
214 // JNIEnv* is always the second argument if it is passed to an event. Needed since C++ does not
215 // allow partial template function specialization.
Alex Light9df79b72017-09-12 08:57:31 -0700216 //
217 // We need both of these since we want to make sure to push a stack frame when it is possible for
218 // the event to allocate local references.
Alex Lightb7edcda2017-04-27 13:20:31 -0700219 template <ArtJvmtiEvent kEvent, typename ...Args>
220 ALWAYS_INLINE
Alex Lightb284f8d2017-11-21 00:00:48 +0000221 inline void DispatchEvent(art::Thread* thread, JNIEnv* jnienv, Args... args) const
222 REQUIRES(!envs_lock_);
Andreas Gampe27fa96c2016-10-07 15:05:24 -0700223
Alex Light73afd322017-01-18 11:17:47 -0800224 // Tell the event handler capabilities were added/lost so it can adjust the sent events.If
225 // caps_added is true then caps is all the newly set capabilities of the jvmtiEnv. If it is false
226 // then caps is the set of all capabilities that were removed from the jvmtiEnv.
227 ALWAYS_INLINE
228 inline void HandleChangedCapabilities(ArtJvmTiEnv* env,
229 const jvmtiCapabilities& caps,
Alex Lightb284f8d2017-11-21 00:00:48 +0000230 bool added)
231 REQUIRES(!envs_lock_);
Alex Light73afd322017-01-18 11:17:47 -0800232
Alex Light9df79b72017-09-12 08:57:31 -0700233 // Dispatch event to the given environment, only.
234 template <ArtJvmtiEvent kEvent, typename ...Args>
235 ALWAYS_INLINE
Alex Lightb284f8d2017-11-21 00:00:48 +0000236 inline void DispatchEventOnEnv(ArtJvmTiEnv* env,
237 art::Thread* thread,
238 JNIEnv* jnienv,
239 Args... args) const
240 REQUIRES(!envs_lock_);
Alex Light9df79b72017-09-12 08:57:31 -0700241
242 // Dispatch event to the given environment, only.
243 template <ArtJvmtiEvent kEvent, typename ...Args>
244 ALWAYS_INLINE
Alex Lightb284f8d2017-11-21 00:00:48 +0000245 inline void DispatchEventOnEnv(ArtJvmTiEnv* env, art::Thread* thread, Args... args) const
246 REQUIRES(!envs_lock_);
Alex Light9df79b72017-09-12 08:57:31 -0700247
Andreas Gampe27fa96c2016-10-07 15:05:24 -0700248 private:
Alex Lightf6df1b52017-11-29 14:46:53 -0800249 void SetupTraceListener(JvmtiMethodTraceListener* listener, ArtJvmtiEvent event, bool enable);
250
Alex Lightf5d5eb12018-03-06 15:13:59 -0800251 // Specifically handle the FramePop event which it might not always be possible to turn off.
252 void SetupFramePopTraceListener(bool enable);
253
Alex Lightb284f8d2017-11-21 00:00:48 +0000254 template <ArtJvmtiEvent kEvent, typename ...Args>
255 ALWAYS_INLINE
256 inline std::vector<impl::EventHandlerFunc<kEvent>> CollectEvents(art::Thread* thread,
257 Args... args) const
258 REQUIRES(!envs_lock_);
259
Andreas Gampe983c1752017-01-23 19:46:56 -0800260 template <ArtJvmtiEvent kEvent>
Alex Light40d87f42017-01-18 10:27:06 -0800261 ALWAYS_INLINE
Alex Lightb284f8d2017-11-21 00:00:48 +0000262 inline bool ShouldDispatchOnThread(ArtJvmTiEnv* env, art::Thread* thread) const;
Alex Light9df79b72017-09-12 08:57:31 -0700263
264 template <ArtJvmtiEvent kEvent, typename ...Args>
265 ALWAYS_INLINE
Alex Lightb284f8d2017-11-21 00:00:48 +0000266 static inline void ExecuteCallback(impl::EventHandlerFunc<kEvent> handler,
267 JNIEnv* env,
268 Args... args)
269 REQUIRES(!envs_lock_);
Alex Light9df79b72017-09-12 08:57:31 -0700270
271 template <ArtJvmtiEvent kEvent, typename ...Args>
272 ALWAYS_INLINE
Alex Lightb284f8d2017-11-21 00:00:48 +0000273 static inline void ExecuteCallback(impl::EventHandlerFunc<kEvent> handler, Args... args)
274 REQUIRES(!envs_lock_);
275
276 // Public for use to collect dispatches
277 template <ArtJvmtiEvent kEvent, typename ...Args>
278 ALWAYS_INLINE
Alex Light9df79b72017-09-12 08:57:31 -0700279 inline bool ShouldDispatch(ArtJvmTiEnv* env, art::Thread* thread, Args... args) const;
Alex Light40d87f42017-01-18 10:27:06 -0800280
Alex Light73afd322017-01-18 11:17:47 -0800281 ALWAYS_INLINE
282 inline bool NeedsEventUpdate(ArtJvmTiEnv* env,
283 const jvmtiCapabilities& caps,
284 bool added);
285
286 // Recalculates the event mask for the given event.
287 ALWAYS_INLINE
Alex Lightb284f8d2017-11-21 00:00:48 +0000288 inline void RecalculateGlobalEventMask(ArtJvmtiEvent event) REQUIRES(!envs_lock_);
289 ALWAYS_INLINE
Alex Light2a96fe82018-01-22 17:45:02 -0800290 inline void RecalculateGlobalEventMaskLocked(ArtJvmtiEvent event) REQUIRES_SHARED(envs_lock_);
Alex Light73afd322017-01-18 11:17:47 -0800291
Andreas Gampe983c1752017-01-23 19:46:56 -0800292 template <ArtJvmtiEvent kEvent>
Alex Light6ac57502017-01-19 15:05:06 -0800293 ALWAYS_INLINE inline void DispatchClassFileLoadHookEvent(art::Thread* thread,
Andreas Gampe983c1752017-01-23 19:46:56 -0800294 JNIEnv* jnienv,
295 jclass class_being_redefined,
296 jobject loader,
297 const char* name,
298 jobject protection_domain,
299 jint class_data_len,
300 const unsigned char* class_data,
301 jint* new_class_data_len,
Alex Lightb284f8d2017-11-21 00:00:48 +0000302 unsigned char** new_class_data) const
303 REQUIRES(!envs_lock_);
Alex Light6ac57502017-01-19 15:05:06 -0800304
Alex Light0aa7a5a2018-10-10 15:58:14 +0000305 template <ArtJvmtiEvent kEvent>
306 ALWAYS_INLINE inline void DispatchClassLoadOrPrepareEvent(art::Thread* thread,
307 JNIEnv* jnienv,
308 jthread jni_thread,
309 jclass klass) const
310 REQUIRES(!envs_lock_);
311
Alex Light40d87f42017-01-18 10:27:06 -0800312 void HandleEventType(ArtJvmtiEvent event, bool enable);
Alex Lightbebd7bd2017-07-25 14:05:52 -0700313 void HandleLocalAccessCapabilityAdded();
Alex Light0fa17862017-10-24 13:43:05 -0700314 void HandleBreakpointEventsChanged(bool enable);
Andreas Gampe27fa96c2016-10-07 15:05:24 -0700315
Alex Light77fee872017-09-05 14:51:49 -0700316 bool OtherMonitorEventsEnabledAnywhere(ArtJvmtiEvent event);
317
Alex Lightb284f8d2017-11-21 00:00:48 +0000318 // List of all JvmTiEnv objects that have been created, in their creation order. It is a std::list
319 // since we mostly access it by iterating over the entire thing, only ever append to the end, and
320 // need to be able to remove arbitrary elements from it.
321 std::list<ArtJvmTiEnv*> envs GUARDED_BY(envs_lock_);
322
323 // Top level lock. Nothing at all should be held when we lock this.
Alex Light2a96fe82018-01-22 17:45:02 -0800324 mutable art::ReaderWriterMutex envs_lock_
325 ACQUIRED_BEFORE(art::Locks::instrument_entrypoints_lock_);
Andreas Gampe27fa96c2016-10-07 15:05:24 -0700326
327 // A union of all enabled events, anywhere.
328 EventMask global_mask;
329
330 std::unique_ptr<JvmtiAllocationListener> alloc_listener_;
Alex Light8c2b9292017-11-09 13:21:01 -0800331 std::unique_ptr<JvmtiDdmChunkListener> ddm_listener_;
Andreas Gampe9b8c5882016-10-21 15:27:46 -0700332 std::unique_ptr<JvmtiGcPauseListener> gc_pause_listener_;
Alex Lightb7edcda2017-04-27 13:20:31 -0700333 std::unique_ptr<JvmtiMethodTraceListener> method_trace_listener_;
Alex Light77fee872017-09-05 14:51:49 -0700334 std::unique_ptr<JvmtiMonitorListener> monitor_listener_;
Charles Munger5cc0e752018-11-09 12:30:46 -0800335 std::unique_ptr<JvmtiParkListener> park_listener_;
Alex Lighte814f9d2017-07-31 16:14:39 -0700336
337 // True if frame pop has ever been enabled. Since we store pointers to stack frames we need to
338 // continue to listen to this event even if it has been disabled.
339 // TODO We could remove the listeners once all jvmtiEnvs have drained their shadow-frame vectors.
340 bool frame_pop_enabled;
Andreas Gampe77708d92016-10-07 11:48:21 -0700341};
342
343} // namespace openjdkjvmti
344
Andreas Gampe06c42a52017-07-26 14:17:14 -0700345#endif // ART_OPENJDKJVMTI_EVENTS_H_