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 | |
Andreas Gampe | 06c42a5 | 2017-07-26 14:17:14 -0700 | [diff] [blame] | 17 | #ifndef ART_OPENJDKJVMTI_EVENTS_INL_H_ |
| 18 | #define ART_OPENJDKJVMTI_EVENTS_INL_H_ |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 19 | |
Alex Light | 73afd32 | 2017-01-18 11:17:47 -0800 | [diff] [blame] | 20 | #include <array> |
Alex Light | 9df79b7 | 2017-09-12 08:57:31 -0700 | [diff] [blame] | 21 | #include <type_traits> |
| 22 | #include <tuple> |
Alex Light | 73afd32 | 2017-01-18 11:17:47 -0800 | [diff] [blame] | 23 | |
Alex Light | b6106d5 | 2017-10-18 15:02:15 -0700 | [diff] [blame] | 24 | #include "base/mutex-inl.h" |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 25 | #include "events.h" |
Alex Light | 084fa37 | 2017-06-16 08:58:34 -0700 | [diff] [blame] | 26 | #include "jni_internal.h" |
Andreas Gampe | 373a9b5 | 2017-10-18 09:01:57 -0700 | [diff] [blame] | 27 | #include "nativehelper/scoped_local_ref.h" |
Alex Light | 9df79b7 | 2017-09-12 08:57:31 -0700 | [diff] [blame] | 28 | #include "scoped_thread_state_change-inl.h" |
Alex Light | a26e349 | 2017-06-27 17:55:37 -0700 | [diff] [blame] | 29 | #include "ti_breakpoint.h" |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 30 | |
| 31 | #include "art_jvmti.h" |
| 32 | |
| 33 | namespace openjdkjvmti { |
| 34 | |
Alex Light | 73afd32 | 2017-01-18 11:17:47 -0800 | [diff] [blame] | 35 | static inline ArtJvmtiEvent GetArtJvmtiEvent(ArtJvmTiEnv* env, jvmtiEvent e) { |
| 36 | if (UNLIKELY(e == JVMTI_EVENT_CLASS_FILE_LOAD_HOOK)) { |
| 37 | if (env->capabilities.can_retransform_classes) { |
| 38 | return ArtJvmtiEvent::kClassFileLoadHookRetransformable; |
| 39 | } else { |
| 40 | return ArtJvmtiEvent::kClassFileLoadHookNonRetransformable; |
| 41 | } |
| 42 | } else { |
| 43 | return static_cast<ArtJvmtiEvent>(e); |
| 44 | } |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 45 | } |
| 46 | |
Andreas Gampe | 983c175 | 2017-01-23 19:46:56 -0800 | [diff] [blame] | 47 | namespace impl { |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 48 | |
Andreas Gampe | 983c175 | 2017-01-23 19:46:56 -0800 | [diff] [blame] | 49 | // Infrastructure to achieve type safety for event dispatch. |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 50 | |
Andreas Gampe | 983c175 | 2017-01-23 19:46:56 -0800 | [diff] [blame] | 51 | #define FORALL_EVENT_TYPES(fn) \ |
| 52 | fn(VMInit, ArtJvmtiEvent::kVmInit) \ |
| 53 | fn(VMDeath, ArtJvmtiEvent::kVmDeath) \ |
| 54 | fn(ThreadStart, ArtJvmtiEvent::kThreadStart) \ |
| 55 | fn(ThreadEnd, ArtJvmtiEvent::kThreadEnd) \ |
| 56 | fn(ClassFileLoadHook, ArtJvmtiEvent::kClassFileLoadHookRetransformable) \ |
| 57 | fn(ClassFileLoadHook, ArtJvmtiEvent::kClassFileLoadHookNonRetransformable) \ |
| 58 | fn(ClassLoad, ArtJvmtiEvent::kClassLoad) \ |
| 59 | fn(ClassPrepare, ArtJvmtiEvent::kClassPrepare) \ |
| 60 | fn(VMStart, ArtJvmtiEvent::kVmStart) \ |
| 61 | fn(Exception, ArtJvmtiEvent::kException) \ |
| 62 | fn(ExceptionCatch, ArtJvmtiEvent::kExceptionCatch) \ |
| 63 | fn(SingleStep, ArtJvmtiEvent::kSingleStep) \ |
| 64 | fn(FramePop, ArtJvmtiEvent::kFramePop) \ |
| 65 | fn(Breakpoint, ArtJvmtiEvent::kBreakpoint) \ |
| 66 | fn(FieldAccess, ArtJvmtiEvent::kFieldAccess) \ |
| 67 | fn(FieldModification, ArtJvmtiEvent::kFieldModification) \ |
| 68 | fn(MethodEntry, ArtJvmtiEvent::kMethodEntry) \ |
| 69 | fn(MethodExit, ArtJvmtiEvent::kMethodExit) \ |
| 70 | fn(NativeMethodBind, ArtJvmtiEvent::kNativeMethodBind) \ |
| 71 | fn(CompiledMethodLoad, ArtJvmtiEvent::kCompiledMethodLoad) \ |
| 72 | fn(CompiledMethodUnload, ArtJvmtiEvent::kCompiledMethodUnload) \ |
| 73 | fn(DynamicCodeGenerated, ArtJvmtiEvent::kDynamicCodeGenerated) \ |
| 74 | fn(DataDumpRequest, ArtJvmtiEvent::kDataDumpRequest) \ |
| 75 | fn(MonitorWait, ArtJvmtiEvent::kMonitorWait) \ |
| 76 | fn(MonitorWaited, ArtJvmtiEvent::kMonitorWaited) \ |
| 77 | fn(MonitorContendedEnter, ArtJvmtiEvent::kMonitorContendedEnter) \ |
| 78 | fn(MonitorContendedEntered, ArtJvmtiEvent::kMonitorContendedEntered) \ |
| 79 | fn(ResourceExhausted, ArtJvmtiEvent::kResourceExhausted) \ |
| 80 | fn(GarbageCollectionStart, ArtJvmtiEvent::kGarbageCollectionStart) \ |
| 81 | fn(GarbageCollectionFinish, ArtJvmtiEvent::kGarbageCollectionFinish) \ |
| 82 | fn(ObjectFree, ArtJvmtiEvent::kObjectFree) \ |
Alex Light | 8c2b929 | 2017-11-09 13:21:01 -0800 | [diff] [blame^] | 83 | fn(VMObjectAlloc, ArtJvmtiEvent::kVmObjectAlloc) \ |
| 84 | fn(DdmPublishChunk, ArtJvmtiEvent::kDdmPublishChunk) |
Andreas Gampe | 983c175 | 2017-01-23 19:46:56 -0800 | [diff] [blame] | 85 | |
| 86 | template <ArtJvmtiEvent kEvent> |
| 87 | struct EventFnType { |
| 88 | }; |
| 89 | |
Alex Light | 8c2b929 | 2017-11-09 13:21:01 -0800 | [diff] [blame^] | 90 | #define EVENT_FN_TYPE(name, enum_name) \ |
| 91 | template <> \ |
| 92 | struct EventFnType<enum_name> { \ |
| 93 | using type = decltype(ArtJvmtiEventCallbacks().name); \ |
Andreas Gampe | 983c175 | 2017-01-23 19:46:56 -0800 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | FORALL_EVENT_TYPES(EVENT_FN_TYPE) |
| 97 | |
| 98 | #undef EVENT_FN_TYPE |
| 99 | |
| 100 | template <ArtJvmtiEvent kEvent> |
| 101 | ALWAYS_INLINE inline typename EventFnType<kEvent>::type GetCallback(ArtJvmTiEnv* env); |
| 102 | |
| 103 | #define GET_CALLBACK(name, enum_name) \ |
| 104 | template <> \ |
| 105 | ALWAYS_INLINE inline EventFnType<enum_name>::type GetCallback<enum_name>( \ |
| 106 | ArtJvmTiEnv* env) { \ |
| 107 | if (env->event_callbacks == nullptr) { \ |
| 108 | return nullptr; \ |
| 109 | } \ |
| 110 | return env->event_callbacks->name; \ |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Andreas Gampe | 983c175 | 2017-01-23 19:46:56 -0800 | [diff] [blame] | 113 | FORALL_EVENT_TYPES(GET_CALLBACK) |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 114 | |
Andreas Gampe | 983c175 | 2017-01-23 19:46:56 -0800 | [diff] [blame] | 115 | #undef GET_CALLBACK |
| 116 | |
| 117 | #undef FORALL_EVENT_TYPES |
| 118 | |
| 119 | } // namespace impl |
| 120 | |
| 121 | // C++ does not allow partial template function specialization. The dispatch for our separated |
| 122 | // ClassFileLoadHook event types is the same, so use this helper for code deduplication. |
Andreas Gampe | 983c175 | 2017-01-23 19:46:56 -0800 | [diff] [blame] | 123 | template <ArtJvmtiEvent kEvent> |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 124 | inline void EventHandler::DispatchClassFileLoadHookEvent(art::Thread* thread, |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 125 | JNIEnv* jnienv, |
| 126 | jclass class_being_redefined, |
| 127 | jobject loader, |
| 128 | const char* name, |
| 129 | jobject protection_domain, |
| 130 | jint class_data_len, |
| 131 | const unsigned char* class_data, |
| 132 | jint* new_class_data_len, |
| 133 | unsigned char** new_class_data) const { |
Andreas Gampe | 983c175 | 2017-01-23 19:46:56 -0800 | [diff] [blame] | 134 | static_assert(kEvent == ArtJvmtiEvent::kClassFileLoadHookRetransformable || |
| 135 | kEvent == ArtJvmtiEvent::kClassFileLoadHookNonRetransformable, "Unsupported event"); |
Alex Light | 5127178 | 2017-03-24 17:28:30 -0700 | [diff] [blame] | 136 | DCHECK(*new_class_data == nullptr); |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 137 | jint current_len = class_data_len; |
| 138 | unsigned char* current_class_data = const_cast<unsigned char*>(class_data); |
| 139 | ArtJvmTiEnv* last_env = nullptr; |
| 140 | for (ArtJvmTiEnv* env : envs) { |
Alex Light | bb76646 | 2017-04-12 16:13:33 -0700 | [diff] [blame] | 141 | if (env == nullptr) { |
| 142 | continue; |
| 143 | } |
Alex Light | 9df79b7 | 2017-09-12 08:57:31 -0700 | [diff] [blame] | 144 | jint new_len = 0; |
| 145 | unsigned char* new_data = nullptr; |
| 146 | DispatchEventOnEnv<kEvent>(env, |
| 147 | thread, |
| 148 | jnienv, |
| 149 | class_being_redefined, |
| 150 | loader, |
| 151 | name, |
| 152 | protection_domain, |
| 153 | current_len, |
| 154 | static_cast<const unsigned char*>(current_class_data), |
| 155 | &new_len, |
| 156 | &new_data); |
| 157 | if (new_data != nullptr && new_data != current_class_data) { |
| 158 | // Destroy the data the last transformer made. We skip this if the previous state was the |
| 159 | // initial one since we don't know here which jvmtiEnv allocated it. |
| 160 | // NB Currently this doesn't matter since all allocations just go to malloc but in the |
| 161 | // future we might have jvmtiEnv's keep track of their allocations for leak-checking. |
| 162 | if (last_env != nullptr) { |
| 163 | last_env->Deallocate(current_class_data); |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 164 | } |
Alex Light | 9df79b7 | 2017-09-12 08:57:31 -0700 | [diff] [blame] | 165 | last_env = env; |
| 166 | current_class_data = new_data; |
| 167 | current_len = new_len; |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | if (last_env != nullptr) { |
| 171 | *new_class_data_len = current_len; |
| 172 | *new_class_data = current_class_data; |
| 173 | } |
| 174 | } |
| 175 | |
Andreas Gampe | 983c175 | 2017-01-23 19:46:56 -0800 | [diff] [blame] | 176 | // Our goal for DispatchEvent: Do not allow implicit type conversion. Types of ...args must match |
| 177 | // exactly the argument types of the corresponding Jvmti kEvent function pointer. |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 178 | |
Andreas Gampe | 983c175 | 2017-01-23 19:46:56 -0800 | [diff] [blame] | 179 | template <ArtJvmtiEvent kEvent, typename ...Args> |
Alex Light | 9df79b7 | 2017-09-12 08:57:31 -0700 | [diff] [blame] | 180 | inline void EventHandler::ExecuteCallback(ArtJvmTiEnv* env, Args... args) { |
| 181 | using FnType = typename impl::EventFnType<kEvent>::type; |
| 182 | FnType callback = impl::GetCallback<kEvent>(env); |
| 183 | if (callback != nullptr) { |
| 184 | (*callback)(env, args...); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | template <ArtJvmtiEvent kEvent, typename ...Args> |
Andreas Gampe | a1705ea | 2017-03-28 20:12:13 -0700 | [diff] [blame] | 189 | inline void EventHandler::DispatchEvent(art::Thread* thread, Args... args) const { |
Alex Light | 9df79b7 | 2017-09-12 08:57:31 -0700 | [diff] [blame] | 190 | static_assert(!std::is_same<JNIEnv*, |
| 191 | typename std::decay_t< |
| 192 | std::tuple_element_t<0, std::tuple<Args..., nullptr_t>>>>::value, |
| 193 | "Should be calling DispatchEvent with explicit JNIEnv* argument!"); |
| 194 | DCHECK(thread == nullptr || !thread->IsExceptionPending()); |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 195 | for (ArtJvmTiEnv* env : envs) { |
Alex Light | bb76646 | 2017-04-12 16:13:33 -0700 | [diff] [blame] | 196 | if (env != nullptr) { |
Alex Light | 9df79b7 | 2017-09-12 08:57:31 -0700 | [diff] [blame] | 197 | DispatchEventOnEnv<kEvent, Args...>(env, thread, args...); |
Alex Light | bb76646 | 2017-04-12 16:13:33 -0700 | [diff] [blame] | 198 | } |
Andreas Gampe | a1705ea | 2017-03-28 20:12:13 -0700 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | |
Alex Light | 9df79b7 | 2017-09-12 08:57:31 -0700 | [diff] [blame] | 202 | // Helper for ensuring that the dispatch environment is sane. Events with JNIEnvs need to stash |
| 203 | // pending exceptions since they can cause new ones to be thrown. In accordance with the JVMTI |
| 204 | // specification we allow exceptions originating from events to overwrite the current exception, |
| 205 | // including exceptions originating from earlier events. |
| 206 | class ScopedEventDispatchEnvironment FINAL : public art::ValueObject { |
| 207 | public: |
| 208 | explicit ScopedEventDispatchEnvironment(JNIEnv* env) |
| 209 | : env_(env), |
| 210 | thr_(env_, env_->ExceptionOccurred()), |
| 211 | suspend_(art::Thread::Current(), art::kNative) { |
| 212 | // The spec doesn't say how much local data should be there, so we just give 128 which seems |
| 213 | // likely to be enough for most cases. |
| 214 | env_->PushLocalFrame(128); |
| 215 | env_->ExceptionClear(); |
| 216 | UNUSED(suspend_); |
| 217 | } |
| 218 | |
| 219 | ~ScopedEventDispatchEnvironment() { |
| 220 | if (thr_.get() != nullptr && !env_->ExceptionCheck()) { |
| 221 | // TODO It would be nice to add the overwritten exceptions to the suppressed exceptions list |
| 222 | // of the newest exception. |
| 223 | env_->Throw(thr_.get()); |
| 224 | } |
| 225 | env_->PopLocalFrame(nullptr); |
| 226 | } |
| 227 | |
| 228 | private: |
| 229 | JNIEnv* env_; |
| 230 | ScopedLocalRef<jthrowable> thr_; |
| 231 | // Not actually unused. The destructor/constructor does important work. |
| 232 | art::ScopedThreadStateChange suspend_; |
| 233 | |
| 234 | DISALLOW_COPY_AND_ASSIGN(ScopedEventDispatchEnvironment); |
| 235 | }; |
| 236 | |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 237 | template <ArtJvmtiEvent kEvent, typename ...Args> |
| 238 | inline void EventHandler::DispatchEvent(art::Thread* thread, JNIEnv* jnienv, Args... args) const { |
| 239 | for (ArtJvmTiEnv* env : envs) { |
| 240 | if (env != nullptr) { |
Alex Light | 9df79b7 | 2017-09-12 08:57:31 -0700 | [diff] [blame] | 241 | DispatchEventOnEnv<kEvent, Args...>(env, thread, jnienv, args...); |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 242 | } |
| 243 | } |
| 244 | } |
| 245 | |
Andreas Gampe | a1705ea | 2017-03-28 20:12:13 -0700 | [diff] [blame] | 246 | template <ArtJvmtiEvent kEvent, typename ...Args> |
Alex Light | 9df79b7 | 2017-09-12 08:57:31 -0700 | [diff] [blame] | 247 | inline void EventHandler::DispatchEventOnEnv( |
| 248 | ArtJvmTiEnv* env, art::Thread* thread, JNIEnv* jnienv, Args... args) const { |
| 249 | DCHECK(env != nullptr); |
| 250 | if (ShouldDispatch<kEvent, JNIEnv*, Args...>(env, thread, jnienv, args...)) { |
| 251 | ScopedEventDispatchEnvironment sede(jnienv); |
| 252 | ExecuteCallback<kEvent, JNIEnv*, Args...>(env, jnienv, args...); |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 253 | } |
| 254 | } |
| 255 | |
Alex Light | 9df79b7 | 2017-09-12 08:57:31 -0700 | [diff] [blame] | 256 | template <ArtJvmtiEvent kEvent, typename ...Args> |
| 257 | inline void EventHandler::DispatchEventOnEnv( |
| 258 | ArtJvmTiEnv* env, art::Thread* thread, Args... args) const { |
| 259 | static_assert(!std::is_same<JNIEnv*, |
| 260 | typename std::decay_t< |
| 261 | std::tuple_element_t<0, std::tuple<Args..., nullptr_t>>>>::value, |
| 262 | "Should be calling DispatchEventOnEnv with explicit JNIEnv* argument!"); |
| 263 | if (ShouldDispatch<kEvent>(env, thread, args...)) { |
| 264 | ExecuteCallback<kEvent, Args...>(env, args...); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | // Events that need custom logic for if we send the event but are otherwise normal. This includes |
| 269 | // the kBreakpoint, kFramePop, kFieldAccess, and kFieldModification events. |
| 270 | |
Alex Light | a26e349 | 2017-06-27 17:55:37 -0700 | [diff] [blame] | 271 | // Need to give custom specializations for Breakpoint since it needs to filter out which particular |
| 272 | // methods/dex_pcs agents get notified on. |
| 273 | template <> |
Alex Light | 9df79b7 | 2017-09-12 08:57:31 -0700 | [diff] [blame] | 274 | inline bool EventHandler::ShouldDispatch<ArtJvmtiEvent::kBreakpoint>( |
| 275 | ArtJvmTiEnv* env, |
| 276 | art::Thread* thread, |
| 277 | JNIEnv* jnienv ATTRIBUTE_UNUSED, |
| 278 | jthread jni_thread ATTRIBUTE_UNUSED, |
| 279 | jmethodID jmethod, |
| 280 | jlocation location) const { |
Alex Light | b6106d5 | 2017-10-18 15:02:15 -0700 | [diff] [blame] | 281 | art::ReaderMutexLock lk(art::Thread::Current(), env->event_info_mutex_); |
Alex Light | a26e349 | 2017-06-27 17:55:37 -0700 | [diff] [blame] | 282 | art::ArtMethod* method = art::jni::DecodeArtMethod(jmethod); |
Alex Light | 9df79b7 | 2017-09-12 08:57:31 -0700 | [diff] [blame] | 283 | return ShouldDispatchOnThread<ArtJvmtiEvent::kBreakpoint>(env, thread) && |
| 284 | env->breakpoints.find({method, location}) != env->breakpoints.end(); |
Alex Light | a26e349 | 2017-06-27 17:55:37 -0700 | [diff] [blame] | 285 | } |
| 286 | |
Alex Light | e814f9d | 2017-07-31 16:14:39 -0700 | [diff] [blame] | 287 | template <> |
Alex Light | 9df79b7 | 2017-09-12 08:57:31 -0700 | [diff] [blame] | 288 | inline bool EventHandler::ShouldDispatch<ArtJvmtiEvent::kFramePop>( |
| 289 | ArtJvmTiEnv* env, |
Alex Light | e814f9d | 2017-07-31 16:14:39 -0700 | [diff] [blame] | 290 | art::Thread* thread, |
Alex Light | 9df79b7 | 2017-09-12 08:57:31 -0700 | [diff] [blame] | 291 | JNIEnv* jnienv ATTRIBUTE_UNUSED, |
| 292 | jthread jni_thread ATTRIBUTE_UNUSED, |
| 293 | jmethodID jmethod ATTRIBUTE_UNUSED, |
| 294 | jboolean is_exception ATTRIBUTE_UNUSED, |
Alex Light | e814f9d | 2017-07-31 16:14:39 -0700 | [diff] [blame] | 295 | const art::ShadowFrame* frame) const { |
Alex Light | 9df79b7 | 2017-09-12 08:57:31 -0700 | [diff] [blame] | 296 | // Search for the frame. Do this before checking if we need to send the event so that we don't |
| 297 | // have to deal with use-after-free or the frames being reallocated later. |
Alex Light | b6106d5 | 2017-10-18 15:02:15 -0700 | [diff] [blame] | 298 | art::WriterMutexLock lk(art::Thread::Current(), env->event_info_mutex_); |
Alex Light | 9df79b7 | 2017-09-12 08:57:31 -0700 | [diff] [blame] | 299 | return env->notify_frames.erase(frame) != 0 && |
| 300 | ShouldDispatchOnThread<ArtJvmtiEvent::kFramePop>(env, thread); |
Alex Light | e814f9d | 2017-07-31 16:14:39 -0700 | [diff] [blame] | 301 | } |
| 302 | |
Alex Light | 084fa37 | 2017-06-16 08:58:34 -0700 | [diff] [blame] | 303 | // Need to give custom specializations for FieldAccess and FieldModification since they need to |
| 304 | // filter out which particular fields agents want to get notified on. |
| 305 | // TODO The spec allows us to do shortcuts like only allow one agent to ever set these watches. This |
| 306 | // could make the system more performant. |
| 307 | template <> |
Alex Light | 9df79b7 | 2017-09-12 08:57:31 -0700 | [diff] [blame] | 308 | inline bool EventHandler::ShouldDispatch<ArtJvmtiEvent::kFieldModification>( |
| 309 | ArtJvmTiEnv* env, |
| 310 | art::Thread* thread, |
| 311 | JNIEnv* jnienv ATTRIBUTE_UNUSED, |
| 312 | jthread jni_thread ATTRIBUTE_UNUSED, |
| 313 | jmethodID method ATTRIBUTE_UNUSED, |
| 314 | jlocation location ATTRIBUTE_UNUSED, |
| 315 | jclass field_klass ATTRIBUTE_UNUSED, |
| 316 | jobject object ATTRIBUTE_UNUSED, |
| 317 | jfieldID field, |
| 318 | char type_char ATTRIBUTE_UNUSED, |
| 319 | jvalue val ATTRIBUTE_UNUSED) const { |
Alex Light | b6106d5 | 2017-10-18 15:02:15 -0700 | [diff] [blame] | 320 | art::ReaderMutexLock lk(art::Thread::Current(), env->event_info_mutex_); |
Alex Light | 9df79b7 | 2017-09-12 08:57:31 -0700 | [diff] [blame] | 321 | return ShouldDispatchOnThread<ArtJvmtiEvent::kFieldModification>(env, thread) && |
| 322 | env->modify_watched_fields.find( |
| 323 | art::jni::DecodeArtField(field)) != env->modify_watched_fields.end(); |
Alex Light | 084fa37 | 2017-06-16 08:58:34 -0700 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | template <> |
Alex Light | 9df79b7 | 2017-09-12 08:57:31 -0700 | [diff] [blame] | 327 | inline bool EventHandler::ShouldDispatch<ArtJvmtiEvent::kFieldAccess>( |
| 328 | ArtJvmTiEnv* env, |
| 329 | art::Thread* thread, |
| 330 | JNIEnv* jnienv ATTRIBUTE_UNUSED, |
| 331 | jthread jni_thread ATTRIBUTE_UNUSED, |
| 332 | jmethodID method ATTRIBUTE_UNUSED, |
| 333 | jlocation location ATTRIBUTE_UNUSED, |
| 334 | jclass field_klass ATTRIBUTE_UNUSED, |
| 335 | jobject object ATTRIBUTE_UNUSED, |
| 336 | jfieldID field) const { |
Alex Light | b6106d5 | 2017-10-18 15:02:15 -0700 | [diff] [blame] | 337 | art::ReaderMutexLock lk(art::Thread::Current(), env->event_info_mutex_); |
Alex Light | 9df79b7 | 2017-09-12 08:57:31 -0700 | [diff] [blame] | 338 | return ShouldDispatchOnThread<ArtJvmtiEvent::kFieldAccess>(env, thread) && |
| 339 | env->access_watched_fields.find( |
| 340 | art::jni::DecodeArtField(field)) != env->access_watched_fields.end(); |
| 341 | } |
| 342 | |
| 343 | // Need to give custom specializations for FramePop since it needs to filter out which particular |
| 344 | // agents get the event. This specialization gets an extra argument so we can determine which (if |
| 345 | // any) environments have the frame pop. |
| 346 | // TODO It might be useful to use more template magic to have this only define ShouldDispatch or |
| 347 | // something. |
| 348 | template <> |
| 349 | inline void EventHandler::ExecuteCallback<ArtJvmtiEvent::kFramePop>( |
| 350 | ArtJvmTiEnv* env, |
| 351 | JNIEnv* jnienv, |
| 352 | jthread jni_thread, |
| 353 | jmethodID jmethod, |
| 354 | jboolean is_exception, |
| 355 | const art::ShadowFrame* frame ATTRIBUTE_UNUSED) { |
| 356 | ExecuteCallback<ArtJvmtiEvent::kFramePop>( |
| 357 | env, jnienv, jni_thread, jmethod, is_exception); |
Alex Light | 084fa37 | 2017-06-16 08:58:34 -0700 | [diff] [blame] | 358 | } |
| 359 | |
Alex Light | d78ddec | 2017-04-18 15:20:38 -0700 | [diff] [blame] | 360 | // Need to give a custom specialization for NativeMethodBind since it has to deal with an out |
| 361 | // variable. |
| 362 | template <> |
| 363 | inline void EventHandler::DispatchEvent<ArtJvmtiEvent::kNativeMethodBind>(art::Thread* thread, |
| 364 | JNIEnv* jnienv, |
| 365 | jthread jni_thread, |
| 366 | jmethodID method, |
| 367 | void* cur_method, |
| 368 | void** new_method) const { |
| 369 | *new_method = cur_method; |
| 370 | for (ArtJvmTiEnv* env : envs) { |
Alex Light | 9df79b7 | 2017-09-12 08:57:31 -0700 | [diff] [blame] | 371 | if (env != nullptr) { |
| 372 | *new_method = cur_method; |
| 373 | DispatchEventOnEnv<ArtJvmtiEvent::kNativeMethodBind>(env, |
| 374 | thread, |
| 375 | jnienv, |
| 376 | jni_thread, |
| 377 | method, |
| 378 | cur_method, |
| 379 | new_method); |
Alex Light | d78ddec | 2017-04-18 15:20:38 -0700 | [diff] [blame] | 380 | if (*new_method != nullptr) { |
| 381 | cur_method = *new_method; |
| 382 | } |
| 383 | } |
| 384 | } |
Alex Light | 9df79b7 | 2017-09-12 08:57:31 -0700 | [diff] [blame] | 385 | *new_method = cur_method; |
Alex Light | d78ddec | 2017-04-18 15:20:38 -0700 | [diff] [blame] | 386 | } |
| 387 | |
Andreas Gampe | 983c175 | 2017-01-23 19:46:56 -0800 | [diff] [blame] | 388 | // C++ does not allow partial template function specialization. The dispatch for our separated |
| 389 | // ClassFileLoadHook event types is the same, and in the DispatchClassFileLoadHookEvent helper. |
| 390 | // The following two DispatchEvent specializations dispatch to it. |
| 391 | template <> |
| 392 | inline void EventHandler::DispatchEvent<ArtJvmtiEvent::kClassFileLoadHookRetransformable>( |
| 393 | art::Thread* thread, |
| 394 | JNIEnv* jnienv, |
| 395 | jclass class_being_redefined, |
| 396 | jobject loader, |
| 397 | const char* name, |
| 398 | jobject protection_domain, |
| 399 | jint class_data_len, |
| 400 | const unsigned char* class_data, |
| 401 | jint* new_class_data_len, |
| 402 | unsigned char** new_class_data) const { |
| 403 | return DispatchClassFileLoadHookEvent<ArtJvmtiEvent::kClassFileLoadHookRetransformable>( |
| 404 | thread, |
| 405 | jnienv, |
| 406 | class_being_redefined, |
| 407 | loader, |
| 408 | name, |
| 409 | protection_domain, |
| 410 | class_data_len, |
| 411 | class_data, |
| 412 | new_class_data_len, |
| 413 | new_class_data); |
| 414 | } |
Alex Light | 9df79b7 | 2017-09-12 08:57:31 -0700 | [diff] [blame] | 415 | |
Andreas Gampe | 983c175 | 2017-01-23 19:46:56 -0800 | [diff] [blame] | 416 | template <> |
| 417 | inline void EventHandler::DispatchEvent<ArtJvmtiEvent::kClassFileLoadHookNonRetransformable>( |
| 418 | art::Thread* thread, |
| 419 | JNIEnv* jnienv, |
| 420 | jclass class_being_redefined, |
| 421 | jobject loader, |
| 422 | const char* name, |
| 423 | jobject protection_domain, |
| 424 | jint class_data_len, |
| 425 | const unsigned char* class_data, |
| 426 | jint* new_class_data_len, |
| 427 | unsigned char** new_class_data) const { |
| 428 | return DispatchClassFileLoadHookEvent<ArtJvmtiEvent::kClassFileLoadHookNonRetransformable>( |
| 429 | thread, |
| 430 | jnienv, |
| 431 | class_being_redefined, |
| 432 | loader, |
| 433 | name, |
| 434 | protection_domain, |
| 435 | class_data_len, |
| 436 | class_data, |
| 437 | new_class_data_len, |
| 438 | new_class_data); |
| 439 | } |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 440 | |
Andreas Gampe | 983c175 | 2017-01-23 19:46:56 -0800 | [diff] [blame] | 441 | template <ArtJvmtiEvent kEvent> |
Alex Light | 9df79b7 | 2017-09-12 08:57:31 -0700 | [diff] [blame] | 442 | inline bool EventHandler::ShouldDispatchOnThread(ArtJvmTiEnv* env, art::Thread* thread) { |
Andreas Gampe | 983c175 | 2017-01-23 19:46:56 -0800 | [diff] [blame] | 443 | bool dispatch = env->event_masks.global_event_mask.Test(kEvent); |
| 444 | |
| 445 | if (!dispatch && thread != nullptr && env->event_masks.unioned_thread_event_mask.Test(kEvent)) { |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 446 | EventMask* mask = env->event_masks.GetEventMaskOrNull(thread); |
Andreas Gampe | 983c175 | 2017-01-23 19:46:56 -0800 | [diff] [blame] | 447 | dispatch = mask != nullptr && mask->Test(kEvent); |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 448 | } |
| 449 | return dispatch; |
| 450 | } |
| 451 | |
Alex Light | 9df79b7 | 2017-09-12 08:57:31 -0700 | [diff] [blame] | 452 | template <ArtJvmtiEvent kEvent, typename ...Args> |
| 453 | inline bool EventHandler::ShouldDispatch(ArtJvmTiEnv* env, |
| 454 | art::Thread* thread, |
| 455 | Args... args ATTRIBUTE_UNUSED) const { |
| 456 | static_assert(std::is_same<typename impl::EventFnType<kEvent>::type, |
| 457 | void(*)(jvmtiEnv*, Args...)>::value, |
| 458 | "Unexpected different type of shouldDispatch"); |
| 459 | |
| 460 | return ShouldDispatchOnThread<kEvent>(env, thread); |
| 461 | } |
| 462 | |
Alex Light | 73afd32 | 2017-01-18 11:17:47 -0800 | [diff] [blame] | 463 | inline void EventHandler::RecalculateGlobalEventMask(ArtJvmtiEvent event) { |
| 464 | bool union_value = false; |
| 465 | for (const ArtJvmTiEnv* stored_env : envs) { |
Alex Light | bb76646 | 2017-04-12 16:13:33 -0700 | [diff] [blame] | 466 | if (stored_env == nullptr) { |
| 467 | continue; |
| 468 | } |
Alex Light | 73afd32 | 2017-01-18 11:17:47 -0800 | [diff] [blame] | 469 | union_value |= stored_env->event_masks.global_event_mask.Test(event); |
| 470 | union_value |= stored_env->event_masks.unioned_thread_event_mask.Test(event); |
| 471 | if (union_value) { |
| 472 | break; |
| 473 | } |
| 474 | } |
| 475 | global_mask.Set(event, union_value); |
| 476 | } |
| 477 | |
| 478 | inline bool EventHandler::NeedsEventUpdate(ArtJvmTiEnv* env, |
| 479 | const jvmtiCapabilities& caps, |
| 480 | bool added) { |
| 481 | ArtJvmtiEvent event = added ? ArtJvmtiEvent::kClassFileLoadHookNonRetransformable |
| 482 | : ArtJvmtiEvent::kClassFileLoadHookRetransformable; |
Alex Light | bebd7bd | 2017-07-25 14:05:52 -0700 | [diff] [blame] | 483 | return (added && caps.can_access_local_variables == 1) || |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 484 | caps.can_generate_breakpoint_events == 1 || |
Alex Light | bebd7bd | 2017-07-25 14:05:52 -0700 | [diff] [blame] | 485 | (caps.can_retransform_classes == 1 && |
| 486 | IsEventEnabledAnywhere(event) && |
| 487 | env->event_masks.IsEnabledAnywhere(event)); |
Alex Light | 73afd32 | 2017-01-18 11:17:47 -0800 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | inline void EventHandler::HandleChangedCapabilities(ArtJvmTiEnv* env, |
| 491 | const jvmtiCapabilities& caps, |
| 492 | bool added) { |
| 493 | if (UNLIKELY(NeedsEventUpdate(env, caps, added))) { |
| 494 | env->event_masks.HandleChangedCapabilities(caps, added); |
| 495 | if (caps.can_retransform_classes == 1) { |
| 496 | RecalculateGlobalEventMask(ArtJvmtiEvent::kClassFileLoadHookRetransformable); |
| 497 | RecalculateGlobalEventMask(ArtJvmtiEvent::kClassFileLoadHookNonRetransformable); |
| 498 | } |
Alex Light | bebd7bd | 2017-07-25 14:05:52 -0700 | [diff] [blame] | 499 | if (added && caps.can_access_local_variables == 1) { |
| 500 | HandleLocalAccessCapabilityAdded(); |
| 501 | } |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 502 | if (caps.can_generate_breakpoint_events == 1) { |
| 503 | HandleBreakpointEventsChanged(added); |
| 504 | } |
Alex Light | 73afd32 | 2017-01-18 11:17:47 -0800 | [diff] [blame] | 505 | } |
| 506 | } |
| 507 | |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 508 | } // namespace openjdkjvmti |
| 509 | |
Andreas Gampe | 06c42a5 | 2017-07-26 14:17:14 -0700 | [diff] [blame] | 510 | #endif // ART_OPENJDKJVMTI_EVENTS_INL_H_ |