blob: 87caf1b4579fb9dfa1575fe50603a376c773bf01 [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),
Alex Light6e1607e2017-08-23 10:06:18 -0700108 have_exception_thrown_listeners_(false),
Alex Lighte814f9d2017-07-31 16:14:39 -0700109 have_watched_frame_pop_listeners_(false),
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000110 have_branch_listeners_(false),
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +0000111 have_invoke_virtual_or_interface_listeners_(false),
Mathieu Chartierb8aa1e42016-04-05 14:36:57 -0700112 deoptimized_methods_lock_("deoptimized methods lock", kDeoptimizedMethodsLock),
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700113 deoptimization_enabled_(false),
114 interpreter_handler_table_(kMainHandlerTable),
Mathieu Chartier50e93312016-03-16 11:25:29 -0700115 quick_alloc_entry_points_instrumentation_counter_(0),
116 alloc_entrypoints_instrumented_(false) {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700117}
118
Sebastien Hertza10aa372015-01-21 17:30:58 +0100119void Instrumentation::InstallStubsForClass(mirror::Class* klass) {
Vladimir Marko72ab6842017-01-20 19:32:50 +0000120 if (!klass->IsResolved()) {
Sebastien Hertza8a697f2015-01-15 12:28:47 +0100121 // We need the class to be resolved to install/uninstall stubs. Otherwise its methods
122 // could not be initialized or linked with regards to class inheritance.
Vladimir Marko72ab6842017-01-20 19:32:50 +0000123 } else if (klass->IsErroneousResolved()) {
124 // We can't execute code in a erroneous class: do nothing.
Sebastien Hertza8a697f2015-01-15 12:28:47 +0100125 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700126 for (ArtMethod& method : klass->GetMethods(kRuntimePointerSize)) {
Alex Light51a64d52015-12-17 13:55:59 -0800127 InstallStubsForMethod(&method);
Sebastien Hertza8a697f2015-01-15 12:28:47 +0100128 }
jeffhao725a9572012-11-13 18:20:12 -0800129 }
jeffhao725a9572012-11-13 18:20:12 -0800130}
131
Mathieu Chartiere401d142015-04-22 13:56:20 -0700132static void UpdateEntrypoints(ArtMethod* method, const void* quick_code)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700133 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800134 method->SetEntryPointFromQuickCompiledCode(quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100135}
136
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000137bool Instrumentation::NeedDebugVersionFor(ArtMethod* method) const REQUIRES_SHARED(Locks::mutator_lock_) {
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800138 return Dbg::IsDebuggerActive() &&
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000139 Runtime::Current()->IsJavaDebuggable() &&
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800140 !method->IsNative() &&
141 !method->IsProxyMethod();
142}
143
Mathieu Chartiere401d142015-04-22 13:56:20 -0700144void Instrumentation::InstallStubsForMethod(ArtMethod* method) {
Alex Light9139e002015-10-09 15:59:48 -0700145 if (!method->IsInvokable() || method->IsProxyMethod()) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100146 // Do not change stubs for these methods.
147 return;
148 }
Jeff Hao56802772014-08-19 10:17:36 -0700149 // Don't stub Proxy.<init>. Note that the Proxy class itself is not a proxy class.
150 if (method->IsConstructor() &&
151 method->GetDeclaringClass()->DescriptorEquals("Ljava/lang/reflect/Proxy;")) {
Jeff Haodb8a6642014-08-14 17:18:52 -0700152 return;
153 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800154 const void* new_quick_code;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100155 bool uninstall = !entry_exit_stubs_installed_ && !interpreter_stubs_installed_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800156 Runtime* const runtime = Runtime::Current();
157 ClassLinker* const class_linker = runtime->GetClassLinker();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100158 bool is_class_initialized = method->GetDeclaringClass()->IsInitialized();
159 if (uninstall) {
160 if ((forced_interpret_only_ || IsDeoptimized(method)) && !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800161 new_quick_code = GetQuickToInterpreterBridge();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100162 } else if (is_class_initialized || !method->IsStatic() || method->IsConstructor()) {
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000163 if (NeedDebugVersionFor(method)) {
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800164 new_quick_code = GetQuickToInterpreterBridge();
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000165 } else {
166 new_quick_code = class_linker->GetQuickOatCodeFor(method);
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800167 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100168 } else {
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700169 new_quick_code = GetQuickResolutionStub();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100170 }
171 } else { // !uninstall
Sebastien Hertzbae182c2013-12-17 10:42:03 +0100172 if ((interpreter_stubs_installed_ || forced_interpret_only_ || IsDeoptimized(method)) &&
173 !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800174 new_quick_code = GetQuickToInterpreterBridge();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100175 } else {
176 // Do not overwrite resolution trampoline. When the trampoline initializes the method's
177 // class, all its static methods code will be set to the instrumentation entry point.
178 // For more details, see ClassLinker::FixupStaticTrampolines.
179 if (is_class_initialized || !method->IsStatic() || method->IsConstructor()) {
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000180 if (NeedDebugVersionFor(method)) {
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800181 // Oat code should not be used. Don't install instrumentation stub and
182 // use interpreter for instrumentation.
183 new_quick_code = GetQuickToInterpreterBridge();
184 } else if (entry_exit_stubs_installed_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800185 new_quick_code = GetQuickInstrumentationEntryPoint();
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000186 } else {
187 new_quick_code = class_linker->GetQuickOatCodeFor(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100188 }
189 } else {
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700190 new_quick_code = GetQuickResolutionStub();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100191 }
192 }
193 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800194 UpdateEntrypoints(method, new_quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100195}
196
Ian Rogers62d6c772013-02-27 08:32:07 -0800197// Places the instrumentation exit pc as the return PC for every quick frame. This also allows
198// deoptimization of quick frames to interpreter frames.
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100199// Since we may already have done this previously, we need to push new instrumentation frame before
200// existing instrumentation frames.
Ian Rogers62d6c772013-02-27 08:32:07 -0800201static void InstrumentationInstallStack(Thread* thread, void* arg)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700202 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200203 struct InstallStackVisitor FINAL : public StackVisitor {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800204 InstallStackVisitor(Thread* thread_in, Context* context, uintptr_t instrumentation_exit_pc)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100205 : StackVisitor(thread_in, context, kInstrumentationStackWalk),
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800206 instrumentation_stack_(thread_in->GetInstrumentationStack()),
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100207 instrumentation_exit_pc_(instrumentation_exit_pc),
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100208 reached_existing_instrumentation_frames_(false), instrumentation_stack_depth_(0),
209 last_return_pc_(0) {
210 }
jeffhao725a9572012-11-13 18:20:12 -0800211
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700212 bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700213 ArtMethod* m = GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700214 if (m == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800215 if (kVerboseInstrumentation) {
216 LOG(INFO) << " Skipping upcall. Frame " << GetFrameId();
217 }
218 last_return_pc_ = 0;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700219 return true; // Ignore upcalls.
Ian Rogers306057f2012-11-26 12:45:53 -0800220 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700221 if (GetCurrentQuickFrame() == nullptr) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800222 bool interpreter_frame = true;
Sebastien Hertz320deb22014-06-11 19:45:05 +0200223 InstrumentationStackFrame instrumentation_frame(GetThisObject(), m, 0, GetFrameId(),
224 interpreter_frame);
Jeff Haoa15a81b2014-05-27 18:25:47 -0700225 if (kVerboseInstrumentation) {
226 LOG(INFO) << "Pushing shadow frame " << instrumentation_frame.Dump();
227 }
228 shadow_stack_.push_back(instrumentation_frame);
229 return true; // Continue.
230 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800231 uintptr_t return_pc = GetReturnPc();
Sebastien Hertz320deb22014-06-11 19:45:05 +0200232 if (kVerboseInstrumentation) {
233 LOG(INFO) << " Installing exit stub in " << DescribeLocation();
234 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100235 if (return_pc == instrumentation_exit_pc_) {
Mingyao Yang047abb22017-08-23 15:26:57 -0700236 CHECK_LT(instrumentation_stack_depth_, instrumentation_stack_->size());
237
238 if (m->IsRuntimeMethod()) {
239 const InstrumentationStackFrame& frame =
240 instrumentation_stack_->at(instrumentation_stack_depth_);
241 if (frame.interpreter_entry_) {
242 // This instrumentation frame is for an interpreter bridge and is
243 // pushed when executing the instrumented interpreter bridge. So method
244 // enter event must have been reported. However we need to push a DEX pc
245 // into the dex_pcs_ list to match size of instrumentation stack.
246 uint32_t dex_pc = DexFile::kDexNoIndex;
247 dex_pcs_.push_back(dex_pc);
248 last_return_pc_ = frame.return_pc_;
249 ++instrumentation_stack_depth_;
250 return true;
251 }
252 }
253
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100254 // We've reached a frame which has already been installed with instrumentation exit stub.
255 // We should have already installed instrumentation on previous frames.
256 reached_existing_instrumentation_frames_ = true;
257
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200258 const InstrumentationStackFrame& frame =
259 instrumentation_stack_->at(instrumentation_stack_depth_);
David Sehr709b0702016-10-13 09:12:37 -0700260 CHECK_EQ(m, frame.method_) << "Expected " << ArtMethod::PrettyMethod(m)
261 << ", Found " << ArtMethod::PrettyMethod(frame.method_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100262 return_pc = frame.return_pc_;
263 if (kVerboseInstrumentation) {
264 LOG(INFO) << "Ignoring already instrumented " << frame.Dump();
265 }
266 } else {
267 CHECK_NE(return_pc, 0U);
268 CHECK(!reached_existing_instrumentation_frames_);
Mingyao Yang047abb22017-08-23 15:26:57 -0700269 InstrumentationStackFrame instrumentation_frame(
270 m->IsRuntimeMethod() ? nullptr : GetThisObject(),
271 m,
272 return_pc,
273 GetFrameId(), // A runtime method still gets a frame id.
274 false);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100275 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 }
Mingyao Yang047abb22017-08-23 15:26:57 -0700291 uint32_t dex_pc = DexFile::kDexNoIndex;
292 if (last_return_pc_ != 0 &&
293 GetCurrentOatQuickMethodHeader() != nullptr) {
294 dex_pc = GetCurrentOatQuickMethodHeader()->ToDexPc(m, last_return_pc_);
295 }
296 dex_pcs_.push_back(dex_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800297 last_return_pc_ = return_pc;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100298 ++instrumentation_stack_depth_;
Ian Rogers306057f2012-11-26 12:45:53 -0800299 return true; // Continue.
300 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800301 std::deque<InstrumentationStackFrame>* const instrumentation_stack_;
Jeff Haoa15a81b2014-05-27 18:25:47 -0700302 std::vector<InstrumentationStackFrame> shadow_stack_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800303 std::vector<uint32_t> dex_pcs_;
Ian Rogers306057f2012-11-26 12:45:53 -0800304 const uintptr_t instrumentation_exit_pc_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100305 bool reached_existing_instrumentation_frames_;
306 size_t instrumentation_stack_depth_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800307 uintptr_t last_return_pc_;
Ian Rogers306057f2012-11-26 12:45:53 -0800308 };
Ian Rogers62d6c772013-02-27 08:32:07 -0800309 if (kVerboseInstrumentation) {
310 std::string thread_name;
311 thread->GetThreadName(thread_name);
312 LOG(INFO) << "Installing exit stubs in " << thread_name;
Ian Rogers306057f2012-11-26 12:45:53 -0800313 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100314
315 Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg);
Ian Rogers700a4022014-05-19 16:49:03 -0700316 std::unique_ptr<Context> context(Context::Create());
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700317 uintptr_t instrumentation_exit_pc = reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc());
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100318 InstallStackVisitor visitor(thread, context.get(), instrumentation_exit_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800319 visitor.WalkStack(true);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100320 CHECK_EQ(visitor.dex_pcs_.size(), thread->GetInstrumentationStack()->size());
Ian Rogers62d6c772013-02-27 08:32:07 -0800321
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100322 if (instrumentation->ShouldNotifyMethodEnterExitEvents()) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100323 // Create method enter events for all methods currently on the thread's stack. We only do this
324 // if no debugger is attached to prevent from posting events twice.
Jeff Haoa15a81b2014-05-27 18:25:47 -0700325 auto ssi = visitor.shadow_stack_.rbegin();
326 for (auto isi = thread->GetInstrumentationStack()->rbegin(),
327 end = thread->GetInstrumentationStack()->rend(); isi != end; ++isi) {
328 while (ssi != visitor.shadow_stack_.rend() && (*ssi).frame_id_ < (*isi).frame_id_) {
329 instrumentation->MethodEnterEvent(thread, (*ssi).this_object_, (*ssi).method_, 0);
330 ++ssi;
331 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100332 uint32_t dex_pc = visitor.dex_pcs_.back();
333 visitor.dex_pcs_.pop_back();
Sebastien Hertz320deb22014-06-11 19:45:05 +0200334 if (!isi->interpreter_entry_) {
335 instrumentation->MethodEnterEvent(thread, (*isi).this_object_, (*isi).method_, dex_pc);
336 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100337 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800338 }
339 thread->VerifyStack();
Ian Rogers306057f2012-11-26 12:45:53 -0800340}
341
Mingyao Yang99170c62015-07-06 11:10:37 -0700342void Instrumentation::InstrumentThreadStack(Thread* thread) {
343 instrumentation_stubs_installed_ = true;
344 InstrumentationInstallStack(thread, this);
345}
346
Ian Rogers62d6c772013-02-27 08:32:07 -0800347// Removes the instrumentation exit pc as the return PC for every quick frame.
348static void InstrumentationRestoreStack(Thread* thread, void* arg)
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +0000349 REQUIRES(Locks::mutator_lock_) {
350 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
351
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200352 struct RestoreStackVisitor FINAL : public StackVisitor {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800353 RestoreStackVisitor(Thread* thread_in, uintptr_t instrumentation_exit_pc,
Ian Rogers62d6c772013-02-27 08:32:07 -0800354 Instrumentation* instrumentation)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100355 : StackVisitor(thread_in, nullptr, kInstrumentationStackWalk),
356 thread_(thread_in),
Ian Rogers62d6c772013-02-27 08:32:07 -0800357 instrumentation_exit_pc_(instrumentation_exit_pc),
358 instrumentation_(instrumentation),
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800359 instrumentation_stack_(thread_in->GetInstrumentationStack()),
Ian Rogers62d6c772013-02-27 08:32:07 -0800360 frames_removed_(0) {}
Ian Rogers306057f2012-11-26 12:45:53 -0800361
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700362 bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800363 if (instrumentation_stack_->size() == 0) {
jeffhao725a9572012-11-13 18:20:12 -0800364 return false; // Stop.
365 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700366 ArtMethod* m = GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700367 if (GetCurrentQuickFrame() == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800368 if (kVerboseInstrumentation) {
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200369 LOG(INFO) << " Ignoring a shadow frame. Frame " << GetFrameId()
David Sehr709b0702016-10-13 09:12:37 -0700370 << " Method=" << ArtMethod::PrettyMethod(m);
Ian Rogers62d6c772013-02-27 08:32:07 -0800371 }
372 return true; // Ignore shadow frames.
373 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700374 if (m == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800375 if (kVerboseInstrumentation) {
376 LOG(INFO) << " Skipping upcall. Frame " << GetFrameId();
377 }
Ian Rogers306057f2012-11-26 12:45:53 -0800378 return true; // Ignore upcalls.
379 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800380 bool removed_stub = false;
381 // TODO: make this search more efficient?
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100382 const size_t frameId = GetFrameId();
383 for (const InstrumentationStackFrame& instrumentation_frame : *instrumentation_stack_) {
384 if (instrumentation_frame.frame_id_ == frameId) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800385 if (kVerboseInstrumentation) {
386 LOG(INFO) << " Removing exit stub in " << DescribeLocation();
387 }
Jeff Hao9a916d32013-06-27 18:45:37 -0700388 if (instrumentation_frame.interpreter_entry_) {
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700389 CHECK(m == Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs));
Jeff Hao9a916d32013-06-27 18:45:37 -0700390 } else {
David Sehr709b0702016-10-13 09:12:37 -0700391 CHECK(m == instrumentation_frame.method_) << ArtMethod::PrettyMethod(m);
Jeff Hao9a916d32013-06-27 18:45:37 -0700392 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800393 SetReturnPc(instrumentation_frame.return_pc_);
Mingyao Yang047abb22017-08-23 15:26:57 -0700394 if (instrumentation_->ShouldNotifyMethodEnterExitEvents() &&
395 !m->IsRuntimeMethod()) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100396 // Create the method exit events. As the methods didn't really exit the result is 0.
397 // We only do this if no debugger is attached to prevent from posting events twice.
398 instrumentation_->MethodExitEvent(thread_, instrumentation_frame.this_object_, m,
399 GetDexPc(), JValue());
400 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800401 frames_removed_++;
402 removed_stub = true;
403 break;
404 }
405 }
406 if (!removed_stub) {
407 if (kVerboseInstrumentation) {
408 LOG(INFO) << " No exit stub in " << DescribeLocation();
Ian Rogers306057f2012-11-26 12:45:53 -0800409 }
jeffhao725a9572012-11-13 18:20:12 -0800410 }
411 return true; // Continue.
412 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800413 Thread* const thread_;
Ian Rogers306057f2012-11-26 12:45:53 -0800414 const uintptr_t instrumentation_exit_pc_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800415 Instrumentation* const instrumentation_;
416 std::deque<instrumentation::InstrumentationStackFrame>* const instrumentation_stack_;
417 size_t frames_removed_;
jeffhao725a9572012-11-13 18:20:12 -0800418 };
Ian Rogers62d6c772013-02-27 08:32:07 -0800419 if (kVerboseInstrumentation) {
420 std::string thread_name;
421 thread->GetThreadName(thread_name);
422 LOG(INFO) << "Removing exit stubs in " << thread_name;
423 }
424 std::deque<instrumentation::InstrumentationStackFrame>* stack = thread->GetInstrumentationStack();
425 if (stack->size() > 0) {
426 Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700427 uintptr_t instrumentation_exit_pc =
428 reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc());
Ian Rogers62d6c772013-02-27 08:32:07 -0800429 RestoreStackVisitor visitor(thread, instrumentation_exit_pc, instrumentation);
430 visitor.WalkStack(true);
431 CHECK_EQ(visitor.frames_removed_, stack->size());
432 while (stack->size() > 0) {
433 stack->pop_front();
434 }
jeffhao725a9572012-11-13 18:20:12 -0800435 }
436}
437
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200438static bool HasEvent(Instrumentation::InstrumentationEvent expected, uint32_t events) {
439 return (events & expected) != 0;
440}
441
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000442static void PotentiallyAddListenerTo(Instrumentation::InstrumentationEvent event,
443 uint32_t events,
444 std::list<InstrumentationListener*>& list,
445 InstrumentationListener* listener,
446 bool* has_listener)
447 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_) {
448 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
449 if (!HasEvent(event, events)) {
450 return;
451 }
452 // If there is a free slot in the list, we insert the listener in that slot.
453 // Otherwise we add it to the end of the list.
454 auto it = std::find(list.begin(), list.end(), nullptr);
455 if (it != list.end()) {
456 *it = listener;
457 } else {
458 list.push_back(listener);
459 }
460 *has_listener = true;
461}
462
Ian Rogers62d6c772013-02-27 08:32:07 -0800463void Instrumentation::AddListener(InstrumentationListener* listener, uint32_t events) {
464 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000465 PotentiallyAddListenerTo(kMethodEntered,
466 events,
467 method_entry_listeners_,
468 listener,
469 &have_method_entry_listeners_);
470 PotentiallyAddListenerTo(kMethodExited,
471 events,
472 method_exit_listeners_,
473 listener,
474 &have_method_exit_listeners_);
475 PotentiallyAddListenerTo(kMethodUnwind,
476 events,
477 method_unwind_listeners_,
478 listener,
479 &have_method_unwind_listeners_);
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000480 PotentiallyAddListenerTo(kBranch,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000481 events,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000482 branch_listeners_,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000483 listener,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000484 &have_branch_listeners_);
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000485 PotentiallyAddListenerTo(kInvokeVirtualOrInterface,
486 events,
487 invoke_virtual_or_interface_listeners_,
488 listener,
489 &have_invoke_virtual_or_interface_listeners_);
490 PotentiallyAddListenerTo(kDexPcMoved,
491 events,
492 dex_pc_listeners_,
493 listener,
494 &have_dex_pc_listeners_);
495 PotentiallyAddListenerTo(kFieldRead,
496 events,
497 field_read_listeners_,
498 listener,
499 &have_field_read_listeners_);
500 PotentiallyAddListenerTo(kFieldWritten,
501 events,
502 field_write_listeners_,
503 listener,
504 &have_field_write_listeners_);
Alex Light6e1607e2017-08-23 10:06:18 -0700505 PotentiallyAddListenerTo(kExceptionThrown,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000506 events,
Alex Light6e1607e2017-08-23 10:06:18 -0700507 exception_thrown_listeners_,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000508 listener,
Alex Light6e1607e2017-08-23 10:06:18 -0700509 &have_exception_thrown_listeners_);
Alex Lighte814f9d2017-07-31 16:14:39 -0700510 PotentiallyAddListenerTo(kWatchedFramePop,
511 events,
512 watched_frame_pop_listeners_,
513 listener,
514 &have_watched_frame_pop_listeners_);
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200515 UpdateInterpreterHandlerTable();
jeffhao725a9572012-11-13 18:20:12 -0800516}
517
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000518static void PotentiallyRemoveListenerFrom(Instrumentation::InstrumentationEvent event,
519 uint32_t events,
520 std::list<InstrumentationListener*>& list,
521 InstrumentationListener* listener,
522 bool* has_listener)
523 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_) {
524 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
525 if (!HasEvent(event, events)) {
526 return;
527 }
528 auto it = std::find(list.begin(), list.end(), listener);
529 if (it != list.end()) {
530 // Just update the entry, do not remove from the list. Removing entries in the list
531 // is unsafe when mutators are iterating over it.
532 *it = nullptr;
533 }
534
535 // Check if the list contains any non-null listener, and update 'has_listener'.
536 for (InstrumentationListener* l : list) {
537 if (l != nullptr) {
538 *has_listener = true;
539 return;
540 }
541 }
542 *has_listener = false;
543}
544
Ian Rogers62d6c772013-02-27 08:32:07 -0800545void Instrumentation::RemoveListener(InstrumentationListener* listener, uint32_t events) {
546 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000547 PotentiallyRemoveListenerFrom(kMethodEntered,
548 events,
549 method_entry_listeners_,
550 listener,
551 &have_method_entry_listeners_);
552 PotentiallyRemoveListenerFrom(kMethodExited,
553 events,
554 method_exit_listeners_,
555 listener,
556 &have_method_exit_listeners_);
557 PotentiallyRemoveListenerFrom(kMethodUnwind,
558 events,
559 method_unwind_listeners_,
560 listener,
561 &have_method_unwind_listeners_);
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000562 PotentiallyRemoveListenerFrom(kBranch,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000563 events,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000564 branch_listeners_,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000565 listener,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000566 &have_branch_listeners_);
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000567 PotentiallyRemoveListenerFrom(kInvokeVirtualOrInterface,
568 events,
569 invoke_virtual_or_interface_listeners_,
570 listener,
571 &have_invoke_virtual_or_interface_listeners_);
572 PotentiallyRemoveListenerFrom(kDexPcMoved,
573 events,
574 dex_pc_listeners_,
575 listener,
576 &have_dex_pc_listeners_);
577 PotentiallyRemoveListenerFrom(kFieldRead,
578 events,
579 field_read_listeners_,
580 listener,
581 &have_field_read_listeners_);
582 PotentiallyRemoveListenerFrom(kFieldWritten,
583 events,
584 field_write_listeners_,
585 listener,
586 &have_field_write_listeners_);
Alex Light6e1607e2017-08-23 10:06:18 -0700587 PotentiallyRemoveListenerFrom(kExceptionThrown,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000588 events,
Alex Light6e1607e2017-08-23 10:06:18 -0700589 exception_thrown_listeners_,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000590 listener,
Alex Light6e1607e2017-08-23 10:06:18 -0700591 &have_exception_thrown_listeners_);
Alex Lighte814f9d2017-07-31 16:14:39 -0700592 PotentiallyRemoveListenerFrom(kWatchedFramePop,
593 events,
594 watched_frame_pop_listeners_,
595 listener,
596 &have_watched_frame_pop_listeners_);
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200597 UpdateInterpreterHandlerTable();
jeffhao725a9572012-11-13 18:20:12 -0800598}
599
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200600Instrumentation::InstrumentationLevel Instrumentation::GetCurrentInstrumentationLevel() const {
Alex Light4ba388a2017-01-27 10:26:49 -0800601 if (interpreter_stubs_installed_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200602 return InstrumentationLevel::kInstrumentWithInterpreter;
Ian Rogers62d6c772013-02-27 08:32:07 -0800603 } else if (entry_exit_stubs_installed_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200604 return InstrumentationLevel::kInstrumentWithInstrumentationStubs;
Ian Rogers62d6c772013-02-27 08:32:07 -0800605 } else {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200606 return InstrumentationLevel::kInstrumentNothing;
Ian Rogers62d6c772013-02-27 08:32:07 -0800607 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200608}
609
Alex Lightdba61482016-12-21 08:20:29 -0800610bool Instrumentation::RequiresInstrumentationInstallation(InstrumentationLevel new_level) const {
Alex Light4ba388a2017-01-27 10:26:49 -0800611 // We need to reinstall instrumentation if we go to a different level.
612 return GetCurrentInstrumentationLevel() != new_level;
Alex Lightdba61482016-12-21 08:20:29 -0800613}
614
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200615void Instrumentation::ConfigureStubs(const char* key, InstrumentationLevel desired_level) {
616 // Store the instrumentation level for this key or remove it.
617 if (desired_level == InstrumentationLevel::kInstrumentNothing) {
618 // The client no longer needs instrumentation.
619 requested_instrumentation_levels_.erase(key);
620 } else {
621 // The client needs instrumentation.
622 requested_instrumentation_levels_.Overwrite(key, desired_level);
623 }
624
625 // Look for the highest required instrumentation level.
626 InstrumentationLevel requested_level = InstrumentationLevel::kInstrumentNothing;
627 for (const auto& v : requested_instrumentation_levels_) {
628 requested_level = std::max(requested_level, v.second);
629 }
630
631 interpret_only_ = (requested_level == InstrumentationLevel::kInstrumentWithInterpreter) ||
632 forced_interpret_only_;
633
Alex Lightdba61482016-12-21 08:20:29 -0800634 if (!RequiresInstrumentationInstallation(requested_level)) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800635 // We're already set.
636 return;
637 }
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100638 Thread* const self = Thread::Current();
Ian Rogers62d6c772013-02-27 08:32:07 -0800639 Runtime* runtime = Runtime::Current();
Sebastien Hertza8a697f2015-01-15 12:28:47 +0100640 Locks::mutator_lock_->AssertExclusiveHeld(self);
Ian Rogers62d6c772013-02-27 08:32:07 -0800641 Locks::thread_list_lock_->AssertNotHeld(self);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200642 if (requested_level > InstrumentationLevel::kInstrumentNothing) {
Alex Light4ba388a2017-01-27 10:26:49 -0800643 if (requested_level == InstrumentationLevel::kInstrumentWithInterpreter) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800644 interpreter_stubs_installed_ = true;
Ian Rogers62d6c772013-02-27 08:32:07 -0800645 entry_exit_stubs_installed_ = true;
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200646 } else {
647 CHECK_EQ(requested_level, InstrumentationLevel::kInstrumentWithInstrumentationStubs);
648 entry_exit_stubs_installed_ = true;
649 interpreter_stubs_installed_ = false;
Ian Rogers62d6c772013-02-27 08:32:07 -0800650 }
Mathieu Chartiere0671ce2015-07-28 17:23:28 -0700651 InstallStubsClassVisitor visitor(this);
652 runtime->GetClassLinker()->VisitClasses(&visitor);
Ian Rogers62d6c772013-02-27 08:32:07 -0800653 instrumentation_stubs_installed_ = true;
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100654 MutexLock mu(self, *Locks::thread_list_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800655 runtime->GetThreadList()->ForEach(InstrumentationInstallStack, this);
656 } else {
657 interpreter_stubs_installed_ = false;
658 entry_exit_stubs_installed_ = false;
Mathieu Chartiere0671ce2015-07-28 17:23:28 -0700659 InstallStubsClassVisitor visitor(this);
660 runtime->GetClassLinker()->VisitClasses(&visitor);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100661 // Restore stack only if there is no method currently deoptimized.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700662 bool empty;
663 {
664 ReaderMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700665 empty = IsDeoptimizedMethodsEmpty(); // Avoid lock violation.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700666 }
667 if (empty) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100668 MutexLock mu(self, *Locks::thread_list_lock_);
669 Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this);
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +0000670 // Only do this after restoring, as walking the stack when restoring will see
671 // the instrumentation exit pc.
672 instrumentation_stubs_installed_ = false;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100673 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800674 }
jeffhao725a9572012-11-13 18:20:12 -0800675}
676
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200677static void ResetQuickAllocEntryPointsForThread(Thread* thread, void* arg ATTRIBUTE_UNUSED) {
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800678 thread->ResetQuickAllocEntryPointsForThread(kUseReadBarrier && thread->GetIsGcMarking());
Ian Rogersfa824272013-11-05 16:12:57 -0800679}
680
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700681void Instrumentation::SetEntrypointsInstrumented(bool instrumented) {
682 Thread* self = Thread::Current();
Mathieu Chartier661974a2014-01-09 11:23:53 -0800683 Runtime* runtime = Runtime::Current();
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700684 Locks::mutator_lock_->AssertNotHeld(self);
685 Locks::instrument_entrypoints_lock_->AssertHeld(self);
686 if (runtime->IsStarted()) {
Mathieu Chartier4f55e222015-09-04 13:26:21 -0700687 ScopedSuspendAll ssa(__FUNCTION__);
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700688 MutexLock mu(self, *Locks::runtime_shutdown_lock_);
Mathieu Chartier661974a2014-01-09 11:23:53 -0800689 SetQuickAllocEntryPointsInstrumented(instrumented);
690 ResetQuickAllocEntryPoints();
Mathieu Chartier50e93312016-03-16 11:25:29 -0700691 alloc_entrypoints_instrumented_ = instrumented;
Mathieu Chartier4f55e222015-09-04 13:26:21 -0700692 } else {
693 MutexLock mu(self, *Locks::runtime_shutdown_lock_);
694 SetQuickAllocEntryPointsInstrumented(instrumented);
Andreas Gampe157c77e2016-10-17 17:44:41 -0700695
696 // Note: ResetQuickAllocEntryPoints only works when the runtime is started. Manually run the
697 // update for just this thread.
Andreas Gampe162ae502016-10-18 10:03:42 -0700698 // Note: self may be null. One of those paths is setting instrumentation in the Heap
699 // constructor for gcstress mode.
700 if (self != nullptr) {
701 ResetQuickAllocEntryPointsForThread(self, nullptr);
702 }
Andreas Gampe157c77e2016-10-17 17:44:41 -0700703
Mathieu Chartier50e93312016-03-16 11:25:29 -0700704 alloc_entrypoints_instrumented_ = instrumented;
Mathieu Chartier661974a2014-01-09 11:23:53 -0800705 }
706}
707
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700708void Instrumentation::InstrumentQuickAllocEntryPoints() {
709 MutexLock mu(Thread::Current(), *Locks::instrument_entrypoints_lock_);
710 InstrumentQuickAllocEntryPointsLocked();
Ian Rogersfa824272013-11-05 16:12:57 -0800711}
712
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700713void Instrumentation::UninstrumentQuickAllocEntryPoints() {
714 MutexLock mu(Thread::Current(), *Locks::instrument_entrypoints_lock_);
715 UninstrumentQuickAllocEntryPointsLocked();
716}
717
718void Instrumentation::InstrumentQuickAllocEntryPointsLocked() {
719 Locks::instrument_entrypoints_lock_->AssertHeld(Thread::Current());
720 if (quick_alloc_entry_points_instrumentation_counter_ == 0) {
721 SetEntrypointsInstrumented(true);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800722 }
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700723 ++quick_alloc_entry_points_instrumentation_counter_;
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700724}
725
726void Instrumentation::UninstrumentQuickAllocEntryPointsLocked() {
727 Locks::instrument_entrypoints_lock_->AssertHeld(Thread::Current());
728 CHECK_GT(quick_alloc_entry_points_instrumentation_counter_, 0U);
729 --quick_alloc_entry_points_instrumentation_counter_;
730 if (quick_alloc_entry_points_instrumentation_counter_ == 0) {
731 SetEntrypointsInstrumented(false);
732 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800733}
734
735void Instrumentation::ResetQuickAllocEntryPoints() {
736 Runtime* runtime = Runtime::Current();
737 if (runtime->IsStarted()) {
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800738 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700739 runtime->GetThreadList()->ForEach(ResetQuickAllocEntryPointsForThread, nullptr);
Ian Rogersfa824272013-11-05 16:12:57 -0800740 }
741}
742
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700743void Instrumentation::UpdateMethodsCodeImpl(ArtMethod* method, const void* quick_code) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800744 const void* new_quick_code;
Ian Rogers62d6c772013-02-27 08:32:07 -0800745 if (LIKELY(!instrumentation_stubs_installed_)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800746 new_quick_code = quick_code;
Jeff Hao65d15d92013-07-16 16:39:33 -0700747 } else {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100748 if ((interpreter_stubs_installed_ || IsDeoptimized(method)) && !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800749 new_quick_code = GetQuickToInterpreterBridge();
Jeff Hao65d15d92013-07-16 16:39:33 -0700750 } else {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700751 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700752 if (class_linker->IsQuickResolutionStub(quick_code) ||
753 class_linker->IsQuickToInterpreterBridge(quick_code)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700754 new_quick_code = quick_code;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700755 } else if (entry_exit_stubs_installed_) {
756 new_quick_code = GetQuickInstrumentationEntryPoint();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700757 } else {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700758 new_quick_code = quick_code;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700759 }
Jeff Hao65d15d92013-07-16 16:39:33 -0700760 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800761 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800762 UpdateEntrypoints(method, new_quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100763}
764
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700765void Instrumentation::UpdateMethodsCode(ArtMethod* method, const void* quick_code) {
766 DCHECK(method->GetDeclaringClass()->IsResolved());
767 UpdateMethodsCodeImpl(method, quick_code);
768}
769
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000770void Instrumentation::UpdateMethodsCodeForJavaDebuggable(ArtMethod* method,
771 const void* quick_code) {
772 // When the runtime is set to Java debuggable, we may update the entry points of
773 // all methods of a class to the interpreter bridge. A method's declaring class
774 // might not be in resolved state yet in that case, so we bypass the DCHECK in
775 // UpdateMethodsCode.
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700776 UpdateMethodsCodeImpl(method, quick_code);
777}
778
Mathieu Chartiere401d142015-04-22 13:56:20 -0700779bool Instrumentation::AddDeoptimizedMethod(ArtMethod* method) {
780 if (IsDeoptimizedMethod(method)) {
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700781 // Already in the map. Return.
782 return false;
783 }
784 // Not found. Add it.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700785 deoptimized_methods_.insert(method);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700786 return true;
787}
788
Mathieu Chartiere401d142015-04-22 13:56:20 -0700789bool Instrumentation::IsDeoptimizedMethod(ArtMethod* method) {
790 return deoptimized_methods_.find(method) != deoptimized_methods_.end();
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700791}
792
Mathieu Chartiere401d142015-04-22 13:56:20 -0700793ArtMethod* Instrumentation::BeginDeoptimizedMethod() {
794 if (deoptimized_methods_.empty()) {
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700795 // Empty.
796 return nullptr;
797 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700798 return *deoptimized_methods_.begin();
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700799}
800
Mathieu Chartiere401d142015-04-22 13:56:20 -0700801bool Instrumentation::RemoveDeoptimizedMethod(ArtMethod* method) {
802 auto it = deoptimized_methods_.find(method);
803 if (it == deoptimized_methods_.end()) {
804 return false;
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700805 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700806 deoptimized_methods_.erase(it);
807 return true;
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700808}
809
810bool Instrumentation::IsDeoptimizedMethodsEmpty() const {
811 return deoptimized_methods_.empty();
812}
813
Mathieu Chartiere401d142015-04-22 13:56:20 -0700814void Instrumentation::Deoptimize(ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100815 CHECK(!method->IsNative());
816 CHECK(!method->IsProxyMethod());
Alex Light9139e002015-10-09 15:59:48 -0700817 CHECK(method->IsInvokable());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100818
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700819 Thread* self = Thread::Current();
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700820 {
821 WriterMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700822 bool has_not_been_deoptimized = AddDeoptimizedMethod(method);
David Sehr709b0702016-10-13 09:12:37 -0700823 CHECK(has_not_been_deoptimized) << "Method " << ArtMethod::PrettyMethod(method)
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200824 << " is already deoptimized";
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700825 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100826 if (!interpreter_stubs_installed_) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800827 UpdateEntrypoints(method, GetQuickInstrumentationEntryPoint());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100828
829 // Install instrumentation exit stub and instrumentation frames. We may already have installed
830 // these previously so it will only cover the newly created frames.
831 instrumentation_stubs_installed_ = true;
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700832 MutexLock mu(self, *Locks::thread_list_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100833 Runtime::Current()->GetThreadList()->ForEach(InstrumentationInstallStack, this);
834 }
835}
836
Mathieu Chartiere401d142015-04-22 13:56:20 -0700837void Instrumentation::Undeoptimize(ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100838 CHECK(!method->IsNative());
839 CHECK(!method->IsProxyMethod());
Alex Light9139e002015-10-09 15:59:48 -0700840 CHECK(method->IsInvokable());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100841
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700842 Thread* self = Thread::Current();
843 bool empty;
844 {
845 WriterMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700846 bool found_and_erased = RemoveDeoptimizedMethod(method);
David Sehr709b0702016-10-13 09:12:37 -0700847 CHECK(found_and_erased) << "Method " << ArtMethod::PrettyMethod(method)
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700848 << " is not deoptimized";
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700849 empty = IsDeoptimizedMethodsEmpty();
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700850 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100851
852 // Restore code and possibly stack only if we did not deoptimize everything.
853 if (!interpreter_stubs_installed_) {
854 // Restore its code or resolution trampoline.
855 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800856 if (method->IsStatic() && !method->IsConstructor() &&
857 !method->GetDeclaringClass()->IsInitialized()) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800858 UpdateEntrypoints(method, GetQuickResolutionStub());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100859 } else {
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000860 const void* quick_code = NeedDebugVersionFor(method)
861 ? GetQuickToInterpreterBridge()
862 : class_linker->GetQuickOatCodeFor(method);
Elliott Hughes956af0f2014-12-11 14:34:28 -0800863 UpdateEntrypoints(method, quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100864 }
865
866 // If there is no deoptimized method left, we can restore the stack of each thread.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700867 if (empty) {
868 MutexLock mu(self, *Locks::thread_list_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100869 Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this);
870 instrumentation_stubs_installed_ = false;
871 }
872 }
873}
874
Mathieu Chartiere401d142015-04-22 13:56:20 -0700875bool Instrumentation::IsDeoptimized(ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100876 DCHECK(method != nullptr);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700877 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700878 return IsDeoptimizedMethod(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100879}
880
881void Instrumentation::EnableDeoptimization() {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700882 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700883 CHECK(IsDeoptimizedMethodsEmpty());
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100884 CHECK_EQ(deoptimization_enabled_, false);
885 deoptimization_enabled_ = true;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100886}
887
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200888void Instrumentation::DisableDeoptimization(const char* key) {
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100889 CHECK_EQ(deoptimization_enabled_, true);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100890 // If we deoptimized everything, undo it.
Alex Lightdba61482016-12-21 08:20:29 -0800891 InstrumentationLevel level = GetCurrentInstrumentationLevel();
892 if (level == InstrumentationLevel::kInstrumentWithInterpreter) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200893 UndeoptimizeEverything(key);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100894 }
895 // Undeoptimized selected methods.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700896 while (true) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700897 ArtMethod* method;
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700898 {
899 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700900 if (IsDeoptimizedMethodsEmpty()) {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700901 break;
902 }
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700903 method = BeginDeoptimizedMethod();
904 CHECK(method != nullptr);
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700905 }
906 Undeoptimize(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100907 }
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100908 deoptimization_enabled_ = false;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100909}
910
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100911// Indicates if instrumentation should notify method enter/exit events to the listeners.
912bool Instrumentation::ShouldNotifyMethodEnterExitEvents() const {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200913 if (!HasMethodEntryListeners() && !HasMethodExitListeners()) {
914 return false;
915 }
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100916 return !deoptimization_enabled_ && !interpreter_stubs_installed_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100917}
918
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200919void Instrumentation::DeoptimizeEverything(const char* key) {
920 CHECK(deoptimization_enabled_);
921 ConfigureStubs(key, InstrumentationLevel::kInstrumentWithInterpreter);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100922}
923
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200924void Instrumentation::UndeoptimizeEverything(const char* key) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100925 CHECK(interpreter_stubs_installed_);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200926 CHECK(deoptimization_enabled_);
927 ConfigureStubs(key, InstrumentationLevel::kInstrumentNothing);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100928}
929
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200930void Instrumentation::EnableMethodTracing(const char* key, bool needs_interpreter) {
931 InstrumentationLevel level;
932 if (needs_interpreter) {
933 level = InstrumentationLevel::kInstrumentWithInterpreter;
934 } else {
935 level = InstrumentationLevel::kInstrumentWithInstrumentationStubs;
936 }
937 ConfigureStubs(key, level);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100938}
939
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200940void Instrumentation::DisableMethodTracing(const char* key) {
941 ConfigureStubs(key, InstrumentationLevel::kInstrumentNothing);
jeffhao725a9572012-11-13 18:20:12 -0800942}
943
Andreas Gampe542451c2016-07-26 09:02:02 -0700944const void* Instrumentation::GetQuickCodeFor(ArtMethod* method, PointerSize pointer_size) const {
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100945 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers62d6c772013-02-27 08:32:07 -0800946 if (LIKELY(!instrumentation_stubs_installed_)) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800947 const void* code = method->GetEntryPointFromQuickCompiledCodePtrSize(pointer_size);
Vladimir Marko8a630572014-04-09 18:45:35 +0100948 DCHECK(code != nullptr);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700949 if (LIKELY(!class_linker->IsQuickResolutionStub(code) &&
950 !class_linker->IsQuickToInterpreterBridge(code)) &&
951 !class_linker->IsQuickResolutionStub(code) &&
952 !class_linker->IsQuickToInterpreterBridge(code)) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800953 return code;
954 }
955 }
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100956 return class_linker->GetQuickOatCodeFor(method);
jeffhao725a9572012-11-13 18:20:12 -0800957}
958
Alex Lightd7661582017-05-01 13:48:16 -0700959void Instrumentation::MethodEnterEventImpl(Thread* thread,
960 ObjPtr<mirror::Object> this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700961 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800962 uint32_t dex_pc) const {
Mingyao Yang047abb22017-08-23 15:26:57 -0700963 DCHECK(!method->IsRuntimeMethod());
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000964 if (HasMethodEntryListeners()) {
Alex Lightd7661582017-05-01 13:48:16 -0700965 Thread* self = Thread::Current();
966 StackHandleScope<1> hs(self);
967 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000968 for (InstrumentationListener* listener : method_entry_listeners_) {
969 if (listener != nullptr) {
Alex Lightd7661582017-05-01 13:48:16 -0700970 listener->MethodEntered(thread, thiz, method, dex_pc);
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000971 }
972 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800973 }
974}
975
Alex Lightd7661582017-05-01 13:48:16 -0700976void Instrumentation::MethodExitEventImpl(Thread* thread,
977 ObjPtr<mirror::Object> this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700978 ArtMethod* method,
Alex Lightd7661582017-05-01 13:48:16 -0700979 uint32_t dex_pc,
980 const JValue& return_value) const {
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000981 if (HasMethodExitListeners()) {
Alex Lightd7661582017-05-01 13:48:16 -0700982 Thread* self = Thread::Current();
983 StackHandleScope<2> hs(self);
984 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
985 if (method->GetInterfaceMethodIfProxy(kRuntimePointerSize)
986 ->GetReturnTypePrimitive() != Primitive::kPrimNot) {
987 for (InstrumentationListener* listener : method_exit_listeners_) {
988 if (listener != nullptr) {
989 listener->MethodExited(thread, thiz, method, dex_pc, return_value);
990 }
991 }
992 } else {
993 Handle<mirror::Object> ret(hs.NewHandle(return_value.GetL()));
994 for (InstrumentationListener* listener : method_exit_listeners_) {
995 if (listener != nullptr) {
996 listener->MethodExited(thread, thiz, method, dex_pc, ret);
997 }
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000998 }
999 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001000 }
1001}
1002
Alex Lightd7661582017-05-01 13:48:16 -07001003void Instrumentation::MethodUnwindEvent(Thread* thread,
1004 mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001005 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -08001006 uint32_t dex_pc) const {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +02001007 if (HasMethodUnwindListeners()) {
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));
Mathieu Chartier02e25112013-08-14 16:14:24 -07001011 for (InstrumentationListener* listener : method_unwind_listeners_) {
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001012 if (listener != nullptr) {
Alex Lightd7661582017-05-01 13:48:16 -07001013 listener->MethodUnwind(thread, thiz, method, dex_pc);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001014 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001015 }
1016 }
1017}
1018
Alex Lightd7661582017-05-01 13:48:16 -07001019void Instrumentation::DexPcMovedEventImpl(Thread* thread,
1020 ObjPtr<mirror::Object> this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001021 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -08001022 uint32_t dex_pc) const {
Alex Lightd7661582017-05-01 13:48:16 -07001023 Thread* self = Thread::Current();
1024 StackHandleScope<1> hs(self);
1025 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001026 for (InstrumentationListener* listener : dex_pc_listeners_) {
1027 if (listener != nullptr) {
Alex Lightd7661582017-05-01 13:48:16 -07001028 listener->DexPcMoved(thread, thiz, method, dex_pc);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001029 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001030 }
1031}
1032
Nicolas Geoffray81f0f952016-01-20 16:25:19 +00001033void Instrumentation::BranchImpl(Thread* thread,
1034 ArtMethod* method,
1035 uint32_t dex_pc,
1036 int32_t offset) const {
1037 for (InstrumentationListener* listener : branch_listeners_) {
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001038 if (listener != nullptr) {
Nicolas Geoffray81f0f952016-01-20 16:25:19 +00001039 listener->Branch(thread, method, dex_pc, offset);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001040 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001041 }
1042}
1043
Nicolas Geoffray5550ca82015-08-21 18:38:30 +01001044void Instrumentation::InvokeVirtualOrInterfaceImpl(Thread* thread,
Alex Lightd7661582017-05-01 13:48:16 -07001045 ObjPtr<mirror::Object> this_object,
Nicolas Geoffray5550ca82015-08-21 18:38:30 +01001046 ArtMethod* caller,
1047 uint32_t dex_pc,
1048 ArtMethod* callee) 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 Geoffray5550ca82015-08-21 18:38:30 +01001052 for (InstrumentationListener* listener : invoke_virtual_or_interface_listeners_) {
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001053 if (listener != nullptr) {
Alex Lightd7661582017-05-01 13:48:16 -07001054 listener->InvokeVirtualOrInterface(thread, thiz, caller, dex_pc, callee);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001055 }
Nicolas Geoffray5550ca82015-08-21 18:38:30 +01001056 }
1057}
1058
Alex Lighte814f9d2017-07-31 16:14:39 -07001059void Instrumentation::WatchedFramePopImpl(Thread* thread, const ShadowFrame& frame) const {
1060 for (InstrumentationListener* listener : watched_frame_pop_listeners_) {
1061 if (listener != nullptr) {
1062 listener->WatchedFramePop(thread, frame);
1063 }
1064 }
1065}
1066
Alex Lightd7661582017-05-01 13:48:16 -07001067void Instrumentation::FieldReadEventImpl(Thread* thread,
1068 ObjPtr<mirror::Object> this_object,
1069 ArtMethod* method,
1070 uint32_t dex_pc,
Mathieu Chartierc7853442015-03-27 14:35:38 -07001071 ArtField* field) const {
Alex Lightd7661582017-05-01 13:48:16 -07001072 Thread* self = Thread::Current();
1073 StackHandleScope<1> hs(self);
1074 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001075 for (InstrumentationListener* listener : field_read_listeners_) {
1076 if (listener != nullptr) {
Alex Lightd7661582017-05-01 13:48:16 -07001077 listener->FieldRead(thread, thiz, method, dex_pc, field);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001078 }
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +02001079 }
1080}
1081
Alex Lightd7661582017-05-01 13:48:16 -07001082void Instrumentation::FieldWriteEventImpl(Thread* thread,
1083 ObjPtr<mirror::Object> this_object,
1084 ArtMethod* method,
1085 uint32_t dex_pc,
1086 ArtField* field,
1087 const JValue& field_value) const {
1088 Thread* self = Thread::Current();
1089 StackHandleScope<2> hs(self);
1090 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
1091 if (field->IsPrimitiveType()) {
1092 for (InstrumentationListener* listener : field_write_listeners_) {
1093 if (listener != nullptr) {
1094 listener->FieldWritten(thread, thiz, method, dex_pc, field, field_value);
1095 }
1096 }
1097 } else {
1098 Handle<mirror::Object> val(hs.NewHandle(field_value.GetL()));
1099 for (InstrumentationListener* listener : field_write_listeners_) {
1100 if (listener != nullptr) {
1101 listener->FieldWritten(thread, thiz, method, dex_pc, field, val);
1102 }
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001103 }
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +02001104 }
1105}
1106
Alex Light6e1607e2017-08-23 10:06:18 -07001107void Instrumentation::ExceptionThrownEvent(Thread* thread,
Sebastien Hertz947ff082013-09-17 14:10:13 +02001108 mirror::Throwable* exception_object) const {
Hiroshi Yamauchi3481f7a2017-02-10 12:07:36 -08001109 Thread* self = Thread::Current();
1110 StackHandleScope<1> hs(self);
1111 Handle<mirror::Throwable> h_exception(hs.NewHandle(exception_object));
Alex Light6e1607e2017-08-23 10:06:18 -07001112 if (HasExceptionThrownListeners()) {
Hiroshi Yamauchi3481f7a2017-02-10 12:07:36 -08001113 DCHECK_EQ(thread->GetException(), h_exception.Get());
Jeff Haoc0bd4da2013-04-11 15:52:28 -07001114 thread->ClearException();
Alex Light6e1607e2017-08-23 10:06:18 -07001115 for (InstrumentationListener* listener : exception_thrown_listeners_) {
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001116 if (listener != nullptr) {
Alex Light6e1607e2017-08-23 10:06:18 -07001117 listener->ExceptionThrown(thread, h_exception);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001118 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001119 }
Hiroshi Yamauchi3481f7a2017-02-10 12:07:36 -08001120 thread->SetException(h_exception.Get());
Ian Rogers62d6c772013-02-27 08:32:07 -08001121 }
1122}
1123
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +00001124// Computes a frame ID by ignoring inlined frames.
1125size_t Instrumentation::ComputeFrameId(Thread* self,
1126 size_t frame_depth,
1127 size_t inlined_frames_before_frame) {
1128 CHECK_GE(frame_depth, inlined_frames_before_frame);
1129 size_t no_inline_depth = frame_depth - inlined_frames_before_frame;
1130 return StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk) - no_inline_depth;
1131}
1132
Ian Rogers62d6c772013-02-27 08:32:07 -08001133static void CheckStackDepth(Thread* self, const InstrumentationStackFrame& instrumentation_frame,
1134 int delta)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001135 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +01001136 size_t frame_id = StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk) + delta;
Ian Rogers62d6c772013-02-27 08:32:07 -08001137 if (frame_id != instrumentation_frame.frame_id_) {
1138 LOG(ERROR) << "Expected frame_id=" << frame_id << " but found "
1139 << instrumentation_frame.frame_id_;
1140 StackVisitor::DescribeStack(self);
1141 CHECK_EQ(frame_id, instrumentation_frame.frame_id_);
1142 }
1143}
1144
1145void Instrumentation::PushInstrumentationStackFrame(Thread* self, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001146 ArtMethod* method,
Jeff Hao9a916d32013-06-27 18:45:37 -07001147 uintptr_t lr, bool interpreter_entry) {
Alex Lightb7edcda2017-04-27 13:20:31 -07001148 DCHECK(!self->IsExceptionPending());
Ian Rogers62d6c772013-02-27 08:32:07 -08001149 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
1150 if (kVerboseInstrumentation) {
David Sehr709b0702016-10-13 09:12:37 -07001151 LOG(INFO) << "Entering " << ArtMethod::PrettyMethod(method) << " from PC "
1152 << reinterpret_cast<void*>(lr);
Ian Rogers62d6c772013-02-27 08:32:07 -08001153 }
Alex Lightb7edcda2017-04-27 13:20:31 -07001154
1155 // We send the enter event before pushing the instrumentation frame to make cleanup easier. If the
1156 // event causes an exception we can simply send the unwind event and return.
1157 StackHandleScope<1> hs(self);
1158 Handle<mirror::Object> h_this(hs.NewHandle(this_object));
1159 if (!interpreter_entry) {
1160 MethodEnterEvent(self, h_this.Get(), method, 0);
1161 if (self->IsExceptionPending()) {
1162 MethodUnwindEvent(self, h_this.Get(), method, 0);
1163 return;
1164 }
1165 }
1166
1167 // We have a callee-save frame meaning this value is guaranteed to never be 0.
1168 DCHECK(!self->IsExceptionPending());
1169 size_t frame_id = StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk);
1170
1171 instrumentation::InstrumentationStackFrame instrumentation_frame(h_this.Get(), method, lr,
Jeff Hao9a916d32013-06-27 18:45:37 -07001172 frame_id, interpreter_entry);
Ian Rogers62d6c772013-02-27 08:32:07 -08001173 stack->push_front(instrumentation_frame);
Ian Rogers62d6c772013-02-27 08:32:07 -08001174}
1175
Mingyao Yang047abb22017-08-23 15:26:57 -07001176DeoptimizationMethodType Instrumentation::GetDeoptimizationMethodType(ArtMethod* method) {
1177 if (method->IsRuntimeMethod()) {
1178 // Certain methods have strict requirement on whether the dex instruction
1179 // should be re-executed upon deoptimization.
1180 if (method == Runtime::Current()->GetCalleeSaveMethod(
1181 CalleeSaveType::kSaveEverythingForClinit)) {
1182 return DeoptimizationMethodType::kKeepDexPc;
1183 }
1184 if (method == Runtime::Current()->GetCalleeSaveMethod(
1185 CalleeSaveType::kSaveEverythingForSuspendCheck)) {
1186 return DeoptimizationMethodType::kKeepDexPc;
1187 }
1188 }
1189 return DeoptimizationMethodType::kDefault;
1190}
1191
1192// Try to get the shorty of a runtime method if it's an invocation stub.
1193struct RuntimeMethodShortyVisitor : public StackVisitor {
1194 explicit RuntimeMethodShortyVisitor(Thread* thread)
1195 : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames),
1196 shorty('V') {}
1197
1198 bool VisitFrame() REQUIRES_SHARED(Locks::mutator_lock_) {
1199 ArtMethod* m = GetMethod();
1200 if (m != nullptr && !m->IsRuntimeMethod()) {
1201 // The first Java method.
1202 if (m->IsNative()) {
1203 // Use JNI method's shorty for the jni stub.
1204 shorty = m->GetShorty()[0];
1205 return false;
1206 }
1207 const DexFile::CodeItem* code_item = m->GetCodeItem();
1208 const Instruction* instr = Instruction::At(&code_item->insns_[GetDexPc()]);
1209 if (instr->IsInvoke()) {
1210 // If it's an invoke, use its shorty.
1211 uint32_t method_idx = instr->VRegB();
1212 shorty = m->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetDexFile()
1213 ->GetMethodShorty(method_idx)[0];
1214 }
1215 // Stop stack walking since we've seen a Java frame.
1216 return false;
1217 }
1218 return true;
1219 }
1220
1221 char shorty;
1222};
1223
Alex Lightb7edcda2017-04-27 13:20:31 -07001224TwoWordReturn Instrumentation::PopInstrumentationStackFrame(Thread* self,
1225 uintptr_t* return_pc,
1226 uint64_t* gpr_result,
1227 uint64_t* fpr_result) {
1228 DCHECK(gpr_result != nullptr);
1229 DCHECK(fpr_result != nullptr);
Ian Rogers62d6c772013-02-27 08:32:07 -08001230 // Do the pop.
1231 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
1232 CHECK_GT(stack->size(), 0U);
1233 InstrumentationStackFrame instrumentation_frame = stack->front();
1234 stack->pop_front();
1235
1236 // Set return PC and check the sanity of the stack.
1237 *return_pc = instrumentation_frame.return_pc_;
1238 CheckStackDepth(self, instrumentation_frame, 0);
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001239 self->VerifyStack();
Ian Rogers62d6c772013-02-27 08:32:07 -08001240
Mathieu Chartiere401d142015-04-22 13:56:20 -07001241 ArtMethod* method = instrumentation_frame.method_;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001242 uint32_t length;
Andreas Gampe542451c2016-07-26 09:02:02 -07001243 const PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Mingyao Yang047abb22017-08-23 15:26:57 -07001244 char return_shorty;
1245
1246 // Runtime method does not call into MethodExitEvent() so there should not be
1247 // suspension point below.
1248 ScopedAssertNoThreadSuspension ants(__FUNCTION__, method->IsRuntimeMethod());
1249 if (method->IsRuntimeMethod()) {
1250 if (method != Runtime::Current()->GetCalleeSaveMethod(
1251 CalleeSaveType::kSaveEverythingForClinit)) {
1252 // If the caller is at an invocation point and the runtime method is not
1253 // for clinit, we need to pass return results to the caller.
1254 // We need the correct shorty to decide whether we need to pass the return
1255 // result for deoptimization below.
1256 RuntimeMethodShortyVisitor visitor(self);
1257 visitor.WalkStack();
1258 return_shorty = visitor.shorty;
1259 } else {
1260 // Some runtime methods such as allocations, unresolved field getters, etc.
1261 // have return value. We don't need to set return_value since MethodExitEvent()
1262 // below isn't called for runtime methods. Deoptimization doesn't need the
1263 // value either since the dex instruction will be re-executed by the
1264 // interpreter, except these two cases:
1265 // (1) For an invoke, which is handled above to get the correct shorty.
1266 // (2) For MONITOR_ENTER/EXIT, which cannot be re-executed since it's not
1267 // idempotent. However there is no return value for it anyway.
1268 return_shorty = 'V';
1269 }
1270 } else {
1271 return_shorty = method->GetInterfaceMethodIfProxy(pointer_size)->GetShorty(&length)[0];
1272 }
1273
Alex Lightb7edcda2017-04-27 13:20:31 -07001274 bool is_ref = return_shorty == '[' || return_shorty == 'L';
1275 StackHandleScope<1> hs(self);
1276 MutableHandle<mirror::Object> res(hs.NewHandle<mirror::Object>(nullptr));
Ian Rogers62d6c772013-02-27 08:32:07 -08001277 JValue return_value;
1278 if (return_shorty == 'V') {
1279 return_value.SetJ(0);
1280 } else if (return_shorty == 'F' || return_shorty == 'D') {
Alex Lightb7edcda2017-04-27 13:20:31 -07001281 return_value.SetJ(*fpr_result);
Ian Rogers62d6c772013-02-27 08:32:07 -08001282 } else {
Alex Lightb7edcda2017-04-27 13:20:31 -07001283 return_value.SetJ(*gpr_result);
1284 }
1285 if (is_ref) {
1286 // Take a handle to the return value so we won't lose it if we suspend.
1287 res.Assign(return_value.GetL());
Ian Rogers62d6c772013-02-27 08:32:07 -08001288 }
1289 // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to
1290 // return_pc.
1291 uint32_t dex_pc = DexFile::kDexNoIndex;
1292 mirror::Object* this_object = instrumentation_frame.this_object_;
Mingyao Yang047abb22017-08-23 15:26:57 -07001293 if (!method->IsRuntimeMethod() && !instrumentation_frame.interpreter_entry_) {
Sebastien Hertz320deb22014-06-11 19:45:05 +02001294 MethodExitEvent(self, this_object, instrumentation_frame.method_, dex_pc, return_value);
1295 }
jeffhao725a9572012-11-13 18:20:12 -08001296
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01001297 // Deoptimize if the caller needs to continue execution in the interpreter. Do nothing if we get
1298 // back to an upcall.
1299 NthCallerVisitor visitor(self, 1, true);
1300 visitor.WalkStack(true);
Sebastien Hertz270a0e12015-01-16 19:49:09 +01001301 bool deoptimize = (visitor.caller != nullptr) &&
Daniel Mihalyieb076692014-08-22 17:33:31 +02001302 (interpreter_stubs_installed_ || IsDeoptimized(visitor.caller) ||
1303 Dbg::IsForcedInterpreterNeededForUpcall(self, visitor.caller));
Alex Lightb7edcda2017-04-27 13:20:31 -07001304 if (is_ref) {
1305 // Restore the return value if it's a reference since it might have moved.
1306 *reinterpret_cast<mirror::Object**>(gpr_result) = res.Get();
1307 }
Nicolas Geoffraya0619e22016-12-20 13:57:43 +00001308 if (deoptimize && Runtime::Current()->IsAsyncDeoptimizeable(*return_pc)) {
Ian Rogers62d6c772013-02-27 08:32:07 -08001309 if (kVerboseInstrumentation) {
Andreas Gampe46ee31b2016-12-14 10:11:49 -08001310 LOG(INFO) << "Deoptimizing "
1311 << visitor.caller->PrettyMethod()
1312 << " by returning from "
1313 << method->PrettyMethod()
1314 << " with result "
1315 << std::hex << return_value.GetJ() << std::dec
1316 << " in "
1317 << *self;
Ian Rogers62d6c772013-02-27 08:32:07 -08001318 }
Mingyao Yang047abb22017-08-23 15:26:57 -07001319 DeoptimizationMethodType deopt_method_type = GetDeoptimizationMethodType(method);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01001320 self->PushDeoptimizationContext(return_value,
Mingyao Yang047abb22017-08-23 15:26:57 -07001321 return_shorty == 'L' || return_shorty == '[',
1322 nullptr /* no pending exception */,
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01001323 false /* from_code */,
Mingyao Yang047abb22017-08-23 15:26:57 -07001324 deopt_method_type);
Andreas Gamped58342c2014-06-05 14:18:08 -07001325 return GetTwoWordSuccessValue(*return_pc,
1326 reinterpret_cast<uintptr_t>(GetQuickDeoptimizationEntryPoint()));
Ian Rogers62d6c772013-02-27 08:32:07 -08001327 } else {
Nicolas Geoffraya0619e22016-12-20 13:57:43 +00001328 if (deoptimize && !Runtime::Current()->IsAsyncDeoptimizeable(*return_pc)) {
1329 LOG(WARNING) << "Got a deoptimization request on un-deoptimizable " << method->PrettyMethod()
1330 << " at PC " << reinterpret_cast<void*>(*return_pc);
1331 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001332 if (kVerboseInstrumentation) {
David Sehr709b0702016-10-13 09:12:37 -07001333 LOG(INFO) << "Returning from " << method->PrettyMethod()
Brian Carlstrom2d888622013-07-18 17:02:00 -07001334 << " to PC " << reinterpret_cast<void*>(*return_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -08001335 }
Andreas Gamped58342c2014-06-05 14:18:08 -07001336 return GetTwoWordSuccessValue(0, *return_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -08001337 }
jeffhao725a9572012-11-13 18:20:12 -08001338}
1339
Mingyao Yangf711f2c2016-05-23 12:29:39 -07001340uintptr_t Instrumentation::PopMethodForUnwind(Thread* self, bool is_deoptimization) const {
Ian Rogers62d6c772013-02-27 08:32:07 -08001341 // Do the pop.
1342 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
1343 CHECK_GT(stack->size(), 0U);
Alex Lightb7edcda2017-04-27 13:20:31 -07001344 size_t idx = stack->size();
Ian Rogers62d6c772013-02-27 08:32:07 -08001345 InstrumentationStackFrame instrumentation_frame = stack->front();
Ian Rogers62d6c772013-02-27 08:32:07 -08001346
Mathieu Chartiere401d142015-04-22 13:56:20 -07001347 ArtMethod* method = instrumentation_frame.method_;
Ian Rogers62d6c772013-02-27 08:32:07 -08001348 if (is_deoptimization) {
1349 if (kVerboseInstrumentation) {
David Sehr709b0702016-10-13 09:12:37 -07001350 LOG(INFO) << "Popping for deoptimization " << ArtMethod::PrettyMethod(method);
Ian Rogers62d6c772013-02-27 08:32:07 -08001351 }
1352 } else {
1353 if (kVerboseInstrumentation) {
David Sehr709b0702016-10-13 09:12:37 -07001354 LOG(INFO) << "Popping for unwind " << ArtMethod::PrettyMethod(method);
Ian Rogers62d6c772013-02-27 08:32:07 -08001355 }
1356
1357 // Notify listeners of method unwind.
1358 // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to
1359 // return_pc.
1360 uint32_t dex_pc = DexFile::kDexNoIndex;
Mingyao Yang047abb22017-08-23 15:26:57 -07001361 if (!method->IsRuntimeMethod()) {
1362 MethodUnwindEvent(self, instrumentation_frame.this_object_, method, dex_pc);
1363 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001364 }
Alex Lightb7edcda2017-04-27 13:20:31 -07001365 // TODO: bring back CheckStackDepth(self, instrumentation_frame, 2);
1366 CHECK_EQ(stack->size(), idx);
1367 DCHECK(instrumentation_frame.method_ == stack->front().method_);
1368 stack->pop_front();
Mingyao Yangf711f2c2016-05-23 12:29:39 -07001369 return instrumentation_frame.return_pc_;
Ian Rogers62d6c772013-02-27 08:32:07 -08001370}
1371
1372std::string InstrumentationStackFrame::Dump() const {
1373 std::ostringstream os;
David Sehr709b0702016-10-13 09:12:37 -07001374 os << "Frame " << frame_id_ << " " << ArtMethod::PrettyMethod(method_) << ":"
Ian Rogers62d6c772013-02-27 08:32:07 -08001375 << reinterpret_cast<void*>(return_pc_) << " this=" << reinterpret_cast<void*>(this_object_);
1376 return os.str();
1377}
1378
1379} // namespace instrumentation
jeffhao725a9572012-11-13 18:20:12 -08001380} // namespace art