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 | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 118 | inline void EventHandler::DispatchEvent(art::Thread* thread, |
| 119 | ArtJvmtiEvent event, |
| 120 | Args... args) const { |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 121 | using FnType = void(jvmtiEnv*, Args...); |
| 122 | for (ArtJvmTiEnv* env : envs) { |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 123 | if (ShouldDispatch(event, env, thread)) { |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 124 | FnType* callback = GetCallback<FnType>(env, event); |
| 125 | if (callback != nullptr) { |
| 126 | (*callback)(env, args...); |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 132 | inline bool EventHandler::ShouldDispatch(ArtJvmtiEvent event, |
| 133 | ArtJvmTiEnv* env, |
| 134 | art::Thread* thread) { |
| 135 | bool dispatch = env->event_masks.global_event_mask.Test(event); |
| 136 | |
| 137 | if (!dispatch && thread != nullptr && env->event_masks.unioned_thread_event_mask.Test(event)) { |
| 138 | EventMask* mask = env->event_masks.GetEventMaskOrNull(thread); |
| 139 | dispatch = mask != nullptr && mask->Test(event); |
| 140 | } |
| 141 | return dispatch; |
| 142 | } |
| 143 | |
Alex Light | 73afd32 | 2017-01-18 11:17:47 -0800 | [diff] [blame^] | 144 | inline void EventHandler::RecalculateGlobalEventMask(ArtJvmtiEvent event) { |
| 145 | bool union_value = false; |
| 146 | for (const ArtJvmTiEnv* stored_env : envs) { |
| 147 | union_value |= stored_env->event_masks.global_event_mask.Test(event); |
| 148 | union_value |= stored_env->event_masks.unioned_thread_event_mask.Test(event); |
| 149 | if (union_value) { |
| 150 | break; |
| 151 | } |
| 152 | } |
| 153 | global_mask.Set(event, union_value); |
| 154 | } |
| 155 | |
| 156 | inline bool EventHandler::NeedsEventUpdate(ArtJvmTiEnv* env, |
| 157 | const jvmtiCapabilities& caps, |
| 158 | bool added) { |
| 159 | ArtJvmtiEvent event = added ? ArtJvmtiEvent::kClassFileLoadHookNonRetransformable |
| 160 | : ArtJvmtiEvent::kClassFileLoadHookRetransformable; |
| 161 | return caps.can_retransform_classes == 1 && |
| 162 | IsEventEnabledAnywhere(event) && |
| 163 | env->event_masks.IsEnabledAnywhere(event); |
| 164 | } |
| 165 | |
| 166 | inline void EventHandler::HandleChangedCapabilities(ArtJvmTiEnv* env, |
| 167 | const jvmtiCapabilities& caps, |
| 168 | bool added) { |
| 169 | if (UNLIKELY(NeedsEventUpdate(env, caps, added))) { |
| 170 | env->event_masks.HandleChangedCapabilities(caps, added); |
| 171 | if (caps.can_retransform_classes == 1) { |
| 172 | RecalculateGlobalEventMask(ArtJvmtiEvent::kClassFileLoadHookRetransformable); |
| 173 | RecalculateGlobalEventMask(ArtJvmtiEvent::kClassFileLoadHookNonRetransformable); |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 178 | } // namespace openjdkjvmti |
| 179 | |
| 180 | #endif // ART_RUNTIME_OPENJDKJVMTI_EVENTS_INL_H_ |