blob: d059b97181f346209800fcbdf842d92485457adb [file] [log] [blame]
jeffhao725a9572012-11-13 18:20:12 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "instrumentation.h"
18
Ian Rogersc7dd2952014-10-21 23:31:19 -070019#include <sstream>
20
Ian Rogerse63db272014-07-15 15:36:11 -070021#include "arch/context.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070022#include "art_method-inl.h"
Ian Rogersef7d42f2014-01-06 12:55:46 -080023#include "atomic.h"
jeffhao725a9572012-11-13 18:20:12 -080024#include "class_linker.h"
25#include "debugger.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080026#include "dex_file-inl.h"
Ian Rogersc7dd2952014-10-21 23:31:19 -070027#include "entrypoints/quick/quick_entrypoints.h"
Mathieu Chartierd8891782014-03-02 13:28:37 -080028#include "entrypoints/quick/quick_alloc_entrypoints.h"
Ian Rogers6f3dbba2014-10-14 17:41:57 -070029#include "entrypoints/runtime_asm_entrypoints.h"
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070030#include "gc_root-inl.h"
Sebastien Hertz138dbfc2013-12-04 18:15:25 +010031#include "interpreter/interpreter.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080032#include "jit/jit.h"
33#include "jit/jit_code_cache.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080034#include "mirror/class-inl.h"
35#include "mirror/dex_cache.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080036#include "mirror/object_array-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070037#include "mirror/object-inl.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080038#include "nth_caller_visitor.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010039#include "oat_quick_method_header.h"
jeffhao725a9572012-11-13 18:20:12 -080040#include "thread.h"
41#include "thread_list.h"
jeffhao725a9572012-11-13 18:20:12 -080042
43namespace art {
Ian Rogers62d6c772013-02-27 08:32:07 -080044namespace instrumentation {
jeffhao725a9572012-11-13 18:20:12 -080045
Sebastien Hertz0462c4c2015-04-01 16:34:17 +020046constexpr bool kVerboseInstrumentation = false;
Sebastien Hertz5bfd5c92013-11-15 11:36:07 +010047
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +010048// Instrumentation works on non-inlined frames by updating returned PCs
49// of compiled frames.
50static constexpr StackVisitor::StackWalkKind kInstrumentationStackWalk =
51 StackVisitor::StackWalkKind::kSkipInlinedFrames;
52
Mathieu Chartiere0671ce2015-07-28 17:23:28 -070053class InstallStubsClassVisitor : public ClassVisitor {
54 public:
55 explicit InstallStubsClassVisitor(Instrumentation* instrumentation)
56 : instrumentation_(instrumentation) {}
57
Mathieu Chartier1aa8ec22016-02-01 10:34:47 -080058 bool operator()(mirror::Class* klass) OVERRIDE REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere0671ce2015-07-28 17:23:28 -070059 instrumentation_->InstallStubsForClass(klass);
60 return true; // we visit all classes.
61 }
62
63 private:
64 Instrumentation* const instrumentation_;
65};
66
Ian Rogers62d6c772013-02-27 08:32:07 -080067
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -070068Instrumentation::Instrumentation()
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +000069 : instrumentation_stubs_installed_(false),
70 entry_exit_stubs_installed_(false),
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -070071 interpreter_stubs_installed_(false),
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +000072 interpret_only_(false),
73 forced_interpret_only_(false),
74 have_method_entry_listeners_(false),
75 have_method_exit_listeners_(false),
76 have_method_unwind_listeners_(false),
77 have_dex_pc_listeners_(false),
78 have_field_read_listeners_(false),
79 have_field_write_listeners_(false),
80 have_exception_caught_listeners_(false),
Nicolas Geoffray81f0f952016-01-20 16:25:19 +000081 have_branch_listeners_(false),
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +000082 have_invoke_virtual_or_interface_listeners_(false),
Mathieu Chartierb8aa1e42016-04-05 14:36:57 -070083 deoptimized_methods_lock_("deoptimized methods lock", kDeoptimizedMethodsLock),
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -070084 deoptimization_enabled_(false),
85 interpreter_handler_table_(kMainHandlerTable),
Mathieu Chartier50e93312016-03-16 11:25:29 -070086 quick_alloc_entry_points_instrumentation_counter_(0),
87 alloc_entrypoints_instrumented_(false) {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -070088}
89
Sebastien Hertza10aa372015-01-21 17:30:58 +010090void Instrumentation::InstallStubsForClass(mirror::Class* klass) {
Sebastien Hertza8a697f2015-01-15 12:28:47 +010091 if (klass->IsErroneous()) {
92 // We can't execute code in a erroneous class: do nothing.
93 } else if (!klass->IsResolved()) {
94 // We need the class to be resolved to install/uninstall stubs. Otherwise its methods
95 // could not be initialized or linked with regards to class inheritance.
96 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -070097 for (ArtMethod& method : klass->GetMethods(kRuntimePointerSize)) {
Alex Light51a64d52015-12-17 13:55:59 -080098 InstallStubsForMethod(&method);
Sebastien Hertza8a697f2015-01-15 12:28:47 +010099 }
jeffhao725a9572012-11-13 18:20:12 -0800100 }
jeffhao725a9572012-11-13 18:20:12 -0800101}
102
Mathieu Chartiere401d142015-04-22 13:56:20 -0700103static void UpdateEntrypoints(ArtMethod* method, const void* quick_code)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700104 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800105 method->SetEntryPointFromQuickCompiledCode(quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100106}
107
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800108bool Instrumentation::NeedDebugVersionForBootImageCode(ArtMethod* method, const void* code) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700109 REQUIRES_SHARED(Locks::mutator_lock_) {
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800110 return Dbg::IsDebuggerActive() &&
111 Runtime::Current()->GetHeap()->IsInBootImageOatFile(code) &&
112 !method->IsNative() &&
113 !method->IsProxyMethod();
114}
115
Mathieu Chartiere401d142015-04-22 13:56:20 -0700116void Instrumentation::InstallStubsForMethod(ArtMethod* method) {
Alex Light9139e002015-10-09 15:59:48 -0700117 if (!method->IsInvokable() || method->IsProxyMethod()) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100118 // Do not change stubs for these methods.
119 return;
120 }
Jeff Hao56802772014-08-19 10:17:36 -0700121 // Don't stub Proxy.<init>. Note that the Proxy class itself is not a proxy class.
122 if (method->IsConstructor() &&
123 method->GetDeclaringClass()->DescriptorEquals("Ljava/lang/reflect/Proxy;")) {
Jeff Haodb8a6642014-08-14 17:18:52 -0700124 return;
125 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800126 const void* new_quick_code;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100127 bool uninstall = !entry_exit_stubs_installed_ && !interpreter_stubs_installed_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800128 Runtime* const runtime = Runtime::Current();
129 ClassLinker* const class_linker = runtime->GetClassLinker();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100130 bool is_class_initialized = method->GetDeclaringClass()->IsInitialized();
131 if (uninstall) {
132 if ((forced_interpret_only_ || IsDeoptimized(method)) && !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800133 new_quick_code = GetQuickToInterpreterBridge();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100134 } else if (is_class_initialized || !method->IsStatic() || method->IsConstructor()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800135 new_quick_code = class_linker->GetQuickOatCodeFor(method);
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800136 if (NeedDebugVersionForBootImageCode(method, new_quick_code)) {
137 new_quick_code = GetQuickToInterpreterBridge();
138 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100139 } else {
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700140 new_quick_code = GetQuickResolutionStub();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100141 }
142 } else { // !uninstall
Sebastien Hertzbae182c2013-12-17 10:42:03 +0100143 if ((interpreter_stubs_installed_ || forced_interpret_only_ || IsDeoptimized(method)) &&
144 !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800145 new_quick_code = GetQuickToInterpreterBridge();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100146 } else {
147 // Do not overwrite resolution trampoline. When the trampoline initializes the method's
148 // class, all its static methods code will be set to the instrumentation entry point.
149 // For more details, see ClassLinker::FixupStaticTrampolines.
150 if (is_class_initialized || !method->IsStatic() || method->IsConstructor()) {
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800151 new_quick_code = class_linker->GetQuickOatCodeFor(method);
152 if (NeedDebugVersionForBootImageCode(method, new_quick_code)) {
153 // Oat code should not be used. Don't install instrumentation stub and
154 // use interpreter for instrumentation.
155 new_quick_code = GetQuickToInterpreterBridge();
156 } else if (entry_exit_stubs_installed_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800157 new_quick_code = GetQuickInstrumentationEntryPoint();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100158 }
159 } else {
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700160 new_quick_code = GetQuickResolutionStub();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100161 }
162 }
163 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800164 UpdateEntrypoints(method, new_quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100165}
166
Ian Rogers62d6c772013-02-27 08:32:07 -0800167// Places the instrumentation exit pc as the return PC for every quick frame. This also allows
168// deoptimization of quick frames to interpreter frames.
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100169// Since we may already have done this previously, we need to push new instrumentation frame before
170// existing instrumentation frames.
Ian Rogers62d6c772013-02-27 08:32:07 -0800171static void InstrumentationInstallStack(Thread* thread, void* arg)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700172 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200173 struct InstallStackVisitor FINAL : public StackVisitor {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800174 InstallStackVisitor(Thread* thread_in, Context* context, uintptr_t instrumentation_exit_pc)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100175 : StackVisitor(thread_in, context, kInstrumentationStackWalk),
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800176 instrumentation_stack_(thread_in->GetInstrumentationStack()),
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100177 instrumentation_exit_pc_(instrumentation_exit_pc),
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100178 reached_existing_instrumentation_frames_(false), instrumentation_stack_depth_(0),
179 last_return_pc_(0) {
180 }
jeffhao725a9572012-11-13 18:20:12 -0800181
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700182 bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700183 ArtMethod* m = GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700184 if (m == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800185 if (kVerboseInstrumentation) {
186 LOG(INFO) << " Skipping upcall. Frame " << GetFrameId();
187 }
188 last_return_pc_ = 0;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700189 return true; // Ignore upcalls.
Ian Rogers306057f2012-11-26 12:45:53 -0800190 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700191 if (GetCurrentQuickFrame() == nullptr) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800192 bool interpreter_frame = true;
Sebastien Hertz320deb22014-06-11 19:45:05 +0200193 InstrumentationStackFrame instrumentation_frame(GetThisObject(), m, 0, GetFrameId(),
194 interpreter_frame);
Jeff Haoa15a81b2014-05-27 18:25:47 -0700195 if (kVerboseInstrumentation) {
196 LOG(INFO) << "Pushing shadow frame " << instrumentation_frame.Dump();
197 }
198 shadow_stack_.push_back(instrumentation_frame);
199 return true; // Continue.
200 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800201 uintptr_t return_pc = GetReturnPc();
Sebastien Hertz320deb22014-06-11 19:45:05 +0200202 if (m->IsRuntimeMethod()) {
203 if (return_pc == instrumentation_exit_pc_) {
204 if (kVerboseInstrumentation) {
205 LOG(INFO) << " Handling quick to interpreter transition. Frame " << GetFrameId();
206 }
207 CHECK_LT(instrumentation_stack_depth_, instrumentation_stack_->size());
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200208 const InstrumentationStackFrame& frame =
209 instrumentation_stack_->at(instrumentation_stack_depth_);
Sebastien Hertz320deb22014-06-11 19:45:05 +0200210 CHECK(frame.interpreter_entry_);
211 // This is an interpreter frame so method enter event must have been reported. However we
212 // need to push a DEX pc into the dex_pcs_ list to match size of instrumentation stack.
213 // Since we won't report method entry here, we can safely push any DEX pc.
214 dex_pcs_.push_back(0);
215 last_return_pc_ = frame.return_pc_;
216 ++instrumentation_stack_depth_;
217 return true;
218 } else {
219 if (kVerboseInstrumentation) {
220 LOG(INFO) << " Skipping runtime method. Frame " << GetFrameId();
221 }
222 last_return_pc_ = GetReturnPc();
223 return true; // Ignore unresolved methods since they will be instrumented after resolution.
224 }
225 }
226 if (kVerboseInstrumentation) {
227 LOG(INFO) << " Installing exit stub in " << DescribeLocation();
228 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100229 if (return_pc == instrumentation_exit_pc_) {
230 // We've reached a frame which has already been installed with instrumentation exit stub.
231 // We should have already installed instrumentation on previous frames.
232 reached_existing_instrumentation_frames_ = true;
233
234 CHECK_LT(instrumentation_stack_depth_, instrumentation_stack_->size());
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200235 const InstrumentationStackFrame& frame =
236 instrumentation_stack_->at(instrumentation_stack_depth_);
David Sehr709b0702016-10-13 09:12:37 -0700237 CHECK_EQ(m, frame.method_) << "Expected " << ArtMethod::PrettyMethod(m)
238 << ", Found " << ArtMethod::PrettyMethod(frame.method_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100239 return_pc = frame.return_pc_;
240 if (kVerboseInstrumentation) {
241 LOG(INFO) << "Ignoring already instrumented " << frame.Dump();
242 }
243 } else {
244 CHECK_NE(return_pc, 0U);
245 CHECK(!reached_existing_instrumentation_frames_);
246 InstrumentationStackFrame instrumentation_frame(GetThisObject(), m, return_pc, GetFrameId(),
247 false);
248 if (kVerboseInstrumentation) {
249 LOG(INFO) << "Pushing frame " << instrumentation_frame.Dump();
250 }
251
Sebastien Hertz320deb22014-06-11 19:45:05 +0200252 // Insert frame at the right position so we do not corrupt the instrumentation stack.
253 // Instrumentation stack frames are in descending frame id order.
254 auto it = instrumentation_stack_->begin();
255 for (auto end = instrumentation_stack_->end(); it != end; ++it) {
256 const InstrumentationStackFrame& current = *it;
257 if (instrumentation_frame.frame_id_ >= current.frame_id_) {
258 break;
259 }
260 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100261 instrumentation_stack_->insert(it, instrumentation_frame);
262 SetReturnPc(instrumentation_exit_pc_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800263 }
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100264 dex_pcs_.push_back((GetCurrentOatQuickMethodHeader() == nullptr)
265 ? DexFile::kDexNoIndex
266 : GetCurrentOatQuickMethodHeader()->ToDexPc(m, last_return_pc_));
Ian Rogers62d6c772013-02-27 08:32:07 -0800267 last_return_pc_ = return_pc;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100268 ++instrumentation_stack_depth_;
Ian Rogers306057f2012-11-26 12:45:53 -0800269 return true; // Continue.
270 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800271 std::deque<InstrumentationStackFrame>* const instrumentation_stack_;
Jeff Haoa15a81b2014-05-27 18:25:47 -0700272 std::vector<InstrumentationStackFrame> shadow_stack_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800273 std::vector<uint32_t> dex_pcs_;
Ian Rogers306057f2012-11-26 12:45:53 -0800274 const uintptr_t instrumentation_exit_pc_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100275 bool reached_existing_instrumentation_frames_;
276 size_t instrumentation_stack_depth_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800277 uintptr_t last_return_pc_;
Ian Rogers306057f2012-11-26 12:45:53 -0800278 };
Ian Rogers62d6c772013-02-27 08:32:07 -0800279 if (kVerboseInstrumentation) {
280 std::string thread_name;
281 thread->GetThreadName(thread_name);
282 LOG(INFO) << "Installing exit stubs in " << thread_name;
Ian Rogers306057f2012-11-26 12:45:53 -0800283 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100284
285 Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg);
Ian Rogers700a4022014-05-19 16:49:03 -0700286 std::unique_ptr<Context> context(Context::Create());
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700287 uintptr_t instrumentation_exit_pc = reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc());
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100288 InstallStackVisitor visitor(thread, context.get(), instrumentation_exit_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800289 visitor.WalkStack(true);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100290 CHECK_EQ(visitor.dex_pcs_.size(), thread->GetInstrumentationStack()->size());
Ian Rogers62d6c772013-02-27 08:32:07 -0800291
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100292 if (instrumentation->ShouldNotifyMethodEnterExitEvents()) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100293 // Create method enter events for all methods currently on the thread's stack. We only do this
294 // if no debugger is attached to prevent from posting events twice.
Jeff Haoa15a81b2014-05-27 18:25:47 -0700295 auto ssi = visitor.shadow_stack_.rbegin();
296 for (auto isi = thread->GetInstrumentationStack()->rbegin(),
297 end = thread->GetInstrumentationStack()->rend(); isi != end; ++isi) {
298 while (ssi != visitor.shadow_stack_.rend() && (*ssi).frame_id_ < (*isi).frame_id_) {
299 instrumentation->MethodEnterEvent(thread, (*ssi).this_object_, (*ssi).method_, 0);
300 ++ssi;
301 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100302 uint32_t dex_pc = visitor.dex_pcs_.back();
303 visitor.dex_pcs_.pop_back();
Sebastien Hertz320deb22014-06-11 19:45:05 +0200304 if (!isi->interpreter_entry_) {
305 instrumentation->MethodEnterEvent(thread, (*isi).this_object_, (*isi).method_, dex_pc);
306 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100307 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800308 }
309 thread->VerifyStack();
Ian Rogers306057f2012-11-26 12:45:53 -0800310}
311
Mingyao Yang99170c62015-07-06 11:10:37 -0700312void Instrumentation::InstrumentThreadStack(Thread* thread) {
313 instrumentation_stubs_installed_ = true;
314 InstrumentationInstallStack(thread, this);
315}
316
Ian Rogers62d6c772013-02-27 08:32:07 -0800317// Removes the instrumentation exit pc as the return PC for every quick frame.
318static void InstrumentationRestoreStack(Thread* thread, void* arg)
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +0000319 REQUIRES(Locks::mutator_lock_) {
320 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
321
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200322 struct RestoreStackVisitor FINAL : public StackVisitor {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800323 RestoreStackVisitor(Thread* thread_in, uintptr_t instrumentation_exit_pc,
Ian Rogers62d6c772013-02-27 08:32:07 -0800324 Instrumentation* instrumentation)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100325 : StackVisitor(thread_in, nullptr, kInstrumentationStackWalk),
326 thread_(thread_in),
Ian Rogers62d6c772013-02-27 08:32:07 -0800327 instrumentation_exit_pc_(instrumentation_exit_pc),
328 instrumentation_(instrumentation),
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800329 instrumentation_stack_(thread_in->GetInstrumentationStack()),
Ian Rogers62d6c772013-02-27 08:32:07 -0800330 frames_removed_(0) {}
Ian Rogers306057f2012-11-26 12:45:53 -0800331
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700332 bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800333 if (instrumentation_stack_->size() == 0) {
jeffhao725a9572012-11-13 18:20:12 -0800334 return false; // Stop.
335 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700336 ArtMethod* m = GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700337 if (GetCurrentQuickFrame() == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800338 if (kVerboseInstrumentation) {
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200339 LOG(INFO) << " Ignoring a shadow frame. Frame " << GetFrameId()
David Sehr709b0702016-10-13 09:12:37 -0700340 << " Method=" << ArtMethod::PrettyMethod(m);
Ian Rogers62d6c772013-02-27 08:32:07 -0800341 }
342 return true; // Ignore shadow frames.
343 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700344 if (m == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800345 if (kVerboseInstrumentation) {
346 LOG(INFO) << " Skipping upcall. Frame " << GetFrameId();
347 }
Ian Rogers306057f2012-11-26 12:45:53 -0800348 return true; // Ignore upcalls.
349 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800350 bool removed_stub = false;
351 // TODO: make this search more efficient?
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100352 const size_t frameId = GetFrameId();
353 for (const InstrumentationStackFrame& instrumentation_frame : *instrumentation_stack_) {
354 if (instrumentation_frame.frame_id_ == frameId) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800355 if (kVerboseInstrumentation) {
356 LOG(INFO) << " Removing exit stub in " << DescribeLocation();
357 }
Jeff Hao9a916d32013-06-27 18:45:37 -0700358 if (instrumentation_frame.interpreter_entry_) {
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100359 CHECK(m == Runtime::Current()->GetCalleeSaveMethod(Runtime::kSaveRefsAndArgs));
Jeff Hao9a916d32013-06-27 18:45:37 -0700360 } else {
David Sehr709b0702016-10-13 09:12:37 -0700361 CHECK(m == instrumentation_frame.method_) << ArtMethod::PrettyMethod(m);
Jeff Hao9a916d32013-06-27 18:45:37 -0700362 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800363 SetReturnPc(instrumentation_frame.return_pc_);
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100364 if (instrumentation_->ShouldNotifyMethodEnterExitEvents()) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100365 // Create the method exit events. As the methods didn't really exit the result is 0.
366 // We only do this if no debugger is attached to prevent from posting events twice.
367 instrumentation_->MethodExitEvent(thread_, instrumentation_frame.this_object_, m,
368 GetDexPc(), JValue());
369 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800370 frames_removed_++;
371 removed_stub = true;
372 break;
373 }
374 }
375 if (!removed_stub) {
376 if (kVerboseInstrumentation) {
377 LOG(INFO) << " No exit stub in " << DescribeLocation();
Ian Rogers306057f2012-11-26 12:45:53 -0800378 }
jeffhao725a9572012-11-13 18:20:12 -0800379 }
380 return true; // Continue.
381 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800382 Thread* const thread_;
Ian Rogers306057f2012-11-26 12:45:53 -0800383 const uintptr_t instrumentation_exit_pc_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800384 Instrumentation* const instrumentation_;
385 std::deque<instrumentation::InstrumentationStackFrame>* const instrumentation_stack_;
386 size_t frames_removed_;
jeffhao725a9572012-11-13 18:20:12 -0800387 };
Ian Rogers62d6c772013-02-27 08:32:07 -0800388 if (kVerboseInstrumentation) {
389 std::string thread_name;
390 thread->GetThreadName(thread_name);
391 LOG(INFO) << "Removing exit stubs in " << thread_name;
392 }
393 std::deque<instrumentation::InstrumentationStackFrame>* stack = thread->GetInstrumentationStack();
394 if (stack->size() > 0) {
395 Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700396 uintptr_t instrumentation_exit_pc =
397 reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc());
Ian Rogers62d6c772013-02-27 08:32:07 -0800398 RestoreStackVisitor visitor(thread, instrumentation_exit_pc, instrumentation);
399 visitor.WalkStack(true);
400 CHECK_EQ(visitor.frames_removed_, stack->size());
401 while (stack->size() > 0) {
402 stack->pop_front();
403 }
jeffhao725a9572012-11-13 18:20:12 -0800404 }
405}
406
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200407static bool HasEvent(Instrumentation::InstrumentationEvent expected, uint32_t events) {
408 return (events & expected) != 0;
409}
410
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000411static void PotentiallyAddListenerTo(Instrumentation::InstrumentationEvent event,
412 uint32_t events,
413 std::list<InstrumentationListener*>& list,
414 InstrumentationListener* listener,
415 bool* has_listener)
416 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_) {
417 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
418 if (!HasEvent(event, events)) {
419 return;
420 }
421 // If there is a free slot in the list, we insert the listener in that slot.
422 // Otherwise we add it to the end of the list.
423 auto it = std::find(list.begin(), list.end(), nullptr);
424 if (it != list.end()) {
425 *it = listener;
426 } else {
427 list.push_back(listener);
428 }
429 *has_listener = true;
430}
431
Ian Rogers62d6c772013-02-27 08:32:07 -0800432void Instrumentation::AddListener(InstrumentationListener* listener, uint32_t events) {
433 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000434 PotentiallyAddListenerTo(kMethodEntered,
435 events,
436 method_entry_listeners_,
437 listener,
438 &have_method_entry_listeners_);
439 PotentiallyAddListenerTo(kMethodExited,
440 events,
441 method_exit_listeners_,
442 listener,
443 &have_method_exit_listeners_);
444 PotentiallyAddListenerTo(kMethodUnwind,
445 events,
446 method_unwind_listeners_,
447 listener,
448 &have_method_unwind_listeners_);
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000449 PotentiallyAddListenerTo(kBranch,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000450 events,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000451 branch_listeners_,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000452 listener,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000453 &have_branch_listeners_);
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000454 PotentiallyAddListenerTo(kInvokeVirtualOrInterface,
455 events,
456 invoke_virtual_or_interface_listeners_,
457 listener,
458 &have_invoke_virtual_or_interface_listeners_);
459 PotentiallyAddListenerTo(kDexPcMoved,
460 events,
461 dex_pc_listeners_,
462 listener,
463 &have_dex_pc_listeners_);
464 PotentiallyAddListenerTo(kFieldRead,
465 events,
466 field_read_listeners_,
467 listener,
468 &have_field_read_listeners_);
469 PotentiallyAddListenerTo(kFieldWritten,
470 events,
471 field_write_listeners_,
472 listener,
473 &have_field_write_listeners_);
474 PotentiallyAddListenerTo(kExceptionCaught,
475 events,
476 exception_caught_listeners_,
477 listener,
478 &have_exception_caught_listeners_);
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200479 UpdateInterpreterHandlerTable();
jeffhao725a9572012-11-13 18:20:12 -0800480}
481
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000482static void PotentiallyRemoveListenerFrom(Instrumentation::InstrumentationEvent event,
483 uint32_t events,
484 std::list<InstrumentationListener*>& list,
485 InstrumentationListener* listener,
486 bool* has_listener)
487 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_) {
488 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
489 if (!HasEvent(event, events)) {
490 return;
491 }
492 auto it = std::find(list.begin(), list.end(), listener);
493 if (it != list.end()) {
494 // Just update the entry, do not remove from the list. Removing entries in the list
495 // is unsafe when mutators are iterating over it.
496 *it = nullptr;
497 }
498
499 // Check if the list contains any non-null listener, and update 'has_listener'.
500 for (InstrumentationListener* l : list) {
501 if (l != nullptr) {
502 *has_listener = true;
503 return;
504 }
505 }
506 *has_listener = false;
507}
508
Ian Rogers62d6c772013-02-27 08:32:07 -0800509void Instrumentation::RemoveListener(InstrumentationListener* listener, uint32_t events) {
510 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000511 PotentiallyRemoveListenerFrom(kMethodEntered,
512 events,
513 method_entry_listeners_,
514 listener,
515 &have_method_entry_listeners_);
516 PotentiallyRemoveListenerFrom(kMethodExited,
517 events,
518 method_exit_listeners_,
519 listener,
520 &have_method_exit_listeners_);
521 PotentiallyRemoveListenerFrom(kMethodUnwind,
522 events,
523 method_unwind_listeners_,
524 listener,
525 &have_method_unwind_listeners_);
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000526 PotentiallyRemoveListenerFrom(kBranch,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000527 events,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000528 branch_listeners_,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000529 listener,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000530 &have_branch_listeners_);
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000531 PotentiallyRemoveListenerFrom(kInvokeVirtualOrInterface,
532 events,
533 invoke_virtual_or_interface_listeners_,
534 listener,
535 &have_invoke_virtual_or_interface_listeners_);
536 PotentiallyRemoveListenerFrom(kDexPcMoved,
537 events,
538 dex_pc_listeners_,
539 listener,
540 &have_dex_pc_listeners_);
541 PotentiallyRemoveListenerFrom(kFieldRead,
542 events,
543 field_read_listeners_,
544 listener,
545 &have_field_read_listeners_);
546 PotentiallyRemoveListenerFrom(kFieldWritten,
547 events,
548 field_write_listeners_,
549 listener,
550 &have_field_write_listeners_);
551 PotentiallyRemoveListenerFrom(kExceptionCaught,
552 events,
553 exception_caught_listeners_,
554 listener,
555 &have_exception_caught_listeners_);
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200556 UpdateInterpreterHandlerTable();
jeffhao725a9572012-11-13 18:20:12 -0800557}
558
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200559Instrumentation::InstrumentationLevel Instrumentation::GetCurrentInstrumentationLevel() const {
Ian Rogers62d6c772013-02-27 08:32:07 -0800560 if (interpreter_stubs_installed_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200561 return InstrumentationLevel::kInstrumentWithInterpreter;
Ian Rogers62d6c772013-02-27 08:32:07 -0800562 } else if (entry_exit_stubs_installed_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200563 return InstrumentationLevel::kInstrumentWithInstrumentationStubs;
Ian Rogers62d6c772013-02-27 08:32:07 -0800564 } else {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200565 return InstrumentationLevel::kInstrumentNothing;
Ian Rogers62d6c772013-02-27 08:32:07 -0800566 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200567}
568
569void Instrumentation::ConfigureStubs(const char* key, InstrumentationLevel desired_level) {
570 // Store the instrumentation level for this key or remove it.
571 if (desired_level == InstrumentationLevel::kInstrumentNothing) {
572 // The client no longer needs instrumentation.
573 requested_instrumentation_levels_.erase(key);
574 } else {
575 // The client needs instrumentation.
576 requested_instrumentation_levels_.Overwrite(key, desired_level);
577 }
578
579 // Look for the highest required instrumentation level.
580 InstrumentationLevel requested_level = InstrumentationLevel::kInstrumentNothing;
581 for (const auto& v : requested_instrumentation_levels_) {
582 requested_level = std::max(requested_level, v.second);
583 }
584
585 interpret_only_ = (requested_level == InstrumentationLevel::kInstrumentWithInterpreter) ||
586 forced_interpret_only_;
587
588 InstrumentationLevel current_level = GetCurrentInstrumentationLevel();
589 if (requested_level == current_level) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800590 // We're already set.
591 return;
592 }
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100593 Thread* const self = Thread::Current();
Ian Rogers62d6c772013-02-27 08:32:07 -0800594 Runtime* runtime = Runtime::Current();
Sebastien Hertza8a697f2015-01-15 12:28:47 +0100595 Locks::mutator_lock_->AssertExclusiveHeld(self);
Ian Rogers62d6c772013-02-27 08:32:07 -0800596 Locks::thread_list_lock_->AssertNotHeld(self);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200597 if (requested_level > InstrumentationLevel::kInstrumentNothing) {
598 if (requested_level == InstrumentationLevel::kInstrumentWithInterpreter) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800599 interpreter_stubs_installed_ = true;
Ian Rogers62d6c772013-02-27 08:32:07 -0800600 entry_exit_stubs_installed_ = true;
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200601 } else {
602 CHECK_EQ(requested_level, InstrumentationLevel::kInstrumentWithInstrumentationStubs);
603 entry_exit_stubs_installed_ = true;
604 interpreter_stubs_installed_ = false;
Ian Rogers62d6c772013-02-27 08:32:07 -0800605 }
Mathieu Chartiere0671ce2015-07-28 17:23:28 -0700606 InstallStubsClassVisitor visitor(this);
607 runtime->GetClassLinker()->VisitClasses(&visitor);
Ian Rogers62d6c772013-02-27 08:32:07 -0800608 instrumentation_stubs_installed_ = true;
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100609 MutexLock mu(self, *Locks::thread_list_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800610 runtime->GetThreadList()->ForEach(InstrumentationInstallStack, this);
611 } else {
612 interpreter_stubs_installed_ = false;
613 entry_exit_stubs_installed_ = false;
Mathieu Chartiere0671ce2015-07-28 17:23:28 -0700614 InstallStubsClassVisitor visitor(this);
615 runtime->GetClassLinker()->VisitClasses(&visitor);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100616 // Restore stack only if there is no method currently deoptimized.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700617 bool empty;
618 {
619 ReaderMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700620 empty = IsDeoptimizedMethodsEmpty(); // Avoid lock violation.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700621 }
622 if (empty) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100623 MutexLock mu(self, *Locks::thread_list_lock_);
624 Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this);
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +0000625 // Only do this after restoring, as walking the stack when restoring will see
626 // the instrumentation exit pc.
627 instrumentation_stubs_installed_ = false;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100628 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800629 }
jeffhao725a9572012-11-13 18:20:12 -0800630}
631
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200632static void ResetQuickAllocEntryPointsForThread(Thread* thread, void* arg ATTRIBUTE_UNUSED) {
Ian Rogersfa824272013-11-05 16:12:57 -0800633 thread->ResetQuickAllocEntryPointsForThread();
634}
635
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700636void Instrumentation::SetEntrypointsInstrumented(bool instrumented) {
637 Thread* self = Thread::Current();
Mathieu Chartier661974a2014-01-09 11:23:53 -0800638 Runtime* runtime = Runtime::Current();
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700639 Locks::mutator_lock_->AssertNotHeld(self);
640 Locks::instrument_entrypoints_lock_->AssertHeld(self);
641 if (runtime->IsStarted()) {
Mathieu Chartier4f55e222015-09-04 13:26:21 -0700642 ScopedSuspendAll ssa(__FUNCTION__);
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700643 MutexLock mu(self, *Locks::runtime_shutdown_lock_);
Mathieu Chartier661974a2014-01-09 11:23:53 -0800644 SetQuickAllocEntryPointsInstrumented(instrumented);
645 ResetQuickAllocEntryPoints();
Mathieu Chartier50e93312016-03-16 11:25:29 -0700646 alloc_entrypoints_instrumented_ = instrumented;
Mathieu Chartier4f55e222015-09-04 13:26:21 -0700647 } else {
648 MutexLock mu(self, *Locks::runtime_shutdown_lock_);
649 SetQuickAllocEntryPointsInstrumented(instrumented);
Andreas Gampe157c77e2016-10-17 17:44:41 -0700650
651 // Note: ResetQuickAllocEntryPoints only works when the runtime is started. Manually run the
652 // update for just this thread.
Andreas Gampe162ae502016-10-18 10:03:42 -0700653 // Note: self may be null. One of those paths is setting instrumentation in the Heap
654 // constructor for gcstress mode.
655 if (self != nullptr) {
656 ResetQuickAllocEntryPointsForThread(self, nullptr);
657 }
Andreas Gampe157c77e2016-10-17 17:44:41 -0700658
Mathieu Chartier50e93312016-03-16 11:25:29 -0700659 alloc_entrypoints_instrumented_ = instrumented;
Mathieu Chartier661974a2014-01-09 11:23:53 -0800660 }
661}
662
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700663void Instrumentation::InstrumentQuickAllocEntryPoints() {
664 MutexLock mu(Thread::Current(), *Locks::instrument_entrypoints_lock_);
665 InstrumentQuickAllocEntryPointsLocked();
Ian Rogersfa824272013-11-05 16:12:57 -0800666}
667
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700668void Instrumentation::UninstrumentQuickAllocEntryPoints() {
669 MutexLock mu(Thread::Current(), *Locks::instrument_entrypoints_lock_);
670 UninstrumentQuickAllocEntryPointsLocked();
671}
672
673void Instrumentation::InstrumentQuickAllocEntryPointsLocked() {
674 Locks::instrument_entrypoints_lock_->AssertHeld(Thread::Current());
675 if (quick_alloc_entry_points_instrumentation_counter_ == 0) {
676 SetEntrypointsInstrumented(true);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800677 }
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700678 ++quick_alloc_entry_points_instrumentation_counter_;
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700679}
680
681void Instrumentation::UninstrumentQuickAllocEntryPointsLocked() {
682 Locks::instrument_entrypoints_lock_->AssertHeld(Thread::Current());
683 CHECK_GT(quick_alloc_entry_points_instrumentation_counter_, 0U);
684 --quick_alloc_entry_points_instrumentation_counter_;
685 if (quick_alloc_entry_points_instrumentation_counter_ == 0) {
686 SetEntrypointsInstrumented(false);
687 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800688}
689
690void Instrumentation::ResetQuickAllocEntryPoints() {
691 Runtime* runtime = Runtime::Current();
692 if (runtime->IsStarted()) {
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800693 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700694 runtime->GetThreadList()->ForEach(ResetQuickAllocEntryPointsForThread, nullptr);
Ian Rogersfa824272013-11-05 16:12:57 -0800695 }
696}
697
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700698void Instrumentation::UpdateMethodsCodeImpl(ArtMethod* method, const void* quick_code) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800699 const void* new_quick_code;
Ian Rogers62d6c772013-02-27 08:32:07 -0800700 if (LIKELY(!instrumentation_stubs_installed_)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800701 new_quick_code = quick_code;
Jeff Hao65d15d92013-07-16 16:39:33 -0700702 } else {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100703 if ((interpreter_stubs_installed_ || IsDeoptimized(method)) && !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800704 new_quick_code = GetQuickToInterpreterBridge();
Jeff Hao65d15d92013-07-16 16:39:33 -0700705 } else {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700706 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700707 if (class_linker->IsQuickResolutionStub(quick_code) ||
708 class_linker->IsQuickToInterpreterBridge(quick_code)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700709 new_quick_code = quick_code;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700710 } else if (entry_exit_stubs_installed_) {
711 new_quick_code = GetQuickInstrumentationEntryPoint();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700712 } else {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700713 new_quick_code = quick_code;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700714 }
Jeff Hao65d15d92013-07-16 16:39:33 -0700715 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800716 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800717 UpdateEntrypoints(method, new_quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100718}
719
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700720void Instrumentation::UpdateMethodsCode(ArtMethod* method, const void* quick_code) {
721 DCHECK(method->GetDeclaringClass()->IsResolved());
722 UpdateMethodsCodeImpl(method, quick_code);
723}
724
725void Instrumentation::UpdateMethodsCodeFromDebugger(ArtMethod* method, const void* quick_code) {
726 // When debugger attaches, we may update the entry points of all methods of a class
727 // to the interpreter bridge. A method's declaring class might not be in resolved
728 // state yet in that case.
729 UpdateMethodsCodeImpl(method, quick_code);
730}
731
Mathieu Chartiere401d142015-04-22 13:56:20 -0700732bool Instrumentation::AddDeoptimizedMethod(ArtMethod* method) {
733 if (IsDeoptimizedMethod(method)) {
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700734 // Already in the map. Return.
735 return false;
736 }
737 // Not found. Add it.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700738 deoptimized_methods_.insert(method);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700739 return true;
740}
741
Mathieu Chartiere401d142015-04-22 13:56:20 -0700742bool Instrumentation::IsDeoptimizedMethod(ArtMethod* method) {
743 return deoptimized_methods_.find(method) != deoptimized_methods_.end();
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700744}
745
Mathieu Chartiere401d142015-04-22 13:56:20 -0700746ArtMethod* Instrumentation::BeginDeoptimizedMethod() {
747 if (deoptimized_methods_.empty()) {
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700748 // Empty.
749 return nullptr;
750 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700751 return *deoptimized_methods_.begin();
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700752}
753
Mathieu Chartiere401d142015-04-22 13:56:20 -0700754bool Instrumentation::RemoveDeoptimizedMethod(ArtMethod* method) {
755 auto it = deoptimized_methods_.find(method);
756 if (it == deoptimized_methods_.end()) {
757 return false;
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700758 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700759 deoptimized_methods_.erase(it);
760 return true;
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700761}
762
763bool Instrumentation::IsDeoptimizedMethodsEmpty() const {
764 return deoptimized_methods_.empty();
765}
766
Mathieu Chartiere401d142015-04-22 13:56:20 -0700767void Instrumentation::Deoptimize(ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100768 CHECK(!method->IsNative());
769 CHECK(!method->IsProxyMethod());
Alex Light9139e002015-10-09 15:59:48 -0700770 CHECK(method->IsInvokable());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100771
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700772 Thread* self = Thread::Current();
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700773 {
774 WriterMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700775 bool has_not_been_deoptimized = AddDeoptimizedMethod(method);
David Sehr709b0702016-10-13 09:12:37 -0700776 CHECK(has_not_been_deoptimized) << "Method " << ArtMethod::PrettyMethod(method)
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200777 << " is already deoptimized";
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700778 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100779 if (!interpreter_stubs_installed_) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800780 UpdateEntrypoints(method, GetQuickInstrumentationEntryPoint());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100781
782 // Install instrumentation exit stub and instrumentation frames. We may already have installed
783 // these previously so it will only cover the newly created frames.
784 instrumentation_stubs_installed_ = true;
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700785 MutexLock mu(self, *Locks::thread_list_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100786 Runtime::Current()->GetThreadList()->ForEach(InstrumentationInstallStack, this);
787 }
788}
789
Mathieu Chartiere401d142015-04-22 13:56:20 -0700790void Instrumentation::Undeoptimize(ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100791 CHECK(!method->IsNative());
792 CHECK(!method->IsProxyMethod());
Alex Light9139e002015-10-09 15:59:48 -0700793 CHECK(method->IsInvokable());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100794
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700795 Thread* self = Thread::Current();
796 bool empty;
797 {
798 WriterMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700799 bool found_and_erased = RemoveDeoptimizedMethod(method);
David Sehr709b0702016-10-13 09:12:37 -0700800 CHECK(found_and_erased) << "Method " << ArtMethod::PrettyMethod(method)
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700801 << " is not deoptimized";
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700802 empty = IsDeoptimizedMethodsEmpty();
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700803 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100804
805 // Restore code and possibly stack only if we did not deoptimize everything.
806 if (!interpreter_stubs_installed_) {
807 // Restore its code or resolution trampoline.
808 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800809 if (method->IsStatic() && !method->IsConstructor() &&
810 !method->GetDeclaringClass()->IsInitialized()) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800811 UpdateEntrypoints(method, GetQuickResolutionStub());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100812 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800813 const void* quick_code = class_linker->GetQuickOatCodeFor(method);
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800814 if (NeedDebugVersionForBootImageCode(method, quick_code)) {
815 quick_code = GetQuickToInterpreterBridge();
816 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800817 UpdateEntrypoints(method, quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100818 }
819
820 // If there is no deoptimized method left, we can restore the stack of each thread.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700821 if (empty) {
822 MutexLock mu(self, *Locks::thread_list_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100823 Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this);
824 instrumentation_stubs_installed_ = false;
825 }
826 }
827}
828
Mathieu Chartiere401d142015-04-22 13:56:20 -0700829bool Instrumentation::IsDeoptimized(ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100830 DCHECK(method != nullptr);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700831 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700832 return IsDeoptimizedMethod(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100833}
834
835void Instrumentation::EnableDeoptimization() {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700836 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700837 CHECK(IsDeoptimizedMethodsEmpty());
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100838 CHECK_EQ(deoptimization_enabled_, false);
839 deoptimization_enabled_ = true;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100840}
841
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200842void Instrumentation::DisableDeoptimization(const char* key) {
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100843 CHECK_EQ(deoptimization_enabled_, true);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100844 // If we deoptimized everything, undo it.
845 if (interpreter_stubs_installed_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200846 UndeoptimizeEverything(key);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100847 }
848 // Undeoptimized selected methods.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700849 while (true) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700850 ArtMethod* method;
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700851 {
852 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700853 if (IsDeoptimizedMethodsEmpty()) {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700854 break;
855 }
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700856 method = BeginDeoptimizedMethod();
857 CHECK(method != nullptr);
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700858 }
859 Undeoptimize(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100860 }
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100861 deoptimization_enabled_ = false;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100862}
863
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100864// Indicates if instrumentation should notify method enter/exit events to the listeners.
865bool Instrumentation::ShouldNotifyMethodEnterExitEvents() const {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200866 if (!HasMethodEntryListeners() && !HasMethodExitListeners()) {
867 return false;
868 }
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100869 return !deoptimization_enabled_ && !interpreter_stubs_installed_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100870}
871
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200872void Instrumentation::DeoptimizeEverything(const char* key) {
873 CHECK(deoptimization_enabled_);
874 ConfigureStubs(key, InstrumentationLevel::kInstrumentWithInterpreter);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100875}
876
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200877void Instrumentation::UndeoptimizeEverything(const char* key) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100878 CHECK(interpreter_stubs_installed_);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200879 CHECK(deoptimization_enabled_);
880 ConfigureStubs(key, InstrumentationLevel::kInstrumentNothing);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100881}
882
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200883void Instrumentation::EnableMethodTracing(const char* key, bool needs_interpreter) {
884 InstrumentationLevel level;
885 if (needs_interpreter) {
886 level = InstrumentationLevel::kInstrumentWithInterpreter;
887 } else {
888 level = InstrumentationLevel::kInstrumentWithInstrumentationStubs;
889 }
890 ConfigureStubs(key, level);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100891}
892
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200893void Instrumentation::DisableMethodTracing(const char* key) {
894 ConfigureStubs(key, InstrumentationLevel::kInstrumentNothing);
jeffhao725a9572012-11-13 18:20:12 -0800895}
896
Andreas Gampe542451c2016-07-26 09:02:02 -0700897const void* Instrumentation::GetQuickCodeFor(ArtMethod* method, PointerSize pointer_size) const {
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100898 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers62d6c772013-02-27 08:32:07 -0800899 if (LIKELY(!instrumentation_stubs_installed_)) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800900 const void* code = method->GetEntryPointFromQuickCompiledCodePtrSize(pointer_size);
Vladimir Marko8a630572014-04-09 18:45:35 +0100901 DCHECK(code != nullptr);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700902 if (LIKELY(!class_linker->IsQuickResolutionStub(code) &&
903 !class_linker->IsQuickToInterpreterBridge(code)) &&
904 !class_linker->IsQuickResolutionStub(code) &&
905 !class_linker->IsQuickToInterpreterBridge(code)) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800906 return code;
907 }
908 }
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100909 return class_linker->GetQuickOatCodeFor(method);
jeffhao725a9572012-11-13 18:20:12 -0800910}
911
Ian Rogers62d6c772013-02-27 08:32:07 -0800912void Instrumentation::MethodEnterEventImpl(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700913 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800914 uint32_t dex_pc) const {
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000915 if (HasMethodEntryListeners()) {
916 for (InstrumentationListener* listener : method_entry_listeners_) {
917 if (listener != nullptr) {
918 listener->MethodEntered(thread, this_object, method, dex_pc);
919 }
920 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800921 }
922}
923
924void Instrumentation::MethodExitEventImpl(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700925 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800926 uint32_t dex_pc, const JValue& return_value) const {
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000927 if (HasMethodExitListeners()) {
928 for (InstrumentationListener* listener : method_exit_listeners_) {
929 if (listener != nullptr) {
930 listener->MethodExited(thread, this_object, method, dex_pc, return_value);
931 }
932 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800933 }
934}
935
936void Instrumentation::MethodUnwindEvent(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700937 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800938 uint32_t dex_pc) const {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200939 if (HasMethodUnwindListeners()) {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700940 for (InstrumentationListener* listener : method_unwind_listeners_) {
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000941 if (listener != nullptr) {
942 listener->MethodUnwind(thread, this_object, method, dex_pc);
943 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800944 }
945 }
946}
947
948void Instrumentation::DexPcMovedEventImpl(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700949 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800950 uint32_t dex_pc) const {
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000951 for (InstrumentationListener* listener : dex_pc_listeners_) {
952 if (listener != nullptr) {
953 listener->DexPcMoved(thread, this_object, method, dex_pc);
954 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800955 }
956}
957
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000958void Instrumentation::BranchImpl(Thread* thread,
959 ArtMethod* method,
960 uint32_t dex_pc,
961 int32_t offset) const {
962 for (InstrumentationListener* listener : branch_listeners_) {
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000963 if (listener != nullptr) {
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000964 listener->Branch(thread, method, dex_pc, offset);
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000965 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800966 }
967}
968
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100969void Instrumentation::InvokeVirtualOrInterfaceImpl(Thread* thread,
970 mirror::Object* this_object,
971 ArtMethod* caller,
972 uint32_t dex_pc,
973 ArtMethod* callee) const {
Roland Levillain91d65e02016-01-19 15:59:16 +0000974 // We cannot have thread suspension since that would cause the this_object parameter to
Mathieu Chartier3fdb3fe2016-01-14 10:24:28 -0800975 // potentially become a dangling pointer. An alternative could be to put it in a handle instead.
Mathieu Chartier268764d2016-09-13 12:09:38 -0700976 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100977 for (InstrumentationListener* listener : invoke_virtual_or_interface_listeners_) {
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000978 if (listener != nullptr) {
979 listener->InvokeVirtualOrInterface(thread, this_object, caller, dex_pc, callee);
980 }
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100981 }
982}
983
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200984void Instrumentation::FieldReadEventImpl(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700985 ArtMethod* method, uint32_t dex_pc,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700986 ArtField* field) const {
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000987 for (InstrumentationListener* listener : field_read_listeners_) {
988 if (listener != nullptr) {
989 listener->FieldRead(thread, this_object, method, dex_pc, field);
990 }
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200991 }
992}
993
994void Instrumentation::FieldWriteEventImpl(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700995 ArtMethod* method, uint32_t dex_pc,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700996 ArtField* field, const JValue& field_value) const {
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000997 for (InstrumentationListener* listener : field_write_listeners_) {
998 if (listener != nullptr) {
999 listener->FieldWritten(thread, this_object, method, dex_pc, field, field_value);
1000 }
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +02001001 }
1002}
1003
Nicolas Geoffray14691c52015-03-05 10:40:17 +00001004void Instrumentation::ExceptionCaughtEvent(Thread* thread,
Sebastien Hertz947ff082013-09-17 14:10:13 +02001005 mirror::Throwable* exception_object) const {
Sebastien Hertz9f102032014-05-23 08:59:42 +02001006 if (HasExceptionCaughtListeners()) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +00001007 DCHECK_EQ(thread->GetException(), exception_object);
Jeff Haoc0bd4da2013-04-11 15:52:28 -07001008 thread->ClearException();
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001009 for (InstrumentationListener* listener : exception_caught_listeners_) {
1010 if (listener != nullptr) {
1011 listener->ExceptionCaught(thread, exception_object);
1012 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001013 }
Nicolas Geoffray14691c52015-03-05 10:40:17 +00001014 thread->SetException(exception_object);
Ian Rogers62d6c772013-02-27 08:32:07 -08001015 }
1016}
1017
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +00001018// Computes a frame ID by ignoring inlined frames.
1019size_t Instrumentation::ComputeFrameId(Thread* self,
1020 size_t frame_depth,
1021 size_t inlined_frames_before_frame) {
1022 CHECK_GE(frame_depth, inlined_frames_before_frame);
1023 size_t no_inline_depth = frame_depth - inlined_frames_before_frame;
1024 return StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk) - no_inline_depth;
1025}
1026
Ian Rogers62d6c772013-02-27 08:32:07 -08001027static void CheckStackDepth(Thread* self, const InstrumentationStackFrame& instrumentation_frame,
1028 int delta)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001029 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +01001030 size_t frame_id = StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk) + delta;
Ian Rogers62d6c772013-02-27 08:32:07 -08001031 if (frame_id != instrumentation_frame.frame_id_) {
1032 LOG(ERROR) << "Expected frame_id=" << frame_id << " but found "
1033 << instrumentation_frame.frame_id_;
1034 StackVisitor::DescribeStack(self);
1035 CHECK_EQ(frame_id, instrumentation_frame.frame_id_);
1036 }
1037}
1038
1039void Instrumentation::PushInstrumentationStackFrame(Thread* self, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001040 ArtMethod* method,
Jeff Hao9a916d32013-06-27 18:45:37 -07001041 uintptr_t lr, bool interpreter_entry) {
Ian Rogers62d6c772013-02-27 08:32:07 -08001042 // We have a callee-save frame meaning this value is guaranteed to never be 0.
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +01001043 size_t frame_id = StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk);
Ian Rogers62d6c772013-02-27 08:32:07 -08001044 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
1045 if (kVerboseInstrumentation) {
David Sehr709b0702016-10-13 09:12:37 -07001046 LOG(INFO) << "Entering " << ArtMethod::PrettyMethod(method) << " from PC "
1047 << reinterpret_cast<void*>(lr);
Ian Rogers62d6c772013-02-27 08:32:07 -08001048 }
1049 instrumentation::InstrumentationStackFrame instrumentation_frame(this_object, method, lr,
Jeff Hao9a916d32013-06-27 18:45:37 -07001050 frame_id, interpreter_entry);
Ian Rogers62d6c772013-02-27 08:32:07 -08001051 stack->push_front(instrumentation_frame);
1052
Sebastien Hertz320deb22014-06-11 19:45:05 +02001053 if (!interpreter_entry) {
1054 MethodEnterEvent(self, this_object, method, 0);
1055 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001056}
1057
Andreas Gamped58342c2014-06-05 14:18:08 -07001058TwoWordReturn Instrumentation::PopInstrumentationStackFrame(Thread* self, uintptr_t* return_pc,
1059 uint64_t gpr_result,
1060 uint64_t fpr_result) {
Ian Rogers62d6c772013-02-27 08:32:07 -08001061 // Do the pop.
1062 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
1063 CHECK_GT(stack->size(), 0U);
1064 InstrumentationStackFrame instrumentation_frame = stack->front();
1065 stack->pop_front();
1066
1067 // Set return PC and check the sanity of the stack.
1068 *return_pc = instrumentation_frame.return_pc_;
1069 CheckStackDepth(self, instrumentation_frame, 0);
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001070 self->VerifyStack();
Ian Rogers62d6c772013-02-27 08:32:07 -08001071
Mathieu Chartiere401d142015-04-22 13:56:20 -07001072 ArtMethod* method = instrumentation_frame.method_;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001073 uint32_t length;
Andreas Gampe542451c2016-07-26 09:02:02 -07001074 const PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Mingyao Yang99170c62015-07-06 11:10:37 -07001075 char return_shorty = method->GetInterfaceMethodIfProxy(pointer_size)->GetShorty(&length)[0];
Ian Rogers62d6c772013-02-27 08:32:07 -08001076 JValue return_value;
1077 if (return_shorty == 'V') {
1078 return_value.SetJ(0);
1079 } else if (return_shorty == 'F' || return_shorty == 'D') {
1080 return_value.SetJ(fpr_result);
1081 } else {
1082 return_value.SetJ(gpr_result);
1083 }
1084 // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to
1085 // return_pc.
1086 uint32_t dex_pc = DexFile::kDexNoIndex;
1087 mirror::Object* this_object = instrumentation_frame.this_object_;
Sebastien Hertz320deb22014-06-11 19:45:05 +02001088 if (!instrumentation_frame.interpreter_entry_) {
1089 MethodExitEvent(self, this_object, instrumentation_frame.method_, dex_pc, return_value);
1090 }
jeffhao725a9572012-11-13 18:20:12 -08001091
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01001092 // Deoptimize if the caller needs to continue execution in the interpreter. Do nothing if we get
1093 // back to an upcall.
1094 NthCallerVisitor visitor(self, 1, true);
1095 visitor.WalkStack(true);
Sebastien Hertz270a0e12015-01-16 19:49:09 +01001096 bool deoptimize = (visitor.caller != nullptr) &&
Daniel Mihalyieb076692014-08-22 17:33:31 +02001097 (interpreter_stubs_installed_ || IsDeoptimized(visitor.caller) ||
1098 Dbg::IsForcedInterpreterNeededForUpcall(self, visitor.caller));
Mingyao Yangf711f2c2016-05-23 12:29:39 -07001099 if (deoptimize && Runtime::Current()->IsDeoptimizeable(*return_pc)) {
Ian Rogers62d6c772013-02-27 08:32:07 -08001100 if (kVerboseInstrumentation) {
Sebastien Hertz270a0e12015-01-16 19:49:09 +01001101 LOG(INFO) << StringPrintf("Deoptimizing %s by returning from %s with result %#" PRIx64 " in ",
David Sehr709b0702016-10-13 09:12:37 -07001102 visitor.caller->PrettyMethod().c_str(),
1103 method->PrettyMethod().c_str(),
Sebastien Hertz270a0e12015-01-16 19:49:09 +01001104 return_value.GetJ()) << *self;
Ian Rogers62d6c772013-02-27 08:32:07 -08001105 }
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01001106 self->PushDeoptimizationContext(return_value,
1107 return_shorty == 'L',
1108 false /* from_code */,
Sebastien Hertz07474662015-08-25 15:12:33 +00001109 nullptr /* no pending exception */);
Andreas Gamped58342c2014-06-05 14:18:08 -07001110 return GetTwoWordSuccessValue(*return_pc,
1111 reinterpret_cast<uintptr_t>(GetQuickDeoptimizationEntryPoint()));
Ian Rogers62d6c772013-02-27 08:32:07 -08001112 } else {
1113 if (kVerboseInstrumentation) {
David Sehr709b0702016-10-13 09:12:37 -07001114 LOG(INFO) << "Returning from " << method->PrettyMethod()
Brian Carlstrom2d888622013-07-18 17:02:00 -07001115 << " to PC " << reinterpret_cast<void*>(*return_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -08001116 }
Andreas Gamped58342c2014-06-05 14:18:08 -07001117 return GetTwoWordSuccessValue(0, *return_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -08001118 }
jeffhao725a9572012-11-13 18:20:12 -08001119}
1120
Mingyao Yangf711f2c2016-05-23 12:29:39 -07001121uintptr_t Instrumentation::PopMethodForUnwind(Thread* self, bool is_deoptimization) const {
Ian Rogers62d6c772013-02-27 08:32:07 -08001122 // Do the pop.
1123 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
1124 CHECK_GT(stack->size(), 0U);
1125 InstrumentationStackFrame instrumentation_frame = stack->front();
1126 // TODO: bring back CheckStackDepth(self, instrumentation_frame, 2);
1127 stack->pop_front();
1128
Mathieu Chartiere401d142015-04-22 13:56:20 -07001129 ArtMethod* method = instrumentation_frame.method_;
Ian Rogers62d6c772013-02-27 08:32:07 -08001130 if (is_deoptimization) {
1131 if (kVerboseInstrumentation) {
David Sehr709b0702016-10-13 09:12:37 -07001132 LOG(INFO) << "Popping for deoptimization " << ArtMethod::PrettyMethod(method);
Ian Rogers62d6c772013-02-27 08:32:07 -08001133 }
1134 } else {
1135 if (kVerboseInstrumentation) {
David Sehr709b0702016-10-13 09:12:37 -07001136 LOG(INFO) << "Popping for unwind " << ArtMethod::PrettyMethod(method);
Ian Rogers62d6c772013-02-27 08:32:07 -08001137 }
1138
1139 // Notify listeners of method unwind.
1140 // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to
1141 // return_pc.
1142 uint32_t dex_pc = DexFile::kDexNoIndex;
1143 MethodUnwindEvent(self, instrumentation_frame.this_object_, method, dex_pc);
1144 }
Mingyao Yangf711f2c2016-05-23 12:29:39 -07001145 return instrumentation_frame.return_pc_;
Ian Rogers62d6c772013-02-27 08:32:07 -08001146}
1147
1148std::string InstrumentationStackFrame::Dump() const {
1149 std::ostringstream os;
David Sehr709b0702016-10-13 09:12:37 -07001150 os << "Frame " << frame_id_ << " " << ArtMethod::PrettyMethod(method_) << ":"
Ian Rogers62d6c772013-02-27 08:32:07 -08001151 << reinterpret_cast<void*>(return_pc_) << " this=" << reinterpret_cast<void*>(this_object_);
1152 return os.str();
1153}
1154
1155} // namespace instrumentation
jeffhao725a9572012-11-13 18:20:12 -08001156} // namespace art