xref: /aosp_15_r20/art/openjdkjvmti/ti_phase.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /* Copyright (C) 2017 The Android Open Source Project
2*795d594fSAndroid Build Coastguard Worker  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * This file implements interfaces from the file jvmti.h. This implementation
5*795d594fSAndroid Build Coastguard Worker  * is licensed under the same terms as the file jvmti.h.  The
6*795d594fSAndroid Build Coastguard Worker  * copyright and license information for the file jvmti.h follows.
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
9*795d594fSAndroid Build Coastguard Worker  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
10*795d594fSAndroid Build Coastguard Worker  *
11*795d594fSAndroid Build Coastguard Worker  * This code is free software; you can redistribute it and/or modify it
12*795d594fSAndroid Build Coastguard Worker  * under the terms of the GNU General Public License version 2 only, as
13*795d594fSAndroid Build Coastguard Worker  * published by the Free Software Foundation.  Oracle designates this
14*795d594fSAndroid Build Coastguard Worker  * particular file as subject to the "Classpath" exception as provided
15*795d594fSAndroid Build Coastguard Worker  * by Oracle in the LICENSE file that accompanied this code.
16*795d594fSAndroid Build Coastguard Worker  *
17*795d594fSAndroid Build Coastguard Worker  * This code is distributed in the hope that it will be useful, but WITHOUT
18*795d594fSAndroid Build Coastguard Worker  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19*795d594fSAndroid Build Coastguard Worker  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
20*795d594fSAndroid Build Coastguard Worker  * version 2 for more details (a copy is included in the LICENSE file that
21*795d594fSAndroid Build Coastguard Worker  * accompanied this code).
22*795d594fSAndroid Build Coastguard Worker  *
23*795d594fSAndroid Build Coastguard Worker  * You should have received a copy of the GNU General Public License version
24*795d594fSAndroid Build Coastguard Worker  * 2 along with this work; if not, write to the Free Software Foundation,
25*795d594fSAndroid Build Coastguard Worker  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
26*795d594fSAndroid Build Coastguard Worker  *
27*795d594fSAndroid Build Coastguard Worker  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
28*795d594fSAndroid Build Coastguard Worker  * or visit www.oracle.com if you need additional information or have any
29*795d594fSAndroid Build Coastguard Worker  * questions.
30*795d594fSAndroid Build Coastguard Worker  */
31*795d594fSAndroid Build Coastguard Worker 
32*795d594fSAndroid Build Coastguard Worker #include "ti_phase.h"
33*795d594fSAndroid Build Coastguard Worker 
34*795d594fSAndroid Build Coastguard Worker #include "art_jvmti.h"
35*795d594fSAndroid Build Coastguard Worker #include "base/macros.h"
36*795d594fSAndroid Build Coastguard Worker #include "events-inl.h"
37*795d594fSAndroid Build Coastguard Worker #include "nativehelper/scoped_local_ref.h"
38*795d594fSAndroid Build Coastguard Worker #include "runtime.h"
39*795d594fSAndroid Build Coastguard Worker #include "runtime_callbacks.h"
40*795d594fSAndroid Build Coastguard Worker #include "scoped_thread_state_change-inl.h"
41*795d594fSAndroid Build Coastguard Worker #include "thread-current-inl.h"
42*795d594fSAndroid Build Coastguard Worker #include "thread_list.h"
43*795d594fSAndroid Build Coastguard Worker #include "ti_thread.h"
44*795d594fSAndroid Build Coastguard Worker 
45*795d594fSAndroid Build Coastguard Worker namespace openjdkjvmti {
46*795d594fSAndroid Build Coastguard Worker 
47*795d594fSAndroid Build Coastguard Worker jvmtiPhase PhaseUtil::current_phase_ = static_cast<jvmtiPhase>(0);
48*795d594fSAndroid Build Coastguard Worker 
49*795d594fSAndroid Build Coastguard Worker struct PhaseUtil::PhaseCallback : public art::RuntimePhaseCallback {
GetJniEnvopenjdkjvmti::PhaseUtil::PhaseCallback50*795d594fSAndroid Build Coastguard Worker   inline static JNIEnv* GetJniEnv() {
51*795d594fSAndroid Build Coastguard Worker     return reinterpret_cast<JNIEnv*>(art::Thread::Current()->GetJniEnv());
52*795d594fSAndroid Build Coastguard Worker   }
53*795d594fSAndroid Build Coastguard Worker 
GetCurrentJThreadopenjdkjvmti::PhaseUtil::PhaseCallback54*795d594fSAndroid Build Coastguard Worker   inline static jthread GetCurrentJThread() {
55*795d594fSAndroid Build Coastguard Worker     art::ScopedObjectAccess soa(art::Thread::Current());
56*795d594fSAndroid Build Coastguard Worker     return soa.AddLocalReference<jthread>(soa.Self()->GetPeer());
57*795d594fSAndroid Build Coastguard Worker   }
58*795d594fSAndroid Build Coastguard Worker 
NextRuntimePhaseopenjdkjvmti::PhaseUtil::PhaseCallback59*795d594fSAndroid Build Coastguard Worker   void NextRuntimePhase(RuntimePhase phase) REQUIRES_SHARED(art::Locks::mutator_lock_) override {
60*795d594fSAndroid Build Coastguard Worker     art::Thread* self = art::Thread::Current();
61*795d594fSAndroid Build Coastguard Worker     switch (phase) {
62*795d594fSAndroid Build Coastguard Worker       case RuntimePhase::kInitialAgents:
63*795d594fSAndroid Build Coastguard Worker         PhaseUtil::current_phase_ = JVMTI_PHASE_PRIMORDIAL;
64*795d594fSAndroid Build Coastguard Worker         break;
65*795d594fSAndroid Build Coastguard Worker       case RuntimePhase::kStart:
66*795d594fSAndroid Build Coastguard Worker         {
67*795d594fSAndroid Build Coastguard Worker           PhaseUtil::current_phase_ = JVMTI_PHASE_START;
68*795d594fSAndroid Build Coastguard Worker           event_handler->DispatchEvent<ArtJvmtiEvent::kVmStart>(self, GetJniEnv());
69*795d594fSAndroid Build Coastguard Worker         }
70*795d594fSAndroid Build Coastguard Worker         break;
71*795d594fSAndroid Build Coastguard Worker       case RuntimePhase::kInit:
72*795d594fSAndroid Build Coastguard Worker         {
73*795d594fSAndroid Build Coastguard Worker           ThreadUtil::CacheData();
74*795d594fSAndroid Build Coastguard Worker           PhaseUtil::current_phase_ = JVMTI_PHASE_LIVE;
75*795d594fSAndroid Build Coastguard Worker           {
76*795d594fSAndroid Build Coastguard Worker             ScopedLocalRef<jthread> thread(GetJniEnv(), GetCurrentJThread());
77*795d594fSAndroid Build Coastguard Worker             event_handler->DispatchEvent<ArtJvmtiEvent::kVmInit>(self, GetJniEnv(), thread.get());
78*795d594fSAndroid Build Coastguard Worker           }
79*795d594fSAndroid Build Coastguard Worker           // We need to have these events be ordered to match behavior expected by some real-world
80*795d594fSAndroid Build Coastguard Worker           // agents. The spec does not really require this but compatibility is a useful property to
81*795d594fSAndroid Build Coastguard Worker           // maintain.
82*795d594fSAndroid Build Coastguard Worker           ThreadUtil::VMInitEventSent();
83*795d594fSAndroid Build Coastguard Worker         }
84*795d594fSAndroid Build Coastguard Worker         break;
85*795d594fSAndroid Build Coastguard Worker       case RuntimePhase::kDeath:
86*795d594fSAndroid Build Coastguard Worker         {
87*795d594fSAndroid Build Coastguard Worker           event_handler->DispatchEvent<ArtJvmtiEvent::kVmDeath>(self, GetJniEnv());
88*795d594fSAndroid Build Coastguard Worker           PhaseUtil::current_phase_ = JVMTI_PHASE_DEAD;
89*795d594fSAndroid Build Coastguard Worker         }
90*795d594fSAndroid Build Coastguard Worker         // TODO: Block events now.
91*795d594fSAndroid Build Coastguard Worker         break;
92*795d594fSAndroid Build Coastguard Worker     }
93*795d594fSAndroid Build Coastguard Worker   }
94*795d594fSAndroid Build Coastguard Worker 
95*795d594fSAndroid Build Coastguard Worker   EventHandler* event_handler = nullptr;
96*795d594fSAndroid Build Coastguard Worker };
97*795d594fSAndroid Build Coastguard Worker 
98*795d594fSAndroid Build Coastguard Worker PhaseUtil::PhaseCallback gPhaseCallback;
99*795d594fSAndroid Build Coastguard Worker 
GetPhase(jvmtiEnv * env,jvmtiPhase * phase_ptr)100*795d594fSAndroid Build Coastguard Worker jvmtiError PhaseUtil::GetPhase([[maybe_unused]] jvmtiEnv* env, jvmtiPhase* phase_ptr) {
101*795d594fSAndroid Build Coastguard Worker   if (phase_ptr == nullptr) {
102*795d594fSAndroid Build Coastguard Worker     return ERR(NULL_POINTER);
103*795d594fSAndroid Build Coastguard Worker   }
104*795d594fSAndroid Build Coastguard Worker   jvmtiPhase now = PhaseUtil::current_phase_;
105*795d594fSAndroid Build Coastguard Worker   DCHECK(now == JVMTI_PHASE_ONLOAD ||
106*795d594fSAndroid Build Coastguard Worker          now == JVMTI_PHASE_PRIMORDIAL ||
107*795d594fSAndroid Build Coastguard Worker          now == JVMTI_PHASE_START ||
108*795d594fSAndroid Build Coastguard Worker          now == JVMTI_PHASE_LIVE ||
109*795d594fSAndroid Build Coastguard Worker          now == JVMTI_PHASE_DEAD);
110*795d594fSAndroid Build Coastguard Worker   *phase_ptr = now;
111*795d594fSAndroid Build Coastguard Worker   return ERR(NONE);
112*795d594fSAndroid Build Coastguard Worker }
113*795d594fSAndroid Build Coastguard Worker 
IsLivePhase()114*795d594fSAndroid Build Coastguard Worker bool PhaseUtil::IsLivePhase() {
115*795d594fSAndroid Build Coastguard Worker   jvmtiPhase now = PhaseUtil::current_phase_;
116*795d594fSAndroid Build Coastguard Worker   DCHECK(now == JVMTI_PHASE_ONLOAD ||
117*795d594fSAndroid Build Coastguard Worker          now == JVMTI_PHASE_PRIMORDIAL ||
118*795d594fSAndroid Build Coastguard Worker          now == JVMTI_PHASE_START ||
119*795d594fSAndroid Build Coastguard Worker          now == JVMTI_PHASE_LIVE ||
120*795d594fSAndroid Build Coastguard Worker          now == JVMTI_PHASE_DEAD);
121*795d594fSAndroid Build Coastguard Worker   return now == JVMTI_PHASE_LIVE;
122*795d594fSAndroid Build Coastguard Worker }
123*795d594fSAndroid Build Coastguard Worker 
SetToOnLoad()124*795d594fSAndroid Build Coastguard Worker void PhaseUtil::SetToOnLoad() {
125*795d594fSAndroid Build Coastguard Worker   DCHECK_EQ(0u, static_cast<size_t>(PhaseUtil::current_phase_));
126*795d594fSAndroid Build Coastguard Worker   PhaseUtil::current_phase_ = JVMTI_PHASE_ONLOAD;
127*795d594fSAndroid Build Coastguard Worker }
128*795d594fSAndroid Build Coastguard Worker 
SetToPrimordial()129*795d594fSAndroid Build Coastguard Worker void PhaseUtil::SetToPrimordial() {
130*795d594fSAndroid Build Coastguard Worker   DCHECK_EQ(static_cast<size_t>(JVMTI_PHASE_ONLOAD), static_cast<size_t>(PhaseUtil::current_phase_));
131*795d594fSAndroid Build Coastguard Worker   PhaseUtil::current_phase_ = JVMTI_PHASE_ONLOAD;
132*795d594fSAndroid Build Coastguard Worker }
133*795d594fSAndroid Build Coastguard Worker 
SetToLive()134*795d594fSAndroid Build Coastguard Worker void PhaseUtil::SetToLive() {
135*795d594fSAndroid Build Coastguard Worker   DCHECK_EQ(static_cast<size_t>(0), static_cast<size_t>(PhaseUtil::current_phase_));
136*795d594fSAndroid Build Coastguard Worker   ThreadUtil::CacheData();
137*795d594fSAndroid Build Coastguard Worker   PhaseUtil::current_phase_ = JVMTI_PHASE_LIVE;
138*795d594fSAndroid Build Coastguard Worker }
139*795d594fSAndroid Build Coastguard Worker 
Register(EventHandler * handler)140*795d594fSAndroid Build Coastguard Worker void PhaseUtil::Register(EventHandler* handler) {
141*795d594fSAndroid Build Coastguard Worker   gPhaseCallback.event_handler = handler;
142*795d594fSAndroid Build Coastguard Worker   art::ScopedThreadStateChange stsc(art::Thread::Current(),
143*795d594fSAndroid Build Coastguard Worker                                     art::ThreadState::kWaitingForDebuggerToAttach);
144*795d594fSAndroid Build Coastguard Worker   art::ScopedSuspendAll ssa("Add phase callback");
145*795d594fSAndroid Build Coastguard Worker   art::Runtime::Current()->GetRuntimeCallbacks()->AddRuntimePhaseCallback(&gPhaseCallback);
146*795d594fSAndroid Build Coastguard Worker }
147*795d594fSAndroid Build Coastguard Worker 
Unregister()148*795d594fSAndroid Build Coastguard Worker void PhaseUtil::Unregister() {
149*795d594fSAndroid Build Coastguard Worker   art::ScopedThreadStateChange stsc(art::Thread::Current(),
150*795d594fSAndroid Build Coastguard Worker                                     art::ThreadState::kWaitingForDebuggerToAttach);
151*795d594fSAndroid Build Coastguard Worker   art::ScopedSuspendAll ssa("Remove phase callback");
152*795d594fSAndroid Build Coastguard Worker   art::Runtime::Current()->GetRuntimeCallbacks()->RemoveRuntimePhaseCallback(&gPhaseCallback);
153*795d594fSAndroid Build Coastguard Worker }
154*795d594fSAndroid Build Coastguard Worker 
GetPhaseUnchecked()155*795d594fSAndroid Build Coastguard Worker jvmtiPhase PhaseUtil::GetPhaseUnchecked() {
156*795d594fSAndroid Build Coastguard Worker   return PhaseUtil::current_phase_;
157*795d594fSAndroid Build Coastguard Worker }
158*795d594fSAndroid Build Coastguard Worker 
159*795d594fSAndroid Build Coastguard Worker }  // namespace openjdkjvmti
160