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