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