blob: 655a53adb3342232b4f9f9cf05541d6a1f239b8a [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 Light6ac57502017-01-19 15:05:06 -0800129 jint current_len = class_data_len;
130 unsigned char* current_class_data = const_cast<unsigned char*>(class_data);
131 ArtJvmTiEnv* last_env = nullptr;
132 for (ArtJvmTiEnv* env : envs) {
Andreas Gampe983c1752017-01-23 19:46:56 -0800133 if (ShouldDispatch<kEvent>(env, thread)) {
Alex Light6ac57502017-01-19 15:05:06 -0800134 jint new_len;
135 unsigned char* new_data;
Andreas Gampe983c1752017-01-23 19:46:56 -0800136 auto callback = impl::GetCallback<kEvent>(env);
Alex Light6ac57502017-01-19 15:05:06 -0800137 callback(env,
138 jnienv,
139 class_being_redefined,
140 loader,
141 name,
142 protection_domain,
143 current_len,
144 current_class_data,
145 &new_len,
146 &new_data);
147 if (new_data != nullptr && new_data != current_class_data) {
148 // Destroy the data the last transformer made. We skip this if the previous state was the
149 // initial one since we don't know here which jvmtiEnv allocated it.
150 // NB Currently this doesn't matter since all allocations just go to malloc but in the
151 // future we might have jvmtiEnv's keep track of their allocations for leak-checking.
152 if (last_env != nullptr) {
153 last_env->Deallocate(current_class_data);
154 }
155 last_env = env;
156 current_class_data = new_data;
157 current_len = new_len;
158 }
159 }
160 }
161 if (last_env != nullptr) {
162 *new_class_data_len = current_len;
163 *new_class_data = current_class_data;
164 }
165}
166
Andreas Gampe983c1752017-01-23 19:46:56 -0800167// Our goal for DispatchEvent: Do not allow implicit type conversion. Types of ...args must match
168// exactly the argument types of the corresponding Jvmti kEvent function pointer.
Alex Light6ac57502017-01-19 15:05:06 -0800169
Andreas Gampe983c1752017-01-23 19:46:56 -0800170template <ArtJvmtiEvent kEvent, typename ...Args>
171inline void EventHandler::DispatchEvent(art::Thread* thread,
172 Args... args) const {
Andreas Gampe77708d92016-10-07 11:48:21 -0700173 using FnType = void(jvmtiEnv*, Args...);
174 for (ArtJvmTiEnv* env : envs) {
Andreas Gampe983c1752017-01-23 19:46:56 -0800175 if (ShouldDispatch<kEvent>(env, thread)) {
176 FnType* callback = impl::GetCallback<kEvent>(env);
Andreas Gampe77708d92016-10-07 11:48:21 -0700177 if (callback != nullptr) {
178 (*callback)(env, args...);
179 }
180 }
181 }
182}
183
Andreas Gampe983c1752017-01-23 19:46:56 -0800184// C++ does not allow partial template function specialization. The dispatch for our separated
185// ClassFileLoadHook event types is the same, and in the DispatchClassFileLoadHookEvent helper.
186// The following two DispatchEvent specializations dispatch to it.
187template <>
188inline void EventHandler::DispatchEvent<ArtJvmtiEvent::kClassFileLoadHookRetransformable>(
189 art::Thread* thread,
190 JNIEnv* jnienv,
191 jclass class_being_redefined,
192 jobject loader,
193 const char* name,
194 jobject protection_domain,
195 jint class_data_len,
196 const unsigned char* class_data,
197 jint* new_class_data_len,
198 unsigned char** new_class_data) const {
199 return DispatchClassFileLoadHookEvent<ArtJvmtiEvent::kClassFileLoadHookRetransformable>(
200 thread,
201 jnienv,
202 class_being_redefined,
203 loader,
204 name,
205 protection_domain,
206 class_data_len,
207 class_data,
208 new_class_data_len,
209 new_class_data);
210}
211template <>
212inline void EventHandler::DispatchEvent<ArtJvmtiEvent::kClassFileLoadHookNonRetransformable>(
213 art::Thread* thread,
214 JNIEnv* jnienv,
215 jclass class_being_redefined,
216 jobject loader,
217 const char* name,
218 jobject protection_domain,
219 jint class_data_len,
220 const unsigned char* class_data,
221 jint* new_class_data_len,
222 unsigned char** new_class_data) const {
223 return DispatchClassFileLoadHookEvent<ArtJvmtiEvent::kClassFileLoadHookNonRetransformable>(
224 thread,
225 jnienv,
226 class_being_redefined,
227 loader,
228 name,
229 protection_domain,
230 class_data_len,
231 class_data,
232 new_class_data_len,
233 new_class_data);
234}
Alex Light40d87f42017-01-18 10:27:06 -0800235
Andreas Gampe983c1752017-01-23 19:46:56 -0800236template <ArtJvmtiEvent kEvent>
237inline bool EventHandler::ShouldDispatch(ArtJvmTiEnv* env,
238 art::Thread* thread) {
239 bool dispatch = env->event_masks.global_event_mask.Test(kEvent);
240
241 if (!dispatch && thread != nullptr && env->event_masks.unioned_thread_event_mask.Test(kEvent)) {
Alex Light40d87f42017-01-18 10:27:06 -0800242 EventMask* mask = env->event_masks.GetEventMaskOrNull(thread);
Andreas Gampe983c1752017-01-23 19:46:56 -0800243 dispatch = mask != nullptr && mask->Test(kEvent);
Alex Light40d87f42017-01-18 10:27:06 -0800244 }
245 return dispatch;
246}
247
Alex Light73afd322017-01-18 11:17:47 -0800248inline void EventHandler::RecalculateGlobalEventMask(ArtJvmtiEvent event) {
249 bool union_value = false;
250 for (const ArtJvmTiEnv* stored_env : envs) {
251 union_value |= stored_env->event_masks.global_event_mask.Test(event);
252 union_value |= stored_env->event_masks.unioned_thread_event_mask.Test(event);
253 if (union_value) {
254 break;
255 }
256 }
257 global_mask.Set(event, union_value);
258}
259
260inline bool EventHandler::NeedsEventUpdate(ArtJvmTiEnv* env,
261 const jvmtiCapabilities& caps,
262 bool added) {
263 ArtJvmtiEvent event = added ? ArtJvmtiEvent::kClassFileLoadHookNonRetransformable
264 : ArtJvmtiEvent::kClassFileLoadHookRetransformable;
265 return caps.can_retransform_classes == 1 &&
266 IsEventEnabledAnywhere(event) &&
267 env->event_masks.IsEnabledAnywhere(event);
268}
269
270inline void EventHandler::HandleChangedCapabilities(ArtJvmTiEnv* env,
271 const jvmtiCapabilities& caps,
272 bool added) {
273 if (UNLIKELY(NeedsEventUpdate(env, caps, added))) {
274 env->event_masks.HandleChangedCapabilities(caps, added);
275 if (caps.can_retransform_classes == 1) {
276 RecalculateGlobalEventMask(ArtJvmtiEvent::kClassFileLoadHookRetransformable);
277 RecalculateGlobalEventMask(ArtJvmtiEvent::kClassFileLoadHookNonRetransformable);
278 }
279 }
280}
281
Andreas Gampe77708d92016-10-07 11:48:21 -0700282} // namespace openjdkjvmti
283
284#endif // ART_RUNTIME_OPENJDKJVMTI_EVENTS_INL_H_