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 | 96eca78 | 2017-01-19 19:45:30 -0800 | [diff] [blame] | 37 | #include "runtime.h" |
| 38 | #include "runtime_callbacks.h" |
Andreas Gampe | 3a7eb14 | 2017-01-19 21:59:22 -0800 | [diff] [blame] | 39 | #include "ScopedLocalRef.h" |
Andreas Gampe | 96eca78 | 2017-01-19 19:45:30 -0800 | [diff] [blame] | 40 | #include "scoped_thread_state_change-inl.h" |
| 41 | #include "thread-inl.h" |
| 42 | #include "thread_list.h" |
| 43 | |
| 44 | namespace openjdkjvmti { |
| 45 | |
| 46 | jvmtiPhase PhaseUtil::current_phase_ = static_cast<jvmtiPhase>(0); |
| 47 | |
| 48 | struct PhaseUtil::PhaseCallback : public art::RuntimePhaseCallback { |
Andreas Gampe | 3a7eb14 | 2017-01-19 21:59:22 -0800 | [diff] [blame] | 49 | inline static JNIEnv* GetJniEnv() { |
| 50 | return reinterpret_cast<JNIEnv*>(art::Thread::Current()->GetJniEnv()); |
| 51 | } |
| 52 | |
| 53 | inline static jthread GetCurrentJThread() { |
| 54 | art::ScopedObjectAccess soa(art::Thread::Current()); |
| 55 | return soa.AddLocalReference<jthread>(soa.Self()->GetPeer()); |
| 56 | } |
| 57 | |
Andreas Gampe | e637746 | 2017-01-20 17:37:50 -0800 | [diff] [blame^] | 58 | void NextRuntimePhase(RuntimePhase phase) REQUIRES_SHARED(art::Locks::mutator_lock_) OVERRIDE { |
Andreas Gampe | 96eca78 | 2017-01-19 19:45:30 -0800 | [diff] [blame] | 59 | // TODO: Events. |
| 60 | switch (phase) { |
| 61 | case RuntimePhase::kInitialAgents: |
| 62 | PhaseUtil::current_phase_ = JVMTI_PHASE_PRIMORDIAL; |
| 63 | break; |
| 64 | case RuntimePhase::kStart: |
Andreas Gampe | e637746 | 2017-01-20 17:37:50 -0800 | [diff] [blame^] | 65 | { |
| 66 | art::ScopedThreadSuspension sts(art::Thread::Current(), art::ThreadState::kNative); |
| 67 | event_handler->DispatchEvent(nullptr, ArtJvmtiEvent::kVmStart, GetJniEnv()); |
| 68 | PhaseUtil::current_phase_ = JVMTI_PHASE_START; |
| 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 | { |
| 73 | ScopedLocalRef<jthread> thread(GetJniEnv(), GetCurrentJThread()); |
Andreas Gampe | e637746 | 2017-01-20 17:37:50 -0800 | [diff] [blame^] | 74 | art::ScopedThreadSuspension sts(art::Thread::Current(), art::ThreadState::kNative); |
Andreas Gampe | 3a7eb14 | 2017-01-19 21:59:22 -0800 | [diff] [blame] | 75 | event_handler->DispatchEvent(nullptr, |
| 76 | ArtJvmtiEvent::kVmInit, |
| 77 | GetJniEnv(), |
| 78 | thread.get()); |
| 79 | PhaseUtil::current_phase_ = JVMTI_PHASE_LIVE; |
| 80 | } |
Andreas Gampe | 96eca78 | 2017-01-19 19:45:30 -0800 | [diff] [blame] | 81 | break; |
| 82 | case RuntimePhase::kDeath: |
Andreas Gampe | e637746 | 2017-01-20 17:37:50 -0800 | [diff] [blame^] | 83 | { |
| 84 | art::ScopedThreadSuspension sts(art::Thread::Current(), art::ThreadState::kNative); |
| 85 | event_handler->DispatchEvent(nullptr, ArtJvmtiEvent::kVmDeath, GetJniEnv()); |
| 86 | PhaseUtil::current_phase_ = JVMTI_PHASE_DEAD; |
| 87 | } |
Andreas Gampe | 3a7eb14 | 2017-01-19 21:59:22 -0800 | [diff] [blame] | 88 | // TODO: Block events now. |
Andreas Gampe | 96eca78 | 2017-01-19 19:45:30 -0800 | [diff] [blame] | 89 | break; |
| 90 | } |
| 91 | } |
Andreas Gampe | 3a7eb14 | 2017-01-19 21:59:22 -0800 | [diff] [blame] | 92 | |
| 93 | EventHandler* event_handler = nullptr; |
Andreas Gampe | 96eca78 | 2017-01-19 19:45:30 -0800 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | PhaseUtil::PhaseCallback gPhaseCallback; |
| 97 | |
| 98 | jvmtiError PhaseUtil::GetPhase(jvmtiEnv* env ATTRIBUTE_UNUSED, jvmtiPhase* phase_ptr) { |
| 99 | if (phase_ptr == nullptr) { |
| 100 | return ERR(NULL_POINTER); |
| 101 | } |
| 102 | jvmtiPhase now = PhaseUtil::current_phase_; |
| 103 | DCHECK(now == JVMTI_PHASE_ONLOAD || |
| 104 | now == JVMTI_PHASE_PRIMORDIAL || |
| 105 | now == JVMTI_PHASE_START || |
| 106 | now == JVMTI_PHASE_LIVE || |
| 107 | now == JVMTI_PHASE_DEAD); |
| 108 | *phase_ptr = now; |
| 109 | return ERR(NONE); |
| 110 | } |
| 111 | |
| 112 | void PhaseUtil::SetToOnLoad() { |
| 113 | DCHECK_EQ(0u, static_cast<size_t>(PhaseUtil::current_phase_)); |
| 114 | PhaseUtil::current_phase_ = JVMTI_PHASE_ONLOAD; |
| 115 | } |
| 116 | |
| 117 | void PhaseUtil::SetToPrimordial() { |
| 118 | DCHECK_EQ(static_cast<size_t>(JVMTI_PHASE_ONLOAD), static_cast<size_t>(PhaseUtil::current_phase_)); |
| 119 | PhaseUtil::current_phase_ = JVMTI_PHASE_ONLOAD; |
| 120 | } |
| 121 | |
| 122 | void PhaseUtil::SetToLive() { |
| 123 | DCHECK_EQ(static_cast<size_t>(0), static_cast<size_t>(PhaseUtil::current_phase_)); |
| 124 | PhaseUtil::current_phase_ = JVMTI_PHASE_LIVE; |
| 125 | } |
| 126 | |
Andreas Gampe | 3a7eb14 | 2017-01-19 21:59:22 -0800 | [diff] [blame] | 127 | void PhaseUtil::Register(EventHandler* handler) { |
| 128 | gPhaseCallback.event_handler = handler; |
Andreas Gampe | 96eca78 | 2017-01-19 19:45:30 -0800 | [diff] [blame] | 129 | art::ScopedThreadStateChange stsc(art::Thread::Current(), |
| 130 | art::ThreadState::kWaitingForDebuggerToAttach); |
| 131 | art::ScopedSuspendAll ssa("Add phase callback"); |
| 132 | art::Runtime::Current()->GetRuntimeCallbacks()->AddRuntimePhaseCallback(&gPhaseCallback); |
| 133 | } |
| 134 | |
Andreas Gampe | eafaf57 | 2017-01-20 12:34:15 -0800 | [diff] [blame] | 135 | void PhaseUtil::Unregister() { |
| 136 | art::ScopedThreadStateChange stsc(art::Thread::Current(), |
| 137 | art::ThreadState::kWaitingForDebuggerToAttach); |
| 138 | art::ScopedSuspendAll ssa("Remove phase callback"); |
| 139 | art::Runtime::Current()->GetRuntimeCallbacks()->RemoveRuntimePhaseCallback(&gPhaseCallback); |
| 140 | } |
Andreas Gampe | 96eca78 | 2017-01-19 19:45:30 -0800 | [diff] [blame] | 141 | |
| 142 | } // namespace openjdkjvmti |