jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "instrumentation.h" |
| 18 | |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 19 | #include <functional> |
| 20 | #include <optional> |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 21 | #include <sstream> |
| 22 | |
Andreas Gampe | c7d878d | 2018-11-19 18:42:06 +0000 | [diff] [blame] | 23 | #include <android-base/logging.h> |
| 24 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 25 | #include "arch/context.h" |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 26 | #include "art_field-inl.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 27 | #include "art_method-inl.h" |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 28 | #include "base/atomic.h" |
Andreas Gampe | 8228cdf | 2017-05-30 15:03:54 -0700 | [diff] [blame] | 29 | #include "base/callee_save_type.h" |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 30 | #include "class_linker.h" |
| 31 | #include "debugger.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 32 | #include "dex/dex_file-inl.h" |
| 33 | #include "dex/dex_file_types.h" |
| 34 | #include "dex/dex_instruction-inl.h" |
Mathieu Chartier | d889178 | 2014-03-02 13:28:37 -0800 | [diff] [blame] | 35 | #include "entrypoints/quick/quick_alloc_entrypoints.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 36 | #include "entrypoints/quick/quick_entrypoints.h" |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 37 | #include "entrypoints/runtime_asm_entrypoints.h" |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 38 | #include "gc_root-inl.h" |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 39 | #include "interpreter/interpreter.h" |
Mingyao Yang | 2ee1790 | 2017-08-30 11:37:08 -0700 | [diff] [blame] | 40 | #include "interpreter/interpreter_common.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 41 | #include "jit/jit.h" |
| 42 | #include "jit/jit_code_cache.h" |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 43 | #include "jvalue-inl.h" |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 44 | #include "jvalue.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 45 | #include "mirror/class-inl.h" |
| 46 | #include "mirror/dex_cache.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 47 | #include "mirror/object-inl.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 48 | #include "mirror/object_array-inl.h" |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 49 | #include "nth_caller_visitor.h" |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 50 | #include "oat_quick_method_header.h" |
David Srbecky | 28f6cff | 2018-10-16 15:07:28 +0100 | [diff] [blame] | 51 | #include "runtime-inl.h" |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 52 | #include "thread.h" |
| 53 | #include "thread_list.h" |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 54 | |
| 55 | namespace art { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 56 | namespace instrumentation { |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 57 | |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 58 | constexpr bool kVerboseInstrumentation = false; |
Sebastien Hertz | 5bfd5c9 | 2013-11-15 11:36:07 +0100 | [diff] [blame] | 59 | |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 60 | void InstrumentationListener::MethodExited( |
| 61 | Thread* thread, |
| 62 | Handle<mirror::Object> this_object, |
| 63 | ArtMethod* method, |
| 64 | uint32_t dex_pc, |
| 65 | OptionalFrame frame, |
| 66 | MutableHandle<mirror::Object>& return_value) { |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 67 | DCHECK_EQ(method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetReturnTypePrimitive(), |
| 68 | Primitive::kPrimNot); |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 69 | const void* original_ret = return_value.Get(); |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 70 | JValue v; |
| 71 | v.SetL(return_value.Get()); |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 72 | MethodExited(thread, this_object, method, dex_pc, frame, v); |
| 73 | DCHECK(original_ret == v.GetL()) << "Return value changed"; |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | void InstrumentationListener::FieldWritten(Thread* thread, |
| 77 | Handle<mirror::Object> this_object, |
| 78 | ArtMethod* method, |
| 79 | uint32_t dex_pc, |
| 80 | ArtField* field, |
| 81 | Handle<mirror::Object> field_value) { |
| 82 | DCHECK(!field->IsPrimitiveType()); |
| 83 | JValue v; |
| 84 | v.SetL(field_value.Get()); |
| 85 | FieldWritten(thread, this_object, method, dex_pc, field, v); |
| 86 | } |
| 87 | |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 88 | // Instrumentation works on non-inlined frames by updating returned PCs |
| 89 | // of compiled frames. |
| 90 | static constexpr StackVisitor::StackWalkKind kInstrumentationStackWalk = |
| 91 | StackVisitor::StackWalkKind::kSkipInlinedFrames; |
| 92 | |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 93 | class InstallStubsClassVisitor : public ClassVisitor { |
| 94 | public: |
| 95 | explicit InstallStubsClassVisitor(Instrumentation* instrumentation) |
| 96 | : instrumentation_(instrumentation) {} |
| 97 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 98 | bool operator()(ObjPtr<mirror::Class> klass) override REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 99 | instrumentation_->InstallStubsForClass(klass.Ptr()); |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 100 | return true; // we visit all classes. |
| 101 | } |
| 102 | |
| 103 | private: |
| 104 | Instrumentation* const instrumentation_; |
| 105 | }; |
| 106 | |
Alex Light | 2c8206f | 2018-06-08 14:51:09 -0700 | [diff] [blame] | 107 | InstrumentationStackPopper::InstrumentationStackPopper(Thread* self) |
| 108 | : self_(self), |
| 109 | instrumentation_(Runtime::Current()->GetInstrumentation()), |
| 110 | frames_to_remove_(0) {} |
| 111 | |
| 112 | InstrumentationStackPopper::~InstrumentationStackPopper() { |
| 113 | std::deque<instrumentation::InstrumentationStackFrame>* stack = self_->GetInstrumentationStack(); |
| 114 | for (size_t i = 0; i < frames_to_remove_; i++) { |
| 115 | stack->pop_front(); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | bool InstrumentationStackPopper::PopFramesTo(uint32_t desired_pops, |
| 120 | MutableHandle<mirror::Throwable>& exception) { |
| 121 | std::deque<instrumentation::InstrumentationStackFrame>* stack = self_->GetInstrumentationStack(); |
| 122 | DCHECK_LE(frames_to_remove_, desired_pops); |
| 123 | DCHECK_GE(stack->size(), desired_pops); |
| 124 | DCHECK(!self_->IsExceptionPending()); |
| 125 | if (!instrumentation_->HasMethodUnwindListeners()) { |
| 126 | frames_to_remove_ = desired_pops; |
| 127 | return true; |
| 128 | } |
| 129 | if (kVerboseInstrumentation) { |
| 130 | LOG(INFO) << "Popping frames for exception " << exception->Dump(); |
| 131 | } |
| 132 | // The instrumentation events expect the exception to be set. |
| 133 | self_->SetException(exception.Get()); |
| 134 | bool new_exception_thrown = false; |
| 135 | for (; frames_to_remove_ < desired_pops && !new_exception_thrown; frames_to_remove_++) { |
| 136 | InstrumentationStackFrame frame = stack->at(frames_to_remove_); |
| 137 | ArtMethod* method = frame.method_; |
| 138 | // Notify listeners of method unwind. |
| 139 | // TODO: improve the dex_pc information here. |
| 140 | uint32_t dex_pc = dex::kDexNoIndex; |
| 141 | if (kVerboseInstrumentation) { |
| 142 | LOG(INFO) << "Popping for unwind " << method->PrettyMethod(); |
| 143 | } |
| 144 | if (!method->IsRuntimeMethod() && !frame.interpreter_entry_) { |
| 145 | instrumentation_->MethodUnwindEvent(self_, frame.this_object_, method, dex_pc); |
| 146 | new_exception_thrown = self_->GetException() != exception.Get(); |
| 147 | } |
| 148 | } |
| 149 | exception.Assign(self_->GetException()); |
| 150 | self_->ClearException(); |
| 151 | if (kVerboseInstrumentation && new_exception_thrown) { |
| 152 | LOG(INFO) << "Failed to pop " << (desired_pops - frames_to_remove_) |
| 153 | << " frames due to new exception"; |
| 154 | } |
| 155 | return !new_exception_thrown; |
| 156 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 157 | |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 158 | Instrumentation::Instrumentation() |
Nicolas Geoffray | 5a23d2e | 2015-11-03 18:58:57 +0000 | [diff] [blame] | 159 | : instrumentation_stubs_installed_(false), |
| 160 | entry_exit_stubs_installed_(false), |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 161 | interpreter_stubs_installed_(false), |
Nicolas Geoffray | 5a23d2e | 2015-11-03 18:58:57 +0000 | [diff] [blame] | 162 | interpret_only_(false), |
| 163 | forced_interpret_only_(false), |
| 164 | have_method_entry_listeners_(false), |
| 165 | have_method_exit_listeners_(false), |
| 166 | have_method_unwind_listeners_(false), |
| 167 | have_dex_pc_listeners_(false), |
| 168 | have_field_read_listeners_(false), |
| 169 | have_field_write_listeners_(false), |
Alex Light | 6e1607e | 2017-08-23 10:06:18 -0700 | [diff] [blame] | 170 | have_exception_thrown_listeners_(false), |
Alex Light | e814f9d | 2017-07-31 16:14:39 -0700 | [diff] [blame] | 171 | have_watched_frame_pop_listeners_(false), |
Nicolas Geoffray | 81f0f95 | 2016-01-20 16:25:19 +0000 | [diff] [blame] | 172 | have_branch_listeners_(false), |
Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame] | 173 | have_exception_handled_listeners_(false), |
Andreas Gampe | 7e56a07 | 2018-11-29 10:40:06 -0800 | [diff] [blame] | 174 | deoptimized_methods_lock_(new ReaderWriterMutex("deoptimized methods lock", |
| 175 | kGenericBottomLock)), |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 176 | deoptimization_enabled_(false), |
| 177 | interpreter_handler_table_(kMainHandlerTable), |
Mathieu Chartier | 50e9331 | 2016-03-16 11:25:29 -0700 | [diff] [blame] | 178 | quick_alloc_entry_points_instrumentation_counter_(0), |
Alex Light | 4060786 | 2019-05-06 18:16:24 +0000 | [diff] [blame] | 179 | alloc_entrypoints_instrumented_(false), |
| 180 | can_use_instrumentation_trampolines_(true) { |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Vladimir Marko | 19711d4 | 2019-04-12 14:05:34 +0100 | [diff] [blame] | 183 | void Instrumentation::InstallStubsForClass(ObjPtr<mirror::Class> klass) { |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 184 | if (!klass->IsResolved()) { |
Sebastien Hertz | a8a697f | 2015-01-15 12:28:47 +0100 | [diff] [blame] | 185 | // We need the class to be resolved to install/uninstall stubs. Otherwise its methods |
| 186 | // could not be initialized or linked with regards to class inheritance. |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 187 | } else if (klass->IsErroneousResolved()) { |
| 188 | // We can't execute code in a erroneous class: do nothing. |
Sebastien Hertz | a8a697f | 2015-01-15 12:28:47 +0100 | [diff] [blame] | 189 | } else { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 190 | for (ArtMethod& method : klass->GetMethods(kRuntimePointerSize)) { |
Alex Light | 51a64d5 | 2015-12-17 13:55:59 -0800 | [diff] [blame] | 191 | InstallStubsForMethod(&method); |
Sebastien Hertz | a8a697f | 2015-01-15 12:28:47 +0100 | [diff] [blame] | 192 | } |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 193 | } |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 194 | } |
| 195 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 196 | static void UpdateEntrypoints(ArtMethod* method, const void* quick_code) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 197 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Nicolas Geoffray | d5a9587 | 2019-08-12 13:24:07 +0100 | [diff] [blame] | 198 | if (kIsDebugBuild) { |
| 199 | jit::Jit* jit = Runtime::Current()->GetJit(); |
| 200 | if (jit != nullptr && jit->GetCodeCache()->ContainsPc(quick_code)) { |
| 201 | // Ensure we always have the thumb entrypoint for JIT on arm32. |
| 202 | if (kRuntimeISA == InstructionSet::kArm) { |
| 203 | CHECK_EQ(reinterpret_cast<uintptr_t>(quick_code) & 1, 1u); |
| 204 | } |
| 205 | } |
| 206 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 207 | method->SetEntryPointFromQuickCompiledCode(quick_code); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 208 | } |
| 209 | |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 210 | bool Instrumentation::NeedDebugVersionFor(ArtMethod* method) const |
| 211 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | f285863 | 2018-04-02 11:28:50 -0700 | [diff] [blame] | 212 | art::Runtime* runtime = Runtime::Current(); |
| 213 | // If anything says we need the debug version or we are debuggable we will need the debug version |
| 214 | // of the method. |
| 215 | return (runtime->GetRuntimeCallbacks()->MethodNeedsDebugVersion(method) || |
| 216 | runtime->IsJavaDebuggable()) && |
Mingyao Yang | 6ea1a0e | 2016-01-29 12:12:49 -0800 | [diff] [blame] | 217 | !method->IsNative() && |
Alex Light | f285863 | 2018-04-02 11:28:50 -0700 | [diff] [blame] | 218 | !method->IsProxyMethod(); |
Mingyao Yang | 6ea1a0e | 2016-01-29 12:12:49 -0800 | [diff] [blame] | 219 | } |
| 220 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 221 | void Instrumentation::InstallStubsForMethod(ArtMethod* method) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 222 | if (!method->IsInvokable() || method->IsProxyMethod()) { |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 223 | // Do not change stubs for these methods. |
| 224 | return; |
| 225 | } |
Jeff Hao | 5680277 | 2014-08-19 10:17:36 -0700 | [diff] [blame] | 226 | // Don't stub Proxy.<init>. Note that the Proxy class itself is not a proxy class. |
Alex Light | 6cae5ea | 2018-06-07 17:07:02 -0700 | [diff] [blame] | 227 | // TODO We should remove the need for this since it means we cannot always correctly detect calls |
| 228 | // to Proxy.<init> |
| 229 | // Annoyingly this can be called before we have actually initialized WellKnownClasses so therefore |
| 230 | // we also need to check this based on the declaring-class descriptor. The check is valid because |
| 231 | // Proxy only has a single constructor. |
| 232 | ArtMethod* well_known_proxy_init = jni::DecodeArtMethod( |
| 233 | WellKnownClasses::java_lang_reflect_Proxy_init); |
| 234 | if ((LIKELY(well_known_proxy_init != nullptr) && UNLIKELY(method == well_known_proxy_init)) || |
| 235 | UNLIKELY(method->IsConstructor() && |
| 236 | method->GetDeclaringClass()->DescriptorEquals("Ljava/lang/reflect/Proxy;"))) { |
Jeff Hao | db8a664 | 2014-08-14 17:18:52 -0700 | [diff] [blame] | 237 | return; |
| 238 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 239 | const void* new_quick_code; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 240 | bool uninstall = !entry_exit_stubs_installed_ && !interpreter_stubs_installed_; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 241 | Runtime* const runtime = Runtime::Current(); |
| 242 | ClassLinker* const class_linker = runtime->GetClassLinker(); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 243 | bool is_class_initialized = method->GetDeclaringClass()->IsInitialized(); |
| 244 | if (uninstall) { |
| 245 | if ((forced_interpret_only_ || IsDeoptimized(method)) && !method->IsNative()) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 246 | new_quick_code = GetQuickToInterpreterBridge(); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 247 | } else if (is_class_initialized || !method->IsStatic() || method->IsConstructor()) { |
Alex Light | 3e36a9c | 2018-06-19 09:45:05 -0700 | [diff] [blame] | 248 | new_quick_code = GetCodeForInvoke(method); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 249 | } else { |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 250 | new_quick_code = GetQuickResolutionStub(); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 251 | } |
| 252 | } else { // !uninstall |
Sebastien Hertz | bae182c | 2013-12-17 10:42:03 +0100 | [diff] [blame] | 253 | if ((interpreter_stubs_installed_ || forced_interpret_only_ || IsDeoptimized(method)) && |
| 254 | !method->IsNative()) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 255 | new_quick_code = GetQuickToInterpreterBridge(); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 256 | } else { |
| 257 | // Do not overwrite resolution trampoline. When the trampoline initializes the method's |
| 258 | // class, all its static methods code will be set to the instrumentation entry point. |
| 259 | // For more details, see ClassLinker::FixupStaticTrampolines. |
| 260 | if (is_class_initialized || !method->IsStatic() || method->IsConstructor()) { |
Alex Light | 2d441b1 | 2018-06-08 15:33:21 -0700 | [diff] [blame] | 261 | if (entry_exit_stubs_installed_) { |
| 262 | // This needs to be checked first since the instrumentation entrypoint will be able to |
| 263 | // find the actual JIT compiled code that corresponds to this method. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 264 | new_quick_code = GetQuickInstrumentationEntryPoint(); |
Alex Light | 2d441b1 | 2018-06-08 15:33:21 -0700 | [diff] [blame] | 265 | } else if (NeedDebugVersionFor(method)) { |
| 266 | // It would be great to search the JIT for its implementation here but we cannot due to |
| 267 | // the locks we hold. Instead just set to the interpreter bridge and that code will search |
| 268 | // the JIT when it gets called and replace the entrypoint then. |
| 269 | new_quick_code = GetQuickToInterpreterBridge(); |
Nicolas Geoffray | a0619e2 | 2016-12-20 13:57:43 +0000 | [diff] [blame] | 270 | } else { |
Alex Light | fc49fec | 2018-01-16 22:28:36 +0000 | [diff] [blame] | 271 | new_quick_code = class_linker->GetQuickOatCodeFor(method); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 272 | } |
| 273 | } else { |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 274 | new_quick_code = GetQuickResolutionStub(); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 275 | } |
| 276 | } |
| 277 | } |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 278 | UpdateEntrypoints(method, new_quick_code); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 279 | } |
| 280 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 281 | // Places the instrumentation exit pc as the return PC for every quick frame. This also allows |
| 282 | // deoptimization of quick frames to interpreter frames. |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 283 | // Since we may already have done this previously, we need to push new instrumentation frame before |
| 284 | // existing instrumentation frames. |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 285 | static void InstrumentationInstallStack(Thread* thread, void* arg) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 286 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 287 | struct InstallStackVisitor final : public StackVisitor { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 288 | InstallStackVisitor(Thread* thread_in, Context* context, uintptr_t instrumentation_exit_pc) |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 289 | : StackVisitor(thread_in, context, kInstrumentationStackWalk), |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 290 | instrumentation_stack_(thread_in->GetInstrumentationStack()), |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 291 | instrumentation_exit_pc_(instrumentation_exit_pc), |
Alex Light | e927866 | 2018-03-08 16:55:58 -0800 | [diff] [blame] | 292 | reached_existing_instrumentation_frames_(false), instrumentation_stack_depth_(0), |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 293 | last_return_pc_(0) { |
| 294 | } |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 295 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 296 | bool VisitFrame() override REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 297 | ArtMethod* m = GetMethod(); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 298 | if (m == nullptr) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 299 | if (kVerboseInstrumentation) { |
| 300 | LOG(INFO) << " Skipping upcall. Frame " << GetFrameId(); |
| 301 | } |
| 302 | last_return_pc_ = 0; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 303 | return true; // Ignore upcalls. |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 304 | } |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 305 | if (GetCurrentQuickFrame() == nullptr) { |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 306 | bool interpreter_frame = true; |
Vladimir Marko | abedfca | 2019-05-23 14:07:47 +0100 | [diff] [blame] | 307 | InstrumentationStackFrame instrumentation_frame(GetThisObject().Ptr(), |
| 308 | m, |
| 309 | /*return_pc=*/ 0, |
| 310 | GetFrameId(), |
Sebastien Hertz | 320deb2 | 2014-06-11 19:45:05 +0200 | [diff] [blame] | 311 | interpreter_frame); |
Jeff Hao | a15a81b | 2014-05-27 18:25:47 -0700 | [diff] [blame] | 312 | if (kVerboseInstrumentation) { |
| 313 | LOG(INFO) << "Pushing shadow frame " << instrumentation_frame.Dump(); |
| 314 | } |
| 315 | shadow_stack_.push_back(instrumentation_frame); |
| 316 | return true; // Continue. |
| 317 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 318 | uintptr_t return_pc = GetReturnPc(); |
Sebastien Hertz | 320deb2 | 2014-06-11 19:45:05 +0200 | [diff] [blame] | 319 | if (kVerboseInstrumentation) { |
| 320 | LOG(INFO) << " Installing exit stub in " << DescribeLocation(); |
| 321 | } |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 322 | if (return_pc == instrumentation_exit_pc_) { |
Mingyao Yang | 2ee1790 | 2017-08-30 11:37:08 -0700 | [diff] [blame] | 323 | CHECK_LT(instrumentation_stack_depth_, instrumentation_stack_->size()); |
| 324 | |
| 325 | if (m->IsRuntimeMethod()) { |
| 326 | const InstrumentationStackFrame& frame = |
Vladimir Marko | 35d5b8a | 2018-07-03 09:18:32 +0100 | [diff] [blame] | 327 | (*instrumentation_stack_)[instrumentation_stack_depth_]; |
Mingyao Yang | 2ee1790 | 2017-08-30 11:37:08 -0700 | [diff] [blame] | 328 | if (frame.interpreter_entry_) { |
| 329 | // This instrumentation frame is for an interpreter bridge and is |
| 330 | // pushed when executing the instrumented interpreter bridge. So method |
| 331 | // enter event must have been reported. However we need to push a DEX pc |
| 332 | // into the dex_pcs_ list to match size of instrumentation stack. |
Andreas Gampe | e2abbc6 | 2017-09-15 11:59:26 -0700 | [diff] [blame] | 333 | uint32_t dex_pc = dex::kDexNoIndex; |
Mingyao Yang | 2ee1790 | 2017-08-30 11:37:08 -0700 | [diff] [blame] | 334 | dex_pcs_.push_back(dex_pc); |
| 335 | last_return_pc_ = frame.return_pc_; |
| 336 | ++instrumentation_stack_depth_; |
| 337 | return true; |
| 338 | } |
| 339 | } |
| 340 | |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 341 | // We've reached a frame which has already been installed with instrumentation exit stub. |
Alex Light | 74c91c9 | 2018-03-08 14:01:44 -0800 | [diff] [blame] | 342 | // We should have already installed instrumentation or be interpreter on previous frames. |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 343 | reached_existing_instrumentation_frames_ = true; |
| 344 | |
Daniel Mihalyi | ca1d06c | 2014-08-18 18:45:31 +0200 | [diff] [blame] | 345 | const InstrumentationStackFrame& frame = |
Vladimir Marko | 35d5b8a | 2018-07-03 09:18:32 +0100 | [diff] [blame] | 346 | (*instrumentation_stack_)[instrumentation_stack_depth_]; |
Alex Light | fc81d80 | 2018-12-07 13:39:05 -0800 | [diff] [blame] | 347 | CHECK_EQ(m->GetNonObsoleteMethod(), frame.method_->GetNonObsoleteMethod()) |
| 348 | << "Expected " << ArtMethod::PrettyMethod(m) |
| 349 | << ", Found " << ArtMethod::PrettyMethod(frame.method_); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 350 | return_pc = frame.return_pc_; |
| 351 | if (kVerboseInstrumentation) { |
| 352 | LOG(INFO) << "Ignoring already instrumented " << frame.Dump(); |
| 353 | } |
| 354 | } else { |
| 355 | CHECK_NE(return_pc, 0U); |
Alex Light | 74c91c9 | 2018-03-08 14:01:44 -0800 | [diff] [blame] | 356 | if (UNLIKELY(reached_existing_instrumentation_frames_ && !m->IsRuntimeMethod())) { |
| 357 | // We already saw an existing instrumentation frame so this should be a runtime-method |
| 358 | // inserted by the interpreter or runtime. |
Alex Light | e927866 | 2018-03-08 16:55:58 -0800 | [diff] [blame] | 359 | std::string thread_name; |
| 360 | GetThread()->GetThreadName(thread_name); |
| 361 | uint32_t dex_pc = dex::kDexNoIndex; |
| 362 | if (last_return_pc_ != 0 && |
| 363 | GetCurrentOatQuickMethodHeader() != nullptr) { |
| 364 | dex_pc = GetCurrentOatQuickMethodHeader()->ToDexPc(m, last_return_pc_); |
| 365 | } |
Alex Light | 74c91c9 | 2018-03-08 14:01:44 -0800 | [diff] [blame] | 366 | LOG(FATAL) << "While walking " << thread_name << " found unexpected non-runtime method" |
| 367 | << " without instrumentation exit return or interpreter frame." |
Alex Light | e927866 | 2018-03-08 16:55:58 -0800 | [diff] [blame] | 368 | << " method is " << GetMethod()->PrettyMethod() |
| 369 | << " return_pc is " << std::hex << return_pc |
| 370 | << " dex pc: " << dex_pc; |
| 371 | UNREACHABLE(); |
| 372 | } |
Mingyao Yang | 2ee1790 | 2017-08-30 11:37:08 -0700 | [diff] [blame] | 373 | InstrumentationStackFrame instrumentation_frame( |
Vladimir Marko | abedfca | 2019-05-23 14:07:47 +0100 | [diff] [blame] | 374 | m->IsRuntimeMethod() ? nullptr : GetThisObject().Ptr(), |
Mingyao Yang | 2ee1790 | 2017-08-30 11:37:08 -0700 | [diff] [blame] | 375 | m, |
| 376 | return_pc, |
| 377 | GetFrameId(), // A runtime method still gets a frame id. |
| 378 | false); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 379 | if (kVerboseInstrumentation) { |
| 380 | LOG(INFO) << "Pushing frame " << instrumentation_frame.Dump(); |
| 381 | } |
| 382 | |
Sebastien Hertz | 320deb2 | 2014-06-11 19:45:05 +0200 | [diff] [blame] | 383 | // Insert frame at the right position so we do not corrupt the instrumentation stack. |
| 384 | // Instrumentation stack frames are in descending frame id order. |
| 385 | auto it = instrumentation_stack_->begin(); |
| 386 | for (auto end = instrumentation_stack_->end(); it != end; ++it) { |
| 387 | const InstrumentationStackFrame& current = *it; |
| 388 | if (instrumentation_frame.frame_id_ >= current.frame_id_) { |
| 389 | break; |
| 390 | } |
| 391 | } |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 392 | instrumentation_stack_->insert(it, instrumentation_frame); |
| 393 | SetReturnPc(instrumentation_exit_pc_); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 394 | } |
Andreas Gampe | e2abbc6 | 2017-09-15 11:59:26 -0700 | [diff] [blame] | 395 | uint32_t dex_pc = dex::kDexNoIndex; |
Mingyao Yang | 2ee1790 | 2017-08-30 11:37:08 -0700 | [diff] [blame] | 396 | if (last_return_pc_ != 0 && |
| 397 | GetCurrentOatQuickMethodHeader() != nullptr) { |
| 398 | dex_pc = GetCurrentOatQuickMethodHeader()->ToDexPc(m, last_return_pc_); |
| 399 | } |
| 400 | dex_pcs_.push_back(dex_pc); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 401 | last_return_pc_ = return_pc; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 402 | ++instrumentation_stack_depth_; |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 403 | return true; // Continue. |
| 404 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 405 | std::deque<InstrumentationStackFrame>* const instrumentation_stack_; |
Jeff Hao | a15a81b | 2014-05-27 18:25:47 -0700 | [diff] [blame] | 406 | std::vector<InstrumentationStackFrame> shadow_stack_; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 407 | std::vector<uint32_t> dex_pcs_; |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 408 | const uintptr_t instrumentation_exit_pc_; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 409 | bool reached_existing_instrumentation_frames_; |
| 410 | size_t instrumentation_stack_depth_; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 411 | uintptr_t last_return_pc_; |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 412 | }; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 413 | if (kVerboseInstrumentation) { |
| 414 | std::string thread_name; |
| 415 | thread->GetThreadName(thread_name); |
| 416 | LOG(INFO) << "Installing exit stubs in " << thread_name; |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 417 | } |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 418 | |
| 419 | Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg); |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 420 | std::unique_ptr<Context> context(Context::Create()); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 421 | uintptr_t instrumentation_exit_pc = reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()); |
Sebastien Hertz | 11d40c2 | 2014-02-19 18:00:17 +0100 | [diff] [blame] | 422 | InstallStackVisitor visitor(thread, context.get(), instrumentation_exit_pc); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 423 | visitor.WalkStack(true); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 424 | CHECK_EQ(visitor.dex_pcs_.size(), thread->GetInstrumentationStack()->size()); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 425 | |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 426 | if (instrumentation->ShouldNotifyMethodEnterExitEvents()) { |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 427 | // Create method enter events for all methods currently on the thread's stack. We only do this |
| 428 | // if no debugger is attached to prevent from posting events twice. |
Jeff Hao | a15a81b | 2014-05-27 18:25:47 -0700 | [diff] [blame] | 429 | auto ssi = visitor.shadow_stack_.rbegin(); |
| 430 | for (auto isi = thread->GetInstrumentationStack()->rbegin(), |
| 431 | end = thread->GetInstrumentationStack()->rend(); isi != end; ++isi) { |
| 432 | while (ssi != visitor.shadow_stack_.rend() && (*ssi).frame_id_ < (*isi).frame_id_) { |
| 433 | instrumentation->MethodEnterEvent(thread, (*ssi).this_object_, (*ssi).method_, 0); |
| 434 | ++ssi; |
| 435 | } |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 436 | uint32_t dex_pc = visitor.dex_pcs_.back(); |
| 437 | visitor.dex_pcs_.pop_back(); |
Alex Light | dc5423f | 2018-06-08 10:43:38 -0700 | [diff] [blame] | 438 | if (!isi->interpreter_entry_ && !isi->method_->IsRuntimeMethod()) { |
Sebastien Hertz | 320deb2 | 2014-06-11 19:45:05 +0200 | [diff] [blame] | 439 | instrumentation->MethodEnterEvent(thread, (*isi).this_object_, (*isi).method_, dex_pc); |
| 440 | } |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 441 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 442 | } |
| 443 | thread->VerifyStack(); |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 444 | } |
| 445 | |
Mingyao Yang | 99170c6 | 2015-07-06 11:10:37 -0700 | [diff] [blame] | 446 | void Instrumentation::InstrumentThreadStack(Thread* thread) { |
| 447 | instrumentation_stubs_installed_ = true; |
| 448 | InstrumentationInstallStack(thread, this); |
| 449 | } |
| 450 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 451 | // Removes the instrumentation exit pc as the return PC for every quick frame. |
| 452 | static void InstrumentationRestoreStack(Thread* thread, void* arg) |
Nicolas Geoffray | 5a23d2e | 2015-11-03 18:58:57 +0000 | [diff] [blame] | 453 | REQUIRES(Locks::mutator_lock_) { |
| 454 | Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current()); |
| 455 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 456 | struct RestoreStackVisitor final : public StackVisitor { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 457 | RestoreStackVisitor(Thread* thread_in, uintptr_t instrumentation_exit_pc, |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 458 | Instrumentation* instrumentation) |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 459 | : StackVisitor(thread_in, nullptr, kInstrumentationStackWalk), |
| 460 | thread_(thread_in), |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 461 | instrumentation_exit_pc_(instrumentation_exit_pc), |
| 462 | instrumentation_(instrumentation), |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 463 | instrumentation_stack_(thread_in->GetInstrumentationStack()), |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 464 | frames_removed_(0) {} |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 465 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 466 | bool VisitFrame() override REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 467 | if (instrumentation_stack_->size() == 0) { |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 468 | return false; // Stop. |
| 469 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 470 | ArtMethod* m = GetMethod(); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 471 | if (GetCurrentQuickFrame() == nullptr) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 472 | if (kVerboseInstrumentation) { |
Daniel Mihalyi | ca1d06c | 2014-08-18 18:45:31 +0200 | [diff] [blame] | 473 | LOG(INFO) << " Ignoring a shadow frame. Frame " << GetFrameId() |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 474 | << " Method=" << ArtMethod::PrettyMethod(m); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 475 | } |
| 476 | return true; // Ignore shadow frames. |
| 477 | } |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 478 | if (m == nullptr) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 479 | if (kVerboseInstrumentation) { |
| 480 | LOG(INFO) << " Skipping upcall. Frame " << GetFrameId(); |
| 481 | } |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 482 | return true; // Ignore upcalls. |
| 483 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 484 | bool removed_stub = false; |
| 485 | // TODO: make this search more efficient? |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 486 | const size_t frameId = GetFrameId(); |
| 487 | for (const InstrumentationStackFrame& instrumentation_frame : *instrumentation_stack_) { |
| 488 | if (instrumentation_frame.frame_id_ == frameId) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 489 | if (kVerboseInstrumentation) { |
| 490 | LOG(INFO) << " Removing exit stub in " << DescribeLocation(); |
| 491 | } |
Jeff Hao | 9a916d3 | 2013-06-27 18:45:37 -0700 | [diff] [blame] | 492 | if (instrumentation_frame.interpreter_entry_) { |
Andreas Gampe | 8228cdf | 2017-05-30 15:03:54 -0700 | [diff] [blame] | 493 | CHECK(m == Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs)); |
Jeff Hao | 9a916d3 | 2013-06-27 18:45:37 -0700 | [diff] [blame] | 494 | } else { |
Alex Light | fc81d80 | 2018-12-07 13:39:05 -0800 | [diff] [blame] | 495 | CHECK_EQ(m->GetNonObsoleteMethod(), |
| 496 | instrumentation_frame.method_->GetNonObsoleteMethod()) |
| 497 | << ArtMethod::PrettyMethod(m); |
Jeff Hao | 9a916d3 | 2013-06-27 18:45:37 -0700 | [diff] [blame] | 498 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 499 | SetReturnPc(instrumentation_frame.return_pc_); |
Mingyao Yang | 2ee1790 | 2017-08-30 11:37:08 -0700 | [diff] [blame] | 500 | if (instrumentation_->ShouldNotifyMethodEnterExitEvents() && |
| 501 | !m->IsRuntimeMethod()) { |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 502 | // Create the method exit events. As the methods didn't really exit the result is 0. |
| 503 | // We only do this if no debugger is attached to prevent from posting events twice. |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 504 | JValue val; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 505 | instrumentation_->MethodExitEvent(thread_, instrumentation_frame.this_object_, m, |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 506 | GetDexPc(), OptionalFrame{}, val); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 507 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 508 | frames_removed_++; |
| 509 | removed_stub = true; |
| 510 | break; |
| 511 | } |
| 512 | } |
| 513 | if (!removed_stub) { |
| 514 | if (kVerboseInstrumentation) { |
| 515 | LOG(INFO) << " No exit stub in " << DescribeLocation(); |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 516 | } |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 517 | } |
| 518 | return true; // Continue. |
| 519 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 520 | Thread* const thread_; |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 521 | const uintptr_t instrumentation_exit_pc_; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 522 | Instrumentation* const instrumentation_; |
| 523 | std::deque<instrumentation::InstrumentationStackFrame>* const instrumentation_stack_; |
| 524 | size_t frames_removed_; |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 525 | }; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 526 | if (kVerboseInstrumentation) { |
| 527 | std::string thread_name; |
| 528 | thread->GetThreadName(thread_name); |
| 529 | LOG(INFO) << "Removing exit stubs in " << thread_name; |
| 530 | } |
| 531 | std::deque<instrumentation::InstrumentationStackFrame>* stack = thread->GetInstrumentationStack(); |
| 532 | if (stack->size() > 0) { |
| 533 | Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 534 | uintptr_t instrumentation_exit_pc = |
| 535 | reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 536 | RestoreStackVisitor visitor(thread, instrumentation_exit_pc, instrumentation); |
| 537 | visitor.WalkStack(true); |
| 538 | CHECK_EQ(visitor.frames_removed_, stack->size()); |
| 539 | while (stack->size() > 0) { |
| 540 | stack->pop_front(); |
| 541 | } |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 542 | } |
| 543 | } |
| 544 | |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 545 | static bool HasEvent(Instrumentation::InstrumentationEvent expected, uint32_t events) { |
| 546 | return (events & expected) != 0; |
| 547 | } |
| 548 | |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 549 | static void PotentiallyAddListenerTo(Instrumentation::InstrumentationEvent event, |
| 550 | uint32_t events, |
| 551 | std::list<InstrumentationListener*>& list, |
| 552 | InstrumentationListener* listener, |
| 553 | bool* has_listener) |
| 554 | REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_) { |
| 555 | Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current()); |
| 556 | if (!HasEvent(event, events)) { |
| 557 | return; |
| 558 | } |
| 559 | // If there is a free slot in the list, we insert the listener in that slot. |
| 560 | // Otherwise we add it to the end of the list. |
| 561 | auto it = std::find(list.begin(), list.end(), nullptr); |
| 562 | if (it != list.end()) { |
| 563 | *it = listener; |
| 564 | } else { |
| 565 | list.push_back(listener); |
| 566 | } |
David Srbecky | 28f6cff | 2018-10-16 15:07:28 +0100 | [diff] [blame] | 567 | Runtime::DoAndMaybeSwitchInterpreter([=](){ *has_listener = true; }); |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 568 | } |
| 569 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 570 | void Instrumentation::AddListener(InstrumentationListener* listener, uint32_t events) { |
| 571 | Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current()); |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 572 | PotentiallyAddListenerTo(kMethodEntered, |
| 573 | events, |
| 574 | method_entry_listeners_, |
| 575 | listener, |
| 576 | &have_method_entry_listeners_); |
| 577 | PotentiallyAddListenerTo(kMethodExited, |
| 578 | events, |
| 579 | method_exit_listeners_, |
| 580 | listener, |
| 581 | &have_method_exit_listeners_); |
| 582 | PotentiallyAddListenerTo(kMethodUnwind, |
| 583 | events, |
| 584 | method_unwind_listeners_, |
| 585 | listener, |
| 586 | &have_method_unwind_listeners_); |
Nicolas Geoffray | 81f0f95 | 2016-01-20 16:25:19 +0000 | [diff] [blame] | 587 | PotentiallyAddListenerTo(kBranch, |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 588 | events, |
Nicolas Geoffray | 81f0f95 | 2016-01-20 16:25:19 +0000 | [diff] [blame] | 589 | branch_listeners_, |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 590 | listener, |
Nicolas Geoffray | 81f0f95 | 2016-01-20 16:25:19 +0000 | [diff] [blame] | 591 | &have_branch_listeners_); |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 592 | PotentiallyAddListenerTo(kDexPcMoved, |
| 593 | events, |
| 594 | dex_pc_listeners_, |
| 595 | listener, |
| 596 | &have_dex_pc_listeners_); |
| 597 | PotentiallyAddListenerTo(kFieldRead, |
| 598 | events, |
| 599 | field_read_listeners_, |
| 600 | listener, |
| 601 | &have_field_read_listeners_); |
| 602 | PotentiallyAddListenerTo(kFieldWritten, |
| 603 | events, |
| 604 | field_write_listeners_, |
| 605 | listener, |
| 606 | &have_field_write_listeners_); |
Alex Light | 6e1607e | 2017-08-23 10:06:18 -0700 | [diff] [blame] | 607 | PotentiallyAddListenerTo(kExceptionThrown, |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 608 | events, |
Alex Light | 6e1607e | 2017-08-23 10:06:18 -0700 | [diff] [blame] | 609 | exception_thrown_listeners_, |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 610 | listener, |
Alex Light | 6e1607e | 2017-08-23 10:06:18 -0700 | [diff] [blame] | 611 | &have_exception_thrown_listeners_); |
Alex Light | e814f9d | 2017-07-31 16:14:39 -0700 | [diff] [blame] | 612 | PotentiallyAddListenerTo(kWatchedFramePop, |
| 613 | events, |
| 614 | watched_frame_pop_listeners_, |
| 615 | listener, |
| 616 | &have_watched_frame_pop_listeners_); |
Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame] | 617 | PotentiallyAddListenerTo(kExceptionHandled, |
| 618 | events, |
| 619 | exception_handled_listeners_, |
| 620 | listener, |
| 621 | &have_exception_handled_listeners_); |
Sebastien Hertz | ee1997a | 2013-09-19 14:47:09 +0200 | [diff] [blame] | 622 | UpdateInterpreterHandlerTable(); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 623 | } |
| 624 | |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 625 | static void PotentiallyRemoveListenerFrom(Instrumentation::InstrumentationEvent event, |
| 626 | uint32_t events, |
| 627 | std::list<InstrumentationListener*>& list, |
| 628 | InstrumentationListener* listener, |
| 629 | bool* has_listener) |
| 630 | REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_) { |
| 631 | Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current()); |
| 632 | if (!HasEvent(event, events)) { |
| 633 | return; |
| 634 | } |
| 635 | auto it = std::find(list.begin(), list.end(), listener); |
| 636 | if (it != list.end()) { |
| 637 | // Just update the entry, do not remove from the list. Removing entries in the list |
| 638 | // is unsafe when mutators are iterating over it. |
| 639 | *it = nullptr; |
| 640 | } |
| 641 | |
| 642 | // Check if the list contains any non-null listener, and update 'has_listener'. |
| 643 | for (InstrumentationListener* l : list) { |
| 644 | if (l != nullptr) { |
David Srbecky | 28f6cff | 2018-10-16 15:07:28 +0100 | [diff] [blame] | 645 | Runtime::DoAndMaybeSwitchInterpreter([=](){ *has_listener = true; }); |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 646 | return; |
| 647 | } |
| 648 | } |
David Srbecky | 28f6cff | 2018-10-16 15:07:28 +0100 | [diff] [blame] | 649 | Runtime::DoAndMaybeSwitchInterpreter([=](){ *has_listener = false; }); |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 650 | } |
| 651 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 652 | void Instrumentation::RemoveListener(InstrumentationListener* listener, uint32_t events) { |
| 653 | Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current()); |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 654 | PotentiallyRemoveListenerFrom(kMethodEntered, |
| 655 | events, |
| 656 | method_entry_listeners_, |
| 657 | listener, |
| 658 | &have_method_entry_listeners_); |
| 659 | PotentiallyRemoveListenerFrom(kMethodExited, |
| 660 | events, |
| 661 | method_exit_listeners_, |
| 662 | listener, |
| 663 | &have_method_exit_listeners_); |
| 664 | PotentiallyRemoveListenerFrom(kMethodUnwind, |
| 665 | events, |
| 666 | method_unwind_listeners_, |
| 667 | listener, |
| 668 | &have_method_unwind_listeners_); |
Nicolas Geoffray | 81f0f95 | 2016-01-20 16:25:19 +0000 | [diff] [blame] | 669 | PotentiallyRemoveListenerFrom(kBranch, |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 670 | events, |
Nicolas Geoffray | 81f0f95 | 2016-01-20 16:25:19 +0000 | [diff] [blame] | 671 | branch_listeners_, |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 672 | listener, |
Nicolas Geoffray | 81f0f95 | 2016-01-20 16:25:19 +0000 | [diff] [blame] | 673 | &have_branch_listeners_); |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 674 | PotentiallyRemoveListenerFrom(kDexPcMoved, |
| 675 | events, |
| 676 | dex_pc_listeners_, |
| 677 | listener, |
| 678 | &have_dex_pc_listeners_); |
| 679 | PotentiallyRemoveListenerFrom(kFieldRead, |
| 680 | events, |
| 681 | field_read_listeners_, |
| 682 | listener, |
| 683 | &have_field_read_listeners_); |
| 684 | PotentiallyRemoveListenerFrom(kFieldWritten, |
| 685 | events, |
| 686 | field_write_listeners_, |
| 687 | listener, |
| 688 | &have_field_write_listeners_); |
Alex Light | 6e1607e | 2017-08-23 10:06:18 -0700 | [diff] [blame] | 689 | PotentiallyRemoveListenerFrom(kExceptionThrown, |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 690 | events, |
Alex Light | 6e1607e | 2017-08-23 10:06:18 -0700 | [diff] [blame] | 691 | exception_thrown_listeners_, |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 692 | listener, |
Alex Light | 6e1607e | 2017-08-23 10:06:18 -0700 | [diff] [blame] | 693 | &have_exception_thrown_listeners_); |
Alex Light | e814f9d | 2017-07-31 16:14:39 -0700 | [diff] [blame] | 694 | PotentiallyRemoveListenerFrom(kWatchedFramePop, |
| 695 | events, |
| 696 | watched_frame_pop_listeners_, |
| 697 | listener, |
| 698 | &have_watched_frame_pop_listeners_); |
Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame] | 699 | PotentiallyRemoveListenerFrom(kExceptionHandled, |
| 700 | events, |
| 701 | exception_handled_listeners_, |
| 702 | listener, |
| 703 | &have_exception_handled_listeners_); |
Sebastien Hertz | ee1997a | 2013-09-19 14:47:09 +0200 | [diff] [blame] | 704 | UpdateInterpreterHandlerTable(); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 705 | } |
| 706 | |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 707 | Instrumentation::InstrumentationLevel Instrumentation::GetCurrentInstrumentationLevel() const { |
Alex Light | 4ba388a | 2017-01-27 10:26:49 -0800 | [diff] [blame] | 708 | if (interpreter_stubs_installed_) { |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 709 | return InstrumentationLevel::kInstrumentWithInterpreter; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 710 | } else if (entry_exit_stubs_installed_) { |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 711 | return InstrumentationLevel::kInstrumentWithInstrumentationStubs; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 712 | } else { |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 713 | return InstrumentationLevel::kInstrumentNothing; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 714 | } |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 715 | } |
| 716 | |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 717 | bool Instrumentation::RequiresInstrumentationInstallation(InstrumentationLevel new_level) const { |
Alex Light | 4ba388a | 2017-01-27 10:26:49 -0800 | [diff] [blame] | 718 | // We need to reinstall instrumentation if we go to a different level. |
| 719 | return GetCurrentInstrumentationLevel() != new_level; |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 720 | } |
| 721 | |
Alex Light | 4060786 | 2019-05-06 18:16:24 +0000 | [diff] [blame] | 722 | void Instrumentation::UpdateInstrumentationLevels(InstrumentationLevel level) { |
| 723 | if (level == InstrumentationLevel::kInstrumentWithInterpreter) { |
| 724 | can_use_instrumentation_trampolines_ = false; |
| 725 | } |
| 726 | if (UNLIKELY(!can_use_instrumentation_trampolines_)) { |
| 727 | for (auto& p : requested_instrumentation_levels_) { |
| 728 | if (p.second == InstrumentationLevel::kInstrumentWithInstrumentationStubs) { |
| 729 | p.second = InstrumentationLevel::kInstrumentWithInterpreter; |
| 730 | } |
| 731 | } |
| 732 | } |
| 733 | } |
| 734 | |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 735 | void Instrumentation::ConfigureStubs(const char* key, InstrumentationLevel desired_level) { |
| 736 | // Store the instrumentation level for this key or remove it. |
| 737 | if (desired_level == InstrumentationLevel::kInstrumentNothing) { |
| 738 | // The client no longer needs instrumentation. |
| 739 | requested_instrumentation_levels_.erase(key); |
| 740 | } else { |
| 741 | // The client needs instrumentation. |
| 742 | requested_instrumentation_levels_.Overwrite(key, desired_level); |
| 743 | } |
| 744 | |
Alex Light | 4060786 | 2019-05-06 18:16:24 +0000 | [diff] [blame] | 745 | UpdateInstrumentationLevels(desired_level); |
| 746 | UpdateStubs(); |
| 747 | } |
| 748 | |
| 749 | void Instrumentation::EnableSingleThreadDeopt() { |
| 750 | // Single-thread deopt only uses interpreter. |
| 751 | can_use_instrumentation_trampolines_ = false; |
| 752 | UpdateInstrumentationLevels(InstrumentationLevel::kInstrumentWithInterpreter); |
| 753 | UpdateStubs(); |
| 754 | } |
| 755 | |
| 756 | void Instrumentation::UpdateStubs() { |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 757 | // Look for the highest required instrumentation level. |
| 758 | InstrumentationLevel requested_level = InstrumentationLevel::kInstrumentNothing; |
| 759 | for (const auto& v : requested_instrumentation_levels_) { |
| 760 | requested_level = std::max(requested_level, v.second); |
| 761 | } |
| 762 | |
Alex Light | 4060786 | 2019-05-06 18:16:24 +0000 | [diff] [blame] | 763 | DCHECK(can_use_instrumentation_trampolines_ || |
| 764 | requested_level != InstrumentationLevel::kInstrumentWithInstrumentationStubs) |
| 765 | << "Use trampolines: " << can_use_instrumentation_trampolines_ << " level " |
| 766 | << requested_level; |
| 767 | |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 768 | interpret_only_ = (requested_level == InstrumentationLevel::kInstrumentWithInterpreter) || |
| 769 | forced_interpret_only_; |
| 770 | |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 771 | if (!RequiresInstrumentationInstallation(requested_level)) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 772 | // We're already set. |
| 773 | return; |
| 774 | } |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 775 | Thread* const self = Thread::Current(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 776 | Runtime* runtime = Runtime::Current(); |
Sebastien Hertz | a8a697f | 2015-01-15 12:28:47 +0100 | [diff] [blame] | 777 | Locks::mutator_lock_->AssertExclusiveHeld(self); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 778 | Locks::thread_list_lock_->AssertNotHeld(self); |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 779 | if (requested_level > InstrumentationLevel::kInstrumentNothing) { |
Alex Light | 4ba388a | 2017-01-27 10:26:49 -0800 | [diff] [blame] | 780 | if (requested_level == InstrumentationLevel::kInstrumentWithInterpreter) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 781 | interpreter_stubs_installed_ = true; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 782 | entry_exit_stubs_installed_ = true; |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 783 | } else { |
| 784 | CHECK_EQ(requested_level, InstrumentationLevel::kInstrumentWithInstrumentationStubs); |
| 785 | entry_exit_stubs_installed_ = true; |
| 786 | interpreter_stubs_installed_ = false; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 787 | } |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 788 | InstallStubsClassVisitor visitor(this); |
| 789 | runtime->GetClassLinker()->VisitClasses(&visitor); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 790 | instrumentation_stubs_installed_ = true; |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 791 | MutexLock mu(self, *Locks::thread_list_lock_); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 792 | runtime->GetThreadList()->ForEach(InstrumentationInstallStack, this); |
| 793 | } else { |
| 794 | interpreter_stubs_installed_ = false; |
| 795 | entry_exit_stubs_installed_ = false; |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 796 | InstallStubsClassVisitor visitor(this); |
| 797 | runtime->GetClassLinker()->VisitClasses(&visitor); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 798 | // Restore stack only if there is no method currently deoptimized. |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 799 | bool empty; |
| 800 | { |
Andreas Gampe | 7e56a07 | 2018-11-29 10:40:06 -0800 | [diff] [blame] | 801 | ReaderMutexLock mu(self, *GetDeoptimizedMethodsLock()); |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 802 | empty = IsDeoptimizedMethodsEmpty(); // Avoid lock violation. |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 803 | } |
| 804 | if (empty) { |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 805 | MutexLock mu(self, *Locks::thread_list_lock_); |
| 806 | Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this); |
Nicolas Geoffray | 5a23d2e | 2015-11-03 18:58:57 +0000 | [diff] [blame] | 807 | // Only do this after restoring, as walking the stack when restoring will see |
| 808 | // the instrumentation exit pc. |
| 809 | instrumentation_stubs_installed_ = false; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 810 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 811 | } |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 812 | } |
| 813 | |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 814 | static void ResetQuickAllocEntryPointsForThread(Thread* thread, void* arg ATTRIBUTE_UNUSED) { |
Mathieu Chartier | 5ace201 | 2016-11-30 10:15:41 -0800 | [diff] [blame] | 815 | thread->ResetQuickAllocEntryPointsForThread(kUseReadBarrier && thread->GetIsGcMarking()); |
Ian Rogers | fa82427 | 2013-11-05 16:12:57 -0800 | [diff] [blame] | 816 | } |
| 817 | |
Mathieu Chartier | 9ef78b5 | 2014-09-25 17:03:12 -0700 | [diff] [blame] | 818 | void Instrumentation::SetEntrypointsInstrumented(bool instrumented) { |
| 819 | Thread* self = Thread::Current(); |
Mathieu Chartier | 661974a | 2014-01-09 11:23:53 -0800 | [diff] [blame] | 820 | Runtime* runtime = Runtime::Current(); |
Mathieu Chartier | 9ef78b5 | 2014-09-25 17:03:12 -0700 | [diff] [blame] | 821 | Locks::mutator_lock_->AssertNotHeld(self); |
| 822 | Locks::instrument_entrypoints_lock_->AssertHeld(self); |
| 823 | if (runtime->IsStarted()) { |
Mathieu Chartier | 4f55e22 | 2015-09-04 13:26:21 -0700 | [diff] [blame] | 824 | ScopedSuspendAll ssa(__FUNCTION__); |
Mathieu Chartier | 9ef78b5 | 2014-09-25 17:03:12 -0700 | [diff] [blame] | 825 | MutexLock mu(self, *Locks::runtime_shutdown_lock_); |
Mathieu Chartier | 661974a | 2014-01-09 11:23:53 -0800 | [diff] [blame] | 826 | SetQuickAllocEntryPointsInstrumented(instrumented); |
| 827 | ResetQuickAllocEntryPoints(); |
Mathieu Chartier | 50e9331 | 2016-03-16 11:25:29 -0700 | [diff] [blame] | 828 | alloc_entrypoints_instrumented_ = instrumented; |
Mathieu Chartier | 4f55e22 | 2015-09-04 13:26:21 -0700 | [diff] [blame] | 829 | } else { |
| 830 | MutexLock mu(self, *Locks::runtime_shutdown_lock_); |
| 831 | SetQuickAllocEntryPointsInstrumented(instrumented); |
Andreas Gampe | 157c77e | 2016-10-17 17:44:41 -0700 | [diff] [blame] | 832 | |
| 833 | // Note: ResetQuickAllocEntryPoints only works when the runtime is started. Manually run the |
| 834 | // update for just this thread. |
Andreas Gampe | 162ae50 | 2016-10-18 10:03:42 -0700 | [diff] [blame] | 835 | // Note: self may be null. One of those paths is setting instrumentation in the Heap |
| 836 | // constructor for gcstress mode. |
| 837 | if (self != nullptr) { |
| 838 | ResetQuickAllocEntryPointsForThread(self, nullptr); |
| 839 | } |
Andreas Gampe | 157c77e | 2016-10-17 17:44:41 -0700 | [diff] [blame] | 840 | |
Mathieu Chartier | 50e9331 | 2016-03-16 11:25:29 -0700 | [diff] [blame] | 841 | alloc_entrypoints_instrumented_ = instrumented; |
Mathieu Chartier | 661974a | 2014-01-09 11:23:53 -0800 | [diff] [blame] | 842 | } |
| 843 | } |
| 844 | |
Mathieu Chartier | 9ef78b5 | 2014-09-25 17:03:12 -0700 | [diff] [blame] | 845 | void Instrumentation::InstrumentQuickAllocEntryPoints() { |
| 846 | MutexLock mu(Thread::Current(), *Locks::instrument_entrypoints_lock_); |
| 847 | InstrumentQuickAllocEntryPointsLocked(); |
Ian Rogers | fa82427 | 2013-11-05 16:12:57 -0800 | [diff] [blame] | 848 | } |
| 849 | |
Mathieu Chartier | 9ef78b5 | 2014-09-25 17:03:12 -0700 | [diff] [blame] | 850 | void Instrumentation::UninstrumentQuickAllocEntryPoints() { |
| 851 | MutexLock mu(Thread::Current(), *Locks::instrument_entrypoints_lock_); |
| 852 | UninstrumentQuickAllocEntryPointsLocked(); |
| 853 | } |
| 854 | |
| 855 | void Instrumentation::InstrumentQuickAllocEntryPointsLocked() { |
| 856 | Locks::instrument_entrypoints_lock_->AssertHeld(Thread::Current()); |
| 857 | if (quick_alloc_entry_points_instrumentation_counter_ == 0) { |
| 858 | SetEntrypointsInstrumented(true); |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 859 | } |
Mathieu Chartier | 9ef78b5 | 2014-09-25 17:03:12 -0700 | [diff] [blame] | 860 | ++quick_alloc_entry_points_instrumentation_counter_; |
Mathieu Chartier | 9ef78b5 | 2014-09-25 17:03:12 -0700 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | void Instrumentation::UninstrumentQuickAllocEntryPointsLocked() { |
| 864 | Locks::instrument_entrypoints_lock_->AssertHeld(Thread::Current()); |
| 865 | CHECK_GT(quick_alloc_entry_points_instrumentation_counter_, 0U); |
| 866 | --quick_alloc_entry_points_instrumentation_counter_; |
| 867 | if (quick_alloc_entry_points_instrumentation_counter_ == 0) { |
| 868 | SetEntrypointsInstrumented(false); |
| 869 | } |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 870 | } |
| 871 | |
| 872 | void Instrumentation::ResetQuickAllocEntryPoints() { |
| 873 | Runtime* runtime = Runtime::Current(); |
| 874 | if (runtime->IsStarted()) { |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 875 | MutexLock mu(Thread::Current(), *Locks::thread_list_lock_); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 876 | runtime->GetThreadList()->ForEach(ResetQuickAllocEntryPointsForThread, nullptr); |
Ian Rogers | fa82427 | 2013-11-05 16:12:57 -0800 | [diff] [blame] | 877 | } |
| 878 | } |
| 879 | |
Mingyao Yang | 3fd448a | 2016-05-10 14:30:41 -0700 | [diff] [blame] | 880 | void Instrumentation::UpdateMethodsCodeImpl(ArtMethod* method, const void* quick_code) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 881 | const void* new_quick_code; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 882 | if (LIKELY(!instrumentation_stubs_installed_)) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 883 | new_quick_code = quick_code; |
Jeff Hao | 65d15d9 | 2013-07-16 16:39:33 -0700 | [diff] [blame] | 884 | } else { |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 885 | if ((interpreter_stubs_installed_ || IsDeoptimized(method)) && !method->IsNative()) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 886 | new_quick_code = GetQuickToInterpreterBridge(); |
Jeff Hao | 65d15d9 | 2013-07-16 16:39:33 -0700 | [diff] [blame] | 887 | } else { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 888 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 889 | if (class_linker->IsQuickResolutionStub(quick_code) || |
| 890 | class_linker->IsQuickToInterpreterBridge(quick_code)) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 891 | new_quick_code = quick_code; |
Alex Light | 6cae5ea | 2018-06-07 17:07:02 -0700 | [diff] [blame] | 892 | } else if (entry_exit_stubs_installed_ && |
| 893 | // We need to make sure not to replace anything that InstallStubsForMethod |
| 894 | // wouldn't. Specifically we cannot stub out Proxy.<init> since subtypes copy the |
| 895 | // implementation directly and this will confuse the instrumentation trampolines. |
| 896 | // TODO We should remove the need for this since it makes it impossible to profile |
| 897 | // Proxy.<init> correctly in all cases. |
| 898 | method != jni::DecodeArtMethod(WellKnownClasses::java_lang_reflect_Proxy_init)) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 899 | new_quick_code = GetQuickInstrumentationEntryPoint(); |
Alex Light | 2d441b1 | 2018-06-08 15:33:21 -0700 | [diff] [blame] | 900 | if (!method->IsNative() && Runtime::Current()->GetJit() != nullptr) { |
| 901 | // Native methods use trampoline entrypoints during interpreter tracing. |
Nicolas Geoffray | 226805d | 2018-12-14 10:59:02 +0000 | [diff] [blame] | 902 | DCHECK(!Runtime::Current()->GetJit()->GetCodeCache()->GetGarbageCollectCodeUnsafe()); |
Alex Light | 2d441b1 | 2018-06-08 15:33:21 -0700 | [diff] [blame] | 903 | ProfilingInfo* profiling_info = method->GetProfilingInfo(kRuntimePointerSize); |
| 904 | // Tracing will look at the saved entry point in the profiling info to know the actual |
| 905 | // entrypoint, so we store it here. |
| 906 | if (profiling_info != nullptr) { |
| 907 | profiling_info->SetSavedEntryPoint(quick_code); |
| 908 | } |
| 909 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 910 | } else { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 911 | new_quick_code = quick_code; |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 912 | } |
Jeff Hao | 65d15d9 | 2013-07-16 16:39:33 -0700 | [diff] [blame] | 913 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 914 | } |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 915 | UpdateEntrypoints(method, new_quick_code); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 916 | } |
| 917 | |
Nicolas Geoffray | a6e0e7d | 2018-01-26 13:16:50 +0000 | [diff] [blame] | 918 | void Instrumentation::UpdateNativeMethodsCodeToJitCode(ArtMethod* method, const void* quick_code) { |
| 919 | // We don't do any read barrier on `method`'s declaring class in this code, as the JIT might |
| 920 | // enter here on a soon-to-be deleted ArtMethod. Updating the entrypoint is OK though, as |
| 921 | // the ArtMethod is still in memory. |
| 922 | const void* new_quick_code = quick_code; |
| 923 | if (UNLIKELY(instrumentation_stubs_installed_) && entry_exit_stubs_installed_) { |
| 924 | new_quick_code = GetQuickInstrumentationEntryPoint(); |
| 925 | } |
| 926 | UpdateEntrypoints(method, new_quick_code); |
| 927 | } |
| 928 | |
Mingyao Yang | 3fd448a | 2016-05-10 14:30:41 -0700 | [diff] [blame] | 929 | void Instrumentation::UpdateMethodsCode(ArtMethod* method, const void* quick_code) { |
| 930 | DCHECK(method->GetDeclaringClass()->IsResolved()); |
| 931 | UpdateMethodsCodeImpl(method, quick_code); |
| 932 | } |
| 933 | |
Alex Light | 0a5ec3d | 2017-07-25 16:50:26 -0700 | [diff] [blame] | 934 | void Instrumentation::UpdateMethodsCodeToInterpreterEntryPoint(ArtMethod* method) { |
| 935 | UpdateMethodsCodeImpl(method, GetQuickToInterpreterBridge()); |
| 936 | } |
| 937 | |
Nicolas Geoffray | a0619e2 | 2016-12-20 13:57:43 +0000 | [diff] [blame] | 938 | void Instrumentation::UpdateMethodsCodeForJavaDebuggable(ArtMethod* method, |
| 939 | const void* quick_code) { |
| 940 | // When the runtime is set to Java debuggable, we may update the entry points of |
| 941 | // all methods of a class to the interpreter bridge. A method's declaring class |
| 942 | // might not be in resolved state yet in that case, so we bypass the DCHECK in |
| 943 | // UpdateMethodsCode. |
Mingyao Yang | 3fd448a | 2016-05-10 14:30:41 -0700 | [diff] [blame] | 944 | UpdateMethodsCodeImpl(method, quick_code); |
| 945 | } |
| 946 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 947 | bool Instrumentation::AddDeoptimizedMethod(ArtMethod* method) { |
| 948 | if (IsDeoptimizedMethod(method)) { |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 949 | // Already in the map. Return. |
| 950 | return false; |
| 951 | } |
| 952 | // Not found. Add it. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 953 | deoptimized_methods_.insert(method); |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 954 | return true; |
| 955 | } |
| 956 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 957 | bool Instrumentation::IsDeoptimizedMethod(ArtMethod* method) { |
| 958 | return deoptimized_methods_.find(method) != deoptimized_methods_.end(); |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 959 | } |
| 960 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 961 | ArtMethod* Instrumentation::BeginDeoptimizedMethod() { |
| 962 | if (deoptimized_methods_.empty()) { |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 963 | // Empty. |
| 964 | return nullptr; |
| 965 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 966 | return *deoptimized_methods_.begin(); |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 967 | } |
| 968 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 969 | bool Instrumentation::RemoveDeoptimizedMethod(ArtMethod* method) { |
| 970 | auto it = deoptimized_methods_.find(method); |
| 971 | if (it == deoptimized_methods_.end()) { |
| 972 | return false; |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 973 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 974 | deoptimized_methods_.erase(it); |
| 975 | return true; |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 976 | } |
| 977 | |
| 978 | bool Instrumentation::IsDeoptimizedMethodsEmpty() const { |
| 979 | return deoptimized_methods_.empty(); |
| 980 | } |
| 981 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 982 | void Instrumentation::Deoptimize(ArtMethod* method) { |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 983 | CHECK(!method->IsNative()); |
| 984 | CHECK(!method->IsProxyMethod()); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 985 | CHECK(method->IsInvokable()); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 986 | |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 987 | Thread* self = Thread::Current(); |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 988 | { |
Andreas Gampe | 7e56a07 | 2018-11-29 10:40:06 -0800 | [diff] [blame] | 989 | WriterMutexLock mu(self, *GetDeoptimizedMethodsLock()); |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 990 | bool has_not_been_deoptimized = AddDeoptimizedMethod(method); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 991 | CHECK(has_not_been_deoptimized) << "Method " << ArtMethod::PrettyMethod(method) |
Daniel Mihalyi | ca1d06c | 2014-08-18 18:45:31 +0200 | [diff] [blame] | 992 | << " is already deoptimized"; |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 993 | } |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 994 | if (!interpreter_stubs_installed_) { |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 995 | UpdateEntrypoints(method, GetQuickInstrumentationEntryPoint()); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 996 | |
| 997 | // Install instrumentation exit stub and instrumentation frames. We may already have installed |
| 998 | // these previously so it will only cover the newly created frames. |
| 999 | instrumentation_stubs_installed_ = true; |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 1000 | MutexLock mu(self, *Locks::thread_list_lock_); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 1001 | Runtime::Current()->GetThreadList()->ForEach(InstrumentationInstallStack, this); |
| 1002 | } |
| 1003 | } |
| 1004 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1005 | void Instrumentation::Undeoptimize(ArtMethod* method) { |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 1006 | CHECK(!method->IsNative()); |
| 1007 | CHECK(!method->IsProxyMethod()); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 1008 | CHECK(method->IsInvokable()); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 1009 | |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 1010 | Thread* self = Thread::Current(); |
| 1011 | bool empty; |
| 1012 | { |
Andreas Gampe | 7e56a07 | 2018-11-29 10:40:06 -0800 | [diff] [blame] | 1013 | WriterMutexLock mu(self, *GetDeoptimizedMethodsLock()); |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 1014 | bool found_and_erased = RemoveDeoptimizedMethod(method); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1015 | CHECK(found_and_erased) << "Method " << ArtMethod::PrettyMethod(method) |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 1016 | << " is not deoptimized"; |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 1017 | empty = IsDeoptimizedMethodsEmpty(); |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 1018 | } |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 1019 | |
| 1020 | // Restore code and possibly stack only if we did not deoptimize everything. |
| 1021 | if (!interpreter_stubs_installed_) { |
| 1022 | // Restore its code or resolution trampoline. |
| 1023 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1024 | if (method->IsStatic() && !method->IsConstructor() && |
| 1025 | !method->GetDeclaringClass()->IsInitialized()) { |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 1026 | UpdateEntrypoints(method, GetQuickResolutionStub()); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 1027 | } else { |
Nicolas Geoffray | a0619e2 | 2016-12-20 13:57:43 +0000 | [diff] [blame] | 1028 | const void* quick_code = NeedDebugVersionFor(method) |
| 1029 | ? GetQuickToInterpreterBridge() |
Alex Light | fc49fec | 2018-01-16 22:28:36 +0000 | [diff] [blame] | 1030 | : class_linker->GetQuickOatCodeFor(method); |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 1031 | UpdateEntrypoints(method, quick_code); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 1032 | } |
| 1033 | |
| 1034 | // If there is no deoptimized method left, we can restore the stack of each thread. |
Alex Light | f244a57 | 2018-06-08 13:56:51 -0700 | [diff] [blame] | 1035 | if (empty && !entry_exit_stubs_installed_) { |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 1036 | MutexLock mu(self, *Locks::thread_list_lock_); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 1037 | Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this); |
| 1038 | instrumentation_stubs_installed_ = false; |
| 1039 | } |
| 1040 | } |
| 1041 | } |
| 1042 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1043 | bool Instrumentation::IsDeoptimized(ArtMethod* method) { |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 1044 | DCHECK(method != nullptr); |
Andreas Gampe | 7e56a07 | 2018-11-29 10:40:06 -0800 | [diff] [blame] | 1045 | ReaderMutexLock mu(Thread::Current(), *GetDeoptimizedMethodsLock()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1046 | return IsDeoptimizedMethod(method); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 1047 | } |
| 1048 | |
| 1049 | void Instrumentation::EnableDeoptimization() { |
Andreas Gampe | 7e56a07 | 2018-11-29 10:40:06 -0800 | [diff] [blame] | 1050 | ReaderMutexLock mu(Thread::Current(), *GetDeoptimizedMethodsLock()); |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 1051 | CHECK(IsDeoptimizedMethodsEmpty()); |
Sebastien Hertz | 11d40c2 | 2014-02-19 18:00:17 +0100 | [diff] [blame] | 1052 | CHECK_EQ(deoptimization_enabled_, false); |
| 1053 | deoptimization_enabled_ = true; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 1054 | } |
| 1055 | |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 1056 | void Instrumentation::DisableDeoptimization(const char* key) { |
Sebastien Hertz | 11d40c2 | 2014-02-19 18:00:17 +0100 | [diff] [blame] | 1057 | CHECK_EQ(deoptimization_enabled_, true); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 1058 | // If we deoptimized everything, undo it. |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 1059 | InstrumentationLevel level = GetCurrentInstrumentationLevel(); |
| 1060 | if (level == InstrumentationLevel::kInstrumentWithInterpreter) { |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 1061 | UndeoptimizeEverything(key); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 1062 | } |
| 1063 | // Undeoptimized selected methods. |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 1064 | while (true) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1065 | ArtMethod* method; |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 1066 | { |
Andreas Gampe | 7e56a07 | 2018-11-29 10:40:06 -0800 | [diff] [blame] | 1067 | ReaderMutexLock mu(Thread::Current(), *GetDeoptimizedMethodsLock()); |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 1068 | if (IsDeoptimizedMethodsEmpty()) { |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 1069 | break; |
| 1070 | } |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 1071 | method = BeginDeoptimizedMethod(); |
| 1072 | CHECK(method != nullptr); |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 1073 | } |
| 1074 | Undeoptimize(method); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 1075 | } |
Sebastien Hertz | 11d40c2 | 2014-02-19 18:00:17 +0100 | [diff] [blame] | 1076 | deoptimization_enabled_ = false; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 1077 | } |
| 1078 | |
Sebastien Hertz | 11d40c2 | 2014-02-19 18:00:17 +0100 | [diff] [blame] | 1079 | // Indicates if instrumentation should notify method enter/exit events to the listeners. |
| 1080 | bool Instrumentation::ShouldNotifyMethodEnterExitEvents() const { |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 1081 | if (!HasMethodEntryListeners() && !HasMethodExitListeners()) { |
| 1082 | return false; |
| 1083 | } |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 1084 | return !deoptimization_enabled_ && !interpreter_stubs_installed_; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 1085 | } |
| 1086 | |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 1087 | void Instrumentation::DeoptimizeEverything(const char* key) { |
| 1088 | CHECK(deoptimization_enabled_); |
| 1089 | ConfigureStubs(key, InstrumentationLevel::kInstrumentWithInterpreter); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 1090 | } |
| 1091 | |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 1092 | void Instrumentation::UndeoptimizeEverything(const char* key) { |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 1093 | CHECK(interpreter_stubs_installed_); |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 1094 | CHECK(deoptimization_enabled_); |
| 1095 | ConfigureStubs(key, InstrumentationLevel::kInstrumentNothing); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 1096 | } |
| 1097 | |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 1098 | void Instrumentation::EnableMethodTracing(const char* key, bool needs_interpreter) { |
| 1099 | InstrumentationLevel level; |
| 1100 | if (needs_interpreter) { |
| 1101 | level = InstrumentationLevel::kInstrumentWithInterpreter; |
| 1102 | } else { |
| 1103 | level = InstrumentationLevel::kInstrumentWithInstrumentationStubs; |
| 1104 | } |
| 1105 | ConfigureStubs(key, level); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 1106 | } |
| 1107 | |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 1108 | void Instrumentation::DisableMethodTracing(const char* key) { |
| 1109 | ConfigureStubs(key, InstrumentationLevel::kInstrumentNothing); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 1110 | } |
| 1111 | |
Alex Light | 2d441b1 | 2018-06-08 15:33:21 -0700 | [diff] [blame] | 1112 | const void* Instrumentation::GetCodeForInvoke(ArtMethod* method) const { |
| 1113 | // This is called by instrumentation entry only and that should never be getting proxy methods. |
| 1114 | DCHECK(!method->IsProxyMethod()) << method->PrettyMethod(); |
| 1115 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 1116 | if (LIKELY(!instrumentation_stubs_installed_ && !interpreter_stubs_installed_)) { |
| 1117 | // In general we just return whatever the method thinks its entrypoint is here. The only |
| 1118 | // exception is if it still has the instrumentation entrypoint. That means we are racing another |
| 1119 | // thread getting rid of instrumentation which is unexpected but possible. In that case we want |
| 1120 | // to wait and try to get it from the oat file or jit. |
| 1121 | const void* code = method->GetEntryPointFromQuickCompiledCodePtrSize(kRuntimePointerSize); |
| 1122 | DCHECK(code != nullptr); |
| 1123 | if (code != GetQuickInstrumentationEntryPoint()) { |
| 1124 | return code; |
| 1125 | } else if (method->IsNative()) { |
| 1126 | return class_linker->GetQuickOatCodeFor(method); |
| 1127 | } |
| 1128 | // We don't know what it is. Fallthough to try to find the code from the JIT or Oat file. |
| 1129 | } else if (method->IsNative()) { |
| 1130 | // TODO We could have JIT compiled native entrypoints. It might be worth it to find these. |
| 1131 | return class_linker->GetQuickOatCodeFor(method); |
| 1132 | } else if (UNLIKELY(interpreter_stubs_installed_)) { |
| 1133 | return GetQuickToInterpreterBridge(); |
| 1134 | } |
| 1135 | // Since the method cannot be native due to ifs above we can always fall back to interpreter |
| 1136 | // bridge. |
| 1137 | const void* result = GetQuickToInterpreterBridge(); |
| 1138 | if (!NeedDebugVersionFor(method)) { |
| 1139 | // If we don't need a debug version we should see what the oat file/class linker has to say. |
| 1140 | result = class_linker->GetQuickOatCodeFor(method); |
| 1141 | } |
| 1142 | // If both those fail try the jit. |
| 1143 | if (result == GetQuickToInterpreterBridge()) { |
| 1144 | jit::Jit* jit = Runtime::Current()->GetJit(); |
| 1145 | if (jit != nullptr) { |
| 1146 | const void* res = jit->GetCodeCache()->FindCompiledCodeForInstrumentation(method); |
| 1147 | if (res != nullptr) { |
| 1148 | result = res; |
| 1149 | } |
| 1150 | } |
| 1151 | } |
| 1152 | return result; |
| 1153 | } |
| 1154 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1155 | const void* Instrumentation::GetQuickCodeFor(ArtMethod* method, PointerSize pointer_size) const { |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 1156 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1157 | if (LIKELY(!instrumentation_stubs_installed_)) { |
Mathieu Chartier | a7dd038 | 2014-11-20 17:08:58 -0800 | [diff] [blame] | 1158 | const void* code = method->GetEntryPointFromQuickCompiledCodePtrSize(pointer_size); |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 1159 | DCHECK(code != nullptr); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 1160 | if (LIKELY(!class_linker->IsQuickResolutionStub(code) && |
| 1161 | !class_linker->IsQuickToInterpreterBridge(code)) && |
| 1162 | !class_linker->IsQuickResolutionStub(code) && |
| 1163 | !class_linker->IsQuickToInterpreterBridge(code)) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1164 | return code; |
| 1165 | } |
| 1166 | } |
Alex Light | fc49fec | 2018-01-16 22:28:36 +0000 | [diff] [blame] | 1167 | return class_linker->GetQuickOatCodeFor(method); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 1168 | } |
| 1169 | |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 1170 | void Instrumentation::MethodEnterEventImpl(Thread* thread, |
| 1171 | ObjPtr<mirror::Object> this_object, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1172 | ArtMethod* method, |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1173 | uint32_t dex_pc) const { |
Mingyao Yang | 2ee1790 | 2017-08-30 11:37:08 -0700 | [diff] [blame] | 1174 | DCHECK(!method->IsRuntimeMethod()); |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 1175 | if (HasMethodEntryListeners()) { |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 1176 | Thread* self = Thread::Current(); |
| 1177 | StackHandleScope<1> hs(self); |
| 1178 | Handle<mirror::Object> thiz(hs.NewHandle(this_object)); |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 1179 | for (InstrumentationListener* listener : method_entry_listeners_) { |
| 1180 | if (listener != nullptr) { |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 1181 | listener->MethodEntered(thread, thiz, method, dex_pc); |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 1182 | } |
| 1183 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1184 | } |
| 1185 | } |
| 1186 | |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 1187 | template <> |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 1188 | void Instrumentation::MethodExitEventImpl(Thread* thread, |
| 1189 | ObjPtr<mirror::Object> this_object, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1190 | ArtMethod* method, |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 1191 | uint32_t dex_pc, |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 1192 | OptionalFrame frame, |
| 1193 | MutableHandle<mirror::Object>& return_value) const { |
| 1194 | if (HasMethodExitListeners()) { |
| 1195 | Thread* self = Thread::Current(); |
| 1196 | StackHandleScope<1> hs(self); |
| 1197 | Handle<mirror::Object> thiz(hs.NewHandle(this_object)); |
| 1198 | for (InstrumentationListener* listener : method_exit_listeners_) { |
| 1199 | if (listener != nullptr) { |
| 1200 | listener->MethodExited(thread, thiz, method, dex_pc, frame, return_value); |
| 1201 | } |
| 1202 | } |
| 1203 | } |
| 1204 | } |
| 1205 | |
| 1206 | template<> void Instrumentation::MethodExitEventImpl(Thread* thread, |
| 1207 | ObjPtr<mirror::Object> this_object, |
| 1208 | ArtMethod* method, |
| 1209 | uint32_t dex_pc, |
| 1210 | OptionalFrame frame, |
| 1211 | JValue& return_value) const { |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 1212 | if (HasMethodExitListeners()) { |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 1213 | Thread* self = Thread::Current(); |
| 1214 | StackHandleScope<2> hs(self); |
| 1215 | Handle<mirror::Object> thiz(hs.NewHandle(this_object)); |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 1216 | if (method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetReturnTypePrimitive() != |
| 1217 | Primitive::kPrimNot) { |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 1218 | for (InstrumentationListener* listener : method_exit_listeners_) { |
| 1219 | if (listener != nullptr) { |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 1220 | listener->MethodExited(thread, thiz, method, dex_pc, frame, return_value); |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 1221 | } |
| 1222 | } |
| 1223 | } else { |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 1224 | MutableHandle<mirror::Object> ret(hs.NewHandle(return_value.GetL())); |
| 1225 | MethodExitEventImpl(thread, thiz.Get(), method, dex_pc, frame, ret); |
| 1226 | return_value.SetL(ret.Get()); |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 1227 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1228 | } |
| 1229 | } |
| 1230 | |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 1231 | void Instrumentation::MethodUnwindEvent(Thread* thread, |
Vladimir Marko | 19711d4 | 2019-04-12 14:05:34 +0100 | [diff] [blame] | 1232 | ObjPtr<mirror::Object> this_object, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1233 | ArtMethod* method, |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1234 | uint32_t dex_pc) const { |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 1235 | if (HasMethodUnwindListeners()) { |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 1236 | Thread* self = Thread::Current(); |
| 1237 | StackHandleScope<1> hs(self); |
| 1238 | Handle<mirror::Object> thiz(hs.NewHandle(this_object)); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 1239 | for (InstrumentationListener* listener : method_unwind_listeners_) { |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 1240 | if (listener != nullptr) { |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 1241 | listener->MethodUnwind(thread, thiz, method, dex_pc); |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 1242 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1243 | } |
| 1244 | } |
| 1245 | } |
| 1246 | |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 1247 | void Instrumentation::DexPcMovedEventImpl(Thread* thread, |
| 1248 | ObjPtr<mirror::Object> this_object, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1249 | ArtMethod* method, |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1250 | uint32_t dex_pc) const { |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 1251 | Thread* self = Thread::Current(); |
| 1252 | StackHandleScope<1> hs(self); |
| 1253 | Handle<mirror::Object> thiz(hs.NewHandle(this_object)); |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 1254 | for (InstrumentationListener* listener : dex_pc_listeners_) { |
| 1255 | if (listener != nullptr) { |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 1256 | listener->DexPcMoved(thread, thiz, method, dex_pc); |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 1257 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1258 | } |
| 1259 | } |
| 1260 | |
Nicolas Geoffray | 81f0f95 | 2016-01-20 16:25:19 +0000 | [diff] [blame] | 1261 | void Instrumentation::BranchImpl(Thread* thread, |
| 1262 | ArtMethod* method, |
| 1263 | uint32_t dex_pc, |
| 1264 | int32_t offset) const { |
| 1265 | for (InstrumentationListener* listener : branch_listeners_) { |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 1266 | if (listener != nullptr) { |
Nicolas Geoffray | 81f0f95 | 2016-01-20 16:25:19 +0000 | [diff] [blame] | 1267 | listener->Branch(thread, method, dex_pc, offset); |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 1268 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 1269 | } |
| 1270 | } |
| 1271 | |
Alex Light | e814f9d | 2017-07-31 16:14:39 -0700 | [diff] [blame] | 1272 | void Instrumentation::WatchedFramePopImpl(Thread* thread, const ShadowFrame& frame) const { |
| 1273 | for (InstrumentationListener* listener : watched_frame_pop_listeners_) { |
| 1274 | if (listener != nullptr) { |
| 1275 | listener->WatchedFramePop(thread, frame); |
| 1276 | } |
| 1277 | } |
| 1278 | } |
| 1279 | |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 1280 | void Instrumentation::FieldReadEventImpl(Thread* thread, |
| 1281 | ObjPtr<mirror::Object> this_object, |
| 1282 | ArtMethod* method, |
| 1283 | uint32_t dex_pc, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1284 | ArtField* field) const { |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 1285 | Thread* self = Thread::Current(); |
| 1286 | StackHandleScope<1> hs(self); |
| 1287 | Handle<mirror::Object> thiz(hs.NewHandle(this_object)); |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 1288 | for (InstrumentationListener* listener : field_read_listeners_) { |
| 1289 | if (listener != nullptr) { |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 1290 | listener->FieldRead(thread, thiz, method, dex_pc, field); |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 1291 | } |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 1292 | } |
| 1293 | } |
| 1294 | |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 1295 | void Instrumentation::FieldWriteEventImpl(Thread* thread, |
| 1296 | ObjPtr<mirror::Object> this_object, |
| 1297 | ArtMethod* method, |
| 1298 | uint32_t dex_pc, |
| 1299 | ArtField* field, |
| 1300 | const JValue& field_value) const { |
| 1301 | Thread* self = Thread::Current(); |
| 1302 | StackHandleScope<2> hs(self); |
| 1303 | Handle<mirror::Object> thiz(hs.NewHandle(this_object)); |
| 1304 | if (field->IsPrimitiveType()) { |
| 1305 | for (InstrumentationListener* listener : field_write_listeners_) { |
| 1306 | if (listener != nullptr) { |
| 1307 | listener->FieldWritten(thread, thiz, method, dex_pc, field, field_value); |
| 1308 | } |
| 1309 | } |
| 1310 | } else { |
| 1311 | Handle<mirror::Object> val(hs.NewHandle(field_value.GetL())); |
| 1312 | for (InstrumentationListener* listener : field_write_listeners_) { |
| 1313 | if (listener != nullptr) { |
| 1314 | listener->FieldWritten(thread, thiz, method, dex_pc, field, val); |
| 1315 | } |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 1316 | } |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 1317 | } |
| 1318 | } |
| 1319 | |
Alex Light | 6e1607e | 2017-08-23 10:06:18 -0700 | [diff] [blame] | 1320 | void Instrumentation::ExceptionThrownEvent(Thread* thread, |
Vladimir Marko | 19711d4 | 2019-04-12 14:05:34 +0100 | [diff] [blame] | 1321 | ObjPtr<mirror::Throwable> exception_object) const { |
Hiroshi Yamauchi | 3481f7a | 2017-02-10 12:07:36 -0800 | [diff] [blame] | 1322 | Thread* self = Thread::Current(); |
| 1323 | StackHandleScope<1> hs(self); |
| 1324 | Handle<mirror::Throwable> h_exception(hs.NewHandle(exception_object)); |
Alex Light | 6e1607e | 2017-08-23 10:06:18 -0700 | [diff] [blame] | 1325 | if (HasExceptionThrownListeners()) { |
Hiroshi Yamauchi | 3481f7a | 2017-02-10 12:07:36 -0800 | [diff] [blame] | 1326 | DCHECK_EQ(thread->GetException(), h_exception.Get()); |
Jeff Hao | c0bd4da | 2013-04-11 15:52:28 -0700 | [diff] [blame] | 1327 | thread->ClearException(); |
Alex Light | 6e1607e | 2017-08-23 10:06:18 -0700 | [diff] [blame] | 1328 | for (InstrumentationListener* listener : exception_thrown_listeners_) { |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 1329 | if (listener != nullptr) { |
Alex Light | 6e1607e | 2017-08-23 10:06:18 -0700 | [diff] [blame] | 1330 | listener->ExceptionThrown(thread, h_exception); |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 1331 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1332 | } |
Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame] | 1333 | // See b/65049545 for discussion about this behavior. |
| 1334 | thread->AssertNoPendingException(); |
Hiroshi Yamauchi | 3481f7a | 2017-02-10 12:07:36 -0800 | [diff] [blame] | 1335 | thread->SetException(h_exception.Get()); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1336 | } |
| 1337 | } |
| 1338 | |
Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame] | 1339 | void Instrumentation::ExceptionHandledEvent(Thread* thread, |
Vladimir Marko | 19711d4 | 2019-04-12 14:05:34 +0100 | [diff] [blame] | 1340 | ObjPtr<mirror::Throwable> exception_object) const { |
Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame] | 1341 | Thread* self = Thread::Current(); |
| 1342 | StackHandleScope<1> hs(self); |
| 1343 | Handle<mirror::Throwable> h_exception(hs.NewHandle(exception_object)); |
| 1344 | if (HasExceptionHandledListeners()) { |
| 1345 | // We should have cleared the exception so that callers can detect a new one. |
| 1346 | DCHECK(thread->GetException() == nullptr); |
| 1347 | for (InstrumentationListener* listener : exception_handled_listeners_) { |
| 1348 | if (listener != nullptr) { |
| 1349 | listener->ExceptionHandled(thread, h_exception); |
| 1350 | } |
| 1351 | } |
| 1352 | } |
| 1353 | } |
| 1354 | |
Sebastien Hertz | b2feaaf | 2015-10-12 13:40:10 +0000 | [diff] [blame] | 1355 | // Computes a frame ID by ignoring inlined frames. |
| 1356 | size_t Instrumentation::ComputeFrameId(Thread* self, |
| 1357 | size_t frame_depth, |
| 1358 | size_t inlined_frames_before_frame) { |
| 1359 | CHECK_GE(frame_depth, inlined_frames_before_frame); |
| 1360 | size_t no_inline_depth = frame_depth - inlined_frames_before_frame; |
| 1361 | return StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk) - no_inline_depth; |
| 1362 | } |
| 1363 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1364 | static void CheckStackDepth(Thread* self, const InstrumentationStackFrame& instrumentation_frame, |
| 1365 | int delta) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1366 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 1367 | size_t frame_id = StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk) + delta; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1368 | if (frame_id != instrumentation_frame.frame_id_) { |
| 1369 | LOG(ERROR) << "Expected frame_id=" << frame_id << " but found " |
| 1370 | << instrumentation_frame.frame_id_; |
| 1371 | StackVisitor::DescribeStack(self); |
| 1372 | CHECK_EQ(frame_id, instrumentation_frame.frame_id_); |
| 1373 | } |
| 1374 | } |
| 1375 | |
Vladimir Marko | 19711d4 | 2019-04-12 14:05:34 +0100 | [diff] [blame] | 1376 | void Instrumentation::PushInstrumentationStackFrame(Thread* self, |
| 1377 | ObjPtr<mirror::Object> this_object, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1378 | ArtMethod* method, |
Vladimir Marko | 19711d4 | 2019-04-12 14:05:34 +0100 | [diff] [blame] | 1379 | uintptr_t lr, |
| 1380 | bool interpreter_entry) { |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 1381 | DCHECK(!self->IsExceptionPending()); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1382 | std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack(); |
| 1383 | if (kVerboseInstrumentation) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1384 | LOG(INFO) << "Entering " << ArtMethod::PrettyMethod(method) << " from PC " |
| 1385 | << reinterpret_cast<void*>(lr); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1386 | } |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 1387 | |
| 1388 | // We send the enter event before pushing the instrumentation frame to make cleanup easier. If the |
| 1389 | // event causes an exception we can simply send the unwind event and return. |
| 1390 | StackHandleScope<1> hs(self); |
| 1391 | Handle<mirror::Object> h_this(hs.NewHandle(this_object)); |
| 1392 | if (!interpreter_entry) { |
| 1393 | MethodEnterEvent(self, h_this.Get(), method, 0); |
| 1394 | if (self->IsExceptionPending()) { |
| 1395 | MethodUnwindEvent(self, h_this.Get(), method, 0); |
| 1396 | return; |
| 1397 | } |
| 1398 | } |
| 1399 | |
| 1400 | // We have a callee-save frame meaning this value is guaranteed to never be 0. |
| 1401 | DCHECK(!self->IsExceptionPending()); |
| 1402 | size_t frame_id = StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk); |
| 1403 | |
| 1404 | instrumentation::InstrumentationStackFrame instrumentation_frame(h_this.Get(), method, lr, |
Jeff Hao | 9a916d3 | 2013-06-27 18:45:37 -0700 | [diff] [blame] | 1405 | frame_id, interpreter_entry); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1406 | stack->push_front(instrumentation_frame); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1407 | } |
| 1408 | |
Mingyao Yang | 2ee1790 | 2017-08-30 11:37:08 -0700 | [diff] [blame] | 1409 | DeoptimizationMethodType Instrumentation::GetDeoptimizationMethodType(ArtMethod* method) { |
| 1410 | if (method->IsRuntimeMethod()) { |
| 1411 | // Certain methods have strict requirement on whether the dex instruction |
| 1412 | // should be re-executed upon deoptimization. |
| 1413 | if (method == Runtime::Current()->GetCalleeSaveMethod( |
| 1414 | CalleeSaveType::kSaveEverythingForClinit)) { |
| 1415 | return DeoptimizationMethodType::kKeepDexPc; |
| 1416 | } |
| 1417 | if (method == Runtime::Current()->GetCalleeSaveMethod( |
| 1418 | CalleeSaveType::kSaveEverythingForSuspendCheck)) { |
| 1419 | return DeoptimizationMethodType::kKeepDexPc; |
| 1420 | } |
| 1421 | } |
| 1422 | return DeoptimizationMethodType::kDefault; |
| 1423 | } |
| 1424 | |
| 1425 | // Try to get the shorty of a runtime method if it's an invocation stub. |
Andreas Gampe | c7d878d | 2018-11-19 18:42:06 +0000 | [diff] [blame] | 1426 | static char GetRuntimeMethodShorty(Thread* thread) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1427 | char shorty = 'V'; |
| 1428 | StackVisitor::WalkStack( |
| 1429 | [&shorty](const art::StackVisitor* stack_visitor) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1430 | ArtMethod* m = stack_visitor->GetMethod(); |
| 1431 | if (m == nullptr || m->IsRuntimeMethod()) { |
| 1432 | return true; |
Andreas Gampe | 3d477f3 | 2018-11-16 16:40:45 +0000 | [diff] [blame] | 1433 | } |
Andreas Gampe | c7d878d | 2018-11-19 18:42:06 +0000 | [diff] [blame] | 1434 | // The first Java method. |
| 1435 | if (m->IsNative()) { |
| 1436 | // Use JNI method's shorty for the jni stub. |
| 1437 | shorty = m->GetShorty()[0]; |
| 1438 | } else if (m->IsProxyMethod()) { |
| 1439 | // Proxy method just invokes its proxied method via |
| 1440 | // art_quick_proxy_invoke_handler. |
| 1441 | shorty = m->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty()[0]; |
| 1442 | } else { |
| 1443 | const Instruction& instr = m->DexInstructions().InstructionAt(stack_visitor->GetDexPc()); |
| 1444 | if (instr.IsInvoke()) { |
| 1445 | auto get_method_index_fn = [](ArtMethod* caller, |
| 1446 | const Instruction& inst, |
| 1447 | uint32_t dex_pc) |
| 1448 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1449 | switch (inst.Opcode()) { |
| 1450 | case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: |
| 1451 | case Instruction::INVOKE_VIRTUAL_QUICK: { |
| 1452 | uint16_t method_idx = caller->GetIndexFromQuickening(dex_pc); |
| 1453 | CHECK_NE(method_idx, DexFile::kDexNoIndex16); |
| 1454 | return method_idx; |
| 1455 | } |
| 1456 | default: { |
| 1457 | return static_cast<uint16_t>(inst.VRegB()); |
| 1458 | } |
| 1459 | } |
| 1460 | }; |
Nicolas Geoffray | ec43a01 | 2018-11-17 13:10:40 +0000 | [diff] [blame] | 1461 | |
Andreas Gampe | c7d878d | 2018-11-19 18:42:06 +0000 | [diff] [blame] | 1462 | uint16_t method_index = get_method_index_fn(m, instr, stack_visitor->GetDexPc()); |
| 1463 | const DexFile* dex_file = m->GetDexFile(); |
| 1464 | if (interpreter::IsStringInit(dex_file, method_index)) { |
| 1465 | // Invoking string init constructor is turned into invoking |
| 1466 | // StringFactory.newStringFromChars() which returns a string. |
| 1467 | shorty = 'L'; |
| 1468 | } else { |
| 1469 | shorty = dex_file->GetMethodShorty(method_index)[0]; |
| 1470 | } |
| 1471 | |
| 1472 | } else { |
| 1473 | // It could be that a non-invoke opcode invokes a stub, which in turn |
| 1474 | // invokes Java code. In such cases, we should never expect a return |
| 1475 | // value from the stub. |
| 1476 | } |
| 1477 | } |
| 1478 | // Stop stack walking since we've seen a Java frame. |
| 1479 | return false; |
| 1480 | }, |
| 1481 | thread, |
| 1482 | /* context= */ nullptr, |
| 1483 | art::StackVisitor::StackWalkKind::kIncludeInlinedFrames); |
| 1484 | return shorty; |
| 1485 | } |
Mingyao Yang | 2ee1790 | 2017-08-30 11:37:08 -0700 | [diff] [blame] | 1486 | |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 1487 | TwoWordReturn Instrumentation::PopInstrumentationStackFrame(Thread* self, |
| 1488 | uintptr_t* return_pc, |
| 1489 | uint64_t* gpr_result, |
| 1490 | uint64_t* fpr_result) { |
| 1491 | DCHECK(gpr_result != nullptr); |
| 1492 | DCHECK(fpr_result != nullptr); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1493 | // Do the pop. |
| 1494 | std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack(); |
| 1495 | CHECK_GT(stack->size(), 0U); |
| 1496 | InstrumentationStackFrame instrumentation_frame = stack->front(); |
| 1497 | stack->pop_front(); |
| 1498 | |
| 1499 | // Set return PC and check the sanity of the stack. |
| 1500 | *return_pc = instrumentation_frame.return_pc_; |
| 1501 | CheckStackDepth(self, instrumentation_frame, 0); |
Ian Rogers | 1d8cdbc | 2014-09-22 22:51:09 -0700 | [diff] [blame] | 1502 | self->VerifyStack(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1503 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1504 | ArtMethod* method = instrumentation_frame.method_; |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1505 | uint32_t length; |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1506 | const PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize(); |
Mingyao Yang | 2ee1790 | 2017-08-30 11:37:08 -0700 | [diff] [blame] | 1507 | char return_shorty; |
| 1508 | |
| 1509 | // Runtime method does not call into MethodExitEvent() so there should not be |
| 1510 | // suspension point below. |
| 1511 | ScopedAssertNoThreadSuspension ants(__FUNCTION__, method->IsRuntimeMethod()); |
| 1512 | if (method->IsRuntimeMethod()) { |
| 1513 | if (method != Runtime::Current()->GetCalleeSaveMethod( |
| 1514 | CalleeSaveType::kSaveEverythingForClinit)) { |
| 1515 | // If the caller is at an invocation point and the runtime method is not |
| 1516 | // for clinit, we need to pass return results to the caller. |
| 1517 | // We need the correct shorty to decide whether we need to pass the return |
| 1518 | // result for deoptimization below. |
Andreas Gampe | c7d878d | 2018-11-19 18:42:06 +0000 | [diff] [blame] | 1519 | return_shorty = GetRuntimeMethodShorty(self); |
Mingyao Yang | 2ee1790 | 2017-08-30 11:37:08 -0700 | [diff] [blame] | 1520 | } else { |
| 1521 | // Some runtime methods such as allocations, unresolved field getters, etc. |
| 1522 | // have return value. We don't need to set return_value since MethodExitEvent() |
| 1523 | // below isn't called for runtime methods. Deoptimization doesn't need the |
| 1524 | // value either since the dex instruction will be re-executed by the |
| 1525 | // interpreter, except these two cases: |
| 1526 | // (1) For an invoke, which is handled above to get the correct shorty. |
| 1527 | // (2) For MONITOR_ENTER/EXIT, which cannot be re-executed since it's not |
| 1528 | // idempotent. However there is no return value for it anyway. |
| 1529 | return_shorty = 'V'; |
| 1530 | } |
| 1531 | } else { |
| 1532 | return_shorty = method->GetInterfaceMethodIfProxy(pointer_size)->GetShorty(&length)[0]; |
| 1533 | } |
| 1534 | |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 1535 | bool is_ref = return_shorty == '[' || return_shorty == 'L'; |
| 1536 | StackHandleScope<1> hs(self); |
| 1537 | MutableHandle<mirror::Object> res(hs.NewHandle<mirror::Object>(nullptr)); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1538 | JValue return_value; |
| 1539 | if (return_shorty == 'V') { |
| 1540 | return_value.SetJ(0); |
| 1541 | } else if (return_shorty == 'F' || return_shorty == 'D') { |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 1542 | return_value.SetJ(*fpr_result); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1543 | } else { |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 1544 | return_value.SetJ(*gpr_result); |
| 1545 | } |
| 1546 | if (is_ref) { |
| 1547 | // Take a handle to the return value so we won't lose it if we suspend. |
| 1548 | res.Assign(return_value.GetL()); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1549 | } |
| 1550 | // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to |
| 1551 | // return_pc. |
Andreas Gampe | e2abbc6 | 2017-09-15 11:59:26 -0700 | [diff] [blame] | 1552 | uint32_t dex_pc = dex::kDexNoIndex; |
Mingyao Yang | 2ee1790 | 2017-08-30 11:37:08 -0700 | [diff] [blame] | 1553 | if (!method->IsRuntimeMethod() && !instrumentation_frame.interpreter_entry_) { |
Vladimir Marko | 19711d4 | 2019-04-12 14:05:34 +0100 | [diff] [blame] | 1554 | ObjPtr<mirror::Object> this_object = instrumentation_frame.this_object_; |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 1555 | MethodExitEvent( |
| 1556 | self, this_object, instrumentation_frame.method_, dex_pc, OptionalFrame{}, return_value); |
Sebastien Hertz | 320deb2 | 2014-06-11 19:45:05 +0200 | [diff] [blame] | 1557 | } |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 1558 | |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 1559 | // Deoptimize if the caller needs to continue execution in the interpreter. Do nothing if we get |
| 1560 | // back to an upcall. |
| 1561 | NthCallerVisitor visitor(self, 1, true); |
| 1562 | visitor.WalkStack(true); |
Sebastien Hertz | 270a0e1 | 2015-01-16 19:49:09 +0100 | [diff] [blame] | 1563 | bool deoptimize = (visitor.caller != nullptr) && |
Daniel Mihalyi | eb07669 | 2014-08-22 17:33:31 +0200 | [diff] [blame] | 1564 | (interpreter_stubs_installed_ || IsDeoptimized(visitor.caller) || |
Alex Light | 3dacdd6 | 2019-03-12 15:45:47 +0000 | [diff] [blame] | 1565 | self->IsForceInterpreter() || |
Daniel Mihalyi | eb07669 | 2014-08-22 17:33:31 +0200 | [diff] [blame] | 1566 | Dbg::IsForcedInterpreterNeededForUpcall(self, visitor.caller)); |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 1567 | if (is_ref) { |
| 1568 | // Restore the return value if it's a reference since it might have moved. |
| 1569 | *reinterpret_cast<mirror::Object**>(gpr_result) = res.Get(); |
| 1570 | } |
Nicolas Geoffray | a0619e2 | 2016-12-20 13:57:43 +0000 | [diff] [blame] | 1571 | if (deoptimize && Runtime::Current()->IsAsyncDeoptimizeable(*return_pc)) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1572 | if (kVerboseInstrumentation) { |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 1573 | LOG(INFO) << "Deoptimizing " |
| 1574 | << visitor.caller->PrettyMethod() |
| 1575 | << " by returning from " |
| 1576 | << method->PrettyMethod() |
| 1577 | << " with result " |
| 1578 | << std::hex << return_value.GetJ() << std::dec |
| 1579 | << " in " |
| 1580 | << *self; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1581 | } |
Mingyao Yang | 2ee1790 | 2017-08-30 11:37:08 -0700 | [diff] [blame] | 1582 | DeoptimizationMethodType deopt_method_type = GetDeoptimizationMethodType(method); |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 1583 | self->PushDeoptimizationContext(return_value, |
Mingyao Yang | 2ee1790 | 2017-08-30 11:37:08 -0700 | [diff] [blame] | 1584 | return_shorty == 'L' || return_shorty == '[', |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 1585 | /* exception= */ nullptr , |
| 1586 | /* from_code= */ false, |
Mingyao Yang | 2ee1790 | 2017-08-30 11:37:08 -0700 | [diff] [blame] | 1587 | deopt_method_type); |
Andreas Gampe | d58342c | 2014-06-05 14:18:08 -0700 | [diff] [blame] | 1588 | return GetTwoWordSuccessValue(*return_pc, |
| 1589 | reinterpret_cast<uintptr_t>(GetQuickDeoptimizationEntryPoint())); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1590 | } else { |
Nicolas Geoffray | a0619e2 | 2016-12-20 13:57:43 +0000 | [diff] [blame] | 1591 | if (deoptimize && !Runtime::Current()->IsAsyncDeoptimizeable(*return_pc)) { |
Alex Light | d8eb673 | 2018-01-29 15:16:02 -0800 | [diff] [blame] | 1592 | VLOG(deopt) << "Got a deoptimization request on un-deoptimizable " << method->PrettyMethod() |
| 1593 | << " at PC " << reinterpret_cast<void*>(*return_pc); |
Nicolas Geoffray | a0619e2 | 2016-12-20 13:57:43 +0000 | [diff] [blame] | 1594 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1595 | if (kVerboseInstrumentation) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1596 | LOG(INFO) << "Returning from " << method->PrettyMethod() |
Brian Carlstrom | 2d88862 | 2013-07-18 17:02:00 -0700 | [diff] [blame] | 1597 | << " to PC " << reinterpret_cast<void*>(*return_pc); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1598 | } |
Andreas Gampe | d58342c | 2014-06-05 14:18:08 -0700 | [diff] [blame] | 1599 | return GetTwoWordSuccessValue(0, *return_pc); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1600 | } |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 1601 | } |
| 1602 | |
Alex Light | 2c8206f | 2018-06-08 14:51:09 -0700 | [diff] [blame] | 1603 | uintptr_t Instrumentation::PopFramesForDeoptimization(Thread* self, size_t nframes) const { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1604 | std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack(); |
Alex Light | 2c8206f | 2018-06-08 14:51:09 -0700 | [diff] [blame] | 1605 | CHECK_GE(stack->size(), nframes); |
| 1606 | if (nframes == 0) { |
| 1607 | return 0u; |
| 1608 | } |
| 1609 | // Only need to send instrumentation events if it's not for deopt (do give the log messages if we |
| 1610 | // have verbose-instrumentation anyway though). |
| 1611 | if (kVerboseInstrumentation) { |
| 1612 | for (size_t i = 0; i < nframes; i++) { |
| 1613 | LOG(INFO) << "Popping for deoptimization " << stack->at(i).method_->PrettyMethod(); |
Mingyao Yang | 2ee1790 | 2017-08-30 11:37:08 -0700 | [diff] [blame] | 1614 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1615 | } |
Alex Light | 2c8206f | 2018-06-08 14:51:09 -0700 | [diff] [blame] | 1616 | // Now that we've sent all the instrumentation events we can actually modify the |
| 1617 | // instrumentation-stack. We cannot do this earlier since MethodUnwindEvent can re-enter java and |
| 1618 | // do other things that require the instrumentation stack to be in a consistent state with the |
| 1619 | // actual stack. |
| 1620 | for (size_t i = 0; i < nframes - 1; i++) { |
| 1621 | stack->pop_front(); |
| 1622 | } |
| 1623 | uintptr_t return_pc = stack->front().return_pc_; |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 1624 | stack->pop_front(); |
Alex Light | 2c8206f | 2018-06-08 14:51:09 -0700 | [diff] [blame] | 1625 | return return_pc; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1626 | } |
| 1627 | |
| 1628 | std::string InstrumentationStackFrame::Dump() const { |
| 1629 | std::ostringstream os; |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1630 | os << "Frame " << frame_id_ << " " << ArtMethod::PrettyMethod(method_) << ":" |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1631 | << reinterpret_cast<void*>(return_pc_) << " this=" << reinterpret_cast<void*>(this_object_); |
| 1632 | return os.str(); |
| 1633 | } |
| 1634 | |
| 1635 | } // namespace instrumentation |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 1636 | } // namespace art |