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" |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 24 | #include "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" |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 28 | #include "dex_file-inl.h" |
Mathieu Chartier | d889178 | 2014-03-02 13:28:37 -0800 | [diff] [blame] | 29 | #include "entrypoints/quick/quick_alloc_entrypoints.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 30 | #include "entrypoints/quick/quick_entrypoints.h" |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 31 | #include "entrypoints/runtime_asm_entrypoints.h" |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 32 | #include "gc_root-inl.h" |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 33 | #include "interpreter/interpreter.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 34 | #include "jit/jit.h" |
| 35 | #include "jit/jit_code_cache.h" |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 36 | #include "jvalue-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 37 | #include "mirror/class-inl.h" |
| 38 | #include "mirror/dex_cache.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 39 | #include "mirror/object-inl.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 40 | #include "mirror/object_array-inl.h" |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 41 | #include "nth_caller_visitor.h" |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 42 | #include "oat_quick_method_header.h" |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 43 | #include "thread.h" |
| 44 | #include "thread_list.h" |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 45 | |
| 46 | namespace art { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 47 | namespace instrumentation { |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 48 | |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 49 | constexpr bool kVerboseInstrumentation = false; |
Sebastien Hertz | 5bfd5c9 | 2013-11-15 11:36:07 +0100 | [diff] [blame] | 50 | |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 51 | void InstrumentationListener::MethodExited(Thread* thread, |
| 52 | Handle<mirror::Object> this_object, |
| 53 | ArtMethod* method, |
| 54 | uint32_t dex_pc, |
| 55 | Handle<mirror::Object> return_value) { |
| 56 | DCHECK_EQ(method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetReturnTypePrimitive(), |
| 57 | Primitive::kPrimNot); |
| 58 | JValue v; |
| 59 | v.SetL(return_value.Get()); |
| 60 | MethodExited(thread, this_object, method, dex_pc, v); |
| 61 | } |
| 62 | |
| 63 | void InstrumentationListener::FieldWritten(Thread* thread, |
| 64 | Handle<mirror::Object> this_object, |
| 65 | ArtMethod* method, |
| 66 | uint32_t dex_pc, |
| 67 | ArtField* field, |
| 68 | Handle<mirror::Object> field_value) { |
| 69 | DCHECK(!field->IsPrimitiveType()); |
| 70 | JValue v; |
| 71 | v.SetL(field_value.Get()); |
| 72 | FieldWritten(thread, this_object, method, dex_pc, field, v); |
| 73 | } |
| 74 | |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 75 | // Instrumentation works on non-inlined frames by updating returned PCs |
| 76 | // of compiled frames. |
| 77 | static constexpr StackVisitor::StackWalkKind kInstrumentationStackWalk = |
| 78 | StackVisitor::StackWalkKind::kSkipInlinedFrames; |
| 79 | |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 80 | class InstallStubsClassVisitor : public ClassVisitor { |
| 81 | public: |
| 82 | explicit InstallStubsClassVisitor(Instrumentation* instrumentation) |
| 83 | : instrumentation_(instrumentation) {} |
| 84 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 85 | bool operator()(ObjPtr<mirror::Class> klass) OVERRIDE REQUIRES(Locks::mutator_lock_) { |
| 86 | instrumentation_->InstallStubsForClass(klass.Ptr()); |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 87 | return true; // we visit all classes. |
| 88 | } |
| 89 | |
| 90 | private: |
| 91 | Instrumentation* const instrumentation_; |
| 92 | }; |
| 93 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 94 | |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 95 | Instrumentation::Instrumentation() |
Nicolas Geoffray | 5a23d2e | 2015-11-03 18:58:57 +0000 | [diff] [blame] | 96 | : instrumentation_stubs_installed_(false), |
| 97 | entry_exit_stubs_installed_(false), |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 98 | interpreter_stubs_installed_(false), |
Nicolas Geoffray | 5a23d2e | 2015-11-03 18:58:57 +0000 | [diff] [blame] | 99 | interpret_only_(false), |
| 100 | forced_interpret_only_(false), |
| 101 | have_method_entry_listeners_(false), |
| 102 | have_method_exit_listeners_(false), |
| 103 | have_method_unwind_listeners_(false), |
| 104 | have_dex_pc_listeners_(false), |
| 105 | have_field_read_listeners_(false), |
| 106 | have_field_write_listeners_(false), |
Alex Light | 6e1607e | 2017-08-23 10:06:18 -0700 | [diff] [blame] | 107 | have_exception_thrown_listeners_(false), |
Alex Light | e814f9d | 2017-07-31 16:14:39 -0700 | [diff] [blame] | 108 | have_watched_frame_pop_listeners_(false), |
Nicolas Geoffray | 81f0f95 | 2016-01-20 16:25:19 +0000 | [diff] [blame] | 109 | have_branch_listeners_(false), |
Nicolas Geoffray | 5a23d2e | 2015-11-03 18:58:57 +0000 | [diff] [blame] | 110 | have_invoke_virtual_or_interface_listeners_(false), |
Mathieu Chartier | b8aa1e4 | 2016-04-05 14:36:57 -0700 | [diff] [blame] | 111 | deoptimized_methods_lock_("deoptimized methods lock", kDeoptimizedMethodsLock), |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 112 | deoptimization_enabled_(false), |
| 113 | interpreter_handler_table_(kMainHandlerTable), |
Mathieu Chartier | 50e9331 | 2016-03-16 11:25:29 -0700 | [diff] [blame] | 114 | quick_alloc_entry_points_instrumentation_counter_(0), |
| 115 | alloc_entrypoints_instrumented_(false) { |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Sebastien Hertz | a10aa37 | 2015-01-21 17:30:58 +0100 | [diff] [blame] | 118 | void Instrumentation::InstallStubsForClass(mirror::Class* klass) { |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 119 | if (!klass->IsResolved()) { |
Sebastien Hertz | a8a697f | 2015-01-15 12:28:47 +0100 | [diff] [blame] | 120 | // We need the class to be resolved to install/uninstall stubs. Otherwise its methods |
| 121 | // could not be initialized or linked with regards to class inheritance. |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 122 | } else if (klass->IsErroneousResolved()) { |
| 123 | // We can't execute code in a erroneous class: do nothing. |
Sebastien Hertz | a8a697f | 2015-01-15 12:28:47 +0100 | [diff] [blame] | 124 | } else { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 125 | for (ArtMethod& method : klass->GetMethods(kRuntimePointerSize)) { |
Alex Light | 51a64d5 | 2015-12-17 13:55:59 -0800 | [diff] [blame] | 126 | InstallStubsForMethod(&method); |
Sebastien Hertz | a8a697f | 2015-01-15 12:28:47 +0100 | [diff] [blame] | 127 | } |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 128 | } |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 129 | } |
| 130 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 131 | static void UpdateEntrypoints(ArtMethod* method, const void* quick_code) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 132 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 133 | method->SetEntryPointFromQuickCompiledCode(quick_code); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 134 | } |
| 135 | |
Nicolas Geoffray | a0619e2 | 2016-12-20 13:57:43 +0000 | [diff] [blame] | 136 | bool Instrumentation::NeedDebugVersionFor(ArtMethod* method) const REQUIRES_SHARED(Locks::mutator_lock_) { |
Mingyao Yang | 6ea1a0e | 2016-01-29 12:12:49 -0800 | [diff] [blame] | 137 | return Dbg::IsDebuggerActive() && |
Nicolas Geoffray | a0619e2 | 2016-12-20 13:57:43 +0000 | [diff] [blame] | 138 | Runtime::Current()->IsJavaDebuggable() && |
Mingyao Yang | 6ea1a0e | 2016-01-29 12:12:49 -0800 | [diff] [blame] | 139 | !method->IsNative() && |
| 140 | !method->IsProxyMethod(); |
| 141 | } |
| 142 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 143 | void Instrumentation::InstallStubsForMethod(ArtMethod* method) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 144 | if (!method->IsInvokable() || method->IsProxyMethod()) { |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 145 | // Do not change stubs for these methods. |
| 146 | return; |
| 147 | } |
Jeff Hao | 5680277 | 2014-08-19 10:17:36 -0700 | [diff] [blame] | 148 | // Don't stub Proxy.<init>. Note that the Proxy class itself is not a proxy class. |
| 149 | if (method->IsConstructor() && |
| 150 | method->GetDeclaringClass()->DescriptorEquals("Ljava/lang/reflect/Proxy;")) { |
Jeff Hao | db8a664 | 2014-08-14 17:18:52 -0700 | [diff] [blame] | 151 | return; |
| 152 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 153 | const void* new_quick_code; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 154 | bool uninstall = !entry_exit_stubs_installed_ && !interpreter_stubs_installed_; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 155 | Runtime* const runtime = Runtime::Current(); |
| 156 | ClassLinker* const class_linker = runtime->GetClassLinker(); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 157 | bool is_class_initialized = method->GetDeclaringClass()->IsInitialized(); |
| 158 | if (uninstall) { |
| 159 | if ((forced_interpret_only_ || IsDeoptimized(method)) && !method->IsNative()) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 160 | new_quick_code = GetQuickToInterpreterBridge(); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 161 | } else if (is_class_initialized || !method->IsStatic() || method->IsConstructor()) { |
Nicolas Geoffray | a0619e2 | 2016-12-20 13:57:43 +0000 | [diff] [blame] | 162 | if (NeedDebugVersionFor(method)) { |
Mingyao Yang | 6ea1a0e | 2016-01-29 12:12:49 -0800 | [diff] [blame] | 163 | new_quick_code = GetQuickToInterpreterBridge(); |
Nicolas Geoffray | a0619e2 | 2016-12-20 13:57:43 +0000 | [diff] [blame] | 164 | } else { |
| 165 | new_quick_code = class_linker->GetQuickOatCodeFor(method); |
Mingyao Yang | 6ea1a0e | 2016-01-29 12:12:49 -0800 | [diff] [blame] | 166 | } |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 167 | } else { |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 168 | new_quick_code = GetQuickResolutionStub(); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 169 | } |
| 170 | } else { // !uninstall |
Sebastien Hertz | bae182c | 2013-12-17 10:42:03 +0100 | [diff] [blame] | 171 | if ((interpreter_stubs_installed_ || forced_interpret_only_ || IsDeoptimized(method)) && |
| 172 | !method->IsNative()) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 173 | new_quick_code = GetQuickToInterpreterBridge(); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 174 | } else { |
| 175 | // Do not overwrite resolution trampoline. When the trampoline initializes the method's |
| 176 | // class, all its static methods code will be set to the instrumentation entry point. |
| 177 | // For more details, see ClassLinker::FixupStaticTrampolines. |
| 178 | if (is_class_initialized || !method->IsStatic() || method->IsConstructor()) { |
Nicolas Geoffray | a0619e2 | 2016-12-20 13:57:43 +0000 | [diff] [blame] | 179 | if (NeedDebugVersionFor(method)) { |
Mingyao Yang | 6ea1a0e | 2016-01-29 12:12:49 -0800 | [diff] [blame] | 180 | // Oat code should not be used. Don't install instrumentation stub and |
| 181 | // use interpreter for instrumentation. |
| 182 | new_quick_code = GetQuickToInterpreterBridge(); |
| 183 | } else if (entry_exit_stubs_installed_) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 184 | new_quick_code = GetQuickInstrumentationEntryPoint(); |
Nicolas Geoffray | a0619e2 | 2016-12-20 13:57:43 +0000 | [diff] [blame] | 185 | } else { |
| 186 | new_quick_code = class_linker->GetQuickOatCodeFor(method); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 187 | } |
| 188 | } else { |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 189 | new_quick_code = GetQuickResolutionStub(); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | } |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 193 | UpdateEntrypoints(method, new_quick_code); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 194 | } |
| 195 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 196 | // Places the instrumentation exit pc as the return PC for every quick frame. This also allows |
| 197 | // deoptimization of quick frames to interpreter frames. |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 198 | // Since we may already have done this previously, we need to push new instrumentation frame before |
| 199 | // existing instrumentation frames. |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 200 | static void InstrumentationInstallStack(Thread* thread, void* arg) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 201 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 202 | struct InstallStackVisitor FINAL : public StackVisitor { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 203 | InstallStackVisitor(Thread* thread_in, Context* context, uintptr_t instrumentation_exit_pc) |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 204 | : StackVisitor(thread_in, context, kInstrumentationStackWalk), |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 205 | instrumentation_stack_(thread_in->GetInstrumentationStack()), |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 206 | instrumentation_exit_pc_(instrumentation_exit_pc), |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 207 | reached_existing_instrumentation_frames_(false), instrumentation_stack_depth_(0), |
| 208 | last_return_pc_(0) { |
| 209 | } |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 210 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 211 | bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 212 | ArtMethod* m = GetMethod(); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 213 | if (m == nullptr) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 214 | if (kVerboseInstrumentation) { |
| 215 | LOG(INFO) << " Skipping upcall. Frame " << GetFrameId(); |
| 216 | } |
| 217 | last_return_pc_ = 0; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 218 | return true; // Ignore upcalls. |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 219 | } |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 220 | if (GetCurrentQuickFrame() == nullptr) { |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 221 | bool interpreter_frame = true; |
Sebastien Hertz | 320deb2 | 2014-06-11 19:45:05 +0200 | [diff] [blame] | 222 | InstrumentationStackFrame instrumentation_frame(GetThisObject(), m, 0, GetFrameId(), |
| 223 | interpreter_frame); |
Jeff Hao | a15a81b | 2014-05-27 18:25:47 -0700 | [diff] [blame] | 224 | if (kVerboseInstrumentation) { |
| 225 | LOG(INFO) << "Pushing shadow frame " << instrumentation_frame.Dump(); |
| 226 | } |
| 227 | shadow_stack_.push_back(instrumentation_frame); |
| 228 | return true; // Continue. |
| 229 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 230 | uintptr_t return_pc = GetReturnPc(); |
Nicolas Geoffray | 07c7028 | 2017-08-30 08:09:42 +0000 | [diff] [blame] | 231 | if (m->IsRuntimeMethod()) { |
| 232 | if (return_pc == instrumentation_exit_pc_) { |
| 233 | if (kVerboseInstrumentation) { |
| 234 | LOG(INFO) << " Handling quick to interpreter transition. Frame " << GetFrameId(); |
| 235 | } |
| 236 | CHECK_LT(instrumentation_stack_depth_, instrumentation_stack_->size()); |
| 237 | const InstrumentationStackFrame& frame = |
| 238 | instrumentation_stack_->at(instrumentation_stack_depth_); |
| 239 | CHECK(frame.interpreter_entry_); |
| 240 | // This is an interpreter frame so method enter event must have been reported. However we |
| 241 | // need to push a DEX pc into the dex_pcs_ list to match size of instrumentation stack. |
| 242 | // Since we won't report method entry here, we can safely push any DEX pc. |
| 243 | dex_pcs_.push_back(0); |
| 244 | last_return_pc_ = frame.return_pc_; |
| 245 | ++instrumentation_stack_depth_; |
| 246 | return true; |
| 247 | } else { |
| 248 | if (kVerboseInstrumentation) { |
| 249 | LOG(INFO) << " Skipping runtime method. Frame " << GetFrameId(); |
| 250 | } |
| 251 | last_return_pc_ = GetReturnPc(); |
| 252 | return true; // Ignore unresolved methods since they will be instrumented after resolution. |
| 253 | } |
| 254 | } |
Sebastien Hertz | 320deb2 | 2014-06-11 19:45:05 +0200 | [diff] [blame] | 255 | if (kVerboseInstrumentation) { |
| 256 | LOG(INFO) << " Installing exit stub in " << DescribeLocation(); |
| 257 | } |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 258 | if (return_pc == instrumentation_exit_pc_) { |
| 259 | // We've reached a frame which has already been installed with instrumentation exit stub. |
| 260 | // We should have already installed instrumentation on previous frames. |
| 261 | reached_existing_instrumentation_frames_ = true; |
| 262 | |
Nicolas Geoffray | 07c7028 | 2017-08-30 08:09:42 +0000 | [diff] [blame] | 263 | CHECK_LT(instrumentation_stack_depth_, instrumentation_stack_->size()); |
Daniel Mihalyi | ca1d06c | 2014-08-18 18:45:31 +0200 | [diff] [blame] | 264 | const InstrumentationStackFrame& frame = |
| 265 | instrumentation_stack_->at(instrumentation_stack_depth_); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 266 | CHECK_EQ(m, frame.method_) << "Expected " << ArtMethod::PrettyMethod(m) |
| 267 | << ", Found " << ArtMethod::PrettyMethod(frame.method_); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 268 | return_pc = frame.return_pc_; |
| 269 | if (kVerboseInstrumentation) { |
| 270 | LOG(INFO) << "Ignoring already instrumented " << frame.Dump(); |
| 271 | } |
| 272 | } else { |
| 273 | CHECK_NE(return_pc, 0U); |
| 274 | CHECK(!reached_existing_instrumentation_frames_); |
Nicolas Geoffray | 07c7028 | 2017-08-30 08:09:42 +0000 | [diff] [blame] | 275 | InstrumentationStackFrame instrumentation_frame(GetThisObject(), m, return_pc, GetFrameId(), |
| 276 | false); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 277 | if (kVerboseInstrumentation) { |
| 278 | LOG(INFO) << "Pushing frame " << instrumentation_frame.Dump(); |
| 279 | } |
| 280 | |
Sebastien Hertz | 320deb2 | 2014-06-11 19:45:05 +0200 | [diff] [blame] | 281 | // Insert frame at the right position so we do not corrupt the instrumentation stack. |
| 282 | // Instrumentation stack frames are in descending frame id order. |
| 283 | auto it = instrumentation_stack_->begin(); |
| 284 | for (auto end = instrumentation_stack_->end(); it != end; ++it) { |
| 285 | const InstrumentationStackFrame& current = *it; |
| 286 | if (instrumentation_frame.frame_id_ >= current.frame_id_) { |
| 287 | break; |
| 288 | } |
| 289 | } |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 290 | instrumentation_stack_->insert(it, instrumentation_frame); |
| 291 | SetReturnPc(instrumentation_exit_pc_); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 292 | } |
Nicolas Geoffray | 07c7028 | 2017-08-30 08:09:42 +0000 | [diff] [blame] | 293 | dex_pcs_.push_back((GetCurrentOatQuickMethodHeader() == nullptr) |
| 294 | ? DexFile::kDexNoIndex |
| 295 | : GetCurrentOatQuickMethodHeader()->ToDexPc(m, last_return_pc_)); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 296 | last_return_pc_ = return_pc; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 297 | ++instrumentation_stack_depth_; |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 298 | return true; // Continue. |
| 299 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 300 | std::deque<InstrumentationStackFrame>* const instrumentation_stack_; |
Jeff Hao | a15a81b | 2014-05-27 18:25:47 -0700 | [diff] [blame] | 301 | std::vector<InstrumentationStackFrame> shadow_stack_; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 302 | std::vector<uint32_t> dex_pcs_; |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 303 | const uintptr_t instrumentation_exit_pc_; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 304 | bool reached_existing_instrumentation_frames_; |
| 305 | size_t instrumentation_stack_depth_; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 306 | uintptr_t last_return_pc_; |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 307 | }; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 308 | if (kVerboseInstrumentation) { |
| 309 | std::string thread_name; |
| 310 | thread->GetThreadName(thread_name); |
| 311 | LOG(INFO) << "Installing exit stubs in " << thread_name; |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 312 | } |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 313 | |
| 314 | Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg); |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 315 | std::unique_ptr<Context> context(Context::Create()); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 316 | uintptr_t instrumentation_exit_pc = reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()); |
Sebastien Hertz | 11d40c2 | 2014-02-19 18:00:17 +0100 | [diff] [blame] | 317 | InstallStackVisitor visitor(thread, context.get(), instrumentation_exit_pc); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 318 | visitor.WalkStack(true); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 319 | CHECK_EQ(visitor.dex_pcs_.size(), thread->GetInstrumentationStack()->size()); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 320 | |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 321 | if (instrumentation->ShouldNotifyMethodEnterExitEvents()) { |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 322 | // Create method enter events for all methods currently on the thread's stack. We only do this |
| 323 | // if no debugger is attached to prevent from posting events twice. |
Jeff Hao | a15a81b | 2014-05-27 18:25:47 -0700 | [diff] [blame] | 324 | auto ssi = visitor.shadow_stack_.rbegin(); |
| 325 | for (auto isi = thread->GetInstrumentationStack()->rbegin(), |
| 326 | end = thread->GetInstrumentationStack()->rend(); isi != end; ++isi) { |
| 327 | while (ssi != visitor.shadow_stack_.rend() && (*ssi).frame_id_ < (*isi).frame_id_) { |
| 328 | instrumentation->MethodEnterEvent(thread, (*ssi).this_object_, (*ssi).method_, 0); |
| 329 | ++ssi; |
| 330 | } |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 331 | uint32_t dex_pc = visitor.dex_pcs_.back(); |
| 332 | visitor.dex_pcs_.pop_back(); |
Sebastien Hertz | 320deb2 | 2014-06-11 19:45:05 +0200 | [diff] [blame] | 333 | if (!isi->interpreter_entry_) { |
| 334 | instrumentation->MethodEnterEvent(thread, (*isi).this_object_, (*isi).method_, dex_pc); |
| 335 | } |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 336 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 337 | } |
| 338 | thread->VerifyStack(); |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 339 | } |
| 340 | |
Mingyao Yang | 99170c6 | 2015-07-06 11:10:37 -0700 | [diff] [blame] | 341 | void Instrumentation::InstrumentThreadStack(Thread* thread) { |
| 342 | instrumentation_stubs_installed_ = true; |
| 343 | InstrumentationInstallStack(thread, this); |
| 344 | } |
| 345 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 346 | // Removes the instrumentation exit pc as the return PC for every quick frame. |
| 347 | static void InstrumentationRestoreStack(Thread* thread, void* arg) |
Nicolas Geoffray | 5a23d2e | 2015-11-03 18:58:57 +0000 | [diff] [blame] | 348 | REQUIRES(Locks::mutator_lock_) { |
| 349 | Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current()); |
| 350 | |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 351 | struct RestoreStackVisitor FINAL : public StackVisitor { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 352 | RestoreStackVisitor(Thread* thread_in, uintptr_t instrumentation_exit_pc, |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 353 | Instrumentation* instrumentation) |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 354 | : StackVisitor(thread_in, nullptr, kInstrumentationStackWalk), |
| 355 | thread_(thread_in), |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 356 | instrumentation_exit_pc_(instrumentation_exit_pc), |
| 357 | instrumentation_(instrumentation), |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 358 | instrumentation_stack_(thread_in->GetInstrumentationStack()), |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 359 | frames_removed_(0) {} |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 360 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 361 | bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 362 | if (instrumentation_stack_->size() == 0) { |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 363 | return false; // Stop. |
| 364 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 365 | ArtMethod* m = GetMethod(); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 366 | if (GetCurrentQuickFrame() == nullptr) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 367 | if (kVerboseInstrumentation) { |
Daniel Mihalyi | ca1d06c | 2014-08-18 18:45:31 +0200 | [diff] [blame] | 368 | LOG(INFO) << " Ignoring a shadow frame. Frame " << GetFrameId() |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 369 | << " Method=" << ArtMethod::PrettyMethod(m); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 370 | } |
| 371 | return true; // Ignore shadow frames. |
| 372 | } |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 373 | if (m == nullptr) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 374 | if (kVerboseInstrumentation) { |
| 375 | LOG(INFO) << " Skipping upcall. Frame " << GetFrameId(); |
| 376 | } |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 377 | return true; // Ignore upcalls. |
| 378 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 379 | bool removed_stub = false; |
| 380 | // TODO: make this search more efficient? |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 381 | const size_t frameId = GetFrameId(); |
| 382 | for (const InstrumentationStackFrame& instrumentation_frame : *instrumentation_stack_) { |
| 383 | if (instrumentation_frame.frame_id_ == frameId) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 384 | if (kVerboseInstrumentation) { |
| 385 | LOG(INFO) << " Removing exit stub in " << DescribeLocation(); |
| 386 | } |
Jeff Hao | 9a916d3 | 2013-06-27 18:45:37 -0700 | [diff] [blame] | 387 | if (instrumentation_frame.interpreter_entry_) { |
Andreas Gampe | 8228cdf | 2017-05-30 15:03:54 -0700 | [diff] [blame] | 388 | CHECK(m == Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs)); |
Jeff Hao | 9a916d3 | 2013-06-27 18:45:37 -0700 | [diff] [blame] | 389 | } else { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 390 | CHECK(m == instrumentation_frame.method_) << ArtMethod::PrettyMethod(m); |
Jeff Hao | 9a916d3 | 2013-06-27 18:45:37 -0700 | [diff] [blame] | 391 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 392 | SetReturnPc(instrumentation_frame.return_pc_); |
Nicolas Geoffray | 07c7028 | 2017-08-30 08:09:42 +0000 | [diff] [blame] | 393 | if (instrumentation_->ShouldNotifyMethodEnterExitEvents()) { |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 394 | // Create the method exit events. As the methods didn't really exit the result is 0. |
| 395 | // We only do this if no debugger is attached to prevent from posting events twice. |
| 396 | instrumentation_->MethodExitEvent(thread_, instrumentation_frame.this_object_, m, |
| 397 | GetDexPc(), JValue()); |
| 398 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 399 | frames_removed_++; |
| 400 | removed_stub = true; |
| 401 | break; |
| 402 | } |
| 403 | } |
| 404 | if (!removed_stub) { |
| 405 | if (kVerboseInstrumentation) { |
| 406 | LOG(INFO) << " No exit stub in " << DescribeLocation(); |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 407 | } |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 408 | } |
| 409 | return true; // Continue. |
| 410 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 411 | Thread* const thread_; |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 412 | const uintptr_t instrumentation_exit_pc_; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 413 | Instrumentation* const instrumentation_; |
| 414 | std::deque<instrumentation::InstrumentationStackFrame>* const instrumentation_stack_; |
| 415 | size_t frames_removed_; |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 416 | }; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 417 | if (kVerboseInstrumentation) { |
| 418 | std::string thread_name; |
| 419 | thread->GetThreadName(thread_name); |
| 420 | LOG(INFO) << "Removing exit stubs in " << thread_name; |
| 421 | } |
| 422 | std::deque<instrumentation::InstrumentationStackFrame>* stack = thread->GetInstrumentationStack(); |
| 423 | if (stack->size() > 0) { |
| 424 | Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 425 | uintptr_t instrumentation_exit_pc = |
| 426 | reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 427 | RestoreStackVisitor visitor(thread, instrumentation_exit_pc, instrumentation); |
| 428 | visitor.WalkStack(true); |
| 429 | CHECK_EQ(visitor.frames_removed_, stack->size()); |
| 430 | while (stack->size() > 0) { |
| 431 | stack->pop_front(); |
| 432 | } |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 433 | } |
| 434 | } |
| 435 | |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 436 | static bool HasEvent(Instrumentation::InstrumentationEvent expected, uint32_t events) { |
| 437 | return (events & expected) != 0; |
| 438 | } |
| 439 | |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 440 | static void PotentiallyAddListenerTo(Instrumentation::InstrumentationEvent event, |
| 441 | uint32_t events, |
| 442 | std::list<InstrumentationListener*>& list, |
| 443 | InstrumentationListener* listener, |
| 444 | bool* has_listener) |
| 445 | REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_) { |
| 446 | Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current()); |
| 447 | if (!HasEvent(event, events)) { |
| 448 | return; |
| 449 | } |
| 450 | // If there is a free slot in the list, we insert the listener in that slot. |
| 451 | // Otherwise we add it to the end of the list. |
| 452 | auto it = std::find(list.begin(), list.end(), nullptr); |
| 453 | if (it != list.end()) { |
| 454 | *it = listener; |
| 455 | } else { |
| 456 | list.push_back(listener); |
| 457 | } |
| 458 | *has_listener = true; |
| 459 | } |
| 460 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 461 | void Instrumentation::AddListener(InstrumentationListener* listener, uint32_t events) { |
| 462 | Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current()); |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 463 | PotentiallyAddListenerTo(kMethodEntered, |
| 464 | events, |
| 465 | method_entry_listeners_, |
| 466 | listener, |
| 467 | &have_method_entry_listeners_); |
| 468 | PotentiallyAddListenerTo(kMethodExited, |
| 469 | events, |
| 470 | method_exit_listeners_, |
| 471 | listener, |
| 472 | &have_method_exit_listeners_); |
| 473 | PotentiallyAddListenerTo(kMethodUnwind, |
| 474 | events, |
| 475 | method_unwind_listeners_, |
| 476 | listener, |
| 477 | &have_method_unwind_listeners_); |
Nicolas Geoffray | 81f0f95 | 2016-01-20 16:25:19 +0000 | [diff] [blame] | 478 | PotentiallyAddListenerTo(kBranch, |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 479 | events, |
Nicolas Geoffray | 81f0f95 | 2016-01-20 16:25:19 +0000 | [diff] [blame] | 480 | branch_listeners_, |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 481 | listener, |
Nicolas Geoffray | 81f0f95 | 2016-01-20 16:25:19 +0000 | [diff] [blame] | 482 | &have_branch_listeners_); |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 483 | PotentiallyAddListenerTo(kInvokeVirtualOrInterface, |
| 484 | events, |
| 485 | invoke_virtual_or_interface_listeners_, |
| 486 | listener, |
| 487 | &have_invoke_virtual_or_interface_listeners_); |
| 488 | PotentiallyAddListenerTo(kDexPcMoved, |
| 489 | events, |
| 490 | dex_pc_listeners_, |
| 491 | listener, |
| 492 | &have_dex_pc_listeners_); |
| 493 | PotentiallyAddListenerTo(kFieldRead, |
| 494 | events, |
| 495 | field_read_listeners_, |
| 496 | listener, |
| 497 | &have_field_read_listeners_); |
| 498 | PotentiallyAddListenerTo(kFieldWritten, |
| 499 | events, |
| 500 | field_write_listeners_, |
| 501 | listener, |
| 502 | &have_field_write_listeners_); |
Alex Light | 6e1607e | 2017-08-23 10:06:18 -0700 | [diff] [blame] | 503 | PotentiallyAddListenerTo(kExceptionThrown, |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 504 | events, |
Alex Light | 6e1607e | 2017-08-23 10:06:18 -0700 | [diff] [blame] | 505 | exception_thrown_listeners_, |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 506 | listener, |
Alex Light | 6e1607e | 2017-08-23 10:06:18 -0700 | [diff] [blame] | 507 | &have_exception_thrown_listeners_); |
Alex Light | e814f9d | 2017-07-31 16:14:39 -0700 | [diff] [blame] | 508 | PotentiallyAddListenerTo(kWatchedFramePop, |
| 509 | events, |
| 510 | watched_frame_pop_listeners_, |
| 511 | listener, |
| 512 | &have_watched_frame_pop_listeners_); |
Sebastien Hertz | ee1997a | 2013-09-19 14:47:09 +0200 | [diff] [blame] | 513 | UpdateInterpreterHandlerTable(); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 514 | } |
| 515 | |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 516 | static void PotentiallyRemoveListenerFrom(Instrumentation::InstrumentationEvent event, |
| 517 | uint32_t events, |
| 518 | std::list<InstrumentationListener*>& list, |
| 519 | InstrumentationListener* listener, |
| 520 | bool* has_listener) |
| 521 | REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_) { |
| 522 | Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current()); |
| 523 | if (!HasEvent(event, events)) { |
| 524 | return; |
| 525 | } |
| 526 | auto it = std::find(list.begin(), list.end(), listener); |
| 527 | if (it != list.end()) { |
| 528 | // Just update the entry, do not remove from the list. Removing entries in the list |
| 529 | // is unsafe when mutators are iterating over it. |
| 530 | *it = nullptr; |
| 531 | } |
| 532 | |
| 533 | // Check if the list contains any non-null listener, and update 'has_listener'. |
| 534 | for (InstrumentationListener* l : list) { |
| 535 | if (l != nullptr) { |
| 536 | *has_listener = true; |
| 537 | return; |
| 538 | } |
| 539 | } |
| 540 | *has_listener = false; |
| 541 | } |
| 542 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 543 | void Instrumentation::RemoveListener(InstrumentationListener* listener, uint32_t events) { |
| 544 | Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current()); |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 545 | PotentiallyRemoveListenerFrom(kMethodEntered, |
| 546 | events, |
| 547 | method_entry_listeners_, |
| 548 | listener, |
| 549 | &have_method_entry_listeners_); |
| 550 | PotentiallyRemoveListenerFrom(kMethodExited, |
| 551 | events, |
| 552 | method_exit_listeners_, |
| 553 | listener, |
| 554 | &have_method_exit_listeners_); |
| 555 | PotentiallyRemoveListenerFrom(kMethodUnwind, |
| 556 | events, |
| 557 | method_unwind_listeners_, |
| 558 | listener, |
| 559 | &have_method_unwind_listeners_); |
Nicolas Geoffray | 81f0f95 | 2016-01-20 16:25:19 +0000 | [diff] [blame] | 560 | PotentiallyRemoveListenerFrom(kBranch, |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 561 | events, |
Nicolas Geoffray | 81f0f95 | 2016-01-20 16:25:19 +0000 | [diff] [blame] | 562 | branch_listeners_, |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 563 | listener, |
Nicolas Geoffray | 81f0f95 | 2016-01-20 16:25:19 +0000 | [diff] [blame] | 564 | &have_branch_listeners_); |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 565 | PotentiallyRemoveListenerFrom(kInvokeVirtualOrInterface, |
| 566 | events, |
| 567 | invoke_virtual_or_interface_listeners_, |
| 568 | listener, |
| 569 | &have_invoke_virtual_or_interface_listeners_); |
| 570 | PotentiallyRemoveListenerFrom(kDexPcMoved, |
| 571 | events, |
| 572 | dex_pc_listeners_, |
| 573 | listener, |
| 574 | &have_dex_pc_listeners_); |
| 575 | PotentiallyRemoveListenerFrom(kFieldRead, |
| 576 | events, |
| 577 | field_read_listeners_, |
| 578 | listener, |
| 579 | &have_field_read_listeners_); |
| 580 | PotentiallyRemoveListenerFrom(kFieldWritten, |
| 581 | events, |
| 582 | field_write_listeners_, |
| 583 | listener, |
| 584 | &have_field_write_listeners_); |
Alex Light | 6e1607e | 2017-08-23 10:06:18 -0700 | [diff] [blame] | 585 | PotentiallyRemoveListenerFrom(kExceptionThrown, |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 586 | events, |
Alex Light | 6e1607e | 2017-08-23 10:06:18 -0700 | [diff] [blame] | 587 | exception_thrown_listeners_, |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 588 | listener, |
Alex Light | 6e1607e | 2017-08-23 10:06:18 -0700 | [diff] [blame] | 589 | &have_exception_thrown_listeners_); |
Alex Light | e814f9d | 2017-07-31 16:14:39 -0700 | [diff] [blame] | 590 | PotentiallyRemoveListenerFrom(kWatchedFramePop, |
| 591 | events, |
| 592 | watched_frame_pop_listeners_, |
| 593 | listener, |
| 594 | &have_watched_frame_pop_listeners_); |
Sebastien Hertz | ee1997a | 2013-09-19 14:47:09 +0200 | [diff] [blame] | 595 | UpdateInterpreterHandlerTable(); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 596 | } |
| 597 | |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 598 | Instrumentation::InstrumentationLevel Instrumentation::GetCurrentInstrumentationLevel() const { |
Alex Light | 4ba388a | 2017-01-27 10:26:49 -0800 | [diff] [blame] | 599 | if (interpreter_stubs_installed_) { |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 600 | return InstrumentationLevel::kInstrumentWithInterpreter; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 601 | } else if (entry_exit_stubs_installed_) { |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 602 | return InstrumentationLevel::kInstrumentWithInstrumentationStubs; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 603 | } else { |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 604 | return InstrumentationLevel::kInstrumentNothing; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 605 | } |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 606 | } |
| 607 | |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 608 | bool Instrumentation::RequiresInstrumentationInstallation(InstrumentationLevel new_level) const { |
Alex Light | 4ba388a | 2017-01-27 10:26:49 -0800 | [diff] [blame] | 609 | // We need to reinstall instrumentation if we go to a different level. |
| 610 | return GetCurrentInstrumentationLevel() != new_level; |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 611 | } |
| 612 | |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 613 | void Instrumentation::ConfigureStubs(const char* key, InstrumentationLevel desired_level) { |
| 614 | // Store the instrumentation level for this key or remove it. |
| 615 | if (desired_level == InstrumentationLevel::kInstrumentNothing) { |
| 616 | // The client no longer needs instrumentation. |
| 617 | requested_instrumentation_levels_.erase(key); |
| 618 | } else { |
| 619 | // The client needs instrumentation. |
| 620 | requested_instrumentation_levels_.Overwrite(key, desired_level); |
| 621 | } |
| 622 | |
| 623 | // Look for the highest required instrumentation level. |
| 624 | InstrumentationLevel requested_level = InstrumentationLevel::kInstrumentNothing; |
| 625 | for (const auto& v : requested_instrumentation_levels_) { |
| 626 | requested_level = std::max(requested_level, v.second); |
| 627 | } |
| 628 | |
| 629 | interpret_only_ = (requested_level == InstrumentationLevel::kInstrumentWithInterpreter) || |
| 630 | forced_interpret_only_; |
| 631 | |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 632 | if (!RequiresInstrumentationInstallation(requested_level)) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 633 | // We're already set. |
| 634 | return; |
| 635 | } |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 636 | Thread* const self = Thread::Current(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 637 | Runtime* runtime = Runtime::Current(); |
Sebastien Hertz | a8a697f | 2015-01-15 12:28:47 +0100 | [diff] [blame] | 638 | Locks::mutator_lock_->AssertExclusiveHeld(self); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 639 | Locks::thread_list_lock_->AssertNotHeld(self); |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 640 | if (requested_level > InstrumentationLevel::kInstrumentNothing) { |
Alex Light | 4ba388a | 2017-01-27 10:26:49 -0800 | [diff] [blame] | 641 | if (requested_level == InstrumentationLevel::kInstrumentWithInterpreter) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 642 | interpreter_stubs_installed_ = true; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 643 | entry_exit_stubs_installed_ = true; |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 644 | } else { |
| 645 | CHECK_EQ(requested_level, InstrumentationLevel::kInstrumentWithInstrumentationStubs); |
| 646 | entry_exit_stubs_installed_ = true; |
| 647 | interpreter_stubs_installed_ = false; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 648 | } |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 649 | InstallStubsClassVisitor visitor(this); |
| 650 | runtime->GetClassLinker()->VisitClasses(&visitor); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 651 | instrumentation_stubs_installed_ = true; |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 652 | MutexLock mu(self, *Locks::thread_list_lock_); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 653 | runtime->GetThreadList()->ForEach(InstrumentationInstallStack, this); |
| 654 | } else { |
| 655 | interpreter_stubs_installed_ = false; |
| 656 | entry_exit_stubs_installed_ = false; |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 657 | InstallStubsClassVisitor visitor(this); |
| 658 | runtime->GetClassLinker()->VisitClasses(&visitor); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 659 | // Restore stack only if there is no method currently deoptimized. |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 660 | bool empty; |
| 661 | { |
| 662 | ReaderMutexLock mu(self, deoptimized_methods_lock_); |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 663 | empty = IsDeoptimizedMethodsEmpty(); // Avoid lock violation. |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 664 | } |
| 665 | if (empty) { |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 666 | MutexLock mu(self, *Locks::thread_list_lock_); |
| 667 | Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this); |
Nicolas Geoffray | 5a23d2e | 2015-11-03 18:58:57 +0000 | [diff] [blame] | 668 | // Only do this after restoring, as walking the stack when restoring will see |
| 669 | // the instrumentation exit pc. |
| 670 | instrumentation_stubs_installed_ = false; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 671 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 672 | } |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 673 | } |
| 674 | |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 675 | static void ResetQuickAllocEntryPointsForThread(Thread* thread, void* arg ATTRIBUTE_UNUSED) { |
Mathieu Chartier | 5ace201 | 2016-11-30 10:15:41 -0800 | [diff] [blame] | 676 | thread->ResetQuickAllocEntryPointsForThread(kUseReadBarrier && thread->GetIsGcMarking()); |
Ian Rogers | fa82427 | 2013-11-05 16:12:57 -0800 | [diff] [blame] | 677 | } |
| 678 | |
Mathieu Chartier | 9ef78b5 | 2014-09-25 17:03:12 -0700 | [diff] [blame] | 679 | void Instrumentation::SetEntrypointsInstrumented(bool instrumented) { |
| 680 | Thread* self = Thread::Current(); |
Mathieu Chartier | 661974a | 2014-01-09 11:23:53 -0800 | [diff] [blame] | 681 | Runtime* runtime = Runtime::Current(); |
Mathieu Chartier | 9ef78b5 | 2014-09-25 17:03:12 -0700 | [diff] [blame] | 682 | Locks::mutator_lock_->AssertNotHeld(self); |
| 683 | Locks::instrument_entrypoints_lock_->AssertHeld(self); |
| 684 | if (runtime->IsStarted()) { |
Mathieu Chartier | 4f55e22 | 2015-09-04 13:26:21 -0700 | [diff] [blame] | 685 | ScopedSuspendAll ssa(__FUNCTION__); |
Mathieu Chartier | 9ef78b5 | 2014-09-25 17:03:12 -0700 | [diff] [blame] | 686 | MutexLock mu(self, *Locks::runtime_shutdown_lock_); |
Mathieu Chartier | 661974a | 2014-01-09 11:23:53 -0800 | [diff] [blame] | 687 | SetQuickAllocEntryPointsInstrumented(instrumented); |
| 688 | ResetQuickAllocEntryPoints(); |
Mathieu Chartier | 50e9331 | 2016-03-16 11:25:29 -0700 | [diff] [blame] | 689 | alloc_entrypoints_instrumented_ = instrumented; |
Mathieu Chartier | 4f55e22 | 2015-09-04 13:26:21 -0700 | [diff] [blame] | 690 | } else { |
| 691 | MutexLock mu(self, *Locks::runtime_shutdown_lock_); |
| 692 | SetQuickAllocEntryPointsInstrumented(instrumented); |
Andreas Gampe | 157c77e | 2016-10-17 17:44:41 -0700 | [diff] [blame] | 693 | |
| 694 | // Note: ResetQuickAllocEntryPoints only works when the runtime is started. Manually run the |
| 695 | // update for just this thread. |
Andreas Gampe | 162ae50 | 2016-10-18 10:03:42 -0700 | [diff] [blame] | 696 | // Note: self may be null. One of those paths is setting instrumentation in the Heap |
| 697 | // constructor for gcstress mode. |
| 698 | if (self != nullptr) { |
| 699 | ResetQuickAllocEntryPointsForThread(self, nullptr); |
| 700 | } |
Andreas Gampe | 157c77e | 2016-10-17 17:44:41 -0700 | [diff] [blame] | 701 | |
Mathieu Chartier | 50e9331 | 2016-03-16 11:25:29 -0700 | [diff] [blame] | 702 | alloc_entrypoints_instrumented_ = instrumented; |
Mathieu Chartier | 661974a | 2014-01-09 11:23:53 -0800 | [diff] [blame] | 703 | } |
| 704 | } |
| 705 | |
Mathieu Chartier | 9ef78b5 | 2014-09-25 17:03:12 -0700 | [diff] [blame] | 706 | void Instrumentation::InstrumentQuickAllocEntryPoints() { |
| 707 | MutexLock mu(Thread::Current(), *Locks::instrument_entrypoints_lock_); |
| 708 | InstrumentQuickAllocEntryPointsLocked(); |
Ian Rogers | fa82427 | 2013-11-05 16:12:57 -0800 | [diff] [blame] | 709 | } |
| 710 | |
Mathieu Chartier | 9ef78b5 | 2014-09-25 17:03:12 -0700 | [diff] [blame] | 711 | void Instrumentation::UninstrumentQuickAllocEntryPoints() { |
| 712 | MutexLock mu(Thread::Current(), *Locks::instrument_entrypoints_lock_); |
| 713 | UninstrumentQuickAllocEntryPointsLocked(); |
| 714 | } |
| 715 | |
| 716 | void Instrumentation::InstrumentQuickAllocEntryPointsLocked() { |
| 717 | Locks::instrument_entrypoints_lock_->AssertHeld(Thread::Current()); |
| 718 | if (quick_alloc_entry_points_instrumentation_counter_ == 0) { |
| 719 | SetEntrypointsInstrumented(true); |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 720 | } |
Mathieu Chartier | 9ef78b5 | 2014-09-25 17:03:12 -0700 | [diff] [blame] | 721 | ++quick_alloc_entry_points_instrumentation_counter_; |
Mathieu Chartier | 9ef78b5 | 2014-09-25 17:03:12 -0700 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | void Instrumentation::UninstrumentQuickAllocEntryPointsLocked() { |
| 725 | Locks::instrument_entrypoints_lock_->AssertHeld(Thread::Current()); |
| 726 | CHECK_GT(quick_alloc_entry_points_instrumentation_counter_, 0U); |
| 727 | --quick_alloc_entry_points_instrumentation_counter_; |
| 728 | if (quick_alloc_entry_points_instrumentation_counter_ == 0) { |
| 729 | SetEntrypointsInstrumented(false); |
| 730 | } |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | void Instrumentation::ResetQuickAllocEntryPoints() { |
| 734 | Runtime* runtime = Runtime::Current(); |
| 735 | if (runtime->IsStarted()) { |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 736 | MutexLock mu(Thread::Current(), *Locks::thread_list_lock_); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 737 | runtime->GetThreadList()->ForEach(ResetQuickAllocEntryPointsForThread, nullptr); |
Ian Rogers | fa82427 | 2013-11-05 16:12:57 -0800 | [diff] [blame] | 738 | } |
| 739 | } |
| 740 | |
Mingyao Yang | 3fd448a | 2016-05-10 14:30:41 -0700 | [diff] [blame] | 741 | void Instrumentation::UpdateMethodsCodeImpl(ArtMethod* method, const void* quick_code) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 742 | const void* new_quick_code; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 743 | if (LIKELY(!instrumentation_stubs_installed_)) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 744 | new_quick_code = quick_code; |
Jeff Hao | 65d15d9 | 2013-07-16 16:39:33 -0700 | [diff] [blame] | 745 | } else { |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 746 | if ((interpreter_stubs_installed_ || IsDeoptimized(method)) && !method->IsNative()) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 747 | new_quick_code = GetQuickToInterpreterBridge(); |
Jeff Hao | 65d15d9 | 2013-07-16 16:39:33 -0700 | [diff] [blame] | 748 | } else { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 749 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 750 | if (class_linker->IsQuickResolutionStub(quick_code) || |
| 751 | class_linker->IsQuickToInterpreterBridge(quick_code)) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 752 | new_quick_code = quick_code; |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 753 | } else if (entry_exit_stubs_installed_) { |
| 754 | new_quick_code = GetQuickInstrumentationEntryPoint(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 755 | } else { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 756 | new_quick_code = quick_code; |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 757 | } |
Jeff Hao | 65d15d9 | 2013-07-16 16:39:33 -0700 | [diff] [blame] | 758 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 759 | } |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 760 | UpdateEntrypoints(method, new_quick_code); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 761 | } |
| 762 | |
Mingyao Yang | 3fd448a | 2016-05-10 14:30:41 -0700 | [diff] [blame] | 763 | void Instrumentation::UpdateMethodsCode(ArtMethod* method, const void* quick_code) { |
| 764 | DCHECK(method->GetDeclaringClass()->IsResolved()); |
| 765 | UpdateMethodsCodeImpl(method, quick_code); |
| 766 | } |
| 767 | |
Nicolas Geoffray | a0619e2 | 2016-12-20 13:57:43 +0000 | [diff] [blame] | 768 | void Instrumentation::UpdateMethodsCodeForJavaDebuggable(ArtMethod* method, |
| 769 | const void* quick_code) { |
| 770 | // When the runtime is set to Java debuggable, we may update the entry points of |
| 771 | // all methods of a class to the interpreter bridge. A method's declaring class |
| 772 | // might not be in resolved state yet in that case, so we bypass the DCHECK in |
| 773 | // UpdateMethodsCode. |
Mingyao Yang | 3fd448a | 2016-05-10 14:30:41 -0700 | [diff] [blame] | 774 | UpdateMethodsCodeImpl(method, quick_code); |
| 775 | } |
| 776 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 777 | bool Instrumentation::AddDeoptimizedMethod(ArtMethod* method) { |
| 778 | if (IsDeoptimizedMethod(method)) { |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 779 | // Already in the map. Return. |
| 780 | return false; |
| 781 | } |
| 782 | // Not found. Add it. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 783 | deoptimized_methods_.insert(method); |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 784 | return true; |
| 785 | } |
| 786 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 787 | bool Instrumentation::IsDeoptimizedMethod(ArtMethod* method) { |
| 788 | return deoptimized_methods_.find(method) != deoptimized_methods_.end(); |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 789 | } |
| 790 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 791 | ArtMethod* Instrumentation::BeginDeoptimizedMethod() { |
| 792 | if (deoptimized_methods_.empty()) { |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 793 | // Empty. |
| 794 | return nullptr; |
| 795 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 796 | return *deoptimized_methods_.begin(); |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 797 | } |
| 798 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 799 | bool Instrumentation::RemoveDeoptimizedMethod(ArtMethod* method) { |
| 800 | auto it = deoptimized_methods_.find(method); |
| 801 | if (it == deoptimized_methods_.end()) { |
| 802 | return false; |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 803 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 804 | deoptimized_methods_.erase(it); |
| 805 | return true; |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | bool Instrumentation::IsDeoptimizedMethodsEmpty() const { |
| 809 | return deoptimized_methods_.empty(); |
| 810 | } |
| 811 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 812 | void Instrumentation::Deoptimize(ArtMethod* method) { |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 813 | CHECK(!method->IsNative()); |
| 814 | CHECK(!method->IsProxyMethod()); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 815 | CHECK(method->IsInvokable()); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 816 | |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 817 | Thread* self = Thread::Current(); |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 818 | { |
| 819 | WriterMutexLock mu(self, deoptimized_methods_lock_); |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 820 | bool has_not_been_deoptimized = AddDeoptimizedMethod(method); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 821 | CHECK(has_not_been_deoptimized) << "Method " << ArtMethod::PrettyMethod(method) |
Daniel Mihalyi | ca1d06c | 2014-08-18 18:45:31 +0200 | [diff] [blame] | 822 | << " is already deoptimized"; |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 823 | } |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 824 | if (!interpreter_stubs_installed_) { |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 825 | UpdateEntrypoints(method, GetQuickInstrumentationEntryPoint()); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 826 | |
| 827 | // Install instrumentation exit stub and instrumentation frames. We may already have installed |
| 828 | // these previously so it will only cover the newly created frames. |
| 829 | instrumentation_stubs_installed_ = true; |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 830 | MutexLock mu(self, *Locks::thread_list_lock_); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 831 | Runtime::Current()->GetThreadList()->ForEach(InstrumentationInstallStack, this); |
| 832 | } |
| 833 | } |
| 834 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 835 | void Instrumentation::Undeoptimize(ArtMethod* method) { |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 836 | CHECK(!method->IsNative()); |
| 837 | CHECK(!method->IsProxyMethod()); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 838 | CHECK(method->IsInvokable()); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 839 | |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 840 | Thread* self = Thread::Current(); |
| 841 | bool empty; |
| 842 | { |
| 843 | WriterMutexLock mu(self, deoptimized_methods_lock_); |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 844 | bool found_and_erased = RemoveDeoptimizedMethod(method); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 845 | CHECK(found_and_erased) << "Method " << ArtMethod::PrettyMethod(method) |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 846 | << " is not deoptimized"; |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 847 | empty = IsDeoptimizedMethodsEmpty(); |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 848 | } |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 849 | |
| 850 | // Restore code and possibly stack only if we did not deoptimize everything. |
| 851 | if (!interpreter_stubs_installed_) { |
| 852 | // Restore its code or resolution trampoline. |
| 853 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 854 | if (method->IsStatic() && !method->IsConstructor() && |
| 855 | !method->GetDeclaringClass()->IsInitialized()) { |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 856 | UpdateEntrypoints(method, GetQuickResolutionStub()); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 857 | } else { |
Nicolas Geoffray | a0619e2 | 2016-12-20 13:57:43 +0000 | [diff] [blame] | 858 | const void* quick_code = NeedDebugVersionFor(method) |
| 859 | ? GetQuickToInterpreterBridge() |
| 860 | : class_linker->GetQuickOatCodeFor(method); |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 861 | UpdateEntrypoints(method, quick_code); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 862 | } |
| 863 | |
| 864 | // If there is no deoptimized method left, we can restore the stack of each thread. |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 865 | if (empty) { |
| 866 | MutexLock mu(self, *Locks::thread_list_lock_); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 867 | Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this); |
| 868 | instrumentation_stubs_installed_ = false; |
| 869 | } |
| 870 | } |
| 871 | } |
| 872 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 873 | bool Instrumentation::IsDeoptimized(ArtMethod* method) { |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 874 | DCHECK(method != nullptr); |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 875 | ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 876 | return IsDeoptimizedMethod(method); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 877 | } |
| 878 | |
| 879 | void Instrumentation::EnableDeoptimization() { |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 880 | ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_); |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 881 | CHECK(IsDeoptimizedMethodsEmpty()); |
Sebastien Hertz | 11d40c2 | 2014-02-19 18:00:17 +0100 | [diff] [blame] | 882 | CHECK_EQ(deoptimization_enabled_, false); |
| 883 | deoptimization_enabled_ = true; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 884 | } |
| 885 | |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 886 | void Instrumentation::DisableDeoptimization(const char* key) { |
Sebastien Hertz | 11d40c2 | 2014-02-19 18:00:17 +0100 | [diff] [blame] | 887 | CHECK_EQ(deoptimization_enabled_, true); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 888 | // If we deoptimized everything, undo it. |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 889 | InstrumentationLevel level = GetCurrentInstrumentationLevel(); |
| 890 | if (level == InstrumentationLevel::kInstrumentWithInterpreter) { |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 891 | UndeoptimizeEverything(key); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 892 | } |
| 893 | // Undeoptimized selected methods. |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 894 | while (true) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 895 | ArtMethod* method; |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 896 | { |
| 897 | ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_); |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 898 | if (IsDeoptimizedMethodsEmpty()) { |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 899 | break; |
| 900 | } |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 901 | method = BeginDeoptimizedMethod(); |
| 902 | CHECK(method != nullptr); |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 903 | } |
| 904 | Undeoptimize(method); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 905 | } |
Sebastien Hertz | 11d40c2 | 2014-02-19 18:00:17 +0100 | [diff] [blame] | 906 | deoptimization_enabled_ = false; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 907 | } |
| 908 | |
Sebastien Hertz | 11d40c2 | 2014-02-19 18:00:17 +0100 | [diff] [blame] | 909 | // Indicates if instrumentation should notify method enter/exit events to the listeners. |
| 910 | bool Instrumentation::ShouldNotifyMethodEnterExitEvents() const { |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 911 | if (!HasMethodEntryListeners() && !HasMethodExitListeners()) { |
| 912 | return false; |
| 913 | } |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 914 | return !deoptimization_enabled_ && !interpreter_stubs_installed_; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 915 | } |
| 916 | |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 917 | void Instrumentation::DeoptimizeEverything(const char* key) { |
| 918 | CHECK(deoptimization_enabled_); |
| 919 | ConfigureStubs(key, InstrumentationLevel::kInstrumentWithInterpreter); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 920 | } |
| 921 | |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 922 | void Instrumentation::UndeoptimizeEverything(const char* key) { |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 923 | CHECK(interpreter_stubs_installed_); |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 924 | CHECK(deoptimization_enabled_); |
| 925 | ConfigureStubs(key, InstrumentationLevel::kInstrumentNothing); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 926 | } |
| 927 | |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 928 | void Instrumentation::EnableMethodTracing(const char* key, bool needs_interpreter) { |
| 929 | InstrumentationLevel level; |
| 930 | if (needs_interpreter) { |
| 931 | level = InstrumentationLevel::kInstrumentWithInterpreter; |
| 932 | } else { |
| 933 | level = InstrumentationLevel::kInstrumentWithInstrumentationStubs; |
| 934 | } |
| 935 | ConfigureStubs(key, level); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 936 | } |
| 937 | |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 938 | void Instrumentation::DisableMethodTracing(const char* key) { |
| 939 | ConfigureStubs(key, InstrumentationLevel::kInstrumentNothing); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 940 | } |
| 941 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 942 | const void* Instrumentation::GetQuickCodeFor(ArtMethod* method, PointerSize pointer_size) const { |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 943 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 944 | if (LIKELY(!instrumentation_stubs_installed_)) { |
Mathieu Chartier | a7dd038 | 2014-11-20 17:08:58 -0800 | [diff] [blame] | 945 | const void* code = method->GetEntryPointFromQuickCompiledCodePtrSize(pointer_size); |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 946 | DCHECK(code != nullptr); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 947 | if (LIKELY(!class_linker->IsQuickResolutionStub(code) && |
| 948 | !class_linker->IsQuickToInterpreterBridge(code)) && |
| 949 | !class_linker->IsQuickResolutionStub(code) && |
| 950 | !class_linker->IsQuickToInterpreterBridge(code)) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 951 | return code; |
| 952 | } |
| 953 | } |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 954 | return class_linker->GetQuickOatCodeFor(method); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 955 | } |
| 956 | |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 957 | void Instrumentation::MethodEnterEventImpl(Thread* thread, |
| 958 | ObjPtr<mirror::Object> this_object, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 959 | ArtMethod* method, |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 960 | uint32_t dex_pc) const { |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 961 | if (HasMethodEntryListeners()) { |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 962 | Thread* self = Thread::Current(); |
| 963 | StackHandleScope<1> hs(self); |
| 964 | Handle<mirror::Object> thiz(hs.NewHandle(this_object)); |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 965 | for (InstrumentationListener* listener : method_entry_listeners_) { |
| 966 | if (listener != nullptr) { |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 967 | listener->MethodEntered(thread, thiz, method, dex_pc); |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 968 | } |
| 969 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 970 | } |
| 971 | } |
| 972 | |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 973 | void Instrumentation::MethodExitEventImpl(Thread* thread, |
| 974 | ObjPtr<mirror::Object> this_object, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 975 | ArtMethod* method, |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 976 | uint32_t dex_pc, |
| 977 | const JValue& return_value) const { |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 978 | if (HasMethodExitListeners()) { |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 979 | Thread* self = Thread::Current(); |
| 980 | StackHandleScope<2> hs(self); |
| 981 | Handle<mirror::Object> thiz(hs.NewHandle(this_object)); |
| 982 | if (method->GetInterfaceMethodIfProxy(kRuntimePointerSize) |
| 983 | ->GetReturnTypePrimitive() != Primitive::kPrimNot) { |
| 984 | for (InstrumentationListener* listener : method_exit_listeners_) { |
| 985 | if (listener != nullptr) { |
| 986 | listener->MethodExited(thread, thiz, method, dex_pc, return_value); |
| 987 | } |
| 988 | } |
| 989 | } else { |
| 990 | Handle<mirror::Object> ret(hs.NewHandle(return_value.GetL())); |
| 991 | for (InstrumentationListener* listener : method_exit_listeners_) { |
| 992 | if (listener != nullptr) { |
| 993 | listener->MethodExited(thread, thiz, method, dex_pc, ret); |
| 994 | } |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 995 | } |
| 996 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 997 | } |
| 998 | } |
| 999 | |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 1000 | void Instrumentation::MethodUnwindEvent(Thread* thread, |
| 1001 | mirror::Object* this_object, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1002 | ArtMethod* method, |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1003 | uint32_t dex_pc) const { |
Sebastien Hertz | 0462c4c | 2015-04-01 16:34:17 +0200 | [diff] [blame] | 1004 | if (HasMethodUnwindListeners()) { |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 1005 | Thread* self = Thread::Current(); |
| 1006 | StackHandleScope<1> hs(self); |
| 1007 | Handle<mirror::Object> thiz(hs.NewHandle(this_object)); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 1008 | for (InstrumentationListener* listener : method_unwind_listeners_) { |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 1009 | if (listener != nullptr) { |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 1010 | listener->MethodUnwind(thread, thiz, method, dex_pc); |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 1011 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1012 | } |
| 1013 | } |
| 1014 | } |
| 1015 | |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 1016 | void Instrumentation::DexPcMovedEventImpl(Thread* thread, |
| 1017 | ObjPtr<mirror::Object> this_object, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1018 | ArtMethod* method, |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1019 | uint32_t dex_pc) const { |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 1020 | Thread* self = Thread::Current(); |
| 1021 | StackHandleScope<1> hs(self); |
| 1022 | Handle<mirror::Object> thiz(hs.NewHandle(this_object)); |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 1023 | for (InstrumentationListener* listener : dex_pc_listeners_) { |
| 1024 | if (listener != nullptr) { |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 1025 | listener->DexPcMoved(thread, thiz, method, dex_pc); |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 1026 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1027 | } |
| 1028 | } |
| 1029 | |
Nicolas Geoffray | 81f0f95 | 2016-01-20 16:25:19 +0000 | [diff] [blame] | 1030 | void Instrumentation::BranchImpl(Thread* thread, |
| 1031 | ArtMethod* method, |
| 1032 | uint32_t dex_pc, |
| 1033 | int32_t offset) const { |
| 1034 | for (InstrumentationListener* listener : branch_listeners_) { |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 1035 | if (listener != nullptr) { |
Nicolas Geoffray | 81f0f95 | 2016-01-20 16:25:19 +0000 | [diff] [blame] | 1036 | listener->Branch(thread, method, dex_pc, offset); |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 1037 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 1038 | } |
| 1039 | } |
| 1040 | |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 1041 | void Instrumentation::InvokeVirtualOrInterfaceImpl(Thread* thread, |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 1042 | ObjPtr<mirror::Object> this_object, |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 1043 | ArtMethod* caller, |
| 1044 | uint32_t dex_pc, |
| 1045 | ArtMethod* callee) const { |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 1046 | Thread* self = Thread::Current(); |
| 1047 | StackHandleScope<1> hs(self); |
| 1048 | Handle<mirror::Object> thiz(hs.NewHandle(this_object)); |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 1049 | for (InstrumentationListener* listener : invoke_virtual_or_interface_listeners_) { |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 1050 | if (listener != nullptr) { |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 1051 | listener->InvokeVirtualOrInterface(thread, thiz, caller, dex_pc, callee); |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 1052 | } |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 1053 | } |
| 1054 | } |
| 1055 | |
Alex Light | e814f9d | 2017-07-31 16:14:39 -0700 | [diff] [blame] | 1056 | void Instrumentation::WatchedFramePopImpl(Thread* thread, const ShadowFrame& frame) const { |
| 1057 | for (InstrumentationListener* listener : watched_frame_pop_listeners_) { |
| 1058 | if (listener != nullptr) { |
| 1059 | listener->WatchedFramePop(thread, frame); |
| 1060 | } |
| 1061 | } |
| 1062 | } |
| 1063 | |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 1064 | void Instrumentation::FieldReadEventImpl(Thread* thread, |
| 1065 | ObjPtr<mirror::Object> this_object, |
| 1066 | ArtMethod* method, |
| 1067 | uint32_t dex_pc, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1068 | ArtField* field) const { |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 1069 | Thread* self = Thread::Current(); |
| 1070 | StackHandleScope<1> hs(self); |
| 1071 | Handle<mirror::Object> thiz(hs.NewHandle(this_object)); |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 1072 | for (InstrumentationListener* listener : field_read_listeners_) { |
| 1073 | if (listener != nullptr) { |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 1074 | listener->FieldRead(thread, thiz, method, dex_pc, field); |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 1075 | } |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 1076 | } |
| 1077 | } |
| 1078 | |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 1079 | void Instrumentation::FieldWriteEventImpl(Thread* thread, |
| 1080 | ObjPtr<mirror::Object> this_object, |
| 1081 | ArtMethod* method, |
| 1082 | uint32_t dex_pc, |
| 1083 | ArtField* field, |
| 1084 | const JValue& field_value) const { |
| 1085 | Thread* self = Thread::Current(); |
| 1086 | StackHandleScope<2> hs(self); |
| 1087 | Handle<mirror::Object> thiz(hs.NewHandle(this_object)); |
| 1088 | if (field->IsPrimitiveType()) { |
| 1089 | for (InstrumentationListener* listener : field_write_listeners_) { |
| 1090 | if (listener != nullptr) { |
| 1091 | listener->FieldWritten(thread, thiz, method, dex_pc, field, field_value); |
| 1092 | } |
| 1093 | } |
| 1094 | } else { |
| 1095 | Handle<mirror::Object> val(hs.NewHandle(field_value.GetL())); |
| 1096 | for (InstrumentationListener* listener : field_write_listeners_) { |
| 1097 | if (listener != nullptr) { |
| 1098 | listener->FieldWritten(thread, thiz, method, dex_pc, field, val); |
| 1099 | } |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 1100 | } |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 1101 | } |
| 1102 | } |
| 1103 | |
Alex Light | 6e1607e | 2017-08-23 10:06:18 -0700 | [diff] [blame] | 1104 | void Instrumentation::ExceptionThrownEvent(Thread* thread, |
Sebastien Hertz | 947ff08 | 2013-09-17 14:10:13 +0200 | [diff] [blame] | 1105 | mirror::Throwable* exception_object) const { |
Hiroshi Yamauchi | 3481f7a | 2017-02-10 12:07:36 -0800 | [diff] [blame] | 1106 | Thread* self = Thread::Current(); |
| 1107 | StackHandleScope<1> hs(self); |
| 1108 | Handle<mirror::Throwable> h_exception(hs.NewHandle(exception_object)); |
Alex Light | 6e1607e | 2017-08-23 10:06:18 -0700 | [diff] [blame] | 1109 | if (HasExceptionThrownListeners()) { |
Hiroshi Yamauchi | 3481f7a | 2017-02-10 12:07:36 -0800 | [diff] [blame] | 1110 | DCHECK_EQ(thread->GetException(), h_exception.Get()); |
Jeff Hao | c0bd4da | 2013-04-11 15:52:28 -0700 | [diff] [blame] | 1111 | thread->ClearException(); |
Alex Light | 6e1607e | 2017-08-23 10:06:18 -0700 | [diff] [blame] | 1112 | for (InstrumentationListener* listener : exception_thrown_listeners_) { |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 1113 | if (listener != nullptr) { |
Alex Light | 6e1607e | 2017-08-23 10:06:18 -0700 | [diff] [blame] | 1114 | listener->ExceptionThrown(thread, h_exception); |
Nicolas Geoffray | 514a616 | 2015-11-03 11:44:24 +0000 | [diff] [blame] | 1115 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1116 | } |
Hiroshi Yamauchi | 3481f7a | 2017-02-10 12:07:36 -0800 | [diff] [blame] | 1117 | thread->SetException(h_exception.Get()); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1118 | } |
| 1119 | } |
| 1120 | |
Sebastien Hertz | b2feaaf | 2015-10-12 13:40:10 +0000 | [diff] [blame] | 1121 | // Computes a frame ID by ignoring inlined frames. |
| 1122 | size_t Instrumentation::ComputeFrameId(Thread* self, |
| 1123 | size_t frame_depth, |
| 1124 | size_t inlined_frames_before_frame) { |
| 1125 | CHECK_GE(frame_depth, inlined_frames_before_frame); |
| 1126 | size_t no_inline_depth = frame_depth - inlined_frames_before_frame; |
| 1127 | return StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk) - no_inline_depth; |
| 1128 | } |
| 1129 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1130 | static void CheckStackDepth(Thread* self, const InstrumentationStackFrame& instrumentation_frame, |
| 1131 | int delta) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1132 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 1133 | size_t frame_id = StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk) + delta; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1134 | if (frame_id != instrumentation_frame.frame_id_) { |
| 1135 | LOG(ERROR) << "Expected frame_id=" << frame_id << " but found " |
| 1136 | << instrumentation_frame.frame_id_; |
| 1137 | StackVisitor::DescribeStack(self); |
| 1138 | CHECK_EQ(frame_id, instrumentation_frame.frame_id_); |
| 1139 | } |
| 1140 | } |
| 1141 | |
| 1142 | void Instrumentation::PushInstrumentationStackFrame(Thread* self, mirror::Object* this_object, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1143 | ArtMethod* method, |
Jeff Hao | 9a916d3 | 2013-06-27 18:45:37 -0700 | [diff] [blame] | 1144 | uintptr_t lr, bool interpreter_entry) { |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 1145 | DCHECK(!self->IsExceptionPending()); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1146 | std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack(); |
| 1147 | if (kVerboseInstrumentation) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1148 | LOG(INFO) << "Entering " << ArtMethod::PrettyMethod(method) << " from PC " |
| 1149 | << reinterpret_cast<void*>(lr); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1150 | } |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 1151 | |
| 1152 | // We send the enter event before pushing the instrumentation frame to make cleanup easier. If the |
| 1153 | // event causes an exception we can simply send the unwind event and return. |
| 1154 | StackHandleScope<1> hs(self); |
| 1155 | Handle<mirror::Object> h_this(hs.NewHandle(this_object)); |
| 1156 | if (!interpreter_entry) { |
| 1157 | MethodEnterEvent(self, h_this.Get(), method, 0); |
| 1158 | if (self->IsExceptionPending()) { |
| 1159 | MethodUnwindEvent(self, h_this.Get(), method, 0); |
| 1160 | return; |
| 1161 | } |
| 1162 | } |
| 1163 | |
| 1164 | // We have a callee-save frame meaning this value is guaranteed to never be 0. |
| 1165 | DCHECK(!self->IsExceptionPending()); |
| 1166 | size_t frame_id = StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk); |
| 1167 | |
| 1168 | instrumentation::InstrumentationStackFrame instrumentation_frame(h_this.Get(), method, lr, |
Jeff Hao | 9a916d3 | 2013-06-27 18:45:37 -0700 | [diff] [blame] | 1169 | frame_id, interpreter_entry); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1170 | stack->push_front(instrumentation_frame); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1171 | } |
| 1172 | |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 1173 | TwoWordReturn Instrumentation::PopInstrumentationStackFrame(Thread* self, |
| 1174 | uintptr_t* return_pc, |
| 1175 | uint64_t* gpr_result, |
| 1176 | uint64_t* fpr_result) { |
| 1177 | DCHECK(gpr_result != nullptr); |
| 1178 | DCHECK(fpr_result != nullptr); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1179 | // Do the pop. |
| 1180 | std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack(); |
| 1181 | CHECK_GT(stack->size(), 0U); |
| 1182 | InstrumentationStackFrame instrumentation_frame = stack->front(); |
| 1183 | stack->pop_front(); |
| 1184 | |
| 1185 | // Set return PC and check the sanity of the stack. |
| 1186 | *return_pc = instrumentation_frame.return_pc_; |
| 1187 | CheckStackDepth(self, instrumentation_frame, 0); |
Ian Rogers | 1d8cdbc | 2014-09-22 22:51:09 -0700 | [diff] [blame] | 1188 | self->VerifyStack(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1189 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1190 | ArtMethod* method = instrumentation_frame.method_; |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1191 | uint32_t length; |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1192 | const PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize(); |
Nicolas Geoffray | 07c7028 | 2017-08-30 08:09:42 +0000 | [diff] [blame] | 1193 | char return_shorty = method->GetInterfaceMethodIfProxy(pointer_size)->GetShorty(&length)[0]; |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 1194 | bool is_ref = return_shorty == '[' || return_shorty == 'L'; |
| 1195 | StackHandleScope<1> hs(self); |
| 1196 | MutableHandle<mirror::Object> res(hs.NewHandle<mirror::Object>(nullptr)); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1197 | JValue return_value; |
| 1198 | if (return_shorty == 'V') { |
| 1199 | return_value.SetJ(0); |
| 1200 | } else if (return_shorty == 'F' || return_shorty == 'D') { |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 1201 | return_value.SetJ(*fpr_result); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1202 | } else { |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 1203 | return_value.SetJ(*gpr_result); |
| 1204 | } |
| 1205 | if (is_ref) { |
| 1206 | // Take a handle to the return value so we won't lose it if we suspend. |
| 1207 | res.Assign(return_value.GetL()); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1208 | } |
| 1209 | // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to |
| 1210 | // return_pc. |
| 1211 | uint32_t dex_pc = DexFile::kDexNoIndex; |
| 1212 | mirror::Object* this_object = instrumentation_frame.this_object_; |
Nicolas Geoffray | 07c7028 | 2017-08-30 08:09:42 +0000 | [diff] [blame] | 1213 | if (!instrumentation_frame.interpreter_entry_) { |
Sebastien Hertz | 320deb2 | 2014-06-11 19:45:05 +0200 | [diff] [blame] | 1214 | MethodExitEvent(self, this_object, instrumentation_frame.method_, dex_pc, return_value); |
| 1215 | } |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 1216 | |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 1217 | // Deoptimize if the caller needs to continue execution in the interpreter. Do nothing if we get |
| 1218 | // back to an upcall. |
| 1219 | NthCallerVisitor visitor(self, 1, true); |
| 1220 | visitor.WalkStack(true); |
Sebastien Hertz | 270a0e1 | 2015-01-16 19:49:09 +0100 | [diff] [blame] | 1221 | bool deoptimize = (visitor.caller != nullptr) && |
Daniel Mihalyi | eb07669 | 2014-08-22 17:33:31 +0200 | [diff] [blame] | 1222 | (interpreter_stubs_installed_ || IsDeoptimized(visitor.caller) || |
| 1223 | Dbg::IsForcedInterpreterNeededForUpcall(self, visitor.caller)); |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 1224 | if (is_ref) { |
| 1225 | // Restore the return value if it's a reference since it might have moved. |
| 1226 | *reinterpret_cast<mirror::Object**>(gpr_result) = res.Get(); |
| 1227 | } |
Nicolas Geoffray | a0619e2 | 2016-12-20 13:57:43 +0000 | [diff] [blame] | 1228 | if (deoptimize && Runtime::Current()->IsAsyncDeoptimizeable(*return_pc)) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1229 | if (kVerboseInstrumentation) { |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 1230 | LOG(INFO) << "Deoptimizing " |
| 1231 | << visitor.caller->PrettyMethod() |
| 1232 | << " by returning from " |
| 1233 | << method->PrettyMethod() |
| 1234 | << " with result " |
| 1235 | << std::hex << return_value.GetJ() << std::dec |
| 1236 | << " in " |
| 1237 | << *self; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1238 | } |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 1239 | self->PushDeoptimizationContext(return_value, |
Nicolas Geoffray | 07c7028 | 2017-08-30 08:09:42 +0000 | [diff] [blame] | 1240 | return_shorty == 'L', |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 1241 | false /* from_code */, |
Nicolas Geoffray | 07c7028 | 2017-08-30 08:09:42 +0000 | [diff] [blame] | 1242 | nullptr /* no pending exception */); |
Andreas Gampe | d58342c | 2014-06-05 14:18:08 -0700 | [diff] [blame] | 1243 | return GetTwoWordSuccessValue(*return_pc, |
| 1244 | reinterpret_cast<uintptr_t>(GetQuickDeoptimizationEntryPoint())); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1245 | } else { |
Nicolas Geoffray | a0619e2 | 2016-12-20 13:57:43 +0000 | [diff] [blame] | 1246 | if (deoptimize && !Runtime::Current()->IsAsyncDeoptimizeable(*return_pc)) { |
| 1247 | LOG(WARNING) << "Got a deoptimization request on un-deoptimizable " << method->PrettyMethod() |
| 1248 | << " at PC " << reinterpret_cast<void*>(*return_pc); |
| 1249 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1250 | if (kVerboseInstrumentation) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1251 | LOG(INFO) << "Returning from " << method->PrettyMethod() |
Brian Carlstrom | 2d88862 | 2013-07-18 17:02:00 -0700 | [diff] [blame] | 1252 | << " to PC " << reinterpret_cast<void*>(*return_pc); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1253 | } |
Andreas Gampe | d58342c | 2014-06-05 14:18:08 -0700 | [diff] [blame] | 1254 | return GetTwoWordSuccessValue(0, *return_pc); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1255 | } |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 1256 | } |
| 1257 | |
Mingyao Yang | f711f2c | 2016-05-23 12:29:39 -0700 | [diff] [blame] | 1258 | uintptr_t Instrumentation::PopMethodForUnwind(Thread* self, bool is_deoptimization) const { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1259 | // Do the pop. |
| 1260 | std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack(); |
| 1261 | CHECK_GT(stack->size(), 0U); |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 1262 | size_t idx = stack->size(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1263 | InstrumentationStackFrame instrumentation_frame = stack->front(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1264 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1265 | ArtMethod* method = instrumentation_frame.method_; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1266 | if (is_deoptimization) { |
| 1267 | if (kVerboseInstrumentation) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1268 | LOG(INFO) << "Popping for deoptimization " << ArtMethod::PrettyMethod(method); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1269 | } |
| 1270 | } else { |
| 1271 | if (kVerboseInstrumentation) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1272 | LOG(INFO) << "Popping for unwind " << ArtMethod::PrettyMethod(method); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1273 | } |
| 1274 | |
| 1275 | // Notify listeners of method unwind. |
| 1276 | // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to |
| 1277 | // return_pc. |
| 1278 | uint32_t dex_pc = DexFile::kDexNoIndex; |
Nicolas Geoffray | 07c7028 | 2017-08-30 08:09:42 +0000 | [diff] [blame] | 1279 | MethodUnwindEvent(self, instrumentation_frame.this_object_, method, dex_pc); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1280 | } |
Alex Light | b7edcda | 2017-04-27 13:20:31 -0700 | [diff] [blame] | 1281 | // TODO: bring back CheckStackDepth(self, instrumentation_frame, 2); |
| 1282 | CHECK_EQ(stack->size(), idx); |
| 1283 | DCHECK(instrumentation_frame.method_ == stack->front().method_); |
| 1284 | stack->pop_front(); |
Mingyao Yang | f711f2c | 2016-05-23 12:29:39 -0700 | [diff] [blame] | 1285 | return instrumentation_frame.return_pc_; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1286 | } |
| 1287 | |
| 1288 | std::string InstrumentationStackFrame::Dump() const { |
| 1289 | std::ostringstream os; |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1290 | os << "Frame " << frame_id_ << " " << ArtMethod::PrettyMethod(method_) << ":" |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1291 | << reinterpret_cast<void*>(return_pc_) << " this=" << reinterpret_cast<void*>(this_object_); |
| 1292 | return os.str(); |
| 1293 | } |
| 1294 | |
| 1295 | } // namespace instrumentation |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 1296 | } // namespace art |