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