Andreas Gampe | 96eca78 | 2017-01-19 19:45:30 -0800 | [diff] [blame] | 1 | /* Copyright (C) 2017 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 | |
| 32 | #include "ti_phase.h" |
| 33 | |
| 34 | #include "art_jvmti.h" |
| 35 | #include "base/macros.h" |
Andreas Gampe | 3a7eb14 | 2017-01-19 21:59:22 -0800 | [diff] [blame] | 36 | #include "events-inl.h" |
Andreas Gampe | 373a9b5 | 2017-10-18 09:01:57 -0700 | [diff] [blame] | 37 | #include "nativehelper/scoped_local_ref.h" |
Andreas Gampe | 96eca78 | 2017-01-19 19:45:30 -0800 | [diff] [blame] | 38 | #include "runtime.h" |
| 39 | #include "runtime_callbacks.h" |
| 40 | #include "scoped_thread_state_change-inl.h" |
Andreas Gampe | b486a98 | 2017-06-01 13:45:54 -0700 | [diff] [blame] | 41 | #include "thread-current-inl.h" |
Andreas Gampe | 96eca78 | 2017-01-19 19:45:30 -0800 | [diff] [blame] | 42 | #include "thread_list.h" |
Andreas Gampe | db6c2ab | 2017-03-28 17:28:32 -0700 | [diff] [blame] | 43 | #include "ti_thread.h" |
Andreas Gampe | 96eca78 | 2017-01-19 19:45:30 -0800 | [diff] [blame] | 44 | |
| 45 | namespace openjdkjvmti { |
| 46 | |
| 47 | jvmtiPhase PhaseUtil::current_phase_ = static_cast<jvmtiPhase>(0); |
| 48 | |
| 49 | struct PhaseUtil::PhaseCallback : public art::RuntimePhaseCallback { |
Andreas Gampe | 3a7eb14 | 2017-01-19 21:59:22 -0800 | [diff] [blame] | 50 | inline static JNIEnv* GetJniEnv() { |
| 51 | return reinterpret_cast<JNIEnv*>(art::Thread::Current()->GetJniEnv()); |
| 52 | } |
| 53 | |
| 54 | inline static jthread GetCurrentJThread() { |
| 55 | art::ScopedObjectAccess soa(art::Thread::Current()); |
| 56 | return soa.AddLocalReference<jthread>(soa.Self()->GetPeer()); |
| 57 | } |
| 58 | |
Andreas Gampe | e637746 | 2017-01-20 17:37:50 -0800 | [diff] [blame] | 59 | void NextRuntimePhase(RuntimePhase phase) REQUIRES_SHARED(art::Locks::mutator_lock_) OVERRIDE { |
Alex Light | b284f8d | 2017-11-21 00:00:48 +0000 | [diff] [blame] | 60 | art::Thread* self = art::Thread::Current(); |
Andreas Gampe | 96eca78 | 2017-01-19 19:45:30 -0800 | [diff] [blame] | 61 | switch (phase) { |
| 62 | case RuntimePhase::kInitialAgents: |
| 63 | PhaseUtil::current_phase_ = JVMTI_PHASE_PRIMORDIAL; |
| 64 | break; |
| 65 | case RuntimePhase::kStart: |
Andreas Gampe | e637746 | 2017-01-20 17:37:50 -0800 | [diff] [blame] | 66 | { |
Alex Light | bf9e516 | 2017-08-16 16:07:37 -0700 | [diff] [blame] | 67 | PhaseUtil::current_phase_ = JVMTI_PHASE_START; |
Alex Light | b284f8d | 2017-11-21 00:00:48 +0000 | [diff] [blame] | 68 | event_handler->DispatchEvent<ArtJvmtiEvent::kVmStart>(self, GetJniEnv()); |
Andreas Gampe | e637746 | 2017-01-20 17:37:50 -0800 | [diff] [blame] | 69 | } |
Andreas Gampe | 96eca78 | 2017-01-19 19:45:30 -0800 | [diff] [blame] | 70 | break; |
| 71 | case RuntimePhase::kInit: |
Andreas Gampe | 3a7eb14 | 2017-01-19 21:59:22 -0800 | [diff] [blame] | 72 | { |
Andreas Gampe | db6c2ab | 2017-03-28 17:28:32 -0700 | [diff] [blame] | 73 | ThreadUtil::CacheData(); |
Alex Light | bf9e516 | 2017-08-16 16:07:37 -0700 | [diff] [blame] | 74 | PhaseUtil::current_phase_ = JVMTI_PHASE_LIVE; |
Alex Light | 1d8a974 | 2017-08-17 11:12:06 -0700 | [diff] [blame] | 75 | { |
| 76 | ScopedLocalRef<jthread> thread(GetJniEnv(), GetCurrentJThread()); |
Alex Light | b284f8d | 2017-11-21 00:00:48 +0000 | [diff] [blame] | 77 | event_handler->DispatchEvent<ArtJvmtiEvent::kVmInit>(self, GetJniEnv(), thread.get()); |
Alex Light | 1d8a974 | 2017-08-17 11:12:06 -0700 | [diff] [blame] | 78 | } |
| 79 | // We need to have these events be ordered to match behavior expected by some real-world |
| 80 | // agents. The spec does not really require this but compatibility is a useful property to |
| 81 | // maintain. |
| 82 | ThreadUtil::VMInitEventSent(); |
Andreas Gampe | 3a7eb14 | 2017-01-19 21:59:22 -0800 | [diff] [blame] | 83 | } |
Andreas Gampe | 96eca78 | 2017-01-19 19:45:30 -0800 | [diff] [blame] | 84 | break; |
| 85 | case RuntimePhase::kDeath: |
Andreas Gampe | e637746 | 2017-01-20 17:37:50 -0800 | [diff] [blame] | 86 | { |
Alex Light | b284f8d | 2017-11-21 00:00:48 +0000 | [diff] [blame] | 87 | event_handler->DispatchEvent<ArtJvmtiEvent::kVmDeath>(self, GetJniEnv()); |
Andreas Gampe | e637746 | 2017-01-20 17:37:50 -0800 | [diff] [blame] | 88 | PhaseUtil::current_phase_ = JVMTI_PHASE_DEAD; |
| 89 | } |
Andreas Gampe | 3a7eb14 | 2017-01-19 21:59:22 -0800 | [diff] [blame] | 90 | // TODO: Block events now. |
Andreas Gampe | 96eca78 | 2017-01-19 19:45:30 -0800 | [diff] [blame] | 91 | break; |
| 92 | } |
| 93 | } |
Andreas Gampe | 3a7eb14 | 2017-01-19 21:59:22 -0800 | [diff] [blame] | 94 | |
| 95 | EventHandler* event_handler = nullptr; |
Andreas Gampe | 96eca78 | 2017-01-19 19:45:30 -0800 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | PhaseUtil::PhaseCallback gPhaseCallback; |
| 99 | |
| 100 | jvmtiError PhaseUtil::GetPhase(jvmtiEnv* env ATTRIBUTE_UNUSED, jvmtiPhase* phase_ptr) { |
| 101 | if (phase_ptr == nullptr) { |
| 102 | return ERR(NULL_POINTER); |
| 103 | } |
| 104 | jvmtiPhase now = PhaseUtil::current_phase_; |
| 105 | DCHECK(now == JVMTI_PHASE_ONLOAD || |
| 106 | now == JVMTI_PHASE_PRIMORDIAL || |
| 107 | now == JVMTI_PHASE_START || |
| 108 | now == JVMTI_PHASE_LIVE || |
| 109 | now == JVMTI_PHASE_DEAD); |
| 110 | *phase_ptr = now; |
| 111 | return ERR(NONE); |
| 112 | } |
| 113 | |
Andreas Gampe | db6c2ab | 2017-03-28 17:28:32 -0700 | [diff] [blame] | 114 | bool PhaseUtil::IsLivePhase() { |
| 115 | jvmtiPhase now = PhaseUtil::current_phase_; |
| 116 | DCHECK(now == JVMTI_PHASE_ONLOAD || |
| 117 | now == JVMTI_PHASE_PRIMORDIAL || |
| 118 | now == JVMTI_PHASE_START || |
| 119 | now == JVMTI_PHASE_LIVE || |
| 120 | now == JVMTI_PHASE_DEAD); |
| 121 | return now == JVMTI_PHASE_LIVE; |
| 122 | } |
| 123 | |
Andreas Gampe | 96eca78 | 2017-01-19 19:45:30 -0800 | [diff] [blame] | 124 | void PhaseUtil::SetToOnLoad() { |
| 125 | DCHECK_EQ(0u, static_cast<size_t>(PhaseUtil::current_phase_)); |
| 126 | PhaseUtil::current_phase_ = JVMTI_PHASE_ONLOAD; |
| 127 | } |
| 128 | |
| 129 | void PhaseUtil::SetToPrimordial() { |
| 130 | DCHECK_EQ(static_cast<size_t>(JVMTI_PHASE_ONLOAD), static_cast<size_t>(PhaseUtil::current_phase_)); |
| 131 | PhaseUtil::current_phase_ = JVMTI_PHASE_ONLOAD; |
| 132 | } |
| 133 | |
| 134 | void PhaseUtil::SetToLive() { |
| 135 | DCHECK_EQ(static_cast<size_t>(0), static_cast<size_t>(PhaseUtil::current_phase_)); |
Andreas Gampe | db6c2ab | 2017-03-28 17:28:32 -0700 | [diff] [blame] | 136 | ThreadUtil::CacheData(); |
Andreas Gampe | 96eca78 | 2017-01-19 19:45:30 -0800 | [diff] [blame] | 137 | PhaseUtil::current_phase_ = JVMTI_PHASE_LIVE; |
| 138 | } |
| 139 | |
Andreas Gampe | 3a7eb14 | 2017-01-19 21:59:22 -0800 | [diff] [blame] | 140 | void PhaseUtil::Register(EventHandler* handler) { |
| 141 | gPhaseCallback.event_handler = handler; |
Andreas Gampe | 96eca78 | 2017-01-19 19:45:30 -0800 | [diff] [blame] | 142 | art::ScopedThreadStateChange stsc(art::Thread::Current(), |
| 143 | art::ThreadState::kWaitingForDebuggerToAttach); |
| 144 | art::ScopedSuspendAll ssa("Add phase callback"); |
| 145 | art::Runtime::Current()->GetRuntimeCallbacks()->AddRuntimePhaseCallback(&gPhaseCallback); |
| 146 | } |
| 147 | |
Andreas Gampe | eafaf57 | 2017-01-20 12:34:15 -0800 | [diff] [blame] | 148 | void PhaseUtil::Unregister() { |
| 149 | art::ScopedThreadStateChange stsc(art::Thread::Current(), |
| 150 | art::ThreadState::kWaitingForDebuggerToAttach); |
| 151 | art::ScopedSuspendAll ssa("Remove phase callback"); |
| 152 | art::Runtime::Current()->GetRuntimeCallbacks()->RemoveRuntimePhaseCallback(&gPhaseCallback); |
| 153 | } |
Andreas Gampe | 96eca78 | 2017-01-19 19:45:30 -0800 | [diff] [blame] | 154 | |
Andreas Gampe | cefaa14 | 2017-01-23 15:04:59 -0800 | [diff] [blame] | 155 | jvmtiPhase PhaseUtil::GetPhaseUnchecked() { |
| 156 | return PhaseUtil::current_phase_; |
| 157 | } |
| 158 | |
Andreas Gampe | 96eca78 | 2017-01-19 19:45:30 -0800 | [diff] [blame] | 159 | } // namespace openjdkjvmti |