blob: f810bc8289df4ba559c674b85b329d0dfdb80554 [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"
Ian Rogersef7d42f2014-01-06 12:55:46 -080022#include "atomic.h"
jeffhao725a9572012-11-13 18:20:12 -080023#include "class_linker.h"
24#include "debugger.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080025#include "dex_file-inl.h"
Ian Rogersc7dd2952014-10-21 23:31:19 -070026#include "entrypoints/quick/quick_entrypoints.h"
Mathieu Chartierd8891782014-03-02 13:28:37 -080027#include "entrypoints/quick/quick_alloc_entrypoints.h"
Ian Rogers6f3dbba2014-10-14 17:41:57 -070028#include "entrypoints/runtime_asm_entrypoints.h"
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070029#include "gc_root-inl.h"
Sebastien Hertz138dbfc2013-12-04 18:15:25 +010030#include "interpreter/interpreter.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080031#include "jit/jit.h"
32#include "jit/jit_code_cache.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070033#include "mirror/art_method-inl.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"
jeffhao725a9572012-11-13 18:20:12 -080039#include "thread.h"
40#include "thread_list.h"
jeffhao725a9572012-11-13 18:20:12 -080041
42namespace art {
Ian Rogers62d6c772013-02-27 08:32:07 -080043namespace instrumentation {
jeffhao725a9572012-11-13 18:20:12 -080044
Sebastien Hertz0462c4c2015-04-01 16:34:17 +020045constexpr bool kVerboseInstrumentation = false;
Sebastien Hertz5bfd5c92013-11-15 11:36:07 +010046
Ian Rogers62d6c772013-02-27 08:32:07 -080047static bool InstallStubsClassVisitor(mirror::Class* klass, void* arg)
Sebastien Hertza8a697f2015-01-15 12:28:47 +010048 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -080049 Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg);
Sebastien Hertza10aa372015-01-21 17:30:58 +010050 instrumentation->InstallStubsForClass(klass);
51 return true; // we visit all classes.
Ian Rogers62d6c772013-02-27 08:32:07 -080052}
53
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -070054Instrumentation::Instrumentation()
55 : instrumentation_stubs_installed_(false), entry_exit_stubs_installed_(false),
56 interpreter_stubs_installed_(false),
57 interpret_only_(false), forced_interpret_only_(false),
58 have_method_entry_listeners_(false), have_method_exit_listeners_(false),
59 have_method_unwind_listeners_(false), have_dex_pc_listeners_(false),
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +020060 have_field_read_listeners_(false), have_field_write_listeners_(false),
Sebastien Hertz0462c4c2015-04-01 16:34:17 +020061 have_exception_caught_listeners_(false), have_backward_branch_listeners_(false),
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -070062 deoptimized_methods_lock_("deoptimized methods lock"),
63 deoptimization_enabled_(false),
64 interpreter_handler_table_(kMainHandlerTable),
65 quick_alloc_entry_points_instrumentation_counter_(0) {
66}
67
Sebastien Hertza10aa372015-01-21 17:30:58 +010068void Instrumentation::InstallStubsForClass(mirror::Class* klass) {
Sebastien Hertza8a697f2015-01-15 12:28:47 +010069 if (klass->IsErroneous()) {
70 // We can't execute code in a erroneous class: do nothing.
71 } else if (!klass->IsResolved()) {
72 // We need the class to be resolved to install/uninstall stubs. Otherwise its methods
73 // could not be initialized or linked with regards to class inheritance.
74 } else {
75 for (size_t i = 0, e = klass->NumDirectMethods(); i < e; i++) {
76 InstallStubsForMethod(klass->GetDirectMethod(i));
77 }
78 for (size_t i = 0, e = klass->NumVirtualMethods(); i < e; i++) {
79 InstallStubsForMethod(klass->GetVirtualMethod(i));
80 }
jeffhao725a9572012-11-13 18:20:12 -080081 }
jeffhao725a9572012-11-13 18:20:12 -080082}
83
Elliott Hughes956af0f2014-12-11 14:34:28 -080084static void UpdateEntrypoints(mirror::ArtMethod* method, const void* quick_code)
Ian Rogersef7d42f2014-01-06 12:55:46 -080085 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080086 Runtime* const runtime = Runtime::Current();
87 jit::Jit* jit = runtime->GetJit();
88 if (jit != nullptr) {
89 const void* old_code_ptr = method->GetEntryPointFromQuickCompiledCode();
90 jit::JitCodeCache* code_cache = jit->GetCodeCache();
91 if (code_cache->ContainsCodePtr(old_code_ptr)) {
92 // Save the old compiled code since we need it to implement ClassLinker::GetQuickOatCodeFor.
93 code_cache->SaveCompiledCode(method, old_code_ptr);
94 }
95 }
Ian Rogersef7d42f2014-01-06 12:55:46 -080096 method->SetEntryPointFromQuickCompiledCode(quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +010097 if (!method->IsResolutionMethod()) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -070098 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers6f3dbba2014-10-14 17:41:57 -070099 if (class_linker->IsQuickToInterpreterBridge(quick_code) ||
100 (class_linker->IsQuickResolutionStub(quick_code) &&
101 Runtime::Current()->GetInstrumentation()->IsForcedInterpretOnly() &&
102 !method->IsNative() && !method->IsProxyMethod())) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800103 DCHECK(!method->IsNative()) << PrettyMethod(method);
Hiroshi Yamauchi563b47c2014-02-28 17:18:37 -0800104 DCHECK(!method->IsProxyMethod()) << PrettyMethod(method);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700105 method->SetEntryPointFromInterpreter(art::artInterpreterToInterpreterBridge);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100106 } else {
107 method->SetEntryPointFromInterpreter(art::artInterpreterToCompiledCodeBridge);
108 }
109 }
110}
111
112void Instrumentation::InstallStubsForMethod(mirror::ArtMethod* method) {
113 if (method->IsAbstract() || method->IsProxyMethod()) {
114 // Do not change stubs for these methods.
115 return;
116 }
Jeff Hao56802772014-08-19 10:17:36 -0700117 // Don't stub Proxy.<init>. Note that the Proxy class itself is not a proxy class.
118 if (method->IsConstructor() &&
119 method->GetDeclaringClass()->DescriptorEquals("Ljava/lang/reflect/Proxy;")) {
Jeff Haodb8a6642014-08-14 17:18:52 -0700120 return;
121 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800122 const void* new_quick_code;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100123 bool uninstall = !entry_exit_stubs_installed_ && !interpreter_stubs_installed_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800124 Runtime* const runtime = Runtime::Current();
125 ClassLinker* const class_linker = runtime->GetClassLinker();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100126 bool is_class_initialized = method->GetDeclaringClass()->IsInitialized();
127 if (uninstall) {
128 if ((forced_interpret_only_ || IsDeoptimized(method)) && !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800129 new_quick_code = GetQuickToInterpreterBridge();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100130 } else if (is_class_initialized || !method->IsStatic() || method->IsConstructor()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800131 new_quick_code = class_linker->GetQuickOatCodeFor(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100132 } else {
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700133 new_quick_code = GetQuickResolutionStub();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100134 }
135 } else { // !uninstall
Sebastien Hertzbae182c2013-12-17 10:42:03 +0100136 if ((interpreter_stubs_installed_ || forced_interpret_only_ || IsDeoptimized(method)) &&
137 !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800138 new_quick_code = GetQuickToInterpreterBridge();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100139 } else {
140 // Do not overwrite resolution trampoline. When the trampoline initializes the method's
141 // class, all its static methods code will be set to the instrumentation entry point.
142 // For more details, see ClassLinker::FixupStaticTrampolines.
143 if (is_class_initialized || !method->IsStatic() || method->IsConstructor()) {
Sebastien Hertz320deb22014-06-11 19:45:05 +0200144 if (entry_exit_stubs_installed_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800145 new_quick_code = GetQuickInstrumentationEntryPoint();
Sebastien Hertz320deb22014-06-11 19:45:05 +0200146 } else {
Sebastien Hertz320deb22014-06-11 19:45:05 +0200147 new_quick_code = class_linker->GetQuickOatCodeFor(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100148 }
149 } else {
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700150 new_quick_code = GetQuickResolutionStub();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100151 }
152 }
153 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800154 UpdateEntrypoints(method, new_quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100155}
156
Ian Rogers62d6c772013-02-27 08:32:07 -0800157// Places the instrumentation exit pc as the return PC for every quick frame. This also allows
158// deoptimization of quick frames to interpreter frames.
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100159// Since we may already have done this previously, we need to push new instrumentation frame before
160// existing instrumentation frames.
Ian Rogers62d6c772013-02-27 08:32:07 -0800161static void InstrumentationInstallStack(Thread* thread, void* arg)
Ian Rogers306057f2012-11-26 12:45:53 -0800162 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200163 struct InstallStackVisitor FINAL : public StackVisitor {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800164 InstallStackVisitor(Thread* thread_in, Context* context, uintptr_t instrumentation_exit_pc)
165 : StackVisitor(thread_in, context),
166 instrumentation_stack_(thread_in->GetInstrumentationStack()),
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100167 instrumentation_exit_pc_(instrumentation_exit_pc),
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100168 reached_existing_instrumentation_frames_(false), instrumentation_stack_depth_(0),
169 last_return_pc_(0) {
170 }
jeffhao725a9572012-11-13 18:20:12 -0800171
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200172 bool VisitFrame() OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700173 mirror::ArtMethod* m = GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700174 if (m == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800175 if (kVerboseInstrumentation) {
176 LOG(INFO) << " Skipping upcall. Frame " << GetFrameId();
177 }
178 last_return_pc_ = 0;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700179 return true; // Ignore upcalls.
Ian Rogers306057f2012-11-26 12:45:53 -0800180 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700181 if (GetCurrentQuickFrame() == nullptr) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800182 bool interpreter_frame = true;
Sebastien Hertz320deb22014-06-11 19:45:05 +0200183 InstrumentationStackFrame instrumentation_frame(GetThisObject(), m, 0, GetFrameId(),
184 interpreter_frame);
Jeff Haoa15a81b2014-05-27 18:25:47 -0700185 if (kVerboseInstrumentation) {
186 LOG(INFO) << "Pushing shadow frame " << instrumentation_frame.Dump();
187 }
188 shadow_stack_.push_back(instrumentation_frame);
189 return true; // Continue.
190 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800191 uintptr_t return_pc = GetReturnPc();
Sebastien Hertz320deb22014-06-11 19:45:05 +0200192 if (m->IsRuntimeMethod()) {
193 if (return_pc == instrumentation_exit_pc_) {
194 if (kVerboseInstrumentation) {
195 LOG(INFO) << " Handling quick to interpreter transition. Frame " << GetFrameId();
196 }
197 CHECK_LT(instrumentation_stack_depth_, instrumentation_stack_->size());
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200198 const InstrumentationStackFrame& frame =
199 instrumentation_stack_->at(instrumentation_stack_depth_);
Sebastien Hertz320deb22014-06-11 19:45:05 +0200200 CHECK(frame.interpreter_entry_);
201 // This is an interpreter frame so method enter event must have been reported. However we
202 // need to push a DEX pc into the dex_pcs_ list to match size of instrumentation stack.
203 // Since we won't report method entry here, we can safely push any DEX pc.
204 dex_pcs_.push_back(0);
205 last_return_pc_ = frame.return_pc_;
206 ++instrumentation_stack_depth_;
207 return true;
208 } else {
209 if (kVerboseInstrumentation) {
210 LOG(INFO) << " Skipping runtime method. Frame " << GetFrameId();
211 }
212 last_return_pc_ = GetReturnPc();
213 return true; // Ignore unresolved methods since they will be instrumented after resolution.
214 }
215 }
216 if (kVerboseInstrumentation) {
217 LOG(INFO) << " Installing exit stub in " << DescribeLocation();
218 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100219 if (return_pc == instrumentation_exit_pc_) {
220 // We've reached a frame which has already been installed with instrumentation exit stub.
221 // We should have already installed instrumentation on previous frames.
222 reached_existing_instrumentation_frames_ = true;
223
224 CHECK_LT(instrumentation_stack_depth_, instrumentation_stack_->size());
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200225 const InstrumentationStackFrame& frame =
226 instrumentation_stack_->at(instrumentation_stack_depth_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100227 CHECK_EQ(m, frame.method_) << "Expected " << PrettyMethod(m)
228 << ", Found " << PrettyMethod(frame.method_);
229 return_pc = frame.return_pc_;
230 if (kVerboseInstrumentation) {
231 LOG(INFO) << "Ignoring already instrumented " << frame.Dump();
232 }
233 } else {
234 CHECK_NE(return_pc, 0U);
235 CHECK(!reached_existing_instrumentation_frames_);
236 InstrumentationStackFrame instrumentation_frame(GetThisObject(), m, return_pc, GetFrameId(),
237 false);
238 if (kVerboseInstrumentation) {
239 LOG(INFO) << "Pushing frame " << instrumentation_frame.Dump();
240 }
241
Sebastien Hertz320deb22014-06-11 19:45:05 +0200242 // Insert frame at the right position so we do not corrupt the instrumentation stack.
243 // Instrumentation stack frames are in descending frame id order.
244 auto it = instrumentation_stack_->begin();
245 for (auto end = instrumentation_stack_->end(); it != end; ++it) {
246 const InstrumentationStackFrame& current = *it;
247 if (instrumentation_frame.frame_id_ >= current.frame_id_) {
248 break;
249 }
250 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100251 instrumentation_stack_->insert(it, instrumentation_frame);
252 SetReturnPc(instrumentation_exit_pc_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800253 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800254 dex_pcs_.push_back(m->ToDexPc(last_return_pc_));
Ian Rogers62d6c772013-02-27 08:32:07 -0800255 last_return_pc_ = return_pc;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100256 ++instrumentation_stack_depth_;
Ian Rogers306057f2012-11-26 12:45:53 -0800257 return true; // Continue.
258 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800259 std::deque<InstrumentationStackFrame>* const instrumentation_stack_;
Jeff Haoa15a81b2014-05-27 18:25:47 -0700260 std::vector<InstrumentationStackFrame> shadow_stack_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800261 std::vector<uint32_t> dex_pcs_;
Ian Rogers306057f2012-11-26 12:45:53 -0800262 const uintptr_t instrumentation_exit_pc_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100263 bool reached_existing_instrumentation_frames_;
264 size_t instrumentation_stack_depth_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800265 uintptr_t last_return_pc_;
Ian Rogers306057f2012-11-26 12:45:53 -0800266 };
Ian Rogers62d6c772013-02-27 08:32:07 -0800267 if (kVerboseInstrumentation) {
268 std::string thread_name;
269 thread->GetThreadName(thread_name);
270 LOG(INFO) << "Installing exit stubs in " << thread_name;
Ian Rogers306057f2012-11-26 12:45:53 -0800271 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100272
273 Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg);
Ian Rogers700a4022014-05-19 16:49:03 -0700274 std::unique_ptr<Context> context(Context::Create());
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700275 uintptr_t instrumentation_exit_pc = reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc());
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100276 InstallStackVisitor visitor(thread, context.get(), instrumentation_exit_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800277 visitor.WalkStack(true);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100278 CHECK_EQ(visitor.dex_pcs_.size(), thread->GetInstrumentationStack()->size());
Ian Rogers62d6c772013-02-27 08:32:07 -0800279
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100280 if (instrumentation->ShouldNotifyMethodEnterExitEvents()) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100281 // Create method enter events for all methods currently on the thread's stack. We only do this
282 // if no debugger is attached to prevent from posting events twice.
Jeff Haoa15a81b2014-05-27 18:25:47 -0700283 auto ssi = visitor.shadow_stack_.rbegin();
284 for (auto isi = thread->GetInstrumentationStack()->rbegin(),
285 end = thread->GetInstrumentationStack()->rend(); isi != end; ++isi) {
286 while (ssi != visitor.shadow_stack_.rend() && (*ssi).frame_id_ < (*isi).frame_id_) {
287 instrumentation->MethodEnterEvent(thread, (*ssi).this_object_, (*ssi).method_, 0);
288 ++ssi;
289 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100290 uint32_t dex_pc = visitor.dex_pcs_.back();
291 visitor.dex_pcs_.pop_back();
Sebastien Hertz320deb22014-06-11 19:45:05 +0200292 if (!isi->interpreter_entry_) {
293 instrumentation->MethodEnterEvent(thread, (*isi).this_object_, (*isi).method_, dex_pc);
294 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100295 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800296 }
297 thread->VerifyStack();
Ian Rogers306057f2012-11-26 12:45:53 -0800298}
299
Ian Rogers62d6c772013-02-27 08:32:07 -0800300// Removes the instrumentation exit pc as the return PC for every quick frame.
301static void InstrumentationRestoreStack(Thread* thread, void* arg)
Ian Rogers306057f2012-11-26 12:45:53 -0800302 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200303 struct RestoreStackVisitor FINAL : public StackVisitor {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800304 RestoreStackVisitor(Thread* thread_in, uintptr_t instrumentation_exit_pc,
Ian Rogers62d6c772013-02-27 08:32:07 -0800305 Instrumentation* instrumentation)
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700306 : StackVisitor(thread_in, nullptr), thread_(thread_in),
Ian Rogers62d6c772013-02-27 08:32:07 -0800307 instrumentation_exit_pc_(instrumentation_exit_pc),
308 instrumentation_(instrumentation),
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800309 instrumentation_stack_(thread_in->GetInstrumentationStack()),
Ian Rogers62d6c772013-02-27 08:32:07 -0800310 frames_removed_(0) {}
Ian Rogers306057f2012-11-26 12:45:53 -0800311
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200312 bool VisitFrame() OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800313 if (instrumentation_stack_->size() == 0) {
jeffhao725a9572012-11-13 18:20:12 -0800314 return false; // Stop.
315 }
Brian Carlstromea46f952013-07-30 01:26:50 -0700316 mirror::ArtMethod* m = GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700317 if (GetCurrentQuickFrame() == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800318 if (kVerboseInstrumentation) {
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200319 LOG(INFO) << " Ignoring a shadow frame. Frame " << GetFrameId()
320 << " Method=" << PrettyMethod(m);
Ian Rogers62d6c772013-02-27 08:32:07 -0800321 }
322 return true; // Ignore shadow frames.
323 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700324 if (m == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800325 if (kVerboseInstrumentation) {
326 LOG(INFO) << " Skipping upcall. Frame " << GetFrameId();
327 }
Ian Rogers306057f2012-11-26 12:45:53 -0800328 return true; // Ignore upcalls.
329 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800330 bool removed_stub = false;
331 // TODO: make this search more efficient?
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100332 const size_t frameId = GetFrameId();
333 for (const InstrumentationStackFrame& instrumentation_frame : *instrumentation_stack_) {
334 if (instrumentation_frame.frame_id_ == frameId) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800335 if (kVerboseInstrumentation) {
336 LOG(INFO) << " Removing exit stub in " << DescribeLocation();
337 }
Jeff Hao9a916d32013-06-27 18:45:37 -0700338 if (instrumentation_frame.interpreter_entry_) {
339 CHECK(m == Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs));
340 } else {
341 CHECK(m == instrumentation_frame.method_) << PrettyMethod(m);
342 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800343 SetReturnPc(instrumentation_frame.return_pc_);
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100344 if (instrumentation_->ShouldNotifyMethodEnterExitEvents()) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100345 // Create the method exit events. As the methods didn't really exit the result is 0.
346 // We only do this if no debugger is attached to prevent from posting events twice.
347 instrumentation_->MethodExitEvent(thread_, instrumentation_frame.this_object_, m,
348 GetDexPc(), JValue());
349 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800350 frames_removed_++;
351 removed_stub = true;
352 break;
353 }
354 }
355 if (!removed_stub) {
356 if (kVerboseInstrumentation) {
357 LOG(INFO) << " No exit stub in " << DescribeLocation();
Ian Rogers306057f2012-11-26 12:45:53 -0800358 }
jeffhao725a9572012-11-13 18:20:12 -0800359 }
360 return true; // Continue.
361 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800362 Thread* const thread_;
Ian Rogers306057f2012-11-26 12:45:53 -0800363 const uintptr_t instrumentation_exit_pc_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800364 Instrumentation* const instrumentation_;
365 std::deque<instrumentation::InstrumentationStackFrame>* const instrumentation_stack_;
366 size_t frames_removed_;
jeffhao725a9572012-11-13 18:20:12 -0800367 };
Ian Rogers62d6c772013-02-27 08:32:07 -0800368 if (kVerboseInstrumentation) {
369 std::string thread_name;
370 thread->GetThreadName(thread_name);
371 LOG(INFO) << "Removing exit stubs in " << thread_name;
372 }
373 std::deque<instrumentation::InstrumentationStackFrame>* stack = thread->GetInstrumentationStack();
374 if (stack->size() > 0) {
375 Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700376 uintptr_t instrumentation_exit_pc =
377 reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc());
Ian Rogers62d6c772013-02-27 08:32:07 -0800378 RestoreStackVisitor visitor(thread, instrumentation_exit_pc, instrumentation);
379 visitor.WalkStack(true);
380 CHECK_EQ(visitor.frames_removed_, stack->size());
381 while (stack->size() > 0) {
382 stack->pop_front();
383 }
jeffhao725a9572012-11-13 18:20:12 -0800384 }
385}
386
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200387static bool HasEvent(Instrumentation::InstrumentationEvent expected, uint32_t events) {
388 return (events & expected) != 0;
389}
390
Ian Rogers62d6c772013-02-27 08:32:07 -0800391void Instrumentation::AddListener(InstrumentationListener* listener, uint32_t events) {
392 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200393 if (HasEvent(kMethodEntered, events)) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800394 method_entry_listeners_.push_back(listener);
Ian Rogers62d6c772013-02-27 08:32:07 -0800395 have_method_entry_listeners_ = true;
396 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200397 if (HasEvent(kMethodExited, events)) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800398 method_exit_listeners_.push_back(listener);
Ian Rogers62d6c772013-02-27 08:32:07 -0800399 have_method_exit_listeners_ = true;
400 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200401 if (HasEvent(kMethodUnwind, events)) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800402 method_unwind_listeners_.push_back(listener);
403 have_method_unwind_listeners_ = true;
404 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200405 if (HasEvent(kBackwardBranch, events)) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800406 backward_branch_listeners_.push_back(listener);
407 have_backward_branch_listeners_ = true;
408 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200409 if (HasEvent(kDexPcMoved, events)) {
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200410 std::list<InstrumentationListener*>* modified;
411 if (have_dex_pc_listeners_) {
412 modified = new std::list<InstrumentationListener*>(*dex_pc_listeners_.get());
413 } else {
414 modified = new std::list<InstrumentationListener*>();
415 }
416 modified->push_back(listener);
417 dex_pc_listeners_.reset(modified);
Ian Rogers62d6c772013-02-27 08:32:07 -0800418 have_dex_pc_listeners_ = true;
419 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200420 if (HasEvent(kFieldRead, events)) {
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200421 std::list<InstrumentationListener*>* modified;
422 if (have_field_read_listeners_) {
423 modified = new std::list<InstrumentationListener*>(*field_read_listeners_.get());
424 } else {
425 modified = new std::list<InstrumentationListener*>();
426 }
427 modified->push_back(listener);
428 field_read_listeners_.reset(modified);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200429 have_field_read_listeners_ = true;
430 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200431 if (HasEvent(kFieldWritten, events)) {
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200432 std::list<InstrumentationListener*>* modified;
433 if (have_field_write_listeners_) {
434 modified = new std::list<InstrumentationListener*>(*field_write_listeners_.get());
435 } else {
436 modified = new std::list<InstrumentationListener*>();
437 }
438 modified->push_back(listener);
439 field_write_listeners_.reset(modified);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200440 have_field_write_listeners_ = true;
441 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200442 if (HasEvent(kExceptionCaught, events)) {
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200443 std::list<InstrumentationListener*>* modified;
444 if (have_exception_caught_listeners_) {
445 modified = new std::list<InstrumentationListener*>(*exception_caught_listeners_.get());
446 } else {
447 modified = new std::list<InstrumentationListener*>();
448 }
449 modified->push_back(listener);
450 exception_caught_listeners_.reset(modified);
Jeff Hao14dd5a82013-04-11 10:23:36 -0700451 have_exception_caught_listeners_ = true;
452 }
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200453 UpdateInterpreterHandlerTable();
jeffhao725a9572012-11-13 18:20:12 -0800454}
455
Ian Rogers62d6c772013-02-27 08:32:07 -0800456void Instrumentation::RemoveListener(InstrumentationListener* listener, uint32_t events) {
457 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
Ian Rogers62d6c772013-02-27 08:32:07 -0800458
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200459 if (HasEvent(kMethodEntered, events) && have_method_entry_listeners_) {
460 method_entry_listeners_.remove(listener);
461 have_method_entry_listeners_ = !method_entry_listeners_.empty();
Ian Rogers62d6c772013-02-27 08:32:07 -0800462 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200463 if (HasEvent(kMethodExited, events) && have_method_exit_listeners_) {
464 method_exit_listeners_.remove(listener);
465 have_method_exit_listeners_ = !method_exit_listeners_.empty();
Ian Rogers62d6c772013-02-27 08:32:07 -0800466 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200467 if (HasEvent(kMethodUnwind, events) && have_method_unwind_listeners_) {
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200468 method_unwind_listeners_.remove(listener);
469 have_method_unwind_listeners_ = !method_unwind_listeners_.empty();
Ian Rogers62d6c772013-02-27 08:32:07 -0800470 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200471 if (HasEvent(kBackwardBranch, events) && have_backward_branch_listeners_) {
472 backward_branch_listeners_.remove(listener);
473 have_backward_branch_listeners_ = !backward_branch_listeners_.empty();
474 }
475 if (HasEvent(kDexPcMoved, events) && have_dex_pc_listeners_) {
476 std::list<InstrumentationListener*>* modified =
477 new std::list<InstrumentationListener*>(*dex_pc_listeners_.get());
478 modified->remove(listener);
479 have_dex_pc_listeners_ = !modified->empty();
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200480 if (have_dex_pc_listeners_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200481 dex_pc_listeners_.reset(modified);
482 } else {
483 dex_pc_listeners_.reset();
484 delete modified;
Ian Rogers62d6c772013-02-27 08:32:07 -0800485 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800486 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200487 if (HasEvent(kFieldRead, events) && have_field_read_listeners_) {
488 std::list<InstrumentationListener*>* modified =
489 new std::list<InstrumentationListener*>(*field_read_listeners_.get());
490 modified->remove(listener);
491 have_field_read_listeners_ = !modified->empty();
Daniel Mihalyi66445212014-08-21 15:57:25 +0200492 if (have_field_read_listeners_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200493 field_read_listeners_.reset(modified);
494 } else {
495 field_read_listeners_.reset();
496 delete modified;
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200497 }
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200498 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200499 if (HasEvent(kFieldWritten, events) && have_field_write_listeners_) {
500 std::list<InstrumentationListener*>* modified =
501 new std::list<InstrumentationListener*>(*field_write_listeners_.get());
502 modified->remove(listener);
503 have_field_write_listeners_ = !modified->empty();
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200504 if (have_field_write_listeners_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200505 field_write_listeners_.reset(modified);
506 } else {
507 field_write_listeners_.reset();
508 delete modified;
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200509 }
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200510 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200511 if (HasEvent(kExceptionCaught, events) && have_exception_caught_listeners_) {
512 std::list<InstrumentationListener*>* modified =
513 new std::list<InstrumentationListener*>(*exception_caught_listeners_.get());
514 modified->remove(listener);
515 have_exception_caught_listeners_ = !modified->empty();
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200516 if (have_exception_caught_listeners_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200517 exception_caught_listeners_.reset(modified);
518 } else {
519 exception_caught_listeners_.reset();
520 delete modified;
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200521 }
Jeff Hao14dd5a82013-04-11 10:23:36 -0700522 }
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200523 UpdateInterpreterHandlerTable();
jeffhao725a9572012-11-13 18:20:12 -0800524}
525
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200526Instrumentation::InstrumentationLevel Instrumentation::GetCurrentInstrumentationLevel() const {
Ian Rogers62d6c772013-02-27 08:32:07 -0800527 if (interpreter_stubs_installed_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200528 return InstrumentationLevel::kInstrumentWithInterpreter;
Ian Rogers62d6c772013-02-27 08:32:07 -0800529 } else if (entry_exit_stubs_installed_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200530 return InstrumentationLevel::kInstrumentWithInstrumentationStubs;
Ian Rogers62d6c772013-02-27 08:32:07 -0800531 } else {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200532 return InstrumentationLevel::kInstrumentNothing;
Ian Rogers62d6c772013-02-27 08:32:07 -0800533 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200534}
535
536void Instrumentation::ConfigureStubs(const char* key, InstrumentationLevel desired_level) {
537 // Store the instrumentation level for this key or remove it.
538 if (desired_level == InstrumentationLevel::kInstrumentNothing) {
539 // The client no longer needs instrumentation.
540 requested_instrumentation_levels_.erase(key);
541 } else {
542 // The client needs instrumentation.
543 requested_instrumentation_levels_.Overwrite(key, desired_level);
544 }
545
546 // Look for the highest required instrumentation level.
547 InstrumentationLevel requested_level = InstrumentationLevel::kInstrumentNothing;
548 for (const auto& v : requested_instrumentation_levels_) {
549 requested_level = std::max(requested_level, v.second);
550 }
551
552 interpret_only_ = (requested_level == InstrumentationLevel::kInstrumentWithInterpreter) ||
553 forced_interpret_only_;
554
555 InstrumentationLevel current_level = GetCurrentInstrumentationLevel();
556 if (requested_level == current_level) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800557 // We're already set.
558 return;
559 }
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100560 Thread* const self = Thread::Current();
Ian Rogers62d6c772013-02-27 08:32:07 -0800561 Runtime* runtime = Runtime::Current();
Sebastien Hertza8a697f2015-01-15 12:28:47 +0100562 Locks::mutator_lock_->AssertExclusiveHeld(self);
Ian Rogers62d6c772013-02-27 08:32:07 -0800563 Locks::thread_list_lock_->AssertNotHeld(self);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200564 if (requested_level > InstrumentationLevel::kInstrumentNothing) {
565 if (requested_level == InstrumentationLevel::kInstrumentWithInterpreter) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800566 interpreter_stubs_installed_ = true;
Ian Rogers62d6c772013-02-27 08:32:07 -0800567 entry_exit_stubs_installed_ = true;
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200568 } else {
569 CHECK_EQ(requested_level, InstrumentationLevel::kInstrumentWithInstrumentationStubs);
570 entry_exit_stubs_installed_ = true;
571 interpreter_stubs_installed_ = false;
Ian Rogers62d6c772013-02-27 08:32:07 -0800572 }
573 runtime->GetClassLinker()->VisitClasses(InstallStubsClassVisitor, this);
574 instrumentation_stubs_installed_ = true;
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100575 MutexLock mu(self, *Locks::thread_list_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800576 runtime->GetThreadList()->ForEach(InstrumentationInstallStack, this);
577 } else {
578 interpreter_stubs_installed_ = false;
579 entry_exit_stubs_installed_ = false;
580 runtime->GetClassLinker()->VisitClasses(InstallStubsClassVisitor, this);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100581 // Restore stack only if there is no method currently deoptimized.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700582 bool empty;
583 {
584 ReaderMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700585 empty = IsDeoptimizedMethodsEmpty(); // Avoid lock violation.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700586 }
587 if (empty) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100588 instrumentation_stubs_installed_ = false;
589 MutexLock mu(self, *Locks::thread_list_lock_);
590 Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this);
591 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800592 }
jeffhao725a9572012-11-13 18:20:12 -0800593}
594
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200595static void ResetQuickAllocEntryPointsForThread(Thread* thread, void* arg ATTRIBUTE_UNUSED) {
Ian Rogersfa824272013-11-05 16:12:57 -0800596 thread->ResetQuickAllocEntryPointsForThread();
597}
598
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700599void Instrumentation::SetEntrypointsInstrumented(bool instrumented) {
600 Thread* self = Thread::Current();
Mathieu Chartier661974a2014-01-09 11:23:53 -0800601 Runtime* runtime = Runtime::Current();
602 ThreadList* tl = runtime->GetThreadList();
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700603 Locks::mutator_lock_->AssertNotHeld(self);
604 Locks::instrument_entrypoints_lock_->AssertHeld(self);
605 if (runtime->IsStarted()) {
Mathieu Chartierbf9fc582015-03-13 17:21:25 -0700606 tl->SuspendAll(__FUNCTION__);
Mathieu Chartier661974a2014-01-09 11:23:53 -0800607 }
608 {
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700609 MutexLock mu(self, *Locks::runtime_shutdown_lock_);
Mathieu Chartier661974a2014-01-09 11:23:53 -0800610 SetQuickAllocEntryPointsInstrumented(instrumented);
611 ResetQuickAllocEntryPoints();
612 }
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700613 if (runtime->IsStarted()) {
Mathieu Chartier661974a2014-01-09 11:23:53 -0800614 tl->ResumeAll();
615 }
616}
617
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700618void Instrumentation::InstrumentQuickAllocEntryPoints() {
619 MutexLock mu(Thread::Current(), *Locks::instrument_entrypoints_lock_);
620 InstrumentQuickAllocEntryPointsLocked();
Ian Rogersfa824272013-11-05 16:12:57 -0800621}
622
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700623void Instrumentation::UninstrumentQuickAllocEntryPoints() {
624 MutexLock mu(Thread::Current(), *Locks::instrument_entrypoints_lock_);
625 UninstrumentQuickAllocEntryPointsLocked();
626}
627
628void Instrumentation::InstrumentQuickAllocEntryPointsLocked() {
629 Locks::instrument_entrypoints_lock_->AssertHeld(Thread::Current());
630 if (quick_alloc_entry_points_instrumentation_counter_ == 0) {
631 SetEntrypointsInstrumented(true);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800632 }
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700633 ++quick_alloc_entry_points_instrumentation_counter_;
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700634}
635
636void Instrumentation::UninstrumentQuickAllocEntryPointsLocked() {
637 Locks::instrument_entrypoints_lock_->AssertHeld(Thread::Current());
638 CHECK_GT(quick_alloc_entry_points_instrumentation_counter_, 0U);
639 --quick_alloc_entry_points_instrumentation_counter_;
640 if (quick_alloc_entry_points_instrumentation_counter_ == 0) {
641 SetEntrypointsInstrumented(false);
642 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800643}
644
645void Instrumentation::ResetQuickAllocEntryPoints() {
646 Runtime* runtime = Runtime::Current();
647 if (runtime->IsStarted()) {
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800648 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700649 runtime->GetThreadList()->ForEach(ResetQuickAllocEntryPointsForThread, nullptr);
Ian Rogersfa824272013-11-05 16:12:57 -0800650 }
651}
652
Elliott Hughes956af0f2014-12-11 14:34:28 -0800653void Instrumentation::UpdateMethodsCode(mirror::ArtMethod* method, const void* quick_code) {
Sebastien Hertza8a697f2015-01-15 12:28:47 +0100654 DCHECK(method->GetDeclaringClass()->IsResolved());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800655 const void* new_quick_code;
Ian Rogers62d6c772013-02-27 08:32:07 -0800656 if (LIKELY(!instrumentation_stubs_installed_)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800657 new_quick_code = quick_code;
Jeff Hao65d15d92013-07-16 16:39:33 -0700658 } else {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100659 if ((interpreter_stubs_installed_ || IsDeoptimized(method)) && !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800660 new_quick_code = GetQuickToInterpreterBridge();
Jeff Hao65d15d92013-07-16 16:39:33 -0700661 } else {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700662 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700663 if (class_linker->IsQuickResolutionStub(quick_code) ||
664 class_linker->IsQuickToInterpreterBridge(quick_code)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700665 new_quick_code = quick_code;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700666 } else if (entry_exit_stubs_installed_) {
667 new_quick_code = GetQuickInstrumentationEntryPoint();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700668 } else {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700669 new_quick_code = quick_code;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700670 }
Jeff Hao65d15d92013-07-16 16:39:33 -0700671 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800672 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800673 UpdateEntrypoints(method, new_quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100674}
675
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700676bool Instrumentation::AddDeoptimizedMethod(mirror::ArtMethod* method) {
677 // Note that the insert() below isn't read barrier-aware. So, this
678 // FindDeoptimizedMethod() call is necessary or else we would end up
679 // storing the same method twice in the map (the from-space and the
680 // to-space ones).
681 if (FindDeoptimizedMethod(method)) {
682 // Already in the map. Return.
683 return false;
684 }
685 // Not found. Add it.
Mathieu Chartier4c4d6092015-01-22 17:02:27 -0800686 static_assert(!kMovingMethods, "Not safe if methods can move");
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700687 int32_t hash_code = method->IdentityHashCode();
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700688 deoptimized_methods_.insert(std::make_pair(hash_code, GcRoot<mirror::ArtMethod>(method)));
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700689 return true;
690}
691
692bool Instrumentation::FindDeoptimizedMethod(mirror::ArtMethod* method) {
Mathieu Chartier4c4d6092015-01-22 17:02:27 -0800693 static_assert(!kMovingMethods, "Not safe if methods can move");
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700694 int32_t hash_code = method->IdentityHashCode();
695 auto range = deoptimized_methods_.equal_range(hash_code);
696 for (auto it = range.first; it != range.second; ++it) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700697 mirror::ArtMethod* m = it->second.Read();
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700698 if (m == method) {
699 // Found.
700 return true;
701 }
702 }
703 // Not found.
704 return false;
705}
706
707mirror::ArtMethod* Instrumentation::BeginDeoptimizedMethod() {
708 auto it = deoptimized_methods_.begin();
709 if (it == deoptimized_methods_.end()) {
710 // Empty.
711 return nullptr;
712 }
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700713 return it->second.Read();
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700714}
715
716bool Instrumentation::RemoveDeoptimizedMethod(mirror::ArtMethod* method) {
Mathieu Chartier4c4d6092015-01-22 17:02:27 -0800717 static_assert(!kMovingMethods, "Not safe if methods can move");
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700718 int32_t hash_code = method->IdentityHashCode();
719 auto range = deoptimized_methods_.equal_range(hash_code);
720 for (auto it = range.first; it != range.second; ++it) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700721 mirror::ArtMethod* m = it->second.Read();
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700722 if (m == method) {
723 // Found. Erase and return.
724 deoptimized_methods_.erase(it);
725 return true;
726 }
727 }
728 // Not found.
729 return false;
730}
731
732bool Instrumentation::IsDeoptimizedMethodsEmpty() const {
733 return deoptimized_methods_.empty();
734}
735
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100736void Instrumentation::Deoptimize(mirror::ArtMethod* method) {
737 CHECK(!method->IsNative());
738 CHECK(!method->IsProxyMethod());
739 CHECK(!method->IsAbstract());
740
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700741 Thread* self = Thread::Current();
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700742 {
743 WriterMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700744 bool has_not_been_deoptimized = AddDeoptimizedMethod(method);
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200745 CHECK(has_not_been_deoptimized) << "Method " << PrettyMethod(method)
746 << " is already deoptimized";
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700747 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100748 if (!interpreter_stubs_installed_) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800749 UpdateEntrypoints(method, GetQuickInstrumentationEntryPoint());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100750
751 // Install instrumentation exit stub and instrumentation frames. We may already have installed
752 // these previously so it will only cover the newly created frames.
753 instrumentation_stubs_installed_ = true;
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700754 MutexLock mu(self, *Locks::thread_list_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100755 Runtime::Current()->GetThreadList()->ForEach(InstrumentationInstallStack, this);
756 }
757}
758
759void Instrumentation::Undeoptimize(mirror::ArtMethod* method) {
760 CHECK(!method->IsNative());
761 CHECK(!method->IsProxyMethod());
762 CHECK(!method->IsAbstract());
763
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700764 Thread* self = Thread::Current();
765 bool empty;
766 {
767 WriterMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700768 bool found_and_erased = RemoveDeoptimizedMethod(method);
769 CHECK(found_and_erased) << "Method " << PrettyMethod(method)
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700770 << " is not deoptimized";
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700771 empty = IsDeoptimizedMethodsEmpty();
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700772 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100773
774 // Restore code and possibly stack only if we did not deoptimize everything.
775 if (!interpreter_stubs_installed_) {
776 // Restore its code or resolution trampoline.
777 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800778 if (method->IsStatic() && !method->IsConstructor() &&
779 !method->GetDeclaringClass()->IsInitialized()) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800780 UpdateEntrypoints(method, GetQuickResolutionStub());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100781 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800782 const void* quick_code = class_linker->GetQuickOatCodeFor(method);
Elliott Hughes956af0f2014-12-11 14:34:28 -0800783 UpdateEntrypoints(method, quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100784 }
785
786 // If there is no deoptimized method left, we can restore the stack of each thread.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700787 if (empty) {
788 MutexLock mu(self, *Locks::thread_list_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100789 Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this);
790 instrumentation_stubs_installed_ = false;
791 }
792 }
793}
794
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700795bool Instrumentation::IsDeoptimized(mirror::ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100796 DCHECK(method != nullptr);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700797 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
798 return FindDeoptimizedMethod(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100799}
800
801void Instrumentation::EnableDeoptimization() {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700802 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700803 CHECK(IsDeoptimizedMethodsEmpty());
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100804 CHECK_EQ(deoptimization_enabled_, false);
805 deoptimization_enabled_ = true;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100806}
807
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200808void Instrumentation::DisableDeoptimization(const char* key) {
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100809 CHECK_EQ(deoptimization_enabled_, true);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100810 // If we deoptimized everything, undo it.
811 if (interpreter_stubs_installed_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200812 UndeoptimizeEverything(key);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100813 }
814 // Undeoptimized selected methods.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700815 while (true) {
816 mirror::ArtMethod* method;
817 {
818 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700819 if (IsDeoptimizedMethodsEmpty()) {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700820 break;
821 }
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700822 method = BeginDeoptimizedMethod();
823 CHECK(method != nullptr);
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700824 }
825 Undeoptimize(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100826 }
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100827 deoptimization_enabled_ = false;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100828}
829
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100830// Indicates if instrumentation should notify method enter/exit events to the listeners.
831bool Instrumentation::ShouldNotifyMethodEnterExitEvents() const {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200832 if (!HasMethodEntryListeners() && !HasMethodExitListeners()) {
833 return false;
834 }
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100835 return !deoptimization_enabled_ && !interpreter_stubs_installed_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100836}
837
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200838void Instrumentation::DeoptimizeEverything(const char* key) {
839 CHECK(deoptimization_enabled_);
840 ConfigureStubs(key, InstrumentationLevel::kInstrumentWithInterpreter);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100841}
842
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200843void Instrumentation::UndeoptimizeEverything(const char* key) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100844 CHECK(interpreter_stubs_installed_);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200845 CHECK(deoptimization_enabled_);
846 ConfigureStubs(key, InstrumentationLevel::kInstrumentNothing);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100847}
848
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200849void Instrumentation::EnableMethodTracing(const char* key, bool needs_interpreter) {
850 InstrumentationLevel level;
851 if (needs_interpreter) {
852 level = InstrumentationLevel::kInstrumentWithInterpreter;
853 } else {
854 level = InstrumentationLevel::kInstrumentWithInstrumentationStubs;
855 }
856 ConfigureStubs(key, level);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100857}
858
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200859void Instrumentation::DisableMethodTracing(const char* key) {
860 ConfigureStubs(key, InstrumentationLevel::kInstrumentNothing);
jeffhao725a9572012-11-13 18:20:12 -0800861}
862
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800863const void* Instrumentation::GetQuickCodeFor(mirror::ArtMethod* method, size_t pointer_size) const {
Ian Rogers62d6c772013-02-27 08:32:07 -0800864 Runtime* runtime = Runtime::Current();
865 if (LIKELY(!instrumentation_stubs_installed_)) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800866 const void* code = method->GetEntryPointFromQuickCompiledCodePtrSize(pointer_size);
Vladimir Marko8a630572014-04-09 18:45:35 +0100867 DCHECK(code != nullptr);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700868 ClassLinker* class_linker = runtime->GetClassLinker();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700869 if (LIKELY(!class_linker->IsQuickResolutionStub(code) &&
870 !class_linker->IsQuickToInterpreterBridge(code)) &&
871 !class_linker->IsQuickResolutionStub(code) &&
872 !class_linker->IsQuickToInterpreterBridge(code)) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800873 return code;
874 }
875 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800876 return runtime->GetClassLinker()->GetQuickOatCodeFor(method);
jeffhao725a9572012-11-13 18:20:12 -0800877}
878
Ian Rogers62d6c772013-02-27 08:32:07 -0800879void Instrumentation::MethodEnterEventImpl(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800880 mirror::ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800881 uint32_t dex_pc) const {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700882 auto it = method_entry_listeners_.begin();
Jeff Hao65d15d92013-07-16 16:39:33 -0700883 bool is_end = (it == method_entry_listeners_.end());
884 // Implemented this way to prevent problems caused by modification of the list while iterating.
885 while (!is_end) {
886 InstrumentationListener* cur = *it;
887 ++it;
888 is_end = (it == method_entry_listeners_.end());
889 cur->MethodEntered(thread, this_object, method, dex_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800890 }
891}
892
893void Instrumentation::MethodExitEventImpl(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800894 mirror::ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800895 uint32_t dex_pc, const JValue& return_value) const {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700896 auto it = method_exit_listeners_.begin();
Jeff Hao65d15d92013-07-16 16:39:33 -0700897 bool is_end = (it == method_exit_listeners_.end());
898 // Implemented this way to prevent problems caused by modification of the list while iterating.
899 while (!is_end) {
900 InstrumentationListener* cur = *it;
901 ++it;
902 is_end = (it == method_exit_listeners_.end());
903 cur->MethodExited(thread, this_object, method, dex_pc, return_value);
Ian Rogers62d6c772013-02-27 08:32:07 -0800904 }
905}
906
907void Instrumentation::MethodUnwindEvent(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800908 mirror::ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800909 uint32_t dex_pc) const {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200910 if (HasMethodUnwindListeners()) {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700911 for (InstrumentationListener* listener : method_unwind_listeners_) {
Sebastien Hertz51db44a2013-11-19 10:00:29 +0100912 listener->MethodUnwind(thread, this_object, method, dex_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800913 }
914 }
915}
916
917void Instrumentation::DexPcMovedEventImpl(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800918 mirror::ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800919 uint32_t dex_pc) const {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200920 std::shared_ptr<std::list<InstrumentationListener*>> original(dex_pc_listeners_);
921 for (InstrumentationListener* listener : *original.get()) {
922 listener->DexPcMoved(thread, this_object, method, dex_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800923 }
924}
925
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800926void Instrumentation::BackwardBranchImpl(Thread* thread, mirror::ArtMethod* method,
927 int32_t offset) const {
928 for (InstrumentationListener* listener : backward_branch_listeners_) {
929 listener->BackwardBranch(thread, method, offset);
930 }
931}
932
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200933void Instrumentation::FieldReadEventImpl(Thread* thread, mirror::Object* this_object,
934 mirror::ArtMethod* method, uint32_t dex_pc,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700935 ArtField* field) const {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200936 std::shared_ptr<std::list<InstrumentationListener*>> original(field_read_listeners_);
937 for (InstrumentationListener* listener : *original.get()) {
938 listener->FieldRead(thread, this_object, method, dex_pc, field);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200939 }
940}
941
942void Instrumentation::FieldWriteEventImpl(Thread* thread, mirror::Object* this_object,
943 mirror::ArtMethod* method, uint32_t dex_pc,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700944 ArtField* field, const JValue& field_value) const {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200945 std::shared_ptr<std::list<InstrumentationListener*>> original(field_write_listeners_);
946 for (InstrumentationListener* listener : *original.get()) {
947 listener->FieldWritten(thread, this_object, method, dex_pc, field, field_value);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200948 }
949}
950
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000951void Instrumentation::ExceptionCaughtEvent(Thread* thread,
Sebastien Hertz947ff082013-09-17 14:10:13 +0200952 mirror::Throwable* exception_object) const {
Sebastien Hertz9f102032014-05-23 08:59:42 +0200953 if (HasExceptionCaughtListeners()) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000954 DCHECK_EQ(thread->GetException(), exception_object);
Jeff Haoc0bd4da2013-04-11 15:52:28 -0700955 thread->ClearException();
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200956 std::shared_ptr<std::list<InstrumentationListener*>> original(exception_caught_listeners_);
957 for (InstrumentationListener* listener : *original.get()) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000958 listener->ExceptionCaught(thread, exception_object);
Ian Rogers62d6c772013-02-27 08:32:07 -0800959 }
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000960 thread->SetException(exception_object);
Ian Rogers62d6c772013-02-27 08:32:07 -0800961 }
962}
963
964static void CheckStackDepth(Thread* self, const InstrumentationStackFrame& instrumentation_frame,
965 int delta)
966 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
967 size_t frame_id = StackVisitor::ComputeNumFrames(self) + delta;
968 if (frame_id != instrumentation_frame.frame_id_) {
969 LOG(ERROR) << "Expected frame_id=" << frame_id << " but found "
970 << instrumentation_frame.frame_id_;
971 StackVisitor::DescribeStack(self);
972 CHECK_EQ(frame_id, instrumentation_frame.frame_id_);
973 }
974}
975
976void Instrumentation::PushInstrumentationStackFrame(Thread* self, mirror::Object* this_object,
Brian Carlstromea46f952013-07-30 01:26:50 -0700977 mirror::ArtMethod* method,
Jeff Hao9a916d32013-06-27 18:45:37 -0700978 uintptr_t lr, bool interpreter_entry) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800979 // We have a callee-save frame meaning this value is guaranteed to never be 0.
980 size_t frame_id = StackVisitor::ComputeNumFrames(self);
981 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
982 if (kVerboseInstrumentation) {
Brian Carlstrom2d888622013-07-18 17:02:00 -0700983 LOG(INFO) << "Entering " << PrettyMethod(method) << " from PC " << reinterpret_cast<void*>(lr);
Ian Rogers62d6c772013-02-27 08:32:07 -0800984 }
985 instrumentation::InstrumentationStackFrame instrumentation_frame(this_object, method, lr,
Jeff Hao9a916d32013-06-27 18:45:37 -0700986 frame_id, interpreter_entry);
Ian Rogers62d6c772013-02-27 08:32:07 -0800987 stack->push_front(instrumentation_frame);
988
Sebastien Hertz320deb22014-06-11 19:45:05 +0200989 if (!interpreter_entry) {
990 MethodEnterEvent(self, this_object, method, 0);
991 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800992}
993
Andreas Gamped58342c2014-06-05 14:18:08 -0700994TwoWordReturn Instrumentation::PopInstrumentationStackFrame(Thread* self, uintptr_t* return_pc,
995 uint64_t gpr_result,
996 uint64_t fpr_result) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800997 // Do the pop.
998 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
999 CHECK_GT(stack->size(), 0U);
1000 InstrumentationStackFrame instrumentation_frame = stack->front();
1001 stack->pop_front();
1002
1003 // Set return PC and check the sanity of the stack.
1004 *return_pc = instrumentation_frame.return_pc_;
1005 CheckStackDepth(self, instrumentation_frame, 0);
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001006 self->VerifyStack();
Ian Rogers62d6c772013-02-27 08:32:07 -08001007
Brian Carlstromea46f952013-07-30 01:26:50 -07001008 mirror::ArtMethod* method = instrumentation_frame.method_;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001009 uint32_t length;
1010 char return_shorty = method->GetShorty(&length)[0];
Ian Rogers62d6c772013-02-27 08:32:07 -08001011 JValue return_value;
1012 if (return_shorty == 'V') {
1013 return_value.SetJ(0);
1014 } else if (return_shorty == 'F' || return_shorty == 'D') {
1015 return_value.SetJ(fpr_result);
1016 } else {
1017 return_value.SetJ(gpr_result);
1018 }
1019 // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to
1020 // return_pc.
1021 uint32_t dex_pc = DexFile::kDexNoIndex;
1022 mirror::Object* this_object = instrumentation_frame.this_object_;
Sebastien Hertz320deb22014-06-11 19:45:05 +02001023 if (!instrumentation_frame.interpreter_entry_) {
1024 MethodExitEvent(self, this_object, instrumentation_frame.method_, dex_pc, return_value);
1025 }
jeffhao725a9572012-11-13 18:20:12 -08001026
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01001027 // Deoptimize if the caller needs to continue execution in the interpreter. Do nothing if we get
1028 // back to an upcall.
1029 NthCallerVisitor visitor(self, 1, true);
1030 visitor.WalkStack(true);
Sebastien Hertz270a0e12015-01-16 19:49:09 +01001031 bool deoptimize = (visitor.caller != nullptr) &&
Daniel Mihalyieb076692014-08-22 17:33:31 +02001032 (interpreter_stubs_installed_ || IsDeoptimized(visitor.caller) ||
1033 Dbg::IsForcedInterpreterNeededForUpcall(self, visitor.caller));
Ian Rogers62d6c772013-02-27 08:32:07 -08001034 if (deoptimize) {
1035 if (kVerboseInstrumentation) {
Sebastien Hertz270a0e12015-01-16 19:49:09 +01001036 LOG(INFO) << StringPrintf("Deoptimizing %s by returning from %s with result %#" PRIx64 " in ",
1037 PrettyMethod(visitor.caller).c_str(),
1038 PrettyMethod(method).c_str(),
1039 return_value.GetJ()) << *self;
Ian Rogers62d6c772013-02-27 08:32:07 -08001040 }
1041 self->SetDeoptimizationReturnValue(return_value);
Andreas Gamped58342c2014-06-05 14:18:08 -07001042 return GetTwoWordSuccessValue(*return_pc,
1043 reinterpret_cast<uintptr_t>(GetQuickDeoptimizationEntryPoint()));
Ian Rogers62d6c772013-02-27 08:32:07 -08001044 } else {
1045 if (kVerboseInstrumentation) {
Brian Carlstrom2d888622013-07-18 17:02:00 -07001046 LOG(INFO) << "Returning from " << PrettyMethod(method)
1047 << " to PC " << reinterpret_cast<void*>(*return_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -08001048 }
Andreas Gamped58342c2014-06-05 14:18:08 -07001049 return GetTwoWordSuccessValue(0, *return_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -08001050 }
jeffhao725a9572012-11-13 18:20:12 -08001051}
1052
Ian Rogers62d6c772013-02-27 08:32:07 -08001053void Instrumentation::PopMethodForUnwind(Thread* self, bool is_deoptimization) const {
1054 // Do the pop.
1055 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
1056 CHECK_GT(stack->size(), 0U);
1057 InstrumentationStackFrame instrumentation_frame = stack->front();
1058 // TODO: bring back CheckStackDepth(self, instrumentation_frame, 2);
1059 stack->pop_front();
1060
Brian Carlstromea46f952013-07-30 01:26:50 -07001061 mirror::ArtMethod* method = instrumentation_frame.method_;
Ian Rogers62d6c772013-02-27 08:32:07 -08001062 if (is_deoptimization) {
1063 if (kVerboseInstrumentation) {
1064 LOG(INFO) << "Popping for deoptimization " << PrettyMethod(method);
1065 }
1066 } else {
1067 if (kVerboseInstrumentation) {
1068 LOG(INFO) << "Popping for unwind " << PrettyMethod(method);
1069 }
1070
1071 // Notify listeners of method unwind.
1072 // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to
1073 // return_pc.
1074 uint32_t dex_pc = DexFile::kDexNoIndex;
1075 MethodUnwindEvent(self, instrumentation_frame.this_object_, method, dex_pc);
1076 }
1077}
1078
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07001079void Instrumentation::VisitRoots(RootVisitor* visitor) {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -07001080 WriterMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -07001081 if (IsDeoptimizedMethodsEmpty()) {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -07001082 return;
1083 }
Mathieu Chartier4809d0a2015-04-07 10:39:04 -07001084 BufferedRootVisitor<kDefaultBufferedRootCount> roots(visitor, RootInfo(kRootVMInternal));
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -07001085 for (auto pair : deoptimized_methods_) {
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07001086 roots.VisitRoot(pair.second);
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -07001087 }
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -07001088}
1089
Ian Rogers62d6c772013-02-27 08:32:07 -08001090std::string InstrumentationStackFrame::Dump() const {
1091 std::ostringstream os;
1092 os << "Frame " << frame_id_ << " " << PrettyMethod(method_) << ":"
1093 << reinterpret_cast<void*>(return_pc_) << " this=" << reinterpret_cast<void*>(this_object_);
1094 return os.str();
1095}
1096
1097} // namespace instrumentation
jeffhao725a9572012-11-13 18:20:12 -08001098} // namespace art