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