Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 1 | /* |
| 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 Gampe | 06c42a5 | 2017-07-26 14:17:14 -0700 | [diff] [blame] | 17 | #ifndef ART_OPENJDKJVMTI_EVENTS_H_ |
| 18 | #define ART_OPENJDKJVMTI_EVENTS_H_ |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 19 | |
| 20 | #include <bitset> |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 21 | #include <unordered_map> |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 22 | #include <vector> |
| 23 | |
Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 24 | #include <android-base/logging.h> |
Alex Light | 6683446 | 2019-04-08 16:28:29 +0000 | [diff] [blame] | 25 | #include <android-base/thread_annotations.h> |
Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 26 | |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 27 | #include "android-base/thread_annotations.h" |
Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 28 | #include "base/macros.h" |
| 29 | #include "base/mutex.h" |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 30 | #include "jvmti.h" |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 31 | #include "managed_stack.h" |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 32 | #include "thread.h" |
| 33 | |
| 34 | namespace openjdkjvmti { |
| 35 | |
| 36 | struct ArtJvmTiEnv; |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 37 | class JvmtiAllocationListener; |
Alex Light | 8c2b929 | 2017-11-09 13:21:01 -0800 | [diff] [blame] | 38 | class JvmtiDdmChunkListener; |
Andreas Gampe | 9b8c588 | 2016-10-21 15:27:46 -0700 | [diff] [blame] | 39 | class JvmtiGcPauseListener; |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 40 | class JvmtiMethodTraceListener; |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 41 | class JvmtiMonitorListener; |
Charles Munger | 5cc0e75 | 2018-11-09 12:30:46 -0800 | [diff] [blame] | 42 | class JvmtiParkListener; |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 43 | |
Alex Light | 73afd32 | 2017-01-18 11:17:47 -0800 | [diff] [blame] | 44 | // an enum for ArtEvents. This differs from the JVMTI events only in that we distinguish between |
| 45 | // retransformation capable and incapable loading |
Alex Light | 8c2b929 | 2017-11-09 13:21:01 -0800 | [diff] [blame] | 46 | enum class ArtJvmtiEvent : jint { |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 47 | 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 Light | 73afd32 | 2017-01-18 11:17:47 -0800 | [diff] [blame] | 52 | kClassFileLoadHookNonRetransformable = JVMTI_EVENT_CLASS_FILE_LOAD_HOOK, |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 53 | 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 Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 79 | // Internal event to mark a ClassFileLoadHook as one created with the can_retransform_classes |
| 80 | // capability. |
Alex Light | 73afd32 | 2017-01-18 11:17:47 -0800 | [diff] [blame] | 81 | kClassFileLoadHookRetransformable = JVMTI_MAX_EVENT_TYPE_VAL + 1, |
Alex Light | 8c2b929 | 2017-11-09 13:21:01 -0800 | [diff] [blame] | 82 | kDdmPublishChunk = JVMTI_MAX_EVENT_TYPE_VAL + 2, |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 83 | kMaxNormalEventTypeVal = kDdmPublishChunk, |
| 84 | |
| 85 | // All that follow are events used to implement internal JVMTI functions. They are not settable |
| 86 | // directly by agents. |
| 87 | kMinInternalEventTypeVal = kMaxNormalEventTypeVal + 1, |
| 88 | |
| 89 | // Internal event we use to implement the ForceEarlyReturn functions. |
| 90 | kForceEarlyReturnUpdateReturnValue = kMinInternalEventTypeVal, |
| 91 | kMaxInternalEventTypeVal = kForceEarlyReturnUpdateReturnValue, |
| 92 | |
| 93 | kMaxEventTypeVal = kMaxInternalEventTypeVal, |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 94 | }; |
| 95 | |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 96 | constexpr jint kInternalEventCount = static_cast<jint>(ArtJvmtiEvent::kMaxInternalEventTypeVal) - |
| 97 | static_cast<jint>(ArtJvmtiEvent::kMinInternalEventTypeVal) + 1; |
| 98 | |
Alex Light | 8c2b929 | 2017-11-09 13:21:01 -0800 | [diff] [blame] | 99 | using ArtJvmtiEventDdmPublishChunk = void (*)(jvmtiEnv *jvmti_env, |
| 100 | JNIEnv* jni_env, |
| 101 | jint data_type, |
| 102 | jint data_len, |
| 103 | const jbyte* data); |
| 104 | |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 105 | // It is not enough to store a Thread pointer, as these may be reused. Use the pointer and the |
| 106 | // thread id. |
| 107 | // Note: We could just use the tid like tracing does. |
| 108 | using UniqueThread = std::pair<art::Thread*, uint32_t>; |
| 109 | |
| 110 | struct UniqueThreadHasher { |
| 111 | std::size_t operator()(const UniqueThread& k) const { |
| 112 | return std::hash<uint32_t>{}(k.second) ^ (std::hash<void*>{}(k.first) << 1); |
| 113 | } |
| 114 | }; |
| 115 | |
Alex Light | 8c2b929 | 2017-11-09 13:21:01 -0800 | [diff] [blame] | 116 | struct ArtJvmtiEventCallbacks : jvmtiEventCallbacks { |
| 117 | ArtJvmtiEventCallbacks() : DdmPublishChunk(nullptr) { |
| 118 | memset(this, 0, sizeof(jvmtiEventCallbacks)); |
| 119 | } |
| 120 | |
| 121 | // Copies extension functions from other callback struct if it exists. There must not have been |
| 122 | // any modifications to this struct when it is called. |
| 123 | void CopyExtensionsFrom(const ArtJvmtiEventCallbacks* cb); |
| 124 | |
| 125 | jvmtiError Set(jint index, jvmtiExtensionEvent cb); |
| 126 | |
| 127 | ArtJvmtiEventDdmPublishChunk DdmPublishChunk; |
| 128 | }; |
| 129 | |
| 130 | bool IsExtensionEvent(jint e); |
| 131 | bool IsExtensionEvent(ArtJvmtiEvent e); |
| 132 | |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 133 | // Convert a jvmtiEvent into a ArtJvmtiEvent |
| 134 | ALWAYS_INLINE static inline ArtJvmtiEvent GetArtJvmtiEvent(ArtJvmTiEnv* env, jvmtiEvent e); |
| 135 | |
Alex Light | 73afd32 | 2017-01-18 11:17:47 -0800 | [diff] [blame] | 136 | static inline jvmtiEvent GetJvmtiEvent(ArtJvmtiEvent e) { |
| 137 | if (UNLIKELY(e == ArtJvmtiEvent::kClassFileLoadHookRetransformable)) { |
| 138 | return JVMTI_EVENT_CLASS_FILE_LOAD_HOOK; |
| 139 | } else { |
| 140 | return static_cast<jvmtiEvent>(e); |
| 141 | } |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 142 | } |
| 143 | |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 144 | struct EventMask { |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 145 | static constexpr size_t kEventsSize = |
| 146 | static_cast<size_t>(ArtJvmtiEvent::kMaxEventTypeVal) - |
| 147 | static_cast<size_t>(ArtJvmtiEvent::kMinEventTypeVal) + 1; |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 148 | std::bitset<kEventsSize> bit_set; |
| 149 | |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 150 | static bool EventIsInRange(ArtJvmtiEvent event) { |
| 151 | return event >= ArtJvmtiEvent::kMinEventTypeVal && event <= ArtJvmtiEvent::kMaxEventTypeVal; |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 154 | void Set(ArtJvmtiEvent event, bool value = true) { |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 155 | DCHECK(EventIsInRange(event)); |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 156 | bit_set.set(static_cast<size_t>(event) - static_cast<size_t>(ArtJvmtiEvent::kMinEventTypeVal), |
| 157 | value); |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 160 | bool Test(ArtJvmtiEvent event) const { |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 161 | DCHECK(EventIsInRange(event)); |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 162 | return bit_set.test( |
| 163 | static_cast<size_t>(event) - static_cast<size_t>(ArtJvmtiEvent::kMinEventTypeVal)); |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 164 | } |
| 165 | }; |
| 166 | |
| 167 | struct EventMasks { |
| 168 | // The globally enabled events. |
| 169 | EventMask global_event_mask; |
| 170 | |
| 171 | // The per-thread enabled events. |
| 172 | |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 173 | // TODO: Native thread objects are immovable, so we can use them as keys in an (unordered) map, |
| 174 | // if necessary. |
| 175 | std::vector<std::pair<UniqueThread, EventMask>> thread_event_masks; |
| 176 | |
| 177 | // A union of the per-thread events, for fast-pathing. |
| 178 | EventMask unioned_thread_event_mask; |
| 179 | |
| 180 | EventMask& GetEventMask(art::Thread* thread); |
| 181 | EventMask* GetEventMaskOrNull(art::Thread* thread); |
Alex Light | 74c8440 | 2017-11-29 15:26:38 -0800 | [diff] [blame] | 182 | // Circular dependencies mean we cannot see the definition of ArtJvmTiEnv so the mutex is simply |
| 183 | // asserted in the function. |
| 184 | // Note that the 'env' passed in must be the same env this EventMasks is associated with. |
| 185 | void EnableEvent(ArtJvmTiEnv* env, art::Thread* thread, ArtJvmtiEvent event); |
| 186 | // REQUIRES(env->event_info_mutex_); |
| 187 | // Circular dependencies mean we cannot see the definition of ArtJvmTiEnv so the mutex is simply |
| 188 | // asserted in the function. |
| 189 | // Note that the 'env' passed in must be the same env this EventMasks is associated with. |
| 190 | void DisableEvent(ArtJvmTiEnv* env, art::Thread* thread, ArtJvmtiEvent event); |
| 191 | // REQUIRES(env->event_info_mutex_); |
Alex Light | 73afd32 | 2017-01-18 11:17:47 -0800 | [diff] [blame] | 192 | bool IsEnabledAnywhere(ArtJvmtiEvent event); |
| 193 | // Make any changes to event masks needed for the given capability changes. If caps_added is true |
| 194 | // then caps is all the newly set capabilities of the jvmtiEnv. If it is false then caps is the |
| 195 | // set of all capabilities that were removed from the jvmtiEnv. |
| 196 | void HandleChangedCapabilities(const jvmtiCapabilities& caps, bool caps_added); |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 197 | }; |
| 198 | |
Alex Light | b284f8d | 2017-11-21 00:00:48 +0000 | [diff] [blame] | 199 | namespace impl { |
| 200 | template <ArtJvmtiEvent kEvent> struct EventHandlerFunc { }; |
| 201 | } // namespace impl |
| 202 | |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 203 | // Helper class for event handling. |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 204 | class EventHandler { |
| 205 | public: |
| 206 | EventHandler(); |
| 207 | ~EventHandler(); |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 208 | |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 209 | // do cleanup for the event handler. |
| 210 | void Shutdown(); |
| 211 | |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 212 | // Register an env. It is assumed that this happens on env creation, that is, no events are |
| 213 | // enabled, yet. |
Alex Light | b284f8d | 2017-11-21 00:00:48 +0000 | [diff] [blame] | 214 | void RegisterArtJvmTiEnv(ArtJvmTiEnv* env) REQUIRES(!envs_lock_); |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 215 | |
Andreas Gampe | 3a7eb14 | 2017-01-19 21:59:22 -0800 | [diff] [blame] | 216 | // Remove an env. |
Alex Light | b284f8d | 2017-11-21 00:00:48 +0000 | [diff] [blame] | 217 | void RemoveArtJvmTiEnv(ArtJvmTiEnv* env) REQUIRES(!envs_lock_); |
Andreas Gampe | 3a7eb14 | 2017-01-19 21:59:22 -0800 | [diff] [blame] | 218 | |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 219 | bool IsEventEnabledAnywhere(ArtJvmtiEvent event) const { |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 220 | if (!EventMask::EventIsInRange(event)) { |
| 221 | return false; |
| 222 | } |
| 223 | return global_mask.Test(event); |
| 224 | } |
| 225 | |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 226 | // Sets an internal event. Unlike normal JVMTI events internal events are not associated with any |
| 227 | // particular jvmtiEnv and are refcounted. This refcounting is done to allow us to easily enable |
| 228 | // events during functions and disable them during the requested event callback. Since these are |
| 229 | // used to implement various JVMTI functions these events always have a single target thread. If |
| 230 | // target is null the current thread is used. |
| 231 | jvmtiError SetInternalEvent(jthread target, |
| 232 | ArtJvmtiEvent event, |
| 233 | jvmtiEventMode mode) |
| 234 | REQUIRES(!envs_lock_, !art::Locks::mutator_lock_); |
| 235 | |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 236 | jvmtiError SetEvent(ArtJvmTiEnv* env, |
Alex Light | 3dacdd6 | 2019-03-12 15:45:47 +0000 | [diff] [blame] | 237 | jthread thread, |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 238 | ArtJvmtiEvent event, |
Alex Light | b284f8d | 2017-11-21 00:00:48 +0000 | [diff] [blame] | 239 | jvmtiEventMode mode) |
| 240 | REQUIRES(!envs_lock_); |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 241 | |
Alex Light | 9df79b7 | 2017-09-12 08:57:31 -0700 | [diff] [blame] | 242 | // Dispatch event to all registered environments. Since this one doesn't have a JNIEnv* it doesn't |
| 243 | // matter if it has the mutator_lock. |
Andreas Gampe | 983c175 | 2017-01-23 19:46:56 -0800 | [diff] [blame] | 244 | template <ArtJvmtiEvent kEvent, typename ...Args> |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 245 | ALWAYS_INLINE |
Alex Light | b284f8d | 2017-11-21 00:00:48 +0000 | [diff] [blame] | 246 | inline void DispatchEvent(art::Thread* thread, Args... args) const |
| 247 | REQUIRES(!envs_lock_); |
Alex Light | 9df79b7 | 2017-09-12 08:57:31 -0700 | [diff] [blame] | 248 | |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 249 | // Dispatch event to all registered environments stashing exceptions as needed. This works since |
| 250 | // JNIEnv* is always the second argument if it is passed to an event. Needed since C++ does not |
| 251 | // allow partial template function specialization. |
Alex Light | 9df79b7 | 2017-09-12 08:57:31 -0700 | [diff] [blame] | 252 | // |
| 253 | // We need both of these since we want to make sure to push a stack frame when it is possible for |
| 254 | // the event to allocate local references. |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 255 | template <ArtJvmtiEvent kEvent, typename ...Args> |
| 256 | ALWAYS_INLINE |
Alex Light | b284f8d | 2017-11-21 00:00:48 +0000 | [diff] [blame] | 257 | inline void DispatchEvent(art::Thread* thread, JNIEnv* jnienv, Args... args) const |
| 258 | REQUIRES(!envs_lock_); |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 259 | |
Alex Light | 73afd32 | 2017-01-18 11:17:47 -0800 | [diff] [blame] | 260 | // Tell the event handler capabilities were added/lost so it can adjust the sent events.If |
| 261 | // caps_added is true then caps is all the newly set capabilities of the jvmtiEnv. If it is false |
| 262 | // then caps is the set of all capabilities that were removed from the jvmtiEnv. |
| 263 | ALWAYS_INLINE |
| 264 | inline void HandleChangedCapabilities(ArtJvmTiEnv* env, |
| 265 | const jvmtiCapabilities& caps, |
Alex Light | b284f8d | 2017-11-21 00:00:48 +0000 | [diff] [blame] | 266 | bool added) |
| 267 | REQUIRES(!envs_lock_); |
Alex Light | 73afd32 | 2017-01-18 11:17:47 -0800 | [diff] [blame] | 268 | |
Alex Light | 9df79b7 | 2017-09-12 08:57:31 -0700 | [diff] [blame] | 269 | // Dispatch event to the given environment, only. |
| 270 | template <ArtJvmtiEvent kEvent, typename ...Args> |
| 271 | ALWAYS_INLINE |
Alex Light | b284f8d | 2017-11-21 00:00:48 +0000 | [diff] [blame] | 272 | inline void DispatchEventOnEnv(ArtJvmTiEnv* env, |
| 273 | art::Thread* thread, |
| 274 | JNIEnv* jnienv, |
| 275 | Args... args) const |
| 276 | REQUIRES(!envs_lock_); |
Alex Light | 9df79b7 | 2017-09-12 08:57:31 -0700 | [diff] [blame] | 277 | |
| 278 | // Dispatch event to the given environment, only. |
| 279 | template <ArtJvmtiEvent kEvent, typename ...Args> |
| 280 | ALWAYS_INLINE |
Alex Light | b284f8d | 2017-11-21 00:00:48 +0000 | [diff] [blame] | 281 | inline void DispatchEventOnEnv(ArtJvmTiEnv* env, art::Thread* thread, Args... args) const |
| 282 | REQUIRES(!envs_lock_); |
Alex Light | 9df79b7 | 2017-09-12 08:57:31 -0700 | [diff] [blame] | 283 | |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 284 | void AddDelayedNonStandardExitEvent(const art::ShadowFrame* frame, bool is_object, jvalue val) |
| 285 | REQUIRES_SHARED(art::Locks::mutator_lock_) |
| 286 | REQUIRES(art::Locks::user_code_suspension_lock_, art::Locks::thread_list_lock_); |
| 287 | |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 288 | private: |
Alex Light | 4060786 | 2019-05-06 18:16:24 +0000 | [diff] [blame] | 289 | void SetupTraceListener(JvmtiMethodTraceListener* listener, ArtJvmtiEvent event, bool enable); |
Alex Light | f6df1b5 | 2017-11-29 14:46:53 -0800 | [diff] [blame] | 290 | |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 291 | uint32_t GetInstrumentationEventsFor(ArtJvmtiEvent event); |
| 292 | |
Alex Light | f5d5eb1 | 2018-03-06 15:13:59 -0800 | [diff] [blame] | 293 | // Specifically handle the FramePop event which it might not always be possible to turn off. |
Alex Light | 4060786 | 2019-05-06 18:16:24 +0000 | [diff] [blame] | 294 | void SetupFramePopTraceListener(bool enable); |
Alex Light | f5d5eb1 | 2018-03-06 15:13:59 -0800 | [diff] [blame] | 295 | |
Alex Light | b284f8d | 2017-11-21 00:00:48 +0000 | [diff] [blame] | 296 | template <ArtJvmtiEvent kEvent, typename ...Args> |
| 297 | ALWAYS_INLINE |
| 298 | inline std::vector<impl::EventHandlerFunc<kEvent>> CollectEvents(art::Thread* thread, |
| 299 | Args... args) const |
| 300 | REQUIRES(!envs_lock_); |
| 301 | |
Andreas Gampe | 983c175 | 2017-01-23 19:46:56 -0800 | [diff] [blame] | 302 | template <ArtJvmtiEvent kEvent> |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 303 | ALWAYS_INLINE |
Alex Light | b284f8d | 2017-11-21 00:00:48 +0000 | [diff] [blame] | 304 | inline bool ShouldDispatchOnThread(ArtJvmTiEnv* env, art::Thread* thread) const; |
Alex Light | 9df79b7 | 2017-09-12 08:57:31 -0700 | [diff] [blame] | 305 | |
| 306 | template <ArtJvmtiEvent kEvent, typename ...Args> |
| 307 | ALWAYS_INLINE |
Alex Light | b284f8d | 2017-11-21 00:00:48 +0000 | [diff] [blame] | 308 | static inline void ExecuteCallback(impl::EventHandlerFunc<kEvent> handler, |
| 309 | JNIEnv* env, |
| 310 | Args... args) |
| 311 | REQUIRES(!envs_lock_); |
Alex Light | 9df79b7 | 2017-09-12 08:57:31 -0700 | [diff] [blame] | 312 | |
| 313 | template <ArtJvmtiEvent kEvent, typename ...Args> |
| 314 | ALWAYS_INLINE |
Alex Light | b284f8d | 2017-11-21 00:00:48 +0000 | [diff] [blame] | 315 | static inline void ExecuteCallback(impl::EventHandlerFunc<kEvent> handler, Args... args) |
| 316 | REQUIRES(!envs_lock_); |
| 317 | |
| 318 | // Public for use to collect dispatches |
| 319 | template <ArtJvmtiEvent kEvent, typename ...Args> |
| 320 | ALWAYS_INLINE |
Alex Light | 9df79b7 | 2017-09-12 08:57:31 -0700 | [diff] [blame] | 321 | inline bool ShouldDispatch(ArtJvmTiEnv* env, art::Thread* thread, Args... args) const; |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 322 | |
Alex Light | 73afd32 | 2017-01-18 11:17:47 -0800 | [diff] [blame] | 323 | ALWAYS_INLINE |
| 324 | inline bool NeedsEventUpdate(ArtJvmTiEnv* env, |
| 325 | const jvmtiCapabilities& caps, |
| 326 | bool added); |
| 327 | |
| 328 | // Recalculates the event mask for the given event. |
| 329 | ALWAYS_INLINE |
Alex Light | b284f8d | 2017-11-21 00:00:48 +0000 | [diff] [blame] | 330 | inline void RecalculateGlobalEventMask(ArtJvmtiEvent event) REQUIRES(!envs_lock_); |
| 331 | ALWAYS_INLINE |
Alex Light | 2a96fe8 | 2018-01-22 17:45:02 -0800 | [diff] [blame] | 332 | inline void RecalculateGlobalEventMaskLocked(ArtJvmtiEvent event) REQUIRES_SHARED(envs_lock_); |
Alex Light | 73afd32 | 2017-01-18 11:17:47 -0800 | [diff] [blame] | 333 | |
Alex Light | 4060786 | 2019-05-06 18:16:24 +0000 | [diff] [blame] | 334 | // Returns whether there are any active requests for the given event on the given thread. This |
| 335 | // should only be used while modifying the events for a thread. |
| 336 | bool GetThreadEventState(ArtJvmtiEvent event, art::Thread* thread) |
| 337 | REQUIRES(envs_lock_, art::Locks::thread_list_lock_); |
| 338 | |
Andreas Gampe | 983c175 | 2017-01-23 19:46:56 -0800 | [diff] [blame] | 339 | template <ArtJvmtiEvent kEvent> |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 340 | ALWAYS_INLINE inline void DispatchClassFileLoadHookEvent(art::Thread* thread, |
Andreas Gampe | 983c175 | 2017-01-23 19:46:56 -0800 | [diff] [blame] | 341 | JNIEnv* jnienv, |
| 342 | jclass class_being_redefined, |
| 343 | jobject loader, |
| 344 | const char* name, |
| 345 | jobject protection_domain, |
| 346 | jint class_data_len, |
| 347 | const unsigned char* class_data, |
| 348 | jint* new_class_data_len, |
Alex Light | b284f8d | 2017-11-21 00:00:48 +0000 | [diff] [blame] | 349 | unsigned char** new_class_data) const |
| 350 | REQUIRES(!envs_lock_); |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 351 | |
Alex Light | 0aa7a5a | 2018-10-10 15:58:14 +0000 | [diff] [blame] | 352 | template <ArtJvmtiEvent kEvent> |
| 353 | ALWAYS_INLINE inline void DispatchClassLoadOrPrepareEvent(art::Thread* thread, |
| 354 | JNIEnv* jnienv, |
| 355 | jthread jni_thread, |
| 356 | jclass klass) const |
| 357 | REQUIRES(!envs_lock_); |
| 358 | |
Alex Light | 4060786 | 2019-05-06 18:16:24 +0000 | [diff] [blame] | 359 | // Sets up the global state needed for the first/last enable of an event across all threads |
| 360 | void HandleEventType(ArtJvmtiEvent event, bool enable); |
| 361 | // Perform deopts required for enabling the event on the given thread. Null thread indicates |
| 362 | // global event enabled. |
| 363 | jvmtiError HandleEventDeopt(ArtJvmtiEvent event, jthread thread, bool enable); |
Alex Light | bebd7bd | 2017-07-25 14:05:52 -0700 | [diff] [blame] | 364 | void HandleLocalAccessCapabilityAdded(); |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 365 | void HandleBreakpointEventsChanged(bool enable); |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 366 | |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 367 | bool OtherMonitorEventsEnabledAnywhere(ArtJvmtiEvent event); |
| 368 | |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 369 | int32_t GetInternalEventRefcount(ArtJvmtiEvent event) const REQUIRES(envs_lock_); |
| 370 | // Increment internal event refcount for the given event and return the new count. |
| 371 | int32_t IncrInternalEventRefcount(ArtJvmtiEvent event) REQUIRES(envs_lock_); |
| 372 | // Decrement internal event refcount for the given event and return the new count. |
| 373 | int32_t DecrInternalEventRefcount(ArtJvmtiEvent event) REQUIRES(envs_lock_); |
| 374 | |
| 375 | int32_t& GetInternalEventThreadRefcount(ArtJvmtiEvent event, art::Thread* target) |
| 376 | REQUIRES(envs_lock_, art::Locks::thread_list_lock_); |
| 377 | // Increment internal event refcount for the given event and return the new count. |
| 378 | int32_t IncrInternalEventThreadRefcount(ArtJvmtiEvent event, art::Thread* target) |
| 379 | REQUIRES(envs_lock_, art::Locks::thread_list_lock_); |
| 380 | // Decrement internal event refcount for the given event and return the new count. |
| 381 | int32_t DecrInternalEventThreadRefcount(ArtJvmtiEvent event, art::Thread* target) |
| 382 | REQUIRES(envs_lock_, art::Locks::thread_list_lock_); |
| 383 | |
Alex Light | b284f8d | 2017-11-21 00:00:48 +0000 | [diff] [blame] | 384 | // List of all JvmTiEnv objects that have been created, in their creation order. It is a std::list |
| 385 | // since we mostly access it by iterating over the entire thing, only ever append to the end, and |
| 386 | // need to be able to remove arbitrary elements from it. |
| 387 | std::list<ArtJvmTiEnv*> envs GUARDED_BY(envs_lock_); |
| 388 | |
Alex Light | 6683446 | 2019-04-08 16:28:29 +0000 | [diff] [blame] | 389 | // Close to top level lock. Nothing should be held when we lock this (except for mutator_lock_ |
| 390 | // which is needed when setting new events). |
| 391 | mutable art::ReaderWriterMutex envs_lock_ ACQUIRED_AFTER(art::Locks::mutator_lock_); |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 392 | |
| 393 | // A union of all enabled events, anywhere. |
| 394 | EventMask global_mask; |
| 395 | |
| 396 | std::unique_ptr<JvmtiAllocationListener> alloc_listener_; |
Alex Light | 8c2b929 | 2017-11-09 13:21:01 -0800 | [diff] [blame] | 397 | std::unique_ptr<JvmtiDdmChunkListener> ddm_listener_; |
Andreas Gampe | 9b8c588 | 2016-10-21 15:27:46 -0700 | [diff] [blame] | 398 | std::unique_ptr<JvmtiGcPauseListener> gc_pause_listener_; |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 399 | std::unique_ptr<JvmtiMethodTraceListener> method_trace_listener_; |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 400 | std::unique_ptr<JvmtiMonitorListener> monitor_listener_; |
Charles Munger | 5cc0e75 | 2018-11-09 12:30:46 -0800 | [diff] [blame] | 401 | std::unique_ptr<JvmtiParkListener> park_listener_; |
Alex Light | e814f9d | 2017-07-31 16:14:39 -0700 | [diff] [blame] | 402 | |
| 403 | // True if frame pop has ever been enabled. Since we store pointers to stack frames we need to |
| 404 | // continue to listen to this event even if it has been disabled. |
| 405 | // TODO We could remove the listeners once all jvmtiEnvs have drained their shadow-frame vectors. |
| 406 | bool frame_pop_enabled; |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 407 | |
| 408 | // The overall refcount for each internal event across all threads. |
| 409 | std::array<int32_t, kInternalEventCount> internal_event_refcount_ GUARDED_BY(envs_lock_); |
| 410 | // The refcount for each thread for each internal event. |
| 411 | // TODO We should clean both this and the normal EventMask lists up when threads end. |
| 412 | std::array<std::unordered_map<UniqueThread, int32_t, UniqueThreadHasher>, kInternalEventCount> |
| 413 | internal_event_thread_refcount_ |
| 414 | GUARDED_BY(envs_lock_) GUARDED_BY(art::Locks::thread_list_lock_); |
| 415 | |
| 416 | friend class JvmtiMethodTraceListener; |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 417 | }; |
| 418 | |
| 419 | } // namespace openjdkjvmti |
| 420 | |
Andreas Gampe | 06c42a5 | 2017-07-26 14:17:14 -0700 | [diff] [blame] | 421 | #endif // ART_OPENJDKJVMTI_EVENTS_H_ |