blob: be0839bdc5e1f03ed914a04f353ee40b9089d96f [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>
Alex Lightb7c640d2019-03-20 15:52:13 -070021#include <unordered_map>
Andreas Gampe77708d92016-10-07 11:48:21 -070022#include <vector>
23
Andreas Gampe57943812017-12-06 21:39:13 -080024#include <android-base/logging.h>
Alex Light66834462019-04-08 16:28:29 +000025#include <android-base/thread_annotations.h>
Andreas Gampe57943812017-12-06 21:39:13 -080026
Alex Lightb7c640d2019-03-20 15:52:13 -070027#include "android-base/thread_annotations.h"
Andreas Gampe57943812017-12-06 21:39:13 -080028#include "base/macros.h"
29#include "base/mutex.h"
Andreas Gampe77708d92016-10-07 11:48:21 -070030#include "jvmti.h"
Alex Lightb7c640d2019-03-20 15:52:13 -070031#include "managed_stack.h"
Andreas Gampe77708d92016-10-07 11:48:21 -070032#include "thread.h"
33
34namespace openjdkjvmti {
35
36struct ArtJvmTiEnv;
Alex Light986914b2019-11-19 01:12:25 +000037class JvmtiEventAllocationListener;
Alex Light8c2b9292017-11-09 13:21:01 -080038class JvmtiDdmChunkListener;
Andreas Gampe9b8c5882016-10-21 15:27:46 -070039class JvmtiGcPauseListener;
Alex Lightb7edcda2017-04-27 13:20:31 -070040class JvmtiMethodTraceListener;
Alex Light77fee872017-09-05 14:51:49 -070041class JvmtiMonitorListener;
Charles Munger5cc0e752018-11-09 12:30:46 -080042class JvmtiParkListener;
Andreas Gampe77708d92016-10-07 11:48:21 -070043
Alex Light73afd322017-01-18 11:17:47 -080044// an enum for ArtEvents. This differs from the JVMTI events only in that we distinguish between
45// retransformation capable and incapable loading
Alex Light8c2b9292017-11-09 13:21:01 -080046enum class ArtJvmtiEvent : jint {
Alex Light40d87f42017-01-18 10:27:06 -080047 kMinEventTypeVal = JVMTI_MIN_EVENT_TYPE_VAL,
48 kVmInit = JVMTI_EVENT_VM_INIT,
49 kVmDeath = JVMTI_EVENT_VM_DEATH,
50 kThreadStart = JVMTI_EVENT_THREAD_START,
51 kThreadEnd = JVMTI_EVENT_THREAD_END,
Alex Light73afd322017-01-18 11:17:47 -080052 kClassFileLoadHookNonRetransformable = JVMTI_EVENT_CLASS_FILE_LOAD_HOOK,
Alex Light40d87f42017-01-18 10:27:06 -080053 kClassLoad = JVMTI_EVENT_CLASS_LOAD,
54 kClassPrepare = JVMTI_EVENT_CLASS_PREPARE,
55 kVmStart = JVMTI_EVENT_VM_START,
56 kException = JVMTI_EVENT_EXCEPTION,
57 kExceptionCatch = JVMTI_EVENT_EXCEPTION_CATCH,
58 kSingleStep = JVMTI_EVENT_SINGLE_STEP,
59 kFramePop = JVMTI_EVENT_FRAME_POP,
60 kBreakpoint = JVMTI_EVENT_BREAKPOINT,
61 kFieldAccess = JVMTI_EVENT_FIELD_ACCESS,
62 kFieldModification = JVMTI_EVENT_FIELD_MODIFICATION,
63 kMethodEntry = JVMTI_EVENT_METHOD_ENTRY,
64 kMethodExit = JVMTI_EVENT_METHOD_EXIT,
65 kNativeMethodBind = JVMTI_EVENT_NATIVE_METHOD_BIND,
66 kCompiledMethodLoad = JVMTI_EVENT_COMPILED_METHOD_LOAD,
67 kCompiledMethodUnload = JVMTI_EVENT_COMPILED_METHOD_UNLOAD,
68 kDynamicCodeGenerated = JVMTI_EVENT_DYNAMIC_CODE_GENERATED,
69 kDataDumpRequest = JVMTI_EVENT_DATA_DUMP_REQUEST,
70 kMonitorWait = JVMTI_EVENT_MONITOR_WAIT,
71 kMonitorWaited = JVMTI_EVENT_MONITOR_WAITED,
72 kMonitorContendedEnter = JVMTI_EVENT_MONITOR_CONTENDED_ENTER,
73 kMonitorContendedEntered = JVMTI_EVENT_MONITOR_CONTENDED_ENTERED,
74 kResourceExhausted = JVMTI_EVENT_RESOURCE_EXHAUSTED,
75 kGarbageCollectionStart = JVMTI_EVENT_GARBAGE_COLLECTION_START,
76 kGarbageCollectionFinish = JVMTI_EVENT_GARBAGE_COLLECTION_FINISH,
77 kObjectFree = JVMTI_EVENT_OBJECT_FREE,
78 kVmObjectAlloc = JVMTI_EVENT_VM_OBJECT_ALLOC,
Alex Lightb7c640d2019-03-20 15:52:13 -070079 // Internal event to mark a ClassFileLoadHook as one created with the can_retransform_classes
80 // capability.
Alex Light73afd322017-01-18 11:17:47 -080081 kClassFileLoadHookRetransformable = JVMTI_MAX_EVENT_TYPE_VAL + 1,
Alex Light8c2b9292017-11-09 13:21:01 -080082 kDdmPublishChunk = JVMTI_MAX_EVENT_TYPE_VAL + 2,
Alex Light72d7e942019-07-23 13:10:20 -070083 kObsoleteObjectCreated = JVMTI_MAX_EVENT_TYPE_VAL + 3,
Alex Lightd55b8442019-10-15 15:46:07 -070084 kStructuralDexFileLoadHook = JVMTI_MAX_EVENT_TYPE_VAL + 4,
85 kMaxNormalEventTypeVal = kStructuralDexFileLoadHook,
Alex Lightb7c640d2019-03-20 15:52:13 -070086
87 // All that follow are events used to implement internal JVMTI functions. They are not settable
88 // directly by agents.
89 kMinInternalEventTypeVal = kMaxNormalEventTypeVal + 1,
90
91 // Internal event we use to implement the ForceEarlyReturn functions.
92 kForceEarlyReturnUpdateReturnValue = kMinInternalEventTypeVal,
93 kMaxInternalEventTypeVal = kForceEarlyReturnUpdateReturnValue,
94
95 kMaxEventTypeVal = kMaxInternalEventTypeVal,
Alex Light40d87f42017-01-18 10:27:06 -080096};
97
Alex Lightb7c640d2019-03-20 15:52:13 -070098constexpr jint kInternalEventCount = static_cast<jint>(ArtJvmtiEvent::kMaxInternalEventTypeVal) -
99 static_cast<jint>(ArtJvmtiEvent::kMinInternalEventTypeVal) + 1;
100
Alex Light8c2b9292017-11-09 13:21:01 -0800101using ArtJvmtiEventDdmPublishChunk = void (*)(jvmtiEnv *jvmti_env,
Alex Light8c2b9292017-11-09 13:21:01 -0800102 jint data_type,
103 jint data_len,
104 const jbyte* data);
105
Alex Light72d7e942019-07-23 13:10:20 -0700106using ArtJvmtiEventObsoleteObjectCreated = void (*)(jvmtiEnv *jvmti_env,
107 jlong* obsolete_tag,
108 jlong* new_tag);
109
Alex Lightd55b8442019-10-15 15:46:07 -0700110using ArtJvmtiEventStructuralDexFileLoadHook = void (*)(jvmtiEnv *jvmti_env,
111 JNIEnv* jni_env,
112 jclass class_being_redefined,
113 jobject loader,
114 const char* name,
115 jobject protection_domain,
116 jint dex_data_len,
117 const unsigned char* dex_data,
118 jint* new_dex_data_len,
119 unsigned char** new_dex_data);
120
Alex Lightb7c640d2019-03-20 15:52:13 -0700121// It is not enough to store a Thread pointer, as these may be reused. Use the pointer and the
122// thread id.
123// Note: We could just use the tid like tracing does.
124using UniqueThread = std::pair<art::Thread*, uint32_t>;
125
126struct UniqueThreadHasher {
127 std::size_t operator()(const UniqueThread& k) const {
128 return std::hash<uint32_t>{}(k.second) ^ (std::hash<void*>{}(k.first) << 1);
129 }
130};
131
Alex Light8c2b9292017-11-09 13:21:01 -0800132struct ArtJvmtiEventCallbacks : jvmtiEventCallbacks {
Alex Lightd55b8442019-10-15 15:46:07 -0700133 ArtJvmtiEventCallbacks()
134 : DdmPublishChunk(nullptr),
135 ObsoleteObjectCreated(nullptr),
136 StructuralDexFileLoadHook(nullptr) {
Alex Light8c2b9292017-11-09 13:21:01 -0800137 memset(this, 0, sizeof(jvmtiEventCallbacks));
138 }
139
140 // Copies extension functions from other callback struct if it exists. There must not have been
141 // any modifications to this struct when it is called.
142 void CopyExtensionsFrom(const ArtJvmtiEventCallbacks* cb);
143
144 jvmtiError Set(jint index, jvmtiExtensionEvent cb);
145
146 ArtJvmtiEventDdmPublishChunk DdmPublishChunk;
Alex Light72d7e942019-07-23 13:10:20 -0700147 ArtJvmtiEventObsoleteObjectCreated ObsoleteObjectCreated;
Alex Lightd55b8442019-10-15 15:46:07 -0700148 ArtJvmtiEventStructuralDexFileLoadHook StructuralDexFileLoadHook;
Alex Light8c2b9292017-11-09 13:21:01 -0800149};
150
151bool IsExtensionEvent(jint e);
152bool IsExtensionEvent(ArtJvmtiEvent e);
153
Alex Light40d87f42017-01-18 10:27:06 -0800154// Convert a jvmtiEvent into a ArtJvmtiEvent
155ALWAYS_INLINE static inline ArtJvmtiEvent GetArtJvmtiEvent(ArtJvmTiEnv* env, jvmtiEvent e);
156
Alex Light73afd322017-01-18 11:17:47 -0800157static inline jvmtiEvent GetJvmtiEvent(ArtJvmtiEvent e) {
158 if (UNLIKELY(e == ArtJvmtiEvent::kClassFileLoadHookRetransformable)) {
159 return JVMTI_EVENT_CLASS_FILE_LOAD_HOOK;
160 } else {
161 return static_cast<jvmtiEvent>(e);
162 }
Alex Light40d87f42017-01-18 10:27:06 -0800163}
164
Andreas Gampe77708d92016-10-07 11:48:21 -0700165struct EventMask {
Alex Light40d87f42017-01-18 10:27:06 -0800166 static constexpr size_t kEventsSize =
167 static_cast<size_t>(ArtJvmtiEvent::kMaxEventTypeVal) -
168 static_cast<size_t>(ArtJvmtiEvent::kMinEventTypeVal) + 1;
Andreas Gampe77708d92016-10-07 11:48:21 -0700169 std::bitset<kEventsSize> bit_set;
170
Alex Light40d87f42017-01-18 10:27:06 -0800171 static bool EventIsInRange(ArtJvmtiEvent event) {
172 return event >= ArtJvmtiEvent::kMinEventTypeVal && event <= ArtJvmtiEvent::kMaxEventTypeVal;
Andreas Gampe77708d92016-10-07 11:48:21 -0700173 }
174
Alex Light40d87f42017-01-18 10:27:06 -0800175 void Set(ArtJvmtiEvent event, bool value = true) {
Andreas Gampe77708d92016-10-07 11:48:21 -0700176 DCHECK(EventIsInRange(event));
Alex Light40d87f42017-01-18 10:27:06 -0800177 bit_set.set(static_cast<size_t>(event) - static_cast<size_t>(ArtJvmtiEvent::kMinEventTypeVal),
178 value);
Andreas Gampe77708d92016-10-07 11:48:21 -0700179 }
180
Alex Light40d87f42017-01-18 10:27:06 -0800181 bool Test(ArtJvmtiEvent event) const {
Andreas Gampe77708d92016-10-07 11:48:21 -0700182 DCHECK(EventIsInRange(event));
Alex Light40d87f42017-01-18 10:27:06 -0800183 return bit_set.test(
184 static_cast<size_t>(event) - static_cast<size_t>(ArtJvmtiEvent::kMinEventTypeVal));
Andreas Gampe77708d92016-10-07 11:48:21 -0700185 }
186};
187
188struct EventMasks {
189 // The globally enabled events.
190 EventMask global_event_mask;
191
192 // The per-thread enabled events.
193
Andreas Gampe77708d92016-10-07 11:48:21 -0700194 // TODO: Native thread objects are immovable, so we can use them as keys in an (unordered) map,
195 // if necessary.
196 std::vector<std::pair<UniqueThread, EventMask>> thread_event_masks;
197
198 // A union of the per-thread events, for fast-pathing.
199 EventMask unioned_thread_event_mask;
200
201 EventMask& GetEventMask(art::Thread* thread);
202 EventMask* GetEventMaskOrNull(art::Thread* thread);
Alex Light74c84402017-11-29 15:26:38 -0800203 // Circular dependencies mean we cannot see the definition of ArtJvmTiEnv so the mutex is simply
204 // asserted in the function.
205 // Note that the 'env' passed in must be the same env this EventMasks is associated with.
206 void EnableEvent(ArtJvmTiEnv* env, art::Thread* thread, ArtJvmtiEvent event);
207 // REQUIRES(env->event_info_mutex_);
208 // Circular dependencies mean we cannot see the definition of ArtJvmTiEnv so the mutex is simply
209 // asserted in the function.
210 // Note that the 'env' passed in must be the same env this EventMasks is associated with.
211 void DisableEvent(ArtJvmTiEnv* env, art::Thread* thread, ArtJvmtiEvent event);
212 // REQUIRES(env->event_info_mutex_);
Alex Light73afd322017-01-18 11:17:47 -0800213 bool IsEnabledAnywhere(ArtJvmtiEvent event);
214 // Make any changes to event masks needed for the given capability changes. If caps_added is true
215 // then caps is all the newly set capabilities of the jvmtiEnv. If it is false then caps is the
216 // set of all capabilities that were removed from the jvmtiEnv.
217 void HandleChangedCapabilities(const jvmtiCapabilities& caps, bool caps_added);
Andreas Gampe77708d92016-10-07 11:48:21 -0700218};
219
Alex Lightb284f8d2017-11-21 00:00:48 +0000220namespace impl {
221template <ArtJvmtiEvent kEvent> struct EventHandlerFunc { };
222} // namespace impl
223
Andreas Gampe77708d92016-10-07 11:48:21 -0700224// Helper class for event handling.
Andreas Gampe27fa96c2016-10-07 15:05:24 -0700225class EventHandler {
226 public:
227 EventHandler();
228 ~EventHandler();
Andreas Gampe77708d92016-10-07 11:48:21 -0700229
Alex Lightb7edcda2017-04-27 13:20:31 -0700230 // do cleanup for the event handler.
231 void Shutdown();
232
Andreas Gampe77708d92016-10-07 11:48:21 -0700233 // Register an env. It is assumed that this happens on env creation, that is, no events are
234 // enabled, yet.
Alex Lightb284f8d2017-11-21 00:00:48 +0000235 void RegisterArtJvmTiEnv(ArtJvmTiEnv* env) REQUIRES(!envs_lock_);
Andreas Gampe77708d92016-10-07 11:48:21 -0700236
Andreas Gampe3a7eb142017-01-19 21:59:22 -0800237 // Remove an env.
Alex Lightb284f8d2017-11-21 00:00:48 +0000238 void RemoveArtJvmTiEnv(ArtJvmTiEnv* env) REQUIRES(!envs_lock_);
Andreas Gampe3a7eb142017-01-19 21:59:22 -0800239
Alex Light40d87f42017-01-18 10:27:06 -0800240 bool IsEventEnabledAnywhere(ArtJvmtiEvent event) const {
Andreas Gampe77708d92016-10-07 11:48:21 -0700241 if (!EventMask::EventIsInRange(event)) {
242 return false;
243 }
244 return global_mask.Test(event);
245 }
246
Alex Lightb7c640d2019-03-20 15:52:13 -0700247 // Sets an internal event. Unlike normal JVMTI events internal events are not associated with any
248 // particular jvmtiEnv and are refcounted. This refcounting is done to allow us to easily enable
249 // events during functions and disable them during the requested event callback. Since these are
250 // used to implement various JVMTI functions these events always have a single target thread. If
251 // target is null the current thread is used.
252 jvmtiError SetInternalEvent(jthread target,
253 ArtJvmtiEvent event,
254 jvmtiEventMode mode)
255 REQUIRES(!envs_lock_, !art::Locks::mutator_lock_);
256
Alex Light40d87f42017-01-18 10:27:06 -0800257 jvmtiError SetEvent(ArtJvmTiEnv* env,
Alex Light3dacdd62019-03-12 15:45:47 +0000258 jthread thread,
Alex Light40d87f42017-01-18 10:27:06 -0800259 ArtJvmtiEvent event,
Alex Lightb284f8d2017-11-21 00:00:48 +0000260 jvmtiEventMode mode)
261 REQUIRES(!envs_lock_);
Andreas Gampe77708d92016-10-07 11:48:21 -0700262
Alex Light9df79b72017-09-12 08:57:31 -0700263 // Dispatch event to all registered environments. Since this one doesn't have a JNIEnv* it doesn't
264 // matter if it has the mutator_lock.
Andreas Gampe983c1752017-01-23 19:46:56 -0800265 template <ArtJvmtiEvent kEvent, typename ...Args>
Alex Light40d87f42017-01-18 10:27:06 -0800266 ALWAYS_INLINE
Alex Lightb284f8d2017-11-21 00:00:48 +0000267 inline void DispatchEvent(art::Thread* thread, Args... args) const
268 REQUIRES(!envs_lock_);
Alex Light9df79b72017-09-12 08:57:31 -0700269
Alex Lightb7edcda2017-04-27 13:20:31 -0700270 // Dispatch event to all registered environments stashing exceptions as needed. This works since
271 // JNIEnv* is always the second argument if it is passed to an event. Needed since C++ does not
272 // allow partial template function specialization.
Alex Light9df79b72017-09-12 08:57:31 -0700273 //
274 // We need both of these since we want to make sure to push a stack frame when it is possible for
275 // the event to allocate local references.
Alex Lightb7edcda2017-04-27 13:20:31 -0700276 template <ArtJvmtiEvent kEvent, typename ...Args>
277 ALWAYS_INLINE
Alex Lightb284f8d2017-11-21 00:00:48 +0000278 inline void DispatchEvent(art::Thread* thread, JNIEnv* jnienv, Args... args) const
279 REQUIRES(!envs_lock_);
Andreas Gampe27fa96c2016-10-07 15:05:24 -0700280
Alex Light73afd322017-01-18 11:17:47 -0800281 // Tell the event handler capabilities were added/lost so it can adjust the sent events.If
282 // caps_added is true then caps is all the newly set capabilities of the jvmtiEnv. If it is false
283 // then caps is the set of all capabilities that were removed from the jvmtiEnv.
284 ALWAYS_INLINE
285 inline void HandleChangedCapabilities(ArtJvmTiEnv* env,
286 const jvmtiCapabilities& caps,
Alex Lightb284f8d2017-11-21 00:00:48 +0000287 bool added)
288 REQUIRES(!envs_lock_);
Alex Light73afd322017-01-18 11:17:47 -0800289
Alex Light9df79b72017-09-12 08:57:31 -0700290 // Dispatch event to the given environment, only.
291 template <ArtJvmtiEvent kEvent, typename ...Args>
292 ALWAYS_INLINE
Alex Lightb284f8d2017-11-21 00:00:48 +0000293 inline void DispatchEventOnEnv(ArtJvmTiEnv* env,
294 art::Thread* thread,
295 JNIEnv* jnienv,
296 Args... args) const
297 REQUIRES(!envs_lock_);
Alex Light9df79b72017-09-12 08:57:31 -0700298
299 // Dispatch event to the given environment, only.
300 template <ArtJvmtiEvent kEvent, typename ...Args>
301 ALWAYS_INLINE
Alex Lightb284f8d2017-11-21 00:00:48 +0000302 inline void DispatchEventOnEnv(ArtJvmTiEnv* env, art::Thread* thread, Args... args) const
303 REQUIRES(!envs_lock_);
Alex Light9df79b72017-09-12 08:57:31 -0700304
Alex Lightb7c640d2019-03-20 15:52:13 -0700305 void AddDelayedNonStandardExitEvent(const art::ShadowFrame* frame, bool is_object, jvalue val)
306 REQUIRES_SHARED(art::Locks::mutator_lock_)
307 REQUIRES(art::Locks::user_code_suspension_lock_, art::Locks::thread_list_lock_);
308
Alex Light72d7e942019-07-23 13:10:20 -0700309 template<typename Visitor>
310 void ForEachEnv(art::Thread* self, Visitor v) REQUIRES(!envs_lock_) {
311 art::ReaderMutexLock mu(self, envs_lock_);
312 for (ArtJvmTiEnv* e : envs) {
313 if (e != nullptr) {
314 v(e);
315 }
316 }
317 }
318
Andreas Gampe27fa96c2016-10-07 15:05:24 -0700319 private:
Alex Light40607862019-05-06 18:16:24 +0000320 void SetupTraceListener(JvmtiMethodTraceListener* listener, ArtJvmtiEvent event, bool enable);
Alex Lightf6df1b52017-11-29 14:46:53 -0800321
Alex Lightb7c640d2019-03-20 15:52:13 -0700322 uint32_t GetInstrumentationEventsFor(ArtJvmtiEvent event);
323
Alex Lightf5d5eb12018-03-06 15:13:59 -0800324 // Specifically handle the FramePop event which it might not always be possible to turn off.
Alex Light40607862019-05-06 18:16:24 +0000325 void SetupFramePopTraceListener(bool enable);
Alex Lightf5d5eb12018-03-06 15:13:59 -0800326
Alex Lightb284f8d2017-11-21 00:00:48 +0000327 template <ArtJvmtiEvent kEvent, typename ...Args>
328 ALWAYS_INLINE
329 inline std::vector<impl::EventHandlerFunc<kEvent>> CollectEvents(art::Thread* thread,
330 Args... args) const
331 REQUIRES(!envs_lock_);
332
Andreas Gampe983c1752017-01-23 19:46:56 -0800333 template <ArtJvmtiEvent kEvent>
Alex Light40d87f42017-01-18 10:27:06 -0800334 ALWAYS_INLINE
Alex Lightb284f8d2017-11-21 00:00:48 +0000335 inline bool ShouldDispatchOnThread(ArtJvmTiEnv* env, art::Thread* thread) const;
Alex Light9df79b72017-09-12 08:57:31 -0700336
337 template <ArtJvmtiEvent kEvent, typename ...Args>
338 ALWAYS_INLINE
Alex Lightb284f8d2017-11-21 00:00:48 +0000339 static inline void ExecuteCallback(impl::EventHandlerFunc<kEvent> handler,
340 JNIEnv* env,
341 Args... args)
342 REQUIRES(!envs_lock_);
Alex Light9df79b72017-09-12 08:57:31 -0700343
344 template <ArtJvmtiEvent kEvent, typename ...Args>
345 ALWAYS_INLINE
Alex Lightb284f8d2017-11-21 00:00:48 +0000346 static inline void ExecuteCallback(impl::EventHandlerFunc<kEvent> handler, Args... args)
347 REQUIRES(!envs_lock_);
348
349 // Public for use to collect dispatches
350 template <ArtJvmtiEvent kEvent, typename ...Args>
351 ALWAYS_INLINE
Alex Light9df79b72017-09-12 08:57:31 -0700352 inline bool ShouldDispatch(ArtJvmTiEnv* env, art::Thread* thread, Args... args) const;
Alex Light40d87f42017-01-18 10:27:06 -0800353
Alex Light73afd322017-01-18 11:17:47 -0800354 ALWAYS_INLINE
355 inline bool NeedsEventUpdate(ArtJvmTiEnv* env,
356 const jvmtiCapabilities& caps,
357 bool added);
358
359 // Recalculates the event mask for the given event.
360 ALWAYS_INLINE
Alex Lightb284f8d2017-11-21 00:00:48 +0000361 inline void RecalculateGlobalEventMask(ArtJvmtiEvent event) REQUIRES(!envs_lock_);
362 ALWAYS_INLINE
Alex Light2a96fe82018-01-22 17:45:02 -0800363 inline void RecalculateGlobalEventMaskLocked(ArtJvmtiEvent event) REQUIRES_SHARED(envs_lock_);
Alex Light73afd322017-01-18 11:17:47 -0800364
Alex Light40607862019-05-06 18:16:24 +0000365 // Returns whether there are any active requests for the given event on the given thread. This
366 // should only be used while modifying the events for a thread.
367 bool GetThreadEventState(ArtJvmtiEvent event, art::Thread* thread)
368 REQUIRES(envs_lock_, art::Locks::thread_list_lock_);
369
Andreas Gampe983c1752017-01-23 19:46:56 -0800370 template <ArtJvmtiEvent kEvent>
Alex Light6ac57502017-01-19 15:05:06 -0800371 ALWAYS_INLINE inline void DispatchClassFileLoadHookEvent(art::Thread* thread,
Andreas Gampe983c1752017-01-23 19:46:56 -0800372 JNIEnv* jnienv,
373 jclass class_being_redefined,
374 jobject loader,
375 const char* name,
376 jobject protection_domain,
377 jint class_data_len,
378 const unsigned char* class_data,
379 jint* new_class_data_len,
Alex Lightb284f8d2017-11-21 00:00:48 +0000380 unsigned char** new_class_data) const
381 REQUIRES(!envs_lock_);
Alex Light6ac57502017-01-19 15:05:06 -0800382
Alex Light0aa7a5a2018-10-10 15:58:14 +0000383 template <ArtJvmtiEvent kEvent>
384 ALWAYS_INLINE inline void DispatchClassLoadOrPrepareEvent(art::Thread* thread,
385 JNIEnv* jnienv,
386 jthread jni_thread,
387 jclass klass) const
388 REQUIRES(!envs_lock_);
389
Alex Light40607862019-05-06 18:16:24 +0000390 // Sets up the global state needed for the first/last enable of an event across all threads
391 void HandleEventType(ArtJvmtiEvent event, bool enable);
392 // Perform deopts required for enabling the event on the given thread. Null thread indicates
393 // global event enabled.
394 jvmtiError HandleEventDeopt(ArtJvmtiEvent event, jthread thread, bool enable);
Alex Lightbebd7bd2017-07-25 14:05:52 -0700395 void HandleLocalAccessCapabilityAdded();
Alex Light0fa17862017-10-24 13:43:05 -0700396 void HandleBreakpointEventsChanged(bool enable);
Andreas Gampe27fa96c2016-10-07 15:05:24 -0700397
Alex Light77fee872017-09-05 14:51:49 -0700398 bool OtherMonitorEventsEnabledAnywhere(ArtJvmtiEvent event);
399
Alex Lightb7c640d2019-03-20 15:52:13 -0700400 int32_t GetInternalEventRefcount(ArtJvmtiEvent event) const REQUIRES(envs_lock_);
401 // Increment internal event refcount for the given event and return the new count.
402 int32_t IncrInternalEventRefcount(ArtJvmtiEvent event) REQUIRES(envs_lock_);
403 // Decrement internal event refcount for the given event and return the new count.
404 int32_t DecrInternalEventRefcount(ArtJvmtiEvent event) REQUIRES(envs_lock_);
405
406 int32_t& GetInternalEventThreadRefcount(ArtJvmtiEvent event, art::Thread* target)
407 REQUIRES(envs_lock_, art::Locks::thread_list_lock_);
408 // Increment internal event refcount for the given event and return the new count.
409 int32_t IncrInternalEventThreadRefcount(ArtJvmtiEvent event, art::Thread* target)
410 REQUIRES(envs_lock_, art::Locks::thread_list_lock_);
411 // Decrement internal event refcount for the given event and return the new count.
412 int32_t DecrInternalEventThreadRefcount(ArtJvmtiEvent event, art::Thread* target)
413 REQUIRES(envs_lock_, art::Locks::thread_list_lock_);
414
Alex Lightb284f8d2017-11-21 00:00:48 +0000415 // List of all JvmTiEnv objects that have been created, in their creation order. It is a std::list
416 // since we mostly access it by iterating over the entire thing, only ever append to the end, and
417 // need to be able to remove arbitrary elements from it.
418 std::list<ArtJvmTiEnv*> envs GUARDED_BY(envs_lock_);
419
Alex Light66834462019-04-08 16:28:29 +0000420 // Close to top level lock. Nothing should be held when we lock this (except for mutator_lock_
421 // which is needed when setting new events).
422 mutable art::ReaderWriterMutex envs_lock_ ACQUIRED_AFTER(art::Locks::mutator_lock_);
Andreas Gampe27fa96c2016-10-07 15:05:24 -0700423
424 // A union of all enabled events, anywhere.
425 EventMask global_mask;
426
Alex Light986914b2019-11-19 01:12:25 +0000427 std::unique_ptr<JvmtiEventAllocationListener> alloc_listener_;
Alex Light8c2b9292017-11-09 13:21:01 -0800428 std::unique_ptr<JvmtiDdmChunkListener> ddm_listener_;
Andreas Gampe9b8c5882016-10-21 15:27:46 -0700429 std::unique_ptr<JvmtiGcPauseListener> gc_pause_listener_;
Alex Lightb7edcda2017-04-27 13:20:31 -0700430 std::unique_ptr<JvmtiMethodTraceListener> method_trace_listener_;
Alex Light77fee872017-09-05 14:51:49 -0700431 std::unique_ptr<JvmtiMonitorListener> monitor_listener_;
Charles Munger5cc0e752018-11-09 12:30:46 -0800432 std::unique_ptr<JvmtiParkListener> park_listener_;
Alex Lighte814f9d2017-07-31 16:14:39 -0700433
434 // True if frame pop has ever been enabled. Since we store pointers to stack frames we need to
435 // continue to listen to this event even if it has been disabled.
436 // TODO We could remove the listeners once all jvmtiEnvs have drained their shadow-frame vectors.
437 bool frame_pop_enabled;
Alex Lightb7c640d2019-03-20 15:52:13 -0700438
439 // The overall refcount for each internal event across all threads.
440 std::array<int32_t, kInternalEventCount> internal_event_refcount_ GUARDED_BY(envs_lock_);
441 // The refcount for each thread for each internal event.
442 // TODO We should clean both this and the normal EventMask lists up when threads end.
443 std::array<std::unordered_map<UniqueThread, int32_t, UniqueThreadHasher>, kInternalEventCount>
444 internal_event_thread_refcount_
445 GUARDED_BY(envs_lock_) GUARDED_BY(art::Locks::thread_list_lock_);
446
447 friend class JvmtiMethodTraceListener;
Andreas Gampe77708d92016-10-07 11:48:21 -0700448};
449
450} // namespace openjdkjvmti
451
Andreas Gampe06c42a52017-07-26 14:17:14 -0700452#endif // ART_OPENJDKJVMTI_EVENTS_H_