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