blob: 8120cc484e952213387b31655bcc2a651bf49fe4 [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"
Alex Lightd7661582017-05-01 13:48:16 -070023#include "art_field-inl.h"
Ian Rogersef7d42f2014-01-06 12:55:46 -080024#include "atomic.h"
Andreas Gampe8228cdf2017-05-30 15:03:54 -070025#include "base/callee_save_type.h"
jeffhao725a9572012-11-13 18:20:12 -080026#include "class_linker.h"
27#include "debugger.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080028#include "dex_file-inl.h"
Ian Rogersc7dd2952014-10-21 23:31:19 -070029#include "entrypoints/quick/quick_entrypoints.h"
Mathieu Chartierd8891782014-03-02 13:28:37 -080030#include "entrypoints/quick/quick_alloc_entrypoints.h"
Ian Rogers6f3dbba2014-10-14 17:41:57 -070031#include "entrypoints/runtime_asm_entrypoints.h"
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070032#include "gc_root-inl.h"
Sebastien Hertz138dbfc2013-12-04 18:15:25 +010033#include "interpreter/interpreter.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080034#include "jit/jit.h"
35#include "jit/jit_code_cache.h"
Alex Lightd7661582017-05-01 13:48:16 -070036#include "jvalue-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080037#include "mirror/class-inl.h"
38#include "mirror/dex_cache.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080039#include "mirror/object_array-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070040#include "mirror/object-inl.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080041#include "nth_caller_visitor.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010042#include "oat_quick_method_header.h"
jeffhao725a9572012-11-13 18:20:12 -080043#include "thread.h"
44#include "thread_list.h"
jeffhao725a9572012-11-13 18:20:12 -080045
46namespace art {
Ian Rogers62d6c772013-02-27 08:32:07 -080047namespace instrumentation {
jeffhao725a9572012-11-13 18:20:12 -080048
Sebastien Hertz0462c4c2015-04-01 16:34:17 +020049constexpr bool kVerboseInstrumentation = false;
Sebastien Hertz5bfd5c92013-11-15 11:36:07 +010050
Alex Lightd7661582017-05-01 13:48:16 -070051void InstrumentationListener::MethodExited(Thread* thread,
52 Handle<mirror::Object> this_object,
53 ArtMethod* method,
54 uint32_t dex_pc,
55 Handle<mirror::Object> return_value) {
56 DCHECK_EQ(method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetReturnTypePrimitive(),
57 Primitive::kPrimNot);
58 JValue v;
59 v.SetL(return_value.Get());
60 MethodExited(thread, this_object, method, dex_pc, v);
61}
62
63void InstrumentationListener::FieldWritten(Thread* thread,
64 Handle<mirror::Object> this_object,
65 ArtMethod* method,
66 uint32_t dex_pc,
67 ArtField* field,
68 Handle<mirror::Object> field_value) {
69 DCHECK(!field->IsPrimitiveType());
70 JValue v;
71 v.SetL(field_value.Get());
72 FieldWritten(thread, this_object, method, dex_pc, field, v);
73}
74
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +010075// Instrumentation works on non-inlined frames by updating returned PCs
76// of compiled frames.
77static constexpr StackVisitor::StackWalkKind kInstrumentationStackWalk =
78 StackVisitor::StackWalkKind::kSkipInlinedFrames;
79
Mathieu Chartiere0671ce2015-07-28 17:23:28 -070080class InstallStubsClassVisitor : public ClassVisitor {
81 public:
82 explicit InstallStubsClassVisitor(Instrumentation* instrumentation)
83 : instrumentation_(instrumentation) {}
84
Mathieu Chartier28357fa2016-10-18 16:27:40 -070085 bool operator()(ObjPtr<mirror::Class> klass) OVERRIDE REQUIRES(Locks::mutator_lock_) {
86 instrumentation_->InstallStubsForClass(klass.Ptr());
Mathieu Chartiere0671ce2015-07-28 17:23:28 -070087 return true; // we visit all classes.
88 }
89
90 private:
91 Instrumentation* const instrumentation_;
92};
93
Ian Rogers62d6c772013-02-27 08:32:07 -080094
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -070095Instrumentation::Instrumentation()
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +000096 : instrumentation_stubs_installed_(false),
97 entry_exit_stubs_installed_(false),
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -070098 interpreter_stubs_installed_(false),
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +000099 interpret_only_(false),
100 forced_interpret_only_(false),
101 have_method_entry_listeners_(false),
102 have_method_exit_listeners_(false),
103 have_method_unwind_listeners_(false),
104 have_dex_pc_listeners_(false),
105 have_field_read_listeners_(false),
106 have_field_write_listeners_(false),
107 have_exception_caught_listeners_(false),
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000108 have_branch_listeners_(false),
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +0000109 have_invoke_virtual_or_interface_listeners_(false),
Mathieu Chartierb8aa1e42016-04-05 14:36:57 -0700110 deoptimized_methods_lock_("deoptimized methods lock", kDeoptimizedMethodsLock),
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700111 deoptimization_enabled_(false),
112 interpreter_handler_table_(kMainHandlerTable),
Mathieu Chartier50e93312016-03-16 11:25:29 -0700113 quick_alloc_entry_points_instrumentation_counter_(0),
114 alloc_entrypoints_instrumented_(false) {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700115}
116
Sebastien Hertza10aa372015-01-21 17:30:58 +0100117void Instrumentation::InstallStubsForClass(mirror::Class* klass) {
Vladimir Marko72ab6842017-01-20 19:32:50 +0000118 if (!klass->IsResolved()) {
Sebastien Hertza8a697f2015-01-15 12:28:47 +0100119 // We need the class to be resolved to install/uninstall stubs. Otherwise its methods
120 // could not be initialized or linked with regards to class inheritance.
Vladimir Marko72ab6842017-01-20 19:32:50 +0000121 } else if (klass->IsErroneousResolved()) {
122 // We can't execute code in a erroneous class: do nothing.
Sebastien Hertza8a697f2015-01-15 12:28:47 +0100123 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700124 for (ArtMethod& method : klass->GetMethods(kRuntimePointerSize)) {
Alex Light51a64d52015-12-17 13:55:59 -0800125 InstallStubsForMethod(&method);
Sebastien Hertza8a697f2015-01-15 12:28:47 +0100126 }
jeffhao725a9572012-11-13 18:20:12 -0800127 }
jeffhao725a9572012-11-13 18:20:12 -0800128}
129
Mathieu Chartiere401d142015-04-22 13:56:20 -0700130static void UpdateEntrypoints(ArtMethod* method, const void* quick_code)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700131 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800132 method->SetEntryPointFromQuickCompiledCode(quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100133}
134
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000135bool Instrumentation::NeedDebugVersionFor(ArtMethod* method) const REQUIRES_SHARED(Locks::mutator_lock_) {
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800136 return Dbg::IsDebuggerActive() &&
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000137 Runtime::Current()->IsJavaDebuggable() &&
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800138 !method->IsNative() &&
139 !method->IsProxyMethod();
140}
141
Mathieu Chartiere401d142015-04-22 13:56:20 -0700142void Instrumentation::InstallStubsForMethod(ArtMethod* method) {
Alex Light9139e002015-10-09 15:59:48 -0700143 if (!method->IsInvokable() || method->IsProxyMethod()) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100144 // Do not change stubs for these methods.
145 return;
146 }
Jeff Hao56802772014-08-19 10:17:36 -0700147 // Don't stub Proxy.<init>. Note that the Proxy class itself is not a proxy class.
148 if (method->IsConstructor() &&
149 method->GetDeclaringClass()->DescriptorEquals("Ljava/lang/reflect/Proxy;")) {
Jeff Haodb8a6642014-08-14 17:18:52 -0700150 return;
151 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800152 const void* new_quick_code;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100153 bool uninstall = !entry_exit_stubs_installed_ && !interpreter_stubs_installed_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800154 Runtime* const runtime = Runtime::Current();
155 ClassLinker* const class_linker = runtime->GetClassLinker();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100156 bool is_class_initialized = method->GetDeclaringClass()->IsInitialized();
157 if (uninstall) {
158 if ((forced_interpret_only_ || IsDeoptimized(method)) && !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800159 new_quick_code = GetQuickToInterpreterBridge();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100160 } else if (is_class_initialized || !method->IsStatic() || method->IsConstructor()) {
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000161 if (NeedDebugVersionFor(method)) {
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800162 new_quick_code = GetQuickToInterpreterBridge();
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000163 } else {
164 new_quick_code = class_linker->GetQuickOatCodeFor(method);
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800165 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100166 } else {
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700167 new_quick_code = GetQuickResolutionStub();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100168 }
169 } else { // !uninstall
Sebastien Hertzbae182c2013-12-17 10:42:03 +0100170 if ((interpreter_stubs_installed_ || forced_interpret_only_ || IsDeoptimized(method)) &&
171 !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800172 new_quick_code = GetQuickToInterpreterBridge();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100173 } else {
174 // Do not overwrite resolution trampoline. When the trampoline initializes the method's
175 // class, all its static methods code will be set to the instrumentation entry point.
176 // For more details, see ClassLinker::FixupStaticTrampolines.
177 if (is_class_initialized || !method->IsStatic() || method->IsConstructor()) {
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000178 if (NeedDebugVersionFor(method)) {
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800179 // Oat code should not be used. Don't install instrumentation stub and
180 // use interpreter for instrumentation.
181 new_quick_code = GetQuickToInterpreterBridge();
182 } else if (entry_exit_stubs_installed_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800183 new_quick_code = GetQuickInstrumentationEntryPoint();
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000184 } else {
185 new_quick_code = class_linker->GetQuickOatCodeFor(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100186 }
187 } else {
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700188 new_quick_code = GetQuickResolutionStub();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100189 }
190 }
191 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800192 UpdateEntrypoints(method, new_quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100193}
194
Ian Rogers62d6c772013-02-27 08:32:07 -0800195// Places the instrumentation exit pc as the return PC for every quick frame. This also allows
196// deoptimization of quick frames to interpreter frames.
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100197// Since we may already have done this previously, we need to push new instrumentation frame before
198// existing instrumentation frames.
Ian Rogers62d6c772013-02-27 08:32:07 -0800199static void InstrumentationInstallStack(Thread* thread, void* arg)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700200 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200201 struct InstallStackVisitor FINAL : public StackVisitor {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800202 InstallStackVisitor(Thread* thread_in, Context* context, uintptr_t instrumentation_exit_pc)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100203 : StackVisitor(thread_in, context, kInstrumentationStackWalk),
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800204 instrumentation_stack_(thread_in->GetInstrumentationStack()),
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100205 instrumentation_exit_pc_(instrumentation_exit_pc),
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100206 reached_existing_instrumentation_frames_(false), instrumentation_stack_depth_(0),
207 last_return_pc_(0) {
208 }
jeffhao725a9572012-11-13 18:20:12 -0800209
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700210 bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700211 ArtMethod* m = GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700212 if (m == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800213 if (kVerboseInstrumentation) {
214 LOG(INFO) << " Skipping upcall. Frame " << GetFrameId();
215 }
216 last_return_pc_ = 0;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700217 return true; // Ignore upcalls.
Ian Rogers306057f2012-11-26 12:45:53 -0800218 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700219 if (GetCurrentQuickFrame() == nullptr) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800220 bool interpreter_frame = true;
Sebastien Hertz320deb22014-06-11 19:45:05 +0200221 InstrumentationStackFrame instrumentation_frame(GetThisObject(), m, 0, GetFrameId(),
222 interpreter_frame);
Jeff Haoa15a81b2014-05-27 18:25:47 -0700223 if (kVerboseInstrumentation) {
224 LOG(INFO) << "Pushing shadow frame " << instrumentation_frame.Dump();
225 }
226 shadow_stack_.push_back(instrumentation_frame);
227 return true; // Continue.
228 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800229 uintptr_t return_pc = GetReturnPc();
Sebastien Hertz320deb22014-06-11 19:45:05 +0200230 if (m->IsRuntimeMethod()) {
231 if (return_pc == instrumentation_exit_pc_) {
232 if (kVerboseInstrumentation) {
233 LOG(INFO) << " Handling quick to interpreter transition. Frame " << GetFrameId();
234 }
235 CHECK_LT(instrumentation_stack_depth_, instrumentation_stack_->size());
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200236 const InstrumentationStackFrame& frame =
237 instrumentation_stack_->at(instrumentation_stack_depth_);
Sebastien Hertz320deb22014-06-11 19:45:05 +0200238 CHECK(frame.interpreter_entry_);
239 // This is an interpreter frame so method enter event must have been reported. However we
240 // need to push a DEX pc into the dex_pcs_ list to match size of instrumentation stack.
241 // Since we won't report method entry here, we can safely push any DEX pc.
242 dex_pcs_.push_back(0);
243 last_return_pc_ = frame.return_pc_;
244 ++instrumentation_stack_depth_;
245 return true;
246 } else {
247 if (kVerboseInstrumentation) {
248 LOG(INFO) << " Skipping runtime method. Frame " << GetFrameId();
249 }
250 last_return_pc_ = GetReturnPc();
251 return true; // Ignore unresolved methods since they will be instrumented after resolution.
252 }
253 }
254 if (kVerboseInstrumentation) {
255 LOG(INFO) << " Installing exit stub in " << DescribeLocation();
256 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100257 if (return_pc == instrumentation_exit_pc_) {
258 // We've reached a frame which has already been installed with instrumentation exit stub.
259 // We should have already installed instrumentation on previous frames.
260 reached_existing_instrumentation_frames_ = true;
261
262 CHECK_LT(instrumentation_stack_depth_, instrumentation_stack_->size());
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200263 const InstrumentationStackFrame& frame =
264 instrumentation_stack_->at(instrumentation_stack_depth_);
David Sehr709b0702016-10-13 09:12:37 -0700265 CHECK_EQ(m, frame.method_) << "Expected " << ArtMethod::PrettyMethod(m)
266 << ", Found " << ArtMethod::PrettyMethod(frame.method_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100267 return_pc = frame.return_pc_;
268 if (kVerboseInstrumentation) {
269 LOG(INFO) << "Ignoring already instrumented " << frame.Dump();
270 }
271 } else {
272 CHECK_NE(return_pc, 0U);
273 CHECK(!reached_existing_instrumentation_frames_);
274 InstrumentationStackFrame instrumentation_frame(GetThisObject(), m, return_pc, GetFrameId(),
275 false);
276 if (kVerboseInstrumentation) {
277 LOG(INFO) << "Pushing frame " << instrumentation_frame.Dump();
278 }
279
Sebastien Hertz320deb22014-06-11 19:45:05 +0200280 // Insert frame at the right position so we do not corrupt the instrumentation stack.
281 // Instrumentation stack frames are in descending frame id order.
282 auto it = instrumentation_stack_->begin();
283 for (auto end = instrumentation_stack_->end(); it != end; ++it) {
284 const InstrumentationStackFrame& current = *it;
285 if (instrumentation_frame.frame_id_ >= current.frame_id_) {
286 break;
287 }
288 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100289 instrumentation_stack_->insert(it, instrumentation_frame);
290 SetReturnPc(instrumentation_exit_pc_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800291 }
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100292 dex_pcs_.push_back((GetCurrentOatQuickMethodHeader() == nullptr)
293 ? DexFile::kDexNoIndex
294 : GetCurrentOatQuickMethodHeader()->ToDexPc(m, last_return_pc_));
Ian Rogers62d6c772013-02-27 08:32:07 -0800295 last_return_pc_ = return_pc;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100296 ++instrumentation_stack_depth_;
Ian Rogers306057f2012-11-26 12:45:53 -0800297 return true; // Continue.
298 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800299 std::deque<InstrumentationStackFrame>* const instrumentation_stack_;
Jeff Haoa15a81b2014-05-27 18:25:47 -0700300 std::vector<InstrumentationStackFrame> shadow_stack_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800301 std::vector<uint32_t> dex_pcs_;
Ian Rogers306057f2012-11-26 12:45:53 -0800302 const uintptr_t instrumentation_exit_pc_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100303 bool reached_existing_instrumentation_frames_;
304 size_t instrumentation_stack_depth_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800305 uintptr_t last_return_pc_;
Ian Rogers306057f2012-11-26 12:45:53 -0800306 };
Ian Rogers62d6c772013-02-27 08:32:07 -0800307 if (kVerboseInstrumentation) {
308 std::string thread_name;
309 thread->GetThreadName(thread_name);
310 LOG(INFO) << "Installing exit stubs in " << thread_name;
Ian Rogers306057f2012-11-26 12:45:53 -0800311 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100312
313 Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg);
Ian Rogers700a4022014-05-19 16:49:03 -0700314 std::unique_ptr<Context> context(Context::Create());
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700315 uintptr_t instrumentation_exit_pc = reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc());
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100316 InstallStackVisitor visitor(thread, context.get(), instrumentation_exit_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800317 visitor.WalkStack(true);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100318 CHECK_EQ(visitor.dex_pcs_.size(), thread->GetInstrumentationStack()->size());
Ian Rogers62d6c772013-02-27 08:32:07 -0800319
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100320 if (instrumentation->ShouldNotifyMethodEnterExitEvents()) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100321 // Create method enter events for all methods currently on the thread's stack. We only do this
322 // if no debugger is attached to prevent from posting events twice.
Jeff Haoa15a81b2014-05-27 18:25:47 -0700323 auto ssi = visitor.shadow_stack_.rbegin();
324 for (auto isi = thread->GetInstrumentationStack()->rbegin(),
325 end = thread->GetInstrumentationStack()->rend(); isi != end; ++isi) {
326 while (ssi != visitor.shadow_stack_.rend() && (*ssi).frame_id_ < (*isi).frame_id_) {
327 instrumentation->MethodEnterEvent(thread, (*ssi).this_object_, (*ssi).method_, 0);
328 ++ssi;
329 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100330 uint32_t dex_pc = visitor.dex_pcs_.back();
331 visitor.dex_pcs_.pop_back();
Sebastien Hertz320deb22014-06-11 19:45:05 +0200332 if (!isi->interpreter_entry_) {
333 instrumentation->MethodEnterEvent(thread, (*isi).this_object_, (*isi).method_, dex_pc);
334 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100335 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800336 }
337 thread->VerifyStack();
Ian Rogers306057f2012-11-26 12:45:53 -0800338}
339
Mingyao Yang99170c62015-07-06 11:10:37 -0700340void Instrumentation::InstrumentThreadStack(Thread* thread) {
341 instrumentation_stubs_installed_ = true;
342 InstrumentationInstallStack(thread, this);
343}
344
Ian Rogers62d6c772013-02-27 08:32:07 -0800345// Removes the instrumentation exit pc as the return PC for every quick frame.
346static void InstrumentationRestoreStack(Thread* thread, void* arg)
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +0000347 REQUIRES(Locks::mutator_lock_) {
348 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
349
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200350 struct RestoreStackVisitor FINAL : public StackVisitor {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800351 RestoreStackVisitor(Thread* thread_in, uintptr_t instrumentation_exit_pc,
Ian Rogers62d6c772013-02-27 08:32:07 -0800352 Instrumentation* instrumentation)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100353 : StackVisitor(thread_in, nullptr, kInstrumentationStackWalk),
354 thread_(thread_in),
Ian Rogers62d6c772013-02-27 08:32:07 -0800355 instrumentation_exit_pc_(instrumentation_exit_pc),
356 instrumentation_(instrumentation),
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800357 instrumentation_stack_(thread_in->GetInstrumentationStack()),
Ian Rogers62d6c772013-02-27 08:32:07 -0800358 frames_removed_(0) {}
Ian Rogers306057f2012-11-26 12:45:53 -0800359
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700360 bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800361 if (instrumentation_stack_->size() == 0) {
jeffhao725a9572012-11-13 18:20:12 -0800362 return false; // Stop.
363 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700364 ArtMethod* m = GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700365 if (GetCurrentQuickFrame() == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800366 if (kVerboseInstrumentation) {
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200367 LOG(INFO) << " Ignoring a shadow frame. Frame " << GetFrameId()
David Sehr709b0702016-10-13 09:12:37 -0700368 << " Method=" << ArtMethod::PrettyMethod(m);
Ian Rogers62d6c772013-02-27 08:32:07 -0800369 }
370 return true; // Ignore shadow frames.
371 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700372 if (m == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800373 if (kVerboseInstrumentation) {
374 LOG(INFO) << " Skipping upcall. Frame " << GetFrameId();
375 }
Ian Rogers306057f2012-11-26 12:45:53 -0800376 return true; // Ignore upcalls.
377 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800378 bool removed_stub = false;
379 // TODO: make this search more efficient?
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100380 const size_t frameId = GetFrameId();
381 for (const InstrumentationStackFrame& instrumentation_frame : *instrumentation_stack_) {
382 if (instrumentation_frame.frame_id_ == frameId) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800383 if (kVerboseInstrumentation) {
384 LOG(INFO) << " Removing exit stub in " << DescribeLocation();
385 }
Jeff Hao9a916d32013-06-27 18:45:37 -0700386 if (instrumentation_frame.interpreter_entry_) {
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700387 CHECK(m == Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs));
Jeff Hao9a916d32013-06-27 18:45:37 -0700388 } else {
David Sehr709b0702016-10-13 09:12:37 -0700389 CHECK(m == instrumentation_frame.method_) << ArtMethod::PrettyMethod(m);
Jeff Hao9a916d32013-06-27 18:45:37 -0700390 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800391 SetReturnPc(instrumentation_frame.return_pc_);
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100392 if (instrumentation_->ShouldNotifyMethodEnterExitEvents()) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100393 // Create the method exit events. As the methods didn't really exit the result is 0.
394 // We only do this if no debugger is attached to prevent from posting events twice.
395 instrumentation_->MethodExitEvent(thread_, instrumentation_frame.this_object_, m,
396 GetDexPc(), JValue());
397 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800398 frames_removed_++;
399 removed_stub = true;
400 break;
401 }
402 }
403 if (!removed_stub) {
404 if (kVerboseInstrumentation) {
405 LOG(INFO) << " No exit stub in " << DescribeLocation();
Ian Rogers306057f2012-11-26 12:45:53 -0800406 }
jeffhao725a9572012-11-13 18:20:12 -0800407 }
408 return true; // Continue.
409 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800410 Thread* const thread_;
Ian Rogers306057f2012-11-26 12:45:53 -0800411 const uintptr_t instrumentation_exit_pc_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800412 Instrumentation* const instrumentation_;
413 std::deque<instrumentation::InstrumentationStackFrame>* const instrumentation_stack_;
414 size_t frames_removed_;
jeffhao725a9572012-11-13 18:20:12 -0800415 };
Ian Rogers62d6c772013-02-27 08:32:07 -0800416 if (kVerboseInstrumentation) {
417 std::string thread_name;
418 thread->GetThreadName(thread_name);
419 LOG(INFO) << "Removing exit stubs in " << thread_name;
420 }
421 std::deque<instrumentation::InstrumentationStackFrame>* stack = thread->GetInstrumentationStack();
422 if (stack->size() > 0) {
423 Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700424 uintptr_t instrumentation_exit_pc =
425 reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc());
Ian Rogers62d6c772013-02-27 08:32:07 -0800426 RestoreStackVisitor visitor(thread, instrumentation_exit_pc, instrumentation);
427 visitor.WalkStack(true);
428 CHECK_EQ(visitor.frames_removed_, stack->size());
429 while (stack->size() > 0) {
430 stack->pop_front();
431 }
jeffhao725a9572012-11-13 18:20:12 -0800432 }
433}
434
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200435static bool HasEvent(Instrumentation::InstrumentationEvent expected, uint32_t events) {
436 return (events & expected) != 0;
437}
438
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000439static void PotentiallyAddListenerTo(Instrumentation::InstrumentationEvent event,
440 uint32_t events,
441 std::list<InstrumentationListener*>& list,
442 InstrumentationListener* listener,
443 bool* has_listener)
444 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_) {
445 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
446 if (!HasEvent(event, events)) {
447 return;
448 }
449 // If there is a free slot in the list, we insert the listener in that slot.
450 // Otherwise we add it to the end of the list.
451 auto it = std::find(list.begin(), list.end(), nullptr);
452 if (it != list.end()) {
453 *it = listener;
454 } else {
455 list.push_back(listener);
456 }
457 *has_listener = true;
458}
459
Ian Rogers62d6c772013-02-27 08:32:07 -0800460void Instrumentation::AddListener(InstrumentationListener* listener, uint32_t events) {
461 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000462 PotentiallyAddListenerTo(kMethodEntered,
463 events,
464 method_entry_listeners_,
465 listener,
466 &have_method_entry_listeners_);
467 PotentiallyAddListenerTo(kMethodExited,
468 events,
469 method_exit_listeners_,
470 listener,
471 &have_method_exit_listeners_);
472 PotentiallyAddListenerTo(kMethodUnwind,
473 events,
474 method_unwind_listeners_,
475 listener,
476 &have_method_unwind_listeners_);
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000477 PotentiallyAddListenerTo(kBranch,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000478 events,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000479 branch_listeners_,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000480 listener,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000481 &have_branch_listeners_);
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000482 PotentiallyAddListenerTo(kInvokeVirtualOrInterface,
483 events,
484 invoke_virtual_or_interface_listeners_,
485 listener,
486 &have_invoke_virtual_or_interface_listeners_);
487 PotentiallyAddListenerTo(kDexPcMoved,
488 events,
489 dex_pc_listeners_,
490 listener,
491 &have_dex_pc_listeners_);
492 PotentiallyAddListenerTo(kFieldRead,
493 events,
494 field_read_listeners_,
495 listener,
496 &have_field_read_listeners_);
497 PotentiallyAddListenerTo(kFieldWritten,
498 events,
499 field_write_listeners_,
500 listener,
501 &have_field_write_listeners_);
502 PotentiallyAddListenerTo(kExceptionCaught,
503 events,
504 exception_caught_listeners_,
505 listener,
506 &have_exception_caught_listeners_);
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200507 UpdateInterpreterHandlerTable();
jeffhao725a9572012-11-13 18:20:12 -0800508}
509
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000510static void PotentiallyRemoveListenerFrom(Instrumentation::InstrumentationEvent event,
511 uint32_t events,
512 std::list<InstrumentationListener*>& list,
513 InstrumentationListener* listener,
514 bool* has_listener)
515 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_) {
516 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
517 if (!HasEvent(event, events)) {
518 return;
519 }
520 auto it = std::find(list.begin(), list.end(), listener);
521 if (it != list.end()) {
522 // Just update the entry, do not remove from the list. Removing entries in the list
523 // is unsafe when mutators are iterating over it.
524 *it = nullptr;
525 }
526
527 // Check if the list contains any non-null listener, and update 'has_listener'.
528 for (InstrumentationListener* l : list) {
529 if (l != nullptr) {
530 *has_listener = true;
531 return;
532 }
533 }
534 *has_listener = false;
535}
536
Ian Rogers62d6c772013-02-27 08:32:07 -0800537void Instrumentation::RemoveListener(InstrumentationListener* listener, uint32_t events) {
538 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000539 PotentiallyRemoveListenerFrom(kMethodEntered,
540 events,
541 method_entry_listeners_,
542 listener,
543 &have_method_entry_listeners_);
544 PotentiallyRemoveListenerFrom(kMethodExited,
545 events,
546 method_exit_listeners_,
547 listener,
548 &have_method_exit_listeners_);
549 PotentiallyRemoveListenerFrom(kMethodUnwind,
550 events,
551 method_unwind_listeners_,
552 listener,
553 &have_method_unwind_listeners_);
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000554 PotentiallyRemoveListenerFrom(kBranch,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000555 events,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000556 branch_listeners_,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000557 listener,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000558 &have_branch_listeners_);
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000559 PotentiallyRemoveListenerFrom(kInvokeVirtualOrInterface,
560 events,
561 invoke_virtual_or_interface_listeners_,
562 listener,
563 &have_invoke_virtual_or_interface_listeners_);
564 PotentiallyRemoveListenerFrom(kDexPcMoved,
565 events,
566 dex_pc_listeners_,
567 listener,
568 &have_dex_pc_listeners_);
569 PotentiallyRemoveListenerFrom(kFieldRead,
570 events,
571 field_read_listeners_,
572 listener,
573 &have_field_read_listeners_);
574 PotentiallyRemoveListenerFrom(kFieldWritten,
575 events,
576 field_write_listeners_,
577 listener,
578 &have_field_write_listeners_);
579 PotentiallyRemoveListenerFrom(kExceptionCaught,
580 events,
581 exception_caught_listeners_,
582 listener,
583 &have_exception_caught_listeners_);
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200584 UpdateInterpreterHandlerTable();
jeffhao725a9572012-11-13 18:20:12 -0800585}
586
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200587Instrumentation::InstrumentationLevel Instrumentation::GetCurrentInstrumentationLevel() const {
Alex Light4ba388a2017-01-27 10:26:49 -0800588 if (interpreter_stubs_installed_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200589 return InstrumentationLevel::kInstrumentWithInterpreter;
Ian Rogers62d6c772013-02-27 08:32:07 -0800590 } else if (entry_exit_stubs_installed_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200591 return InstrumentationLevel::kInstrumentWithInstrumentationStubs;
Ian Rogers62d6c772013-02-27 08:32:07 -0800592 } else {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200593 return InstrumentationLevel::kInstrumentNothing;
Ian Rogers62d6c772013-02-27 08:32:07 -0800594 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200595}
596
Alex Lightdba61482016-12-21 08:20:29 -0800597bool Instrumentation::RequiresInstrumentationInstallation(InstrumentationLevel new_level) const {
Alex Light4ba388a2017-01-27 10:26:49 -0800598 // We need to reinstall instrumentation if we go to a different level.
599 return GetCurrentInstrumentationLevel() != new_level;
Alex Lightdba61482016-12-21 08:20:29 -0800600}
601
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200602void Instrumentation::ConfigureStubs(const char* key, InstrumentationLevel desired_level) {
603 // Store the instrumentation level for this key or remove it.
604 if (desired_level == InstrumentationLevel::kInstrumentNothing) {
605 // The client no longer needs instrumentation.
606 requested_instrumentation_levels_.erase(key);
607 } else {
608 // The client needs instrumentation.
609 requested_instrumentation_levels_.Overwrite(key, desired_level);
610 }
611
612 // Look for the highest required instrumentation level.
613 InstrumentationLevel requested_level = InstrumentationLevel::kInstrumentNothing;
614 for (const auto& v : requested_instrumentation_levels_) {
615 requested_level = std::max(requested_level, v.second);
616 }
617
618 interpret_only_ = (requested_level == InstrumentationLevel::kInstrumentWithInterpreter) ||
619 forced_interpret_only_;
620
Alex Lightdba61482016-12-21 08:20:29 -0800621 if (!RequiresInstrumentationInstallation(requested_level)) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800622 // We're already set.
623 return;
624 }
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100625 Thread* const self = Thread::Current();
Ian Rogers62d6c772013-02-27 08:32:07 -0800626 Runtime* runtime = Runtime::Current();
Sebastien Hertza8a697f2015-01-15 12:28:47 +0100627 Locks::mutator_lock_->AssertExclusiveHeld(self);
Ian Rogers62d6c772013-02-27 08:32:07 -0800628 Locks::thread_list_lock_->AssertNotHeld(self);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200629 if (requested_level > InstrumentationLevel::kInstrumentNothing) {
Alex Light4ba388a2017-01-27 10:26:49 -0800630 if (requested_level == InstrumentationLevel::kInstrumentWithInterpreter) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800631 interpreter_stubs_installed_ = true;
Ian Rogers62d6c772013-02-27 08:32:07 -0800632 entry_exit_stubs_installed_ = true;
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200633 } else {
634 CHECK_EQ(requested_level, InstrumentationLevel::kInstrumentWithInstrumentationStubs);
635 entry_exit_stubs_installed_ = true;
636 interpreter_stubs_installed_ = false;
Ian Rogers62d6c772013-02-27 08:32:07 -0800637 }
Mathieu Chartiere0671ce2015-07-28 17:23:28 -0700638 InstallStubsClassVisitor visitor(this);
639 runtime->GetClassLinker()->VisitClasses(&visitor);
Ian Rogers62d6c772013-02-27 08:32:07 -0800640 instrumentation_stubs_installed_ = true;
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100641 MutexLock mu(self, *Locks::thread_list_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800642 runtime->GetThreadList()->ForEach(InstrumentationInstallStack, this);
643 } else {
644 interpreter_stubs_installed_ = false;
645 entry_exit_stubs_installed_ = false;
Mathieu Chartiere0671ce2015-07-28 17:23:28 -0700646 InstallStubsClassVisitor visitor(this);
647 runtime->GetClassLinker()->VisitClasses(&visitor);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100648 // Restore stack only if there is no method currently deoptimized.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700649 bool empty;
650 {
651 ReaderMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700652 empty = IsDeoptimizedMethodsEmpty(); // Avoid lock violation.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700653 }
654 if (empty) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100655 MutexLock mu(self, *Locks::thread_list_lock_);
656 Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this);
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +0000657 // Only do this after restoring, as walking the stack when restoring will see
658 // the instrumentation exit pc.
659 instrumentation_stubs_installed_ = false;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100660 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800661 }
jeffhao725a9572012-11-13 18:20:12 -0800662}
663
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200664static void ResetQuickAllocEntryPointsForThread(Thread* thread, void* arg ATTRIBUTE_UNUSED) {
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800665 thread->ResetQuickAllocEntryPointsForThread(kUseReadBarrier && thread->GetIsGcMarking());
Ian Rogersfa824272013-11-05 16:12:57 -0800666}
667
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700668void Instrumentation::SetEntrypointsInstrumented(bool instrumented) {
669 Thread* self = Thread::Current();
Mathieu Chartier661974a2014-01-09 11:23:53 -0800670 Runtime* runtime = Runtime::Current();
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700671 Locks::mutator_lock_->AssertNotHeld(self);
672 Locks::instrument_entrypoints_lock_->AssertHeld(self);
673 if (runtime->IsStarted()) {
Mathieu Chartier4f55e222015-09-04 13:26:21 -0700674 ScopedSuspendAll ssa(__FUNCTION__);
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700675 MutexLock mu(self, *Locks::runtime_shutdown_lock_);
Mathieu Chartier661974a2014-01-09 11:23:53 -0800676 SetQuickAllocEntryPointsInstrumented(instrumented);
677 ResetQuickAllocEntryPoints();
Mathieu Chartier50e93312016-03-16 11:25:29 -0700678 alloc_entrypoints_instrumented_ = instrumented;
Mathieu Chartier4f55e222015-09-04 13:26:21 -0700679 } else {
680 MutexLock mu(self, *Locks::runtime_shutdown_lock_);
681 SetQuickAllocEntryPointsInstrumented(instrumented);
Andreas Gampe157c77e2016-10-17 17:44:41 -0700682
683 // Note: ResetQuickAllocEntryPoints only works when the runtime is started. Manually run the
684 // update for just this thread.
Andreas Gampe162ae502016-10-18 10:03:42 -0700685 // Note: self may be null. One of those paths is setting instrumentation in the Heap
686 // constructor for gcstress mode.
687 if (self != nullptr) {
688 ResetQuickAllocEntryPointsForThread(self, nullptr);
689 }
Andreas Gampe157c77e2016-10-17 17:44:41 -0700690
Mathieu Chartier50e93312016-03-16 11:25:29 -0700691 alloc_entrypoints_instrumented_ = instrumented;
Mathieu Chartier661974a2014-01-09 11:23:53 -0800692 }
693}
694
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700695void Instrumentation::InstrumentQuickAllocEntryPoints() {
696 MutexLock mu(Thread::Current(), *Locks::instrument_entrypoints_lock_);
697 InstrumentQuickAllocEntryPointsLocked();
Ian Rogersfa824272013-11-05 16:12:57 -0800698}
699
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700700void Instrumentation::UninstrumentQuickAllocEntryPoints() {
701 MutexLock mu(Thread::Current(), *Locks::instrument_entrypoints_lock_);
702 UninstrumentQuickAllocEntryPointsLocked();
703}
704
705void Instrumentation::InstrumentQuickAllocEntryPointsLocked() {
706 Locks::instrument_entrypoints_lock_->AssertHeld(Thread::Current());
707 if (quick_alloc_entry_points_instrumentation_counter_ == 0) {
708 SetEntrypointsInstrumented(true);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800709 }
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700710 ++quick_alloc_entry_points_instrumentation_counter_;
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700711}
712
713void Instrumentation::UninstrumentQuickAllocEntryPointsLocked() {
714 Locks::instrument_entrypoints_lock_->AssertHeld(Thread::Current());
715 CHECK_GT(quick_alloc_entry_points_instrumentation_counter_, 0U);
716 --quick_alloc_entry_points_instrumentation_counter_;
717 if (quick_alloc_entry_points_instrumentation_counter_ == 0) {
718 SetEntrypointsInstrumented(false);
719 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800720}
721
722void Instrumentation::ResetQuickAllocEntryPoints() {
723 Runtime* runtime = Runtime::Current();
724 if (runtime->IsStarted()) {
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800725 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700726 runtime->GetThreadList()->ForEach(ResetQuickAllocEntryPointsForThread, nullptr);
Ian Rogersfa824272013-11-05 16:12:57 -0800727 }
728}
729
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700730void Instrumentation::UpdateMethodsCodeImpl(ArtMethod* method, const void* quick_code) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800731 const void* new_quick_code;
Ian Rogers62d6c772013-02-27 08:32:07 -0800732 if (LIKELY(!instrumentation_stubs_installed_)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800733 new_quick_code = quick_code;
Jeff Hao65d15d92013-07-16 16:39:33 -0700734 } else {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100735 if ((interpreter_stubs_installed_ || IsDeoptimized(method)) && !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800736 new_quick_code = GetQuickToInterpreterBridge();
Jeff Hao65d15d92013-07-16 16:39:33 -0700737 } else {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700738 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700739 if (class_linker->IsQuickResolutionStub(quick_code) ||
740 class_linker->IsQuickToInterpreterBridge(quick_code)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700741 new_quick_code = quick_code;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700742 } else if (entry_exit_stubs_installed_) {
743 new_quick_code = GetQuickInstrumentationEntryPoint();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700744 } else {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700745 new_quick_code = quick_code;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700746 }
Jeff Hao65d15d92013-07-16 16:39:33 -0700747 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800748 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800749 UpdateEntrypoints(method, new_quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100750}
751
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700752void Instrumentation::UpdateMethodsCode(ArtMethod* method, const void* quick_code) {
753 DCHECK(method->GetDeclaringClass()->IsResolved());
754 UpdateMethodsCodeImpl(method, quick_code);
755}
756
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000757void Instrumentation::UpdateMethodsCodeForJavaDebuggable(ArtMethod* method,
758 const void* quick_code) {
759 // When the runtime is set to Java debuggable, we may update the entry points of
760 // all methods of a class to the interpreter bridge. A method's declaring class
761 // might not be in resolved state yet in that case, so we bypass the DCHECK in
762 // UpdateMethodsCode.
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700763 UpdateMethodsCodeImpl(method, quick_code);
764}
765
Mathieu Chartiere401d142015-04-22 13:56:20 -0700766bool Instrumentation::AddDeoptimizedMethod(ArtMethod* method) {
767 if (IsDeoptimizedMethod(method)) {
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700768 // Already in the map. Return.
769 return false;
770 }
771 // Not found. Add it.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700772 deoptimized_methods_.insert(method);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700773 return true;
774}
775
Mathieu Chartiere401d142015-04-22 13:56:20 -0700776bool Instrumentation::IsDeoptimizedMethod(ArtMethod* method) {
777 return deoptimized_methods_.find(method) != deoptimized_methods_.end();
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700778}
779
Mathieu Chartiere401d142015-04-22 13:56:20 -0700780ArtMethod* Instrumentation::BeginDeoptimizedMethod() {
781 if (deoptimized_methods_.empty()) {
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700782 // Empty.
783 return nullptr;
784 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700785 return *deoptimized_methods_.begin();
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700786}
787
Mathieu Chartiere401d142015-04-22 13:56:20 -0700788bool Instrumentation::RemoveDeoptimizedMethod(ArtMethod* method) {
789 auto it = deoptimized_methods_.find(method);
790 if (it == deoptimized_methods_.end()) {
791 return false;
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700792 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700793 deoptimized_methods_.erase(it);
794 return true;
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700795}
796
797bool Instrumentation::IsDeoptimizedMethodsEmpty() const {
798 return deoptimized_methods_.empty();
799}
800
Mathieu Chartiere401d142015-04-22 13:56:20 -0700801void Instrumentation::Deoptimize(ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100802 CHECK(!method->IsNative());
803 CHECK(!method->IsProxyMethod());
Alex Light9139e002015-10-09 15:59:48 -0700804 CHECK(method->IsInvokable());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100805
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700806 Thread* self = Thread::Current();
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700807 {
808 WriterMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700809 bool has_not_been_deoptimized = AddDeoptimizedMethod(method);
David Sehr709b0702016-10-13 09:12:37 -0700810 CHECK(has_not_been_deoptimized) << "Method " << ArtMethod::PrettyMethod(method)
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200811 << " is already deoptimized";
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700812 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100813 if (!interpreter_stubs_installed_) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800814 UpdateEntrypoints(method, GetQuickInstrumentationEntryPoint());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100815
816 // Install instrumentation exit stub and instrumentation frames. We may already have installed
817 // these previously so it will only cover the newly created frames.
818 instrumentation_stubs_installed_ = true;
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700819 MutexLock mu(self, *Locks::thread_list_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100820 Runtime::Current()->GetThreadList()->ForEach(InstrumentationInstallStack, this);
821 }
822}
823
Mathieu Chartiere401d142015-04-22 13:56:20 -0700824void Instrumentation::Undeoptimize(ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100825 CHECK(!method->IsNative());
826 CHECK(!method->IsProxyMethod());
Alex Light9139e002015-10-09 15:59:48 -0700827 CHECK(method->IsInvokable());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100828
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700829 Thread* self = Thread::Current();
830 bool empty;
831 {
832 WriterMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700833 bool found_and_erased = RemoveDeoptimizedMethod(method);
David Sehr709b0702016-10-13 09:12:37 -0700834 CHECK(found_and_erased) << "Method " << ArtMethod::PrettyMethod(method)
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700835 << " is not deoptimized";
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700836 empty = IsDeoptimizedMethodsEmpty();
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700837 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100838
839 // Restore code and possibly stack only if we did not deoptimize everything.
840 if (!interpreter_stubs_installed_) {
841 // Restore its code or resolution trampoline.
842 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800843 if (method->IsStatic() && !method->IsConstructor() &&
844 !method->GetDeclaringClass()->IsInitialized()) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800845 UpdateEntrypoints(method, GetQuickResolutionStub());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100846 } else {
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000847 const void* quick_code = NeedDebugVersionFor(method)
848 ? GetQuickToInterpreterBridge()
849 : class_linker->GetQuickOatCodeFor(method);
Elliott Hughes956af0f2014-12-11 14:34:28 -0800850 UpdateEntrypoints(method, quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100851 }
852
853 // If there is no deoptimized method left, we can restore the stack of each thread.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700854 if (empty) {
855 MutexLock mu(self, *Locks::thread_list_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100856 Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this);
857 instrumentation_stubs_installed_ = false;
858 }
859 }
860}
861
Mathieu Chartiere401d142015-04-22 13:56:20 -0700862bool Instrumentation::IsDeoptimized(ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100863 DCHECK(method != nullptr);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700864 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700865 return IsDeoptimizedMethod(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100866}
867
868void Instrumentation::EnableDeoptimization() {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700869 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700870 CHECK(IsDeoptimizedMethodsEmpty());
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100871 CHECK_EQ(deoptimization_enabled_, false);
872 deoptimization_enabled_ = true;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100873}
874
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200875void Instrumentation::DisableDeoptimization(const char* key) {
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100876 CHECK_EQ(deoptimization_enabled_, true);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100877 // If we deoptimized everything, undo it.
Alex Lightdba61482016-12-21 08:20:29 -0800878 InstrumentationLevel level = GetCurrentInstrumentationLevel();
879 if (level == InstrumentationLevel::kInstrumentWithInterpreter) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200880 UndeoptimizeEverything(key);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100881 }
882 // Undeoptimized selected methods.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700883 while (true) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700884 ArtMethod* method;
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700885 {
886 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700887 if (IsDeoptimizedMethodsEmpty()) {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700888 break;
889 }
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700890 method = BeginDeoptimizedMethod();
891 CHECK(method != nullptr);
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700892 }
893 Undeoptimize(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100894 }
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100895 deoptimization_enabled_ = false;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100896}
897
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100898// Indicates if instrumentation should notify method enter/exit events to the listeners.
899bool Instrumentation::ShouldNotifyMethodEnterExitEvents() const {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200900 if (!HasMethodEntryListeners() && !HasMethodExitListeners()) {
901 return false;
902 }
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100903 return !deoptimization_enabled_ && !interpreter_stubs_installed_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100904}
905
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200906void Instrumentation::DeoptimizeEverything(const char* key) {
907 CHECK(deoptimization_enabled_);
908 ConfigureStubs(key, InstrumentationLevel::kInstrumentWithInterpreter);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100909}
910
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200911void Instrumentation::UndeoptimizeEverything(const char* key) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100912 CHECK(interpreter_stubs_installed_);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200913 CHECK(deoptimization_enabled_);
914 ConfigureStubs(key, InstrumentationLevel::kInstrumentNothing);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100915}
916
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200917void Instrumentation::EnableMethodTracing(const char* key, bool needs_interpreter) {
918 InstrumentationLevel level;
919 if (needs_interpreter) {
920 level = InstrumentationLevel::kInstrumentWithInterpreter;
921 } else {
922 level = InstrumentationLevel::kInstrumentWithInstrumentationStubs;
923 }
924 ConfigureStubs(key, level);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100925}
926
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200927void Instrumentation::DisableMethodTracing(const char* key) {
928 ConfigureStubs(key, InstrumentationLevel::kInstrumentNothing);
jeffhao725a9572012-11-13 18:20:12 -0800929}
930
Andreas Gampe542451c2016-07-26 09:02:02 -0700931const void* Instrumentation::GetQuickCodeFor(ArtMethod* method, PointerSize pointer_size) const {
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100932 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers62d6c772013-02-27 08:32:07 -0800933 if (LIKELY(!instrumentation_stubs_installed_)) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800934 const void* code = method->GetEntryPointFromQuickCompiledCodePtrSize(pointer_size);
Vladimir Marko8a630572014-04-09 18:45:35 +0100935 DCHECK(code != nullptr);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700936 if (LIKELY(!class_linker->IsQuickResolutionStub(code) &&
937 !class_linker->IsQuickToInterpreterBridge(code)) &&
938 !class_linker->IsQuickResolutionStub(code) &&
939 !class_linker->IsQuickToInterpreterBridge(code)) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800940 return code;
941 }
942 }
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100943 return class_linker->GetQuickOatCodeFor(method);
jeffhao725a9572012-11-13 18:20:12 -0800944}
945
Alex Lightd7661582017-05-01 13:48:16 -0700946void Instrumentation::MethodEnterEventImpl(Thread* thread,
947 ObjPtr<mirror::Object> this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700948 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800949 uint32_t dex_pc) const {
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000950 if (HasMethodEntryListeners()) {
Alex Lightd7661582017-05-01 13:48:16 -0700951 Thread* self = Thread::Current();
952 StackHandleScope<1> hs(self);
953 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000954 for (InstrumentationListener* listener : method_entry_listeners_) {
955 if (listener != nullptr) {
Alex Lightd7661582017-05-01 13:48:16 -0700956 listener->MethodEntered(thread, thiz, method, dex_pc);
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000957 }
958 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800959 }
960}
961
Alex Lightd7661582017-05-01 13:48:16 -0700962void Instrumentation::MethodExitEventImpl(Thread* thread,
963 ObjPtr<mirror::Object> this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700964 ArtMethod* method,
Alex Lightd7661582017-05-01 13:48:16 -0700965 uint32_t dex_pc,
966 const JValue& return_value) const {
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000967 if (HasMethodExitListeners()) {
Alex Lightd7661582017-05-01 13:48:16 -0700968 Thread* self = Thread::Current();
969 StackHandleScope<2> hs(self);
970 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
971 if (method->GetInterfaceMethodIfProxy(kRuntimePointerSize)
972 ->GetReturnTypePrimitive() != Primitive::kPrimNot) {
973 for (InstrumentationListener* listener : method_exit_listeners_) {
974 if (listener != nullptr) {
975 listener->MethodExited(thread, thiz, method, dex_pc, return_value);
976 }
977 }
978 } else {
979 Handle<mirror::Object> ret(hs.NewHandle(return_value.GetL()));
980 for (InstrumentationListener* listener : method_exit_listeners_) {
981 if (listener != nullptr) {
982 listener->MethodExited(thread, thiz, method, dex_pc, ret);
983 }
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000984 }
985 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800986 }
987}
988
Alex Lightd7661582017-05-01 13:48:16 -0700989void Instrumentation::MethodUnwindEvent(Thread* thread,
990 mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700991 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800992 uint32_t dex_pc) const {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200993 if (HasMethodUnwindListeners()) {
Alex Lightd7661582017-05-01 13:48:16 -0700994 Thread* self = Thread::Current();
995 StackHandleScope<1> hs(self);
996 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
Mathieu Chartier02e25112013-08-14 16:14:24 -0700997 for (InstrumentationListener* listener : method_unwind_listeners_) {
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000998 if (listener != nullptr) {
Alex Lightd7661582017-05-01 13:48:16 -0700999 listener->MethodUnwind(thread, thiz, method, dex_pc);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001000 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001001 }
1002 }
1003}
1004
Alex Lightd7661582017-05-01 13:48:16 -07001005void Instrumentation::DexPcMovedEventImpl(Thread* thread,
1006 ObjPtr<mirror::Object> this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001007 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -08001008 uint32_t dex_pc) const {
Alex Lightd7661582017-05-01 13:48:16 -07001009 Thread* self = Thread::Current();
1010 StackHandleScope<1> hs(self);
1011 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001012 for (InstrumentationListener* listener : dex_pc_listeners_) {
1013 if (listener != nullptr) {
Alex Lightd7661582017-05-01 13:48:16 -07001014 listener->DexPcMoved(thread, thiz, method, dex_pc);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001015 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001016 }
1017}
1018
Nicolas Geoffray81f0f952016-01-20 16:25:19 +00001019void Instrumentation::BranchImpl(Thread* thread,
1020 ArtMethod* method,
1021 uint32_t dex_pc,
1022 int32_t offset) const {
1023 for (InstrumentationListener* listener : branch_listeners_) {
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001024 if (listener != nullptr) {
Nicolas Geoffray81f0f952016-01-20 16:25:19 +00001025 listener->Branch(thread, method, dex_pc, offset);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001026 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001027 }
1028}
1029
Nicolas Geoffray5550ca82015-08-21 18:38:30 +01001030void Instrumentation::InvokeVirtualOrInterfaceImpl(Thread* thread,
Alex Lightd7661582017-05-01 13:48:16 -07001031 ObjPtr<mirror::Object> this_object,
Nicolas Geoffray5550ca82015-08-21 18:38:30 +01001032 ArtMethod* caller,
1033 uint32_t dex_pc,
1034 ArtMethod* callee) const {
Alex Lightd7661582017-05-01 13:48:16 -07001035 Thread* self = Thread::Current();
1036 StackHandleScope<1> hs(self);
1037 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
Nicolas Geoffray5550ca82015-08-21 18:38:30 +01001038 for (InstrumentationListener* listener : invoke_virtual_or_interface_listeners_) {
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001039 if (listener != nullptr) {
Alex Lightd7661582017-05-01 13:48:16 -07001040 listener->InvokeVirtualOrInterface(thread, thiz, caller, dex_pc, callee);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001041 }
Nicolas Geoffray5550ca82015-08-21 18:38:30 +01001042 }
1043}
1044
Alex Lightd7661582017-05-01 13:48:16 -07001045void Instrumentation::FieldReadEventImpl(Thread* thread,
1046 ObjPtr<mirror::Object> this_object,
1047 ArtMethod* method,
1048 uint32_t dex_pc,
Mathieu Chartierc7853442015-03-27 14:35:38 -07001049 ArtField* field) const {
Alex Lightd7661582017-05-01 13:48:16 -07001050 Thread* self = Thread::Current();
1051 StackHandleScope<1> hs(self);
1052 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001053 for (InstrumentationListener* listener : field_read_listeners_) {
1054 if (listener != nullptr) {
Alex Lightd7661582017-05-01 13:48:16 -07001055 listener->FieldRead(thread, thiz, method, dex_pc, field);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001056 }
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +02001057 }
1058}
1059
Alex Lightd7661582017-05-01 13:48:16 -07001060void Instrumentation::FieldWriteEventImpl(Thread* thread,
1061 ObjPtr<mirror::Object> this_object,
1062 ArtMethod* method,
1063 uint32_t dex_pc,
1064 ArtField* field,
1065 const JValue& field_value) const {
1066 Thread* self = Thread::Current();
1067 StackHandleScope<2> hs(self);
1068 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
1069 if (field->IsPrimitiveType()) {
1070 for (InstrumentationListener* listener : field_write_listeners_) {
1071 if (listener != nullptr) {
1072 listener->FieldWritten(thread, thiz, method, dex_pc, field, field_value);
1073 }
1074 }
1075 } else {
1076 Handle<mirror::Object> val(hs.NewHandle(field_value.GetL()));
1077 for (InstrumentationListener* listener : field_write_listeners_) {
1078 if (listener != nullptr) {
1079 listener->FieldWritten(thread, thiz, method, dex_pc, field, val);
1080 }
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001081 }
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +02001082 }
1083}
1084
Nicolas Geoffray14691c52015-03-05 10:40:17 +00001085void Instrumentation::ExceptionCaughtEvent(Thread* thread,
Sebastien Hertz947ff082013-09-17 14:10:13 +02001086 mirror::Throwable* exception_object) const {
Hiroshi Yamauchi3481f7a2017-02-10 12:07:36 -08001087 Thread* self = Thread::Current();
1088 StackHandleScope<1> hs(self);
1089 Handle<mirror::Throwable> h_exception(hs.NewHandle(exception_object));
Sebastien Hertz9f102032014-05-23 08:59:42 +02001090 if (HasExceptionCaughtListeners()) {
Hiroshi Yamauchi3481f7a2017-02-10 12:07:36 -08001091 DCHECK_EQ(thread->GetException(), h_exception.Get());
Jeff Haoc0bd4da2013-04-11 15:52:28 -07001092 thread->ClearException();
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001093 for (InstrumentationListener* listener : exception_caught_listeners_) {
1094 if (listener != nullptr) {
Alex Lightd7661582017-05-01 13:48:16 -07001095 listener->ExceptionCaught(thread, h_exception);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001096 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001097 }
Hiroshi Yamauchi3481f7a2017-02-10 12:07:36 -08001098 thread->SetException(h_exception.Get());
Ian Rogers62d6c772013-02-27 08:32:07 -08001099 }
1100}
1101
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +00001102// Computes a frame ID by ignoring inlined frames.
1103size_t Instrumentation::ComputeFrameId(Thread* self,
1104 size_t frame_depth,
1105 size_t inlined_frames_before_frame) {
1106 CHECK_GE(frame_depth, inlined_frames_before_frame);
1107 size_t no_inline_depth = frame_depth - inlined_frames_before_frame;
1108 return StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk) - no_inline_depth;
1109}
1110
Ian Rogers62d6c772013-02-27 08:32:07 -08001111static void CheckStackDepth(Thread* self, const InstrumentationStackFrame& instrumentation_frame,
1112 int delta)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001113 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +01001114 size_t frame_id = StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk) + delta;
Ian Rogers62d6c772013-02-27 08:32:07 -08001115 if (frame_id != instrumentation_frame.frame_id_) {
1116 LOG(ERROR) << "Expected frame_id=" << frame_id << " but found "
1117 << instrumentation_frame.frame_id_;
1118 StackVisitor::DescribeStack(self);
1119 CHECK_EQ(frame_id, instrumentation_frame.frame_id_);
1120 }
1121}
1122
1123void Instrumentation::PushInstrumentationStackFrame(Thread* self, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001124 ArtMethod* method,
Jeff Hao9a916d32013-06-27 18:45:37 -07001125 uintptr_t lr, bool interpreter_entry) {
Alex Lightb7edcda2017-04-27 13:20:31 -07001126 DCHECK(!self->IsExceptionPending());
Ian Rogers62d6c772013-02-27 08:32:07 -08001127 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
1128 if (kVerboseInstrumentation) {
David Sehr709b0702016-10-13 09:12:37 -07001129 LOG(INFO) << "Entering " << ArtMethod::PrettyMethod(method) << " from PC "
1130 << reinterpret_cast<void*>(lr);
Ian Rogers62d6c772013-02-27 08:32:07 -08001131 }
Alex Lightb7edcda2017-04-27 13:20:31 -07001132
1133 // We send the enter event before pushing the instrumentation frame to make cleanup easier. If the
1134 // event causes an exception we can simply send the unwind event and return.
1135 StackHandleScope<1> hs(self);
1136 Handle<mirror::Object> h_this(hs.NewHandle(this_object));
1137 if (!interpreter_entry) {
1138 MethodEnterEvent(self, h_this.Get(), method, 0);
1139 if (self->IsExceptionPending()) {
1140 MethodUnwindEvent(self, h_this.Get(), method, 0);
1141 return;
1142 }
1143 }
1144
1145 // We have a callee-save frame meaning this value is guaranteed to never be 0.
1146 DCHECK(!self->IsExceptionPending());
1147 size_t frame_id = StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk);
1148
1149 instrumentation::InstrumentationStackFrame instrumentation_frame(h_this.Get(), method, lr,
Jeff Hao9a916d32013-06-27 18:45:37 -07001150 frame_id, interpreter_entry);
Ian Rogers62d6c772013-02-27 08:32:07 -08001151 stack->push_front(instrumentation_frame);
Ian Rogers62d6c772013-02-27 08:32:07 -08001152}
1153
Alex Lightb7edcda2017-04-27 13:20:31 -07001154TwoWordReturn Instrumentation::PopInstrumentationStackFrame(Thread* self,
1155 uintptr_t* return_pc,
1156 uint64_t* gpr_result,
1157 uint64_t* fpr_result) {
1158 DCHECK(gpr_result != nullptr);
1159 DCHECK(fpr_result != nullptr);
Ian Rogers62d6c772013-02-27 08:32:07 -08001160 // Do the pop.
1161 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
1162 CHECK_GT(stack->size(), 0U);
1163 InstrumentationStackFrame instrumentation_frame = stack->front();
1164 stack->pop_front();
1165
1166 // Set return PC and check the sanity of the stack.
1167 *return_pc = instrumentation_frame.return_pc_;
1168 CheckStackDepth(self, instrumentation_frame, 0);
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001169 self->VerifyStack();
Ian Rogers62d6c772013-02-27 08:32:07 -08001170
Mathieu Chartiere401d142015-04-22 13:56:20 -07001171 ArtMethod* method = instrumentation_frame.method_;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001172 uint32_t length;
Andreas Gampe542451c2016-07-26 09:02:02 -07001173 const PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Mingyao Yang99170c62015-07-06 11:10:37 -07001174 char return_shorty = method->GetInterfaceMethodIfProxy(pointer_size)->GetShorty(&length)[0];
Alex Lightb7edcda2017-04-27 13:20:31 -07001175 bool is_ref = return_shorty == '[' || return_shorty == 'L';
1176 StackHandleScope<1> hs(self);
1177 MutableHandle<mirror::Object> res(hs.NewHandle<mirror::Object>(nullptr));
Ian Rogers62d6c772013-02-27 08:32:07 -08001178 JValue return_value;
1179 if (return_shorty == 'V') {
1180 return_value.SetJ(0);
1181 } else if (return_shorty == 'F' || return_shorty == 'D') {
Alex Lightb7edcda2017-04-27 13:20:31 -07001182 return_value.SetJ(*fpr_result);
Ian Rogers62d6c772013-02-27 08:32:07 -08001183 } else {
Alex Lightb7edcda2017-04-27 13:20:31 -07001184 return_value.SetJ(*gpr_result);
1185 }
1186 if (is_ref) {
1187 // Take a handle to the return value so we won't lose it if we suspend.
1188 res.Assign(return_value.GetL());
Ian Rogers62d6c772013-02-27 08:32:07 -08001189 }
1190 // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to
1191 // return_pc.
1192 uint32_t dex_pc = DexFile::kDexNoIndex;
1193 mirror::Object* this_object = instrumentation_frame.this_object_;
Sebastien Hertz320deb22014-06-11 19:45:05 +02001194 if (!instrumentation_frame.interpreter_entry_) {
1195 MethodExitEvent(self, this_object, instrumentation_frame.method_, dex_pc, return_value);
1196 }
jeffhao725a9572012-11-13 18:20:12 -08001197
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01001198 // Deoptimize if the caller needs to continue execution in the interpreter. Do nothing if we get
1199 // back to an upcall.
1200 NthCallerVisitor visitor(self, 1, true);
1201 visitor.WalkStack(true);
Sebastien Hertz270a0e12015-01-16 19:49:09 +01001202 bool deoptimize = (visitor.caller != nullptr) &&
Daniel Mihalyieb076692014-08-22 17:33:31 +02001203 (interpreter_stubs_installed_ || IsDeoptimized(visitor.caller) ||
1204 Dbg::IsForcedInterpreterNeededForUpcall(self, visitor.caller));
Alex Lightb7edcda2017-04-27 13:20:31 -07001205 if (is_ref) {
1206 // Restore the return value if it's a reference since it might have moved.
1207 *reinterpret_cast<mirror::Object**>(gpr_result) = res.Get();
1208 }
Nicolas Geoffraya0619e22016-12-20 13:57:43 +00001209 if (deoptimize && Runtime::Current()->IsAsyncDeoptimizeable(*return_pc)) {
Ian Rogers62d6c772013-02-27 08:32:07 -08001210 if (kVerboseInstrumentation) {
Andreas Gampe46ee31b2016-12-14 10:11:49 -08001211 LOG(INFO) << "Deoptimizing "
1212 << visitor.caller->PrettyMethod()
1213 << " by returning from "
1214 << method->PrettyMethod()
1215 << " with result "
1216 << std::hex << return_value.GetJ() << std::dec
1217 << " in "
1218 << *self;
Ian Rogers62d6c772013-02-27 08:32:07 -08001219 }
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01001220 self->PushDeoptimizationContext(return_value,
1221 return_shorty == 'L',
1222 false /* from_code */,
Sebastien Hertz07474662015-08-25 15:12:33 +00001223 nullptr /* no pending exception */);
Andreas Gamped58342c2014-06-05 14:18:08 -07001224 return GetTwoWordSuccessValue(*return_pc,
1225 reinterpret_cast<uintptr_t>(GetQuickDeoptimizationEntryPoint()));
Ian Rogers62d6c772013-02-27 08:32:07 -08001226 } else {
Nicolas Geoffraya0619e22016-12-20 13:57:43 +00001227 if (deoptimize && !Runtime::Current()->IsAsyncDeoptimizeable(*return_pc)) {
1228 LOG(WARNING) << "Got a deoptimization request on un-deoptimizable " << method->PrettyMethod()
1229 << " at PC " << reinterpret_cast<void*>(*return_pc);
1230 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001231 if (kVerboseInstrumentation) {
David Sehr709b0702016-10-13 09:12:37 -07001232 LOG(INFO) << "Returning from " << method->PrettyMethod()
Brian Carlstrom2d888622013-07-18 17:02:00 -07001233 << " to PC " << reinterpret_cast<void*>(*return_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -08001234 }
Andreas Gamped58342c2014-06-05 14:18:08 -07001235 return GetTwoWordSuccessValue(0, *return_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -08001236 }
jeffhao725a9572012-11-13 18:20:12 -08001237}
1238
Mingyao Yangf711f2c2016-05-23 12:29:39 -07001239uintptr_t Instrumentation::PopMethodForUnwind(Thread* self, bool is_deoptimization) const {
Ian Rogers62d6c772013-02-27 08:32:07 -08001240 // Do the pop.
1241 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
1242 CHECK_GT(stack->size(), 0U);
Alex Lightb7edcda2017-04-27 13:20:31 -07001243 size_t idx = stack->size();
Ian Rogers62d6c772013-02-27 08:32:07 -08001244 InstrumentationStackFrame instrumentation_frame = stack->front();
Ian Rogers62d6c772013-02-27 08:32:07 -08001245
Mathieu Chartiere401d142015-04-22 13:56:20 -07001246 ArtMethod* method = instrumentation_frame.method_;
Ian Rogers62d6c772013-02-27 08:32:07 -08001247 if (is_deoptimization) {
1248 if (kVerboseInstrumentation) {
David Sehr709b0702016-10-13 09:12:37 -07001249 LOG(INFO) << "Popping for deoptimization " << ArtMethod::PrettyMethod(method);
Ian Rogers62d6c772013-02-27 08:32:07 -08001250 }
1251 } else {
1252 if (kVerboseInstrumentation) {
David Sehr709b0702016-10-13 09:12:37 -07001253 LOG(INFO) << "Popping for unwind " << ArtMethod::PrettyMethod(method);
Ian Rogers62d6c772013-02-27 08:32:07 -08001254 }
1255
1256 // Notify listeners of method unwind.
1257 // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to
1258 // return_pc.
1259 uint32_t dex_pc = DexFile::kDexNoIndex;
1260 MethodUnwindEvent(self, instrumentation_frame.this_object_, method, dex_pc);
1261 }
Alex Lightb7edcda2017-04-27 13:20:31 -07001262 // TODO: bring back CheckStackDepth(self, instrumentation_frame, 2);
1263 CHECK_EQ(stack->size(), idx);
1264 DCHECK(instrumentation_frame.method_ == stack->front().method_);
1265 stack->pop_front();
Mingyao Yangf711f2c2016-05-23 12:29:39 -07001266 return instrumentation_frame.return_pc_;
Ian Rogers62d6c772013-02-27 08:32:07 -08001267}
1268
1269std::string InstrumentationStackFrame::Dump() const {
1270 std::ostringstream os;
David Sehr709b0702016-10-13 09:12:37 -07001271 os << "Frame " << frame_id_ << " " << ArtMethod::PrettyMethod(method_) << ":"
Ian Rogers62d6c772013-02-27 08:32:07 -08001272 << reinterpret_cast<void*>(return_pc_) << " this=" << reinterpret_cast<void*>(this_object_);
1273 return os.str();
1274}
1275
1276} // namespace instrumentation
jeffhao725a9572012-11-13 18:20:12 -08001277} // namespace art