blob: c6a9e6ce771ac9122095b0b163842c862299897c [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) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100111 if (method->IsAbstract() || method->IsProxyMethod()) {
112 // 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
Ian Rogers62d6c772013-02-27 08:32:07 -0800399void Instrumentation::AddListener(InstrumentationListener* listener, uint32_t events) {
400 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200401 if (HasEvent(kMethodEntered, events)) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800402 method_entry_listeners_.push_back(listener);
Ian Rogers62d6c772013-02-27 08:32:07 -0800403 have_method_entry_listeners_ = true;
404 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200405 if (HasEvent(kMethodExited, events)) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800406 method_exit_listeners_.push_back(listener);
Ian Rogers62d6c772013-02-27 08:32:07 -0800407 have_method_exit_listeners_ = true;
408 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200409 if (HasEvent(kMethodUnwind, events)) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800410 method_unwind_listeners_.push_back(listener);
411 have_method_unwind_listeners_ = true;
412 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200413 if (HasEvent(kBackwardBranch, events)) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800414 backward_branch_listeners_.push_back(listener);
415 have_backward_branch_listeners_ = true;
416 }
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100417 if (HasEvent(kInvokeVirtualOrInterface, events)) {
418 invoke_virtual_or_interface_listeners_.push_back(listener);
419 have_invoke_virtual_or_interface_listeners_ = true;
420 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200421 if (HasEvent(kDexPcMoved, events)) {
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200422 std::list<InstrumentationListener*>* modified;
423 if (have_dex_pc_listeners_) {
424 modified = new std::list<InstrumentationListener*>(*dex_pc_listeners_.get());
425 } else {
426 modified = new std::list<InstrumentationListener*>();
427 }
428 modified->push_back(listener);
429 dex_pc_listeners_.reset(modified);
Ian Rogers62d6c772013-02-27 08:32:07 -0800430 have_dex_pc_listeners_ = true;
431 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200432 if (HasEvent(kFieldRead, events)) {
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200433 std::list<InstrumentationListener*>* modified;
434 if (have_field_read_listeners_) {
435 modified = new std::list<InstrumentationListener*>(*field_read_listeners_.get());
436 } else {
437 modified = new std::list<InstrumentationListener*>();
438 }
439 modified->push_back(listener);
440 field_read_listeners_.reset(modified);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200441 have_field_read_listeners_ = true;
442 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200443 if (HasEvent(kFieldWritten, events)) {
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200444 std::list<InstrumentationListener*>* modified;
445 if (have_field_write_listeners_) {
446 modified = new std::list<InstrumentationListener*>(*field_write_listeners_.get());
447 } else {
448 modified = new std::list<InstrumentationListener*>();
449 }
450 modified->push_back(listener);
451 field_write_listeners_.reset(modified);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200452 have_field_write_listeners_ = true;
453 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200454 if (HasEvent(kExceptionCaught, events)) {
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200455 std::list<InstrumentationListener*>* modified;
456 if (have_exception_caught_listeners_) {
457 modified = new std::list<InstrumentationListener*>(*exception_caught_listeners_.get());
458 } else {
459 modified = new std::list<InstrumentationListener*>();
460 }
461 modified->push_back(listener);
462 exception_caught_listeners_.reset(modified);
Jeff Hao14dd5a82013-04-11 10:23:36 -0700463 have_exception_caught_listeners_ = true;
464 }
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200465 UpdateInterpreterHandlerTable();
jeffhao725a9572012-11-13 18:20:12 -0800466}
467
Ian Rogers62d6c772013-02-27 08:32:07 -0800468void Instrumentation::RemoveListener(InstrumentationListener* listener, uint32_t events) {
469 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
Ian Rogers62d6c772013-02-27 08:32:07 -0800470
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200471 if (HasEvent(kMethodEntered, events) && have_method_entry_listeners_) {
472 method_entry_listeners_.remove(listener);
473 have_method_entry_listeners_ = !method_entry_listeners_.empty();
Ian Rogers62d6c772013-02-27 08:32:07 -0800474 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200475 if (HasEvent(kMethodExited, events) && have_method_exit_listeners_) {
476 method_exit_listeners_.remove(listener);
477 have_method_exit_listeners_ = !method_exit_listeners_.empty();
Ian Rogers62d6c772013-02-27 08:32:07 -0800478 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200479 if (HasEvent(kMethodUnwind, events) && have_method_unwind_listeners_) {
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100480 method_unwind_listeners_.remove(listener);
481 have_method_unwind_listeners_ = !method_unwind_listeners_.empty();
Ian Rogers62d6c772013-02-27 08:32:07 -0800482 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200483 if (HasEvent(kBackwardBranch, events) && have_backward_branch_listeners_) {
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100484 backward_branch_listeners_.remove(listener);
485 have_backward_branch_listeners_ = !backward_branch_listeners_.empty();
486 }
487 if (HasEvent(kInvokeVirtualOrInterface, events) && have_invoke_virtual_or_interface_listeners_) {
488 invoke_virtual_or_interface_listeners_.remove(listener);
489 have_invoke_virtual_or_interface_listeners_ = !invoke_virtual_or_interface_listeners_.empty();
490 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200491 if (HasEvent(kDexPcMoved, events) && have_dex_pc_listeners_) {
492 std::list<InstrumentationListener*>* modified =
493 new std::list<InstrumentationListener*>(*dex_pc_listeners_.get());
494 modified->remove(listener);
495 have_dex_pc_listeners_ = !modified->empty();
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200496 if (have_dex_pc_listeners_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200497 dex_pc_listeners_.reset(modified);
498 } else {
499 dex_pc_listeners_.reset();
500 delete modified;
Ian Rogers62d6c772013-02-27 08:32:07 -0800501 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800502 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200503 if (HasEvent(kFieldRead, events) && have_field_read_listeners_) {
504 std::list<InstrumentationListener*>* modified =
505 new std::list<InstrumentationListener*>(*field_read_listeners_.get());
506 modified->remove(listener);
507 have_field_read_listeners_ = !modified->empty();
Daniel Mihalyi66445212014-08-21 15:57:25 +0200508 if (have_field_read_listeners_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200509 field_read_listeners_.reset(modified);
510 } else {
511 field_read_listeners_.reset();
512 delete modified;
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200513 }
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200514 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200515 if (HasEvent(kFieldWritten, events) && have_field_write_listeners_) {
516 std::list<InstrumentationListener*>* modified =
517 new std::list<InstrumentationListener*>(*field_write_listeners_.get());
518 modified->remove(listener);
519 have_field_write_listeners_ = !modified->empty();
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200520 if (have_field_write_listeners_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200521 field_write_listeners_.reset(modified);
522 } else {
523 field_write_listeners_.reset();
524 delete modified;
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200525 }
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200526 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200527 if (HasEvent(kExceptionCaught, events) && have_exception_caught_listeners_) {
528 std::list<InstrumentationListener*>* modified =
529 new std::list<InstrumentationListener*>(*exception_caught_listeners_.get());
530 modified->remove(listener);
531 have_exception_caught_listeners_ = !modified->empty();
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200532 if (have_exception_caught_listeners_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200533 exception_caught_listeners_.reset(modified);
534 } else {
535 exception_caught_listeners_.reset();
536 delete modified;
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200537 }
Jeff Hao14dd5a82013-04-11 10:23:36 -0700538 }
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200539 UpdateInterpreterHandlerTable();
jeffhao725a9572012-11-13 18:20:12 -0800540}
541
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200542Instrumentation::InstrumentationLevel Instrumentation::GetCurrentInstrumentationLevel() const {
Ian Rogers62d6c772013-02-27 08:32:07 -0800543 if (interpreter_stubs_installed_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200544 return InstrumentationLevel::kInstrumentWithInterpreter;
Ian Rogers62d6c772013-02-27 08:32:07 -0800545 } else if (entry_exit_stubs_installed_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200546 return InstrumentationLevel::kInstrumentWithInstrumentationStubs;
Ian Rogers62d6c772013-02-27 08:32:07 -0800547 } else {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200548 return InstrumentationLevel::kInstrumentNothing;
Ian Rogers62d6c772013-02-27 08:32:07 -0800549 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200550}
551
552void Instrumentation::ConfigureStubs(const char* key, InstrumentationLevel desired_level) {
553 // Store the instrumentation level for this key or remove it.
554 if (desired_level == InstrumentationLevel::kInstrumentNothing) {
555 // The client no longer needs instrumentation.
556 requested_instrumentation_levels_.erase(key);
557 } else {
558 // The client needs instrumentation.
559 requested_instrumentation_levels_.Overwrite(key, desired_level);
560 }
561
562 // Look for the highest required instrumentation level.
563 InstrumentationLevel requested_level = InstrumentationLevel::kInstrumentNothing;
564 for (const auto& v : requested_instrumentation_levels_) {
565 requested_level = std::max(requested_level, v.second);
566 }
567
568 interpret_only_ = (requested_level == InstrumentationLevel::kInstrumentWithInterpreter) ||
569 forced_interpret_only_;
570
571 InstrumentationLevel current_level = GetCurrentInstrumentationLevel();
572 if (requested_level == current_level) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800573 // We're already set.
574 return;
575 }
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100576 Thread* const self = Thread::Current();
Ian Rogers62d6c772013-02-27 08:32:07 -0800577 Runtime* runtime = Runtime::Current();
Sebastien Hertza8a697f2015-01-15 12:28:47 +0100578 Locks::mutator_lock_->AssertExclusiveHeld(self);
Ian Rogers62d6c772013-02-27 08:32:07 -0800579 Locks::thread_list_lock_->AssertNotHeld(self);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200580 if (requested_level > InstrumentationLevel::kInstrumentNothing) {
581 if (requested_level == InstrumentationLevel::kInstrumentWithInterpreter) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800582 interpreter_stubs_installed_ = true;
Ian Rogers62d6c772013-02-27 08:32:07 -0800583 entry_exit_stubs_installed_ = true;
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200584 } else {
585 CHECK_EQ(requested_level, InstrumentationLevel::kInstrumentWithInstrumentationStubs);
586 entry_exit_stubs_installed_ = true;
587 interpreter_stubs_installed_ = false;
Ian Rogers62d6c772013-02-27 08:32:07 -0800588 }
Mathieu Chartiere0671ce2015-07-28 17:23:28 -0700589 InstallStubsClassVisitor visitor(this);
590 runtime->GetClassLinker()->VisitClasses(&visitor);
Ian Rogers62d6c772013-02-27 08:32:07 -0800591 instrumentation_stubs_installed_ = true;
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100592 MutexLock mu(self, *Locks::thread_list_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800593 runtime->GetThreadList()->ForEach(InstrumentationInstallStack, this);
594 } else {
595 interpreter_stubs_installed_ = false;
596 entry_exit_stubs_installed_ = false;
Mathieu Chartiere0671ce2015-07-28 17:23:28 -0700597 InstallStubsClassVisitor visitor(this);
598 runtime->GetClassLinker()->VisitClasses(&visitor);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100599 // Restore stack only if there is no method currently deoptimized.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700600 bool empty;
601 {
602 ReaderMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700603 empty = IsDeoptimizedMethodsEmpty(); // Avoid lock violation.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700604 }
605 if (empty) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100606 MutexLock mu(self, *Locks::thread_list_lock_);
607 Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this);
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +0000608 // Only do this after restoring, as walking the stack when restoring will see
609 // the instrumentation exit pc.
610 instrumentation_stubs_installed_ = false;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100611 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800612 }
jeffhao725a9572012-11-13 18:20:12 -0800613}
614
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200615static void ResetQuickAllocEntryPointsForThread(Thread* thread, void* arg ATTRIBUTE_UNUSED) {
Ian Rogersfa824272013-11-05 16:12:57 -0800616 thread->ResetQuickAllocEntryPointsForThread();
617}
618
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700619void Instrumentation::SetEntrypointsInstrumented(bool instrumented) {
620 Thread* self = Thread::Current();
Mathieu Chartier661974a2014-01-09 11:23:53 -0800621 Runtime* runtime = Runtime::Current();
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700622 Locks::mutator_lock_->AssertNotHeld(self);
623 Locks::instrument_entrypoints_lock_->AssertHeld(self);
624 if (runtime->IsStarted()) {
Mathieu Chartier4f55e222015-09-04 13:26:21 -0700625 ScopedSuspendAll ssa(__FUNCTION__);
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700626 MutexLock mu(self, *Locks::runtime_shutdown_lock_);
Mathieu Chartier661974a2014-01-09 11:23:53 -0800627 SetQuickAllocEntryPointsInstrumented(instrumented);
628 ResetQuickAllocEntryPoints();
Mathieu Chartier4f55e222015-09-04 13:26:21 -0700629 } else {
630 MutexLock mu(self, *Locks::runtime_shutdown_lock_);
631 SetQuickAllocEntryPointsInstrumented(instrumented);
632 ResetQuickAllocEntryPoints();
Mathieu Chartier661974a2014-01-09 11:23:53 -0800633 }
634}
635
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700636void Instrumentation::InstrumentQuickAllocEntryPoints() {
637 MutexLock mu(Thread::Current(), *Locks::instrument_entrypoints_lock_);
638 InstrumentQuickAllocEntryPointsLocked();
Ian Rogersfa824272013-11-05 16:12:57 -0800639}
640
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700641void Instrumentation::UninstrumentQuickAllocEntryPoints() {
642 MutexLock mu(Thread::Current(), *Locks::instrument_entrypoints_lock_);
643 UninstrumentQuickAllocEntryPointsLocked();
644}
645
646void Instrumentation::InstrumentQuickAllocEntryPointsLocked() {
647 Locks::instrument_entrypoints_lock_->AssertHeld(Thread::Current());
648 if (quick_alloc_entry_points_instrumentation_counter_ == 0) {
649 SetEntrypointsInstrumented(true);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800650 }
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700651 ++quick_alloc_entry_points_instrumentation_counter_;
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700652}
653
654void Instrumentation::UninstrumentQuickAllocEntryPointsLocked() {
655 Locks::instrument_entrypoints_lock_->AssertHeld(Thread::Current());
656 CHECK_GT(quick_alloc_entry_points_instrumentation_counter_, 0U);
657 --quick_alloc_entry_points_instrumentation_counter_;
658 if (quick_alloc_entry_points_instrumentation_counter_ == 0) {
659 SetEntrypointsInstrumented(false);
660 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800661}
662
663void Instrumentation::ResetQuickAllocEntryPoints() {
664 Runtime* runtime = Runtime::Current();
665 if (runtime->IsStarted()) {
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800666 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700667 runtime->GetThreadList()->ForEach(ResetQuickAllocEntryPointsForThread, nullptr);
Ian Rogersfa824272013-11-05 16:12:57 -0800668 }
669}
670
Mathieu Chartiere401d142015-04-22 13:56:20 -0700671void Instrumentation::UpdateMethodsCode(ArtMethod* method, const void* quick_code) {
Sebastien Hertza8a697f2015-01-15 12:28:47 +0100672 DCHECK(method->GetDeclaringClass()->IsResolved());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800673 const void* new_quick_code;
Ian Rogers62d6c772013-02-27 08:32:07 -0800674 if (LIKELY(!instrumentation_stubs_installed_)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800675 new_quick_code = quick_code;
Jeff Hao65d15d92013-07-16 16:39:33 -0700676 } else {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100677 if ((interpreter_stubs_installed_ || IsDeoptimized(method)) && !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800678 new_quick_code = GetQuickToInterpreterBridge();
Jeff Hao65d15d92013-07-16 16:39:33 -0700679 } else {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700680 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700681 if (class_linker->IsQuickResolutionStub(quick_code) ||
682 class_linker->IsQuickToInterpreterBridge(quick_code)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700683 new_quick_code = quick_code;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700684 } else if (entry_exit_stubs_installed_) {
685 new_quick_code = GetQuickInstrumentationEntryPoint();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700686 } else {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700687 new_quick_code = quick_code;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700688 }
Jeff Hao65d15d92013-07-16 16:39:33 -0700689 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800690 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800691 UpdateEntrypoints(method, new_quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100692}
693
Mathieu Chartiere401d142015-04-22 13:56:20 -0700694bool Instrumentation::AddDeoptimizedMethod(ArtMethod* method) {
695 if (IsDeoptimizedMethod(method)) {
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700696 // Already in the map. Return.
697 return false;
698 }
699 // Not found. Add it.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700700 deoptimized_methods_.insert(method);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700701 return true;
702}
703
Mathieu Chartiere401d142015-04-22 13:56:20 -0700704bool Instrumentation::IsDeoptimizedMethod(ArtMethod* method) {
705 return deoptimized_methods_.find(method) != deoptimized_methods_.end();
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700706}
707
Mathieu Chartiere401d142015-04-22 13:56:20 -0700708ArtMethod* Instrumentation::BeginDeoptimizedMethod() {
709 if (deoptimized_methods_.empty()) {
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700710 // Empty.
711 return nullptr;
712 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700713 return *deoptimized_methods_.begin();
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700714}
715
Mathieu Chartiere401d142015-04-22 13:56:20 -0700716bool Instrumentation::RemoveDeoptimizedMethod(ArtMethod* method) {
717 auto it = deoptimized_methods_.find(method);
718 if (it == deoptimized_methods_.end()) {
719 return false;
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700720 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700721 deoptimized_methods_.erase(it);
722 return true;
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700723}
724
725bool Instrumentation::IsDeoptimizedMethodsEmpty() const {
726 return deoptimized_methods_.empty();
727}
728
Mathieu Chartiere401d142015-04-22 13:56:20 -0700729void Instrumentation::Deoptimize(ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100730 CHECK(!method->IsNative());
731 CHECK(!method->IsProxyMethod());
732 CHECK(!method->IsAbstract());
733
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700734 Thread* self = Thread::Current();
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700735 {
736 WriterMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700737 bool has_not_been_deoptimized = AddDeoptimizedMethod(method);
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200738 CHECK(has_not_been_deoptimized) << "Method " << PrettyMethod(method)
739 << " is already deoptimized";
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700740 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100741 if (!interpreter_stubs_installed_) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800742 UpdateEntrypoints(method, GetQuickInstrumentationEntryPoint());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100743
744 // Install instrumentation exit stub and instrumentation frames. We may already have installed
745 // these previously so it will only cover the newly created frames.
746 instrumentation_stubs_installed_ = true;
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700747 MutexLock mu(self, *Locks::thread_list_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100748 Runtime::Current()->GetThreadList()->ForEach(InstrumentationInstallStack, this);
749 }
750}
751
Mathieu Chartiere401d142015-04-22 13:56:20 -0700752void Instrumentation::Undeoptimize(ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100753 CHECK(!method->IsNative());
754 CHECK(!method->IsProxyMethod());
755 CHECK(!method->IsAbstract());
756
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700757 Thread* self = Thread::Current();
758 bool empty;
759 {
760 WriterMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700761 bool found_and_erased = RemoveDeoptimizedMethod(method);
762 CHECK(found_and_erased) << "Method " << PrettyMethod(method)
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700763 << " is not deoptimized";
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700764 empty = IsDeoptimizedMethodsEmpty();
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700765 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100766
767 // Restore code and possibly stack only if we did not deoptimize everything.
768 if (!interpreter_stubs_installed_) {
769 // Restore its code or resolution trampoline.
770 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800771 if (method->IsStatic() && !method->IsConstructor() &&
772 !method->GetDeclaringClass()->IsInitialized()) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800773 UpdateEntrypoints(method, GetQuickResolutionStub());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100774 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800775 const void* quick_code = class_linker->GetQuickOatCodeFor(method);
Elliott Hughes956af0f2014-12-11 14:34:28 -0800776 UpdateEntrypoints(method, quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100777 }
778
779 // If there is no deoptimized method left, we can restore the stack of each thread.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700780 if (empty) {
781 MutexLock mu(self, *Locks::thread_list_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100782 Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this);
783 instrumentation_stubs_installed_ = false;
784 }
785 }
786}
787
Mathieu Chartiere401d142015-04-22 13:56:20 -0700788bool Instrumentation::IsDeoptimized(ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100789 DCHECK(method != nullptr);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700790 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700791 return IsDeoptimizedMethod(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100792}
793
794void Instrumentation::EnableDeoptimization() {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700795 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700796 CHECK(IsDeoptimizedMethodsEmpty());
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100797 CHECK_EQ(deoptimization_enabled_, false);
798 deoptimization_enabled_ = true;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100799}
800
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200801void Instrumentation::DisableDeoptimization(const char* key) {
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100802 CHECK_EQ(deoptimization_enabled_, true);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100803 // If we deoptimized everything, undo it.
804 if (interpreter_stubs_installed_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200805 UndeoptimizeEverything(key);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100806 }
807 // Undeoptimized selected methods.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700808 while (true) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700809 ArtMethod* method;
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700810 {
811 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700812 if (IsDeoptimizedMethodsEmpty()) {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700813 break;
814 }
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700815 method = BeginDeoptimizedMethod();
816 CHECK(method != nullptr);
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700817 }
818 Undeoptimize(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100819 }
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100820 deoptimization_enabled_ = false;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100821}
822
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100823// Indicates if instrumentation should notify method enter/exit events to the listeners.
824bool Instrumentation::ShouldNotifyMethodEnterExitEvents() const {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200825 if (!HasMethodEntryListeners() && !HasMethodExitListeners()) {
826 return false;
827 }
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100828 return !deoptimization_enabled_ && !interpreter_stubs_installed_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100829}
830
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200831void Instrumentation::DeoptimizeEverything(const char* key) {
832 CHECK(deoptimization_enabled_);
833 ConfigureStubs(key, InstrumentationLevel::kInstrumentWithInterpreter);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100834}
835
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200836void Instrumentation::UndeoptimizeEverything(const char* key) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100837 CHECK(interpreter_stubs_installed_);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200838 CHECK(deoptimization_enabled_);
839 ConfigureStubs(key, InstrumentationLevel::kInstrumentNothing);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100840}
841
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200842void Instrumentation::EnableMethodTracing(const char* key, bool needs_interpreter) {
843 InstrumentationLevel level;
844 if (needs_interpreter) {
845 level = InstrumentationLevel::kInstrumentWithInterpreter;
846 } else {
847 level = InstrumentationLevel::kInstrumentWithInstrumentationStubs;
848 }
849 ConfigureStubs(key, level);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100850}
851
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200852void Instrumentation::DisableMethodTracing(const char* key) {
853 ConfigureStubs(key, InstrumentationLevel::kInstrumentNothing);
jeffhao725a9572012-11-13 18:20:12 -0800854}
855
Mathieu Chartiere401d142015-04-22 13:56:20 -0700856const void* Instrumentation::GetQuickCodeFor(ArtMethod* method, size_t pointer_size) const {
Ian Rogers62d6c772013-02-27 08:32:07 -0800857 Runtime* runtime = Runtime::Current();
858 if (LIKELY(!instrumentation_stubs_installed_)) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800859 const void* code = method->GetEntryPointFromQuickCompiledCodePtrSize(pointer_size);
Vladimir Marko8a630572014-04-09 18:45:35 +0100860 DCHECK(code != nullptr);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700861 ClassLinker* class_linker = runtime->GetClassLinker();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700862 if (LIKELY(!class_linker->IsQuickResolutionStub(code) &&
863 !class_linker->IsQuickToInterpreterBridge(code)) &&
864 !class_linker->IsQuickResolutionStub(code) &&
865 !class_linker->IsQuickToInterpreterBridge(code)) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800866 return code;
867 }
868 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800869 return runtime->GetClassLinker()->GetQuickOatCodeFor(method);
jeffhao725a9572012-11-13 18:20:12 -0800870}
871
Ian Rogers62d6c772013-02-27 08:32:07 -0800872void Instrumentation::MethodEnterEventImpl(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700873 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800874 uint32_t dex_pc) const {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700875 auto it = method_entry_listeners_.begin();
Jeff Hao65d15d92013-07-16 16:39:33 -0700876 bool is_end = (it == method_entry_listeners_.end());
877 // Implemented this way to prevent problems caused by modification of the list while iterating.
878 while (!is_end) {
879 InstrumentationListener* cur = *it;
880 ++it;
881 is_end = (it == method_entry_listeners_.end());
882 cur->MethodEntered(thread, this_object, method, dex_pc);
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 {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700889 auto it = method_exit_listeners_.begin();
Jeff Hao65d15d92013-07-16 16:39:33 -0700890 bool is_end = (it == method_exit_listeners_.end());
891 // Implemented this way to prevent problems caused by modification of the list while iterating.
892 while (!is_end) {
893 InstrumentationListener* cur = *it;
894 ++it;
895 is_end = (it == method_exit_listeners_.end());
896 cur->MethodExited(thread, this_object, method, dex_pc, return_value);
Ian Rogers62d6c772013-02-27 08:32:07 -0800897 }
898}
899
900void Instrumentation::MethodUnwindEvent(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700901 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800902 uint32_t dex_pc) const {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200903 if (HasMethodUnwindListeners()) {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700904 for (InstrumentationListener* listener : method_unwind_listeners_) {
Sebastien Hertz51db44a2013-11-19 10:00:29 +0100905 listener->MethodUnwind(thread, this_object, method, dex_pc);
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 {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200913 std::shared_ptr<std::list<InstrumentationListener*>> original(dex_pc_listeners_);
914 for (InstrumentationListener* listener : *original.get()) {
915 listener->DexPcMoved(thread, this_object, method, dex_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800916 }
917}
918
Mathieu Chartiere401d142015-04-22 13:56:20 -0700919void Instrumentation::BackwardBranchImpl(Thread* thread, ArtMethod* method,
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800920 int32_t offset) const {
921 for (InstrumentationListener* listener : backward_branch_listeners_) {
922 listener->BackwardBranch(thread, method, offset);
923 }
924}
925
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100926void Instrumentation::InvokeVirtualOrInterfaceImpl(Thread* thread,
927 mirror::Object* this_object,
928 ArtMethod* caller,
929 uint32_t dex_pc,
930 ArtMethod* callee) const {
931 for (InstrumentationListener* listener : invoke_virtual_or_interface_listeners_) {
932 listener->InvokeVirtualOrInterface(thread, this_object, caller, dex_pc, callee);
933 }
934}
935
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200936void Instrumentation::FieldReadEventImpl(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700937 ArtMethod* method, uint32_t dex_pc,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700938 ArtField* field) const {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200939 std::shared_ptr<std::list<InstrumentationListener*>> original(field_read_listeners_);
940 for (InstrumentationListener* listener : *original.get()) {
941 listener->FieldRead(thread, this_object, method, dex_pc, field);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200942 }
943}
944
945void Instrumentation::FieldWriteEventImpl(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700946 ArtMethod* method, uint32_t dex_pc,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700947 ArtField* field, const JValue& field_value) const {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200948 std::shared_ptr<std::list<InstrumentationListener*>> original(field_write_listeners_);
949 for (InstrumentationListener* listener : *original.get()) {
950 listener->FieldWritten(thread, this_object, method, dex_pc, field, field_value);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200951 }
952}
953
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000954void Instrumentation::ExceptionCaughtEvent(Thread* thread,
Sebastien Hertz947ff082013-09-17 14:10:13 +0200955 mirror::Throwable* exception_object) const {
Sebastien Hertz9f102032014-05-23 08:59:42 +0200956 if (HasExceptionCaughtListeners()) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000957 DCHECK_EQ(thread->GetException(), exception_object);
Jeff Haoc0bd4da2013-04-11 15:52:28 -0700958 thread->ClearException();
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200959 std::shared_ptr<std::list<InstrumentationListener*>> original(exception_caught_listeners_);
960 for (InstrumentationListener* listener : *original.get()) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000961 listener->ExceptionCaught(thread, exception_object);
Ian Rogers62d6c772013-02-27 08:32:07 -0800962 }
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000963 thread->SetException(exception_object);
Ian Rogers62d6c772013-02-27 08:32:07 -0800964 }
965}
966
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +0000967// Computes a frame ID by ignoring inlined frames.
968size_t Instrumentation::ComputeFrameId(Thread* self,
969 size_t frame_depth,
970 size_t inlined_frames_before_frame) {
971 CHECK_GE(frame_depth, inlined_frames_before_frame);
972 size_t no_inline_depth = frame_depth - inlined_frames_before_frame;
973 return StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk) - no_inline_depth;
974}
975
Ian Rogers62d6c772013-02-27 08:32:07 -0800976static void CheckStackDepth(Thread* self, const InstrumentationStackFrame& instrumentation_frame,
977 int delta)
Mathieu Chartier90443472015-07-16 20:32:27 -0700978 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100979 size_t frame_id = StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk) + delta;
Ian Rogers62d6c772013-02-27 08:32:07 -0800980 if (frame_id != instrumentation_frame.frame_id_) {
981 LOG(ERROR) << "Expected frame_id=" << frame_id << " but found "
982 << instrumentation_frame.frame_id_;
983 StackVisitor::DescribeStack(self);
984 CHECK_EQ(frame_id, instrumentation_frame.frame_id_);
985 }
986}
987
988void Instrumentation::PushInstrumentationStackFrame(Thread* self, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700989 ArtMethod* method,
Jeff Hao9a916d32013-06-27 18:45:37 -0700990 uintptr_t lr, bool interpreter_entry) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800991 // We have a callee-save frame meaning this value is guaranteed to never be 0.
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100992 size_t frame_id = StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk);
Ian Rogers62d6c772013-02-27 08:32:07 -0800993 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
994 if (kVerboseInstrumentation) {
Brian Carlstrom2d888622013-07-18 17:02:00 -0700995 LOG(INFO) << "Entering " << PrettyMethod(method) << " from PC " << reinterpret_cast<void*>(lr);
Ian Rogers62d6c772013-02-27 08:32:07 -0800996 }
997 instrumentation::InstrumentationStackFrame instrumentation_frame(this_object, method, lr,
Jeff Hao9a916d32013-06-27 18:45:37 -0700998 frame_id, interpreter_entry);
Ian Rogers62d6c772013-02-27 08:32:07 -0800999 stack->push_front(instrumentation_frame);
1000
Sebastien Hertz320deb22014-06-11 19:45:05 +02001001 if (!interpreter_entry) {
1002 MethodEnterEvent(self, this_object, method, 0);
1003 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001004}
1005
Andreas Gamped58342c2014-06-05 14:18:08 -07001006TwoWordReturn Instrumentation::PopInstrumentationStackFrame(Thread* self, uintptr_t* return_pc,
1007 uint64_t gpr_result,
1008 uint64_t fpr_result) {
Ian Rogers62d6c772013-02-27 08:32:07 -08001009 // Do the pop.
1010 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
1011 CHECK_GT(stack->size(), 0U);
1012 InstrumentationStackFrame instrumentation_frame = stack->front();
1013 stack->pop_front();
1014
1015 // Set return PC and check the sanity of the stack.
1016 *return_pc = instrumentation_frame.return_pc_;
1017 CheckStackDepth(self, instrumentation_frame, 0);
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001018 self->VerifyStack();
Ian Rogers62d6c772013-02-27 08:32:07 -08001019
Mathieu Chartiere401d142015-04-22 13:56:20 -07001020 ArtMethod* method = instrumentation_frame.method_;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001021 uint32_t length;
Mingyao Yang99170c62015-07-06 11:10:37 -07001022 const size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
1023 char return_shorty = method->GetInterfaceMethodIfProxy(pointer_size)->GetShorty(&length)[0];
Ian Rogers62d6c772013-02-27 08:32:07 -08001024 JValue return_value;
1025 if (return_shorty == 'V') {
1026 return_value.SetJ(0);
1027 } else if (return_shorty == 'F' || return_shorty == 'D') {
1028 return_value.SetJ(fpr_result);
1029 } else {
1030 return_value.SetJ(gpr_result);
1031 }
1032 // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to
1033 // return_pc.
1034 uint32_t dex_pc = DexFile::kDexNoIndex;
1035 mirror::Object* this_object = instrumentation_frame.this_object_;
Sebastien Hertz320deb22014-06-11 19:45:05 +02001036 if (!instrumentation_frame.interpreter_entry_) {
1037 MethodExitEvent(self, this_object, instrumentation_frame.method_, dex_pc, return_value);
1038 }
jeffhao725a9572012-11-13 18:20:12 -08001039
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01001040 // Deoptimize if the caller needs to continue execution in the interpreter. Do nothing if we get
1041 // back to an upcall.
1042 NthCallerVisitor visitor(self, 1, true);
1043 visitor.WalkStack(true);
Sebastien Hertz270a0e12015-01-16 19:49:09 +01001044 bool deoptimize = (visitor.caller != nullptr) &&
Daniel Mihalyieb076692014-08-22 17:33:31 +02001045 (interpreter_stubs_installed_ || IsDeoptimized(visitor.caller) ||
1046 Dbg::IsForcedInterpreterNeededForUpcall(self, visitor.caller));
Ian Rogers62d6c772013-02-27 08:32:07 -08001047 if (deoptimize) {
1048 if (kVerboseInstrumentation) {
Sebastien Hertz270a0e12015-01-16 19:49:09 +01001049 LOG(INFO) << StringPrintf("Deoptimizing %s by returning from %s with result %#" PRIx64 " in ",
1050 PrettyMethod(visitor.caller).c_str(),
1051 PrettyMethod(method).c_str(),
1052 return_value.GetJ()) << *self;
Ian Rogers62d6c772013-02-27 08:32:07 -08001053 }
Sebastien Hertz07474662015-08-25 15:12:33 +00001054 self->PushDeoptimizationContext(return_value, return_shorty == 'L',
1055 nullptr /* no pending exception */);
Andreas Gamped58342c2014-06-05 14:18:08 -07001056 return GetTwoWordSuccessValue(*return_pc,
1057 reinterpret_cast<uintptr_t>(GetQuickDeoptimizationEntryPoint()));
Ian Rogers62d6c772013-02-27 08:32:07 -08001058 } else {
1059 if (kVerboseInstrumentation) {
Brian Carlstrom2d888622013-07-18 17:02:00 -07001060 LOG(INFO) << "Returning from " << PrettyMethod(method)
1061 << " to PC " << reinterpret_cast<void*>(*return_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -08001062 }
Andreas Gamped58342c2014-06-05 14:18:08 -07001063 return GetTwoWordSuccessValue(0, *return_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -08001064 }
jeffhao725a9572012-11-13 18:20:12 -08001065}
1066
Ian Rogers62d6c772013-02-27 08:32:07 -08001067void Instrumentation::PopMethodForUnwind(Thread* self, bool is_deoptimization) const {
1068 // Do the pop.
1069 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
1070 CHECK_GT(stack->size(), 0U);
1071 InstrumentationStackFrame instrumentation_frame = stack->front();
1072 // TODO: bring back CheckStackDepth(self, instrumentation_frame, 2);
1073 stack->pop_front();
1074
Mathieu Chartiere401d142015-04-22 13:56:20 -07001075 ArtMethod* method = instrumentation_frame.method_;
Ian Rogers62d6c772013-02-27 08:32:07 -08001076 if (is_deoptimization) {
1077 if (kVerboseInstrumentation) {
1078 LOG(INFO) << "Popping for deoptimization " << PrettyMethod(method);
1079 }
1080 } else {
1081 if (kVerboseInstrumentation) {
1082 LOG(INFO) << "Popping for unwind " << PrettyMethod(method);
1083 }
1084
1085 // Notify listeners of method unwind.
1086 // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to
1087 // return_pc.
1088 uint32_t dex_pc = DexFile::kDexNoIndex;
1089 MethodUnwindEvent(self, instrumentation_frame.this_object_, method, dex_pc);
1090 }
1091}
1092
1093std::string InstrumentationStackFrame::Dump() const {
1094 std::ostringstream os;
1095 os << "Frame " << frame_id_ << " " << PrettyMethod(method_) << ":"
1096 << reinterpret_cast<void*>(return_pc_) << " this=" << reinterpret_cast<void*>(this_object_);
1097 return os.str();
1098}
1099
1100} // namespace instrumentation
jeffhao725a9572012-11-13 18:20:12 -08001101} // namespace art