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