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_INL_H_ |
| 18 | #define ART_RUNTIME_OPENJDKJVMTI_EVENTS_INL_H_ |
| 19 | |
Alex Light | 73afd32 | 2017-01-18 11:17:47 -0800 | [diff] [blame] | 20 | #include <array> |
| 21 | |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 22 | #include "events.h" |
| 23 | |
| 24 | #include "art_jvmti.h" |
| 25 | |
| 26 | namespace openjdkjvmti { |
| 27 | |
Alex Light | 73afd32 | 2017-01-18 11:17:47 -0800 | [diff] [blame] | 28 | static inline ArtJvmtiEvent GetArtJvmtiEvent(ArtJvmTiEnv* env, jvmtiEvent e) { |
| 29 | if (UNLIKELY(e == JVMTI_EVENT_CLASS_FILE_LOAD_HOOK)) { |
| 30 | if (env->capabilities.can_retransform_classes) { |
| 31 | return ArtJvmtiEvent::kClassFileLoadHookRetransformable; |
| 32 | } else { |
| 33 | return ArtJvmtiEvent::kClassFileLoadHookNonRetransformable; |
| 34 | } |
| 35 | } else { |
| 36 | return static_cast<ArtJvmtiEvent>(e); |
| 37 | } |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 38 | } |
| 39 | |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 40 | template <typename FnType> |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 41 | ALWAYS_INLINE static inline FnType* GetCallback(ArtJvmTiEnv* env, ArtJvmtiEvent event) { |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 42 | if (env->event_callbacks == nullptr) { |
| 43 | return nullptr; |
| 44 | } |
| 45 | |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 46 | // TODO: Add a type check. Can be done, for example, by an explicitly instantiated template |
| 47 | // function. |
| 48 | |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 49 | switch (event) { |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 50 | case ArtJvmtiEvent::kVmInit: |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 51 | return reinterpret_cast<FnType*>(env->event_callbacks->VMInit); |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 52 | case ArtJvmtiEvent::kVmDeath: |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 53 | return reinterpret_cast<FnType*>(env->event_callbacks->VMDeath); |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 54 | case ArtJvmtiEvent::kThreadStart: |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 55 | return reinterpret_cast<FnType*>(env->event_callbacks->ThreadStart); |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 56 | case ArtJvmtiEvent::kThreadEnd: |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 57 | return reinterpret_cast<FnType*>(env->event_callbacks->ThreadEnd); |
Alex Light | 73afd32 | 2017-01-18 11:17:47 -0800 | [diff] [blame] | 58 | case ArtJvmtiEvent::kClassFileLoadHookRetransformable: |
| 59 | case ArtJvmtiEvent::kClassFileLoadHookNonRetransformable: |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 60 | return reinterpret_cast<FnType*>(env->event_callbacks->ClassFileLoadHook); |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 61 | case ArtJvmtiEvent::kClassLoad: |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 62 | return reinterpret_cast<FnType*>(env->event_callbacks->ClassLoad); |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 63 | case ArtJvmtiEvent::kClassPrepare: |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 64 | return reinterpret_cast<FnType*>(env->event_callbacks->ClassPrepare); |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 65 | case ArtJvmtiEvent::kVmStart: |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 66 | return reinterpret_cast<FnType*>(env->event_callbacks->VMStart); |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 67 | case ArtJvmtiEvent::kException: |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 68 | return reinterpret_cast<FnType*>(env->event_callbacks->Exception); |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 69 | case ArtJvmtiEvent::kExceptionCatch: |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 70 | return reinterpret_cast<FnType*>(env->event_callbacks->ExceptionCatch); |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 71 | case ArtJvmtiEvent::kSingleStep: |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 72 | return reinterpret_cast<FnType*>(env->event_callbacks->SingleStep); |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 73 | case ArtJvmtiEvent::kFramePop: |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 74 | return reinterpret_cast<FnType*>(env->event_callbacks->FramePop); |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 75 | case ArtJvmtiEvent::kBreakpoint: |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 76 | return reinterpret_cast<FnType*>(env->event_callbacks->Breakpoint); |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 77 | case ArtJvmtiEvent::kFieldAccess: |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 78 | return reinterpret_cast<FnType*>(env->event_callbacks->FieldAccess); |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 79 | case ArtJvmtiEvent::kFieldModification: |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 80 | return reinterpret_cast<FnType*>(env->event_callbacks->FieldModification); |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 81 | case ArtJvmtiEvent::kMethodEntry: |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 82 | return reinterpret_cast<FnType*>(env->event_callbacks->MethodEntry); |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 83 | case ArtJvmtiEvent::kMethodExit: |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 84 | return reinterpret_cast<FnType*>(env->event_callbacks->MethodExit); |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 85 | case ArtJvmtiEvent::kNativeMethodBind: |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 86 | return reinterpret_cast<FnType*>(env->event_callbacks->NativeMethodBind); |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 87 | case ArtJvmtiEvent::kCompiledMethodLoad: |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 88 | return reinterpret_cast<FnType*>(env->event_callbacks->CompiledMethodLoad); |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 89 | case ArtJvmtiEvent::kCompiledMethodUnload: |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 90 | return reinterpret_cast<FnType*>(env->event_callbacks->CompiledMethodUnload); |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 91 | case ArtJvmtiEvent::kDynamicCodeGenerated: |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 92 | return reinterpret_cast<FnType*>(env->event_callbacks->DynamicCodeGenerated); |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 93 | case ArtJvmtiEvent::kDataDumpRequest: |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 94 | return reinterpret_cast<FnType*>(env->event_callbacks->DataDumpRequest); |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 95 | case ArtJvmtiEvent::kMonitorWait: |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 96 | return reinterpret_cast<FnType*>(env->event_callbacks->MonitorWait); |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 97 | case ArtJvmtiEvent::kMonitorWaited: |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 98 | return reinterpret_cast<FnType*>(env->event_callbacks->MonitorWaited); |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 99 | case ArtJvmtiEvent::kMonitorContendedEnter: |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 100 | return reinterpret_cast<FnType*>(env->event_callbacks->MonitorContendedEnter); |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 101 | case ArtJvmtiEvent::kMonitorContendedEntered: |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 102 | return reinterpret_cast<FnType*>(env->event_callbacks->MonitorContendedEntered); |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 103 | case ArtJvmtiEvent::kResourceExhausted: |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 104 | return reinterpret_cast<FnType*>(env->event_callbacks->ResourceExhausted); |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 105 | case ArtJvmtiEvent::kGarbageCollectionStart: |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 106 | return reinterpret_cast<FnType*>(env->event_callbacks->GarbageCollectionStart); |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 107 | case ArtJvmtiEvent::kGarbageCollectionFinish: |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 108 | return reinterpret_cast<FnType*>(env->event_callbacks->GarbageCollectionFinish); |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 109 | case ArtJvmtiEvent::kObjectFree: |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 110 | return reinterpret_cast<FnType*>(env->event_callbacks->ObjectFree); |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 111 | case ArtJvmtiEvent::kVmObjectAlloc: |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 112 | return reinterpret_cast<FnType*>(env->event_callbacks->VMObjectAlloc); |
| 113 | } |
| 114 | return nullptr; |
| 115 | } |
| 116 | |
| 117 | template <typename ...Args> |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 118 | inline void EventHandler::DispatchClassFileLoadHookEvent(art::Thread*, |
| 119 | ArtJvmtiEvent event, |
| 120 | Args... args ATTRIBUTE_UNUSED) const { |
| 121 | CHECK(event == ArtJvmtiEvent::kClassFileLoadHookRetransformable || |
| 122 | event == ArtJvmtiEvent::kClassFileLoadHookNonRetransformable); |
| 123 | LOG(FATAL) << "Incorrect arguments to ClassFileLoadHook!"; |
| 124 | } |
| 125 | |
| 126 | // TODO Locking of some type! |
| 127 | template <> |
| 128 | inline void EventHandler::DispatchClassFileLoadHookEvent(art::Thread* thread, |
| 129 | ArtJvmtiEvent event, |
| 130 | JNIEnv* jnienv, |
| 131 | jclass class_being_redefined, |
| 132 | jobject loader, |
| 133 | const char* name, |
| 134 | jobject protection_domain, |
| 135 | jint class_data_len, |
| 136 | const unsigned char* class_data, |
| 137 | jint* new_class_data_len, |
| 138 | unsigned char** new_class_data) const { |
| 139 | CHECK(event == ArtJvmtiEvent::kClassFileLoadHookRetransformable || |
| 140 | event == ArtJvmtiEvent::kClassFileLoadHookNonRetransformable); |
| 141 | using FnType = void(jvmtiEnv* /* jvmti_env */, |
| 142 | JNIEnv* /* jnienv */, |
| 143 | jclass /* class_being_redefined */, |
| 144 | jobject /* loader */, |
| 145 | const char* /* name */, |
| 146 | jobject /* protection_domain */, |
| 147 | jint /* class_data_len */, |
| 148 | const unsigned char* /* class_data */, |
| 149 | jint* /* new_class_data_len */, |
| 150 | unsigned char** /* new_class_data */); |
| 151 | jint current_len = class_data_len; |
| 152 | unsigned char* current_class_data = const_cast<unsigned char*>(class_data); |
| 153 | ArtJvmTiEnv* last_env = nullptr; |
| 154 | for (ArtJvmTiEnv* env : envs) { |
| 155 | if (ShouldDispatch(event, env, thread)) { |
| 156 | jint new_len; |
| 157 | unsigned char* new_data; |
| 158 | FnType* callback = GetCallback<FnType>(env, event); |
| 159 | callback(env, |
| 160 | jnienv, |
| 161 | class_being_redefined, |
| 162 | loader, |
| 163 | name, |
| 164 | protection_domain, |
| 165 | current_len, |
| 166 | current_class_data, |
| 167 | &new_len, |
| 168 | &new_data); |
| 169 | if (new_data != nullptr && new_data != current_class_data) { |
| 170 | // Destroy the data the last transformer made. We skip this if the previous state was the |
| 171 | // initial one since we don't know here which jvmtiEnv allocated it. |
| 172 | // NB Currently this doesn't matter since all allocations just go to malloc but in the |
| 173 | // future we might have jvmtiEnv's keep track of their allocations for leak-checking. |
| 174 | if (last_env != nullptr) { |
| 175 | last_env->Deallocate(current_class_data); |
| 176 | } |
| 177 | last_env = env; |
| 178 | current_class_data = new_data; |
| 179 | current_len = new_len; |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | if (last_env != nullptr) { |
| 184 | *new_class_data_len = current_len; |
| 185 | *new_class_data = current_class_data; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | template <typename ...Args> |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 190 | inline void EventHandler::DispatchEvent(art::Thread* thread, |
| 191 | ArtJvmtiEvent event, |
| 192 | Args... args) const { |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 193 | switch (event) { |
| 194 | case ArtJvmtiEvent::kClassFileLoadHookRetransformable: |
| 195 | case ArtJvmtiEvent::kClassFileLoadHookNonRetransformable: |
| 196 | return DispatchClassFileLoadHookEvent(thread, event, args...); |
| 197 | default: |
| 198 | return GenericDispatchEvent(thread, event, args...); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | // TODO Locking of some type! |
| 203 | template <typename ...Args> |
| 204 | inline void EventHandler::GenericDispatchEvent(art::Thread* thread, |
| 205 | ArtJvmtiEvent event, |
| 206 | Args... args) const { |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 207 | using FnType = void(jvmtiEnv*, Args...); |
| 208 | for (ArtJvmTiEnv* env : envs) { |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 209 | if (ShouldDispatch(event, env, thread)) { |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 210 | FnType* callback = GetCallback<FnType>(env, event); |
| 211 | if (callback != nullptr) { |
| 212 | (*callback)(env, args...); |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 218 | inline bool EventHandler::ShouldDispatch(ArtJvmtiEvent event, |
| 219 | ArtJvmTiEnv* env, |
| 220 | art::Thread* thread) { |
| 221 | bool dispatch = env->event_masks.global_event_mask.Test(event); |
| 222 | |
| 223 | if (!dispatch && thread != nullptr && env->event_masks.unioned_thread_event_mask.Test(event)) { |
| 224 | EventMask* mask = env->event_masks.GetEventMaskOrNull(thread); |
| 225 | dispatch = mask != nullptr && mask->Test(event); |
| 226 | } |
| 227 | return dispatch; |
| 228 | } |
| 229 | |
Alex Light | 73afd32 | 2017-01-18 11:17:47 -0800 | [diff] [blame] | 230 | inline void EventHandler::RecalculateGlobalEventMask(ArtJvmtiEvent event) { |
| 231 | bool union_value = false; |
| 232 | for (const ArtJvmTiEnv* stored_env : envs) { |
| 233 | union_value |= stored_env->event_masks.global_event_mask.Test(event); |
| 234 | union_value |= stored_env->event_masks.unioned_thread_event_mask.Test(event); |
| 235 | if (union_value) { |
| 236 | break; |
| 237 | } |
| 238 | } |
| 239 | global_mask.Set(event, union_value); |
| 240 | } |
| 241 | |
| 242 | inline bool EventHandler::NeedsEventUpdate(ArtJvmTiEnv* env, |
| 243 | const jvmtiCapabilities& caps, |
| 244 | bool added) { |
| 245 | ArtJvmtiEvent event = added ? ArtJvmtiEvent::kClassFileLoadHookNonRetransformable |
| 246 | : ArtJvmtiEvent::kClassFileLoadHookRetransformable; |
| 247 | return caps.can_retransform_classes == 1 && |
| 248 | IsEventEnabledAnywhere(event) && |
| 249 | env->event_masks.IsEnabledAnywhere(event); |
| 250 | } |
| 251 | |
| 252 | inline void EventHandler::HandleChangedCapabilities(ArtJvmTiEnv* env, |
| 253 | const jvmtiCapabilities& caps, |
| 254 | bool added) { |
| 255 | if (UNLIKELY(NeedsEventUpdate(env, caps, added))) { |
| 256 | env->event_masks.HandleChangedCapabilities(caps, added); |
| 257 | if (caps.can_retransform_classes == 1) { |
| 258 | RecalculateGlobalEventMask(ArtJvmtiEvent::kClassFileLoadHookRetransformable); |
| 259 | RecalculateGlobalEventMask(ArtJvmtiEvent::kClassFileLoadHookNonRetransformable); |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 264 | } // namespace openjdkjvmti |
| 265 | |
| 266 | #endif // ART_RUNTIME_OPENJDKJVMTI_EVENTS_INL_H_ |