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