blob: 60371cfafe2aa5ff647d5fcb60460e39a87c996d [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 Gampe96eca782017-01-19 19:45:30 -080037#include "runtime.h"
38#include "runtime_callbacks.h"
Andreas Gampe3a7eb142017-01-19 21:59:22 -080039#include "ScopedLocalRef.h"
Andreas Gampe96eca782017-01-19 19:45:30 -080040#include "scoped_thread_state_change-inl.h"
41#include "thread-inl.h"
42#include "thread_list.h"
43
44namespace openjdkjvmti {
45
46jvmtiPhase PhaseUtil::current_phase_ = static_cast<jvmtiPhase>(0);
47
48struct PhaseUtil::PhaseCallback : public art::RuntimePhaseCallback {
Andreas Gampe3a7eb142017-01-19 21:59:22 -080049 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 Gampee6377462017-01-20 17:37:50 -080058 void NextRuntimePhase(RuntimePhase phase) REQUIRES_SHARED(art::Locks::mutator_lock_) OVERRIDE {
Andreas Gampe96eca782017-01-19 19:45:30 -080059 // TODO: Events.
60 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 {
66 art::ScopedThreadSuspension sts(art::Thread::Current(), art::ThreadState::kNative);
Andreas Gampe983c1752017-01-23 19:46:56 -080067 event_handler->DispatchEvent<ArtJvmtiEvent::kVmStart>(nullptr, GetJniEnv());
Andreas Gampee6377462017-01-20 17:37:50 -080068 PhaseUtil::current_phase_ = JVMTI_PHASE_START;
69 }
Andreas Gampe96eca782017-01-19 19:45:30 -080070 break;
71 case RuntimePhase::kInit:
Andreas Gampe3a7eb142017-01-19 21:59:22 -080072 {
73 ScopedLocalRef<jthread> thread(GetJniEnv(), GetCurrentJThread());
Andreas Gampee6377462017-01-20 17:37:50 -080074 art::ScopedThreadSuspension sts(art::Thread::Current(), art::ThreadState::kNative);
Andreas Gampe983c1752017-01-23 19:46:56 -080075 event_handler->DispatchEvent<ArtJvmtiEvent::kVmInit>(nullptr, GetJniEnv(), thread.get());
Andreas Gampe3a7eb142017-01-19 21:59:22 -080076 PhaseUtil::current_phase_ = JVMTI_PHASE_LIVE;
77 }
Andreas Gampe96eca782017-01-19 19:45:30 -080078 break;
79 case RuntimePhase::kDeath:
Andreas Gampee6377462017-01-20 17:37:50 -080080 {
81 art::ScopedThreadSuspension sts(art::Thread::Current(), art::ThreadState::kNative);
Andreas Gampe983c1752017-01-23 19:46:56 -080082 event_handler->DispatchEvent<ArtJvmtiEvent::kVmDeath>(nullptr, GetJniEnv());
Andreas Gampee6377462017-01-20 17:37:50 -080083 PhaseUtil::current_phase_ = JVMTI_PHASE_DEAD;
84 }
Andreas Gampe3a7eb142017-01-19 21:59:22 -080085 // TODO: Block events now.
Andreas Gampe96eca782017-01-19 19:45:30 -080086 break;
87 }
88 }
Andreas Gampe3a7eb142017-01-19 21:59:22 -080089
90 EventHandler* event_handler = nullptr;
Andreas Gampe96eca782017-01-19 19:45:30 -080091};
92
93PhaseUtil::PhaseCallback gPhaseCallback;
94
95jvmtiError PhaseUtil::GetPhase(jvmtiEnv* env ATTRIBUTE_UNUSED, jvmtiPhase* phase_ptr) {
96 if (phase_ptr == nullptr) {
97 return ERR(NULL_POINTER);
98 }
99 jvmtiPhase now = PhaseUtil::current_phase_;
100 DCHECK(now == JVMTI_PHASE_ONLOAD ||
101 now == JVMTI_PHASE_PRIMORDIAL ||
102 now == JVMTI_PHASE_START ||
103 now == JVMTI_PHASE_LIVE ||
104 now == JVMTI_PHASE_DEAD);
105 *phase_ptr = now;
106 return ERR(NONE);
107}
108
109void PhaseUtil::SetToOnLoad() {
110 DCHECK_EQ(0u, static_cast<size_t>(PhaseUtil::current_phase_));
111 PhaseUtil::current_phase_ = JVMTI_PHASE_ONLOAD;
112}
113
114void PhaseUtil::SetToPrimordial() {
115 DCHECK_EQ(static_cast<size_t>(JVMTI_PHASE_ONLOAD), static_cast<size_t>(PhaseUtil::current_phase_));
116 PhaseUtil::current_phase_ = JVMTI_PHASE_ONLOAD;
117}
118
119void PhaseUtil::SetToLive() {
120 DCHECK_EQ(static_cast<size_t>(0), static_cast<size_t>(PhaseUtil::current_phase_));
121 PhaseUtil::current_phase_ = JVMTI_PHASE_LIVE;
122}
123
Andreas Gampe3a7eb142017-01-19 21:59:22 -0800124void PhaseUtil::Register(EventHandler* handler) {
125 gPhaseCallback.event_handler = handler;
Andreas Gampe96eca782017-01-19 19:45:30 -0800126 art::ScopedThreadStateChange stsc(art::Thread::Current(),
127 art::ThreadState::kWaitingForDebuggerToAttach);
128 art::ScopedSuspendAll ssa("Add phase callback");
129 art::Runtime::Current()->GetRuntimeCallbacks()->AddRuntimePhaseCallback(&gPhaseCallback);
130}
131
Andreas Gampeeafaf572017-01-20 12:34:15 -0800132void PhaseUtil::Unregister() {
133 art::ScopedThreadStateChange stsc(art::Thread::Current(),
134 art::ThreadState::kWaitingForDebuggerToAttach);
135 art::ScopedSuspendAll ssa("Remove phase callback");
136 art::Runtime::Current()->GetRuntimeCallbacks()->RemoveRuntimePhaseCallback(&gPhaseCallback);
137}
Andreas Gampe96eca782017-01-19 19:45:30 -0800138
Andreas Gampecefaa142017-01-23 15:04:59 -0800139jvmtiPhase PhaseUtil::GetPhaseUnchecked() {
140 return PhaseUtil::current_phase_;
141}
142
Andreas Gampe96eca782017-01-19 19:45:30 -0800143} // namespace openjdkjvmti