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