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