blob: 748463529eab3264ce21dd8a7beba6c28a1fbcfb [file] [log] [blame]
jeffhao725a9572012-11-13 18:20:12 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "instrumentation.h"
18
Ian Rogersc7dd2952014-10-21 23:31:19 -070019#include <sstream>
20
Ian Rogerse63db272014-07-15 15:36:11 -070021#include "arch/context.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070022#include "art_method-inl.h"
Ian Rogersef7d42f2014-01-06 12:55:46 -080023#include "atomic.h"
jeffhao725a9572012-11-13 18:20:12 -080024#include "class_linker.h"
25#include "debugger.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080026#include "dex_file-inl.h"
Ian Rogersc7dd2952014-10-21 23:31:19 -070027#include "entrypoints/quick/quick_entrypoints.h"
Mathieu Chartierd8891782014-03-02 13:28:37 -080028#include "entrypoints/quick/quick_alloc_entrypoints.h"
Ian Rogers6f3dbba2014-10-14 17:41:57 -070029#include "entrypoints/runtime_asm_entrypoints.h"
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070030#include "gc_root-inl.h"
Sebastien Hertz138dbfc2013-12-04 18:15:25 +010031#include "interpreter/interpreter.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080032#include "jit/jit.h"
33#include "jit/jit_code_cache.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080034#include "mirror/class-inl.h"
35#include "mirror/dex_cache.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080036#include "mirror/object_array-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070037#include "mirror/object-inl.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080038#include "nth_caller_visitor.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010039#include "oat_quick_method_header.h"
jeffhao725a9572012-11-13 18:20:12 -080040#include "thread.h"
41#include "thread_list.h"
jeffhao725a9572012-11-13 18:20:12 -080042
43namespace art {
Ian Rogers62d6c772013-02-27 08:32:07 -080044namespace instrumentation {
jeffhao725a9572012-11-13 18:20:12 -080045
Sebastien Hertz0462c4c2015-04-01 16:34:17 +020046constexpr bool kVerboseInstrumentation = false;
Sebastien Hertz5bfd5c92013-11-15 11:36:07 +010047
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +010048// Instrumentation works on non-inlined frames by updating returned PCs
49// of compiled frames.
50static constexpr StackVisitor::StackWalkKind kInstrumentationStackWalk =
51 StackVisitor::StackWalkKind::kSkipInlinedFrames;
52
Mathieu Chartiere0671ce2015-07-28 17:23:28 -070053class InstallStubsClassVisitor : public ClassVisitor {
54 public:
55 explicit InstallStubsClassVisitor(Instrumentation* instrumentation)
56 : instrumentation_(instrumentation) {}
57
Mathieu Chartier1aa8ec22016-02-01 10:34:47 -080058 bool operator()(mirror::Class* klass) OVERRIDE REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere0671ce2015-07-28 17:23:28 -070059 instrumentation_->InstallStubsForClass(klass);
60 return true; // we visit all classes.
61 }
62
63 private:
64 Instrumentation* const instrumentation_;
65};
66
Ian Rogers62d6c772013-02-27 08:32:07 -080067
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -070068Instrumentation::Instrumentation()
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +000069 : instrumentation_stubs_installed_(false),
70 entry_exit_stubs_installed_(false),
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -070071 interpreter_stubs_installed_(false),
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +000072 interpret_only_(false),
73 forced_interpret_only_(false),
74 have_method_entry_listeners_(false),
75 have_method_exit_listeners_(false),
76 have_method_unwind_listeners_(false),
77 have_dex_pc_listeners_(false),
78 have_field_read_listeners_(false),
79 have_field_write_listeners_(false),
80 have_exception_caught_listeners_(false),
Nicolas Geoffray81f0f952016-01-20 16:25:19 +000081 have_branch_listeners_(false),
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +000082 have_invoke_virtual_or_interface_listeners_(false),
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -070083 deoptimized_methods_lock_("deoptimized methods lock"),
84 deoptimization_enabled_(false),
85 interpreter_handler_table_(kMainHandlerTable),
86 quick_alloc_entry_points_instrumentation_counter_(0) {
87}
88
Sebastien Hertza10aa372015-01-21 17:30:58 +010089void Instrumentation::InstallStubsForClass(mirror::Class* klass) {
Sebastien Hertza8a697f2015-01-15 12:28:47 +010090 if (klass->IsErroneous()) {
91 // We can't execute code in a erroneous class: do nothing.
92 } else if (!klass->IsResolved()) {
93 // We need the class to be resolved to install/uninstall stubs. Otherwise its methods
94 // could not be initialized or linked with regards to class inheritance.
95 } else {
Alex Light51a64d52015-12-17 13:55:59 -080096 for (ArtMethod& method : klass->GetMethods(sizeof(void*))) {
97 InstallStubsForMethod(&method);
Sebastien Hertza8a697f2015-01-15 12:28:47 +010098 }
jeffhao725a9572012-11-13 18:20:12 -080099 }
jeffhao725a9572012-11-13 18:20:12 -0800100}
101
Mathieu Chartiere401d142015-04-22 13:56:20 -0700102static void UpdateEntrypoints(ArtMethod* method, const void* quick_code)
Mathieu Chartier90443472015-07-16 20:32:27 -0700103 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800104 method->SetEntryPointFromQuickCompiledCode(quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100105}
106
Mathieu Chartiere401d142015-04-22 13:56:20 -0700107void Instrumentation::InstallStubsForMethod(ArtMethod* method) {
Alex Light9139e002015-10-09 15:59:48 -0700108 if (!method->IsInvokable() || method->IsProxyMethod()) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100109 // Do not change stubs for these methods.
110 return;
111 }
Jeff Hao56802772014-08-19 10:17:36 -0700112 // Don't stub Proxy.<init>. Note that the Proxy class itself is not a proxy class.
113 if (method->IsConstructor() &&
114 method->GetDeclaringClass()->DescriptorEquals("Ljava/lang/reflect/Proxy;")) {
Jeff Haodb8a6642014-08-14 17:18:52 -0700115 return;
116 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800117 const void* new_quick_code;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100118 bool uninstall = !entry_exit_stubs_installed_ && !interpreter_stubs_installed_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800119 Runtime* const runtime = Runtime::Current();
120 ClassLinker* const class_linker = runtime->GetClassLinker();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100121 bool is_class_initialized = method->GetDeclaringClass()->IsInitialized();
122 if (uninstall) {
123 if ((forced_interpret_only_ || IsDeoptimized(method)) && !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800124 new_quick_code = GetQuickToInterpreterBridge();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100125 } else if (is_class_initialized || !method->IsStatic() || method->IsConstructor()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800126 new_quick_code = class_linker->GetQuickOatCodeFor(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100127 } else {
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700128 new_quick_code = GetQuickResolutionStub();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100129 }
130 } else { // !uninstall
Sebastien Hertzbae182c2013-12-17 10:42:03 +0100131 if ((interpreter_stubs_installed_ || forced_interpret_only_ || IsDeoptimized(method)) &&
132 !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800133 new_quick_code = GetQuickToInterpreterBridge();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100134 } else {
135 // Do not overwrite resolution trampoline. When the trampoline initializes the method's
136 // class, all its static methods code will be set to the instrumentation entry point.
137 // For more details, see ClassLinker::FixupStaticTrampolines.
138 if (is_class_initialized || !method->IsStatic() || method->IsConstructor()) {
Sebastien Hertz320deb22014-06-11 19:45:05 +0200139 if (entry_exit_stubs_installed_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800140 new_quick_code = GetQuickInstrumentationEntryPoint();
Sebastien Hertz320deb22014-06-11 19:45:05 +0200141 } else {
Sebastien Hertz320deb22014-06-11 19:45:05 +0200142 new_quick_code = class_linker->GetQuickOatCodeFor(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100143 }
144 } else {
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700145 new_quick_code = GetQuickResolutionStub();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100146 }
147 }
148 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800149 UpdateEntrypoints(method, new_quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100150}
151
Ian Rogers62d6c772013-02-27 08:32:07 -0800152// Places the instrumentation exit pc as the return PC for every quick frame. This also allows
153// deoptimization of quick frames to interpreter frames.
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100154// Since we may already have done this previously, we need to push new instrumentation frame before
155// existing instrumentation frames.
Ian Rogers62d6c772013-02-27 08:32:07 -0800156static void InstrumentationInstallStack(Thread* thread, void* arg)
Mathieu Chartier90443472015-07-16 20:32:27 -0700157 SHARED_REQUIRES(Locks::mutator_lock_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200158 struct InstallStackVisitor FINAL : public StackVisitor {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800159 InstallStackVisitor(Thread* thread_in, Context* context, uintptr_t instrumentation_exit_pc)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100160 : StackVisitor(thread_in, context, kInstrumentationStackWalk),
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800161 instrumentation_stack_(thread_in->GetInstrumentationStack()),
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100162 instrumentation_exit_pc_(instrumentation_exit_pc),
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100163 reached_existing_instrumentation_frames_(false), instrumentation_stack_depth_(0),
164 last_return_pc_(0) {
165 }
jeffhao725a9572012-11-13 18:20:12 -0800166
Mathieu Chartier90443472015-07-16 20:32:27 -0700167 bool VisitFrame() OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700168 ArtMethod* m = GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700169 if (m == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800170 if (kVerboseInstrumentation) {
171 LOG(INFO) << " Skipping upcall. Frame " << GetFrameId();
172 }
173 last_return_pc_ = 0;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700174 return true; // Ignore upcalls.
Ian Rogers306057f2012-11-26 12:45:53 -0800175 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700176 if (GetCurrentQuickFrame() == nullptr) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800177 bool interpreter_frame = true;
Sebastien Hertz320deb22014-06-11 19:45:05 +0200178 InstrumentationStackFrame instrumentation_frame(GetThisObject(), m, 0, GetFrameId(),
179 interpreter_frame);
Jeff Haoa15a81b2014-05-27 18:25:47 -0700180 if (kVerboseInstrumentation) {
181 LOG(INFO) << "Pushing shadow frame " << instrumentation_frame.Dump();
182 }
183 shadow_stack_.push_back(instrumentation_frame);
184 return true; // Continue.
185 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800186 uintptr_t return_pc = GetReturnPc();
Sebastien Hertz320deb22014-06-11 19:45:05 +0200187 if (m->IsRuntimeMethod()) {
188 if (return_pc == instrumentation_exit_pc_) {
189 if (kVerboseInstrumentation) {
190 LOG(INFO) << " Handling quick to interpreter transition. Frame " << GetFrameId();
191 }
192 CHECK_LT(instrumentation_stack_depth_, instrumentation_stack_->size());
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200193 const InstrumentationStackFrame& frame =
194 instrumentation_stack_->at(instrumentation_stack_depth_);
Sebastien Hertz320deb22014-06-11 19:45:05 +0200195 CHECK(frame.interpreter_entry_);
196 // This is an interpreter frame so method enter event must have been reported. However we
197 // need to push a DEX pc into the dex_pcs_ list to match size of instrumentation stack.
198 // Since we won't report method entry here, we can safely push any DEX pc.
199 dex_pcs_.push_back(0);
200 last_return_pc_ = frame.return_pc_;
201 ++instrumentation_stack_depth_;
202 return true;
203 } else {
204 if (kVerboseInstrumentation) {
205 LOG(INFO) << " Skipping runtime method. Frame " << GetFrameId();
206 }
207 last_return_pc_ = GetReturnPc();
208 return true; // Ignore unresolved methods since they will be instrumented after resolution.
209 }
210 }
211 if (kVerboseInstrumentation) {
212 LOG(INFO) << " Installing exit stub in " << DescribeLocation();
213 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100214 if (return_pc == instrumentation_exit_pc_) {
215 // We've reached a frame which has already been installed with instrumentation exit stub.
216 // We should have already installed instrumentation on previous frames.
217 reached_existing_instrumentation_frames_ = true;
218
219 CHECK_LT(instrumentation_stack_depth_, instrumentation_stack_->size());
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200220 const InstrumentationStackFrame& frame =
221 instrumentation_stack_->at(instrumentation_stack_depth_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100222 CHECK_EQ(m, frame.method_) << "Expected " << PrettyMethod(m)
223 << ", Found " << PrettyMethod(frame.method_);
224 return_pc = frame.return_pc_;
225 if (kVerboseInstrumentation) {
226 LOG(INFO) << "Ignoring already instrumented " << frame.Dump();
227 }
228 } else {
229 CHECK_NE(return_pc, 0U);
230 CHECK(!reached_existing_instrumentation_frames_);
231 InstrumentationStackFrame instrumentation_frame(GetThisObject(), m, return_pc, GetFrameId(),
232 false);
233 if (kVerboseInstrumentation) {
234 LOG(INFO) << "Pushing frame " << instrumentation_frame.Dump();
235 }
236
Sebastien Hertz320deb22014-06-11 19:45:05 +0200237 // Insert frame at the right position so we do not corrupt the instrumentation stack.
238 // Instrumentation stack frames are in descending frame id order.
239 auto it = instrumentation_stack_->begin();
240 for (auto end = instrumentation_stack_->end(); it != end; ++it) {
241 const InstrumentationStackFrame& current = *it;
242 if (instrumentation_frame.frame_id_ >= current.frame_id_) {
243 break;
244 }
245 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100246 instrumentation_stack_->insert(it, instrumentation_frame);
247 SetReturnPc(instrumentation_exit_pc_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800248 }
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100249 dex_pcs_.push_back((GetCurrentOatQuickMethodHeader() == nullptr)
250 ? DexFile::kDexNoIndex
251 : GetCurrentOatQuickMethodHeader()->ToDexPc(m, last_return_pc_));
Ian Rogers62d6c772013-02-27 08:32:07 -0800252 last_return_pc_ = return_pc;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100253 ++instrumentation_stack_depth_;
Ian Rogers306057f2012-11-26 12:45:53 -0800254 return true; // Continue.
255 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800256 std::deque<InstrumentationStackFrame>* const instrumentation_stack_;
Jeff Haoa15a81b2014-05-27 18:25:47 -0700257 std::vector<InstrumentationStackFrame> shadow_stack_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800258 std::vector<uint32_t> dex_pcs_;
Ian Rogers306057f2012-11-26 12:45:53 -0800259 const uintptr_t instrumentation_exit_pc_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100260 bool reached_existing_instrumentation_frames_;
261 size_t instrumentation_stack_depth_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800262 uintptr_t last_return_pc_;
Ian Rogers306057f2012-11-26 12:45:53 -0800263 };
Ian Rogers62d6c772013-02-27 08:32:07 -0800264 if (kVerboseInstrumentation) {
265 std::string thread_name;
266 thread->GetThreadName(thread_name);
267 LOG(INFO) << "Installing exit stubs in " << thread_name;
Ian Rogers306057f2012-11-26 12:45:53 -0800268 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100269
270 Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg);
Ian Rogers700a4022014-05-19 16:49:03 -0700271 std::unique_ptr<Context> context(Context::Create());
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700272 uintptr_t instrumentation_exit_pc = reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc());
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100273 InstallStackVisitor visitor(thread, context.get(), instrumentation_exit_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800274 visitor.WalkStack(true);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100275 CHECK_EQ(visitor.dex_pcs_.size(), thread->GetInstrumentationStack()->size());
Ian Rogers62d6c772013-02-27 08:32:07 -0800276
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100277 if (instrumentation->ShouldNotifyMethodEnterExitEvents()) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100278 // Create method enter events for all methods currently on the thread's stack. We only do this
279 // if no debugger is attached to prevent from posting events twice.
Jeff Haoa15a81b2014-05-27 18:25:47 -0700280 auto ssi = visitor.shadow_stack_.rbegin();
281 for (auto isi = thread->GetInstrumentationStack()->rbegin(),
282 end = thread->GetInstrumentationStack()->rend(); isi != end; ++isi) {
283 while (ssi != visitor.shadow_stack_.rend() && (*ssi).frame_id_ < (*isi).frame_id_) {
284 instrumentation->MethodEnterEvent(thread, (*ssi).this_object_, (*ssi).method_, 0);
285 ++ssi;
286 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100287 uint32_t dex_pc = visitor.dex_pcs_.back();
288 visitor.dex_pcs_.pop_back();
Sebastien Hertz320deb22014-06-11 19:45:05 +0200289 if (!isi->interpreter_entry_) {
290 instrumentation->MethodEnterEvent(thread, (*isi).this_object_, (*isi).method_, dex_pc);
291 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100292 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800293 }
294 thread->VerifyStack();
Ian Rogers306057f2012-11-26 12:45:53 -0800295}
296
Mingyao Yang99170c62015-07-06 11:10:37 -0700297void Instrumentation::InstrumentThreadStack(Thread* thread) {
298 instrumentation_stubs_installed_ = true;
299 InstrumentationInstallStack(thread, this);
300}
301
Ian Rogers62d6c772013-02-27 08:32:07 -0800302// Removes the instrumentation exit pc as the return PC for every quick frame.
303static void InstrumentationRestoreStack(Thread* thread, void* arg)
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +0000304 REQUIRES(Locks::mutator_lock_) {
305 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
306
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200307 struct RestoreStackVisitor FINAL : public StackVisitor {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800308 RestoreStackVisitor(Thread* thread_in, uintptr_t instrumentation_exit_pc,
Ian Rogers62d6c772013-02-27 08:32:07 -0800309 Instrumentation* instrumentation)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100310 : StackVisitor(thread_in, nullptr, kInstrumentationStackWalk),
311 thread_(thread_in),
Ian Rogers62d6c772013-02-27 08:32:07 -0800312 instrumentation_exit_pc_(instrumentation_exit_pc),
313 instrumentation_(instrumentation),
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800314 instrumentation_stack_(thread_in->GetInstrumentationStack()),
Ian Rogers62d6c772013-02-27 08:32:07 -0800315 frames_removed_(0) {}
Ian Rogers306057f2012-11-26 12:45:53 -0800316
Mathieu Chartier90443472015-07-16 20:32:27 -0700317 bool VisitFrame() OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800318 if (instrumentation_stack_->size() == 0) {
jeffhao725a9572012-11-13 18:20:12 -0800319 return false; // Stop.
320 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700321 ArtMethod* m = GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700322 if (GetCurrentQuickFrame() == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800323 if (kVerboseInstrumentation) {
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200324 LOG(INFO) << " Ignoring a shadow frame. Frame " << GetFrameId()
325 << " Method=" << PrettyMethod(m);
Ian Rogers62d6c772013-02-27 08:32:07 -0800326 }
327 return true; // Ignore shadow frames.
328 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700329 if (m == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800330 if (kVerboseInstrumentation) {
331 LOG(INFO) << " Skipping upcall. Frame " << GetFrameId();
332 }
Ian Rogers306057f2012-11-26 12:45:53 -0800333 return true; // Ignore upcalls.
334 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800335 bool removed_stub = false;
336 // TODO: make this search more efficient?
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100337 const size_t frameId = GetFrameId();
338 for (const InstrumentationStackFrame& instrumentation_frame : *instrumentation_stack_) {
339 if (instrumentation_frame.frame_id_ == frameId) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800340 if (kVerboseInstrumentation) {
341 LOG(INFO) << " Removing exit stub in " << DescribeLocation();
342 }
Jeff Hao9a916d32013-06-27 18:45:37 -0700343 if (instrumentation_frame.interpreter_entry_) {
344 CHECK(m == Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs));
345 } else {
346 CHECK(m == instrumentation_frame.method_) << PrettyMethod(m);
347 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800348 SetReturnPc(instrumentation_frame.return_pc_);
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100349 if (instrumentation_->ShouldNotifyMethodEnterExitEvents()) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100350 // Create the method exit events. As the methods didn't really exit the result is 0.
351 // We only do this if no debugger is attached to prevent from posting events twice.
352 instrumentation_->MethodExitEvent(thread_, instrumentation_frame.this_object_, m,
353 GetDexPc(), JValue());
354 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800355 frames_removed_++;
356 removed_stub = true;
357 break;
358 }
359 }
360 if (!removed_stub) {
361 if (kVerboseInstrumentation) {
362 LOG(INFO) << " No exit stub in " << DescribeLocation();
Ian Rogers306057f2012-11-26 12:45:53 -0800363 }
jeffhao725a9572012-11-13 18:20:12 -0800364 }
365 return true; // Continue.
366 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800367 Thread* const thread_;
Ian Rogers306057f2012-11-26 12:45:53 -0800368 const uintptr_t instrumentation_exit_pc_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800369 Instrumentation* const instrumentation_;
370 std::deque<instrumentation::InstrumentationStackFrame>* const instrumentation_stack_;
371 size_t frames_removed_;
jeffhao725a9572012-11-13 18:20:12 -0800372 };
Ian Rogers62d6c772013-02-27 08:32:07 -0800373 if (kVerboseInstrumentation) {
374 std::string thread_name;
375 thread->GetThreadName(thread_name);
376 LOG(INFO) << "Removing exit stubs in " << thread_name;
377 }
378 std::deque<instrumentation::InstrumentationStackFrame>* stack = thread->GetInstrumentationStack();
379 if (stack->size() > 0) {
380 Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700381 uintptr_t instrumentation_exit_pc =
382 reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc());
Ian Rogers62d6c772013-02-27 08:32:07 -0800383 RestoreStackVisitor visitor(thread, instrumentation_exit_pc, instrumentation);
384 visitor.WalkStack(true);
385 CHECK_EQ(visitor.frames_removed_, stack->size());
386 while (stack->size() > 0) {
387 stack->pop_front();
388 }
jeffhao725a9572012-11-13 18:20:12 -0800389 }
390}
391
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200392static bool HasEvent(Instrumentation::InstrumentationEvent expected, uint32_t events) {
393 return (events & expected) != 0;
394}
395
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000396static void PotentiallyAddListenerTo(Instrumentation::InstrumentationEvent event,
397 uint32_t events,
398 std::list<InstrumentationListener*>& list,
399 InstrumentationListener* listener,
400 bool* has_listener)
401 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_) {
402 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
403 if (!HasEvent(event, events)) {
404 return;
405 }
406 // If there is a free slot in the list, we insert the listener in that slot.
407 // Otherwise we add it to the end of the list.
408 auto it = std::find(list.begin(), list.end(), nullptr);
409 if (it != list.end()) {
410 *it = listener;
411 } else {
412 list.push_back(listener);
413 }
414 *has_listener = true;
415}
416
Ian Rogers62d6c772013-02-27 08:32:07 -0800417void Instrumentation::AddListener(InstrumentationListener* listener, uint32_t events) {
418 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000419 PotentiallyAddListenerTo(kMethodEntered,
420 events,
421 method_entry_listeners_,
422 listener,
423 &have_method_entry_listeners_);
424 PotentiallyAddListenerTo(kMethodExited,
425 events,
426 method_exit_listeners_,
427 listener,
428 &have_method_exit_listeners_);
429 PotentiallyAddListenerTo(kMethodUnwind,
430 events,
431 method_unwind_listeners_,
432 listener,
433 &have_method_unwind_listeners_);
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000434 PotentiallyAddListenerTo(kBranch,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000435 events,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000436 branch_listeners_,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000437 listener,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000438 &have_branch_listeners_);
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000439 PotentiallyAddListenerTo(kInvokeVirtualOrInterface,
440 events,
441 invoke_virtual_or_interface_listeners_,
442 listener,
443 &have_invoke_virtual_or_interface_listeners_);
444 PotentiallyAddListenerTo(kDexPcMoved,
445 events,
446 dex_pc_listeners_,
447 listener,
448 &have_dex_pc_listeners_);
449 PotentiallyAddListenerTo(kFieldRead,
450 events,
451 field_read_listeners_,
452 listener,
453 &have_field_read_listeners_);
454 PotentiallyAddListenerTo(kFieldWritten,
455 events,
456 field_write_listeners_,
457 listener,
458 &have_field_write_listeners_);
459 PotentiallyAddListenerTo(kExceptionCaught,
460 events,
461 exception_caught_listeners_,
462 listener,
463 &have_exception_caught_listeners_);
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200464 UpdateInterpreterHandlerTable();
jeffhao725a9572012-11-13 18:20:12 -0800465}
466
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000467static void PotentiallyRemoveListenerFrom(Instrumentation::InstrumentationEvent event,
468 uint32_t events,
469 std::list<InstrumentationListener*>& list,
470 InstrumentationListener* listener,
471 bool* has_listener)
472 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_) {
473 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
474 if (!HasEvent(event, events)) {
475 return;
476 }
477 auto it = std::find(list.begin(), list.end(), listener);
478 if (it != list.end()) {
479 // Just update the entry, do not remove from the list. Removing entries in the list
480 // is unsafe when mutators are iterating over it.
481 *it = nullptr;
482 }
483
484 // Check if the list contains any non-null listener, and update 'has_listener'.
485 for (InstrumentationListener* l : list) {
486 if (l != nullptr) {
487 *has_listener = true;
488 return;
489 }
490 }
491 *has_listener = false;
492}
493
Ian Rogers62d6c772013-02-27 08:32:07 -0800494void Instrumentation::RemoveListener(InstrumentationListener* listener, uint32_t events) {
495 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000496 PotentiallyRemoveListenerFrom(kMethodEntered,
497 events,
498 method_entry_listeners_,
499 listener,
500 &have_method_entry_listeners_);
501 PotentiallyRemoveListenerFrom(kMethodExited,
502 events,
503 method_exit_listeners_,
504 listener,
505 &have_method_exit_listeners_);
506 PotentiallyRemoveListenerFrom(kMethodUnwind,
507 events,
508 method_unwind_listeners_,
509 listener,
510 &have_method_unwind_listeners_);
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000511 PotentiallyRemoveListenerFrom(kBranch,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000512 events,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000513 branch_listeners_,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000514 listener,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000515 &have_branch_listeners_);
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000516 PotentiallyRemoveListenerFrom(kInvokeVirtualOrInterface,
517 events,
518 invoke_virtual_or_interface_listeners_,
519 listener,
520 &have_invoke_virtual_or_interface_listeners_);
521 PotentiallyRemoveListenerFrom(kDexPcMoved,
522 events,
523 dex_pc_listeners_,
524 listener,
525 &have_dex_pc_listeners_);
526 PotentiallyRemoveListenerFrom(kFieldRead,
527 events,
528 field_read_listeners_,
529 listener,
530 &have_field_read_listeners_);
531 PotentiallyRemoveListenerFrom(kFieldWritten,
532 events,
533 field_write_listeners_,
534 listener,
535 &have_field_write_listeners_);
536 PotentiallyRemoveListenerFrom(kExceptionCaught,
537 events,
538 exception_caught_listeners_,
539 listener,
540 &have_exception_caught_listeners_);
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200541 UpdateInterpreterHandlerTable();
jeffhao725a9572012-11-13 18:20:12 -0800542}
543
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200544Instrumentation::InstrumentationLevel Instrumentation::GetCurrentInstrumentationLevel() const {
Ian Rogers62d6c772013-02-27 08:32:07 -0800545 if (interpreter_stubs_installed_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200546 return InstrumentationLevel::kInstrumentWithInterpreter;
Ian Rogers62d6c772013-02-27 08:32:07 -0800547 } else if (entry_exit_stubs_installed_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200548 return InstrumentationLevel::kInstrumentWithInstrumentationStubs;
Ian Rogers62d6c772013-02-27 08:32:07 -0800549 } else {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200550 return InstrumentationLevel::kInstrumentNothing;
Ian Rogers62d6c772013-02-27 08:32:07 -0800551 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200552}
553
554void Instrumentation::ConfigureStubs(const char* key, InstrumentationLevel desired_level) {
555 // Store the instrumentation level for this key or remove it.
556 if (desired_level == InstrumentationLevel::kInstrumentNothing) {
557 // The client no longer needs instrumentation.
558 requested_instrumentation_levels_.erase(key);
559 } else {
560 // The client needs instrumentation.
561 requested_instrumentation_levels_.Overwrite(key, desired_level);
562 }
563
564 // Look for the highest required instrumentation level.
565 InstrumentationLevel requested_level = InstrumentationLevel::kInstrumentNothing;
566 for (const auto& v : requested_instrumentation_levels_) {
567 requested_level = std::max(requested_level, v.second);
568 }
569
570 interpret_only_ = (requested_level == InstrumentationLevel::kInstrumentWithInterpreter) ||
571 forced_interpret_only_;
572
573 InstrumentationLevel current_level = GetCurrentInstrumentationLevel();
574 if (requested_level == current_level) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800575 // We're already set.
576 return;
577 }
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100578 Thread* const self = Thread::Current();
Ian Rogers62d6c772013-02-27 08:32:07 -0800579 Runtime* runtime = Runtime::Current();
Sebastien Hertza8a697f2015-01-15 12:28:47 +0100580 Locks::mutator_lock_->AssertExclusiveHeld(self);
Ian Rogers62d6c772013-02-27 08:32:07 -0800581 Locks::thread_list_lock_->AssertNotHeld(self);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200582 if (requested_level > InstrumentationLevel::kInstrumentNothing) {
583 if (requested_level == InstrumentationLevel::kInstrumentWithInterpreter) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800584 interpreter_stubs_installed_ = true;
Ian Rogers62d6c772013-02-27 08:32:07 -0800585 entry_exit_stubs_installed_ = true;
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200586 } else {
587 CHECK_EQ(requested_level, InstrumentationLevel::kInstrumentWithInstrumentationStubs);
588 entry_exit_stubs_installed_ = true;
589 interpreter_stubs_installed_ = false;
Ian Rogers62d6c772013-02-27 08:32:07 -0800590 }
Mathieu Chartiere0671ce2015-07-28 17:23:28 -0700591 InstallStubsClassVisitor visitor(this);
592 runtime->GetClassLinker()->VisitClasses(&visitor);
Ian Rogers62d6c772013-02-27 08:32:07 -0800593 instrumentation_stubs_installed_ = true;
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100594 MutexLock mu(self, *Locks::thread_list_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800595 runtime->GetThreadList()->ForEach(InstrumentationInstallStack, this);
596 } else {
597 interpreter_stubs_installed_ = false;
598 entry_exit_stubs_installed_ = false;
Mathieu Chartiere0671ce2015-07-28 17:23:28 -0700599 InstallStubsClassVisitor visitor(this);
600 runtime->GetClassLinker()->VisitClasses(&visitor);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100601 // Restore stack only if there is no method currently deoptimized.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700602 bool empty;
603 {
604 ReaderMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700605 empty = IsDeoptimizedMethodsEmpty(); // Avoid lock violation.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700606 }
607 if (empty) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100608 MutexLock mu(self, *Locks::thread_list_lock_);
609 Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this);
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +0000610 // Only do this after restoring, as walking the stack when restoring will see
611 // the instrumentation exit pc.
612 instrumentation_stubs_installed_ = false;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100613 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800614 }
jeffhao725a9572012-11-13 18:20:12 -0800615}
616
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200617static void ResetQuickAllocEntryPointsForThread(Thread* thread, void* arg ATTRIBUTE_UNUSED) {
Ian Rogersfa824272013-11-05 16:12:57 -0800618 thread->ResetQuickAllocEntryPointsForThread();
619}
620
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700621void Instrumentation::SetEntrypointsInstrumented(bool instrumented) {
622 Thread* self = Thread::Current();
Mathieu Chartier661974a2014-01-09 11:23:53 -0800623 Runtime* runtime = Runtime::Current();
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700624 Locks::mutator_lock_->AssertNotHeld(self);
625 Locks::instrument_entrypoints_lock_->AssertHeld(self);
626 if (runtime->IsStarted()) {
Mathieu Chartier4f55e222015-09-04 13:26:21 -0700627 ScopedSuspendAll ssa(__FUNCTION__);
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700628 MutexLock mu(self, *Locks::runtime_shutdown_lock_);
Mathieu Chartier661974a2014-01-09 11:23:53 -0800629 SetQuickAllocEntryPointsInstrumented(instrumented);
630 ResetQuickAllocEntryPoints();
Mathieu Chartier4f55e222015-09-04 13:26:21 -0700631 } else {
632 MutexLock mu(self, *Locks::runtime_shutdown_lock_);
633 SetQuickAllocEntryPointsInstrumented(instrumented);
634 ResetQuickAllocEntryPoints();
Mathieu Chartier661974a2014-01-09 11:23:53 -0800635 }
636}
637
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700638void Instrumentation::InstrumentQuickAllocEntryPoints() {
639 MutexLock mu(Thread::Current(), *Locks::instrument_entrypoints_lock_);
640 InstrumentQuickAllocEntryPointsLocked();
Ian Rogersfa824272013-11-05 16:12:57 -0800641}
642
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700643void Instrumentation::UninstrumentQuickAllocEntryPoints() {
644 MutexLock mu(Thread::Current(), *Locks::instrument_entrypoints_lock_);
645 UninstrumentQuickAllocEntryPointsLocked();
646}
647
648void Instrumentation::InstrumentQuickAllocEntryPointsLocked() {
649 Locks::instrument_entrypoints_lock_->AssertHeld(Thread::Current());
650 if (quick_alloc_entry_points_instrumentation_counter_ == 0) {
651 SetEntrypointsInstrumented(true);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800652 }
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700653 ++quick_alloc_entry_points_instrumentation_counter_;
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700654}
655
656void Instrumentation::UninstrumentQuickAllocEntryPointsLocked() {
657 Locks::instrument_entrypoints_lock_->AssertHeld(Thread::Current());
658 CHECK_GT(quick_alloc_entry_points_instrumentation_counter_, 0U);
659 --quick_alloc_entry_points_instrumentation_counter_;
660 if (quick_alloc_entry_points_instrumentation_counter_ == 0) {
661 SetEntrypointsInstrumented(false);
662 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800663}
664
665void Instrumentation::ResetQuickAllocEntryPoints() {
666 Runtime* runtime = Runtime::Current();
667 if (runtime->IsStarted()) {
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800668 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700669 runtime->GetThreadList()->ForEach(ResetQuickAllocEntryPointsForThread, nullptr);
Ian Rogersfa824272013-11-05 16:12:57 -0800670 }
671}
672
Mathieu Chartiere401d142015-04-22 13:56:20 -0700673void Instrumentation::UpdateMethodsCode(ArtMethod* method, const void* quick_code) {
Sebastien Hertza8a697f2015-01-15 12:28:47 +0100674 DCHECK(method->GetDeclaringClass()->IsResolved());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800675 const void* new_quick_code;
Ian Rogers62d6c772013-02-27 08:32:07 -0800676 if (LIKELY(!instrumentation_stubs_installed_)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800677 new_quick_code = quick_code;
Jeff Hao65d15d92013-07-16 16:39:33 -0700678 } else {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100679 if ((interpreter_stubs_installed_ || IsDeoptimized(method)) && !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800680 new_quick_code = GetQuickToInterpreterBridge();
Jeff Hao65d15d92013-07-16 16:39:33 -0700681 } else {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700682 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700683 if (class_linker->IsQuickResolutionStub(quick_code) ||
684 class_linker->IsQuickToInterpreterBridge(quick_code)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700685 new_quick_code = quick_code;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700686 } else if (entry_exit_stubs_installed_) {
687 new_quick_code = GetQuickInstrumentationEntryPoint();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700688 } else {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700689 new_quick_code = quick_code;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700690 }
Jeff Hao65d15d92013-07-16 16:39:33 -0700691 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800692 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800693 UpdateEntrypoints(method, new_quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100694}
695
Mathieu Chartiere401d142015-04-22 13:56:20 -0700696bool Instrumentation::AddDeoptimizedMethod(ArtMethod* method) {
697 if (IsDeoptimizedMethod(method)) {
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700698 // Already in the map. Return.
699 return false;
700 }
701 // Not found. Add it.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700702 deoptimized_methods_.insert(method);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700703 return true;
704}
705
Mathieu Chartiere401d142015-04-22 13:56:20 -0700706bool Instrumentation::IsDeoptimizedMethod(ArtMethod* method) {
707 return deoptimized_methods_.find(method) != deoptimized_methods_.end();
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700708}
709
Mathieu Chartiere401d142015-04-22 13:56:20 -0700710ArtMethod* Instrumentation::BeginDeoptimizedMethod() {
711 if (deoptimized_methods_.empty()) {
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700712 // Empty.
713 return nullptr;
714 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700715 return *deoptimized_methods_.begin();
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700716}
717
Mathieu Chartiere401d142015-04-22 13:56:20 -0700718bool Instrumentation::RemoveDeoptimizedMethod(ArtMethod* method) {
719 auto it = deoptimized_methods_.find(method);
720 if (it == deoptimized_methods_.end()) {
721 return false;
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700722 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700723 deoptimized_methods_.erase(it);
724 return true;
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700725}
726
727bool Instrumentation::IsDeoptimizedMethodsEmpty() const {
728 return deoptimized_methods_.empty();
729}
730
Mathieu Chartiere401d142015-04-22 13:56:20 -0700731void Instrumentation::Deoptimize(ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100732 CHECK(!method->IsNative());
733 CHECK(!method->IsProxyMethod());
Alex Light9139e002015-10-09 15:59:48 -0700734 CHECK(method->IsInvokable());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100735
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700736 Thread* self = Thread::Current();
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700737 {
738 WriterMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700739 bool has_not_been_deoptimized = AddDeoptimizedMethod(method);
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200740 CHECK(has_not_been_deoptimized) << "Method " << PrettyMethod(method)
741 << " is already deoptimized";
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700742 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100743 if (!interpreter_stubs_installed_) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800744 UpdateEntrypoints(method, GetQuickInstrumentationEntryPoint());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100745
746 // Install instrumentation exit stub and instrumentation frames. We may already have installed
747 // these previously so it will only cover the newly created frames.
748 instrumentation_stubs_installed_ = true;
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700749 MutexLock mu(self, *Locks::thread_list_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100750 Runtime::Current()->GetThreadList()->ForEach(InstrumentationInstallStack, this);
751 }
752}
753
Mathieu Chartiere401d142015-04-22 13:56:20 -0700754void Instrumentation::Undeoptimize(ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100755 CHECK(!method->IsNative());
756 CHECK(!method->IsProxyMethod());
Alex Light9139e002015-10-09 15:59:48 -0700757 CHECK(method->IsInvokable());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100758
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700759 Thread* self = Thread::Current();
760 bool empty;
761 {
762 WriterMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700763 bool found_and_erased = RemoveDeoptimizedMethod(method);
764 CHECK(found_and_erased) << "Method " << PrettyMethod(method)
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700765 << " is not deoptimized";
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700766 empty = IsDeoptimizedMethodsEmpty();
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700767 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100768
769 // Restore code and possibly stack only if we did not deoptimize everything.
770 if (!interpreter_stubs_installed_) {
771 // Restore its code or resolution trampoline.
772 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800773 if (method->IsStatic() && !method->IsConstructor() &&
774 !method->GetDeclaringClass()->IsInitialized()) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800775 UpdateEntrypoints(method, GetQuickResolutionStub());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100776 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800777 const void* quick_code = class_linker->GetQuickOatCodeFor(method);
Elliott Hughes956af0f2014-12-11 14:34:28 -0800778 UpdateEntrypoints(method, quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100779 }
780
781 // If there is no deoptimized method left, we can restore the stack of each thread.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700782 if (empty) {
783 MutexLock mu(self, *Locks::thread_list_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100784 Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this);
785 instrumentation_stubs_installed_ = false;
786 }
787 }
788}
789
Mathieu Chartiere401d142015-04-22 13:56:20 -0700790bool Instrumentation::IsDeoptimized(ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100791 DCHECK(method != nullptr);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700792 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700793 return IsDeoptimizedMethod(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100794}
795
796void Instrumentation::EnableDeoptimization() {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700797 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700798 CHECK(IsDeoptimizedMethodsEmpty());
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100799 CHECK_EQ(deoptimization_enabled_, false);
800 deoptimization_enabled_ = true;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100801}
802
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200803void Instrumentation::DisableDeoptimization(const char* key) {
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100804 CHECK_EQ(deoptimization_enabled_, true);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100805 // If we deoptimized everything, undo it.
806 if (interpreter_stubs_installed_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200807 UndeoptimizeEverything(key);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100808 }
809 // Undeoptimized selected methods.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700810 while (true) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700811 ArtMethod* method;
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700812 {
813 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700814 if (IsDeoptimizedMethodsEmpty()) {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700815 break;
816 }
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700817 method = BeginDeoptimizedMethod();
818 CHECK(method != nullptr);
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700819 }
820 Undeoptimize(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100821 }
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100822 deoptimization_enabled_ = false;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100823}
824
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100825// Indicates if instrumentation should notify method enter/exit events to the listeners.
826bool Instrumentation::ShouldNotifyMethodEnterExitEvents() const {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200827 if (!HasMethodEntryListeners() && !HasMethodExitListeners()) {
828 return false;
829 }
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100830 return !deoptimization_enabled_ && !interpreter_stubs_installed_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100831}
832
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200833void Instrumentation::DeoptimizeEverything(const char* key) {
834 CHECK(deoptimization_enabled_);
835 ConfigureStubs(key, InstrumentationLevel::kInstrumentWithInterpreter);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100836}
837
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200838void Instrumentation::UndeoptimizeEverything(const char* key) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100839 CHECK(interpreter_stubs_installed_);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200840 CHECK(deoptimization_enabled_);
841 ConfigureStubs(key, InstrumentationLevel::kInstrumentNothing);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100842}
843
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200844void Instrumentation::EnableMethodTracing(const char* key, bool needs_interpreter) {
845 InstrumentationLevel level;
846 if (needs_interpreter) {
847 level = InstrumentationLevel::kInstrumentWithInterpreter;
848 } else {
849 level = InstrumentationLevel::kInstrumentWithInstrumentationStubs;
850 }
851 ConfigureStubs(key, level);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100852}
853
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200854void Instrumentation::DisableMethodTracing(const char* key) {
855 ConfigureStubs(key, InstrumentationLevel::kInstrumentNothing);
jeffhao725a9572012-11-13 18:20:12 -0800856}
857
Mathieu Chartiere401d142015-04-22 13:56:20 -0700858const void* Instrumentation::GetQuickCodeFor(ArtMethod* method, size_t pointer_size) const {
Ian Rogers62d6c772013-02-27 08:32:07 -0800859 Runtime* runtime = Runtime::Current();
860 if (LIKELY(!instrumentation_stubs_installed_)) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800861 const void* code = method->GetEntryPointFromQuickCompiledCodePtrSize(pointer_size);
Vladimir Marko8a630572014-04-09 18:45:35 +0100862 DCHECK(code != nullptr);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700863 ClassLinker* class_linker = runtime->GetClassLinker();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700864 if (LIKELY(!class_linker->IsQuickResolutionStub(code) &&
865 !class_linker->IsQuickToInterpreterBridge(code)) &&
866 !class_linker->IsQuickResolutionStub(code) &&
867 !class_linker->IsQuickToInterpreterBridge(code)) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800868 return code;
869 }
870 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800871 return runtime->GetClassLinker()->GetQuickOatCodeFor(method);
jeffhao725a9572012-11-13 18:20:12 -0800872}
873
Ian Rogers62d6c772013-02-27 08:32:07 -0800874void Instrumentation::MethodEnterEventImpl(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700875 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800876 uint32_t dex_pc) const {
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000877 if (HasMethodEntryListeners()) {
878 for (InstrumentationListener* listener : method_entry_listeners_) {
879 if (listener != nullptr) {
880 listener->MethodEntered(thread, this_object, method, dex_pc);
881 }
882 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800883 }
884}
885
886void Instrumentation::MethodExitEventImpl(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700887 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800888 uint32_t dex_pc, const JValue& return_value) const {
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000889 if (HasMethodExitListeners()) {
890 for (InstrumentationListener* listener : method_exit_listeners_) {
891 if (listener != nullptr) {
892 listener->MethodExited(thread, this_object, method, dex_pc, return_value);
893 }
894 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800895 }
896}
897
898void Instrumentation::MethodUnwindEvent(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700899 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800900 uint32_t dex_pc) const {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200901 if (HasMethodUnwindListeners()) {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700902 for (InstrumentationListener* listener : method_unwind_listeners_) {
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000903 if (listener != nullptr) {
904 listener->MethodUnwind(thread, this_object, method, dex_pc);
905 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800906 }
907 }
908}
909
910void Instrumentation::DexPcMovedEventImpl(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700911 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800912 uint32_t dex_pc) const {
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000913 for (InstrumentationListener* listener : dex_pc_listeners_) {
914 if (listener != nullptr) {
915 listener->DexPcMoved(thread, this_object, method, dex_pc);
916 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800917 }
918}
919
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000920void Instrumentation::BranchImpl(Thread* thread,
921 ArtMethod* method,
922 uint32_t dex_pc,
923 int32_t offset) const {
924 for (InstrumentationListener* listener : branch_listeners_) {
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000925 if (listener != nullptr) {
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000926 listener->Branch(thread, method, dex_pc, offset);
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000927 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800928 }
929}
930
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100931void Instrumentation::InvokeVirtualOrInterfaceImpl(Thread* thread,
932 mirror::Object* this_object,
933 ArtMethod* caller,
934 uint32_t dex_pc,
935 ArtMethod* callee) const {
Roland Levillain91d65e02016-01-19 15:59:16 +0000936 // We cannot have thread suspension since that would cause the this_object parameter to
Mathieu Chartier3fdb3fe2016-01-14 10:24:28 -0800937 // potentially become a dangling pointer. An alternative could be to put it in a handle instead.
938 ScopedAssertNoThreadSuspension ants(thread, __FUNCTION__);
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100939 for (InstrumentationListener* listener : invoke_virtual_or_interface_listeners_) {
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000940 if (listener != nullptr) {
941 listener->InvokeVirtualOrInterface(thread, this_object, caller, dex_pc, callee);
942 }
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100943 }
944}
945
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200946void Instrumentation::FieldReadEventImpl(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700947 ArtMethod* method, uint32_t dex_pc,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700948 ArtField* field) const {
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000949 for (InstrumentationListener* listener : field_read_listeners_) {
950 if (listener != nullptr) {
951 listener->FieldRead(thread, this_object, method, dex_pc, field);
952 }
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200953 }
954}
955
956void Instrumentation::FieldWriteEventImpl(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700957 ArtMethod* method, uint32_t dex_pc,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700958 ArtField* field, const JValue& field_value) const {
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000959 for (InstrumentationListener* listener : field_write_listeners_) {
960 if (listener != nullptr) {
961 listener->FieldWritten(thread, this_object, method, dex_pc, field, field_value);
962 }
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200963 }
964}
965
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000966void Instrumentation::ExceptionCaughtEvent(Thread* thread,
Sebastien Hertz947ff082013-09-17 14:10:13 +0200967 mirror::Throwable* exception_object) const {
Sebastien Hertz9f102032014-05-23 08:59:42 +0200968 if (HasExceptionCaughtListeners()) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000969 DCHECK_EQ(thread->GetException(), exception_object);
Jeff Haoc0bd4da2013-04-11 15:52:28 -0700970 thread->ClearException();
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000971 for (InstrumentationListener* listener : exception_caught_listeners_) {
972 if (listener != nullptr) {
973 listener->ExceptionCaught(thread, exception_object);
974 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800975 }
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000976 thread->SetException(exception_object);
Ian Rogers62d6c772013-02-27 08:32:07 -0800977 }
978}
979
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +0000980// Computes a frame ID by ignoring inlined frames.
981size_t Instrumentation::ComputeFrameId(Thread* self,
982 size_t frame_depth,
983 size_t inlined_frames_before_frame) {
984 CHECK_GE(frame_depth, inlined_frames_before_frame);
985 size_t no_inline_depth = frame_depth - inlined_frames_before_frame;
986 return StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk) - no_inline_depth;
987}
988
Ian Rogers62d6c772013-02-27 08:32:07 -0800989static void CheckStackDepth(Thread* self, const InstrumentationStackFrame& instrumentation_frame,
990 int delta)
Mathieu Chartier90443472015-07-16 20:32:27 -0700991 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100992 size_t frame_id = StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk) + delta;
Ian Rogers62d6c772013-02-27 08:32:07 -0800993 if (frame_id != instrumentation_frame.frame_id_) {
994 LOG(ERROR) << "Expected frame_id=" << frame_id << " but found "
995 << instrumentation_frame.frame_id_;
996 StackVisitor::DescribeStack(self);
997 CHECK_EQ(frame_id, instrumentation_frame.frame_id_);
998 }
999}
1000
1001void Instrumentation::PushInstrumentationStackFrame(Thread* self, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001002 ArtMethod* method,
Jeff Hao9a916d32013-06-27 18:45:37 -07001003 uintptr_t lr, bool interpreter_entry) {
Ian Rogers62d6c772013-02-27 08:32:07 -08001004 // We have a callee-save frame meaning this value is guaranteed to never be 0.
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +01001005 size_t frame_id = StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk);
Ian Rogers62d6c772013-02-27 08:32:07 -08001006 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
1007 if (kVerboseInstrumentation) {
Brian Carlstrom2d888622013-07-18 17:02:00 -07001008 LOG(INFO) << "Entering " << PrettyMethod(method) << " from PC " << reinterpret_cast<void*>(lr);
Ian Rogers62d6c772013-02-27 08:32:07 -08001009 }
1010 instrumentation::InstrumentationStackFrame instrumentation_frame(this_object, method, lr,
Jeff Hao9a916d32013-06-27 18:45:37 -07001011 frame_id, interpreter_entry);
Ian Rogers62d6c772013-02-27 08:32:07 -08001012 stack->push_front(instrumentation_frame);
1013
Sebastien Hertz320deb22014-06-11 19:45:05 +02001014 if (!interpreter_entry) {
1015 MethodEnterEvent(self, this_object, method, 0);
1016 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001017}
1018
Andreas Gamped58342c2014-06-05 14:18:08 -07001019TwoWordReturn Instrumentation::PopInstrumentationStackFrame(Thread* self, uintptr_t* return_pc,
1020 uint64_t gpr_result,
1021 uint64_t fpr_result) {
Ian Rogers62d6c772013-02-27 08:32:07 -08001022 // Do the pop.
1023 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
1024 CHECK_GT(stack->size(), 0U);
1025 InstrumentationStackFrame instrumentation_frame = stack->front();
1026 stack->pop_front();
1027
1028 // Set return PC and check the sanity of the stack.
1029 *return_pc = instrumentation_frame.return_pc_;
1030 CheckStackDepth(self, instrumentation_frame, 0);
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001031 self->VerifyStack();
Ian Rogers62d6c772013-02-27 08:32:07 -08001032
Mathieu Chartiere401d142015-04-22 13:56:20 -07001033 ArtMethod* method = instrumentation_frame.method_;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001034 uint32_t length;
Mingyao Yang99170c62015-07-06 11:10:37 -07001035 const size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
1036 char return_shorty = method->GetInterfaceMethodIfProxy(pointer_size)->GetShorty(&length)[0];
Ian Rogers62d6c772013-02-27 08:32:07 -08001037 JValue return_value;
1038 if (return_shorty == 'V') {
1039 return_value.SetJ(0);
1040 } else if (return_shorty == 'F' || return_shorty == 'D') {
1041 return_value.SetJ(fpr_result);
1042 } else {
1043 return_value.SetJ(gpr_result);
1044 }
1045 // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to
1046 // return_pc.
1047 uint32_t dex_pc = DexFile::kDexNoIndex;
1048 mirror::Object* this_object = instrumentation_frame.this_object_;
Sebastien Hertz320deb22014-06-11 19:45:05 +02001049 if (!instrumentation_frame.interpreter_entry_) {
1050 MethodExitEvent(self, this_object, instrumentation_frame.method_, dex_pc, return_value);
1051 }
jeffhao725a9572012-11-13 18:20:12 -08001052
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01001053 // Deoptimize if the caller needs to continue execution in the interpreter. Do nothing if we get
1054 // back to an upcall.
1055 NthCallerVisitor visitor(self, 1, true);
1056 visitor.WalkStack(true);
Sebastien Hertz270a0e12015-01-16 19:49:09 +01001057 bool deoptimize = (visitor.caller != nullptr) &&
Daniel Mihalyieb076692014-08-22 17:33:31 +02001058 (interpreter_stubs_installed_ || IsDeoptimized(visitor.caller) ||
1059 Dbg::IsForcedInterpreterNeededForUpcall(self, visitor.caller));
Ian Rogers62d6c772013-02-27 08:32:07 -08001060 if (deoptimize) {
1061 if (kVerboseInstrumentation) {
Sebastien Hertz270a0e12015-01-16 19:49:09 +01001062 LOG(INFO) << StringPrintf("Deoptimizing %s by returning from %s with result %#" PRIx64 " in ",
1063 PrettyMethod(visitor.caller).c_str(),
1064 PrettyMethod(method).c_str(),
1065 return_value.GetJ()) << *self;
Ian Rogers62d6c772013-02-27 08:32:07 -08001066 }
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01001067 self->PushDeoptimizationContext(return_value,
1068 return_shorty == 'L',
1069 false /* from_code */,
Sebastien Hertz07474662015-08-25 15:12:33 +00001070 nullptr /* no pending exception */);
Andreas Gamped58342c2014-06-05 14:18:08 -07001071 return GetTwoWordSuccessValue(*return_pc,
1072 reinterpret_cast<uintptr_t>(GetQuickDeoptimizationEntryPoint()));
Ian Rogers62d6c772013-02-27 08:32:07 -08001073 } else {
1074 if (kVerboseInstrumentation) {
Brian Carlstrom2d888622013-07-18 17:02:00 -07001075 LOG(INFO) << "Returning from " << PrettyMethod(method)
1076 << " to PC " << reinterpret_cast<void*>(*return_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -08001077 }
Andreas Gamped58342c2014-06-05 14:18:08 -07001078 return GetTwoWordSuccessValue(0, *return_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -08001079 }
jeffhao725a9572012-11-13 18:20:12 -08001080}
1081
Ian Rogers62d6c772013-02-27 08:32:07 -08001082void Instrumentation::PopMethodForUnwind(Thread* self, bool is_deoptimization) const {
1083 // Do the pop.
1084 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
1085 CHECK_GT(stack->size(), 0U);
1086 InstrumentationStackFrame instrumentation_frame = stack->front();
1087 // TODO: bring back CheckStackDepth(self, instrumentation_frame, 2);
1088 stack->pop_front();
1089
Mathieu Chartiere401d142015-04-22 13:56:20 -07001090 ArtMethod* method = instrumentation_frame.method_;
Ian Rogers62d6c772013-02-27 08:32:07 -08001091 if (is_deoptimization) {
1092 if (kVerboseInstrumentation) {
1093 LOG(INFO) << "Popping for deoptimization " << PrettyMethod(method);
1094 }
1095 } else {
1096 if (kVerboseInstrumentation) {
1097 LOG(INFO) << "Popping for unwind " << PrettyMethod(method);
1098 }
1099
1100 // Notify listeners of method unwind.
1101 // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to
1102 // return_pc.
1103 uint32_t dex_pc = DexFile::kDexNoIndex;
1104 MethodUnwindEvent(self, instrumentation_frame.this_object_, method, dex_pc);
1105 }
1106}
1107
1108std::string InstrumentationStackFrame::Dump() const {
1109 std::ostringstream os;
1110 os << "Frame " << frame_id_ << " " << PrettyMethod(method_) << ":"
1111 << reinterpret_cast<void*>(return_pc_) << " this=" << reinterpret_cast<void*>(this_object_);
1112 return os.str();
1113}
1114
1115} // namespace instrumentation
jeffhao725a9572012-11-13 18:20:12 -08001116} // namespace art