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 | |
| 19 | #include <sys/uio.h> |
| 20 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 21 | #include "arch/context.h" |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 22 | #include "atomic.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 23 | #include "base/unix_file/fd_file.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" |
Mathieu Chartier | d889178 | 2014-03-02 13:28:37 -0800 | [diff] [blame] | 27 | #include "entrypoints/quick/quick_alloc_entrypoints.h" |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 28 | #include "interpreter/interpreter.h" |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 29 | #include "mirror/art_method-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 30 | #include "mirror/class-inl.h" |
| 31 | #include "mirror/dex_cache.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 32 | #include "mirror/object_array-inl.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 33 | #include "mirror/object-inl.h" |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 34 | #include "nth_caller_visitor.h" |
Ian Rogers | c928de9 | 2013-02-27 14:30:44 -0800 | [diff] [blame] | 35 | #if !defined(ART_USE_PORTABLE_COMPILER) |
Ian Rogers | 166db04 | 2013-07-26 12:05:57 -0700 | [diff] [blame] | 36 | #include "entrypoints/quick/quick_entrypoints.h" |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 37 | #endif |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 38 | #include "os.h" |
| 39 | #include "scoped_thread_state_change.h" |
| 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 | fa82427 | 2013-11-05 16:12:57 -0800 | [diff] [blame] | 44 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 45 | namespace instrumentation { |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 46 | |
Sebastien Hertz | 5bfd5c9 | 2013-11-15 11:36:07 +0100 | [diff] [blame] | 47 | const bool kVerboseInstrumentation = false; |
| 48 | |
Ian Rogers | 816432e | 2013-09-06 15:47:45 -0700 | [diff] [blame] | 49 | // Do we want to deoptimize for method entry and exit listeners or just try to intercept |
| 50 | // invocations? Deoptimization forces all code to run in the interpreter and considerably hurts the |
| 51 | // application's performance. |
Ian Rogers | 7b6da36 | 2013-09-11 09:29:40 -0700 | [diff] [blame] | 52 | static constexpr bool kDeoptimizeForAccurateMethodEntryExitListeners = false; |
Ian Rogers | 816432e | 2013-09-06 15:47:45 -0700 | [diff] [blame] | 53 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 54 | static bool InstallStubsClassVisitor(mirror::Class* klass, void* arg) |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 55 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 56 | Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg); |
| 57 | return instrumentation->InstallStubsForClass(klass); |
| 58 | } |
| 59 | |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 60 | Instrumentation::Instrumentation() |
| 61 | : instrumentation_stubs_installed_(false), entry_exit_stubs_installed_(false), |
| 62 | interpreter_stubs_installed_(false), |
| 63 | interpret_only_(false), forced_interpret_only_(false), |
| 64 | have_method_entry_listeners_(false), have_method_exit_listeners_(false), |
| 65 | have_method_unwind_listeners_(false), have_dex_pc_listeners_(false), |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 66 | have_field_read_listeners_(false), have_field_write_listeners_(false), |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 67 | have_exception_caught_listeners_(false), |
| 68 | deoptimized_methods_lock_("deoptimized methods lock"), |
| 69 | deoptimization_enabled_(false), |
| 70 | interpreter_handler_table_(kMainHandlerTable), |
| 71 | quick_alloc_entry_points_instrumentation_counter_(0) { |
| 72 | } |
| 73 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 74 | bool Instrumentation::InstallStubsForClass(mirror::Class* klass) { |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 75 | for (size_t i = 0, e = klass->NumDirectMethods(); i < e; i++) { |
| 76 | InstallStubsForMethod(klass->GetDirectMethod(i)); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 77 | } |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 78 | for (size_t i = 0, e = klass->NumVirtualMethods(); i < e; i++) { |
| 79 | InstallStubsForMethod(klass->GetVirtualMethod(i)); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 80 | } |
| 81 | return true; |
| 82 | } |
| 83 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 84 | static void UpdateEntrypoints(mirror::ArtMethod* method, const void* quick_code, |
| 85 | const void* portable_code, bool have_portable_code) |
| 86 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 87 | method->SetEntryPointFromPortableCompiledCode(portable_code); |
| 88 | method->SetEntryPointFromQuickCompiledCode(quick_code); |
| 89 | bool portable_enabled = method->IsPortableCompiled(); |
| 90 | if (have_portable_code && !portable_enabled) { |
| 91 | method->SetIsPortableCompiled(); |
| 92 | } else if (portable_enabled) { |
| 93 | method->ClearIsPortableCompiled(); |
| 94 | } |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 95 | if (!method->IsResolutionMethod()) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 96 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Hiroshi Yamauchi | 563b47c | 2014-02-28 17:18:37 -0800 | [diff] [blame] | 97 | if (quick_code == GetQuickToInterpreterBridge() || |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 98 | quick_code == class_linker->GetQuickToInterpreterBridgeTrampoline() || |
| 99 | (quick_code == class_linker->GetQuickResolutionTrampoline() && |
Hiroshi Yamauchi | 563b47c | 2014-02-28 17:18:37 -0800 | [diff] [blame] | 100 | Runtime::Current()->GetInstrumentation()->IsForcedInterpretOnly() |
| 101 | && !method->IsNative() && !method->IsProxyMethod())) { |
| 102 | if (kIsDebugBuild) { |
| 103 | if (quick_code == GetQuickToInterpreterBridge()) { |
| 104 | DCHECK(portable_code == GetPortableToInterpreterBridge()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 105 | } else if (quick_code == class_linker->GetQuickResolutionTrampoline()) { |
| 106 | DCHECK(portable_code == class_linker->GetPortableResolutionTrampoline()); |
Hiroshi Yamauchi | 563b47c | 2014-02-28 17:18:37 -0800 | [diff] [blame] | 107 | } |
| 108 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 109 | DCHECK(!method->IsNative()) << PrettyMethod(method); |
Hiroshi Yamauchi | 563b47c | 2014-02-28 17:18:37 -0800 | [diff] [blame] | 110 | DCHECK(!method->IsProxyMethod()) << PrettyMethod(method); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 111 | method->SetEntryPointFromInterpreter(art::interpreter::artInterpreterToInterpreterBridge); |
| 112 | } else { |
| 113 | method->SetEntryPointFromInterpreter(art::artInterpreterToCompiledCodeBridge); |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | void Instrumentation::InstallStubsForMethod(mirror::ArtMethod* method) { |
| 119 | if (method->IsAbstract() || method->IsProxyMethod()) { |
| 120 | // Do not change stubs for these methods. |
| 121 | return; |
| 122 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 123 | const void* new_portable_code; |
| 124 | const void* new_quick_code; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 125 | bool uninstall = !entry_exit_stubs_installed_ && !interpreter_stubs_installed_; |
| 126 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 127 | bool is_class_initialized = method->GetDeclaringClass()->IsInitialized(); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 128 | bool have_portable_code = false; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 129 | if (uninstall) { |
| 130 | if ((forced_interpret_only_ || IsDeoptimized(method)) && !method->IsNative()) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 131 | new_portable_code = GetPortableToInterpreterBridge(); |
| 132 | new_quick_code = GetQuickToInterpreterBridge(); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 133 | } else if (is_class_initialized || !method->IsStatic() || method->IsConstructor()) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 134 | new_portable_code = class_linker->GetPortableOatCodeFor(method, &have_portable_code); |
| 135 | new_quick_code = class_linker->GetQuickOatCodeFor(method); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 136 | } else { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 137 | new_portable_code = class_linker->GetPortableResolutionTrampoline(); |
| 138 | new_quick_code = class_linker->GetQuickResolutionTrampoline(); |
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_portable_code = GetPortableToInterpreterBridge(); |
| 144 | new_quick_code = GetQuickToInterpreterBridge(); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 145 | } else { |
| 146 | // Do not overwrite resolution trampoline. When the trampoline initializes the method's |
| 147 | // class, all its static methods code will be set to the instrumentation entry point. |
| 148 | // For more details, see ClassLinker::FixupStaticTrampolines. |
| 149 | if (is_class_initialized || !method->IsStatic() || method->IsConstructor()) { |
Sebastien Hertz | 320deb2 | 2014-06-11 19:45:05 +0200 | [diff] [blame] | 150 | if (entry_exit_stubs_installed_) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 151 | new_portable_code = GetPortableToInterpreterBridge(); |
| 152 | new_quick_code = GetQuickInstrumentationEntryPoint(); |
Sebastien Hertz | 320deb2 | 2014-06-11 19:45:05 +0200 | [diff] [blame] | 153 | } else { |
| 154 | new_portable_code = class_linker->GetPortableOatCodeFor(method, &have_portable_code); |
| 155 | new_quick_code = class_linker->GetQuickOatCodeFor(method); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 156 | DCHECK(new_quick_code != class_linker->GetQuickToInterpreterBridgeTrampoline()); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 157 | } |
| 158 | } else { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 159 | new_portable_code = class_linker->GetPortableResolutionTrampoline(); |
| 160 | new_quick_code = class_linker->GetQuickResolutionTrampoline(); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 164 | UpdateEntrypoints(method, new_quick_code, new_portable_code, have_portable_code); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 165 | } |
| 166 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 167 | // Places the instrumentation exit pc as the return PC for every quick frame. This also allows |
| 168 | // deoptimization of quick frames to interpreter frames. |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 169 | // Since we may already have done this previously, we need to push new instrumentation frame before |
| 170 | // existing instrumentation frames. |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 171 | static void InstrumentationInstallStack(Thread* thread, void* arg) |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 172 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 173 | struct InstallStackVisitor : public StackVisitor { |
Sebastien Hertz | 11d40c2 | 2014-02-19 18:00:17 +0100 | [diff] [blame] | 174 | InstallStackVisitor(Thread* thread, Context* context, uintptr_t instrumentation_exit_pc) |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 175 | : StackVisitor(thread, context), instrumentation_stack_(thread->GetInstrumentationStack()), |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 176 | instrumentation_exit_pc_(instrumentation_exit_pc), |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 177 | reached_existing_instrumentation_frames_(false), instrumentation_stack_depth_(0), |
| 178 | last_return_pc_(0) { |
| 179 | } |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 180 | |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 181 | virtual bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 182 | mirror::ArtMethod* m = GetMethod(); |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 183 | if (m == NULL) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 184 | if (kVerboseInstrumentation) { |
| 185 | LOG(INFO) << " Skipping upcall. Frame " << GetFrameId(); |
| 186 | } |
| 187 | last_return_pc_ = 0; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 188 | return true; // Ignore upcalls. |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 189 | } |
Jeff Hao | a15a81b | 2014-05-27 18:25:47 -0700 | [diff] [blame] | 190 | if (GetCurrentQuickFrame() == NULL) { |
Sebastien Hertz | 320deb2 | 2014-06-11 19:45:05 +0200 | [diff] [blame] | 191 | bool interpreter_frame = !m->IsPortableCompiled(); |
| 192 | InstrumentationStackFrame instrumentation_frame(GetThisObject(), m, 0, GetFrameId(), |
| 193 | interpreter_frame); |
Jeff Hao | a15a81b | 2014-05-27 18:25:47 -0700 | [diff] [blame] | 194 | if (kVerboseInstrumentation) { |
| 195 | LOG(INFO) << "Pushing shadow frame " << instrumentation_frame.Dump(); |
| 196 | } |
| 197 | shadow_stack_.push_back(instrumentation_frame); |
| 198 | return true; // Continue. |
| 199 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 200 | uintptr_t return_pc = GetReturnPc(); |
Sebastien Hertz | 320deb2 | 2014-06-11 19:45:05 +0200 | [diff] [blame] | 201 | if (m->IsRuntimeMethod()) { |
| 202 | if (return_pc == instrumentation_exit_pc_) { |
| 203 | if (kVerboseInstrumentation) { |
| 204 | LOG(INFO) << " Handling quick to interpreter transition. Frame " << GetFrameId(); |
| 205 | } |
| 206 | CHECK_LT(instrumentation_stack_depth_, instrumentation_stack_->size()); |
| 207 | const InstrumentationStackFrame& frame = instrumentation_stack_->at(instrumentation_stack_depth_); |
| 208 | CHECK(frame.interpreter_entry_); |
| 209 | // This is an interpreter frame so method enter event must have been reported. However we |
| 210 | // need to push a DEX pc into the dex_pcs_ list to match size of instrumentation stack. |
| 211 | // Since we won't report method entry here, we can safely push any DEX pc. |
| 212 | dex_pcs_.push_back(0); |
| 213 | last_return_pc_ = frame.return_pc_; |
| 214 | ++instrumentation_stack_depth_; |
| 215 | return true; |
| 216 | } else { |
| 217 | if (kVerboseInstrumentation) { |
| 218 | LOG(INFO) << " Skipping runtime method. Frame " << GetFrameId(); |
| 219 | } |
| 220 | last_return_pc_ = GetReturnPc(); |
| 221 | return true; // Ignore unresolved methods since they will be instrumented after resolution. |
| 222 | } |
| 223 | } |
| 224 | if (kVerboseInstrumentation) { |
| 225 | LOG(INFO) << " Installing exit stub in " << DescribeLocation(); |
| 226 | } |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 227 | if (return_pc == instrumentation_exit_pc_) { |
| 228 | // We've reached a frame which has already been installed with instrumentation exit stub. |
| 229 | // We should have already installed instrumentation on previous frames. |
| 230 | reached_existing_instrumentation_frames_ = true; |
| 231 | |
| 232 | CHECK_LT(instrumentation_stack_depth_, instrumentation_stack_->size()); |
| 233 | const InstrumentationStackFrame& frame = instrumentation_stack_->at(instrumentation_stack_depth_); |
| 234 | CHECK_EQ(m, frame.method_) << "Expected " << PrettyMethod(m) |
| 235 | << ", Found " << PrettyMethod(frame.method_); |
| 236 | return_pc = frame.return_pc_; |
| 237 | if (kVerboseInstrumentation) { |
| 238 | LOG(INFO) << "Ignoring already instrumented " << frame.Dump(); |
| 239 | } |
| 240 | } else { |
| 241 | CHECK_NE(return_pc, 0U); |
| 242 | CHECK(!reached_existing_instrumentation_frames_); |
| 243 | InstrumentationStackFrame instrumentation_frame(GetThisObject(), m, return_pc, GetFrameId(), |
| 244 | false); |
| 245 | if (kVerboseInstrumentation) { |
| 246 | LOG(INFO) << "Pushing frame " << instrumentation_frame.Dump(); |
| 247 | } |
| 248 | |
Sebastien Hertz | 320deb2 | 2014-06-11 19:45:05 +0200 | [diff] [blame] | 249 | // Insert frame at the right position so we do not corrupt the instrumentation stack. |
| 250 | // Instrumentation stack frames are in descending frame id order. |
| 251 | auto it = instrumentation_stack_->begin(); |
| 252 | for (auto end = instrumentation_stack_->end(); it != end; ++it) { |
| 253 | const InstrumentationStackFrame& current = *it; |
| 254 | if (instrumentation_frame.frame_id_ >= current.frame_id_) { |
| 255 | break; |
| 256 | } |
| 257 | } |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 258 | instrumentation_stack_->insert(it, instrumentation_frame); |
| 259 | SetReturnPc(instrumentation_exit_pc_); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 260 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 261 | dex_pcs_.push_back(m->ToDexPc(last_return_pc_)); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 262 | last_return_pc_ = return_pc; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 263 | ++instrumentation_stack_depth_; |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 264 | return true; // Continue. |
| 265 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 266 | std::deque<InstrumentationStackFrame>* const instrumentation_stack_; |
Jeff Hao | a15a81b | 2014-05-27 18:25:47 -0700 | [diff] [blame] | 267 | std::vector<InstrumentationStackFrame> shadow_stack_; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 268 | std::vector<uint32_t> dex_pcs_; |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 269 | const uintptr_t instrumentation_exit_pc_; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 270 | bool reached_existing_instrumentation_frames_; |
| 271 | size_t instrumentation_stack_depth_; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 272 | uintptr_t last_return_pc_; |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 273 | }; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 274 | if (kVerboseInstrumentation) { |
| 275 | std::string thread_name; |
| 276 | thread->GetThreadName(thread_name); |
| 277 | LOG(INFO) << "Installing exit stubs in " << thread_name; |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 278 | } |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 279 | |
| 280 | Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg); |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 281 | std::unique_ptr<Context> context(Context::Create()); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 282 | uintptr_t instrumentation_exit_pc = GetQuickInstrumentationExitPc(); |
Sebastien Hertz | 11d40c2 | 2014-02-19 18:00:17 +0100 | [diff] [blame] | 283 | InstallStackVisitor visitor(thread, context.get(), instrumentation_exit_pc); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 284 | visitor.WalkStack(true); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 285 | CHECK_EQ(visitor.dex_pcs_.size(), thread->GetInstrumentationStack()->size()); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 286 | |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 287 | if (instrumentation->ShouldNotifyMethodEnterExitEvents()) { |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 288 | // Create method enter events for all methods currently on the thread's stack. We only do this |
| 289 | // if no debugger is attached to prevent from posting events twice. |
Jeff Hao | a15a81b | 2014-05-27 18:25:47 -0700 | [diff] [blame] | 290 | auto ssi = visitor.shadow_stack_.rbegin(); |
| 291 | for (auto isi = thread->GetInstrumentationStack()->rbegin(), |
| 292 | end = thread->GetInstrumentationStack()->rend(); isi != end; ++isi) { |
| 293 | while (ssi != visitor.shadow_stack_.rend() && (*ssi).frame_id_ < (*isi).frame_id_) { |
| 294 | instrumentation->MethodEnterEvent(thread, (*ssi).this_object_, (*ssi).method_, 0); |
| 295 | ++ssi; |
| 296 | } |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 297 | uint32_t dex_pc = visitor.dex_pcs_.back(); |
| 298 | visitor.dex_pcs_.pop_back(); |
Sebastien Hertz | 320deb2 | 2014-06-11 19:45:05 +0200 | [diff] [blame] | 299 | if (!isi->interpreter_entry_) { |
| 300 | instrumentation->MethodEnterEvent(thread, (*isi).this_object_, (*isi).method_, dex_pc); |
| 301 | } |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 302 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 303 | } |
| 304 | thread->VerifyStack(); |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 305 | } |
| 306 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 307 | // Removes the instrumentation exit pc as the return PC for every quick frame. |
| 308 | static void InstrumentationRestoreStack(Thread* thread, void* arg) |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 309 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 310 | struct RestoreStackVisitor : public StackVisitor { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 311 | RestoreStackVisitor(Thread* thread, uintptr_t instrumentation_exit_pc, |
| 312 | Instrumentation* instrumentation) |
| 313 | : StackVisitor(thread, NULL), thread_(thread), |
| 314 | instrumentation_exit_pc_(instrumentation_exit_pc), |
| 315 | instrumentation_(instrumentation), |
| 316 | instrumentation_stack_(thread->GetInstrumentationStack()), |
| 317 | frames_removed_(0) {} |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 318 | |
| 319 | virtual bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 320 | if (instrumentation_stack_->size() == 0) { |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 321 | return false; // Stop. |
| 322 | } |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 323 | mirror::ArtMethod* m = GetMethod(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 324 | if (GetCurrentQuickFrame() == NULL) { |
| 325 | if (kVerboseInstrumentation) { |
| 326 | LOG(INFO) << " Ignoring a shadow frame. Frame " << GetFrameId() << " Method=" << PrettyMethod(m); |
| 327 | } |
| 328 | return true; // Ignore shadow frames. |
| 329 | } |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 330 | if (m == NULL) { |
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 | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 382 | uintptr_t instrumentation_exit_pc = 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 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 392 | void Instrumentation::AddListener(InstrumentationListener* listener, uint32_t events) { |
| 393 | Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current()); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 394 | if ((events & kMethodEntered) != 0) { |
| 395 | method_entry_listeners_.push_back(listener); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 396 | have_method_entry_listeners_ = true; |
| 397 | } |
| 398 | if ((events & kMethodExited) != 0) { |
| 399 | method_exit_listeners_.push_back(listener); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 400 | have_method_exit_listeners_ = true; |
| 401 | } |
| 402 | if ((events & kMethodUnwind) != 0) { |
| 403 | method_unwind_listeners_.push_back(listener); |
| 404 | have_method_unwind_listeners_ = true; |
| 405 | } |
| 406 | if ((events & kDexPcMoved) != 0) { |
| 407 | dex_pc_listeners_.push_back(listener); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 408 | have_dex_pc_listeners_ = true; |
| 409 | } |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 410 | if ((events & kFieldRead) != 0) { |
| 411 | field_read_listeners_.push_back(listener); |
| 412 | have_field_read_listeners_ = true; |
| 413 | } |
| 414 | if ((events & kFieldWritten) != 0) { |
| 415 | field_write_listeners_.push_back(listener); |
| 416 | have_field_write_listeners_ = true; |
| 417 | } |
Jeff Hao | 14dd5a8 | 2013-04-11 10:23:36 -0700 | [diff] [blame] | 418 | if ((events & kExceptionCaught) != 0) { |
| 419 | exception_caught_listeners_.push_back(listener); |
| 420 | have_exception_caught_listeners_ = true; |
| 421 | } |
Sebastien Hertz | ee1997a | 2013-09-19 14:47:09 +0200 | [diff] [blame] | 422 | UpdateInterpreterHandlerTable(); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 423 | } |
| 424 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 425 | void Instrumentation::RemoveListener(InstrumentationListener* listener, uint32_t events) { |
| 426 | Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current()); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 427 | |
| 428 | if ((events & kMethodEntered) != 0) { |
| 429 | bool contains = std::find(method_entry_listeners_.begin(), method_entry_listeners_.end(), |
| 430 | listener) != method_entry_listeners_.end(); |
| 431 | if (contains) { |
| 432 | method_entry_listeners_.remove(listener); |
| 433 | } |
| 434 | have_method_entry_listeners_ = method_entry_listeners_.size() > 0; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 435 | } |
| 436 | if ((events & kMethodExited) != 0) { |
| 437 | bool contains = std::find(method_exit_listeners_.begin(), method_exit_listeners_.end(), |
| 438 | listener) != method_exit_listeners_.end(); |
| 439 | if (contains) { |
| 440 | method_exit_listeners_.remove(listener); |
| 441 | } |
| 442 | have_method_exit_listeners_ = method_exit_listeners_.size() > 0; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 443 | } |
| 444 | if ((events & kMethodUnwind) != 0) { |
| 445 | method_unwind_listeners_.remove(listener); |
| 446 | } |
| 447 | if ((events & kDexPcMoved) != 0) { |
| 448 | bool contains = std::find(dex_pc_listeners_.begin(), dex_pc_listeners_.end(), |
| 449 | listener) != dex_pc_listeners_.end(); |
| 450 | if (contains) { |
| 451 | dex_pc_listeners_.remove(listener); |
| 452 | } |
| 453 | have_dex_pc_listeners_ = dex_pc_listeners_.size() > 0; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 454 | } |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 455 | if ((events & kFieldRead) != 0) { |
| 456 | bool contains = std::find(field_read_listeners_.begin(), field_read_listeners_.end(), |
| 457 | listener) != field_read_listeners_.end(); |
| 458 | if (contains) { |
| 459 | field_read_listeners_.remove(listener); |
| 460 | } |
| 461 | have_field_read_listeners_ = field_read_listeners_.size() > 0; |
| 462 | } |
| 463 | if ((events & kFieldWritten) != 0) { |
| 464 | bool contains = std::find(field_write_listeners_.begin(), field_write_listeners_.end(), |
| 465 | listener) != field_write_listeners_.end(); |
| 466 | if (contains) { |
| 467 | field_write_listeners_.remove(listener); |
| 468 | } |
| 469 | have_field_write_listeners_ = field_write_listeners_.size() > 0; |
| 470 | } |
Jeff Hao | 14dd5a8 | 2013-04-11 10:23:36 -0700 | [diff] [blame] | 471 | if ((events & kExceptionCaught) != 0) { |
| 472 | exception_caught_listeners_.remove(listener); |
| 473 | have_exception_caught_listeners_ = exception_caught_listeners_.size() > 0; |
| 474 | } |
Sebastien Hertz | ee1997a | 2013-09-19 14:47:09 +0200 | [diff] [blame] | 475 | UpdateInterpreterHandlerTable(); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 476 | } |
| 477 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 478 | void Instrumentation::ConfigureStubs(bool require_entry_exit_stubs, bool require_interpreter) { |
| 479 | interpret_only_ = require_interpreter || forced_interpret_only_; |
| 480 | // Compute what level of instrumentation is required and compare to current. |
| 481 | int desired_level, current_level; |
| 482 | if (require_interpreter) { |
| 483 | desired_level = 2; |
| 484 | } else if (require_entry_exit_stubs) { |
| 485 | desired_level = 1; |
| 486 | } else { |
| 487 | desired_level = 0; |
| 488 | } |
| 489 | if (interpreter_stubs_installed_) { |
| 490 | current_level = 2; |
| 491 | } else if (entry_exit_stubs_installed_) { |
| 492 | current_level = 1; |
| 493 | } else { |
| 494 | current_level = 0; |
| 495 | } |
| 496 | if (desired_level == current_level) { |
| 497 | // We're already set. |
| 498 | return; |
| 499 | } |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 500 | Thread* const self = Thread::Current(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 501 | Runtime* runtime = Runtime::Current(); |
| 502 | Locks::thread_list_lock_->AssertNotHeld(self); |
| 503 | if (desired_level > 0) { |
| 504 | if (require_interpreter) { |
| 505 | interpreter_stubs_installed_ = true; |
| 506 | } else { |
| 507 | CHECK(require_entry_exit_stubs); |
| 508 | entry_exit_stubs_installed_ = true; |
| 509 | } |
| 510 | runtime->GetClassLinker()->VisitClasses(InstallStubsClassVisitor, this); |
| 511 | instrumentation_stubs_installed_ = true; |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 512 | MutexLock mu(self, *Locks::thread_list_lock_); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 513 | runtime->GetThreadList()->ForEach(InstrumentationInstallStack, this); |
| 514 | } else { |
| 515 | interpreter_stubs_installed_ = false; |
| 516 | entry_exit_stubs_installed_ = false; |
| 517 | runtime->GetClassLinker()->VisitClasses(InstallStubsClassVisitor, this); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 518 | // Restore stack only if there is no method currently deoptimized. |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 519 | bool empty; |
| 520 | { |
| 521 | ReaderMutexLock mu(self, deoptimized_methods_lock_); |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame^] | 522 | empty = IsDeoptimizedMethodsEmpty(); // Avoid lock violation. |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 523 | } |
| 524 | if (empty) { |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 525 | instrumentation_stubs_installed_ = false; |
| 526 | MutexLock mu(self, *Locks::thread_list_lock_); |
| 527 | Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this); |
| 528 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 529 | } |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 530 | } |
| 531 | |
Ian Rogers | fa82427 | 2013-11-05 16:12:57 -0800 | [diff] [blame] | 532 | static void ResetQuickAllocEntryPointsForThread(Thread* thread, void* arg) { |
| 533 | thread->ResetQuickAllocEntryPointsForThread(); |
| 534 | } |
| 535 | |
Mathieu Chartier | 661974a | 2014-01-09 11:23:53 -0800 | [diff] [blame] | 536 | void Instrumentation::SetEntrypointsInstrumented(bool instrumented) { |
| 537 | Runtime* runtime = Runtime::Current(); |
| 538 | ThreadList* tl = runtime->GetThreadList(); |
| 539 | if (runtime->IsStarted()) { |
| 540 | tl->SuspendAll(); |
| 541 | } |
| 542 | { |
| 543 | MutexLock mu(Thread::Current(), *Locks::runtime_shutdown_lock_); |
| 544 | SetQuickAllocEntryPointsInstrumented(instrumented); |
| 545 | ResetQuickAllocEntryPoints(); |
| 546 | } |
| 547 | if (runtime->IsStarted()) { |
| 548 | tl->ResumeAll(); |
| 549 | } |
| 550 | } |
| 551 | |
Ian Rogers | fa82427 | 2013-11-05 16:12:57 -0800 | [diff] [blame] | 552 | void Instrumentation::InstrumentQuickAllocEntryPoints() { |
| 553 | // TODO: the read of quick_alloc_entry_points_instrumentation_counter_ is racey and this code |
| 554 | // should be guarded by a lock. |
Ian Rogers | 3e5cf30 | 2014-05-20 16:40:37 -0700 | [diff] [blame] | 555 | DCHECK_GE(quick_alloc_entry_points_instrumentation_counter_.LoadSequentiallyConsistent(), 0); |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 556 | const bool enable_instrumentation = |
Ian Rogers | 3e5cf30 | 2014-05-20 16:40:37 -0700 | [diff] [blame] | 557 | quick_alloc_entry_points_instrumentation_counter_.FetchAndAddSequentiallyConsistent(1) == 0; |
Ian Rogers | fa82427 | 2013-11-05 16:12:57 -0800 | [diff] [blame] | 558 | if (enable_instrumentation) { |
Mathieu Chartier | 661974a | 2014-01-09 11:23:53 -0800 | [diff] [blame] | 559 | SetEntrypointsInstrumented(true); |
Ian Rogers | fa82427 | 2013-11-05 16:12:57 -0800 | [diff] [blame] | 560 | } |
| 561 | } |
| 562 | |
| 563 | void Instrumentation::UninstrumentQuickAllocEntryPoints() { |
| 564 | // TODO: the read of quick_alloc_entry_points_instrumentation_counter_ is racey and this code |
| 565 | // should be guarded by a lock. |
Ian Rogers | 3e5cf30 | 2014-05-20 16:40:37 -0700 | [diff] [blame] | 566 | DCHECK_GT(quick_alloc_entry_points_instrumentation_counter_.LoadSequentiallyConsistent(), 0); |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 567 | const bool disable_instrumentation = |
Ian Rogers | 3e5cf30 | 2014-05-20 16:40:37 -0700 | [diff] [blame] | 568 | quick_alloc_entry_points_instrumentation_counter_.FetchAndSubSequentiallyConsistent(1) == 1; |
Ian Rogers | fa82427 | 2013-11-05 16:12:57 -0800 | [diff] [blame] | 569 | if (disable_instrumentation) { |
Mathieu Chartier | 661974a | 2014-01-09 11:23:53 -0800 | [diff] [blame] | 570 | SetEntrypointsInstrumented(false); |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 571 | } |
| 572 | } |
| 573 | |
| 574 | void Instrumentation::ResetQuickAllocEntryPoints() { |
| 575 | Runtime* runtime = Runtime::Current(); |
| 576 | if (runtime->IsStarted()) { |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 577 | MutexLock mu(Thread::Current(), *Locks::thread_list_lock_); |
| 578 | runtime->GetThreadList()->ForEach(ResetQuickAllocEntryPointsForThread, NULL); |
Ian Rogers | fa82427 | 2013-11-05 16:12:57 -0800 | [diff] [blame] | 579 | } |
| 580 | } |
| 581 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 582 | void Instrumentation::UpdateMethodsCode(mirror::ArtMethod* method, const void* quick_code, |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame^] | 583 | const void* portable_code, bool have_portable_code) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 584 | const void* new_portable_code; |
| 585 | const void* new_quick_code; |
| 586 | bool new_have_portable_code; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 587 | if (LIKELY(!instrumentation_stubs_installed_)) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 588 | new_portable_code = portable_code; |
| 589 | new_quick_code = quick_code; |
| 590 | new_have_portable_code = have_portable_code; |
Jeff Hao | 65d15d9 | 2013-07-16 16:39:33 -0700 | [diff] [blame] | 591 | } else { |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 592 | if ((interpreter_stubs_installed_ || IsDeoptimized(method)) && !method->IsNative()) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 593 | new_portable_code = GetPortableToInterpreterBridge(); |
| 594 | new_quick_code = GetQuickToInterpreterBridge(); |
| 595 | new_have_portable_code = false; |
Jeff Hao | 65d15d9 | 2013-07-16 16:39:33 -0700 | [diff] [blame] | 596 | } else { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 597 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 598 | if (quick_code == class_linker->GetQuickResolutionTrampoline() || |
| 599 | quick_code == class_linker->GetQuickToInterpreterBridgeTrampoline() || |
| 600 | quick_code == GetQuickToInterpreterBridge()) { |
| 601 | DCHECK((portable_code == class_linker->GetPortableResolutionTrampoline()) || |
| 602 | (portable_code == GetPortableToInterpreterBridge())); |
| 603 | new_portable_code = portable_code; |
| 604 | new_quick_code = quick_code; |
| 605 | new_have_portable_code = have_portable_code; |
| 606 | } else if (entry_exit_stubs_installed_) { |
| 607 | new_quick_code = GetQuickInstrumentationEntryPoint(); |
| 608 | new_portable_code = GetPortableToInterpreterBridge(); |
| 609 | new_have_portable_code = false; |
| 610 | } else { |
| 611 | new_portable_code = portable_code; |
| 612 | new_quick_code = quick_code; |
| 613 | new_have_portable_code = have_portable_code; |
| 614 | } |
Jeff Hao | 65d15d9 | 2013-07-16 16:39:33 -0700 | [diff] [blame] | 615 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 616 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 617 | UpdateEntrypoints(method, new_quick_code, new_portable_code, new_have_portable_code); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 618 | } |
| 619 | |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame^] | 620 | bool Instrumentation::AddDeoptimizedMethod(mirror::ArtMethod* method) { |
| 621 | // Note that the insert() below isn't read barrier-aware. So, this |
| 622 | // FindDeoptimizedMethod() call is necessary or else we would end up |
| 623 | // storing the same method twice in the map (the from-space and the |
| 624 | // to-space ones). |
| 625 | if (FindDeoptimizedMethod(method)) { |
| 626 | // Already in the map. Return. |
| 627 | return false; |
| 628 | } |
| 629 | // Not found. Add it. |
| 630 | int32_t hash_code = method->IdentityHashCode(); |
| 631 | deoptimized_methods_.insert(std::make_pair(hash_code, method)); |
| 632 | return true; |
| 633 | } |
| 634 | |
| 635 | bool Instrumentation::FindDeoptimizedMethod(mirror::ArtMethod* method) { |
| 636 | int32_t hash_code = method->IdentityHashCode(); |
| 637 | auto range = deoptimized_methods_.equal_range(hash_code); |
| 638 | for (auto it = range.first; it != range.second; ++it) { |
| 639 | mirror::ArtMethod** root = &it->second; |
| 640 | mirror::ArtMethod* m = ReadBarrier::BarrierForRoot<mirror::ArtMethod>(root); |
| 641 | if (m == method) { |
| 642 | // Found. |
| 643 | return true; |
| 644 | } |
| 645 | } |
| 646 | // Not found. |
| 647 | return false; |
| 648 | } |
| 649 | |
| 650 | mirror::ArtMethod* Instrumentation::BeginDeoptimizedMethod() { |
| 651 | auto it = deoptimized_methods_.begin(); |
| 652 | if (it == deoptimized_methods_.end()) { |
| 653 | // Empty. |
| 654 | return nullptr; |
| 655 | } |
| 656 | mirror::ArtMethod** root = &it->second; |
| 657 | return ReadBarrier::BarrierForRoot<mirror::ArtMethod>(root); |
| 658 | } |
| 659 | |
| 660 | bool Instrumentation::RemoveDeoptimizedMethod(mirror::ArtMethod* method) { |
| 661 | int32_t hash_code = method->IdentityHashCode(); |
| 662 | auto range = deoptimized_methods_.equal_range(hash_code); |
| 663 | for (auto it = range.first; it != range.second; ++it) { |
| 664 | mirror::ArtMethod** root = &it->second; |
| 665 | mirror::ArtMethod* m = ReadBarrier::BarrierForRoot<mirror::ArtMethod>(root); |
| 666 | if (m == method) { |
| 667 | // Found. Erase and return. |
| 668 | deoptimized_methods_.erase(it); |
| 669 | return true; |
| 670 | } |
| 671 | } |
| 672 | // Not found. |
| 673 | return false; |
| 674 | } |
| 675 | |
| 676 | bool Instrumentation::IsDeoptimizedMethodsEmpty() const { |
| 677 | return deoptimized_methods_.empty(); |
| 678 | } |
| 679 | |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 680 | void Instrumentation::Deoptimize(mirror::ArtMethod* method) { |
| 681 | CHECK(!method->IsNative()); |
| 682 | CHECK(!method->IsProxyMethod()); |
| 683 | CHECK(!method->IsAbstract()); |
| 684 | |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 685 | Thread* self = Thread::Current(); |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 686 | { |
| 687 | WriterMutexLock mu(self, deoptimized_methods_lock_); |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame^] | 688 | bool has_not_been_deoptimized = AddDeoptimizedMethod(method); |
| 689 | CHECK(has_not_been_deoptimized) << "Method " << PrettyMethod(method) << " is already deoptimized"; |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 690 | } |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 691 | if (!interpreter_stubs_installed_) { |
Sebastien Hertz | 320deb2 | 2014-06-11 19:45:05 +0200 | [diff] [blame] | 692 | UpdateEntrypoints(method, GetQuickInstrumentationEntryPoint(), GetPortableToInterpreterBridge(), |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 693 | false); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 694 | |
| 695 | // Install instrumentation exit stub and instrumentation frames. We may already have installed |
| 696 | // these previously so it will only cover the newly created frames. |
| 697 | instrumentation_stubs_installed_ = true; |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 698 | MutexLock mu(self, *Locks::thread_list_lock_); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 699 | Runtime::Current()->GetThreadList()->ForEach(InstrumentationInstallStack, this); |
| 700 | } |
| 701 | } |
| 702 | |
| 703 | void Instrumentation::Undeoptimize(mirror::ArtMethod* method) { |
| 704 | CHECK(!method->IsNative()); |
| 705 | CHECK(!method->IsProxyMethod()); |
| 706 | CHECK(!method->IsAbstract()); |
| 707 | |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 708 | Thread* self = Thread::Current(); |
| 709 | bool empty; |
| 710 | { |
| 711 | WriterMutexLock mu(self, deoptimized_methods_lock_); |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame^] | 712 | bool found_and_erased = RemoveDeoptimizedMethod(method); |
| 713 | CHECK(found_and_erased) << "Method " << PrettyMethod(method) |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 714 | << " is not deoptimized"; |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame^] | 715 | empty = IsDeoptimizedMethodsEmpty(); |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 716 | } |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 717 | |
| 718 | // Restore code and possibly stack only if we did not deoptimize everything. |
| 719 | if (!interpreter_stubs_installed_) { |
| 720 | // Restore its code or resolution trampoline. |
| 721 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 722 | if (method->IsStatic() && !method->IsConstructor() && |
| 723 | !method->GetDeclaringClass()->IsInitialized()) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 724 | // TODO: we're updating to entrypoints in the image here, we can avoid the trampoline. |
| 725 | UpdateEntrypoints(method, class_linker->GetQuickResolutionTrampoline(), |
| 726 | class_linker->GetPortableResolutionTrampoline(), false); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 727 | } else { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 728 | bool have_portable_code = false; |
| 729 | const void* quick_code = class_linker->GetQuickOatCodeFor(method); |
| 730 | const void* portable_code = class_linker->GetPortableOatCodeFor(method, &have_portable_code); |
| 731 | UpdateEntrypoints(method, quick_code, portable_code, have_portable_code); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | // 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] | 735 | if (empty) { |
| 736 | MutexLock mu(self, *Locks::thread_list_lock_); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 737 | Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this); |
| 738 | instrumentation_stubs_installed_ = false; |
| 739 | } |
| 740 | } |
| 741 | } |
| 742 | |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame^] | 743 | bool Instrumentation::IsDeoptimized(mirror::ArtMethod* method) { |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 744 | DCHECK(method != nullptr); |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame^] | 745 | ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_); |
| 746 | return FindDeoptimizedMethod(method); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 747 | } |
| 748 | |
| 749 | void Instrumentation::EnableDeoptimization() { |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 750 | ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_); |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame^] | 751 | CHECK(IsDeoptimizedMethodsEmpty()); |
Sebastien Hertz | 11d40c2 | 2014-02-19 18:00:17 +0100 | [diff] [blame] | 752 | CHECK_EQ(deoptimization_enabled_, false); |
| 753 | deoptimization_enabled_ = true; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | void Instrumentation::DisableDeoptimization() { |
Sebastien Hertz | 11d40c2 | 2014-02-19 18:00:17 +0100 | [diff] [blame] | 757 | CHECK_EQ(deoptimization_enabled_, true); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 758 | // If we deoptimized everything, undo it. |
| 759 | if (interpreter_stubs_installed_) { |
| 760 | UndeoptimizeEverything(); |
| 761 | } |
| 762 | // Undeoptimized selected methods. |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 763 | while (true) { |
| 764 | mirror::ArtMethod* method; |
| 765 | { |
| 766 | ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_); |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame^] | 767 | if (IsDeoptimizedMethodsEmpty()) { |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 768 | break; |
| 769 | } |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame^] | 770 | method = BeginDeoptimizedMethod(); |
| 771 | CHECK(method != nullptr); |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 772 | } |
| 773 | Undeoptimize(method); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 774 | } |
Sebastien Hertz | 11d40c2 | 2014-02-19 18:00:17 +0100 | [diff] [blame] | 775 | deoptimization_enabled_ = false; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 776 | } |
| 777 | |
Sebastien Hertz | 11d40c2 | 2014-02-19 18:00:17 +0100 | [diff] [blame] | 778 | // Indicates if instrumentation should notify method enter/exit events to the listeners. |
| 779 | bool Instrumentation::ShouldNotifyMethodEnterExitEvents() const { |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 780 | return !deoptimization_enabled_ && !interpreter_stubs_installed_; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | void Instrumentation::DeoptimizeEverything() { |
| 784 | CHECK(!interpreter_stubs_installed_); |
| 785 | ConfigureStubs(false, true); |
| 786 | } |
| 787 | |
| 788 | void Instrumentation::UndeoptimizeEverything() { |
| 789 | CHECK(interpreter_stubs_installed_); |
| 790 | ConfigureStubs(false, false); |
| 791 | } |
| 792 | |
| 793 | void Instrumentation::EnableMethodTracing() { |
| 794 | bool require_interpreter = kDeoptimizeForAccurateMethodEntryExitListeners; |
| 795 | ConfigureStubs(!require_interpreter, require_interpreter); |
| 796 | } |
| 797 | |
| 798 | void Instrumentation::DisableMethodTracing() { |
| 799 | ConfigureStubs(false, false); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 800 | } |
| 801 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 802 | const void* Instrumentation::GetQuickCodeFor(mirror::ArtMethod* method) const { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 803 | Runtime* runtime = Runtime::Current(); |
| 804 | if (LIKELY(!instrumentation_stubs_installed_)) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 805 | const void* code = method->GetEntryPointFromQuickCompiledCode(); |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 806 | DCHECK(code != nullptr); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 807 | ClassLinker* class_linker = runtime->GetClassLinker(); |
| 808 | if (LIKELY(code != class_linker->GetQuickResolutionTrampoline()) && |
| 809 | LIKELY(code != class_linker->GetQuickToInterpreterBridgeTrampoline()) && |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 810 | LIKELY(code != GetQuickToInterpreterBridge())) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 811 | return code; |
| 812 | } |
| 813 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 814 | return runtime->GetClassLinker()->GetQuickOatCodeFor(method); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 815 | } |
| 816 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 817 | void Instrumentation::MethodEnterEventImpl(Thread* thread, mirror::Object* this_object, |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 818 | mirror::ArtMethod* method, |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 819 | uint32_t dex_pc) const { |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 820 | auto it = method_entry_listeners_.begin(); |
Jeff Hao | 65d15d9 | 2013-07-16 16:39:33 -0700 | [diff] [blame] | 821 | bool is_end = (it == method_entry_listeners_.end()); |
| 822 | // Implemented this way to prevent problems caused by modification of the list while iterating. |
| 823 | while (!is_end) { |
| 824 | InstrumentationListener* cur = *it; |
| 825 | ++it; |
| 826 | is_end = (it == method_entry_listeners_.end()); |
| 827 | cur->MethodEntered(thread, this_object, method, dex_pc); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 828 | } |
| 829 | } |
| 830 | |
| 831 | void Instrumentation::MethodExitEventImpl(Thread* thread, mirror::Object* this_object, |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 832 | mirror::ArtMethod* method, |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 833 | uint32_t dex_pc, const JValue& return_value) const { |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 834 | auto it = method_exit_listeners_.begin(); |
Jeff Hao | 65d15d9 | 2013-07-16 16:39:33 -0700 | [diff] [blame] | 835 | bool is_end = (it == method_exit_listeners_.end()); |
| 836 | // Implemented this way to prevent problems caused by modification of the list while iterating. |
| 837 | while (!is_end) { |
| 838 | InstrumentationListener* cur = *it; |
| 839 | ++it; |
| 840 | is_end = (it == method_exit_listeners_.end()); |
| 841 | cur->MethodExited(thread, this_object, method, dex_pc, return_value); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 842 | } |
| 843 | } |
| 844 | |
| 845 | void Instrumentation::MethodUnwindEvent(Thread* thread, mirror::Object* this_object, |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 846 | mirror::ArtMethod* method, |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 847 | uint32_t dex_pc) const { |
| 848 | if (have_method_unwind_listeners_) { |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 849 | for (InstrumentationListener* listener : method_unwind_listeners_) { |
Sebastien Hertz | 51db44a | 2013-11-19 10:00:29 +0100 | [diff] [blame] | 850 | listener->MethodUnwind(thread, this_object, method, dex_pc); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 851 | } |
| 852 | } |
| 853 | } |
| 854 | |
| 855 | void Instrumentation::DexPcMovedEventImpl(Thread* thread, mirror::Object* this_object, |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 856 | mirror::ArtMethod* method, |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 857 | uint32_t dex_pc) const { |
| 858 | // TODO: STL copy-on-write collection? The copy below is due to the debug listener having an |
| 859 | // action where it can remove itself as a listener and break the iterator. The copy only works |
| 860 | // around the problem and in general we may have to move to something like reference counting to |
| 861 | // ensure listeners are deleted correctly. |
| 862 | std::list<InstrumentationListener*> copy(dex_pc_listeners_); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 863 | for (InstrumentationListener* listener : copy) { |
| 864 | listener->DexPcMoved(thread, this_object, method, dex_pc); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 865 | } |
| 866 | } |
| 867 | |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 868 | void Instrumentation::FieldReadEventImpl(Thread* thread, mirror::Object* this_object, |
| 869 | mirror::ArtMethod* method, uint32_t dex_pc, |
| 870 | mirror::ArtField* field) const { |
Sebastien Hertz | 9f10203 | 2014-05-23 08:59:42 +0200 | [diff] [blame] | 871 | // TODO: same comment than DexPcMovedEventImpl. |
| 872 | std::list<InstrumentationListener*> copy(field_read_listeners_); |
| 873 | for (InstrumentationListener* listener : copy) { |
| 874 | listener->FieldRead(thread, this_object, method, dex_pc, field); |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 875 | } |
| 876 | } |
| 877 | |
| 878 | void Instrumentation::FieldWriteEventImpl(Thread* thread, mirror::Object* this_object, |
| 879 | mirror::ArtMethod* method, uint32_t dex_pc, |
| 880 | mirror::ArtField* field, const JValue& field_value) const { |
Sebastien Hertz | 9f10203 | 2014-05-23 08:59:42 +0200 | [diff] [blame] | 881 | // TODO: same comment than DexPcMovedEventImpl. |
| 882 | std::list<InstrumentationListener*> copy(field_write_listeners_); |
| 883 | for (InstrumentationListener* listener : copy) { |
| 884 | listener->FieldWritten(thread, this_object, method, dex_pc, field, field_value); |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 885 | } |
| 886 | } |
| 887 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 888 | void Instrumentation::ExceptionCaughtEvent(Thread* thread, const ThrowLocation& throw_location, |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 889 | mirror::ArtMethod* catch_method, |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 890 | uint32_t catch_dex_pc, |
Sebastien Hertz | 947ff08 | 2013-09-17 14:10:13 +0200 | [diff] [blame] | 891 | mirror::Throwable* exception_object) const { |
Sebastien Hertz | 9f10203 | 2014-05-23 08:59:42 +0200 | [diff] [blame] | 892 | if (HasExceptionCaughtListeners()) { |
| 893 | DCHECK_EQ(thread->GetException(nullptr), exception_object); |
| 894 | bool is_exception_reported = thread->IsExceptionReportedToInstrumentation(); |
Jeff Hao | c0bd4da | 2013-04-11 15:52:28 -0700 | [diff] [blame] | 895 | thread->ClearException(); |
Sebastien Hertz | bf079fe | 2014-04-01 15:31:05 +0200 | [diff] [blame] | 896 | // TODO: The copy below is due to the debug listener having an action where it can remove |
| 897 | // itself as a listener and break the iterator. The copy only works around the problem. |
| 898 | std::list<InstrumentationListener*> copy(exception_caught_listeners_); |
| 899 | for (InstrumentationListener* listener : copy) { |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 900 | listener->ExceptionCaught(thread, throw_location, catch_method, catch_dex_pc, exception_object); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 901 | } |
Jeff Hao | c0bd4da | 2013-04-11 15:52:28 -0700 | [diff] [blame] | 902 | thread->SetException(throw_location, exception_object); |
Sebastien Hertz | 9f10203 | 2014-05-23 08:59:42 +0200 | [diff] [blame] | 903 | thread->SetExceptionReportedToInstrumentation(is_exception_reported); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 904 | } |
| 905 | } |
| 906 | |
| 907 | static void CheckStackDepth(Thread* self, const InstrumentationStackFrame& instrumentation_frame, |
| 908 | int delta) |
| 909 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 910 | size_t frame_id = StackVisitor::ComputeNumFrames(self) + delta; |
| 911 | if (frame_id != instrumentation_frame.frame_id_) { |
| 912 | LOG(ERROR) << "Expected frame_id=" << frame_id << " but found " |
| 913 | << instrumentation_frame.frame_id_; |
| 914 | StackVisitor::DescribeStack(self); |
| 915 | CHECK_EQ(frame_id, instrumentation_frame.frame_id_); |
| 916 | } |
| 917 | } |
| 918 | |
| 919 | void Instrumentation::PushInstrumentationStackFrame(Thread* self, mirror::Object* this_object, |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 920 | mirror::ArtMethod* method, |
Jeff Hao | 9a916d3 | 2013-06-27 18:45:37 -0700 | [diff] [blame] | 921 | uintptr_t lr, bool interpreter_entry) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 922 | // We have a callee-save frame meaning this value is guaranteed to never be 0. |
| 923 | size_t frame_id = StackVisitor::ComputeNumFrames(self); |
| 924 | std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack(); |
| 925 | if (kVerboseInstrumentation) { |
Brian Carlstrom | 2d88862 | 2013-07-18 17:02:00 -0700 | [diff] [blame] | 926 | LOG(INFO) << "Entering " << PrettyMethod(method) << " from PC " << reinterpret_cast<void*>(lr); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 927 | } |
| 928 | instrumentation::InstrumentationStackFrame instrumentation_frame(this_object, method, lr, |
Jeff Hao | 9a916d3 | 2013-06-27 18:45:37 -0700 | [diff] [blame] | 929 | frame_id, interpreter_entry); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 930 | stack->push_front(instrumentation_frame); |
| 931 | |
Sebastien Hertz | 320deb2 | 2014-06-11 19:45:05 +0200 | [diff] [blame] | 932 | if (!interpreter_entry) { |
| 933 | MethodEnterEvent(self, this_object, method, 0); |
| 934 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 935 | } |
| 936 | |
Andreas Gampe | d58342c | 2014-06-05 14:18:08 -0700 | [diff] [blame] | 937 | TwoWordReturn Instrumentation::PopInstrumentationStackFrame(Thread* self, uintptr_t* return_pc, |
| 938 | uint64_t gpr_result, |
| 939 | uint64_t fpr_result) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 940 | // Do the pop. |
| 941 | std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack(); |
| 942 | CHECK_GT(stack->size(), 0U); |
| 943 | InstrumentationStackFrame instrumentation_frame = stack->front(); |
| 944 | stack->pop_front(); |
| 945 | |
| 946 | // Set return PC and check the sanity of the stack. |
| 947 | *return_pc = instrumentation_frame.return_pc_; |
| 948 | CheckStackDepth(self, instrumentation_frame, 0); |
| 949 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 950 | mirror::ArtMethod* method = instrumentation_frame.method_; |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 951 | uint32_t length; |
| 952 | char return_shorty = method->GetShorty(&length)[0]; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 953 | JValue return_value; |
| 954 | if (return_shorty == 'V') { |
| 955 | return_value.SetJ(0); |
| 956 | } else if (return_shorty == 'F' || return_shorty == 'D') { |
| 957 | return_value.SetJ(fpr_result); |
| 958 | } else { |
| 959 | return_value.SetJ(gpr_result); |
| 960 | } |
| 961 | // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to |
| 962 | // return_pc. |
| 963 | uint32_t dex_pc = DexFile::kDexNoIndex; |
| 964 | mirror::Object* this_object = instrumentation_frame.this_object_; |
Sebastien Hertz | 320deb2 | 2014-06-11 19:45:05 +0200 | [diff] [blame] | 965 | if (!instrumentation_frame.interpreter_entry_) { |
| 966 | MethodExitEvent(self, this_object, instrumentation_frame.method_, dex_pc, return_value); |
| 967 | } |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 968 | |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 969 | // Deoptimize if the caller needs to continue execution in the interpreter. Do nothing if we get |
| 970 | // back to an upcall. |
| 971 | NthCallerVisitor visitor(self, 1, true); |
| 972 | visitor.WalkStack(true); |
| 973 | bool deoptimize = (visitor.caller != NULL) && |
| 974 | (interpreter_stubs_installed_ || IsDeoptimized(visitor.caller)); |
| 975 | if (deoptimize && kVerboseInstrumentation) { |
| 976 | LOG(INFO) << "Deoptimizing into " << PrettyMethod(visitor.caller); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 977 | } |
| 978 | if (deoptimize) { |
| 979 | if (kVerboseInstrumentation) { |
| 980 | LOG(INFO) << "Deoptimizing from " << PrettyMethod(method) |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 981 | << " result is " << std::hex << return_value.GetJ(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 982 | } |
| 983 | self->SetDeoptimizationReturnValue(return_value); |
Andreas Gampe | d58342c | 2014-06-05 14:18:08 -0700 | [diff] [blame] | 984 | return GetTwoWordSuccessValue(*return_pc, |
| 985 | reinterpret_cast<uintptr_t>(GetQuickDeoptimizationEntryPoint())); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 986 | } else { |
| 987 | if (kVerboseInstrumentation) { |
Brian Carlstrom | 2d88862 | 2013-07-18 17:02:00 -0700 | [diff] [blame] | 988 | LOG(INFO) << "Returning from " << PrettyMethod(method) |
| 989 | << " to PC " << reinterpret_cast<void*>(*return_pc); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 990 | } |
Andreas Gampe | d58342c | 2014-06-05 14:18:08 -0700 | [diff] [blame] | 991 | return GetTwoWordSuccessValue(0, *return_pc); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 992 | } |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 993 | } |
| 994 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 995 | void Instrumentation::PopMethodForUnwind(Thread* self, bool is_deoptimization) const { |
| 996 | // Do the pop. |
| 997 | std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack(); |
| 998 | CHECK_GT(stack->size(), 0U); |
| 999 | InstrumentationStackFrame instrumentation_frame = stack->front(); |
| 1000 | // TODO: bring back CheckStackDepth(self, instrumentation_frame, 2); |
| 1001 | stack->pop_front(); |
| 1002 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1003 | mirror::ArtMethod* method = instrumentation_frame.method_; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1004 | if (is_deoptimization) { |
| 1005 | if (kVerboseInstrumentation) { |
| 1006 | LOG(INFO) << "Popping for deoptimization " << PrettyMethod(method); |
| 1007 | } |
| 1008 | } else { |
| 1009 | if (kVerboseInstrumentation) { |
| 1010 | LOG(INFO) << "Popping for unwind " << PrettyMethod(method); |
| 1011 | } |
| 1012 | |
| 1013 | // Notify listeners of method unwind. |
| 1014 | // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to |
| 1015 | // return_pc. |
| 1016 | uint32_t dex_pc = DexFile::kDexNoIndex; |
| 1017 | MethodUnwindEvent(self, instrumentation_frame.this_object_, method, dex_pc); |
| 1018 | } |
| 1019 | } |
| 1020 | |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 1021 | void Instrumentation::VisitRoots(RootCallback* callback, void* arg) { |
| 1022 | WriterMutexLock mu(Thread::Current(), deoptimized_methods_lock_); |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame^] | 1023 | if (IsDeoptimizedMethodsEmpty()) { |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 1024 | return; |
| 1025 | } |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame^] | 1026 | for (auto pair : deoptimized_methods_) { |
| 1027 | mirror::ArtMethod** root = &pair.second; |
| 1028 | callback(reinterpret_cast<mirror::Object**>(root), arg, 0, kRootVMInternal); |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 1029 | } |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 1030 | } |
| 1031 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1032 | std::string InstrumentationStackFrame::Dump() const { |
| 1033 | std::ostringstream os; |
| 1034 | os << "Frame " << frame_id_ << " " << PrettyMethod(method_) << ":" |
| 1035 | << reinterpret_cast<void*>(return_pc_) << " this=" << reinterpret_cast<void*>(this_object_); |
| 1036 | return os.str(); |
| 1037 | } |
| 1038 | |
| 1039 | } // namespace instrumentation |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 1040 | } // namespace art |