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