blob: 9711cf238e23df7b834e0ab88a6feb153955a0d6 [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"
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
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +010047// Instrumentation works on non-inlined frames by updating returned PCs
48// of compiled frames.
49static constexpr StackVisitor::StackWalkKind kInstrumentationStackWalk =
50 StackVisitor::StackWalkKind::kSkipInlinedFrames;
51
Ian Rogers62d6c772013-02-27 08:32:07 -080052static bool InstallStubsClassVisitor(mirror::Class* klass, void* arg)
Mathieu Chartier90443472015-07-16 20:32:27 -070053 REQUIRES(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -080054 Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg);
Sebastien Hertza10aa372015-01-21 17:30:58 +010055 instrumentation->InstallStubsForClass(klass);
56 return true; // we visit all classes.
Ian Rogers62d6c772013-02-27 08:32:07 -080057}
58
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -070059Instrumentation::Instrumentation()
60 : instrumentation_stubs_installed_(false), entry_exit_stubs_installed_(false),
61 interpreter_stubs_installed_(false),
62 interpret_only_(false), forced_interpret_only_(false),
63 have_method_entry_listeners_(false), have_method_exit_listeners_(false),
64 have_method_unwind_listeners_(false), have_dex_pc_listeners_(false),
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +020065 have_field_read_listeners_(false), have_field_write_listeners_(false),
Sebastien Hertz0462c4c2015-04-01 16:34:17 +020066 have_exception_caught_listeners_(false), have_backward_branch_listeners_(false),
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -070067 deoptimized_methods_lock_("deoptimized methods lock"),
68 deoptimization_enabled_(false),
69 interpreter_handler_table_(kMainHandlerTable),
70 quick_alloc_entry_points_instrumentation_counter_(0) {
71}
72
Sebastien Hertza10aa372015-01-21 17:30:58 +010073void Instrumentation::InstallStubsForClass(mirror::Class* klass) {
Sebastien Hertza8a697f2015-01-15 12:28:47 +010074 if (klass->IsErroneous()) {
75 // We can't execute code in a erroneous class: do nothing.
76 } else if (!klass->IsResolved()) {
77 // We need the class to be resolved to install/uninstall stubs. Otherwise its methods
78 // could not be initialized or linked with regards to class inheritance.
79 } else {
80 for (size_t i = 0, e = klass->NumDirectMethods(); i < e; i++) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070081 InstallStubsForMethod(klass->GetDirectMethod(i, sizeof(void*)));
Sebastien Hertza8a697f2015-01-15 12:28:47 +010082 }
83 for (size_t i = 0, e = klass->NumVirtualMethods(); i < e; i++) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070084 InstallStubsForMethod(klass->GetVirtualMethod(i, sizeof(void*)));
Sebastien Hertza8a697f2015-01-15 12:28:47 +010085 }
jeffhao725a9572012-11-13 18:20:12 -080086 }
jeffhao725a9572012-11-13 18:20:12 -080087}
88
Mathieu Chartiere401d142015-04-22 13:56:20 -070089static void UpdateEntrypoints(ArtMethod* method, const void* quick_code)
Mathieu Chartier90443472015-07-16 20:32:27 -070090 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080091 Runtime* const runtime = Runtime::Current();
92 jit::Jit* jit = runtime->GetJit();
93 if (jit != nullptr) {
94 const void* old_code_ptr = method->GetEntryPointFromQuickCompiledCode();
95 jit::JitCodeCache* code_cache = jit->GetCodeCache();
96 if (code_cache->ContainsCodePtr(old_code_ptr)) {
97 // Save the old compiled code since we need it to implement ClassLinker::GetQuickOatCodeFor.
98 code_cache->SaveCompiledCode(method, old_code_ptr);
99 }
100 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800101 method->SetEntryPointFromQuickCompiledCode(quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100102}
103
Mathieu Chartiere401d142015-04-22 13:56:20 -0700104void Instrumentation::InstallStubsForMethod(ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100105 if (method->IsAbstract() || method->IsProxyMethod()) {
106 // Do not change stubs for these methods.
107 return;
108 }
Jeff Hao56802772014-08-19 10:17:36 -0700109 // Don't stub Proxy.<init>. Note that the Proxy class itself is not a proxy class.
110 if (method->IsConstructor() &&
111 method->GetDeclaringClass()->DescriptorEquals("Ljava/lang/reflect/Proxy;")) {
Jeff Haodb8a6642014-08-14 17:18:52 -0700112 return;
113 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800114 const void* new_quick_code;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100115 bool uninstall = !entry_exit_stubs_installed_ && !interpreter_stubs_installed_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800116 Runtime* const runtime = Runtime::Current();
117 ClassLinker* const class_linker = runtime->GetClassLinker();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100118 bool is_class_initialized = method->GetDeclaringClass()->IsInitialized();
119 if (uninstall) {
120 if ((forced_interpret_only_ || IsDeoptimized(method)) && !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800121 new_quick_code = GetQuickToInterpreterBridge();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100122 } else if (is_class_initialized || !method->IsStatic() || method->IsConstructor()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800123 new_quick_code = class_linker->GetQuickOatCodeFor(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100124 } else {
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700125 new_quick_code = GetQuickResolutionStub();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100126 }
127 } else { // !uninstall
Sebastien Hertzbae182c2013-12-17 10:42:03 +0100128 if ((interpreter_stubs_installed_ || forced_interpret_only_ || IsDeoptimized(method)) &&
129 !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800130 new_quick_code = GetQuickToInterpreterBridge();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100131 } else {
132 // Do not overwrite resolution trampoline. When the trampoline initializes the method's
133 // class, all its static methods code will be set to the instrumentation entry point.
134 // For more details, see ClassLinker::FixupStaticTrampolines.
135 if (is_class_initialized || !method->IsStatic() || method->IsConstructor()) {
Sebastien Hertz320deb22014-06-11 19:45:05 +0200136 if (entry_exit_stubs_installed_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800137 new_quick_code = GetQuickInstrumentationEntryPoint();
Sebastien Hertz320deb22014-06-11 19:45:05 +0200138 } else {
Sebastien Hertz320deb22014-06-11 19:45:05 +0200139 new_quick_code = class_linker->GetQuickOatCodeFor(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100140 }
141 } else {
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700142 new_quick_code = GetQuickResolutionStub();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100143 }
144 }
145 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800146 UpdateEntrypoints(method, new_quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100147}
148
Ian Rogers62d6c772013-02-27 08:32:07 -0800149// Places the instrumentation exit pc as the return PC for every quick frame. This also allows
150// deoptimization of quick frames to interpreter frames.
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100151// Since we may already have done this previously, we need to push new instrumentation frame before
152// existing instrumentation frames.
Ian Rogers62d6c772013-02-27 08:32:07 -0800153static void InstrumentationInstallStack(Thread* thread, void* arg)
Mathieu Chartier90443472015-07-16 20:32:27 -0700154 SHARED_REQUIRES(Locks::mutator_lock_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200155 struct InstallStackVisitor FINAL : public StackVisitor {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800156 InstallStackVisitor(Thread* thread_in, Context* context, uintptr_t instrumentation_exit_pc)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100157 : StackVisitor(thread_in, context, kInstrumentationStackWalk),
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800158 instrumentation_stack_(thread_in->GetInstrumentationStack()),
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100159 instrumentation_exit_pc_(instrumentation_exit_pc),
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100160 reached_existing_instrumentation_frames_(false), instrumentation_stack_depth_(0),
161 last_return_pc_(0) {
162 }
jeffhao725a9572012-11-13 18:20:12 -0800163
Mathieu Chartier90443472015-07-16 20:32:27 -0700164 bool VisitFrame() OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700165 ArtMethod* m = GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700166 if (m == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800167 if (kVerboseInstrumentation) {
168 LOG(INFO) << " Skipping upcall. Frame " << GetFrameId();
169 }
170 last_return_pc_ = 0;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700171 return true; // Ignore upcalls.
Ian Rogers306057f2012-11-26 12:45:53 -0800172 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700173 if (GetCurrentQuickFrame() == nullptr) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800174 bool interpreter_frame = true;
Sebastien Hertz320deb22014-06-11 19:45:05 +0200175 InstrumentationStackFrame instrumentation_frame(GetThisObject(), m, 0, GetFrameId(),
176 interpreter_frame);
Jeff Haoa15a81b2014-05-27 18:25:47 -0700177 if (kVerboseInstrumentation) {
178 LOG(INFO) << "Pushing shadow frame " << instrumentation_frame.Dump();
179 }
180 shadow_stack_.push_back(instrumentation_frame);
181 return true; // Continue.
182 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800183 uintptr_t return_pc = GetReturnPc();
Sebastien Hertz320deb22014-06-11 19:45:05 +0200184 if (m->IsRuntimeMethod()) {
185 if (return_pc == instrumentation_exit_pc_) {
186 if (kVerboseInstrumentation) {
187 LOG(INFO) << " Handling quick to interpreter transition. Frame " << GetFrameId();
188 }
189 CHECK_LT(instrumentation_stack_depth_, instrumentation_stack_->size());
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200190 const InstrumentationStackFrame& frame =
191 instrumentation_stack_->at(instrumentation_stack_depth_);
Sebastien Hertz320deb22014-06-11 19:45:05 +0200192 CHECK(frame.interpreter_entry_);
193 // This is an interpreter frame so method enter event must have been reported. However we
194 // need to push a DEX pc into the dex_pcs_ list to match size of instrumentation stack.
195 // Since we won't report method entry here, we can safely push any DEX pc.
196 dex_pcs_.push_back(0);
197 last_return_pc_ = frame.return_pc_;
198 ++instrumentation_stack_depth_;
199 return true;
200 } else {
201 if (kVerboseInstrumentation) {
202 LOG(INFO) << " Skipping runtime method. Frame " << GetFrameId();
203 }
204 last_return_pc_ = GetReturnPc();
205 return true; // Ignore unresolved methods since they will be instrumented after resolution.
206 }
207 }
208 if (kVerboseInstrumentation) {
209 LOG(INFO) << " Installing exit stub in " << DescribeLocation();
210 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100211 if (return_pc == instrumentation_exit_pc_) {
212 // We've reached a frame which has already been installed with instrumentation exit stub.
213 // We should have already installed instrumentation on previous frames.
214 reached_existing_instrumentation_frames_ = true;
215
216 CHECK_LT(instrumentation_stack_depth_, instrumentation_stack_->size());
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200217 const InstrumentationStackFrame& frame =
218 instrumentation_stack_->at(instrumentation_stack_depth_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100219 CHECK_EQ(m, frame.method_) << "Expected " << PrettyMethod(m)
220 << ", Found " << PrettyMethod(frame.method_);
221 return_pc = frame.return_pc_;
222 if (kVerboseInstrumentation) {
223 LOG(INFO) << "Ignoring already instrumented " << frame.Dump();
224 }
225 } else {
226 CHECK_NE(return_pc, 0U);
227 CHECK(!reached_existing_instrumentation_frames_);
228 InstrumentationStackFrame instrumentation_frame(GetThisObject(), m, return_pc, GetFrameId(),
229 false);
230 if (kVerboseInstrumentation) {
231 LOG(INFO) << "Pushing frame " << instrumentation_frame.Dump();
232 }
233
Sebastien Hertz320deb22014-06-11 19:45:05 +0200234 // Insert frame at the right position so we do not corrupt the instrumentation stack.
235 // Instrumentation stack frames are in descending frame id order.
236 auto it = instrumentation_stack_->begin();
237 for (auto end = instrumentation_stack_->end(); it != end; ++it) {
238 const InstrumentationStackFrame& current = *it;
239 if (instrumentation_frame.frame_id_ >= current.frame_id_) {
240 break;
241 }
242 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100243 instrumentation_stack_->insert(it, instrumentation_frame);
244 SetReturnPc(instrumentation_exit_pc_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800245 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800246 dex_pcs_.push_back(m->ToDexPc(last_return_pc_));
Ian Rogers62d6c772013-02-27 08:32:07 -0800247 last_return_pc_ = return_pc;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100248 ++instrumentation_stack_depth_;
Ian Rogers306057f2012-11-26 12:45:53 -0800249 return true; // Continue.
250 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800251 std::deque<InstrumentationStackFrame>* const instrumentation_stack_;
Jeff Haoa15a81b2014-05-27 18:25:47 -0700252 std::vector<InstrumentationStackFrame> shadow_stack_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800253 std::vector<uint32_t> dex_pcs_;
Ian Rogers306057f2012-11-26 12:45:53 -0800254 const uintptr_t instrumentation_exit_pc_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100255 bool reached_existing_instrumentation_frames_;
256 size_t instrumentation_stack_depth_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800257 uintptr_t last_return_pc_;
Ian Rogers306057f2012-11-26 12:45:53 -0800258 };
Ian Rogers62d6c772013-02-27 08:32:07 -0800259 if (kVerboseInstrumentation) {
260 std::string thread_name;
261 thread->GetThreadName(thread_name);
262 LOG(INFO) << "Installing exit stubs in " << thread_name;
Ian Rogers306057f2012-11-26 12:45:53 -0800263 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100264
265 Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg);
Ian Rogers700a4022014-05-19 16:49:03 -0700266 std::unique_ptr<Context> context(Context::Create());
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700267 uintptr_t instrumentation_exit_pc = reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc());
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100268 InstallStackVisitor visitor(thread, context.get(), instrumentation_exit_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800269 visitor.WalkStack(true);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100270 CHECK_EQ(visitor.dex_pcs_.size(), thread->GetInstrumentationStack()->size());
Ian Rogers62d6c772013-02-27 08:32:07 -0800271
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100272 if (instrumentation->ShouldNotifyMethodEnterExitEvents()) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100273 // Create method enter events for all methods currently on the thread's stack. We only do this
274 // if no debugger is attached to prevent from posting events twice.
Jeff Haoa15a81b2014-05-27 18:25:47 -0700275 auto ssi = visitor.shadow_stack_.rbegin();
276 for (auto isi = thread->GetInstrumentationStack()->rbegin(),
277 end = thread->GetInstrumentationStack()->rend(); isi != end; ++isi) {
278 while (ssi != visitor.shadow_stack_.rend() && (*ssi).frame_id_ < (*isi).frame_id_) {
279 instrumentation->MethodEnterEvent(thread, (*ssi).this_object_, (*ssi).method_, 0);
280 ++ssi;
281 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100282 uint32_t dex_pc = visitor.dex_pcs_.back();
283 visitor.dex_pcs_.pop_back();
Sebastien Hertz320deb22014-06-11 19:45:05 +0200284 if (!isi->interpreter_entry_) {
285 instrumentation->MethodEnterEvent(thread, (*isi).this_object_, (*isi).method_, dex_pc);
286 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100287 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800288 }
289 thread->VerifyStack();
Ian Rogers306057f2012-11-26 12:45:53 -0800290}
291
Ian Rogers62d6c772013-02-27 08:32:07 -0800292// Removes the instrumentation exit pc as the return PC for every quick frame.
293static void InstrumentationRestoreStack(Thread* thread, void* arg)
Mathieu Chartier90443472015-07-16 20:32:27 -0700294 SHARED_REQUIRES(Locks::mutator_lock_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200295 struct RestoreStackVisitor FINAL : public StackVisitor {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800296 RestoreStackVisitor(Thread* thread_in, uintptr_t instrumentation_exit_pc,
Ian Rogers62d6c772013-02-27 08:32:07 -0800297 Instrumentation* instrumentation)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100298 : StackVisitor(thread_in, nullptr, kInstrumentationStackWalk),
299 thread_(thread_in),
Ian Rogers62d6c772013-02-27 08:32:07 -0800300 instrumentation_exit_pc_(instrumentation_exit_pc),
301 instrumentation_(instrumentation),
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800302 instrumentation_stack_(thread_in->GetInstrumentationStack()),
Ian Rogers62d6c772013-02-27 08:32:07 -0800303 frames_removed_(0) {}
Ian Rogers306057f2012-11-26 12:45:53 -0800304
Mathieu Chartier90443472015-07-16 20:32:27 -0700305 bool VisitFrame() OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800306 if (instrumentation_stack_->size() == 0) {
jeffhao725a9572012-11-13 18:20:12 -0800307 return false; // Stop.
308 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700309 ArtMethod* m = GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700310 if (GetCurrentQuickFrame() == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800311 if (kVerboseInstrumentation) {
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200312 LOG(INFO) << " Ignoring a shadow frame. Frame " << GetFrameId()
313 << " Method=" << PrettyMethod(m);
Ian Rogers62d6c772013-02-27 08:32:07 -0800314 }
315 return true; // Ignore shadow frames.
316 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700317 if (m == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800318 if (kVerboseInstrumentation) {
319 LOG(INFO) << " Skipping upcall. Frame " << GetFrameId();
320 }
Ian Rogers306057f2012-11-26 12:45:53 -0800321 return true; // Ignore upcalls.
322 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800323 bool removed_stub = false;
324 // TODO: make this search more efficient?
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100325 const size_t frameId = GetFrameId();
326 for (const InstrumentationStackFrame& instrumentation_frame : *instrumentation_stack_) {
327 if (instrumentation_frame.frame_id_ == frameId) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800328 if (kVerboseInstrumentation) {
329 LOG(INFO) << " Removing exit stub in " << DescribeLocation();
330 }
Jeff Hao9a916d32013-06-27 18:45:37 -0700331 if (instrumentation_frame.interpreter_entry_) {
332 CHECK(m == Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs));
333 } else {
334 CHECK(m == instrumentation_frame.method_) << PrettyMethod(m);
335 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800336 SetReturnPc(instrumentation_frame.return_pc_);
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100337 if (instrumentation_->ShouldNotifyMethodEnterExitEvents()) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100338 // Create the method exit events. As the methods didn't really exit the result is 0.
339 // We only do this if no debugger is attached to prevent from posting events twice.
340 instrumentation_->MethodExitEvent(thread_, instrumentation_frame.this_object_, m,
341 GetDexPc(), JValue());
342 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800343 frames_removed_++;
344 removed_stub = true;
345 break;
346 }
347 }
348 if (!removed_stub) {
349 if (kVerboseInstrumentation) {
350 LOG(INFO) << " No exit stub in " << DescribeLocation();
Ian Rogers306057f2012-11-26 12:45:53 -0800351 }
jeffhao725a9572012-11-13 18:20:12 -0800352 }
353 return true; // Continue.
354 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800355 Thread* const thread_;
Ian Rogers306057f2012-11-26 12:45:53 -0800356 const uintptr_t instrumentation_exit_pc_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800357 Instrumentation* const instrumentation_;
358 std::deque<instrumentation::InstrumentationStackFrame>* const instrumentation_stack_;
359 size_t frames_removed_;
jeffhao725a9572012-11-13 18:20:12 -0800360 };
Ian Rogers62d6c772013-02-27 08:32:07 -0800361 if (kVerboseInstrumentation) {
362 std::string thread_name;
363 thread->GetThreadName(thread_name);
364 LOG(INFO) << "Removing exit stubs in " << thread_name;
365 }
366 std::deque<instrumentation::InstrumentationStackFrame>* stack = thread->GetInstrumentationStack();
367 if (stack->size() > 0) {
368 Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700369 uintptr_t instrumentation_exit_pc =
370 reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc());
Ian Rogers62d6c772013-02-27 08:32:07 -0800371 RestoreStackVisitor visitor(thread, instrumentation_exit_pc, instrumentation);
372 visitor.WalkStack(true);
373 CHECK_EQ(visitor.frames_removed_, stack->size());
374 while (stack->size() > 0) {
375 stack->pop_front();
376 }
jeffhao725a9572012-11-13 18:20:12 -0800377 }
378}
379
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200380static bool HasEvent(Instrumentation::InstrumentationEvent expected, uint32_t events) {
381 return (events & expected) != 0;
382}
383
Ian Rogers62d6c772013-02-27 08:32:07 -0800384void Instrumentation::AddListener(InstrumentationListener* listener, uint32_t events) {
385 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200386 if (HasEvent(kMethodEntered, events)) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800387 method_entry_listeners_.push_back(listener);
Ian Rogers62d6c772013-02-27 08:32:07 -0800388 have_method_entry_listeners_ = true;
389 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200390 if (HasEvent(kMethodExited, events)) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800391 method_exit_listeners_.push_back(listener);
Ian Rogers62d6c772013-02-27 08:32:07 -0800392 have_method_exit_listeners_ = true;
393 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200394 if (HasEvent(kMethodUnwind, events)) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800395 method_unwind_listeners_.push_back(listener);
396 have_method_unwind_listeners_ = true;
397 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200398 if (HasEvent(kBackwardBranch, events)) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800399 backward_branch_listeners_.push_back(listener);
400 have_backward_branch_listeners_ = true;
401 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200402 if (HasEvent(kDexPcMoved, events)) {
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200403 std::list<InstrumentationListener*>* modified;
404 if (have_dex_pc_listeners_) {
405 modified = new std::list<InstrumentationListener*>(*dex_pc_listeners_.get());
406 } else {
407 modified = new std::list<InstrumentationListener*>();
408 }
409 modified->push_back(listener);
410 dex_pc_listeners_.reset(modified);
Ian Rogers62d6c772013-02-27 08:32:07 -0800411 have_dex_pc_listeners_ = true;
412 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200413 if (HasEvent(kFieldRead, events)) {
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200414 std::list<InstrumentationListener*>* modified;
415 if (have_field_read_listeners_) {
416 modified = new std::list<InstrumentationListener*>(*field_read_listeners_.get());
417 } else {
418 modified = new std::list<InstrumentationListener*>();
419 }
420 modified->push_back(listener);
421 field_read_listeners_.reset(modified);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200422 have_field_read_listeners_ = true;
423 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200424 if (HasEvent(kFieldWritten, events)) {
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200425 std::list<InstrumentationListener*>* modified;
426 if (have_field_write_listeners_) {
427 modified = new std::list<InstrumentationListener*>(*field_write_listeners_.get());
428 } else {
429 modified = new std::list<InstrumentationListener*>();
430 }
431 modified->push_back(listener);
432 field_write_listeners_.reset(modified);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200433 have_field_write_listeners_ = true;
434 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200435 if (HasEvent(kExceptionCaught, events)) {
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200436 std::list<InstrumentationListener*>* modified;
437 if (have_exception_caught_listeners_) {
438 modified = new std::list<InstrumentationListener*>(*exception_caught_listeners_.get());
439 } else {
440 modified = new std::list<InstrumentationListener*>();
441 }
442 modified->push_back(listener);
443 exception_caught_listeners_.reset(modified);
Jeff Hao14dd5a82013-04-11 10:23:36 -0700444 have_exception_caught_listeners_ = true;
445 }
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200446 UpdateInterpreterHandlerTable();
jeffhao725a9572012-11-13 18:20:12 -0800447}
448
Ian Rogers62d6c772013-02-27 08:32:07 -0800449void Instrumentation::RemoveListener(InstrumentationListener* listener, uint32_t events) {
450 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
Ian Rogers62d6c772013-02-27 08:32:07 -0800451
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200452 if (HasEvent(kMethodEntered, events) && have_method_entry_listeners_) {
453 method_entry_listeners_.remove(listener);
454 have_method_entry_listeners_ = !method_entry_listeners_.empty();
Ian Rogers62d6c772013-02-27 08:32:07 -0800455 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200456 if (HasEvent(kMethodExited, events) && have_method_exit_listeners_) {
457 method_exit_listeners_.remove(listener);
458 have_method_exit_listeners_ = !method_exit_listeners_.empty();
Ian Rogers62d6c772013-02-27 08:32:07 -0800459 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200460 if (HasEvent(kMethodUnwind, events) && have_method_unwind_listeners_) {
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200461 method_unwind_listeners_.remove(listener);
462 have_method_unwind_listeners_ = !method_unwind_listeners_.empty();
Ian Rogers62d6c772013-02-27 08:32:07 -0800463 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200464 if (HasEvent(kBackwardBranch, events) && have_backward_branch_listeners_) {
465 backward_branch_listeners_.remove(listener);
466 have_backward_branch_listeners_ = !backward_branch_listeners_.empty();
467 }
468 if (HasEvent(kDexPcMoved, events) && have_dex_pc_listeners_) {
469 std::list<InstrumentationListener*>* modified =
470 new std::list<InstrumentationListener*>(*dex_pc_listeners_.get());
471 modified->remove(listener);
472 have_dex_pc_listeners_ = !modified->empty();
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200473 if (have_dex_pc_listeners_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200474 dex_pc_listeners_.reset(modified);
475 } else {
476 dex_pc_listeners_.reset();
477 delete modified;
Ian Rogers62d6c772013-02-27 08:32:07 -0800478 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800479 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200480 if (HasEvent(kFieldRead, events) && have_field_read_listeners_) {
481 std::list<InstrumentationListener*>* modified =
482 new std::list<InstrumentationListener*>(*field_read_listeners_.get());
483 modified->remove(listener);
484 have_field_read_listeners_ = !modified->empty();
Daniel Mihalyi66445212014-08-21 15:57:25 +0200485 if (have_field_read_listeners_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200486 field_read_listeners_.reset(modified);
487 } else {
488 field_read_listeners_.reset();
489 delete modified;
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200490 }
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200491 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200492 if (HasEvent(kFieldWritten, events) && have_field_write_listeners_) {
493 std::list<InstrumentationListener*>* modified =
494 new std::list<InstrumentationListener*>(*field_write_listeners_.get());
495 modified->remove(listener);
496 have_field_write_listeners_ = !modified->empty();
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200497 if (have_field_write_listeners_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200498 field_write_listeners_.reset(modified);
499 } else {
500 field_write_listeners_.reset();
501 delete modified;
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200502 }
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200503 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200504 if (HasEvent(kExceptionCaught, events) && have_exception_caught_listeners_) {
505 std::list<InstrumentationListener*>* modified =
506 new std::list<InstrumentationListener*>(*exception_caught_listeners_.get());
507 modified->remove(listener);
508 have_exception_caught_listeners_ = !modified->empty();
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200509 if (have_exception_caught_listeners_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200510 exception_caught_listeners_.reset(modified);
511 } else {
512 exception_caught_listeners_.reset();
513 delete modified;
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200514 }
Jeff Hao14dd5a82013-04-11 10:23:36 -0700515 }
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200516 UpdateInterpreterHandlerTable();
jeffhao725a9572012-11-13 18:20:12 -0800517}
518
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200519Instrumentation::InstrumentationLevel Instrumentation::GetCurrentInstrumentationLevel() const {
Ian Rogers62d6c772013-02-27 08:32:07 -0800520 if (interpreter_stubs_installed_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200521 return InstrumentationLevel::kInstrumentWithInterpreter;
Ian Rogers62d6c772013-02-27 08:32:07 -0800522 } else if (entry_exit_stubs_installed_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200523 return InstrumentationLevel::kInstrumentWithInstrumentationStubs;
Ian Rogers62d6c772013-02-27 08:32:07 -0800524 } else {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200525 return InstrumentationLevel::kInstrumentNothing;
Ian Rogers62d6c772013-02-27 08:32:07 -0800526 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200527}
528
529void Instrumentation::ConfigureStubs(const char* key, InstrumentationLevel desired_level) {
530 // Store the instrumentation level for this key or remove it.
531 if (desired_level == InstrumentationLevel::kInstrumentNothing) {
532 // The client no longer needs instrumentation.
533 requested_instrumentation_levels_.erase(key);
534 } else {
535 // The client needs instrumentation.
536 requested_instrumentation_levels_.Overwrite(key, desired_level);
537 }
538
539 // Look for the highest required instrumentation level.
540 InstrumentationLevel requested_level = InstrumentationLevel::kInstrumentNothing;
541 for (const auto& v : requested_instrumentation_levels_) {
542 requested_level = std::max(requested_level, v.second);
543 }
544
545 interpret_only_ = (requested_level == InstrumentationLevel::kInstrumentWithInterpreter) ||
546 forced_interpret_only_;
547
548 InstrumentationLevel current_level = GetCurrentInstrumentationLevel();
549 if (requested_level == current_level) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800550 // We're already set.
551 return;
552 }
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100553 Thread* const self = Thread::Current();
Ian Rogers62d6c772013-02-27 08:32:07 -0800554 Runtime* runtime = Runtime::Current();
Sebastien Hertza8a697f2015-01-15 12:28:47 +0100555 Locks::mutator_lock_->AssertExclusiveHeld(self);
Ian Rogers62d6c772013-02-27 08:32:07 -0800556 Locks::thread_list_lock_->AssertNotHeld(self);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200557 if (requested_level > InstrumentationLevel::kInstrumentNothing) {
558 if (requested_level == InstrumentationLevel::kInstrumentWithInterpreter) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800559 interpreter_stubs_installed_ = true;
Ian Rogers62d6c772013-02-27 08:32:07 -0800560 entry_exit_stubs_installed_ = true;
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200561 } else {
562 CHECK_EQ(requested_level, InstrumentationLevel::kInstrumentWithInstrumentationStubs);
563 entry_exit_stubs_installed_ = true;
564 interpreter_stubs_installed_ = false;
Ian Rogers62d6c772013-02-27 08:32:07 -0800565 }
566 runtime->GetClassLinker()->VisitClasses(InstallStubsClassVisitor, this);
567 instrumentation_stubs_installed_ = true;
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100568 MutexLock mu(self, *Locks::thread_list_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800569 runtime->GetThreadList()->ForEach(InstrumentationInstallStack, this);
570 } else {
571 interpreter_stubs_installed_ = false;
572 entry_exit_stubs_installed_ = false;
573 runtime->GetClassLinker()->VisitClasses(InstallStubsClassVisitor, this);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100574 // Restore stack only if there is no method currently deoptimized.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700575 bool empty;
576 {
577 ReaderMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700578 empty = IsDeoptimizedMethodsEmpty(); // Avoid lock violation.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700579 }
580 if (empty) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100581 instrumentation_stubs_installed_ = false;
582 MutexLock mu(self, *Locks::thread_list_lock_);
583 Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this);
584 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800585 }
jeffhao725a9572012-11-13 18:20:12 -0800586}
587
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200588static void ResetQuickAllocEntryPointsForThread(Thread* thread, void* arg ATTRIBUTE_UNUSED) {
Ian Rogersfa824272013-11-05 16:12:57 -0800589 thread->ResetQuickAllocEntryPointsForThread();
590}
591
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700592void Instrumentation::SetEntrypointsInstrumented(bool instrumented) {
593 Thread* self = Thread::Current();
Mathieu Chartier661974a2014-01-09 11:23:53 -0800594 Runtime* runtime = Runtime::Current();
595 ThreadList* tl = runtime->GetThreadList();
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700596 Locks::mutator_lock_->AssertNotHeld(self);
597 Locks::instrument_entrypoints_lock_->AssertHeld(self);
598 if (runtime->IsStarted()) {
Mathieu Chartierbf9fc582015-03-13 17:21:25 -0700599 tl->SuspendAll(__FUNCTION__);
Mathieu Chartier661974a2014-01-09 11:23:53 -0800600 }
601 {
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700602 MutexLock mu(self, *Locks::runtime_shutdown_lock_);
Mathieu Chartier661974a2014-01-09 11:23:53 -0800603 SetQuickAllocEntryPointsInstrumented(instrumented);
604 ResetQuickAllocEntryPoints();
605 }
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700606 if (runtime->IsStarted()) {
Mathieu Chartier661974a2014-01-09 11:23:53 -0800607 tl->ResumeAll();
608 }
609}
610
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700611void Instrumentation::InstrumentQuickAllocEntryPoints() {
612 MutexLock mu(Thread::Current(), *Locks::instrument_entrypoints_lock_);
613 InstrumentQuickAllocEntryPointsLocked();
Ian Rogersfa824272013-11-05 16:12:57 -0800614}
615
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700616void Instrumentation::UninstrumentQuickAllocEntryPoints() {
617 MutexLock mu(Thread::Current(), *Locks::instrument_entrypoints_lock_);
618 UninstrumentQuickAllocEntryPointsLocked();
619}
620
621void Instrumentation::InstrumentQuickAllocEntryPointsLocked() {
622 Locks::instrument_entrypoints_lock_->AssertHeld(Thread::Current());
623 if (quick_alloc_entry_points_instrumentation_counter_ == 0) {
624 SetEntrypointsInstrumented(true);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800625 }
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700626 ++quick_alloc_entry_points_instrumentation_counter_;
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700627}
628
629void Instrumentation::UninstrumentQuickAllocEntryPointsLocked() {
630 Locks::instrument_entrypoints_lock_->AssertHeld(Thread::Current());
631 CHECK_GT(quick_alloc_entry_points_instrumentation_counter_, 0U);
632 --quick_alloc_entry_points_instrumentation_counter_;
633 if (quick_alloc_entry_points_instrumentation_counter_ == 0) {
634 SetEntrypointsInstrumented(false);
635 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800636}
637
638void Instrumentation::ResetQuickAllocEntryPoints() {
639 Runtime* runtime = Runtime::Current();
640 if (runtime->IsStarted()) {
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800641 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700642 runtime->GetThreadList()->ForEach(ResetQuickAllocEntryPointsForThread, nullptr);
Ian Rogersfa824272013-11-05 16:12:57 -0800643 }
644}
645
Mathieu Chartiere401d142015-04-22 13:56:20 -0700646void Instrumentation::UpdateMethodsCode(ArtMethod* method, const void* quick_code) {
Sebastien Hertza8a697f2015-01-15 12:28:47 +0100647 DCHECK(method->GetDeclaringClass()->IsResolved());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800648 const void* new_quick_code;
Ian Rogers62d6c772013-02-27 08:32:07 -0800649 if (LIKELY(!instrumentation_stubs_installed_)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800650 new_quick_code = quick_code;
Jeff Hao65d15d92013-07-16 16:39:33 -0700651 } else {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100652 if ((interpreter_stubs_installed_ || IsDeoptimized(method)) && !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800653 new_quick_code = GetQuickToInterpreterBridge();
Jeff Hao65d15d92013-07-16 16:39:33 -0700654 } else {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700655 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700656 if (class_linker->IsQuickResolutionStub(quick_code) ||
657 class_linker->IsQuickToInterpreterBridge(quick_code)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700658 new_quick_code = quick_code;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700659 } else if (entry_exit_stubs_installed_) {
660 new_quick_code = GetQuickInstrumentationEntryPoint();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700661 } else {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700662 new_quick_code = quick_code;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700663 }
Jeff Hao65d15d92013-07-16 16:39:33 -0700664 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800665 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800666 UpdateEntrypoints(method, new_quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100667}
668
Mathieu Chartiere401d142015-04-22 13:56:20 -0700669bool Instrumentation::AddDeoptimizedMethod(ArtMethod* method) {
670 if (IsDeoptimizedMethod(method)) {
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700671 // Already in the map. Return.
672 return false;
673 }
674 // Not found. Add it.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700675 deoptimized_methods_.insert(method);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700676 return true;
677}
678
Mathieu Chartiere401d142015-04-22 13:56:20 -0700679bool Instrumentation::IsDeoptimizedMethod(ArtMethod* method) {
680 return deoptimized_methods_.find(method) != deoptimized_methods_.end();
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700681}
682
Mathieu Chartiere401d142015-04-22 13:56:20 -0700683ArtMethod* Instrumentation::BeginDeoptimizedMethod() {
684 if (deoptimized_methods_.empty()) {
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700685 // Empty.
686 return nullptr;
687 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700688 return *deoptimized_methods_.begin();
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700689}
690
Mathieu Chartiere401d142015-04-22 13:56:20 -0700691bool Instrumentation::RemoveDeoptimizedMethod(ArtMethod* method) {
692 auto it = deoptimized_methods_.find(method);
693 if (it == deoptimized_methods_.end()) {
694 return false;
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700695 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700696 deoptimized_methods_.erase(it);
697 return true;
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700698}
699
700bool Instrumentation::IsDeoptimizedMethodsEmpty() const {
701 return deoptimized_methods_.empty();
702}
703
Mathieu Chartiere401d142015-04-22 13:56:20 -0700704void Instrumentation::Deoptimize(ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100705 CHECK(!method->IsNative());
706 CHECK(!method->IsProxyMethod());
707 CHECK(!method->IsAbstract());
708
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700709 Thread* self = Thread::Current();
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700710 {
711 WriterMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700712 bool has_not_been_deoptimized = AddDeoptimizedMethod(method);
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200713 CHECK(has_not_been_deoptimized) << "Method " << PrettyMethod(method)
714 << " is already deoptimized";
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700715 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100716 if (!interpreter_stubs_installed_) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800717 UpdateEntrypoints(method, GetQuickInstrumentationEntryPoint());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100718
719 // Install instrumentation exit stub and instrumentation frames. We may already have installed
720 // these previously so it will only cover the newly created frames.
721 instrumentation_stubs_installed_ = true;
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700722 MutexLock mu(self, *Locks::thread_list_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100723 Runtime::Current()->GetThreadList()->ForEach(InstrumentationInstallStack, this);
724 }
725}
726
Mathieu Chartiere401d142015-04-22 13:56:20 -0700727void Instrumentation::Undeoptimize(ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100728 CHECK(!method->IsNative());
729 CHECK(!method->IsProxyMethod());
730 CHECK(!method->IsAbstract());
731
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700732 Thread* self = Thread::Current();
733 bool empty;
734 {
735 WriterMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700736 bool found_and_erased = RemoveDeoptimizedMethod(method);
737 CHECK(found_and_erased) << "Method " << PrettyMethod(method)
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700738 << " is not deoptimized";
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700739 empty = IsDeoptimizedMethodsEmpty();
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700740 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100741
742 // Restore code and possibly stack only if we did not deoptimize everything.
743 if (!interpreter_stubs_installed_) {
744 // Restore its code or resolution trampoline.
745 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800746 if (method->IsStatic() && !method->IsConstructor() &&
747 !method->GetDeclaringClass()->IsInitialized()) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800748 UpdateEntrypoints(method, GetQuickResolutionStub());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100749 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800750 const void* quick_code = class_linker->GetQuickOatCodeFor(method);
Elliott Hughes956af0f2014-12-11 14:34:28 -0800751 UpdateEntrypoints(method, quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100752 }
753
754 // If there is no deoptimized method left, we can restore the stack of each thread.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700755 if (empty) {
756 MutexLock mu(self, *Locks::thread_list_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100757 Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this);
758 instrumentation_stubs_installed_ = false;
759 }
760 }
761}
762
Mathieu Chartiere401d142015-04-22 13:56:20 -0700763bool Instrumentation::IsDeoptimized(ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100764 DCHECK(method != nullptr);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700765 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700766 return IsDeoptimizedMethod(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100767}
768
769void Instrumentation::EnableDeoptimization() {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700770 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700771 CHECK(IsDeoptimizedMethodsEmpty());
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100772 CHECK_EQ(deoptimization_enabled_, false);
773 deoptimization_enabled_ = true;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100774}
775
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200776void Instrumentation::DisableDeoptimization(const char* key) {
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100777 CHECK_EQ(deoptimization_enabled_, true);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100778 // If we deoptimized everything, undo it.
779 if (interpreter_stubs_installed_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200780 UndeoptimizeEverything(key);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100781 }
782 // Undeoptimized selected methods.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700783 while (true) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700784 ArtMethod* method;
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700785 {
786 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700787 if (IsDeoptimizedMethodsEmpty()) {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700788 break;
789 }
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700790 method = BeginDeoptimizedMethod();
791 CHECK(method != nullptr);
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700792 }
793 Undeoptimize(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100794 }
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100795 deoptimization_enabled_ = false;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100796}
797
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100798// Indicates if instrumentation should notify method enter/exit events to the listeners.
799bool Instrumentation::ShouldNotifyMethodEnterExitEvents() const {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200800 if (!HasMethodEntryListeners() && !HasMethodExitListeners()) {
801 return false;
802 }
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100803 return !deoptimization_enabled_ && !interpreter_stubs_installed_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100804}
805
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200806void Instrumentation::DeoptimizeEverything(const char* key) {
807 CHECK(deoptimization_enabled_);
808 ConfigureStubs(key, InstrumentationLevel::kInstrumentWithInterpreter);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100809}
810
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200811void Instrumentation::UndeoptimizeEverything(const char* key) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100812 CHECK(interpreter_stubs_installed_);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200813 CHECK(deoptimization_enabled_);
814 ConfigureStubs(key, InstrumentationLevel::kInstrumentNothing);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100815}
816
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200817void Instrumentation::EnableMethodTracing(const char* key, bool needs_interpreter) {
818 InstrumentationLevel level;
819 if (needs_interpreter) {
820 level = InstrumentationLevel::kInstrumentWithInterpreter;
821 } else {
822 level = InstrumentationLevel::kInstrumentWithInstrumentationStubs;
823 }
824 ConfigureStubs(key, level);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100825}
826
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200827void Instrumentation::DisableMethodTracing(const char* key) {
828 ConfigureStubs(key, InstrumentationLevel::kInstrumentNothing);
jeffhao725a9572012-11-13 18:20:12 -0800829}
830
Mathieu Chartiere401d142015-04-22 13:56:20 -0700831const void* Instrumentation::GetQuickCodeFor(ArtMethod* method, size_t pointer_size) const {
Ian Rogers62d6c772013-02-27 08:32:07 -0800832 Runtime* runtime = Runtime::Current();
833 if (LIKELY(!instrumentation_stubs_installed_)) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800834 const void* code = method->GetEntryPointFromQuickCompiledCodePtrSize(pointer_size);
Vladimir Marko8a630572014-04-09 18:45:35 +0100835 DCHECK(code != nullptr);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700836 ClassLinker* class_linker = runtime->GetClassLinker();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700837 if (LIKELY(!class_linker->IsQuickResolutionStub(code) &&
838 !class_linker->IsQuickToInterpreterBridge(code)) &&
839 !class_linker->IsQuickResolutionStub(code) &&
840 !class_linker->IsQuickToInterpreterBridge(code)) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800841 return code;
842 }
843 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800844 return runtime->GetClassLinker()->GetQuickOatCodeFor(method);
jeffhao725a9572012-11-13 18:20:12 -0800845}
846
Ian Rogers62d6c772013-02-27 08:32:07 -0800847void Instrumentation::MethodEnterEventImpl(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700848 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800849 uint32_t dex_pc) const {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700850 auto it = method_entry_listeners_.begin();
Jeff Hao65d15d92013-07-16 16:39:33 -0700851 bool is_end = (it == method_entry_listeners_.end());
852 // Implemented this way to prevent problems caused by modification of the list while iterating.
853 while (!is_end) {
854 InstrumentationListener* cur = *it;
855 ++it;
856 is_end = (it == method_entry_listeners_.end());
857 cur->MethodEntered(thread, this_object, method, dex_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800858 }
859}
860
861void Instrumentation::MethodExitEventImpl(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700862 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800863 uint32_t dex_pc, const JValue& return_value) const {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700864 auto it = method_exit_listeners_.begin();
Jeff Hao65d15d92013-07-16 16:39:33 -0700865 bool is_end = (it == method_exit_listeners_.end());
866 // Implemented this way to prevent problems caused by modification of the list while iterating.
867 while (!is_end) {
868 InstrumentationListener* cur = *it;
869 ++it;
870 is_end = (it == method_exit_listeners_.end());
871 cur->MethodExited(thread, this_object, method, dex_pc, return_value);
Ian Rogers62d6c772013-02-27 08:32:07 -0800872 }
873}
874
875void Instrumentation::MethodUnwindEvent(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700876 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800877 uint32_t dex_pc) const {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200878 if (HasMethodUnwindListeners()) {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700879 for (InstrumentationListener* listener : method_unwind_listeners_) {
Sebastien Hertz51db44a2013-11-19 10:00:29 +0100880 listener->MethodUnwind(thread, this_object, method, dex_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800881 }
882 }
883}
884
885void Instrumentation::DexPcMovedEventImpl(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 {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200888 std::shared_ptr<std::list<InstrumentationListener*>> original(dex_pc_listeners_);
889 for (InstrumentationListener* listener : *original.get()) {
890 listener->DexPcMoved(thread, this_object, method, dex_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800891 }
892}
893
Mathieu Chartiere401d142015-04-22 13:56:20 -0700894void Instrumentation::BackwardBranchImpl(Thread* thread, ArtMethod* method,
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800895 int32_t offset) const {
896 for (InstrumentationListener* listener : backward_branch_listeners_) {
897 listener->BackwardBranch(thread, method, offset);
898 }
899}
900
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200901void Instrumentation::FieldReadEventImpl(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700902 ArtMethod* method, uint32_t dex_pc,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700903 ArtField* field) const {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200904 std::shared_ptr<std::list<InstrumentationListener*>> original(field_read_listeners_);
905 for (InstrumentationListener* listener : *original.get()) {
906 listener->FieldRead(thread, this_object, method, dex_pc, field);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200907 }
908}
909
910void Instrumentation::FieldWriteEventImpl(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700911 ArtMethod* method, uint32_t dex_pc,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700912 ArtField* field, const JValue& field_value) const {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200913 std::shared_ptr<std::list<InstrumentationListener*>> original(field_write_listeners_);
914 for (InstrumentationListener* listener : *original.get()) {
915 listener->FieldWritten(thread, this_object, method, dex_pc, field, field_value);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200916 }
917}
918
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000919void Instrumentation::ExceptionCaughtEvent(Thread* thread,
Sebastien Hertz947ff082013-09-17 14:10:13 +0200920 mirror::Throwable* exception_object) const {
Sebastien Hertz9f102032014-05-23 08:59:42 +0200921 if (HasExceptionCaughtListeners()) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000922 DCHECK_EQ(thread->GetException(), exception_object);
Jeff Haoc0bd4da2013-04-11 15:52:28 -0700923 thread->ClearException();
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200924 std::shared_ptr<std::list<InstrumentationListener*>> original(exception_caught_listeners_);
925 for (InstrumentationListener* listener : *original.get()) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000926 listener->ExceptionCaught(thread, exception_object);
Ian Rogers62d6c772013-02-27 08:32:07 -0800927 }
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000928 thread->SetException(exception_object);
Ian Rogers62d6c772013-02-27 08:32:07 -0800929 }
930}
931
932static void CheckStackDepth(Thread* self, const InstrumentationStackFrame& instrumentation_frame,
933 int delta)
Mathieu Chartier90443472015-07-16 20:32:27 -0700934 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100935 size_t frame_id = StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk) + delta;
Ian Rogers62d6c772013-02-27 08:32:07 -0800936 if (frame_id != instrumentation_frame.frame_id_) {
937 LOG(ERROR) << "Expected frame_id=" << frame_id << " but found "
938 << instrumentation_frame.frame_id_;
939 StackVisitor::DescribeStack(self);
940 CHECK_EQ(frame_id, instrumentation_frame.frame_id_);
941 }
942}
943
944void Instrumentation::PushInstrumentationStackFrame(Thread* self, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700945 ArtMethod* method,
Jeff Hao9a916d32013-06-27 18:45:37 -0700946 uintptr_t lr, bool interpreter_entry) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800947 // We have a callee-save frame meaning this value is guaranteed to never be 0.
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100948 size_t frame_id = StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk);
Ian Rogers62d6c772013-02-27 08:32:07 -0800949 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
950 if (kVerboseInstrumentation) {
Brian Carlstrom2d888622013-07-18 17:02:00 -0700951 LOG(INFO) << "Entering " << PrettyMethod(method) << " from PC " << reinterpret_cast<void*>(lr);
Ian Rogers62d6c772013-02-27 08:32:07 -0800952 }
953 instrumentation::InstrumentationStackFrame instrumentation_frame(this_object, method, lr,
Jeff Hao9a916d32013-06-27 18:45:37 -0700954 frame_id, interpreter_entry);
Ian Rogers62d6c772013-02-27 08:32:07 -0800955 stack->push_front(instrumentation_frame);
956
Sebastien Hertz320deb22014-06-11 19:45:05 +0200957 if (!interpreter_entry) {
958 MethodEnterEvent(self, this_object, method, 0);
959 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800960}
961
Andreas Gamped58342c2014-06-05 14:18:08 -0700962TwoWordReturn Instrumentation::PopInstrumentationStackFrame(Thread* self, uintptr_t* return_pc,
963 uint64_t gpr_result,
964 uint64_t fpr_result) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800965 // Do the pop.
966 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
967 CHECK_GT(stack->size(), 0U);
968 InstrumentationStackFrame instrumentation_frame = stack->front();
969 stack->pop_front();
970
971 // Set return PC and check the sanity of the stack.
972 *return_pc = instrumentation_frame.return_pc_;
973 CheckStackDepth(self, instrumentation_frame, 0);
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700974 self->VerifyStack();
Ian Rogers62d6c772013-02-27 08:32:07 -0800975
Mathieu Chartiere401d142015-04-22 13:56:20 -0700976 ArtMethod* method = instrumentation_frame.method_;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700977 uint32_t length;
978 char return_shorty = method->GetShorty(&length)[0];
Ian Rogers62d6c772013-02-27 08:32:07 -0800979 JValue return_value;
980 if (return_shorty == 'V') {
981 return_value.SetJ(0);
982 } else if (return_shorty == 'F' || return_shorty == 'D') {
983 return_value.SetJ(fpr_result);
984 } else {
985 return_value.SetJ(gpr_result);
986 }
987 // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to
988 // return_pc.
989 uint32_t dex_pc = DexFile::kDexNoIndex;
990 mirror::Object* this_object = instrumentation_frame.this_object_;
Sebastien Hertz320deb22014-06-11 19:45:05 +0200991 if (!instrumentation_frame.interpreter_entry_) {
992 MethodExitEvent(self, this_object, instrumentation_frame.method_, dex_pc, return_value);
993 }
jeffhao725a9572012-11-13 18:20:12 -0800994
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100995 // Deoptimize if the caller needs to continue execution in the interpreter. Do nothing if we get
996 // back to an upcall.
997 NthCallerVisitor visitor(self, 1, true);
998 visitor.WalkStack(true);
Sebastien Hertz270a0e12015-01-16 19:49:09 +0100999 bool deoptimize = (visitor.caller != nullptr) &&
Daniel Mihalyieb076692014-08-22 17:33:31 +02001000 (interpreter_stubs_installed_ || IsDeoptimized(visitor.caller) ||
1001 Dbg::IsForcedInterpreterNeededForUpcall(self, visitor.caller));
Ian Rogers62d6c772013-02-27 08:32:07 -08001002 if (deoptimize) {
1003 if (kVerboseInstrumentation) {
Sebastien Hertz270a0e12015-01-16 19:49:09 +01001004 LOG(INFO) << StringPrintf("Deoptimizing %s by returning from %s with result %#" PRIx64 " in ",
1005 PrettyMethod(visitor.caller).c_str(),
1006 PrettyMethod(method).c_str(),
1007 return_value.GetJ()) << *self;
Ian Rogers62d6c772013-02-27 08:32:07 -08001008 }
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -07001009 self->SetDeoptimizationReturnValue(return_value, return_shorty == 'L');
Andreas Gamped58342c2014-06-05 14:18:08 -07001010 return GetTwoWordSuccessValue(*return_pc,
1011 reinterpret_cast<uintptr_t>(GetQuickDeoptimizationEntryPoint()));
Ian Rogers62d6c772013-02-27 08:32:07 -08001012 } else {
1013 if (kVerboseInstrumentation) {
Brian Carlstrom2d888622013-07-18 17:02:00 -07001014 LOG(INFO) << "Returning from " << PrettyMethod(method)
1015 << " to PC " << reinterpret_cast<void*>(*return_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -08001016 }
Andreas Gamped58342c2014-06-05 14:18:08 -07001017 return GetTwoWordSuccessValue(0, *return_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -08001018 }
jeffhao725a9572012-11-13 18:20:12 -08001019}
1020
Ian Rogers62d6c772013-02-27 08:32:07 -08001021void Instrumentation::PopMethodForUnwind(Thread* self, bool is_deoptimization) const {
1022 // Do the pop.
1023 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
1024 CHECK_GT(stack->size(), 0U);
1025 InstrumentationStackFrame instrumentation_frame = stack->front();
1026 // TODO: bring back CheckStackDepth(self, instrumentation_frame, 2);
1027 stack->pop_front();
1028
Mathieu Chartiere401d142015-04-22 13:56:20 -07001029 ArtMethod* method = instrumentation_frame.method_;
Ian Rogers62d6c772013-02-27 08:32:07 -08001030 if (is_deoptimization) {
1031 if (kVerboseInstrumentation) {
1032 LOG(INFO) << "Popping for deoptimization " << PrettyMethod(method);
1033 }
1034 } else {
1035 if (kVerboseInstrumentation) {
1036 LOG(INFO) << "Popping for unwind " << PrettyMethod(method);
1037 }
1038
1039 // Notify listeners of method unwind.
1040 // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to
1041 // return_pc.
1042 uint32_t dex_pc = DexFile::kDexNoIndex;
1043 MethodUnwindEvent(self, instrumentation_frame.this_object_, method, dex_pc);
1044 }
1045}
1046
1047std::string InstrumentationStackFrame::Dump() const {
1048 std::ostringstream os;
1049 os << "Frame " << frame_id_ << " " << PrettyMethod(method_) << ":"
1050 << reinterpret_cast<void*>(return_pc_) << " this=" << reinterpret_cast<void*>(this_object_);
1051 return os.str();
1052}
1053
1054} // namespace instrumentation
jeffhao725a9572012-11-13 18:20:12 -08001055} // namespace art