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 | |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 32 | #include <android-base/thread_annotations.h> |
| 33 | |
Alex Light | 986914b | 2019-11-19 01:12:25 +0000 | [diff] [blame^] | 34 | #include "alloc_manager.h" |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 35 | #include "base/locks.h" |
| 36 | #include "base/mutex.h" |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 37 | #include "events-inl.h" |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 38 | |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 39 | #include <array> |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 40 | #include <functional> |
Charles Munger | 5cc0e75 | 2018-11-09 12:30:46 -0800 | [diff] [blame] | 41 | #include <sys/time.h> |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 42 | |
Andreas Gampe | e5d2398 | 2019-01-08 10:34:26 -0800 | [diff] [blame] | 43 | #include "arch/context.h" |
Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 44 | #include "art_field-inl.h" |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 45 | #include "art_jvmti.h" |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 46 | #include "art_method-inl.h" |
Alex Light | 6683446 | 2019-04-08 16:28:29 +0000 | [diff] [blame] | 47 | #include "base/mutex.h" |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 48 | #include "deopt_manager.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 49 | #include "dex/dex_file_types.h" |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 50 | #include "events.h" |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 51 | #include "gc/allocation_listener.h" |
Andreas Gampe | 9b8c588 | 2016-10-21 15:27:46 -0700 | [diff] [blame] | 52 | #include "gc/gc_pause_listener.h" |
| 53 | #include "gc/heap.h" |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 54 | #include "gc/scoped_gc_critical_section.h" |
Andreas Gampe | c15a2f4 | 2017-04-21 12:09:39 -0700 | [diff] [blame] | 55 | #include "handle_scope-inl.h" |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 56 | #include "indirect_reference_table.h" |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 57 | #include "instrumentation.h" |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 58 | #include "interpreter/shadow_frame.h" |
Vladimir Marko | a3ad0cd | 2018-05-04 10:06:38 +0100 | [diff] [blame] | 59 | #include "jni/jni_env_ext-inl.h" |
| 60 | #include "jni/jni_internal.h" |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 61 | #include "jvalue-inl.h" |
| 62 | #include "jvalue.h" |
| 63 | #include "jvmti.h" |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 64 | #include "mirror/class.h" |
Andreas Gampe | c15a2f4 | 2017-04-21 12:09:39 -0700 | [diff] [blame] | 65 | #include "mirror/object-inl.h" |
Vladimir Marko | f52d92f | 2019-03-29 12:33:02 +0000 | [diff] [blame] | 66 | #include "monitor-inl.h" |
Andreas Gampe | 373a9b5 | 2017-10-18 09:01:57 -0700 | [diff] [blame] | 67 | #include "nativehelper/scoped_local_ref.h" |
Alex Light | 55eccdf | 2019-10-07 13:51:13 +0000 | [diff] [blame] | 68 | #include "reflective_handle.h" |
| 69 | #include "reflective_handle_scope-inl.h" |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 70 | #include "runtime.h" |
Andreas Gampe | c02685c | 2016-10-17 17:40:27 -0700 | [diff] [blame] | 71 | #include "scoped_thread_state_change-inl.h" |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 72 | #include "scoped_thread_state_change.h" |
Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame] | 73 | #include "stack.h" |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 74 | #include "thread.h" |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 75 | #include "thread-inl.h" |
| 76 | #include "thread_list.h" |
| 77 | #include "ti_phase.h" |
Alex Light | 679dec1 | 2019-04-08 16:30:14 +0000 | [diff] [blame] | 78 | #include "ti_thread.h" |
Charles Munger | 5cc0e75 | 2018-11-09 12:30:46 -0800 | [diff] [blame] | 79 | #include "well_known_classes.h" |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 80 | |
| 81 | namespace openjdkjvmti { |
| 82 | |
Alex Light | 8c2b929 | 2017-11-09 13:21:01 -0800 | [diff] [blame] | 83 | void ArtJvmtiEventCallbacks::CopyExtensionsFrom(const ArtJvmtiEventCallbacks* cb) { |
| 84 | if (art::kIsDebugBuild) { |
| 85 | ArtJvmtiEventCallbacks clean; |
| 86 | DCHECK_EQ(memcmp(&clean, this, sizeof(clean)), 0) |
| 87 | << "CopyExtensionsFrom called with initialized eventsCallbacks!"; |
| 88 | } |
| 89 | if (cb != nullptr) { |
| 90 | memcpy(this, cb, sizeof(*this)); |
| 91 | } else { |
| 92 | memset(this, 0, sizeof(*this)); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | jvmtiError ArtJvmtiEventCallbacks::Set(jint index, jvmtiExtensionEvent cb) { |
| 97 | switch (index) { |
Alex Light | 72d7e94 | 2019-07-23 13:10:20 -0700 | [diff] [blame] | 98 | case static_cast<jint>(ArtJvmtiEvent::kObsoleteObjectCreated): |
| 99 | ObsoleteObjectCreated = reinterpret_cast<ArtJvmtiEventObsoleteObjectCreated>(cb); |
| 100 | return OK; |
Alex Light | 8c2b929 | 2017-11-09 13:21:01 -0800 | [diff] [blame] | 101 | case static_cast<jint>(ArtJvmtiEvent::kDdmPublishChunk): |
| 102 | DdmPublishChunk = reinterpret_cast<ArtJvmtiEventDdmPublishChunk>(cb); |
| 103 | return OK; |
Alex Light | d55b844 | 2019-10-15 15:46:07 -0700 | [diff] [blame] | 104 | case static_cast<jint>(ArtJvmtiEvent::kStructuralDexFileLoadHook): |
| 105 | StructuralDexFileLoadHook = reinterpret_cast<ArtJvmtiEventStructuralDexFileLoadHook>(cb); |
| 106 | return OK; |
Alex Light | 8c2b929 | 2017-11-09 13:21:01 -0800 | [diff] [blame] | 107 | default: |
| 108 | return ERR(ILLEGAL_ARGUMENT); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | |
| 113 | bool IsExtensionEvent(jint e) { |
| 114 | return e >= static_cast<jint>(ArtJvmtiEvent::kMinEventTypeVal) && |
| 115 | e <= static_cast<jint>(ArtJvmtiEvent::kMaxEventTypeVal) && |
| 116 | IsExtensionEvent(static_cast<ArtJvmtiEvent>(e)); |
| 117 | } |
| 118 | |
| 119 | bool IsExtensionEvent(ArtJvmtiEvent e) { |
| 120 | switch (e) { |
| 121 | case ArtJvmtiEvent::kDdmPublishChunk: |
Alex Light | 72d7e94 | 2019-07-23 13:10:20 -0700 | [diff] [blame] | 122 | case ArtJvmtiEvent::kObsoleteObjectCreated: |
Alex Light | d55b844 | 2019-10-15 15:46:07 -0700 | [diff] [blame] | 123 | case ArtJvmtiEvent::kStructuralDexFileLoadHook: |
Alex Light | 8c2b929 | 2017-11-09 13:21:01 -0800 | [diff] [blame] | 124 | return true; |
| 125 | default: |
| 126 | return false; |
| 127 | } |
| 128 | } |
| 129 | |
Alex Light | 73afd32 | 2017-01-18 11:17:47 -0800 | [diff] [blame] | 130 | bool EventMasks::IsEnabledAnywhere(ArtJvmtiEvent event) { |
| 131 | return global_event_mask.Test(event) || unioned_thread_event_mask.Test(event); |
| 132 | } |
| 133 | |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 134 | EventMask& EventMasks::GetEventMask(art::Thread* thread) { |
| 135 | if (thread == nullptr) { |
| 136 | return global_event_mask; |
| 137 | } |
| 138 | |
| 139 | for (auto& pair : thread_event_masks) { |
| 140 | const UniqueThread& unique_thread = pair.first; |
| 141 | if (unique_thread.first == thread && |
| 142 | unique_thread.second == static_cast<uint32_t>(thread->GetTid())) { |
| 143 | return pair.second; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | // TODO: Remove old UniqueThread with the same pointer, if exists. |
| 148 | |
| 149 | thread_event_masks.emplace_back(UniqueThread(thread, thread->GetTid()), EventMask()); |
| 150 | return thread_event_masks.back().second; |
| 151 | } |
| 152 | |
| 153 | EventMask* EventMasks::GetEventMaskOrNull(art::Thread* thread) { |
| 154 | if (thread == nullptr) { |
| 155 | return &global_event_mask; |
| 156 | } |
| 157 | |
| 158 | for (auto& pair : thread_event_masks) { |
| 159 | const UniqueThread& unique_thread = pair.first; |
| 160 | if (unique_thread.first == thread && |
| 161 | unique_thread.second == static_cast<uint32_t>(thread->GetTid())) { |
| 162 | return &pair.second; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | return nullptr; |
| 167 | } |
| 168 | |
| 169 | |
Alex Light | 74c8440 | 2017-11-29 15:26:38 -0800 | [diff] [blame] | 170 | void EventMasks::EnableEvent(ArtJvmTiEnv* env, art::Thread* thread, ArtJvmtiEvent event) { |
| 171 | DCHECK_EQ(&env->event_masks, this); |
| 172 | env->event_info_mutex_.AssertExclusiveHeld(art::Thread::Current()); |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 173 | DCHECK(EventMask::EventIsInRange(event)); |
| 174 | GetEventMask(thread).Set(event); |
| 175 | if (thread != nullptr) { |
| 176 | unioned_thread_event_mask.Set(event, true); |
| 177 | } |
| 178 | } |
| 179 | |
Alex Light | 74c8440 | 2017-11-29 15:26:38 -0800 | [diff] [blame] | 180 | void EventMasks::DisableEvent(ArtJvmTiEnv* env, art::Thread* thread, ArtJvmtiEvent event) { |
| 181 | DCHECK_EQ(&env->event_masks, this); |
| 182 | env->event_info_mutex_.AssertExclusiveHeld(art::Thread::Current()); |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 183 | DCHECK(EventMask::EventIsInRange(event)); |
| 184 | GetEventMask(thread).Set(event, false); |
| 185 | if (thread != nullptr) { |
| 186 | // Regenerate union for the event. |
| 187 | bool union_value = false; |
| 188 | for (auto& pair : thread_event_masks) { |
| 189 | union_value |= pair.second.Test(event); |
| 190 | if (union_value) { |
| 191 | break; |
| 192 | } |
| 193 | } |
| 194 | unioned_thread_event_mask.Set(event, union_value); |
| 195 | } |
| 196 | } |
| 197 | |
Alex Light | 73afd32 | 2017-01-18 11:17:47 -0800 | [diff] [blame] | 198 | void EventMasks::HandleChangedCapabilities(const jvmtiCapabilities& caps, bool caps_added) { |
| 199 | if (UNLIKELY(caps.can_retransform_classes == 1)) { |
| 200 | // If we are giving this env the retransform classes cap we need to switch all events of |
| 201 | // NonTransformable to Transformable and vice versa. |
| 202 | ArtJvmtiEvent to_remove = caps_added ? ArtJvmtiEvent::kClassFileLoadHookNonRetransformable |
| 203 | : ArtJvmtiEvent::kClassFileLoadHookRetransformable; |
| 204 | ArtJvmtiEvent to_add = caps_added ? ArtJvmtiEvent::kClassFileLoadHookRetransformable |
| 205 | : ArtJvmtiEvent::kClassFileLoadHookNonRetransformable; |
| 206 | if (global_event_mask.Test(to_remove)) { |
| 207 | CHECK(!global_event_mask.Test(to_add)); |
| 208 | global_event_mask.Set(to_remove, false); |
| 209 | global_event_mask.Set(to_add, true); |
| 210 | } |
| 211 | |
| 212 | if (unioned_thread_event_mask.Test(to_remove)) { |
| 213 | CHECK(!unioned_thread_event_mask.Test(to_add)); |
| 214 | unioned_thread_event_mask.Set(to_remove, false); |
| 215 | unioned_thread_event_mask.Set(to_add, true); |
| 216 | } |
| 217 | for (auto thread_mask : thread_event_masks) { |
| 218 | if (thread_mask.second.Test(to_remove)) { |
| 219 | CHECK(!thread_mask.second.Test(to_add)); |
| 220 | thread_mask.second.Set(to_remove, false); |
| 221 | thread_mask.second.Set(to_add, true); |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 227 | void EventHandler::RegisterArtJvmTiEnv(ArtJvmTiEnv* env) { |
Alex Light | 2a96fe8 | 2018-01-22 17:45:02 -0800 | [diff] [blame] | 228 | art::WriterMutexLock mu(art::Thread::Current(), envs_lock_); |
Alex Light | b284f8d | 2017-11-21 00:00:48 +0000 | [diff] [blame] | 229 | envs.push_back(env); |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 230 | } |
| 231 | |
Andreas Gampe | 3a7eb14 | 2017-01-19 21:59:22 -0800 | [diff] [blame] | 232 | void EventHandler::RemoveArtJvmTiEnv(ArtJvmTiEnv* env) { |
Alex Light | 2a96fe8 | 2018-01-22 17:45:02 -0800 | [diff] [blame] | 233 | art::WriterMutexLock mu(art::Thread::Current(), envs_lock_); |
Alex Light | bb76646 | 2017-04-12 16:13:33 -0700 | [diff] [blame] | 234 | // Since we might be currently iterating over the envs list we cannot actually erase elements. |
| 235 | // Instead we will simply replace them with 'nullptr' and skip them manually. |
Andreas Gampe | 3a7eb14 | 2017-01-19 21:59:22 -0800 | [diff] [blame] | 236 | auto it = std::find(envs.begin(), envs.end(), env); |
| 237 | if (it != envs.end()) { |
Alex Light | b284f8d | 2017-11-21 00:00:48 +0000 | [diff] [blame] | 238 | envs.erase(it); |
Andreas Gampe | 3a7eb14 | 2017-01-19 21:59:22 -0800 | [diff] [blame] | 239 | for (size_t i = static_cast<size_t>(ArtJvmtiEvent::kMinEventTypeVal); |
| 240 | i <= static_cast<size_t>(ArtJvmtiEvent::kMaxEventTypeVal); |
| 241 | ++i) { |
Alex Light | b284f8d | 2017-11-21 00:00:48 +0000 | [diff] [blame] | 242 | RecalculateGlobalEventMaskLocked(static_cast<ArtJvmtiEvent>(i)); |
Andreas Gampe | 3a7eb14 | 2017-01-19 21:59:22 -0800 | [diff] [blame] | 243 | } |
| 244 | } |
| 245 | } |
| 246 | |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 247 | static bool IsThreadControllable(ArtJvmtiEvent event) { |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 248 | switch (event) { |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 249 | case ArtJvmtiEvent::kVmInit: |
| 250 | case ArtJvmtiEvent::kVmStart: |
| 251 | case ArtJvmtiEvent::kVmDeath: |
| 252 | case ArtJvmtiEvent::kThreadStart: |
| 253 | case ArtJvmtiEvent::kCompiledMethodLoad: |
| 254 | case ArtJvmtiEvent::kCompiledMethodUnload: |
| 255 | case ArtJvmtiEvent::kDynamicCodeGenerated: |
| 256 | case ArtJvmtiEvent::kDataDumpRequest: |
Alex Light | 72d7e94 | 2019-07-23 13:10:20 -0700 | [diff] [blame] | 257 | case ArtJvmtiEvent::kObsoleteObjectCreated: |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 258 | return false; |
| 259 | |
| 260 | default: |
| 261 | return true; |
| 262 | } |
| 263 | } |
| 264 | |
Alex Light | 9df79b7 | 2017-09-12 08:57:31 -0700 | [diff] [blame] | 265 | template<typename Type> |
Vladimir Marko | f52d92f | 2019-03-29 12:33:02 +0000 | [diff] [blame] | 266 | static Type AddLocalRef(art::JNIEnvExt* e, art::ObjPtr<art::mirror::Object> obj) |
Alex Light | 9df79b7 | 2017-09-12 08:57:31 -0700 | [diff] [blame] | 267 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 268 | return (obj == nullptr) ? nullptr : e->AddLocalReference<Type>(obj); |
| 269 | } |
| 270 | |
| 271 | template<ArtJvmtiEvent kEvent, typename ...Args> |
| 272 | static void RunEventCallback(EventHandler* handler, |
| 273 | art::Thread* self, |
| 274 | art::JNIEnvExt* jnienv, |
| 275 | Args... args) |
| 276 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 277 | ScopedLocalRef<jthread> thread_jni(jnienv, AddLocalRef<jthread>(jnienv, self->GetPeer())); |
| 278 | handler->DispatchEvent<kEvent>(self, |
| 279 | static_cast<JNIEnv*>(jnienv), |
| 280 | thread_jni.get(), |
| 281 | args...); |
| 282 | } |
| 283 | |
Alex Light | 8c2b929 | 2017-11-09 13:21:01 -0800 | [diff] [blame] | 284 | static void SetupDdmTracking(art::DdmCallback* listener, bool enable) { |
| 285 | art::ScopedObjectAccess soa(art::Thread::Current()); |
| 286 | if (enable) { |
| 287 | art::Runtime::Current()->GetRuntimeCallbacks()->AddDdmCallback(listener); |
| 288 | } else { |
| 289 | art::Runtime::Current()->GetRuntimeCallbacks()->RemoveDdmCallback(listener); |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | class JvmtiDdmChunkListener : public art::DdmCallback { |
| 294 | public: |
| 295 | explicit JvmtiDdmChunkListener(EventHandler* handler) : handler_(handler) {} |
| 296 | |
| 297 | void DdmPublishChunk(uint32_t type, const art::ArrayRef<const uint8_t>& data) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 298 | override REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Alex Light | 8c2b929 | 2017-11-09 13:21:01 -0800 | [diff] [blame] | 299 | if (handler_->IsEventEnabledAnywhere(ArtJvmtiEvent::kDdmPublishChunk)) { |
| 300 | art::Thread* self = art::Thread::Current(); |
| 301 | handler_->DispatchEvent<ArtJvmtiEvent::kDdmPublishChunk>( |
| 302 | self, |
| 303 | static_cast<JNIEnv*>(self->GetJniEnv()), |
| 304 | static_cast<jint>(type), |
| 305 | static_cast<jint>(data.size()), |
| 306 | reinterpret_cast<const jbyte*>(data.data())); |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | private: |
| 311 | EventHandler* handler_; |
| 312 | |
| 313 | DISALLOW_COPY_AND_ASSIGN(JvmtiDdmChunkListener); |
| 314 | }; |
| 315 | |
Alex Light | 986914b | 2019-11-19 01:12:25 +0000 | [diff] [blame^] | 316 | class JvmtiEventAllocationListener : public AllocationManager::AllocationCallback { |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 317 | public: |
Alex Light | 986914b | 2019-11-19 01:12:25 +0000 | [diff] [blame^] | 318 | explicit JvmtiEventAllocationListener(EventHandler* handler) : handler_(handler) {} |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 319 | |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 320 | 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] | 321 | override REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 322 | DCHECK_EQ(self, art::Thread::Current()); |
| 323 | |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 324 | if (handler_->IsEventEnabledAnywhere(ArtJvmtiEvent::kVmObjectAlloc)) { |
Mathieu Chartier | a711804 | 2016-10-12 15:45:58 -0700 | [diff] [blame] | 325 | art::StackHandleScope<1> hs(self); |
| 326 | auto h = hs.NewHandleWrapper(obj); |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 327 | // jvmtiEventVMObjectAlloc parameters: |
| 328 | // jvmtiEnv *jvmti_env, |
| 329 | // JNIEnv* jni_env, |
| 330 | // jthread thread, |
| 331 | // jobject object, |
| 332 | // jclass object_klass, |
| 333 | // jlong size |
| 334 | art::JNIEnvExt* jni_env = self->GetJniEnv(); |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 335 | ScopedLocalRef<jobject> object( |
| 336 | jni_env, jni_env->AddLocalReference<jobject>(*obj)); |
| 337 | ScopedLocalRef<jclass> klass( |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 338 | jni_env, jni_env->AddLocalReference<jclass>(obj->Ptr()->GetClass())); |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 339 | |
Alex Light | 9df79b7 | 2017-09-12 08:57:31 -0700 | [diff] [blame] | 340 | RunEventCallback<ArtJvmtiEvent::kVmObjectAlloc>(handler_, |
| 341 | self, |
| 342 | jni_env, |
| 343 | object.get(), |
| 344 | klass.get(), |
| 345 | static_cast<jlong>(byte_count)); |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 346 | } |
| 347 | } |
| 348 | |
| 349 | private: |
| 350 | EventHandler* handler_; |
| 351 | }; |
| 352 | |
Alex Light | 986914b | 2019-11-19 01:12:25 +0000 | [diff] [blame^] | 353 | static void SetupObjectAllocationTracking(bool enable) { |
Andreas Gampe | c02685c | 2016-10-17 17:40:27 -0700 | [diff] [blame] | 354 | // We must not hold the mutator lock here, but if we're in FastJNI, for example, we might. For |
| 355 | // now, do a workaround: (possibly) acquire and release. |
| 356 | art::ScopedObjectAccess soa(art::Thread::Current()); |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 357 | if (enable) { |
Alex Light | 986914b | 2019-11-19 01:12:25 +0000 | [diff] [blame^] | 358 | AllocationManager::Get()->EnableAllocationCallback(soa.Self()); |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 359 | } else { |
Alex Light | 986914b | 2019-11-19 01:12:25 +0000 | [diff] [blame^] | 360 | AllocationManager::Get()->DisableAllocationCallback(soa.Self()); |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 361 | } |
| 362 | } |
| 363 | |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 364 | class JvmtiMonitorListener : public art::MonitorCallback { |
| 365 | public: |
| 366 | explicit JvmtiMonitorListener(EventHandler* handler) : handler_(handler) {} |
| 367 | |
| 368 | void MonitorContendedLocking(art::Monitor* m) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 369 | override REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 370 | if (handler_->IsEventEnabledAnywhere(ArtJvmtiEvent::kMonitorContendedEnter)) { |
| 371 | art::Thread* self = art::Thread::Current(); |
| 372 | art::JNIEnvExt* jnienv = self->GetJniEnv(); |
| 373 | ScopedLocalRef<jobject> mon(jnienv, AddLocalRef<jobject>(jnienv, m->GetObject())); |
| 374 | RunEventCallback<ArtJvmtiEvent::kMonitorContendedEnter>( |
| 375 | handler_, |
| 376 | self, |
| 377 | jnienv, |
| 378 | mon.get()); |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | void MonitorContendedLocked(art::Monitor* m) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 383 | override REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 384 | if (handler_->IsEventEnabledAnywhere(ArtJvmtiEvent::kMonitorContendedEntered)) { |
| 385 | art::Thread* self = art::Thread::Current(); |
| 386 | art::JNIEnvExt* jnienv = self->GetJniEnv(); |
| 387 | ScopedLocalRef<jobject> mon(jnienv, AddLocalRef<jobject>(jnienv, m->GetObject())); |
| 388 | RunEventCallback<ArtJvmtiEvent::kMonitorContendedEntered>( |
| 389 | handler_, |
| 390 | self, |
| 391 | jnienv, |
| 392 | mon.get()); |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | void ObjectWaitStart(art::Handle<art::mirror::Object> obj, int64_t timeout) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 397 | override REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 398 | if (handler_->IsEventEnabledAnywhere(ArtJvmtiEvent::kMonitorWait)) { |
| 399 | art::Thread* self = art::Thread::Current(); |
| 400 | art::JNIEnvExt* jnienv = self->GetJniEnv(); |
| 401 | ScopedLocalRef<jobject> mon(jnienv, AddLocalRef<jobject>(jnienv, obj.Get())); |
| 402 | RunEventCallback<ArtJvmtiEvent::kMonitorWait>( |
| 403 | handler_, |
| 404 | self, |
| 405 | jnienv, |
| 406 | mon.get(), |
| 407 | static_cast<jlong>(timeout)); |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | |
| 412 | // Our interpretation of the spec is that the JVMTI_EVENT_MONITOR_WAITED will be sent immediately |
| 413 | // after a thread has woken up from a sleep caused by a call to Object#wait. If the thread will |
| 414 | // never go to sleep (due to not having the lock, having bad arguments, or having an exception |
| 415 | // propogated from JVMTI_EVENT_MONITOR_WAIT) we will not send this event. |
| 416 | // |
| 417 | // This does not fully match the RI semantics. Specifically, we will not send the |
| 418 | // JVMTI_EVENT_MONITOR_WAITED event in one situation where the RI would, there was an exception in |
| 419 | // the JVMTI_EVENT_MONITOR_WAIT event but otherwise the call was fine. In that case the RI would |
| 420 | // send this event and return without going to sleep. |
| 421 | // |
| 422 | // See b/65558434 for more discussion. |
| 423 | void MonitorWaitFinished(art::Monitor* m, bool timeout) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 424 | override REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 425 | if (handler_->IsEventEnabledAnywhere(ArtJvmtiEvent::kMonitorWaited)) { |
| 426 | art::Thread* self = art::Thread::Current(); |
| 427 | art::JNIEnvExt* jnienv = self->GetJniEnv(); |
| 428 | ScopedLocalRef<jobject> mon(jnienv, AddLocalRef<jobject>(jnienv, m->GetObject())); |
| 429 | RunEventCallback<ArtJvmtiEvent::kMonitorWaited>( |
| 430 | handler_, |
| 431 | self, |
| 432 | jnienv, |
| 433 | mon.get(), |
| 434 | static_cast<jboolean>(timeout)); |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | private: |
| 439 | EventHandler* handler_; |
| 440 | }; |
| 441 | |
Charles Munger | 5cc0e75 | 2018-11-09 12:30:46 -0800 | [diff] [blame] | 442 | class JvmtiParkListener : public art::ParkCallback { |
| 443 | public: |
| 444 | explicit JvmtiParkListener(EventHandler* handler) : handler_(handler) {} |
| 445 | |
| 446 | void ThreadParkStart(bool is_absolute, int64_t timeout) |
| 447 | override REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 448 | if (handler_->IsEventEnabledAnywhere(ArtJvmtiEvent::kMonitorWait)) { |
| 449 | art::Thread* self = art::Thread::Current(); |
| 450 | art::JNIEnvExt* jnienv = self->GetJniEnv(); |
| 451 | art::ArtField* parkBlockerField = art::jni::DecodeArtField( |
| 452 | art::WellKnownClasses::java_lang_Thread_parkBlocker); |
| 453 | art::ObjPtr<art::mirror::Object> blocker_obj = parkBlockerField->GetObj(self->GetPeer()); |
| 454 | if (blocker_obj.IsNull()) { |
| 455 | blocker_obj = self->GetPeer(); |
| 456 | } |
| 457 | int64_t timeout_ms; |
| 458 | if (!is_absolute) { |
| 459 | if (timeout == 0) { |
| 460 | timeout_ms = 0; |
| 461 | } else { |
| 462 | timeout_ms = timeout / 1000000; |
| 463 | if (timeout_ms == 0) { |
| 464 | // If we were instructed to park for a nonzero number of nanoseconds, but not enough |
| 465 | // to be a full millisecond, round up to 1 ms. A nonzero park() call will return |
| 466 | // soon, but a 0 wait or park call will wait indefinitely. |
| 467 | timeout_ms = 1; |
| 468 | } |
| 469 | } |
| 470 | } else { |
| 471 | struct timeval tv; |
| 472 | gettimeofday(&tv, (struct timezone *) nullptr); |
| 473 | int64_t now = tv.tv_sec * 1000LL + tv.tv_usec / 1000; |
| 474 | if (now < timeout) { |
| 475 | timeout_ms = timeout - now; |
| 476 | } else { |
| 477 | // Waiting for 0 ms is an indefinite wait; parking until a time in |
| 478 | // the past or the current time will return immediately, so emulate |
| 479 | // the shortest possible wait event. |
| 480 | timeout_ms = 1; |
| 481 | } |
| 482 | } |
| 483 | ScopedLocalRef<jobject> blocker(jnienv, AddLocalRef<jobject>(jnienv, blocker_obj.Ptr())); |
| 484 | RunEventCallback<ArtJvmtiEvent::kMonitorWait>( |
| 485 | handler_, |
| 486 | self, |
| 487 | jnienv, |
| 488 | blocker.get(), |
| 489 | static_cast<jlong>(timeout_ms)); |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | |
| 494 | // Our interpretation of the spec is that the JVMTI_EVENT_MONITOR_WAITED will be sent immediately |
| 495 | // after a thread has woken up from a sleep caused by a call to Object#wait. If the thread will |
| 496 | // never go to sleep (due to not having the lock, having bad arguments, or having an exception |
| 497 | // propogated from JVMTI_EVENT_MONITOR_WAIT) we will not send this event. |
| 498 | // |
| 499 | // This does not fully match the RI semantics. Specifically, we will not send the |
| 500 | // JVMTI_EVENT_MONITOR_WAITED event in one situation where the RI would, there was an exception in |
| 501 | // the JVMTI_EVENT_MONITOR_WAIT event but otherwise the call was fine. In that case the RI would |
| 502 | // send this event and return without going to sleep. |
| 503 | // |
| 504 | // See b/65558434 for more discussion. |
| 505 | void ThreadParkFinished(bool timeout) override REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 506 | if (handler_->IsEventEnabledAnywhere(ArtJvmtiEvent::kMonitorWaited)) { |
| 507 | art::Thread* self = art::Thread::Current(); |
| 508 | art::JNIEnvExt* jnienv = self->GetJniEnv(); |
| 509 | art::ArtField* parkBlockerField = art::jni::DecodeArtField( |
| 510 | art::WellKnownClasses::java_lang_Thread_parkBlocker); |
| 511 | art::ObjPtr<art::mirror::Object> blocker_obj = parkBlockerField->GetObj(self->GetPeer()); |
| 512 | if (blocker_obj.IsNull()) { |
| 513 | blocker_obj = self->GetPeer(); |
| 514 | } |
| 515 | ScopedLocalRef<jobject> blocker(jnienv, AddLocalRef<jobject>(jnienv, blocker_obj.Ptr())); |
| 516 | RunEventCallback<ArtJvmtiEvent::kMonitorWaited>( |
| 517 | handler_, |
| 518 | self, |
| 519 | jnienv, |
| 520 | blocker.get(), |
| 521 | static_cast<jboolean>(timeout)); |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | private: |
| 526 | EventHandler* handler_; |
| 527 | }; |
| 528 | |
| 529 | 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] | 530 | // We must not hold the mutator lock here, but if we're in FastJNI, for example, we might. For |
| 531 | // now, do a workaround: (possibly) acquire and release. |
| 532 | art::ScopedObjectAccess soa(art::Thread::Current()); |
| 533 | if (enable) { |
Charles Munger | 5cc0e75 | 2018-11-09 12:30:46 -0800 | [diff] [blame] | 534 | art::Runtime::Current()->GetRuntimeCallbacks()->AddMonitorCallback(monitor_listener); |
| 535 | art::Runtime::Current()->GetRuntimeCallbacks()->AddParkCallback(park_listener); |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 536 | } else { |
Charles Munger | 5cc0e75 | 2018-11-09 12:30:46 -0800 | [diff] [blame] | 537 | art::Runtime::Current()->GetRuntimeCallbacks()->RemoveMonitorCallback(monitor_listener); |
| 538 | art::Runtime::Current()->GetRuntimeCallbacks()->RemoveParkCallback(park_listener); |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 539 | } |
| 540 | } |
| 541 | |
Andreas Gampe | 9b8c588 | 2016-10-21 15:27:46 -0700 | [diff] [blame] | 542 | // Report GC pauses (see spec) as GARBAGE_COLLECTION_START and GARBAGE_COLLECTION_END. |
| 543 | class JvmtiGcPauseListener : public art::gc::GcPauseListener { |
| 544 | public: |
| 545 | explicit JvmtiGcPauseListener(EventHandler* handler) |
| 546 | : handler_(handler), |
| 547 | start_enabled_(false), |
| 548 | finish_enabled_(false) {} |
| 549 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 550 | void StartPause() override { |
Alex Light | b284f8d | 2017-11-21 00:00:48 +0000 | [diff] [blame] | 551 | handler_->DispatchEvent<ArtJvmtiEvent::kGarbageCollectionStart>(art::Thread::Current()); |
Andreas Gampe | 9b8c588 | 2016-10-21 15:27:46 -0700 | [diff] [blame] | 552 | } |
| 553 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 554 | void EndPause() override { |
Alex Light | b284f8d | 2017-11-21 00:00:48 +0000 | [diff] [blame] | 555 | handler_->DispatchEvent<ArtJvmtiEvent::kGarbageCollectionFinish>(art::Thread::Current()); |
Andreas Gampe | 9b8c588 | 2016-10-21 15:27:46 -0700 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | bool IsEnabled() { |
| 559 | return start_enabled_ || finish_enabled_; |
| 560 | } |
| 561 | |
| 562 | void SetStartEnabled(bool e) { |
| 563 | start_enabled_ = e; |
| 564 | } |
| 565 | |
| 566 | void SetFinishEnabled(bool e) { |
| 567 | finish_enabled_ = e; |
| 568 | } |
| 569 | |
| 570 | private: |
| 571 | EventHandler* handler_; |
| 572 | bool start_enabled_; |
| 573 | bool finish_enabled_; |
| 574 | }; |
| 575 | |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 576 | static void SetupGcPauseTracking(JvmtiGcPauseListener* listener, ArtJvmtiEvent event, bool enable) { |
Andreas Gampe | 9b8c588 | 2016-10-21 15:27:46 -0700 | [diff] [blame] | 577 | bool old_state = listener->IsEnabled(); |
| 578 | |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 579 | if (event == ArtJvmtiEvent::kGarbageCollectionStart) { |
Andreas Gampe | 9b8c588 | 2016-10-21 15:27:46 -0700 | [diff] [blame] | 580 | listener->SetStartEnabled(enable); |
| 581 | } else { |
| 582 | listener->SetFinishEnabled(enable); |
| 583 | } |
| 584 | |
| 585 | bool new_state = listener->IsEnabled(); |
| 586 | |
| 587 | if (old_state != new_state) { |
| 588 | if (new_state) { |
| 589 | art::Runtime::Current()->GetHeap()->SetGcPauseListener(listener); |
| 590 | } else { |
| 591 | art::Runtime::Current()->GetHeap()->RemoveGcPauseListener(); |
| 592 | } |
| 593 | } |
| 594 | } |
| 595 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 596 | class JvmtiMethodTraceListener final : public art::instrumentation::InstrumentationListener { |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 597 | public: |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 598 | explicit JvmtiMethodTraceListener(EventHandler* handler) |
| 599 | : event_handler_(handler), |
| 600 | non_standard_exits_lock_("JVMTI NonStandard Exits list lock", |
| 601 | art::LockLevel::kGenericBottomLock) {} |
| 602 | |
| 603 | void AddDelayedNonStandardExitEvent(const art::ShadowFrame* frame, bool is_object, jvalue val) |
| 604 | REQUIRES_SHARED(art::Locks::mutator_lock_) |
| 605 | REQUIRES(art::Locks::user_code_suspension_lock_, art::Locks::thread_list_lock_) { |
| 606 | art::Thread* self = art::Thread::Current(); |
| 607 | jobject to_cleanup = nullptr; |
| 608 | jobject new_val = is_object ? self->GetJniEnv()->NewGlobalRef(val.l) : nullptr; |
| 609 | { |
| 610 | art::MutexLock mu(self, non_standard_exits_lock_); |
| 611 | NonStandardExitEventInfo saved{ nullptr, { .j = 0 } }; |
| 612 | if (is_object) { |
| 613 | saved.return_val_obj_ = new_val; |
| 614 | saved.return_val_.l = saved.return_val_obj_; |
| 615 | } else { |
| 616 | saved.return_val_.j = val.j; |
| 617 | } |
| 618 | // only objects need cleanup. |
| 619 | if (UNLIKELY(is_object && non_standard_exits_.find(frame) != non_standard_exits_.end())) { |
| 620 | to_cleanup = non_standard_exits_.find(frame)->second.return_val_obj_; |
| 621 | } |
| 622 | non_standard_exits_.insert_or_assign(frame, saved); |
| 623 | } |
| 624 | self->GetJniEnv()->DeleteGlobalRef(to_cleanup); |
| 625 | } |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 626 | |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 627 | // Call-back for when a method is entered. |
| 628 | void MethodEntered(art::Thread* self, |
| 629 | art::Handle<art::mirror::Object> this_object ATTRIBUTE_UNUSED, |
| 630 | art::ArtMethod* method, |
| 631 | uint32_t dex_pc ATTRIBUTE_UNUSED) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 632 | REQUIRES_SHARED(art::Locks::mutator_lock_) override { |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 633 | if (!method->IsRuntimeMethod() && |
| 634 | event_handler_->IsEventEnabledAnywhere(ArtJvmtiEvent::kMethodEntry)) { |
| 635 | art::JNIEnvExt* jnienv = self->GetJniEnv(); |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 636 | RunEventCallback<ArtJvmtiEvent::kMethodEntry>(event_handler_, |
| 637 | self, |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 638 | jnienv, |
| 639 | art::jni::EncodeArtMethod(method)); |
| 640 | } |
| 641 | } |
| 642 | |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 643 | // TODO Maybe try to combine this with below using templates? |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 644 | // Callback for when a method is exited with a reference return value. |
| 645 | void MethodExited(art::Thread* self, |
| 646 | art::Handle<art::mirror::Object> this_object ATTRIBUTE_UNUSED, |
| 647 | art::ArtMethod* method, |
| 648 | uint32_t dex_pc ATTRIBUTE_UNUSED, |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 649 | art::instrumentation::OptionalFrame frame, |
| 650 | art::MutableHandle<art::mirror::Object>& return_value) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 651 | REQUIRES_SHARED(art::Locks::mutator_lock_) override { |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 652 | if (method->IsRuntimeMethod()) { |
| 653 | return; |
| 654 | } |
| 655 | if (frame.has_value() && UNLIKELY(event_handler_->IsEventEnabledAnywhere( |
| 656 | ArtJvmtiEvent::kForceEarlyReturnUpdateReturnValue))) { |
| 657 | DCHECK(!frame->get().GetSkipMethodExitEvents()); |
| 658 | bool has_return = false; |
| 659 | jobject ret_val = nullptr; |
| 660 | { |
| 661 | art::MutexLock mu(self, non_standard_exits_lock_); |
| 662 | const art::ShadowFrame* sframe = &frame.value().get(); |
| 663 | const auto it = non_standard_exits_.find(sframe); |
| 664 | if (it != non_standard_exits_.end()) { |
| 665 | ret_val = it->second.return_val_obj_; |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 666 | non_standard_exits_.erase(it); |
| 667 | has_return = true; |
| 668 | } |
| 669 | } |
| 670 | if (has_return) { |
Alex Light | c5c5186 | 2019-06-14 09:17:41 -0700 | [diff] [blame] | 671 | return_value.Assign(self->DecodeJObject(ret_val)); |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 672 | ScopedLocalRef<jthread> thr(self->GetJniEnv(), |
| 673 | self->GetJniEnv()->NewLocalRef(self->GetPeer())); |
| 674 | art::ScopedThreadSuspension sts(self, art::ThreadState::kNative); |
| 675 | self->GetJniEnv()->DeleteGlobalRef(ret_val); |
| 676 | event_handler_->SetInternalEvent( |
| 677 | thr.get(), ArtJvmtiEvent::kForceEarlyReturnUpdateReturnValue, JVMTI_DISABLE); |
| 678 | } |
| 679 | } |
| 680 | if (event_handler_->IsEventEnabledAnywhere(ArtJvmtiEvent::kMethodExit)) { |
Alex Light | c916736 | 2018-06-11 16:46:43 -0700 | [diff] [blame] | 681 | DCHECK_EQ( |
| 682 | method->GetInterfaceMethodIfProxy(art::kRuntimePointerSize)->GetReturnTypePrimitive(), |
| 683 | art::Primitive::kPrimNot) << method->PrettyMethod(); |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 684 | DCHECK(!self->IsExceptionPending()); |
| 685 | jvalue val; |
| 686 | art::JNIEnvExt* jnienv = self->GetJniEnv(); |
| 687 | ScopedLocalRef<jobject> return_jobj(jnienv, AddLocalRef<jobject>(jnienv, return_value.Get())); |
| 688 | val.l = return_jobj.get(); |
| 689 | RunEventCallback<ArtJvmtiEvent::kMethodExit>( |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 690 | event_handler_, |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 691 | self, |
| 692 | jnienv, |
| 693 | art::jni::EncodeArtMethod(method), |
Andreas Gampe | 6e89776 | 2018-10-16 13:09:32 -0700 | [diff] [blame] | 694 | /*was_popped_by_exception=*/ static_cast<jboolean>(JNI_FALSE), |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 695 | val); |
| 696 | } |
| 697 | } |
| 698 | |
| 699 | // Call-back for when a method is exited. |
| 700 | void MethodExited(art::Thread* self, |
| 701 | art::Handle<art::mirror::Object> this_object ATTRIBUTE_UNUSED, |
| 702 | art::ArtMethod* method, |
| 703 | uint32_t dex_pc ATTRIBUTE_UNUSED, |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 704 | art::instrumentation::OptionalFrame frame, |
| 705 | art::JValue& return_value) REQUIRES_SHARED(art::Locks::mutator_lock_) override { |
| 706 | if (frame.has_value() && |
| 707 | UNLIKELY(event_handler_->IsEventEnabledAnywhere( |
| 708 | ArtJvmtiEvent::kForceEarlyReturnUpdateReturnValue))) { |
| 709 | DCHECK(!frame->get().GetSkipMethodExitEvents()); |
| 710 | bool has_return = false; |
| 711 | { |
| 712 | art::MutexLock mu(self, non_standard_exits_lock_); |
| 713 | const art::ShadowFrame* sframe = &frame.value().get(); |
| 714 | const auto it = non_standard_exits_.find(sframe); |
| 715 | if (it != non_standard_exits_.end()) { |
| 716 | return_value.SetJ(it->second.return_val_.j); |
| 717 | non_standard_exits_.erase(it); |
| 718 | has_return = true; |
| 719 | } |
| 720 | } |
| 721 | if (has_return) { |
| 722 | ScopedLocalRef<jthread> thr(self->GetJniEnv(), |
| 723 | self->GetJniEnv()->NewLocalRef(self->GetPeer())); |
| 724 | art::ScopedThreadSuspension sts(self, art::ThreadState::kNative); |
| 725 | event_handler_->SetInternalEvent( |
| 726 | thr.get(), ArtJvmtiEvent::kForceEarlyReturnUpdateReturnValue, JVMTI_DISABLE); |
| 727 | } |
| 728 | } |
| 729 | if (event_handler_->IsEventEnabledAnywhere(ArtJvmtiEvent::kMethodExit)) { |
Alex Light | c916736 | 2018-06-11 16:46:43 -0700 | [diff] [blame] | 730 | DCHECK_NE( |
| 731 | method->GetInterfaceMethodIfProxy(art::kRuntimePointerSize)->GetReturnTypePrimitive(), |
| 732 | art::Primitive::kPrimNot) << method->PrettyMethod(); |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 733 | DCHECK(!self->IsExceptionPending()) << self->GetException()->Dump(); |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 734 | jvalue val; |
| 735 | art::JNIEnvExt* jnienv = self->GetJniEnv(); |
| 736 | // 64bit integer is the largest value in the union so we should be fine simply copying it into |
| 737 | // the union. |
| 738 | val.j = return_value.GetJ(); |
| 739 | RunEventCallback<ArtJvmtiEvent::kMethodExit>( |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 740 | event_handler_, |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 741 | self, |
| 742 | jnienv, |
| 743 | art::jni::EncodeArtMethod(method), |
Andreas Gampe | 6e89776 | 2018-10-16 13:09:32 -0700 | [diff] [blame] | 744 | /*was_popped_by_exception=*/ static_cast<jboolean>(JNI_FALSE), |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 745 | val); |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | // Call-back for when a method is popped due to an exception throw. A method will either cause a |
| 750 | // MethodExited call-back or a MethodUnwind call-back when its activation is removed. |
| 751 | void MethodUnwind(art::Thread* self, |
| 752 | art::Handle<art::mirror::Object> this_object ATTRIBUTE_UNUSED, |
| 753 | art::ArtMethod* method, |
| 754 | uint32_t dex_pc ATTRIBUTE_UNUSED) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 755 | REQUIRES_SHARED(art::Locks::mutator_lock_) override { |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 756 | if (!method->IsRuntimeMethod() && |
| 757 | event_handler_->IsEventEnabledAnywhere(ArtJvmtiEvent::kMethodExit)) { |
| 758 | jvalue val; |
| 759 | // Just set this to 0xffffffffffffffff so it's not uninitialized. |
| 760 | val.j = static_cast<jlong>(-1); |
| 761 | art::JNIEnvExt* jnienv = self->GetJniEnv(); |
| 762 | art::StackHandleScope<1> hs(self); |
| 763 | art::Handle<art::mirror::Throwable> old_exception(hs.NewHandle(self->GetException())); |
| 764 | CHECK(!old_exception.IsNull()); |
| 765 | self->ClearException(); |
| 766 | RunEventCallback<ArtJvmtiEvent::kMethodExit>( |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 767 | event_handler_, |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 768 | self, |
| 769 | jnienv, |
| 770 | art::jni::EncodeArtMethod(method), |
Andreas Gampe | 6e89776 | 2018-10-16 13:09:32 -0700 | [diff] [blame] | 771 | /*was_popped_by_exception=*/ static_cast<jboolean>(JNI_TRUE), |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 772 | val); |
| 773 | // Match RI behavior of just throwing away original exception if a new one is thrown. |
| 774 | if (LIKELY(!self->IsExceptionPending())) { |
| 775 | self->SetException(old_exception.Get()); |
| 776 | } |
| 777 | } |
| 778 | } |
| 779 | |
Alex Light | a26e349 | 2017-06-27 17:55:37 -0700 | [diff] [blame] | 780 | // Call-back for when the dex pc moves in a method. |
| 781 | void DexPcMoved(art::Thread* self, |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 782 | art::Handle<art::mirror::Object> this_object ATTRIBUTE_UNUSED, |
Alex Light | a26e349 | 2017-06-27 17:55:37 -0700 | [diff] [blame] | 783 | art::ArtMethod* method, |
| 784 | uint32_t new_dex_pc) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 785 | REQUIRES_SHARED(art::Locks::mutator_lock_) override { |
Alex Light | a26e349 | 2017-06-27 17:55:37 -0700 | [diff] [blame] | 786 | DCHECK(!method->IsRuntimeMethod()); |
| 787 | // Default methods might be copied to multiple classes. We need to get the canonical version of |
| 788 | // this method so that we can check for breakpoints correctly. |
| 789 | // TODO We should maybe do this on other events to ensure that we are consistent WRT default |
| 790 | // methods. This could interact with obsolete methods if we ever let interface redefinition |
| 791 | // happen though. |
| 792 | method = method->GetCanonicalMethod(); |
| 793 | art::JNIEnvExt* jnienv = self->GetJniEnv(); |
| 794 | jmethodID jmethod = art::jni::EncodeArtMethod(method); |
| 795 | jlocation location = static_cast<jlocation>(new_dex_pc); |
| 796 | // Step event is reported first according to the spec. |
| 797 | if (event_handler_->IsEventEnabledAnywhere(ArtJvmtiEvent::kSingleStep)) { |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 798 | RunEventCallback<ArtJvmtiEvent::kSingleStep>(event_handler_, self, jnienv, jmethod, location); |
Alex Light | a26e349 | 2017-06-27 17:55:37 -0700 | [diff] [blame] | 799 | } |
| 800 | // Next we do the Breakpoint events. The Dispatch code will filter the individual |
| 801 | if (event_handler_->IsEventEnabledAnywhere(ArtJvmtiEvent::kBreakpoint)) { |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 802 | RunEventCallback<ArtJvmtiEvent::kBreakpoint>(event_handler_, self, jnienv, jmethod, location); |
Alex Light | a26e349 | 2017-06-27 17:55:37 -0700 | [diff] [blame] | 803 | } |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | // Call-back for when we read from a field. |
Alex Light | 084fa37 | 2017-06-16 08:58:34 -0700 | [diff] [blame] | 807 | void FieldRead(art::Thread* self, |
| 808 | art::Handle<art::mirror::Object> this_object, |
Alex Light | 55eccdf | 2019-10-07 13:51:13 +0000 | [diff] [blame] | 809 | art::ArtMethod* method_p, |
Alex Light | 084fa37 | 2017-06-16 08:58:34 -0700 | [diff] [blame] | 810 | uint32_t dex_pc, |
Alex Light | 55eccdf | 2019-10-07 13:51:13 +0000 | [diff] [blame] | 811 | art::ArtField* field_p) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 812 | REQUIRES_SHARED(art::Locks::mutator_lock_) override { |
Alex Light | 084fa37 | 2017-06-16 08:58:34 -0700 | [diff] [blame] | 813 | if (event_handler_->IsEventEnabledAnywhere(ArtJvmtiEvent::kFieldAccess)) { |
Alex Light | 55eccdf | 2019-10-07 13:51:13 +0000 | [diff] [blame] | 814 | art::StackReflectiveHandleScope<1, 1> rhs(self); |
| 815 | art::ReflectiveHandle<art::ArtField> field(rhs.NewHandle(field_p)); |
| 816 | art::ReflectiveHandle<art::ArtMethod> method(rhs.NewHandle(method_p)); |
Alex Light | 084fa37 | 2017-06-16 08:58:34 -0700 | [diff] [blame] | 817 | art::JNIEnvExt* jnienv = self->GetJniEnv(); |
| 818 | // DCHECK(!self->IsExceptionPending()); |
| 819 | ScopedLocalRef<jobject> this_ref(jnienv, AddLocalRef<jobject>(jnienv, this_object.Get())); |
| 820 | ScopedLocalRef<jobject> fklass(jnienv, |
| 821 | AddLocalRef<jobject>(jnienv, |
| 822 | field->GetDeclaringClass().Ptr())); |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 823 | RunEventCallback<ArtJvmtiEvent::kFieldAccess>(event_handler_, |
| 824 | self, |
Alex Light | 084fa37 | 2017-06-16 08:58:34 -0700 | [diff] [blame] | 825 | jnienv, |
| 826 | art::jni::EncodeArtMethod(method), |
| 827 | static_cast<jlocation>(dex_pc), |
| 828 | static_cast<jclass>(fklass.get()), |
| 829 | this_ref.get(), |
| 830 | art::jni::EncodeArtField(field)); |
| 831 | } |
| 832 | } |
| 833 | |
| 834 | void FieldWritten(art::Thread* self, |
| 835 | art::Handle<art::mirror::Object> this_object, |
Alex Light | 55eccdf | 2019-10-07 13:51:13 +0000 | [diff] [blame] | 836 | art::ArtMethod* method_p, |
Alex Light | 084fa37 | 2017-06-16 08:58:34 -0700 | [diff] [blame] | 837 | uint32_t dex_pc, |
Alex Light | 55eccdf | 2019-10-07 13:51:13 +0000 | [diff] [blame] | 838 | art::ArtField* field_p, |
Alex Light | 084fa37 | 2017-06-16 08:58:34 -0700 | [diff] [blame] | 839 | art::Handle<art::mirror::Object> new_val) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 840 | REQUIRES_SHARED(art::Locks::mutator_lock_) override { |
Alex Light | 084fa37 | 2017-06-16 08:58:34 -0700 | [diff] [blame] | 841 | if (event_handler_->IsEventEnabledAnywhere(ArtJvmtiEvent::kFieldModification)) { |
| 842 | art::JNIEnvExt* jnienv = self->GetJniEnv(); |
Alex Light | 55eccdf | 2019-10-07 13:51:13 +0000 | [diff] [blame] | 843 | art::StackReflectiveHandleScope<1, 1> rhs(self); |
| 844 | art::ReflectiveHandle<art::ArtField> field(rhs.NewHandle(field_p)); |
| 845 | art::ReflectiveHandle<art::ArtMethod> method(rhs.NewHandle(method_p)); |
Alex Light | 084fa37 | 2017-06-16 08:58:34 -0700 | [diff] [blame] | 846 | // DCHECK(!self->IsExceptionPending()); |
| 847 | ScopedLocalRef<jobject> this_ref(jnienv, AddLocalRef<jobject>(jnienv, this_object.Get())); |
| 848 | ScopedLocalRef<jobject> fklass(jnienv, |
| 849 | AddLocalRef<jobject>(jnienv, |
| 850 | field->GetDeclaringClass().Ptr())); |
| 851 | ScopedLocalRef<jobject> fval(jnienv, AddLocalRef<jobject>(jnienv, new_val.Get())); |
| 852 | jvalue val; |
| 853 | val.l = fval.get(); |
| 854 | RunEventCallback<ArtJvmtiEvent::kFieldModification>( |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 855 | event_handler_, |
Alex Light | 084fa37 | 2017-06-16 08:58:34 -0700 | [diff] [blame] | 856 | self, |
| 857 | jnienv, |
| 858 | art::jni::EncodeArtMethod(method), |
| 859 | static_cast<jlocation>(dex_pc), |
| 860 | static_cast<jclass>(fklass.get()), |
| 861 | field->IsStatic() ? nullptr : this_ref.get(), |
| 862 | art::jni::EncodeArtField(field), |
| 863 | 'L', // type_char |
| 864 | val); |
| 865 | } |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 866 | } |
| 867 | |
| 868 | // Call-back for when we write into a field. |
Alex Light | 084fa37 | 2017-06-16 08:58:34 -0700 | [diff] [blame] | 869 | void FieldWritten(art::Thread* self, |
| 870 | art::Handle<art::mirror::Object> this_object, |
Alex Light | 55eccdf | 2019-10-07 13:51:13 +0000 | [diff] [blame] | 871 | art::ArtMethod* method_p, |
Alex Light | 084fa37 | 2017-06-16 08:58:34 -0700 | [diff] [blame] | 872 | uint32_t dex_pc, |
Alex Light | 55eccdf | 2019-10-07 13:51:13 +0000 | [diff] [blame] | 873 | art::ArtField* field_p, |
Alex Light | 084fa37 | 2017-06-16 08:58:34 -0700 | [diff] [blame] | 874 | const art::JValue& field_value) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 875 | REQUIRES_SHARED(art::Locks::mutator_lock_) override { |
Alex Light | 084fa37 | 2017-06-16 08:58:34 -0700 | [diff] [blame] | 876 | if (event_handler_->IsEventEnabledAnywhere(ArtJvmtiEvent::kFieldModification)) { |
| 877 | art::JNIEnvExt* jnienv = self->GetJniEnv(); |
Alex Light | 55eccdf | 2019-10-07 13:51:13 +0000 | [diff] [blame] | 878 | art::StackReflectiveHandleScope<1, 1> rhs(self); |
| 879 | art::ReflectiveHandle<art::ArtField> field(rhs.NewHandle(field_p)); |
| 880 | art::ReflectiveHandle<art::ArtMethod> method(rhs.NewHandle(method_p)); |
Alex Light | 084fa37 | 2017-06-16 08:58:34 -0700 | [diff] [blame] | 881 | DCHECK(!self->IsExceptionPending()); |
| 882 | ScopedLocalRef<jobject> this_ref(jnienv, AddLocalRef<jobject>(jnienv, this_object.Get())); |
| 883 | ScopedLocalRef<jobject> fklass(jnienv, |
| 884 | AddLocalRef<jobject>(jnienv, |
| 885 | field->GetDeclaringClass().Ptr())); |
| 886 | char type_char = art::Primitive::Descriptor(field->GetTypeAsPrimitiveType())[0]; |
| 887 | jvalue val; |
| 888 | // 64bit integer is the largest value in the union so we should be fine simply copying it into |
| 889 | // the union. |
| 890 | val.j = field_value.GetJ(); |
| 891 | RunEventCallback<ArtJvmtiEvent::kFieldModification>( |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 892 | event_handler_, |
Alex Light | 084fa37 | 2017-06-16 08:58:34 -0700 | [diff] [blame] | 893 | self, |
| 894 | jnienv, |
| 895 | art::jni::EncodeArtMethod(method), |
| 896 | static_cast<jlocation>(dex_pc), |
| 897 | static_cast<jclass>(fklass.get()), |
| 898 | field->IsStatic() ? nullptr : this_ref.get(), // nb static field modification get given |
| 899 | // the class as this_object for some |
| 900 | // reason. |
| 901 | art::jni::EncodeArtField(field), |
| 902 | type_char, |
| 903 | val); |
| 904 | } |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 905 | } |
| 906 | |
Alex Light | e814f9d | 2017-07-31 16:14:39 -0700 | [diff] [blame] | 907 | void WatchedFramePop(art::Thread* self, const art::ShadowFrame& frame) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 908 | REQUIRES_SHARED(art::Locks::mutator_lock_) override { |
Alex Light | e814f9d | 2017-07-31 16:14:39 -0700 | [diff] [blame] | 909 | art::JNIEnvExt* jnienv = self->GetJniEnv(); |
Alex Light | 3dacdd6 | 2019-03-12 15:45:47 +0000 | [diff] [blame] | 910 | // Remove the force-interpreter added by the WatchFrame. |
| 911 | { |
| 912 | art::MutexLock mu(self, *art::Locks::thread_list_lock_); |
| 913 | CHECK_GT(self->ForceInterpreterCount(), 0u); |
| 914 | self->DecrementForceInterpreterCount(); |
| 915 | } |
Alex Light | 9df79b7 | 2017-09-12 08:57:31 -0700 | [diff] [blame] | 916 | jboolean is_exception_pending = self->IsExceptionPending(); |
| 917 | RunEventCallback<ArtJvmtiEvent::kFramePop>( |
| 918 | event_handler_, |
| 919 | self, |
| 920 | jnienv, |
| 921 | art::jni::EncodeArtMethod(frame.GetMethod()), |
| 922 | is_exception_pending, |
| 923 | &frame); |
Alex Light | e814f9d | 2017-07-31 16:14:39 -0700 | [diff] [blame] | 924 | } |
| 925 | |
Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame] | 926 | static void FindCatchMethodsFromThrow(art::Thread* self, |
| 927 | art::Handle<art::mirror::Throwable> exception, |
| 928 | /*out*/ art::ArtMethod** out_method, |
| 929 | /*out*/ uint32_t* dex_pc) |
| 930 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 931 | // Finds the location where this exception will most likely be caught. We ignore intervening |
| 932 | // native frames (which could catch the exception) and return the closest java frame with a |
| 933 | // compatible catch statement. |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 934 | class CatchLocationFinder final : public art::StackVisitor { |
Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame] | 935 | public: |
| 936 | CatchLocationFinder(art::Thread* target, |
| 937 | art::Handle<art::mirror::Class> exception_class, |
| 938 | art::Context* context, |
| 939 | /*out*/ art::ArtMethod** out_catch_method, |
| 940 | /*out*/ uint32_t* out_catch_pc) |
| 941 | REQUIRES_SHARED(art::Locks::mutator_lock_) |
| 942 | : StackVisitor(target, context, art::StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
| 943 | exception_class_(exception_class), |
| 944 | catch_method_ptr_(out_catch_method), |
| 945 | catch_dex_pc_ptr_(out_catch_pc) {} |
| 946 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 947 | bool VisitFrame() override REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame] | 948 | art::ArtMethod* method = GetMethod(); |
| 949 | DCHECK(method != nullptr); |
| 950 | if (method->IsRuntimeMethod()) { |
| 951 | return true; |
| 952 | } |
| 953 | |
| 954 | if (!method->IsNative()) { |
| 955 | uint32_t cur_dex_pc = GetDexPc(); |
Andreas Gampe | e2abbc6 | 2017-09-15 11:59:26 -0700 | [diff] [blame] | 956 | if (cur_dex_pc == art::dex::kDexNoIndex) { |
Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame] | 957 | // This frame looks opaque. Just keep on going. |
| 958 | return true; |
| 959 | } |
| 960 | bool has_no_move_exception = false; |
| 961 | uint32_t found_dex_pc = method->FindCatchBlock( |
| 962 | exception_class_, cur_dex_pc, &has_no_move_exception); |
Andreas Gampe | e2abbc6 | 2017-09-15 11:59:26 -0700 | [diff] [blame] | 963 | if (found_dex_pc != art::dex::kDexNoIndex) { |
Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame] | 964 | // We found the catch. Store the result and return. |
| 965 | *catch_method_ptr_ = method; |
| 966 | *catch_dex_pc_ptr_ = found_dex_pc; |
| 967 | return false; |
| 968 | } |
| 969 | } |
| 970 | return true; |
| 971 | } |
| 972 | |
| 973 | private: |
| 974 | art::Handle<art::mirror::Class> exception_class_; |
| 975 | art::ArtMethod** catch_method_ptr_; |
| 976 | uint32_t* catch_dex_pc_ptr_; |
| 977 | |
| 978 | DISALLOW_COPY_AND_ASSIGN(CatchLocationFinder); |
| 979 | }; |
| 980 | |
| 981 | art::StackHandleScope<1> hs(self); |
| 982 | *out_method = nullptr; |
| 983 | *dex_pc = 0; |
| 984 | std::unique_ptr<art::Context> context(art::Context::Create()); |
| 985 | |
| 986 | CatchLocationFinder clf(self, |
| 987 | hs.NewHandle(exception->GetClass()), |
| 988 | context.get(), |
| 989 | /*out*/ out_method, |
| 990 | /*out*/ dex_pc); |
Andreas Gampe | 6e89776 | 2018-10-16 13:09:32 -0700 | [diff] [blame] | 991 | clf.WalkStack(/* include_transitions= */ false); |
Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame] | 992 | } |
| 993 | |
Alex Light | 6e1607e | 2017-08-23 10:06:18 -0700 | [diff] [blame] | 994 | // Call-back when an exception is thrown. |
Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame] | 995 | void ExceptionThrown(art::Thread* self, art::Handle<art::mirror::Throwable> exception_object) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 996 | REQUIRES_SHARED(art::Locks::mutator_lock_) override { |
Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame] | 997 | DCHECK(self->IsExceptionThrownByCurrentMethod(exception_object.Get())); |
| 998 | // The instrumentation events get rid of this for us. |
| 999 | DCHECK(!self->IsExceptionPending()); |
| 1000 | if (event_handler_->IsEventEnabledAnywhere(ArtJvmtiEvent::kException)) { |
| 1001 | art::JNIEnvExt* jnienv = self->GetJniEnv(); |
| 1002 | art::ArtMethod* catch_method; |
| 1003 | uint32_t catch_pc; |
| 1004 | FindCatchMethodsFromThrow(self, exception_object, &catch_method, &catch_pc); |
| 1005 | uint32_t dex_pc = 0; |
| 1006 | art::ArtMethod* method = self->GetCurrentMethod(&dex_pc, |
Andreas Gampe | 6e89776 | 2018-10-16 13:09:32 -0700 | [diff] [blame] | 1007 | /* check_suspended= */ true, |
| 1008 | /* abort_on_error= */ art::kIsDebugBuild); |
Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame] | 1009 | ScopedLocalRef<jobject> exception(jnienv, |
| 1010 | AddLocalRef<jobject>(jnienv, exception_object.Get())); |
| 1011 | RunEventCallback<ArtJvmtiEvent::kException>( |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 1012 | event_handler_, |
Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame] | 1013 | self, |
| 1014 | jnienv, |
| 1015 | art::jni::EncodeArtMethod(method), |
| 1016 | static_cast<jlocation>(dex_pc), |
| 1017 | exception.get(), |
| 1018 | art::jni::EncodeArtMethod(catch_method), |
| 1019 | static_cast<jlocation>(catch_pc)); |
| 1020 | } |
| 1021 | return; |
| 1022 | } |
| 1023 | |
| 1024 | // Call-back when an exception is handled. |
| 1025 | void ExceptionHandled(art::Thread* self, art::Handle<art::mirror::Throwable> exception_object) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 1026 | REQUIRES_SHARED(art::Locks::mutator_lock_) override { |
Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame] | 1027 | // Since the exception has already been handled there shouldn't be one pending. |
| 1028 | DCHECK(!self->IsExceptionPending()); |
| 1029 | if (event_handler_->IsEventEnabledAnywhere(ArtJvmtiEvent::kExceptionCatch)) { |
| 1030 | art::JNIEnvExt* jnienv = self->GetJniEnv(); |
| 1031 | uint32_t dex_pc; |
| 1032 | art::ArtMethod* method = self->GetCurrentMethod(&dex_pc, |
Andreas Gampe | 6e89776 | 2018-10-16 13:09:32 -0700 | [diff] [blame] | 1033 | /* check_suspended= */ true, |
| 1034 | /* abort_on_error= */ art::kIsDebugBuild); |
Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame] | 1035 | ScopedLocalRef<jobject> exception(jnienv, |
| 1036 | AddLocalRef<jobject>(jnienv, exception_object.Get())); |
| 1037 | RunEventCallback<ArtJvmtiEvent::kExceptionCatch>( |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 1038 | event_handler_, |
Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame] | 1039 | self, |
| 1040 | jnienv, |
| 1041 | art::jni::EncodeArtMethod(method), |
| 1042 | static_cast<jlocation>(dex_pc), |
| 1043 | exception.get()); |
| 1044 | } |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 1045 | return; |
| 1046 | } |
| 1047 | |
| 1048 | // Call-back for when we execute a branch. |
| 1049 | void Branch(art::Thread* self ATTRIBUTE_UNUSED, |
| 1050 | art::ArtMethod* method ATTRIBUTE_UNUSED, |
| 1051 | uint32_t dex_pc ATTRIBUTE_UNUSED, |
| 1052 | int32_t dex_pc_offset ATTRIBUTE_UNUSED) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 1053 | REQUIRES_SHARED(art::Locks::mutator_lock_) override { |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 1054 | return; |
| 1055 | } |
| 1056 | |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 1057 | private: |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 1058 | struct NonStandardExitEventInfo { |
| 1059 | // if non-null is a GlobalReference to the returned value. |
| 1060 | jobject return_val_obj_; |
| 1061 | // The return-value to be passed to the MethodExit event. |
| 1062 | jvalue return_val_; |
| 1063 | }; |
| 1064 | |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 1065 | EventHandler* const event_handler_; |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 1066 | |
| 1067 | mutable art::Mutex non_standard_exits_lock_ |
| 1068 | ACQUIRED_BEFORE(art::Locks::instrument_entrypoints_lock_); |
| 1069 | |
| 1070 | std::unordered_map<const art::ShadowFrame*, NonStandardExitEventInfo> non_standard_exits_ |
| 1071 | GUARDED_BY(non_standard_exits_lock_); |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 1072 | }; |
| 1073 | |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 1074 | uint32_t EventHandler::GetInstrumentationEventsFor(ArtJvmtiEvent event) { |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 1075 | switch (event) { |
| 1076 | case ArtJvmtiEvent::kMethodEntry: |
| 1077 | return art::instrumentation::Instrumentation::kMethodEntered; |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 1078 | case ArtJvmtiEvent::kForceEarlyReturnUpdateReturnValue: |
| 1079 | // TODO We want to do this but supporting only having a single one is difficult. |
| 1080 | // return art::instrumentation::Instrumentation::kMethodExited; |
| 1081 | case ArtJvmtiEvent::kMethodExit: { |
| 1082 | DCHECK(event == ArtJvmtiEvent::kMethodExit || |
| 1083 | event == ArtJvmtiEvent::kForceEarlyReturnUpdateReturnValue) |
| 1084 | << "event = " << static_cast<uint32_t>(event); |
| 1085 | ArtJvmtiEvent other = event == ArtJvmtiEvent::kMethodExit |
| 1086 | ? ArtJvmtiEvent::kForceEarlyReturnUpdateReturnValue |
| 1087 | : ArtJvmtiEvent::kMethodExit; |
| 1088 | if (LIKELY(!IsEventEnabledAnywhere(other))) { |
| 1089 | return art::instrumentation::Instrumentation::kMethodExited | |
| 1090 | art::instrumentation::Instrumentation::kMethodUnwind; |
| 1091 | } else { |
| 1092 | // The event needs to be kept around/is already enabled by the other jvmti event that uses |
| 1093 | // the same instrumentation event. |
| 1094 | return 0u; |
| 1095 | } |
| 1096 | } |
Alex Light | 084fa37 | 2017-06-16 08:58:34 -0700 | [diff] [blame] | 1097 | case ArtJvmtiEvent::kFieldModification: |
| 1098 | return art::instrumentation::Instrumentation::kFieldWritten; |
| 1099 | case ArtJvmtiEvent::kFieldAccess: |
| 1100 | return art::instrumentation::Instrumentation::kFieldRead; |
Alex Light | a26e349 | 2017-06-27 17:55:37 -0700 | [diff] [blame] | 1101 | case ArtJvmtiEvent::kBreakpoint: |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 1102 | case ArtJvmtiEvent::kSingleStep: { |
| 1103 | // Need to skip adding the listeners if the event is breakpoint/single-step since those events |
| 1104 | // share the same art-instrumentation underlying event. We need to give them their own deopt |
| 1105 | // request though so the test waits until here. |
| 1106 | DCHECK(event == ArtJvmtiEvent::kBreakpoint || event == ArtJvmtiEvent::kSingleStep); |
| 1107 | ArtJvmtiEvent other = event == ArtJvmtiEvent::kBreakpoint ? ArtJvmtiEvent::kSingleStep |
| 1108 | : ArtJvmtiEvent::kBreakpoint; |
| 1109 | if (LIKELY(!IsEventEnabledAnywhere(other))) { |
| 1110 | return art::instrumentation::Instrumentation::kDexPcMoved; |
| 1111 | } else { |
| 1112 | // The event needs to be kept around/is already enabled by the other jvmti event that uses |
| 1113 | // the same instrumentation event. |
| 1114 | return 0u; |
| 1115 | } |
| 1116 | } |
Alex Light | e814f9d | 2017-07-31 16:14:39 -0700 | [diff] [blame] | 1117 | case ArtJvmtiEvent::kFramePop: |
| 1118 | return art::instrumentation::Instrumentation::kWatchedFramePop; |
Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame] | 1119 | case ArtJvmtiEvent::kException: |
| 1120 | return art::instrumentation::Instrumentation::kExceptionThrown; |
| 1121 | case ArtJvmtiEvent::kExceptionCatch: |
| 1122 | return art::instrumentation::Instrumentation::kExceptionHandled; |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 1123 | default: |
| 1124 | LOG(FATAL) << "Unknown event "; |
Elliott Hughes | c1896c9 | 2018-11-29 11:33:18 -0800 | [diff] [blame] | 1125 | UNREACHABLE(); |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 1126 | } |
| 1127 | } |
| 1128 | |
Alex Light | 3dacdd6 | 2019-03-12 15:45:47 +0000 | [diff] [blame] | 1129 | enum class DeoptRequirement { |
Alex Light | 4060786 | 2019-05-06 18:16:24 +0000 | [diff] [blame] | 1130 | // No deoptimization work required. |
| 1131 | kNone, |
Alex Light | 3dacdd6 | 2019-03-12 15:45:47 +0000 | [diff] [blame] | 1132 | // Limited/no deopt required. |
| 1133 | kLimited, |
| 1134 | // A single thread must be put into interpret only. |
| 1135 | kThread, |
| 1136 | // All methods and all threads deopted. |
| 1137 | kFull, |
| 1138 | }; |
| 1139 | |
| 1140 | static DeoptRequirement GetDeoptRequirement(ArtJvmtiEvent event, jthread thread) { |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 1141 | switch (event) { |
| 1142 | case ArtJvmtiEvent::kBreakpoint: |
| 1143 | case ArtJvmtiEvent::kException: |
Alex Light | 3dacdd6 | 2019-03-12 15:45:47 +0000 | [diff] [blame] | 1144 | return DeoptRequirement::kLimited; |
| 1145 | // TODO MethodEntry is needed due to inconsistencies between the interpreter and the trampoline |
| 1146 | // in how to handle exceptions. |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 1147 | case ArtJvmtiEvent::kMethodEntry: |
Alex Light | d7da314 | 2018-07-18 15:39:16 +0000 | [diff] [blame] | 1148 | case ArtJvmtiEvent::kExceptionCatch: |
Alex Light | 3dacdd6 | 2019-03-12 15:45:47 +0000 | [diff] [blame] | 1149 | return DeoptRequirement::kFull; |
David Srbecky | d25eb2c | 2018-07-19 12:17:04 +0000 | [diff] [blame] | 1150 | case ArtJvmtiEvent::kMethodExit: |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 1151 | case ArtJvmtiEvent::kFieldModification: |
| 1152 | case ArtJvmtiEvent::kFieldAccess: |
| 1153 | case ArtJvmtiEvent::kSingleStep: |
Nicolas Geoffray | ad344b6 | 2019-03-09 17:49:52 +0000 | [diff] [blame] | 1154 | case ArtJvmtiEvent::kFramePop: |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 1155 | case ArtJvmtiEvent::kForceEarlyReturnUpdateReturnValue: |
Alex Light | 3dacdd6 | 2019-03-12 15:45:47 +0000 | [diff] [blame] | 1156 | return thread == nullptr ? DeoptRequirement::kFull : DeoptRequirement::kThread; |
Alex Light | 4060786 | 2019-05-06 18:16:24 +0000 | [diff] [blame] | 1157 | case ArtJvmtiEvent::kVmInit: |
| 1158 | case ArtJvmtiEvent::kVmDeath: |
| 1159 | case ArtJvmtiEvent::kThreadStart: |
| 1160 | case ArtJvmtiEvent::kThreadEnd: |
| 1161 | case ArtJvmtiEvent::kClassFileLoadHookNonRetransformable: |
| 1162 | case ArtJvmtiEvent::kClassLoad: |
| 1163 | case ArtJvmtiEvent::kClassPrepare: |
| 1164 | case ArtJvmtiEvent::kVmStart: |
| 1165 | case ArtJvmtiEvent::kNativeMethodBind: |
| 1166 | case ArtJvmtiEvent::kCompiledMethodLoad: |
| 1167 | case ArtJvmtiEvent::kCompiledMethodUnload: |
| 1168 | case ArtJvmtiEvent::kDynamicCodeGenerated: |
| 1169 | case ArtJvmtiEvent::kDataDumpRequest: |
| 1170 | case ArtJvmtiEvent::kMonitorWait: |
| 1171 | case ArtJvmtiEvent::kMonitorWaited: |
| 1172 | case ArtJvmtiEvent::kMonitorContendedEnter: |
| 1173 | case ArtJvmtiEvent::kMonitorContendedEntered: |
| 1174 | case ArtJvmtiEvent::kResourceExhausted: |
| 1175 | case ArtJvmtiEvent::kGarbageCollectionStart: |
| 1176 | case ArtJvmtiEvent::kGarbageCollectionFinish: |
| 1177 | case ArtJvmtiEvent::kObjectFree: |
| 1178 | case ArtJvmtiEvent::kVmObjectAlloc: |
| 1179 | case ArtJvmtiEvent::kClassFileLoadHookRetransformable: |
| 1180 | case ArtJvmtiEvent::kDdmPublishChunk: |
Alex Light | 72d7e94 | 2019-07-23 13:10:20 -0700 | [diff] [blame] | 1181 | case ArtJvmtiEvent::kObsoleteObjectCreated: |
Alex Light | d55b844 | 2019-10-15 15:46:07 -0700 | [diff] [blame] | 1182 | case ArtJvmtiEvent::kStructuralDexFileLoadHook: |
Alex Light | 4060786 | 2019-05-06 18:16:24 +0000 | [diff] [blame] | 1183 | return DeoptRequirement::kNone; |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 1184 | } |
| 1185 | } |
| 1186 | |
Alex Light | 4060786 | 2019-05-06 18:16:24 +0000 | [diff] [blame] | 1187 | jvmtiError EventHandler::HandleEventDeopt(ArtJvmtiEvent event, jthread thread, bool enable) { |
Alex Light | 3dacdd6 | 2019-03-12 15:45:47 +0000 | [diff] [blame] | 1188 | DeoptRequirement deopt_req = GetDeoptRequirement(event, thread); |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 1189 | // Make sure we can deopt. |
Alex Light | 4060786 | 2019-05-06 18:16:24 +0000 | [diff] [blame] | 1190 | if (deopt_req != DeoptRequirement::kNone) { |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 1191 | art::ScopedObjectAccess soa(art::Thread::Current()); |
| 1192 | DeoptManager* deopt_manager = DeoptManager::Get(); |
Alex Light | 3dacdd6 | 2019-03-12 15:45:47 +0000 | [diff] [blame] | 1193 | jvmtiError err = OK; |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 1194 | if (enable) { |
| 1195 | deopt_manager->AddDeoptimizationRequester(); |
Alex Light | 3dacdd6 | 2019-03-12 15:45:47 +0000 | [diff] [blame] | 1196 | switch (deopt_req) { |
| 1197 | case DeoptRequirement::kFull: |
| 1198 | deopt_manager->AddDeoptimizeAllMethods(); |
| 1199 | break; |
| 1200 | case DeoptRequirement::kThread: |
| 1201 | err = deopt_manager->AddDeoptimizeThreadMethods(soa, thread); |
| 1202 | break; |
| 1203 | default: |
| 1204 | break; |
| 1205 | } |
| 1206 | if (err != OK) { |
| 1207 | deopt_manager->RemoveDeoptimizationRequester(); |
| 1208 | return err; |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 1209 | } |
| 1210 | } else { |
Alex Light | 3dacdd6 | 2019-03-12 15:45:47 +0000 | [diff] [blame] | 1211 | switch (deopt_req) { |
| 1212 | case DeoptRequirement::kFull: |
| 1213 | deopt_manager->RemoveDeoptimizeAllMethods(); |
| 1214 | break; |
| 1215 | case DeoptRequirement::kThread: |
| 1216 | err = deopt_manager->RemoveDeoptimizeThreadMethods(soa, thread); |
| 1217 | break; |
| 1218 | default: |
| 1219 | break; |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 1220 | } |
| 1221 | deopt_manager->RemoveDeoptimizationRequester(); |
Alex Light | 3dacdd6 | 2019-03-12 15:45:47 +0000 | [diff] [blame] | 1222 | if (err != OK) { |
| 1223 | return err; |
| 1224 | } |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 1225 | } |
| 1226 | } |
Alex Light | 4060786 | 2019-05-06 18:16:24 +0000 | [diff] [blame] | 1227 | return OK; |
| 1228 | } |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 1229 | |
Alex Light | 4060786 | 2019-05-06 18:16:24 +0000 | [diff] [blame] | 1230 | void EventHandler::SetupTraceListener(JvmtiMethodTraceListener* listener, |
| 1231 | ArtJvmtiEvent event, |
| 1232 | bool enable) { |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 1233 | // Add the actual listeners. |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 1234 | uint32_t new_events = GetInstrumentationEventsFor(event); |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 1235 | if (new_events == 0) { |
| 1236 | return; |
Alex Light | f6df1b5 | 2017-11-29 14:46:53 -0800 | [diff] [blame] | 1237 | } |
| 1238 | art::ScopedThreadStateChange stsc(art::Thread::Current(), art::ThreadState::kNative); |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 1239 | art::instrumentation::Instrumentation* instr = art::Runtime::Current()->GetInstrumentation(); |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 1240 | art::ScopedSuspendAll ssa("jvmti method tracing installation"); |
| 1241 | if (enable) { |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 1242 | instr->AddListener(listener, new_events); |
| 1243 | } else { |
| 1244 | instr->RemoveListener(listener, new_events); |
| 1245 | } |
Alex Light | 4060786 | 2019-05-06 18:16:24 +0000 | [diff] [blame] | 1246 | return; |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 1247 | } |
| 1248 | |
Alex Light | 0a5ec3d | 2017-07-25 16:50:26 -0700 | [diff] [blame] | 1249 | // Makes sure that all compiled methods are AsyncDeoptimizable so we can deoptimize (and force to |
| 1250 | // 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] | 1251 | void EventHandler::HandleLocalAccessCapabilityAdded() { |
Alex Light | 0a5ec3d | 2017-07-25 16:50:26 -0700 | [diff] [blame] | 1252 | class UpdateEntryPointsClassVisitor : public art::ClassVisitor { |
| 1253 | public: |
| 1254 | explicit UpdateEntryPointsClassVisitor(art::Runtime* runtime) |
| 1255 | : runtime_(runtime) {} |
| 1256 | |
| 1257 | bool operator()(art::ObjPtr<art::mirror::Class> klass) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 1258 | override REQUIRES(art::Locks::mutator_lock_) { |
Alex Light | a567deb | 2017-10-10 16:44:11 -0700 | [diff] [blame] | 1259 | if (!klass->IsLoaded()) { |
| 1260 | // Skip classes that aren't loaded since they might not have fully allocated and initialized |
| 1261 | // their methods. Furthemore since the jvmti-plugin must have been loaded by this point |
| 1262 | // these methods will definitately be using debuggable code. |
| 1263 | return true; |
| 1264 | } |
Alex Light | 0a5ec3d | 2017-07-25 16:50:26 -0700 | [diff] [blame] | 1265 | for (auto& m : klass->GetMethods(art::kRuntimePointerSize)) { |
| 1266 | const void* code = m.GetEntryPointFromQuickCompiledCode(); |
| 1267 | if (m.IsNative() || m.IsProxyMethod()) { |
| 1268 | continue; |
| 1269 | } else if (!runtime_->GetClassLinker()->IsQuickToInterpreterBridge(code) && |
| 1270 | !runtime_->IsAsyncDeoptimizeable(reinterpret_cast<uintptr_t>(code))) { |
| 1271 | runtime_->GetInstrumentation()->UpdateMethodsCodeToInterpreterEntryPoint(&m); |
| 1272 | } |
| 1273 | } |
| 1274 | return true; |
| 1275 | } |
| 1276 | |
| 1277 | private: |
| 1278 | art::Runtime* runtime_; |
| 1279 | }; |
| 1280 | art::ScopedObjectAccess soa(art::Thread::Current()); |
| 1281 | UpdateEntryPointsClassVisitor visitor(art::Runtime::Current()); |
| 1282 | art::Runtime::Current()->GetClassLinker()->VisitClasses(&visitor); |
Alex Light | bebd7bd | 2017-07-25 14:05:52 -0700 | [diff] [blame] | 1283 | } |
| 1284 | |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 1285 | bool EventHandler::OtherMonitorEventsEnabledAnywhere(ArtJvmtiEvent event) { |
| 1286 | std::array<ArtJvmtiEvent, 4> events { |
| 1287 | { |
| 1288 | ArtJvmtiEvent::kMonitorContendedEnter, |
| 1289 | ArtJvmtiEvent::kMonitorContendedEntered, |
| 1290 | ArtJvmtiEvent::kMonitorWait, |
| 1291 | ArtJvmtiEvent::kMonitorWaited |
| 1292 | } |
| 1293 | }; |
| 1294 | for (ArtJvmtiEvent e : events) { |
| 1295 | if (e != event && IsEventEnabledAnywhere(e)) { |
| 1296 | return true; |
| 1297 | } |
| 1298 | } |
| 1299 | return false; |
| 1300 | } |
| 1301 | |
Alex Light | 4060786 | 2019-05-06 18:16:24 +0000 | [diff] [blame] | 1302 | void EventHandler::SetupFramePopTraceListener(bool enable) { |
Alex Light | f5d5eb1 | 2018-03-06 15:13:59 -0800 | [diff] [blame] | 1303 | if (enable) { |
| 1304 | frame_pop_enabled = true; |
Alex Light | 4060786 | 2019-05-06 18:16:24 +0000 | [diff] [blame] | 1305 | SetupTraceListener(method_trace_listener_.get(), ArtJvmtiEvent::kFramePop, enable); |
Alex Light | f5d5eb1 | 2018-03-06 15:13:59 -0800 | [diff] [blame] | 1306 | } else { |
| 1307 | // remove the listener if we have no outstanding frames. |
| 1308 | { |
| 1309 | art::ReaderMutexLock mu(art::Thread::Current(), envs_lock_); |
Alex Light | 3dacdd6 | 2019-03-12 15:45:47 +0000 | [diff] [blame] | 1310 | for (ArtJvmTiEnv *env : envs) { |
Alex Light | f5d5eb1 | 2018-03-06 15:13:59 -0800 | [diff] [blame] | 1311 | art::ReaderMutexLock event_mu(art::Thread::Current(), env->event_info_mutex_); |
| 1312 | if (!env->notify_frames.empty()) { |
| 1313 | // Leaving FramePop listener since there are unsent FramePop events. |
Alex Light | 4060786 | 2019-05-06 18:16:24 +0000 | [diff] [blame] | 1314 | return; |
Alex Light | f5d5eb1 | 2018-03-06 15:13:59 -0800 | [diff] [blame] | 1315 | } |
| 1316 | } |
| 1317 | frame_pop_enabled = false; |
| 1318 | } |
Alex Light | 4060786 | 2019-05-06 18:16:24 +0000 | [diff] [blame] | 1319 | SetupTraceListener(method_trace_listener_.get(), ArtJvmtiEvent::kFramePop, enable); |
Alex Light | f5d5eb1 | 2018-03-06 15:13:59 -0800 | [diff] [blame] | 1320 | } |
| 1321 | } |
| 1322 | |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 1323 | // Handle special work for the given event type, if necessary. |
Alex Light | 4060786 | 2019-05-06 18:16:24 +0000 | [diff] [blame] | 1324 | void EventHandler::HandleEventType(ArtJvmtiEvent event, bool enable) { |
Andreas Gampe | 9b8c588 | 2016-10-21 15:27:46 -0700 | [diff] [blame] | 1325 | switch (event) { |
Alex Light | 8c2b929 | 2017-11-09 13:21:01 -0800 | [diff] [blame] | 1326 | case ArtJvmtiEvent::kDdmPublishChunk: |
| 1327 | SetupDdmTracking(ddm_listener_.get(), enable); |
Alex Light | 4060786 | 2019-05-06 18:16:24 +0000 | [diff] [blame] | 1328 | return; |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 1329 | case ArtJvmtiEvent::kVmObjectAlloc: |
Alex Light | 986914b | 2019-11-19 01:12:25 +0000 | [diff] [blame^] | 1330 | SetupObjectAllocationTracking(enable); |
Alex Light | 4060786 | 2019-05-06 18:16:24 +0000 | [diff] [blame] | 1331 | return; |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 1332 | case ArtJvmtiEvent::kGarbageCollectionStart: |
| 1333 | case ArtJvmtiEvent::kGarbageCollectionFinish: |
Andreas Gampe | 9b8c588 | 2016-10-21 15:27:46 -0700 | [diff] [blame] | 1334 | SetupGcPauseTracking(gc_pause_listener_.get(), event, enable); |
Alex Light | 4060786 | 2019-05-06 18:16:24 +0000 | [diff] [blame] | 1335 | return; |
Alex Light | f5d5eb1 | 2018-03-06 15:13:59 -0800 | [diff] [blame] | 1336 | // FramePop can never be disabled once it's been turned on if it was turned off with outstanding |
| 1337 | // 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] | 1338 | case ArtJvmtiEvent::kFramePop: |
Alex Light | f5d5eb1 | 2018-03-06 15:13:59 -0800 | [diff] [blame] | 1339 | if (enable && frame_pop_enabled) { |
| 1340 | // 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] | 1341 | } else { |
Alex Light | 4060786 | 2019-05-06 18:16:24 +0000 | [diff] [blame] | 1342 | SetupFramePopTraceListener(enable); |
Alex Light | e814f9d | 2017-07-31 16:14:39 -0700 | [diff] [blame] | 1343 | } |
Alex Light | 4060786 | 2019-05-06 18:16:24 +0000 | [diff] [blame] | 1344 | return; |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 1345 | case ArtJvmtiEvent::kMethodEntry: |
| 1346 | case ArtJvmtiEvent::kMethodExit: |
Alex Light | 084fa37 | 2017-06-16 08:58:34 -0700 | [diff] [blame] | 1347 | case ArtJvmtiEvent::kFieldAccess: |
| 1348 | case ArtJvmtiEvent::kFieldModification: |
Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame] | 1349 | case ArtJvmtiEvent::kException: |
| 1350 | case ArtJvmtiEvent::kExceptionCatch: |
Alex Light | f6df1b5 | 2017-11-29 14:46:53 -0800 | [diff] [blame] | 1351 | case ArtJvmtiEvent::kBreakpoint: |
| 1352 | case ArtJvmtiEvent::kSingleStep: |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 1353 | case ArtJvmtiEvent::kForceEarlyReturnUpdateReturnValue: |
Alex Light | 4060786 | 2019-05-06 18:16:24 +0000 | [diff] [blame] | 1354 | SetupTraceListener(method_trace_listener_.get(), event, enable); |
| 1355 | return; |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 1356 | case ArtJvmtiEvent::kMonitorContendedEnter: |
| 1357 | case ArtJvmtiEvent::kMonitorContendedEntered: |
| 1358 | case ArtJvmtiEvent::kMonitorWait: |
| 1359 | case ArtJvmtiEvent::kMonitorWaited: |
| 1360 | if (!OtherMonitorEventsEnabledAnywhere(event)) { |
Charles Munger | 5cc0e75 | 2018-11-09 12:30:46 -0800 | [diff] [blame] | 1361 | SetupMonitorListener(monitor_listener_.get(), park_listener_.get(), enable); |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 1362 | } |
Alex Light | 4060786 | 2019-05-06 18:16:24 +0000 | [diff] [blame] | 1363 | return; |
Andreas Gampe | 9b8c588 | 2016-10-21 15:27:46 -0700 | [diff] [blame] | 1364 | default: |
| 1365 | break; |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 1366 | } |
Alex Light | 4060786 | 2019-05-06 18:16:24 +0000 | [diff] [blame] | 1367 | return; |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 1368 | } |
| 1369 | |
Alex Light | 9db679d | 2017-01-25 15:28:04 -0800 | [diff] [blame] | 1370 | // Checks to see if the env has the capabilities associated with the given event. |
| 1371 | static bool HasAssociatedCapability(ArtJvmTiEnv* env, |
| 1372 | ArtJvmtiEvent event) { |
| 1373 | jvmtiCapabilities caps = env->capabilities; |
| 1374 | switch (event) { |
| 1375 | case ArtJvmtiEvent::kBreakpoint: |
| 1376 | return caps.can_generate_breakpoint_events == 1; |
| 1377 | |
| 1378 | case ArtJvmtiEvent::kCompiledMethodLoad: |
| 1379 | case ArtJvmtiEvent::kCompiledMethodUnload: |
| 1380 | return caps.can_generate_compiled_method_load_events == 1; |
| 1381 | |
| 1382 | case ArtJvmtiEvent::kException: |
| 1383 | case ArtJvmtiEvent::kExceptionCatch: |
| 1384 | return caps.can_generate_exception_events == 1; |
| 1385 | |
| 1386 | case ArtJvmtiEvent::kFieldAccess: |
| 1387 | return caps.can_generate_field_access_events == 1; |
| 1388 | |
| 1389 | case ArtJvmtiEvent::kFieldModification: |
| 1390 | return caps.can_generate_field_modification_events == 1; |
| 1391 | |
| 1392 | case ArtJvmtiEvent::kFramePop: |
| 1393 | return caps.can_generate_frame_pop_events == 1; |
| 1394 | |
| 1395 | case ArtJvmtiEvent::kGarbageCollectionStart: |
| 1396 | case ArtJvmtiEvent::kGarbageCollectionFinish: |
| 1397 | return caps.can_generate_garbage_collection_events == 1; |
| 1398 | |
| 1399 | case ArtJvmtiEvent::kMethodEntry: |
| 1400 | return caps.can_generate_method_entry_events == 1; |
| 1401 | |
| 1402 | case ArtJvmtiEvent::kMethodExit: |
| 1403 | return caps.can_generate_method_exit_events == 1; |
| 1404 | |
| 1405 | case ArtJvmtiEvent::kMonitorContendedEnter: |
| 1406 | case ArtJvmtiEvent::kMonitorContendedEntered: |
| 1407 | case ArtJvmtiEvent::kMonitorWait: |
| 1408 | case ArtJvmtiEvent::kMonitorWaited: |
| 1409 | return caps.can_generate_monitor_events == 1; |
| 1410 | |
| 1411 | case ArtJvmtiEvent::kNativeMethodBind: |
| 1412 | return caps.can_generate_native_method_bind_events == 1; |
| 1413 | |
| 1414 | case ArtJvmtiEvent::kObjectFree: |
| 1415 | return caps.can_generate_object_free_events == 1; |
| 1416 | |
| 1417 | case ArtJvmtiEvent::kSingleStep: |
| 1418 | return caps.can_generate_single_step_events == 1; |
| 1419 | |
| 1420 | case ArtJvmtiEvent::kVmObjectAlloc: |
| 1421 | return caps.can_generate_vm_object_alloc_events == 1; |
| 1422 | |
| 1423 | default: |
| 1424 | return true; |
| 1425 | } |
| 1426 | } |
| 1427 | |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 1428 | static bool IsInternalEvent(ArtJvmtiEvent event) { |
| 1429 | return static_cast<uint32_t>(event) >= |
| 1430 | static_cast<uint32_t>(ArtJvmtiEvent::kMinInternalEventTypeVal); |
| 1431 | } |
| 1432 | |
| 1433 | jvmtiError EventHandler::SetInternalEvent(jthread thread, |
| 1434 | ArtJvmtiEvent event, |
| 1435 | jvmtiEventMode mode) { |
| 1436 | CHECK(IsInternalEvent(event)) << static_cast<uint32_t>(event); |
| 1437 | |
| 1438 | art::Thread* self = art::Thread::Current(); |
| 1439 | art::Thread* target = nullptr; |
| 1440 | ScopedNoUserCodeSuspension snucs(self); |
| 1441 | // The overall state across all threads and jvmtiEnvs. This is used to control the state of the |
| 1442 | // instrumentation handlers since we only want each added once. |
| 1443 | bool old_state; |
| 1444 | bool new_state; |
| 1445 | // The state for just the current 'thread' (including null) across all jvmtiEnvs. This is used to |
| 1446 | // control the deoptimization state since we do refcounting for that and need to perform different |
| 1447 | // actions depending on if the event is limited to a single thread or global. |
| 1448 | bool old_thread_state; |
| 1449 | bool new_thread_state; |
| 1450 | { |
| 1451 | // From now on we know we cannot get suspended by user-code. |
| 1452 | // NB This does a SuspendCheck (during thread state change) so we need to |
| 1453 | // make sure we don't have the 'suspend_lock' locked here. |
| 1454 | art::ScopedObjectAccess soa(self); |
| 1455 | art::WriterMutexLock el_mu(self, envs_lock_); |
| 1456 | art::MutexLock tll_mu(self, *art::Locks::thread_list_lock_); |
| 1457 | jvmtiError err = ERR(INTERNAL); |
| 1458 | if (!ThreadUtil::GetAliveNativeThread(thread, soa, &target, &err)) { |
| 1459 | return err; |
| 1460 | } else if (target->IsStillStarting() || target->GetState() == art::ThreadState::kStarting) { |
| 1461 | target->Dump(LOG_STREAM(WARNING) << "Is not alive: "); |
| 1462 | return ERR(THREAD_NOT_ALIVE); |
| 1463 | } |
| 1464 | |
| 1465 | // Make sure we have a valid jthread to pass to deopt-manager. |
| 1466 | ScopedLocalRef<jthread> thread_lr( |
| 1467 | soa.Env(), thread != nullptr ? nullptr : soa.AddLocalReference<jthread>(target->GetPeer())); |
| 1468 | if (thread == nullptr) { |
| 1469 | thread = thread_lr.get(); |
| 1470 | } |
| 1471 | CHECK(thread != nullptr); |
| 1472 | |
| 1473 | { |
| 1474 | DCHECK_GE(GetInternalEventRefcount(event) + (mode == JVMTI_ENABLE ? 1 : -1), 0) |
| 1475 | << "Refcount: " << GetInternalEventRefcount(event); |
| 1476 | DCHECK_GE(GetInternalEventThreadRefcount(event, target) + (mode == JVMTI_ENABLE ? 1 : -1), 0) |
| 1477 | << "Refcount: " << GetInternalEventThreadRefcount(event, target); |
| 1478 | DCHECK_GE(GetInternalEventRefcount(event), GetInternalEventThreadRefcount(event, target)); |
| 1479 | old_state = GetInternalEventRefcount(event) > 0; |
| 1480 | old_thread_state = GetInternalEventThreadRefcount(event, target) > 0; |
| 1481 | if (mode == JVMTI_ENABLE) { |
| 1482 | new_state = IncrInternalEventRefcount(event) > 0; |
| 1483 | new_thread_state = IncrInternalEventThreadRefcount(event, target) > 0; |
| 1484 | } else { |
| 1485 | new_state = DecrInternalEventRefcount(event) > 0; |
| 1486 | new_thread_state = DecrInternalEventThreadRefcount(event, target) > 0; |
| 1487 | } |
| 1488 | if (old_state != new_state) { |
| 1489 | global_mask.Set(event, new_state); |
| 1490 | } |
| 1491 | } |
| 1492 | } |
| 1493 | // Handle any special work required for the event type. We still have the |
| 1494 | // user_code_suspend_count_lock_ so there won't be any interleaving here. |
| 1495 | if (new_state != old_state) { |
| 1496 | HandleEventType(event, mode == JVMTI_ENABLE); |
| 1497 | } |
| 1498 | if (old_thread_state != new_thread_state) { |
| 1499 | HandleEventDeopt(event, thread, new_thread_state); |
| 1500 | } |
| 1501 | return OK; |
| 1502 | } |
| 1503 | |
| 1504 | static bool IsDirectlySettableEvent(ArtJvmtiEvent event) { |
| 1505 | return !IsInternalEvent(event); |
| 1506 | } |
| 1507 | |
| 1508 | static bool EventIsNormal(ArtJvmtiEvent event) { |
| 1509 | return EventMask::EventIsInRange(event) && IsDirectlySettableEvent(event); |
| 1510 | } |
| 1511 | |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 1512 | jvmtiError EventHandler::SetEvent(ArtJvmTiEnv* env, |
Alex Light | 3dacdd6 | 2019-03-12 15:45:47 +0000 | [diff] [blame] | 1513 | jthread thread, |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 1514 | ArtJvmtiEvent event, |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 1515 | jvmtiEventMode mode) { |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 1516 | if (mode != JVMTI_ENABLE && mode != JVMTI_DISABLE) { |
| 1517 | return ERR(ILLEGAL_ARGUMENT); |
| 1518 | } |
| 1519 | |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 1520 | if (!EventIsNormal(event)) { |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 1521 | return ERR(INVALID_EVENT_TYPE); |
| 1522 | } |
| 1523 | |
Alex Light | 9db679d | 2017-01-25 15:28:04 -0800 | [diff] [blame] | 1524 | if (!HasAssociatedCapability(env, event)) { |
| 1525 | return ERR(MUST_POSSESS_CAPABILITY); |
| 1526 | } |
| 1527 | |
Alex Light | 6683446 | 2019-04-08 16:28:29 +0000 | [diff] [blame] | 1528 | if (thread != nullptr && !IsThreadControllable(event)) { |
| 1529 | return ERR(ILLEGAL_ARGUMENT); |
Alex Light | 3dacdd6 | 2019-03-12 15:45:47 +0000 | [diff] [blame] | 1530 | } |
| 1531 | |
Alex Light | 6683446 | 2019-04-08 16:28:29 +0000 | [diff] [blame] | 1532 | art::Thread* self = art::Thread::Current(); |
| 1533 | art::Thread* target = nullptr; |
| 1534 | ScopedNoUserCodeSuspension snucs(self); |
Alex Light | 4060786 | 2019-05-06 18:16:24 +0000 | [diff] [blame] | 1535 | // The overall state across all threads and jvmtiEnvs. This is used to control the state of the |
| 1536 | // instrumentation handlers since we only want each added once. |
Alex Light | eaa4831 | 2019-04-08 16:14:33 +0000 | [diff] [blame] | 1537 | bool old_state; |
| 1538 | bool new_state; |
Alex Light | 4060786 | 2019-05-06 18:16:24 +0000 | [diff] [blame] | 1539 | // The state for just the current 'thread' (including null) across all jvmtiEnvs. This is used to |
| 1540 | // control the deoptimization state since we do refcounting for that and need to perform different |
| 1541 | // actions depending on if the event is limited to a single thread or global. |
| 1542 | bool old_thread_state; |
| 1543 | bool new_thread_state; |
Alex Light | eaa4831 | 2019-04-08 16:14:33 +0000 | [diff] [blame] | 1544 | { |
Alex Light | 6683446 | 2019-04-08 16:28:29 +0000 | [diff] [blame] | 1545 | // From now on we know we cannot get suspended by user-code. |
| 1546 | // NB This does a SuspendCheck (during thread state change) so we need to |
| 1547 | // make sure we don't have the 'suspend_lock' locked here. |
| 1548 | art::ScopedObjectAccess soa(self); |
| 1549 | art::WriterMutexLock el_mu(self, envs_lock_); |
| 1550 | art::MutexLock tll_mu(self, *art::Locks::thread_list_lock_); |
| 1551 | jvmtiError err = ERR(INTERNAL); |
| 1552 | if (thread != nullptr) { |
| 1553 | if (!ThreadUtil::GetAliveNativeThread(thread, soa, &target, &err)) { |
| 1554 | return err; |
| 1555 | } else if (target->IsStillStarting() || |
| 1556 | target->GetState() == art::ThreadState::kStarting) { |
| 1557 | target->Dump(LOG_STREAM(WARNING) << "Is not alive: "); |
| 1558 | return ERR(THREAD_NOT_ALIVE); |
| 1559 | } |
| 1560 | } |
| 1561 | |
Alex Light | 4060786 | 2019-05-06 18:16:24 +0000 | [diff] [blame] | 1562 | |
Alex Light | 6683446 | 2019-04-08 16:28:29 +0000 | [diff] [blame] | 1563 | art::WriterMutexLock ei_mu(self, env->event_info_mutex_); |
Alex Light | 4060786 | 2019-05-06 18:16:24 +0000 | [diff] [blame] | 1564 | old_thread_state = GetThreadEventState(event, target); |
Alex Light | eaa4831 | 2019-04-08 16:14:33 +0000 | [diff] [blame] | 1565 | old_state = global_mask.Test(event); |
| 1566 | if (mode == JVMTI_ENABLE) { |
Alex Light | 6683446 | 2019-04-08 16:28:29 +0000 | [diff] [blame] | 1567 | env->event_masks.EnableEvent(env, target, event); |
Alex Light | eaa4831 | 2019-04-08 16:14:33 +0000 | [diff] [blame] | 1568 | global_mask.Set(event); |
| 1569 | new_state = true; |
Alex Light | 4060786 | 2019-05-06 18:16:24 +0000 | [diff] [blame] | 1570 | new_thread_state = true; |
| 1571 | DCHECK(GetThreadEventState(event, target)); |
Alex Light | eaa4831 | 2019-04-08 16:14:33 +0000 | [diff] [blame] | 1572 | } else { |
| 1573 | DCHECK_EQ(mode, JVMTI_DISABLE); |
Alex Light | c723b81 | 2019-04-08 16:13:24 +0000 | [diff] [blame] | 1574 | |
Alex Light | 6683446 | 2019-04-08 16:28:29 +0000 | [diff] [blame] | 1575 | env->event_masks.DisableEvent(env, target, event); |
Alex Light | eaa4831 | 2019-04-08 16:14:33 +0000 | [diff] [blame] | 1576 | RecalculateGlobalEventMaskLocked(event); |
| 1577 | new_state = global_mask.Test(event); |
Alex Light | 4060786 | 2019-05-06 18:16:24 +0000 | [diff] [blame] | 1578 | new_thread_state = GetThreadEventState(event, target); |
| 1579 | DCHECK(new_state || !new_thread_state); |
Alex Light | 3fa8b6d | 2019-04-03 17:00:02 -0700 | [diff] [blame] | 1580 | } |
Alex Light | eaa4831 | 2019-04-08 16:14:33 +0000 | [diff] [blame] | 1581 | } |
Alex Light | 6683446 | 2019-04-08 16:28:29 +0000 | [diff] [blame] | 1582 | // Handle any special work required for the event type. We still have the |
| 1583 | // user_code_suspend_count_lock_ so there won't be any interleaving here. |
Alex Light | eaa4831 | 2019-04-08 16:14:33 +0000 | [diff] [blame] | 1584 | if (new_state != old_state) { |
Alex Light | 4060786 | 2019-05-06 18:16:24 +0000 | [diff] [blame] | 1585 | HandleEventType(event, mode == JVMTI_ENABLE); |
| 1586 | } |
| 1587 | if (old_thread_state != new_thread_state) { |
| 1588 | return HandleEventDeopt(event, thread, new_thread_state); |
Alex Light | eaa4831 | 2019-04-08 16:14:33 +0000 | [diff] [blame] | 1589 | } |
Alex Light | 6683446 | 2019-04-08 16:28:29 +0000 | [diff] [blame] | 1590 | return OK; |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 1591 | } |
| 1592 | |
Alex Light | 4060786 | 2019-05-06 18:16:24 +0000 | [diff] [blame] | 1593 | bool EventHandler::GetThreadEventState(ArtJvmtiEvent event, art::Thread* thread) { |
| 1594 | for (ArtJvmTiEnv* stored_env : envs) { |
| 1595 | if (stored_env == nullptr) { |
| 1596 | continue; |
| 1597 | } |
| 1598 | auto& masks = stored_env->event_masks; |
| 1599 | if (thread == nullptr && masks.global_event_mask.Test(event)) { |
| 1600 | return true; |
| 1601 | } else if (thread != nullptr) { |
| 1602 | EventMask* mask = masks.GetEventMaskOrNull(thread); |
| 1603 | if (mask != nullptr && mask->Test(event)) { |
| 1604 | return true; |
| 1605 | } |
| 1606 | } |
| 1607 | } |
| 1608 | return false; |
| 1609 | } |
| 1610 | |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 1611 | void EventHandler::HandleBreakpointEventsChanged(bool added) { |
| 1612 | if (added) { |
| 1613 | DeoptManager::Get()->AddDeoptimizationRequester(); |
| 1614 | } else { |
| 1615 | DeoptManager::Get()->RemoveDeoptimizationRequester(); |
| 1616 | } |
| 1617 | } |
| 1618 | |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 1619 | void EventHandler::AddDelayedNonStandardExitEvent(const art::ShadowFrame *frame, |
| 1620 | bool is_object, |
| 1621 | jvalue val) { |
| 1622 | method_trace_listener_->AddDelayedNonStandardExitEvent(frame, is_object, val); |
| 1623 | } |
| 1624 | |
| 1625 | static size_t GetInternalEventIndex(ArtJvmtiEvent event) { |
| 1626 | CHECK(IsInternalEvent(event)); |
| 1627 | return static_cast<size_t>(event) - static_cast<size_t>(ArtJvmtiEvent::kMinInternalEventTypeVal); |
| 1628 | } |
| 1629 | |
| 1630 | int32_t EventHandler::DecrInternalEventThreadRefcount(ArtJvmtiEvent event, art::Thread* target) { |
| 1631 | return --GetInternalEventThreadRefcount(event, target); |
| 1632 | } |
| 1633 | |
| 1634 | int32_t EventHandler::IncrInternalEventThreadRefcount(ArtJvmtiEvent event, art::Thread* target) { |
| 1635 | return ++GetInternalEventThreadRefcount(event, target); |
| 1636 | } |
| 1637 | |
| 1638 | int32_t& EventHandler::GetInternalEventThreadRefcount(ArtJvmtiEvent event, art::Thread* target) { |
| 1639 | auto& refs = internal_event_thread_refcount_[GetInternalEventIndex(event)]; |
| 1640 | UniqueThread target_ut{target, target->GetTid()}; |
| 1641 | if (refs.find(target_ut) == refs.end()) { |
| 1642 | refs.insert({target_ut, 0}); |
| 1643 | } |
| 1644 | return refs.at(target_ut); |
| 1645 | } |
| 1646 | |
| 1647 | int32_t EventHandler::DecrInternalEventRefcount(ArtJvmtiEvent event) { |
| 1648 | return --internal_event_refcount_[GetInternalEventIndex(event)]; |
| 1649 | } |
| 1650 | |
| 1651 | int32_t EventHandler::IncrInternalEventRefcount(ArtJvmtiEvent event) { |
| 1652 | return ++internal_event_refcount_[GetInternalEventIndex(event)]; |
| 1653 | } |
| 1654 | |
| 1655 | int32_t EventHandler::GetInternalEventRefcount(ArtJvmtiEvent event) const { |
| 1656 | return internal_event_refcount_[GetInternalEventIndex(event)]; |
| 1657 | } |
| 1658 | |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 1659 | void EventHandler::Shutdown() { |
| 1660 | // Need to remove the method_trace_listener_ if it's there. |
| 1661 | art::Thread* self = art::Thread::Current(); |
| 1662 | art::gc::ScopedGCCriticalSection gcs(self, |
| 1663 | art::gc::kGcCauseInstrumentation, |
| 1664 | art::gc::kCollectorTypeInstrumentation); |
| 1665 | art::ScopedSuspendAll ssa("jvmti method tracing uninstallation"); |
| 1666 | // Just remove every possible event. |
| 1667 | art::Runtime::Current()->GetInstrumentation()->RemoveListener(method_trace_listener_.get(), ~0); |
Alex Light | 986914b | 2019-11-19 01:12:25 +0000 | [diff] [blame^] | 1668 | AllocationManager::Get()->RemoveAllocListener(); |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 1669 | } |
| 1670 | |
Alex Light | 0e84118 | 2018-02-12 17:42:50 +0000 | [diff] [blame] | 1671 | EventHandler::EventHandler() |
Alex Light | 6683446 | 2019-04-08 16:28:29 +0000 | [diff] [blame] | 1672 | : envs_lock_("JVMTI Environment List Lock", art::LockLevel::kPostMutatorTopLockLevel), |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 1673 | frame_pop_enabled(false), |
| 1674 | internal_event_refcount_({0}) { |
Alex Light | 986914b | 2019-11-19 01:12:25 +0000 | [diff] [blame^] | 1675 | alloc_listener_.reset(new JvmtiEventAllocationListener(this)); |
| 1676 | AllocationManager::Get()->SetAllocListener(alloc_listener_.get()); |
Alex Light | 8c2b929 | 2017-11-09 13:21:01 -0800 | [diff] [blame] | 1677 | ddm_listener_.reset(new JvmtiDdmChunkListener(this)); |
Andreas Gampe | 9b8c588 | 2016-10-21 15:27:46 -0700 | [diff] [blame] | 1678 | gc_pause_listener_.reset(new JvmtiGcPauseListener(this)); |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 1679 | method_trace_listener_.reset(new JvmtiMethodTraceListener(this)); |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 1680 | monitor_listener_.reset(new JvmtiMonitorListener(this)); |
Charles Munger | 5cc0e75 | 2018-11-09 12:30:46 -0800 | [diff] [blame] | 1681 | park_listener_.reset(new JvmtiParkListener(this)); |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 1682 | } |
| 1683 | |
| 1684 | EventHandler::~EventHandler() { |
| 1685 | } |
| 1686 | |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 1687 | } // namespace openjdkjvmti |