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 | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 21 | #include <sstream> |
| 22 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 23 | #include "arch/context.h" |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 24 | #include "atomic.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 25 | #include "base/unix_file/fd_file.h" |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 26 | #include "class_linker.h" |
| 27 | #include "debugger.h" |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 28 | #include "dex_file-inl.h" |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 29 | #include "entrypoints/quick/quick_entrypoints.h" |
Mathieu Chartier | d889178 | 2014-03-02 13:28:37 -0800 | [diff] [blame] | 30 | #include "entrypoints/quick/quick_alloc_entrypoints.h" |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 31 | #include "entrypoints/runtime_asm_entrypoints.h" |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 32 | #include "gc_root-inl.h" |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 33 | #include "interpreter/interpreter.h" |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 34 | #include "mirror/art_method-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 35 | #include "mirror/class-inl.h" |
| 36 | #include "mirror/dex_cache.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 37 | #include "mirror/object_array-inl.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 38 | #include "mirror/object-inl.h" |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 39 | #include "nth_caller_visitor.h" |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 40 | #include "os.h" |
| 41 | #include "scoped_thread_state_change.h" |
| 42 | #include "thread.h" |
| 43 | #include "thread_list.h" |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 44 | |
| 45 | namespace art { |
Ian Rogers | fa82427 | 2013-11-05 16:12:57 -0800 | [diff] [blame] | 46 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 47 | namespace instrumentation { |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 48 | |
Sebastien Hertz | 5bfd5c9 | 2013-11-15 11:36:07 +0100 | [diff] [blame] | 49 | const bool kVerboseInstrumentation = false; |
| 50 | |
Ian Rogers | 816432e | 2013-09-06 15:47:45 -0700 | [diff] [blame] | 51 | // Do we want to deoptimize for method entry and exit listeners or just try to intercept |
| 52 | // invocations? Deoptimization forces all code to run in the interpreter and considerably hurts the |
| 53 | // application's performance. |
Jeff Hao | bc678bb | 2014-08-11 18:00:29 -0700 | [diff] [blame] | 54 | static constexpr bool kDeoptimizeForAccurateMethodEntryExitListeners = true; |
Ian Rogers | 816432e | 2013-09-06 15:47:45 -0700 | [diff] [blame] | 55 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 56 | static bool InstallStubsClassVisitor(mirror::Class* klass, void* arg) |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 57 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 58 | Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg); |
| 59 | return instrumentation->InstallStubsForClass(klass); |
| 60 | } |
| 61 | |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 62 | Instrumentation::Instrumentation() |
| 63 | : instrumentation_stubs_installed_(false), entry_exit_stubs_installed_(false), |
| 64 | interpreter_stubs_installed_(false), |
| 65 | interpret_only_(false), forced_interpret_only_(false), |
| 66 | have_method_entry_listeners_(false), have_method_exit_listeners_(false), |
| 67 | have_method_unwind_listeners_(false), have_dex_pc_listeners_(false), |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 68 | have_field_read_listeners_(false), have_field_write_listeners_(false), |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 69 | have_exception_caught_listeners_(false), |
| 70 | deoptimized_methods_lock_("deoptimized methods lock"), |
| 71 | deoptimization_enabled_(false), |
| 72 | interpreter_handler_table_(kMainHandlerTable), |
| 73 | quick_alloc_entry_points_instrumentation_counter_(0) { |
| 74 | } |
| 75 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 76 | bool Instrumentation::InstallStubsForClass(mirror::Class* klass) { |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 77 | for (size_t i = 0, e = klass->NumDirectMethods(); i < e; i++) { |
| 78 | InstallStubsForMethod(klass->GetDirectMethod(i)); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 79 | } |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 80 | for (size_t i = 0, e = klass->NumVirtualMethods(); i < e; i++) { |
| 81 | InstallStubsForMethod(klass->GetVirtualMethod(i)); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 82 | } |
| 83 | return true; |
| 84 | } |
| 85 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 86 | static void UpdateEntrypoints(mirror::ArtMethod* method, const void* quick_code, |
| 87 | const void* portable_code, bool have_portable_code) |
| 88 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 89 | method->SetEntryPointFromPortableCompiledCode(portable_code); |
| 90 | method->SetEntryPointFromQuickCompiledCode(quick_code); |
| 91 | bool portable_enabled = method->IsPortableCompiled(); |
| 92 | if (have_portable_code && !portable_enabled) { |
| 93 | method->SetIsPortableCompiled(); |
| 94 | } else if (portable_enabled) { |
| 95 | method->ClearIsPortableCompiled(); |
| 96 | } |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 97 | if (!method->IsResolutionMethod()) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 98 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 99 | if (class_linker->IsQuickToInterpreterBridge(quick_code) || |
| 100 | (class_linker->IsQuickResolutionStub(quick_code) && |
| 101 | Runtime::Current()->GetInstrumentation()->IsForcedInterpretOnly() && |
| 102 | !method->IsNative() && !method->IsProxyMethod())) { |
Hiroshi Yamauchi | 563b47c | 2014-02-28 17:18:37 -0800 | [diff] [blame] | 103 | if (kIsDebugBuild) { |
| 104 | if (quick_code == GetQuickToInterpreterBridge()) { |
| 105 | DCHECK(portable_code == GetPortableToInterpreterBridge()); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 106 | } else if (class_linker->IsQuickResolutionStub(quick_code)) { |
| 107 | DCHECK(class_linker->IsPortableResolutionStub(portable_code)); |
Hiroshi Yamauchi | 563b47c | 2014-02-28 17:18:37 -0800 | [diff] [blame] | 108 | } |
| 109 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 110 | DCHECK(!method->IsNative()) << PrettyMethod(method); |
Hiroshi Yamauchi | 563b47c | 2014-02-28 17:18:37 -0800 | [diff] [blame] | 111 | DCHECK(!method->IsProxyMethod()) << PrettyMethod(method); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 112 | method->SetEntryPointFromInterpreter(art::artInterpreterToInterpreterBridge); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 113 | } else { |
| 114 | method->SetEntryPointFromInterpreter(art::artInterpreterToCompiledCodeBridge); |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | void Instrumentation::InstallStubsForMethod(mirror::ArtMethod* method) { |
| 120 | if (method->IsAbstract() || method->IsProxyMethod()) { |
| 121 | // Do not change stubs for these methods. |
| 122 | return; |
| 123 | } |
Jeff Hao | 5680277 | 2014-08-19 10:17:36 -0700 | [diff] [blame] | 124 | // Don't stub Proxy.<init>. Note that the Proxy class itself is not a proxy class. |
| 125 | if (method->IsConstructor() && |
| 126 | method->GetDeclaringClass()->DescriptorEquals("Ljava/lang/reflect/Proxy;")) { |
Jeff Hao | db8a664 | 2014-08-14 17:18:52 -0700 | [diff] [blame] | 127 | return; |
| 128 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 129 | const void* new_portable_code; |
| 130 | const void* new_quick_code; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 131 | bool uninstall = !entry_exit_stubs_installed_ && !interpreter_stubs_installed_; |
| 132 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 133 | bool is_class_initialized = method->GetDeclaringClass()->IsInitialized(); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 134 | bool have_portable_code = false; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 135 | if (uninstall) { |
| 136 | if ((forced_interpret_only_ || IsDeoptimized(method)) && !method->IsNative()) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 137 | new_portable_code = GetPortableToInterpreterBridge(); |
| 138 | new_quick_code = GetQuickToInterpreterBridge(); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 139 | } else if (is_class_initialized || !method->IsStatic() || method->IsConstructor()) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 140 | new_portable_code = class_linker->GetPortableOatCodeFor(method, &have_portable_code); |
| 141 | new_quick_code = class_linker->GetQuickOatCodeFor(method); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 142 | } else { |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 143 | new_portable_code = GetPortableResolutionStub(); |
| 144 | new_quick_code = GetQuickResolutionStub(); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 145 | } |
| 146 | } else { // !uninstall |
Sebastien Hertz | bae182c | 2013-12-17 10:42:03 +0100 | [diff] [blame] | 147 | if ((interpreter_stubs_installed_ || forced_interpret_only_ || IsDeoptimized(method)) && |
| 148 | !method->IsNative()) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 149 | new_portable_code = GetPortableToInterpreterBridge(); |
| 150 | new_quick_code = GetQuickToInterpreterBridge(); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 151 | } else { |
| 152 | // Do not overwrite resolution trampoline. When the trampoline initializes the method's |
| 153 | // class, all its static methods code will be set to the instrumentation entry point. |
| 154 | // For more details, see ClassLinker::FixupStaticTrampolines. |
| 155 | if (is_class_initialized || !method->IsStatic() || method->IsConstructor()) { |
Sebastien Hertz | 320deb2 | 2014-06-11 19:45:05 +0200 | [diff] [blame] | 156 | if (entry_exit_stubs_installed_) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 157 | new_portable_code = GetPortableToInterpreterBridge(); |
| 158 | new_quick_code = GetQuickInstrumentationEntryPoint(); |
Sebastien Hertz | 320deb2 | 2014-06-11 19:45:05 +0200 | [diff] [blame] | 159 | } else { |
| 160 | new_portable_code = class_linker->GetPortableOatCodeFor(method, &have_portable_code); |
| 161 | new_quick_code = class_linker->GetQuickOatCodeFor(method); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 162 | DCHECK(!class_linker->IsQuickToInterpreterBridge(new_quick_code)); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 163 | } |
| 164 | } else { |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 165 | new_portable_code = GetPortableResolutionStub(); |
| 166 | new_quick_code = GetQuickResolutionStub(); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 170 | UpdateEntrypoints(method, new_quick_code, new_portable_code, have_portable_code); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 171 | } |
| 172 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 173 | // Places the instrumentation exit pc as the return PC for every quick frame. This also allows |
| 174 | // deoptimization of quick frames to interpreter frames. |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 175 | // Since we may already have done this previously, we need to push new instrumentation frame before |
| 176 | // existing instrumentation frames. |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 177 | static void InstrumentationInstallStack(Thread* thread, void* arg) |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 178 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 179 | struct InstallStackVisitor : public StackVisitor { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 180 | InstallStackVisitor(Thread* thread_in, Context* context, uintptr_t instrumentation_exit_pc) |
| 181 | : StackVisitor(thread_in, context), |
| 182 | instrumentation_stack_(thread_in->GetInstrumentationStack()), |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 183 | instrumentation_exit_pc_(instrumentation_exit_pc), |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 184 | reached_existing_instrumentation_frames_(false), instrumentation_stack_depth_(0), |
| 185 | last_return_pc_(0) { |
| 186 | } |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 187 | |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 188 | virtual bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 189 | mirror::ArtMethod* m = GetMethod(); |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 190 | if (m == NULL) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 191 | if (kVerboseInstrumentation) { |
| 192 | LOG(INFO) << " Skipping upcall. Frame " << GetFrameId(); |
| 193 | } |
| 194 | last_return_pc_ = 0; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 195 | return true; // Ignore upcalls. |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 196 | } |
Jeff Hao | a15a81b | 2014-05-27 18:25:47 -0700 | [diff] [blame] | 197 | if (GetCurrentQuickFrame() == NULL) { |
Sebastien Hertz | 320deb2 | 2014-06-11 19:45:05 +0200 | [diff] [blame] | 198 | bool interpreter_frame = !m->IsPortableCompiled(); |
| 199 | InstrumentationStackFrame instrumentation_frame(GetThisObject(), m, 0, GetFrameId(), |
| 200 | interpreter_frame); |
Jeff Hao | a15a81b | 2014-05-27 18:25:47 -0700 | [diff] [blame] | 201 | if (kVerboseInstrumentation) { |
| 202 | LOG(INFO) << "Pushing shadow frame " << instrumentation_frame.Dump(); |
| 203 | } |
| 204 | shadow_stack_.push_back(instrumentation_frame); |
| 205 | return true; // Continue. |
| 206 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 207 | uintptr_t return_pc = GetReturnPc(); |
Sebastien Hertz | 320deb2 | 2014-06-11 19:45:05 +0200 | [diff] [blame] | 208 | if (m->IsRuntimeMethod()) { |
| 209 | if (return_pc == instrumentation_exit_pc_) { |
| 210 | if (kVerboseInstrumentation) { |
| 211 | LOG(INFO) << " Handling quick to interpreter transition. Frame " << GetFrameId(); |
| 212 | } |
| 213 | CHECK_LT(instrumentation_stack_depth_, instrumentation_stack_->size()); |
Daniel Mihalyi | ca1d06c | 2014-08-18 18:45:31 +0200 | [diff] [blame] | 214 | const InstrumentationStackFrame& frame = |
| 215 | instrumentation_stack_->at(instrumentation_stack_depth_); |
Sebastien Hertz | 320deb2 | 2014-06-11 19:45:05 +0200 | [diff] [blame] | 216 | CHECK(frame.interpreter_entry_); |
| 217 | // This is an interpreter frame so method enter event must have been reported. However we |
| 218 | // need to push a DEX pc into the dex_pcs_ list to match size of instrumentation stack. |
| 219 | // Since we won't report method entry here, we can safely push any DEX pc. |
| 220 | dex_pcs_.push_back(0); |
| 221 | last_return_pc_ = frame.return_pc_; |
| 222 | ++instrumentation_stack_depth_; |
| 223 | return true; |
| 224 | } else { |
| 225 | if (kVerboseInstrumentation) { |
| 226 | LOG(INFO) << " Skipping runtime method. Frame " << GetFrameId(); |
| 227 | } |
| 228 | last_return_pc_ = GetReturnPc(); |
| 229 | return true; // Ignore unresolved methods since they will be instrumented after resolution. |
| 230 | } |
| 231 | } |
| 232 | if (kVerboseInstrumentation) { |
| 233 | LOG(INFO) << " Installing exit stub in " << DescribeLocation(); |
| 234 | } |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 235 | if (return_pc == instrumentation_exit_pc_) { |
| 236 | // We've reached a frame which has already been installed with instrumentation exit stub. |
| 237 | // We should have already installed instrumentation on previous frames. |
| 238 | reached_existing_instrumentation_frames_ = true; |
| 239 | |
| 240 | CHECK_LT(instrumentation_stack_depth_, instrumentation_stack_->size()); |
Daniel Mihalyi | ca1d06c | 2014-08-18 18:45:31 +0200 | [diff] [blame] | 241 | const InstrumentationStackFrame& frame = |
| 242 | instrumentation_stack_->at(instrumentation_stack_depth_); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 243 | CHECK_EQ(m, frame.method_) << "Expected " << PrettyMethod(m) |
| 244 | << ", Found " << PrettyMethod(frame.method_); |
| 245 | return_pc = frame.return_pc_; |
| 246 | if (kVerboseInstrumentation) { |
| 247 | LOG(INFO) << "Ignoring already instrumented " << frame.Dump(); |
| 248 | } |
| 249 | } else { |
| 250 | CHECK_NE(return_pc, 0U); |
| 251 | CHECK(!reached_existing_instrumentation_frames_); |
| 252 | InstrumentationStackFrame instrumentation_frame(GetThisObject(), m, return_pc, GetFrameId(), |
| 253 | false); |
| 254 | if (kVerboseInstrumentation) { |
| 255 | LOG(INFO) << "Pushing frame " << instrumentation_frame.Dump(); |
| 256 | } |
| 257 | |
Sebastien Hertz | 320deb2 | 2014-06-11 19:45:05 +0200 | [diff] [blame] | 258 | // Insert frame at the right position so we do not corrupt the instrumentation stack. |
| 259 | // Instrumentation stack frames are in descending frame id order. |
| 260 | auto it = instrumentation_stack_->begin(); |
| 261 | for (auto end = instrumentation_stack_->end(); it != end; ++it) { |
| 262 | const InstrumentationStackFrame& current = *it; |
| 263 | if (instrumentation_frame.frame_id_ >= current.frame_id_) { |
| 264 | break; |
| 265 | } |
| 266 | } |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 267 | instrumentation_stack_->insert(it, instrumentation_frame); |
| 268 | SetReturnPc(instrumentation_exit_pc_); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 269 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 270 | dex_pcs_.push_back(m->ToDexPc(last_return_pc_)); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 271 | last_return_pc_ = return_pc; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 272 | ++instrumentation_stack_depth_; |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 273 | return true; // Continue. |
| 274 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 275 | std::deque<InstrumentationStackFrame>* const instrumentation_stack_; |
Jeff Hao | a15a81b | 2014-05-27 18:25:47 -0700 | [diff] [blame] | 276 | std::vector<InstrumentationStackFrame> shadow_stack_; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 277 | std::vector<uint32_t> dex_pcs_; |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 278 | const uintptr_t instrumentation_exit_pc_; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 279 | bool reached_existing_instrumentation_frames_; |
| 280 | size_t instrumentation_stack_depth_; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 281 | uintptr_t last_return_pc_; |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 282 | }; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 283 | if (kVerboseInstrumentation) { |
| 284 | std::string thread_name; |
| 285 | thread->GetThreadName(thread_name); |
| 286 | LOG(INFO) << "Installing exit stubs in " << thread_name; |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 287 | } |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 288 | |
| 289 | Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg); |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 290 | std::unique_ptr<Context> context(Context::Create()); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 291 | uintptr_t instrumentation_exit_pc = reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()); |
Sebastien Hertz | 11d40c2 | 2014-02-19 18:00:17 +0100 | [diff] [blame] | 292 | InstallStackVisitor visitor(thread, context.get(), instrumentation_exit_pc); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 293 | visitor.WalkStack(true); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 294 | CHECK_EQ(visitor.dex_pcs_.size(), thread->GetInstrumentationStack()->size()); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 295 | |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 296 | if (instrumentation->ShouldNotifyMethodEnterExitEvents()) { |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 297 | // Create method enter events for all methods currently on the thread's stack. We only do this |
| 298 | // if no debugger is attached to prevent from posting events twice. |
Jeff Hao | a15a81b | 2014-05-27 18:25:47 -0700 | [diff] [blame] | 299 | auto ssi = visitor.shadow_stack_.rbegin(); |
| 300 | for (auto isi = thread->GetInstrumentationStack()->rbegin(), |
| 301 | end = thread->GetInstrumentationStack()->rend(); isi != end; ++isi) { |
| 302 | while (ssi != visitor.shadow_stack_.rend() && (*ssi).frame_id_ < (*isi).frame_id_) { |
| 303 | instrumentation->MethodEnterEvent(thread, (*ssi).this_object_, (*ssi).method_, 0); |
| 304 | ++ssi; |
| 305 | } |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 306 | uint32_t dex_pc = visitor.dex_pcs_.back(); |
| 307 | visitor.dex_pcs_.pop_back(); |
Sebastien Hertz | 320deb2 | 2014-06-11 19:45:05 +0200 | [diff] [blame] | 308 | if (!isi->interpreter_entry_) { |
| 309 | instrumentation->MethodEnterEvent(thread, (*isi).this_object_, (*isi).method_, dex_pc); |
| 310 | } |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 311 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 312 | } |
| 313 | thread->VerifyStack(); |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 314 | } |
| 315 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 316 | // Removes the instrumentation exit pc as the return PC for every quick frame. |
| 317 | static void InstrumentationRestoreStack(Thread* thread, void* arg) |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 318 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 319 | struct RestoreStackVisitor : public StackVisitor { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 320 | RestoreStackVisitor(Thread* thread_in, uintptr_t instrumentation_exit_pc, |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 321 | Instrumentation* instrumentation) |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 322 | : StackVisitor(thread_in, NULL), thread_(thread_in), |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 323 | instrumentation_exit_pc_(instrumentation_exit_pc), |
| 324 | instrumentation_(instrumentation), |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 325 | instrumentation_stack_(thread_in->GetInstrumentationStack()), |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 326 | frames_removed_(0) {} |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 327 | |
| 328 | virtual bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 329 | if (instrumentation_stack_->size() == 0) { |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 330 | return false; // Stop. |
| 331 | } |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 332 | mirror::ArtMethod* m = GetMethod(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 333 | if (GetCurrentQuickFrame() == NULL) { |
| 334 | if (kVerboseInstrumentation) { |
Daniel Mihalyi | ca1d06c | 2014-08-18 18:45:31 +0200 | [diff] [blame] | 335 | LOG(INFO) << " Ignoring a shadow frame. Frame " << GetFrameId() |
| 336 | << " Method=" << PrettyMethod(m); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 337 | } |
| 338 | return true; // Ignore shadow frames. |
| 339 | } |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 340 | if (m == NULL) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 341 | if (kVerboseInstrumentation) { |
| 342 | LOG(INFO) << " Skipping upcall. Frame " << GetFrameId(); |
| 343 | } |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 344 | return true; // Ignore upcalls. |
| 345 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 346 | bool removed_stub = false; |
| 347 | // TODO: make this search more efficient? |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 348 | const size_t frameId = GetFrameId(); |
| 349 | for (const InstrumentationStackFrame& instrumentation_frame : *instrumentation_stack_) { |
| 350 | if (instrumentation_frame.frame_id_ == frameId) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 351 | if (kVerboseInstrumentation) { |
| 352 | LOG(INFO) << " Removing exit stub in " << DescribeLocation(); |
| 353 | } |
Jeff Hao | 9a916d3 | 2013-06-27 18:45:37 -0700 | [diff] [blame] | 354 | if (instrumentation_frame.interpreter_entry_) { |
| 355 | CHECK(m == Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)); |
| 356 | } else { |
| 357 | CHECK(m == instrumentation_frame.method_) << PrettyMethod(m); |
| 358 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 359 | SetReturnPc(instrumentation_frame.return_pc_); |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 360 | if (instrumentation_->ShouldNotifyMethodEnterExitEvents()) { |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 361 | // Create the method exit events. As the methods didn't really exit the result is 0. |
| 362 | // We only do this if no debugger is attached to prevent from posting events twice. |
| 363 | instrumentation_->MethodExitEvent(thread_, instrumentation_frame.this_object_, m, |
| 364 | GetDexPc(), JValue()); |
| 365 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 366 | frames_removed_++; |
| 367 | removed_stub = true; |
| 368 | break; |
| 369 | } |
| 370 | } |
| 371 | if (!removed_stub) { |
| 372 | if (kVerboseInstrumentation) { |
| 373 | LOG(INFO) << " No exit stub in " << DescribeLocation(); |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 374 | } |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 375 | } |
| 376 | return true; // Continue. |
| 377 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 378 | Thread* const thread_; |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 379 | const uintptr_t instrumentation_exit_pc_; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 380 | Instrumentation* const instrumentation_; |
| 381 | std::deque<instrumentation::InstrumentationStackFrame>* const instrumentation_stack_; |
| 382 | size_t frames_removed_; |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 383 | }; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 384 | if (kVerboseInstrumentation) { |
| 385 | std::string thread_name; |
| 386 | thread->GetThreadName(thread_name); |
| 387 | LOG(INFO) << "Removing exit stubs in " << thread_name; |
| 388 | } |
| 389 | std::deque<instrumentation::InstrumentationStackFrame>* stack = thread->GetInstrumentationStack(); |
| 390 | if (stack->size() > 0) { |
| 391 | Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 392 | uintptr_t instrumentation_exit_pc = |
| 393 | reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 394 | RestoreStackVisitor visitor(thread, instrumentation_exit_pc, instrumentation); |
| 395 | visitor.WalkStack(true); |
| 396 | CHECK_EQ(visitor.frames_removed_, stack->size()); |
| 397 | while (stack->size() > 0) { |
| 398 | stack->pop_front(); |
| 399 | } |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 400 | } |
| 401 | } |
| 402 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 403 | void Instrumentation::AddListener(InstrumentationListener* listener, uint32_t events) { |
| 404 | Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current()); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 405 | if ((events & kMethodEntered) != 0) { |
| 406 | method_entry_listeners_.push_back(listener); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 407 | have_method_entry_listeners_ = true; |
| 408 | } |
| 409 | if ((events & kMethodExited) != 0) { |
| 410 | method_exit_listeners_.push_back(listener); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 411 | have_method_exit_listeners_ = true; |
| 412 | } |
| 413 | if ((events & kMethodUnwind) != 0) { |
| 414 | method_unwind_listeners_.push_back(listener); |
| 415 | have_method_unwind_listeners_ = true; |
| 416 | } |
| 417 | if ((events & kDexPcMoved) != 0) { |
Daniel Mihalyi | ca1d06c | 2014-08-18 18:45:31 +0200 | [diff] [blame] | 418 | std::list<InstrumentationListener*>* modified; |
| 419 | if (have_dex_pc_listeners_) { |
| 420 | modified = new std::list<InstrumentationListener*>(*dex_pc_listeners_.get()); |
| 421 | } else { |
| 422 | modified = new std::list<InstrumentationListener*>(); |
| 423 | } |
| 424 | modified->push_back(listener); |
| 425 | dex_pc_listeners_.reset(modified); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 426 | have_dex_pc_listeners_ = true; |
| 427 | } |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 428 | if ((events & kFieldRead) != 0) { |
Daniel Mihalyi | ca1d06c | 2014-08-18 18:45:31 +0200 | [diff] [blame] | 429 | std::list<InstrumentationListener*>* modified; |
| 430 | if (have_field_read_listeners_) { |
| 431 | modified = new std::list<InstrumentationListener*>(*field_read_listeners_.get()); |
| 432 | } else { |
| 433 | modified = new std::list<InstrumentationListener*>(); |
| 434 | } |
| 435 | modified->push_back(listener); |
| 436 | field_read_listeners_.reset(modified); |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 437 | have_field_read_listeners_ = true; |
| 438 | } |
| 439 | if ((events & kFieldWritten) != 0) { |
Daniel Mihalyi | ca1d06c | 2014-08-18 18:45:31 +0200 | [diff] [blame] | 440 | std::list<InstrumentationListener*>* modified; |
| 441 | if (have_field_write_listeners_) { |
| 442 | modified = new std::list<InstrumentationListener*>(*field_write_listeners_.get()); |
| 443 | } else { |
| 444 | modified = new std::list<InstrumentationListener*>(); |
| 445 | } |
| 446 | modified->push_back(listener); |
| 447 | field_write_listeners_.reset(modified); |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 448 | have_field_write_listeners_ = true; |
| 449 | } |
Jeff Hao | 14dd5a8 | 2013-04-11 10:23:36 -0700 | [diff] [blame] | 450 | if ((events & kExceptionCaught) != 0) { |
Daniel Mihalyi | ca1d06c | 2014-08-18 18:45:31 +0200 | [diff] [blame] | 451 | std::list<InstrumentationListener*>* modified; |
| 452 | if (have_exception_caught_listeners_) { |
| 453 | modified = new std::list<InstrumentationListener*>(*exception_caught_listeners_.get()); |
| 454 | } else { |
| 455 | modified = new std::list<InstrumentationListener*>(); |
| 456 | } |
| 457 | modified->push_back(listener); |
| 458 | exception_caught_listeners_.reset(modified); |
Jeff Hao | 14dd5a8 | 2013-04-11 10:23:36 -0700 | [diff] [blame] | 459 | have_exception_caught_listeners_ = true; |
| 460 | } |
Sebastien Hertz | ee1997a | 2013-09-19 14:47:09 +0200 | [diff] [blame] | 461 | UpdateInterpreterHandlerTable(); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 462 | } |
| 463 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 464 | void Instrumentation::RemoveListener(InstrumentationListener* listener, uint32_t events) { |
| 465 | Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current()); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 466 | |
| 467 | if ((events & kMethodEntered) != 0) { |
Daniel Mihalyi | ca1d06c | 2014-08-18 18:45:31 +0200 | [diff] [blame] | 468 | if (have_method_entry_listeners_) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 469 | method_entry_listeners_.remove(listener); |
Daniel Mihalyi | ca1d06c | 2014-08-18 18:45:31 +0200 | [diff] [blame] | 470 | have_method_entry_listeners_ = !method_entry_listeners_.empty(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 471 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 472 | } |
| 473 | if ((events & kMethodExited) != 0) { |
Daniel Mihalyi | ca1d06c | 2014-08-18 18:45:31 +0200 | [diff] [blame] | 474 | if (have_method_exit_listeners_) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 475 | method_exit_listeners_.remove(listener); |
Daniel Mihalyi | ca1d06c | 2014-08-18 18:45:31 +0200 | [diff] [blame] | 476 | have_method_exit_listeners_ = !method_exit_listeners_.empty(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 477 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 478 | } |
| 479 | if ((events & kMethodUnwind) != 0) { |
Daniel Mihalyi | ca1d06c | 2014-08-18 18:45:31 +0200 | [diff] [blame] | 480 | if (have_method_unwind_listeners_) { |
| 481 | method_unwind_listeners_.remove(listener); |
| 482 | have_method_unwind_listeners_ = !method_unwind_listeners_.empty(); |
| 483 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 484 | } |
| 485 | if ((events & kDexPcMoved) != 0) { |
Daniel Mihalyi | ca1d06c | 2014-08-18 18:45:31 +0200 | [diff] [blame] | 486 | if (have_dex_pc_listeners_) { |
| 487 | std::list<InstrumentationListener*>* modified = |
| 488 | new std::list<InstrumentationListener*>(*dex_pc_listeners_.get()); |
| 489 | modified->remove(listener); |
| 490 | have_dex_pc_listeners_ = !modified->empty(); |
| 491 | if (have_dex_pc_listeners_) { |
| 492 | dex_pc_listeners_.reset(modified); |
| 493 | } else { |
| 494 | dex_pc_listeners_.reset(); |
| 495 | delete modified; |
| 496 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 497 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 498 | } |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 499 | if ((events & kFieldRead) != 0) { |
Daniel Mihalyi | 6644521 | 2014-08-21 15:57:25 +0200 | [diff] [blame] | 500 | if (have_field_read_listeners_) { |
Daniel Mihalyi | ca1d06c | 2014-08-18 18:45:31 +0200 | [diff] [blame] | 501 | std::list<InstrumentationListener*>* modified = |
| 502 | new std::list<InstrumentationListener*>(*field_read_listeners_.get()); |
| 503 | modified->remove(listener); |
| 504 | have_field_read_listeners_ = !modified->empty(); |
| 505 | if (have_field_read_listeners_) { |
| 506 | field_read_listeners_.reset(modified); |
| 507 | } else { |
| 508 | field_read_listeners_.reset(); |
| 509 | delete modified; |
| 510 | } |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 511 | } |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 512 | } |
| 513 | if ((events & kFieldWritten) != 0) { |
Daniel Mihalyi | ca1d06c | 2014-08-18 18:45:31 +0200 | [diff] [blame] | 514 | if (have_field_write_listeners_) { |
| 515 | std::list<InstrumentationListener*>* modified = |
| 516 | new std::list<InstrumentationListener*>(*field_write_listeners_.get()); |
| 517 | modified->remove(listener); |
| 518 | have_field_write_listeners_ = !modified->empty(); |
| 519 | if (have_field_write_listeners_) { |
| 520 | field_write_listeners_.reset(modified); |
| 521 | } else { |
| 522 | field_write_listeners_.reset(); |
| 523 | delete modified; |
| 524 | } |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 525 | } |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 526 | } |
Jeff Hao | 14dd5a8 | 2013-04-11 10:23:36 -0700 | [diff] [blame] | 527 | if ((events & kExceptionCaught) != 0) { |
Daniel Mihalyi | ca1d06c | 2014-08-18 18:45:31 +0200 | [diff] [blame] | 528 | if (have_exception_caught_listeners_) { |
| 529 | std::list<InstrumentationListener*>* modified = |
| 530 | new std::list<InstrumentationListener*>(*exception_caught_listeners_.get()); |
| 531 | modified->remove(listener); |
| 532 | have_exception_caught_listeners_ = !modified->empty(); |
| 533 | if (have_exception_caught_listeners_) { |
| 534 | exception_caught_listeners_.reset(modified); |
| 535 | } else { |
| 536 | exception_caught_listeners_.reset(); |
| 537 | delete modified; |
| 538 | } |
| 539 | } |
Jeff Hao | 14dd5a8 | 2013-04-11 10:23:36 -0700 | [diff] [blame] | 540 | } |
Sebastien Hertz | ee1997a | 2013-09-19 14:47:09 +0200 | [diff] [blame] | 541 | UpdateInterpreterHandlerTable(); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 542 | } |
| 543 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 544 | void Instrumentation::ConfigureStubs(bool require_entry_exit_stubs, bool require_interpreter) { |
| 545 | interpret_only_ = require_interpreter || forced_interpret_only_; |
| 546 | // Compute what level of instrumentation is required and compare to current. |
| 547 | int desired_level, current_level; |
| 548 | if (require_interpreter) { |
| 549 | desired_level = 2; |
| 550 | } else if (require_entry_exit_stubs) { |
| 551 | desired_level = 1; |
| 552 | } else { |
| 553 | desired_level = 0; |
| 554 | } |
| 555 | if (interpreter_stubs_installed_) { |
| 556 | current_level = 2; |
| 557 | } else if (entry_exit_stubs_installed_) { |
| 558 | current_level = 1; |
| 559 | } else { |
| 560 | current_level = 0; |
| 561 | } |
| 562 | if (desired_level == current_level) { |
| 563 | // We're already set. |
| 564 | return; |
| 565 | } |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 566 | Thread* const self = Thread::Current(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 567 | Runtime* runtime = Runtime::Current(); |
| 568 | Locks::thread_list_lock_->AssertNotHeld(self); |
| 569 | if (desired_level > 0) { |
| 570 | if (require_interpreter) { |
| 571 | interpreter_stubs_installed_ = true; |
| 572 | } else { |
| 573 | CHECK(require_entry_exit_stubs); |
| 574 | entry_exit_stubs_installed_ = true; |
| 575 | } |
| 576 | runtime->GetClassLinker()->VisitClasses(InstallStubsClassVisitor, this); |
| 577 | instrumentation_stubs_installed_ = true; |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 578 | MutexLock mu(self, *Locks::thread_list_lock_); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 579 | runtime->GetThreadList()->ForEach(InstrumentationInstallStack, this); |
| 580 | } else { |
| 581 | interpreter_stubs_installed_ = false; |
| 582 | entry_exit_stubs_installed_ = false; |
| 583 | runtime->GetClassLinker()->VisitClasses(InstallStubsClassVisitor, this); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 584 | // Restore stack only if there is no method currently deoptimized. |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 585 | bool empty; |
| 586 | { |
| 587 | ReaderMutexLock mu(self, deoptimized_methods_lock_); |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 588 | empty = IsDeoptimizedMethodsEmpty(); // Avoid lock violation. |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 589 | } |
| 590 | if (empty) { |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 591 | instrumentation_stubs_installed_ = false; |
| 592 | MutexLock mu(self, *Locks::thread_list_lock_); |
| 593 | Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this); |
| 594 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 595 | } |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 596 | } |
| 597 | |
Ian Rogers | fa82427 | 2013-11-05 16:12:57 -0800 | [diff] [blame] | 598 | static void ResetQuickAllocEntryPointsForThread(Thread* thread, void* arg) { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 599 | UNUSED(arg); |
Ian Rogers | fa82427 | 2013-11-05 16:12:57 -0800 | [diff] [blame] | 600 | thread->ResetQuickAllocEntryPointsForThread(); |
| 601 | } |
| 602 | |
Mathieu Chartier | 9ef78b5 | 2014-09-25 17:03:12 -0700 | [diff] [blame] | 603 | void Instrumentation::SetEntrypointsInstrumented(bool instrumented) { |
| 604 | Thread* self = Thread::Current(); |
Mathieu Chartier | 661974a | 2014-01-09 11:23:53 -0800 | [diff] [blame] | 605 | Runtime* runtime = Runtime::Current(); |
| 606 | ThreadList* tl = runtime->GetThreadList(); |
Mathieu Chartier | 9ef78b5 | 2014-09-25 17:03:12 -0700 | [diff] [blame] | 607 | Locks::mutator_lock_->AssertNotHeld(self); |
| 608 | Locks::instrument_entrypoints_lock_->AssertHeld(self); |
| 609 | if (runtime->IsStarted()) { |
Mathieu Chartier | 661974a | 2014-01-09 11:23:53 -0800 | [diff] [blame] | 610 | tl->SuspendAll(); |
| 611 | } |
| 612 | { |
Mathieu Chartier | 9ef78b5 | 2014-09-25 17:03:12 -0700 | [diff] [blame] | 613 | MutexLock mu(self, *Locks::runtime_shutdown_lock_); |
Mathieu Chartier | 661974a | 2014-01-09 11:23:53 -0800 | [diff] [blame] | 614 | SetQuickAllocEntryPointsInstrumented(instrumented); |
| 615 | ResetQuickAllocEntryPoints(); |
| 616 | } |
Mathieu Chartier | 9ef78b5 | 2014-09-25 17:03:12 -0700 | [diff] [blame] | 617 | if (runtime->IsStarted()) { |
Mathieu Chartier | 661974a | 2014-01-09 11:23:53 -0800 | [diff] [blame] | 618 | tl->ResumeAll(); |
| 619 | } |
| 620 | } |
| 621 | |
Mathieu Chartier | 9ef78b5 | 2014-09-25 17:03:12 -0700 | [diff] [blame] | 622 | void Instrumentation::InstrumentQuickAllocEntryPoints() { |
| 623 | MutexLock mu(Thread::Current(), *Locks::instrument_entrypoints_lock_); |
| 624 | InstrumentQuickAllocEntryPointsLocked(); |
Ian Rogers | fa82427 | 2013-11-05 16:12:57 -0800 | [diff] [blame] | 625 | } |
| 626 | |
Mathieu Chartier | 9ef78b5 | 2014-09-25 17:03:12 -0700 | [diff] [blame] | 627 | void Instrumentation::UninstrumentQuickAllocEntryPoints() { |
| 628 | MutexLock mu(Thread::Current(), *Locks::instrument_entrypoints_lock_); |
| 629 | UninstrumentQuickAllocEntryPointsLocked(); |
| 630 | } |
| 631 | |
| 632 | void Instrumentation::InstrumentQuickAllocEntryPointsLocked() { |
| 633 | Locks::instrument_entrypoints_lock_->AssertHeld(Thread::Current()); |
| 634 | if (quick_alloc_entry_points_instrumentation_counter_ == 0) { |
| 635 | SetEntrypointsInstrumented(true); |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 636 | } |
Mathieu Chartier | 9ef78b5 | 2014-09-25 17:03:12 -0700 | [diff] [blame] | 637 | ++quick_alloc_entry_points_instrumentation_counter_; |
Mathieu Chartier | 9ef78b5 | 2014-09-25 17:03:12 -0700 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | void Instrumentation::UninstrumentQuickAllocEntryPointsLocked() { |
| 641 | Locks::instrument_entrypoints_lock_->AssertHeld(Thread::Current()); |
| 642 | CHECK_GT(quick_alloc_entry_points_instrumentation_counter_, 0U); |
| 643 | --quick_alloc_entry_points_instrumentation_counter_; |
| 644 | if (quick_alloc_entry_points_instrumentation_counter_ == 0) { |
| 645 | SetEntrypointsInstrumented(false); |
| 646 | } |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | void Instrumentation::ResetQuickAllocEntryPoints() { |
| 650 | Runtime* runtime = Runtime::Current(); |
| 651 | if (runtime->IsStarted()) { |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 652 | MutexLock mu(Thread::Current(), *Locks::thread_list_lock_); |
| 653 | runtime->GetThreadList()->ForEach(ResetQuickAllocEntryPointsForThread, NULL); |
Ian Rogers | fa82427 | 2013-11-05 16:12:57 -0800 | [diff] [blame] | 654 | } |
| 655 | } |
| 656 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 657 | void Instrumentation::UpdateMethodsCode(mirror::ArtMethod* method, const void* quick_code, |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 658 | const void* portable_code, bool have_portable_code) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 659 | const void* new_portable_code; |
| 660 | const void* new_quick_code; |
| 661 | bool new_have_portable_code; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 662 | if (LIKELY(!instrumentation_stubs_installed_)) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 663 | new_portable_code = portable_code; |
| 664 | new_quick_code = quick_code; |
| 665 | new_have_portable_code = have_portable_code; |
Jeff Hao | 65d15d9 | 2013-07-16 16:39:33 -0700 | [diff] [blame] | 666 | } else { |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 667 | if ((interpreter_stubs_installed_ || IsDeoptimized(method)) && !method->IsNative()) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 668 | new_portable_code = GetPortableToInterpreterBridge(); |
| 669 | new_quick_code = GetQuickToInterpreterBridge(); |
| 670 | new_have_portable_code = false; |
Jeff Hao | 65d15d9 | 2013-07-16 16:39:33 -0700 | [diff] [blame] | 671 | } else { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 672 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 673 | if (class_linker->IsQuickResolutionStub(quick_code) || |
| 674 | class_linker->IsQuickToInterpreterBridge(quick_code)) { |
| 675 | DCHECK(class_linker->IsPortableResolutionStub(portable_code) || |
| 676 | class_linker->IsPortableToInterpreterBridge(portable_code)); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 677 | new_portable_code = portable_code; |
| 678 | new_quick_code = quick_code; |
| 679 | new_have_portable_code = have_portable_code; |
| 680 | } else if (entry_exit_stubs_installed_) { |
| 681 | new_quick_code = GetQuickInstrumentationEntryPoint(); |
| 682 | new_portable_code = GetPortableToInterpreterBridge(); |
| 683 | new_have_portable_code = false; |
| 684 | } else { |
| 685 | new_portable_code = portable_code; |
| 686 | new_quick_code = quick_code; |
| 687 | new_have_portable_code = have_portable_code; |
| 688 | } |
Jeff Hao | 65d15d9 | 2013-07-16 16:39:33 -0700 | [diff] [blame] | 689 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 690 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 691 | UpdateEntrypoints(method, new_quick_code, new_portable_code, new_have_portable_code); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 692 | } |
| 693 | |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 694 | bool Instrumentation::AddDeoptimizedMethod(mirror::ArtMethod* method) { |
| 695 | // Note that the insert() below isn't read barrier-aware. So, this |
| 696 | // FindDeoptimizedMethod() call is necessary or else we would end up |
| 697 | // storing the same method twice in the map (the from-space and the |
| 698 | // to-space ones). |
| 699 | if (FindDeoptimizedMethod(method)) { |
| 700 | // Already in the map. Return. |
| 701 | return false; |
| 702 | } |
| 703 | // Not found. Add it. |
| 704 | int32_t hash_code = method->IdentityHashCode(); |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 705 | deoptimized_methods_.insert(std::make_pair(hash_code, GcRoot<mirror::ArtMethod>(method))); |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 706 | return true; |
| 707 | } |
| 708 | |
| 709 | bool Instrumentation::FindDeoptimizedMethod(mirror::ArtMethod* method) { |
| 710 | int32_t hash_code = method->IdentityHashCode(); |
| 711 | auto range = deoptimized_methods_.equal_range(hash_code); |
| 712 | for (auto it = range.first; it != range.second; ++it) { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 713 | mirror::ArtMethod* m = it->second.Read(); |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 714 | if (m == method) { |
| 715 | // Found. |
| 716 | return true; |
| 717 | } |
| 718 | } |
| 719 | // Not found. |
| 720 | return false; |
| 721 | } |
| 722 | |
| 723 | mirror::ArtMethod* Instrumentation::BeginDeoptimizedMethod() { |
| 724 | auto it = deoptimized_methods_.begin(); |
| 725 | if (it == deoptimized_methods_.end()) { |
| 726 | // Empty. |
| 727 | return nullptr; |
| 728 | } |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 729 | return it->second.Read(); |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 730 | } |
| 731 | |
| 732 | bool Instrumentation::RemoveDeoptimizedMethod(mirror::ArtMethod* method) { |
| 733 | int32_t hash_code = method->IdentityHashCode(); |
| 734 | auto range = deoptimized_methods_.equal_range(hash_code); |
| 735 | for (auto it = range.first; it != range.second; ++it) { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 736 | mirror::ArtMethod* m = it->second.Read(); |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 737 | if (m == method) { |
| 738 | // Found. Erase and return. |
| 739 | deoptimized_methods_.erase(it); |
| 740 | return true; |
| 741 | } |
| 742 | } |
| 743 | // Not found. |
| 744 | return false; |
| 745 | } |
| 746 | |
| 747 | bool Instrumentation::IsDeoptimizedMethodsEmpty() const { |
| 748 | return deoptimized_methods_.empty(); |
| 749 | } |
| 750 | |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 751 | void Instrumentation::Deoptimize(mirror::ArtMethod* method) { |
| 752 | CHECK(!method->IsNative()); |
| 753 | CHECK(!method->IsProxyMethod()); |
| 754 | CHECK(!method->IsAbstract()); |
| 755 | |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 756 | Thread* self = Thread::Current(); |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 757 | { |
| 758 | WriterMutexLock mu(self, deoptimized_methods_lock_); |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 759 | bool has_not_been_deoptimized = AddDeoptimizedMethod(method); |
Daniel Mihalyi | ca1d06c | 2014-08-18 18:45:31 +0200 | [diff] [blame] | 760 | CHECK(has_not_been_deoptimized) << "Method " << PrettyMethod(method) |
| 761 | << " is already deoptimized"; |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 762 | } |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 763 | if (!interpreter_stubs_installed_) { |
Sebastien Hertz | 320deb2 | 2014-06-11 19:45:05 +0200 | [diff] [blame] | 764 | UpdateEntrypoints(method, GetQuickInstrumentationEntryPoint(), GetPortableToInterpreterBridge(), |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 765 | false); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 766 | |
| 767 | // Install instrumentation exit stub and instrumentation frames. We may already have installed |
| 768 | // these previously so it will only cover the newly created frames. |
| 769 | instrumentation_stubs_installed_ = true; |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 770 | MutexLock mu(self, *Locks::thread_list_lock_); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 771 | Runtime::Current()->GetThreadList()->ForEach(InstrumentationInstallStack, this); |
| 772 | } |
| 773 | } |
| 774 | |
| 775 | void Instrumentation::Undeoptimize(mirror::ArtMethod* method) { |
| 776 | CHECK(!method->IsNative()); |
| 777 | CHECK(!method->IsProxyMethod()); |
| 778 | CHECK(!method->IsAbstract()); |
| 779 | |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 780 | Thread* self = Thread::Current(); |
| 781 | bool empty; |
| 782 | { |
| 783 | WriterMutexLock mu(self, deoptimized_methods_lock_); |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 784 | bool found_and_erased = RemoveDeoptimizedMethod(method); |
| 785 | CHECK(found_and_erased) << "Method " << PrettyMethod(method) |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 786 | << " is not deoptimized"; |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 787 | empty = IsDeoptimizedMethodsEmpty(); |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 788 | } |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 789 | |
| 790 | // Restore code and possibly stack only if we did not deoptimize everything. |
| 791 | if (!interpreter_stubs_installed_) { |
| 792 | // Restore its code or resolution trampoline. |
| 793 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 794 | if (method->IsStatic() && !method->IsConstructor() && |
| 795 | !method->GetDeclaringClass()->IsInitialized()) { |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 796 | UpdateEntrypoints(method, GetQuickResolutionStub(), GetPortableResolutionStub(), false); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 797 | } else { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 798 | bool have_portable_code = false; |
| 799 | const void* quick_code = class_linker->GetQuickOatCodeFor(method); |
| 800 | const void* portable_code = class_linker->GetPortableOatCodeFor(method, &have_portable_code); |
| 801 | UpdateEntrypoints(method, quick_code, portable_code, have_portable_code); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 802 | } |
| 803 | |
| 804 | // If there is no deoptimized method left, we can restore the stack of each thread. |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 805 | if (empty) { |
| 806 | MutexLock mu(self, *Locks::thread_list_lock_); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 807 | Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this); |
| 808 | instrumentation_stubs_installed_ = false; |
| 809 | } |
| 810 | } |
| 811 | } |
| 812 | |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 813 | bool Instrumentation::IsDeoptimized(mirror::ArtMethod* method) { |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 814 | DCHECK(method != nullptr); |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 815 | ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_); |
| 816 | return FindDeoptimizedMethod(method); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 817 | } |
| 818 | |
| 819 | void Instrumentation::EnableDeoptimization() { |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 820 | ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_); |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 821 | CHECK(IsDeoptimizedMethodsEmpty()); |
Sebastien Hertz | 11d40c2 | 2014-02-19 18:00:17 +0100 | [diff] [blame] | 822 | CHECK_EQ(deoptimization_enabled_, false); |
| 823 | deoptimization_enabled_ = true; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | void Instrumentation::DisableDeoptimization() { |
Sebastien Hertz | 11d40c2 | 2014-02-19 18:00:17 +0100 | [diff] [blame] | 827 | CHECK_EQ(deoptimization_enabled_, true); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 828 | // If we deoptimized everything, undo it. |
| 829 | if (interpreter_stubs_installed_) { |
| 830 | UndeoptimizeEverything(); |
| 831 | } |
| 832 | // Undeoptimized selected methods. |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 833 | while (true) { |
| 834 | mirror::ArtMethod* method; |
| 835 | { |
| 836 | ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_); |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 837 | if (IsDeoptimizedMethodsEmpty()) { |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 838 | break; |
| 839 | } |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 840 | method = BeginDeoptimizedMethod(); |
| 841 | CHECK(method != nullptr); |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 842 | } |
| 843 | Undeoptimize(method); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 844 | } |
Sebastien Hertz | 11d40c2 | 2014-02-19 18:00:17 +0100 | [diff] [blame] | 845 | deoptimization_enabled_ = false; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 846 | } |
| 847 | |
Sebastien Hertz | 11d40c2 | 2014-02-19 18:00:17 +0100 | [diff] [blame] | 848 | // Indicates if instrumentation should notify method enter/exit events to the listeners. |
| 849 | bool Instrumentation::ShouldNotifyMethodEnterExitEvents() const { |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 850 | return !deoptimization_enabled_ && !interpreter_stubs_installed_; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | void Instrumentation::DeoptimizeEverything() { |
| 854 | CHECK(!interpreter_stubs_installed_); |
| 855 | ConfigureStubs(false, true); |
| 856 | } |
| 857 | |
| 858 | void Instrumentation::UndeoptimizeEverything() { |
| 859 | CHECK(interpreter_stubs_installed_); |
| 860 | ConfigureStubs(false, false); |
| 861 | } |
| 862 | |
| 863 | void Instrumentation::EnableMethodTracing() { |
| 864 | bool require_interpreter = kDeoptimizeForAccurateMethodEntryExitListeners; |
| 865 | ConfigureStubs(!require_interpreter, require_interpreter); |
| 866 | } |
| 867 | |
| 868 | void Instrumentation::DisableMethodTracing() { |
| 869 | ConfigureStubs(false, false); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 870 | } |
| 871 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 872 | const void* Instrumentation::GetQuickCodeFor(mirror::ArtMethod* method) const { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 873 | Runtime* runtime = Runtime::Current(); |
| 874 | if (LIKELY(!instrumentation_stubs_installed_)) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 875 | const void* code = method->GetEntryPointFromQuickCompiledCode(); |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 876 | DCHECK(code != nullptr); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 877 | ClassLinker* class_linker = runtime->GetClassLinker(); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 878 | if (LIKELY(!class_linker->IsQuickResolutionStub(code) && |
| 879 | !class_linker->IsQuickToInterpreterBridge(code)) && |
| 880 | !class_linker->IsQuickResolutionStub(code) && |
| 881 | !class_linker->IsQuickToInterpreterBridge(code)) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 882 | return code; |
| 883 | } |
| 884 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 885 | return runtime->GetClassLinker()->GetQuickOatCodeFor(method); |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 886 | } |
| 887 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 888 | void Instrumentation::MethodEnterEventImpl(Thread* thread, mirror::Object* this_object, |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 889 | mirror::ArtMethod* method, |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 890 | uint32_t dex_pc) const { |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 891 | auto it = method_entry_listeners_.begin(); |
Jeff Hao | 65d15d9 | 2013-07-16 16:39:33 -0700 | [diff] [blame] | 892 | bool is_end = (it == method_entry_listeners_.end()); |
| 893 | // Implemented this way to prevent problems caused by modification of the list while iterating. |
| 894 | while (!is_end) { |
| 895 | InstrumentationListener* cur = *it; |
| 896 | ++it; |
| 897 | is_end = (it == method_entry_listeners_.end()); |
| 898 | cur->MethodEntered(thread, this_object, method, dex_pc); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 899 | } |
| 900 | } |
| 901 | |
| 902 | void Instrumentation::MethodExitEventImpl(Thread* thread, mirror::Object* this_object, |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 903 | mirror::ArtMethod* method, |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 904 | uint32_t dex_pc, const JValue& return_value) const { |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 905 | auto it = method_exit_listeners_.begin(); |
Jeff Hao | 65d15d9 | 2013-07-16 16:39:33 -0700 | [diff] [blame] | 906 | bool is_end = (it == method_exit_listeners_.end()); |
| 907 | // Implemented this way to prevent problems caused by modification of the list while iterating. |
| 908 | while (!is_end) { |
| 909 | InstrumentationListener* cur = *it; |
| 910 | ++it; |
| 911 | is_end = (it == method_exit_listeners_.end()); |
| 912 | cur->MethodExited(thread, this_object, method, dex_pc, return_value); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 913 | } |
| 914 | } |
| 915 | |
| 916 | void Instrumentation::MethodUnwindEvent(Thread* thread, mirror::Object* this_object, |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 917 | mirror::ArtMethod* method, |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 918 | uint32_t dex_pc) const { |
| 919 | if (have_method_unwind_listeners_) { |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 920 | for (InstrumentationListener* listener : method_unwind_listeners_) { |
Sebastien Hertz | 51db44a | 2013-11-19 10:00:29 +0100 | [diff] [blame] | 921 | listener->MethodUnwind(thread, this_object, method, dex_pc); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 922 | } |
| 923 | } |
| 924 | } |
| 925 | |
| 926 | void Instrumentation::DexPcMovedEventImpl(Thread* thread, mirror::Object* this_object, |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 927 | mirror::ArtMethod* method, |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 928 | uint32_t dex_pc) const { |
Daniel Mihalyi | ca1d06c | 2014-08-18 18:45:31 +0200 | [diff] [blame] | 929 | if (HasDexPcListeners()) { |
| 930 | std::shared_ptr<std::list<InstrumentationListener*>> original(dex_pc_listeners_); |
| 931 | for (InstrumentationListener* listener : *original.get()) { |
| 932 | listener->DexPcMoved(thread, this_object, method, dex_pc); |
| 933 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 934 | } |
| 935 | } |
| 936 | |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 937 | void Instrumentation::FieldReadEventImpl(Thread* thread, mirror::Object* this_object, |
| 938 | mirror::ArtMethod* method, uint32_t dex_pc, |
| 939 | mirror::ArtField* field) const { |
Daniel Mihalyi | ca1d06c | 2014-08-18 18:45:31 +0200 | [diff] [blame] | 940 | if (HasFieldReadListeners()) { |
| 941 | std::shared_ptr<std::list<InstrumentationListener*>> original(field_read_listeners_); |
| 942 | for (InstrumentationListener* listener : *original.get()) { |
| 943 | listener->FieldRead(thread, this_object, method, dex_pc, field); |
| 944 | } |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 945 | } |
| 946 | } |
| 947 | |
| 948 | void Instrumentation::FieldWriteEventImpl(Thread* thread, mirror::Object* this_object, |
| 949 | mirror::ArtMethod* method, uint32_t dex_pc, |
| 950 | mirror::ArtField* field, const JValue& field_value) const { |
Daniel Mihalyi | ca1d06c | 2014-08-18 18:45:31 +0200 | [diff] [blame] | 951 | if (HasFieldWriteListeners()) { |
| 952 | std::shared_ptr<std::list<InstrumentationListener*>> original(field_write_listeners_); |
| 953 | for (InstrumentationListener* listener : *original.get()) { |
| 954 | listener->FieldWritten(thread, this_object, method, dex_pc, field, field_value); |
| 955 | } |
Sebastien Hertz | 3f52eaf | 2014-04-04 17:50:18 +0200 | [diff] [blame] | 956 | } |
| 957 | } |
| 958 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 959 | void Instrumentation::ExceptionCaughtEvent(Thread* thread, const ThrowLocation& throw_location, |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 960 | mirror::ArtMethod* catch_method, |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 961 | uint32_t catch_dex_pc, |
Sebastien Hertz | 947ff08 | 2013-09-17 14:10:13 +0200 | [diff] [blame] | 962 | mirror::Throwable* exception_object) const { |
Sebastien Hertz | 9f10203 | 2014-05-23 08:59:42 +0200 | [diff] [blame] | 963 | if (HasExceptionCaughtListeners()) { |
| 964 | DCHECK_EQ(thread->GetException(nullptr), exception_object); |
| 965 | bool is_exception_reported = thread->IsExceptionReportedToInstrumentation(); |
Jeff Hao | c0bd4da | 2013-04-11 15:52:28 -0700 | [diff] [blame] | 966 | thread->ClearException(); |
Daniel Mihalyi | ca1d06c | 2014-08-18 18:45:31 +0200 | [diff] [blame] | 967 | std::shared_ptr<std::list<InstrumentationListener*>> original(exception_caught_listeners_); |
| 968 | for (InstrumentationListener* listener : *original.get()) { |
| 969 | listener->ExceptionCaught(thread, throw_location, catch_method, catch_dex_pc, |
| 970 | exception_object); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 971 | } |
Jeff Hao | c0bd4da | 2013-04-11 15:52:28 -0700 | [diff] [blame] | 972 | thread->SetException(throw_location, exception_object); |
Sebastien Hertz | 9f10203 | 2014-05-23 08:59:42 +0200 | [diff] [blame] | 973 | thread->SetExceptionReportedToInstrumentation(is_exception_reported); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 974 | } |
| 975 | } |
| 976 | |
| 977 | static void CheckStackDepth(Thread* self, const InstrumentationStackFrame& instrumentation_frame, |
| 978 | int delta) |
| 979 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 980 | size_t frame_id = StackVisitor::ComputeNumFrames(self) + delta; |
| 981 | if (frame_id != instrumentation_frame.frame_id_) { |
| 982 | LOG(ERROR) << "Expected frame_id=" << frame_id << " but found " |
| 983 | << instrumentation_frame.frame_id_; |
| 984 | StackVisitor::DescribeStack(self); |
| 985 | CHECK_EQ(frame_id, instrumentation_frame.frame_id_); |
| 986 | } |
| 987 | } |
| 988 | |
| 989 | void Instrumentation::PushInstrumentationStackFrame(Thread* self, mirror::Object* this_object, |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 990 | mirror::ArtMethod* method, |
Jeff Hao | 9a916d3 | 2013-06-27 18:45:37 -0700 | [diff] [blame] | 991 | uintptr_t lr, bool interpreter_entry) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 992 | // We have a callee-save frame meaning this value is guaranteed to never be 0. |
| 993 | size_t frame_id = StackVisitor::ComputeNumFrames(self); |
| 994 | std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack(); |
| 995 | if (kVerboseInstrumentation) { |
Brian Carlstrom | 2d88862 | 2013-07-18 17:02:00 -0700 | [diff] [blame] | 996 | LOG(INFO) << "Entering " << PrettyMethod(method) << " from PC " << reinterpret_cast<void*>(lr); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 997 | } |
| 998 | instrumentation::InstrumentationStackFrame instrumentation_frame(this_object, method, lr, |
Jeff Hao | 9a916d3 | 2013-06-27 18:45:37 -0700 | [diff] [blame] | 999 | frame_id, interpreter_entry); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1000 | stack->push_front(instrumentation_frame); |
| 1001 | |
Sebastien Hertz | 320deb2 | 2014-06-11 19:45:05 +0200 | [diff] [blame] | 1002 | if (!interpreter_entry) { |
| 1003 | MethodEnterEvent(self, this_object, method, 0); |
| 1004 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1005 | } |
| 1006 | |
Andreas Gampe | d58342c | 2014-06-05 14:18:08 -0700 | [diff] [blame] | 1007 | TwoWordReturn Instrumentation::PopInstrumentationStackFrame(Thread* self, uintptr_t* return_pc, |
| 1008 | uint64_t gpr_result, |
| 1009 | uint64_t fpr_result) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1010 | // Do the pop. |
| 1011 | std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack(); |
| 1012 | CHECK_GT(stack->size(), 0U); |
| 1013 | InstrumentationStackFrame instrumentation_frame = stack->front(); |
| 1014 | stack->pop_front(); |
| 1015 | |
| 1016 | // Set return PC and check the sanity of the stack. |
| 1017 | *return_pc = instrumentation_frame.return_pc_; |
| 1018 | CheckStackDepth(self, instrumentation_frame, 0); |
Ian Rogers | 1d8cdbc | 2014-09-22 22:51:09 -0700 | [diff] [blame] | 1019 | self->VerifyStack(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1020 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1021 | mirror::ArtMethod* method = instrumentation_frame.method_; |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1022 | uint32_t length; |
| 1023 | char return_shorty = method->GetShorty(&length)[0]; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1024 | JValue return_value; |
| 1025 | if (return_shorty == 'V') { |
| 1026 | return_value.SetJ(0); |
| 1027 | } else if (return_shorty == 'F' || return_shorty == 'D') { |
| 1028 | return_value.SetJ(fpr_result); |
| 1029 | } else { |
| 1030 | return_value.SetJ(gpr_result); |
| 1031 | } |
| 1032 | // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to |
| 1033 | // return_pc. |
| 1034 | uint32_t dex_pc = DexFile::kDexNoIndex; |
| 1035 | mirror::Object* this_object = instrumentation_frame.this_object_; |
Sebastien Hertz | 320deb2 | 2014-06-11 19:45:05 +0200 | [diff] [blame] | 1036 | if (!instrumentation_frame.interpreter_entry_) { |
| 1037 | MethodExitEvent(self, this_object, instrumentation_frame.method_, dex_pc, return_value); |
| 1038 | } |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 1039 | |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 1040 | // Deoptimize if the caller needs to continue execution in the interpreter. Do nothing if we get |
| 1041 | // back to an upcall. |
| 1042 | NthCallerVisitor visitor(self, 1, true); |
| 1043 | visitor.WalkStack(true); |
| 1044 | bool deoptimize = (visitor.caller != NULL) && |
| 1045 | (interpreter_stubs_installed_ || IsDeoptimized(visitor.caller)); |
| 1046 | if (deoptimize && kVerboseInstrumentation) { |
| 1047 | LOG(INFO) << "Deoptimizing into " << PrettyMethod(visitor.caller); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1048 | } |
| 1049 | if (deoptimize) { |
| 1050 | if (kVerboseInstrumentation) { |
| 1051 | LOG(INFO) << "Deoptimizing from " << PrettyMethod(method) |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 1052 | << " result is " << std::hex << return_value.GetJ(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1053 | } |
| 1054 | self->SetDeoptimizationReturnValue(return_value); |
Andreas Gampe | d58342c | 2014-06-05 14:18:08 -0700 | [diff] [blame] | 1055 | return GetTwoWordSuccessValue(*return_pc, |
| 1056 | reinterpret_cast<uintptr_t>(GetQuickDeoptimizationEntryPoint())); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1057 | } else { |
| 1058 | if (kVerboseInstrumentation) { |
Brian Carlstrom | 2d88862 | 2013-07-18 17:02:00 -0700 | [diff] [blame] | 1059 | LOG(INFO) << "Returning from " << PrettyMethod(method) |
| 1060 | << " to PC " << reinterpret_cast<void*>(*return_pc); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1061 | } |
Andreas Gampe | d58342c | 2014-06-05 14:18:08 -0700 | [diff] [blame] | 1062 | return GetTwoWordSuccessValue(0, *return_pc); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1063 | } |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 1064 | } |
| 1065 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1066 | void Instrumentation::PopMethodForUnwind(Thread* self, bool is_deoptimization) const { |
| 1067 | // Do the pop. |
| 1068 | std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack(); |
| 1069 | CHECK_GT(stack->size(), 0U); |
| 1070 | InstrumentationStackFrame instrumentation_frame = stack->front(); |
| 1071 | // TODO: bring back CheckStackDepth(self, instrumentation_frame, 2); |
| 1072 | stack->pop_front(); |
| 1073 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1074 | mirror::ArtMethod* method = instrumentation_frame.method_; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1075 | if (is_deoptimization) { |
| 1076 | if (kVerboseInstrumentation) { |
| 1077 | LOG(INFO) << "Popping for deoptimization " << PrettyMethod(method); |
| 1078 | } |
| 1079 | } else { |
| 1080 | if (kVerboseInstrumentation) { |
| 1081 | LOG(INFO) << "Popping for unwind " << PrettyMethod(method); |
| 1082 | } |
| 1083 | |
| 1084 | // Notify listeners of method unwind. |
| 1085 | // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to |
| 1086 | // return_pc. |
| 1087 | uint32_t dex_pc = DexFile::kDexNoIndex; |
| 1088 | MethodUnwindEvent(self, instrumentation_frame.this_object_, method, dex_pc); |
| 1089 | } |
| 1090 | } |
| 1091 | |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 1092 | void Instrumentation::VisitRoots(RootCallback* callback, void* arg) { |
| 1093 | WriterMutexLock mu(Thread::Current(), deoptimized_methods_lock_); |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 1094 | if (IsDeoptimizedMethodsEmpty()) { |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 1095 | return; |
| 1096 | } |
Hiroshi Yamauchi | 799eb3a | 2014-07-18 15:38:17 -0700 | [diff] [blame] | 1097 | for (auto pair : deoptimized_methods_) { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 1098 | pair.second.VisitRoot(callback, arg, 0, kRootVMInternal); |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 1099 | } |
Mathieu Chartier | 3b05e9b | 2014-03-25 09:29:43 -0700 | [diff] [blame] | 1100 | } |
| 1101 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1102 | std::string InstrumentationStackFrame::Dump() const { |
| 1103 | std::ostringstream os; |
| 1104 | os << "Frame " << frame_id_ << " " << PrettyMethod(method_) << ":" |
| 1105 | << reinterpret_cast<void*>(return_pc_) << " this=" << reinterpret_cast<void*>(this_object_); |
| 1106 | return os.str(); |
| 1107 | } |
| 1108 | |
| 1109 | } // namespace instrumentation |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 1110 | } // namespace art |