blob: 264cd2c78530fa2ab1d2bda048e43ddeb8fdbfcc [file] [log] [blame]
jeffhao725a9572012-11-13 18:20:12 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "instrumentation.h"
18
Ian Rogersc7dd2952014-10-21 23:31:19 -070019#include <sstream>
20
Ian Rogerse63db272014-07-15 15:36:11 -070021#include "arch/context.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070022#include "art_method-inl.h"
Ian Rogersef7d42f2014-01-06 12:55:46 -080023#include "atomic.h"
jeffhao725a9572012-11-13 18:20:12 -080024#include "class_linker.h"
25#include "debugger.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080026#include "dex_file-inl.h"
Ian Rogersc7dd2952014-10-21 23:31:19 -070027#include "entrypoints/quick/quick_entrypoints.h"
Mathieu Chartierd8891782014-03-02 13:28:37 -080028#include "entrypoints/quick/quick_alloc_entrypoints.h"
Ian Rogers6f3dbba2014-10-14 17:41:57 -070029#include "entrypoints/runtime_asm_entrypoints.h"
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070030#include "gc_root-inl.h"
Sebastien Hertz138dbfc2013-12-04 18:15:25 +010031#include "interpreter/interpreter.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080032#include "jit/jit.h"
33#include "jit/jit_code_cache.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080034#include "mirror/class-inl.h"
35#include "mirror/dex_cache.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080036#include "mirror/object_array-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070037#include "mirror/object-inl.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080038#include "nth_caller_visitor.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010039#include "oat_quick_method_header.h"
jeffhao725a9572012-11-13 18:20:12 -080040#include "thread.h"
41#include "thread_list.h"
jeffhao725a9572012-11-13 18:20:12 -080042
43namespace art {
Ian Rogers62d6c772013-02-27 08:32:07 -080044namespace instrumentation {
jeffhao725a9572012-11-13 18:20:12 -080045
Sebastien Hertz0462c4c2015-04-01 16:34:17 +020046constexpr bool kVerboseInstrumentation = false;
Sebastien Hertz5bfd5c92013-11-15 11:36:07 +010047
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +010048// Instrumentation works on non-inlined frames by updating returned PCs
49// of compiled frames.
50static constexpr StackVisitor::StackWalkKind kInstrumentationStackWalk =
51 StackVisitor::StackWalkKind::kSkipInlinedFrames;
52
Mathieu Chartiere0671ce2015-07-28 17:23:28 -070053class InstallStubsClassVisitor : public ClassVisitor {
54 public:
55 explicit InstallStubsClassVisitor(Instrumentation* instrumentation)
56 : instrumentation_(instrumentation) {}
57
58 bool Visit(mirror::Class* klass) OVERRIDE REQUIRES(Locks::mutator_lock_) {
59 instrumentation_->InstallStubsForClass(klass);
60 return true; // we visit all classes.
61 }
62
63 private:
64 Instrumentation* const instrumentation_;
65};
66
Ian Rogers62d6c772013-02-27 08:32:07 -080067
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -070068Instrumentation::Instrumentation()
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),
81 have_backward_branch_listeners_(false),
82 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 {
96 for (size_t i = 0, e = klass->NumDirectMethods(); i < e; i++) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070097 InstallStubsForMethod(klass->GetDirectMethod(i, sizeof(void*)));
Sebastien Hertza8a697f2015-01-15 12:28:47 +010098 }
99 for (size_t i = 0, e = klass->NumVirtualMethods(); i < e; i++) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700100 InstallStubsForMethod(klass->GetVirtualMethod(i, sizeof(void*)));
Sebastien Hertza8a697f2015-01-15 12:28:47 +0100101 }
jeffhao725a9572012-11-13 18:20:12 -0800102 }
jeffhao725a9572012-11-13 18:20:12 -0800103}
104
Mathieu Chartiere401d142015-04-22 13:56:20 -0700105static void UpdateEntrypoints(ArtMethod* method, const void* quick_code)
Mathieu Chartier90443472015-07-16 20:32:27 -0700106 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800107 method->SetEntryPointFromQuickCompiledCode(quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100108}
109
Mathieu Chartiere401d142015-04-22 13:56:20 -0700110void Instrumentation::InstallStubsForMethod(ArtMethod* method) {
Alex Light9139e002015-10-09 15:59:48 -0700111 if (!method->IsInvokable() || method->IsProxyMethod()) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100112 // Do not change stubs for these methods.
113 return;
114 }
Jeff Hao56802772014-08-19 10:17:36 -0700115 // Don't stub Proxy.<init>. Note that the Proxy class itself is not a proxy class.
116 if (method->IsConstructor() &&
117 method->GetDeclaringClass()->DescriptorEquals("Ljava/lang/reflect/Proxy;")) {
Jeff Haodb8a6642014-08-14 17:18:52 -0700118 return;
119 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800120 const void* new_quick_code;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100121 bool uninstall = !entry_exit_stubs_installed_ && !interpreter_stubs_installed_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800122 Runtime* const runtime = Runtime::Current();
123 ClassLinker* const class_linker = runtime->GetClassLinker();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100124 bool is_class_initialized = method->GetDeclaringClass()->IsInitialized();
125 if (uninstall) {
126 if ((forced_interpret_only_ || IsDeoptimized(method)) && !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800127 new_quick_code = GetQuickToInterpreterBridge();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100128 } else if (is_class_initialized || !method->IsStatic() || method->IsConstructor()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800129 new_quick_code = class_linker->GetQuickOatCodeFor(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100130 } else {
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700131 new_quick_code = GetQuickResolutionStub();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100132 }
133 } else { // !uninstall
Sebastien Hertzbae182c2013-12-17 10:42:03 +0100134 if ((interpreter_stubs_installed_ || forced_interpret_only_ || IsDeoptimized(method)) &&
135 !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800136 new_quick_code = GetQuickToInterpreterBridge();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100137 } else {
138 // Do not overwrite resolution trampoline. When the trampoline initializes the method's
139 // class, all its static methods code will be set to the instrumentation entry point.
140 // For more details, see ClassLinker::FixupStaticTrampolines.
141 if (is_class_initialized || !method->IsStatic() || method->IsConstructor()) {
Sebastien Hertz320deb22014-06-11 19:45:05 +0200142 if (entry_exit_stubs_installed_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800143 new_quick_code = GetQuickInstrumentationEntryPoint();
Sebastien Hertz320deb22014-06-11 19:45:05 +0200144 } else {
Sebastien Hertz320deb22014-06-11 19:45:05 +0200145 new_quick_code = class_linker->GetQuickOatCodeFor(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100146 }
147 } else {
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700148 new_quick_code = GetQuickResolutionStub();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100149 }
150 }
151 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800152 UpdateEntrypoints(method, new_quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100153}
154
Ian Rogers62d6c772013-02-27 08:32:07 -0800155// Places the instrumentation exit pc as the return PC for every quick frame. This also allows
156// deoptimization of quick frames to interpreter frames.
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100157// Since we may already have done this previously, we need to push new instrumentation frame before
158// existing instrumentation frames.
Ian Rogers62d6c772013-02-27 08:32:07 -0800159static void InstrumentationInstallStack(Thread* thread, void* arg)
Mathieu Chartier90443472015-07-16 20:32:27 -0700160 SHARED_REQUIRES(Locks::mutator_lock_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200161 struct InstallStackVisitor FINAL : public StackVisitor {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800162 InstallStackVisitor(Thread* thread_in, Context* context, uintptr_t instrumentation_exit_pc)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100163 : StackVisitor(thread_in, context, kInstrumentationStackWalk),
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800164 instrumentation_stack_(thread_in->GetInstrumentationStack()),
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100165 instrumentation_exit_pc_(instrumentation_exit_pc),
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100166 reached_existing_instrumentation_frames_(false), instrumentation_stack_depth_(0),
167 last_return_pc_(0) {
168 }
jeffhao725a9572012-11-13 18:20:12 -0800169
Mathieu Chartier90443472015-07-16 20:32:27 -0700170 bool VisitFrame() OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700171 ArtMethod* m = GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700172 if (m == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800173 if (kVerboseInstrumentation) {
174 LOG(INFO) << " Skipping upcall. Frame " << GetFrameId();
175 }
176 last_return_pc_ = 0;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700177 return true; // Ignore upcalls.
Ian Rogers306057f2012-11-26 12:45:53 -0800178 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700179 if (GetCurrentQuickFrame() == nullptr) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800180 bool interpreter_frame = true;
Sebastien Hertz320deb22014-06-11 19:45:05 +0200181 InstrumentationStackFrame instrumentation_frame(GetThisObject(), m, 0, GetFrameId(),
182 interpreter_frame);
Jeff Haoa15a81b2014-05-27 18:25:47 -0700183 if (kVerboseInstrumentation) {
184 LOG(INFO) << "Pushing shadow frame " << instrumentation_frame.Dump();
185 }
186 shadow_stack_.push_back(instrumentation_frame);
187 return true; // Continue.
188 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800189 uintptr_t return_pc = GetReturnPc();
Sebastien Hertz320deb22014-06-11 19:45:05 +0200190 if (m->IsRuntimeMethod()) {
191 if (return_pc == instrumentation_exit_pc_) {
192 if (kVerboseInstrumentation) {
193 LOG(INFO) << " Handling quick to interpreter transition. Frame " << GetFrameId();
194 }
195 CHECK_LT(instrumentation_stack_depth_, instrumentation_stack_->size());
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200196 const InstrumentationStackFrame& frame =
197 instrumentation_stack_->at(instrumentation_stack_depth_);
Sebastien Hertz320deb22014-06-11 19:45:05 +0200198 CHECK(frame.interpreter_entry_);
199 // This is an interpreter frame so method enter event must have been reported. However we
200 // need to push a DEX pc into the dex_pcs_ list to match size of instrumentation stack.
201 // Since we won't report method entry here, we can safely push any DEX pc.
202 dex_pcs_.push_back(0);
203 last_return_pc_ = frame.return_pc_;
204 ++instrumentation_stack_depth_;
205 return true;
206 } else {
207 if (kVerboseInstrumentation) {
208 LOG(INFO) << " Skipping runtime method. Frame " << GetFrameId();
209 }
210 last_return_pc_ = GetReturnPc();
211 return true; // Ignore unresolved methods since they will be instrumented after resolution.
212 }
213 }
214 if (kVerboseInstrumentation) {
215 LOG(INFO) << " Installing exit stub in " << DescribeLocation();
216 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100217 if (return_pc == instrumentation_exit_pc_) {
218 // We've reached a frame which has already been installed with instrumentation exit stub.
219 // We should have already installed instrumentation on previous frames.
220 reached_existing_instrumentation_frames_ = true;
221
222 CHECK_LT(instrumentation_stack_depth_, instrumentation_stack_->size());
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200223 const InstrumentationStackFrame& frame =
224 instrumentation_stack_->at(instrumentation_stack_depth_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100225 CHECK_EQ(m, frame.method_) << "Expected " << PrettyMethod(m)
226 << ", Found " << PrettyMethod(frame.method_);
227 return_pc = frame.return_pc_;
228 if (kVerboseInstrumentation) {
229 LOG(INFO) << "Ignoring already instrumented " << frame.Dump();
230 }
231 } else {
232 CHECK_NE(return_pc, 0U);
233 CHECK(!reached_existing_instrumentation_frames_);
234 InstrumentationStackFrame instrumentation_frame(GetThisObject(), m, return_pc, GetFrameId(),
235 false);
236 if (kVerboseInstrumentation) {
237 LOG(INFO) << "Pushing frame " << instrumentation_frame.Dump();
238 }
239
Sebastien Hertz320deb22014-06-11 19:45:05 +0200240 // Insert frame at the right position so we do not corrupt the instrumentation stack.
241 // Instrumentation stack frames are in descending frame id order.
242 auto it = instrumentation_stack_->begin();
243 for (auto end = instrumentation_stack_->end(); it != end; ++it) {
244 const InstrumentationStackFrame& current = *it;
245 if (instrumentation_frame.frame_id_ >= current.frame_id_) {
246 break;
247 }
248 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100249 instrumentation_stack_->insert(it, instrumentation_frame);
250 SetReturnPc(instrumentation_exit_pc_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800251 }
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100252 dex_pcs_.push_back((GetCurrentOatQuickMethodHeader() == nullptr)
253 ? DexFile::kDexNoIndex
254 : GetCurrentOatQuickMethodHeader()->ToDexPc(m, last_return_pc_));
Ian Rogers62d6c772013-02-27 08:32:07 -0800255 last_return_pc_ = return_pc;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100256 ++instrumentation_stack_depth_;
Ian Rogers306057f2012-11-26 12:45:53 -0800257 return true; // Continue.
258 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800259 std::deque<InstrumentationStackFrame>* const instrumentation_stack_;
Jeff Haoa15a81b2014-05-27 18:25:47 -0700260 std::vector<InstrumentationStackFrame> shadow_stack_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800261 std::vector<uint32_t> dex_pcs_;
Ian Rogers306057f2012-11-26 12:45:53 -0800262 const uintptr_t instrumentation_exit_pc_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100263 bool reached_existing_instrumentation_frames_;
264 size_t instrumentation_stack_depth_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800265 uintptr_t last_return_pc_;
Ian Rogers306057f2012-11-26 12:45:53 -0800266 };
Ian Rogers62d6c772013-02-27 08:32:07 -0800267 if (kVerboseInstrumentation) {
268 std::string thread_name;
269 thread->GetThreadName(thread_name);
270 LOG(INFO) << "Installing exit stubs in " << thread_name;
Ian Rogers306057f2012-11-26 12:45:53 -0800271 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100272
273 Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg);
Ian Rogers700a4022014-05-19 16:49:03 -0700274 std::unique_ptr<Context> context(Context::Create());
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700275 uintptr_t instrumentation_exit_pc = reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc());
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100276 InstallStackVisitor visitor(thread, context.get(), instrumentation_exit_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800277 visitor.WalkStack(true);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100278 CHECK_EQ(visitor.dex_pcs_.size(), thread->GetInstrumentationStack()->size());
Ian Rogers62d6c772013-02-27 08:32:07 -0800279
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100280 if (instrumentation->ShouldNotifyMethodEnterExitEvents()) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100281 // Create method enter events for all methods currently on the thread's stack. We only do this
282 // if no debugger is attached to prevent from posting events twice.
Jeff Haoa15a81b2014-05-27 18:25:47 -0700283 auto ssi = visitor.shadow_stack_.rbegin();
284 for (auto isi = thread->GetInstrumentationStack()->rbegin(),
285 end = thread->GetInstrumentationStack()->rend(); isi != end; ++isi) {
286 while (ssi != visitor.shadow_stack_.rend() && (*ssi).frame_id_ < (*isi).frame_id_) {
287 instrumentation->MethodEnterEvent(thread, (*ssi).this_object_, (*ssi).method_, 0);
288 ++ssi;
289 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100290 uint32_t dex_pc = visitor.dex_pcs_.back();
291 visitor.dex_pcs_.pop_back();
Sebastien Hertz320deb22014-06-11 19:45:05 +0200292 if (!isi->interpreter_entry_) {
293 instrumentation->MethodEnterEvent(thread, (*isi).this_object_, (*isi).method_, dex_pc);
294 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100295 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800296 }
297 thread->VerifyStack();
Ian Rogers306057f2012-11-26 12:45:53 -0800298}
299
Mingyao Yang99170c62015-07-06 11:10:37 -0700300void Instrumentation::InstrumentThreadStack(Thread* thread) {
301 instrumentation_stubs_installed_ = true;
302 InstrumentationInstallStack(thread, this);
303}
304
Ian Rogers62d6c772013-02-27 08:32:07 -0800305// Removes the instrumentation exit pc as the return PC for every quick frame.
306static void InstrumentationRestoreStack(Thread* thread, void* arg)
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +0000307 REQUIRES(Locks::mutator_lock_) {
308 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
309
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200310 struct RestoreStackVisitor FINAL : public StackVisitor {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800311 RestoreStackVisitor(Thread* thread_in, uintptr_t instrumentation_exit_pc,
Ian Rogers62d6c772013-02-27 08:32:07 -0800312 Instrumentation* instrumentation)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100313 : StackVisitor(thread_in, nullptr, kInstrumentationStackWalk),
314 thread_(thread_in),
Ian Rogers62d6c772013-02-27 08:32:07 -0800315 instrumentation_exit_pc_(instrumentation_exit_pc),
316 instrumentation_(instrumentation),
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800317 instrumentation_stack_(thread_in->GetInstrumentationStack()),
Ian Rogers62d6c772013-02-27 08:32:07 -0800318 frames_removed_(0) {}
Ian Rogers306057f2012-11-26 12:45:53 -0800319
Mathieu Chartier90443472015-07-16 20:32:27 -0700320 bool VisitFrame() OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800321 if (instrumentation_stack_->size() == 0) {
jeffhao725a9572012-11-13 18:20:12 -0800322 return false; // Stop.
323 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700324 ArtMethod* m = GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700325 if (GetCurrentQuickFrame() == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800326 if (kVerboseInstrumentation) {
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200327 LOG(INFO) << " Ignoring a shadow frame. Frame " << GetFrameId()
328 << " Method=" << PrettyMethod(m);
Ian Rogers62d6c772013-02-27 08:32:07 -0800329 }
330 return true; // Ignore shadow frames.
331 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700332 if (m == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800333 if (kVerboseInstrumentation) {
334 LOG(INFO) << " Skipping upcall. Frame " << GetFrameId();
335 }
Ian Rogers306057f2012-11-26 12:45:53 -0800336 return true; // Ignore upcalls.
337 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800338 bool removed_stub = false;
339 // TODO: make this search more efficient?
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100340 const size_t frameId = GetFrameId();
341 for (const InstrumentationStackFrame& instrumentation_frame : *instrumentation_stack_) {
342 if (instrumentation_frame.frame_id_ == frameId) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800343 if (kVerboseInstrumentation) {
344 LOG(INFO) << " Removing exit stub in " << DescribeLocation();
345 }
Jeff Hao9a916d32013-06-27 18:45:37 -0700346 if (instrumentation_frame.interpreter_entry_) {
347 CHECK(m == Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs));
348 } else {
349 CHECK(m == instrumentation_frame.method_) << PrettyMethod(m);
350 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800351 SetReturnPc(instrumentation_frame.return_pc_);
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100352 if (instrumentation_->ShouldNotifyMethodEnterExitEvents()) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100353 // Create the method exit events. As the methods didn't really exit the result is 0.
354 // We only do this if no debugger is attached to prevent from posting events twice.
355 instrumentation_->MethodExitEvent(thread_, instrumentation_frame.this_object_, m,
356 GetDexPc(), JValue());
357 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800358 frames_removed_++;
359 removed_stub = true;
360 break;
361 }
362 }
363 if (!removed_stub) {
364 if (kVerboseInstrumentation) {
365 LOG(INFO) << " No exit stub in " << DescribeLocation();
Ian Rogers306057f2012-11-26 12:45:53 -0800366 }
jeffhao725a9572012-11-13 18:20:12 -0800367 }
368 return true; // Continue.
369 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800370 Thread* const thread_;
Ian Rogers306057f2012-11-26 12:45:53 -0800371 const uintptr_t instrumentation_exit_pc_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800372 Instrumentation* const instrumentation_;
373 std::deque<instrumentation::InstrumentationStackFrame>* const instrumentation_stack_;
374 size_t frames_removed_;
jeffhao725a9572012-11-13 18:20:12 -0800375 };
Ian Rogers62d6c772013-02-27 08:32:07 -0800376 if (kVerboseInstrumentation) {
377 std::string thread_name;
378 thread->GetThreadName(thread_name);
379 LOG(INFO) << "Removing exit stubs in " << thread_name;
380 }
381 std::deque<instrumentation::InstrumentationStackFrame>* stack = thread->GetInstrumentationStack();
382 if (stack->size() > 0) {
383 Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700384 uintptr_t instrumentation_exit_pc =
385 reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc());
Ian Rogers62d6c772013-02-27 08:32:07 -0800386 RestoreStackVisitor visitor(thread, instrumentation_exit_pc, instrumentation);
387 visitor.WalkStack(true);
388 CHECK_EQ(visitor.frames_removed_, stack->size());
389 while (stack->size() > 0) {
390 stack->pop_front();
391 }
jeffhao725a9572012-11-13 18:20:12 -0800392 }
393}
394
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200395static bool HasEvent(Instrumentation::InstrumentationEvent expected, uint32_t events) {
396 return (events & expected) != 0;
397}
398
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000399static void PotentiallyAddListenerTo(Instrumentation::InstrumentationEvent event,
400 uint32_t events,
401 std::list<InstrumentationListener*>& list,
402 InstrumentationListener* listener,
403 bool* has_listener)
404 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_) {
405 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
406 if (!HasEvent(event, events)) {
407 return;
408 }
409 // If there is a free slot in the list, we insert the listener in that slot.
410 // Otherwise we add it to the end of the list.
411 auto it = std::find(list.begin(), list.end(), nullptr);
412 if (it != list.end()) {
413 *it = listener;
414 } else {
415 list.push_back(listener);
416 }
417 *has_listener = true;
418}
419
Ian Rogers62d6c772013-02-27 08:32:07 -0800420void Instrumentation::AddListener(InstrumentationListener* listener, uint32_t events) {
421 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000422 PotentiallyAddListenerTo(kMethodEntered,
423 events,
424 method_entry_listeners_,
425 listener,
426 &have_method_entry_listeners_);
427 PotentiallyAddListenerTo(kMethodExited,
428 events,
429 method_exit_listeners_,
430 listener,
431 &have_method_exit_listeners_);
432 PotentiallyAddListenerTo(kMethodUnwind,
433 events,
434 method_unwind_listeners_,
435 listener,
436 &have_method_unwind_listeners_);
437 PotentiallyAddListenerTo(kBackwardBranch,
438 events,
439 backward_branch_listeners_,
440 listener,
441 &have_backward_branch_listeners_);
442 PotentiallyAddListenerTo(kInvokeVirtualOrInterface,
443 events,
444 invoke_virtual_or_interface_listeners_,
445 listener,
446 &have_invoke_virtual_or_interface_listeners_);
447 PotentiallyAddListenerTo(kDexPcMoved,
448 events,
449 dex_pc_listeners_,
450 listener,
451 &have_dex_pc_listeners_);
452 PotentiallyAddListenerTo(kFieldRead,
453 events,
454 field_read_listeners_,
455 listener,
456 &have_field_read_listeners_);
457 PotentiallyAddListenerTo(kFieldWritten,
458 events,
459 field_write_listeners_,
460 listener,
461 &have_field_write_listeners_);
462 PotentiallyAddListenerTo(kExceptionCaught,
463 events,
464 exception_caught_listeners_,
465 listener,
466 &have_exception_caught_listeners_);
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200467 UpdateInterpreterHandlerTable();
jeffhao725a9572012-11-13 18:20:12 -0800468}
469
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000470static void PotentiallyRemoveListenerFrom(Instrumentation::InstrumentationEvent event,
471 uint32_t events,
472 std::list<InstrumentationListener*>& list,
473 InstrumentationListener* listener,
474 bool* has_listener)
475 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_) {
476 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
477 if (!HasEvent(event, events)) {
478 return;
479 }
480 auto it = std::find(list.begin(), list.end(), listener);
481 if (it != list.end()) {
482 // Just update the entry, do not remove from the list. Removing entries in the list
483 // is unsafe when mutators are iterating over it.
484 *it = nullptr;
485 }
486
487 // Check if the list contains any non-null listener, and update 'has_listener'.
488 for (InstrumentationListener* l : list) {
489 if (l != nullptr) {
490 *has_listener = true;
491 return;
492 }
493 }
494 *has_listener = false;
495}
496
Ian Rogers62d6c772013-02-27 08:32:07 -0800497void Instrumentation::RemoveListener(InstrumentationListener* listener, uint32_t events) {
498 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000499 PotentiallyRemoveListenerFrom(kMethodEntered,
500 events,
501 method_entry_listeners_,
502 listener,
503 &have_method_entry_listeners_);
504 PotentiallyRemoveListenerFrom(kMethodExited,
505 events,
506 method_exit_listeners_,
507 listener,
508 &have_method_exit_listeners_);
509 PotentiallyRemoveListenerFrom(kMethodUnwind,
510 events,
511 method_unwind_listeners_,
512 listener,
513 &have_method_unwind_listeners_);
514 PotentiallyRemoveListenerFrom(kBackwardBranch,
515 events,
516 backward_branch_listeners_,
517 listener,
518 &have_backward_branch_listeners_);
519 PotentiallyRemoveListenerFrom(kInvokeVirtualOrInterface,
520 events,
521 invoke_virtual_or_interface_listeners_,
522 listener,
523 &have_invoke_virtual_or_interface_listeners_);
524 PotentiallyRemoveListenerFrom(kDexPcMoved,
525 events,
526 dex_pc_listeners_,
527 listener,
528 &have_dex_pc_listeners_);
529 PotentiallyRemoveListenerFrom(kFieldRead,
530 events,
531 field_read_listeners_,
532 listener,
533 &have_field_read_listeners_);
534 PotentiallyRemoveListenerFrom(kFieldWritten,
535 events,
536 field_write_listeners_,
537 listener,
538 &have_field_write_listeners_);
539 PotentiallyRemoveListenerFrom(kExceptionCaught,
540 events,
541 exception_caught_listeners_,
542 listener,
543 &have_exception_caught_listeners_);
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200544 UpdateInterpreterHandlerTable();
jeffhao725a9572012-11-13 18:20:12 -0800545}
546
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200547Instrumentation::InstrumentationLevel Instrumentation::GetCurrentInstrumentationLevel() const {
Ian Rogers62d6c772013-02-27 08:32:07 -0800548 if (interpreter_stubs_installed_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200549 return InstrumentationLevel::kInstrumentWithInterpreter;
Ian Rogers62d6c772013-02-27 08:32:07 -0800550 } else if (entry_exit_stubs_installed_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200551 return InstrumentationLevel::kInstrumentWithInstrumentationStubs;
Ian Rogers62d6c772013-02-27 08:32:07 -0800552 } else {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200553 return InstrumentationLevel::kInstrumentNothing;
Ian Rogers62d6c772013-02-27 08:32:07 -0800554 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200555}
556
557void Instrumentation::ConfigureStubs(const char* key, InstrumentationLevel desired_level) {
558 // Store the instrumentation level for this key or remove it.
559 if (desired_level == InstrumentationLevel::kInstrumentNothing) {
560 // The client no longer needs instrumentation.
561 requested_instrumentation_levels_.erase(key);
562 } else {
563 // The client needs instrumentation.
564 requested_instrumentation_levels_.Overwrite(key, desired_level);
565 }
566
567 // Look for the highest required instrumentation level.
568 InstrumentationLevel requested_level = InstrumentationLevel::kInstrumentNothing;
569 for (const auto& v : requested_instrumentation_levels_) {
570 requested_level = std::max(requested_level, v.second);
571 }
572
573 interpret_only_ = (requested_level == InstrumentationLevel::kInstrumentWithInterpreter) ||
574 forced_interpret_only_;
575
576 InstrumentationLevel current_level = GetCurrentInstrumentationLevel();
577 if (requested_level == current_level) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800578 // We're already set.
579 return;
580 }
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100581 Thread* const self = Thread::Current();
Ian Rogers62d6c772013-02-27 08:32:07 -0800582 Runtime* runtime = Runtime::Current();
Sebastien Hertza8a697f2015-01-15 12:28:47 +0100583 Locks::mutator_lock_->AssertExclusiveHeld(self);
Ian Rogers62d6c772013-02-27 08:32:07 -0800584 Locks::thread_list_lock_->AssertNotHeld(self);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200585 if (requested_level > InstrumentationLevel::kInstrumentNothing) {
586 if (requested_level == InstrumentationLevel::kInstrumentWithInterpreter) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800587 interpreter_stubs_installed_ = true;
Ian Rogers62d6c772013-02-27 08:32:07 -0800588 entry_exit_stubs_installed_ = true;
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200589 } else {
590 CHECK_EQ(requested_level, InstrumentationLevel::kInstrumentWithInstrumentationStubs);
591 entry_exit_stubs_installed_ = true;
592 interpreter_stubs_installed_ = false;
Ian Rogers62d6c772013-02-27 08:32:07 -0800593 }
Mathieu Chartiere0671ce2015-07-28 17:23:28 -0700594 InstallStubsClassVisitor visitor(this);
595 runtime->GetClassLinker()->VisitClasses(&visitor);
Ian Rogers62d6c772013-02-27 08:32:07 -0800596 instrumentation_stubs_installed_ = true;
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100597 MutexLock mu(self, *Locks::thread_list_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800598 runtime->GetThreadList()->ForEach(InstrumentationInstallStack, this);
599 } else {
600 interpreter_stubs_installed_ = false;
601 entry_exit_stubs_installed_ = false;
Mathieu Chartiere0671ce2015-07-28 17:23:28 -0700602 InstallStubsClassVisitor visitor(this);
603 runtime->GetClassLinker()->VisitClasses(&visitor);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100604 // Restore stack only if there is no method currently deoptimized.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700605 bool empty;
606 {
607 ReaderMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700608 empty = IsDeoptimizedMethodsEmpty(); // Avoid lock violation.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700609 }
610 if (empty) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100611 MutexLock mu(self, *Locks::thread_list_lock_);
612 Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this);
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +0000613 // Only do this after restoring, as walking the stack when restoring will see
614 // the instrumentation exit pc.
615 instrumentation_stubs_installed_ = false;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100616 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800617 }
jeffhao725a9572012-11-13 18:20:12 -0800618}
619
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200620static void ResetQuickAllocEntryPointsForThread(Thread* thread, void* arg ATTRIBUTE_UNUSED) {
Ian Rogersfa824272013-11-05 16:12:57 -0800621 thread->ResetQuickAllocEntryPointsForThread();
622}
623
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700624void Instrumentation::SetEntrypointsInstrumented(bool instrumented) {
625 Thread* self = Thread::Current();
Mathieu Chartier661974a2014-01-09 11:23:53 -0800626 Runtime* runtime = Runtime::Current();
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700627 Locks::mutator_lock_->AssertNotHeld(self);
628 Locks::instrument_entrypoints_lock_->AssertHeld(self);
629 if (runtime->IsStarted()) {
Mathieu Chartier4f55e222015-09-04 13:26:21 -0700630 ScopedSuspendAll ssa(__FUNCTION__);
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700631 MutexLock mu(self, *Locks::runtime_shutdown_lock_);
Mathieu Chartier661974a2014-01-09 11:23:53 -0800632 SetQuickAllocEntryPointsInstrumented(instrumented);
633 ResetQuickAllocEntryPoints();
Mathieu Chartier4f55e222015-09-04 13:26:21 -0700634 } else {
635 MutexLock mu(self, *Locks::runtime_shutdown_lock_);
636 SetQuickAllocEntryPointsInstrumented(instrumented);
637 ResetQuickAllocEntryPoints();
Mathieu Chartier661974a2014-01-09 11:23:53 -0800638 }
639}
640
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700641void Instrumentation::InstrumentQuickAllocEntryPoints() {
642 MutexLock mu(Thread::Current(), *Locks::instrument_entrypoints_lock_);
643 InstrumentQuickAllocEntryPointsLocked();
Ian Rogersfa824272013-11-05 16:12:57 -0800644}
645
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700646void Instrumentation::UninstrumentQuickAllocEntryPoints() {
647 MutexLock mu(Thread::Current(), *Locks::instrument_entrypoints_lock_);
648 UninstrumentQuickAllocEntryPointsLocked();
649}
650
651void Instrumentation::InstrumentQuickAllocEntryPointsLocked() {
652 Locks::instrument_entrypoints_lock_->AssertHeld(Thread::Current());
653 if (quick_alloc_entry_points_instrumentation_counter_ == 0) {
654 SetEntrypointsInstrumented(true);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800655 }
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700656 ++quick_alloc_entry_points_instrumentation_counter_;
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700657}
658
659void Instrumentation::UninstrumentQuickAllocEntryPointsLocked() {
660 Locks::instrument_entrypoints_lock_->AssertHeld(Thread::Current());
661 CHECK_GT(quick_alloc_entry_points_instrumentation_counter_, 0U);
662 --quick_alloc_entry_points_instrumentation_counter_;
663 if (quick_alloc_entry_points_instrumentation_counter_ == 0) {
664 SetEntrypointsInstrumented(false);
665 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800666}
667
668void Instrumentation::ResetQuickAllocEntryPoints() {
669 Runtime* runtime = Runtime::Current();
670 if (runtime->IsStarted()) {
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800671 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700672 runtime->GetThreadList()->ForEach(ResetQuickAllocEntryPointsForThread, nullptr);
Ian Rogersfa824272013-11-05 16:12:57 -0800673 }
674}
675
Mathieu Chartiere401d142015-04-22 13:56:20 -0700676void Instrumentation::UpdateMethodsCode(ArtMethod* method, const void* quick_code) {
Sebastien Hertza8a697f2015-01-15 12:28:47 +0100677 DCHECK(method->GetDeclaringClass()->IsResolved());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800678 const void* new_quick_code;
Ian Rogers62d6c772013-02-27 08:32:07 -0800679 if (LIKELY(!instrumentation_stubs_installed_)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800680 new_quick_code = quick_code;
Jeff Hao65d15d92013-07-16 16:39:33 -0700681 } else {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100682 if ((interpreter_stubs_installed_ || IsDeoptimized(method)) && !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800683 new_quick_code = GetQuickToInterpreterBridge();
Jeff Hao65d15d92013-07-16 16:39:33 -0700684 } else {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700685 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700686 if (class_linker->IsQuickResolutionStub(quick_code) ||
687 class_linker->IsQuickToInterpreterBridge(quick_code)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700688 new_quick_code = quick_code;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700689 } else if (entry_exit_stubs_installed_) {
690 new_quick_code = GetQuickInstrumentationEntryPoint();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700691 } else {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700692 new_quick_code = quick_code;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700693 }
Jeff Hao65d15d92013-07-16 16:39:33 -0700694 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800695 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800696 UpdateEntrypoints(method, new_quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100697}
698
Mathieu Chartiere401d142015-04-22 13:56:20 -0700699bool Instrumentation::AddDeoptimizedMethod(ArtMethod* method) {
700 if (IsDeoptimizedMethod(method)) {
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700701 // Already in the map. Return.
702 return false;
703 }
704 // Not found. Add it.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700705 deoptimized_methods_.insert(method);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700706 return true;
707}
708
Mathieu Chartiere401d142015-04-22 13:56:20 -0700709bool Instrumentation::IsDeoptimizedMethod(ArtMethod* method) {
710 return deoptimized_methods_.find(method) != deoptimized_methods_.end();
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700711}
712
Mathieu Chartiere401d142015-04-22 13:56:20 -0700713ArtMethod* Instrumentation::BeginDeoptimizedMethod() {
714 if (deoptimized_methods_.empty()) {
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700715 // Empty.
716 return nullptr;
717 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700718 return *deoptimized_methods_.begin();
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700719}
720
Mathieu Chartiere401d142015-04-22 13:56:20 -0700721bool Instrumentation::RemoveDeoptimizedMethod(ArtMethod* method) {
722 auto it = deoptimized_methods_.find(method);
723 if (it == deoptimized_methods_.end()) {
724 return false;
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700725 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700726 deoptimized_methods_.erase(it);
727 return true;
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700728}
729
730bool Instrumentation::IsDeoptimizedMethodsEmpty() const {
731 return deoptimized_methods_.empty();
732}
733
Mathieu Chartiere401d142015-04-22 13:56:20 -0700734void Instrumentation::Deoptimize(ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100735 CHECK(!method->IsNative());
736 CHECK(!method->IsProxyMethod());
Alex Light9139e002015-10-09 15:59:48 -0700737 CHECK(method->IsInvokable());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100738
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700739 Thread* self = Thread::Current();
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700740 {
741 WriterMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700742 bool has_not_been_deoptimized = AddDeoptimizedMethod(method);
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200743 CHECK(has_not_been_deoptimized) << "Method " << PrettyMethod(method)
744 << " is already deoptimized";
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700745 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100746 if (!interpreter_stubs_installed_) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800747 UpdateEntrypoints(method, GetQuickInstrumentationEntryPoint());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100748
749 // Install instrumentation exit stub and instrumentation frames. We may already have installed
750 // these previously so it will only cover the newly created frames.
751 instrumentation_stubs_installed_ = true;
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700752 MutexLock mu(self, *Locks::thread_list_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100753 Runtime::Current()->GetThreadList()->ForEach(InstrumentationInstallStack, this);
754 }
755}
756
Mathieu Chartiere401d142015-04-22 13:56:20 -0700757void Instrumentation::Undeoptimize(ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100758 CHECK(!method->IsNative());
759 CHECK(!method->IsProxyMethod());
Alex Light9139e002015-10-09 15:59:48 -0700760 CHECK(method->IsInvokable());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100761
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700762 Thread* self = Thread::Current();
763 bool empty;
764 {
765 WriterMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700766 bool found_and_erased = RemoveDeoptimizedMethod(method);
767 CHECK(found_and_erased) << "Method " << PrettyMethod(method)
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700768 << " is not deoptimized";
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700769 empty = IsDeoptimizedMethodsEmpty();
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700770 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100771
772 // Restore code and possibly stack only if we did not deoptimize everything.
773 if (!interpreter_stubs_installed_) {
774 // Restore its code or resolution trampoline.
775 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800776 if (method->IsStatic() && !method->IsConstructor() &&
777 !method->GetDeclaringClass()->IsInitialized()) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800778 UpdateEntrypoints(method, GetQuickResolutionStub());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100779 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800780 const void* quick_code = class_linker->GetQuickOatCodeFor(method);
Elliott Hughes956af0f2014-12-11 14:34:28 -0800781 UpdateEntrypoints(method, quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100782 }
783
784 // If there is no deoptimized method left, we can restore the stack of each thread.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700785 if (empty) {
786 MutexLock mu(self, *Locks::thread_list_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100787 Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this);
788 instrumentation_stubs_installed_ = false;
789 }
790 }
791}
792
Mathieu Chartiere401d142015-04-22 13:56:20 -0700793bool Instrumentation::IsDeoptimized(ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100794 DCHECK(method != nullptr);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700795 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700796 return IsDeoptimizedMethod(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100797}
798
799void Instrumentation::EnableDeoptimization() {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700800 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700801 CHECK(IsDeoptimizedMethodsEmpty());
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100802 CHECK_EQ(deoptimization_enabled_, false);
803 deoptimization_enabled_ = true;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100804}
805
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200806void Instrumentation::DisableDeoptimization(const char* key) {
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100807 CHECK_EQ(deoptimization_enabled_, true);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100808 // If we deoptimized everything, undo it.
809 if (interpreter_stubs_installed_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200810 UndeoptimizeEverything(key);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100811 }
812 // Undeoptimized selected methods.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700813 while (true) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700814 ArtMethod* method;
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700815 {
816 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700817 if (IsDeoptimizedMethodsEmpty()) {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700818 break;
819 }
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700820 method = BeginDeoptimizedMethod();
821 CHECK(method != nullptr);
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700822 }
823 Undeoptimize(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100824 }
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100825 deoptimization_enabled_ = false;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100826}
827
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100828// Indicates if instrumentation should notify method enter/exit events to the listeners.
829bool Instrumentation::ShouldNotifyMethodEnterExitEvents() const {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200830 if (!HasMethodEntryListeners() && !HasMethodExitListeners()) {
831 return false;
832 }
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100833 return !deoptimization_enabled_ && !interpreter_stubs_installed_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100834}
835
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200836void Instrumentation::DeoptimizeEverything(const char* key) {
837 CHECK(deoptimization_enabled_);
838 ConfigureStubs(key, InstrumentationLevel::kInstrumentWithInterpreter);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100839}
840
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200841void Instrumentation::UndeoptimizeEverything(const char* key) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100842 CHECK(interpreter_stubs_installed_);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200843 CHECK(deoptimization_enabled_);
844 ConfigureStubs(key, InstrumentationLevel::kInstrumentNothing);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100845}
846
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200847void Instrumentation::EnableMethodTracing(const char* key, bool needs_interpreter) {
848 InstrumentationLevel level;
849 if (needs_interpreter) {
850 level = InstrumentationLevel::kInstrumentWithInterpreter;
851 } else {
852 level = InstrumentationLevel::kInstrumentWithInstrumentationStubs;
853 }
854 ConfigureStubs(key, level);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100855}
856
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200857void Instrumentation::DisableMethodTracing(const char* key) {
858 ConfigureStubs(key, InstrumentationLevel::kInstrumentNothing);
jeffhao725a9572012-11-13 18:20:12 -0800859}
860
Mathieu Chartiere401d142015-04-22 13:56:20 -0700861const void* Instrumentation::GetQuickCodeFor(ArtMethod* method, size_t pointer_size) const {
Ian Rogers62d6c772013-02-27 08:32:07 -0800862 Runtime* runtime = Runtime::Current();
863 if (LIKELY(!instrumentation_stubs_installed_)) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800864 const void* code = method->GetEntryPointFromQuickCompiledCodePtrSize(pointer_size);
Vladimir Marko8a630572014-04-09 18:45:35 +0100865 DCHECK(code != nullptr);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700866 ClassLinker* class_linker = runtime->GetClassLinker();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700867 if (LIKELY(!class_linker->IsQuickResolutionStub(code) &&
868 !class_linker->IsQuickToInterpreterBridge(code)) &&
869 !class_linker->IsQuickResolutionStub(code) &&
870 !class_linker->IsQuickToInterpreterBridge(code)) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800871 return code;
872 }
873 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800874 return runtime->GetClassLinker()->GetQuickOatCodeFor(method);
jeffhao725a9572012-11-13 18:20:12 -0800875}
876
Ian Rogers62d6c772013-02-27 08:32:07 -0800877void Instrumentation::MethodEnterEventImpl(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700878 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800879 uint32_t dex_pc) const {
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000880 if (HasMethodEntryListeners()) {
881 for (InstrumentationListener* listener : method_entry_listeners_) {
882 if (listener != nullptr) {
883 listener->MethodEntered(thread, this_object, method, dex_pc);
884 }
885 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800886 }
887}
888
889void Instrumentation::MethodExitEventImpl(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700890 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800891 uint32_t dex_pc, const JValue& return_value) const {
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000892 if (HasMethodExitListeners()) {
893 for (InstrumentationListener* listener : method_exit_listeners_) {
894 if (listener != nullptr) {
895 listener->MethodExited(thread, this_object, method, dex_pc, return_value);
896 }
897 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800898 }
899}
900
901void Instrumentation::MethodUnwindEvent(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700902 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800903 uint32_t dex_pc) const {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200904 if (HasMethodUnwindListeners()) {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700905 for (InstrumentationListener* listener : method_unwind_listeners_) {
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000906 if (listener != nullptr) {
907 listener->MethodUnwind(thread, this_object, method, dex_pc);
908 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800909 }
910 }
911}
912
913void Instrumentation::DexPcMovedEventImpl(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700914 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800915 uint32_t dex_pc) const {
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000916 for (InstrumentationListener* listener : dex_pc_listeners_) {
917 if (listener != nullptr) {
918 listener->DexPcMoved(thread, this_object, method, dex_pc);
919 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800920 }
921}
922
Mathieu Chartiere401d142015-04-22 13:56:20 -0700923void Instrumentation::BackwardBranchImpl(Thread* thread, ArtMethod* method,
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800924 int32_t offset) const {
925 for (InstrumentationListener* listener : backward_branch_listeners_) {
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000926 if (listener != nullptr) {
927 listener->BackwardBranch(thread, method, offset);
928 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800929 }
930}
931
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100932void Instrumentation::InvokeVirtualOrInterfaceImpl(Thread* thread,
933 mirror::Object* this_object,
934 ArtMethod* caller,
935 uint32_t dex_pc,
936 ArtMethod* callee) const {
937 for (InstrumentationListener* listener : invoke_virtual_or_interface_listeners_) {
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000938 if (listener != nullptr) {
939 listener->InvokeVirtualOrInterface(thread, this_object, caller, dex_pc, callee);
940 }
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100941 }
942}
943
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200944void Instrumentation::FieldReadEventImpl(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700945 ArtMethod* method, uint32_t dex_pc,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700946 ArtField* field) const {
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000947 for (InstrumentationListener* listener : field_read_listeners_) {
948 if (listener != nullptr) {
949 listener->FieldRead(thread, this_object, method, dex_pc, field);
950 }
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200951 }
952}
953
954void Instrumentation::FieldWriteEventImpl(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700955 ArtMethod* method, uint32_t dex_pc,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700956 ArtField* field, const JValue& field_value) const {
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000957 for (InstrumentationListener* listener : field_write_listeners_) {
958 if (listener != nullptr) {
959 listener->FieldWritten(thread, this_object, method, dex_pc, field, field_value);
960 }
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200961 }
962}
963
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000964void Instrumentation::ExceptionCaughtEvent(Thread* thread,
Sebastien Hertz947ff082013-09-17 14:10:13 +0200965 mirror::Throwable* exception_object) const {
Sebastien Hertz9f102032014-05-23 08:59:42 +0200966 if (HasExceptionCaughtListeners()) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000967 DCHECK_EQ(thread->GetException(), exception_object);
Jeff Haoc0bd4da2013-04-11 15:52:28 -0700968 thread->ClearException();
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000969 for (InstrumentationListener* listener : exception_caught_listeners_) {
970 if (listener != nullptr) {
971 listener->ExceptionCaught(thread, exception_object);
972 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800973 }
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000974 thread->SetException(exception_object);
Ian Rogers62d6c772013-02-27 08:32:07 -0800975 }
976}
977
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +0000978// Computes a frame ID by ignoring inlined frames.
979size_t Instrumentation::ComputeFrameId(Thread* self,
980 size_t frame_depth,
981 size_t inlined_frames_before_frame) {
982 CHECK_GE(frame_depth, inlined_frames_before_frame);
983 size_t no_inline_depth = frame_depth - inlined_frames_before_frame;
984 return StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk) - no_inline_depth;
985}
986
Ian Rogers62d6c772013-02-27 08:32:07 -0800987static void CheckStackDepth(Thread* self, const InstrumentationStackFrame& instrumentation_frame,
988 int delta)
Mathieu Chartier90443472015-07-16 20:32:27 -0700989 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100990 size_t frame_id = StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk) + delta;
Ian Rogers62d6c772013-02-27 08:32:07 -0800991 if (frame_id != instrumentation_frame.frame_id_) {
992 LOG(ERROR) << "Expected frame_id=" << frame_id << " but found "
993 << instrumentation_frame.frame_id_;
994 StackVisitor::DescribeStack(self);
995 CHECK_EQ(frame_id, instrumentation_frame.frame_id_);
996 }
997}
998
999void Instrumentation::PushInstrumentationStackFrame(Thread* self, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001000 ArtMethod* method,
Jeff Hao9a916d32013-06-27 18:45:37 -07001001 uintptr_t lr, bool interpreter_entry) {
Ian Rogers62d6c772013-02-27 08:32:07 -08001002 // We have a callee-save frame meaning this value is guaranteed to never be 0.
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +01001003 size_t frame_id = StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk);
Ian Rogers62d6c772013-02-27 08:32:07 -08001004 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
1005 if (kVerboseInstrumentation) {
Brian Carlstrom2d888622013-07-18 17:02:00 -07001006 LOG(INFO) << "Entering " << PrettyMethod(method) << " from PC " << reinterpret_cast<void*>(lr);
Ian Rogers62d6c772013-02-27 08:32:07 -08001007 }
1008 instrumentation::InstrumentationStackFrame instrumentation_frame(this_object, method, lr,
Jeff Hao9a916d32013-06-27 18:45:37 -07001009 frame_id, interpreter_entry);
Ian Rogers62d6c772013-02-27 08:32:07 -08001010 stack->push_front(instrumentation_frame);
1011
Sebastien Hertz320deb22014-06-11 19:45:05 +02001012 if (!interpreter_entry) {
1013 MethodEnterEvent(self, this_object, method, 0);
1014 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001015}
1016
Andreas Gamped58342c2014-06-05 14:18:08 -07001017TwoWordReturn Instrumentation::PopInstrumentationStackFrame(Thread* self, uintptr_t* return_pc,
1018 uint64_t gpr_result,
1019 uint64_t fpr_result) {
Ian Rogers62d6c772013-02-27 08:32:07 -08001020 // Do the pop.
1021 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
1022 CHECK_GT(stack->size(), 0U);
1023 InstrumentationStackFrame instrumentation_frame = stack->front();
1024 stack->pop_front();
1025
1026 // Set return PC and check the sanity of the stack.
1027 *return_pc = instrumentation_frame.return_pc_;
1028 CheckStackDepth(self, instrumentation_frame, 0);
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001029 self->VerifyStack();
Ian Rogers62d6c772013-02-27 08:32:07 -08001030
Mathieu Chartiere401d142015-04-22 13:56:20 -07001031 ArtMethod* method = instrumentation_frame.method_;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001032 uint32_t length;
Mingyao Yang99170c62015-07-06 11:10:37 -07001033 const size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
1034 char return_shorty = method->GetInterfaceMethodIfProxy(pointer_size)->GetShorty(&length)[0];
Ian Rogers62d6c772013-02-27 08:32:07 -08001035 JValue return_value;
1036 if (return_shorty == 'V') {
1037 return_value.SetJ(0);
1038 } else if (return_shorty == 'F' || return_shorty == 'D') {
1039 return_value.SetJ(fpr_result);
1040 } else {
1041 return_value.SetJ(gpr_result);
1042 }
1043 // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to
1044 // return_pc.
1045 uint32_t dex_pc = DexFile::kDexNoIndex;
1046 mirror::Object* this_object = instrumentation_frame.this_object_;
Sebastien Hertz320deb22014-06-11 19:45:05 +02001047 if (!instrumentation_frame.interpreter_entry_) {
1048 MethodExitEvent(self, this_object, instrumentation_frame.method_, dex_pc, return_value);
1049 }
jeffhao725a9572012-11-13 18:20:12 -08001050
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01001051 // Deoptimize if the caller needs to continue execution in the interpreter. Do nothing if we get
1052 // back to an upcall.
1053 NthCallerVisitor visitor(self, 1, true);
1054 visitor.WalkStack(true);
Sebastien Hertz270a0e12015-01-16 19:49:09 +01001055 bool deoptimize = (visitor.caller != nullptr) &&
Daniel Mihalyieb076692014-08-22 17:33:31 +02001056 (interpreter_stubs_installed_ || IsDeoptimized(visitor.caller) ||
1057 Dbg::IsForcedInterpreterNeededForUpcall(self, visitor.caller));
Ian Rogers62d6c772013-02-27 08:32:07 -08001058 if (deoptimize) {
1059 if (kVerboseInstrumentation) {
Sebastien Hertz270a0e12015-01-16 19:49:09 +01001060 LOG(INFO) << StringPrintf("Deoptimizing %s by returning from %s with result %#" PRIx64 " in ",
1061 PrettyMethod(visitor.caller).c_str(),
1062 PrettyMethod(method).c_str(),
1063 return_value.GetJ()) << *self;
Ian Rogers62d6c772013-02-27 08:32:07 -08001064 }
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01001065 self->PushDeoptimizationContext(return_value,
1066 return_shorty == 'L',
1067 false /* from_code */,
Sebastien Hertz07474662015-08-25 15:12:33 +00001068 nullptr /* no pending exception */);
Andreas Gamped58342c2014-06-05 14:18:08 -07001069 return GetTwoWordSuccessValue(*return_pc,
1070 reinterpret_cast<uintptr_t>(GetQuickDeoptimizationEntryPoint()));
Ian Rogers62d6c772013-02-27 08:32:07 -08001071 } else {
1072 if (kVerboseInstrumentation) {
Brian Carlstrom2d888622013-07-18 17:02:00 -07001073 LOG(INFO) << "Returning from " << PrettyMethod(method)
1074 << " to PC " << reinterpret_cast<void*>(*return_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -08001075 }
Andreas Gamped58342c2014-06-05 14:18:08 -07001076 return GetTwoWordSuccessValue(0, *return_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -08001077 }
jeffhao725a9572012-11-13 18:20:12 -08001078}
1079
Ian Rogers62d6c772013-02-27 08:32:07 -08001080void Instrumentation::PopMethodForUnwind(Thread* self, bool is_deoptimization) const {
1081 // Do the pop.
1082 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
1083 CHECK_GT(stack->size(), 0U);
1084 InstrumentationStackFrame instrumentation_frame = stack->front();
1085 // TODO: bring back CheckStackDepth(self, instrumentation_frame, 2);
1086 stack->pop_front();
1087
Mathieu Chartiere401d142015-04-22 13:56:20 -07001088 ArtMethod* method = instrumentation_frame.method_;
Ian Rogers62d6c772013-02-27 08:32:07 -08001089 if (is_deoptimization) {
1090 if (kVerboseInstrumentation) {
1091 LOG(INFO) << "Popping for deoptimization " << PrettyMethod(method);
1092 }
1093 } else {
1094 if (kVerboseInstrumentation) {
1095 LOG(INFO) << "Popping for unwind " << PrettyMethod(method);
1096 }
1097
1098 // Notify listeners of method unwind.
1099 // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to
1100 // return_pc.
1101 uint32_t dex_pc = DexFile::kDexNoIndex;
1102 MethodUnwindEvent(self, instrumentation_frame.this_object_, method, dex_pc);
1103 }
1104}
1105
1106std::string InstrumentationStackFrame::Dump() const {
1107 std::ostringstream os;
1108 os << "Frame " << frame_id_ << " " << PrettyMethod(method_) << ":"
1109 << reinterpret_cast<void*>(return_pc_) << " this=" << reinterpret_cast<void*>(this_object_);
1110 return os.str();
1111}
1112
1113} // namespace instrumentation
jeffhao725a9572012-11-13 18:20:12 -08001114} // namespace art