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