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