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