blob: e36b947b53a4d431f7803e9ee0a207fddb1ae05c [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"
36#include "runtime.h"
37#include "runtime_callbacks.h"
38#include "scoped_thread_state_change-inl.h"
39#include "thread-inl.h"
40#include "thread_list.h"
41
42namespace openjdkjvmti {
43
44jvmtiPhase PhaseUtil::current_phase_ = static_cast<jvmtiPhase>(0);
45
46struct PhaseUtil::PhaseCallback : public art::RuntimePhaseCallback {
47 void NextRuntimePhase(RuntimePhase phase) OVERRIDE {
48 // TODO: Events.
49 switch (phase) {
50 case RuntimePhase::kInitialAgents:
51 PhaseUtil::current_phase_ = JVMTI_PHASE_PRIMORDIAL;
52 break;
53 case RuntimePhase::kStart:
54 PhaseUtil::current_phase_ = JVMTI_PHASE_START;
55 break;
56 case RuntimePhase::kInit:
57 PhaseUtil::current_phase_ = JVMTI_PHASE_LIVE;
58 break;
59 case RuntimePhase::kDeath:
60 PhaseUtil::current_phase_ = JVMTI_PHASE_DEAD;
61 break;
62 }
63 }
64};
65
66PhaseUtil::PhaseCallback gPhaseCallback;
67
68jvmtiError PhaseUtil::GetPhase(jvmtiEnv* env ATTRIBUTE_UNUSED, jvmtiPhase* phase_ptr) {
69 if (phase_ptr == nullptr) {
70 return ERR(NULL_POINTER);
71 }
72 jvmtiPhase now = PhaseUtil::current_phase_;
73 DCHECK(now == JVMTI_PHASE_ONLOAD ||
74 now == JVMTI_PHASE_PRIMORDIAL ||
75 now == JVMTI_PHASE_START ||
76 now == JVMTI_PHASE_LIVE ||
77 now == JVMTI_PHASE_DEAD);
78 *phase_ptr = now;
79 return ERR(NONE);
80}
81
82void PhaseUtil::SetToOnLoad() {
83 DCHECK_EQ(0u, static_cast<size_t>(PhaseUtil::current_phase_));
84 PhaseUtil::current_phase_ = JVMTI_PHASE_ONLOAD;
85}
86
87void PhaseUtil::SetToPrimordial() {
88 DCHECK_EQ(static_cast<size_t>(JVMTI_PHASE_ONLOAD), static_cast<size_t>(PhaseUtil::current_phase_));
89 PhaseUtil::current_phase_ = JVMTI_PHASE_ONLOAD;
90}
91
92void PhaseUtil::SetToLive() {
93 DCHECK_EQ(static_cast<size_t>(0), static_cast<size_t>(PhaseUtil::current_phase_));
94 PhaseUtil::current_phase_ = JVMTI_PHASE_LIVE;
95}
96
97void PhaseUtil::Register() {
98 art::ScopedThreadStateChange stsc(art::Thread::Current(),
99 art::ThreadState::kWaitingForDebuggerToAttach);
100 art::ScopedSuspendAll ssa("Add phase callback");
101 art::Runtime::Current()->GetRuntimeCallbacks()->AddRuntimePhaseCallback(&gPhaseCallback);
102}
103
104
105} // namespace openjdkjvmti