blob: ed64d7efbe553e4b1ab8e9365138a6d1549c1de7 [file] [log] [blame]
jeffhao725a9572012-11-13 18:20:12 -08001/*
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
Ian Rogersc7dd2952014-10-21 23:31:19 -070019#include <sstream>
20
Ian Rogerse63db272014-07-15 15:36:11 -070021#include "arch/context.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070022#include "art_method-inl.h"
Ian Rogersef7d42f2014-01-06 12:55:46 -080023#include "atomic.h"
jeffhao725a9572012-11-13 18:20:12 -080024#include "class_linker.h"
25#include "debugger.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080026#include "dex_file-inl.h"
Ian Rogersc7dd2952014-10-21 23:31:19 -070027#include "entrypoints/quick/quick_entrypoints.h"
Mathieu Chartierd8891782014-03-02 13:28:37 -080028#include "entrypoints/quick/quick_alloc_entrypoints.h"
Ian Rogers6f3dbba2014-10-14 17:41:57 -070029#include "entrypoints/runtime_asm_entrypoints.h"
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070030#include "gc_root-inl.h"
Sebastien Hertz138dbfc2013-12-04 18:15:25 +010031#include "interpreter/interpreter.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080032#include "jit/jit.h"
33#include "jit/jit_code_cache.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080034#include "mirror/class-inl.h"
35#include "mirror/dex_cache.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080036#include "mirror/object_array-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070037#include "mirror/object-inl.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080038#include "nth_caller_visitor.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010039#include "oat_quick_method_header.h"
jeffhao725a9572012-11-13 18:20:12 -080040#include "thread.h"
41#include "thread_list.h"
jeffhao725a9572012-11-13 18:20:12 -080042
43namespace art {
Ian Rogers62d6c772013-02-27 08:32:07 -080044namespace instrumentation {
jeffhao725a9572012-11-13 18:20:12 -080045
Sebastien Hertz0462c4c2015-04-01 16:34:17 +020046constexpr bool kVerboseInstrumentation = false;
Sebastien Hertz5bfd5c92013-11-15 11:36:07 +010047
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +010048// Instrumentation works on non-inlined frames by updating returned PCs
49// of compiled frames.
50static constexpr StackVisitor::StackWalkKind kInstrumentationStackWalk =
51 StackVisitor::StackWalkKind::kSkipInlinedFrames;
52
Mathieu Chartiere0671ce2015-07-28 17:23:28 -070053class InstallStubsClassVisitor : public ClassVisitor {
54 public:
55 explicit InstallStubsClassVisitor(Instrumentation* instrumentation)
56 : instrumentation_(instrumentation) {}
57
58 bool Visit(mirror::Class* klass) OVERRIDE REQUIRES(Locks::mutator_lock_) {
59 instrumentation_->InstallStubsForClass(klass);
60 return true; // we visit all classes.
61 }
62
63 private:
64 Instrumentation* const instrumentation_;
65};
66
Ian Rogers62d6c772013-02-27 08:32:07 -080067
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -070068Instrumentation::Instrumentation()
69 : instrumentation_stubs_installed_(false), entry_exit_stubs_installed_(false),
70 interpreter_stubs_installed_(false),
71 interpret_only_(false), forced_interpret_only_(false),
72 have_method_entry_listeners_(false), have_method_exit_listeners_(false),
73 have_method_unwind_listeners_(false), have_dex_pc_listeners_(false),
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +020074 have_field_read_listeners_(false), have_field_write_listeners_(false),
Sebastien Hertz0462c4c2015-04-01 16:34:17 +020075 have_exception_caught_listeners_(false), have_backward_branch_listeners_(false),
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -070076 deoptimized_methods_lock_("deoptimized methods lock"),
77 deoptimization_enabled_(false),
78 interpreter_handler_table_(kMainHandlerTable),
79 quick_alloc_entry_points_instrumentation_counter_(0) {
80}
81
Sebastien Hertza10aa372015-01-21 17:30:58 +010082void Instrumentation::InstallStubsForClass(mirror::Class* klass) {
Sebastien Hertza8a697f2015-01-15 12:28:47 +010083 if (klass->IsErroneous()) {
84 // We can't execute code in a erroneous class: do nothing.
85 } else if (!klass->IsResolved()) {
86 // We need the class to be resolved to install/uninstall stubs. Otherwise its methods
87 // could not be initialized or linked with regards to class inheritance.
88 } else {
89 for (size_t i = 0, e = klass->NumDirectMethods(); i < e; i++) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070090 InstallStubsForMethod(klass->GetDirectMethod(i, sizeof(void*)));
Sebastien Hertza8a697f2015-01-15 12:28:47 +010091 }
92 for (size_t i = 0, e = klass->NumVirtualMethods(); i < e; i++) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070093 InstallStubsForMethod(klass->GetVirtualMethod(i, sizeof(void*)));
Sebastien Hertza8a697f2015-01-15 12:28:47 +010094 }
jeffhao725a9572012-11-13 18:20:12 -080095 }
jeffhao725a9572012-11-13 18:20:12 -080096}
97
Mathieu Chartiere401d142015-04-22 13:56:20 -070098static void UpdateEntrypoints(ArtMethod* method, const void* quick_code)
Mathieu Chartier90443472015-07-16 20:32:27 -070099 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800100 Runtime* const runtime = Runtime::Current();
101 jit::Jit* jit = runtime->GetJit();
102 if (jit != nullptr) {
103 const void* old_code_ptr = method->GetEntryPointFromQuickCompiledCode();
104 jit::JitCodeCache* code_cache = jit->GetCodeCache();
105 if (code_cache->ContainsCodePtr(old_code_ptr)) {
106 // Save the old compiled code since we need it to implement ClassLinker::GetQuickOatCodeFor.
107 code_cache->SaveCompiledCode(method, old_code_ptr);
108 }
109 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800110 method->SetEntryPointFromQuickCompiledCode(quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100111}
112
Mathieu Chartiere401d142015-04-22 13:56:20 -0700113void Instrumentation::InstallStubsForMethod(ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100114 if (method->IsAbstract() || method->IsProxyMethod()) {
115 // Do not change stubs for these methods.
116 return;
117 }
Jeff Hao56802772014-08-19 10:17:36 -0700118 // Don't stub Proxy.<init>. Note that the Proxy class itself is not a proxy class.
119 if (method->IsConstructor() &&
120 method->GetDeclaringClass()->DescriptorEquals("Ljava/lang/reflect/Proxy;")) {
Jeff Haodb8a6642014-08-14 17:18:52 -0700121 return;
122 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800123 const void* new_quick_code;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100124 bool uninstall = !entry_exit_stubs_installed_ && !interpreter_stubs_installed_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800125 Runtime* const runtime = Runtime::Current();
126 ClassLinker* const class_linker = runtime->GetClassLinker();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100127 bool is_class_initialized = method->GetDeclaringClass()->IsInitialized();
128 if (uninstall) {
129 if ((forced_interpret_only_ || IsDeoptimized(method)) && !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800130 new_quick_code = GetQuickToInterpreterBridge();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100131 } else if (is_class_initialized || !method->IsStatic() || method->IsConstructor()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800132 new_quick_code = class_linker->GetQuickOatCodeFor(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100133 } else {
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700134 new_quick_code = GetQuickResolutionStub();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100135 }
136 } else { // !uninstall
Sebastien Hertzbae182c2013-12-17 10:42:03 +0100137 if ((interpreter_stubs_installed_ || forced_interpret_only_ || IsDeoptimized(method)) &&
138 !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800139 new_quick_code = GetQuickToInterpreterBridge();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100140 } else {
141 // Do not overwrite resolution trampoline. When the trampoline initializes the method's
142 // class, all its static methods code will be set to the instrumentation entry point.
143 // For more details, see ClassLinker::FixupStaticTrampolines.
144 if (is_class_initialized || !method->IsStatic() || method->IsConstructor()) {
Sebastien Hertz320deb22014-06-11 19:45:05 +0200145 if (entry_exit_stubs_installed_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800146 new_quick_code = GetQuickInstrumentationEntryPoint();
Sebastien Hertz320deb22014-06-11 19:45:05 +0200147 } else {
Sebastien Hertz320deb22014-06-11 19:45:05 +0200148 new_quick_code = class_linker->GetQuickOatCodeFor(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100149 }
150 } else {
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700151 new_quick_code = GetQuickResolutionStub();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100152 }
153 }
154 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800155 UpdateEntrypoints(method, new_quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100156}
157
Ian Rogers62d6c772013-02-27 08:32:07 -0800158// Places the instrumentation exit pc as the return PC for every quick frame. This also allows
159// deoptimization of quick frames to interpreter frames.
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100160// Since we may already have done this previously, we need to push new instrumentation frame before
161// existing instrumentation frames.
Ian Rogers62d6c772013-02-27 08:32:07 -0800162static void InstrumentationInstallStack(Thread* thread, void* arg)
Mathieu Chartier90443472015-07-16 20:32:27 -0700163 SHARED_REQUIRES(Locks::mutator_lock_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200164 struct InstallStackVisitor FINAL : public StackVisitor {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800165 InstallStackVisitor(Thread* thread_in, Context* context, uintptr_t instrumentation_exit_pc)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100166 : StackVisitor(thread_in, context, kInstrumentationStackWalk),
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800167 instrumentation_stack_(thread_in->GetInstrumentationStack()),
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100168 instrumentation_exit_pc_(instrumentation_exit_pc),
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100169 reached_existing_instrumentation_frames_(false), instrumentation_stack_depth_(0),
170 last_return_pc_(0) {
171 }
jeffhao725a9572012-11-13 18:20:12 -0800172
Mathieu Chartier90443472015-07-16 20:32:27 -0700173 bool VisitFrame() OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700174 ArtMethod* m = GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700175 if (m == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800176 if (kVerboseInstrumentation) {
177 LOG(INFO) << " Skipping upcall. Frame " << GetFrameId();
178 }
179 last_return_pc_ = 0;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700180 return true; // Ignore upcalls.
Ian Rogers306057f2012-11-26 12:45:53 -0800181 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700182 if (GetCurrentQuickFrame() == nullptr) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800183 bool interpreter_frame = true;
Sebastien Hertz320deb22014-06-11 19:45:05 +0200184 InstrumentationStackFrame instrumentation_frame(GetThisObject(), m, 0, GetFrameId(),
185 interpreter_frame);
Jeff Haoa15a81b2014-05-27 18:25:47 -0700186 if (kVerboseInstrumentation) {
187 LOG(INFO) << "Pushing shadow frame " << instrumentation_frame.Dump();
188 }
189 shadow_stack_.push_back(instrumentation_frame);
190 return true; // Continue.
191 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800192 uintptr_t return_pc = GetReturnPc();
Sebastien Hertz320deb22014-06-11 19:45:05 +0200193 if (m->IsRuntimeMethod()) {
194 if (return_pc == instrumentation_exit_pc_) {
195 if (kVerboseInstrumentation) {
196 LOG(INFO) << " Handling quick to interpreter transition. Frame " << GetFrameId();
197 }
198 CHECK_LT(instrumentation_stack_depth_, instrumentation_stack_->size());
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200199 const InstrumentationStackFrame& frame =
200 instrumentation_stack_->at(instrumentation_stack_depth_);
Sebastien Hertz320deb22014-06-11 19:45:05 +0200201 CHECK(frame.interpreter_entry_);
202 // This is an interpreter frame so method enter event must have been reported. However we
203 // need to push a DEX pc into the dex_pcs_ list to match size of instrumentation stack.
204 // Since we won't report method entry here, we can safely push any DEX pc.
205 dex_pcs_.push_back(0);
206 last_return_pc_ = frame.return_pc_;
207 ++instrumentation_stack_depth_;
208 return true;
209 } else {
210 if (kVerboseInstrumentation) {
211 LOG(INFO) << " Skipping runtime method. Frame " << GetFrameId();
212 }
213 last_return_pc_ = GetReturnPc();
214 return true; // Ignore unresolved methods since they will be instrumented after resolution.
215 }
216 }
217 if (kVerboseInstrumentation) {
218 LOG(INFO) << " Installing exit stub in " << DescribeLocation();
219 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100220 if (return_pc == instrumentation_exit_pc_) {
221 // We've reached a frame which has already been installed with instrumentation exit stub.
222 // We should have already installed instrumentation on previous frames.
223 reached_existing_instrumentation_frames_ = true;
224
225 CHECK_LT(instrumentation_stack_depth_, instrumentation_stack_->size());
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200226 const InstrumentationStackFrame& frame =
227 instrumentation_stack_->at(instrumentation_stack_depth_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100228 CHECK_EQ(m, frame.method_) << "Expected " << PrettyMethod(m)
229 << ", Found " << PrettyMethod(frame.method_);
230 return_pc = frame.return_pc_;
231 if (kVerboseInstrumentation) {
232 LOG(INFO) << "Ignoring already instrumented " << frame.Dump();
233 }
234 } else {
235 CHECK_NE(return_pc, 0U);
236 CHECK(!reached_existing_instrumentation_frames_);
237 InstrumentationStackFrame instrumentation_frame(GetThisObject(), m, return_pc, GetFrameId(),
238 false);
239 if (kVerboseInstrumentation) {
240 LOG(INFO) << "Pushing frame " << instrumentation_frame.Dump();
241 }
242
Sebastien Hertz320deb22014-06-11 19:45:05 +0200243 // Insert frame at the right position so we do not corrupt the instrumentation stack.
244 // Instrumentation stack frames are in descending frame id order.
245 auto it = instrumentation_stack_->begin();
246 for (auto end = instrumentation_stack_->end(); it != end; ++it) {
247 const InstrumentationStackFrame& current = *it;
248 if (instrumentation_frame.frame_id_ >= current.frame_id_) {
249 break;
250 }
251 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100252 instrumentation_stack_->insert(it, instrumentation_frame);
253 SetReturnPc(instrumentation_exit_pc_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800254 }
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100255 dex_pcs_.push_back((GetCurrentOatQuickMethodHeader() == nullptr)
256 ? DexFile::kDexNoIndex
257 : GetCurrentOatQuickMethodHeader()->ToDexPc(m, last_return_pc_));
Ian Rogers62d6c772013-02-27 08:32:07 -0800258 last_return_pc_ = return_pc;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100259 ++instrumentation_stack_depth_;
Ian Rogers306057f2012-11-26 12:45:53 -0800260 return true; // Continue.
261 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800262 std::deque<InstrumentationStackFrame>* const instrumentation_stack_;
Jeff Haoa15a81b2014-05-27 18:25:47 -0700263 std::vector<InstrumentationStackFrame> shadow_stack_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800264 std::vector<uint32_t> dex_pcs_;
Ian Rogers306057f2012-11-26 12:45:53 -0800265 const uintptr_t instrumentation_exit_pc_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100266 bool reached_existing_instrumentation_frames_;
267 size_t instrumentation_stack_depth_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800268 uintptr_t last_return_pc_;
Ian Rogers306057f2012-11-26 12:45:53 -0800269 };
Ian Rogers62d6c772013-02-27 08:32:07 -0800270 if (kVerboseInstrumentation) {
271 std::string thread_name;
272 thread->GetThreadName(thread_name);
273 LOG(INFO) << "Installing exit stubs in " << thread_name;
Ian Rogers306057f2012-11-26 12:45:53 -0800274 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100275
276 Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg);
Ian Rogers700a4022014-05-19 16:49:03 -0700277 std::unique_ptr<Context> context(Context::Create());
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700278 uintptr_t instrumentation_exit_pc = reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc());
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100279 InstallStackVisitor visitor(thread, context.get(), instrumentation_exit_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800280 visitor.WalkStack(true);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100281 CHECK_EQ(visitor.dex_pcs_.size(), thread->GetInstrumentationStack()->size());
Ian Rogers62d6c772013-02-27 08:32:07 -0800282
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100283 if (instrumentation->ShouldNotifyMethodEnterExitEvents()) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100284 // Create method enter events for all methods currently on the thread's stack. We only do this
285 // if no debugger is attached to prevent from posting events twice.
Jeff Haoa15a81b2014-05-27 18:25:47 -0700286 auto ssi = visitor.shadow_stack_.rbegin();
287 for (auto isi = thread->GetInstrumentationStack()->rbegin(),
288 end = thread->GetInstrumentationStack()->rend(); isi != end; ++isi) {
289 while (ssi != visitor.shadow_stack_.rend() && (*ssi).frame_id_ < (*isi).frame_id_) {
290 instrumentation->MethodEnterEvent(thread, (*ssi).this_object_, (*ssi).method_, 0);
291 ++ssi;
292 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100293 uint32_t dex_pc = visitor.dex_pcs_.back();
294 visitor.dex_pcs_.pop_back();
Sebastien Hertz320deb22014-06-11 19:45:05 +0200295 if (!isi->interpreter_entry_) {
296 instrumentation->MethodEnterEvent(thread, (*isi).this_object_, (*isi).method_, dex_pc);
297 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100298 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800299 }
300 thread->VerifyStack();
Ian Rogers306057f2012-11-26 12:45:53 -0800301}
302
Mingyao Yang99170c62015-07-06 11:10:37 -0700303void Instrumentation::InstrumentThreadStack(Thread* thread) {
304 instrumentation_stubs_installed_ = true;
305 InstrumentationInstallStack(thread, this);
306}
307
Ian Rogers62d6c772013-02-27 08:32:07 -0800308// Removes the instrumentation exit pc as the return PC for every quick frame.
309static void InstrumentationRestoreStack(Thread* thread, void* arg)
Mathieu Chartier90443472015-07-16 20:32:27 -0700310 SHARED_REQUIRES(Locks::mutator_lock_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200311 struct RestoreStackVisitor FINAL : public StackVisitor {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800312 RestoreStackVisitor(Thread* thread_in, uintptr_t instrumentation_exit_pc,
Ian Rogers62d6c772013-02-27 08:32:07 -0800313 Instrumentation* instrumentation)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100314 : StackVisitor(thread_in, nullptr, kInstrumentationStackWalk),
315 thread_(thread_in),
Ian Rogers62d6c772013-02-27 08:32:07 -0800316 instrumentation_exit_pc_(instrumentation_exit_pc),
317 instrumentation_(instrumentation),
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800318 instrumentation_stack_(thread_in->GetInstrumentationStack()),
Ian Rogers62d6c772013-02-27 08:32:07 -0800319 frames_removed_(0) {}
Ian Rogers306057f2012-11-26 12:45:53 -0800320
Mathieu Chartier90443472015-07-16 20:32:27 -0700321 bool VisitFrame() OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800322 if (instrumentation_stack_->size() == 0) {
jeffhao725a9572012-11-13 18:20:12 -0800323 return false; // Stop.
324 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700325 ArtMethod* m = GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700326 if (GetCurrentQuickFrame() == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800327 if (kVerboseInstrumentation) {
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200328 LOG(INFO) << " Ignoring a shadow frame. Frame " << GetFrameId()
329 << " Method=" << PrettyMethod(m);
Ian Rogers62d6c772013-02-27 08:32:07 -0800330 }
331 return true; // Ignore shadow frames.
332 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700333 if (m == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800334 if (kVerboseInstrumentation) {
335 LOG(INFO) << " Skipping upcall. Frame " << GetFrameId();
336 }
Ian Rogers306057f2012-11-26 12:45:53 -0800337 return true; // Ignore upcalls.
338 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800339 bool removed_stub = false;
340 // TODO: make this search more efficient?
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100341 const size_t frameId = GetFrameId();
342 for (const InstrumentationStackFrame& instrumentation_frame : *instrumentation_stack_) {
343 if (instrumentation_frame.frame_id_ == frameId) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800344 if (kVerboseInstrumentation) {
345 LOG(INFO) << " Removing exit stub in " << DescribeLocation();
346 }
Jeff Hao9a916d32013-06-27 18:45:37 -0700347 if (instrumentation_frame.interpreter_entry_) {
348 CHECK(m == Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs));
349 } else {
350 CHECK(m == instrumentation_frame.method_) << PrettyMethod(m);
351 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800352 SetReturnPc(instrumentation_frame.return_pc_);
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100353 if (instrumentation_->ShouldNotifyMethodEnterExitEvents()) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100354 // Create the method exit events. As the methods didn't really exit the result is 0.
355 // We only do this if no debugger is attached to prevent from posting events twice.
356 instrumentation_->MethodExitEvent(thread_, instrumentation_frame.this_object_, m,
357 GetDexPc(), JValue());
358 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800359 frames_removed_++;
360 removed_stub = true;
361 break;
362 }
363 }
364 if (!removed_stub) {
365 if (kVerboseInstrumentation) {
366 LOG(INFO) << " No exit stub in " << DescribeLocation();
Ian Rogers306057f2012-11-26 12:45:53 -0800367 }
jeffhao725a9572012-11-13 18:20:12 -0800368 }
369 return true; // Continue.
370 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800371 Thread* const thread_;
Ian Rogers306057f2012-11-26 12:45:53 -0800372 const uintptr_t instrumentation_exit_pc_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800373 Instrumentation* const instrumentation_;
374 std::deque<instrumentation::InstrumentationStackFrame>* const instrumentation_stack_;
375 size_t frames_removed_;
jeffhao725a9572012-11-13 18:20:12 -0800376 };
Ian Rogers62d6c772013-02-27 08:32:07 -0800377 if (kVerboseInstrumentation) {
378 std::string thread_name;
379 thread->GetThreadName(thread_name);
380 LOG(INFO) << "Removing exit stubs in " << thread_name;
381 }
382 std::deque<instrumentation::InstrumentationStackFrame>* stack = thread->GetInstrumentationStack();
383 if (stack->size() > 0) {
384 Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700385 uintptr_t instrumentation_exit_pc =
386 reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc());
Ian Rogers62d6c772013-02-27 08:32:07 -0800387 RestoreStackVisitor visitor(thread, instrumentation_exit_pc, instrumentation);
388 visitor.WalkStack(true);
389 CHECK_EQ(visitor.frames_removed_, stack->size());
390 while (stack->size() > 0) {
391 stack->pop_front();
392 }
jeffhao725a9572012-11-13 18:20:12 -0800393 }
394}
395
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200396static bool HasEvent(Instrumentation::InstrumentationEvent expected, uint32_t events) {
397 return (events & expected) != 0;
398}
399
Ian Rogers62d6c772013-02-27 08:32:07 -0800400void Instrumentation::AddListener(InstrumentationListener* listener, uint32_t events) {
401 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200402 if (HasEvent(kMethodEntered, events)) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800403 method_entry_listeners_.push_back(listener);
Ian Rogers62d6c772013-02-27 08:32:07 -0800404 have_method_entry_listeners_ = true;
405 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200406 if (HasEvent(kMethodExited, events)) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800407 method_exit_listeners_.push_back(listener);
Ian Rogers62d6c772013-02-27 08:32:07 -0800408 have_method_exit_listeners_ = true;
409 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200410 if (HasEvent(kMethodUnwind, events)) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800411 method_unwind_listeners_.push_back(listener);
412 have_method_unwind_listeners_ = true;
413 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200414 if (HasEvent(kBackwardBranch, events)) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800415 backward_branch_listeners_.push_back(listener);
416 have_backward_branch_listeners_ = true;
417 }
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100418 if (HasEvent(kInvokeVirtualOrInterface, events)) {
419 invoke_virtual_or_interface_listeners_.push_back(listener);
420 have_invoke_virtual_or_interface_listeners_ = true;
421 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200422 if (HasEvent(kDexPcMoved, events)) {
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200423 std::list<InstrumentationListener*>* modified;
424 if (have_dex_pc_listeners_) {
425 modified = new std::list<InstrumentationListener*>(*dex_pc_listeners_.get());
426 } else {
427 modified = new std::list<InstrumentationListener*>();
428 }
429 modified->push_back(listener);
430 dex_pc_listeners_.reset(modified);
Ian Rogers62d6c772013-02-27 08:32:07 -0800431 have_dex_pc_listeners_ = true;
432 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200433 if (HasEvent(kFieldRead, events)) {
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200434 std::list<InstrumentationListener*>* modified;
435 if (have_field_read_listeners_) {
436 modified = new std::list<InstrumentationListener*>(*field_read_listeners_.get());
437 } else {
438 modified = new std::list<InstrumentationListener*>();
439 }
440 modified->push_back(listener);
441 field_read_listeners_.reset(modified);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200442 have_field_read_listeners_ = true;
443 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200444 if (HasEvent(kFieldWritten, events)) {
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200445 std::list<InstrumentationListener*>* modified;
446 if (have_field_write_listeners_) {
447 modified = new std::list<InstrumentationListener*>(*field_write_listeners_.get());
448 } else {
449 modified = new std::list<InstrumentationListener*>();
450 }
451 modified->push_back(listener);
452 field_write_listeners_.reset(modified);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200453 have_field_write_listeners_ = true;
454 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200455 if (HasEvent(kExceptionCaught, events)) {
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200456 std::list<InstrumentationListener*>* modified;
457 if (have_exception_caught_listeners_) {
458 modified = new std::list<InstrumentationListener*>(*exception_caught_listeners_.get());
459 } else {
460 modified = new std::list<InstrumentationListener*>();
461 }
462 modified->push_back(listener);
463 exception_caught_listeners_.reset(modified);
Jeff Hao14dd5a82013-04-11 10:23:36 -0700464 have_exception_caught_listeners_ = true;
465 }
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200466 UpdateInterpreterHandlerTable();
jeffhao725a9572012-11-13 18:20:12 -0800467}
468
Ian Rogers62d6c772013-02-27 08:32:07 -0800469void Instrumentation::RemoveListener(InstrumentationListener* listener, uint32_t events) {
470 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
Ian Rogers62d6c772013-02-27 08:32:07 -0800471
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200472 if (HasEvent(kMethodEntered, events) && have_method_entry_listeners_) {
473 method_entry_listeners_.remove(listener);
474 have_method_entry_listeners_ = !method_entry_listeners_.empty();
Ian Rogers62d6c772013-02-27 08:32:07 -0800475 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200476 if (HasEvent(kMethodExited, events) && have_method_exit_listeners_) {
477 method_exit_listeners_.remove(listener);
478 have_method_exit_listeners_ = !method_exit_listeners_.empty();
Ian Rogers62d6c772013-02-27 08:32:07 -0800479 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200480 if (HasEvent(kMethodUnwind, events) && have_method_unwind_listeners_) {
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100481 method_unwind_listeners_.remove(listener);
482 have_method_unwind_listeners_ = !method_unwind_listeners_.empty();
Ian Rogers62d6c772013-02-27 08:32:07 -0800483 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200484 if (HasEvent(kBackwardBranch, events) && have_backward_branch_listeners_) {
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100485 backward_branch_listeners_.remove(listener);
486 have_backward_branch_listeners_ = !backward_branch_listeners_.empty();
487 }
488 if (HasEvent(kInvokeVirtualOrInterface, events) && have_invoke_virtual_or_interface_listeners_) {
489 invoke_virtual_or_interface_listeners_.remove(listener);
490 have_invoke_virtual_or_interface_listeners_ = !invoke_virtual_or_interface_listeners_.empty();
491 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200492 if (HasEvent(kDexPcMoved, events) && have_dex_pc_listeners_) {
493 std::list<InstrumentationListener*>* modified =
494 new std::list<InstrumentationListener*>(*dex_pc_listeners_.get());
495 modified->remove(listener);
496 have_dex_pc_listeners_ = !modified->empty();
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200497 if (have_dex_pc_listeners_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200498 dex_pc_listeners_.reset(modified);
499 } else {
500 dex_pc_listeners_.reset();
501 delete modified;
Ian Rogers62d6c772013-02-27 08:32:07 -0800502 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800503 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200504 if (HasEvent(kFieldRead, events) && have_field_read_listeners_) {
505 std::list<InstrumentationListener*>* modified =
506 new std::list<InstrumentationListener*>(*field_read_listeners_.get());
507 modified->remove(listener);
508 have_field_read_listeners_ = !modified->empty();
Daniel Mihalyi66445212014-08-21 15:57:25 +0200509 if (have_field_read_listeners_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200510 field_read_listeners_.reset(modified);
511 } else {
512 field_read_listeners_.reset();
513 delete modified;
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200514 }
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200515 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200516 if (HasEvent(kFieldWritten, events) && have_field_write_listeners_) {
517 std::list<InstrumentationListener*>* modified =
518 new std::list<InstrumentationListener*>(*field_write_listeners_.get());
519 modified->remove(listener);
520 have_field_write_listeners_ = !modified->empty();
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200521 if (have_field_write_listeners_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200522 field_write_listeners_.reset(modified);
523 } else {
524 field_write_listeners_.reset();
525 delete modified;
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200526 }
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200527 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200528 if (HasEvent(kExceptionCaught, events) && 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();
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200533 if (have_exception_caught_listeners_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200534 exception_caught_listeners_.reset(modified);
535 } else {
536 exception_caught_listeners_.reset();
537 delete modified;
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200538 }
Jeff Hao14dd5a82013-04-11 10:23:36 -0700539 }
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200540 UpdateInterpreterHandlerTable();
jeffhao725a9572012-11-13 18:20:12 -0800541}
542
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200543Instrumentation::InstrumentationLevel Instrumentation::GetCurrentInstrumentationLevel() const {
Ian Rogers62d6c772013-02-27 08:32:07 -0800544 if (interpreter_stubs_installed_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200545 return InstrumentationLevel::kInstrumentWithInterpreter;
Ian Rogers62d6c772013-02-27 08:32:07 -0800546 } else if (entry_exit_stubs_installed_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200547 return InstrumentationLevel::kInstrumentWithInstrumentationStubs;
Ian Rogers62d6c772013-02-27 08:32:07 -0800548 } else {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200549 return InstrumentationLevel::kInstrumentNothing;
Ian Rogers62d6c772013-02-27 08:32:07 -0800550 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200551}
552
553void Instrumentation::ConfigureStubs(const char* key, InstrumentationLevel desired_level) {
554 // Store the instrumentation level for this key or remove it.
555 if (desired_level == InstrumentationLevel::kInstrumentNothing) {
556 // The client no longer needs instrumentation.
557 requested_instrumentation_levels_.erase(key);
558 } else {
559 // The client needs instrumentation.
560 requested_instrumentation_levels_.Overwrite(key, desired_level);
561 }
562
563 // Look for the highest required instrumentation level.
564 InstrumentationLevel requested_level = InstrumentationLevel::kInstrumentNothing;
565 for (const auto& v : requested_instrumentation_levels_) {
566 requested_level = std::max(requested_level, v.second);
567 }
568
569 interpret_only_ = (requested_level == InstrumentationLevel::kInstrumentWithInterpreter) ||
570 forced_interpret_only_;
571
572 InstrumentationLevel current_level = GetCurrentInstrumentationLevel();
573 if (requested_level == current_level) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800574 // We're already set.
575 return;
576 }
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100577 Thread* const self = Thread::Current();
Ian Rogers62d6c772013-02-27 08:32:07 -0800578 Runtime* runtime = Runtime::Current();
Sebastien Hertza8a697f2015-01-15 12:28:47 +0100579 Locks::mutator_lock_->AssertExclusiveHeld(self);
Ian Rogers62d6c772013-02-27 08:32:07 -0800580 Locks::thread_list_lock_->AssertNotHeld(self);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200581 if (requested_level > InstrumentationLevel::kInstrumentNothing) {
582 if (requested_level == InstrumentationLevel::kInstrumentWithInterpreter) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800583 interpreter_stubs_installed_ = true;
Ian Rogers62d6c772013-02-27 08:32:07 -0800584 entry_exit_stubs_installed_ = true;
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200585 } else {
586 CHECK_EQ(requested_level, InstrumentationLevel::kInstrumentWithInstrumentationStubs);
587 entry_exit_stubs_installed_ = true;
588 interpreter_stubs_installed_ = false;
Ian Rogers62d6c772013-02-27 08:32:07 -0800589 }
Mathieu Chartiere0671ce2015-07-28 17:23:28 -0700590 InstallStubsClassVisitor visitor(this);
591 runtime->GetClassLinker()->VisitClasses(&visitor);
Ian Rogers62d6c772013-02-27 08:32:07 -0800592 instrumentation_stubs_installed_ = true;
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100593 MutexLock mu(self, *Locks::thread_list_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800594 runtime->GetThreadList()->ForEach(InstrumentationInstallStack, this);
595 } else {
596 interpreter_stubs_installed_ = false;
597 entry_exit_stubs_installed_ = false;
Mathieu Chartiere0671ce2015-07-28 17:23:28 -0700598 InstallStubsClassVisitor visitor(this);
599 runtime->GetClassLinker()->VisitClasses(&visitor);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100600 // Restore stack only if there is no method currently deoptimized.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700601 bool empty;
602 {
603 ReaderMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700604 empty = IsDeoptimizedMethodsEmpty(); // Avoid lock violation.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700605 }
606 if (empty) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100607 instrumentation_stubs_installed_ = false;
608 MutexLock mu(self, *Locks::thread_list_lock_);
609 Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this);
610 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800611 }
jeffhao725a9572012-11-13 18:20:12 -0800612}
613
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200614static void ResetQuickAllocEntryPointsForThread(Thread* thread, void* arg ATTRIBUTE_UNUSED) {
Ian Rogersfa824272013-11-05 16:12:57 -0800615 thread->ResetQuickAllocEntryPointsForThread();
616}
617
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700618void Instrumentation::SetEntrypointsInstrumented(bool instrumented) {
619 Thread* self = Thread::Current();
Mathieu Chartier661974a2014-01-09 11:23:53 -0800620 Runtime* runtime = Runtime::Current();
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700621 Locks::mutator_lock_->AssertNotHeld(self);
622 Locks::instrument_entrypoints_lock_->AssertHeld(self);
623 if (runtime->IsStarted()) {
Mathieu Chartier4f55e222015-09-04 13:26:21 -0700624 ScopedSuspendAll ssa(__FUNCTION__);
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700625 MutexLock mu(self, *Locks::runtime_shutdown_lock_);
Mathieu Chartier661974a2014-01-09 11:23:53 -0800626 SetQuickAllocEntryPointsInstrumented(instrumented);
627 ResetQuickAllocEntryPoints();
Mathieu Chartier4f55e222015-09-04 13:26:21 -0700628 } else {
629 MutexLock mu(self, *Locks::runtime_shutdown_lock_);
630 SetQuickAllocEntryPointsInstrumented(instrumented);
631 ResetQuickAllocEntryPoints();
Mathieu Chartier661974a2014-01-09 11:23:53 -0800632 }
633}
634
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700635void Instrumentation::InstrumentQuickAllocEntryPoints() {
636 MutexLock mu(Thread::Current(), *Locks::instrument_entrypoints_lock_);
637 InstrumentQuickAllocEntryPointsLocked();
Ian Rogersfa824272013-11-05 16:12:57 -0800638}
639
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700640void Instrumentation::UninstrumentQuickAllocEntryPoints() {
641 MutexLock mu(Thread::Current(), *Locks::instrument_entrypoints_lock_);
642 UninstrumentQuickAllocEntryPointsLocked();
643}
644
645void Instrumentation::InstrumentQuickAllocEntryPointsLocked() {
646 Locks::instrument_entrypoints_lock_->AssertHeld(Thread::Current());
647 if (quick_alloc_entry_points_instrumentation_counter_ == 0) {
648 SetEntrypointsInstrumented(true);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800649 }
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700650 ++quick_alloc_entry_points_instrumentation_counter_;
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700651}
652
653void Instrumentation::UninstrumentQuickAllocEntryPointsLocked() {
654 Locks::instrument_entrypoints_lock_->AssertHeld(Thread::Current());
655 CHECK_GT(quick_alloc_entry_points_instrumentation_counter_, 0U);
656 --quick_alloc_entry_points_instrumentation_counter_;
657 if (quick_alloc_entry_points_instrumentation_counter_ == 0) {
658 SetEntrypointsInstrumented(false);
659 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800660}
661
662void Instrumentation::ResetQuickAllocEntryPoints() {
663 Runtime* runtime = Runtime::Current();
664 if (runtime->IsStarted()) {
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800665 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700666 runtime->GetThreadList()->ForEach(ResetQuickAllocEntryPointsForThread, nullptr);
Ian Rogersfa824272013-11-05 16:12:57 -0800667 }
668}
669
Mathieu Chartiere401d142015-04-22 13:56:20 -0700670void Instrumentation::UpdateMethodsCode(ArtMethod* method, const void* quick_code) {
Sebastien Hertza8a697f2015-01-15 12:28:47 +0100671 DCHECK(method->GetDeclaringClass()->IsResolved());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800672 const void* new_quick_code;
Ian Rogers62d6c772013-02-27 08:32:07 -0800673 if (LIKELY(!instrumentation_stubs_installed_)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800674 new_quick_code = quick_code;
Jeff Hao65d15d92013-07-16 16:39:33 -0700675 } else {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100676 if ((interpreter_stubs_installed_ || IsDeoptimized(method)) && !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800677 new_quick_code = GetQuickToInterpreterBridge();
Jeff Hao65d15d92013-07-16 16:39:33 -0700678 } else {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700679 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700680 if (class_linker->IsQuickResolutionStub(quick_code) ||
681 class_linker->IsQuickToInterpreterBridge(quick_code)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700682 new_quick_code = quick_code;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700683 } else if (entry_exit_stubs_installed_) {
684 new_quick_code = GetQuickInstrumentationEntryPoint();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700685 } else {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700686 new_quick_code = quick_code;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700687 }
Jeff Hao65d15d92013-07-16 16:39:33 -0700688 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800689 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800690 UpdateEntrypoints(method, new_quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100691}
692
Mathieu Chartiere401d142015-04-22 13:56:20 -0700693bool Instrumentation::AddDeoptimizedMethod(ArtMethod* method) {
694 if (IsDeoptimizedMethod(method)) {
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700695 // Already in the map. Return.
696 return false;
697 }
698 // Not found. Add it.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700699 deoptimized_methods_.insert(method);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700700 return true;
701}
702
Mathieu Chartiere401d142015-04-22 13:56:20 -0700703bool Instrumentation::IsDeoptimizedMethod(ArtMethod* method) {
704 return deoptimized_methods_.find(method) != deoptimized_methods_.end();
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700705}
706
Mathieu Chartiere401d142015-04-22 13:56:20 -0700707ArtMethod* Instrumentation::BeginDeoptimizedMethod() {
708 if (deoptimized_methods_.empty()) {
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700709 // Empty.
710 return nullptr;
711 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700712 return *deoptimized_methods_.begin();
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700713}
714
Mathieu Chartiere401d142015-04-22 13:56:20 -0700715bool Instrumentation::RemoveDeoptimizedMethod(ArtMethod* method) {
716 auto it = deoptimized_methods_.find(method);
717 if (it == deoptimized_methods_.end()) {
718 return false;
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700719 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700720 deoptimized_methods_.erase(it);
721 return true;
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700722}
723
724bool Instrumentation::IsDeoptimizedMethodsEmpty() const {
725 return deoptimized_methods_.empty();
726}
727
Mathieu Chartiere401d142015-04-22 13:56:20 -0700728void Instrumentation::Deoptimize(ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100729 CHECK(!method->IsNative());
730 CHECK(!method->IsProxyMethod());
731 CHECK(!method->IsAbstract());
732
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700733 Thread* self = Thread::Current();
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700734 {
735 WriterMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700736 bool has_not_been_deoptimized = AddDeoptimizedMethod(method);
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200737 CHECK(has_not_been_deoptimized) << "Method " << PrettyMethod(method)
738 << " is already deoptimized";
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700739 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100740 if (!interpreter_stubs_installed_) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800741 UpdateEntrypoints(method, GetQuickInstrumentationEntryPoint());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100742
743 // Install instrumentation exit stub and instrumentation frames. We may already have installed
744 // these previously so it will only cover the newly created frames.
745 instrumentation_stubs_installed_ = true;
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700746 MutexLock mu(self, *Locks::thread_list_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100747 Runtime::Current()->GetThreadList()->ForEach(InstrumentationInstallStack, this);
748 }
749}
750
Mathieu Chartiere401d142015-04-22 13:56:20 -0700751void Instrumentation::Undeoptimize(ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100752 CHECK(!method->IsNative());
753 CHECK(!method->IsProxyMethod());
754 CHECK(!method->IsAbstract());
755
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700756 Thread* self = Thread::Current();
757 bool empty;
758 {
759 WriterMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700760 bool found_and_erased = RemoveDeoptimizedMethod(method);
761 CHECK(found_and_erased) << "Method " << PrettyMethod(method)
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700762 << " is not deoptimized";
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700763 empty = IsDeoptimizedMethodsEmpty();
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700764 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100765
766 // Restore code and possibly stack only if we did not deoptimize everything.
767 if (!interpreter_stubs_installed_) {
768 // Restore its code or resolution trampoline.
769 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800770 if (method->IsStatic() && !method->IsConstructor() &&
771 !method->GetDeclaringClass()->IsInitialized()) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800772 UpdateEntrypoints(method, GetQuickResolutionStub());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100773 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800774 const void* quick_code = class_linker->GetQuickOatCodeFor(method);
Elliott Hughes956af0f2014-12-11 14:34:28 -0800775 UpdateEntrypoints(method, quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100776 }
777
778 // If there is no deoptimized method left, we can restore the stack of each thread.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700779 if (empty) {
780 MutexLock mu(self, *Locks::thread_list_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100781 Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this);
782 instrumentation_stubs_installed_ = false;
783 }
784 }
785}
786
Mathieu Chartiere401d142015-04-22 13:56:20 -0700787bool Instrumentation::IsDeoptimized(ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100788 DCHECK(method != nullptr);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700789 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700790 return IsDeoptimizedMethod(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100791}
792
793void Instrumentation::EnableDeoptimization() {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700794 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700795 CHECK(IsDeoptimizedMethodsEmpty());
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100796 CHECK_EQ(deoptimization_enabled_, false);
797 deoptimization_enabled_ = true;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100798}
799
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200800void Instrumentation::DisableDeoptimization(const char* key) {
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100801 CHECK_EQ(deoptimization_enabled_, true);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100802 // If we deoptimized everything, undo it.
803 if (interpreter_stubs_installed_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200804 UndeoptimizeEverything(key);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100805 }
806 // Undeoptimized selected methods.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700807 while (true) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700808 ArtMethod* method;
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700809 {
810 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700811 if (IsDeoptimizedMethodsEmpty()) {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700812 break;
813 }
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700814 method = BeginDeoptimizedMethod();
815 CHECK(method != nullptr);
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700816 }
817 Undeoptimize(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100818 }
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100819 deoptimization_enabled_ = false;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100820}
821
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100822// Indicates if instrumentation should notify method enter/exit events to the listeners.
823bool Instrumentation::ShouldNotifyMethodEnterExitEvents() const {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200824 if (!HasMethodEntryListeners() && !HasMethodExitListeners()) {
825 return false;
826 }
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100827 return !deoptimization_enabled_ && !interpreter_stubs_installed_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100828}
829
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200830void Instrumentation::DeoptimizeEverything(const char* key) {
831 CHECK(deoptimization_enabled_);
832 ConfigureStubs(key, InstrumentationLevel::kInstrumentWithInterpreter);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100833}
834
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200835void Instrumentation::UndeoptimizeEverything(const char* key) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100836 CHECK(interpreter_stubs_installed_);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200837 CHECK(deoptimization_enabled_);
838 ConfigureStubs(key, InstrumentationLevel::kInstrumentNothing);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100839}
840
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200841void Instrumentation::EnableMethodTracing(const char* key, bool needs_interpreter) {
842 InstrumentationLevel level;
843 if (needs_interpreter) {
844 level = InstrumentationLevel::kInstrumentWithInterpreter;
845 } else {
846 level = InstrumentationLevel::kInstrumentWithInstrumentationStubs;
847 }
848 ConfigureStubs(key, level);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100849}
850
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200851void Instrumentation::DisableMethodTracing(const char* key) {
852 ConfigureStubs(key, InstrumentationLevel::kInstrumentNothing);
jeffhao725a9572012-11-13 18:20:12 -0800853}
854
Mathieu Chartiere401d142015-04-22 13:56:20 -0700855const void* Instrumentation::GetQuickCodeFor(ArtMethod* method, size_t pointer_size) const {
Ian Rogers62d6c772013-02-27 08:32:07 -0800856 Runtime* runtime = Runtime::Current();
857 if (LIKELY(!instrumentation_stubs_installed_)) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800858 const void* code = method->GetEntryPointFromQuickCompiledCodePtrSize(pointer_size);
Vladimir Marko8a630572014-04-09 18:45:35 +0100859 DCHECK(code != nullptr);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700860 ClassLinker* class_linker = runtime->GetClassLinker();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700861 if (LIKELY(!class_linker->IsQuickResolutionStub(code) &&
862 !class_linker->IsQuickToInterpreterBridge(code)) &&
863 !class_linker->IsQuickResolutionStub(code) &&
864 !class_linker->IsQuickToInterpreterBridge(code)) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800865 return code;
866 }
867 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800868 return runtime->GetClassLinker()->GetQuickOatCodeFor(method);
jeffhao725a9572012-11-13 18:20:12 -0800869}
870
Ian Rogers62d6c772013-02-27 08:32:07 -0800871void Instrumentation::MethodEnterEventImpl(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700872 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800873 uint32_t dex_pc) const {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700874 auto it = method_entry_listeners_.begin();
Jeff Hao65d15d92013-07-16 16:39:33 -0700875 bool is_end = (it == method_entry_listeners_.end());
876 // Implemented this way to prevent problems caused by modification of the list while iterating.
877 while (!is_end) {
878 InstrumentationListener* cur = *it;
879 ++it;
880 is_end = (it == method_entry_listeners_.end());
881 cur->MethodEntered(thread, this_object, method, dex_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800882 }
883}
884
885void Instrumentation::MethodExitEventImpl(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700886 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800887 uint32_t dex_pc, const JValue& return_value) const {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700888 auto it = method_exit_listeners_.begin();
Jeff Hao65d15d92013-07-16 16:39:33 -0700889 bool is_end = (it == method_exit_listeners_.end());
890 // Implemented this way to prevent problems caused by modification of the list while iterating.
891 while (!is_end) {
892 InstrumentationListener* cur = *it;
893 ++it;
894 is_end = (it == method_exit_listeners_.end());
895 cur->MethodExited(thread, this_object, method, dex_pc, return_value);
Ian Rogers62d6c772013-02-27 08:32:07 -0800896 }
897}
898
899void Instrumentation::MethodUnwindEvent(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700900 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800901 uint32_t dex_pc) const {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200902 if (HasMethodUnwindListeners()) {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700903 for (InstrumentationListener* listener : method_unwind_listeners_) {
Sebastien Hertz51db44a2013-11-19 10:00:29 +0100904 listener->MethodUnwind(thread, this_object, method, dex_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800905 }
906 }
907}
908
909void Instrumentation::DexPcMovedEventImpl(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700910 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800911 uint32_t dex_pc) const {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200912 std::shared_ptr<std::list<InstrumentationListener*>> original(dex_pc_listeners_);
913 for (InstrumentationListener* listener : *original.get()) {
914 listener->DexPcMoved(thread, this_object, method, dex_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800915 }
916}
917
Mathieu Chartiere401d142015-04-22 13:56:20 -0700918void Instrumentation::BackwardBranchImpl(Thread* thread, ArtMethod* method,
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800919 int32_t offset) const {
920 for (InstrumentationListener* listener : backward_branch_listeners_) {
921 listener->BackwardBranch(thread, method, offset);
922 }
923}
924
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100925void Instrumentation::InvokeVirtualOrInterfaceImpl(Thread* thread,
926 mirror::Object* this_object,
927 ArtMethod* caller,
928 uint32_t dex_pc,
929 ArtMethod* callee) const {
930 for (InstrumentationListener* listener : invoke_virtual_or_interface_listeners_) {
931 listener->InvokeVirtualOrInterface(thread, this_object, caller, dex_pc, callee);
932 }
933}
934
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200935void Instrumentation::FieldReadEventImpl(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700936 ArtMethod* method, uint32_t dex_pc,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700937 ArtField* field) const {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200938 std::shared_ptr<std::list<InstrumentationListener*>> original(field_read_listeners_);
939 for (InstrumentationListener* listener : *original.get()) {
940 listener->FieldRead(thread, this_object, method, dex_pc, field);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200941 }
942}
943
944void Instrumentation::FieldWriteEventImpl(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700945 ArtMethod* method, uint32_t dex_pc,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700946 ArtField* field, const JValue& field_value) const {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200947 std::shared_ptr<std::list<InstrumentationListener*>> original(field_write_listeners_);
948 for (InstrumentationListener* listener : *original.get()) {
949 listener->FieldWritten(thread, this_object, method, dex_pc, field, field_value);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200950 }
951}
952
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000953void Instrumentation::ExceptionCaughtEvent(Thread* thread,
Sebastien Hertz947ff082013-09-17 14:10:13 +0200954 mirror::Throwable* exception_object) const {
Sebastien Hertz9f102032014-05-23 08:59:42 +0200955 if (HasExceptionCaughtListeners()) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000956 DCHECK_EQ(thread->GetException(), exception_object);
Jeff Haoc0bd4da2013-04-11 15:52:28 -0700957 thread->ClearException();
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200958 std::shared_ptr<std::list<InstrumentationListener*>> original(exception_caught_listeners_);
959 for (InstrumentationListener* listener : *original.get()) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000960 listener->ExceptionCaught(thread, exception_object);
Ian Rogers62d6c772013-02-27 08:32:07 -0800961 }
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000962 thread->SetException(exception_object);
Ian Rogers62d6c772013-02-27 08:32:07 -0800963 }
964}
965
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +0000966// Computes a frame ID by ignoring inlined frames.
967size_t Instrumentation::ComputeFrameId(Thread* self,
968 size_t frame_depth,
969 size_t inlined_frames_before_frame) {
970 CHECK_GE(frame_depth, inlined_frames_before_frame);
971 size_t no_inline_depth = frame_depth - inlined_frames_before_frame;
972 return StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk) - no_inline_depth;
973}
974
Ian Rogers62d6c772013-02-27 08:32:07 -0800975static void CheckStackDepth(Thread* self, const InstrumentationStackFrame& instrumentation_frame,
976 int delta)
Mathieu Chartier90443472015-07-16 20:32:27 -0700977 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100978 size_t frame_id = StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk) + delta;
Ian Rogers62d6c772013-02-27 08:32:07 -0800979 if (frame_id != instrumentation_frame.frame_id_) {
980 LOG(ERROR) << "Expected frame_id=" << frame_id << " but found "
981 << instrumentation_frame.frame_id_;
982 StackVisitor::DescribeStack(self);
983 CHECK_EQ(frame_id, instrumentation_frame.frame_id_);
984 }
985}
986
987void Instrumentation::PushInstrumentationStackFrame(Thread* self, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700988 ArtMethod* method,
Jeff Hao9a916d32013-06-27 18:45:37 -0700989 uintptr_t lr, bool interpreter_entry) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800990 // We have a callee-save frame meaning this value is guaranteed to never be 0.
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100991 size_t frame_id = StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk);
Ian Rogers62d6c772013-02-27 08:32:07 -0800992 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
993 if (kVerboseInstrumentation) {
Brian Carlstrom2d888622013-07-18 17:02:00 -0700994 LOG(INFO) << "Entering " << PrettyMethod(method) << " from PC " << reinterpret_cast<void*>(lr);
Ian Rogers62d6c772013-02-27 08:32:07 -0800995 }
996 instrumentation::InstrumentationStackFrame instrumentation_frame(this_object, method, lr,
Jeff Hao9a916d32013-06-27 18:45:37 -0700997 frame_id, interpreter_entry);
Ian Rogers62d6c772013-02-27 08:32:07 -0800998 stack->push_front(instrumentation_frame);
999
Sebastien Hertz320deb22014-06-11 19:45:05 +02001000 if (!interpreter_entry) {
1001 MethodEnterEvent(self, this_object, method, 0);
1002 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001003}
1004
Andreas Gamped58342c2014-06-05 14:18:08 -07001005TwoWordReturn Instrumentation::PopInstrumentationStackFrame(Thread* self, uintptr_t* return_pc,
1006 uint64_t gpr_result,
1007 uint64_t fpr_result) {
Ian Rogers62d6c772013-02-27 08:32:07 -08001008 // Do the pop.
1009 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
1010 CHECK_GT(stack->size(), 0U);
1011 InstrumentationStackFrame instrumentation_frame = stack->front();
1012 stack->pop_front();
1013
1014 // Set return PC and check the sanity of the stack.
1015 *return_pc = instrumentation_frame.return_pc_;
1016 CheckStackDepth(self, instrumentation_frame, 0);
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001017 self->VerifyStack();
Ian Rogers62d6c772013-02-27 08:32:07 -08001018
Mathieu Chartiere401d142015-04-22 13:56:20 -07001019 ArtMethod* method = instrumentation_frame.method_;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001020 uint32_t length;
Mingyao Yang99170c62015-07-06 11:10:37 -07001021 const size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
1022 char return_shorty = method->GetInterfaceMethodIfProxy(pointer_size)->GetShorty(&length)[0];
Ian Rogers62d6c772013-02-27 08:32:07 -08001023 JValue return_value;
1024 if (return_shorty == 'V') {
1025 return_value.SetJ(0);
1026 } else if (return_shorty == 'F' || return_shorty == 'D') {
1027 return_value.SetJ(fpr_result);
1028 } else {
1029 return_value.SetJ(gpr_result);
1030 }
1031 // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to
1032 // return_pc.
1033 uint32_t dex_pc = DexFile::kDexNoIndex;
1034 mirror::Object* this_object = instrumentation_frame.this_object_;
Sebastien Hertz320deb22014-06-11 19:45:05 +02001035 if (!instrumentation_frame.interpreter_entry_) {
1036 MethodExitEvent(self, this_object, instrumentation_frame.method_, dex_pc, return_value);
1037 }
jeffhao725a9572012-11-13 18:20:12 -08001038
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01001039 // Deoptimize if the caller needs to continue execution in the interpreter. Do nothing if we get
1040 // back to an upcall.
1041 NthCallerVisitor visitor(self, 1, true);
1042 visitor.WalkStack(true);
Sebastien Hertz270a0e12015-01-16 19:49:09 +01001043 bool deoptimize = (visitor.caller != nullptr) &&
Daniel Mihalyieb076692014-08-22 17:33:31 +02001044 (interpreter_stubs_installed_ || IsDeoptimized(visitor.caller) ||
1045 Dbg::IsForcedInterpreterNeededForUpcall(self, visitor.caller));
Ian Rogers62d6c772013-02-27 08:32:07 -08001046 if (deoptimize) {
1047 if (kVerboseInstrumentation) {
Sebastien Hertz270a0e12015-01-16 19:49:09 +01001048 LOG(INFO) << StringPrintf("Deoptimizing %s by returning from %s with result %#" PRIx64 " in ",
1049 PrettyMethod(visitor.caller).c_str(),
1050 PrettyMethod(method).c_str(),
1051 return_value.GetJ()) << *self;
Ian Rogers62d6c772013-02-27 08:32:07 -08001052 }
Sebastien Hertz07474662015-08-25 15:12:33 +00001053 self->PushDeoptimizationContext(return_value, return_shorty == 'L',
1054 nullptr /* no pending exception */);
Andreas Gamped58342c2014-06-05 14:18:08 -07001055 return GetTwoWordSuccessValue(*return_pc,
1056 reinterpret_cast<uintptr_t>(GetQuickDeoptimizationEntryPoint()));
Ian Rogers62d6c772013-02-27 08:32:07 -08001057 } else {
1058 if (kVerboseInstrumentation) {
Brian Carlstrom2d888622013-07-18 17:02:00 -07001059 LOG(INFO) << "Returning from " << PrettyMethod(method)
1060 << " to PC " << reinterpret_cast<void*>(*return_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -08001061 }
Andreas Gamped58342c2014-06-05 14:18:08 -07001062 return GetTwoWordSuccessValue(0, *return_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -08001063 }
jeffhao725a9572012-11-13 18:20:12 -08001064}
1065
Ian Rogers62d6c772013-02-27 08:32:07 -08001066void 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
Mathieu Chartiere401d142015-04-22 13:56:20 -07001074 ArtMethod* method = instrumentation_frame.method_;
Ian Rogers62d6c772013-02-27 08:32:07 -08001075 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
1092std::string InstrumentationStackFrame::Dump() const {
1093 std::ostringstream os;
1094 os << "Frame " << frame_id_ << " " << PrettyMethod(method_) << ":"
1095 << reinterpret_cast<void*>(return_pc_) << " this=" << reinterpret_cast<void*>(this_object_);
1096 return os.str();
1097}
1098
1099} // namespace instrumentation
jeffhao725a9572012-11-13 18:20:12 -08001100} // namespace art