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 <functional>
33*795d594fSAndroid Build Coastguard Worker
34*795d594fSAndroid Build Coastguard Worker #include "ti_breakpoint.h"
35*795d594fSAndroid Build Coastguard Worker
36*795d594fSAndroid Build Coastguard Worker #include "art_jvmti.h"
37*795d594fSAndroid Build Coastguard Worker #include "art_method-inl.h"
38*795d594fSAndroid Build Coastguard Worker #include "base/mutex-inl.h"
39*795d594fSAndroid Build Coastguard Worker #include "base/pointer_size.h"
40*795d594fSAndroid Build Coastguard Worker #include "deopt_manager.h"
41*795d594fSAndroid Build Coastguard Worker #include "dex/dex_file_annotations.h"
42*795d594fSAndroid Build Coastguard Worker #include "dex/modifiers.h"
43*795d594fSAndroid Build Coastguard Worker #include "events-inl.h"
44*795d594fSAndroid Build Coastguard Worker #include "jni/jni_internal.h"
45*795d594fSAndroid Build Coastguard Worker #include "mirror/class-inl.h"
46*795d594fSAndroid Build Coastguard Worker #include "mirror/object_array-inl.h"
47*795d594fSAndroid Build Coastguard Worker #include "nativehelper/scoped_local_ref.h"
48*795d594fSAndroid Build Coastguard Worker #include "runtime_callbacks.h"
49*795d594fSAndroid Build Coastguard Worker #include "scoped_thread_state_change-inl.h"
50*795d594fSAndroid Build Coastguard Worker #include "thread-current-inl.h"
51*795d594fSAndroid Build Coastguard Worker #include "thread_list.h"
52*795d594fSAndroid Build Coastguard Worker #include "ti_phase.h"
53*795d594fSAndroid Build Coastguard Worker
54*795d594fSAndroid Build Coastguard Worker namespace openjdkjvmti {
55*795d594fSAndroid Build Coastguard Worker
56*795d594fSAndroid Build Coastguard Worker class JvmtiBreakpointReflectionSource : public art::ReflectionSourceInfo {
57*795d594fSAndroid Build Coastguard Worker public:
JvmtiBreakpointReflectionSource(size_t pc,art::ArtMethod * m)58*795d594fSAndroid Build Coastguard Worker JvmtiBreakpointReflectionSource(size_t pc, art::ArtMethod* m)
59*795d594fSAndroid Build Coastguard Worker : art::ReflectionSourceInfo(art::ReflectionSourceType::kSourceMiscInternal),
60*795d594fSAndroid Build Coastguard Worker pc_(pc),
61*795d594fSAndroid Build Coastguard Worker m_(m) {}
62*795d594fSAndroid Build Coastguard Worker
Describe(std::ostream & os) const63*795d594fSAndroid Build Coastguard Worker void Describe(std::ostream& os) const override REQUIRES_SHARED(art::Locks::mutator_lock_) {
64*795d594fSAndroid Build Coastguard Worker art::ReflectionSourceInfo::Describe(os);
65*795d594fSAndroid Build Coastguard Worker os << " jvmti Breakpoint Method=" << m_->PrettyMethod() << " PC=" << pc_;
66*795d594fSAndroid Build Coastguard Worker }
67*795d594fSAndroid Build Coastguard Worker
68*795d594fSAndroid Build Coastguard Worker private:
69*795d594fSAndroid Build Coastguard Worker size_t pc_;
70*795d594fSAndroid Build Coastguard Worker art::ArtMethod* m_;
71*795d594fSAndroid Build Coastguard Worker };
72*795d594fSAndroid Build Coastguard Worker
73*795d594fSAndroid Build Coastguard Worker class BreakpointReflectiveValueCallback : public art::ReflectiveValueVisitCallback {
74*795d594fSAndroid Build Coastguard Worker public:
VisitReflectiveTargets(art::ReflectiveValueVisitor * visitor)75*795d594fSAndroid Build Coastguard Worker void VisitReflectiveTargets(art::ReflectiveValueVisitor* visitor)
76*795d594fSAndroid Build Coastguard Worker REQUIRES(art::Locks::mutator_lock_) {
77*795d594fSAndroid Build Coastguard Worker art::Thread* self = art::Thread::Current();
78*795d594fSAndroid Build Coastguard Worker eh_->ForEachEnv(self, [&](ArtJvmTiEnv* env) NO_THREAD_SAFETY_ANALYSIS {
79*795d594fSAndroid Build Coastguard Worker art::Locks::mutator_lock_->AssertExclusiveHeld(self);
80*795d594fSAndroid Build Coastguard Worker art::WriterMutexLock mu(self, env->event_info_mutex_);
81*795d594fSAndroid Build Coastguard Worker std::vector<std::pair<Breakpoint, Breakpoint>> updated_breakpoints;
82*795d594fSAndroid Build Coastguard Worker for (auto it : env->breakpoints) {
83*795d594fSAndroid Build Coastguard Worker art::ArtMethod* orig_method = it.GetMethod();
84*795d594fSAndroid Build Coastguard Worker art::ArtMethod* am = visitor->VisitMethod(
85*795d594fSAndroid Build Coastguard Worker orig_method, JvmtiBreakpointReflectionSource(it.GetLocation(), orig_method));
86*795d594fSAndroid Build Coastguard Worker if (am != orig_method) {
87*795d594fSAndroid Build Coastguard Worker updated_breakpoints.push_back({ Breakpoint { am, it.GetLocation() }, it });
88*795d594fSAndroid Build Coastguard Worker }
89*795d594fSAndroid Build Coastguard Worker }
90*795d594fSAndroid Build Coastguard Worker for (auto it : updated_breakpoints) {
91*795d594fSAndroid Build Coastguard Worker DCHECK(env->breakpoints.find(it.second) != env->breakpoints.end());
92*795d594fSAndroid Build Coastguard Worker env->breakpoints.erase(it.second);
93*795d594fSAndroid Build Coastguard Worker env->breakpoints.insert(it.first);
94*795d594fSAndroid Build Coastguard Worker }
95*795d594fSAndroid Build Coastguard Worker });
96*795d594fSAndroid Build Coastguard Worker }
97*795d594fSAndroid Build Coastguard Worker
98*795d594fSAndroid Build Coastguard Worker EventHandler* eh_;
99*795d594fSAndroid Build Coastguard Worker };
100*795d594fSAndroid Build Coastguard Worker
101*795d594fSAndroid Build Coastguard Worker static BreakpointReflectiveValueCallback gReflectiveValueCallback;
Register(EventHandler * eh)102*795d594fSAndroid Build Coastguard Worker void BreakpointUtil::Register(EventHandler* eh) {
103*795d594fSAndroid Build Coastguard Worker gReflectiveValueCallback.eh_ = eh;
104*795d594fSAndroid Build Coastguard Worker art::ScopedThreadStateChange stsc(art::Thread::Current(),
105*795d594fSAndroid Build Coastguard Worker art::ThreadState::kWaitingForDebuggerToAttach);
106*795d594fSAndroid Build Coastguard Worker art::ScopedSuspendAll ssa("Add breakpoint reflective value visit callback");
107*795d594fSAndroid Build Coastguard Worker art::RuntimeCallbacks* callbacks = art::Runtime::Current()->GetRuntimeCallbacks();
108*795d594fSAndroid Build Coastguard Worker callbacks->AddReflectiveValueVisitCallback(&gReflectiveValueCallback);
109*795d594fSAndroid Build Coastguard Worker }
110*795d594fSAndroid Build Coastguard Worker
Unregister()111*795d594fSAndroid Build Coastguard Worker void BreakpointUtil::Unregister() {
112*795d594fSAndroid Build Coastguard Worker art::ScopedThreadStateChange stsc(art::Thread::Current(),
113*795d594fSAndroid Build Coastguard Worker art::ThreadState::kWaitingForDebuggerToAttach);
114*795d594fSAndroid Build Coastguard Worker art::ScopedSuspendAll ssa("Remove reflective value visit callback");
115*795d594fSAndroid Build Coastguard Worker art::RuntimeCallbacks* callbacks = art::Runtime::Current()->GetRuntimeCallbacks();
116*795d594fSAndroid Build Coastguard Worker callbacks->RemoveReflectiveValueVisitCallback(&gReflectiveValueCallback);
117*795d594fSAndroid Build Coastguard Worker }
118*795d594fSAndroid Build Coastguard Worker
hash() const119*795d594fSAndroid Build Coastguard Worker size_t Breakpoint::hash() const {
120*795d594fSAndroid Build Coastguard Worker return std::hash<uintptr_t> {}(reinterpret_cast<uintptr_t>(method_))
121*795d594fSAndroid Build Coastguard Worker ^ std::hash<jlocation> {}(location_);
122*795d594fSAndroid Build Coastguard Worker }
123*795d594fSAndroid Build Coastguard Worker
Breakpoint(art::ArtMethod * m,jlocation loc)124*795d594fSAndroid Build Coastguard Worker Breakpoint::Breakpoint(art::ArtMethod* m, jlocation loc) : method_(m), location_(loc) {
125*795d594fSAndroid Build Coastguard Worker DCHECK(!m->IsDefault() || !m->IsCopied() || !m->IsInvokable())
126*795d594fSAndroid Build Coastguard Worker << "Flags are: 0x" << std::hex << m->GetAccessFlags();
127*795d594fSAndroid Build Coastguard Worker }
128*795d594fSAndroid Build Coastguard Worker
RemoveBreakpointsInClass(ArtJvmTiEnv * env,art::mirror::Class * klass)129*795d594fSAndroid Build Coastguard Worker void BreakpointUtil::RemoveBreakpointsInClass(ArtJvmTiEnv* env, art::mirror::Class* klass) {
130*795d594fSAndroid Build Coastguard Worker std::vector<Breakpoint> to_remove;
131*795d594fSAndroid Build Coastguard Worker {
132*795d594fSAndroid Build Coastguard Worker art::WriterMutexLock lk(art::Thread::Current(), env->event_info_mutex_);
133*795d594fSAndroid Build Coastguard Worker for (const Breakpoint& b : env->breakpoints) {
134*795d594fSAndroid Build Coastguard Worker if (b.GetMethod()->GetDeclaringClass() == klass) {
135*795d594fSAndroid Build Coastguard Worker to_remove.push_back(b);
136*795d594fSAndroid Build Coastguard Worker }
137*795d594fSAndroid Build Coastguard Worker }
138*795d594fSAndroid Build Coastguard Worker for (const Breakpoint& b : to_remove) {
139*795d594fSAndroid Build Coastguard Worker auto it = env->breakpoints.find(b);
140*795d594fSAndroid Build Coastguard Worker DCHECK(it != env->breakpoints.end());
141*795d594fSAndroid Build Coastguard Worker env->breakpoints.erase(it);
142*795d594fSAndroid Build Coastguard Worker }
143*795d594fSAndroid Build Coastguard Worker }
144*795d594fSAndroid Build Coastguard Worker DeoptManager* deopt = DeoptManager::Get();
145*795d594fSAndroid Build Coastguard Worker for (const Breakpoint& b : to_remove) {
146*795d594fSAndroid Build Coastguard Worker // TODO It might be good to send these all at once instead.
147*795d594fSAndroid Build Coastguard Worker deopt->RemoveMethodBreakpoint(b.GetMethod());
148*795d594fSAndroid Build Coastguard Worker }
149*795d594fSAndroid Build Coastguard Worker }
150*795d594fSAndroid Build Coastguard Worker
SetBreakpoint(jvmtiEnv * jenv,jmethodID method,jlocation location)151*795d594fSAndroid Build Coastguard Worker jvmtiError BreakpointUtil::SetBreakpoint(jvmtiEnv* jenv, jmethodID method, jlocation location) {
152*795d594fSAndroid Build Coastguard Worker ArtJvmTiEnv* env = ArtJvmTiEnv::AsArtJvmTiEnv(jenv);
153*795d594fSAndroid Build Coastguard Worker if (method == nullptr) {
154*795d594fSAndroid Build Coastguard Worker return ERR(INVALID_METHODID);
155*795d594fSAndroid Build Coastguard Worker }
156*795d594fSAndroid Build Coastguard Worker art::ScopedObjectAccess soa(art::Thread::Current());
157*795d594fSAndroid Build Coastguard Worker art::ArtMethod* art_method = art::jni::DecodeArtMethod(method)->GetCanonicalMethod();
158*795d594fSAndroid Build Coastguard Worker if (location < 0 || static_cast<uint32_t>(location) >=
159*795d594fSAndroid Build Coastguard Worker art_method->DexInstructions().InsnsSizeInCodeUnits()) {
160*795d594fSAndroid Build Coastguard Worker return ERR(INVALID_LOCATION);
161*795d594fSAndroid Build Coastguard Worker }
162*795d594fSAndroid Build Coastguard Worker DeoptManager::Get()->AddMethodBreakpoint(art_method);
163*795d594fSAndroid Build Coastguard Worker {
164*795d594fSAndroid Build Coastguard Worker art::WriterMutexLock lk(art::Thread::Current(), env->event_info_mutex_);
165*795d594fSAndroid Build Coastguard Worker auto res_pair = env->breakpoints.insert(/* Breakpoint */ {art_method, location});
166*795d594fSAndroid Build Coastguard Worker if (LIKELY(res_pair.second)) {
167*795d594fSAndroid Build Coastguard Worker return OK;
168*795d594fSAndroid Build Coastguard Worker }
169*795d594fSAndroid Build Coastguard Worker }
170*795d594fSAndroid Build Coastguard Worker // Didn't get inserted because it's already present!
171*795d594fSAndroid Build Coastguard Worker DeoptManager::Get()->RemoveMethodBreakpoint(art_method);
172*795d594fSAndroid Build Coastguard Worker return ERR(DUPLICATE);
173*795d594fSAndroid Build Coastguard Worker }
174*795d594fSAndroid Build Coastguard Worker
ClearBreakpoint(jvmtiEnv * jenv,jmethodID method,jlocation location)175*795d594fSAndroid Build Coastguard Worker jvmtiError BreakpointUtil::ClearBreakpoint(jvmtiEnv* jenv, jmethodID method, jlocation location) {
176*795d594fSAndroid Build Coastguard Worker ArtJvmTiEnv* env = ArtJvmTiEnv::AsArtJvmTiEnv(jenv);
177*795d594fSAndroid Build Coastguard Worker if (method == nullptr) {
178*795d594fSAndroid Build Coastguard Worker return ERR(INVALID_METHODID);
179*795d594fSAndroid Build Coastguard Worker }
180*795d594fSAndroid Build Coastguard Worker art::ScopedObjectAccess soa(art::Thread::Current());
181*795d594fSAndroid Build Coastguard Worker art::ArtMethod* art_method = art::jni::DecodeArtMethod(method)->GetCanonicalMethod();
182*795d594fSAndroid Build Coastguard Worker {
183*795d594fSAndroid Build Coastguard Worker art::WriterMutexLock lk(art::Thread::Current(), env->event_info_mutex_);
184*795d594fSAndroid Build Coastguard Worker auto pos = env->breakpoints.find(/* Breakpoint */ {art_method, location});
185*795d594fSAndroid Build Coastguard Worker if (pos == env->breakpoints.end()) {
186*795d594fSAndroid Build Coastguard Worker return ERR(NOT_FOUND);
187*795d594fSAndroid Build Coastguard Worker }
188*795d594fSAndroid Build Coastguard Worker env->breakpoints.erase(pos);
189*795d594fSAndroid Build Coastguard Worker }
190*795d594fSAndroid Build Coastguard Worker DeoptManager::Get()->RemoveMethodBreakpoint(art_method);
191*795d594fSAndroid Build Coastguard Worker return OK;
192*795d594fSAndroid Build Coastguard Worker }
193*795d594fSAndroid Build Coastguard Worker
194*795d594fSAndroid Build Coastguard Worker } // namespace openjdkjvmti
195