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