blob: de25a6d1e697ab3ff42ca84b3b86a4d0299ecbc9 [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"
Alex Lightd7661582017-05-01 13:48:16 -070022#include "art_field-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070023#include "art_method-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"
Mingyao Yang047abb22017-08-23 15:26:57 -070029#include "dex_instruction-inl.h"
Mathieu Chartierd8891782014-03-02 13:28:37 -080030#include "entrypoints/quick/quick_alloc_entrypoints.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070031#include "entrypoints/quick/quick_entrypoints.h"
Ian Rogers6f3dbba2014-10-14 17:41:57 -070032#include "entrypoints/runtime_asm_entrypoints.h"
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070033#include "gc_root-inl.h"
Sebastien Hertz138dbfc2013-12-04 18:15:25 +010034#include "interpreter/interpreter.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080035#include "jit/jit.h"
36#include "jit/jit_code_cache.h"
Alex Lightd7661582017-05-01 13:48:16 -070037#include "jvalue-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080038#include "mirror/class-inl.h"
39#include "mirror/dex_cache.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070040#include "mirror/object-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070041#include "mirror/object_array-inl.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080042#include "nth_caller_visitor.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010043#include "oat_quick_method_header.h"
jeffhao725a9572012-11-13 18:20:12 -080044#include "thread.h"
45#include "thread_list.h"
jeffhao725a9572012-11-13 18:20:12 -080046
47namespace art {
Ian Rogers62d6c772013-02-27 08:32:07 -080048namespace instrumentation {
jeffhao725a9572012-11-13 18:20:12 -080049
Sebastien Hertz0462c4c2015-04-01 16:34:17 +020050constexpr bool kVerboseInstrumentation = false;
Sebastien Hertz5bfd5c92013-11-15 11:36:07 +010051
Alex Lightd7661582017-05-01 13:48:16 -070052void InstrumentationListener::MethodExited(Thread* thread,
53 Handle<mirror::Object> this_object,
54 ArtMethod* method,
55 uint32_t dex_pc,
56 Handle<mirror::Object> return_value) {
57 DCHECK_EQ(method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetReturnTypePrimitive(),
58 Primitive::kPrimNot);
59 JValue v;
60 v.SetL(return_value.Get());
61 MethodExited(thread, this_object, method, dex_pc, v);
62}
63
64void InstrumentationListener::FieldWritten(Thread* thread,
65 Handle<mirror::Object> this_object,
66 ArtMethod* method,
67 uint32_t dex_pc,
68 ArtField* field,
69 Handle<mirror::Object> field_value) {
70 DCHECK(!field->IsPrimitiveType());
71 JValue v;
72 v.SetL(field_value.Get());
73 FieldWritten(thread, this_object, method, dex_pc, field, v);
74}
75
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +010076// Instrumentation works on non-inlined frames by updating returned PCs
77// of compiled frames.
78static constexpr StackVisitor::StackWalkKind kInstrumentationStackWalk =
79 StackVisitor::StackWalkKind::kSkipInlinedFrames;
80
Mathieu Chartiere0671ce2015-07-28 17:23:28 -070081class InstallStubsClassVisitor : public ClassVisitor {
82 public:
83 explicit InstallStubsClassVisitor(Instrumentation* instrumentation)
84 : instrumentation_(instrumentation) {}
85
Mathieu Chartier28357fa2016-10-18 16:27:40 -070086 bool operator()(ObjPtr<mirror::Class> klass) OVERRIDE REQUIRES(Locks::mutator_lock_) {
87 instrumentation_->InstallStubsForClass(klass.Ptr());
Mathieu Chartiere0671ce2015-07-28 17:23:28 -070088 return true; // we visit all classes.
89 }
90
91 private:
92 Instrumentation* const instrumentation_;
93};
94
Ian Rogers62d6c772013-02-27 08:32:07 -080095
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -070096Instrumentation::Instrumentation()
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +000097 : instrumentation_stubs_installed_(false),
98 entry_exit_stubs_installed_(false),
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -070099 interpreter_stubs_installed_(false),
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +0000100 interpret_only_(false),
101 forced_interpret_only_(false),
102 have_method_entry_listeners_(false),
103 have_method_exit_listeners_(false),
104 have_method_unwind_listeners_(false),
105 have_dex_pc_listeners_(false),
106 have_field_read_listeners_(false),
107 have_field_write_listeners_(false),
108 have_exception_caught_listeners_(false),
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000109 have_branch_listeners_(false),
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +0000110 have_invoke_virtual_or_interface_listeners_(false),
Mathieu Chartierb8aa1e42016-04-05 14:36:57 -0700111 deoptimized_methods_lock_("deoptimized methods lock", kDeoptimizedMethodsLock),
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700112 deoptimization_enabled_(false),
113 interpreter_handler_table_(kMainHandlerTable),
Mathieu Chartier50e93312016-03-16 11:25:29 -0700114 quick_alloc_entry_points_instrumentation_counter_(0),
115 alloc_entrypoints_instrumented_(false) {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700116}
117
Sebastien Hertza10aa372015-01-21 17:30:58 +0100118void Instrumentation::InstallStubsForClass(mirror::Class* klass) {
Vladimir Marko72ab6842017-01-20 19:32:50 +0000119 if (!klass->IsResolved()) {
Sebastien Hertza8a697f2015-01-15 12:28:47 +0100120 // We need the class to be resolved to install/uninstall stubs. Otherwise its methods
121 // could not be initialized or linked with regards to class inheritance.
Vladimir Marko72ab6842017-01-20 19:32:50 +0000122 } else if (klass->IsErroneousResolved()) {
123 // We can't execute code in a erroneous class: do nothing.
Sebastien Hertza8a697f2015-01-15 12:28:47 +0100124 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700125 for (ArtMethod& method : klass->GetMethods(kRuntimePointerSize)) {
Alex Light51a64d52015-12-17 13:55:59 -0800126 InstallStubsForMethod(&method);
Sebastien Hertza8a697f2015-01-15 12:28:47 +0100127 }
jeffhao725a9572012-11-13 18:20:12 -0800128 }
jeffhao725a9572012-11-13 18:20:12 -0800129}
130
Mathieu Chartiere401d142015-04-22 13:56:20 -0700131static void UpdateEntrypoints(ArtMethod* method, const void* quick_code)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700132 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800133 method->SetEntryPointFromQuickCompiledCode(quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100134}
135
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000136bool Instrumentation::NeedDebugVersionFor(ArtMethod* method) const REQUIRES_SHARED(Locks::mutator_lock_) {
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800137 return Dbg::IsDebuggerActive() &&
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000138 Runtime::Current()->IsJavaDebuggable() &&
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800139 !method->IsNative() &&
140 !method->IsProxyMethod();
141}
142
Mathieu Chartiere401d142015-04-22 13:56:20 -0700143void Instrumentation::InstallStubsForMethod(ArtMethod* method) {
Alex Light9139e002015-10-09 15:59:48 -0700144 if (!method->IsInvokable() || method->IsProxyMethod()) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100145 // Do not change stubs for these methods.
146 return;
147 }
Jeff Hao56802772014-08-19 10:17:36 -0700148 // Don't stub Proxy.<init>. Note that the Proxy class itself is not a proxy class.
149 if (method->IsConstructor() &&
150 method->GetDeclaringClass()->DescriptorEquals("Ljava/lang/reflect/Proxy;")) {
Jeff Haodb8a6642014-08-14 17:18:52 -0700151 return;
152 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800153 const void* new_quick_code;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100154 bool uninstall = !entry_exit_stubs_installed_ && !interpreter_stubs_installed_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800155 Runtime* const runtime = Runtime::Current();
156 ClassLinker* const class_linker = runtime->GetClassLinker();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100157 bool is_class_initialized = method->GetDeclaringClass()->IsInitialized();
158 if (uninstall) {
159 if ((forced_interpret_only_ || IsDeoptimized(method)) && !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800160 new_quick_code = GetQuickToInterpreterBridge();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100161 } else if (is_class_initialized || !method->IsStatic() || method->IsConstructor()) {
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000162 if (NeedDebugVersionFor(method)) {
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800163 new_quick_code = GetQuickToInterpreterBridge();
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000164 } else {
165 new_quick_code = class_linker->GetQuickOatCodeFor(method);
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800166 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100167 } else {
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700168 new_quick_code = GetQuickResolutionStub();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100169 }
170 } else { // !uninstall
Sebastien Hertzbae182c2013-12-17 10:42:03 +0100171 if ((interpreter_stubs_installed_ || forced_interpret_only_ || IsDeoptimized(method)) &&
172 !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800173 new_quick_code = GetQuickToInterpreterBridge();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100174 } else {
175 // Do not overwrite resolution trampoline. When the trampoline initializes the method's
176 // class, all its static methods code will be set to the instrumentation entry point.
177 // For more details, see ClassLinker::FixupStaticTrampolines.
178 if (is_class_initialized || !method->IsStatic() || method->IsConstructor()) {
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000179 if (NeedDebugVersionFor(method)) {
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800180 // Oat code should not be used. Don't install instrumentation stub and
181 // use interpreter for instrumentation.
182 new_quick_code = GetQuickToInterpreterBridge();
183 } else if (entry_exit_stubs_installed_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800184 new_quick_code = GetQuickInstrumentationEntryPoint();
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000185 } else {
186 new_quick_code = class_linker->GetQuickOatCodeFor(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100187 }
188 } else {
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700189 new_quick_code = GetQuickResolutionStub();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100190 }
191 }
192 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800193 UpdateEntrypoints(method, new_quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100194}
195
Ian Rogers62d6c772013-02-27 08:32:07 -0800196// Places the instrumentation exit pc as the return PC for every quick frame. This also allows
197// deoptimization of quick frames to interpreter frames.
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100198// Since we may already have done this previously, we need to push new instrumentation frame before
199// existing instrumentation frames.
Ian Rogers62d6c772013-02-27 08:32:07 -0800200static void InstrumentationInstallStack(Thread* thread, void* arg)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700201 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200202 struct InstallStackVisitor FINAL : public StackVisitor {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800203 InstallStackVisitor(Thread* thread_in, Context* context, uintptr_t instrumentation_exit_pc)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100204 : StackVisitor(thread_in, context, kInstrumentationStackWalk),
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800205 instrumentation_stack_(thread_in->GetInstrumentationStack()),
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100206 instrumentation_exit_pc_(instrumentation_exit_pc),
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100207 reached_existing_instrumentation_frames_(false), instrumentation_stack_depth_(0),
208 last_return_pc_(0) {
209 }
jeffhao725a9572012-11-13 18:20:12 -0800210
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700211 bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700212 ArtMethod* m = GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700213 if (m == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800214 if (kVerboseInstrumentation) {
215 LOG(INFO) << " Skipping upcall. Frame " << GetFrameId();
216 }
217 last_return_pc_ = 0;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700218 return true; // Ignore upcalls.
Ian Rogers306057f2012-11-26 12:45:53 -0800219 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700220 if (GetCurrentQuickFrame() == nullptr) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800221 bool interpreter_frame = true;
Sebastien Hertz320deb22014-06-11 19:45:05 +0200222 InstrumentationStackFrame instrumentation_frame(GetThisObject(), m, 0, GetFrameId(),
223 interpreter_frame);
Jeff Haoa15a81b2014-05-27 18:25:47 -0700224 if (kVerboseInstrumentation) {
225 LOG(INFO) << "Pushing shadow frame " << instrumentation_frame.Dump();
226 }
227 shadow_stack_.push_back(instrumentation_frame);
228 return true; // Continue.
229 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800230 uintptr_t return_pc = GetReturnPc();
Sebastien Hertz320deb22014-06-11 19:45:05 +0200231 if (kVerboseInstrumentation) {
232 LOG(INFO) << " Installing exit stub in " << DescribeLocation();
233 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100234 if (return_pc == instrumentation_exit_pc_) {
Mingyao Yang047abb22017-08-23 15:26:57 -0700235 CHECK_LT(instrumentation_stack_depth_, instrumentation_stack_->size());
236
237 if (m->IsRuntimeMethod()) {
238 const InstrumentationStackFrame& frame =
239 instrumentation_stack_->at(instrumentation_stack_depth_);
240 if (frame.interpreter_entry_) {
241 // This instrumentation frame is for an interpreter bridge and is
242 // pushed when executing the instrumented interpreter bridge. So method
243 // enter event must have been reported. However we need to push a DEX pc
244 // into the dex_pcs_ list to match size of instrumentation stack.
245 uint32_t dex_pc = DexFile::kDexNoIndex;
246 dex_pcs_.push_back(dex_pc);
247 last_return_pc_ = frame.return_pc_;
248 ++instrumentation_stack_depth_;
249 return true;
250 }
251 }
252
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100253 // We've reached a frame which has already been installed with instrumentation exit stub.
254 // We should have already installed instrumentation on previous frames.
255 reached_existing_instrumentation_frames_ = true;
256
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200257 const InstrumentationStackFrame& frame =
258 instrumentation_stack_->at(instrumentation_stack_depth_);
David Sehr709b0702016-10-13 09:12:37 -0700259 CHECK_EQ(m, frame.method_) << "Expected " << ArtMethod::PrettyMethod(m)
260 << ", Found " << ArtMethod::PrettyMethod(frame.method_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100261 return_pc = frame.return_pc_;
262 if (kVerboseInstrumentation) {
263 LOG(INFO) << "Ignoring already instrumented " << frame.Dump();
264 }
265 } else {
266 CHECK_NE(return_pc, 0U);
267 CHECK(!reached_existing_instrumentation_frames_);
Mingyao Yang047abb22017-08-23 15:26:57 -0700268 InstrumentationStackFrame instrumentation_frame(
269 m->IsRuntimeMethod() ? nullptr : GetThisObject(),
270 m,
271 return_pc,
272 GetFrameId(), // A runtime method still gets a frame id.
273 false);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100274 if (kVerboseInstrumentation) {
275 LOG(INFO) << "Pushing frame " << instrumentation_frame.Dump();
276 }
277
Sebastien Hertz320deb22014-06-11 19:45:05 +0200278 // Insert frame at the right position so we do not corrupt the instrumentation stack.
279 // Instrumentation stack frames are in descending frame id order.
280 auto it = instrumentation_stack_->begin();
281 for (auto end = instrumentation_stack_->end(); it != end; ++it) {
282 const InstrumentationStackFrame& current = *it;
283 if (instrumentation_frame.frame_id_ >= current.frame_id_) {
284 break;
285 }
286 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100287 instrumentation_stack_->insert(it, instrumentation_frame);
288 SetReturnPc(instrumentation_exit_pc_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800289 }
Mingyao Yang047abb22017-08-23 15:26:57 -0700290 uint32_t dex_pc = DexFile::kDexNoIndex;
291 if (last_return_pc_ != 0 &&
292 GetCurrentOatQuickMethodHeader() != nullptr) {
293 dex_pc = GetCurrentOatQuickMethodHeader()->ToDexPc(m, last_return_pc_);
294 }
295 dex_pcs_.push_back(dex_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800296 last_return_pc_ = return_pc;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100297 ++instrumentation_stack_depth_;
Ian Rogers306057f2012-11-26 12:45:53 -0800298 return true; // Continue.
299 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800300 std::deque<InstrumentationStackFrame>* const instrumentation_stack_;
Jeff Haoa15a81b2014-05-27 18:25:47 -0700301 std::vector<InstrumentationStackFrame> shadow_stack_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800302 std::vector<uint32_t> dex_pcs_;
Ian Rogers306057f2012-11-26 12:45:53 -0800303 const uintptr_t instrumentation_exit_pc_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100304 bool reached_existing_instrumentation_frames_;
305 size_t instrumentation_stack_depth_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800306 uintptr_t last_return_pc_;
Ian Rogers306057f2012-11-26 12:45:53 -0800307 };
Ian Rogers62d6c772013-02-27 08:32:07 -0800308 if (kVerboseInstrumentation) {
309 std::string thread_name;
310 thread->GetThreadName(thread_name);
311 LOG(INFO) << "Installing exit stubs in " << thread_name;
Ian Rogers306057f2012-11-26 12:45:53 -0800312 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100313
314 Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg);
Ian Rogers700a4022014-05-19 16:49:03 -0700315 std::unique_ptr<Context> context(Context::Create());
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700316 uintptr_t instrumentation_exit_pc = reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc());
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100317 InstallStackVisitor visitor(thread, context.get(), instrumentation_exit_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800318 visitor.WalkStack(true);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100319 CHECK_EQ(visitor.dex_pcs_.size(), thread->GetInstrumentationStack()->size());
Ian Rogers62d6c772013-02-27 08:32:07 -0800320
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100321 if (instrumentation->ShouldNotifyMethodEnterExitEvents()) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100322 // Create method enter events for all methods currently on the thread's stack. We only do this
323 // if no debugger is attached to prevent from posting events twice.
Jeff Haoa15a81b2014-05-27 18:25:47 -0700324 auto ssi = visitor.shadow_stack_.rbegin();
325 for (auto isi = thread->GetInstrumentationStack()->rbegin(),
326 end = thread->GetInstrumentationStack()->rend(); isi != end; ++isi) {
327 while (ssi != visitor.shadow_stack_.rend() && (*ssi).frame_id_ < (*isi).frame_id_) {
328 instrumentation->MethodEnterEvent(thread, (*ssi).this_object_, (*ssi).method_, 0);
329 ++ssi;
330 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100331 uint32_t dex_pc = visitor.dex_pcs_.back();
332 visitor.dex_pcs_.pop_back();
Sebastien Hertz320deb22014-06-11 19:45:05 +0200333 if (!isi->interpreter_entry_) {
334 instrumentation->MethodEnterEvent(thread, (*isi).this_object_, (*isi).method_, dex_pc);
335 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100336 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800337 }
338 thread->VerifyStack();
Ian Rogers306057f2012-11-26 12:45:53 -0800339}
340
Mingyao Yang99170c62015-07-06 11:10:37 -0700341void Instrumentation::InstrumentThreadStack(Thread* thread) {
342 instrumentation_stubs_installed_ = true;
343 InstrumentationInstallStack(thread, this);
344}
345
Ian Rogers62d6c772013-02-27 08:32:07 -0800346// Removes the instrumentation exit pc as the return PC for every quick frame.
347static void InstrumentationRestoreStack(Thread* thread, void* arg)
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +0000348 REQUIRES(Locks::mutator_lock_) {
349 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
350
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200351 struct RestoreStackVisitor FINAL : public StackVisitor {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800352 RestoreStackVisitor(Thread* thread_in, uintptr_t instrumentation_exit_pc,
Ian Rogers62d6c772013-02-27 08:32:07 -0800353 Instrumentation* instrumentation)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100354 : StackVisitor(thread_in, nullptr, kInstrumentationStackWalk),
355 thread_(thread_in),
Ian Rogers62d6c772013-02-27 08:32:07 -0800356 instrumentation_exit_pc_(instrumentation_exit_pc),
357 instrumentation_(instrumentation),
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800358 instrumentation_stack_(thread_in->GetInstrumentationStack()),
Ian Rogers62d6c772013-02-27 08:32:07 -0800359 frames_removed_(0) {}
Ian Rogers306057f2012-11-26 12:45:53 -0800360
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700361 bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800362 if (instrumentation_stack_->size() == 0) {
jeffhao725a9572012-11-13 18:20:12 -0800363 return false; // Stop.
364 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700365 ArtMethod* m = GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700366 if (GetCurrentQuickFrame() == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800367 if (kVerboseInstrumentation) {
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200368 LOG(INFO) << " Ignoring a shadow frame. Frame " << GetFrameId()
David Sehr709b0702016-10-13 09:12:37 -0700369 << " Method=" << ArtMethod::PrettyMethod(m);
Ian Rogers62d6c772013-02-27 08:32:07 -0800370 }
371 return true; // Ignore shadow frames.
372 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700373 if (m == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800374 if (kVerboseInstrumentation) {
375 LOG(INFO) << " Skipping upcall. Frame " << GetFrameId();
376 }
Ian Rogers306057f2012-11-26 12:45:53 -0800377 return true; // Ignore upcalls.
378 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800379 bool removed_stub = false;
380 // TODO: make this search more efficient?
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100381 const size_t frameId = GetFrameId();
382 for (const InstrumentationStackFrame& instrumentation_frame : *instrumentation_stack_) {
383 if (instrumentation_frame.frame_id_ == frameId) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800384 if (kVerboseInstrumentation) {
385 LOG(INFO) << " Removing exit stub in " << DescribeLocation();
386 }
Jeff Hao9a916d32013-06-27 18:45:37 -0700387 if (instrumentation_frame.interpreter_entry_) {
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700388 CHECK(m == Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs));
Jeff Hao9a916d32013-06-27 18:45:37 -0700389 } else {
David Sehr709b0702016-10-13 09:12:37 -0700390 CHECK(m == instrumentation_frame.method_) << ArtMethod::PrettyMethod(m);
Jeff Hao9a916d32013-06-27 18:45:37 -0700391 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800392 SetReturnPc(instrumentation_frame.return_pc_);
Mingyao Yang047abb22017-08-23 15:26:57 -0700393 if (instrumentation_->ShouldNotifyMethodEnterExitEvents() &&
394 !m->IsRuntimeMethod()) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100395 // Create the method exit events. As the methods didn't really exit the result is 0.
396 // We only do this if no debugger is attached to prevent from posting events twice.
397 instrumentation_->MethodExitEvent(thread_, instrumentation_frame.this_object_, m,
398 GetDexPc(), JValue());
399 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800400 frames_removed_++;
401 removed_stub = true;
402 break;
403 }
404 }
405 if (!removed_stub) {
406 if (kVerboseInstrumentation) {
407 LOG(INFO) << " No exit stub in " << DescribeLocation();
Ian Rogers306057f2012-11-26 12:45:53 -0800408 }
jeffhao725a9572012-11-13 18:20:12 -0800409 }
410 return true; // Continue.
411 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800412 Thread* const thread_;
Ian Rogers306057f2012-11-26 12:45:53 -0800413 const uintptr_t instrumentation_exit_pc_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800414 Instrumentation* const instrumentation_;
415 std::deque<instrumentation::InstrumentationStackFrame>* const instrumentation_stack_;
416 size_t frames_removed_;
jeffhao725a9572012-11-13 18:20:12 -0800417 };
Ian Rogers62d6c772013-02-27 08:32:07 -0800418 if (kVerboseInstrumentation) {
419 std::string thread_name;
420 thread->GetThreadName(thread_name);
421 LOG(INFO) << "Removing exit stubs in " << thread_name;
422 }
423 std::deque<instrumentation::InstrumentationStackFrame>* stack = thread->GetInstrumentationStack();
424 if (stack->size() > 0) {
425 Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700426 uintptr_t instrumentation_exit_pc =
427 reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc());
Ian Rogers62d6c772013-02-27 08:32:07 -0800428 RestoreStackVisitor visitor(thread, instrumentation_exit_pc, instrumentation);
429 visitor.WalkStack(true);
430 CHECK_EQ(visitor.frames_removed_, stack->size());
431 while (stack->size() > 0) {
432 stack->pop_front();
433 }
jeffhao725a9572012-11-13 18:20:12 -0800434 }
435}
436
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200437static bool HasEvent(Instrumentation::InstrumentationEvent expected, uint32_t events) {
438 return (events & expected) != 0;
439}
440
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000441static void PotentiallyAddListenerTo(Instrumentation::InstrumentationEvent event,
442 uint32_t events,
443 std::list<InstrumentationListener*>& list,
444 InstrumentationListener* listener,
445 bool* has_listener)
446 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_) {
447 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
448 if (!HasEvent(event, events)) {
449 return;
450 }
451 // If there is a free slot in the list, we insert the listener in that slot.
452 // Otherwise we add it to the end of the list.
453 auto it = std::find(list.begin(), list.end(), nullptr);
454 if (it != list.end()) {
455 *it = listener;
456 } else {
457 list.push_back(listener);
458 }
459 *has_listener = true;
460}
461
Ian Rogers62d6c772013-02-27 08:32:07 -0800462void Instrumentation::AddListener(InstrumentationListener* listener, uint32_t events) {
463 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000464 PotentiallyAddListenerTo(kMethodEntered,
465 events,
466 method_entry_listeners_,
467 listener,
468 &have_method_entry_listeners_);
469 PotentiallyAddListenerTo(kMethodExited,
470 events,
471 method_exit_listeners_,
472 listener,
473 &have_method_exit_listeners_);
474 PotentiallyAddListenerTo(kMethodUnwind,
475 events,
476 method_unwind_listeners_,
477 listener,
478 &have_method_unwind_listeners_);
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000479 PotentiallyAddListenerTo(kBranch,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000480 events,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000481 branch_listeners_,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000482 listener,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000483 &have_branch_listeners_);
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000484 PotentiallyAddListenerTo(kInvokeVirtualOrInterface,
485 events,
486 invoke_virtual_or_interface_listeners_,
487 listener,
488 &have_invoke_virtual_or_interface_listeners_);
489 PotentiallyAddListenerTo(kDexPcMoved,
490 events,
491 dex_pc_listeners_,
492 listener,
493 &have_dex_pc_listeners_);
494 PotentiallyAddListenerTo(kFieldRead,
495 events,
496 field_read_listeners_,
497 listener,
498 &have_field_read_listeners_);
499 PotentiallyAddListenerTo(kFieldWritten,
500 events,
501 field_write_listeners_,
502 listener,
503 &have_field_write_listeners_);
504 PotentiallyAddListenerTo(kExceptionCaught,
505 events,
506 exception_caught_listeners_,
507 listener,
508 &have_exception_caught_listeners_);
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200509 UpdateInterpreterHandlerTable();
jeffhao725a9572012-11-13 18:20:12 -0800510}
511
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000512static void PotentiallyRemoveListenerFrom(Instrumentation::InstrumentationEvent event,
513 uint32_t events,
514 std::list<InstrumentationListener*>& list,
515 InstrumentationListener* listener,
516 bool* has_listener)
517 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_) {
518 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
519 if (!HasEvent(event, events)) {
520 return;
521 }
522 auto it = std::find(list.begin(), list.end(), listener);
523 if (it != list.end()) {
524 // Just update the entry, do not remove from the list. Removing entries in the list
525 // is unsafe when mutators are iterating over it.
526 *it = nullptr;
527 }
528
529 // Check if the list contains any non-null listener, and update 'has_listener'.
530 for (InstrumentationListener* l : list) {
531 if (l != nullptr) {
532 *has_listener = true;
533 return;
534 }
535 }
536 *has_listener = false;
537}
538
Ian Rogers62d6c772013-02-27 08:32:07 -0800539void Instrumentation::RemoveListener(InstrumentationListener* listener, uint32_t events) {
540 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000541 PotentiallyRemoveListenerFrom(kMethodEntered,
542 events,
543 method_entry_listeners_,
544 listener,
545 &have_method_entry_listeners_);
546 PotentiallyRemoveListenerFrom(kMethodExited,
547 events,
548 method_exit_listeners_,
549 listener,
550 &have_method_exit_listeners_);
551 PotentiallyRemoveListenerFrom(kMethodUnwind,
552 events,
553 method_unwind_listeners_,
554 listener,
555 &have_method_unwind_listeners_);
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000556 PotentiallyRemoveListenerFrom(kBranch,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000557 events,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000558 branch_listeners_,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000559 listener,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000560 &have_branch_listeners_);
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000561 PotentiallyRemoveListenerFrom(kInvokeVirtualOrInterface,
562 events,
563 invoke_virtual_or_interface_listeners_,
564 listener,
565 &have_invoke_virtual_or_interface_listeners_);
566 PotentiallyRemoveListenerFrom(kDexPcMoved,
567 events,
568 dex_pc_listeners_,
569 listener,
570 &have_dex_pc_listeners_);
571 PotentiallyRemoveListenerFrom(kFieldRead,
572 events,
573 field_read_listeners_,
574 listener,
575 &have_field_read_listeners_);
576 PotentiallyRemoveListenerFrom(kFieldWritten,
577 events,
578 field_write_listeners_,
579 listener,
580 &have_field_write_listeners_);
581 PotentiallyRemoveListenerFrom(kExceptionCaught,
582 events,
583 exception_caught_listeners_,
584 listener,
585 &have_exception_caught_listeners_);
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200586 UpdateInterpreterHandlerTable();
jeffhao725a9572012-11-13 18:20:12 -0800587}
588
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200589Instrumentation::InstrumentationLevel Instrumentation::GetCurrentInstrumentationLevel() const {
Alex Light4ba388a2017-01-27 10:26:49 -0800590 if (interpreter_stubs_installed_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200591 return InstrumentationLevel::kInstrumentWithInterpreter;
Ian Rogers62d6c772013-02-27 08:32:07 -0800592 } else if (entry_exit_stubs_installed_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200593 return InstrumentationLevel::kInstrumentWithInstrumentationStubs;
Ian Rogers62d6c772013-02-27 08:32:07 -0800594 } else {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200595 return InstrumentationLevel::kInstrumentNothing;
Ian Rogers62d6c772013-02-27 08:32:07 -0800596 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200597}
598
Alex Lightdba61482016-12-21 08:20:29 -0800599bool Instrumentation::RequiresInstrumentationInstallation(InstrumentationLevel new_level) const {
Alex Light4ba388a2017-01-27 10:26:49 -0800600 // We need to reinstall instrumentation if we go to a different level.
601 return GetCurrentInstrumentationLevel() != new_level;
Alex Lightdba61482016-12-21 08:20:29 -0800602}
603
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200604void Instrumentation::ConfigureStubs(const char* key, InstrumentationLevel desired_level) {
605 // Store the instrumentation level for this key or remove it.
606 if (desired_level == InstrumentationLevel::kInstrumentNothing) {
607 // The client no longer needs instrumentation.
608 requested_instrumentation_levels_.erase(key);
609 } else {
610 // The client needs instrumentation.
611 requested_instrumentation_levels_.Overwrite(key, desired_level);
612 }
613
614 // Look for the highest required instrumentation level.
615 InstrumentationLevel requested_level = InstrumentationLevel::kInstrumentNothing;
616 for (const auto& v : requested_instrumentation_levels_) {
617 requested_level = std::max(requested_level, v.second);
618 }
619
620 interpret_only_ = (requested_level == InstrumentationLevel::kInstrumentWithInterpreter) ||
621 forced_interpret_only_;
622
Alex Lightdba61482016-12-21 08:20:29 -0800623 if (!RequiresInstrumentationInstallation(requested_level)) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800624 // We're already set.
625 return;
626 }
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100627 Thread* const self = Thread::Current();
Ian Rogers62d6c772013-02-27 08:32:07 -0800628 Runtime* runtime = Runtime::Current();
Sebastien Hertza8a697f2015-01-15 12:28:47 +0100629 Locks::mutator_lock_->AssertExclusiveHeld(self);
Ian Rogers62d6c772013-02-27 08:32:07 -0800630 Locks::thread_list_lock_->AssertNotHeld(self);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200631 if (requested_level > InstrumentationLevel::kInstrumentNothing) {
Alex Light4ba388a2017-01-27 10:26:49 -0800632 if (requested_level == InstrumentationLevel::kInstrumentWithInterpreter) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800633 interpreter_stubs_installed_ = true;
Ian Rogers62d6c772013-02-27 08:32:07 -0800634 entry_exit_stubs_installed_ = true;
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200635 } else {
636 CHECK_EQ(requested_level, InstrumentationLevel::kInstrumentWithInstrumentationStubs);
637 entry_exit_stubs_installed_ = true;
638 interpreter_stubs_installed_ = false;
Ian Rogers62d6c772013-02-27 08:32:07 -0800639 }
Mathieu Chartiere0671ce2015-07-28 17:23:28 -0700640 InstallStubsClassVisitor visitor(this);
641 runtime->GetClassLinker()->VisitClasses(&visitor);
Ian Rogers62d6c772013-02-27 08:32:07 -0800642 instrumentation_stubs_installed_ = true;
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100643 MutexLock mu(self, *Locks::thread_list_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800644 runtime->GetThreadList()->ForEach(InstrumentationInstallStack, this);
645 } else {
646 interpreter_stubs_installed_ = false;
647 entry_exit_stubs_installed_ = false;
Mathieu Chartiere0671ce2015-07-28 17:23:28 -0700648 InstallStubsClassVisitor visitor(this);
649 runtime->GetClassLinker()->VisitClasses(&visitor);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100650 // Restore stack only if there is no method currently deoptimized.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700651 bool empty;
652 {
653 ReaderMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700654 empty = IsDeoptimizedMethodsEmpty(); // Avoid lock violation.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700655 }
656 if (empty) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100657 MutexLock mu(self, *Locks::thread_list_lock_);
658 Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this);
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +0000659 // Only do this after restoring, as walking the stack when restoring will see
660 // the instrumentation exit pc.
661 instrumentation_stubs_installed_ = false;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100662 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800663 }
jeffhao725a9572012-11-13 18:20:12 -0800664}
665
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200666static void ResetQuickAllocEntryPointsForThread(Thread* thread, void* arg ATTRIBUTE_UNUSED) {
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800667 thread->ResetQuickAllocEntryPointsForThread(kUseReadBarrier && thread->GetIsGcMarking());
Ian Rogersfa824272013-11-05 16:12:57 -0800668}
669
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700670void Instrumentation::SetEntrypointsInstrumented(bool instrumented) {
671 Thread* self = Thread::Current();
Mathieu Chartier661974a2014-01-09 11:23:53 -0800672 Runtime* runtime = Runtime::Current();
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700673 Locks::mutator_lock_->AssertNotHeld(self);
674 Locks::instrument_entrypoints_lock_->AssertHeld(self);
675 if (runtime->IsStarted()) {
Mathieu Chartier4f55e222015-09-04 13:26:21 -0700676 ScopedSuspendAll ssa(__FUNCTION__);
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700677 MutexLock mu(self, *Locks::runtime_shutdown_lock_);
Mathieu Chartier661974a2014-01-09 11:23:53 -0800678 SetQuickAllocEntryPointsInstrumented(instrumented);
679 ResetQuickAllocEntryPoints();
Mathieu Chartier50e93312016-03-16 11:25:29 -0700680 alloc_entrypoints_instrumented_ = instrumented;
Mathieu Chartier4f55e222015-09-04 13:26:21 -0700681 } else {
682 MutexLock mu(self, *Locks::runtime_shutdown_lock_);
683 SetQuickAllocEntryPointsInstrumented(instrumented);
Andreas Gampe157c77e2016-10-17 17:44:41 -0700684
685 // Note: ResetQuickAllocEntryPoints only works when the runtime is started. Manually run the
686 // update for just this thread.
Andreas Gampe162ae502016-10-18 10:03:42 -0700687 // Note: self may be null. One of those paths is setting instrumentation in the Heap
688 // constructor for gcstress mode.
689 if (self != nullptr) {
690 ResetQuickAllocEntryPointsForThread(self, nullptr);
691 }
Andreas Gampe157c77e2016-10-17 17:44:41 -0700692
Mathieu Chartier50e93312016-03-16 11:25:29 -0700693 alloc_entrypoints_instrumented_ = instrumented;
Mathieu Chartier661974a2014-01-09 11:23:53 -0800694 }
695}
696
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700697void Instrumentation::InstrumentQuickAllocEntryPoints() {
698 MutexLock mu(Thread::Current(), *Locks::instrument_entrypoints_lock_);
699 InstrumentQuickAllocEntryPointsLocked();
Ian Rogersfa824272013-11-05 16:12:57 -0800700}
701
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700702void Instrumentation::UninstrumentQuickAllocEntryPoints() {
703 MutexLock mu(Thread::Current(), *Locks::instrument_entrypoints_lock_);
704 UninstrumentQuickAllocEntryPointsLocked();
705}
706
707void Instrumentation::InstrumentQuickAllocEntryPointsLocked() {
708 Locks::instrument_entrypoints_lock_->AssertHeld(Thread::Current());
709 if (quick_alloc_entry_points_instrumentation_counter_ == 0) {
710 SetEntrypointsInstrumented(true);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800711 }
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700712 ++quick_alloc_entry_points_instrumentation_counter_;
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700713}
714
715void Instrumentation::UninstrumentQuickAllocEntryPointsLocked() {
716 Locks::instrument_entrypoints_lock_->AssertHeld(Thread::Current());
717 CHECK_GT(quick_alloc_entry_points_instrumentation_counter_, 0U);
718 --quick_alloc_entry_points_instrumentation_counter_;
719 if (quick_alloc_entry_points_instrumentation_counter_ == 0) {
720 SetEntrypointsInstrumented(false);
721 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800722}
723
724void Instrumentation::ResetQuickAllocEntryPoints() {
725 Runtime* runtime = Runtime::Current();
726 if (runtime->IsStarted()) {
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800727 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700728 runtime->GetThreadList()->ForEach(ResetQuickAllocEntryPointsForThread, nullptr);
Ian Rogersfa824272013-11-05 16:12:57 -0800729 }
730}
731
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700732void Instrumentation::UpdateMethodsCodeImpl(ArtMethod* method, const void* quick_code) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800733 const void* new_quick_code;
Ian Rogers62d6c772013-02-27 08:32:07 -0800734 if (LIKELY(!instrumentation_stubs_installed_)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800735 new_quick_code = quick_code;
Jeff Hao65d15d92013-07-16 16:39:33 -0700736 } else {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100737 if ((interpreter_stubs_installed_ || IsDeoptimized(method)) && !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800738 new_quick_code = GetQuickToInterpreterBridge();
Jeff Hao65d15d92013-07-16 16:39:33 -0700739 } else {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700740 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700741 if (class_linker->IsQuickResolutionStub(quick_code) ||
742 class_linker->IsQuickToInterpreterBridge(quick_code)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700743 new_quick_code = quick_code;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700744 } else if (entry_exit_stubs_installed_) {
745 new_quick_code = GetQuickInstrumentationEntryPoint();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700746 } else {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700747 new_quick_code = quick_code;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700748 }
Jeff Hao65d15d92013-07-16 16:39:33 -0700749 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800750 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800751 UpdateEntrypoints(method, new_quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100752}
753
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700754void Instrumentation::UpdateMethodsCode(ArtMethod* method, const void* quick_code) {
755 DCHECK(method->GetDeclaringClass()->IsResolved());
756 UpdateMethodsCodeImpl(method, quick_code);
757}
758
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000759void Instrumentation::UpdateMethodsCodeForJavaDebuggable(ArtMethod* method,
760 const void* quick_code) {
761 // When the runtime is set to Java debuggable, we may update the entry points of
762 // all methods of a class to the interpreter bridge. A method's declaring class
763 // might not be in resolved state yet in that case, so we bypass the DCHECK in
764 // UpdateMethodsCode.
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700765 UpdateMethodsCodeImpl(method, quick_code);
766}
767
Mathieu Chartiere401d142015-04-22 13:56:20 -0700768bool Instrumentation::AddDeoptimizedMethod(ArtMethod* method) {
769 if (IsDeoptimizedMethod(method)) {
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700770 // Already in the map. Return.
771 return false;
772 }
773 // Not found. Add it.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700774 deoptimized_methods_.insert(method);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700775 return true;
776}
777
Mathieu Chartiere401d142015-04-22 13:56:20 -0700778bool Instrumentation::IsDeoptimizedMethod(ArtMethod* method) {
779 return deoptimized_methods_.find(method) != deoptimized_methods_.end();
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700780}
781
Mathieu Chartiere401d142015-04-22 13:56:20 -0700782ArtMethod* Instrumentation::BeginDeoptimizedMethod() {
783 if (deoptimized_methods_.empty()) {
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700784 // Empty.
785 return nullptr;
786 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700787 return *deoptimized_methods_.begin();
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700788}
789
Mathieu Chartiere401d142015-04-22 13:56:20 -0700790bool Instrumentation::RemoveDeoptimizedMethod(ArtMethod* method) {
791 auto it = deoptimized_methods_.find(method);
792 if (it == deoptimized_methods_.end()) {
793 return false;
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700794 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700795 deoptimized_methods_.erase(it);
796 return true;
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700797}
798
799bool Instrumentation::IsDeoptimizedMethodsEmpty() const {
800 return deoptimized_methods_.empty();
801}
802
Mathieu Chartiere401d142015-04-22 13:56:20 -0700803void Instrumentation::Deoptimize(ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100804 CHECK(!method->IsNative());
805 CHECK(!method->IsProxyMethod());
Alex Light9139e002015-10-09 15:59:48 -0700806 CHECK(method->IsInvokable());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100807
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700808 Thread* self = Thread::Current();
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700809 {
810 WriterMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700811 bool has_not_been_deoptimized = AddDeoptimizedMethod(method);
David Sehr709b0702016-10-13 09:12:37 -0700812 CHECK(has_not_been_deoptimized) << "Method " << ArtMethod::PrettyMethod(method)
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200813 << " is already deoptimized";
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700814 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100815 if (!interpreter_stubs_installed_) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800816 UpdateEntrypoints(method, GetQuickInstrumentationEntryPoint());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100817
818 // Install instrumentation exit stub and instrumentation frames. We may already have installed
819 // these previously so it will only cover the newly created frames.
820 instrumentation_stubs_installed_ = true;
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700821 MutexLock mu(self, *Locks::thread_list_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100822 Runtime::Current()->GetThreadList()->ForEach(InstrumentationInstallStack, this);
823 }
824}
825
Mathieu Chartiere401d142015-04-22 13:56:20 -0700826void Instrumentation::Undeoptimize(ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100827 CHECK(!method->IsNative());
828 CHECK(!method->IsProxyMethod());
Alex Light9139e002015-10-09 15:59:48 -0700829 CHECK(method->IsInvokable());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100830
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700831 Thread* self = Thread::Current();
832 bool empty;
833 {
834 WriterMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700835 bool found_and_erased = RemoveDeoptimizedMethod(method);
David Sehr709b0702016-10-13 09:12:37 -0700836 CHECK(found_and_erased) << "Method " << ArtMethod::PrettyMethod(method)
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700837 << " is not deoptimized";
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700838 empty = IsDeoptimizedMethodsEmpty();
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700839 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100840
841 // Restore code and possibly stack only if we did not deoptimize everything.
842 if (!interpreter_stubs_installed_) {
843 // Restore its code or resolution trampoline.
844 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800845 if (method->IsStatic() && !method->IsConstructor() &&
846 !method->GetDeclaringClass()->IsInitialized()) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800847 UpdateEntrypoints(method, GetQuickResolutionStub());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100848 } else {
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000849 const void* quick_code = NeedDebugVersionFor(method)
850 ? GetQuickToInterpreterBridge()
851 : class_linker->GetQuickOatCodeFor(method);
Elliott Hughes956af0f2014-12-11 14:34:28 -0800852 UpdateEntrypoints(method, quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100853 }
854
855 // If there is no deoptimized method left, we can restore the stack of each thread.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700856 if (empty) {
857 MutexLock mu(self, *Locks::thread_list_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100858 Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this);
859 instrumentation_stubs_installed_ = false;
860 }
861 }
862}
863
Mathieu Chartiere401d142015-04-22 13:56:20 -0700864bool Instrumentation::IsDeoptimized(ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100865 DCHECK(method != nullptr);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700866 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700867 return IsDeoptimizedMethod(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100868}
869
870void Instrumentation::EnableDeoptimization() {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700871 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700872 CHECK(IsDeoptimizedMethodsEmpty());
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100873 CHECK_EQ(deoptimization_enabled_, false);
874 deoptimization_enabled_ = true;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100875}
876
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200877void Instrumentation::DisableDeoptimization(const char* key) {
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100878 CHECK_EQ(deoptimization_enabled_, true);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100879 // If we deoptimized everything, undo it.
Alex Lightdba61482016-12-21 08:20:29 -0800880 InstrumentationLevel level = GetCurrentInstrumentationLevel();
881 if (level == InstrumentationLevel::kInstrumentWithInterpreter) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200882 UndeoptimizeEverything(key);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100883 }
884 // Undeoptimized selected methods.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700885 while (true) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700886 ArtMethod* method;
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700887 {
888 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700889 if (IsDeoptimizedMethodsEmpty()) {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700890 break;
891 }
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700892 method = BeginDeoptimizedMethod();
893 CHECK(method != nullptr);
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700894 }
895 Undeoptimize(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100896 }
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100897 deoptimization_enabled_ = false;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100898}
899
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100900// Indicates if instrumentation should notify method enter/exit events to the listeners.
901bool Instrumentation::ShouldNotifyMethodEnterExitEvents() const {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200902 if (!HasMethodEntryListeners() && !HasMethodExitListeners()) {
903 return false;
904 }
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100905 return !deoptimization_enabled_ && !interpreter_stubs_installed_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100906}
907
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200908void Instrumentation::DeoptimizeEverything(const char* key) {
909 CHECK(deoptimization_enabled_);
910 ConfigureStubs(key, InstrumentationLevel::kInstrumentWithInterpreter);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100911}
912
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200913void Instrumentation::UndeoptimizeEverything(const char* key) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100914 CHECK(interpreter_stubs_installed_);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200915 CHECK(deoptimization_enabled_);
916 ConfigureStubs(key, InstrumentationLevel::kInstrumentNothing);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100917}
918
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200919void Instrumentation::EnableMethodTracing(const char* key, bool needs_interpreter) {
920 InstrumentationLevel level;
921 if (needs_interpreter) {
922 level = InstrumentationLevel::kInstrumentWithInterpreter;
923 } else {
924 level = InstrumentationLevel::kInstrumentWithInstrumentationStubs;
925 }
926 ConfigureStubs(key, level);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100927}
928
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200929void Instrumentation::DisableMethodTracing(const char* key) {
930 ConfigureStubs(key, InstrumentationLevel::kInstrumentNothing);
jeffhao725a9572012-11-13 18:20:12 -0800931}
932
Andreas Gampe542451c2016-07-26 09:02:02 -0700933const void* Instrumentation::GetQuickCodeFor(ArtMethod* method, PointerSize pointer_size) const {
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100934 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers62d6c772013-02-27 08:32:07 -0800935 if (LIKELY(!instrumentation_stubs_installed_)) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800936 const void* code = method->GetEntryPointFromQuickCompiledCodePtrSize(pointer_size);
Vladimir Marko8a630572014-04-09 18:45:35 +0100937 DCHECK(code != nullptr);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700938 if (LIKELY(!class_linker->IsQuickResolutionStub(code) &&
939 !class_linker->IsQuickToInterpreterBridge(code)) &&
940 !class_linker->IsQuickResolutionStub(code) &&
941 !class_linker->IsQuickToInterpreterBridge(code)) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800942 return code;
943 }
944 }
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100945 return class_linker->GetQuickOatCodeFor(method);
jeffhao725a9572012-11-13 18:20:12 -0800946}
947
Alex Lightd7661582017-05-01 13:48:16 -0700948void Instrumentation::MethodEnterEventImpl(Thread* thread,
949 ObjPtr<mirror::Object> this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700950 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800951 uint32_t dex_pc) const {
Mingyao Yang047abb22017-08-23 15:26:57 -0700952 DCHECK(!method->IsRuntimeMethod());
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000953 if (HasMethodEntryListeners()) {
Alex Lightd7661582017-05-01 13:48:16 -0700954 Thread* self = Thread::Current();
955 StackHandleScope<1> hs(self);
956 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000957 for (InstrumentationListener* listener : method_entry_listeners_) {
958 if (listener != nullptr) {
Alex Lightd7661582017-05-01 13:48:16 -0700959 listener->MethodEntered(thread, thiz, method, dex_pc);
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000960 }
961 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800962 }
963}
964
Alex Lightd7661582017-05-01 13:48:16 -0700965void Instrumentation::MethodExitEventImpl(Thread* thread,
966 ObjPtr<mirror::Object> this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700967 ArtMethod* method,
Alex Lightd7661582017-05-01 13:48:16 -0700968 uint32_t dex_pc,
969 const JValue& return_value) const {
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000970 if (HasMethodExitListeners()) {
Alex Lightd7661582017-05-01 13:48:16 -0700971 Thread* self = Thread::Current();
972 StackHandleScope<2> hs(self);
973 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
974 if (method->GetInterfaceMethodIfProxy(kRuntimePointerSize)
975 ->GetReturnTypePrimitive() != Primitive::kPrimNot) {
976 for (InstrumentationListener* listener : method_exit_listeners_) {
977 if (listener != nullptr) {
978 listener->MethodExited(thread, thiz, method, dex_pc, return_value);
979 }
980 }
981 } else {
982 Handle<mirror::Object> ret(hs.NewHandle(return_value.GetL()));
983 for (InstrumentationListener* listener : method_exit_listeners_) {
984 if (listener != nullptr) {
985 listener->MethodExited(thread, thiz, method, dex_pc, ret);
986 }
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000987 }
988 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800989 }
990}
991
Alex Lightd7661582017-05-01 13:48:16 -0700992void Instrumentation::MethodUnwindEvent(Thread* thread,
993 mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700994 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800995 uint32_t dex_pc) const {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200996 if (HasMethodUnwindListeners()) {
Alex Lightd7661582017-05-01 13:48:16 -0700997 Thread* self = Thread::Current();
998 StackHandleScope<1> hs(self);
999 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
Mathieu Chartier02e25112013-08-14 16:14:24 -07001000 for (InstrumentationListener* listener : method_unwind_listeners_) {
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001001 if (listener != nullptr) {
Alex Lightd7661582017-05-01 13:48:16 -07001002 listener->MethodUnwind(thread, thiz, method, dex_pc);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001003 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001004 }
1005 }
1006}
1007
Alex Lightd7661582017-05-01 13:48:16 -07001008void Instrumentation::DexPcMovedEventImpl(Thread* thread,
1009 ObjPtr<mirror::Object> this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001010 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -08001011 uint32_t dex_pc) const {
Alex Lightd7661582017-05-01 13:48:16 -07001012 Thread* self = Thread::Current();
1013 StackHandleScope<1> hs(self);
1014 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001015 for (InstrumentationListener* listener : dex_pc_listeners_) {
1016 if (listener != nullptr) {
Alex Lightd7661582017-05-01 13:48:16 -07001017 listener->DexPcMoved(thread, thiz, method, dex_pc);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001018 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001019 }
1020}
1021
Nicolas Geoffray81f0f952016-01-20 16:25:19 +00001022void Instrumentation::BranchImpl(Thread* thread,
1023 ArtMethod* method,
1024 uint32_t dex_pc,
1025 int32_t offset) const {
1026 for (InstrumentationListener* listener : branch_listeners_) {
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001027 if (listener != nullptr) {
Nicolas Geoffray81f0f952016-01-20 16:25:19 +00001028 listener->Branch(thread, method, dex_pc, offset);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001029 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001030 }
1031}
1032
Nicolas Geoffray5550ca82015-08-21 18:38:30 +01001033void Instrumentation::InvokeVirtualOrInterfaceImpl(Thread* thread,
Alex Lightd7661582017-05-01 13:48:16 -07001034 ObjPtr<mirror::Object> this_object,
Nicolas Geoffray5550ca82015-08-21 18:38:30 +01001035 ArtMethod* caller,
1036 uint32_t dex_pc,
1037 ArtMethod* callee) const {
Alex Lightd7661582017-05-01 13:48:16 -07001038 Thread* self = Thread::Current();
1039 StackHandleScope<1> hs(self);
1040 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
Nicolas Geoffray5550ca82015-08-21 18:38:30 +01001041 for (InstrumentationListener* listener : invoke_virtual_or_interface_listeners_) {
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001042 if (listener != nullptr) {
Alex Lightd7661582017-05-01 13:48:16 -07001043 listener->InvokeVirtualOrInterface(thread, thiz, caller, dex_pc, callee);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001044 }
Nicolas Geoffray5550ca82015-08-21 18:38:30 +01001045 }
1046}
1047
Alex Lightd7661582017-05-01 13:48:16 -07001048void Instrumentation::FieldReadEventImpl(Thread* thread,
1049 ObjPtr<mirror::Object> this_object,
1050 ArtMethod* method,
1051 uint32_t dex_pc,
Mathieu Chartierc7853442015-03-27 14:35:38 -07001052 ArtField* field) const {
Alex Lightd7661582017-05-01 13:48:16 -07001053 Thread* self = Thread::Current();
1054 StackHandleScope<1> hs(self);
1055 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001056 for (InstrumentationListener* listener : field_read_listeners_) {
1057 if (listener != nullptr) {
Alex Lightd7661582017-05-01 13:48:16 -07001058 listener->FieldRead(thread, thiz, method, dex_pc, field);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001059 }
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +02001060 }
1061}
1062
Alex Lightd7661582017-05-01 13:48:16 -07001063void Instrumentation::FieldWriteEventImpl(Thread* thread,
1064 ObjPtr<mirror::Object> this_object,
1065 ArtMethod* method,
1066 uint32_t dex_pc,
1067 ArtField* field,
1068 const JValue& field_value) const {
1069 Thread* self = Thread::Current();
1070 StackHandleScope<2> hs(self);
1071 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
1072 if (field->IsPrimitiveType()) {
1073 for (InstrumentationListener* listener : field_write_listeners_) {
1074 if (listener != nullptr) {
1075 listener->FieldWritten(thread, thiz, method, dex_pc, field, field_value);
1076 }
1077 }
1078 } else {
1079 Handle<mirror::Object> val(hs.NewHandle(field_value.GetL()));
1080 for (InstrumentationListener* listener : field_write_listeners_) {
1081 if (listener != nullptr) {
1082 listener->FieldWritten(thread, thiz, method, dex_pc, field, val);
1083 }
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001084 }
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +02001085 }
1086}
1087
Nicolas Geoffray14691c52015-03-05 10:40:17 +00001088void Instrumentation::ExceptionCaughtEvent(Thread* thread,
Sebastien Hertz947ff082013-09-17 14:10:13 +02001089 mirror::Throwable* exception_object) const {
Hiroshi Yamauchi3481f7a2017-02-10 12:07:36 -08001090 Thread* self = Thread::Current();
1091 StackHandleScope<1> hs(self);
1092 Handle<mirror::Throwable> h_exception(hs.NewHandle(exception_object));
Sebastien Hertz9f102032014-05-23 08:59:42 +02001093 if (HasExceptionCaughtListeners()) {
Hiroshi Yamauchi3481f7a2017-02-10 12:07:36 -08001094 DCHECK_EQ(thread->GetException(), h_exception.Get());
Jeff Haoc0bd4da2013-04-11 15:52:28 -07001095 thread->ClearException();
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001096 for (InstrumentationListener* listener : exception_caught_listeners_) {
1097 if (listener != nullptr) {
Alex Lightd7661582017-05-01 13:48:16 -07001098 listener->ExceptionCaught(thread, h_exception);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001099 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001100 }
Hiroshi Yamauchi3481f7a2017-02-10 12:07:36 -08001101 thread->SetException(h_exception.Get());
Ian Rogers62d6c772013-02-27 08:32:07 -08001102 }
1103}
1104
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +00001105// Computes a frame ID by ignoring inlined frames.
1106size_t Instrumentation::ComputeFrameId(Thread* self,
1107 size_t frame_depth,
1108 size_t inlined_frames_before_frame) {
1109 CHECK_GE(frame_depth, inlined_frames_before_frame);
1110 size_t no_inline_depth = frame_depth - inlined_frames_before_frame;
1111 return StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk) - no_inline_depth;
1112}
1113
Ian Rogers62d6c772013-02-27 08:32:07 -08001114static void CheckStackDepth(Thread* self, const InstrumentationStackFrame& instrumentation_frame,
1115 int delta)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001116 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +01001117 size_t frame_id = StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk) + delta;
Ian Rogers62d6c772013-02-27 08:32:07 -08001118 if (frame_id != instrumentation_frame.frame_id_) {
1119 LOG(ERROR) << "Expected frame_id=" << frame_id << " but found "
1120 << instrumentation_frame.frame_id_;
1121 StackVisitor::DescribeStack(self);
1122 CHECK_EQ(frame_id, instrumentation_frame.frame_id_);
1123 }
1124}
1125
1126void Instrumentation::PushInstrumentationStackFrame(Thread* self, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001127 ArtMethod* method,
Jeff Hao9a916d32013-06-27 18:45:37 -07001128 uintptr_t lr, bool interpreter_entry) {
Alex Lightb7edcda2017-04-27 13:20:31 -07001129 DCHECK(!self->IsExceptionPending());
Ian Rogers62d6c772013-02-27 08:32:07 -08001130 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
1131 if (kVerboseInstrumentation) {
David Sehr709b0702016-10-13 09:12:37 -07001132 LOG(INFO) << "Entering " << ArtMethod::PrettyMethod(method) << " from PC "
1133 << reinterpret_cast<void*>(lr);
Ian Rogers62d6c772013-02-27 08:32:07 -08001134 }
Alex Lightb7edcda2017-04-27 13:20:31 -07001135
1136 // We send the enter event before pushing the instrumentation frame to make cleanup easier. If the
1137 // event causes an exception we can simply send the unwind event and return.
1138 StackHandleScope<1> hs(self);
1139 Handle<mirror::Object> h_this(hs.NewHandle(this_object));
1140 if (!interpreter_entry) {
1141 MethodEnterEvent(self, h_this.Get(), method, 0);
1142 if (self->IsExceptionPending()) {
1143 MethodUnwindEvent(self, h_this.Get(), method, 0);
1144 return;
1145 }
1146 }
1147
1148 // We have a callee-save frame meaning this value is guaranteed to never be 0.
1149 DCHECK(!self->IsExceptionPending());
1150 size_t frame_id = StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk);
1151
1152 instrumentation::InstrumentationStackFrame instrumentation_frame(h_this.Get(), method, lr,
Jeff Hao9a916d32013-06-27 18:45:37 -07001153 frame_id, interpreter_entry);
Ian Rogers62d6c772013-02-27 08:32:07 -08001154 stack->push_front(instrumentation_frame);
Ian Rogers62d6c772013-02-27 08:32:07 -08001155}
1156
Mingyao Yang047abb22017-08-23 15:26:57 -07001157DeoptimizationMethodType Instrumentation::GetDeoptimizationMethodType(ArtMethod* method) {
1158 if (method->IsRuntimeMethod()) {
1159 // Certain methods have strict requirement on whether the dex instruction
1160 // should be re-executed upon deoptimization.
1161 if (method == Runtime::Current()->GetCalleeSaveMethod(
1162 CalleeSaveType::kSaveEverythingForClinit)) {
1163 return DeoptimizationMethodType::kKeepDexPc;
1164 }
1165 if (method == Runtime::Current()->GetCalleeSaveMethod(
1166 CalleeSaveType::kSaveEverythingForSuspendCheck)) {
1167 return DeoptimizationMethodType::kKeepDexPc;
1168 }
1169 }
1170 return DeoptimizationMethodType::kDefault;
1171}
1172
1173// Try to get the shorty of a runtime method if it's an invocation stub.
1174struct RuntimeMethodShortyVisitor : public StackVisitor {
1175 explicit RuntimeMethodShortyVisitor(Thread* thread)
1176 : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames),
1177 shorty('V') {}
1178
1179 bool VisitFrame() REQUIRES_SHARED(Locks::mutator_lock_) {
1180 ArtMethod* m = GetMethod();
1181 if (m != nullptr && !m->IsRuntimeMethod()) {
1182 // The first Java method.
1183 if (m->IsNative()) {
1184 // Use JNI method's shorty for the jni stub.
1185 shorty = m->GetShorty()[0];
1186 return false;
1187 }
1188 const DexFile::CodeItem* code_item = m->GetCodeItem();
1189 const Instruction* instr = Instruction::At(&code_item->insns_[GetDexPc()]);
1190 if (instr->IsInvoke()) {
1191 // If it's an invoke, use its shorty.
1192 uint32_t method_idx = instr->VRegB();
1193 shorty = m->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetDexFile()
1194 ->GetMethodShorty(method_idx)[0];
1195 }
1196 // Stop stack walking since we've seen a Java frame.
1197 return false;
1198 }
1199 return true;
1200 }
1201
1202 char shorty;
1203};
1204
Alex Lightb7edcda2017-04-27 13:20:31 -07001205TwoWordReturn Instrumentation::PopInstrumentationStackFrame(Thread* self,
1206 uintptr_t* return_pc,
1207 uint64_t* gpr_result,
1208 uint64_t* fpr_result) {
1209 DCHECK(gpr_result != nullptr);
1210 DCHECK(fpr_result != nullptr);
Ian Rogers62d6c772013-02-27 08:32:07 -08001211 // Do the pop.
1212 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
1213 CHECK_GT(stack->size(), 0U);
1214 InstrumentationStackFrame instrumentation_frame = stack->front();
1215 stack->pop_front();
1216
1217 // Set return PC and check the sanity of the stack.
1218 *return_pc = instrumentation_frame.return_pc_;
1219 CheckStackDepth(self, instrumentation_frame, 0);
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001220 self->VerifyStack();
Ian Rogers62d6c772013-02-27 08:32:07 -08001221
Mathieu Chartiere401d142015-04-22 13:56:20 -07001222 ArtMethod* method = instrumentation_frame.method_;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001223 uint32_t length;
Andreas Gampe542451c2016-07-26 09:02:02 -07001224 const PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Mingyao Yang047abb22017-08-23 15:26:57 -07001225 char return_shorty;
1226
1227 // Runtime method does not call into MethodExitEvent() so there should not be
1228 // suspension point below.
1229 ScopedAssertNoThreadSuspension ants(__FUNCTION__, method->IsRuntimeMethod());
1230 if (method->IsRuntimeMethod()) {
1231 if (method != Runtime::Current()->GetCalleeSaveMethod(
1232 CalleeSaveType::kSaveEverythingForClinit)) {
1233 // If the caller is at an invocation point and the runtime method is not
1234 // for clinit, we need to pass return results to the caller.
1235 // We need the correct shorty to decide whether we need to pass the return
1236 // result for deoptimization below.
1237 RuntimeMethodShortyVisitor visitor(self);
1238 visitor.WalkStack();
1239 return_shorty = visitor.shorty;
1240 } else {
1241 // Some runtime methods such as allocations, unresolved field getters, etc.
1242 // have return value. We don't need to set return_value since MethodExitEvent()
1243 // below isn't called for runtime methods. Deoptimization doesn't need the
1244 // value either since the dex instruction will be re-executed by the
1245 // interpreter, except these two cases:
1246 // (1) For an invoke, which is handled above to get the correct shorty.
1247 // (2) For MONITOR_ENTER/EXIT, which cannot be re-executed since it's not
1248 // idempotent. However there is no return value for it anyway.
1249 return_shorty = 'V';
1250 }
1251 } else {
1252 return_shorty = method->GetInterfaceMethodIfProxy(pointer_size)->GetShorty(&length)[0];
1253 }
1254
Alex Lightb7edcda2017-04-27 13:20:31 -07001255 bool is_ref = return_shorty == '[' || return_shorty == 'L';
1256 StackHandleScope<1> hs(self);
1257 MutableHandle<mirror::Object> res(hs.NewHandle<mirror::Object>(nullptr));
Ian Rogers62d6c772013-02-27 08:32:07 -08001258 JValue return_value;
1259 if (return_shorty == 'V') {
1260 return_value.SetJ(0);
1261 } else if (return_shorty == 'F' || return_shorty == 'D') {
Alex Lightb7edcda2017-04-27 13:20:31 -07001262 return_value.SetJ(*fpr_result);
Ian Rogers62d6c772013-02-27 08:32:07 -08001263 } else {
Alex Lightb7edcda2017-04-27 13:20:31 -07001264 return_value.SetJ(*gpr_result);
1265 }
1266 if (is_ref) {
1267 // Take a handle to the return value so we won't lose it if we suspend.
1268 res.Assign(return_value.GetL());
Ian Rogers62d6c772013-02-27 08:32:07 -08001269 }
1270 // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to
1271 // return_pc.
1272 uint32_t dex_pc = DexFile::kDexNoIndex;
1273 mirror::Object* this_object = instrumentation_frame.this_object_;
Mingyao Yang047abb22017-08-23 15:26:57 -07001274 if (!method->IsRuntimeMethod() && !instrumentation_frame.interpreter_entry_) {
Sebastien Hertz320deb22014-06-11 19:45:05 +02001275 MethodExitEvent(self, this_object, instrumentation_frame.method_, dex_pc, return_value);
1276 }
jeffhao725a9572012-11-13 18:20:12 -08001277
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01001278 // Deoptimize if the caller needs to continue execution in the interpreter. Do nothing if we get
1279 // back to an upcall.
1280 NthCallerVisitor visitor(self, 1, true);
1281 visitor.WalkStack(true);
Sebastien Hertz270a0e12015-01-16 19:49:09 +01001282 bool deoptimize = (visitor.caller != nullptr) &&
Daniel Mihalyieb076692014-08-22 17:33:31 +02001283 (interpreter_stubs_installed_ || IsDeoptimized(visitor.caller) ||
1284 Dbg::IsForcedInterpreterNeededForUpcall(self, visitor.caller));
Alex Lightb7edcda2017-04-27 13:20:31 -07001285 if (is_ref) {
1286 // Restore the return value if it's a reference since it might have moved.
1287 *reinterpret_cast<mirror::Object**>(gpr_result) = res.Get();
1288 }
Nicolas Geoffraya0619e22016-12-20 13:57:43 +00001289 if (deoptimize && Runtime::Current()->IsAsyncDeoptimizeable(*return_pc)) {
Ian Rogers62d6c772013-02-27 08:32:07 -08001290 if (kVerboseInstrumentation) {
Andreas Gampe46ee31b2016-12-14 10:11:49 -08001291 LOG(INFO) << "Deoptimizing "
1292 << visitor.caller->PrettyMethod()
1293 << " by returning from "
1294 << method->PrettyMethod()
1295 << " with result "
1296 << std::hex << return_value.GetJ() << std::dec
1297 << " in "
1298 << *self;
Ian Rogers62d6c772013-02-27 08:32:07 -08001299 }
Mingyao Yang047abb22017-08-23 15:26:57 -07001300 DeoptimizationMethodType deopt_method_type = GetDeoptimizationMethodType(method);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01001301 self->PushDeoptimizationContext(return_value,
Mingyao Yang047abb22017-08-23 15:26:57 -07001302 return_shorty == 'L' || return_shorty == '[',
1303 nullptr /* no pending exception */,
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01001304 false /* from_code */,
Mingyao Yang047abb22017-08-23 15:26:57 -07001305 deopt_method_type);
Andreas Gamped58342c2014-06-05 14:18:08 -07001306 return GetTwoWordSuccessValue(*return_pc,
1307 reinterpret_cast<uintptr_t>(GetQuickDeoptimizationEntryPoint()));
Ian Rogers62d6c772013-02-27 08:32:07 -08001308 } else {
Nicolas Geoffraya0619e22016-12-20 13:57:43 +00001309 if (deoptimize && !Runtime::Current()->IsAsyncDeoptimizeable(*return_pc)) {
1310 LOG(WARNING) << "Got a deoptimization request on un-deoptimizable " << method->PrettyMethod()
1311 << " at PC " << reinterpret_cast<void*>(*return_pc);
1312 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001313 if (kVerboseInstrumentation) {
David Sehr709b0702016-10-13 09:12:37 -07001314 LOG(INFO) << "Returning from " << method->PrettyMethod()
Brian Carlstrom2d888622013-07-18 17:02:00 -07001315 << " to PC " << reinterpret_cast<void*>(*return_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -08001316 }
Andreas Gamped58342c2014-06-05 14:18:08 -07001317 return GetTwoWordSuccessValue(0, *return_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -08001318 }
jeffhao725a9572012-11-13 18:20:12 -08001319}
1320
Mingyao Yangf711f2c2016-05-23 12:29:39 -07001321uintptr_t Instrumentation::PopMethodForUnwind(Thread* self, bool is_deoptimization) const {
Ian Rogers62d6c772013-02-27 08:32:07 -08001322 // Do the pop.
1323 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
1324 CHECK_GT(stack->size(), 0U);
Alex Lightb7edcda2017-04-27 13:20:31 -07001325 size_t idx = stack->size();
Ian Rogers62d6c772013-02-27 08:32:07 -08001326 InstrumentationStackFrame instrumentation_frame = stack->front();
Ian Rogers62d6c772013-02-27 08:32:07 -08001327
Mathieu Chartiere401d142015-04-22 13:56:20 -07001328 ArtMethod* method = instrumentation_frame.method_;
Ian Rogers62d6c772013-02-27 08:32:07 -08001329 if (is_deoptimization) {
1330 if (kVerboseInstrumentation) {
David Sehr709b0702016-10-13 09:12:37 -07001331 LOG(INFO) << "Popping for deoptimization " << ArtMethod::PrettyMethod(method);
Ian Rogers62d6c772013-02-27 08:32:07 -08001332 }
1333 } else {
1334 if (kVerboseInstrumentation) {
David Sehr709b0702016-10-13 09:12:37 -07001335 LOG(INFO) << "Popping for unwind " << ArtMethod::PrettyMethod(method);
Ian Rogers62d6c772013-02-27 08:32:07 -08001336 }
1337
1338 // Notify listeners of method unwind.
1339 // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to
1340 // return_pc.
1341 uint32_t dex_pc = DexFile::kDexNoIndex;
Mingyao Yang047abb22017-08-23 15:26:57 -07001342 if (!method->IsRuntimeMethod()) {
1343 MethodUnwindEvent(self, instrumentation_frame.this_object_, method, dex_pc);
1344 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001345 }
Alex Lightb7edcda2017-04-27 13:20:31 -07001346 // TODO: bring back CheckStackDepth(self, instrumentation_frame, 2);
1347 CHECK_EQ(stack->size(), idx);
1348 DCHECK(instrumentation_frame.method_ == stack->front().method_);
1349 stack->pop_front();
Mingyao Yangf711f2c2016-05-23 12:29:39 -07001350 return instrumentation_frame.return_pc_;
Ian Rogers62d6c772013-02-27 08:32:07 -08001351}
1352
1353std::string InstrumentationStackFrame::Dump() const {
1354 std::ostringstream os;
David Sehr709b0702016-10-13 09:12:37 -07001355 os << "Frame " << frame_id_ << " " << ArtMethod::PrettyMethod(method_) << ":"
Ian Rogers62d6c772013-02-27 08:32:07 -08001356 << reinterpret_cast<void*>(return_pc_) << " this=" << reinterpret_cast<void*>(this_object_);
1357 return os.str();
1358}
1359
1360} // namespace instrumentation
jeffhao725a9572012-11-13 18:20:12 -08001361} // namespace art