blob: 2c82cb1acd0f93bfff29ddce3765d96fd726139e [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"
Andreas Gampee2abbc62017-09-15 11:59:26 -070029#include "dex_file_types.h"
Mingyao Yang2ee17902017-08-30 11:37:08 -070030#include "dex_instruction-inl.h"
Mathieu Chartierd8891782014-03-02 13:28:37 -080031#include "entrypoints/quick/quick_alloc_entrypoints.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070032#include "entrypoints/quick/quick_entrypoints.h"
Ian Rogers6f3dbba2014-10-14 17:41:57 -070033#include "entrypoints/runtime_asm_entrypoints.h"
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070034#include "gc_root-inl.h"
Sebastien Hertz138dbfc2013-12-04 18:15:25 +010035#include "interpreter/interpreter.h"
Mingyao Yang2ee17902017-08-30 11:37:08 -070036#include "interpreter/interpreter_common.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080037#include "jit/jit.h"
38#include "jit/jit_code_cache.h"
Alex Lightd7661582017-05-01 13:48:16 -070039#include "jvalue-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080040#include "mirror/class-inl.h"
41#include "mirror/dex_cache.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070042#include "mirror/object-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070043#include "mirror/object_array-inl.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080044#include "nth_caller_visitor.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010045#include "oat_quick_method_header.h"
jeffhao725a9572012-11-13 18:20:12 -080046#include "thread.h"
47#include "thread_list.h"
jeffhao725a9572012-11-13 18:20:12 -080048
49namespace art {
Ian Rogers62d6c772013-02-27 08:32:07 -080050namespace instrumentation {
jeffhao725a9572012-11-13 18:20:12 -080051
Sebastien Hertz0462c4c2015-04-01 16:34:17 +020052constexpr bool kVerboseInstrumentation = false;
Sebastien Hertz5bfd5c92013-11-15 11:36:07 +010053
Alex Lightd7661582017-05-01 13:48:16 -070054void InstrumentationListener::MethodExited(Thread* thread,
55 Handle<mirror::Object> this_object,
56 ArtMethod* method,
57 uint32_t dex_pc,
58 Handle<mirror::Object> return_value) {
59 DCHECK_EQ(method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetReturnTypePrimitive(),
60 Primitive::kPrimNot);
61 JValue v;
62 v.SetL(return_value.Get());
63 MethodExited(thread, this_object, method, dex_pc, v);
64}
65
66void InstrumentationListener::FieldWritten(Thread* thread,
67 Handle<mirror::Object> this_object,
68 ArtMethod* method,
69 uint32_t dex_pc,
70 ArtField* field,
71 Handle<mirror::Object> field_value) {
72 DCHECK(!field->IsPrimitiveType());
73 JValue v;
74 v.SetL(field_value.Get());
75 FieldWritten(thread, this_object, method, dex_pc, field, v);
76}
77
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +010078// Instrumentation works on non-inlined frames by updating returned PCs
79// of compiled frames.
80static constexpr StackVisitor::StackWalkKind kInstrumentationStackWalk =
81 StackVisitor::StackWalkKind::kSkipInlinedFrames;
82
Mathieu Chartiere0671ce2015-07-28 17:23:28 -070083class InstallStubsClassVisitor : public ClassVisitor {
84 public:
85 explicit InstallStubsClassVisitor(Instrumentation* instrumentation)
86 : instrumentation_(instrumentation) {}
87
Mathieu Chartier28357fa2016-10-18 16:27:40 -070088 bool operator()(ObjPtr<mirror::Class> klass) OVERRIDE REQUIRES(Locks::mutator_lock_) {
89 instrumentation_->InstallStubsForClass(klass.Ptr());
Mathieu Chartiere0671ce2015-07-28 17:23:28 -070090 return true; // we visit all classes.
91 }
92
93 private:
94 Instrumentation* const instrumentation_;
95};
96
Ian Rogers62d6c772013-02-27 08:32:07 -080097
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -070098Instrumentation::Instrumentation()
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +000099 : instrumentation_stubs_installed_(false),
100 entry_exit_stubs_installed_(false),
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700101 interpreter_stubs_installed_(false),
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +0000102 interpret_only_(false),
103 forced_interpret_only_(false),
104 have_method_entry_listeners_(false),
105 have_method_exit_listeners_(false),
106 have_method_unwind_listeners_(false),
107 have_dex_pc_listeners_(false),
108 have_field_read_listeners_(false),
109 have_field_write_listeners_(false),
Alex Light6e1607e2017-08-23 10:06:18 -0700110 have_exception_thrown_listeners_(false),
Alex Lighte814f9d2017-07-31 16:14:39 -0700111 have_watched_frame_pop_listeners_(false),
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000112 have_branch_listeners_(false),
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +0000113 have_invoke_virtual_or_interface_listeners_(false),
Alex Light9fb1ab12017-09-05 09:32:49 -0700114 have_exception_handled_listeners_(false),
Mathieu Chartierb8aa1e42016-04-05 14:36:57 -0700115 deoptimized_methods_lock_("deoptimized methods lock", kDeoptimizedMethodsLock),
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700116 deoptimization_enabled_(false),
117 interpreter_handler_table_(kMainHandlerTable),
Mathieu Chartier50e93312016-03-16 11:25:29 -0700118 quick_alloc_entry_points_instrumentation_counter_(0),
119 alloc_entrypoints_instrumented_(false) {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700120}
121
Sebastien Hertza10aa372015-01-21 17:30:58 +0100122void Instrumentation::InstallStubsForClass(mirror::Class* klass) {
Vladimir Marko72ab6842017-01-20 19:32:50 +0000123 if (!klass->IsResolved()) {
Sebastien Hertza8a697f2015-01-15 12:28:47 +0100124 // We need the class to be resolved to install/uninstall stubs. Otherwise its methods
125 // could not be initialized or linked with regards to class inheritance.
Vladimir Marko72ab6842017-01-20 19:32:50 +0000126 } else if (klass->IsErroneousResolved()) {
127 // We can't execute code in a erroneous class: do nothing.
Sebastien Hertza8a697f2015-01-15 12:28:47 +0100128 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700129 for (ArtMethod& method : klass->GetMethods(kRuntimePointerSize)) {
Alex Light51a64d52015-12-17 13:55:59 -0800130 InstallStubsForMethod(&method);
Sebastien Hertza8a697f2015-01-15 12:28:47 +0100131 }
jeffhao725a9572012-11-13 18:20:12 -0800132 }
jeffhao725a9572012-11-13 18:20:12 -0800133}
134
Mathieu Chartiere401d142015-04-22 13:56:20 -0700135static void UpdateEntrypoints(ArtMethod* method, const void* quick_code)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700136 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800137 method->SetEntryPointFromQuickCompiledCode(quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100138}
139
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000140bool Instrumentation::NeedDebugVersionFor(ArtMethod* method) const REQUIRES_SHARED(Locks::mutator_lock_) {
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800141 return Dbg::IsDebuggerActive() &&
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000142 Runtime::Current()->IsJavaDebuggable() &&
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800143 !method->IsNative() &&
144 !method->IsProxyMethod();
145}
146
Mathieu Chartiere401d142015-04-22 13:56:20 -0700147void Instrumentation::InstallStubsForMethod(ArtMethod* method) {
Alex Light9139e002015-10-09 15:59:48 -0700148 if (!method->IsInvokable() || method->IsProxyMethod()) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100149 // Do not change stubs for these methods.
150 return;
151 }
Jeff Hao56802772014-08-19 10:17:36 -0700152 // Don't stub Proxy.<init>. Note that the Proxy class itself is not a proxy class.
153 if (method->IsConstructor() &&
154 method->GetDeclaringClass()->DescriptorEquals("Ljava/lang/reflect/Proxy;")) {
Jeff Haodb8a6642014-08-14 17:18:52 -0700155 return;
156 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800157 const void* new_quick_code;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100158 bool uninstall = !entry_exit_stubs_installed_ && !interpreter_stubs_installed_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800159 Runtime* const runtime = Runtime::Current();
160 ClassLinker* const class_linker = runtime->GetClassLinker();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100161 bool is_class_initialized = method->GetDeclaringClass()->IsInitialized();
162 if (uninstall) {
163 if ((forced_interpret_only_ || IsDeoptimized(method)) && !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800164 new_quick_code = GetQuickToInterpreterBridge();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100165 } else if (is_class_initialized || !method->IsStatic() || method->IsConstructor()) {
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000166 if (NeedDebugVersionFor(method)) {
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800167 new_quick_code = GetQuickToInterpreterBridge();
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000168 } else {
169 new_quick_code = class_linker->GetQuickOatCodeFor(method);
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800170 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100171 } else {
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700172 new_quick_code = GetQuickResolutionStub();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100173 }
174 } else { // !uninstall
Sebastien Hertzbae182c2013-12-17 10:42:03 +0100175 if ((interpreter_stubs_installed_ || forced_interpret_only_ || IsDeoptimized(method)) &&
176 !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800177 new_quick_code = GetQuickToInterpreterBridge();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100178 } else {
179 // Do not overwrite resolution trampoline. When the trampoline initializes the method's
180 // class, all its static methods code will be set to the instrumentation entry point.
181 // For more details, see ClassLinker::FixupStaticTrampolines.
182 if (is_class_initialized || !method->IsStatic() || method->IsConstructor()) {
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000183 if (NeedDebugVersionFor(method)) {
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800184 // Oat code should not be used. Don't install instrumentation stub and
185 // use interpreter for instrumentation.
186 new_quick_code = GetQuickToInterpreterBridge();
187 } else if (entry_exit_stubs_installed_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800188 new_quick_code = GetQuickInstrumentationEntryPoint();
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000189 } else {
190 new_quick_code = class_linker->GetQuickOatCodeFor(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100191 }
192 } else {
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700193 new_quick_code = GetQuickResolutionStub();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100194 }
195 }
196 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800197 UpdateEntrypoints(method, new_quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100198}
199
Ian Rogers62d6c772013-02-27 08:32:07 -0800200// Places the instrumentation exit pc as the return PC for every quick frame. This also allows
201// deoptimization of quick frames to interpreter frames.
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100202// Since we may already have done this previously, we need to push new instrumentation frame before
203// existing instrumentation frames.
Ian Rogers62d6c772013-02-27 08:32:07 -0800204static void InstrumentationInstallStack(Thread* thread, void* arg)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700205 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200206 struct InstallStackVisitor FINAL : public StackVisitor {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800207 InstallStackVisitor(Thread* thread_in, Context* context, uintptr_t instrumentation_exit_pc)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100208 : StackVisitor(thread_in, context, kInstrumentationStackWalk),
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800209 instrumentation_stack_(thread_in->GetInstrumentationStack()),
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100210 instrumentation_exit_pc_(instrumentation_exit_pc),
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100211 reached_existing_instrumentation_frames_(false), instrumentation_stack_depth_(0),
212 last_return_pc_(0) {
213 }
jeffhao725a9572012-11-13 18:20:12 -0800214
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700215 bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700216 ArtMethod* m = GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700217 if (m == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800218 if (kVerboseInstrumentation) {
219 LOG(INFO) << " Skipping upcall. Frame " << GetFrameId();
220 }
221 last_return_pc_ = 0;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700222 return true; // Ignore upcalls.
Ian Rogers306057f2012-11-26 12:45:53 -0800223 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700224 if (GetCurrentQuickFrame() == nullptr) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800225 bool interpreter_frame = true;
Sebastien Hertz320deb22014-06-11 19:45:05 +0200226 InstrumentationStackFrame instrumentation_frame(GetThisObject(), m, 0, GetFrameId(),
227 interpreter_frame);
Jeff Haoa15a81b2014-05-27 18:25:47 -0700228 if (kVerboseInstrumentation) {
229 LOG(INFO) << "Pushing shadow frame " << instrumentation_frame.Dump();
230 }
231 shadow_stack_.push_back(instrumentation_frame);
232 return true; // Continue.
233 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800234 uintptr_t return_pc = GetReturnPc();
Sebastien Hertz320deb22014-06-11 19:45:05 +0200235 if (kVerboseInstrumentation) {
236 LOG(INFO) << " Installing exit stub in " << DescribeLocation();
237 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100238 if (return_pc == instrumentation_exit_pc_) {
Mingyao Yang2ee17902017-08-30 11:37:08 -0700239 CHECK_LT(instrumentation_stack_depth_, instrumentation_stack_->size());
240
241 if (m->IsRuntimeMethod()) {
242 const InstrumentationStackFrame& frame =
243 instrumentation_stack_->at(instrumentation_stack_depth_);
244 if (frame.interpreter_entry_) {
245 // This instrumentation frame is for an interpreter bridge and is
246 // pushed when executing the instrumented interpreter bridge. So method
247 // enter event must have been reported. However we need to push a DEX pc
248 // into the dex_pcs_ list to match size of instrumentation stack.
Andreas Gampee2abbc62017-09-15 11:59:26 -0700249 uint32_t dex_pc = dex::kDexNoIndex;
Mingyao Yang2ee17902017-08-30 11:37:08 -0700250 dex_pcs_.push_back(dex_pc);
251 last_return_pc_ = frame.return_pc_;
252 ++instrumentation_stack_depth_;
253 return true;
254 }
255 }
256
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100257 // 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
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200261 const InstrumentationStackFrame& frame =
262 instrumentation_stack_->at(instrumentation_stack_depth_);
David Sehr709b0702016-10-13 09:12:37 -0700263 CHECK_EQ(m, frame.method_) << "Expected " << ArtMethod::PrettyMethod(m)
264 << ", Found " << ArtMethod::PrettyMethod(frame.method_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100265 return_pc = frame.return_pc_;
266 if (kVerboseInstrumentation) {
267 LOG(INFO) << "Ignoring already instrumented " << frame.Dump();
268 }
269 } else {
270 CHECK_NE(return_pc, 0U);
271 CHECK(!reached_existing_instrumentation_frames_);
Mingyao Yang2ee17902017-08-30 11:37:08 -0700272 InstrumentationStackFrame instrumentation_frame(
273 m->IsRuntimeMethod() ? nullptr : GetThisObject(),
274 m,
275 return_pc,
276 GetFrameId(), // A runtime method still gets a frame id.
277 false);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100278 if (kVerboseInstrumentation) {
279 LOG(INFO) << "Pushing frame " << instrumentation_frame.Dump();
280 }
281
Sebastien Hertz320deb22014-06-11 19:45:05 +0200282 // Insert frame at the right position so we do not corrupt the instrumentation stack.
283 // Instrumentation stack frames are in descending frame id order.
284 auto it = instrumentation_stack_->begin();
285 for (auto end = instrumentation_stack_->end(); it != end; ++it) {
286 const InstrumentationStackFrame& current = *it;
287 if (instrumentation_frame.frame_id_ >= current.frame_id_) {
288 break;
289 }
290 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100291 instrumentation_stack_->insert(it, instrumentation_frame);
292 SetReturnPc(instrumentation_exit_pc_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800293 }
Andreas Gampee2abbc62017-09-15 11:59:26 -0700294 uint32_t dex_pc = dex::kDexNoIndex;
Mingyao Yang2ee17902017-08-30 11:37:08 -0700295 if (last_return_pc_ != 0 &&
296 GetCurrentOatQuickMethodHeader() != nullptr) {
297 dex_pc = GetCurrentOatQuickMethodHeader()->ToDexPc(m, last_return_pc_);
298 }
299 dex_pcs_.push_back(dex_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800300 last_return_pc_ = return_pc;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100301 ++instrumentation_stack_depth_;
Ian Rogers306057f2012-11-26 12:45:53 -0800302 return true; // Continue.
303 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800304 std::deque<InstrumentationStackFrame>* const instrumentation_stack_;
Jeff Haoa15a81b2014-05-27 18:25:47 -0700305 std::vector<InstrumentationStackFrame> shadow_stack_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800306 std::vector<uint32_t> dex_pcs_;
Ian Rogers306057f2012-11-26 12:45:53 -0800307 const uintptr_t instrumentation_exit_pc_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100308 bool reached_existing_instrumentation_frames_;
309 size_t instrumentation_stack_depth_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800310 uintptr_t last_return_pc_;
Ian Rogers306057f2012-11-26 12:45:53 -0800311 };
Ian Rogers62d6c772013-02-27 08:32:07 -0800312 if (kVerboseInstrumentation) {
313 std::string thread_name;
314 thread->GetThreadName(thread_name);
315 LOG(INFO) << "Installing exit stubs in " << thread_name;
Ian Rogers306057f2012-11-26 12:45:53 -0800316 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100317
318 Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg);
Ian Rogers700a4022014-05-19 16:49:03 -0700319 std::unique_ptr<Context> context(Context::Create());
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700320 uintptr_t instrumentation_exit_pc = reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc());
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100321 InstallStackVisitor visitor(thread, context.get(), instrumentation_exit_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800322 visitor.WalkStack(true);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100323 CHECK_EQ(visitor.dex_pcs_.size(), thread->GetInstrumentationStack()->size());
Ian Rogers62d6c772013-02-27 08:32:07 -0800324
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100325 if (instrumentation->ShouldNotifyMethodEnterExitEvents()) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100326 // Create method enter events for all methods currently on the thread's stack. We only do this
327 // if no debugger is attached to prevent from posting events twice.
Jeff Haoa15a81b2014-05-27 18:25:47 -0700328 auto ssi = visitor.shadow_stack_.rbegin();
329 for (auto isi = thread->GetInstrumentationStack()->rbegin(),
330 end = thread->GetInstrumentationStack()->rend(); isi != end; ++isi) {
331 while (ssi != visitor.shadow_stack_.rend() && (*ssi).frame_id_ < (*isi).frame_id_) {
332 instrumentation->MethodEnterEvent(thread, (*ssi).this_object_, (*ssi).method_, 0);
333 ++ssi;
334 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100335 uint32_t dex_pc = visitor.dex_pcs_.back();
336 visitor.dex_pcs_.pop_back();
Sebastien Hertz320deb22014-06-11 19:45:05 +0200337 if (!isi->interpreter_entry_) {
338 instrumentation->MethodEnterEvent(thread, (*isi).this_object_, (*isi).method_, dex_pc);
339 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100340 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800341 }
342 thread->VerifyStack();
Ian Rogers306057f2012-11-26 12:45:53 -0800343}
344
Mingyao Yang99170c62015-07-06 11:10:37 -0700345void Instrumentation::InstrumentThreadStack(Thread* thread) {
346 instrumentation_stubs_installed_ = true;
347 InstrumentationInstallStack(thread, this);
348}
349
Ian Rogers62d6c772013-02-27 08:32:07 -0800350// Removes the instrumentation exit pc as the return PC for every quick frame.
351static void InstrumentationRestoreStack(Thread* thread, void* arg)
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +0000352 REQUIRES(Locks::mutator_lock_) {
353 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
354
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200355 struct RestoreStackVisitor FINAL : public StackVisitor {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800356 RestoreStackVisitor(Thread* thread_in, uintptr_t instrumentation_exit_pc,
Ian Rogers62d6c772013-02-27 08:32:07 -0800357 Instrumentation* instrumentation)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100358 : StackVisitor(thread_in, nullptr, kInstrumentationStackWalk),
359 thread_(thread_in),
Ian Rogers62d6c772013-02-27 08:32:07 -0800360 instrumentation_exit_pc_(instrumentation_exit_pc),
361 instrumentation_(instrumentation),
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800362 instrumentation_stack_(thread_in->GetInstrumentationStack()),
Ian Rogers62d6c772013-02-27 08:32:07 -0800363 frames_removed_(0) {}
Ian Rogers306057f2012-11-26 12:45:53 -0800364
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700365 bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800366 if (instrumentation_stack_->size() == 0) {
jeffhao725a9572012-11-13 18:20:12 -0800367 return false; // Stop.
368 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700369 ArtMethod* m = GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700370 if (GetCurrentQuickFrame() == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800371 if (kVerboseInstrumentation) {
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200372 LOG(INFO) << " Ignoring a shadow frame. Frame " << GetFrameId()
David Sehr709b0702016-10-13 09:12:37 -0700373 << " Method=" << ArtMethod::PrettyMethod(m);
Ian Rogers62d6c772013-02-27 08:32:07 -0800374 }
375 return true; // Ignore shadow frames.
376 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700377 if (m == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800378 if (kVerboseInstrumentation) {
379 LOG(INFO) << " Skipping upcall. Frame " << GetFrameId();
380 }
Ian Rogers306057f2012-11-26 12:45:53 -0800381 return true; // Ignore upcalls.
382 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800383 bool removed_stub = false;
384 // TODO: make this search more efficient?
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100385 const size_t frameId = GetFrameId();
386 for (const InstrumentationStackFrame& instrumentation_frame : *instrumentation_stack_) {
387 if (instrumentation_frame.frame_id_ == frameId) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800388 if (kVerboseInstrumentation) {
389 LOG(INFO) << " Removing exit stub in " << DescribeLocation();
390 }
Jeff Hao9a916d32013-06-27 18:45:37 -0700391 if (instrumentation_frame.interpreter_entry_) {
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700392 CHECK(m == Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs));
Jeff Hao9a916d32013-06-27 18:45:37 -0700393 } else {
David Sehr709b0702016-10-13 09:12:37 -0700394 CHECK(m == instrumentation_frame.method_) << ArtMethod::PrettyMethod(m);
Jeff Hao9a916d32013-06-27 18:45:37 -0700395 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800396 SetReturnPc(instrumentation_frame.return_pc_);
Mingyao Yang2ee17902017-08-30 11:37:08 -0700397 if (instrumentation_->ShouldNotifyMethodEnterExitEvents() &&
398 !m->IsRuntimeMethod()) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100399 // Create the method exit events. As the methods didn't really exit the result is 0.
400 // We only do this if no debugger is attached to prevent from posting events twice.
401 instrumentation_->MethodExitEvent(thread_, instrumentation_frame.this_object_, m,
402 GetDexPc(), JValue());
403 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800404 frames_removed_++;
405 removed_stub = true;
406 break;
407 }
408 }
409 if (!removed_stub) {
410 if (kVerboseInstrumentation) {
411 LOG(INFO) << " No exit stub in " << DescribeLocation();
Ian Rogers306057f2012-11-26 12:45:53 -0800412 }
jeffhao725a9572012-11-13 18:20:12 -0800413 }
414 return true; // Continue.
415 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800416 Thread* const thread_;
Ian Rogers306057f2012-11-26 12:45:53 -0800417 const uintptr_t instrumentation_exit_pc_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800418 Instrumentation* const instrumentation_;
419 std::deque<instrumentation::InstrumentationStackFrame>* const instrumentation_stack_;
420 size_t frames_removed_;
jeffhao725a9572012-11-13 18:20:12 -0800421 };
Ian Rogers62d6c772013-02-27 08:32:07 -0800422 if (kVerboseInstrumentation) {
423 std::string thread_name;
424 thread->GetThreadName(thread_name);
425 LOG(INFO) << "Removing exit stubs in " << thread_name;
426 }
427 std::deque<instrumentation::InstrumentationStackFrame>* stack = thread->GetInstrumentationStack();
428 if (stack->size() > 0) {
429 Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700430 uintptr_t instrumentation_exit_pc =
431 reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc());
Ian Rogers62d6c772013-02-27 08:32:07 -0800432 RestoreStackVisitor visitor(thread, instrumentation_exit_pc, instrumentation);
433 visitor.WalkStack(true);
434 CHECK_EQ(visitor.frames_removed_, stack->size());
435 while (stack->size() > 0) {
436 stack->pop_front();
437 }
jeffhao725a9572012-11-13 18:20:12 -0800438 }
439}
440
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200441static bool HasEvent(Instrumentation::InstrumentationEvent expected, uint32_t events) {
442 return (events & expected) != 0;
443}
444
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000445static void PotentiallyAddListenerTo(Instrumentation::InstrumentationEvent event,
446 uint32_t events,
447 std::list<InstrumentationListener*>& list,
448 InstrumentationListener* listener,
449 bool* has_listener)
450 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_) {
451 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
452 if (!HasEvent(event, events)) {
453 return;
454 }
455 // If there is a free slot in the list, we insert the listener in that slot.
456 // Otherwise we add it to the end of the list.
457 auto it = std::find(list.begin(), list.end(), nullptr);
458 if (it != list.end()) {
459 *it = listener;
460 } else {
461 list.push_back(listener);
462 }
463 *has_listener = true;
464}
465
Ian Rogers62d6c772013-02-27 08:32:07 -0800466void Instrumentation::AddListener(InstrumentationListener* listener, uint32_t events) {
467 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000468 PotentiallyAddListenerTo(kMethodEntered,
469 events,
470 method_entry_listeners_,
471 listener,
472 &have_method_entry_listeners_);
473 PotentiallyAddListenerTo(kMethodExited,
474 events,
475 method_exit_listeners_,
476 listener,
477 &have_method_exit_listeners_);
478 PotentiallyAddListenerTo(kMethodUnwind,
479 events,
480 method_unwind_listeners_,
481 listener,
482 &have_method_unwind_listeners_);
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000483 PotentiallyAddListenerTo(kBranch,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000484 events,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000485 branch_listeners_,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000486 listener,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000487 &have_branch_listeners_);
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000488 PotentiallyAddListenerTo(kInvokeVirtualOrInterface,
489 events,
490 invoke_virtual_or_interface_listeners_,
491 listener,
492 &have_invoke_virtual_or_interface_listeners_);
493 PotentiallyAddListenerTo(kDexPcMoved,
494 events,
495 dex_pc_listeners_,
496 listener,
497 &have_dex_pc_listeners_);
498 PotentiallyAddListenerTo(kFieldRead,
499 events,
500 field_read_listeners_,
501 listener,
502 &have_field_read_listeners_);
503 PotentiallyAddListenerTo(kFieldWritten,
504 events,
505 field_write_listeners_,
506 listener,
507 &have_field_write_listeners_);
Alex Light6e1607e2017-08-23 10:06:18 -0700508 PotentiallyAddListenerTo(kExceptionThrown,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000509 events,
Alex Light6e1607e2017-08-23 10:06:18 -0700510 exception_thrown_listeners_,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000511 listener,
Alex Light6e1607e2017-08-23 10:06:18 -0700512 &have_exception_thrown_listeners_);
Alex Lighte814f9d2017-07-31 16:14:39 -0700513 PotentiallyAddListenerTo(kWatchedFramePop,
514 events,
515 watched_frame_pop_listeners_,
516 listener,
517 &have_watched_frame_pop_listeners_);
Alex Light9fb1ab12017-09-05 09:32:49 -0700518 PotentiallyAddListenerTo(kExceptionHandled,
519 events,
520 exception_handled_listeners_,
521 listener,
522 &have_exception_handled_listeners_);
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200523 UpdateInterpreterHandlerTable();
jeffhao725a9572012-11-13 18:20:12 -0800524}
525
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000526static void PotentiallyRemoveListenerFrom(Instrumentation::InstrumentationEvent event,
527 uint32_t events,
528 std::list<InstrumentationListener*>& list,
529 InstrumentationListener* listener,
530 bool* has_listener)
531 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_) {
532 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
533 if (!HasEvent(event, events)) {
534 return;
535 }
536 auto it = std::find(list.begin(), list.end(), listener);
537 if (it != list.end()) {
538 // Just update the entry, do not remove from the list. Removing entries in the list
539 // is unsafe when mutators are iterating over it.
540 *it = nullptr;
541 }
542
543 // Check if the list contains any non-null listener, and update 'has_listener'.
544 for (InstrumentationListener* l : list) {
545 if (l != nullptr) {
546 *has_listener = true;
547 return;
548 }
549 }
550 *has_listener = false;
551}
552
Ian Rogers62d6c772013-02-27 08:32:07 -0800553void Instrumentation::RemoveListener(InstrumentationListener* listener, uint32_t events) {
554 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000555 PotentiallyRemoveListenerFrom(kMethodEntered,
556 events,
557 method_entry_listeners_,
558 listener,
559 &have_method_entry_listeners_);
560 PotentiallyRemoveListenerFrom(kMethodExited,
561 events,
562 method_exit_listeners_,
563 listener,
564 &have_method_exit_listeners_);
565 PotentiallyRemoveListenerFrom(kMethodUnwind,
566 events,
567 method_unwind_listeners_,
568 listener,
569 &have_method_unwind_listeners_);
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000570 PotentiallyRemoveListenerFrom(kBranch,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000571 events,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000572 branch_listeners_,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000573 listener,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000574 &have_branch_listeners_);
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000575 PotentiallyRemoveListenerFrom(kInvokeVirtualOrInterface,
576 events,
577 invoke_virtual_or_interface_listeners_,
578 listener,
579 &have_invoke_virtual_or_interface_listeners_);
580 PotentiallyRemoveListenerFrom(kDexPcMoved,
581 events,
582 dex_pc_listeners_,
583 listener,
584 &have_dex_pc_listeners_);
585 PotentiallyRemoveListenerFrom(kFieldRead,
586 events,
587 field_read_listeners_,
588 listener,
589 &have_field_read_listeners_);
590 PotentiallyRemoveListenerFrom(kFieldWritten,
591 events,
592 field_write_listeners_,
593 listener,
594 &have_field_write_listeners_);
Alex Light6e1607e2017-08-23 10:06:18 -0700595 PotentiallyRemoveListenerFrom(kExceptionThrown,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000596 events,
Alex Light6e1607e2017-08-23 10:06:18 -0700597 exception_thrown_listeners_,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000598 listener,
Alex Light6e1607e2017-08-23 10:06:18 -0700599 &have_exception_thrown_listeners_);
Alex Lighte814f9d2017-07-31 16:14:39 -0700600 PotentiallyRemoveListenerFrom(kWatchedFramePop,
601 events,
602 watched_frame_pop_listeners_,
603 listener,
604 &have_watched_frame_pop_listeners_);
Alex Light9fb1ab12017-09-05 09:32:49 -0700605 PotentiallyRemoveListenerFrom(kExceptionHandled,
606 events,
607 exception_handled_listeners_,
608 listener,
609 &have_exception_handled_listeners_);
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200610 UpdateInterpreterHandlerTable();
jeffhao725a9572012-11-13 18:20:12 -0800611}
612
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200613Instrumentation::InstrumentationLevel Instrumentation::GetCurrentInstrumentationLevel() const {
Alex Light4ba388a2017-01-27 10:26:49 -0800614 if (interpreter_stubs_installed_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200615 return InstrumentationLevel::kInstrumentWithInterpreter;
Ian Rogers62d6c772013-02-27 08:32:07 -0800616 } else if (entry_exit_stubs_installed_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200617 return InstrumentationLevel::kInstrumentWithInstrumentationStubs;
Ian Rogers62d6c772013-02-27 08:32:07 -0800618 } else {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200619 return InstrumentationLevel::kInstrumentNothing;
Ian Rogers62d6c772013-02-27 08:32:07 -0800620 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200621}
622
Alex Lightdba61482016-12-21 08:20:29 -0800623bool Instrumentation::RequiresInstrumentationInstallation(InstrumentationLevel new_level) const {
Alex Light4ba388a2017-01-27 10:26:49 -0800624 // We need to reinstall instrumentation if we go to a different level.
625 return GetCurrentInstrumentationLevel() != new_level;
Alex Lightdba61482016-12-21 08:20:29 -0800626}
627
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200628void Instrumentation::ConfigureStubs(const char* key, InstrumentationLevel desired_level) {
629 // Store the instrumentation level for this key or remove it.
630 if (desired_level == InstrumentationLevel::kInstrumentNothing) {
631 // The client no longer needs instrumentation.
632 requested_instrumentation_levels_.erase(key);
633 } else {
634 // The client needs instrumentation.
635 requested_instrumentation_levels_.Overwrite(key, desired_level);
636 }
637
638 // Look for the highest required instrumentation level.
639 InstrumentationLevel requested_level = InstrumentationLevel::kInstrumentNothing;
640 for (const auto& v : requested_instrumentation_levels_) {
641 requested_level = std::max(requested_level, v.second);
642 }
643
644 interpret_only_ = (requested_level == InstrumentationLevel::kInstrumentWithInterpreter) ||
645 forced_interpret_only_;
646
Alex Lightdba61482016-12-21 08:20:29 -0800647 if (!RequiresInstrumentationInstallation(requested_level)) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800648 // We're already set.
649 return;
650 }
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100651 Thread* const self = Thread::Current();
Ian Rogers62d6c772013-02-27 08:32:07 -0800652 Runtime* runtime = Runtime::Current();
Sebastien Hertza8a697f2015-01-15 12:28:47 +0100653 Locks::mutator_lock_->AssertExclusiveHeld(self);
Ian Rogers62d6c772013-02-27 08:32:07 -0800654 Locks::thread_list_lock_->AssertNotHeld(self);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200655 if (requested_level > InstrumentationLevel::kInstrumentNothing) {
Alex Light4ba388a2017-01-27 10:26:49 -0800656 if (requested_level == InstrumentationLevel::kInstrumentWithInterpreter) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800657 interpreter_stubs_installed_ = true;
Ian Rogers62d6c772013-02-27 08:32:07 -0800658 entry_exit_stubs_installed_ = true;
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200659 } else {
660 CHECK_EQ(requested_level, InstrumentationLevel::kInstrumentWithInstrumentationStubs);
661 entry_exit_stubs_installed_ = true;
662 interpreter_stubs_installed_ = false;
Ian Rogers62d6c772013-02-27 08:32:07 -0800663 }
Mathieu Chartiere0671ce2015-07-28 17:23:28 -0700664 InstallStubsClassVisitor visitor(this);
665 runtime->GetClassLinker()->VisitClasses(&visitor);
Ian Rogers62d6c772013-02-27 08:32:07 -0800666 instrumentation_stubs_installed_ = true;
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100667 MutexLock mu(self, *Locks::thread_list_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800668 runtime->GetThreadList()->ForEach(InstrumentationInstallStack, this);
669 } else {
670 interpreter_stubs_installed_ = false;
671 entry_exit_stubs_installed_ = false;
Mathieu Chartiere0671ce2015-07-28 17:23:28 -0700672 InstallStubsClassVisitor visitor(this);
673 runtime->GetClassLinker()->VisitClasses(&visitor);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100674 // Restore stack only if there is no method currently deoptimized.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700675 bool empty;
676 {
677 ReaderMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700678 empty = IsDeoptimizedMethodsEmpty(); // Avoid lock violation.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700679 }
680 if (empty) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100681 MutexLock mu(self, *Locks::thread_list_lock_);
682 Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this);
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +0000683 // Only do this after restoring, as walking the stack when restoring will see
684 // the instrumentation exit pc.
685 instrumentation_stubs_installed_ = false;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100686 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800687 }
jeffhao725a9572012-11-13 18:20:12 -0800688}
689
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200690static void ResetQuickAllocEntryPointsForThread(Thread* thread, void* arg ATTRIBUTE_UNUSED) {
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800691 thread->ResetQuickAllocEntryPointsForThread(kUseReadBarrier && thread->GetIsGcMarking());
Ian Rogersfa824272013-11-05 16:12:57 -0800692}
693
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700694void Instrumentation::SetEntrypointsInstrumented(bool instrumented) {
695 Thread* self = Thread::Current();
Mathieu Chartier661974a2014-01-09 11:23:53 -0800696 Runtime* runtime = Runtime::Current();
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700697 Locks::mutator_lock_->AssertNotHeld(self);
698 Locks::instrument_entrypoints_lock_->AssertHeld(self);
699 if (runtime->IsStarted()) {
Mathieu Chartier4f55e222015-09-04 13:26:21 -0700700 ScopedSuspendAll ssa(__FUNCTION__);
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700701 MutexLock mu(self, *Locks::runtime_shutdown_lock_);
Mathieu Chartier661974a2014-01-09 11:23:53 -0800702 SetQuickAllocEntryPointsInstrumented(instrumented);
703 ResetQuickAllocEntryPoints();
Mathieu Chartier50e93312016-03-16 11:25:29 -0700704 alloc_entrypoints_instrumented_ = instrumented;
Mathieu Chartier4f55e222015-09-04 13:26:21 -0700705 } else {
706 MutexLock mu(self, *Locks::runtime_shutdown_lock_);
707 SetQuickAllocEntryPointsInstrumented(instrumented);
Andreas Gampe157c77e2016-10-17 17:44:41 -0700708
709 // Note: ResetQuickAllocEntryPoints only works when the runtime is started. Manually run the
710 // update for just this thread.
Andreas Gampe162ae502016-10-18 10:03:42 -0700711 // Note: self may be null. One of those paths is setting instrumentation in the Heap
712 // constructor for gcstress mode.
713 if (self != nullptr) {
714 ResetQuickAllocEntryPointsForThread(self, nullptr);
715 }
Andreas Gampe157c77e2016-10-17 17:44:41 -0700716
Mathieu Chartier50e93312016-03-16 11:25:29 -0700717 alloc_entrypoints_instrumented_ = instrumented;
Mathieu Chartier661974a2014-01-09 11:23:53 -0800718 }
719}
720
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700721void Instrumentation::InstrumentQuickAllocEntryPoints() {
722 MutexLock mu(Thread::Current(), *Locks::instrument_entrypoints_lock_);
723 InstrumentQuickAllocEntryPointsLocked();
Ian Rogersfa824272013-11-05 16:12:57 -0800724}
725
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700726void Instrumentation::UninstrumentQuickAllocEntryPoints() {
727 MutexLock mu(Thread::Current(), *Locks::instrument_entrypoints_lock_);
728 UninstrumentQuickAllocEntryPointsLocked();
729}
730
731void Instrumentation::InstrumentQuickAllocEntryPointsLocked() {
732 Locks::instrument_entrypoints_lock_->AssertHeld(Thread::Current());
733 if (quick_alloc_entry_points_instrumentation_counter_ == 0) {
734 SetEntrypointsInstrumented(true);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800735 }
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700736 ++quick_alloc_entry_points_instrumentation_counter_;
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700737}
738
739void Instrumentation::UninstrumentQuickAllocEntryPointsLocked() {
740 Locks::instrument_entrypoints_lock_->AssertHeld(Thread::Current());
741 CHECK_GT(quick_alloc_entry_points_instrumentation_counter_, 0U);
742 --quick_alloc_entry_points_instrumentation_counter_;
743 if (quick_alloc_entry_points_instrumentation_counter_ == 0) {
744 SetEntrypointsInstrumented(false);
745 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800746}
747
748void Instrumentation::ResetQuickAllocEntryPoints() {
749 Runtime* runtime = Runtime::Current();
750 if (runtime->IsStarted()) {
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800751 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700752 runtime->GetThreadList()->ForEach(ResetQuickAllocEntryPointsForThread, nullptr);
Ian Rogersfa824272013-11-05 16:12:57 -0800753 }
754}
755
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700756void Instrumentation::UpdateMethodsCodeImpl(ArtMethod* method, const void* quick_code) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800757 const void* new_quick_code;
Ian Rogers62d6c772013-02-27 08:32:07 -0800758 if (LIKELY(!instrumentation_stubs_installed_)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800759 new_quick_code = quick_code;
Jeff Hao65d15d92013-07-16 16:39:33 -0700760 } else {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100761 if ((interpreter_stubs_installed_ || IsDeoptimized(method)) && !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800762 new_quick_code = GetQuickToInterpreterBridge();
Jeff Hao65d15d92013-07-16 16:39:33 -0700763 } else {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700764 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700765 if (class_linker->IsQuickResolutionStub(quick_code) ||
766 class_linker->IsQuickToInterpreterBridge(quick_code)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700767 new_quick_code = quick_code;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700768 } else if (entry_exit_stubs_installed_) {
769 new_quick_code = GetQuickInstrumentationEntryPoint();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700770 } else {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700771 new_quick_code = quick_code;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700772 }
Jeff Hao65d15d92013-07-16 16:39:33 -0700773 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800774 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800775 UpdateEntrypoints(method, new_quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100776}
777
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700778void Instrumentation::UpdateMethodsCode(ArtMethod* method, const void* quick_code) {
779 DCHECK(method->GetDeclaringClass()->IsResolved());
780 UpdateMethodsCodeImpl(method, quick_code);
781}
782
Alex Light0a5ec3d2017-07-25 16:50:26 -0700783void Instrumentation::UpdateMethodsCodeToInterpreterEntryPoint(ArtMethod* method) {
784 UpdateMethodsCodeImpl(method, GetQuickToInterpreterBridge());
785}
786
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000787void Instrumentation::UpdateMethodsCodeForJavaDebuggable(ArtMethod* method,
788 const void* quick_code) {
789 // When the runtime is set to Java debuggable, we may update the entry points of
790 // all methods of a class to the interpreter bridge. A method's declaring class
791 // might not be in resolved state yet in that case, so we bypass the DCHECK in
792 // UpdateMethodsCode.
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700793 UpdateMethodsCodeImpl(method, quick_code);
794}
795
Mathieu Chartiere401d142015-04-22 13:56:20 -0700796bool Instrumentation::AddDeoptimizedMethod(ArtMethod* method) {
797 if (IsDeoptimizedMethod(method)) {
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700798 // Already in the map. Return.
799 return false;
800 }
801 // Not found. Add it.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700802 deoptimized_methods_.insert(method);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700803 return true;
804}
805
Mathieu Chartiere401d142015-04-22 13:56:20 -0700806bool Instrumentation::IsDeoptimizedMethod(ArtMethod* method) {
807 return deoptimized_methods_.find(method) != deoptimized_methods_.end();
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700808}
809
Mathieu Chartiere401d142015-04-22 13:56:20 -0700810ArtMethod* Instrumentation::BeginDeoptimizedMethod() {
811 if (deoptimized_methods_.empty()) {
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700812 // Empty.
813 return nullptr;
814 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700815 return *deoptimized_methods_.begin();
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700816}
817
Mathieu Chartiere401d142015-04-22 13:56:20 -0700818bool Instrumentation::RemoveDeoptimizedMethod(ArtMethod* method) {
819 auto it = deoptimized_methods_.find(method);
820 if (it == deoptimized_methods_.end()) {
821 return false;
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700822 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700823 deoptimized_methods_.erase(it);
824 return true;
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700825}
826
827bool Instrumentation::IsDeoptimizedMethodsEmpty() const {
828 return deoptimized_methods_.empty();
829}
830
Mathieu Chartiere401d142015-04-22 13:56:20 -0700831void Instrumentation::Deoptimize(ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100832 CHECK(!method->IsNative());
833 CHECK(!method->IsProxyMethod());
Alex Light9139e002015-10-09 15:59:48 -0700834 CHECK(method->IsInvokable());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100835
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700836 Thread* self = Thread::Current();
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700837 {
838 WriterMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700839 bool has_not_been_deoptimized = AddDeoptimizedMethod(method);
David Sehr709b0702016-10-13 09:12:37 -0700840 CHECK(has_not_been_deoptimized) << "Method " << ArtMethod::PrettyMethod(method)
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200841 << " is already deoptimized";
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700842 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100843 if (!interpreter_stubs_installed_) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800844 UpdateEntrypoints(method, GetQuickInstrumentationEntryPoint());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100845
846 // Install instrumentation exit stub and instrumentation frames. We may already have installed
847 // these previously so it will only cover the newly created frames.
848 instrumentation_stubs_installed_ = true;
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700849 MutexLock mu(self, *Locks::thread_list_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100850 Runtime::Current()->GetThreadList()->ForEach(InstrumentationInstallStack, this);
851 }
852}
853
Mathieu Chartiere401d142015-04-22 13:56:20 -0700854void Instrumentation::Undeoptimize(ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100855 CHECK(!method->IsNative());
856 CHECK(!method->IsProxyMethod());
Alex Light9139e002015-10-09 15:59:48 -0700857 CHECK(method->IsInvokable());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100858
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700859 Thread* self = Thread::Current();
860 bool empty;
861 {
862 WriterMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700863 bool found_and_erased = RemoveDeoptimizedMethod(method);
David Sehr709b0702016-10-13 09:12:37 -0700864 CHECK(found_and_erased) << "Method " << ArtMethod::PrettyMethod(method)
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700865 << " is not deoptimized";
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700866 empty = IsDeoptimizedMethodsEmpty();
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700867 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100868
869 // Restore code and possibly stack only if we did not deoptimize everything.
870 if (!interpreter_stubs_installed_) {
871 // Restore its code or resolution trampoline.
872 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800873 if (method->IsStatic() && !method->IsConstructor() &&
874 !method->GetDeclaringClass()->IsInitialized()) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800875 UpdateEntrypoints(method, GetQuickResolutionStub());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100876 } else {
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000877 const void* quick_code = NeedDebugVersionFor(method)
878 ? GetQuickToInterpreterBridge()
879 : class_linker->GetQuickOatCodeFor(method);
Elliott Hughes956af0f2014-12-11 14:34:28 -0800880 UpdateEntrypoints(method, quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100881 }
882
883 // If there is no deoptimized method left, we can restore the stack of each thread.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700884 if (empty) {
885 MutexLock mu(self, *Locks::thread_list_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100886 Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this);
887 instrumentation_stubs_installed_ = false;
888 }
889 }
890}
891
Mathieu Chartiere401d142015-04-22 13:56:20 -0700892bool Instrumentation::IsDeoptimized(ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100893 DCHECK(method != nullptr);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700894 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700895 return IsDeoptimizedMethod(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100896}
897
898void Instrumentation::EnableDeoptimization() {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700899 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700900 CHECK(IsDeoptimizedMethodsEmpty());
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100901 CHECK_EQ(deoptimization_enabled_, false);
902 deoptimization_enabled_ = true;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100903}
904
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200905void Instrumentation::DisableDeoptimization(const char* key) {
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100906 CHECK_EQ(deoptimization_enabled_, true);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100907 // If we deoptimized everything, undo it.
Alex Lightdba61482016-12-21 08:20:29 -0800908 InstrumentationLevel level = GetCurrentInstrumentationLevel();
909 if (level == InstrumentationLevel::kInstrumentWithInterpreter) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200910 UndeoptimizeEverything(key);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100911 }
912 // Undeoptimized selected methods.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700913 while (true) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700914 ArtMethod* method;
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700915 {
916 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700917 if (IsDeoptimizedMethodsEmpty()) {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700918 break;
919 }
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700920 method = BeginDeoptimizedMethod();
921 CHECK(method != nullptr);
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700922 }
923 Undeoptimize(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100924 }
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100925 deoptimization_enabled_ = false;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100926}
927
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100928// Indicates if instrumentation should notify method enter/exit events to the listeners.
929bool Instrumentation::ShouldNotifyMethodEnterExitEvents() const {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200930 if (!HasMethodEntryListeners() && !HasMethodExitListeners()) {
931 return false;
932 }
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100933 return !deoptimization_enabled_ && !interpreter_stubs_installed_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100934}
935
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200936void Instrumentation::DeoptimizeEverything(const char* key) {
937 CHECK(deoptimization_enabled_);
938 ConfigureStubs(key, InstrumentationLevel::kInstrumentWithInterpreter);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100939}
940
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200941void Instrumentation::UndeoptimizeEverything(const char* key) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100942 CHECK(interpreter_stubs_installed_);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200943 CHECK(deoptimization_enabled_);
944 ConfigureStubs(key, InstrumentationLevel::kInstrumentNothing);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100945}
946
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200947void Instrumentation::EnableMethodTracing(const char* key, bool needs_interpreter) {
948 InstrumentationLevel level;
949 if (needs_interpreter) {
950 level = InstrumentationLevel::kInstrumentWithInterpreter;
951 } else {
952 level = InstrumentationLevel::kInstrumentWithInstrumentationStubs;
953 }
954 ConfigureStubs(key, level);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100955}
956
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200957void Instrumentation::DisableMethodTracing(const char* key) {
958 ConfigureStubs(key, InstrumentationLevel::kInstrumentNothing);
jeffhao725a9572012-11-13 18:20:12 -0800959}
960
Andreas Gampe542451c2016-07-26 09:02:02 -0700961const void* Instrumentation::GetQuickCodeFor(ArtMethod* method, PointerSize pointer_size) const {
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100962 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers62d6c772013-02-27 08:32:07 -0800963 if (LIKELY(!instrumentation_stubs_installed_)) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800964 const void* code = method->GetEntryPointFromQuickCompiledCodePtrSize(pointer_size);
Vladimir Marko8a630572014-04-09 18:45:35 +0100965 DCHECK(code != nullptr);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700966 if (LIKELY(!class_linker->IsQuickResolutionStub(code) &&
967 !class_linker->IsQuickToInterpreterBridge(code)) &&
968 !class_linker->IsQuickResolutionStub(code) &&
969 !class_linker->IsQuickToInterpreterBridge(code)) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800970 return code;
971 }
972 }
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100973 return class_linker->GetQuickOatCodeFor(method);
jeffhao725a9572012-11-13 18:20:12 -0800974}
975
Alex Lightd7661582017-05-01 13:48:16 -0700976void Instrumentation::MethodEnterEventImpl(Thread* thread,
977 ObjPtr<mirror::Object> this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700978 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800979 uint32_t dex_pc) const {
Mingyao Yang2ee17902017-08-30 11:37:08 -0700980 DCHECK(!method->IsRuntimeMethod());
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000981 if (HasMethodEntryListeners()) {
Alex Lightd7661582017-05-01 13:48:16 -0700982 Thread* self = Thread::Current();
983 StackHandleScope<1> hs(self);
984 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000985 for (InstrumentationListener* listener : method_entry_listeners_) {
986 if (listener != nullptr) {
Alex Lightd7661582017-05-01 13:48:16 -0700987 listener->MethodEntered(thread, thiz, method, dex_pc);
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000988 }
989 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800990 }
991}
992
Alex Lightd7661582017-05-01 13:48:16 -0700993void Instrumentation::MethodExitEventImpl(Thread* thread,
994 ObjPtr<mirror::Object> this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700995 ArtMethod* method,
Alex Lightd7661582017-05-01 13:48:16 -0700996 uint32_t dex_pc,
997 const JValue& return_value) const {
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000998 if (HasMethodExitListeners()) {
Alex Lightd7661582017-05-01 13:48:16 -0700999 Thread* self = Thread::Current();
1000 StackHandleScope<2> hs(self);
1001 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
1002 if (method->GetInterfaceMethodIfProxy(kRuntimePointerSize)
1003 ->GetReturnTypePrimitive() != Primitive::kPrimNot) {
1004 for (InstrumentationListener* listener : method_exit_listeners_) {
1005 if (listener != nullptr) {
1006 listener->MethodExited(thread, thiz, method, dex_pc, return_value);
1007 }
1008 }
1009 } else {
1010 Handle<mirror::Object> ret(hs.NewHandle(return_value.GetL()));
1011 for (InstrumentationListener* listener : method_exit_listeners_) {
1012 if (listener != nullptr) {
1013 listener->MethodExited(thread, thiz, method, dex_pc, ret);
1014 }
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001015 }
1016 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001017 }
1018}
1019
Alex Lightd7661582017-05-01 13:48:16 -07001020void Instrumentation::MethodUnwindEvent(Thread* thread,
1021 mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001022 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -08001023 uint32_t dex_pc) const {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +02001024 if (HasMethodUnwindListeners()) {
Alex Lightd7661582017-05-01 13:48:16 -07001025 Thread* self = Thread::Current();
1026 StackHandleScope<1> hs(self);
1027 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
Mathieu Chartier02e25112013-08-14 16:14:24 -07001028 for (InstrumentationListener* listener : method_unwind_listeners_) {
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001029 if (listener != nullptr) {
Alex Lightd7661582017-05-01 13:48:16 -07001030 listener->MethodUnwind(thread, thiz, method, dex_pc);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001031 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001032 }
1033 }
1034}
1035
Alex Lightd7661582017-05-01 13:48:16 -07001036void Instrumentation::DexPcMovedEventImpl(Thread* thread,
1037 ObjPtr<mirror::Object> this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001038 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -08001039 uint32_t dex_pc) const {
Alex Lightd7661582017-05-01 13:48:16 -07001040 Thread* self = Thread::Current();
1041 StackHandleScope<1> hs(self);
1042 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001043 for (InstrumentationListener* listener : dex_pc_listeners_) {
1044 if (listener != nullptr) {
Alex Lightd7661582017-05-01 13:48:16 -07001045 listener->DexPcMoved(thread, thiz, method, dex_pc);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001046 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001047 }
1048}
1049
Nicolas Geoffray81f0f952016-01-20 16:25:19 +00001050void Instrumentation::BranchImpl(Thread* thread,
1051 ArtMethod* method,
1052 uint32_t dex_pc,
1053 int32_t offset) const {
1054 for (InstrumentationListener* listener : branch_listeners_) {
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001055 if (listener != nullptr) {
Nicolas Geoffray81f0f952016-01-20 16:25:19 +00001056 listener->Branch(thread, method, dex_pc, offset);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001057 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001058 }
1059}
1060
Nicolas Geoffray5550ca82015-08-21 18:38:30 +01001061void Instrumentation::InvokeVirtualOrInterfaceImpl(Thread* thread,
Alex Lightd7661582017-05-01 13:48:16 -07001062 ObjPtr<mirror::Object> this_object,
Nicolas Geoffray5550ca82015-08-21 18:38:30 +01001063 ArtMethod* caller,
1064 uint32_t dex_pc,
1065 ArtMethod* callee) const {
Alex Lightd7661582017-05-01 13:48:16 -07001066 Thread* self = Thread::Current();
1067 StackHandleScope<1> hs(self);
1068 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
Nicolas Geoffray5550ca82015-08-21 18:38:30 +01001069 for (InstrumentationListener* listener : invoke_virtual_or_interface_listeners_) {
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001070 if (listener != nullptr) {
Alex Lightd7661582017-05-01 13:48:16 -07001071 listener->InvokeVirtualOrInterface(thread, thiz, caller, dex_pc, callee);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001072 }
Nicolas Geoffray5550ca82015-08-21 18:38:30 +01001073 }
1074}
1075
Alex Lighte814f9d2017-07-31 16:14:39 -07001076void Instrumentation::WatchedFramePopImpl(Thread* thread, const ShadowFrame& frame) const {
1077 for (InstrumentationListener* listener : watched_frame_pop_listeners_) {
1078 if (listener != nullptr) {
1079 listener->WatchedFramePop(thread, frame);
1080 }
1081 }
1082}
1083
Alex Lightd7661582017-05-01 13:48:16 -07001084void Instrumentation::FieldReadEventImpl(Thread* thread,
1085 ObjPtr<mirror::Object> this_object,
1086 ArtMethod* method,
1087 uint32_t dex_pc,
Mathieu Chartierc7853442015-03-27 14:35:38 -07001088 ArtField* field) const {
Alex Lightd7661582017-05-01 13:48:16 -07001089 Thread* self = Thread::Current();
1090 StackHandleScope<1> hs(self);
1091 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001092 for (InstrumentationListener* listener : field_read_listeners_) {
1093 if (listener != nullptr) {
Alex Lightd7661582017-05-01 13:48:16 -07001094 listener->FieldRead(thread, thiz, method, dex_pc, field);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001095 }
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +02001096 }
1097}
1098
Alex Lightd7661582017-05-01 13:48:16 -07001099void Instrumentation::FieldWriteEventImpl(Thread* thread,
1100 ObjPtr<mirror::Object> this_object,
1101 ArtMethod* method,
1102 uint32_t dex_pc,
1103 ArtField* field,
1104 const JValue& field_value) const {
1105 Thread* self = Thread::Current();
1106 StackHandleScope<2> hs(self);
1107 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
1108 if (field->IsPrimitiveType()) {
1109 for (InstrumentationListener* listener : field_write_listeners_) {
1110 if (listener != nullptr) {
1111 listener->FieldWritten(thread, thiz, method, dex_pc, field, field_value);
1112 }
1113 }
1114 } else {
1115 Handle<mirror::Object> val(hs.NewHandle(field_value.GetL()));
1116 for (InstrumentationListener* listener : field_write_listeners_) {
1117 if (listener != nullptr) {
1118 listener->FieldWritten(thread, thiz, method, dex_pc, field, val);
1119 }
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001120 }
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +02001121 }
1122}
1123
Alex Light6e1607e2017-08-23 10:06:18 -07001124void Instrumentation::ExceptionThrownEvent(Thread* thread,
Sebastien Hertz947ff082013-09-17 14:10:13 +02001125 mirror::Throwable* exception_object) const {
Hiroshi Yamauchi3481f7a2017-02-10 12:07:36 -08001126 Thread* self = Thread::Current();
1127 StackHandleScope<1> hs(self);
1128 Handle<mirror::Throwable> h_exception(hs.NewHandle(exception_object));
Alex Light6e1607e2017-08-23 10:06:18 -07001129 if (HasExceptionThrownListeners()) {
Hiroshi Yamauchi3481f7a2017-02-10 12:07:36 -08001130 DCHECK_EQ(thread->GetException(), h_exception.Get());
Jeff Haoc0bd4da2013-04-11 15:52:28 -07001131 thread->ClearException();
Alex Light6e1607e2017-08-23 10:06:18 -07001132 for (InstrumentationListener* listener : exception_thrown_listeners_) {
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001133 if (listener != nullptr) {
Alex Light6e1607e2017-08-23 10:06:18 -07001134 listener->ExceptionThrown(thread, h_exception);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001135 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001136 }
Alex Light9fb1ab12017-09-05 09:32:49 -07001137 // See b/65049545 for discussion about this behavior.
1138 thread->AssertNoPendingException();
Hiroshi Yamauchi3481f7a2017-02-10 12:07:36 -08001139 thread->SetException(h_exception.Get());
Ian Rogers62d6c772013-02-27 08:32:07 -08001140 }
1141}
1142
Alex Light9fb1ab12017-09-05 09:32:49 -07001143void Instrumentation::ExceptionHandledEvent(Thread* thread,
1144 mirror::Throwable* exception_object) const {
1145 Thread* self = Thread::Current();
1146 StackHandleScope<1> hs(self);
1147 Handle<mirror::Throwable> h_exception(hs.NewHandle(exception_object));
1148 if (HasExceptionHandledListeners()) {
1149 // We should have cleared the exception so that callers can detect a new one.
1150 DCHECK(thread->GetException() == nullptr);
1151 for (InstrumentationListener* listener : exception_handled_listeners_) {
1152 if (listener != nullptr) {
1153 listener->ExceptionHandled(thread, h_exception);
1154 }
1155 }
1156 }
1157}
1158
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +00001159// Computes a frame ID by ignoring inlined frames.
1160size_t Instrumentation::ComputeFrameId(Thread* self,
1161 size_t frame_depth,
1162 size_t inlined_frames_before_frame) {
1163 CHECK_GE(frame_depth, inlined_frames_before_frame);
1164 size_t no_inline_depth = frame_depth - inlined_frames_before_frame;
1165 return StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk) - no_inline_depth;
1166}
1167
Ian Rogers62d6c772013-02-27 08:32:07 -08001168static void CheckStackDepth(Thread* self, const InstrumentationStackFrame& instrumentation_frame,
1169 int delta)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001170 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +01001171 size_t frame_id = StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk) + delta;
Ian Rogers62d6c772013-02-27 08:32:07 -08001172 if (frame_id != instrumentation_frame.frame_id_) {
1173 LOG(ERROR) << "Expected frame_id=" << frame_id << " but found "
1174 << instrumentation_frame.frame_id_;
1175 StackVisitor::DescribeStack(self);
1176 CHECK_EQ(frame_id, instrumentation_frame.frame_id_);
1177 }
1178}
1179
1180void Instrumentation::PushInstrumentationStackFrame(Thread* self, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001181 ArtMethod* method,
Jeff Hao9a916d32013-06-27 18:45:37 -07001182 uintptr_t lr, bool interpreter_entry) {
Alex Lightb7edcda2017-04-27 13:20:31 -07001183 DCHECK(!self->IsExceptionPending());
Ian Rogers62d6c772013-02-27 08:32:07 -08001184 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
1185 if (kVerboseInstrumentation) {
David Sehr709b0702016-10-13 09:12:37 -07001186 LOG(INFO) << "Entering " << ArtMethod::PrettyMethod(method) << " from PC "
1187 << reinterpret_cast<void*>(lr);
Ian Rogers62d6c772013-02-27 08:32:07 -08001188 }
Alex Lightb7edcda2017-04-27 13:20:31 -07001189
1190 // We send the enter event before pushing the instrumentation frame to make cleanup easier. If the
1191 // event causes an exception we can simply send the unwind event and return.
1192 StackHandleScope<1> hs(self);
1193 Handle<mirror::Object> h_this(hs.NewHandle(this_object));
1194 if (!interpreter_entry) {
1195 MethodEnterEvent(self, h_this.Get(), method, 0);
1196 if (self->IsExceptionPending()) {
1197 MethodUnwindEvent(self, h_this.Get(), method, 0);
1198 return;
1199 }
1200 }
1201
1202 // We have a callee-save frame meaning this value is guaranteed to never be 0.
1203 DCHECK(!self->IsExceptionPending());
1204 size_t frame_id = StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk);
1205
1206 instrumentation::InstrumentationStackFrame instrumentation_frame(h_this.Get(), method, lr,
Jeff Hao9a916d32013-06-27 18:45:37 -07001207 frame_id, interpreter_entry);
Ian Rogers62d6c772013-02-27 08:32:07 -08001208 stack->push_front(instrumentation_frame);
Ian Rogers62d6c772013-02-27 08:32:07 -08001209}
1210
Mingyao Yang2ee17902017-08-30 11:37:08 -07001211DeoptimizationMethodType Instrumentation::GetDeoptimizationMethodType(ArtMethod* method) {
1212 if (method->IsRuntimeMethod()) {
1213 // Certain methods have strict requirement on whether the dex instruction
1214 // should be re-executed upon deoptimization.
1215 if (method == Runtime::Current()->GetCalleeSaveMethod(
1216 CalleeSaveType::kSaveEverythingForClinit)) {
1217 return DeoptimizationMethodType::kKeepDexPc;
1218 }
1219 if (method == Runtime::Current()->GetCalleeSaveMethod(
1220 CalleeSaveType::kSaveEverythingForSuspendCheck)) {
1221 return DeoptimizationMethodType::kKeepDexPc;
1222 }
1223 }
1224 return DeoptimizationMethodType::kDefault;
1225}
1226
1227// Try to get the shorty of a runtime method if it's an invocation stub.
1228struct RuntimeMethodShortyVisitor : public StackVisitor {
1229 explicit RuntimeMethodShortyVisitor(Thread* thread)
1230 : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames),
1231 shorty('V') {}
1232
1233 bool VisitFrame() REQUIRES_SHARED(Locks::mutator_lock_) {
1234 ArtMethod* m = GetMethod();
1235 if (m != nullptr && !m->IsRuntimeMethod()) {
1236 // The first Java method.
1237 if (m->IsNative()) {
1238 // Use JNI method's shorty for the jni stub.
1239 shorty = m->GetShorty()[0];
1240 return false;
1241 }
1242 if (m->IsProxyMethod()) {
1243 // Proxy method just invokes its proxied method via
1244 // art_quick_proxy_invoke_handler.
1245 shorty = m->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty()[0];
1246 return false;
1247 }
1248 const DexFile::CodeItem* code_item = m->GetCodeItem();
1249 const Instruction* instr = Instruction::At(&code_item->insns_[GetDexPc()]);
1250 if (instr->IsInvoke()) {
1251 const DexFile* dex_file = m->GetDexFile();
1252 if (interpreter::IsStringInit(dex_file, instr->VRegB())) {
1253 // Invoking string init constructor is turned into invoking
1254 // StringFactory.newStringFromChars() which returns a string.
1255 shorty = 'L';
1256 return false;
1257 }
1258 // A regular invoke, use callee's shorty.
1259 uint32_t method_idx = instr->VRegB();
1260 shorty = dex_file->GetMethodShorty(method_idx)[0];
1261 }
1262 // Stop stack walking since we've seen a Java frame.
1263 return false;
1264 }
1265 return true;
1266 }
1267
1268 char shorty;
1269};
1270
Alex Lightb7edcda2017-04-27 13:20:31 -07001271TwoWordReturn Instrumentation::PopInstrumentationStackFrame(Thread* self,
1272 uintptr_t* return_pc,
1273 uint64_t* gpr_result,
1274 uint64_t* fpr_result) {
1275 DCHECK(gpr_result != nullptr);
1276 DCHECK(fpr_result != nullptr);
Ian Rogers62d6c772013-02-27 08:32:07 -08001277 // Do the pop.
1278 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
1279 CHECK_GT(stack->size(), 0U);
1280 InstrumentationStackFrame instrumentation_frame = stack->front();
1281 stack->pop_front();
1282
1283 // Set return PC and check the sanity of the stack.
1284 *return_pc = instrumentation_frame.return_pc_;
1285 CheckStackDepth(self, instrumentation_frame, 0);
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001286 self->VerifyStack();
Ian Rogers62d6c772013-02-27 08:32:07 -08001287
Mathieu Chartiere401d142015-04-22 13:56:20 -07001288 ArtMethod* method = instrumentation_frame.method_;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001289 uint32_t length;
Andreas Gampe542451c2016-07-26 09:02:02 -07001290 const PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Mingyao Yang2ee17902017-08-30 11:37:08 -07001291 char return_shorty;
1292
1293 // Runtime method does not call into MethodExitEvent() so there should not be
1294 // suspension point below.
1295 ScopedAssertNoThreadSuspension ants(__FUNCTION__, method->IsRuntimeMethod());
1296 if (method->IsRuntimeMethod()) {
1297 if (method != Runtime::Current()->GetCalleeSaveMethod(
1298 CalleeSaveType::kSaveEverythingForClinit)) {
1299 // If the caller is at an invocation point and the runtime method is not
1300 // for clinit, we need to pass return results to the caller.
1301 // We need the correct shorty to decide whether we need to pass the return
1302 // result for deoptimization below.
1303 RuntimeMethodShortyVisitor visitor(self);
1304 visitor.WalkStack();
1305 return_shorty = visitor.shorty;
1306 } else {
1307 // Some runtime methods such as allocations, unresolved field getters, etc.
1308 // have return value. We don't need to set return_value since MethodExitEvent()
1309 // below isn't called for runtime methods. Deoptimization doesn't need the
1310 // value either since the dex instruction will be re-executed by the
1311 // interpreter, except these two cases:
1312 // (1) For an invoke, which is handled above to get the correct shorty.
1313 // (2) For MONITOR_ENTER/EXIT, which cannot be re-executed since it's not
1314 // idempotent. However there is no return value for it anyway.
1315 return_shorty = 'V';
1316 }
1317 } else {
1318 return_shorty = method->GetInterfaceMethodIfProxy(pointer_size)->GetShorty(&length)[0];
1319 }
1320
Alex Lightb7edcda2017-04-27 13:20:31 -07001321 bool is_ref = return_shorty == '[' || return_shorty == 'L';
1322 StackHandleScope<1> hs(self);
1323 MutableHandle<mirror::Object> res(hs.NewHandle<mirror::Object>(nullptr));
Ian Rogers62d6c772013-02-27 08:32:07 -08001324 JValue return_value;
1325 if (return_shorty == 'V') {
1326 return_value.SetJ(0);
1327 } else if (return_shorty == 'F' || return_shorty == 'D') {
Alex Lightb7edcda2017-04-27 13:20:31 -07001328 return_value.SetJ(*fpr_result);
Ian Rogers62d6c772013-02-27 08:32:07 -08001329 } else {
Alex Lightb7edcda2017-04-27 13:20:31 -07001330 return_value.SetJ(*gpr_result);
1331 }
1332 if (is_ref) {
1333 // Take a handle to the return value so we won't lose it if we suspend.
1334 res.Assign(return_value.GetL());
Ian Rogers62d6c772013-02-27 08:32:07 -08001335 }
1336 // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to
1337 // return_pc.
Andreas Gampee2abbc62017-09-15 11:59:26 -07001338 uint32_t dex_pc = dex::kDexNoIndex;
Ian Rogers62d6c772013-02-27 08:32:07 -08001339 mirror::Object* this_object = instrumentation_frame.this_object_;
Mingyao Yang2ee17902017-08-30 11:37:08 -07001340 if (!method->IsRuntimeMethod() && !instrumentation_frame.interpreter_entry_) {
Sebastien Hertz320deb22014-06-11 19:45:05 +02001341 MethodExitEvent(self, this_object, instrumentation_frame.method_, dex_pc, return_value);
1342 }
jeffhao725a9572012-11-13 18:20:12 -08001343
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01001344 // Deoptimize if the caller needs to continue execution in the interpreter. Do nothing if we get
1345 // back to an upcall.
1346 NthCallerVisitor visitor(self, 1, true);
1347 visitor.WalkStack(true);
Sebastien Hertz270a0e12015-01-16 19:49:09 +01001348 bool deoptimize = (visitor.caller != nullptr) &&
Daniel Mihalyieb076692014-08-22 17:33:31 +02001349 (interpreter_stubs_installed_ || IsDeoptimized(visitor.caller) ||
1350 Dbg::IsForcedInterpreterNeededForUpcall(self, visitor.caller));
Alex Lightb7edcda2017-04-27 13:20:31 -07001351 if (is_ref) {
1352 // Restore the return value if it's a reference since it might have moved.
1353 *reinterpret_cast<mirror::Object**>(gpr_result) = res.Get();
1354 }
Nicolas Geoffraya0619e22016-12-20 13:57:43 +00001355 if (deoptimize && Runtime::Current()->IsAsyncDeoptimizeable(*return_pc)) {
Ian Rogers62d6c772013-02-27 08:32:07 -08001356 if (kVerboseInstrumentation) {
Andreas Gampe46ee31b2016-12-14 10:11:49 -08001357 LOG(INFO) << "Deoptimizing "
1358 << visitor.caller->PrettyMethod()
1359 << " by returning from "
1360 << method->PrettyMethod()
1361 << " with result "
1362 << std::hex << return_value.GetJ() << std::dec
1363 << " in "
1364 << *self;
Ian Rogers62d6c772013-02-27 08:32:07 -08001365 }
Mingyao Yang2ee17902017-08-30 11:37:08 -07001366 DeoptimizationMethodType deopt_method_type = GetDeoptimizationMethodType(method);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01001367 self->PushDeoptimizationContext(return_value,
Mingyao Yang2ee17902017-08-30 11:37:08 -07001368 return_shorty == 'L' || return_shorty == '[',
1369 nullptr /* no pending exception */,
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01001370 false /* from_code */,
Mingyao Yang2ee17902017-08-30 11:37:08 -07001371 deopt_method_type);
Andreas Gamped58342c2014-06-05 14:18:08 -07001372 return GetTwoWordSuccessValue(*return_pc,
1373 reinterpret_cast<uintptr_t>(GetQuickDeoptimizationEntryPoint()));
Ian Rogers62d6c772013-02-27 08:32:07 -08001374 } else {
Nicolas Geoffraya0619e22016-12-20 13:57:43 +00001375 if (deoptimize && !Runtime::Current()->IsAsyncDeoptimizeable(*return_pc)) {
1376 LOG(WARNING) << "Got a deoptimization request on un-deoptimizable " << method->PrettyMethod()
1377 << " at PC " << reinterpret_cast<void*>(*return_pc);
1378 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001379 if (kVerboseInstrumentation) {
David Sehr709b0702016-10-13 09:12:37 -07001380 LOG(INFO) << "Returning from " << method->PrettyMethod()
Brian Carlstrom2d888622013-07-18 17:02:00 -07001381 << " to PC " << reinterpret_cast<void*>(*return_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -08001382 }
Andreas Gamped58342c2014-06-05 14:18:08 -07001383 return GetTwoWordSuccessValue(0, *return_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -08001384 }
jeffhao725a9572012-11-13 18:20:12 -08001385}
1386
Mingyao Yangf711f2c2016-05-23 12:29:39 -07001387uintptr_t Instrumentation::PopMethodForUnwind(Thread* self, bool is_deoptimization) const {
Ian Rogers62d6c772013-02-27 08:32:07 -08001388 // Do the pop.
1389 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
1390 CHECK_GT(stack->size(), 0U);
Alex Lightb7edcda2017-04-27 13:20:31 -07001391 size_t idx = stack->size();
Ian Rogers62d6c772013-02-27 08:32:07 -08001392 InstrumentationStackFrame instrumentation_frame = stack->front();
Ian Rogers62d6c772013-02-27 08:32:07 -08001393
Mathieu Chartiere401d142015-04-22 13:56:20 -07001394 ArtMethod* method = instrumentation_frame.method_;
Ian Rogers62d6c772013-02-27 08:32:07 -08001395 if (is_deoptimization) {
1396 if (kVerboseInstrumentation) {
David Sehr709b0702016-10-13 09:12:37 -07001397 LOG(INFO) << "Popping for deoptimization " << ArtMethod::PrettyMethod(method);
Ian Rogers62d6c772013-02-27 08:32:07 -08001398 }
1399 } else {
1400 if (kVerboseInstrumentation) {
David Sehr709b0702016-10-13 09:12:37 -07001401 LOG(INFO) << "Popping for unwind " << ArtMethod::PrettyMethod(method);
Ian Rogers62d6c772013-02-27 08:32:07 -08001402 }
1403
1404 // Notify listeners of method unwind.
1405 // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to
1406 // return_pc.
Andreas Gampee2abbc62017-09-15 11:59:26 -07001407 uint32_t dex_pc = dex::kDexNoIndex;
Mingyao Yang2ee17902017-08-30 11:37:08 -07001408 if (!method->IsRuntimeMethod()) {
1409 MethodUnwindEvent(self, instrumentation_frame.this_object_, method, dex_pc);
1410 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001411 }
Alex Lightb7edcda2017-04-27 13:20:31 -07001412 // TODO: bring back CheckStackDepth(self, instrumentation_frame, 2);
1413 CHECK_EQ(stack->size(), idx);
1414 DCHECK(instrumentation_frame.method_ == stack->front().method_);
1415 stack->pop_front();
Mingyao Yangf711f2c2016-05-23 12:29:39 -07001416 return instrumentation_frame.return_pc_;
Ian Rogers62d6c772013-02-27 08:32:07 -08001417}
1418
1419std::string InstrumentationStackFrame::Dump() const {
1420 std::ostringstream os;
David Sehr709b0702016-10-13 09:12:37 -07001421 os << "Frame " << frame_id_ << " " << ArtMethod::PrettyMethod(method_) << ":"
Ian Rogers62d6c772013-02-27 08:32:07 -08001422 << reinterpret_cast<void*>(return_pc_) << " this=" << reinterpret_cast<void*>(this_object_);
1423 return os.str();
1424}
1425
1426} // namespace instrumentation
jeffhao725a9572012-11-13 18:20:12 -08001427} // namespace art