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