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> |
| 21 | #include <vector> |
| 22 | |
| 23 | #include "base/logging.h" |
| 24 | #include "jvmti.h" |
| 25 | #include "thread.h" |
| 26 | |
| 27 | namespace openjdkjvmti { |
| 28 | |
| 29 | struct ArtJvmTiEnv; |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 30 | class JvmtiAllocationListener; |
Andreas Gampe | 9b8c588 | 2016-10-21 15:27:46 -0700 | [diff] [blame] | 31 | class JvmtiGcPauseListener; |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 32 | class JvmtiMethodTraceListener; |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 33 | class JvmtiMonitorListener; |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 34 | |
Alex Light | 73afd32 | 2017-01-18 11:17:47 -0800 | [diff] [blame] | 35 | // an enum for ArtEvents. This differs from the JVMTI events only in that we distinguish between |
| 36 | // retransformation capable and incapable loading |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 37 | enum class ArtJvmtiEvent { |
| 38 | kMinEventTypeVal = JVMTI_MIN_EVENT_TYPE_VAL, |
| 39 | kVmInit = JVMTI_EVENT_VM_INIT, |
| 40 | kVmDeath = JVMTI_EVENT_VM_DEATH, |
| 41 | kThreadStart = JVMTI_EVENT_THREAD_START, |
| 42 | kThreadEnd = JVMTI_EVENT_THREAD_END, |
Alex Light | 73afd32 | 2017-01-18 11:17:47 -0800 | [diff] [blame] | 43 | kClassFileLoadHookNonRetransformable = JVMTI_EVENT_CLASS_FILE_LOAD_HOOK, |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 44 | kClassLoad = JVMTI_EVENT_CLASS_LOAD, |
| 45 | kClassPrepare = JVMTI_EVENT_CLASS_PREPARE, |
| 46 | kVmStart = JVMTI_EVENT_VM_START, |
| 47 | kException = JVMTI_EVENT_EXCEPTION, |
| 48 | kExceptionCatch = JVMTI_EVENT_EXCEPTION_CATCH, |
| 49 | kSingleStep = JVMTI_EVENT_SINGLE_STEP, |
| 50 | kFramePop = JVMTI_EVENT_FRAME_POP, |
| 51 | kBreakpoint = JVMTI_EVENT_BREAKPOINT, |
| 52 | kFieldAccess = JVMTI_EVENT_FIELD_ACCESS, |
| 53 | kFieldModification = JVMTI_EVENT_FIELD_MODIFICATION, |
| 54 | kMethodEntry = JVMTI_EVENT_METHOD_ENTRY, |
| 55 | kMethodExit = JVMTI_EVENT_METHOD_EXIT, |
| 56 | kNativeMethodBind = JVMTI_EVENT_NATIVE_METHOD_BIND, |
| 57 | kCompiledMethodLoad = JVMTI_EVENT_COMPILED_METHOD_LOAD, |
| 58 | kCompiledMethodUnload = JVMTI_EVENT_COMPILED_METHOD_UNLOAD, |
| 59 | kDynamicCodeGenerated = JVMTI_EVENT_DYNAMIC_CODE_GENERATED, |
| 60 | kDataDumpRequest = JVMTI_EVENT_DATA_DUMP_REQUEST, |
| 61 | kMonitorWait = JVMTI_EVENT_MONITOR_WAIT, |
| 62 | kMonitorWaited = JVMTI_EVENT_MONITOR_WAITED, |
| 63 | kMonitorContendedEnter = JVMTI_EVENT_MONITOR_CONTENDED_ENTER, |
| 64 | kMonitorContendedEntered = JVMTI_EVENT_MONITOR_CONTENDED_ENTERED, |
| 65 | kResourceExhausted = JVMTI_EVENT_RESOURCE_EXHAUSTED, |
| 66 | kGarbageCollectionStart = JVMTI_EVENT_GARBAGE_COLLECTION_START, |
| 67 | kGarbageCollectionFinish = JVMTI_EVENT_GARBAGE_COLLECTION_FINISH, |
| 68 | kObjectFree = JVMTI_EVENT_OBJECT_FREE, |
| 69 | kVmObjectAlloc = JVMTI_EVENT_VM_OBJECT_ALLOC, |
Alex Light | 73afd32 | 2017-01-18 11:17:47 -0800 | [diff] [blame] | 70 | kClassFileLoadHookRetransformable = JVMTI_MAX_EVENT_TYPE_VAL + 1, |
| 71 | kMaxEventTypeVal = kClassFileLoadHookRetransformable, |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | // Convert a jvmtiEvent into a ArtJvmtiEvent |
| 75 | ALWAYS_INLINE static inline ArtJvmtiEvent GetArtJvmtiEvent(ArtJvmTiEnv* env, jvmtiEvent e); |
| 76 | |
Alex Light | 73afd32 | 2017-01-18 11:17:47 -0800 | [diff] [blame] | 77 | static inline jvmtiEvent GetJvmtiEvent(ArtJvmtiEvent e) { |
| 78 | if (UNLIKELY(e == ArtJvmtiEvent::kClassFileLoadHookRetransformable)) { |
| 79 | return JVMTI_EVENT_CLASS_FILE_LOAD_HOOK; |
| 80 | } else { |
| 81 | return static_cast<jvmtiEvent>(e); |
| 82 | } |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 83 | } |
| 84 | |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 85 | struct EventMask { |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 86 | static constexpr size_t kEventsSize = |
| 87 | static_cast<size_t>(ArtJvmtiEvent::kMaxEventTypeVal) - |
| 88 | static_cast<size_t>(ArtJvmtiEvent::kMinEventTypeVal) + 1; |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 89 | std::bitset<kEventsSize> bit_set; |
| 90 | |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 91 | static bool EventIsInRange(ArtJvmtiEvent event) { |
| 92 | return event >= ArtJvmtiEvent::kMinEventTypeVal && event <= ArtJvmtiEvent::kMaxEventTypeVal; |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 95 | void Set(ArtJvmtiEvent event, bool value = true) { |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 96 | DCHECK(EventIsInRange(event)); |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 97 | bit_set.set(static_cast<size_t>(event) - static_cast<size_t>(ArtJvmtiEvent::kMinEventTypeVal), |
| 98 | value); |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 101 | bool Test(ArtJvmtiEvent event) const { |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 102 | DCHECK(EventIsInRange(event)); |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 103 | return bit_set.test( |
| 104 | static_cast<size_t>(event) - static_cast<size_t>(ArtJvmtiEvent::kMinEventTypeVal)); |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 105 | } |
| 106 | }; |
| 107 | |
| 108 | struct EventMasks { |
| 109 | // The globally enabled events. |
| 110 | EventMask global_event_mask; |
| 111 | |
| 112 | // The per-thread enabled events. |
| 113 | |
| 114 | // It is not enough to store a Thread pointer, as these may be reused. Use the pointer and the |
| 115 | // thread id. |
| 116 | // Note: We could just use the tid like tracing does. |
| 117 | using UniqueThread = std::pair<art::Thread*, uint32_t>; |
| 118 | // TODO: Native thread objects are immovable, so we can use them as keys in an (unordered) map, |
| 119 | // if necessary. |
| 120 | std::vector<std::pair<UniqueThread, EventMask>> thread_event_masks; |
| 121 | |
| 122 | // A union of the per-thread events, for fast-pathing. |
| 123 | EventMask unioned_thread_event_mask; |
| 124 | |
| 125 | EventMask& GetEventMask(art::Thread* thread); |
| 126 | EventMask* GetEventMaskOrNull(art::Thread* thread); |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 127 | void EnableEvent(art::Thread* thread, ArtJvmtiEvent event); |
| 128 | void DisableEvent(art::Thread* thread, ArtJvmtiEvent event); |
Alex Light | 73afd32 | 2017-01-18 11:17:47 -0800 | [diff] [blame] | 129 | bool IsEnabledAnywhere(ArtJvmtiEvent event); |
| 130 | // Make any changes to event masks needed for the given capability changes. If caps_added is true |
| 131 | // then caps is all the newly set capabilities of the jvmtiEnv. If it is false then caps is the |
| 132 | // set of all capabilities that were removed from the jvmtiEnv. |
| 133 | void HandleChangedCapabilities(const jvmtiCapabilities& caps, bool caps_added); |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 134 | }; |
| 135 | |
| 136 | // Helper class for event handling. |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 137 | class EventHandler { |
| 138 | public: |
| 139 | EventHandler(); |
| 140 | ~EventHandler(); |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 141 | |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 142 | // do cleanup for the event handler. |
| 143 | void Shutdown(); |
| 144 | |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 145 | // Register an env. It is assumed that this happens on env creation, that is, no events are |
| 146 | // enabled, yet. |
| 147 | void RegisterArtJvmTiEnv(ArtJvmTiEnv* env); |
| 148 | |
Andreas Gampe | 3a7eb14 | 2017-01-19 21:59:22 -0800 | [diff] [blame] | 149 | // Remove an env. |
| 150 | void RemoveArtJvmTiEnv(ArtJvmTiEnv* env); |
| 151 | |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 152 | bool IsEventEnabledAnywhere(ArtJvmtiEvent event) const { |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 153 | if (!EventMask::EventIsInRange(event)) { |
| 154 | return false; |
| 155 | } |
| 156 | return global_mask.Test(event); |
| 157 | } |
| 158 | |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 159 | jvmtiError SetEvent(ArtJvmTiEnv* env, |
| 160 | art::Thread* thread, |
| 161 | ArtJvmtiEvent event, |
| 162 | jvmtiEventMode mode); |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 163 | |
Alex Light | 9df79b7 | 2017-09-12 08:57:31 -0700 | [diff] [blame] | 164 | // Dispatch event to all registered environments. Since this one doesn't have a JNIEnv* it doesn't |
| 165 | // matter if it has the mutator_lock. |
Andreas Gampe | 983c175 | 2017-01-23 19:46:56 -0800 | [diff] [blame] | 166 | template <ArtJvmtiEvent kEvent, typename ...Args> |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 167 | ALWAYS_INLINE |
Andreas Gampe | 983c175 | 2017-01-23 19:46:56 -0800 | [diff] [blame] | 168 | inline void DispatchEvent(art::Thread* thread, Args... args) const; |
Alex Light | 9df79b7 | 2017-09-12 08:57:31 -0700 | [diff] [blame] | 169 | |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 170 | // Dispatch event to all registered environments stashing exceptions as needed. This works since |
| 171 | // JNIEnv* is always the second argument if it is passed to an event. Needed since C++ does not |
| 172 | // allow partial template function specialization. |
Alex Light | 9df79b7 | 2017-09-12 08:57:31 -0700 | [diff] [blame] | 173 | // |
| 174 | // We need both of these since we want to make sure to push a stack frame when it is possible for |
| 175 | // the event to allocate local references. |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 176 | template <ArtJvmtiEvent kEvent, typename ...Args> |
| 177 | ALWAYS_INLINE |
Alex Light | 9df79b7 | 2017-09-12 08:57:31 -0700 | [diff] [blame] | 178 | inline void DispatchEvent(art::Thread* thread, JNIEnv* jnienv, Args... args) const; |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 179 | |
Alex Light | 73afd32 | 2017-01-18 11:17:47 -0800 | [diff] [blame] | 180 | // Tell the event handler capabilities were added/lost so it can adjust the sent events.If |
| 181 | // caps_added is true then caps is all the newly set capabilities of the jvmtiEnv. If it is false |
| 182 | // then caps is the set of all capabilities that were removed from the jvmtiEnv. |
| 183 | ALWAYS_INLINE |
| 184 | inline void HandleChangedCapabilities(ArtJvmTiEnv* env, |
| 185 | const jvmtiCapabilities& caps, |
| 186 | bool added); |
| 187 | |
Alex Light | 9df79b7 | 2017-09-12 08:57:31 -0700 | [diff] [blame] | 188 | // Dispatch event to the given environment, only. |
| 189 | template <ArtJvmtiEvent kEvent, typename ...Args> |
| 190 | ALWAYS_INLINE |
| 191 | inline void DispatchEventOnEnv( |
| 192 | ArtJvmTiEnv* env, art::Thread* thread, JNIEnv* jnienv, Args... args) const; |
| 193 | |
| 194 | // Dispatch event to the given environment, only. |
| 195 | template <ArtJvmtiEvent kEvent, typename ...Args> |
| 196 | ALWAYS_INLINE |
| 197 | inline void DispatchEventOnEnv(ArtJvmTiEnv* env, art::Thread* thread, Args... args) const; |
| 198 | |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 199 | private: |
Andreas Gampe | 983c175 | 2017-01-23 19:46:56 -0800 | [diff] [blame] | 200 | template <ArtJvmtiEvent kEvent> |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 201 | ALWAYS_INLINE |
Alex Light | 9df79b7 | 2017-09-12 08:57:31 -0700 | [diff] [blame] | 202 | static inline bool ShouldDispatchOnThread(ArtJvmTiEnv* env, art::Thread* thread); |
| 203 | |
| 204 | template <ArtJvmtiEvent kEvent, typename ...Args> |
| 205 | ALWAYS_INLINE |
| 206 | static inline void ExecuteCallback(ArtJvmTiEnv* env, Args... args); |
| 207 | |
| 208 | template <ArtJvmtiEvent kEvent, typename ...Args> |
| 209 | ALWAYS_INLINE |
| 210 | inline bool ShouldDispatch(ArtJvmTiEnv* env, art::Thread* thread, Args... args) const; |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 211 | |
Alex Light | 73afd32 | 2017-01-18 11:17:47 -0800 | [diff] [blame] | 212 | ALWAYS_INLINE |
| 213 | inline bool NeedsEventUpdate(ArtJvmTiEnv* env, |
| 214 | const jvmtiCapabilities& caps, |
| 215 | bool added); |
| 216 | |
| 217 | // Recalculates the event mask for the given event. |
| 218 | ALWAYS_INLINE |
| 219 | inline void RecalculateGlobalEventMask(ArtJvmtiEvent event); |
| 220 | |
Andreas Gampe | 983c175 | 2017-01-23 19:46:56 -0800 | [diff] [blame] | 221 | template <ArtJvmtiEvent kEvent> |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 222 | ALWAYS_INLINE inline void DispatchClassFileLoadHookEvent(art::Thread* thread, |
Andreas Gampe | 983c175 | 2017-01-23 19:46:56 -0800 | [diff] [blame] | 223 | JNIEnv* jnienv, |
| 224 | jclass class_being_redefined, |
| 225 | jobject loader, |
| 226 | const char* name, |
| 227 | jobject protection_domain, |
| 228 | jint class_data_len, |
| 229 | const unsigned char* class_data, |
| 230 | jint* new_class_data_len, |
| 231 | unsigned char** new_class_data) const; |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 232 | |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 233 | void HandleEventType(ArtJvmtiEvent event, bool enable); |
Alex Light | bebd7bd | 2017-07-25 14:05:52 -0700 | [diff] [blame] | 234 | void HandleLocalAccessCapabilityAdded(); |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 235 | |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 236 | bool OtherMonitorEventsEnabledAnywhere(ArtJvmtiEvent event); |
| 237 | |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 238 | // List of all JvmTiEnv objects that have been created, in their creation order. |
Alex Light | bb76646 | 2017-04-12 16:13:33 -0700 | [diff] [blame] | 239 | // NB Some elements might be null representing envs that have been deleted. They should be skipped |
| 240 | // anytime this list is used. |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 241 | std::vector<ArtJvmTiEnv*> envs; |
| 242 | |
| 243 | // A union of all enabled events, anywhere. |
| 244 | EventMask global_mask; |
| 245 | |
| 246 | std::unique_ptr<JvmtiAllocationListener> alloc_listener_; |
Andreas Gampe | 9b8c588 | 2016-10-21 15:27:46 -0700 | [diff] [blame] | 247 | std::unique_ptr<JvmtiGcPauseListener> gc_pause_listener_; |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 248 | std::unique_ptr<JvmtiMethodTraceListener> method_trace_listener_; |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 249 | std::unique_ptr<JvmtiMonitorListener> monitor_listener_; |
Alex Light | e814f9d | 2017-07-31 16:14:39 -0700 | [diff] [blame] | 250 | |
| 251 | // True if frame pop has ever been enabled. Since we store pointers to stack frames we need to |
| 252 | // continue to listen to this event even if it has been disabled. |
| 253 | // TODO We could remove the listeners once all jvmtiEnvs have drained their shadow-frame vectors. |
| 254 | bool frame_pop_enabled; |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 255 | }; |
| 256 | |
| 257 | } // namespace openjdkjvmti |
| 258 | |
Andreas Gampe | 06c42a5 | 2017-07-26 14:17:14 -0700 | [diff] [blame] | 259 | #endif // ART_OPENJDKJVMTI_EVENTS_H_ |