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