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