blob: 23df27fbda4c55029c228d44da222cd0d392eded [file] [log] [blame]
Andreas Gampe96eca782017-01-19 19:45:30 -08001/* 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 Gampe3a7eb142017-01-19 21:59:22 -080036#include "events-inl.h"
Andreas Gampe373a9b52017-10-18 09:01:57 -070037#include "nativehelper/scoped_local_ref.h"
Andreas Gampe96eca782017-01-19 19:45:30 -080038#include "runtime.h"
39#include "runtime_callbacks.h"
40#include "scoped_thread_state_change-inl.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070041#include "thread-current-inl.h"
Andreas Gampe96eca782017-01-19 19:45:30 -080042#include "thread_list.h"
Andreas Gampedb6c2ab2017-03-28 17:28:32 -070043#include "ti_thread.h"
Andreas Gampe96eca782017-01-19 19:45:30 -080044
45namespace openjdkjvmti {
46
47jvmtiPhase PhaseUtil::current_phase_ = static_cast<jvmtiPhase>(0);
48
49struct PhaseUtil::PhaseCallback : public art::RuntimePhaseCallback {
Andreas Gampe3a7eb142017-01-19 21:59:22 -080050 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 Gampee6377462017-01-20 17:37:50 -080059 void NextRuntimePhase(RuntimePhase phase) REQUIRES_SHARED(art::Locks::mutator_lock_) OVERRIDE {
Andreas Gampe96eca782017-01-19 19:45:30 -080060 switch (phase) {
61 case RuntimePhase::kInitialAgents:
62 PhaseUtil::current_phase_ = JVMTI_PHASE_PRIMORDIAL;
63 break;
64 case RuntimePhase::kStart:
Andreas Gampee6377462017-01-20 17:37:50 -080065 {
Alex Lightbf9e5162017-08-16 16:07:37 -070066 PhaseUtil::current_phase_ = JVMTI_PHASE_START;
Andreas Gampee6377462017-01-20 17:37:50 -080067 art::ScopedThreadSuspension sts(art::Thread::Current(), art::ThreadState::kNative);
Andreas Gampe983c1752017-01-23 19:46:56 -080068 event_handler->DispatchEvent<ArtJvmtiEvent::kVmStart>(nullptr, GetJniEnv());
Andreas Gampee6377462017-01-20 17:37:50 -080069 }
Andreas Gampe96eca782017-01-19 19:45:30 -080070 break;
71 case RuntimePhase::kInit:
Andreas Gampe3a7eb142017-01-19 21:59:22 -080072 {
Andreas Gampedb6c2ab2017-03-28 17:28:32 -070073 ThreadUtil::CacheData();
Alex Lightbf9e5162017-08-16 16:07:37 -070074 PhaseUtil::current_phase_ = JVMTI_PHASE_LIVE;
Alex Light1d8a9742017-08-17 11:12:06 -070075 {
76 ScopedLocalRef<jthread> thread(GetJniEnv(), GetCurrentJThread());
77 art::ScopedThreadSuspension sts(art::Thread::Current(), art::ThreadState::kNative);
78 event_handler->DispatchEvent<ArtJvmtiEvent::kVmInit>(
79 nullptr, GetJniEnv(), thread.get());
80 }
81 // We need to have these events be ordered to match behavior expected by some real-world
82 // agents. The spec does not really require this but compatibility is a useful property to
83 // maintain.
84 ThreadUtil::VMInitEventSent();
Andreas Gampe3a7eb142017-01-19 21:59:22 -080085 }
Andreas Gampe96eca782017-01-19 19:45:30 -080086 break;
87 case RuntimePhase::kDeath:
Andreas Gampee6377462017-01-20 17:37:50 -080088 {
89 art::ScopedThreadSuspension sts(art::Thread::Current(), art::ThreadState::kNative);
Andreas Gampe983c1752017-01-23 19:46:56 -080090 event_handler->DispatchEvent<ArtJvmtiEvent::kVmDeath>(nullptr, GetJniEnv());
Andreas Gampee6377462017-01-20 17:37:50 -080091 PhaseUtil::current_phase_ = JVMTI_PHASE_DEAD;
92 }
Andreas Gampe3a7eb142017-01-19 21:59:22 -080093 // TODO: Block events now.
Andreas Gampe96eca782017-01-19 19:45:30 -080094 break;
95 }
96 }
Andreas Gampe3a7eb142017-01-19 21:59:22 -080097
98 EventHandler* event_handler = nullptr;
Andreas Gampe96eca782017-01-19 19:45:30 -080099};
100
101PhaseUtil::PhaseCallback gPhaseCallback;
102
103jvmtiError PhaseUtil::GetPhase(jvmtiEnv* env ATTRIBUTE_UNUSED, jvmtiPhase* phase_ptr) {
104 if (phase_ptr == nullptr) {
105 return ERR(NULL_POINTER);
106 }
107 jvmtiPhase now = PhaseUtil::current_phase_;
108 DCHECK(now == JVMTI_PHASE_ONLOAD ||
109 now == JVMTI_PHASE_PRIMORDIAL ||
110 now == JVMTI_PHASE_START ||
111 now == JVMTI_PHASE_LIVE ||
112 now == JVMTI_PHASE_DEAD);
113 *phase_ptr = now;
114 return ERR(NONE);
115}
116
Andreas Gampedb6c2ab2017-03-28 17:28:32 -0700117bool PhaseUtil::IsLivePhase() {
118 jvmtiPhase now = PhaseUtil::current_phase_;
119 DCHECK(now == JVMTI_PHASE_ONLOAD ||
120 now == JVMTI_PHASE_PRIMORDIAL ||
121 now == JVMTI_PHASE_START ||
122 now == JVMTI_PHASE_LIVE ||
123 now == JVMTI_PHASE_DEAD);
124 return now == JVMTI_PHASE_LIVE;
125}
126
Andreas Gampe96eca782017-01-19 19:45:30 -0800127void PhaseUtil::SetToOnLoad() {
128 DCHECK_EQ(0u, static_cast<size_t>(PhaseUtil::current_phase_));
129 PhaseUtil::current_phase_ = JVMTI_PHASE_ONLOAD;
130}
131
132void PhaseUtil::SetToPrimordial() {
133 DCHECK_EQ(static_cast<size_t>(JVMTI_PHASE_ONLOAD), static_cast<size_t>(PhaseUtil::current_phase_));
134 PhaseUtil::current_phase_ = JVMTI_PHASE_ONLOAD;
135}
136
137void PhaseUtil::SetToLive() {
138 DCHECK_EQ(static_cast<size_t>(0), static_cast<size_t>(PhaseUtil::current_phase_));
Andreas Gampedb6c2ab2017-03-28 17:28:32 -0700139 ThreadUtil::CacheData();
Andreas Gampe96eca782017-01-19 19:45:30 -0800140 PhaseUtil::current_phase_ = JVMTI_PHASE_LIVE;
141}
142
Andreas Gampe3a7eb142017-01-19 21:59:22 -0800143void PhaseUtil::Register(EventHandler* handler) {
144 gPhaseCallback.event_handler = handler;
Andreas Gampe96eca782017-01-19 19:45:30 -0800145 art::ScopedThreadStateChange stsc(art::Thread::Current(),
146 art::ThreadState::kWaitingForDebuggerToAttach);
147 art::ScopedSuspendAll ssa("Add phase callback");
148 art::Runtime::Current()->GetRuntimeCallbacks()->AddRuntimePhaseCallback(&gPhaseCallback);
149}
150
Andreas Gampeeafaf572017-01-20 12:34:15 -0800151void PhaseUtil::Unregister() {
152 art::ScopedThreadStateChange stsc(art::Thread::Current(),
153 art::ThreadState::kWaitingForDebuggerToAttach);
154 art::ScopedSuspendAll ssa("Remove phase callback");
155 art::Runtime::Current()->GetRuntimeCallbacks()->RemoveRuntimePhaseCallback(&gPhaseCallback);
156}
Andreas Gampe96eca782017-01-19 19:45:30 -0800157
Andreas Gampecefaa142017-01-23 15:04:59 -0800158jvmtiPhase PhaseUtil::GetPhaseUnchecked() {
159 return PhaseUtil::current_phase_;
160}
161
Andreas Gampe96eca782017-01-19 19:45:30 -0800162} // namespace openjdkjvmti