blob: 233b45cda88a4127ef7549e0909bfd65f9886c55 [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 Gampe983c1752017-01-23 19:46:56 -080040namespace impl {
Andreas Gampe77708d92016-10-07 11:48:21 -070041
Andreas Gampe983c1752017-01-23 19:46:56 -080042// Infrastructure to achieve type safety for event dispatch.
Andreas Gampe27fa96c2016-10-07 15:05:24 -070043
Andreas Gampe983c1752017-01-23 19:46:56 -080044#define FORALL_EVENT_TYPES(fn) \
45 fn(VMInit, ArtJvmtiEvent::kVmInit) \
46 fn(VMDeath, ArtJvmtiEvent::kVmDeath) \
47 fn(ThreadStart, ArtJvmtiEvent::kThreadStart) \
48 fn(ThreadEnd, ArtJvmtiEvent::kThreadEnd) \
49 fn(ClassFileLoadHook, ArtJvmtiEvent::kClassFileLoadHookRetransformable) \
50 fn(ClassFileLoadHook, ArtJvmtiEvent::kClassFileLoadHookNonRetransformable) \
51 fn(ClassLoad, ArtJvmtiEvent::kClassLoad) \
52 fn(ClassPrepare, ArtJvmtiEvent::kClassPrepare) \
53 fn(VMStart, ArtJvmtiEvent::kVmStart) \
54 fn(Exception, ArtJvmtiEvent::kException) \
55 fn(ExceptionCatch, ArtJvmtiEvent::kExceptionCatch) \
56 fn(SingleStep, ArtJvmtiEvent::kSingleStep) \
57 fn(FramePop, ArtJvmtiEvent::kFramePop) \
58 fn(Breakpoint, ArtJvmtiEvent::kBreakpoint) \
59 fn(FieldAccess, ArtJvmtiEvent::kFieldAccess) \
60 fn(FieldModification, ArtJvmtiEvent::kFieldModification) \
61 fn(MethodEntry, ArtJvmtiEvent::kMethodEntry) \
62 fn(MethodExit, ArtJvmtiEvent::kMethodExit) \
63 fn(NativeMethodBind, ArtJvmtiEvent::kNativeMethodBind) \
64 fn(CompiledMethodLoad, ArtJvmtiEvent::kCompiledMethodLoad) \
65 fn(CompiledMethodUnload, ArtJvmtiEvent::kCompiledMethodUnload) \
66 fn(DynamicCodeGenerated, ArtJvmtiEvent::kDynamicCodeGenerated) \
67 fn(DataDumpRequest, ArtJvmtiEvent::kDataDumpRequest) \
68 fn(MonitorWait, ArtJvmtiEvent::kMonitorWait) \
69 fn(MonitorWaited, ArtJvmtiEvent::kMonitorWaited) \
70 fn(MonitorContendedEnter, ArtJvmtiEvent::kMonitorContendedEnter) \
71 fn(MonitorContendedEntered, ArtJvmtiEvent::kMonitorContendedEntered) \
72 fn(ResourceExhausted, ArtJvmtiEvent::kResourceExhausted) \
73 fn(GarbageCollectionStart, ArtJvmtiEvent::kGarbageCollectionStart) \
74 fn(GarbageCollectionFinish, ArtJvmtiEvent::kGarbageCollectionFinish) \
75 fn(ObjectFree, ArtJvmtiEvent::kObjectFree) \
76 fn(VMObjectAlloc, ArtJvmtiEvent::kVmObjectAlloc)
77
78template <ArtJvmtiEvent kEvent>
79struct EventFnType {
80};
81
82#define EVENT_FN_TYPE(name, enum_name) \
83template <> \
84struct EventFnType<enum_name> { \
85 using type = decltype(jvmtiEventCallbacks().name); \
86};
87
88FORALL_EVENT_TYPES(EVENT_FN_TYPE)
89
90#undef EVENT_FN_TYPE
91
92template <ArtJvmtiEvent kEvent>
93ALWAYS_INLINE inline typename EventFnType<kEvent>::type GetCallback(ArtJvmTiEnv* env);
94
95#define GET_CALLBACK(name, enum_name) \
96template <> \
97ALWAYS_INLINE inline EventFnType<enum_name>::type GetCallback<enum_name>( \
98 ArtJvmTiEnv* env) { \
99 if (env->event_callbacks == nullptr) { \
100 return nullptr; \
101 } \
102 return env->event_callbacks->name; \
Andreas Gampe77708d92016-10-07 11:48:21 -0700103}
104
Andreas Gampe983c1752017-01-23 19:46:56 -0800105FORALL_EVENT_TYPES(GET_CALLBACK)
Alex Light6ac57502017-01-19 15:05:06 -0800106
Andreas Gampe983c1752017-01-23 19:46:56 -0800107#undef GET_CALLBACK
108
109#undef FORALL_EVENT_TYPES
110
111} // namespace impl
112
113// C++ does not allow partial template function specialization. The dispatch for our separated
114// ClassFileLoadHook event types is the same, so use this helper for code deduplication.
Alex Light6ac57502017-01-19 15:05:06 -0800115// TODO Locking of some type!
Andreas Gampe983c1752017-01-23 19:46:56 -0800116template <ArtJvmtiEvent kEvent>
Alex Light6ac57502017-01-19 15:05:06 -0800117inline void EventHandler::DispatchClassFileLoadHookEvent(art::Thread* thread,
Alex Light6ac57502017-01-19 15:05:06 -0800118 JNIEnv* jnienv,
119 jclass class_being_redefined,
120 jobject loader,
121 const char* name,
122 jobject protection_domain,
123 jint class_data_len,
124 const unsigned char* class_data,
125 jint* new_class_data_len,
126 unsigned char** new_class_data) const {
Andreas Gampe983c1752017-01-23 19:46:56 -0800127 static_assert(kEvent == ArtJvmtiEvent::kClassFileLoadHookRetransformable ||
128 kEvent == ArtJvmtiEvent::kClassFileLoadHookNonRetransformable, "Unsupported event");
Alex Light51271782017-03-24 17:28:30 -0700129 DCHECK(*new_class_data == nullptr);
Alex Light6ac57502017-01-19 15:05:06 -0800130 jint current_len = class_data_len;
131 unsigned char* current_class_data = const_cast<unsigned char*>(class_data);
132 ArtJvmTiEnv* last_env = nullptr;
133 for (ArtJvmTiEnv* env : envs) {
Alex Lightbb766462017-04-12 16:13:33 -0700134 if (env == nullptr) {
135 continue;
136 }
Andreas Gampe983c1752017-01-23 19:46:56 -0800137 if (ShouldDispatch<kEvent>(env, thread)) {
Alex Light440b5d92017-01-24 15:32:25 -0800138 jint new_len = 0;
139 unsigned char* new_data = nullptr;
Andreas Gampe983c1752017-01-23 19:46:56 -0800140 auto callback = impl::GetCallback<kEvent>(env);
Alex Light6ac57502017-01-19 15:05:06 -0800141 callback(env,
142 jnienv,
143 class_being_redefined,
144 loader,
145 name,
146 protection_domain,
147 current_len,
148 current_class_data,
149 &new_len,
150 &new_data);
151 if (new_data != nullptr && new_data != current_class_data) {
152 // Destroy the data the last transformer made. We skip this if the previous state was the
153 // initial one since we don't know here which jvmtiEnv allocated it.
154 // NB Currently this doesn't matter since all allocations just go to malloc but in the
155 // future we might have jvmtiEnv's keep track of their allocations for leak-checking.
156 if (last_env != nullptr) {
157 last_env->Deallocate(current_class_data);
158 }
159 last_env = env;
160 current_class_data = new_data;
161 current_len = new_len;
162 }
163 }
164 }
165 if (last_env != nullptr) {
166 *new_class_data_len = current_len;
167 *new_class_data = current_class_data;
168 }
169}
170
Andreas Gampe983c1752017-01-23 19:46:56 -0800171// Our goal for DispatchEvent: Do not allow implicit type conversion. Types of ...args must match
172// exactly the argument types of the corresponding Jvmti kEvent function pointer.
Alex Light6ac57502017-01-19 15:05:06 -0800173
Andreas Gampe983c1752017-01-23 19:46:56 -0800174template <ArtJvmtiEvent kEvent, typename ...Args>
Andreas Gampea1705ea2017-03-28 20:12:13 -0700175inline void EventHandler::DispatchEvent(art::Thread* thread, Args... args) const {
Andreas Gampe77708d92016-10-07 11:48:21 -0700176 for (ArtJvmTiEnv* env : envs) {
Alex Lightbb766462017-04-12 16:13:33 -0700177 if (env != nullptr) {
178 DispatchEvent<kEvent, Args...>(env, thread, args...);
179 }
Andreas Gampea1705ea2017-03-28 20:12:13 -0700180 }
181}
182
183template <ArtJvmtiEvent kEvent, typename ...Args>
184inline void EventHandler::DispatchEvent(ArtJvmTiEnv* env, art::Thread* thread, Args... args) const {
185 using FnType = void(jvmtiEnv*, Args...);
186 if (ShouldDispatch<kEvent>(env, thread)) {
187 FnType* callback = impl::GetCallback<kEvent>(env);
188 if (callback != nullptr) {
189 (*callback)(env, args...);
Andreas Gampe77708d92016-10-07 11:48:21 -0700190 }
191 }
192}
193
Andreas Gampe983c1752017-01-23 19:46:56 -0800194// C++ does not allow partial template function specialization. The dispatch for our separated
195// ClassFileLoadHook event types is the same, and in the DispatchClassFileLoadHookEvent helper.
196// The following two DispatchEvent specializations dispatch to it.
197template <>
198inline void EventHandler::DispatchEvent<ArtJvmtiEvent::kClassFileLoadHookRetransformable>(
199 art::Thread* thread,
200 JNIEnv* jnienv,
201 jclass class_being_redefined,
202 jobject loader,
203 const char* name,
204 jobject protection_domain,
205 jint class_data_len,
206 const unsigned char* class_data,
207 jint* new_class_data_len,
208 unsigned char** new_class_data) const {
209 return DispatchClassFileLoadHookEvent<ArtJvmtiEvent::kClassFileLoadHookRetransformable>(
210 thread,
211 jnienv,
212 class_being_redefined,
213 loader,
214 name,
215 protection_domain,
216 class_data_len,
217 class_data,
218 new_class_data_len,
219 new_class_data);
220}
221template <>
222inline void EventHandler::DispatchEvent<ArtJvmtiEvent::kClassFileLoadHookNonRetransformable>(
223 art::Thread* thread,
224 JNIEnv* jnienv,
225 jclass class_being_redefined,
226 jobject loader,
227 const char* name,
228 jobject protection_domain,
229 jint class_data_len,
230 const unsigned char* class_data,
231 jint* new_class_data_len,
232 unsigned char** new_class_data) const {
233 return DispatchClassFileLoadHookEvent<ArtJvmtiEvent::kClassFileLoadHookNonRetransformable>(
234 thread,
235 jnienv,
236 class_being_redefined,
237 loader,
238 name,
239 protection_domain,
240 class_data_len,
241 class_data,
242 new_class_data_len,
243 new_class_data);
244}
Alex Light40d87f42017-01-18 10:27:06 -0800245
Andreas Gampe983c1752017-01-23 19:46:56 -0800246template <ArtJvmtiEvent kEvent>
247inline bool EventHandler::ShouldDispatch(ArtJvmTiEnv* env,
248 art::Thread* thread) {
249 bool dispatch = env->event_masks.global_event_mask.Test(kEvent);
250
251 if (!dispatch && thread != nullptr && env->event_masks.unioned_thread_event_mask.Test(kEvent)) {
Alex Light40d87f42017-01-18 10:27:06 -0800252 EventMask* mask = env->event_masks.GetEventMaskOrNull(thread);
Andreas Gampe983c1752017-01-23 19:46:56 -0800253 dispatch = mask != nullptr && mask->Test(kEvent);
Alex Light40d87f42017-01-18 10:27:06 -0800254 }
255 return dispatch;
256}
257
Alex Light73afd322017-01-18 11:17:47 -0800258inline void EventHandler::RecalculateGlobalEventMask(ArtJvmtiEvent event) {
259 bool union_value = false;
260 for (const ArtJvmTiEnv* stored_env : envs) {
Alex Lightbb766462017-04-12 16:13:33 -0700261 if (stored_env == nullptr) {
262 continue;
263 }
Alex Light73afd322017-01-18 11:17:47 -0800264 union_value |= stored_env->event_masks.global_event_mask.Test(event);
265 union_value |= stored_env->event_masks.unioned_thread_event_mask.Test(event);
266 if (union_value) {
267 break;
268 }
269 }
270 global_mask.Set(event, union_value);
271}
272
273inline bool EventHandler::NeedsEventUpdate(ArtJvmTiEnv* env,
274 const jvmtiCapabilities& caps,
275 bool added) {
276 ArtJvmtiEvent event = added ? ArtJvmtiEvent::kClassFileLoadHookNonRetransformable
277 : ArtJvmtiEvent::kClassFileLoadHookRetransformable;
278 return caps.can_retransform_classes == 1 &&
279 IsEventEnabledAnywhere(event) &&
280 env->event_masks.IsEnabledAnywhere(event);
281}
282
283inline void EventHandler::HandleChangedCapabilities(ArtJvmTiEnv* env,
284 const jvmtiCapabilities& caps,
285 bool added) {
286 if (UNLIKELY(NeedsEventUpdate(env, caps, added))) {
287 env->event_masks.HandleChangedCapabilities(caps, added);
288 if (caps.can_retransform_classes == 1) {
289 RecalculateGlobalEventMask(ArtJvmtiEvent::kClassFileLoadHookRetransformable);
290 RecalculateGlobalEventMask(ArtJvmtiEvent::kClassFileLoadHookNonRetransformable);
291 }
292 }
293}
294
Andreas Gampe77708d92016-10-07 11:48:21 -0700295} // namespace openjdkjvmti
296
297#endif // ART_RUNTIME_OPENJDKJVMTI_EVENTS_INL_H_