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