Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 1 | /* Copyright (C) 2016 The Android Open Source Project |
| 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 3 | * |
| 4 | * This file implements interfaces from the file jvmti.h. This implementation |
| 5 | * is licensed under the same terms as the file jvmti.h. The |
| 6 | * copyright and license information for the file jvmti.h follows. |
| 7 | * |
| 8 | * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved. |
| 9 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 10 | * |
| 11 | * This code is free software; you can redistribute it and/or modify it |
| 12 | * under the terms of the GNU General Public License version 2 only, as |
| 13 | * published by the Free Software Foundation. Oracle designates this |
| 14 | * particular file as subject to the "Classpath" exception as provided |
| 15 | * by Oracle in the LICENSE file that accompanied this code. |
| 16 | * |
| 17 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 18 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 19 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 20 | * version 2 for more details (a copy is included in the LICENSE file that |
| 21 | * accompanied this code). |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public License version |
| 24 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 25 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 26 | * |
| 27 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 28 | * or visit www.oracle.com if you need additional information or have any |
| 29 | * questions. |
| 30 | */ |
| 31 | |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 32 | #include "events-inl.h" |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 33 | |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 34 | #include <array> |
Charles Munger | 5cc0e75 | 2018-11-09 12:30:46 -0800 | [diff] [blame] | 35 | #include <sys/time.h> |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 36 | |
Andreas Gampe | e5d2398 | 2019-01-08 10:34:26 -0800 | [diff] [blame] | 37 | #include "arch/context.h" |
Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 38 | #include "art_field-inl.h" |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 39 | #include "art_jvmti.h" |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 40 | #include "art_method-inl.h" |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 41 | #include "deopt_manager.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 42 | #include "dex/dex_file_types.h" |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 43 | #include "gc/allocation_listener.h" |
Andreas Gampe | 9b8c588 | 2016-10-21 15:27:46 -0700 | [diff] [blame] | 44 | #include "gc/gc_pause_listener.h" |
| 45 | #include "gc/heap.h" |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 46 | #include "gc/scoped_gc_critical_section.h" |
Andreas Gampe | c15a2f4 | 2017-04-21 12:09:39 -0700 | [diff] [blame] | 47 | #include "handle_scope-inl.h" |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 48 | #include "instrumentation.h" |
Vladimir Marko | a3ad0cd | 2018-05-04 10:06:38 +0100 | [diff] [blame] | 49 | #include "jni/jni_env_ext-inl.h" |
| 50 | #include "jni/jni_internal.h" |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 51 | #include "mirror/class.h" |
Andreas Gampe | c15a2f4 | 2017-04-21 12:09:39 -0700 | [diff] [blame] | 52 | #include "mirror/object-inl.h" |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 53 | #include "monitor.h" |
Andreas Gampe | 373a9b5 | 2017-10-18 09:01:57 -0700 | [diff] [blame] | 54 | #include "nativehelper/scoped_local_ref.h" |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 55 | #include "runtime.h" |
Andreas Gampe | c02685c | 2016-10-17 17:40:27 -0700 | [diff] [blame] | 56 | #include "scoped_thread_state_change-inl.h" |
Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame] | 57 | #include "stack.h" |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 58 | #include "thread-inl.h" |
| 59 | #include "thread_list.h" |
| 60 | #include "ti_phase.h" |
Charles Munger | 5cc0e75 | 2018-11-09 12:30:46 -0800 | [diff] [blame] | 61 | #include "well_known_classes.h" |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 62 | |
| 63 | namespace openjdkjvmti { |
| 64 | |
Alex Light | 8c2b929 | 2017-11-09 13:21:01 -0800 | [diff] [blame] | 65 | void ArtJvmtiEventCallbacks::CopyExtensionsFrom(const ArtJvmtiEventCallbacks* cb) { |
| 66 | if (art::kIsDebugBuild) { |
| 67 | ArtJvmtiEventCallbacks clean; |
| 68 | DCHECK_EQ(memcmp(&clean, this, sizeof(clean)), 0) |
| 69 | << "CopyExtensionsFrom called with initialized eventsCallbacks!"; |
| 70 | } |
| 71 | if (cb != nullptr) { |
| 72 | memcpy(this, cb, sizeof(*this)); |
| 73 | } else { |
| 74 | memset(this, 0, sizeof(*this)); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | jvmtiError ArtJvmtiEventCallbacks::Set(jint index, jvmtiExtensionEvent cb) { |
| 79 | switch (index) { |
| 80 | case static_cast<jint>(ArtJvmtiEvent::kDdmPublishChunk): |
| 81 | DdmPublishChunk = reinterpret_cast<ArtJvmtiEventDdmPublishChunk>(cb); |
| 82 | return OK; |
| 83 | default: |
| 84 | return ERR(ILLEGAL_ARGUMENT); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | |
| 89 | bool IsExtensionEvent(jint e) { |
| 90 | return e >= static_cast<jint>(ArtJvmtiEvent::kMinEventTypeVal) && |
| 91 | e <= static_cast<jint>(ArtJvmtiEvent::kMaxEventTypeVal) && |
| 92 | IsExtensionEvent(static_cast<ArtJvmtiEvent>(e)); |
| 93 | } |
| 94 | |
| 95 | bool IsExtensionEvent(ArtJvmtiEvent e) { |
| 96 | switch (e) { |
| 97 | case ArtJvmtiEvent::kDdmPublishChunk: |
| 98 | return true; |
| 99 | default: |
| 100 | return false; |
| 101 | } |
| 102 | } |
| 103 | |
Alex Light | 73afd32 | 2017-01-18 11:17:47 -0800 | [diff] [blame] | 104 | bool EventMasks::IsEnabledAnywhere(ArtJvmtiEvent event) { |
| 105 | return global_event_mask.Test(event) || unioned_thread_event_mask.Test(event); |
| 106 | } |
| 107 | |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 108 | EventMask& EventMasks::GetEventMask(art::Thread* thread) { |
| 109 | if (thread == nullptr) { |
| 110 | return global_event_mask; |
| 111 | } |
| 112 | |
| 113 | for (auto& pair : thread_event_masks) { |
| 114 | const UniqueThread& unique_thread = pair.first; |
| 115 | if (unique_thread.first == thread && |
| 116 | unique_thread.second == static_cast<uint32_t>(thread->GetTid())) { |
| 117 | return pair.second; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | // TODO: Remove old UniqueThread with the same pointer, if exists. |
| 122 | |
| 123 | thread_event_masks.emplace_back(UniqueThread(thread, thread->GetTid()), EventMask()); |
| 124 | return thread_event_masks.back().second; |
| 125 | } |
| 126 | |
| 127 | EventMask* EventMasks::GetEventMaskOrNull(art::Thread* thread) { |
| 128 | if (thread == nullptr) { |
| 129 | return &global_event_mask; |
| 130 | } |
| 131 | |
| 132 | for (auto& pair : thread_event_masks) { |
| 133 | const UniqueThread& unique_thread = pair.first; |
| 134 | if (unique_thread.first == thread && |
| 135 | unique_thread.second == static_cast<uint32_t>(thread->GetTid())) { |
| 136 | return &pair.second; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | return nullptr; |
| 141 | } |
| 142 | |
| 143 | |
Alex Light | 74c8440 | 2017-11-29 15:26:38 -0800 | [diff] [blame] | 144 | void EventMasks::EnableEvent(ArtJvmTiEnv* env, art::Thread* thread, ArtJvmtiEvent event) { |
| 145 | DCHECK_EQ(&env->event_masks, this); |
| 146 | env->event_info_mutex_.AssertExclusiveHeld(art::Thread::Current()); |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 147 | DCHECK(EventMask::EventIsInRange(event)); |
| 148 | GetEventMask(thread).Set(event); |
| 149 | if (thread != nullptr) { |
| 150 | unioned_thread_event_mask.Set(event, true); |
| 151 | } |
| 152 | } |
| 153 | |
Alex Light | 74c8440 | 2017-11-29 15:26:38 -0800 | [diff] [blame] | 154 | void EventMasks::DisableEvent(ArtJvmTiEnv* env, art::Thread* thread, ArtJvmtiEvent event) { |
| 155 | DCHECK_EQ(&env->event_masks, this); |
| 156 | env->event_info_mutex_.AssertExclusiveHeld(art::Thread::Current()); |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 157 | DCHECK(EventMask::EventIsInRange(event)); |
| 158 | GetEventMask(thread).Set(event, false); |
| 159 | if (thread != nullptr) { |
| 160 | // Regenerate union for the event. |
| 161 | bool union_value = false; |
| 162 | for (auto& pair : thread_event_masks) { |
| 163 | union_value |= pair.second.Test(event); |
| 164 | if (union_value) { |
| 165 | break; |
| 166 | } |
| 167 | } |
| 168 | unioned_thread_event_mask.Set(event, union_value); |
| 169 | } |
| 170 | } |
| 171 | |
Alex Light | 73afd32 | 2017-01-18 11:17:47 -0800 | [diff] [blame] | 172 | void EventMasks::HandleChangedCapabilities(const jvmtiCapabilities& caps, bool caps_added) { |
| 173 | if (UNLIKELY(caps.can_retransform_classes == 1)) { |
| 174 | // If we are giving this env the retransform classes cap we need to switch all events of |
| 175 | // NonTransformable to Transformable and vice versa. |
| 176 | ArtJvmtiEvent to_remove = caps_added ? ArtJvmtiEvent::kClassFileLoadHookNonRetransformable |
| 177 | : ArtJvmtiEvent::kClassFileLoadHookRetransformable; |
| 178 | ArtJvmtiEvent to_add = caps_added ? ArtJvmtiEvent::kClassFileLoadHookRetransformable |
| 179 | : ArtJvmtiEvent::kClassFileLoadHookNonRetransformable; |
| 180 | if (global_event_mask.Test(to_remove)) { |
| 181 | CHECK(!global_event_mask.Test(to_add)); |
| 182 | global_event_mask.Set(to_remove, false); |
| 183 | global_event_mask.Set(to_add, true); |
| 184 | } |
| 185 | |
| 186 | if (unioned_thread_event_mask.Test(to_remove)) { |
| 187 | CHECK(!unioned_thread_event_mask.Test(to_add)); |
| 188 | unioned_thread_event_mask.Set(to_remove, false); |
| 189 | unioned_thread_event_mask.Set(to_add, true); |
| 190 | } |
| 191 | for (auto thread_mask : thread_event_masks) { |
| 192 | if (thread_mask.second.Test(to_remove)) { |
| 193 | CHECK(!thread_mask.second.Test(to_add)); |
| 194 | thread_mask.second.Set(to_remove, false); |
| 195 | thread_mask.second.Set(to_add, true); |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 201 | void EventHandler::RegisterArtJvmTiEnv(ArtJvmTiEnv* env) { |
Alex Light | 2a96fe8 | 2018-01-22 17:45:02 -0800 | [diff] [blame] | 202 | art::WriterMutexLock mu(art::Thread::Current(), envs_lock_); |
Alex Light | b284f8d | 2017-11-21 00:00:48 +0000 | [diff] [blame] | 203 | envs.push_back(env); |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Andreas Gampe | 3a7eb14 | 2017-01-19 21:59:22 -0800 | [diff] [blame] | 206 | void EventHandler::RemoveArtJvmTiEnv(ArtJvmTiEnv* env) { |
Alex Light | 2a96fe8 | 2018-01-22 17:45:02 -0800 | [diff] [blame] | 207 | art::WriterMutexLock mu(art::Thread::Current(), envs_lock_); |
Alex Light | bb76646 | 2017-04-12 16:13:33 -0700 | [diff] [blame] | 208 | // Since we might be currently iterating over the envs list we cannot actually erase elements. |
| 209 | // Instead we will simply replace them with 'nullptr' and skip them manually. |
Andreas Gampe | 3a7eb14 | 2017-01-19 21:59:22 -0800 | [diff] [blame] | 210 | auto it = std::find(envs.begin(), envs.end(), env); |
| 211 | if (it != envs.end()) { |
Alex Light | b284f8d | 2017-11-21 00:00:48 +0000 | [diff] [blame] | 212 | envs.erase(it); |
Andreas Gampe | 3a7eb14 | 2017-01-19 21:59:22 -0800 | [diff] [blame] | 213 | for (size_t i = static_cast<size_t>(ArtJvmtiEvent::kMinEventTypeVal); |
| 214 | i <= static_cast<size_t>(ArtJvmtiEvent::kMaxEventTypeVal); |
| 215 | ++i) { |
Alex Light | b284f8d | 2017-11-21 00:00:48 +0000 | [diff] [blame] | 216 | RecalculateGlobalEventMaskLocked(static_cast<ArtJvmtiEvent>(i)); |
Andreas Gampe | 3a7eb14 | 2017-01-19 21:59:22 -0800 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | } |
| 220 | |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 221 | static bool IsThreadControllable(ArtJvmtiEvent event) { |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 222 | switch (event) { |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 223 | case ArtJvmtiEvent::kVmInit: |
| 224 | case ArtJvmtiEvent::kVmStart: |
| 225 | case ArtJvmtiEvent::kVmDeath: |
| 226 | case ArtJvmtiEvent::kThreadStart: |
| 227 | case ArtJvmtiEvent::kCompiledMethodLoad: |
| 228 | case ArtJvmtiEvent::kCompiledMethodUnload: |
| 229 | case ArtJvmtiEvent::kDynamicCodeGenerated: |
| 230 | case ArtJvmtiEvent::kDataDumpRequest: |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 231 | return false; |
| 232 | |
| 233 | default: |
| 234 | return true; |
| 235 | } |
| 236 | } |
| 237 | |
Alex Light | 9df79b7 | 2017-09-12 08:57:31 -0700 | [diff] [blame] | 238 | template<typename Type> |
| 239 | static Type AddLocalRef(art::JNIEnvExt* e, art::mirror::Object* obj) |
| 240 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 241 | return (obj == nullptr) ? nullptr : e->AddLocalReference<Type>(obj); |
| 242 | } |
| 243 | |
| 244 | template<ArtJvmtiEvent kEvent, typename ...Args> |
| 245 | static void RunEventCallback(EventHandler* handler, |
| 246 | art::Thread* self, |
| 247 | art::JNIEnvExt* jnienv, |
| 248 | Args... args) |
| 249 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 250 | ScopedLocalRef<jthread> thread_jni(jnienv, AddLocalRef<jthread>(jnienv, self->GetPeer())); |
| 251 | handler->DispatchEvent<kEvent>(self, |
| 252 | static_cast<JNIEnv*>(jnienv), |
| 253 | thread_jni.get(), |
| 254 | args...); |
| 255 | } |
| 256 | |
Alex Light | 8c2b929 | 2017-11-09 13:21:01 -0800 | [diff] [blame] | 257 | static void SetupDdmTracking(art::DdmCallback* listener, bool enable) { |
| 258 | art::ScopedObjectAccess soa(art::Thread::Current()); |
| 259 | if (enable) { |
| 260 | art::Runtime::Current()->GetRuntimeCallbacks()->AddDdmCallback(listener); |
| 261 | } else { |
| 262 | art::Runtime::Current()->GetRuntimeCallbacks()->RemoveDdmCallback(listener); |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | class JvmtiDdmChunkListener : public art::DdmCallback { |
| 267 | public: |
| 268 | explicit JvmtiDdmChunkListener(EventHandler* handler) : handler_(handler) {} |
| 269 | |
| 270 | void DdmPublishChunk(uint32_t type, const art::ArrayRef<const uint8_t>& data) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 271 | override REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Alex Light | 8c2b929 | 2017-11-09 13:21:01 -0800 | [diff] [blame] | 272 | if (handler_->IsEventEnabledAnywhere(ArtJvmtiEvent::kDdmPublishChunk)) { |
| 273 | art::Thread* self = art::Thread::Current(); |
| 274 | handler_->DispatchEvent<ArtJvmtiEvent::kDdmPublishChunk>( |
| 275 | self, |
| 276 | static_cast<JNIEnv*>(self->GetJniEnv()), |
| 277 | static_cast<jint>(type), |
| 278 | static_cast<jint>(data.size()), |
| 279 | reinterpret_cast<const jbyte*>(data.data())); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | private: |
| 284 | EventHandler* handler_; |
| 285 | |
| 286 | DISALLOW_COPY_AND_ASSIGN(JvmtiDdmChunkListener); |
| 287 | }; |
| 288 | |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 289 | class JvmtiAllocationListener : public art::gc::AllocationListener { |
| 290 | public: |
| 291 | explicit JvmtiAllocationListener(EventHandler* handler) : handler_(handler) {} |
| 292 | |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 293 | void ObjectAllocated(art::Thread* self, art::ObjPtr<art::mirror::Object>* obj, size_t byte_count) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 294 | override REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 295 | DCHECK_EQ(self, art::Thread::Current()); |
| 296 | |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 297 | if (handler_->IsEventEnabledAnywhere(ArtJvmtiEvent::kVmObjectAlloc)) { |
Mathieu Chartier | a711804 | 2016-10-12 15:45:58 -0700 | [diff] [blame] | 298 | art::StackHandleScope<1> hs(self); |
| 299 | auto h = hs.NewHandleWrapper(obj); |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 300 | // jvmtiEventVMObjectAlloc parameters: |
| 301 | // jvmtiEnv *jvmti_env, |
| 302 | // JNIEnv* jni_env, |
| 303 | // jthread thread, |
| 304 | // jobject object, |
| 305 | // jclass object_klass, |
| 306 | // jlong size |
| 307 | art::JNIEnvExt* jni_env = self->GetJniEnv(); |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 308 | ScopedLocalRef<jobject> object( |
| 309 | jni_env, jni_env->AddLocalReference<jobject>(*obj)); |
| 310 | ScopedLocalRef<jclass> klass( |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 311 | jni_env, jni_env->AddLocalReference<jclass>(obj->Ptr()->GetClass())); |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 312 | |
Alex Light | 9df79b7 | 2017-09-12 08:57:31 -0700 | [diff] [blame] | 313 | RunEventCallback<ArtJvmtiEvent::kVmObjectAlloc>(handler_, |
| 314 | self, |
| 315 | jni_env, |
| 316 | object.get(), |
| 317 | klass.get(), |
| 318 | static_cast<jlong>(byte_count)); |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 319 | } |
| 320 | } |
| 321 | |
| 322 | private: |
| 323 | EventHandler* handler_; |
| 324 | }; |
| 325 | |
| 326 | static void SetupObjectAllocationTracking(art::gc::AllocationListener* listener, bool enable) { |
Andreas Gampe | c02685c | 2016-10-17 17:40:27 -0700 | [diff] [blame] | 327 | // We must not hold the mutator lock here, but if we're in FastJNI, for example, we might. For |
| 328 | // now, do a workaround: (possibly) acquire and release. |
| 329 | art::ScopedObjectAccess soa(art::Thread::Current()); |
| 330 | art::ScopedThreadSuspension sts(soa.Self(), art::ThreadState::kSuspended); |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 331 | if (enable) { |
| 332 | art::Runtime::Current()->GetHeap()->SetAllocationListener(listener); |
| 333 | } else { |
| 334 | art::Runtime::Current()->GetHeap()->RemoveAllocationListener(); |
| 335 | } |
| 336 | } |
| 337 | |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 338 | class JvmtiMonitorListener : public art::MonitorCallback { |
| 339 | public: |
| 340 | explicit JvmtiMonitorListener(EventHandler* handler) : handler_(handler) {} |
| 341 | |
| 342 | void MonitorContendedLocking(art::Monitor* m) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 343 | override REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 344 | if (handler_->IsEventEnabledAnywhere(ArtJvmtiEvent::kMonitorContendedEnter)) { |
| 345 | art::Thread* self = art::Thread::Current(); |
| 346 | art::JNIEnvExt* jnienv = self->GetJniEnv(); |
| 347 | ScopedLocalRef<jobject> mon(jnienv, AddLocalRef<jobject>(jnienv, m->GetObject())); |
| 348 | RunEventCallback<ArtJvmtiEvent::kMonitorContendedEnter>( |
| 349 | handler_, |
| 350 | self, |
| 351 | jnienv, |
| 352 | mon.get()); |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | void MonitorContendedLocked(art::Monitor* m) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 357 | override REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 358 | if (handler_->IsEventEnabledAnywhere(ArtJvmtiEvent::kMonitorContendedEntered)) { |
| 359 | art::Thread* self = art::Thread::Current(); |
| 360 | art::JNIEnvExt* jnienv = self->GetJniEnv(); |
| 361 | ScopedLocalRef<jobject> mon(jnienv, AddLocalRef<jobject>(jnienv, m->GetObject())); |
| 362 | RunEventCallback<ArtJvmtiEvent::kMonitorContendedEntered>( |
| 363 | handler_, |
| 364 | self, |
| 365 | jnienv, |
| 366 | mon.get()); |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | void ObjectWaitStart(art::Handle<art::mirror::Object> obj, int64_t timeout) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 371 | override REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 372 | if (handler_->IsEventEnabledAnywhere(ArtJvmtiEvent::kMonitorWait)) { |
| 373 | art::Thread* self = art::Thread::Current(); |
| 374 | art::JNIEnvExt* jnienv = self->GetJniEnv(); |
| 375 | ScopedLocalRef<jobject> mon(jnienv, AddLocalRef<jobject>(jnienv, obj.Get())); |
| 376 | RunEventCallback<ArtJvmtiEvent::kMonitorWait>( |
| 377 | handler_, |
| 378 | self, |
| 379 | jnienv, |
| 380 | mon.get(), |
| 381 | static_cast<jlong>(timeout)); |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | |
| 386 | // Our interpretation of the spec is that the JVMTI_EVENT_MONITOR_WAITED will be sent immediately |
| 387 | // after a thread has woken up from a sleep caused by a call to Object#wait. If the thread will |
| 388 | // never go to sleep (due to not having the lock, having bad arguments, or having an exception |
| 389 | // propogated from JVMTI_EVENT_MONITOR_WAIT) we will not send this event. |
| 390 | // |
| 391 | // This does not fully match the RI semantics. Specifically, we will not send the |
| 392 | // JVMTI_EVENT_MONITOR_WAITED event in one situation where the RI would, there was an exception in |
| 393 | // the JVMTI_EVENT_MONITOR_WAIT event but otherwise the call was fine. In that case the RI would |
| 394 | // send this event and return without going to sleep. |
| 395 | // |
| 396 | // See b/65558434 for more discussion. |
| 397 | void MonitorWaitFinished(art::Monitor* m, bool timeout) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 398 | override REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 399 | if (handler_->IsEventEnabledAnywhere(ArtJvmtiEvent::kMonitorWaited)) { |
| 400 | art::Thread* self = art::Thread::Current(); |
| 401 | art::JNIEnvExt* jnienv = self->GetJniEnv(); |
| 402 | ScopedLocalRef<jobject> mon(jnienv, AddLocalRef<jobject>(jnienv, m->GetObject())); |
| 403 | RunEventCallback<ArtJvmtiEvent::kMonitorWaited>( |
| 404 | handler_, |
| 405 | self, |
| 406 | jnienv, |
| 407 | mon.get(), |
| 408 | static_cast<jboolean>(timeout)); |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | private: |
| 413 | EventHandler* handler_; |
| 414 | }; |
| 415 | |
Charles Munger | 5cc0e75 | 2018-11-09 12:30:46 -0800 | [diff] [blame] | 416 | class JvmtiParkListener : public art::ParkCallback { |
| 417 | public: |
| 418 | explicit JvmtiParkListener(EventHandler* handler) : handler_(handler) {} |
| 419 | |
| 420 | void ThreadParkStart(bool is_absolute, int64_t timeout) |
| 421 | override REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 422 | if (handler_->IsEventEnabledAnywhere(ArtJvmtiEvent::kMonitorWait)) { |
| 423 | art::Thread* self = art::Thread::Current(); |
| 424 | art::JNIEnvExt* jnienv = self->GetJniEnv(); |
| 425 | art::ArtField* parkBlockerField = art::jni::DecodeArtField( |
| 426 | art::WellKnownClasses::java_lang_Thread_parkBlocker); |
| 427 | art::ObjPtr<art::mirror::Object> blocker_obj = parkBlockerField->GetObj(self->GetPeer()); |
| 428 | if (blocker_obj.IsNull()) { |
| 429 | blocker_obj = self->GetPeer(); |
| 430 | } |
| 431 | int64_t timeout_ms; |
| 432 | if (!is_absolute) { |
| 433 | if (timeout == 0) { |
| 434 | timeout_ms = 0; |
| 435 | } else { |
| 436 | timeout_ms = timeout / 1000000; |
| 437 | if (timeout_ms == 0) { |
| 438 | // If we were instructed to park for a nonzero number of nanoseconds, but not enough |
| 439 | // to be a full millisecond, round up to 1 ms. A nonzero park() call will return |
| 440 | // soon, but a 0 wait or park call will wait indefinitely. |
| 441 | timeout_ms = 1; |
| 442 | } |
| 443 | } |
| 444 | } else { |
| 445 | struct timeval tv; |
| 446 | gettimeofday(&tv, (struct timezone *) nullptr); |
| 447 | int64_t now = tv.tv_sec * 1000LL + tv.tv_usec / 1000; |
| 448 | if (now < timeout) { |
| 449 | timeout_ms = timeout - now; |
| 450 | } else { |
| 451 | // Waiting for 0 ms is an indefinite wait; parking until a time in |
| 452 | // the past or the current time will return immediately, so emulate |
| 453 | // the shortest possible wait event. |
| 454 | timeout_ms = 1; |
| 455 | } |
| 456 | } |
| 457 | ScopedLocalRef<jobject> blocker(jnienv, AddLocalRef<jobject>(jnienv, blocker_obj.Ptr())); |
| 458 | RunEventCallback<ArtJvmtiEvent::kMonitorWait>( |
| 459 | handler_, |
| 460 | self, |
| 461 | jnienv, |
| 462 | blocker.get(), |
| 463 | static_cast<jlong>(timeout_ms)); |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | |
| 468 | // Our interpretation of the spec is that the JVMTI_EVENT_MONITOR_WAITED will be sent immediately |
| 469 | // after a thread has woken up from a sleep caused by a call to Object#wait. If the thread will |
| 470 | // never go to sleep (due to not having the lock, having bad arguments, or having an exception |
| 471 | // propogated from JVMTI_EVENT_MONITOR_WAIT) we will not send this event. |
| 472 | // |
| 473 | // This does not fully match the RI semantics. Specifically, we will not send the |
| 474 | // JVMTI_EVENT_MONITOR_WAITED event in one situation where the RI would, there was an exception in |
| 475 | // the JVMTI_EVENT_MONITOR_WAIT event but otherwise the call was fine. In that case the RI would |
| 476 | // send this event and return without going to sleep. |
| 477 | // |
| 478 | // See b/65558434 for more discussion. |
| 479 | void ThreadParkFinished(bool timeout) override REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 480 | if (handler_->IsEventEnabledAnywhere(ArtJvmtiEvent::kMonitorWaited)) { |
| 481 | art::Thread* self = art::Thread::Current(); |
| 482 | art::JNIEnvExt* jnienv = self->GetJniEnv(); |
| 483 | art::ArtField* parkBlockerField = art::jni::DecodeArtField( |
| 484 | art::WellKnownClasses::java_lang_Thread_parkBlocker); |
| 485 | art::ObjPtr<art::mirror::Object> blocker_obj = parkBlockerField->GetObj(self->GetPeer()); |
| 486 | if (blocker_obj.IsNull()) { |
| 487 | blocker_obj = self->GetPeer(); |
| 488 | } |
| 489 | ScopedLocalRef<jobject> blocker(jnienv, AddLocalRef<jobject>(jnienv, blocker_obj.Ptr())); |
| 490 | RunEventCallback<ArtJvmtiEvent::kMonitorWaited>( |
| 491 | handler_, |
| 492 | self, |
| 493 | jnienv, |
| 494 | blocker.get(), |
| 495 | static_cast<jboolean>(timeout)); |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | private: |
| 500 | EventHandler* handler_; |
| 501 | }; |
| 502 | |
| 503 | static void SetupMonitorListener(art::MonitorCallback* monitor_listener, art::ParkCallback* park_listener, bool enable) { |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 504 | // We must not hold the mutator lock here, but if we're in FastJNI, for example, we might. For |
| 505 | // now, do a workaround: (possibly) acquire and release. |
| 506 | art::ScopedObjectAccess soa(art::Thread::Current()); |
| 507 | if (enable) { |
Charles Munger | 5cc0e75 | 2018-11-09 12:30:46 -0800 | [diff] [blame] | 508 | art::Runtime::Current()->GetRuntimeCallbacks()->AddMonitorCallback(monitor_listener); |
| 509 | art::Runtime::Current()->GetRuntimeCallbacks()->AddParkCallback(park_listener); |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 510 | } else { |
Charles Munger | 5cc0e75 | 2018-11-09 12:30:46 -0800 | [diff] [blame] | 511 | art::Runtime::Current()->GetRuntimeCallbacks()->RemoveMonitorCallback(monitor_listener); |
| 512 | art::Runtime::Current()->GetRuntimeCallbacks()->RemoveParkCallback(park_listener); |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 513 | } |
| 514 | } |
| 515 | |
Andreas Gampe | 9b8c588 | 2016-10-21 15:27:46 -0700 | [diff] [blame] | 516 | // Report GC pauses (see spec) as GARBAGE_COLLECTION_START and GARBAGE_COLLECTION_END. |
| 517 | class JvmtiGcPauseListener : public art::gc::GcPauseListener { |
| 518 | public: |
| 519 | explicit JvmtiGcPauseListener(EventHandler* handler) |
| 520 | : handler_(handler), |
| 521 | start_enabled_(false), |
| 522 | finish_enabled_(false) {} |
| 523 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 524 | void StartPause() override { |
Alex Light | b284f8d | 2017-11-21 00:00:48 +0000 | [diff] [blame] | 525 | handler_->DispatchEvent<ArtJvmtiEvent::kGarbageCollectionStart>(art::Thread::Current()); |
Andreas Gampe | 9b8c588 | 2016-10-21 15:27:46 -0700 | [diff] [blame] | 526 | } |
| 527 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 528 | void EndPause() override { |
Alex Light | b284f8d | 2017-11-21 00:00:48 +0000 | [diff] [blame] | 529 | handler_->DispatchEvent<ArtJvmtiEvent::kGarbageCollectionFinish>(art::Thread::Current()); |
Andreas Gampe | 9b8c588 | 2016-10-21 15:27:46 -0700 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | bool IsEnabled() { |
| 533 | return start_enabled_ || finish_enabled_; |
| 534 | } |
| 535 | |
| 536 | void SetStartEnabled(bool e) { |
| 537 | start_enabled_ = e; |
| 538 | } |
| 539 | |
| 540 | void SetFinishEnabled(bool e) { |
| 541 | finish_enabled_ = e; |
| 542 | } |
| 543 | |
| 544 | private: |
| 545 | EventHandler* handler_; |
| 546 | bool start_enabled_; |
| 547 | bool finish_enabled_; |
| 548 | }; |
| 549 | |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 550 | static void SetupGcPauseTracking(JvmtiGcPauseListener* listener, ArtJvmtiEvent event, bool enable) { |
Andreas Gampe | 9b8c588 | 2016-10-21 15:27:46 -0700 | [diff] [blame] | 551 | bool old_state = listener->IsEnabled(); |
| 552 | |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 553 | if (event == ArtJvmtiEvent::kGarbageCollectionStart) { |
Andreas Gampe | 9b8c588 | 2016-10-21 15:27:46 -0700 | [diff] [blame] | 554 | listener->SetStartEnabled(enable); |
| 555 | } else { |
| 556 | listener->SetFinishEnabled(enable); |
| 557 | } |
| 558 | |
| 559 | bool new_state = listener->IsEnabled(); |
| 560 | |
| 561 | if (old_state != new_state) { |
| 562 | if (new_state) { |
| 563 | art::Runtime::Current()->GetHeap()->SetGcPauseListener(listener); |
| 564 | } else { |
| 565 | art::Runtime::Current()->GetHeap()->RemoveGcPauseListener(); |
| 566 | } |
| 567 | } |
| 568 | } |
| 569 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 570 | class JvmtiMethodTraceListener final : public art::instrumentation::InstrumentationListener { |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 571 | public: |
| 572 | explicit JvmtiMethodTraceListener(EventHandler* handler) : event_handler_(handler) {} |
| 573 | |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 574 | // Call-back for when a method is entered. |
| 575 | void MethodEntered(art::Thread* self, |
| 576 | art::Handle<art::mirror::Object> this_object ATTRIBUTE_UNUSED, |
| 577 | art::ArtMethod* method, |
| 578 | uint32_t dex_pc ATTRIBUTE_UNUSED) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 579 | REQUIRES_SHARED(art::Locks::mutator_lock_) override { |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 580 | if (!method->IsRuntimeMethod() && |
| 581 | event_handler_->IsEventEnabledAnywhere(ArtJvmtiEvent::kMethodEntry)) { |
| 582 | art::JNIEnvExt* jnienv = self->GetJniEnv(); |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 583 | RunEventCallback<ArtJvmtiEvent::kMethodEntry>(event_handler_, |
| 584 | self, |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 585 | jnienv, |
| 586 | art::jni::EncodeArtMethod(method)); |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | // Callback for when a method is exited with a reference return value. |
| 591 | void MethodExited(art::Thread* self, |
| 592 | art::Handle<art::mirror::Object> this_object ATTRIBUTE_UNUSED, |
| 593 | art::ArtMethod* method, |
| 594 | uint32_t dex_pc ATTRIBUTE_UNUSED, |
| 595 | art::Handle<art::mirror::Object> return_value) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 596 | REQUIRES_SHARED(art::Locks::mutator_lock_) override { |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 597 | if (!method->IsRuntimeMethod() && |
| 598 | event_handler_->IsEventEnabledAnywhere(ArtJvmtiEvent::kMethodExit)) { |
Alex Light | c916736 | 2018-06-11 16:46:43 -0700 | [diff] [blame] | 599 | DCHECK_EQ( |
| 600 | method->GetInterfaceMethodIfProxy(art::kRuntimePointerSize)->GetReturnTypePrimitive(), |
| 601 | art::Primitive::kPrimNot) << method->PrettyMethod(); |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 602 | DCHECK(!self->IsExceptionPending()); |
| 603 | jvalue val; |
| 604 | art::JNIEnvExt* jnienv = self->GetJniEnv(); |
| 605 | ScopedLocalRef<jobject> return_jobj(jnienv, AddLocalRef<jobject>(jnienv, return_value.Get())); |
| 606 | val.l = return_jobj.get(); |
| 607 | RunEventCallback<ArtJvmtiEvent::kMethodExit>( |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 608 | event_handler_, |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 609 | self, |
| 610 | jnienv, |
| 611 | art::jni::EncodeArtMethod(method), |
Andreas Gampe | 6e89776 | 2018-10-16 13:09:32 -0700 | [diff] [blame] | 612 | /*was_popped_by_exception=*/ static_cast<jboolean>(JNI_FALSE), |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 613 | val); |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | // Call-back for when a method is exited. |
| 618 | void MethodExited(art::Thread* self, |
| 619 | art::Handle<art::mirror::Object> this_object ATTRIBUTE_UNUSED, |
| 620 | art::ArtMethod* method, |
| 621 | uint32_t dex_pc ATTRIBUTE_UNUSED, |
| 622 | const art::JValue& return_value) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 623 | REQUIRES_SHARED(art::Locks::mutator_lock_) override { |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 624 | if (!method->IsRuntimeMethod() && |
| 625 | event_handler_->IsEventEnabledAnywhere(ArtJvmtiEvent::kMethodExit)) { |
Alex Light | c916736 | 2018-06-11 16:46:43 -0700 | [diff] [blame] | 626 | DCHECK_NE( |
| 627 | method->GetInterfaceMethodIfProxy(art::kRuntimePointerSize)->GetReturnTypePrimitive(), |
| 628 | art::Primitive::kPrimNot) << method->PrettyMethod(); |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 629 | DCHECK(!self->IsExceptionPending()); |
| 630 | jvalue val; |
| 631 | art::JNIEnvExt* jnienv = self->GetJniEnv(); |
| 632 | // 64bit integer is the largest value in the union so we should be fine simply copying it into |
| 633 | // the union. |
| 634 | val.j = return_value.GetJ(); |
| 635 | RunEventCallback<ArtJvmtiEvent::kMethodExit>( |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 636 | event_handler_, |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 637 | self, |
| 638 | jnienv, |
| 639 | art::jni::EncodeArtMethod(method), |
Andreas Gampe | 6e89776 | 2018-10-16 13:09:32 -0700 | [diff] [blame] | 640 | /*was_popped_by_exception=*/ static_cast<jboolean>(JNI_FALSE), |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 641 | val); |
| 642 | } |
| 643 | } |
| 644 | |
| 645 | // Call-back for when a method is popped due to an exception throw. A method will either cause a |
| 646 | // MethodExited call-back or a MethodUnwind call-back when its activation is removed. |
| 647 | void MethodUnwind(art::Thread* self, |
| 648 | art::Handle<art::mirror::Object> this_object ATTRIBUTE_UNUSED, |
| 649 | art::ArtMethod* method, |
| 650 | uint32_t dex_pc ATTRIBUTE_UNUSED) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 651 | REQUIRES_SHARED(art::Locks::mutator_lock_) override { |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 652 | if (!method->IsRuntimeMethod() && |
| 653 | event_handler_->IsEventEnabledAnywhere(ArtJvmtiEvent::kMethodExit)) { |
| 654 | jvalue val; |
| 655 | // Just set this to 0xffffffffffffffff so it's not uninitialized. |
| 656 | val.j = static_cast<jlong>(-1); |
| 657 | art::JNIEnvExt* jnienv = self->GetJniEnv(); |
| 658 | art::StackHandleScope<1> hs(self); |
| 659 | art::Handle<art::mirror::Throwable> old_exception(hs.NewHandle(self->GetException())); |
| 660 | CHECK(!old_exception.IsNull()); |
| 661 | self->ClearException(); |
| 662 | RunEventCallback<ArtJvmtiEvent::kMethodExit>( |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 663 | event_handler_, |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 664 | self, |
| 665 | jnienv, |
| 666 | art::jni::EncodeArtMethod(method), |
Andreas Gampe | 6e89776 | 2018-10-16 13:09:32 -0700 | [diff] [blame] | 667 | /*was_popped_by_exception=*/ static_cast<jboolean>(JNI_TRUE), |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 668 | val); |
| 669 | // Match RI behavior of just throwing away original exception if a new one is thrown. |
| 670 | if (LIKELY(!self->IsExceptionPending())) { |
| 671 | self->SetException(old_exception.Get()); |
| 672 | } |
| 673 | } |
| 674 | } |
| 675 | |
Alex Light | a26e349 | 2017-06-27 17:55:37 -0700 | [diff] [blame] | 676 | // Call-back for when the dex pc moves in a method. |
| 677 | void DexPcMoved(art::Thread* self, |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 678 | art::Handle<art::mirror::Object> this_object ATTRIBUTE_UNUSED, |
Alex Light | a26e349 | 2017-06-27 17:55:37 -0700 | [diff] [blame] | 679 | art::ArtMethod* method, |
| 680 | uint32_t new_dex_pc) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 681 | REQUIRES_SHARED(art::Locks::mutator_lock_) override { |
Alex Light | a26e349 | 2017-06-27 17:55:37 -0700 | [diff] [blame] | 682 | DCHECK(!method->IsRuntimeMethod()); |
| 683 | // Default methods might be copied to multiple classes. We need to get the canonical version of |
| 684 | // this method so that we can check for breakpoints correctly. |
| 685 | // TODO We should maybe do this on other events to ensure that we are consistent WRT default |
| 686 | // methods. This could interact with obsolete methods if we ever let interface redefinition |
| 687 | // happen though. |
| 688 | method = method->GetCanonicalMethod(); |
| 689 | art::JNIEnvExt* jnienv = self->GetJniEnv(); |
| 690 | jmethodID jmethod = art::jni::EncodeArtMethod(method); |
| 691 | jlocation location = static_cast<jlocation>(new_dex_pc); |
| 692 | // Step event is reported first according to the spec. |
| 693 | if (event_handler_->IsEventEnabledAnywhere(ArtJvmtiEvent::kSingleStep)) { |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 694 | RunEventCallback<ArtJvmtiEvent::kSingleStep>(event_handler_, self, jnienv, jmethod, location); |
Alex Light | a26e349 | 2017-06-27 17:55:37 -0700 | [diff] [blame] | 695 | } |
| 696 | // Next we do the Breakpoint events. The Dispatch code will filter the individual |
| 697 | if (event_handler_->IsEventEnabledAnywhere(ArtJvmtiEvent::kBreakpoint)) { |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 698 | RunEventCallback<ArtJvmtiEvent::kBreakpoint>(event_handler_, self, jnienv, jmethod, location); |
Alex Light | a26e349 | 2017-06-27 17:55:37 -0700 | [diff] [blame] | 699 | } |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | // Call-back for when we read from a field. |
Alex Light | 084fa37 | 2017-06-16 08:58:34 -0700 | [diff] [blame] | 703 | void FieldRead(art::Thread* self, |
| 704 | art::Handle<art::mirror::Object> this_object, |
| 705 | art::ArtMethod* method, |
| 706 | uint32_t dex_pc, |
| 707 | art::ArtField* field) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 708 | REQUIRES_SHARED(art::Locks::mutator_lock_) override { |
Alex Light | 084fa37 | 2017-06-16 08:58:34 -0700 | [diff] [blame] | 709 | if (event_handler_->IsEventEnabledAnywhere(ArtJvmtiEvent::kFieldAccess)) { |
| 710 | art::JNIEnvExt* jnienv = self->GetJniEnv(); |
| 711 | // DCHECK(!self->IsExceptionPending()); |
| 712 | ScopedLocalRef<jobject> this_ref(jnienv, AddLocalRef<jobject>(jnienv, this_object.Get())); |
| 713 | ScopedLocalRef<jobject> fklass(jnienv, |
| 714 | AddLocalRef<jobject>(jnienv, |
| 715 | field->GetDeclaringClass().Ptr())); |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 716 | RunEventCallback<ArtJvmtiEvent::kFieldAccess>(event_handler_, |
| 717 | self, |
Alex Light | 084fa37 | 2017-06-16 08:58:34 -0700 | [diff] [blame] | 718 | jnienv, |
| 719 | art::jni::EncodeArtMethod(method), |
| 720 | static_cast<jlocation>(dex_pc), |
| 721 | static_cast<jclass>(fklass.get()), |
| 722 | this_ref.get(), |
| 723 | art::jni::EncodeArtField(field)); |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | void FieldWritten(art::Thread* self, |
| 728 | art::Handle<art::mirror::Object> this_object, |
| 729 | art::ArtMethod* method, |
| 730 | uint32_t dex_pc, |
| 731 | art::ArtField* field, |
| 732 | art::Handle<art::mirror::Object> new_val) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 733 | REQUIRES_SHARED(art::Locks::mutator_lock_) override { |
Alex Light | 084fa37 | 2017-06-16 08:58:34 -0700 | [diff] [blame] | 734 | if (event_handler_->IsEventEnabledAnywhere(ArtJvmtiEvent::kFieldModification)) { |
| 735 | art::JNIEnvExt* jnienv = self->GetJniEnv(); |
| 736 | // DCHECK(!self->IsExceptionPending()); |
| 737 | ScopedLocalRef<jobject> this_ref(jnienv, AddLocalRef<jobject>(jnienv, this_object.Get())); |
| 738 | ScopedLocalRef<jobject> fklass(jnienv, |
| 739 | AddLocalRef<jobject>(jnienv, |
| 740 | field->GetDeclaringClass().Ptr())); |
| 741 | ScopedLocalRef<jobject> fval(jnienv, AddLocalRef<jobject>(jnienv, new_val.Get())); |
| 742 | jvalue val; |
| 743 | val.l = fval.get(); |
| 744 | RunEventCallback<ArtJvmtiEvent::kFieldModification>( |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 745 | event_handler_, |
Alex Light | 084fa37 | 2017-06-16 08:58:34 -0700 | [diff] [blame] | 746 | self, |
| 747 | jnienv, |
| 748 | art::jni::EncodeArtMethod(method), |
| 749 | static_cast<jlocation>(dex_pc), |
| 750 | static_cast<jclass>(fklass.get()), |
| 751 | field->IsStatic() ? nullptr : this_ref.get(), |
| 752 | art::jni::EncodeArtField(field), |
| 753 | 'L', // type_char |
| 754 | val); |
| 755 | } |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | // Call-back for when we write into a field. |
Alex Light | 084fa37 | 2017-06-16 08:58:34 -0700 | [diff] [blame] | 759 | void FieldWritten(art::Thread* self, |
| 760 | art::Handle<art::mirror::Object> this_object, |
| 761 | art::ArtMethod* method, |
| 762 | uint32_t dex_pc, |
| 763 | art::ArtField* field, |
| 764 | const art::JValue& field_value) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 765 | REQUIRES_SHARED(art::Locks::mutator_lock_) override { |
Alex Light | 084fa37 | 2017-06-16 08:58:34 -0700 | [diff] [blame] | 766 | if (event_handler_->IsEventEnabledAnywhere(ArtJvmtiEvent::kFieldModification)) { |
| 767 | art::JNIEnvExt* jnienv = self->GetJniEnv(); |
| 768 | DCHECK(!self->IsExceptionPending()); |
| 769 | ScopedLocalRef<jobject> this_ref(jnienv, AddLocalRef<jobject>(jnienv, this_object.Get())); |
| 770 | ScopedLocalRef<jobject> fklass(jnienv, |
| 771 | AddLocalRef<jobject>(jnienv, |
| 772 | field->GetDeclaringClass().Ptr())); |
| 773 | char type_char = art::Primitive::Descriptor(field->GetTypeAsPrimitiveType())[0]; |
| 774 | jvalue val; |
| 775 | // 64bit integer is the largest value in the union so we should be fine simply copying it into |
| 776 | // the union. |
| 777 | val.j = field_value.GetJ(); |
| 778 | RunEventCallback<ArtJvmtiEvent::kFieldModification>( |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 779 | event_handler_, |
Alex Light | 084fa37 | 2017-06-16 08:58:34 -0700 | [diff] [blame] | 780 | self, |
| 781 | jnienv, |
| 782 | art::jni::EncodeArtMethod(method), |
| 783 | static_cast<jlocation>(dex_pc), |
| 784 | static_cast<jclass>(fklass.get()), |
| 785 | field->IsStatic() ? nullptr : this_ref.get(), // nb static field modification get given |
| 786 | // the class as this_object for some |
| 787 | // reason. |
| 788 | art::jni::EncodeArtField(field), |
| 789 | type_char, |
| 790 | val); |
| 791 | } |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 792 | } |
| 793 | |
Alex Light | e814f9d | 2017-07-31 16:14:39 -0700 | [diff] [blame] | 794 | void WatchedFramePop(art::Thread* self, const art::ShadowFrame& frame) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 795 | REQUIRES_SHARED(art::Locks::mutator_lock_) override { |
Alex Light | e814f9d | 2017-07-31 16:14:39 -0700 | [diff] [blame] | 796 | art::JNIEnvExt* jnienv = self->GetJniEnv(); |
Alex Light | 9df79b7 | 2017-09-12 08:57:31 -0700 | [diff] [blame] | 797 | jboolean is_exception_pending = self->IsExceptionPending(); |
| 798 | RunEventCallback<ArtJvmtiEvent::kFramePop>( |
| 799 | event_handler_, |
| 800 | self, |
| 801 | jnienv, |
| 802 | art::jni::EncodeArtMethod(frame.GetMethod()), |
| 803 | is_exception_pending, |
| 804 | &frame); |
Alex Light | e814f9d | 2017-07-31 16:14:39 -0700 | [diff] [blame] | 805 | } |
| 806 | |
Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame] | 807 | static void FindCatchMethodsFromThrow(art::Thread* self, |
| 808 | art::Handle<art::mirror::Throwable> exception, |
| 809 | /*out*/ art::ArtMethod** out_method, |
| 810 | /*out*/ uint32_t* dex_pc) |
| 811 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 812 | // Finds the location where this exception will most likely be caught. We ignore intervening |
| 813 | // native frames (which could catch the exception) and return the closest java frame with a |
| 814 | // compatible catch statement. |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 815 | class CatchLocationFinder final : public art::StackVisitor { |
Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame] | 816 | public: |
| 817 | CatchLocationFinder(art::Thread* target, |
| 818 | art::Handle<art::mirror::Class> exception_class, |
| 819 | art::Context* context, |
| 820 | /*out*/ art::ArtMethod** out_catch_method, |
| 821 | /*out*/ uint32_t* out_catch_pc) |
| 822 | REQUIRES_SHARED(art::Locks::mutator_lock_) |
| 823 | : StackVisitor(target, context, art::StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
| 824 | exception_class_(exception_class), |
| 825 | catch_method_ptr_(out_catch_method), |
| 826 | catch_dex_pc_ptr_(out_catch_pc) {} |
| 827 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 828 | bool VisitFrame() override REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame] | 829 | art::ArtMethod* method = GetMethod(); |
| 830 | DCHECK(method != nullptr); |
| 831 | if (method->IsRuntimeMethod()) { |
| 832 | return true; |
| 833 | } |
| 834 | |
| 835 | if (!method->IsNative()) { |
| 836 | uint32_t cur_dex_pc = GetDexPc(); |
Andreas Gampe | e2abbc6 | 2017-09-15 11:59:26 -0700 | [diff] [blame] | 837 | if (cur_dex_pc == art::dex::kDexNoIndex) { |
Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame] | 838 | // This frame looks opaque. Just keep on going. |
| 839 | return true; |
| 840 | } |
| 841 | bool has_no_move_exception = false; |
| 842 | uint32_t found_dex_pc = method->FindCatchBlock( |
| 843 | exception_class_, cur_dex_pc, &has_no_move_exception); |
Andreas Gampe | e2abbc6 | 2017-09-15 11:59:26 -0700 | [diff] [blame] | 844 | if (found_dex_pc != art::dex::kDexNoIndex) { |
Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame] | 845 | // We found the catch. Store the result and return. |
| 846 | *catch_method_ptr_ = method; |
| 847 | *catch_dex_pc_ptr_ = found_dex_pc; |
| 848 | return false; |
| 849 | } |
| 850 | } |
| 851 | return true; |
| 852 | } |
| 853 | |
| 854 | private: |
| 855 | art::Handle<art::mirror::Class> exception_class_; |
| 856 | art::ArtMethod** catch_method_ptr_; |
| 857 | uint32_t* catch_dex_pc_ptr_; |
| 858 | |
| 859 | DISALLOW_COPY_AND_ASSIGN(CatchLocationFinder); |
| 860 | }; |
| 861 | |
| 862 | art::StackHandleScope<1> hs(self); |
| 863 | *out_method = nullptr; |
| 864 | *dex_pc = 0; |
| 865 | std::unique_ptr<art::Context> context(art::Context::Create()); |
| 866 | |
| 867 | CatchLocationFinder clf(self, |
| 868 | hs.NewHandle(exception->GetClass()), |
| 869 | context.get(), |
| 870 | /*out*/ out_method, |
| 871 | /*out*/ dex_pc); |
Andreas Gampe | 6e89776 | 2018-10-16 13:09:32 -0700 | [diff] [blame] | 872 | clf.WalkStack(/* include_transitions= */ false); |
Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame] | 873 | } |
| 874 | |
Alex Light | 6e1607e | 2017-08-23 10:06:18 -0700 | [diff] [blame] | 875 | // Call-back when an exception is thrown. |
Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame] | 876 | void ExceptionThrown(art::Thread* self, art::Handle<art::mirror::Throwable> exception_object) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 877 | REQUIRES_SHARED(art::Locks::mutator_lock_) override { |
Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame] | 878 | DCHECK(self->IsExceptionThrownByCurrentMethod(exception_object.Get())); |
| 879 | // The instrumentation events get rid of this for us. |
| 880 | DCHECK(!self->IsExceptionPending()); |
| 881 | if (event_handler_->IsEventEnabledAnywhere(ArtJvmtiEvent::kException)) { |
| 882 | art::JNIEnvExt* jnienv = self->GetJniEnv(); |
| 883 | art::ArtMethod* catch_method; |
| 884 | uint32_t catch_pc; |
| 885 | FindCatchMethodsFromThrow(self, exception_object, &catch_method, &catch_pc); |
| 886 | uint32_t dex_pc = 0; |
| 887 | art::ArtMethod* method = self->GetCurrentMethod(&dex_pc, |
Andreas Gampe | 6e89776 | 2018-10-16 13:09:32 -0700 | [diff] [blame] | 888 | /* check_suspended= */ true, |
| 889 | /* abort_on_error= */ art::kIsDebugBuild); |
Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame] | 890 | ScopedLocalRef<jobject> exception(jnienv, |
| 891 | AddLocalRef<jobject>(jnienv, exception_object.Get())); |
| 892 | RunEventCallback<ArtJvmtiEvent::kException>( |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 893 | event_handler_, |
Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame] | 894 | self, |
| 895 | jnienv, |
| 896 | art::jni::EncodeArtMethod(method), |
| 897 | static_cast<jlocation>(dex_pc), |
| 898 | exception.get(), |
| 899 | art::jni::EncodeArtMethod(catch_method), |
| 900 | static_cast<jlocation>(catch_pc)); |
| 901 | } |
| 902 | return; |
| 903 | } |
| 904 | |
| 905 | // Call-back when an exception is handled. |
| 906 | void ExceptionHandled(art::Thread* self, art::Handle<art::mirror::Throwable> exception_object) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 907 | REQUIRES_SHARED(art::Locks::mutator_lock_) override { |
Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame] | 908 | // Since the exception has already been handled there shouldn't be one pending. |
| 909 | DCHECK(!self->IsExceptionPending()); |
| 910 | if (event_handler_->IsEventEnabledAnywhere(ArtJvmtiEvent::kExceptionCatch)) { |
| 911 | art::JNIEnvExt* jnienv = self->GetJniEnv(); |
| 912 | uint32_t dex_pc; |
| 913 | art::ArtMethod* method = self->GetCurrentMethod(&dex_pc, |
Andreas Gampe | 6e89776 | 2018-10-16 13:09:32 -0700 | [diff] [blame] | 914 | /* check_suspended= */ true, |
| 915 | /* abort_on_error= */ art::kIsDebugBuild); |
Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame] | 916 | ScopedLocalRef<jobject> exception(jnienv, |
| 917 | AddLocalRef<jobject>(jnienv, exception_object.Get())); |
| 918 | RunEventCallback<ArtJvmtiEvent::kExceptionCatch>( |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 919 | event_handler_, |
Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame] | 920 | self, |
| 921 | jnienv, |
| 922 | art::jni::EncodeArtMethod(method), |
| 923 | static_cast<jlocation>(dex_pc), |
| 924 | exception.get()); |
| 925 | } |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 926 | return; |
| 927 | } |
| 928 | |
| 929 | // Call-back for when we execute a branch. |
| 930 | void Branch(art::Thread* self ATTRIBUTE_UNUSED, |
| 931 | art::ArtMethod* method ATTRIBUTE_UNUSED, |
| 932 | uint32_t dex_pc ATTRIBUTE_UNUSED, |
| 933 | int32_t dex_pc_offset ATTRIBUTE_UNUSED) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 934 | REQUIRES_SHARED(art::Locks::mutator_lock_) override { |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 935 | return; |
| 936 | } |
| 937 | |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 938 | private: |
| 939 | EventHandler* const event_handler_; |
| 940 | }; |
| 941 | |
| 942 | static uint32_t GetInstrumentationEventsFor(ArtJvmtiEvent event) { |
| 943 | switch (event) { |
| 944 | case ArtJvmtiEvent::kMethodEntry: |
| 945 | return art::instrumentation::Instrumentation::kMethodEntered; |
| 946 | case ArtJvmtiEvent::kMethodExit: |
| 947 | return art::instrumentation::Instrumentation::kMethodExited | |
| 948 | art::instrumentation::Instrumentation::kMethodUnwind; |
Alex Light | 084fa37 | 2017-06-16 08:58:34 -0700 | [diff] [blame] | 949 | case ArtJvmtiEvent::kFieldModification: |
| 950 | return art::instrumentation::Instrumentation::kFieldWritten; |
| 951 | case ArtJvmtiEvent::kFieldAccess: |
| 952 | return art::instrumentation::Instrumentation::kFieldRead; |
Alex Light | a26e349 | 2017-06-27 17:55:37 -0700 | [diff] [blame] | 953 | case ArtJvmtiEvent::kBreakpoint: |
| 954 | case ArtJvmtiEvent::kSingleStep: |
| 955 | return art::instrumentation::Instrumentation::kDexPcMoved; |
Alex Light | e814f9d | 2017-07-31 16:14:39 -0700 | [diff] [blame] | 956 | case ArtJvmtiEvent::kFramePop: |
| 957 | return art::instrumentation::Instrumentation::kWatchedFramePop; |
Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame] | 958 | case ArtJvmtiEvent::kException: |
| 959 | return art::instrumentation::Instrumentation::kExceptionThrown; |
| 960 | case ArtJvmtiEvent::kExceptionCatch: |
| 961 | return art::instrumentation::Instrumentation::kExceptionHandled; |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 962 | default: |
| 963 | LOG(FATAL) << "Unknown event "; |
Elliott Hughes | c1896c9 | 2018-11-29 11:33:18 -0800 | [diff] [blame] | 964 | UNREACHABLE(); |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 965 | } |
| 966 | } |
| 967 | |
Nicolas Geoffray | ad344b6 | 2019-03-09 17:49:52 +0000 | [diff] [blame^] | 968 | static bool EventNeedsFullDeopt(ArtJvmtiEvent event) { |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 969 | switch (event) { |
| 970 | case ArtJvmtiEvent::kBreakpoint: |
| 971 | case ArtJvmtiEvent::kException: |
Nicolas Geoffray | ad344b6 | 2019-03-09 17:49:52 +0000 | [diff] [blame^] | 972 | return false; |
| 973 | // TODO We should support more of these or at least do something to make them discriminate by |
| 974 | // thread. |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 975 | case ArtJvmtiEvent::kMethodEntry: |
Alex Light | d7da314 | 2018-07-18 15:39:16 +0000 | [diff] [blame] | 976 | case ArtJvmtiEvent::kExceptionCatch: |
David Srbecky | d25eb2c | 2018-07-19 12:17:04 +0000 | [diff] [blame] | 977 | case ArtJvmtiEvent::kMethodExit: |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 978 | case ArtJvmtiEvent::kFieldModification: |
| 979 | case ArtJvmtiEvent::kFieldAccess: |
| 980 | case ArtJvmtiEvent::kSingleStep: |
Nicolas Geoffray | ad344b6 | 2019-03-09 17:49:52 +0000 | [diff] [blame^] | 981 | case ArtJvmtiEvent::kFramePop: |
| 982 | return true; |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 983 | default: |
| 984 | LOG(FATAL) << "Unexpected event type!"; |
| 985 | UNREACHABLE(); |
| 986 | } |
| 987 | } |
| 988 | |
Nicolas Geoffray | ad344b6 | 2019-03-09 17:49:52 +0000 | [diff] [blame^] | 989 | void EventHandler::SetupTraceListener(JvmtiMethodTraceListener* listener, |
| 990 | ArtJvmtiEvent event, |
| 991 | bool enable) { |
| 992 | bool needs_full_deopt = EventNeedsFullDeopt(event); |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 993 | // Make sure we can deopt. |
| 994 | { |
| 995 | art::ScopedObjectAccess soa(art::Thread::Current()); |
| 996 | DeoptManager* deopt_manager = DeoptManager::Get(); |
| 997 | if (enable) { |
| 998 | deopt_manager->AddDeoptimizationRequester(); |
Nicolas Geoffray | ad344b6 | 2019-03-09 17:49:52 +0000 | [diff] [blame^] | 999 | if (needs_full_deopt) { |
| 1000 | deopt_manager->AddDeoptimizeAllMethods(); |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 1001 | } |
| 1002 | } else { |
Nicolas Geoffray | ad344b6 | 2019-03-09 17:49:52 +0000 | [diff] [blame^] | 1003 | if (needs_full_deopt) { |
| 1004 | deopt_manager->RemoveDeoptimizeAllMethods(); |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 1005 | } |
| 1006 | deopt_manager->RemoveDeoptimizationRequester(); |
| 1007 | } |
| 1008 | } |
| 1009 | |
| 1010 | // Add the actual listeners. |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 1011 | uint32_t new_events = GetInstrumentationEventsFor(event); |
Alex Light | f6df1b5 | 2017-11-29 14:46:53 -0800 | [diff] [blame] | 1012 | if (new_events == art::instrumentation::Instrumentation::kDexPcMoved) { |
| 1013 | // Need to skip adding the listeners if the event is breakpoint/single-step since those events |
| 1014 | // share the same art-instrumentation underlying event. We need to give them their own deopt |
| 1015 | // request though so the test waits until here. |
| 1016 | DCHECK(event == ArtJvmtiEvent::kBreakpoint || event == ArtJvmtiEvent::kSingleStep); |
| 1017 | ArtJvmtiEvent other = event == ArtJvmtiEvent::kBreakpoint ? ArtJvmtiEvent::kSingleStep |
| 1018 | : ArtJvmtiEvent::kBreakpoint; |
| 1019 | if (IsEventEnabledAnywhere(other)) { |
| 1020 | // The event needs to be kept around/is already enabled by the other jvmti event that uses the |
| 1021 | // same instrumentation event. |
Nicolas Geoffray | ad344b6 | 2019-03-09 17:49:52 +0000 | [diff] [blame^] | 1022 | return; |
Alex Light | f6df1b5 | 2017-11-29 14:46:53 -0800 | [diff] [blame] | 1023 | } |
| 1024 | } |
| 1025 | art::ScopedThreadStateChange stsc(art::Thread::Current(), art::ThreadState::kNative); |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 1026 | art::instrumentation::Instrumentation* instr = art::Runtime::Current()->GetInstrumentation(); |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 1027 | art::ScopedSuspendAll ssa("jvmti method tracing installation"); |
| 1028 | if (enable) { |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 1029 | instr->AddListener(listener, new_events); |
| 1030 | } else { |
| 1031 | instr->RemoveListener(listener, new_events); |
| 1032 | } |
| 1033 | } |
| 1034 | |
Alex Light | 0a5ec3d | 2017-07-25 16:50:26 -0700 | [diff] [blame] | 1035 | // Makes sure that all compiled methods are AsyncDeoptimizable so we can deoptimize (and force to |
| 1036 | // the switch interpreter) when we try to get or set a local variable. |
Alex Light | bebd7bd | 2017-07-25 14:05:52 -0700 | [diff] [blame] | 1037 | void EventHandler::HandleLocalAccessCapabilityAdded() { |
Alex Light | 0a5ec3d | 2017-07-25 16:50:26 -0700 | [diff] [blame] | 1038 | class UpdateEntryPointsClassVisitor : public art::ClassVisitor { |
| 1039 | public: |
| 1040 | explicit UpdateEntryPointsClassVisitor(art::Runtime* runtime) |
| 1041 | : runtime_(runtime) {} |
| 1042 | |
| 1043 | bool operator()(art::ObjPtr<art::mirror::Class> klass) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 1044 | override REQUIRES(art::Locks::mutator_lock_) { |
Alex Light | a567deb | 2017-10-10 16:44:11 -0700 | [diff] [blame] | 1045 | if (!klass->IsLoaded()) { |
| 1046 | // Skip classes that aren't loaded since they might not have fully allocated and initialized |
| 1047 | // their methods. Furthemore since the jvmti-plugin must have been loaded by this point |
| 1048 | // these methods will definitately be using debuggable code. |
| 1049 | return true; |
| 1050 | } |
Alex Light | 0a5ec3d | 2017-07-25 16:50:26 -0700 | [diff] [blame] | 1051 | for (auto& m : klass->GetMethods(art::kRuntimePointerSize)) { |
| 1052 | const void* code = m.GetEntryPointFromQuickCompiledCode(); |
| 1053 | if (m.IsNative() || m.IsProxyMethod()) { |
| 1054 | continue; |
| 1055 | } else if (!runtime_->GetClassLinker()->IsQuickToInterpreterBridge(code) && |
| 1056 | !runtime_->IsAsyncDeoptimizeable(reinterpret_cast<uintptr_t>(code))) { |
| 1057 | runtime_->GetInstrumentation()->UpdateMethodsCodeToInterpreterEntryPoint(&m); |
| 1058 | } |
| 1059 | } |
| 1060 | return true; |
| 1061 | } |
| 1062 | |
| 1063 | private: |
| 1064 | art::Runtime* runtime_; |
| 1065 | }; |
| 1066 | art::ScopedObjectAccess soa(art::Thread::Current()); |
| 1067 | UpdateEntryPointsClassVisitor visitor(art::Runtime::Current()); |
| 1068 | art::Runtime::Current()->GetClassLinker()->VisitClasses(&visitor); |
Alex Light | bebd7bd | 2017-07-25 14:05:52 -0700 | [diff] [blame] | 1069 | } |
| 1070 | |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 1071 | bool EventHandler::OtherMonitorEventsEnabledAnywhere(ArtJvmtiEvent event) { |
| 1072 | std::array<ArtJvmtiEvent, 4> events { |
| 1073 | { |
| 1074 | ArtJvmtiEvent::kMonitorContendedEnter, |
| 1075 | ArtJvmtiEvent::kMonitorContendedEntered, |
| 1076 | ArtJvmtiEvent::kMonitorWait, |
| 1077 | ArtJvmtiEvent::kMonitorWaited |
| 1078 | } |
| 1079 | }; |
| 1080 | for (ArtJvmtiEvent e : events) { |
| 1081 | if (e != event && IsEventEnabledAnywhere(e)) { |
| 1082 | return true; |
| 1083 | } |
| 1084 | } |
| 1085 | return false; |
| 1086 | } |
| 1087 | |
Nicolas Geoffray | ad344b6 | 2019-03-09 17:49:52 +0000 | [diff] [blame^] | 1088 | void EventHandler::SetupFramePopTraceListener(bool enable) { |
Alex Light | f5d5eb1 | 2018-03-06 15:13:59 -0800 | [diff] [blame] | 1089 | if (enable) { |
| 1090 | frame_pop_enabled = true; |
Nicolas Geoffray | ad344b6 | 2019-03-09 17:49:52 +0000 | [diff] [blame^] | 1091 | SetupTraceListener(method_trace_listener_.get(), ArtJvmtiEvent::kFramePop, enable); |
Alex Light | f5d5eb1 | 2018-03-06 15:13:59 -0800 | [diff] [blame] | 1092 | } else { |
| 1093 | // remove the listener if we have no outstanding frames. |
| 1094 | { |
| 1095 | art::ReaderMutexLock mu(art::Thread::Current(), envs_lock_); |
Nicolas Geoffray | ad344b6 | 2019-03-09 17:49:52 +0000 | [diff] [blame^] | 1096 | for (ArtJvmTiEnv* env : envs) { |
Alex Light | f5d5eb1 | 2018-03-06 15:13:59 -0800 | [diff] [blame] | 1097 | art::ReaderMutexLock event_mu(art::Thread::Current(), env->event_info_mutex_); |
| 1098 | if (!env->notify_frames.empty()) { |
| 1099 | // Leaving FramePop listener since there are unsent FramePop events. |
Nicolas Geoffray | ad344b6 | 2019-03-09 17:49:52 +0000 | [diff] [blame^] | 1100 | return; |
Alex Light | f5d5eb1 | 2018-03-06 15:13:59 -0800 | [diff] [blame] | 1101 | } |
| 1102 | } |
| 1103 | frame_pop_enabled = false; |
| 1104 | } |
Nicolas Geoffray | ad344b6 | 2019-03-09 17:49:52 +0000 | [diff] [blame^] | 1105 | SetupTraceListener(method_trace_listener_.get(), ArtJvmtiEvent::kFramePop, enable); |
Alex Light | f5d5eb1 | 2018-03-06 15:13:59 -0800 | [diff] [blame] | 1106 | } |
| 1107 | } |
| 1108 | |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 1109 | // Handle special work for the given event type, if necessary. |
Nicolas Geoffray | ad344b6 | 2019-03-09 17:49:52 +0000 | [diff] [blame^] | 1110 | void EventHandler::HandleEventType(ArtJvmtiEvent event, bool enable) { |
Andreas Gampe | 9b8c588 | 2016-10-21 15:27:46 -0700 | [diff] [blame] | 1111 | switch (event) { |
Alex Light | 8c2b929 | 2017-11-09 13:21:01 -0800 | [diff] [blame] | 1112 | case ArtJvmtiEvent::kDdmPublishChunk: |
| 1113 | SetupDdmTracking(ddm_listener_.get(), enable); |
Nicolas Geoffray | ad344b6 | 2019-03-09 17:49:52 +0000 | [diff] [blame^] | 1114 | return; |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 1115 | case ArtJvmtiEvent::kVmObjectAlloc: |
Andreas Gampe | 9b8c588 | 2016-10-21 15:27:46 -0700 | [diff] [blame] | 1116 | SetupObjectAllocationTracking(alloc_listener_.get(), enable); |
Nicolas Geoffray | ad344b6 | 2019-03-09 17:49:52 +0000 | [diff] [blame^] | 1117 | return; |
| 1118 | |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 1119 | case ArtJvmtiEvent::kGarbageCollectionStart: |
| 1120 | case ArtJvmtiEvent::kGarbageCollectionFinish: |
Andreas Gampe | 9b8c588 | 2016-10-21 15:27:46 -0700 | [diff] [blame] | 1121 | SetupGcPauseTracking(gc_pause_listener_.get(), event, enable); |
Nicolas Geoffray | ad344b6 | 2019-03-09 17:49:52 +0000 | [diff] [blame^] | 1122 | return; |
Alex Light | f5d5eb1 | 2018-03-06 15:13:59 -0800 | [diff] [blame] | 1123 | // FramePop can never be disabled once it's been turned on if it was turned off with outstanding |
| 1124 | // pop-events since we would either need to deal with dangling pointers or have missed events. |
Alex Light | e814f9d | 2017-07-31 16:14:39 -0700 | [diff] [blame] | 1125 | case ArtJvmtiEvent::kFramePop: |
Alex Light | f5d5eb1 | 2018-03-06 15:13:59 -0800 | [diff] [blame] | 1126 | if (enable && frame_pop_enabled) { |
| 1127 | // The frame-pop event was held on by pending events so we don't need to do anything. |
Alex Light | e814f9d | 2017-07-31 16:14:39 -0700 | [diff] [blame] | 1128 | break; |
| 1129 | } else { |
Nicolas Geoffray | ad344b6 | 2019-03-09 17:49:52 +0000 | [diff] [blame^] | 1130 | SetupFramePopTraceListener(enable); |
| 1131 | break; |
Alex Light | e814f9d | 2017-07-31 16:14:39 -0700 | [diff] [blame] | 1132 | } |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 1133 | case ArtJvmtiEvent::kMethodEntry: |
| 1134 | case ArtJvmtiEvent::kMethodExit: |
Alex Light | 084fa37 | 2017-06-16 08:58:34 -0700 | [diff] [blame] | 1135 | case ArtJvmtiEvent::kFieldAccess: |
| 1136 | case ArtJvmtiEvent::kFieldModification: |
Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame] | 1137 | case ArtJvmtiEvent::kException: |
| 1138 | case ArtJvmtiEvent::kExceptionCatch: |
Alex Light | f6df1b5 | 2017-11-29 14:46:53 -0800 | [diff] [blame] | 1139 | case ArtJvmtiEvent::kBreakpoint: |
| 1140 | case ArtJvmtiEvent::kSingleStep: |
Nicolas Geoffray | ad344b6 | 2019-03-09 17:49:52 +0000 | [diff] [blame^] | 1141 | SetupTraceListener(method_trace_listener_.get(), event, enable); |
| 1142 | return; |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 1143 | case ArtJvmtiEvent::kMonitorContendedEnter: |
| 1144 | case ArtJvmtiEvent::kMonitorContendedEntered: |
| 1145 | case ArtJvmtiEvent::kMonitorWait: |
| 1146 | case ArtJvmtiEvent::kMonitorWaited: |
| 1147 | if (!OtherMonitorEventsEnabledAnywhere(event)) { |
Charles Munger | 5cc0e75 | 2018-11-09 12:30:46 -0800 | [diff] [blame] | 1148 | SetupMonitorListener(monitor_listener_.get(), park_listener_.get(), enable); |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 1149 | } |
Nicolas Geoffray | ad344b6 | 2019-03-09 17:49:52 +0000 | [diff] [blame^] | 1150 | return; |
Andreas Gampe | 9b8c588 | 2016-10-21 15:27:46 -0700 | [diff] [blame] | 1151 | default: |
| 1152 | break; |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 1153 | } |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 1154 | } |
| 1155 | |
Alex Light | 9db679d | 2017-01-25 15:28:04 -0800 | [diff] [blame] | 1156 | // Checks to see if the env has the capabilities associated with the given event. |
| 1157 | static bool HasAssociatedCapability(ArtJvmTiEnv* env, |
| 1158 | ArtJvmtiEvent event) { |
| 1159 | jvmtiCapabilities caps = env->capabilities; |
| 1160 | switch (event) { |
| 1161 | case ArtJvmtiEvent::kBreakpoint: |
| 1162 | return caps.can_generate_breakpoint_events == 1; |
| 1163 | |
| 1164 | case ArtJvmtiEvent::kCompiledMethodLoad: |
| 1165 | case ArtJvmtiEvent::kCompiledMethodUnload: |
| 1166 | return caps.can_generate_compiled_method_load_events == 1; |
| 1167 | |
| 1168 | case ArtJvmtiEvent::kException: |
| 1169 | case ArtJvmtiEvent::kExceptionCatch: |
| 1170 | return caps.can_generate_exception_events == 1; |
| 1171 | |
| 1172 | case ArtJvmtiEvent::kFieldAccess: |
| 1173 | return caps.can_generate_field_access_events == 1; |
| 1174 | |
| 1175 | case ArtJvmtiEvent::kFieldModification: |
| 1176 | return caps.can_generate_field_modification_events == 1; |
| 1177 | |
| 1178 | case ArtJvmtiEvent::kFramePop: |
| 1179 | return caps.can_generate_frame_pop_events == 1; |
| 1180 | |
| 1181 | case ArtJvmtiEvent::kGarbageCollectionStart: |
| 1182 | case ArtJvmtiEvent::kGarbageCollectionFinish: |
| 1183 | return caps.can_generate_garbage_collection_events == 1; |
| 1184 | |
| 1185 | case ArtJvmtiEvent::kMethodEntry: |
| 1186 | return caps.can_generate_method_entry_events == 1; |
| 1187 | |
| 1188 | case ArtJvmtiEvent::kMethodExit: |
| 1189 | return caps.can_generate_method_exit_events == 1; |
| 1190 | |
| 1191 | case ArtJvmtiEvent::kMonitorContendedEnter: |
| 1192 | case ArtJvmtiEvent::kMonitorContendedEntered: |
| 1193 | case ArtJvmtiEvent::kMonitorWait: |
| 1194 | case ArtJvmtiEvent::kMonitorWaited: |
| 1195 | return caps.can_generate_monitor_events == 1; |
| 1196 | |
| 1197 | case ArtJvmtiEvent::kNativeMethodBind: |
| 1198 | return caps.can_generate_native_method_bind_events == 1; |
| 1199 | |
| 1200 | case ArtJvmtiEvent::kObjectFree: |
| 1201 | return caps.can_generate_object_free_events == 1; |
| 1202 | |
| 1203 | case ArtJvmtiEvent::kSingleStep: |
| 1204 | return caps.can_generate_single_step_events == 1; |
| 1205 | |
| 1206 | case ArtJvmtiEvent::kVmObjectAlloc: |
| 1207 | return caps.can_generate_vm_object_alloc_events == 1; |
| 1208 | |
| 1209 | default: |
| 1210 | return true; |
| 1211 | } |
| 1212 | } |
| 1213 | |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 1214 | jvmtiError EventHandler::SetEvent(ArtJvmTiEnv* env, |
Nicolas Geoffray | ad344b6 | 2019-03-09 17:49:52 +0000 | [diff] [blame^] | 1215 | art::Thread* thread, |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 1216 | ArtJvmtiEvent event, |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 1217 | jvmtiEventMode mode) { |
Nicolas Geoffray | ad344b6 | 2019-03-09 17:49:52 +0000 | [diff] [blame^] | 1218 | if (thread != nullptr) { |
| 1219 | art::ThreadState state = thread->GetState(); |
| 1220 | if (state == art::ThreadState::kStarting || |
| 1221 | state == art::ThreadState::kTerminated || |
| 1222 | thread->IsStillStarting()) { |
| 1223 | return ERR(THREAD_NOT_ALIVE); |
| 1224 | } |
| 1225 | if (!IsThreadControllable(event)) { |
| 1226 | return ERR(ILLEGAL_ARGUMENT); |
| 1227 | } |
| 1228 | } |
| 1229 | |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 1230 | if (mode != JVMTI_ENABLE && mode != JVMTI_DISABLE) { |
| 1231 | return ERR(ILLEGAL_ARGUMENT); |
| 1232 | } |
| 1233 | |
| 1234 | if (!EventMask::EventIsInRange(event)) { |
| 1235 | return ERR(INVALID_EVENT_TYPE); |
| 1236 | } |
| 1237 | |
Alex Light | 9db679d | 2017-01-25 15:28:04 -0800 | [diff] [blame] | 1238 | if (!HasAssociatedCapability(env, event)) { |
| 1239 | return ERR(MUST_POSSESS_CAPABILITY); |
| 1240 | } |
| 1241 | |
Alex Light | 74c8440 | 2017-11-29 15:26:38 -0800 | [diff] [blame] | 1242 | bool old_state; |
| 1243 | bool new_state; |
Andreas Gampe | 8b862ff | 2016-10-17 17:49:59 -0700 | [diff] [blame] | 1244 | |
Alex Light | 74c8440 | 2017-11-29 15:26:38 -0800 | [diff] [blame] | 1245 | { |
| 1246 | // Change the event masks atomically. |
| 1247 | art::Thread* self = art::Thread::Current(); |
Alex Light | 2a96fe8 | 2018-01-22 17:45:02 -0800 | [diff] [blame] | 1248 | art::WriterMutexLock mu(self, envs_lock_); |
Alex Light | 74c8440 | 2017-11-29 15:26:38 -0800 | [diff] [blame] | 1249 | art::WriterMutexLock mu_env_info(self, env->event_info_mutex_); |
| 1250 | old_state = global_mask.Test(event); |
| 1251 | if (mode == JVMTI_ENABLE) { |
Nicolas Geoffray | ad344b6 | 2019-03-09 17:49:52 +0000 | [diff] [blame^] | 1252 | env->event_masks.EnableEvent(env, thread, event); |
Alex Light | 74c8440 | 2017-11-29 15:26:38 -0800 | [diff] [blame] | 1253 | global_mask.Set(event); |
| 1254 | new_state = true; |
| 1255 | } else { |
| 1256 | DCHECK_EQ(mode, JVMTI_DISABLE); |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 1257 | |
Nicolas Geoffray | ad344b6 | 2019-03-09 17:49:52 +0000 | [diff] [blame^] | 1258 | env->event_masks.DisableEvent(env, thread, event); |
Alex Light | 74c8440 | 2017-11-29 15:26:38 -0800 | [diff] [blame] | 1259 | RecalculateGlobalEventMaskLocked(event); |
| 1260 | new_state = global_mask.Test(event); |
| 1261 | } |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 1262 | } |
| 1263 | |
| 1264 | // Handle any special work required for the event type. |
Andreas Gampe | 8b862ff | 2016-10-17 17:49:59 -0700 | [diff] [blame] | 1265 | if (new_state != old_state) { |
Nicolas Geoffray | ad344b6 | 2019-03-09 17:49:52 +0000 | [diff] [blame^] | 1266 | HandleEventType(event, mode == JVMTI_ENABLE); |
Andreas Gampe | 8b862ff | 2016-10-17 17:49:59 -0700 | [diff] [blame] | 1267 | } |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 1268 | |
| 1269 | return ERR(NONE); |
| 1270 | } |
| 1271 | |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 1272 | void EventHandler::HandleBreakpointEventsChanged(bool added) { |
| 1273 | if (added) { |
| 1274 | DeoptManager::Get()->AddDeoptimizationRequester(); |
| 1275 | } else { |
| 1276 | DeoptManager::Get()->RemoveDeoptimizationRequester(); |
| 1277 | } |
| 1278 | } |
| 1279 | |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 1280 | void EventHandler::Shutdown() { |
| 1281 | // Need to remove the method_trace_listener_ if it's there. |
| 1282 | art::Thread* self = art::Thread::Current(); |
| 1283 | art::gc::ScopedGCCriticalSection gcs(self, |
| 1284 | art::gc::kGcCauseInstrumentation, |
| 1285 | art::gc::kCollectorTypeInstrumentation); |
| 1286 | art::ScopedSuspendAll ssa("jvmti method tracing uninstallation"); |
| 1287 | // Just remove every possible event. |
| 1288 | art::Runtime::Current()->GetInstrumentation()->RemoveListener(method_trace_listener_.get(), ~0); |
| 1289 | } |
| 1290 | |
Alex Light | 0e84118 | 2018-02-12 17:42:50 +0000 | [diff] [blame] | 1291 | EventHandler::EventHandler() |
| 1292 | : envs_lock_("JVMTI Environment List Lock", art::LockLevel::kTopLockLevel), |
| 1293 | frame_pop_enabled(false) { |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 1294 | alloc_listener_.reset(new JvmtiAllocationListener(this)); |
Alex Light | 8c2b929 | 2017-11-09 13:21:01 -0800 | [diff] [blame] | 1295 | ddm_listener_.reset(new JvmtiDdmChunkListener(this)); |
Andreas Gampe | 9b8c588 | 2016-10-21 15:27:46 -0700 | [diff] [blame] | 1296 | gc_pause_listener_.reset(new JvmtiGcPauseListener(this)); |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 1297 | method_trace_listener_.reset(new JvmtiMethodTraceListener(this)); |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 1298 | monitor_listener_.reset(new JvmtiMonitorListener(this)); |
Charles Munger | 5cc0e75 | 2018-11-09 12:30:46 -0800 | [diff] [blame] | 1299 | park_listener_.reset(new JvmtiParkListener(this)); |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 1300 | } |
| 1301 | |
| 1302 | EventHandler::~EventHandler() { |
| 1303 | } |
| 1304 | |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 1305 | } // namespace openjdkjvmti |