blob: 540b2e7e9bf98d0cfa8dafde20f084f82448d870 [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"
jeffhao725a9572012-11-13 18:20:12 -080025#include "class_linker.h"
26#include "debugger.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080027#include "dex_file-inl.h"
Ian Rogersc7dd2952014-10-21 23:31:19 -070028#include "entrypoints/quick/quick_entrypoints.h"
Mathieu Chartierd8891782014-03-02 13:28:37 -080029#include "entrypoints/quick/quick_alloc_entrypoints.h"
Ian Rogers6f3dbba2014-10-14 17:41:57 -070030#include "entrypoints/runtime_asm_entrypoints.h"
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070031#include "gc_root-inl.h"
Sebastien Hertz138dbfc2013-12-04 18:15:25 +010032#include "interpreter/interpreter.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080033#include "jit/jit.h"
34#include "jit/jit_code_cache.h"
Alex Lightd7661582017-05-01 13:48:16 -070035#include "jvalue-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080036#include "mirror/class-inl.h"
37#include "mirror/dex_cache.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080038#include "mirror/object_array-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070039#include "mirror/object-inl.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080040#include "nth_caller_visitor.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010041#include "oat_quick_method_header.h"
jeffhao725a9572012-11-13 18:20:12 -080042#include "thread.h"
43#include "thread_list.h"
jeffhao725a9572012-11-13 18:20:12 -080044
45namespace art {
Ian Rogers62d6c772013-02-27 08:32:07 -080046namespace instrumentation {
jeffhao725a9572012-11-13 18:20:12 -080047
Sebastien Hertz0462c4c2015-04-01 16:34:17 +020048constexpr bool kVerboseInstrumentation = false;
Sebastien Hertz5bfd5c92013-11-15 11:36:07 +010049
Alex Lightd7661582017-05-01 13:48:16 -070050void InstrumentationListener::MethodExited(Thread* thread,
51 Handle<mirror::Object> this_object,
52 ArtMethod* method,
53 uint32_t dex_pc,
54 Handle<mirror::Object> return_value) {
55 DCHECK_EQ(method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetReturnTypePrimitive(),
56 Primitive::kPrimNot);
57 JValue v;
58 v.SetL(return_value.Get());
59 MethodExited(thread, this_object, method, dex_pc, v);
60}
61
62void InstrumentationListener::FieldWritten(Thread* thread,
63 Handle<mirror::Object> this_object,
64 ArtMethod* method,
65 uint32_t dex_pc,
66 ArtField* field,
67 Handle<mirror::Object> field_value) {
68 DCHECK(!field->IsPrimitiveType());
69 JValue v;
70 v.SetL(field_value.Get());
71 FieldWritten(thread, this_object, method, dex_pc, field, v);
72}
73
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +010074// Instrumentation works on non-inlined frames by updating returned PCs
75// of compiled frames.
76static constexpr StackVisitor::StackWalkKind kInstrumentationStackWalk =
77 StackVisitor::StackWalkKind::kSkipInlinedFrames;
78
Mathieu Chartiere0671ce2015-07-28 17:23:28 -070079class InstallStubsClassVisitor : public ClassVisitor {
80 public:
81 explicit InstallStubsClassVisitor(Instrumentation* instrumentation)
82 : instrumentation_(instrumentation) {}
83
Mathieu Chartier28357fa2016-10-18 16:27:40 -070084 bool operator()(ObjPtr<mirror::Class> klass) OVERRIDE REQUIRES(Locks::mutator_lock_) {
85 instrumentation_->InstallStubsForClass(klass.Ptr());
Mathieu Chartiere0671ce2015-07-28 17:23:28 -070086 return true; // we visit all classes.
87 }
88
89 private:
90 Instrumentation* const instrumentation_;
91};
92
Ian Rogers62d6c772013-02-27 08:32:07 -080093
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -070094Instrumentation::Instrumentation()
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +000095 : instrumentation_stubs_installed_(false),
96 entry_exit_stubs_installed_(false),
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -070097 interpreter_stubs_installed_(false),
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +000098 interpret_only_(false),
99 forced_interpret_only_(false),
100 have_method_entry_listeners_(false),
101 have_method_exit_listeners_(false),
102 have_method_unwind_listeners_(false),
103 have_dex_pc_listeners_(false),
104 have_field_read_listeners_(false),
105 have_field_write_listeners_(false),
106 have_exception_caught_listeners_(false),
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000107 have_branch_listeners_(false),
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +0000108 have_invoke_virtual_or_interface_listeners_(false),
Mathieu Chartierb8aa1e42016-04-05 14:36:57 -0700109 deoptimized_methods_lock_("deoptimized methods lock", kDeoptimizedMethodsLock),
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700110 deoptimization_enabled_(false),
111 interpreter_handler_table_(kMainHandlerTable),
Mathieu Chartier50e93312016-03-16 11:25:29 -0700112 quick_alloc_entry_points_instrumentation_counter_(0),
113 alloc_entrypoints_instrumented_(false) {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700114}
115
Sebastien Hertza10aa372015-01-21 17:30:58 +0100116void Instrumentation::InstallStubsForClass(mirror::Class* klass) {
Vladimir Marko72ab6842017-01-20 19:32:50 +0000117 if (!klass->IsResolved()) {
Sebastien Hertza8a697f2015-01-15 12:28:47 +0100118 // We need the class to be resolved to install/uninstall stubs. Otherwise its methods
119 // could not be initialized or linked with regards to class inheritance.
Vladimir Marko72ab6842017-01-20 19:32:50 +0000120 } else if (klass->IsErroneousResolved()) {
121 // We can't execute code in a erroneous class: do nothing.
Sebastien Hertza8a697f2015-01-15 12:28:47 +0100122 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700123 for (ArtMethod& method : klass->GetMethods(kRuntimePointerSize)) {
Alex Light51a64d52015-12-17 13:55:59 -0800124 InstallStubsForMethod(&method);
Sebastien Hertza8a697f2015-01-15 12:28:47 +0100125 }
jeffhao725a9572012-11-13 18:20:12 -0800126 }
jeffhao725a9572012-11-13 18:20:12 -0800127}
128
Mathieu Chartiere401d142015-04-22 13:56:20 -0700129static void UpdateEntrypoints(ArtMethod* method, const void* quick_code)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700130 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800131 method->SetEntryPointFromQuickCompiledCode(quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100132}
133
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000134bool Instrumentation::NeedDebugVersionFor(ArtMethod* method) const REQUIRES_SHARED(Locks::mutator_lock_) {
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800135 return Dbg::IsDebuggerActive() &&
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000136 Runtime::Current()->IsJavaDebuggable() &&
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800137 !method->IsNative() &&
138 !method->IsProxyMethod();
139}
140
Mathieu Chartiere401d142015-04-22 13:56:20 -0700141void Instrumentation::InstallStubsForMethod(ArtMethod* method) {
Alex Light9139e002015-10-09 15:59:48 -0700142 if (!method->IsInvokable() || method->IsProxyMethod()) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100143 // Do not change stubs for these methods.
144 return;
145 }
Jeff Hao56802772014-08-19 10:17:36 -0700146 // Don't stub Proxy.<init>. Note that the Proxy class itself is not a proxy class.
147 if (method->IsConstructor() &&
148 method->GetDeclaringClass()->DescriptorEquals("Ljava/lang/reflect/Proxy;")) {
Jeff Haodb8a6642014-08-14 17:18:52 -0700149 return;
150 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800151 const void* new_quick_code;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100152 bool uninstall = !entry_exit_stubs_installed_ && !interpreter_stubs_installed_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800153 Runtime* const runtime = Runtime::Current();
154 ClassLinker* const class_linker = runtime->GetClassLinker();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100155 bool is_class_initialized = method->GetDeclaringClass()->IsInitialized();
156 if (uninstall) {
157 if ((forced_interpret_only_ || IsDeoptimized(method)) && !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800158 new_quick_code = GetQuickToInterpreterBridge();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100159 } else if (is_class_initialized || !method->IsStatic() || method->IsConstructor()) {
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000160 if (NeedDebugVersionFor(method)) {
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800161 new_quick_code = GetQuickToInterpreterBridge();
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000162 } else {
163 new_quick_code = class_linker->GetQuickOatCodeFor(method);
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800164 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100165 } else {
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700166 new_quick_code = GetQuickResolutionStub();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100167 }
168 } else { // !uninstall
Sebastien Hertzbae182c2013-12-17 10:42:03 +0100169 if ((interpreter_stubs_installed_ || forced_interpret_only_ || IsDeoptimized(method)) &&
170 !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800171 new_quick_code = GetQuickToInterpreterBridge();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100172 } else {
173 // Do not overwrite resolution trampoline. When the trampoline initializes the method's
174 // class, all its static methods code will be set to the instrumentation entry point.
175 // For more details, see ClassLinker::FixupStaticTrampolines.
176 if (is_class_initialized || !method->IsStatic() || method->IsConstructor()) {
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000177 if (NeedDebugVersionFor(method)) {
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800178 // Oat code should not be used. Don't install instrumentation stub and
179 // use interpreter for instrumentation.
180 new_quick_code = GetQuickToInterpreterBridge();
181 } else if (entry_exit_stubs_installed_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800182 new_quick_code = GetQuickInstrumentationEntryPoint();
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000183 } else {
184 new_quick_code = class_linker->GetQuickOatCodeFor(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100185 }
186 } else {
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700187 new_quick_code = GetQuickResolutionStub();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100188 }
189 }
190 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800191 UpdateEntrypoints(method, new_quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100192}
193
Ian Rogers62d6c772013-02-27 08:32:07 -0800194// Places the instrumentation exit pc as the return PC for every quick frame. This also allows
195// deoptimization of quick frames to interpreter frames.
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100196// Since we may already have done this previously, we need to push new instrumentation frame before
197// existing instrumentation frames.
Ian Rogers62d6c772013-02-27 08:32:07 -0800198static void InstrumentationInstallStack(Thread* thread, void* arg)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700199 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200200 struct InstallStackVisitor FINAL : public StackVisitor {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800201 InstallStackVisitor(Thread* thread_in, Context* context, uintptr_t instrumentation_exit_pc)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100202 : StackVisitor(thread_in, context, kInstrumentationStackWalk),
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800203 instrumentation_stack_(thread_in->GetInstrumentationStack()),
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100204 instrumentation_exit_pc_(instrumentation_exit_pc),
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100205 reached_existing_instrumentation_frames_(false), instrumentation_stack_depth_(0),
206 last_return_pc_(0) {
207 }
jeffhao725a9572012-11-13 18:20:12 -0800208
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700209 bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700210 ArtMethod* m = GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700211 if (m == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800212 if (kVerboseInstrumentation) {
213 LOG(INFO) << " Skipping upcall. Frame " << GetFrameId();
214 }
215 last_return_pc_ = 0;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700216 return true; // Ignore upcalls.
Ian Rogers306057f2012-11-26 12:45:53 -0800217 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700218 if (GetCurrentQuickFrame() == nullptr) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800219 bool interpreter_frame = true;
Sebastien Hertz320deb22014-06-11 19:45:05 +0200220 InstrumentationStackFrame instrumentation_frame(GetThisObject(), m, 0, GetFrameId(),
221 interpreter_frame);
Jeff Haoa15a81b2014-05-27 18:25:47 -0700222 if (kVerboseInstrumentation) {
223 LOG(INFO) << "Pushing shadow frame " << instrumentation_frame.Dump();
224 }
225 shadow_stack_.push_back(instrumentation_frame);
226 return true; // Continue.
227 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800228 uintptr_t return_pc = GetReturnPc();
Sebastien Hertz320deb22014-06-11 19:45:05 +0200229 if (m->IsRuntimeMethod()) {
230 if (return_pc == instrumentation_exit_pc_) {
231 if (kVerboseInstrumentation) {
232 LOG(INFO) << " Handling quick to interpreter transition. Frame " << GetFrameId();
233 }
234 CHECK_LT(instrumentation_stack_depth_, instrumentation_stack_->size());
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200235 const InstrumentationStackFrame& frame =
236 instrumentation_stack_->at(instrumentation_stack_depth_);
Sebastien Hertz320deb22014-06-11 19:45:05 +0200237 CHECK(frame.interpreter_entry_);
238 // This is an interpreter frame so method enter event must have been reported. However we
239 // need to push a DEX pc into the dex_pcs_ list to match size of instrumentation stack.
240 // Since we won't report method entry here, we can safely push any DEX pc.
241 dex_pcs_.push_back(0);
242 last_return_pc_ = frame.return_pc_;
243 ++instrumentation_stack_depth_;
244 return true;
245 } else {
246 if (kVerboseInstrumentation) {
247 LOG(INFO) << " Skipping runtime method. Frame " << GetFrameId();
248 }
249 last_return_pc_ = GetReturnPc();
250 return true; // Ignore unresolved methods since they will be instrumented after resolution.
251 }
252 }
253 if (kVerboseInstrumentation) {
254 LOG(INFO) << " Installing exit stub in " << DescribeLocation();
255 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100256 if (return_pc == instrumentation_exit_pc_) {
257 // We've reached a frame which has already been installed with instrumentation exit stub.
258 // We should have already installed instrumentation on previous frames.
259 reached_existing_instrumentation_frames_ = true;
260
261 CHECK_LT(instrumentation_stack_depth_, instrumentation_stack_->size());
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200262 const InstrumentationStackFrame& frame =
263 instrumentation_stack_->at(instrumentation_stack_depth_);
David Sehr709b0702016-10-13 09:12:37 -0700264 CHECK_EQ(m, frame.method_) << "Expected " << ArtMethod::PrettyMethod(m)
265 << ", Found " << ArtMethod::PrettyMethod(frame.method_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100266 return_pc = frame.return_pc_;
267 if (kVerboseInstrumentation) {
268 LOG(INFO) << "Ignoring already instrumented " << frame.Dump();
269 }
270 } else {
271 CHECK_NE(return_pc, 0U);
272 CHECK(!reached_existing_instrumentation_frames_);
273 InstrumentationStackFrame instrumentation_frame(GetThisObject(), m, return_pc, GetFrameId(),
274 false);
275 if (kVerboseInstrumentation) {
276 LOG(INFO) << "Pushing frame " << instrumentation_frame.Dump();
277 }
278
Sebastien Hertz320deb22014-06-11 19:45:05 +0200279 // Insert frame at the right position so we do not corrupt the instrumentation stack.
280 // Instrumentation stack frames are in descending frame id order.
281 auto it = instrumentation_stack_->begin();
282 for (auto end = instrumentation_stack_->end(); it != end; ++it) {
283 const InstrumentationStackFrame& current = *it;
284 if (instrumentation_frame.frame_id_ >= current.frame_id_) {
285 break;
286 }
287 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100288 instrumentation_stack_->insert(it, instrumentation_frame);
289 SetReturnPc(instrumentation_exit_pc_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800290 }
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100291 dex_pcs_.push_back((GetCurrentOatQuickMethodHeader() == nullptr)
292 ? DexFile::kDexNoIndex
293 : GetCurrentOatQuickMethodHeader()->ToDexPc(m, last_return_pc_));
Ian Rogers62d6c772013-02-27 08:32:07 -0800294 last_return_pc_ = return_pc;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100295 ++instrumentation_stack_depth_;
Ian Rogers306057f2012-11-26 12:45:53 -0800296 return true; // Continue.
297 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800298 std::deque<InstrumentationStackFrame>* const instrumentation_stack_;
Jeff Haoa15a81b2014-05-27 18:25:47 -0700299 std::vector<InstrumentationStackFrame> shadow_stack_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800300 std::vector<uint32_t> dex_pcs_;
Ian Rogers306057f2012-11-26 12:45:53 -0800301 const uintptr_t instrumentation_exit_pc_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100302 bool reached_existing_instrumentation_frames_;
303 size_t instrumentation_stack_depth_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800304 uintptr_t last_return_pc_;
Ian Rogers306057f2012-11-26 12:45:53 -0800305 };
Ian Rogers62d6c772013-02-27 08:32:07 -0800306 if (kVerboseInstrumentation) {
307 std::string thread_name;
308 thread->GetThreadName(thread_name);
309 LOG(INFO) << "Installing exit stubs in " << thread_name;
Ian Rogers306057f2012-11-26 12:45:53 -0800310 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100311
312 Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg);
Ian Rogers700a4022014-05-19 16:49:03 -0700313 std::unique_ptr<Context> context(Context::Create());
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700314 uintptr_t instrumentation_exit_pc = reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc());
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100315 InstallStackVisitor visitor(thread, context.get(), instrumentation_exit_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800316 visitor.WalkStack(true);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100317 CHECK_EQ(visitor.dex_pcs_.size(), thread->GetInstrumentationStack()->size());
Ian Rogers62d6c772013-02-27 08:32:07 -0800318
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100319 if (instrumentation->ShouldNotifyMethodEnterExitEvents()) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100320 // Create method enter events for all methods currently on the thread's stack. We only do this
321 // if no debugger is attached to prevent from posting events twice.
Jeff Haoa15a81b2014-05-27 18:25:47 -0700322 auto ssi = visitor.shadow_stack_.rbegin();
323 for (auto isi = thread->GetInstrumentationStack()->rbegin(),
324 end = thread->GetInstrumentationStack()->rend(); isi != end; ++isi) {
325 while (ssi != visitor.shadow_stack_.rend() && (*ssi).frame_id_ < (*isi).frame_id_) {
326 instrumentation->MethodEnterEvent(thread, (*ssi).this_object_, (*ssi).method_, 0);
327 ++ssi;
328 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100329 uint32_t dex_pc = visitor.dex_pcs_.back();
330 visitor.dex_pcs_.pop_back();
Sebastien Hertz320deb22014-06-11 19:45:05 +0200331 if (!isi->interpreter_entry_) {
332 instrumentation->MethodEnterEvent(thread, (*isi).this_object_, (*isi).method_, dex_pc);
333 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100334 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800335 }
336 thread->VerifyStack();
Ian Rogers306057f2012-11-26 12:45:53 -0800337}
338
Mingyao Yang99170c62015-07-06 11:10:37 -0700339void Instrumentation::InstrumentThreadStack(Thread* thread) {
340 instrumentation_stubs_installed_ = true;
341 InstrumentationInstallStack(thread, this);
342}
343
Ian Rogers62d6c772013-02-27 08:32:07 -0800344// Removes the instrumentation exit pc as the return PC for every quick frame.
345static void InstrumentationRestoreStack(Thread* thread, void* arg)
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +0000346 REQUIRES(Locks::mutator_lock_) {
347 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
348
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200349 struct RestoreStackVisitor FINAL : public StackVisitor {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800350 RestoreStackVisitor(Thread* thread_in, uintptr_t instrumentation_exit_pc,
Ian Rogers62d6c772013-02-27 08:32:07 -0800351 Instrumentation* instrumentation)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100352 : StackVisitor(thread_in, nullptr, kInstrumentationStackWalk),
353 thread_(thread_in),
Ian Rogers62d6c772013-02-27 08:32:07 -0800354 instrumentation_exit_pc_(instrumentation_exit_pc),
355 instrumentation_(instrumentation),
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800356 instrumentation_stack_(thread_in->GetInstrumentationStack()),
Ian Rogers62d6c772013-02-27 08:32:07 -0800357 frames_removed_(0) {}
Ian Rogers306057f2012-11-26 12:45:53 -0800358
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700359 bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800360 if (instrumentation_stack_->size() == 0) {
jeffhao725a9572012-11-13 18:20:12 -0800361 return false; // Stop.
362 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700363 ArtMethod* m = GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700364 if (GetCurrentQuickFrame() == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800365 if (kVerboseInstrumentation) {
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200366 LOG(INFO) << " Ignoring a shadow frame. Frame " << GetFrameId()
David Sehr709b0702016-10-13 09:12:37 -0700367 << " Method=" << ArtMethod::PrettyMethod(m);
Ian Rogers62d6c772013-02-27 08:32:07 -0800368 }
369 return true; // Ignore shadow frames.
370 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700371 if (m == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800372 if (kVerboseInstrumentation) {
373 LOG(INFO) << " Skipping upcall. Frame " << GetFrameId();
374 }
Ian Rogers306057f2012-11-26 12:45:53 -0800375 return true; // Ignore upcalls.
376 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800377 bool removed_stub = false;
378 // TODO: make this search more efficient?
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100379 const size_t frameId = GetFrameId();
380 for (const InstrumentationStackFrame& instrumentation_frame : *instrumentation_stack_) {
381 if (instrumentation_frame.frame_id_ == frameId) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800382 if (kVerboseInstrumentation) {
383 LOG(INFO) << " Removing exit stub in " << DescribeLocation();
384 }
Jeff Hao9a916d32013-06-27 18:45:37 -0700385 if (instrumentation_frame.interpreter_entry_) {
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100386 CHECK(m == Runtime::Current()->GetCalleeSaveMethod(Runtime::kSaveRefsAndArgs));
Jeff Hao9a916d32013-06-27 18:45:37 -0700387 } else {
David Sehr709b0702016-10-13 09:12:37 -0700388 CHECK(m == instrumentation_frame.method_) << ArtMethod::PrettyMethod(m);
Jeff Hao9a916d32013-06-27 18:45:37 -0700389 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800390 SetReturnPc(instrumentation_frame.return_pc_);
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100391 if (instrumentation_->ShouldNotifyMethodEnterExitEvents()) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100392 // Create the method exit events. As the methods didn't really exit the result is 0.
393 // We only do this if no debugger is attached to prevent from posting events twice.
394 instrumentation_->MethodExitEvent(thread_, instrumentation_frame.this_object_, m,
395 GetDexPc(), JValue());
396 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800397 frames_removed_++;
398 removed_stub = true;
399 break;
400 }
401 }
402 if (!removed_stub) {
403 if (kVerboseInstrumentation) {
404 LOG(INFO) << " No exit stub in " << DescribeLocation();
Ian Rogers306057f2012-11-26 12:45:53 -0800405 }
jeffhao725a9572012-11-13 18:20:12 -0800406 }
407 return true; // Continue.
408 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800409 Thread* const thread_;
Ian Rogers306057f2012-11-26 12:45:53 -0800410 const uintptr_t instrumentation_exit_pc_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800411 Instrumentation* const instrumentation_;
412 std::deque<instrumentation::InstrumentationStackFrame>* const instrumentation_stack_;
413 size_t frames_removed_;
jeffhao725a9572012-11-13 18:20:12 -0800414 };
Ian Rogers62d6c772013-02-27 08:32:07 -0800415 if (kVerboseInstrumentation) {
416 std::string thread_name;
417 thread->GetThreadName(thread_name);
418 LOG(INFO) << "Removing exit stubs in " << thread_name;
419 }
420 std::deque<instrumentation::InstrumentationStackFrame>* stack = thread->GetInstrumentationStack();
421 if (stack->size() > 0) {
422 Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700423 uintptr_t instrumentation_exit_pc =
424 reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc());
Ian Rogers62d6c772013-02-27 08:32:07 -0800425 RestoreStackVisitor visitor(thread, instrumentation_exit_pc, instrumentation);
426 visitor.WalkStack(true);
427 CHECK_EQ(visitor.frames_removed_, stack->size());
428 while (stack->size() > 0) {
429 stack->pop_front();
430 }
jeffhao725a9572012-11-13 18:20:12 -0800431 }
432}
433
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200434static bool HasEvent(Instrumentation::InstrumentationEvent expected, uint32_t events) {
435 return (events & expected) != 0;
436}
437
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000438static void PotentiallyAddListenerTo(Instrumentation::InstrumentationEvent event,
439 uint32_t events,
440 std::list<InstrumentationListener*>& list,
441 InstrumentationListener* listener,
442 bool* has_listener)
443 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_) {
444 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
445 if (!HasEvent(event, events)) {
446 return;
447 }
448 // If there is a free slot in the list, we insert the listener in that slot.
449 // Otherwise we add it to the end of the list.
450 auto it = std::find(list.begin(), list.end(), nullptr);
451 if (it != list.end()) {
452 *it = listener;
453 } else {
454 list.push_back(listener);
455 }
456 *has_listener = true;
457}
458
Ian Rogers62d6c772013-02-27 08:32:07 -0800459void Instrumentation::AddListener(InstrumentationListener* listener, uint32_t events) {
460 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000461 PotentiallyAddListenerTo(kMethodEntered,
462 events,
463 method_entry_listeners_,
464 listener,
465 &have_method_entry_listeners_);
466 PotentiallyAddListenerTo(kMethodExited,
467 events,
468 method_exit_listeners_,
469 listener,
470 &have_method_exit_listeners_);
471 PotentiallyAddListenerTo(kMethodUnwind,
472 events,
473 method_unwind_listeners_,
474 listener,
475 &have_method_unwind_listeners_);
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000476 PotentiallyAddListenerTo(kBranch,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000477 events,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000478 branch_listeners_,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000479 listener,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000480 &have_branch_listeners_);
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000481 PotentiallyAddListenerTo(kInvokeVirtualOrInterface,
482 events,
483 invoke_virtual_or_interface_listeners_,
484 listener,
485 &have_invoke_virtual_or_interface_listeners_);
486 PotentiallyAddListenerTo(kDexPcMoved,
487 events,
488 dex_pc_listeners_,
489 listener,
490 &have_dex_pc_listeners_);
491 PotentiallyAddListenerTo(kFieldRead,
492 events,
493 field_read_listeners_,
494 listener,
495 &have_field_read_listeners_);
496 PotentiallyAddListenerTo(kFieldWritten,
497 events,
498 field_write_listeners_,
499 listener,
500 &have_field_write_listeners_);
501 PotentiallyAddListenerTo(kExceptionCaught,
502 events,
503 exception_caught_listeners_,
504 listener,
505 &have_exception_caught_listeners_);
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200506 UpdateInterpreterHandlerTable();
jeffhao725a9572012-11-13 18:20:12 -0800507}
508
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000509static void PotentiallyRemoveListenerFrom(Instrumentation::InstrumentationEvent event,
510 uint32_t events,
511 std::list<InstrumentationListener*>& list,
512 InstrumentationListener* listener,
513 bool* has_listener)
514 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_) {
515 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
516 if (!HasEvent(event, events)) {
517 return;
518 }
519 auto it = std::find(list.begin(), list.end(), listener);
520 if (it != list.end()) {
521 // Just update the entry, do not remove from the list. Removing entries in the list
522 // is unsafe when mutators are iterating over it.
523 *it = nullptr;
524 }
525
526 // Check if the list contains any non-null listener, and update 'has_listener'.
527 for (InstrumentationListener* l : list) {
528 if (l != nullptr) {
529 *has_listener = true;
530 return;
531 }
532 }
533 *has_listener = false;
534}
535
Ian Rogers62d6c772013-02-27 08:32:07 -0800536void Instrumentation::RemoveListener(InstrumentationListener* listener, uint32_t events) {
537 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000538 PotentiallyRemoveListenerFrom(kMethodEntered,
539 events,
540 method_entry_listeners_,
541 listener,
542 &have_method_entry_listeners_);
543 PotentiallyRemoveListenerFrom(kMethodExited,
544 events,
545 method_exit_listeners_,
546 listener,
547 &have_method_exit_listeners_);
548 PotentiallyRemoveListenerFrom(kMethodUnwind,
549 events,
550 method_unwind_listeners_,
551 listener,
552 &have_method_unwind_listeners_);
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000553 PotentiallyRemoveListenerFrom(kBranch,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000554 events,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000555 branch_listeners_,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000556 listener,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000557 &have_branch_listeners_);
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000558 PotentiallyRemoveListenerFrom(kInvokeVirtualOrInterface,
559 events,
560 invoke_virtual_or_interface_listeners_,
561 listener,
562 &have_invoke_virtual_or_interface_listeners_);
563 PotentiallyRemoveListenerFrom(kDexPcMoved,
564 events,
565 dex_pc_listeners_,
566 listener,
567 &have_dex_pc_listeners_);
568 PotentiallyRemoveListenerFrom(kFieldRead,
569 events,
570 field_read_listeners_,
571 listener,
572 &have_field_read_listeners_);
573 PotentiallyRemoveListenerFrom(kFieldWritten,
574 events,
575 field_write_listeners_,
576 listener,
577 &have_field_write_listeners_);
578 PotentiallyRemoveListenerFrom(kExceptionCaught,
579 events,
580 exception_caught_listeners_,
581 listener,
582 &have_exception_caught_listeners_);
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200583 UpdateInterpreterHandlerTable();
jeffhao725a9572012-11-13 18:20:12 -0800584}
585
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200586Instrumentation::InstrumentationLevel Instrumentation::GetCurrentInstrumentationLevel() const {
Alex Light4ba388a2017-01-27 10:26:49 -0800587 if (interpreter_stubs_installed_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200588 return InstrumentationLevel::kInstrumentWithInterpreter;
Ian Rogers62d6c772013-02-27 08:32:07 -0800589 } else if (entry_exit_stubs_installed_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200590 return InstrumentationLevel::kInstrumentWithInstrumentationStubs;
Ian Rogers62d6c772013-02-27 08:32:07 -0800591 } else {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200592 return InstrumentationLevel::kInstrumentNothing;
Ian Rogers62d6c772013-02-27 08:32:07 -0800593 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200594}
595
Alex Lightdba61482016-12-21 08:20:29 -0800596bool Instrumentation::RequiresInstrumentationInstallation(InstrumentationLevel new_level) const {
Alex Light4ba388a2017-01-27 10:26:49 -0800597 // We need to reinstall instrumentation if we go to a different level.
598 return GetCurrentInstrumentationLevel() != new_level;
Alex Lightdba61482016-12-21 08:20:29 -0800599}
600
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200601void Instrumentation::ConfigureStubs(const char* key, InstrumentationLevel desired_level) {
602 // Store the instrumentation level for this key or remove it.
603 if (desired_level == InstrumentationLevel::kInstrumentNothing) {
604 // The client no longer needs instrumentation.
605 requested_instrumentation_levels_.erase(key);
606 } else {
607 // The client needs instrumentation.
608 requested_instrumentation_levels_.Overwrite(key, desired_level);
609 }
610
611 // Look for the highest required instrumentation level.
612 InstrumentationLevel requested_level = InstrumentationLevel::kInstrumentNothing;
613 for (const auto& v : requested_instrumentation_levels_) {
614 requested_level = std::max(requested_level, v.second);
615 }
616
617 interpret_only_ = (requested_level == InstrumentationLevel::kInstrumentWithInterpreter) ||
618 forced_interpret_only_;
619
Alex Lightdba61482016-12-21 08:20:29 -0800620 if (!RequiresInstrumentationInstallation(requested_level)) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800621 // We're already set.
622 return;
623 }
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100624 Thread* const self = Thread::Current();
Ian Rogers62d6c772013-02-27 08:32:07 -0800625 Runtime* runtime = Runtime::Current();
Sebastien Hertza8a697f2015-01-15 12:28:47 +0100626 Locks::mutator_lock_->AssertExclusiveHeld(self);
Ian Rogers62d6c772013-02-27 08:32:07 -0800627 Locks::thread_list_lock_->AssertNotHeld(self);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200628 if (requested_level > InstrumentationLevel::kInstrumentNothing) {
Alex Light4ba388a2017-01-27 10:26:49 -0800629 if (requested_level == InstrumentationLevel::kInstrumentWithInterpreter) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800630 interpreter_stubs_installed_ = true;
Ian Rogers62d6c772013-02-27 08:32:07 -0800631 entry_exit_stubs_installed_ = true;
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200632 } else {
633 CHECK_EQ(requested_level, InstrumentationLevel::kInstrumentWithInstrumentationStubs);
634 entry_exit_stubs_installed_ = true;
635 interpreter_stubs_installed_ = false;
Ian Rogers62d6c772013-02-27 08:32:07 -0800636 }
Mathieu Chartiere0671ce2015-07-28 17:23:28 -0700637 InstallStubsClassVisitor visitor(this);
638 runtime->GetClassLinker()->VisitClasses(&visitor);
Ian Rogers62d6c772013-02-27 08:32:07 -0800639 instrumentation_stubs_installed_ = true;
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100640 MutexLock mu(self, *Locks::thread_list_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800641 runtime->GetThreadList()->ForEach(InstrumentationInstallStack, this);
642 } else {
643 interpreter_stubs_installed_ = false;
644 entry_exit_stubs_installed_ = false;
Mathieu Chartiere0671ce2015-07-28 17:23:28 -0700645 InstallStubsClassVisitor visitor(this);
646 runtime->GetClassLinker()->VisitClasses(&visitor);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100647 // Restore stack only if there is no method currently deoptimized.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700648 bool empty;
649 {
650 ReaderMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700651 empty = IsDeoptimizedMethodsEmpty(); // Avoid lock violation.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700652 }
653 if (empty) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100654 MutexLock mu(self, *Locks::thread_list_lock_);
655 Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this);
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +0000656 // Only do this after restoring, as walking the stack when restoring will see
657 // the instrumentation exit pc.
658 instrumentation_stubs_installed_ = false;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100659 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800660 }
jeffhao725a9572012-11-13 18:20:12 -0800661}
662
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200663static void ResetQuickAllocEntryPointsForThread(Thread* thread, void* arg ATTRIBUTE_UNUSED) {
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800664 thread->ResetQuickAllocEntryPointsForThread(kUseReadBarrier && thread->GetIsGcMarking());
Ian Rogersfa824272013-11-05 16:12:57 -0800665}
666
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700667void Instrumentation::SetEntrypointsInstrumented(bool instrumented) {
668 Thread* self = Thread::Current();
Mathieu Chartier661974a2014-01-09 11:23:53 -0800669 Runtime* runtime = Runtime::Current();
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700670 Locks::mutator_lock_->AssertNotHeld(self);
671 Locks::instrument_entrypoints_lock_->AssertHeld(self);
672 if (runtime->IsStarted()) {
Mathieu Chartier4f55e222015-09-04 13:26:21 -0700673 ScopedSuspendAll ssa(__FUNCTION__);
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700674 MutexLock mu(self, *Locks::runtime_shutdown_lock_);
Mathieu Chartier661974a2014-01-09 11:23:53 -0800675 SetQuickAllocEntryPointsInstrumented(instrumented);
676 ResetQuickAllocEntryPoints();
Mathieu Chartier50e93312016-03-16 11:25:29 -0700677 alloc_entrypoints_instrumented_ = instrumented;
Mathieu Chartier4f55e222015-09-04 13:26:21 -0700678 } else {
679 MutexLock mu(self, *Locks::runtime_shutdown_lock_);
680 SetQuickAllocEntryPointsInstrumented(instrumented);
Andreas Gampe157c77e2016-10-17 17:44:41 -0700681
682 // Note: ResetQuickAllocEntryPoints only works when the runtime is started. Manually run the
683 // update for just this thread.
Andreas Gampe162ae502016-10-18 10:03:42 -0700684 // Note: self may be null. One of those paths is setting instrumentation in the Heap
685 // constructor for gcstress mode.
686 if (self != nullptr) {
687 ResetQuickAllocEntryPointsForThread(self, nullptr);
688 }
Andreas Gampe157c77e2016-10-17 17:44:41 -0700689
Mathieu Chartier50e93312016-03-16 11:25:29 -0700690 alloc_entrypoints_instrumented_ = instrumented;
Mathieu Chartier661974a2014-01-09 11:23:53 -0800691 }
692}
693
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700694void Instrumentation::InstrumentQuickAllocEntryPoints() {
695 MutexLock mu(Thread::Current(), *Locks::instrument_entrypoints_lock_);
696 InstrumentQuickAllocEntryPointsLocked();
Ian Rogersfa824272013-11-05 16:12:57 -0800697}
698
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700699void Instrumentation::UninstrumentQuickAllocEntryPoints() {
700 MutexLock mu(Thread::Current(), *Locks::instrument_entrypoints_lock_);
701 UninstrumentQuickAllocEntryPointsLocked();
702}
703
704void Instrumentation::InstrumentQuickAllocEntryPointsLocked() {
705 Locks::instrument_entrypoints_lock_->AssertHeld(Thread::Current());
706 if (quick_alloc_entry_points_instrumentation_counter_ == 0) {
707 SetEntrypointsInstrumented(true);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800708 }
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700709 ++quick_alloc_entry_points_instrumentation_counter_;
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700710}
711
712void Instrumentation::UninstrumentQuickAllocEntryPointsLocked() {
713 Locks::instrument_entrypoints_lock_->AssertHeld(Thread::Current());
714 CHECK_GT(quick_alloc_entry_points_instrumentation_counter_, 0U);
715 --quick_alloc_entry_points_instrumentation_counter_;
716 if (quick_alloc_entry_points_instrumentation_counter_ == 0) {
717 SetEntrypointsInstrumented(false);
718 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800719}
720
721void Instrumentation::ResetQuickAllocEntryPoints() {
722 Runtime* runtime = Runtime::Current();
723 if (runtime->IsStarted()) {
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800724 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700725 runtime->GetThreadList()->ForEach(ResetQuickAllocEntryPointsForThread, nullptr);
Ian Rogersfa824272013-11-05 16:12:57 -0800726 }
727}
728
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700729void Instrumentation::UpdateMethodsCodeImpl(ArtMethod* method, const void* quick_code) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800730 const void* new_quick_code;
Ian Rogers62d6c772013-02-27 08:32:07 -0800731 if (LIKELY(!instrumentation_stubs_installed_)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800732 new_quick_code = quick_code;
Jeff Hao65d15d92013-07-16 16:39:33 -0700733 } else {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100734 if ((interpreter_stubs_installed_ || IsDeoptimized(method)) && !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800735 new_quick_code = GetQuickToInterpreterBridge();
Jeff Hao65d15d92013-07-16 16:39:33 -0700736 } else {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700737 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700738 if (class_linker->IsQuickResolutionStub(quick_code) ||
739 class_linker->IsQuickToInterpreterBridge(quick_code)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700740 new_quick_code = quick_code;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700741 } else if (entry_exit_stubs_installed_) {
742 new_quick_code = GetQuickInstrumentationEntryPoint();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700743 } else {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700744 new_quick_code = quick_code;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700745 }
Jeff Hao65d15d92013-07-16 16:39:33 -0700746 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800747 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800748 UpdateEntrypoints(method, new_quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100749}
750
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700751void Instrumentation::UpdateMethodsCode(ArtMethod* method, const void* quick_code) {
752 DCHECK(method->GetDeclaringClass()->IsResolved());
753 UpdateMethodsCodeImpl(method, quick_code);
754}
755
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000756void Instrumentation::UpdateMethodsCodeForJavaDebuggable(ArtMethod* method,
757 const void* quick_code) {
758 // When the runtime is set to Java debuggable, we may update the entry points of
759 // all methods of a class to the interpreter bridge. A method's declaring class
760 // might not be in resolved state yet in that case, so we bypass the DCHECK in
761 // UpdateMethodsCode.
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700762 UpdateMethodsCodeImpl(method, quick_code);
763}
764
Mathieu Chartiere401d142015-04-22 13:56:20 -0700765bool Instrumentation::AddDeoptimizedMethod(ArtMethod* method) {
766 if (IsDeoptimizedMethod(method)) {
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700767 // Already in the map. Return.
768 return false;
769 }
770 // Not found. Add it.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700771 deoptimized_methods_.insert(method);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700772 return true;
773}
774
Mathieu Chartiere401d142015-04-22 13:56:20 -0700775bool Instrumentation::IsDeoptimizedMethod(ArtMethod* method) {
776 return deoptimized_methods_.find(method) != deoptimized_methods_.end();
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700777}
778
Mathieu Chartiere401d142015-04-22 13:56:20 -0700779ArtMethod* Instrumentation::BeginDeoptimizedMethod() {
780 if (deoptimized_methods_.empty()) {
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700781 // Empty.
782 return nullptr;
783 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700784 return *deoptimized_methods_.begin();
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700785}
786
Mathieu Chartiere401d142015-04-22 13:56:20 -0700787bool Instrumentation::RemoveDeoptimizedMethod(ArtMethod* method) {
788 auto it = deoptimized_methods_.find(method);
789 if (it == deoptimized_methods_.end()) {
790 return false;
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700791 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700792 deoptimized_methods_.erase(it);
793 return true;
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700794}
795
796bool Instrumentation::IsDeoptimizedMethodsEmpty() const {
797 return deoptimized_methods_.empty();
798}
799
Mathieu Chartiere401d142015-04-22 13:56:20 -0700800void Instrumentation::Deoptimize(ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100801 CHECK(!method->IsNative());
802 CHECK(!method->IsProxyMethod());
Alex Light9139e002015-10-09 15:59:48 -0700803 CHECK(method->IsInvokable());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100804
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700805 Thread* self = Thread::Current();
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700806 {
807 WriterMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700808 bool has_not_been_deoptimized = AddDeoptimizedMethod(method);
David Sehr709b0702016-10-13 09:12:37 -0700809 CHECK(has_not_been_deoptimized) << "Method " << ArtMethod::PrettyMethod(method)
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200810 << " is already deoptimized";
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700811 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100812 if (!interpreter_stubs_installed_) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800813 UpdateEntrypoints(method, GetQuickInstrumentationEntryPoint());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100814
815 // Install instrumentation exit stub and instrumentation frames. We may already have installed
816 // these previously so it will only cover the newly created frames.
817 instrumentation_stubs_installed_ = true;
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700818 MutexLock mu(self, *Locks::thread_list_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100819 Runtime::Current()->GetThreadList()->ForEach(InstrumentationInstallStack, this);
820 }
821}
822
Mathieu Chartiere401d142015-04-22 13:56:20 -0700823void Instrumentation::Undeoptimize(ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100824 CHECK(!method->IsNative());
825 CHECK(!method->IsProxyMethod());
Alex Light9139e002015-10-09 15:59:48 -0700826 CHECK(method->IsInvokable());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100827
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700828 Thread* self = Thread::Current();
829 bool empty;
830 {
831 WriterMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700832 bool found_and_erased = RemoveDeoptimizedMethod(method);
David Sehr709b0702016-10-13 09:12:37 -0700833 CHECK(found_and_erased) << "Method " << ArtMethod::PrettyMethod(method)
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700834 << " is not deoptimized";
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700835 empty = IsDeoptimizedMethodsEmpty();
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700836 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100837
838 // Restore code and possibly stack only if we did not deoptimize everything.
839 if (!interpreter_stubs_installed_) {
840 // Restore its code or resolution trampoline.
841 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800842 if (method->IsStatic() && !method->IsConstructor() &&
843 !method->GetDeclaringClass()->IsInitialized()) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800844 UpdateEntrypoints(method, GetQuickResolutionStub());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100845 } else {
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000846 const void* quick_code = NeedDebugVersionFor(method)
847 ? GetQuickToInterpreterBridge()
848 : class_linker->GetQuickOatCodeFor(method);
Elliott Hughes956af0f2014-12-11 14:34:28 -0800849 UpdateEntrypoints(method, quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100850 }
851
852 // If there is no deoptimized method left, we can restore the stack of each thread.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700853 if (empty) {
854 MutexLock mu(self, *Locks::thread_list_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100855 Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this);
856 instrumentation_stubs_installed_ = false;
857 }
858 }
859}
860
Mathieu Chartiere401d142015-04-22 13:56:20 -0700861bool Instrumentation::IsDeoptimized(ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100862 DCHECK(method != nullptr);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700863 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700864 return IsDeoptimizedMethod(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100865}
866
867void Instrumentation::EnableDeoptimization() {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700868 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700869 CHECK(IsDeoptimizedMethodsEmpty());
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100870 CHECK_EQ(deoptimization_enabled_, false);
871 deoptimization_enabled_ = true;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100872}
873
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200874void Instrumentation::DisableDeoptimization(const char* key) {
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100875 CHECK_EQ(deoptimization_enabled_, true);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100876 // If we deoptimized everything, undo it.
Alex Lightdba61482016-12-21 08:20:29 -0800877 InstrumentationLevel level = GetCurrentInstrumentationLevel();
878 if (level == InstrumentationLevel::kInstrumentWithInterpreter) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200879 UndeoptimizeEverything(key);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100880 }
881 // Undeoptimized selected methods.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700882 while (true) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700883 ArtMethod* method;
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700884 {
885 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700886 if (IsDeoptimizedMethodsEmpty()) {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700887 break;
888 }
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700889 method = BeginDeoptimizedMethod();
890 CHECK(method != nullptr);
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700891 }
892 Undeoptimize(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100893 }
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100894 deoptimization_enabled_ = false;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100895}
896
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100897// Indicates if instrumentation should notify method enter/exit events to the listeners.
898bool Instrumentation::ShouldNotifyMethodEnterExitEvents() const {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200899 if (!HasMethodEntryListeners() && !HasMethodExitListeners()) {
900 return false;
901 }
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100902 return !deoptimization_enabled_ && !interpreter_stubs_installed_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100903}
904
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200905void Instrumentation::DeoptimizeEverything(const char* key) {
906 CHECK(deoptimization_enabled_);
907 ConfigureStubs(key, InstrumentationLevel::kInstrumentWithInterpreter);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100908}
909
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200910void Instrumentation::UndeoptimizeEverything(const char* key) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100911 CHECK(interpreter_stubs_installed_);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200912 CHECK(deoptimization_enabled_);
913 ConfigureStubs(key, InstrumentationLevel::kInstrumentNothing);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100914}
915
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200916void Instrumentation::EnableMethodTracing(const char* key, bool needs_interpreter) {
917 InstrumentationLevel level;
918 if (needs_interpreter) {
919 level = InstrumentationLevel::kInstrumentWithInterpreter;
920 } else {
921 level = InstrumentationLevel::kInstrumentWithInstrumentationStubs;
922 }
923 ConfigureStubs(key, level);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100924}
925
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200926void Instrumentation::DisableMethodTracing(const char* key) {
927 ConfigureStubs(key, InstrumentationLevel::kInstrumentNothing);
jeffhao725a9572012-11-13 18:20:12 -0800928}
929
Andreas Gampe542451c2016-07-26 09:02:02 -0700930const void* Instrumentation::GetQuickCodeFor(ArtMethod* method, PointerSize pointer_size) const {
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100931 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers62d6c772013-02-27 08:32:07 -0800932 if (LIKELY(!instrumentation_stubs_installed_)) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800933 const void* code = method->GetEntryPointFromQuickCompiledCodePtrSize(pointer_size);
Vladimir Marko8a630572014-04-09 18:45:35 +0100934 DCHECK(code != nullptr);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700935 if (LIKELY(!class_linker->IsQuickResolutionStub(code) &&
936 !class_linker->IsQuickToInterpreterBridge(code)) &&
937 !class_linker->IsQuickResolutionStub(code) &&
938 !class_linker->IsQuickToInterpreterBridge(code)) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800939 return code;
940 }
941 }
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100942 return class_linker->GetQuickOatCodeFor(method);
jeffhao725a9572012-11-13 18:20:12 -0800943}
944
Alex Lightd7661582017-05-01 13:48:16 -0700945void Instrumentation::MethodEnterEventImpl(Thread* thread,
946 ObjPtr<mirror::Object> this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700947 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800948 uint32_t dex_pc) const {
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000949 if (HasMethodEntryListeners()) {
Alex Lightd7661582017-05-01 13:48:16 -0700950 Thread* self = Thread::Current();
951 StackHandleScope<1> hs(self);
952 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000953 for (InstrumentationListener* listener : method_entry_listeners_) {
954 if (listener != nullptr) {
Alex Lightd7661582017-05-01 13:48:16 -0700955 listener->MethodEntered(thread, thiz, method, dex_pc);
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000956 }
957 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800958 }
959}
960
Alex Lightd7661582017-05-01 13:48:16 -0700961void Instrumentation::MethodExitEventImpl(Thread* thread,
962 ObjPtr<mirror::Object> this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700963 ArtMethod* method,
Alex Lightd7661582017-05-01 13:48:16 -0700964 uint32_t dex_pc,
965 const JValue& return_value) const {
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000966 if (HasMethodExitListeners()) {
Alex Lightd7661582017-05-01 13:48:16 -0700967 Thread* self = Thread::Current();
968 StackHandleScope<2> hs(self);
969 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
970 if (method->GetInterfaceMethodIfProxy(kRuntimePointerSize)
971 ->GetReturnTypePrimitive() != Primitive::kPrimNot) {
972 for (InstrumentationListener* listener : method_exit_listeners_) {
973 if (listener != nullptr) {
974 listener->MethodExited(thread, thiz, method, dex_pc, return_value);
975 }
976 }
977 } else {
978 Handle<mirror::Object> ret(hs.NewHandle(return_value.GetL()));
979 for (InstrumentationListener* listener : method_exit_listeners_) {
980 if (listener != nullptr) {
981 listener->MethodExited(thread, thiz, method, dex_pc, ret);
982 }
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000983 }
984 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800985 }
986}
987
Alex Lightd7661582017-05-01 13:48:16 -0700988void Instrumentation::MethodUnwindEvent(Thread* thread,
989 mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700990 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800991 uint32_t dex_pc) const {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200992 if (HasMethodUnwindListeners()) {
Alex Lightd7661582017-05-01 13:48:16 -0700993 Thread* self = Thread::Current();
994 StackHandleScope<1> hs(self);
995 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
Mathieu Chartier02e25112013-08-14 16:14:24 -0700996 for (InstrumentationListener* listener : method_unwind_listeners_) {
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000997 if (listener != nullptr) {
Alex Lightd7661582017-05-01 13:48:16 -0700998 listener->MethodUnwind(thread, thiz, method, dex_pc);
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000999 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001000 }
1001 }
1002}
1003
Alex Lightd7661582017-05-01 13:48:16 -07001004void Instrumentation::DexPcMovedEventImpl(Thread* thread,
1005 ObjPtr<mirror::Object> this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001006 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -08001007 uint32_t dex_pc) const {
Alex Lightd7661582017-05-01 13:48:16 -07001008 Thread* self = Thread::Current();
1009 StackHandleScope<1> hs(self);
1010 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001011 for (InstrumentationListener* listener : dex_pc_listeners_) {
1012 if (listener != nullptr) {
Alex Lightd7661582017-05-01 13:48:16 -07001013 listener->DexPcMoved(thread, thiz, method, dex_pc);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001014 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001015 }
1016}
1017
Nicolas Geoffray81f0f952016-01-20 16:25:19 +00001018void Instrumentation::BranchImpl(Thread* thread,
1019 ArtMethod* method,
1020 uint32_t dex_pc,
1021 int32_t offset) const {
1022 for (InstrumentationListener* listener : branch_listeners_) {
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001023 if (listener != nullptr) {
Nicolas Geoffray81f0f952016-01-20 16:25:19 +00001024 listener->Branch(thread, method, dex_pc, offset);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001025 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001026 }
1027}
1028
Nicolas Geoffray5550ca82015-08-21 18:38:30 +01001029void Instrumentation::InvokeVirtualOrInterfaceImpl(Thread* thread,
Alex Lightd7661582017-05-01 13:48:16 -07001030 ObjPtr<mirror::Object> this_object,
Nicolas Geoffray5550ca82015-08-21 18:38:30 +01001031 ArtMethod* caller,
1032 uint32_t dex_pc,
1033 ArtMethod* callee) const {
Alex Lightd7661582017-05-01 13:48:16 -07001034 Thread* self = Thread::Current();
1035 StackHandleScope<1> hs(self);
1036 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
Nicolas Geoffray5550ca82015-08-21 18:38:30 +01001037 for (InstrumentationListener* listener : invoke_virtual_or_interface_listeners_) {
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001038 if (listener != nullptr) {
Alex Lightd7661582017-05-01 13:48:16 -07001039 listener->InvokeVirtualOrInterface(thread, thiz, caller, dex_pc, callee);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001040 }
Nicolas Geoffray5550ca82015-08-21 18:38:30 +01001041 }
1042}
1043
Alex Lightd7661582017-05-01 13:48:16 -07001044void Instrumentation::FieldReadEventImpl(Thread* thread,
1045 ObjPtr<mirror::Object> this_object,
1046 ArtMethod* method,
1047 uint32_t dex_pc,
Mathieu Chartierc7853442015-03-27 14:35:38 -07001048 ArtField* field) const {
Alex Lightd7661582017-05-01 13:48:16 -07001049 Thread* self = Thread::Current();
1050 StackHandleScope<1> hs(self);
1051 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001052 for (InstrumentationListener* listener : field_read_listeners_) {
1053 if (listener != nullptr) {
Alex Lightd7661582017-05-01 13:48:16 -07001054 listener->FieldRead(thread, thiz, method, dex_pc, field);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001055 }
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +02001056 }
1057}
1058
Alex Lightd7661582017-05-01 13:48:16 -07001059void Instrumentation::FieldWriteEventImpl(Thread* thread,
1060 ObjPtr<mirror::Object> this_object,
1061 ArtMethod* method,
1062 uint32_t dex_pc,
1063 ArtField* field,
1064 const JValue& field_value) const {
1065 Thread* self = Thread::Current();
1066 StackHandleScope<2> hs(self);
1067 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
1068 if (field->IsPrimitiveType()) {
1069 for (InstrumentationListener* listener : field_write_listeners_) {
1070 if (listener != nullptr) {
1071 listener->FieldWritten(thread, thiz, method, dex_pc, field, field_value);
1072 }
1073 }
1074 } else {
1075 Handle<mirror::Object> val(hs.NewHandle(field_value.GetL()));
1076 for (InstrumentationListener* listener : field_write_listeners_) {
1077 if (listener != nullptr) {
1078 listener->FieldWritten(thread, thiz, method, dex_pc, field, val);
1079 }
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001080 }
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +02001081 }
1082}
1083
Nicolas Geoffray14691c52015-03-05 10:40:17 +00001084void Instrumentation::ExceptionCaughtEvent(Thread* thread,
Sebastien Hertz947ff082013-09-17 14:10:13 +02001085 mirror::Throwable* exception_object) const {
Hiroshi Yamauchi3481f7a2017-02-10 12:07:36 -08001086 Thread* self = Thread::Current();
1087 StackHandleScope<1> hs(self);
1088 Handle<mirror::Throwable> h_exception(hs.NewHandle(exception_object));
Sebastien Hertz9f102032014-05-23 08:59:42 +02001089 if (HasExceptionCaughtListeners()) {
Hiroshi Yamauchi3481f7a2017-02-10 12:07:36 -08001090 DCHECK_EQ(thread->GetException(), h_exception.Get());
Jeff Haoc0bd4da2013-04-11 15:52:28 -07001091 thread->ClearException();
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001092 for (InstrumentationListener* listener : exception_caught_listeners_) {
1093 if (listener != nullptr) {
Alex Lightd7661582017-05-01 13:48:16 -07001094 listener->ExceptionCaught(thread, h_exception);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001095 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001096 }
Hiroshi Yamauchi3481f7a2017-02-10 12:07:36 -08001097 thread->SetException(h_exception.Get());
Ian Rogers62d6c772013-02-27 08:32:07 -08001098 }
1099}
1100
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +00001101// Computes a frame ID by ignoring inlined frames.
1102size_t Instrumentation::ComputeFrameId(Thread* self,
1103 size_t frame_depth,
1104 size_t inlined_frames_before_frame) {
1105 CHECK_GE(frame_depth, inlined_frames_before_frame);
1106 size_t no_inline_depth = frame_depth - inlined_frames_before_frame;
1107 return StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk) - no_inline_depth;
1108}
1109
Ian Rogers62d6c772013-02-27 08:32:07 -08001110static void CheckStackDepth(Thread* self, const InstrumentationStackFrame& instrumentation_frame,
1111 int delta)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001112 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +01001113 size_t frame_id = StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk) + delta;
Ian Rogers62d6c772013-02-27 08:32:07 -08001114 if (frame_id != instrumentation_frame.frame_id_) {
1115 LOG(ERROR) << "Expected frame_id=" << frame_id << " but found "
1116 << instrumentation_frame.frame_id_;
1117 StackVisitor::DescribeStack(self);
1118 CHECK_EQ(frame_id, instrumentation_frame.frame_id_);
1119 }
1120}
1121
1122void Instrumentation::PushInstrumentationStackFrame(Thread* self, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001123 ArtMethod* method,
Jeff Hao9a916d32013-06-27 18:45:37 -07001124 uintptr_t lr, bool interpreter_entry) {
Ian Rogers62d6c772013-02-27 08:32:07 -08001125 // We have a callee-save frame meaning this value is guaranteed to never be 0.
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +01001126 size_t frame_id = StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk);
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 }
1132 instrumentation::InstrumentationStackFrame instrumentation_frame(this_object, method, lr,
Jeff Hao9a916d32013-06-27 18:45:37 -07001133 frame_id, interpreter_entry);
Ian Rogers62d6c772013-02-27 08:32:07 -08001134 stack->push_front(instrumentation_frame);
1135
Sebastien Hertz320deb22014-06-11 19:45:05 +02001136 if (!interpreter_entry) {
1137 MethodEnterEvent(self, this_object, method, 0);
1138 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001139}
1140
Andreas Gamped58342c2014-06-05 14:18:08 -07001141TwoWordReturn Instrumentation::PopInstrumentationStackFrame(Thread* self, uintptr_t* return_pc,
1142 uint64_t gpr_result,
1143 uint64_t fpr_result) {
Ian Rogers62d6c772013-02-27 08:32:07 -08001144 // Do the pop.
1145 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
1146 CHECK_GT(stack->size(), 0U);
1147 InstrumentationStackFrame instrumentation_frame = stack->front();
1148 stack->pop_front();
1149
1150 // Set return PC and check the sanity of the stack.
1151 *return_pc = instrumentation_frame.return_pc_;
1152 CheckStackDepth(self, instrumentation_frame, 0);
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001153 self->VerifyStack();
Ian Rogers62d6c772013-02-27 08:32:07 -08001154
Mathieu Chartiere401d142015-04-22 13:56:20 -07001155 ArtMethod* method = instrumentation_frame.method_;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001156 uint32_t length;
Andreas Gampe542451c2016-07-26 09:02:02 -07001157 const PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Mingyao Yang99170c62015-07-06 11:10:37 -07001158 char return_shorty = method->GetInterfaceMethodIfProxy(pointer_size)->GetShorty(&length)[0];
Ian Rogers62d6c772013-02-27 08:32:07 -08001159 JValue return_value;
1160 if (return_shorty == 'V') {
1161 return_value.SetJ(0);
1162 } else if (return_shorty == 'F' || return_shorty == 'D') {
1163 return_value.SetJ(fpr_result);
1164 } else {
1165 return_value.SetJ(gpr_result);
1166 }
1167 // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to
1168 // return_pc.
1169 uint32_t dex_pc = DexFile::kDexNoIndex;
1170 mirror::Object* this_object = instrumentation_frame.this_object_;
Sebastien Hertz320deb22014-06-11 19:45:05 +02001171 if (!instrumentation_frame.interpreter_entry_) {
1172 MethodExitEvent(self, this_object, instrumentation_frame.method_, dex_pc, return_value);
1173 }
jeffhao725a9572012-11-13 18:20:12 -08001174
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01001175 // Deoptimize if the caller needs to continue execution in the interpreter. Do nothing if we get
1176 // back to an upcall.
1177 NthCallerVisitor visitor(self, 1, true);
1178 visitor.WalkStack(true);
Sebastien Hertz270a0e12015-01-16 19:49:09 +01001179 bool deoptimize = (visitor.caller != nullptr) &&
Daniel Mihalyieb076692014-08-22 17:33:31 +02001180 (interpreter_stubs_installed_ || IsDeoptimized(visitor.caller) ||
1181 Dbg::IsForcedInterpreterNeededForUpcall(self, visitor.caller));
Nicolas Geoffraya0619e22016-12-20 13:57:43 +00001182 if (deoptimize && Runtime::Current()->IsAsyncDeoptimizeable(*return_pc)) {
Ian Rogers62d6c772013-02-27 08:32:07 -08001183 if (kVerboseInstrumentation) {
Andreas Gampe46ee31b2016-12-14 10:11:49 -08001184 LOG(INFO) << "Deoptimizing "
1185 << visitor.caller->PrettyMethod()
1186 << " by returning from "
1187 << method->PrettyMethod()
1188 << " with result "
1189 << std::hex << return_value.GetJ() << std::dec
1190 << " in "
1191 << *self;
Ian Rogers62d6c772013-02-27 08:32:07 -08001192 }
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01001193 self->PushDeoptimizationContext(return_value,
1194 return_shorty == 'L',
1195 false /* from_code */,
Sebastien Hertz07474662015-08-25 15:12:33 +00001196 nullptr /* no pending exception */);
Andreas Gamped58342c2014-06-05 14:18:08 -07001197 return GetTwoWordSuccessValue(*return_pc,
1198 reinterpret_cast<uintptr_t>(GetQuickDeoptimizationEntryPoint()));
Ian Rogers62d6c772013-02-27 08:32:07 -08001199 } else {
Nicolas Geoffraya0619e22016-12-20 13:57:43 +00001200 if (deoptimize && !Runtime::Current()->IsAsyncDeoptimizeable(*return_pc)) {
1201 LOG(WARNING) << "Got a deoptimization request on un-deoptimizable " << method->PrettyMethod()
1202 << " at PC " << reinterpret_cast<void*>(*return_pc);
1203 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001204 if (kVerboseInstrumentation) {
David Sehr709b0702016-10-13 09:12:37 -07001205 LOG(INFO) << "Returning from " << method->PrettyMethod()
Brian Carlstrom2d888622013-07-18 17:02:00 -07001206 << " to PC " << reinterpret_cast<void*>(*return_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -08001207 }
Andreas Gamped58342c2014-06-05 14:18:08 -07001208 return GetTwoWordSuccessValue(0, *return_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -08001209 }
jeffhao725a9572012-11-13 18:20:12 -08001210}
1211
Mingyao Yangf711f2c2016-05-23 12:29:39 -07001212uintptr_t Instrumentation::PopMethodForUnwind(Thread* self, bool is_deoptimization) const {
Ian Rogers62d6c772013-02-27 08:32:07 -08001213 // Do the pop.
1214 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
1215 CHECK_GT(stack->size(), 0U);
1216 InstrumentationStackFrame instrumentation_frame = stack->front();
1217 // TODO: bring back CheckStackDepth(self, instrumentation_frame, 2);
1218 stack->pop_front();
1219
Mathieu Chartiere401d142015-04-22 13:56:20 -07001220 ArtMethod* method = instrumentation_frame.method_;
Ian Rogers62d6c772013-02-27 08:32:07 -08001221 if (is_deoptimization) {
1222 if (kVerboseInstrumentation) {
David Sehr709b0702016-10-13 09:12:37 -07001223 LOG(INFO) << "Popping for deoptimization " << ArtMethod::PrettyMethod(method);
Ian Rogers62d6c772013-02-27 08:32:07 -08001224 }
1225 } else {
1226 if (kVerboseInstrumentation) {
David Sehr709b0702016-10-13 09:12:37 -07001227 LOG(INFO) << "Popping for unwind " << ArtMethod::PrettyMethod(method);
Ian Rogers62d6c772013-02-27 08:32:07 -08001228 }
1229
1230 // Notify listeners of method unwind.
1231 // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to
1232 // return_pc.
1233 uint32_t dex_pc = DexFile::kDexNoIndex;
1234 MethodUnwindEvent(self, instrumentation_frame.this_object_, method, dex_pc);
1235 }
Mingyao Yangf711f2c2016-05-23 12:29:39 -07001236 return instrumentation_frame.return_pc_;
Ian Rogers62d6c772013-02-27 08:32:07 -08001237}
1238
1239std::string InstrumentationStackFrame::Dump() const {
1240 std::ostringstream os;
David Sehr709b0702016-10-13 09:12:37 -07001241 os << "Frame " << frame_id_ << " " << ArtMethod::PrettyMethod(method_) << ":"
Ian Rogers62d6c772013-02-27 08:32:07 -08001242 << reinterpret_cast<void*>(return_pc_) << " this=" << reinterpret_cast<void*>(this_object_);
1243 return os.str();
1244}
1245
1246} // namespace instrumentation
jeffhao725a9572012-11-13 18:20:12 -08001247} // namespace art