Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 1 | /* Copyright (C) 2016 The Android Open Source Project |
| 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 3 | * |
| 4 | * This file implements interfaces from the file jvmti.h. This implementation |
| 5 | * is licensed under the same terms as the file jvmti.h. The |
| 6 | * copyright and license information for the file jvmti.h follows. |
| 7 | * |
| 8 | * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved. |
| 9 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 10 | * |
| 11 | * This code is free software; you can redistribute it and/or modify it |
| 12 | * under the terms of the GNU General Public License version 2 only, as |
| 13 | * published by the Free Software Foundation. Oracle designates this |
| 14 | * particular file as subject to the "Classpath" exception as provided |
| 15 | * by Oracle in the LICENSE file that accompanied this code. |
| 16 | * |
| 17 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 18 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 19 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 20 | * version 2 for more details (a copy is included in the LICENSE file that |
| 21 | * accompanied this code). |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public License version |
| 24 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 25 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 26 | * |
| 27 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 28 | * or visit www.oracle.com if you need additional information or have any |
| 29 | * questions. |
| 30 | */ |
| 31 | |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 32 | #include "events-inl.h" |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 33 | |
| 34 | #include "art_jvmti.h" |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 35 | #include "base/logging.h" |
| 36 | #include "gc/allocation_listener.h" |
Andreas Gampe | 9b8c588 | 2016-10-21 15:27:46 -0700 | [diff] [blame] | 37 | #include "gc/gc_pause_listener.h" |
| 38 | #include "gc/heap.h" |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 39 | #include "instrumentation.h" |
| 40 | #include "jni_env_ext-inl.h" |
| 41 | #include "mirror/class.h" |
| 42 | #include "mirror/object.h" |
| 43 | #include "runtime.h" |
| 44 | #include "ScopedLocalRef.h" |
Andreas Gampe | c02685c | 2016-10-17 17:40:27 -0700 | [diff] [blame] | 45 | #include "scoped_thread_state_change-inl.h" |
| 46 | #include "thread-inl.h" |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 47 | |
| 48 | namespace openjdkjvmti { |
| 49 | |
Alex Light | 73afd32 | 2017-01-18 11:17:47 -0800 | [diff] [blame] | 50 | bool EventMasks::IsEnabledAnywhere(ArtJvmtiEvent event) { |
| 51 | return global_event_mask.Test(event) || unioned_thread_event_mask.Test(event); |
| 52 | } |
| 53 | |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 54 | EventMask& EventMasks::GetEventMask(art::Thread* thread) { |
| 55 | if (thread == nullptr) { |
| 56 | return global_event_mask; |
| 57 | } |
| 58 | |
| 59 | for (auto& pair : thread_event_masks) { |
| 60 | const UniqueThread& unique_thread = pair.first; |
| 61 | if (unique_thread.first == thread && |
| 62 | unique_thread.second == static_cast<uint32_t>(thread->GetTid())) { |
| 63 | return pair.second; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | // TODO: Remove old UniqueThread with the same pointer, if exists. |
| 68 | |
| 69 | thread_event_masks.emplace_back(UniqueThread(thread, thread->GetTid()), EventMask()); |
| 70 | return thread_event_masks.back().second; |
| 71 | } |
| 72 | |
| 73 | EventMask* EventMasks::GetEventMaskOrNull(art::Thread* thread) { |
| 74 | if (thread == nullptr) { |
| 75 | return &global_event_mask; |
| 76 | } |
| 77 | |
| 78 | for (auto& pair : thread_event_masks) { |
| 79 | const UniqueThread& unique_thread = pair.first; |
| 80 | if (unique_thread.first == thread && |
| 81 | unique_thread.second == static_cast<uint32_t>(thread->GetTid())) { |
| 82 | return &pair.second; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | return nullptr; |
| 87 | } |
| 88 | |
| 89 | |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 90 | void EventMasks::EnableEvent(art::Thread* thread, ArtJvmtiEvent event) { |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 91 | DCHECK(EventMask::EventIsInRange(event)); |
| 92 | GetEventMask(thread).Set(event); |
| 93 | if (thread != nullptr) { |
| 94 | unioned_thread_event_mask.Set(event, true); |
| 95 | } |
| 96 | } |
| 97 | |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 98 | void EventMasks::DisableEvent(art::Thread* thread, ArtJvmtiEvent event) { |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 99 | DCHECK(EventMask::EventIsInRange(event)); |
| 100 | GetEventMask(thread).Set(event, false); |
| 101 | if (thread != nullptr) { |
| 102 | // Regenerate union for the event. |
| 103 | bool union_value = false; |
| 104 | for (auto& pair : thread_event_masks) { |
| 105 | union_value |= pair.second.Test(event); |
| 106 | if (union_value) { |
| 107 | break; |
| 108 | } |
| 109 | } |
| 110 | unioned_thread_event_mask.Set(event, union_value); |
| 111 | } |
| 112 | } |
| 113 | |
Alex Light | 73afd32 | 2017-01-18 11:17:47 -0800 | [diff] [blame] | 114 | void EventMasks::HandleChangedCapabilities(const jvmtiCapabilities& caps, bool caps_added) { |
| 115 | if (UNLIKELY(caps.can_retransform_classes == 1)) { |
| 116 | // If we are giving this env the retransform classes cap we need to switch all events of |
| 117 | // NonTransformable to Transformable and vice versa. |
| 118 | ArtJvmtiEvent to_remove = caps_added ? ArtJvmtiEvent::kClassFileLoadHookNonRetransformable |
| 119 | : ArtJvmtiEvent::kClassFileLoadHookRetransformable; |
| 120 | ArtJvmtiEvent to_add = caps_added ? ArtJvmtiEvent::kClassFileLoadHookRetransformable |
| 121 | : ArtJvmtiEvent::kClassFileLoadHookNonRetransformable; |
| 122 | if (global_event_mask.Test(to_remove)) { |
| 123 | CHECK(!global_event_mask.Test(to_add)); |
| 124 | global_event_mask.Set(to_remove, false); |
| 125 | global_event_mask.Set(to_add, true); |
| 126 | } |
| 127 | |
| 128 | if (unioned_thread_event_mask.Test(to_remove)) { |
| 129 | CHECK(!unioned_thread_event_mask.Test(to_add)); |
| 130 | unioned_thread_event_mask.Set(to_remove, false); |
| 131 | unioned_thread_event_mask.Set(to_add, true); |
| 132 | } |
| 133 | for (auto thread_mask : thread_event_masks) { |
| 134 | if (thread_mask.second.Test(to_remove)) { |
| 135 | CHECK(!thread_mask.second.Test(to_add)); |
| 136 | thread_mask.second.Set(to_remove, false); |
| 137 | thread_mask.second.Set(to_add, true); |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 143 | void EventHandler::RegisterArtJvmTiEnv(ArtJvmTiEnv* env) { |
| 144 | envs.push_back(env); |
| 145 | } |
| 146 | |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 147 | static bool IsThreadControllable(ArtJvmtiEvent event) { |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 148 | switch (event) { |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 149 | case ArtJvmtiEvent::kVmInit: |
| 150 | case ArtJvmtiEvent::kVmStart: |
| 151 | case ArtJvmtiEvent::kVmDeath: |
| 152 | case ArtJvmtiEvent::kThreadStart: |
| 153 | case ArtJvmtiEvent::kCompiledMethodLoad: |
| 154 | case ArtJvmtiEvent::kCompiledMethodUnload: |
| 155 | case ArtJvmtiEvent::kDynamicCodeGenerated: |
| 156 | case ArtJvmtiEvent::kDataDumpRequest: |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 157 | return false; |
| 158 | |
| 159 | default: |
| 160 | return true; |
| 161 | } |
| 162 | } |
| 163 | |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 164 | class JvmtiAllocationListener : public art::gc::AllocationListener { |
| 165 | public: |
| 166 | explicit JvmtiAllocationListener(EventHandler* handler) : handler_(handler) {} |
| 167 | |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 168 | void ObjectAllocated(art::Thread* self, art::ObjPtr<art::mirror::Object>* obj, size_t byte_count) |
Andreas Gampe | 9b8c588 | 2016-10-21 15:27:46 -0700 | [diff] [blame] | 169 | OVERRIDE REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 170 | DCHECK_EQ(self, art::Thread::Current()); |
| 171 | |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 172 | if (handler_->IsEventEnabledAnywhere(ArtJvmtiEvent::kVmObjectAlloc)) { |
Mathieu Chartier | a711804 | 2016-10-12 15:45:58 -0700 | [diff] [blame] | 173 | art::StackHandleScope<1> hs(self); |
| 174 | auto h = hs.NewHandleWrapper(obj); |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 175 | // jvmtiEventVMObjectAlloc parameters: |
| 176 | // jvmtiEnv *jvmti_env, |
| 177 | // JNIEnv* jni_env, |
| 178 | // jthread thread, |
| 179 | // jobject object, |
| 180 | // jclass object_klass, |
| 181 | // jlong size |
| 182 | art::JNIEnvExt* jni_env = self->GetJniEnv(); |
| 183 | |
| 184 | jthread thread_peer; |
| 185 | if (self->IsStillStarting()) { |
| 186 | thread_peer = nullptr; |
| 187 | } else { |
| 188 | thread_peer = jni_env->AddLocalReference<jthread>(self->GetPeer()); |
| 189 | } |
| 190 | |
| 191 | ScopedLocalRef<jthread> thread(jni_env, thread_peer); |
| 192 | ScopedLocalRef<jobject> object( |
| 193 | jni_env, jni_env->AddLocalReference<jobject>(*obj)); |
| 194 | ScopedLocalRef<jclass> klass( |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 195 | jni_env, jni_env->AddLocalReference<jclass>(obj->Ptr()->GetClass())); |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 196 | |
| 197 | handler_->DispatchEvent(self, |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 198 | ArtJvmtiEvent::kVmObjectAlloc, |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 199 | jni_env, |
| 200 | thread.get(), |
| 201 | object.get(), |
| 202 | klass.get(), |
| 203 | byte_count); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | private: |
| 208 | EventHandler* handler_; |
| 209 | }; |
| 210 | |
| 211 | static void SetupObjectAllocationTracking(art::gc::AllocationListener* listener, bool enable) { |
Andreas Gampe | c02685c | 2016-10-17 17:40:27 -0700 | [diff] [blame] | 212 | // We must not hold the mutator lock here, but if we're in FastJNI, for example, we might. For |
| 213 | // now, do a workaround: (possibly) acquire and release. |
| 214 | art::ScopedObjectAccess soa(art::Thread::Current()); |
| 215 | art::ScopedThreadSuspension sts(soa.Self(), art::ThreadState::kSuspended); |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 216 | if (enable) { |
| 217 | art::Runtime::Current()->GetHeap()->SetAllocationListener(listener); |
| 218 | } else { |
| 219 | art::Runtime::Current()->GetHeap()->RemoveAllocationListener(); |
| 220 | } |
| 221 | } |
| 222 | |
Andreas Gampe | 9b8c588 | 2016-10-21 15:27:46 -0700 | [diff] [blame] | 223 | // Report GC pauses (see spec) as GARBAGE_COLLECTION_START and GARBAGE_COLLECTION_END. |
| 224 | class JvmtiGcPauseListener : public art::gc::GcPauseListener { |
| 225 | public: |
| 226 | explicit JvmtiGcPauseListener(EventHandler* handler) |
| 227 | : handler_(handler), |
| 228 | start_enabled_(false), |
| 229 | finish_enabled_(false) {} |
| 230 | |
| 231 | void StartPause() OVERRIDE { |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 232 | handler_->DispatchEvent(nullptr, ArtJvmtiEvent::kGarbageCollectionStart); |
Andreas Gampe | 9b8c588 | 2016-10-21 15:27:46 -0700 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | void EndPause() OVERRIDE { |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 236 | handler_->DispatchEvent(nullptr, ArtJvmtiEvent::kGarbageCollectionFinish); |
Andreas Gampe | 9b8c588 | 2016-10-21 15:27:46 -0700 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | bool IsEnabled() { |
| 240 | return start_enabled_ || finish_enabled_; |
| 241 | } |
| 242 | |
| 243 | void SetStartEnabled(bool e) { |
| 244 | start_enabled_ = e; |
| 245 | } |
| 246 | |
| 247 | void SetFinishEnabled(bool e) { |
| 248 | finish_enabled_ = e; |
| 249 | } |
| 250 | |
| 251 | private: |
| 252 | EventHandler* handler_; |
| 253 | bool start_enabled_; |
| 254 | bool finish_enabled_; |
| 255 | }; |
| 256 | |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 257 | static void SetupGcPauseTracking(JvmtiGcPauseListener* listener, ArtJvmtiEvent event, bool enable) { |
Andreas Gampe | 9b8c588 | 2016-10-21 15:27:46 -0700 | [diff] [blame] | 258 | bool old_state = listener->IsEnabled(); |
| 259 | |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 260 | if (event == ArtJvmtiEvent::kGarbageCollectionStart) { |
Andreas Gampe | 9b8c588 | 2016-10-21 15:27:46 -0700 | [diff] [blame] | 261 | listener->SetStartEnabled(enable); |
| 262 | } else { |
| 263 | listener->SetFinishEnabled(enable); |
| 264 | } |
| 265 | |
| 266 | bool new_state = listener->IsEnabled(); |
| 267 | |
| 268 | if (old_state != new_state) { |
| 269 | if (new_state) { |
| 270 | art::Runtime::Current()->GetHeap()->SetGcPauseListener(listener); |
| 271 | } else { |
| 272 | art::Runtime::Current()->GetHeap()->RemoveGcPauseListener(); |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 277 | // Handle special work for the given event type, if necessary. |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 278 | void EventHandler::HandleEventType(ArtJvmtiEvent event, bool enable) { |
Andreas Gampe | 9b8c588 | 2016-10-21 15:27:46 -0700 | [diff] [blame] | 279 | switch (event) { |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 280 | case ArtJvmtiEvent::kVmObjectAlloc: |
Andreas Gampe | 9b8c588 | 2016-10-21 15:27:46 -0700 | [diff] [blame] | 281 | SetupObjectAllocationTracking(alloc_listener_.get(), enable); |
| 282 | return; |
| 283 | |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 284 | case ArtJvmtiEvent::kGarbageCollectionStart: |
| 285 | case ArtJvmtiEvent::kGarbageCollectionFinish: |
Andreas Gampe | 9b8c588 | 2016-10-21 15:27:46 -0700 | [diff] [blame] | 286 | SetupGcPauseTracking(gc_pause_listener_.get(), event, enable); |
| 287 | return; |
| 288 | |
| 289 | default: |
| 290 | break; |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 291 | } |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | jvmtiError EventHandler::SetEvent(ArtJvmTiEnv* env, |
| 295 | art::Thread* thread, |
Alex Light | 40d87f4 | 2017-01-18 10:27:06 -0800 | [diff] [blame] | 296 | ArtJvmtiEvent event, |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 297 | jvmtiEventMode mode) { |
| 298 | if (thread != nullptr) { |
| 299 | art::ThreadState state = thread->GetState(); |
| 300 | if (state == art::ThreadState::kStarting || |
| 301 | state == art::ThreadState::kTerminated || |
| 302 | thread->IsStillStarting()) { |
| 303 | return ERR(THREAD_NOT_ALIVE); |
| 304 | } |
| 305 | if (!IsThreadControllable(event)) { |
| 306 | return ERR(ILLEGAL_ARGUMENT); |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | // TODO: Capability check. |
| 311 | |
| 312 | if (mode != JVMTI_ENABLE && mode != JVMTI_DISABLE) { |
| 313 | return ERR(ILLEGAL_ARGUMENT); |
| 314 | } |
| 315 | |
| 316 | if (!EventMask::EventIsInRange(event)) { |
| 317 | return ERR(INVALID_EVENT_TYPE); |
| 318 | } |
| 319 | |
Andreas Gampe | 8b862ff | 2016-10-17 17:49:59 -0700 | [diff] [blame] | 320 | bool old_state = global_mask.Test(event); |
| 321 | |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 322 | if (mode == JVMTI_ENABLE) { |
| 323 | env->event_masks.EnableEvent(thread, event); |
| 324 | global_mask.Set(event); |
| 325 | } else { |
| 326 | DCHECK_EQ(mode, JVMTI_DISABLE); |
| 327 | |
| 328 | env->event_masks.DisableEvent(thread, event); |
Alex Light | 73afd32 | 2017-01-18 11:17:47 -0800 | [diff] [blame] | 329 | RecalculateGlobalEventMask(event); |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 330 | } |
| 331 | |
Andreas Gampe | 8b862ff | 2016-10-17 17:49:59 -0700 | [diff] [blame] | 332 | bool new_state = global_mask.Test(event); |
| 333 | |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 334 | // Handle any special work required for the event type. |
Andreas Gampe | 8b862ff | 2016-10-17 17:49:59 -0700 | [diff] [blame] | 335 | if (new_state != old_state) { |
| 336 | HandleEventType(event, mode == JVMTI_ENABLE); |
| 337 | } |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 338 | |
| 339 | return ERR(NONE); |
| 340 | } |
| 341 | |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 342 | EventHandler::EventHandler() { |
| 343 | alloc_listener_.reset(new JvmtiAllocationListener(this)); |
Andreas Gampe | 9b8c588 | 2016-10-21 15:27:46 -0700 | [diff] [blame] | 344 | gc_pause_listener_.reset(new JvmtiGcPauseListener(this)); |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | EventHandler::~EventHandler() { |
| 348 | } |
| 349 | |
Andreas Gampe | 77708d9 | 2016-10-07 11:48:21 -0700 | [diff] [blame] | 350 | } // namespace openjdkjvmti |