blob: 85d6b729c50dee31184158aebcb61765f893ed86 [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 Gampe96eca782017-01-19 19:45:30 -080058 void NextRuntimePhase(RuntimePhase phase) OVERRIDE {
59 // TODO: Events.
60 switch (phase) {
61 case RuntimePhase::kInitialAgents:
62 PhaseUtil::current_phase_ = JVMTI_PHASE_PRIMORDIAL;
63 break;
64 case RuntimePhase::kStart:
Andreas Gampe3a7eb142017-01-19 21:59:22 -080065 event_handler->DispatchEvent(nullptr, ArtJvmtiEvent::kVmStart, GetJniEnv());
Andreas Gampe96eca782017-01-19 19:45:30 -080066 PhaseUtil::current_phase_ = JVMTI_PHASE_START;
67 break;
68 case RuntimePhase::kInit:
Andreas Gampe3a7eb142017-01-19 21:59:22 -080069 {
70 ScopedLocalRef<jthread> thread(GetJniEnv(), GetCurrentJThread());
71 event_handler->DispatchEvent(nullptr,
72 ArtJvmtiEvent::kVmInit,
73 GetJniEnv(),
74 thread.get());
75 PhaseUtil::current_phase_ = JVMTI_PHASE_LIVE;
76 }
Andreas Gampe96eca782017-01-19 19:45:30 -080077 break;
78 case RuntimePhase::kDeath:
Andreas Gampe3a7eb142017-01-19 21:59:22 -080079 event_handler->DispatchEvent(nullptr, ArtJvmtiEvent::kVmDeath, GetJniEnv());
Andreas Gampe96eca782017-01-19 19:45:30 -080080 PhaseUtil::current_phase_ = JVMTI_PHASE_DEAD;
Andreas Gampe3a7eb142017-01-19 21:59:22 -080081 // TODO: Block events now.
Andreas Gampe96eca782017-01-19 19:45:30 -080082 break;
83 }
84 }
Andreas Gampe3a7eb142017-01-19 21:59:22 -080085
86 EventHandler* event_handler = nullptr;
Andreas Gampe96eca782017-01-19 19:45:30 -080087};
88
89PhaseUtil::PhaseCallback gPhaseCallback;
90
91jvmtiError PhaseUtil::GetPhase(jvmtiEnv* env ATTRIBUTE_UNUSED, jvmtiPhase* phase_ptr) {
92 if (phase_ptr == nullptr) {
93 return ERR(NULL_POINTER);
94 }
95 jvmtiPhase now = PhaseUtil::current_phase_;
96 DCHECK(now == JVMTI_PHASE_ONLOAD ||
97 now == JVMTI_PHASE_PRIMORDIAL ||
98 now == JVMTI_PHASE_START ||
99 now == JVMTI_PHASE_LIVE ||
100 now == JVMTI_PHASE_DEAD);
101 *phase_ptr = now;
102 return ERR(NONE);
103}
104
105void PhaseUtil::SetToOnLoad() {
106 DCHECK_EQ(0u, static_cast<size_t>(PhaseUtil::current_phase_));
107 PhaseUtil::current_phase_ = JVMTI_PHASE_ONLOAD;
108}
109
110void PhaseUtil::SetToPrimordial() {
111 DCHECK_EQ(static_cast<size_t>(JVMTI_PHASE_ONLOAD), static_cast<size_t>(PhaseUtil::current_phase_));
112 PhaseUtil::current_phase_ = JVMTI_PHASE_ONLOAD;
113}
114
115void PhaseUtil::SetToLive() {
116 DCHECK_EQ(static_cast<size_t>(0), static_cast<size_t>(PhaseUtil::current_phase_));
117 PhaseUtil::current_phase_ = JVMTI_PHASE_LIVE;
118}
119
Andreas Gampe3a7eb142017-01-19 21:59:22 -0800120void PhaseUtil::Register(EventHandler* handler) {
121 gPhaseCallback.event_handler = handler;
Andreas Gampe96eca782017-01-19 19:45:30 -0800122 art::ScopedThreadStateChange stsc(art::Thread::Current(),
123 art::ThreadState::kWaitingForDebuggerToAttach);
124 art::ScopedSuspendAll ssa("Add phase callback");
125 art::Runtime::Current()->GetRuntimeCallbacks()->AddRuntimePhaseCallback(&gPhaseCallback);
126}
127
128
129} // namespace openjdkjvmti