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 | |
| 17 | #ifndef ART_RUNTIME_OPENJDKJVMTI_EVENTS_H_ |
| 18 | #define ART_RUNTIME_OPENJDKJVMTI_EVENTS_H_ |
| 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; |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 32 | |
Alex Light | 73afd32 | 2017-01-18 11:17:47 -0800 | [diff] [blame] | 33 | // an enum for ArtEvents. This differs from the JVMTI events only in that we distinguish between |
| 34 | // retransformation capable and incapable loading |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 35 | enum class ArtJvmtiEvent { |
| 36 | kMinEventTypeVal = JVMTI_MIN_EVENT_TYPE_VAL, |
| 37 | kVmInit = JVMTI_EVENT_VM_INIT, |
| 38 | kVmDeath = JVMTI_EVENT_VM_DEATH, |
| 39 | kThreadStart = JVMTI_EVENT_THREAD_START, |
| 40 | kThreadEnd = JVMTI_EVENT_THREAD_END, |
Alex Light | 73afd32 | 2017-01-18 11:17:47 -0800 | [diff] [blame] | 41 | kClassFileLoadHookNonRetransformable = JVMTI_EVENT_CLASS_FILE_LOAD_HOOK, |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 42 | kClassLoad = JVMTI_EVENT_CLASS_LOAD, |
| 43 | kClassPrepare = JVMTI_EVENT_CLASS_PREPARE, |
| 44 | kVmStart = JVMTI_EVENT_VM_START, |
| 45 | kException = JVMTI_EVENT_EXCEPTION, |
| 46 | kExceptionCatch = JVMTI_EVENT_EXCEPTION_CATCH, |
| 47 | kSingleStep = JVMTI_EVENT_SINGLE_STEP, |
| 48 | kFramePop = JVMTI_EVENT_FRAME_POP, |
| 49 | kBreakpoint = JVMTI_EVENT_BREAKPOINT, |
| 50 | kFieldAccess = JVMTI_EVENT_FIELD_ACCESS, |
| 51 | kFieldModification = JVMTI_EVENT_FIELD_MODIFICATION, |
| 52 | kMethodEntry = JVMTI_EVENT_METHOD_ENTRY, |
| 53 | kMethodExit = JVMTI_EVENT_METHOD_EXIT, |
| 54 | kNativeMethodBind = JVMTI_EVENT_NATIVE_METHOD_BIND, |
| 55 | kCompiledMethodLoad = JVMTI_EVENT_COMPILED_METHOD_LOAD, |
| 56 | kCompiledMethodUnload = JVMTI_EVENT_COMPILED_METHOD_UNLOAD, |
| 57 | kDynamicCodeGenerated = JVMTI_EVENT_DYNAMIC_CODE_GENERATED, |
| 58 | kDataDumpRequest = JVMTI_EVENT_DATA_DUMP_REQUEST, |
| 59 | kMonitorWait = JVMTI_EVENT_MONITOR_WAIT, |
| 60 | kMonitorWaited = JVMTI_EVENT_MONITOR_WAITED, |
| 61 | kMonitorContendedEnter = JVMTI_EVENT_MONITOR_CONTENDED_ENTER, |
| 62 | kMonitorContendedEntered = JVMTI_EVENT_MONITOR_CONTENDED_ENTERED, |
| 63 | kResourceExhausted = JVMTI_EVENT_RESOURCE_EXHAUSTED, |
| 64 | kGarbageCollectionStart = JVMTI_EVENT_GARBAGE_COLLECTION_START, |
| 65 | kGarbageCollectionFinish = JVMTI_EVENT_GARBAGE_COLLECTION_FINISH, |
| 66 | kObjectFree = JVMTI_EVENT_OBJECT_FREE, |
| 67 | kVmObjectAlloc = JVMTI_EVENT_VM_OBJECT_ALLOC, |
Alex Light | 73afd32 | 2017-01-18 11:17:47 -0800 | [diff] [blame] | 68 | kClassFileLoadHookRetransformable = JVMTI_MAX_EVENT_TYPE_VAL + 1, |
| 69 | kMaxEventTypeVal = kClassFileLoadHookRetransformable, |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 70 | }; |
| 71 | |
| 72 | // Convert a jvmtiEvent into a ArtJvmtiEvent |
| 73 | ALWAYS_INLINE static inline ArtJvmtiEvent GetArtJvmtiEvent(ArtJvmTiEnv* env, jvmtiEvent e); |
| 74 | |
Alex Light | 73afd32 | 2017-01-18 11:17:47 -0800 | [diff] [blame] | 75 | static inline jvmtiEvent GetJvmtiEvent(ArtJvmtiEvent e) { |
| 76 | if (UNLIKELY(e == ArtJvmtiEvent::kClassFileLoadHookRetransformable)) { |
| 77 | return JVMTI_EVENT_CLASS_FILE_LOAD_HOOK; |
| 78 | } else { |
| 79 | return static_cast<jvmtiEvent>(e); |
| 80 | } |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 81 | } |
| 82 | |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 83 | struct EventMask { |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 84 | static constexpr size_t kEventsSize = |
| 85 | static_cast<size_t>(ArtJvmtiEvent::kMaxEventTypeVal) - |
| 86 | static_cast<size_t>(ArtJvmtiEvent::kMinEventTypeVal) + 1; |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 87 | std::bitset<kEventsSize> bit_set; |
| 88 | |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 89 | static bool EventIsInRange(ArtJvmtiEvent event) { |
| 90 | return event >= ArtJvmtiEvent::kMinEventTypeVal && event <= ArtJvmtiEvent::kMaxEventTypeVal; |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 93 | void Set(ArtJvmtiEvent event, bool value = true) { |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 94 | DCHECK(EventIsInRange(event)); |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 95 | bit_set.set(static_cast<size_t>(event) - static_cast<size_t>(ArtJvmtiEvent::kMinEventTypeVal), |
| 96 | value); |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 99 | bool Test(ArtJvmtiEvent event) const { |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 100 | DCHECK(EventIsInRange(event)); |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 101 | return bit_set.test( |
| 102 | static_cast<size_t>(event) - static_cast<size_t>(ArtJvmtiEvent::kMinEventTypeVal)); |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 103 | } |
| 104 | }; |
| 105 | |
| 106 | struct EventMasks { |
| 107 | // The globally enabled events. |
| 108 | EventMask global_event_mask; |
| 109 | |
| 110 | // The per-thread enabled events. |
| 111 | |
| 112 | // It is not enough to store a Thread pointer, as these may be reused. Use the pointer and the |
| 113 | // thread id. |
| 114 | // Note: We could just use the tid like tracing does. |
| 115 | using UniqueThread = std::pair<art::Thread*, uint32_t>; |
| 116 | // TODO: Native thread objects are immovable, so we can use them as keys in an (unordered) map, |
| 117 | // if necessary. |
| 118 | std::vector<std::pair<UniqueThread, EventMask>> thread_event_masks; |
| 119 | |
| 120 | // A union of the per-thread events, for fast-pathing. |
| 121 | EventMask unioned_thread_event_mask; |
| 122 | |
| 123 | EventMask& GetEventMask(art::Thread* thread); |
| 124 | EventMask* GetEventMaskOrNull(art::Thread* thread); |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 125 | void EnableEvent(art::Thread* thread, ArtJvmtiEvent event); |
| 126 | void DisableEvent(art::Thread* thread, ArtJvmtiEvent event); |
Alex Light | 73afd32 | 2017-01-18 11:17:47 -0800 | [diff] [blame] | 127 | bool IsEnabledAnywhere(ArtJvmtiEvent event); |
| 128 | // Make any changes to event masks needed for the given capability changes. If caps_added is true |
| 129 | // then caps is all the newly set capabilities of the jvmtiEnv. If it is false then caps is the |
| 130 | // set of all capabilities that were removed from the jvmtiEnv. |
| 131 | void HandleChangedCapabilities(const jvmtiCapabilities& caps, bool caps_added); |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 132 | }; |
| 133 | |
| 134 | // Helper class for event handling. |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 135 | class EventHandler { |
| 136 | public: |
| 137 | EventHandler(); |
| 138 | ~EventHandler(); |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 139 | |
| 140 | // Register an env. It is assumed that this happens on env creation, that is, no events are |
| 141 | // enabled, yet. |
| 142 | void RegisterArtJvmTiEnv(ArtJvmTiEnv* env); |
| 143 | |
Andreas Gampe | 3a7eb14 | 2017-01-19 21:59:22 -0800 | [diff] [blame] | 144 | // Remove an env. |
| 145 | void RemoveArtJvmTiEnv(ArtJvmTiEnv* env); |
| 146 | |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 147 | bool IsEventEnabledAnywhere(ArtJvmtiEvent event) const { |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 148 | if (!EventMask::EventIsInRange(event)) { |
| 149 | return false; |
| 150 | } |
| 151 | return global_mask.Test(event); |
| 152 | } |
| 153 | |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 154 | jvmtiError SetEvent(ArtJvmTiEnv* env, |
| 155 | art::Thread* thread, |
| 156 | ArtJvmtiEvent event, |
| 157 | jvmtiEventMode mode); |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 158 | |
Andreas Gampe | 983c175 | 2017-01-23 19:46:56 -0800 | [diff] [blame^] | 159 | template <ArtJvmtiEvent kEvent, typename ...Args> |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 160 | ALWAYS_INLINE |
Andreas Gampe | 983c175 | 2017-01-23 19:46:56 -0800 | [diff] [blame^] | 161 | inline void DispatchEvent(art::Thread* thread, Args... args) const; |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 162 | |
Alex Light | 73afd32 | 2017-01-18 11:17:47 -0800 | [diff] [blame] | 163 | // Tell the event handler capabilities were added/lost so it can adjust the sent events.If |
| 164 | // caps_added is true then caps is all the newly set capabilities of the jvmtiEnv. If it is false |
| 165 | // then caps is the set of all capabilities that were removed from the jvmtiEnv. |
| 166 | ALWAYS_INLINE |
| 167 | inline void HandleChangedCapabilities(ArtJvmTiEnv* env, |
| 168 | const jvmtiCapabilities& caps, |
| 169 | bool added); |
| 170 | |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 171 | private: |
Andreas Gampe | 983c175 | 2017-01-23 19:46:56 -0800 | [diff] [blame^] | 172 | template <ArtJvmtiEvent kEvent> |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 173 | ALWAYS_INLINE |
Andreas Gampe | 983c175 | 2017-01-23 19:46:56 -0800 | [diff] [blame^] | 174 | static inline bool ShouldDispatch(ArtJvmTiEnv* env, art::Thread* thread); |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 175 | |
Alex Light | 73afd32 | 2017-01-18 11:17:47 -0800 | [diff] [blame] | 176 | ALWAYS_INLINE |
| 177 | inline bool NeedsEventUpdate(ArtJvmTiEnv* env, |
| 178 | const jvmtiCapabilities& caps, |
| 179 | bool added); |
| 180 | |
| 181 | // Recalculates the event mask for the given event. |
| 182 | ALWAYS_INLINE |
| 183 | inline void RecalculateGlobalEventMask(ArtJvmtiEvent event); |
| 184 | |
Andreas Gampe | 983c175 | 2017-01-23 19:46:56 -0800 | [diff] [blame^] | 185 | template <ArtJvmtiEvent kEvent> |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 186 | ALWAYS_INLINE inline void DispatchClassFileLoadHookEvent(art::Thread* thread, |
Andreas Gampe | 983c175 | 2017-01-23 19:46:56 -0800 | [diff] [blame^] | 187 | JNIEnv* jnienv, |
| 188 | jclass class_being_redefined, |
| 189 | jobject loader, |
| 190 | const char* name, |
| 191 | jobject protection_domain, |
| 192 | jint class_data_len, |
| 193 | const unsigned char* class_data, |
| 194 | jint* new_class_data_len, |
| 195 | unsigned char** new_class_data) const; |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 196 | |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 197 | void HandleEventType(ArtJvmtiEvent event, bool enable); |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 198 | |
| 199 | // List of all JvmTiEnv objects that have been created, in their creation order. |
| 200 | std::vector<ArtJvmTiEnv*> envs; |
| 201 | |
| 202 | // A union of all enabled events, anywhere. |
| 203 | EventMask global_mask; |
| 204 | |
| 205 | std::unique_ptr<JvmtiAllocationListener> alloc_listener_; |
Andreas Gampe | 9b8c588 | 2016-10-21 15:27:46 -0700 | [diff] [blame] | 206 | std::unique_ptr<JvmtiGcPauseListener> gc_pause_listener_; |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 207 | }; |
| 208 | |
| 209 | } // namespace openjdkjvmti |
| 210 | |
| 211 | #endif // ART_RUNTIME_OPENJDKJVMTI_EVENTS_H_ |