blob: 7e2a84df2c482571627273e2b0c03ac588f58971 [file] [log] [blame]
jeffhao725a9572012-11-13 18:20:12 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "instrumentation.h"
18
Ian Rogersc7dd2952014-10-21 23:31:19 -070019#include <sstream>
20
Ian Rogerse63db272014-07-15 15:36:11 -070021#include "arch/context.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070022#include "art_method-inl.h"
Ian Rogersef7d42f2014-01-06 12:55:46 -080023#include "atomic.h"
jeffhao725a9572012-11-13 18:20:12 -080024#include "class_linker.h"
25#include "debugger.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080026#include "dex_file-inl.h"
Ian Rogersc7dd2952014-10-21 23:31:19 -070027#include "entrypoints/quick/quick_entrypoints.h"
Mathieu Chartierd8891782014-03-02 13:28:37 -080028#include "entrypoints/quick/quick_alloc_entrypoints.h"
Ian Rogers6f3dbba2014-10-14 17:41:57 -070029#include "entrypoints/runtime_asm_entrypoints.h"
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070030#include "gc_root-inl.h"
Sebastien Hertz138dbfc2013-12-04 18:15:25 +010031#include "interpreter/interpreter.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080032#include "jit/jit.h"
33#include "jit/jit_code_cache.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080034#include "mirror/class-inl.h"
35#include "mirror/dex_cache.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080036#include "mirror/object_array-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070037#include "mirror/object-inl.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080038#include "nth_caller_visitor.h"
jeffhao725a9572012-11-13 18:20:12 -080039#include "thread.h"
40#include "thread_list.h"
jeffhao725a9572012-11-13 18:20:12 -080041
42namespace art {
Ian Rogers62d6c772013-02-27 08:32:07 -080043namespace instrumentation {
jeffhao725a9572012-11-13 18:20:12 -080044
Sebastien Hertz0462c4c2015-04-01 16:34:17 +020045constexpr bool kVerboseInstrumentation = false;
Sebastien Hertz5bfd5c92013-11-15 11:36:07 +010046
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +010047// Instrumentation works on non-inlined frames by updating returned PCs
48// of compiled frames.
49static constexpr StackVisitor::StackWalkKind kInstrumentationStackWalk =
50 StackVisitor::StackWalkKind::kSkipInlinedFrames;
51
Mathieu Chartiere0671ce2015-07-28 17:23:28 -070052class InstallStubsClassVisitor : public ClassVisitor {
53 public:
54 explicit InstallStubsClassVisitor(Instrumentation* instrumentation)
55 : instrumentation_(instrumentation) {}
56
57 bool Visit(mirror::Class* klass) OVERRIDE REQUIRES(Locks::mutator_lock_) {
58 instrumentation_->InstallStubsForClass(klass);
59 return true; // we visit all classes.
60 }
61
62 private:
63 Instrumentation* const instrumentation_;
64};
65
Ian Rogers62d6c772013-02-27 08:32:07 -080066
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -070067Instrumentation::Instrumentation()
68 : instrumentation_stubs_installed_(false), entry_exit_stubs_installed_(false),
69 interpreter_stubs_installed_(false),
70 interpret_only_(false), forced_interpret_only_(false),
71 have_method_entry_listeners_(false), have_method_exit_listeners_(false),
72 have_method_unwind_listeners_(false), have_dex_pc_listeners_(false),
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +020073 have_field_read_listeners_(false), have_field_write_listeners_(false),
Sebastien Hertz0462c4c2015-04-01 16:34:17 +020074 have_exception_caught_listeners_(false), have_backward_branch_listeners_(false),
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -070075 deoptimized_methods_lock_("deoptimized methods lock"),
76 deoptimization_enabled_(false),
77 interpreter_handler_table_(kMainHandlerTable),
78 quick_alloc_entry_points_instrumentation_counter_(0) {
79}
80
Sebastien Hertza10aa372015-01-21 17:30:58 +010081void Instrumentation::InstallStubsForClass(mirror::Class* klass) {
Sebastien Hertza8a697f2015-01-15 12:28:47 +010082 if (klass->IsErroneous()) {
83 // We can't execute code in a erroneous class: do nothing.
84 } else if (!klass->IsResolved()) {
85 // We need the class to be resolved to install/uninstall stubs. Otherwise its methods
86 // could not be initialized or linked with regards to class inheritance.
87 } else {
88 for (size_t i = 0, e = klass->NumDirectMethods(); i < e; i++) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070089 InstallStubsForMethod(klass->GetDirectMethod(i, sizeof(void*)));
Sebastien Hertza8a697f2015-01-15 12:28:47 +010090 }
91 for (size_t i = 0, e = klass->NumVirtualMethods(); i < e; i++) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070092 InstallStubsForMethod(klass->GetVirtualMethod(i, sizeof(void*)));
Sebastien Hertza8a697f2015-01-15 12:28:47 +010093 }
jeffhao725a9572012-11-13 18:20:12 -080094 }
jeffhao725a9572012-11-13 18:20:12 -080095}
96
Mathieu Chartiere401d142015-04-22 13:56:20 -070097static void UpdateEntrypoints(ArtMethod* method, const void* quick_code)
Mathieu Chartier90443472015-07-16 20:32:27 -070098 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080099 Runtime* const runtime = Runtime::Current();
100 jit::Jit* jit = runtime->GetJit();
101 if (jit != nullptr) {
102 const void* old_code_ptr = method->GetEntryPointFromQuickCompiledCode();
103 jit::JitCodeCache* code_cache = jit->GetCodeCache();
104 if (code_cache->ContainsCodePtr(old_code_ptr)) {
105 // Save the old compiled code since we need it to implement ClassLinker::GetQuickOatCodeFor.
106 code_cache->SaveCompiledCode(method, old_code_ptr);
107 }
108 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800109 method->SetEntryPointFromQuickCompiledCode(quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100110}
111
Mathieu Chartiere401d142015-04-22 13:56:20 -0700112void Instrumentation::InstallStubsForMethod(ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100113 if (method->IsAbstract() || method->IsProxyMethod()) {
114 // Do not change stubs for these methods.
115 return;
116 }
Jeff Hao56802772014-08-19 10:17:36 -0700117 // Don't stub Proxy.<init>. Note that the Proxy class itself is not a proxy class.
118 if (method->IsConstructor() &&
119 method->GetDeclaringClass()->DescriptorEquals("Ljava/lang/reflect/Proxy;")) {
Jeff Haodb8a6642014-08-14 17:18:52 -0700120 return;
121 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800122 const void* new_quick_code;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100123 bool uninstall = !entry_exit_stubs_installed_ && !interpreter_stubs_installed_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800124 Runtime* const runtime = Runtime::Current();
125 ClassLinker* const class_linker = runtime->GetClassLinker();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100126 bool is_class_initialized = method->GetDeclaringClass()->IsInitialized();
127 if (uninstall) {
128 if ((forced_interpret_only_ || IsDeoptimized(method)) && !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800129 new_quick_code = GetQuickToInterpreterBridge();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100130 } else if (is_class_initialized || !method->IsStatic() || method->IsConstructor()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800131 new_quick_code = class_linker->GetQuickOatCodeFor(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100132 } else {
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700133 new_quick_code = GetQuickResolutionStub();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100134 }
135 } else { // !uninstall
Sebastien Hertzbae182c2013-12-17 10:42:03 +0100136 if ((interpreter_stubs_installed_ || forced_interpret_only_ || IsDeoptimized(method)) &&
137 !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800138 new_quick_code = GetQuickToInterpreterBridge();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100139 } else {
140 // Do not overwrite resolution trampoline. When the trampoline initializes the method's
141 // class, all its static methods code will be set to the instrumentation entry point.
142 // For more details, see ClassLinker::FixupStaticTrampolines.
143 if (is_class_initialized || !method->IsStatic() || method->IsConstructor()) {
Sebastien Hertz320deb22014-06-11 19:45:05 +0200144 if (entry_exit_stubs_installed_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800145 new_quick_code = GetQuickInstrumentationEntryPoint();
Sebastien Hertz320deb22014-06-11 19:45:05 +0200146 } else {
Sebastien Hertz320deb22014-06-11 19:45:05 +0200147 new_quick_code = class_linker->GetQuickOatCodeFor(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100148 }
149 } else {
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700150 new_quick_code = GetQuickResolutionStub();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100151 }
152 }
153 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800154 UpdateEntrypoints(method, new_quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100155}
156
Ian Rogers62d6c772013-02-27 08:32:07 -0800157// Places the instrumentation exit pc as the return PC for every quick frame. This also allows
158// deoptimization of quick frames to interpreter frames.
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100159// Since we may already have done this previously, we need to push new instrumentation frame before
160// existing instrumentation frames.
Ian Rogers62d6c772013-02-27 08:32:07 -0800161static void InstrumentationInstallStack(Thread* thread, void* arg)
Mathieu Chartier90443472015-07-16 20:32:27 -0700162 SHARED_REQUIRES(Locks::mutator_lock_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200163 struct InstallStackVisitor FINAL : public StackVisitor {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800164 InstallStackVisitor(Thread* thread_in, Context* context, uintptr_t instrumentation_exit_pc)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100165 : StackVisitor(thread_in, context, kInstrumentationStackWalk),
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800166 instrumentation_stack_(thread_in->GetInstrumentationStack()),
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100167 instrumentation_exit_pc_(instrumentation_exit_pc),
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100168 reached_existing_instrumentation_frames_(false), instrumentation_stack_depth_(0),
169 last_return_pc_(0) {
170 }
jeffhao725a9572012-11-13 18:20:12 -0800171
Mathieu Chartier90443472015-07-16 20:32:27 -0700172 bool VisitFrame() OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700173 ArtMethod* m = GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700174 if (m == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800175 if (kVerboseInstrumentation) {
176 LOG(INFO) << " Skipping upcall. Frame " << GetFrameId();
177 }
178 last_return_pc_ = 0;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700179 return true; // Ignore upcalls.
Ian Rogers306057f2012-11-26 12:45:53 -0800180 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700181 if (GetCurrentQuickFrame() == nullptr) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800182 bool interpreter_frame = true;
Sebastien Hertz320deb22014-06-11 19:45:05 +0200183 InstrumentationStackFrame instrumentation_frame(GetThisObject(), m, 0, GetFrameId(),
184 interpreter_frame);
Jeff Haoa15a81b2014-05-27 18:25:47 -0700185 if (kVerboseInstrumentation) {
186 LOG(INFO) << "Pushing shadow frame " << instrumentation_frame.Dump();
187 }
188 shadow_stack_.push_back(instrumentation_frame);
189 return true; // Continue.
190 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800191 uintptr_t return_pc = GetReturnPc();
Sebastien Hertz320deb22014-06-11 19:45:05 +0200192 if (m->IsRuntimeMethod()) {
193 if (return_pc == instrumentation_exit_pc_) {
194 if (kVerboseInstrumentation) {
195 LOG(INFO) << " Handling quick to interpreter transition. Frame " << GetFrameId();
196 }
197 CHECK_LT(instrumentation_stack_depth_, instrumentation_stack_->size());
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200198 const InstrumentationStackFrame& frame =
199 instrumentation_stack_->at(instrumentation_stack_depth_);
Sebastien Hertz320deb22014-06-11 19:45:05 +0200200 CHECK(frame.interpreter_entry_);
201 // This is an interpreter frame so method enter event must have been reported. However we
202 // need to push a DEX pc into the dex_pcs_ list to match size of instrumentation stack.
203 // Since we won't report method entry here, we can safely push any DEX pc.
204 dex_pcs_.push_back(0);
205 last_return_pc_ = frame.return_pc_;
206 ++instrumentation_stack_depth_;
207 return true;
208 } else {
209 if (kVerboseInstrumentation) {
210 LOG(INFO) << " Skipping runtime method. Frame " << GetFrameId();
211 }
212 last_return_pc_ = GetReturnPc();
213 return true; // Ignore unresolved methods since they will be instrumented after resolution.
214 }
215 }
216 if (kVerboseInstrumentation) {
217 LOG(INFO) << " Installing exit stub in " << DescribeLocation();
218 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100219 if (return_pc == instrumentation_exit_pc_) {
220 // We've reached a frame which has already been installed with instrumentation exit stub.
221 // We should have already installed instrumentation on previous frames.
222 reached_existing_instrumentation_frames_ = true;
223
224 CHECK_LT(instrumentation_stack_depth_, instrumentation_stack_->size());
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200225 const InstrumentationStackFrame& frame =
226 instrumentation_stack_->at(instrumentation_stack_depth_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100227 CHECK_EQ(m, frame.method_) << "Expected " << PrettyMethod(m)
228 << ", Found " << PrettyMethod(frame.method_);
229 return_pc = frame.return_pc_;
230 if (kVerboseInstrumentation) {
231 LOG(INFO) << "Ignoring already instrumented " << frame.Dump();
232 }
233 } else {
234 CHECK_NE(return_pc, 0U);
235 CHECK(!reached_existing_instrumentation_frames_);
236 InstrumentationStackFrame instrumentation_frame(GetThisObject(), m, return_pc, GetFrameId(),
237 false);
238 if (kVerboseInstrumentation) {
239 LOG(INFO) << "Pushing frame " << instrumentation_frame.Dump();
240 }
241
Sebastien Hertz320deb22014-06-11 19:45:05 +0200242 // Insert frame at the right position so we do not corrupt the instrumentation stack.
243 // Instrumentation stack frames are in descending frame id order.
244 auto it = instrumentation_stack_->begin();
245 for (auto end = instrumentation_stack_->end(); it != end; ++it) {
246 const InstrumentationStackFrame& current = *it;
247 if (instrumentation_frame.frame_id_ >= current.frame_id_) {
248 break;
249 }
250 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100251 instrumentation_stack_->insert(it, instrumentation_frame);
252 SetReturnPc(instrumentation_exit_pc_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800253 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800254 dex_pcs_.push_back(m->ToDexPc(last_return_pc_));
Ian Rogers62d6c772013-02-27 08:32:07 -0800255 last_return_pc_ = return_pc;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100256 ++instrumentation_stack_depth_;
Ian Rogers306057f2012-11-26 12:45:53 -0800257 return true; // Continue.
258 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800259 std::deque<InstrumentationStackFrame>* const instrumentation_stack_;
Jeff Haoa15a81b2014-05-27 18:25:47 -0700260 std::vector<InstrumentationStackFrame> shadow_stack_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800261 std::vector<uint32_t> dex_pcs_;
Ian Rogers306057f2012-11-26 12:45:53 -0800262 const uintptr_t instrumentation_exit_pc_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100263 bool reached_existing_instrumentation_frames_;
264 size_t instrumentation_stack_depth_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800265 uintptr_t last_return_pc_;
Ian Rogers306057f2012-11-26 12:45:53 -0800266 };
Ian Rogers62d6c772013-02-27 08:32:07 -0800267 if (kVerboseInstrumentation) {
268 std::string thread_name;
269 thread->GetThreadName(thread_name);
270 LOG(INFO) << "Installing exit stubs in " << thread_name;
Ian Rogers306057f2012-11-26 12:45:53 -0800271 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100272
273 Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg);
Ian Rogers700a4022014-05-19 16:49:03 -0700274 std::unique_ptr<Context> context(Context::Create());
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700275 uintptr_t instrumentation_exit_pc = reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc());
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100276 InstallStackVisitor visitor(thread, context.get(), instrumentation_exit_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800277 visitor.WalkStack(true);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100278 CHECK_EQ(visitor.dex_pcs_.size(), thread->GetInstrumentationStack()->size());
Ian Rogers62d6c772013-02-27 08:32:07 -0800279
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100280 if (instrumentation->ShouldNotifyMethodEnterExitEvents()) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100281 // Create method enter events for all methods currently on the thread's stack. We only do this
282 // if no debugger is attached to prevent from posting events twice.
Jeff Haoa15a81b2014-05-27 18:25:47 -0700283 auto ssi = visitor.shadow_stack_.rbegin();
284 for (auto isi = thread->GetInstrumentationStack()->rbegin(),
285 end = thread->GetInstrumentationStack()->rend(); isi != end; ++isi) {
286 while (ssi != visitor.shadow_stack_.rend() && (*ssi).frame_id_ < (*isi).frame_id_) {
287 instrumentation->MethodEnterEvent(thread, (*ssi).this_object_, (*ssi).method_, 0);
288 ++ssi;
289 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100290 uint32_t dex_pc = visitor.dex_pcs_.back();
291 visitor.dex_pcs_.pop_back();
Sebastien Hertz320deb22014-06-11 19:45:05 +0200292 if (!isi->interpreter_entry_) {
293 instrumentation->MethodEnterEvent(thread, (*isi).this_object_, (*isi).method_, dex_pc);
294 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100295 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800296 }
297 thread->VerifyStack();
Ian Rogers306057f2012-11-26 12:45:53 -0800298}
299
Ian Rogers62d6c772013-02-27 08:32:07 -0800300// Removes the instrumentation exit pc as the return PC for every quick frame.
301static void InstrumentationRestoreStack(Thread* thread, void* arg)
Mathieu Chartier90443472015-07-16 20:32:27 -0700302 SHARED_REQUIRES(Locks::mutator_lock_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200303 struct RestoreStackVisitor FINAL : public StackVisitor {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800304 RestoreStackVisitor(Thread* thread_in, uintptr_t instrumentation_exit_pc,
Ian Rogers62d6c772013-02-27 08:32:07 -0800305 Instrumentation* instrumentation)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100306 : StackVisitor(thread_in, nullptr, kInstrumentationStackWalk),
307 thread_(thread_in),
Ian Rogers62d6c772013-02-27 08:32:07 -0800308 instrumentation_exit_pc_(instrumentation_exit_pc),
309 instrumentation_(instrumentation),
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800310 instrumentation_stack_(thread_in->GetInstrumentationStack()),
Ian Rogers62d6c772013-02-27 08:32:07 -0800311 frames_removed_(0) {}
Ian Rogers306057f2012-11-26 12:45:53 -0800312
Mathieu Chartier90443472015-07-16 20:32:27 -0700313 bool VisitFrame() OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800314 if (instrumentation_stack_->size() == 0) {
jeffhao725a9572012-11-13 18:20:12 -0800315 return false; // Stop.
316 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700317 ArtMethod* m = GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700318 if (GetCurrentQuickFrame() == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800319 if (kVerboseInstrumentation) {
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200320 LOG(INFO) << " Ignoring a shadow frame. Frame " << GetFrameId()
321 << " Method=" << PrettyMethod(m);
Ian Rogers62d6c772013-02-27 08:32:07 -0800322 }
323 return true; // Ignore shadow frames.
324 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700325 if (m == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800326 if (kVerboseInstrumentation) {
327 LOG(INFO) << " Skipping upcall. Frame " << GetFrameId();
328 }
Ian Rogers306057f2012-11-26 12:45:53 -0800329 return true; // Ignore upcalls.
330 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800331 bool removed_stub = false;
332 // TODO: make this search more efficient?
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100333 const size_t frameId = GetFrameId();
334 for (const InstrumentationStackFrame& instrumentation_frame : *instrumentation_stack_) {
335 if (instrumentation_frame.frame_id_ == frameId) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800336 if (kVerboseInstrumentation) {
337 LOG(INFO) << " Removing exit stub in " << DescribeLocation();
338 }
Jeff Hao9a916d32013-06-27 18:45:37 -0700339 if (instrumentation_frame.interpreter_entry_) {
340 CHECK(m == Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs));
341 } else {
342 CHECK(m == instrumentation_frame.method_) << PrettyMethod(m);
343 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800344 SetReturnPc(instrumentation_frame.return_pc_);
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100345 if (instrumentation_->ShouldNotifyMethodEnterExitEvents()) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100346 // Create the method exit events. As the methods didn't really exit the result is 0.
347 // We only do this if no debugger is attached to prevent from posting events twice.
348 instrumentation_->MethodExitEvent(thread_, instrumentation_frame.this_object_, m,
349 GetDexPc(), JValue());
350 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800351 frames_removed_++;
352 removed_stub = true;
353 break;
354 }
355 }
356 if (!removed_stub) {
357 if (kVerboseInstrumentation) {
358 LOG(INFO) << " No exit stub in " << DescribeLocation();
Ian Rogers306057f2012-11-26 12:45:53 -0800359 }
jeffhao725a9572012-11-13 18:20:12 -0800360 }
361 return true; // Continue.
362 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800363 Thread* const thread_;
Ian Rogers306057f2012-11-26 12:45:53 -0800364 const uintptr_t instrumentation_exit_pc_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800365 Instrumentation* const instrumentation_;
366 std::deque<instrumentation::InstrumentationStackFrame>* const instrumentation_stack_;
367 size_t frames_removed_;
jeffhao725a9572012-11-13 18:20:12 -0800368 };
Ian Rogers62d6c772013-02-27 08:32:07 -0800369 if (kVerboseInstrumentation) {
370 std::string thread_name;
371 thread->GetThreadName(thread_name);
372 LOG(INFO) << "Removing exit stubs in " << thread_name;
373 }
374 std::deque<instrumentation::InstrumentationStackFrame>* stack = thread->GetInstrumentationStack();
375 if (stack->size() > 0) {
376 Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700377 uintptr_t instrumentation_exit_pc =
378 reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc());
Ian Rogers62d6c772013-02-27 08:32:07 -0800379 RestoreStackVisitor visitor(thread, instrumentation_exit_pc, instrumentation);
380 visitor.WalkStack(true);
381 CHECK_EQ(visitor.frames_removed_, stack->size());
382 while (stack->size() > 0) {
383 stack->pop_front();
384 }
jeffhao725a9572012-11-13 18:20:12 -0800385 }
386}
387
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200388static bool HasEvent(Instrumentation::InstrumentationEvent expected, uint32_t events) {
389 return (events & expected) != 0;
390}
391
Ian Rogers62d6c772013-02-27 08:32:07 -0800392void Instrumentation::AddListener(InstrumentationListener* listener, uint32_t events) {
393 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200394 if (HasEvent(kMethodEntered, events)) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800395 method_entry_listeners_.push_back(listener);
Ian Rogers62d6c772013-02-27 08:32:07 -0800396 have_method_entry_listeners_ = true;
397 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200398 if (HasEvent(kMethodExited, events)) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800399 method_exit_listeners_.push_back(listener);
Ian Rogers62d6c772013-02-27 08:32:07 -0800400 have_method_exit_listeners_ = true;
401 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200402 if (HasEvent(kMethodUnwind, events)) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800403 method_unwind_listeners_.push_back(listener);
404 have_method_unwind_listeners_ = true;
405 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200406 if (HasEvent(kBackwardBranch, events)) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800407 backward_branch_listeners_.push_back(listener);
408 have_backward_branch_listeners_ = true;
409 }
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100410 if (HasEvent(kInvokeVirtualOrInterface, events)) {
411 invoke_virtual_or_interface_listeners_.push_back(listener);
412 have_invoke_virtual_or_interface_listeners_ = true;
413 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200414 if (HasEvent(kDexPcMoved, events)) {
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200415 std::list<InstrumentationListener*>* modified;
416 if (have_dex_pc_listeners_) {
417 modified = new std::list<InstrumentationListener*>(*dex_pc_listeners_.get());
418 } else {
419 modified = new std::list<InstrumentationListener*>();
420 }
421 modified->push_back(listener);
422 dex_pc_listeners_.reset(modified);
Ian Rogers62d6c772013-02-27 08:32:07 -0800423 have_dex_pc_listeners_ = true;
424 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200425 if (HasEvent(kFieldRead, events)) {
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200426 std::list<InstrumentationListener*>* modified;
427 if (have_field_read_listeners_) {
428 modified = new std::list<InstrumentationListener*>(*field_read_listeners_.get());
429 } else {
430 modified = new std::list<InstrumentationListener*>();
431 }
432 modified->push_back(listener);
433 field_read_listeners_.reset(modified);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200434 have_field_read_listeners_ = true;
435 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200436 if (HasEvent(kFieldWritten, events)) {
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200437 std::list<InstrumentationListener*>* modified;
438 if (have_field_write_listeners_) {
439 modified = new std::list<InstrumentationListener*>(*field_write_listeners_.get());
440 } else {
441 modified = new std::list<InstrumentationListener*>();
442 }
443 modified->push_back(listener);
444 field_write_listeners_.reset(modified);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200445 have_field_write_listeners_ = true;
446 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200447 if (HasEvent(kExceptionCaught, events)) {
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200448 std::list<InstrumentationListener*>* modified;
449 if (have_exception_caught_listeners_) {
450 modified = new std::list<InstrumentationListener*>(*exception_caught_listeners_.get());
451 } else {
452 modified = new std::list<InstrumentationListener*>();
453 }
454 modified->push_back(listener);
455 exception_caught_listeners_.reset(modified);
Jeff Hao14dd5a82013-04-11 10:23:36 -0700456 have_exception_caught_listeners_ = true;
457 }
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200458 UpdateInterpreterHandlerTable();
jeffhao725a9572012-11-13 18:20:12 -0800459}
460
Ian Rogers62d6c772013-02-27 08:32:07 -0800461void Instrumentation::RemoveListener(InstrumentationListener* listener, uint32_t events) {
462 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
Ian Rogers62d6c772013-02-27 08:32:07 -0800463
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200464 if (HasEvent(kMethodEntered, events) && have_method_entry_listeners_) {
465 method_entry_listeners_.remove(listener);
466 have_method_entry_listeners_ = !method_entry_listeners_.empty();
Ian Rogers62d6c772013-02-27 08:32:07 -0800467 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200468 if (HasEvent(kMethodExited, events) && have_method_exit_listeners_) {
469 method_exit_listeners_.remove(listener);
470 have_method_exit_listeners_ = !method_exit_listeners_.empty();
Ian Rogers62d6c772013-02-27 08:32:07 -0800471 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200472 if (HasEvent(kMethodUnwind, events) && have_method_unwind_listeners_) {
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100473 method_unwind_listeners_.remove(listener);
474 have_method_unwind_listeners_ = !method_unwind_listeners_.empty();
Ian Rogers62d6c772013-02-27 08:32:07 -0800475 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200476 if (HasEvent(kBackwardBranch, events) && have_backward_branch_listeners_) {
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100477 backward_branch_listeners_.remove(listener);
478 have_backward_branch_listeners_ = !backward_branch_listeners_.empty();
479 }
480 if (HasEvent(kInvokeVirtualOrInterface, events) && have_invoke_virtual_or_interface_listeners_) {
481 invoke_virtual_or_interface_listeners_.remove(listener);
482 have_invoke_virtual_or_interface_listeners_ = !invoke_virtual_or_interface_listeners_.empty();
483 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200484 if (HasEvent(kDexPcMoved, events) && have_dex_pc_listeners_) {
485 std::list<InstrumentationListener*>* modified =
486 new std::list<InstrumentationListener*>(*dex_pc_listeners_.get());
487 modified->remove(listener);
488 have_dex_pc_listeners_ = !modified->empty();
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200489 if (have_dex_pc_listeners_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200490 dex_pc_listeners_.reset(modified);
491 } else {
492 dex_pc_listeners_.reset();
493 delete modified;
Ian Rogers62d6c772013-02-27 08:32:07 -0800494 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800495 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200496 if (HasEvent(kFieldRead, events) && have_field_read_listeners_) {
497 std::list<InstrumentationListener*>* modified =
498 new std::list<InstrumentationListener*>(*field_read_listeners_.get());
499 modified->remove(listener);
500 have_field_read_listeners_ = !modified->empty();
Daniel Mihalyi66445212014-08-21 15:57:25 +0200501 if (have_field_read_listeners_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200502 field_read_listeners_.reset(modified);
503 } else {
504 field_read_listeners_.reset();
505 delete modified;
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200506 }
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200507 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200508 if (HasEvent(kFieldWritten, events) && have_field_write_listeners_) {
509 std::list<InstrumentationListener*>* modified =
510 new std::list<InstrumentationListener*>(*field_write_listeners_.get());
511 modified->remove(listener);
512 have_field_write_listeners_ = !modified->empty();
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200513 if (have_field_write_listeners_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200514 field_write_listeners_.reset(modified);
515 } else {
516 field_write_listeners_.reset();
517 delete modified;
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200518 }
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200519 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200520 if (HasEvent(kExceptionCaught, events) && have_exception_caught_listeners_) {
521 std::list<InstrumentationListener*>* modified =
522 new std::list<InstrumentationListener*>(*exception_caught_listeners_.get());
523 modified->remove(listener);
524 have_exception_caught_listeners_ = !modified->empty();
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200525 if (have_exception_caught_listeners_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200526 exception_caught_listeners_.reset(modified);
527 } else {
528 exception_caught_listeners_.reset();
529 delete modified;
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200530 }
Jeff Hao14dd5a82013-04-11 10:23:36 -0700531 }
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200532 UpdateInterpreterHandlerTable();
jeffhao725a9572012-11-13 18:20:12 -0800533}
534
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200535Instrumentation::InstrumentationLevel Instrumentation::GetCurrentInstrumentationLevel() const {
Ian Rogers62d6c772013-02-27 08:32:07 -0800536 if (interpreter_stubs_installed_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200537 return InstrumentationLevel::kInstrumentWithInterpreter;
Ian Rogers62d6c772013-02-27 08:32:07 -0800538 } else if (entry_exit_stubs_installed_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200539 return InstrumentationLevel::kInstrumentWithInstrumentationStubs;
Ian Rogers62d6c772013-02-27 08:32:07 -0800540 } else {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200541 return InstrumentationLevel::kInstrumentNothing;
Ian Rogers62d6c772013-02-27 08:32:07 -0800542 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200543}
544
545void Instrumentation::ConfigureStubs(const char* key, InstrumentationLevel desired_level) {
546 // Store the instrumentation level for this key or remove it.
547 if (desired_level == InstrumentationLevel::kInstrumentNothing) {
548 // The client no longer needs instrumentation.
549 requested_instrumentation_levels_.erase(key);
550 } else {
551 // The client needs instrumentation.
552 requested_instrumentation_levels_.Overwrite(key, desired_level);
553 }
554
555 // Look for the highest required instrumentation level.
556 InstrumentationLevel requested_level = InstrumentationLevel::kInstrumentNothing;
557 for (const auto& v : requested_instrumentation_levels_) {
558 requested_level = std::max(requested_level, v.second);
559 }
560
561 interpret_only_ = (requested_level == InstrumentationLevel::kInstrumentWithInterpreter) ||
562 forced_interpret_only_;
563
564 InstrumentationLevel current_level = GetCurrentInstrumentationLevel();
565 if (requested_level == current_level) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800566 // We're already set.
567 return;
568 }
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100569 Thread* const self = Thread::Current();
Ian Rogers62d6c772013-02-27 08:32:07 -0800570 Runtime* runtime = Runtime::Current();
Sebastien Hertza8a697f2015-01-15 12:28:47 +0100571 Locks::mutator_lock_->AssertExclusiveHeld(self);
Ian Rogers62d6c772013-02-27 08:32:07 -0800572 Locks::thread_list_lock_->AssertNotHeld(self);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200573 if (requested_level > InstrumentationLevel::kInstrumentNothing) {
574 if (requested_level == InstrumentationLevel::kInstrumentWithInterpreter) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800575 interpreter_stubs_installed_ = true;
Ian Rogers62d6c772013-02-27 08:32:07 -0800576 entry_exit_stubs_installed_ = true;
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200577 } else {
578 CHECK_EQ(requested_level, InstrumentationLevel::kInstrumentWithInstrumentationStubs);
579 entry_exit_stubs_installed_ = true;
580 interpreter_stubs_installed_ = false;
Ian Rogers62d6c772013-02-27 08:32:07 -0800581 }
Mathieu Chartiere0671ce2015-07-28 17:23:28 -0700582 InstallStubsClassVisitor visitor(this);
583 runtime->GetClassLinker()->VisitClasses(&visitor);
Ian Rogers62d6c772013-02-27 08:32:07 -0800584 instrumentation_stubs_installed_ = true;
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100585 MutexLock mu(self, *Locks::thread_list_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800586 runtime->GetThreadList()->ForEach(InstrumentationInstallStack, this);
587 } else {
588 interpreter_stubs_installed_ = false;
589 entry_exit_stubs_installed_ = false;
Mathieu Chartiere0671ce2015-07-28 17:23:28 -0700590 InstallStubsClassVisitor visitor(this);
591 runtime->GetClassLinker()->VisitClasses(&visitor);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100592 // Restore stack only if there is no method currently deoptimized.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700593 bool empty;
594 {
595 ReaderMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700596 empty = IsDeoptimizedMethodsEmpty(); // Avoid lock violation.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700597 }
598 if (empty) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100599 instrumentation_stubs_installed_ = false;
600 MutexLock mu(self, *Locks::thread_list_lock_);
601 Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this);
602 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800603 }
jeffhao725a9572012-11-13 18:20:12 -0800604}
605
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200606static void ResetQuickAllocEntryPointsForThread(Thread* thread, void* arg ATTRIBUTE_UNUSED) {
Ian Rogersfa824272013-11-05 16:12:57 -0800607 thread->ResetQuickAllocEntryPointsForThread();
608}
609
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700610void Instrumentation::SetEntrypointsInstrumented(bool instrumented) {
611 Thread* self = Thread::Current();
Mathieu Chartier661974a2014-01-09 11:23:53 -0800612 Runtime* runtime = Runtime::Current();
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700613 Locks::mutator_lock_->AssertNotHeld(self);
614 Locks::instrument_entrypoints_lock_->AssertHeld(self);
615 if (runtime->IsStarted()) {
Mathieu Chartier4f55e222015-09-04 13:26:21 -0700616 ScopedSuspendAll ssa(__FUNCTION__);
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700617 MutexLock mu(self, *Locks::runtime_shutdown_lock_);
Mathieu Chartier661974a2014-01-09 11:23:53 -0800618 SetQuickAllocEntryPointsInstrumented(instrumented);
619 ResetQuickAllocEntryPoints();
Mathieu Chartier4f55e222015-09-04 13:26:21 -0700620 } else {
621 MutexLock mu(self, *Locks::runtime_shutdown_lock_);
622 SetQuickAllocEntryPointsInstrumented(instrumented);
623 ResetQuickAllocEntryPoints();
Mathieu Chartier661974a2014-01-09 11:23:53 -0800624 }
625}
626
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700627void Instrumentation::InstrumentQuickAllocEntryPoints() {
628 MutexLock mu(Thread::Current(), *Locks::instrument_entrypoints_lock_);
629 InstrumentQuickAllocEntryPointsLocked();
Ian Rogersfa824272013-11-05 16:12:57 -0800630}
631
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700632void Instrumentation::UninstrumentQuickAllocEntryPoints() {
633 MutexLock mu(Thread::Current(), *Locks::instrument_entrypoints_lock_);
634 UninstrumentQuickAllocEntryPointsLocked();
635}
636
637void Instrumentation::InstrumentQuickAllocEntryPointsLocked() {
638 Locks::instrument_entrypoints_lock_->AssertHeld(Thread::Current());
639 if (quick_alloc_entry_points_instrumentation_counter_ == 0) {
640 SetEntrypointsInstrumented(true);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800641 }
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700642 ++quick_alloc_entry_points_instrumentation_counter_;
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700643}
644
645void Instrumentation::UninstrumentQuickAllocEntryPointsLocked() {
646 Locks::instrument_entrypoints_lock_->AssertHeld(Thread::Current());
647 CHECK_GT(quick_alloc_entry_points_instrumentation_counter_, 0U);
648 --quick_alloc_entry_points_instrumentation_counter_;
649 if (quick_alloc_entry_points_instrumentation_counter_ == 0) {
650 SetEntrypointsInstrumented(false);
651 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800652}
653
654void Instrumentation::ResetQuickAllocEntryPoints() {
655 Runtime* runtime = Runtime::Current();
656 if (runtime->IsStarted()) {
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800657 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700658 runtime->GetThreadList()->ForEach(ResetQuickAllocEntryPointsForThread, nullptr);
Ian Rogersfa824272013-11-05 16:12:57 -0800659 }
660}
661
Mathieu Chartiere401d142015-04-22 13:56:20 -0700662void Instrumentation::UpdateMethodsCode(ArtMethod* method, const void* quick_code) {
Sebastien Hertza8a697f2015-01-15 12:28:47 +0100663 DCHECK(method->GetDeclaringClass()->IsResolved());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800664 const void* new_quick_code;
Ian Rogers62d6c772013-02-27 08:32:07 -0800665 if (LIKELY(!instrumentation_stubs_installed_)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800666 new_quick_code = quick_code;
Jeff Hao65d15d92013-07-16 16:39:33 -0700667 } else {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100668 if ((interpreter_stubs_installed_ || IsDeoptimized(method)) && !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800669 new_quick_code = GetQuickToInterpreterBridge();
Jeff Hao65d15d92013-07-16 16:39:33 -0700670 } else {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700671 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700672 if (class_linker->IsQuickResolutionStub(quick_code) ||
673 class_linker->IsQuickToInterpreterBridge(quick_code)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700674 new_quick_code = quick_code;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700675 } else if (entry_exit_stubs_installed_) {
676 new_quick_code = GetQuickInstrumentationEntryPoint();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700677 } else {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700678 new_quick_code = quick_code;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700679 }
Jeff Hao65d15d92013-07-16 16:39:33 -0700680 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800681 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800682 UpdateEntrypoints(method, new_quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100683}
684
Mathieu Chartiere401d142015-04-22 13:56:20 -0700685bool Instrumentation::AddDeoptimizedMethod(ArtMethod* method) {
686 if (IsDeoptimizedMethod(method)) {
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700687 // Already in the map. Return.
688 return false;
689 }
690 // Not found. Add it.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700691 deoptimized_methods_.insert(method);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700692 return true;
693}
694
Mathieu Chartiere401d142015-04-22 13:56:20 -0700695bool Instrumentation::IsDeoptimizedMethod(ArtMethod* method) {
696 return deoptimized_methods_.find(method) != deoptimized_methods_.end();
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700697}
698
Mathieu Chartiere401d142015-04-22 13:56:20 -0700699ArtMethod* Instrumentation::BeginDeoptimizedMethod() {
700 if (deoptimized_methods_.empty()) {
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700701 // Empty.
702 return nullptr;
703 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700704 return *deoptimized_methods_.begin();
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700705}
706
Mathieu Chartiere401d142015-04-22 13:56:20 -0700707bool Instrumentation::RemoveDeoptimizedMethod(ArtMethod* method) {
708 auto it = deoptimized_methods_.find(method);
709 if (it == deoptimized_methods_.end()) {
710 return false;
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700711 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700712 deoptimized_methods_.erase(it);
713 return true;
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700714}
715
716bool Instrumentation::IsDeoptimizedMethodsEmpty() const {
717 return deoptimized_methods_.empty();
718}
719
Mathieu Chartiere401d142015-04-22 13:56:20 -0700720void Instrumentation::Deoptimize(ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100721 CHECK(!method->IsNative());
722 CHECK(!method->IsProxyMethod());
723 CHECK(!method->IsAbstract());
724
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700725 Thread* self = Thread::Current();
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700726 {
727 WriterMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700728 bool has_not_been_deoptimized = AddDeoptimizedMethod(method);
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200729 CHECK(has_not_been_deoptimized) << "Method " << PrettyMethod(method)
730 << " is already deoptimized";
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700731 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100732 if (!interpreter_stubs_installed_) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800733 UpdateEntrypoints(method, GetQuickInstrumentationEntryPoint());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100734
735 // Install instrumentation exit stub and instrumentation frames. We may already have installed
736 // these previously so it will only cover the newly created frames.
737 instrumentation_stubs_installed_ = true;
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700738 MutexLock mu(self, *Locks::thread_list_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100739 Runtime::Current()->GetThreadList()->ForEach(InstrumentationInstallStack, this);
740 }
741}
742
Mathieu Chartiere401d142015-04-22 13:56:20 -0700743void Instrumentation::Undeoptimize(ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100744 CHECK(!method->IsNative());
745 CHECK(!method->IsProxyMethod());
746 CHECK(!method->IsAbstract());
747
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700748 Thread* self = Thread::Current();
749 bool empty;
750 {
751 WriterMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700752 bool found_and_erased = RemoveDeoptimizedMethod(method);
753 CHECK(found_and_erased) << "Method " << PrettyMethod(method)
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700754 << " is not deoptimized";
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700755 empty = IsDeoptimizedMethodsEmpty();
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700756 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100757
758 // Restore code and possibly stack only if we did not deoptimize everything.
759 if (!interpreter_stubs_installed_) {
760 // Restore its code or resolution trampoline.
761 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800762 if (method->IsStatic() && !method->IsConstructor() &&
763 !method->GetDeclaringClass()->IsInitialized()) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800764 UpdateEntrypoints(method, GetQuickResolutionStub());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100765 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800766 const void* quick_code = class_linker->GetQuickOatCodeFor(method);
Elliott Hughes956af0f2014-12-11 14:34:28 -0800767 UpdateEntrypoints(method, quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100768 }
769
770 // If there is no deoptimized method left, we can restore the stack of each thread.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700771 if (empty) {
772 MutexLock mu(self, *Locks::thread_list_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100773 Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this);
774 instrumentation_stubs_installed_ = false;
775 }
776 }
777}
778
Mathieu Chartiere401d142015-04-22 13:56:20 -0700779bool Instrumentation::IsDeoptimized(ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100780 DCHECK(method != nullptr);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700781 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700782 return IsDeoptimizedMethod(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100783}
784
785void Instrumentation::EnableDeoptimization() {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700786 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700787 CHECK(IsDeoptimizedMethodsEmpty());
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100788 CHECK_EQ(deoptimization_enabled_, false);
789 deoptimization_enabled_ = true;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100790}
791
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200792void Instrumentation::DisableDeoptimization(const char* key) {
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100793 CHECK_EQ(deoptimization_enabled_, true);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100794 // If we deoptimized everything, undo it.
795 if (interpreter_stubs_installed_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200796 UndeoptimizeEverything(key);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100797 }
798 // Undeoptimized selected methods.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700799 while (true) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700800 ArtMethod* method;
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700801 {
802 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700803 if (IsDeoptimizedMethodsEmpty()) {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700804 break;
805 }
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700806 method = BeginDeoptimizedMethod();
807 CHECK(method != nullptr);
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700808 }
809 Undeoptimize(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100810 }
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100811 deoptimization_enabled_ = false;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100812}
813
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100814// Indicates if instrumentation should notify method enter/exit events to the listeners.
815bool Instrumentation::ShouldNotifyMethodEnterExitEvents() const {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200816 if (!HasMethodEntryListeners() && !HasMethodExitListeners()) {
817 return false;
818 }
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100819 return !deoptimization_enabled_ && !interpreter_stubs_installed_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100820}
821
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200822void Instrumentation::DeoptimizeEverything(const char* key) {
823 CHECK(deoptimization_enabled_);
824 ConfigureStubs(key, InstrumentationLevel::kInstrumentWithInterpreter);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100825}
826
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200827void Instrumentation::UndeoptimizeEverything(const char* key) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100828 CHECK(interpreter_stubs_installed_);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200829 CHECK(deoptimization_enabled_);
830 ConfigureStubs(key, InstrumentationLevel::kInstrumentNothing);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100831}
832
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200833void Instrumentation::EnableMethodTracing(const char* key, bool needs_interpreter) {
834 InstrumentationLevel level;
835 if (needs_interpreter) {
836 level = InstrumentationLevel::kInstrumentWithInterpreter;
837 } else {
838 level = InstrumentationLevel::kInstrumentWithInstrumentationStubs;
839 }
840 ConfigureStubs(key, level);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100841}
842
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200843void Instrumentation::DisableMethodTracing(const char* key) {
844 ConfigureStubs(key, InstrumentationLevel::kInstrumentNothing);
jeffhao725a9572012-11-13 18:20:12 -0800845}
846
Mathieu Chartiere401d142015-04-22 13:56:20 -0700847const void* Instrumentation::GetQuickCodeFor(ArtMethod* method, size_t pointer_size) const {
Ian Rogers62d6c772013-02-27 08:32:07 -0800848 Runtime* runtime = Runtime::Current();
849 if (LIKELY(!instrumentation_stubs_installed_)) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800850 const void* code = method->GetEntryPointFromQuickCompiledCodePtrSize(pointer_size);
Vladimir Marko8a630572014-04-09 18:45:35 +0100851 DCHECK(code != nullptr);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700852 ClassLinker* class_linker = runtime->GetClassLinker();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700853 if (LIKELY(!class_linker->IsQuickResolutionStub(code) &&
854 !class_linker->IsQuickToInterpreterBridge(code)) &&
855 !class_linker->IsQuickResolutionStub(code) &&
856 !class_linker->IsQuickToInterpreterBridge(code)) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800857 return code;
858 }
859 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800860 return runtime->GetClassLinker()->GetQuickOatCodeFor(method);
jeffhao725a9572012-11-13 18:20:12 -0800861}
862
Ian Rogers62d6c772013-02-27 08:32:07 -0800863void Instrumentation::MethodEnterEventImpl(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700864 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800865 uint32_t dex_pc) const {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700866 auto it = method_entry_listeners_.begin();
Jeff Hao65d15d92013-07-16 16:39:33 -0700867 bool is_end = (it == method_entry_listeners_.end());
868 // Implemented this way to prevent problems caused by modification of the list while iterating.
869 while (!is_end) {
870 InstrumentationListener* cur = *it;
871 ++it;
872 is_end = (it == method_entry_listeners_.end());
873 cur->MethodEntered(thread, this_object, method, dex_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800874 }
875}
876
877void Instrumentation::MethodExitEventImpl(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 JValue& return_value) const {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700880 auto it = method_exit_listeners_.begin();
Jeff Hao65d15d92013-07-16 16:39:33 -0700881 bool is_end = (it == method_exit_listeners_.end());
882 // Implemented this way to prevent problems caused by modification of the list while iterating.
883 while (!is_end) {
884 InstrumentationListener* cur = *it;
885 ++it;
886 is_end = (it == method_exit_listeners_.end());
887 cur->MethodExited(thread, this_object, method, dex_pc, return_value);
Ian Rogers62d6c772013-02-27 08:32:07 -0800888 }
889}
890
891void Instrumentation::MethodUnwindEvent(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700892 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800893 uint32_t dex_pc) const {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200894 if (HasMethodUnwindListeners()) {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700895 for (InstrumentationListener* listener : method_unwind_listeners_) {
Sebastien Hertz51db44a2013-11-19 10:00:29 +0100896 listener->MethodUnwind(thread, this_object, method, dex_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800897 }
898 }
899}
900
901void Instrumentation::DexPcMovedEventImpl(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 std::shared_ptr<std::list<InstrumentationListener*>> original(dex_pc_listeners_);
905 for (InstrumentationListener* listener : *original.get()) {
906 listener->DexPcMoved(thread, this_object, method, dex_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800907 }
908}
909
Mathieu Chartiere401d142015-04-22 13:56:20 -0700910void Instrumentation::BackwardBranchImpl(Thread* thread, ArtMethod* method,
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800911 int32_t offset) const {
912 for (InstrumentationListener* listener : backward_branch_listeners_) {
913 listener->BackwardBranch(thread, method, offset);
914 }
915}
916
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100917void Instrumentation::InvokeVirtualOrInterfaceImpl(Thread* thread,
918 mirror::Object* this_object,
919 ArtMethod* caller,
920 uint32_t dex_pc,
921 ArtMethod* callee) const {
922 for (InstrumentationListener* listener : invoke_virtual_or_interface_listeners_) {
923 listener->InvokeVirtualOrInterface(thread, this_object, caller, dex_pc, callee);
924 }
925}
926
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200927void Instrumentation::FieldReadEventImpl(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700928 ArtMethod* method, uint32_t dex_pc,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700929 ArtField* field) const {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200930 std::shared_ptr<std::list<InstrumentationListener*>> original(field_read_listeners_);
931 for (InstrumentationListener* listener : *original.get()) {
932 listener->FieldRead(thread, this_object, method, dex_pc, field);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200933 }
934}
935
936void Instrumentation::FieldWriteEventImpl(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 JValue& field_value) const {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200939 std::shared_ptr<std::list<InstrumentationListener*>> original(field_write_listeners_);
940 for (InstrumentationListener* listener : *original.get()) {
941 listener->FieldWritten(thread, this_object, method, dex_pc, field, field_value);
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200942 }
943}
944
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000945void Instrumentation::ExceptionCaughtEvent(Thread* thread,
Sebastien Hertz947ff082013-09-17 14:10:13 +0200946 mirror::Throwable* exception_object) const {
Sebastien Hertz9f102032014-05-23 08:59:42 +0200947 if (HasExceptionCaughtListeners()) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000948 DCHECK_EQ(thread->GetException(), exception_object);
Jeff Haoc0bd4da2013-04-11 15:52:28 -0700949 thread->ClearException();
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200950 std::shared_ptr<std::list<InstrumentationListener*>> original(exception_caught_listeners_);
951 for (InstrumentationListener* listener : *original.get()) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000952 listener->ExceptionCaught(thread, exception_object);
Ian Rogers62d6c772013-02-27 08:32:07 -0800953 }
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000954 thread->SetException(exception_object);
Ian Rogers62d6c772013-02-27 08:32:07 -0800955 }
956}
957
958static void CheckStackDepth(Thread* self, const InstrumentationStackFrame& instrumentation_frame,
959 int delta)
Mathieu Chartier90443472015-07-16 20:32:27 -0700960 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100961 size_t frame_id = StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk) + delta;
Ian Rogers62d6c772013-02-27 08:32:07 -0800962 if (frame_id != instrumentation_frame.frame_id_) {
963 LOG(ERROR) << "Expected frame_id=" << frame_id << " but found "
964 << instrumentation_frame.frame_id_;
965 StackVisitor::DescribeStack(self);
966 CHECK_EQ(frame_id, instrumentation_frame.frame_id_);
967 }
968}
969
970void Instrumentation::PushInstrumentationStackFrame(Thread* self, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700971 ArtMethod* method,
Jeff Hao9a916d32013-06-27 18:45:37 -0700972 uintptr_t lr, bool interpreter_entry) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800973 // We have a callee-save frame meaning this value is guaranteed to never be 0.
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100974 size_t frame_id = StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk);
Ian Rogers62d6c772013-02-27 08:32:07 -0800975 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
976 if (kVerboseInstrumentation) {
Brian Carlstrom2d888622013-07-18 17:02:00 -0700977 LOG(INFO) << "Entering " << PrettyMethod(method) << " from PC " << reinterpret_cast<void*>(lr);
Ian Rogers62d6c772013-02-27 08:32:07 -0800978 }
979 instrumentation::InstrumentationStackFrame instrumentation_frame(this_object, method, lr,
Jeff Hao9a916d32013-06-27 18:45:37 -0700980 frame_id, interpreter_entry);
Ian Rogers62d6c772013-02-27 08:32:07 -0800981 stack->push_front(instrumentation_frame);
982
Sebastien Hertz320deb22014-06-11 19:45:05 +0200983 if (!interpreter_entry) {
984 MethodEnterEvent(self, this_object, method, 0);
985 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800986}
987
Andreas Gamped58342c2014-06-05 14:18:08 -0700988TwoWordReturn Instrumentation::PopInstrumentationStackFrame(Thread* self, uintptr_t* return_pc,
989 uint64_t gpr_result,
990 uint64_t fpr_result) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800991 // Do the pop.
992 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
993 CHECK_GT(stack->size(), 0U);
994 InstrumentationStackFrame instrumentation_frame = stack->front();
995 stack->pop_front();
996
997 // Set return PC and check the sanity of the stack.
998 *return_pc = instrumentation_frame.return_pc_;
999 CheckStackDepth(self, instrumentation_frame, 0);
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001000 self->VerifyStack();
Ian Rogers62d6c772013-02-27 08:32:07 -08001001
Mathieu Chartiere401d142015-04-22 13:56:20 -07001002 ArtMethod* method = instrumentation_frame.method_;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001003 uint32_t length;
1004 char return_shorty = method->GetShorty(&length)[0];
Ian Rogers62d6c772013-02-27 08:32:07 -08001005 JValue return_value;
1006 if (return_shorty == 'V') {
1007 return_value.SetJ(0);
1008 } else if (return_shorty == 'F' || return_shorty == 'D') {
1009 return_value.SetJ(fpr_result);
1010 } else {
1011 return_value.SetJ(gpr_result);
1012 }
1013 // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to
1014 // return_pc.
1015 uint32_t dex_pc = DexFile::kDexNoIndex;
1016 mirror::Object* this_object = instrumentation_frame.this_object_;
Sebastien Hertz320deb22014-06-11 19:45:05 +02001017 if (!instrumentation_frame.interpreter_entry_) {
1018 MethodExitEvent(self, this_object, instrumentation_frame.method_, dex_pc, return_value);
1019 }
jeffhao725a9572012-11-13 18:20:12 -08001020
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01001021 // Deoptimize if the caller needs to continue execution in the interpreter. Do nothing if we get
1022 // back to an upcall.
1023 NthCallerVisitor visitor(self, 1, true);
1024 visitor.WalkStack(true);
Sebastien Hertz270a0e12015-01-16 19:49:09 +01001025 bool deoptimize = (visitor.caller != nullptr) &&
Daniel Mihalyieb076692014-08-22 17:33:31 +02001026 (interpreter_stubs_installed_ || IsDeoptimized(visitor.caller) ||
1027 Dbg::IsForcedInterpreterNeededForUpcall(self, visitor.caller));
Ian Rogers62d6c772013-02-27 08:32:07 -08001028 if (deoptimize) {
1029 if (kVerboseInstrumentation) {
Sebastien Hertz270a0e12015-01-16 19:49:09 +01001030 LOG(INFO) << StringPrintf("Deoptimizing %s by returning from %s with result %#" PRIx64 " in ",
1031 PrettyMethod(visitor.caller).c_str(),
1032 PrettyMethod(method).c_str(),
1033 return_value.GetJ()) << *self;
Ian Rogers62d6c772013-02-27 08:32:07 -08001034 }
Sebastien Hertz07474662015-08-25 15:12:33 +00001035 self->PushDeoptimizationContext(return_value, return_shorty == 'L',
1036 nullptr /* no pending exception */);
Andreas Gamped58342c2014-06-05 14:18:08 -07001037 return GetTwoWordSuccessValue(*return_pc,
1038 reinterpret_cast<uintptr_t>(GetQuickDeoptimizationEntryPoint()));
Ian Rogers62d6c772013-02-27 08:32:07 -08001039 } else {
1040 if (kVerboseInstrumentation) {
Brian Carlstrom2d888622013-07-18 17:02:00 -07001041 LOG(INFO) << "Returning from " << PrettyMethod(method)
1042 << " to PC " << reinterpret_cast<void*>(*return_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -08001043 }
Andreas Gamped58342c2014-06-05 14:18:08 -07001044 return GetTwoWordSuccessValue(0, *return_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -08001045 }
jeffhao725a9572012-11-13 18:20:12 -08001046}
1047
Ian Rogers62d6c772013-02-27 08:32:07 -08001048void Instrumentation::PopMethodForUnwind(Thread* self, bool is_deoptimization) const {
1049 // Do the pop.
1050 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
1051 CHECK_GT(stack->size(), 0U);
1052 InstrumentationStackFrame instrumentation_frame = stack->front();
1053 // TODO: bring back CheckStackDepth(self, instrumentation_frame, 2);
1054 stack->pop_front();
1055
Mathieu Chartiere401d142015-04-22 13:56:20 -07001056 ArtMethod* method = instrumentation_frame.method_;
Ian Rogers62d6c772013-02-27 08:32:07 -08001057 if (is_deoptimization) {
1058 if (kVerboseInstrumentation) {
1059 LOG(INFO) << "Popping for deoptimization " << PrettyMethod(method);
1060 }
1061 } else {
1062 if (kVerboseInstrumentation) {
1063 LOG(INFO) << "Popping for unwind " << PrettyMethod(method);
1064 }
1065
1066 // Notify listeners of method unwind.
1067 // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to
1068 // return_pc.
1069 uint32_t dex_pc = DexFile::kDexNoIndex;
1070 MethodUnwindEvent(self, instrumentation_frame.this_object_, method, dex_pc);
1071 }
1072}
1073
1074std::string InstrumentationStackFrame::Dump() const {
1075 std::ostringstream os;
1076 os << "Frame " << frame_id_ << " " << PrettyMethod(method_) << ":"
1077 << reinterpret_cast<void*>(return_pc_) << " this=" << reinterpret_cast<void*>(this_object_);
1078 return os.str();
1079}
1080
1081} // namespace instrumentation
jeffhao725a9572012-11-13 18:20:12 -08001082} // namespace art