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