blob: 1e07bc6b7bc5130c75dc136c0d4e25bddcae5412 [file] [log] [blame]
Andreas Gampe77708d92016-10-07 11:48:21 -07001/*
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 Light73afd322017-01-18 11:17:47 -080020#include <array>
21
Andreas Gampe77708d92016-10-07 11:48:21 -070022#include "events.h"
23
24#include "art_jvmti.h"
25
26namespace openjdkjvmti {
27
Alex Light73afd322017-01-18 11:17:47 -080028static 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 Light40d87f42017-01-18 10:27:06 -080038}
39
Andreas Gampe77708d92016-10-07 11:48:21 -070040template <typename FnType>
Alex Light40d87f42017-01-18 10:27:06 -080041ALWAYS_INLINE static inline FnType* GetCallback(ArtJvmTiEnv* env, ArtJvmtiEvent event) {
Andreas Gampe77708d92016-10-07 11:48:21 -070042 if (env->event_callbacks == nullptr) {
43 return nullptr;
44 }
45
Andreas Gampe27fa96c2016-10-07 15:05:24 -070046 // TODO: Add a type check. Can be done, for example, by an explicitly instantiated template
47 // function.
48
Andreas Gampe77708d92016-10-07 11:48:21 -070049 switch (event) {
Alex Light40d87f42017-01-18 10:27:06 -080050 case ArtJvmtiEvent::kVmInit:
Andreas Gampe77708d92016-10-07 11:48:21 -070051 return reinterpret_cast<FnType*>(env->event_callbacks->VMInit);
Alex Light40d87f42017-01-18 10:27:06 -080052 case ArtJvmtiEvent::kVmDeath:
Andreas Gampe77708d92016-10-07 11:48:21 -070053 return reinterpret_cast<FnType*>(env->event_callbacks->VMDeath);
Alex Light40d87f42017-01-18 10:27:06 -080054 case ArtJvmtiEvent::kThreadStart:
Andreas Gampe77708d92016-10-07 11:48:21 -070055 return reinterpret_cast<FnType*>(env->event_callbacks->ThreadStart);
Alex Light40d87f42017-01-18 10:27:06 -080056 case ArtJvmtiEvent::kThreadEnd:
Andreas Gampe77708d92016-10-07 11:48:21 -070057 return reinterpret_cast<FnType*>(env->event_callbacks->ThreadEnd);
Alex Light73afd322017-01-18 11:17:47 -080058 case ArtJvmtiEvent::kClassFileLoadHookRetransformable:
59 case ArtJvmtiEvent::kClassFileLoadHookNonRetransformable:
Andreas Gampe77708d92016-10-07 11:48:21 -070060 return reinterpret_cast<FnType*>(env->event_callbacks->ClassFileLoadHook);
Alex Light40d87f42017-01-18 10:27:06 -080061 case ArtJvmtiEvent::kClassLoad:
Andreas Gampe77708d92016-10-07 11:48:21 -070062 return reinterpret_cast<FnType*>(env->event_callbacks->ClassLoad);
Alex Light40d87f42017-01-18 10:27:06 -080063 case ArtJvmtiEvent::kClassPrepare:
Andreas Gampe77708d92016-10-07 11:48:21 -070064 return reinterpret_cast<FnType*>(env->event_callbacks->ClassPrepare);
Alex Light40d87f42017-01-18 10:27:06 -080065 case ArtJvmtiEvent::kVmStart:
Andreas Gampe77708d92016-10-07 11:48:21 -070066 return reinterpret_cast<FnType*>(env->event_callbacks->VMStart);
Alex Light40d87f42017-01-18 10:27:06 -080067 case ArtJvmtiEvent::kException:
Andreas Gampe77708d92016-10-07 11:48:21 -070068 return reinterpret_cast<FnType*>(env->event_callbacks->Exception);
Alex Light40d87f42017-01-18 10:27:06 -080069 case ArtJvmtiEvent::kExceptionCatch:
Andreas Gampe77708d92016-10-07 11:48:21 -070070 return reinterpret_cast<FnType*>(env->event_callbacks->ExceptionCatch);
Alex Light40d87f42017-01-18 10:27:06 -080071 case ArtJvmtiEvent::kSingleStep:
Andreas Gampe77708d92016-10-07 11:48:21 -070072 return reinterpret_cast<FnType*>(env->event_callbacks->SingleStep);
Alex Light40d87f42017-01-18 10:27:06 -080073 case ArtJvmtiEvent::kFramePop:
Andreas Gampe77708d92016-10-07 11:48:21 -070074 return reinterpret_cast<FnType*>(env->event_callbacks->FramePop);
Alex Light40d87f42017-01-18 10:27:06 -080075 case ArtJvmtiEvent::kBreakpoint:
Andreas Gampe77708d92016-10-07 11:48:21 -070076 return reinterpret_cast<FnType*>(env->event_callbacks->Breakpoint);
Alex Light40d87f42017-01-18 10:27:06 -080077 case ArtJvmtiEvent::kFieldAccess:
Andreas Gampe77708d92016-10-07 11:48:21 -070078 return reinterpret_cast<FnType*>(env->event_callbacks->FieldAccess);
Alex Light40d87f42017-01-18 10:27:06 -080079 case ArtJvmtiEvent::kFieldModification:
Andreas Gampe77708d92016-10-07 11:48:21 -070080 return reinterpret_cast<FnType*>(env->event_callbacks->FieldModification);
Alex Light40d87f42017-01-18 10:27:06 -080081 case ArtJvmtiEvent::kMethodEntry:
Andreas Gampe77708d92016-10-07 11:48:21 -070082 return reinterpret_cast<FnType*>(env->event_callbacks->MethodEntry);
Alex Light40d87f42017-01-18 10:27:06 -080083 case ArtJvmtiEvent::kMethodExit:
Andreas Gampe77708d92016-10-07 11:48:21 -070084 return reinterpret_cast<FnType*>(env->event_callbacks->MethodExit);
Alex Light40d87f42017-01-18 10:27:06 -080085 case ArtJvmtiEvent::kNativeMethodBind:
Andreas Gampe77708d92016-10-07 11:48:21 -070086 return reinterpret_cast<FnType*>(env->event_callbacks->NativeMethodBind);
Alex Light40d87f42017-01-18 10:27:06 -080087 case ArtJvmtiEvent::kCompiledMethodLoad:
Andreas Gampe77708d92016-10-07 11:48:21 -070088 return reinterpret_cast<FnType*>(env->event_callbacks->CompiledMethodLoad);
Alex Light40d87f42017-01-18 10:27:06 -080089 case ArtJvmtiEvent::kCompiledMethodUnload:
Andreas Gampe77708d92016-10-07 11:48:21 -070090 return reinterpret_cast<FnType*>(env->event_callbacks->CompiledMethodUnload);
Alex Light40d87f42017-01-18 10:27:06 -080091 case ArtJvmtiEvent::kDynamicCodeGenerated:
Andreas Gampe77708d92016-10-07 11:48:21 -070092 return reinterpret_cast<FnType*>(env->event_callbacks->DynamicCodeGenerated);
Alex Light40d87f42017-01-18 10:27:06 -080093 case ArtJvmtiEvent::kDataDumpRequest:
Andreas Gampe77708d92016-10-07 11:48:21 -070094 return reinterpret_cast<FnType*>(env->event_callbacks->DataDumpRequest);
Alex Light40d87f42017-01-18 10:27:06 -080095 case ArtJvmtiEvent::kMonitorWait:
Andreas Gampe77708d92016-10-07 11:48:21 -070096 return reinterpret_cast<FnType*>(env->event_callbacks->MonitorWait);
Alex Light40d87f42017-01-18 10:27:06 -080097 case ArtJvmtiEvent::kMonitorWaited:
Andreas Gampe77708d92016-10-07 11:48:21 -070098 return reinterpret_cast<FnType*>(env->event_callbacks->MonitorWaited);
Alex Light40d87f42017-01-18 10:27:06 -080099 case ArtJvmtiEvent::kMonitorContendedEnter:
Andreas Gampe77708d92016-10-07 11:48:21 -0700100 return reinterpret_cast<FnType*>(env->event_callbacks->MonitorContendedEnter);
Alex Light40d87f42017-01-18 10:27:06 -0800101 case ArtJvmtiEvent::kMonitorContendedEntered:
Andreas Gampe77708d92016-10-07 11:48:21 -0700102 return reinterpret_cast<FnType*>(env->event_callbacks->MonitorContendedEntered);
Alex Light40d87f42017-01-18 10:27:06 -0800103 case ArtJvmtiEvent::kResourceExhausted:
Andreas Gampe77708d92016-10-07 11:48:21 -0700104 return reinterpret_cast<FnType*>(env->event_callbacks->ResourceExhausted);
Alex Light40d87f42017-01-18 10:27:06 -0800105 case ArtJvmtiEvent::kGarbageCollectionStart:
Andreas Gampe77708d92016-10-07 11:48:21 -0700106 return reinterpret_cast<FnType*>(env->event_callbacks->GarbageCollectionStart);
Alex Light40d87f42017-01-18 10:27:06 -0800107 case ArtJvmtiEvent::kGarbageCollectionFinish:
Andreas Gampe77708d92016-10-07 11:48:21 -0700108 return reinterpret_cast<FnType*>(env->event_callbacks->GarbageCollectionFinish);
Alex Light40d87f42017-01-18 10:27:06 -0800109 case ArtJvmtiEvent::kObjectFree:
Andreas Gampe77708d92016-10-07 11:48:21 -0700110 return reinterpret_cast<FnType*>(env->event_callbacks->ObjectFree);
Alex Light40d87f42017-01-18 10:27:06 -0800111 case ArtJvmtiEvent::kVmObjectAlloc:
Andreas Gampe77708d92016-10-07 11:48:21 -0700112 return reinterpret_cast<FnType*>(env->event_callbacks->VMObjectAlloc);
113 }
114 return nullptr;
115}
116
117template <typename ...Args>
Alex Light40d87f42017-01-18 10:27:06 -0800118inline void EventHandler::DispatchEvent(art::Thread* thread,
119 ArtJvmtiEvent event,
120 Args... args) const {
Andreas Gampe77708d92016-10-07 11:48:21 -0700121 using FnType = void(jvmtiEnv*, Args...);
122 for (ArtJvmTiEnv* env : envs) {
Alex Light40d87f42017-01-18 10:27:06 -0800123 if (ShouldDispatch(event, env, thread)) {
Andreas Gampe77708d92016-10-07 11:48:21 -0700124 FnType* callback = GetCallback<FnType>(env, event);
125 if (callback != nullptr) {
126 (*callback)(env, args...);
127 }
128 }
129 }
130}
131
Alex Light40d87f42017-01-18 10:27:06 -0800132inline 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 Light73afd322017-01-18 11:17:47 -0800144inline 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
156inline 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
166inline 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 Gampe77708d92016-10-07 11:48:21 -0700178} // namespace openjdkjvmti
179
180#endif // ART_RUNTIME_OPENJDKJVMTI_EVENTS_INL_H_