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