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 | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 21 | #include "atomic_integer.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" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 26 | #include "mirror/class-inl.h" |
| 27 | #include "mirror/dex_cache.h" |
| 28 | #include "mirror/abstract_method-inl.h" |
| 29 | #include "mirror/object_array-inl.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 30 | #include "mirror/object-inl.h" |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 31 | #include "nth_caller_visitor.h" |
Ian Rogers | c928de9 | 2013-02-27 14:30:44 -0800 | [diff] [blame] | 32 | #if !defined(ART_USE_PORTABLE_COMPILER) |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 33 | #include "oat/runtime/oat_support_entrypoints.h" |
| 34 | #endif |
| 35 | #include "object_utils.h" |
| 36 | #include "os.h" |
| 37 | #include "scoped_thread_state_change.h" |
| 38 | #include "thread.h" |
| 39 | #include "thread_list.h" |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 40 | |
| 41 | namespace art { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 42 | namespace instrumentation { |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 43 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 44 | static bool InstallStubsClassVisitor(mirror::Class* klass, void* arg) |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 45 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 46 | Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg); |
| 47 | return instrumentation->InstallStubsForClass(klass); |
| 48 | } |
| 49 | |
| 50 | bool Instrumentation::InstallStubsForClass(mirror::Class* klass) { |
| 51 | bool uninstall = !entry_exit_stubs_installed_ && !interpreter_stubs_installed_; |
| 52 | ClassLinker* class_linker = NULL; |
| 53 | if (uninstall) { |
| 54 | class_linker = Runtime::Current()->GetClassLinker(); |
| 55 | } |
| 56 | bool is_initialized = klass->IsInitialized(); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 57 | for (size_t i = 0; i < klass->NumDirectMethods(); i++) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 58 | mirror::AbstractMethod* method = klass->GetDirectMethod(i); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 59 | if (!method->IsAbstract()) { |
| 60 | const void* new_code; |
| 61 | if (uninstall) { |
| 62 | if (is_initialized || !method->IsStatic() || method->IsConstructor()) { |
| 63 | new_code = class_linker->GetOatCodeFor(method); |
| 64 | } else { |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 65 | new_code = GetResolutionTrampoline(class_linker); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 66 | } |
| 67 | } else { // !uninstall |
| 68 | if (!interpreter_stubs_installed_ || method->IsNative()) { |
| 69 | new_code = GetInstrumentationEntryPoint(); |
| 70 | } else { |
| 71 | new_code = GetInterpreterEntryPoint(); |
| 72 | } |
| 73 | } |
Jeff Hao | aa4a793 | 2013-05-13 11:28:27 -0700 | [diff] [blame] | 74 | method->SetEntryPointFromCompiledCode(new_code); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 75 | } |
| 76 | } |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 77 | for (size_t i = 0; i < klass->NumVirtualMethods(); i++) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 78 | mirror::AbstractMethod* method = klass->GetVirtualMethod(i); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 79 | if (!method->IsAbstract()) { |
| 80 | const void* new_code; |
| 81 | if (uninstall) { |
| 82 | new_code = class_linker->GetOatCodeFor(method); |
| 83 | } else { // !uninstall |
| 84 | if (!interpreter_stubs_installed_ || method->IsNative()) { |
| 85 | new_code = GetInstrumentationEntryPoint(); |
| 86 | } else { |
| 87 | new_code = GetInterpreterEntryPoint(); |
| 88 | } |
| 89 | } |
Jeff Hao | aa4a793 | 2013-05-13 11:28:27 -0700 | [diff] [blame] | 90 | method->SetEntryPointFromCompiledCode(new_code); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | return true; |
| 94 | } |
| 95 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 96 | // Places the instrumentation exit pc as the return PC for every quick frame. This also allows |
| 97 | // deoptimization of quick frames to interpreter frames. |
| 98 | static void InstrumentationInstallStack(Thread* thread, void* arg) |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 99 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 100 | struct InstallStackVisitor : public StackVisitor { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 101 | InstallStackVisitor(Thread* thread, Context* context, uintptr_t instrumentation_exit_pc) |
| 102 | : StackVisitor(thread, context), instrumentation_stack_(thread->GetInstrumentationStack()), |
| 103 | instrumentation_exit_pc_(instrumentation_exit_pc), last_return_pc_(0) {} |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 104 | |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 105 | virtual bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 106 | mirror::AbstractMethod* m = GetMethod(); |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 107 | if (GetCurrentQuickFrame() == NULL) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 108 | if (kVerboseInstrumentation) { |
| 109 | LOG(INFO) << " Ignoring a shadow frame. Frame " << GetFrameId() |
| 110 | << " Method=" << PrettyMethod(m); |
| 111 | } |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 112 | return true; // Ignore shadow frames. |
| 113 | } |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 114 | if (m == NULL) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 115 | if (kVerboseInstrumentation) { |
| 116 | LOG(INFO) << " Skipping upcall. Frame " << GetFrameId(); |
| 117 | } |
| 118 | last_return_pc_ = 0; |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 119 | return true; // Ignore upcalls. |
| 120 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 121 | if (m->IsRuntimeMethod()) { |
| 122 | if (kVerboseInstrumentation) { |
| 123 | LOG(INFO) << " Skipping runtime method. Frame " << GetFrameId(); |
| 124 | } |
| 125 | last_return_pc_ = GetReturnPc(); |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 126 | return true; // Ignore unresolved methods since they will be instrumented after resolution. |
| 127 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 128 | if (kVerboseInstrumentation) { |
| 129 | LOG(INFO) << " Installing exit stub in " << DescribeLocation(); |
| 130 | } |
| 131 | uintptr_t return_pc = GetReturnPc(); |
| 132 | CHECK_NE(return_pc, instrumentation_exit_pc_); |
| 133 | CHECK_NE(return_pc, 0U); |
Jeff Hao | 9a916d3 | 2013-06-27 18:45:37 -0700 | [diff] [blame] | 134 | InstrumentationStackFrame instrumentation_frame(GetThisObject(), m, return_pc, GetFrameId(), |
| 135 | false); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 136 | if (kVerboseInstrumentation) { |
| 137 | LOG(INFO) << "Pushing frame " << instrumentation_frame.Dump(); |
| 138 | } |
| 139 | instrumentation_stack_->push_back(instrumentation_frame); |
| 140 | dex_pcs_.push_back(m->ToDexPc(last_return_pc_)); |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 141 | SetReturnPc(instrumentation_exit_pc_); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 142 | last_return_pc_ = return_pc; |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 143 | return true; // Continue. |
| 144 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 145 | std::deque<InstrumentationStackFrame>* const instrumentation_stack_; |
| 146 | std::vector<uint32_t> dex_pcs_; |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 147 | const uintptr_t instrumentation_exit_pc_; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 148 | uintptr_t last_return_pc_; |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 149 | }; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 150 | if (kVerboseInstrumentation) { |
| 151 | std::string thread_name; |
| 152 | thread->GetThreadName(thread_name); |
| 153 | LOG(INFO) << "Installing exit stubs in " << thread_name; |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 154 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 155 | UniquePtr<Context> context(Context::Create()); |
| 156 | uintptr_t instrumentation_exit_pc = GetInstrumentationExitPc(); |
| 157 | InstallStackVisitor visitor(thread, context.get(), instrumentation_exit_pc); |
| 158 | visitor.WalkStack(true); |
| 159 | |
| 160 | // Create method enter events for all methods current on the thread's stack. |
| 161 | Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg); |
| 162 | typedef std::deque<InstrumentationStackFrame>::const_reverse_iterator It; |
| 163 | for (It it = thread->GetInstrumentationStack()->rbegin(), |
| 164 | end = thread->GetInstrumentationStack()->rend(); it != end; ++it) { |
| 165 | mirror::Object* this_object = (*it).this_object_; |
| 166 | mirror::AbstractMethod* method = (*it).method_; |
| 167 | uint32_t dex_pc = visitor.dex_pcs_.back(); |
| 168 | visitor.dex_pcs_.pop_back(); |
| 169 | instrumentation->MethodEnterEvent(thread, this_object, method, dex_pc); |
| 170 | } |
| 171 | thread->VerifyStack(); |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 172 | } |
| 173 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 174 | // Removes the instrumentation exit pc as the return PC for every quick frame. |
| 175 | static void InstrumentationRestoreStack(Thread* thread, void* arg) |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 176 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 177 | struct RestoreStackVisitor : public StackVisitor { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 178 | RestoreStackVisitor(Thread* thread, uintptr_t instrumentation_exit_pc, |
| 179 | Instrumentation* instrumentation) |
| 180 | : StackVisitor(thread, NULL), thread_(thread), |
| 181 | instrumentation_exit_pc_(instrumentation_exit_pc), |
| 182 | instrumentation_(instrumentation), |
| 183 | instrumentation_stack_(thread->GetInstrumentationStack()), |
| 184 | frames_removed_(0) {} |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 185 | |
| 186 | virtual bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 187 | if (instrumentation_stack_->size() == 0) { |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 188 | return false; // Stop. |
| 189 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 190 | mirror::AbstractMethod* m = GetMethod(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 191 | if (GetCurrentQuickFrame() == NULL) { |
| 192 | if (kVerboseInstrumentation) { |
| 193 | LOG(INFO) << " Ignoring a shadow frame. Frame " << GetFrameId() << " Method=" << PrettyMethod(m); |
| 194 | } |
| 195 | return true; // Ignore shadow frames. |
| 196 | } |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 197 | if (m == NULL) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 198 | if (kVerboseInstrumentation) { |
| 199 | LOG(INFO) << " Skipping upcall. Frame " << GetFrameId(); |
| 200 | } |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 201 | return true; // Ignore upcalls. |
| 202 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 203 | typedef std::deque<instrumentation::InstrumentationStackFrame>::const_iterator It; // TODO: C++0x auto |
| 204 | bool removed_stub = false; |
| 205 | // TODO: make this search more efficient? |
| 206 | for (It it = instrumentation_stack_->begin(), end = instrumentation_stack_->end(); it != end; |
| 207 | ++it) { |
| 208 | InstrumentationStackFrame instrumentation_frame = *it; |
| 209 | if (instrumentation_frame.frame_id_ == GetFrameId()) { |
| 210 | if (kVerboseInstrumentation) { |
| 211 | LOG(INFO) << " Removing exit stub in " << DescribeLocation(); |
| 212 | } |
Jeff Hao | 9a916d3 | 2013-06-27 18:45:37 -0700 | [diff] [blame] | 213 | if (instrumentation_frame.interpreter_entry_) { |
| 214 | CHECK(m == Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)); |
| 215 | } else { |
| 216 | CHECK(m == instrumentation_frame.method_) << PrettyMethod(m); |
| 217 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 218 | SetReturnPc(instrumentation_frame.return_pc_); |
| 219 | // Create the method exit events. As the methods didn't really exit the result is 0. |
| 220 | instrumentation_->MethodExitEvent(thread_, instrumentation_frame.this_object_, m, |
| 221 | GetDexPc(), JValue()); |
| 222 | frames_removed_++; |
| 223 | removed_stub = true; |
| 224 | break; |
| 225 | } |
| 226 | } |
| 227 | if (!removed_stub) { |
| 228 | if (kVerboseInstrumentation) { |
| 229 | LOG(INFO) << " No exit stub in " << DescribeLocation(); |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 230 | } |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 231 | } |
| 232 | return true; // Continue. |
| 233 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 234 | Thread* const thread_; |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 235 | const uintptr_t instrumentation_exit_pc_; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 236 | Instrumentation* const instrumentation_; |
| 237 | std::deque<instrumentation::InstrumentationStackFrame>* const instrumentation_stack_; |
| 238 | size_t frames_removed_; |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 239 | }; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 240 | if (kVerboseInstrumentation) { |
| 241 | std::string thread_name; |
| 242 | thread->GetThreadName(thread_name); |
| 243 | LOG(INFO) << "Removing exit stubs in " << thread_name; |
| 244 | } |
| 245 | std::deque<instrumentation::InstrumentationStackFrame>* stack = thread->GetInstrumentationStack(); |
| 246 | if (stack->size() > 0) { |
| 247 | Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg); |
| 248 | uintptr_t instrumentation_exit_pc = GetInstrumentationExitPc(); |
| 249 | RestoreStackVisitor visitor(thread, instrumentation_exit_pc, instrumentation); |
| 250 | visitor.WalkStack(true); |
| 251 | CHECK_EQ(visitor.frames_removed_, stack->size()); |
| 252 | while (stack->size() > 0) { |
| 253 | stack->pop_front(); |
| 254 | } |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 258 | void Instrumentation::AddListener(InstrumentationListener* listener, uint32_t events) { |
| 259 | Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current()); |
| 260 | bool require_entry_exit_stubs = false; |
| 261 | bool require_interpreter = false; |
| 262 | if ((events & kMethodEntered) != 0) { |
| 263 | method_entry_listeners_.push_back(listener); |
| 264 | require_entry_exit_stubs = true; |
| 265 | have_method_entry_listeners_ = true; |
| 266 | } |
| 267 | if ((events & kMethodExited) != 0) { |
| 268 | method_exit_listeners_.push_back(listener); |
| 269 | require_entry_exit_stubs = true; |
| 270 | have_method_exit_listeners_ = true; |
| 271 | } |
| 272 | if ((events & kMethodUnwind) != 0) { |
| 273 | method_unwind_listeners_.push_back(listener); |
| 274 | have_method_unwind_listeners_ = true; |
| 275 | } |
| 276 | if ((events & kDexPcMoved) != 0) { |
| 277 | dex_pc_listeners_.push_back(listener); |
| 278 | require_interpreter = true; |
| 279 | have_dex_pc_listeners_ = true; |
| 280 | } |
Jeff Hao | 14dd5a8 | 2013-04-11 10:23:36 -0700 | [diff] [blame] | 281 | if ((events & kExceptionCaught) != 0) { |
| 282 | exception_caught_listeners_.push_back(listener); |
| 283 | have_exception_caught_listeners_ = true; |
| 284 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 285 | ConfigureStubs(require_entry_exit_stubs, require_interpreter); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 286 | } |
| 287 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 288 | void Instrumentation::RemoveListener(InstrumentationListener* listener, uint32_t events) { |
| 289 | Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current()); |
| 290 | bool require_entry_exit_stubs = false; |
| 291 | bool require_interpreter = false; |
| 292 | |
| 293 | if ((events & kMethodEntered) != 0) { |
| 294 | bool contains = std::find(method_entry_listeners_.begin(), method_entry_listeners_.end(), |
| 295 | listener) != method_entry_listeners_.end(); |
| 296 | if (contains) { |
| 297 | method_entry_listeners_.remove(listener); |
| 298 | } |
| 299 | have_method_entry_listeners_ = method_entry_listeners_.size() > 0; |
| 300 | require_entry_exit_stubs |= have_method_entry_listeners_; |
| 301 | } |
| 302 | if ((events & kMethodExited) != 0) { |
| 303 | bool contains = std::find(method_exit_listeners_.begin(), method_exit_listeners_.end(), |
| 304 | listener) != method_exit_listeners_.end(); |
| 305 | if (contains) { |
| 306 | method_exit_listeners_.remove(listener); |
| 307 | } |
| 308 | have_method_exit_listeners_ = method_exit_listeners_.size() > 0; |
| 309 | require_entry_exit_stubs |= have_method_exit_listeners_; |
| 310 | } |
| 311 | if ((events & kMethodUnwind) != 0) { |
| 312 | method_unwind_listeners_.remove(listener); |
| 313 | } |
| 314 | if ((events & kDexPcMoved) != 0) { |
| 315 | bool contains = std::find(dex_pc_listeners_.begin(), dex_pc_listeners_.end(), |
| 316 | listener) != dex_pc_listeners_.end(); |
| 317 | if (contains) { |
| 318 | dex_pc_listeners_.remove(listener); |
| 319 | } |
| 320 | have_dex_pc_listeners_ = dex_pc_listeners_.size() > 0; |
| 321 | require_interpreter |= have_dex_pc_listeners_; |
| 322 | } |
Jeff Hao | 14dd5a8 | 2013-04-11 10:23:36 -0700 | [diff] [blame] | 323 | if ((events & kExceptionCaught) != 0) { |
| 324 | exception_caught_listeners_.remove(listener); |
| 325 | have_exception_caught_listeners_ = exception_caught_listeners_.size() > 0; |
| 326 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 327 | ConfigureStubs(require_entry_exit_stubs, require_interpreter); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 328 | } |
| 329 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 330 | void Instrumentation::ConfigureStubs(bool require_entry_exit_stubs, bool require_interpreter) { |
| 331 | interpret_only_ = require_interpreter || forced_interpret_only_; |
| 332 | // Compute what level of instrumentation is required and compare to current. |
| 333 | int desired_level, current_level; |
| 334 | if (require_interpreter) { |
| 335 | desired_level = 2; |
| 336 | } else if (require_entry_exit_stubs) { |
| 337 | desired_level = 1; |
| 338 | } else { |
| 339 | desired_level = 0; |
| 340 | } |
| 341 | if (interpreter_stubs_installed_) { |
| 342 | current_level = 2; |
| 343 | } else if (entry_exit_stubs_installed_) { |
| 344 | current_level = 1; |
| 345 | } else { |
| 346 | current_level = 0; |
| 347 | } |
| 348 | if (desired_level == current_level) { |
| 349 | // We're already set. |
| 350 | return; |
| 351 | } |
| 352 | Thread* self = Thread::Current(); |
| 353 | Runtime* runtime = Runtime::Current(); |
| 354 | Locks::thread_list_lock_->AssertNotHeld(self); |
| 355 | if (desired_level > 0) { |
| 356 | if (require_interpreter) { |
| 357 | interpreter_stubs_installed_ = true; |
| 358 | } else { |
| 359 | CHECK(require_entry_exit_stubs); |
| 360 | entry_exit_stubs_installed_ = true; |
| 361 | } |
| 362 | runtime->GetClassLinker()->VisitClasses(InstallStubsClassVisitor, this); |
| 363 | instrumentation_stubs_installed_ = true; |
| 364 | MutexLock mu(Thread::Current(), *Locks::thread_list_lock_); |
| 365 | runtime->GetThreadList()->ForEach(InstrumentationInstallStack, this); |
| 366 | } else { |
| 367 | interpreter_stubs_installed_ = false; |
| 368 | entry_exit_stubs_installed_ = false; |
| 369 | runtime->GetClassLinker()->VisitClasses(InstallStubsClassVisitor, this); |
| 370 | instrumentation_stubs_installed_ = false; |
| 371 | MutexLock mu(self, *Locks::thread_list_lock_); |
| 372 | Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this); |
| 373 | } |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 374 | } |
| 375 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 376 | void Instrumentation::UpdateMethodsCode(mirror::AbstractMethod* method, const void* code) const { |
| 377 | if (LIKELY(!instrumentation_stubs_installed_)) { |
Jeff Hao | aa4a793 | 2013-05-13 11:28:27 -0700 | [diff] [blame] | 378 | method->SetEntryPointFromCompiledCode(code); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 379 | } |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 380 | } |
| 381 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 382 | const void* Instrumentation::GetQuickCodeFor(const mirror::AbstractMethod* method) const { |
| 383 | Runtime* runtime = Runtime::Current(); |
| 384 | if (LIKELY(!instrumentation_stubs_installed_)) { |
Jeff Hao | aa4a793 | 2013-05-13 11:28:27 -0700 | [diff] [blame] | 385 | const void* code = method->GetEntryPointFromCompiledCode(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 386 | DCHECK(code != NULL); |
Jeff Hao | f3e861d | 2013-07-03 15:42:17 -0700 | [diff] [blame] | 387 | if (LIKELY(code != GetResolutionTrampoline(runtime->GetClassLinker()) && |
| 388 | code != GetInterpreterEntryPoint())) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 389 | return code; |
| 390 | } |
| 391 | } |
| 392 | return runtime->GetClassLinker()->GetOatCodeFor(method); |
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::MethodEnterEventImpl(Thread* thread, mirror::Object* this_object, |
| 396 | const mirror::AbstractMethod* method, |
| 397 | uint32_t dex_pc) const { |
| 398 | typedef std::list<InstrumentationListener*>::const_iterator It; // TODO: C++0x auto |
| 399 | for (It it = method_entry_listeners_.begin(), end = method_entry_listeners_.end(); it != end; |
| 400 | ++it) { |
| 401 | (*it)->MethodEntered(thread, this_object, method, dex_pc); |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | void Instrumentation::MethodExitEventImpl(Thread* thread, mirror::Object* this_object, |
| 406 | const mirror::AbstractMethod* method, |
| 407 | uint32_t dex_pc, const JValue& return_value) const { |
| 408 | typedef std::list<InstrumentationListener*>::const_iterator It; // TODO: C++0x auto |
| 409 | for (It it = method_exit_listeners_.begin(), end = method_exit_listeners_.end(); it != end; |
| 410 | ++it) { |
| 411 | (*it)->MethodExited(thread, this_object, method, dex_pc, return_value); |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | void Instrumentation::MethodUnwindEvent(Thread* thread, mirror::Object* this_object, |
| 416 | const mirror::AbstractMethod* method, |
| 417 | uint32_t dex_pc) const { |
| 418 | if (have_method_unwind_listeners_) { |
| 419 | typedef std::list<InstrumentationListener*>::const_iterator It; // TODO: C++0x auto |
| 420 | for (It it = method_unwind_listeners_.begin(), end = method_unwind_listeners_.end(); it != end; |
| 421 | ++it) { |
| 422 | (*it)->MethodUnwind(thread, method, dex_pc); |
| 423 | } |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | void Instrumentation::DexPcMovedEventImpl(Thread* thread, mirror::Object* this_object, |
| 428 | const mirror::AbstractMethod* method, |
| 429 | uint32_t dex_pc) const { |
| 430 | // TODO: STL copy-on-write collection? The copy below is due to the debug listener having an |
| 431 | // action where it can remove itself as a listener and break the iterator. The copy only works |
| 432 | // around the problem and in general we may have to move to something like reference counting to |
| 433 | // ensure listeners are deleted correctly. |
| 434 | std::list<InstrumentationListener*> copy(dex_pc_listeners_); |
| 435 | typedef std::list<InstrumentationListener*>::const_iterator It; // TODO: C++0x auto |
| 436 | for (It it = copy.begin(), end = copy.end(); it != end; ++it) { |
| 437 | (*it)->DexPcMoved(thread, this_object, method, dex_pc); |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | void Instrumentation::ExceptionCaughtEvent(Thread* thread, const ThrowLocation& throw_location, |
| 442 | mirror::AbstractMethod* catch_method, |
| 443 | uint32_t catch_dex_pc, |
| 444 | mirror::Throwable* exception_object) { |
| 445 | if (have_exception_caught_listeners_) { |
Jeff Hao | c0bd4da | 2013-04-11 15:52:28 -0700 | [diff] [blame] | 446 | DCHECK_EQ(thread->GetException(NULL), exception_object); |
| 447 | thread->ClearException(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 448 | typedef std::list<InstrumentationListener*>::const_iterator It; // TODO: C++0x auto |
| 449 | for (It it = exception_caught_listeners_.begin(), end = exception_caught_listeners_.end(); |
| 450 | it != end; ++it) { |
| 451 | (*it)->ExceptionCaught(thread, throw_location, catch_method, catch_dex_pc, exception_object); |
| 452 | } |
Jeff Hao | c0bd4da | 2013-04-11 15:52:28 -0700 | [diff] [blame] | 453 | thread->SetException(throw_location, exception_object); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 454 | } |
| 455 | } |
| 456 | |
| 457 | static void CheckStackDepth(Thread* self, const InstrumentationStackFrame& instrumentation_frame, |
| 458 | int delta) |
| 459 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 460 | size_t frame_id = StackVisitor::ComputeNumFrames(self) + delta; |
| 461 | if (frame_id != instrumentation_frame.frame_id_) { |
| 462 | LOG(ERROR) << "Expected frame_id=" << frame_id << " but found " |
| 463 | << instrumentation_frame.frame_id_; |
| 464 | StackVisitor::DescribeStack(self); |
| 465 | CHECK_EQ(frame_id, instrumentation_frame.frame_id_); |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | void Instrumentation::PushInstrumentationStackFrame(Thread* self, mirror::Object* this_object, |
| 470 | mirror::AbstractMethod* method, |
Jeff Hao | 9a916d3 | 2013-06-27 18:45:37 -0700 | [diff] [blame] | 471 | uintptr_t lr, bool interpreter_entry) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 472 | // We have a callee-save frame meaning this value is guaranteed to never be 0. |
| 473 | size_t frame_id = StackVisitor::ComputeNumFrames(self); |
| 474 | std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack(); |
| 475 | if (kVerboseInstrumentation) { |
Brian Carlstrom | 2d88862 | 2013-07-18 17:02:00 -0700 | [diff] [blame^] | 476 | LOG(INFO) << "Entering " << PrettyMethod(method) << " from PC " << reinterpret_cast<void*>(lr); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 477 | } |
| 478 | instrumentation::InstrumentationStackFrame instrumentation_frame(this_object, method, lr, |
Jeff Hao | 9a916d3 | 2013-06-27 18:45:37 -0700 | [diff] [blame] | 479 | frame_id, interpreter_entry); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 480 | stack->push_front(instrumentation_frame); |
| 481 | |
| 482 | MethodEnterEvent(self, this_object, method, 0); |
| 483 | } |
| 484 | |
| 485 | uint64_t Instrumentation::PopInstrumentationStackFrame(Thread* self, uintptr_t* return_pc, |
| 486 | uint64_t gpr_result, uint64_t fpr_result) { |
| 487 | // Do the pop. |
| 488 | std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack(); |
| 489 | CHECK_GT(stack->size(), 0U); |
| 490 | InstrumentationStackFrame instrumentation_frame = stack->front(); |
| 491 | stack->pop_front(); |
| 492 | |
| 493 | // Set return PC and check the sanity of the stack. |
| 494 | *return_pc = instrumentation_frame.return_pc_; |
| 495 | CheckStackDepth(self, instrumentation_frame, 0); |
| 496 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 497 | mirror::AbstractMethod* method = instrumentation_frame.method_; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 498 | char return_shorty = MethodHelper(method).GetShorty()[0]; |
| 499 | JValue return_value; |
| 500 | if (return_shorty == 'V') { |
| 501 | return_value.SetJ(0); |
| 502 | } else if (return_shorty == 'F' || return_shorty == 'D') { |
| 503 | return_value.SetJ(fpr_result); |
| 504 | } else { |
| 505 | return_value.SetJ(gpr_result); |
| 506 | } |
| 507 | // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to |
| 508 | // return_pc. |
| 509 | uint32_t dex_pc = DexFile::kDexNoIndex; |
| 510 | mirror::Object* this_object = instrumentation_frame.this_object_; |
| 511 | MethodExitEvent(self, this_object, instrumentation_frame.method_, dex_pc, return_value); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 512 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 513 | bool deoptimize = false; |
| 514 | if (interpreter_stubs_installed_) { |
| 515 | // Deoptimize unless we're returning to an upcall. |
| 516 | NthCallerVisitor visitor(self, 1, true); |
| 517 | visitor.WalkStack(true); |
| 518 | deoptimize = visitor.caller != NULL; |
| 519 | if (deoptimize && kVerboseInstrumentation) { |
| 520 | LOG(INFO) << "Deoptimizing into " << PrettyMethod(visitor.caller); |
| 521 | } |
| 522 | } |
| 523 | if (deoptimize) { |
| 524 | if (kVerboseInstrumentation) { |
| 525 | LOG(INFO) << "Deoptimizing from " << PrettyMethod(method) |
| 526 | << " result is " << std::hex << return_value.GetJ(); |
| 527 | } |
| 528 | self->SetDeoptimizationReturnValue(return_value); |
| 529 | return static_cast<uint64_t>(GetDeoptimizationEntryPoint()) | |
| 530 | (static_cast<uint64_t>(*return_pc) << 32); |
| 531 | } else { |
| 532 | if (kVerboseInstrumentation) { |
Brian Carlstrom | 2d88862 | 2013-07-18 17:02:00 -0700 | [diff] [blame^] | 533 | LOG(INFO) << "Returning from " << PrettyMethod(method) |
| 534 | << " to PC " << reinterpret_cast<void*>(*return_pc); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 535 | } |
| 536 | return *return_pc; |
| 537 | } |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 538 | } |
| 539 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 540 | void Instrumentation::PopMethodForUnwind(Thread* self, bool is_deoptimization) const { |
| 541 | // Do the pop. |
| 542 | std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack(); |
| 543 | CHECK_GT(stack->size(), 0U); |
| 544 | InstrumentationStackFrame instrumentation_frame = stack->front(); |
| 545 | // TODO: bring back CheckStackDepth(self, instrumentation_frame, 2); |
| 546 | stack->pop_front(); |
| 547 | |
| 548 | mirror::AbstractMethod* method = instrumentation_frame.method_; |
| 549 | if (is_deoptimization) { |
| 550 | if (kVerboseInstrumentation) { |
| 551 | LOG(INFO) << "Popping for deoptimization " << PrettyMethod(method); |
| 552 | } |
| 553 | } else { |
| 554 | if (kVerboseInstrumentation) { |
| 555 | LOG(INFO) << "Popping for unwind " << PrettyMethod(method); |
| 556 | } |
| 557 | |
| 558 | // Notify listeners of method unwind. |
| 559 | // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to |
| 560 | // return_pc. |
| 561 | uint32_t dex_pc = DexFile::kDexNoIndex; |
| 562 | MethodUnwindEvent(self, instrumentation_frame.this_object_, method, dex_pc); |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | std::string InstrumentationStackFrame::Dump() const { |
| 567 | std::ostringstream os; |
| 568 | os << "Frame " << frame_id_ << " " << PrettyMethod(method_) << ":" |
| 569 | << reinterpret_cast<void*>(return_pc_) << " this=" << reinterpret_cast<void*>(this_object_); |
| 570 | return os.str(); |
| 571 | } |
| 572 | |
| 573 | } // namespace instrumentation |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 574 | } // namespace art |