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