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