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