blob: 0ae6dbfa88b0a078b26a60be047917e884b6af3c [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"
David Sehr9e734c72018-01-04 17:56:19 -080028#include "dex/dex_file-inl.h"
29#include "dex/dex_file_types.h"
30#include "dex/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
Alex Light0fa17862017-10-24 13:43:05 -0700140bool Instrumentation::NeedDebugVersionFor(ArtMethod* method) const
141 REQUIRES_SHARED(Locks::mutator_lock_) {
142 return Runtime::Current()->IsJavaDebuggable() &&
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800143 !method->IsNative() &&
Alex Light0fa17862017-10-24 13:43:05 -0700144 !method->IsProxyMethod() &&
145 Runtime::Current()->GetRuntimeCallbacks()->IsMethodBeingInspected(method);
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800146}
147
Mathieu Chartiere401d142015-04-22 13:56:20 -0700148void Instrumentation::InstallStubsForMethod(ArtMethod* method) {
Alex Light9139e002015-10-09 15:59:48 -0700149 if (!method->IsInvokable() || method->IsProxyMethod()) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100150 // Do not change stubs for these methods.
151 return;
152 }
Jeff Hao56802772014-08-19 10:17:36 -0700153 // Don't stub Proxy.<init>. Note that the Proxy class itself is not a proxy class.
154 if (method->IsConstructor() &&
155 method->GetDeclaringClass()->DescriptorEquals("Ljava/lang/reflect/Proxy;")) {
Jeff Haodb8a6642014-08-14 17:18:52 -0700156 return;
157 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800158 const void* new_quick_code;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100159 bool uninstall = !entry_exit_stubs_installed_ && !interpreter_stubs_installed_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800160 Runtime* const runtime = Runtime::Current();
161 ClassLinker* const class_linker = runtime->GetClassLinker();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100162 bool is_class_initialized = method->GetDeclaringClass()->IsInitialized();
163 if (uninstall) {
164 if ((forced_interpret_only_ || IsDeoptimized(method)) && !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800165 new_quick_code = GetQuickToInterpreterBridge();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100166 } else if (is_class_initialized || !method->IsStatic() || method->IsConstructor()) {
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000167 if (NeedDebugVersionFor(method)) {
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800168 new_quick_code = GetQuickToInterpreterBridge();
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000169 } else {
Alex Lightfc49fec2018-01-16 22:28:36 +0000170 new_quick_code = class_linker->GetQuickOatCodeFor(method);
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800171 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100172 } else {
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700173 new_quick_code = GetQuickResolutionStub();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100174 }
175 } else { // !uninstall
Sebastien Hertzbae182c2013-12-17 10:42:03 +0100176 if ((interpreter_stubs_installed_ || forced_interpret_only_ || IsDeoptimized(method)) &&
177 !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800178 new_quick_code = GetQuickToInterpreterBridge();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100179 } else {
180 // Do not overwrite resolution trampoline. When the trampoline initializes the method's
181 // class, all its static methods code will be set to the instrumentation entry point.
182 // For more details, see ClassLinker::FixupStaticTrampolines.
183 if (is_class_initialized || !method->IsStatic() || method->IsConstructor()) {
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000184 if (NeedDebugVersionFor(method)) {
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800185 // Oat code should not be used. Don't install instrumentation stub and
186 // use interpreter for instrumentation.
187 new_quick_code = GetQuickToInterpreterBridge();
188 } else if (entry_exit_stubs_installed_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800189 new_quick_code = GetQuickInstrumentationEntryPoint();
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000190 } else {
Alex Lightfc49fec2018-01-16 22:28:36 +0000191 new_quick_code = class_linker->GetQuickOatCodeFor(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100192 }
193 } else {
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700194 new_quick_code = GetQuickResolutionStub();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100195 }
196 }
197 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800198 UpdateEntrypoints(method, new_quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100199}
200
Ian Rogers62d6c772013-02-27 08:32:07 -0800201// Places the instrumentation exit pc as the return PC for every quick frame. This also allows
202// deoptimization of quick frames to interpreter frames.
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100203// Since we may already have done this previously, we need to push new instrumentation frame before
204// existing instrumentation frames.
Ian Rogers62d6c772013-02-27 08:32:07 -0800205static void InstrumentationInstallStack(Thread* thread, void* arg)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700206 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200207 struct InstallStackVisitor FINAL : public StackVisitor {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800208 InstallStackVisitor(Thread* thread_in, Context* context, uintptr_t instrumentation_exit_pc)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100209 : StackVisitor(thread_in, context, kInstrumentationStackWalk),
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800210 instrumentation_stack_(thread_in->GetInstrumentationStack()),
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100211 instrumentation_exit_pc_(instrumentation_exit_pc),
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100212 reached_existing_instrumentation_frames_(false), instrumentation_stack_depth_(0),
213 last_return_pc_(0) {
214 }
jeffhao725a9572012-11-13 18:20:12 -0800215
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700216 bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700217 ArtMethod* m = GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700218 if (m == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800219 if (kVerboseInstrumentation) {
220 LOG(INFO) << " Skipping upcall. Frame " << GetFrameId();
221 }
222 last_return_pc_ = 0;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700223 return true; // Ignore upcalls.
Ian Rogers306057f2012-11-26 12:45:53 -0800224 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700225 if (GetCurrentQuickFrame() == nullptr) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800226 bool interpreter_frame = true;
Sebastien Hertz320deb22014-06-11 19:45:05 +0200227 InstrumentationStackFrame instrumentation_frame(GetThisObject(), m, 0, GetFrameId(),
228 interpreter_frame);
Jeff Haoa15a81b2014-05-27 18:25:47 -0700229 if (kVerboseInstrumentation) {
230 LOG(INFO) << "Pushing shadow frame " << instrumentation_frame.Dump();
231 }
232 shadow_stack_.push_back(instrumentation_frame);
233 return true; // Continue.
234 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800235 uintptr_t return_pc = GetReturnPc();
Sebastien Hertz320deb22014-06-11 19:45:05 +0200236 if (kVerboseInstrumentation) {
237 LOG(INFO) << " Installing exit stub in " << DescribeLocation();
238 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100239 if (return_pc == instrumentation_exit_pc_) {
Mingyao Yang2ee17902017-08-30 11:37:08 -0700240 CHECK_LT(instrumentation_stack_depth_, instrumentation_stack_->size());
241
242 if (m->IsRuntimeMethod()) {
243 const InstrumentationStackFrame& frame =
244 instrumentation_stack_->at(instrumentation_stack_depth_);
245 if (frame.interpreter_entry_) {
246 // This instrumentation frame is for an interpreter bridge and is
247 // pushed when executing the instrumented interpreter bridge. So method
248 // enter event must have been reported. However we need to push a DEX pc
249 // into the dex_pcs_ list to match size of instrumentation stack.
Andreas Gampee2abbc62017-09-15 11:59:26 -0700250 uint32_t dex_pc = dex::kDexNoIndex;
Mingyao Yang2ee17902017-08-30 11:37:08 -0700251 dex_pcs_.push_back(dex_pc);
252 last_return_pc_ = frame.return_pc_;
253 ++instrumentation_stack_depth_;
254 return true;
255 }
256 }
257
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100258 // We've reached a frame which has already been installed with instrumentation exit stub.
259 // We should have already installed instrumentation on previous frames.
260 reached_existing_instrumentation_frames_ = true;
261
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200262 const InstrumentationStackFrame& frame =
263 instrumentation_stack_->at(instrumentation_stack_depth_);
David Sehr709b0702016-10-13 09:12:37 -0700264 CHECK_EQ(m, frame.method_) << "Expected " << ArtMethod::PrettyMethod(m)
265 << ", Found " << ArtMethod::PrettyMethod(frame.method_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100266 return_pc = frame.return_pc_;
267 if (kVerboseInstrumentation) {
268 LOG(INFO) << "Ignoring already instrumented " << frame.Dump();
269 }
270 } else {
271 CHECK_NE(return_pc, 0U);
Alex Light0a80b0e2018-02-15 15:58:37 -0800272 if (UNLIKELY(reached_existing_instrumentation_frames_)) {
273 std::string thread_name;
274 GetThread()->GetThreadName(thread_name);
275 uint32_t dex_pc = dex::kDexNoIndex;
276 if (last_return_pc_ != 0 &&
277 GetCurrentOatQuickMethodHeader() != nullptr) {
278 dex_pc = GetCurrentOatQuickMethodHeader()->ToDexPc(m, last_return_pc_);
279 }
280 LOG(FATAL) << "While walking " << thread_name << " found existing instrumentation frames."
281 << " method is " << GetMethod()->PrettyMethod()
282 << " return_pc is " << std::hex << return_pc
283 << " dex pc: " << dex_pc;
284 UNREACHABLE();
285 }
Mingyao Yang2ee17902017-08-30 11:37:08 -0700286 InstrumentationStackFrame instrumentation_frame(
287 m->IsRuntimeMethod() ? nullptr : GetThisObject(),
288 m,
289 return_pc,
290 GetFrameId(), // A runtime method still gets a frame id.
291 false);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100292 if (kVerboseInstrumentation) {
293 LOG(INFO) << "Pushing frame " << instrumentation_frame.Dump();
294 }
295
Sebastien Hertz320deb22014-06-11 19:45:05 +0200296 // Insert frame at the right position so we do not corrupt the instrumentation stack.
297 // Instrumentation stack frames are in descending frame id order.
298 auto it = instrumentation_stack_->begin();
299 for (auto end = instrumentation_stack_->end(); it != end; ++it) {
300 const InstrumentationStackFrame& current = *it;
301 if (instrumentation_frame.frame_id_ >= current.frame_id_) {
302 break;
303 }
304 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100305 instrumentation_stack_->insert(it, instrumentation_frame);
306 SetReturnPc(instrumentation_exit_pc_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800307 }
Andreas Gampee2abbc62017-09-15 11:59:26 -0700308 uint32_t dex_pc = dex::kDexNoIndex;
Mingyao Yang2ee17902017-08-30 11:37:08 -0700309 if (last_return_pc_ != 0 &&
310 GetCurrentOatQuickMethodHeader() != nullptr) {
311 dex_pc = GetCurrentOatQuickMethodHeader()->ToDexPc(m, last_return_pc_);
312 }
313 dex_pcs_.push_back(dex_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800314 last_return_pc_ = return_pc;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100315 ++instrumentation_stack_depth_;
Ian Rogers306057f2012-11-26 12:45:53 -0800316 return true; // Continue.
317 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800318 std::deque<InstrumentationStackFrame>* const instrumentation_stack_;
Jeff Haoa15a81b2014-05-27 18:25:47 -0700319 std::vector<InstrumentationStackFrame> shadow_stack_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800320 std::vector<uint32_t> dex_pcs_;
Ian Rogers306057f2012-11-26 12:45:53 -0800321 const uintptr_t instrumentation_exit_pc_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100322 bool reached_existing_instrumentation_frames_;
323 size_t instrumentation_stack_depth_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800324 uintptr_t last_return_pc_;
Ian Rogers306057f2012-11-26 12:45:53 -0800325 };
Ian Rogers62d6c772013-02-27 08:32:07 -0800326 if (kVerboseInstrumentation) {
327 std::string thread_name;
328 thread->GetThreadName(thread_name);
329 LOG(INFO) << "Installing exit stubs in " << thread_name;
Ian Rogers306057f2012-11-26 12:45:53 -0800330 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100331
332 Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg);
Ian Rogers700a4022014-05-19 16:49:03 -0700333 std::unique_ptr<Context> context(Context::Create());
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700334 uintptr_t instrumentation_exit_pc = reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc());
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100335 InstallStackVisitor visitor(thread, context.get(), instrumentation_exit_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800336 visitor.WalkStack(true);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100337 CHECK_EQ(visitor.dex_pcs_.size(), thread->GetInstrumentationStack()->size());
Ian Rogers62d6c772013-02-27 08:32:07 -0800338
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100339 if (instrumentation->ShouldNotifyMethodEnterExitEvents()) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100340 // Create method enter events for all methods currently on the thread's stack. We only do this
341 // if no debugger is attached to prevent from posting events twice.
Jeff Haoa15a81b2014-05-27 18:25:47 -0700342 auto ssi = visitor.shadow_stack_.rbegin();
343 for (auto isi = thread->GetInstrumentationStack()->rbegin(),
344 end = thread->GetInstrumentationStack()->rend(); isi != end; ++isi) {
345 while (ssi != visitor.shadow_stack_.rend() && (*ssi).frame_id_ < (*isi).frame_id_) {
346 instrumentation->MethodEnterEvent(thread, (*ssi).this_object_, (*ssi).method_, 0);
347 ++ssi;
348 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100349 uint32_t dex_pc = visitor.dex_pcs_.back();
350 visitor.dex_pcs_.pop_back();
Sebastien Hertz320deb22014-06-11 19:45:05 +0200351 if (!isi->interpreter_entry_) {
352 instrumentation->MethodEnterEvent(thread, (*isi).this_object_, (*isi).method_, dex_pc);
353 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100354 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800355 }
356 thread->VerifyStack();
Ian Rogers306057f2012-11-26 12:45:53 -0800357}
358
Mingyao Yang99170c62015-07-06 11:10:37 -0700359void Instrumentation::InstrumentThreadStack(Thread* thread) {
360 instrumentation_stubs_installed_ = true;
361 InstrumentationInstallStack(thread, this);
362}
363
Ian Rogers62d6c772013-02-27 08:32:07 -0800364// Removes the instrumentation exit pc as the return PC for every quick frame.
365static void InstrumentationRestoreStack(Thread* thread, void* arg)
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +0000366 REQUIRES(Locks::mutator_lock_) {
367 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
368
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200369 struct RestoreStackVisitor FINAL : public StackVisitor {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800370 RestoreStackVisitor(Thread* thread_in, uintptr_t instrumentation_exit_pc,
Ian Rogers62d6c772013-02-27 08:32:07 -0800371 Instrumentation* instrumentation)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100372 : StackVisitor(thread_in, nullptr, kInstrumentationStackWalk),
373 thread_(thread_in),
Ian Rogers62d6c772013-02-27 08:32:07 -0800374 instrumentation_exit_pc_(instrumentation_exit_pc),
375 instrumentation_(instrumentation),
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800376 instrumentation_stack_(thread_in->GetInstrumentationStack()),
Ian Rogers62d6c772013-02-27 08:32:07 -0800377 frames_removed_(0) {}
Ian Rogers306057f2012-11-26 12:45:53 -0800378
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700379 bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800380 if (instrumentation_stack_->size() == 0) {
jeffhao725a9572012-11-13 18:20:12 -0800381 return false; // Stop.
382 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700383 ArtMethod* m = GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700384 if (GetCurrentQuickFrame() == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800385 if (kVerboseInstrumentation) {
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200386 LOG(INFO) << " Ignoring a shadow frame. Frame " << GetFrameId()
David Sehr709b0702016-10-13 09:12:37 -0700387 << " Method=" << ArtMethod::PrettyMethod(m);
Ian Rogers62d6c772013-02-27 08:32:07 -0800388 }
389 return true; // Ignore shadow frames.
390 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700391 if (m == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800392 if (kVerboseInstrumentation) {
393 LOG(INFO) << " Skipping upcall. Frame " << GetFrameId();
394 }
Ian Rogers306057f2012-11-26 12:45:53 -0800395 return true; // Ignore upcalls.
396 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800397 bool removed_stub = false;
398 // TODO: make this search more efficient?
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100399 const size_t frameId = GetFrameId();
400 for (const InstrumentationStackFrame& instrumentation_frame : *instrumentation_stack_) {
401 if (instrumentation_frame.frame_id_ == frameId) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800402 if (kVerboseInstrumentation) {
403 LOG(INFO) << " Removing exit stub in " << DescribeLocation();
404 }
Jeff Hao9a916d32013-06-27 18:45:37 -0700405 if (instrumentation_frame.interpreter_entry_) {
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700406 CHECK(m == Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs));
Jeff Hao9a916d32013-06-27 18:45:37 -0700407 } else {
David Sehr709b0702016-10-13 09:12:37 -0700408 CHECK(m == instrumentation_frame.method_) << ArtMethod::PrettyMethod(m);
Jeff Hao9a916d32013-06-27 18:45:37 -0700409 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800410 SetReturnPc(instrumentation_frame.return_pc_);
Mingyao Yang2ee17902017-08-30 11:37:08 -0700411 if (instrumentation_->ShouldNotifyMethodEnterExitEvents() &&
412 !m->IsRuntimeMethod()) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100413 // Create the method exit events. As the methods didn't really exit the result is 0.
414 // We only do this if no debugger is attached to prevent from posting events twice.
415 instrumentation_->MethodExitEvent(thread_, instrumentation_frame.this_object_, m,
416 GetDexPc(), JValue());
417 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800418 frames_removed_++;
419 removed_stub = true;
420 break;
421 }
422 }
423 if (!removed_stub) {
424 if (kVerboseInstrumentation) {
425 LOG(INFO) << " No exit stub in " << DescribeLocation();
Ian Rogers306057f2012-11-26 12:45:53 -0800426 }
jeffhao725a9572012-11-13 18:20:12 -0800427 }
428 return true; // Continue.
429 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800430 Thread* const thread_;
Ian Rogers306057f2012-11-26 12:45:53 -0800431 const uintptr_t instrumentation_exit_pc_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800432 Instrumentation* const instrumentation_;
433 std::deque<instrumentation::InstrumentationStackFrame>* const instrumentation_stack_;
434 size_t frames_removed_;
jeffhao725a9572012-11-13 18:20:12 -0800435 };
Ian Rogers62d6c772013-02-27 08:32:07 -0800436 if (kVerboseInstrumentation) {
437 std::string thread_name;
438 thread->GetThreadName(thread_name);
439 LOG(INFO) << "Removing exit stubs in " << thread_name;
440 }
441 std::deque<instrumentation::InstrumentationStackFrame>* stack = thread->GetInstrumentationStack();
442 if (stack->size() > 0) {
443 Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700444 uintptr_t instrumentation_exit_pc =
445 reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc());
Ian Rogers62d6c772013-02-27 08:32:07 -0800446 RestoreStackVisitor visitor(thread, instrumentation_exit_pc, instrumentation);
447 visitor.WalkStack(true);
448 CHECK_EQ(visitor.frames_removed_, stack->size());
449 while (stack->size() > 0) {
450 stack->pop_front();
451 }
jeffhao725a9572012-11-13 18:20:12 -0800452 }
453}
454
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200455static bool HasEvent(Instrumentation::InstrumentationEvent expected, uint32_t events) {
456 return (events & expected) != 0;
457}
458
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000459static void PotentiallyAddListenerTo(Instrumentation::InstrumentationEvent event,
460 uint32_t events,
461 std::list<InstrumentationListener*>& list,
462 InstrumentationListener* listener,
463 bool* has_listener)
464 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_) {
465 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
466 if (!HasEvent(event, events)) {
467 return;
468 }
469 // If there is a free slot in the list, we insert the listener in that slot.
470 // Otherwise we add it to the end of the list.
471 auto it = std::find(list.begin(), list.end(), nullptr);
472 if (it != list.end()) {
473 *it = listener;
474 } else {
475 list.push_back(listener);
476 }
477 *has_listener = true;
478}
479
Ian Rogers62d6c772013-02-27 08:32:07 -0800480void Instrumentation::AddListener(InstrumentationListener* listener, uint32_t events) {
481 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000482 PotentiallyAddListenerTo(kMethodEntered,
483 events,
484 method_entry_listeners_,
485 listener,
486 &have_method_entry_listeners_);
487 PotentiallyAddListenerTo(kMethodExited,
488 events,
489 method_exit_listeners_,
490 listener,
491 &have_method_exit_listeners_);
492 PotentiallyAddListenerTo(kMethodUnwind,
493 events,
494 method_unwind_listeners_,
495 listener,
496 &have_method_unwind_listeners_);
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000497 PotentiallyAddListenerTo(kBranch,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000498 events,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000499 branch_listeners_,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000500 listener,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000501 &have_branch_listeners_);
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000502 PotentiallyAddListenerTo(kInvokeVirtualOrInterface,
503 events,
504 invoke_virtual_or_interface_listeners_,
505 listener,
506 &have_invoke_virtual_or_interface_listeners_);
507 PotentiallyAddListenerTo(kDexPcMoved,
508 events,
509 dex_pc_listeners_,
510 listener,
511 &have_dex_pc_listeners_);
512 PotentiallyAddListenerTo(kFieldRead,
513 events,
514 field_read_listeners_,
515 listener,
516 &have_field_read_listeners_);
517 PotentiallyAddListenerTo(kFieldWritten,
518 events,
519 field_write_listeners_,
520 listener,
521 &have_field_write_listeners_);
Alex Light6e1607e2017-08-23 10:06:18 -0700522 PotentiallyAddListenerTo(kExceptionThrown,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000523 events,
Alex Light6e1607e2017-08-23 10:06:18 -0700524 exception_thrown_listeners_,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000525 listener,
Alex Light6e1607e2017-08-23 10:06:18 -0700526 &have_exception_thrown_listeners_);
Alex Lighte814f9d2017-07-31 16:14:39 -0700527 PotentiallyAddListenerTo(kWatchedFramePop,
528 events,
529 watched_frame_pop_listeners_,
530 listener,
531 &have_watched_frame_pop_listeners_);
Alex Light9fb1ab12017-09-05 09:32:49 -0700532 PotentiallyAddListenerTo(kExceptionHandled,
533 events,
534 exception_handled_listeners_,
535 listener,
536 &have_exception_handled_listeners_);
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200537 UpdateInterpreterHandlerTable();
jeffhao725a9572012-11-13 18:20:12 -0800538}
539
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000540static void PotentiallyRemoveListenerFrom(Instrumentation::InstrumentationEvent event,
541 uint32_t events,
542 std::list<InstrumentationListener*>& list,
543 InstrumentationListener* listener,
544 bool* has_listener)
545 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_) {
546 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
547 if (!HasEvent(event, events)) {
548 return;
549 }
550 auto it = std::find(list.begin(), list.end(), listener);
551 if (it != list.end()) {
552 // Just update the entry, do not remove from the list. Removing entries in the list
553 // is unsafe when mutators are iterating over it.
554 *it = nullptr;
555 }
556
557 // Check if the list contains any non-null listener, and update 'has_listener'.
558 for (InstrumentationListener* l : list) {
559 if (l != nullptr) {
560 *has_listener = true;
561 return;
562 }
563 }
564 *has_listener = false;
565}
566
Ian Rogers62d6c772013-02-27 08:32:07 -0800567void Instrumentation::RemoveListener(InstrumentationListener* listener, uint32_t events) {
568 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000569 PotentiallyRemoveListenerFrom(kMethodEntered,
570 events,
571 method_entry_listeners_,
572 listener,
573 &have_method_entry_listeners_);
574 PotentiallyRemoveListenerFrom(kMethodExited,
575 events,
576 method_exit_listeners_,
577 listener,
578 &have_method_exit_listeners_);
579 PotentiallyRemoveListenerFrom(kMethodUnwind,
580 events,
581 method_unwind_listeners_,
582 listener,
583 &have_method_unwind_listeners_);
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000584 PotentiallyRemoveListenerFrom(kBranch,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000585 events,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000586 branch_listeners_,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000587 listener,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000588 &have_branch_listeners_);
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000589 PotentiallyRemoveListenerFrom(kInvokeVirtualOrInterface,
590 events,
591 invoke_virtual_or_interface_listeners_,
592 listener,
593 &have_invoke_virtual_or_interface_listeners_);
594 PotentiallyRemoveListenerFrom(kDexPcMoved,
595 events,
596 dex_pc_listeners_,
597 listener,
598 &have_dex_pc_listeners_);
599 PotentiallyRemoveListenerFrom(kFieldRead,
600 events,
601 field_read_listeners_,
602 listener,
603 &have_field_read_listeners_);
604 PotentiallyRemoveListenerFrom(kFieldWritten,
605 events,
606 field_write_listeners_,
607 listener,
608 &have_field_write_listeners_);
Alex Light6e1607e2017-08-23 10:06:18 -0700609 PotentiallyRemoveListenerFrom(kExceptionThrown,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000610 events,
Alex Light6e1607e2017-08-23 10:06:18 -0700611 exception_thrown_listeners_,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000612 listener,
Alex Light6e1607e2017-08-23 10:06:18 -0700613 &have_exception_thrown_listeners_);
Alex Lighte814f9d2017-07-31 16:14:39 -0700614 PotentiallyRemoveListenerFrom(kWatchedFramePop,
615 events,
616 watched_frame_pop_listeners_,
617 listener,
618 &have_watched_frame_pop_listeners_);
Alex Light9fb1ab12017-09-05 09:32:49 -0700619 PotentiallyRemoveListenerFrom(kExceptionHandled,
620 events,
621 exception_handled_listeners_,
622 listener,
623 &have_exception_handled_listeners_);
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200624 UpdateInterpreterHandlerTable();
jeffhao725a9572012-11-13 18:20:12 -0800625}
626
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200627Instrumentation::InstrumentationLevel Instrumentation::GetCurrentInstrumentationLevel() const {
Alex Light4ba388a2017-01-27 10:26:49 -0800628 if (interpreter_stubs_installed_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200629 return InstrumentationLevel::kInstrumentWithInterpreter;
Ian Rogers62d6c772013-02-27 08:32:07 -0800630 } else if (entry_exit_stubs_installed_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200631 return InstrumentationLevel::kInstrumentWithInstrumentationStubs;
Ian Rogers62d6c772013-02-27 08:32:07 -0800632 } else {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200633 return InstrumentationLevel::kInstrumentNothing;
Ian Rogers62d6c772013-02-27 08:32:07 -0800634 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200635}
636
Alex Lightdba61482016-12-21 08:20:29 -0800637bool Instrumentation::RequiresInstrumentationInstallation(InstrumentationLevel new_level) const {
Alex Light4ba388a2017-01-27 10:26:49 -0800638 // We need to reinstall instrumentation if we go to a different level.
639 return GetCurrentInstrumentationLevel() != new_level;
Alex Lightdba61482016-12-21 08:20:29 -0800640}
641
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200642void Instrumentation::ConfigureStubs(const char* key, InstrumentationLevel desired_level) {
643 // Store the instrumentation level for this key or remove it.
644 if (desired_level == InstrumentationLevel::kInstrumentNothing) {
645 // The client no longer needs instrumentation.
646 requested_instrumentation_levels_.erase(key);
647 } else {
648 // The client needs instrumentation.
649 requested_instrumentation_levels_.Overwrite(key, desired_level);
650 }
651
652 // Look for the highest required instrumentation level.
653 InstrumentationLevel requested_level = InstrumentationLevel::kInstrumentNothing;
654 for (const auto& v : requested_instrumentation_levels_) {
655 requested_level = std::max(requested_level, v.second);
656 }
657
658 interpret_only_ = (requested_level == InstrumentationLevel::kInstrumentWithInterpreter) ||
659 forced_interpret_only_;
660
Alex Lightdba61482016-12-21 08:20:29 -0800661 if (!RequiresInstrumentationInstallation(requested_level)) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800662 // We're already set.
663 return;
664 }
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100665 Thread* const self = Thread::Current();
Ian Rogers62d6c772013-02-27 08:32:07 -0800666 Runtime* runtime = Runtime::Current();
Sebastien Hertza8a697f2015-01-15 12:28:47 +0100667 Locks::mutator_lock_->AssertExclusiveHeld(self);
Ian Rogers62d6c772013-02-27 08:32:07 -0800668 Locks::thread_list_lock_->AssertNotHeld(self);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200669 if (requested_level > InstrumentationLevel::kInstrumentNothing) {
Alex Light4ba388a2017-01-27 10:26:49 -0800670 if (requested_level == InstrumentationLevel::kInstrumentWithInterpreter) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800671 interpreter_stubs_installed_ = true;
Ian Rogers62d6c772013-02-27 08:32:07 -0800672 entry_exit_stubs_installed_ = true;
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200673 } else {
674 CHECK_EQ(requested_level, InstrumentationLevel::kInstrumentWithInstrumentationStubs);
675 entry_exit_stubs_installed_ = true;
676 interpreter_stubs_installed_ = false;
Ian Rogers62d6c772013-02-27 08:32:07 -0800677 }
Mathieu Chartiere0671ce2015-07-28 17:23:28 -0700678 InstallStubsClassVisitor visitor(this);
679 runtime->GetClassLinker()->VisitClasses(&visitor);
Ian Rogers62d6c772013-02-27 08:32:07 -0800680 instrumentation_stubs_installed_ = true;
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100681 MutexLock mu(self, *Locks::thread_list_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800682 runtime->GetThreadList()->ForEach(InstrumentationInstallStack, this);
683 } else {
684 interpreter_stubs_installed_ = false;
685 entry_exit_stubs_installed_ = false;
Mathieu Chartiere0671ce2015-07-28 17:23:28 -0700686 InstallStubsClassVisitor visitor(this);
687 runtime->GetClassLinker()->VisitClasses(&visitor);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100688 // Restore stack only if there is no method currently deoptimized.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700689 bool empty;
690 {
691 ReaderMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700692 empty = IsDeoptimizedMethodsEmpty(); // Avoid lock violation.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700693 }
694 if (empty) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100695 MutexLock mu(self, *Locks::thread_list_lock_);
696 Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this);
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +0000697 // Only do this after restoring, as walking the stack when restoring will see
698 // the instrumentation exit pc.
699 instrumentation_stubs_installed_ = false;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100700 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800701 }
jeffhao725a9572012-11-13 18:20:12 -0800702}
703
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200704static void ResetQuickAllocEntryPointsForThread(Thread* thread, void* arg ATTRIBUTE_UNUSED) {
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800705 thread->ResetQuickAllocEntryPointsForThread(kUseReadBarrier && thread->GetIsGcMarking());
Ian Rogersfa824272013-11-05 16:12:57 -0800706}
707
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700708void Instrumentation::SetEntrypointsInstrumented(bool instrumented) {
709 Thread* self = Thread::Current();
Mathieu Chartier661974a2014-01-09 11:23:53 -0800710 Runtime* runtime = Runtime::Current();
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700711 Locks::mutator_lock_->AssertNotHeld(self);
712 Locks::instrument_entrypoints_lock_->AssertHeld(self);
713 if (runtime->IsStarted()) {
Mathieu Chartier4f55e222015-09-04 13:26:21 -0700714 ScopedSuspendAll ssa(__FUNCTION__);
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700715 MutexLock mu(self, *Locks::runtime_shutdown_lock_);
Mathieu Chartier661974a2014-01-09 11:23:53 -0800716 SetQuickAllocEntryPointsInstrumented(instrumented);
717 ResetQuickAllocEntryPoints();
Mathieu Chartier50e93312016-03-16 11:25:29 -0700718 alloc_entrypoints_instrumented_ = instrumented;
Mathieu Chartier4f55e222015-09-04 13:26:21 -0700719 } else {
720 MutexLock mu(self, *Locks::runtime_shutdown_lock_);
721 SetQuickAllocEntryPointsInstrumented(instrumented);
Andreas Gampe157c77e2016-10-17 17:44:41 -0700722
723 // Note: ResetQuickAllocEntryPoints only works when the runtime is started. Manually run the
724 // update for just this thread.
Andreas Gampe162ae502016-10-18 10:03:42 -0700725 // Note: self may be null. One of those paths is setting instrumentation in the Heap
726 // constructor for gcstress mode.
727 if (self != nullptr) {
728 ResetQuickAllocEntryPointsForThread(self, nullptr);
729 }
Andreas Gampe157c77e2016-10-17 17:44:41 -0700730
Mathieu Chartier50e93312016-03-16 11:25:29 -0700731 alloc_entrypoints_instrumented_ = instrumented;
Mathieu Chartier661974a2014-01-09 11:23:53 -0800732 }
733}
734
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700735void Instrumentation::InstrumentQuickAllocEntryPoints() {
736 MutexLock mu(Thread::Current(), *Locks::instrument_entrypoints_lock_);
737 InstrumentQuickAllocEntryPointsLocked();
Ian Rogersfa824272013-11-05 16:12:57 -0800738}
739
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700740void Instrumentation::UninstrumentQuickAllocEntryPoints() {
741 MutexLock mu(Thread::Current(), *Locks::instrument_entrypoints_lock_);
742 UninstrumentQuickAllocEntryPointsLocked();
743}
744
745void Instrumentation::InstrumentQuickAllocEntryPointsLocked() {
746 Locks::instrument_entrypoints_lock_->AssertHeld(Thread::Current());
747 if (quick_alloc_entry_points_instrumentation_counter_ == 0) {
748 SetEntrypointsInstrumented(true);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800749 }
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700750 ++quick_alloc_entry_points_instrumentation_counter_;
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700751}
752
753void Instrumentation::UninstrumentQuickAllocEntryPointsLocked() {
754 Locks::instrument_entrypoints_lock_->AssertHeld(Thread::Current());
755 CHECK_GT(quick_alloc_entry_points_instrumentation_counter_, 0U);
756 --quick_alloc_entry_points_instrumentation_counter_;
757 if (quick_alloc_entry_points_instrumentation_counter_ == 0) {
758 SetEntrypointsInstrumented(false);
759 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800760}
761
762void Instrumentation::ResetQuickAllocEntryPoints() {
763 Runtime* runtime = Runtime::Current();
764 if (runtime->IsStarted()) {
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800765 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700766 runtime->GetThreadList()->ForEach(ResetQuickAllocEntryPointsForThread, nullptr);
Ian Rogersfa824272013-11-05 16:12:57 -0800767 }
768}
769
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700770void Instrumentation::UpdateMethodsCodeImpl(ArtMethod* method, const void* quick_code) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800771 const void* new_quick_code;
Ian Rogers62d6c772013-02-27 08:32:07 -0800772 if (LIKELY(!instrumentation_stubs_installed_)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800773 new_quick_code = quick_code;
Jeff Hao65d15d92013-07-16 16:39:33 -0700774 } else {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100775 if ((interpreter_stubs_installed_ || IsDeoptimized(method)) && !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800776 new_quick_code = GetQuickToInterpreterBridge();
Jeff Hao65d15d92013-07-16 16:39:33 -0700777 } else {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700778 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700779 if (class_linker->IsQuickResolutionStub(quick_code) ||
780 class_linker->IsQuickToInterpreterBridge(quick_code)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700781 new_quick_code = quick_code;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700782 } else if (entry_exit_stubs_installed_) {
783 new_quick_code = GetQuickInstrumentationEntryPoint();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700784 } else {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700785 new_quick_code = quick_code;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700786 }
Jeff Hao65d15d92013-07-16 16:39:33 -0700787 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800788 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800789 UpdateEntrypoints(method, new_quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100790}
791
Nicolas Geoffraya6e0e7d2018-01-26 13:16:50 +0000792void Instrumentation::UpdateNativeMethodsCodeToJitCode(ArtMethod* method, const void* quick_code) {
793 // We don't do any read barrier on `method`'s declaring class in this code, as the JIT might
794 // enter here on a soon-to-be deleted ArtMethod. Updating the entrypoint is OK though, as
795 // the ArtMethod is still in memory.
796 const void* new_quick_code = quick_code;
797 if (UNLIKELY(instrumentation_stubs_installed_) && entry_exit_stubs_installed_) {
798 new_quick_code = GetQuickInstrumentationEntryPoint();
799 }
800 UpdateEntrypoints(method, new_quick_code);
801}
802
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700803void Instrumentation::UpdateMethodsCode(ArtMethod* method, const void* quick_code) {
804 DCHECK(method->GetDeclaringClass()->IsResolved());
805 UpdateMethodsCodeImpl(method, quick_code);
806}
807
Alex Light0a5ec3d2017-07-25 16:50:26 -0700808void Instrumentation::UpdateMethodsCodeToInterpreterEntryPoint(ArtMethod* method) {
809 UpdateMethodsCodeImpl(method, GetQuickToInterpreterBridge());
810}
811
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000812void Instrumentation::UpdateMethodsCodeForJavaDebuggable(ArtMethod* method,
813 const void* quick_code) {
814 // When the runtime is set to Java debuggable, we may update the entry points of
815 // all methods of a class to the interpreter bridge. A method's declaring class
816 // might not be in resolved state yet in that case, so we bypass the DCHECK in
817 // UpdateMethodsCode.
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700818 UpdateMethodsCodeImpl(method, quick_code);
819}
820
Mathieu Chartiere401d142015-04-22 13:56:20 -0700821bool Instrumentation::AddDeoptimizedMethod(ArtMethod* method) {
822 if (IsDeoptimizedMethod(method)) {
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700823 // Already in the map. Return.
824 return false;
825 }
826 // Not found. Add it.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700827 deoptimized_methods_.insert(method);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700828 return true;
829}
830
Mathieu Chartiere401d142015-04-22 13:56:20 -0700831bool Instrumentation::IsDeoptimizedMethod(ArtMethod* method) {
832 return deoptimized_methods_.find(method) != deoptimized_methods_.end();
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700833}
834
Mathieu Chartiere401d142015-04-22 13:56:20 -0700835ArtMethod* Instrumentation::BeginDeoptimizedMethod() {
836 if (deoptimized_methods_.empty()) {
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700837 // Empty.
838 return nullptr;
839 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700840 return *deoptimized_methods_.begin();
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700841}
842
Mathieu Chartiere401d142015-04-22 13:56:20 -0700843bool Instrumentation::RemoveDeoptimizedMethod(ArtMethod* method) {
844 auto it = deoptimized_methods_.find(method);
845 if (it == deoptimized_methods_.end()) {
846 return false;
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700847 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700848 deoptimized_methods_.erase(it);
849 return true;
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700850}
851
852bool Instrumentation::IsDeoptimizedMethodsEmpty() const {
853 return deoptimized_methods_.empty();
854}
855
Mathieu Chartiere401d142015-04-22 13:56:20 -0700856void Instrumentation::Deoptimize(ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100857 CHECK(!method->IsNative());
858 CHECK(!method->IsProxyMethod());
Alex Light9139e002015-10-09 15:59:48 -0700859 CHECK(method->IsInvokable());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100860
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700861 Thread* self = Thread::Current();
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700862 {
863 WriterMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700864 bool has_not_been_deoptimized = AddDeoptimizedMethod(method);
David Sehr709b0702016-10-13 09:12:37 -0700865 CHECK(has_not_been_deoptimized) << "Method " << ArtMethod::PrettyMethod(method)
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200866 << " is already deoptimized";
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700867 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100868 if (!interpreter_stubs_installed_) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800869 UpdateEntrypoints(method, GetQuickInstrumentationEntryPoint());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100870
871 // Install instrumentation exit stub and instrumentation frames. We may already have installed
872 // these previously so it will only cover the newly created frames.
873 instrumentation_stubs_installed_ = true;
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700874 MutexLock mu(self, *Locks::thread_list_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100875 Runtime::Current()->GetThreadList()->ForEach(InstrumentationInstallStack, this);
876 }
877}
878
Mathieu Chartiere401d142015-04-22 13:56:20 -0700879void Instrumentation::Undeoptimize(ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100880 CHECK(!method->IsNative());
881 CHECK(!method->IsProxyMethod());
Alex Light9139e002015-10-09 15:59:48 -0700882 CHECK(method->IsInvokable());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100883
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700884 Thread* self = Thread::Current();
885 bool empty;
886 {
887 WriterMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700888 bool found_and_erased = RemoveDeoptimizedMethod(method);
David Sehr709b0702016-10-13 09:12:37 -0700889 CHECK(found_and_erased) << "Method " << ArtMethod::PrettyMethod(method)
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700890 << " is not deoptimized";
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700891 empty = IsDeoptimizedMethodsEmpty();
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700892 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100893
894 // Restore code and possibly stack only if we did not deoptimize everything.
895 if (!interpreter_stubs_installed_) {
896 // Restore its code or resolution trampoline.
897 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800898 if (method->IsStatic() && !method->IsConstructor() &&
899 !method->GetDeclaringClass()->IsInitialized()) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800900 UpdateEntrypoints(method, GetQuickResolutionStub());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100901 } else {
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000902 const void* quick_code = NeedDebugVersionFor(method)
903 ? GetQuickToInterpreterBridge()
Alex Lightfc49fec2018-01-16 22:28:36 +0000904 : class_linker->GetQuickOatCodeFor(method);
Elliott Hughes956af0f2014-12-11 14:34:28 -0800905 UpdateEntrypoints(method, quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100906 }
907
908 // If there is no deoptimized method left, we can restore the stack of each thread.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700909 if (empty) {
910 MutexLock mu(self, *Locks::thread_list_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100911 Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this);
912 instrumentation_stubs_installed_ = false;
913 }
914 }
915}
916
Mathieu Chartiere401d142015-04-22 13:56:20 -0700917bool Instrumentation::IsDeoptimized(ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100918 DCHECK(method != nullptr);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700919 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700920 return IsDeoptimizedMethod(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100921}
922
923void Instrumentation::EnableDeoptimization() {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700924 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700925 CHECK(IsDeoptimizedMethodsEmpty());
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100926 CHECK_EQ(deoptimization_enabled_, false);
927 deoptimization_enabled_ = true;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100928}
929
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200930void Instrumentation::DisableDeoptimization(const char* key) {
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100931 CHECK_EQ(deoptimization_enabled_, true);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100932 // If we deoptimized everything, undo it.
Alex Lightdba61482016-12-21 08:20:29 -0800933 InstrumentationLevel level = GetCurrentInstrumentationLevel();
934 if (level == InstrumentationLevel::kInstrumentWithInterpreter) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200935 UndeoptimizeEverything(key);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100936 }
937 // Undeoptimized selected methods.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700938 while (true) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700939 ArtMethod* method;
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700940 {
941 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700942 if (IsDeoptimizedMethodsEmpty()) {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700943 break;
944 }
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700945 method = BeginDeoptimizedMethod();
946 CHECK(method != nullptr);
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700947 }
948 Undeoptimize(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100949 }
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100950 deoptimization_enabled_ = false;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100951}
952
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100953// Indicates if instrumentation should notify method enter/exit events to the listeners.
954bool Instrumentation::ShouldNotifyMethodEnterExitEvents() const {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200955 if (!HasMethodEntryListeners() && !HasMethodExitListeners()) {
956 return false;
957 }
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100958 return !deoptimization_enabled_ && !interpreter_stubs_installed_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100959}
960
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200961void Instrumentation::DeoptimizeEverything(const char* key) {
962 CHECK(deoptimization_enabled_);
963 ConfigureStubs(key, InstrumentationLevel::kInstrumentWithInterpreter);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100964}
965
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200966void Instrumentation::UndeoptimizeEverything(const char* key) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100967 CHECK(interpreter_stubs_installed_);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200968 CHECK(deoptimization_enabled_);
969 ConfigureStubs(key, InstrumentationLevel::kInstrumentNothing);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100970}
971
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200972void Instrumentation::EnableMethodTracing(const char* key, bool needs_interpreter) {
973 InstrumentationLevel level;
974 if (needs_interpreter) {
975 level = InstrumentationLevel::kInstrumentWithInterpreter;
976 } else {
977 level = InstrumentationLevel::kInstrumentWithInstrumentationStubs;
978 }
979 ConfigureStubs(key, level);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100980}
981
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200982void Instrumentation::DisableMethodTracing(const char* key) {
983 ConfigureStubs(key, InstrumentationLevel::kInstrumentNothing);
jeffhao725a9572012-11-13 18:20:12 -0800984}
985
Andreas Gampe542451c2016-07-26 09:02:02 -0700986const void* Instrumentation::GetQuickCodeFor(ArtMethod* method, PointerSize pointer_size) const {
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100987 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers62d6c772013-02-27 08:32:07 -0800988 if (LIKELY(!instrumentation_stubs_installed_)) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800989 const void* code = method->GetEntryPointFromQuickCompiledCodePtrSize(pointer_size);
Vladimir Marko8a630572014-04-09 18:45:35 +0100990 DCHECK(code != nullptr);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700991 if (LIKELY(!class_linker->IsQuickResolutionStub(code) &&
992 !class_linker->IsQuickToInterpreterBridge(code)) &&
993 !class_linker->IsQuickResolutionStub(code) &&
994 !class_linker->IsQuickToInterpreterBridge(code)) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800995 return code;
996 }
997 }
Alex Lightfc49fec2018-01-16 22:28:36 +0000998 return class_linker->GetQuickOatCodeFor(method);
jeffhao725a9572012-11-13 18:20:12 -0800999}
1000
Alex Lightd7661582017-05-01 13:48:16 -07001001void Instrumentation::MethodEnterEventImpl(Thread* thread,
1002 ObjPtr<mirror::Object> this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001003 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -08001004 uint32_t dex_pc) const {
Mingyao Yang2ee17902017-08-30 11:37:08 -07001005 DCHECK(!method->IsRuntimeMethod());
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001006 if (HasMethodEntryListeners()) {
Alex Lightd7661582017-05-01 13:48:16 -07001007 Thread* self = Thread::Current();
1008 StackHandleScope<1> hs(self);
1009 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001010 for (InstrumentationListener* listener : method_entry_listeners_) {
1011 if (listener != nullptr) {
Alex Lightd7661582017-05-01 13:48:16 -07001012 listener->MethodEntered(thread, thiz, method, dex_pc);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001013 }
1014 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001015 }
1016}
1017
Alex Lightd7661582017-05-01 13:48:16 -07001018void Instrumentation::MethodExitEventImpl(Thread* thread,
1019 ObjPtr<mirror::Object> this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001020 ArtMethod* method,
Alex Lightd7661582017-05-01 13:48:16 -07001021 uint32_t dex_pc,
1022 const JValue& return_value) const {
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001023 if (HasMethodExitListeners()) {
Alex Lightd7661582017-05-01 13:48:16 -07001024 Thread* self = Thread::Current();
1025 StackHandleScope<2> hs(self);
1026 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
1027 if (method->GetInterfaceMethodIfProxy(kRuntimePointerSize)
1028 ->GetReturnTypePrimitive() != Primitive::kPrimNot) {
1029 for (InstrumentationListener* listener : method_exit_listeners_) {
1030 if (listener != nullptr) {
1031 listener->MethodExited(thread, thiz, method, dex_pc, return_value);
1032 }
1033 }
1034 } else {
1035 Handle<mirror::Object> ret(hs.NewHandle(return_value.GetL()));
1036 for (InstrumentationListener* listener : method_exit_listeners_) {
1037 if (listener != nullptr) {
1038 listener->MethodExited(thread, thiz, method, dex_pc, ret);
1039 }
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001040 }
1041 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001042 }
1043}
1044
Alex Lightd7661582017-05-01 13:48:16 -07001045void Instrumentation::MethodUnwindEvent(Thread* thread,
1046 mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001047 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -08001048 uint32_t dex_pc) const {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +02001049 if (HasMethodUnwindListeners()) {
Alex Lightd7661582017-05-01 13:48:16 -07001050 Thread* self = Thread::Current();
1051 StackHandleScope<1> hs(self);
1052 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
Mathieu Chartier02e25112013-08-14 16:14:24 -07001053 for (InstrumentationListener* listener : method_unwind_listeners_) {
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001054 if (listener != nullptr) {
Alex Lightd7661582017-05-01 13:48:16 -07001055 listener->MethodUnwind(thread, thiz, method, dex_pc);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001056 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001057 }
1058 }
1059}
1060
Alex Lightd7661582017-05-01 13:48:16 -07001061void Instrumentation::DexPcMovedEventImpl(Thread* thread,
1062 ObjPtr<mirror::Object> this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001063 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -08001064 uint32_t dex_pc) const {
Alex Lightd7661582017-05-01 13:48:16 -07001065 Thread* self = Thread::Current();
1066 StackHandleScope<1> hs(self);
1067 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001068 for (InstrumentationListener* listener : dex_pc_listeners_) {
1069 if (listener != nullptr) {
Alex Lightd7661582017-05-01 13:48:16 -07001070 listener->DexPcMoved(thread, thiz, method, dex_pc);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001071 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001072 }
1073}
1074
Nicolas Geoffray81f0f952016-01-20 16:25:19 +00001075void Instrumentation::BranchImpl(Thread* thread,
1076 ArtMethod* method,
1077 uint32_t dex_pc,
1078 int32_t offset) const {
1079 for (InstrumentationListener* listener : branch_listeners_) {
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001080 if (listener != nullptr) {
Nicolas Geoffray81f0f952016-01-20 16:25:19 +00001081 listener->Branch(thread, method, dex_pc, offset);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001082 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001083 }
1084}
1085
Nicolas Geoffray5550ca82015-08-21 18:38:30 +01001086void Instrumentation::InvokeVirtualOrInterfaceImpl(Thread* thread,
Alex Lightd7661582017-05-01 13:48:16 -07001087 ObjPtr<mirror::Object> this_object,
Nicolas Geoffray5550ca82015-08-21 18:38:30 +01001088 ArtMethod* caller,
1089 uint32_t dex_pc,
1090 ArtMethod* callee) const {
Alex Lightd7661582017-05-01 13:48:16 -07001091 Thread* self = Thread::Current();
1092 StackHandleScope<1> hs(self);
1093 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
Nicolas Geoffray5550ca82015-08-21 18:38:30 +01001094 for (InstrumentationListener* listener : invoke_virtual_or_interface_listeners_) {
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001095 if (listener != nullptr) {
Alex Lightd7661582017-05-01 13:48:16 -07001096 listener->InvokeVirtualOrInterface(thread, thiz, caller, dex_pc, callee);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001097 }
Nicolas Geoffray5550ca82015-08-21 18:38:30 +01001098 }
1099}
1100
Alex Lighte814f9d2017-07-31 16:14:39 -07001101void Instrumentation::WatchedFramePopImpl(Thread* thread, const ShadowFrame& frame) const {
1102 for (InstrumentationListener* listener : watched_frame_pop_listeners_) {
1103 if (listener != nullptr) {
1104 listener->WatchedFramePop(thread, frame);
1105 }
1106 }
1107}
1108
Alex Lightd7661582017-05-01 13:48:16 -07001109void Instrumentation::FieldReadEventImpl(Thread* thread,
1110 ObjPtr<mirror::Object> this_object,
1111 ArtMethod* method,
1112 uint32_t dex_pc,
Mathieu Chartierc7853442015-03-27 14:35:38 -07001113 ArtField* field) const {
Alex Lightd7661582017-05-01 13:48:16 -07001114 Thread* self = Thread::Current();
1115 StackHandleScope<1> hs(self);
1116 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001117 for (InstrumentationListener* listener : field_read_listeners_) {
1118 if (listener != nullptr) {
Alex Lightd7661582017-05-01 13:48:16 -07001119 listener->FieldRead(thread, thiz, method, dex_pc, field);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001120 }
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +02001121 }
1122}
1123
Alex Lightd7661582017-05-01 13:48:16 -07001124void Instrumentation::FieldWriteEventImpl(Thread* thread,
1125 ObjPtr<mirror::Object> this_object,
1126 ArtMethod* method,
1127 uint32_t dex_pc,
1128 ArtField* field,
1129 const JValue& field_value) const {
1130 Thread* self = Thread::Current();
1131 StackHandleScope<2> hs(self);
1132 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
1133 if (field->IsPrimitiveType()) {
1134 for (InstrumentationListener* listener : field_write_listeners_) {
1135 if (listener != nullptr) {
1136 listener->FieldWritten(thread, thiz, method, dex_pc, field, field_value);
1137 }
1138 }
1139 } else {
1140 Handle<mirror::Object> val(hs.NewHandle(field_value.GetL()));
1141 for (InstrumentationListener* listener : field_write_listeners_) {
1142 if (listener != nullptr) {
1143 listener->FieldWritten(thread, thiz, method, dex_pc, field, val);
1144 }
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001145 }
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +02001146 }
1147}
1148
Alex Light6e1607e2017-08-23 10:06:18 -07001149void Instrumentation::ExceptionThrownEvent(Thread* thread,
Sebastien Hertz947ff082013-09-17 14:10:13 +02001150 mirror::Throwable* exception_object) const {
Hiroshi Yamauchi3481f7a2017-02-10 12:07:36 -08001151 Thread* self = Thread::Current();
1152 StackHandleScope<1> hs(self);
1153 Handle<mirror::Throwable> h_exception(hs.NewHandle(exception_object));
Alex Light6e1607e2017-08-23 10:06:18 -07001154 if (HasExceptionThrownListeners()) {
Hiroshi Yamauchi3481f7a2017-02-10 12:07:36 -08001155 DCHECK_EQ(thread->GetException(), h_exception.Get());
Jeff Haoc0bd4da2013-04-11 15:52:28 -07001156 thread->ClearException();
Alex Light6e1607e2017-08-23 10:06:18 -07001157 for (InstrumentationListener* listener : exception_thrown_listeners_) {
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001158 if (listener != nullptr) {
Alex Light6e1607e2017-08-23 10:06:18 -07001159 listener->ExceptionThrown(thread, h_exception);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001160 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001161 }
Alex Light9fb1ab12017-09-05 09:32:49 -07001162 // See b/65049545 for discussion about this behavior.
1163 thread->AssertNoPendingException();
Hiroshi Yamauchi3481f7a2017-02-10 12:07:36 -08001164 thread->SetException(h_exception.Get());
Ian Rogers62d6c772013-02-27 08:32:07 -08001165 }
1166}
1167
Alex Light9fb1ab12017-09-05 09:32:49 -07001168void Instrumentation::ExceptionHandledEvent(Thread* thread,
1169 mirror::Throwable* exception_object) const {
1170 Thread* self = Thread::Current();
1171 StackHandleScope<1> hs(self);
1172 Handle<mirror::Throwable> h_exception(hs.NewHandle(exception_object));
1173 if (HasExceptionHandledListeners()) {
1174 // We should have cleared the exception so that callers can detect a new one.
1175 DCHECK(thread->GetException() == nullptr);
1176 for (InstrumentationListener* listener : exception_handled_listeners_) {
1177 if (listener != nullptr) {
1178 listener->ExceptionHandled(thread, h_exception);
1179 }
1180 }
1181 }
1182}
1183
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +00001184// Computes a frame ID by ignoring inlined frames.
1185size_t Instrumentation::ComputeFrameId(Thread* self,
1186 size_t frame_depth,
1187 size_t inlined_frames_before_frame) {
1188 CHECK_GE(frame_depth, inlined_frames_before_frame);
1189 size_t no_inline_depth = frame_depth - inlined_frames_before_frame;
1190 return StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk) - no_inline_depth;
1191}
1192
Ian Rogers62d6c772013-02-27 08:32:07 -08001193static void CheckStackDepth(Thread* self, const InstrumentationStackFrame& instrumentation_frame,
1194 int delta)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001195 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +01001196 size_t frame_id = StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk) + delta;
Ian Rogers62d6c772013-02-27 08:32:07 -08001197 if (frame_id != instrumentation_frame.frame_id_) {
1198 LOG(ERROR) << "Expected frame_id=" << frame_id << " but found "
1199 << instrumentation_frame.frame_id_;
1200 StackVisitor::DescribeStack(self);
1201 CHECK_EQ(frame_id, instrumentation_frame.frame_id_);
1202 }
1203}
1204
1205void Instrumentation::PushInstrumentationStackFrame(Thread* self, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001206 ArtMethod* method,
Jeff Hao9a916d32013-06-27 18:45:37 -07001207 uintptr_t lr, bool interpreter_entry) {
Alex Lightb7edcda2017-04-27 13:20:31 -07001208 DCHECK(!self->IsExceptionPending());
Ian Rogers62d6c772013-02-27 08:32:07 -08001209 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
1210 if (kVerboseInstrumentation) {
David Sehr709b0702016-10-13 09:12:37 -07001211 LOG(INFO) << "Entering " << ArtMethod::PrettyMethod(method) << " from PC "
1212 << reinterpret_cast<void*>(lr);
Ian Rogers62d6c772013-02-27 08:32:07 -08001213 }
Alex Lightb7edcda2017-04-27 13:20:31 -07001214
1215 // We send the enter event before pushing the instrumentation frame to make cleanup easier. If the
1216 // event causes an exception we can simply send the unwind event and return.
1217 StackHandleScope<1> hs(self);
1218 Handle<mirror::Object> h_this(hs.NewHandle(this_object));
1219 if (!interpreter_entry) {
1220 MethodEnterEvent(self, h_this.Get(), method, 0);
1221 if (self->IsExceptionPending()) {
1222 MethodUnwindEvent(self, h_this.Get(), method, 0);
1223 return;
1224 }
1225 }
1226
1227 // We have a callee-save frame meaning this value is guaranteed to never be 0.
1228 DCHECK(!self->IsExceptionPending());
1229 size_t frame_id = StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk);
1230
1231 instrumentation::InstrumentationStackFrame instrumentation_frame(h_this.Get(), method, lr,
Jeff Hao9a916d32013-06-27 18:45:37 -07001232 frame_id, interpreter_entry);
Ian Rogers62d6c772013-02-27 08:32:07 -08001233 stack->push_front(instrumentation_frame);
Ian Rogers62d6c772013-02-27 08:32:07 -08001234}
1235
Mingyao Yang2ee17902017-08-30 11:37:08 -07001236DeoptimizationMethodType Instrumentation::GetDeoptimizationMethodType(ArtMethod* method) {
1237 if (method->IsRuntimeMethod()) {
1238 // Certain methods have strict requirement on whether the dex instruction
1239 // should be re-executed upon deoptimization.
1240 if (method == Runtime::Current()->GetCalleeSaveMethod(
1241 CalleeSaveType::kSaveEverythingForClinit)) {
1242 return DeoptimizationMethodType::kKeepDexPc;
1243 }
1244 if (method == Runtime::Current()->GetCalleeSaveMethod(
1245 CalleeSaveType::kSaveEverythingForSuspendCheck)) {
1246 return DeoptimizationMethodType::kKeepDexPc;
1247 }
1248 }
1249 return DeoptimizationMethodType::kDefault;
1250}
1251
1252// Try to get the shorty of a runtime method if it's an invocation stub.
1253struct RuntimeMethodShortyVisitor : public StackVisitor {
1254 explicit RuntimeMethodShortyVisitor(Thread* thread)
1255 : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames),
1256 shorty('V') {}
1257
1258 bool VisitFrame() REQUIRES_SHARED(Locks::mutator_lock_) {
1259 ArtMethod* m = GetMethod();
1260 if (m != nullptr && !m->IsRuntimeMethod()) {
1261 // The first Java method.
1262 if (m->IsNative()) {
1263 // Use JNI method's shorty for the jni stub.
1264 shorty = m->GetShorty()[0];
1265 return false;
1266 }
1267 if (m->IsProxyMethod()) {
1268 // Proxy method just invokes its proxied method via
1269 // art_quick_proxy_invoke_handler.
1270 shorty = m->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty()[0];
1271 return false;
1272 }
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001273 const Instruction& instr = m->DexInstructions().InstructionAt(GetDexPc());
1274 if (instr.IsInvoke()) {
Mingyao Yang2ee17902017-08-30 11:37:08 -07001275 const DexFile* dex_file = m->GetDexFile();
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001276 if (interpreter::IsStringInit(dex_file, instr.VRegB())) {
Mingyao Yang2ee17902017-08-30 11:37:08 -07001277 // Invoking string init constructor is turned into invoking
1278 // StringFactory.newStringFromChars() which returns a string.
1279 shorty = 'L';
1280 return false;
1281 }
1282 // A regular invoke, use callee's shorty.
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001283 uint32_t method_idx = instr.VRegB();
Mingyao Yang2ee17902017-08-30 11:37:08 -07001284 shorty = dex_file->GetMethodShorty(method_idx)[0];
1285 }
1286 // Stop stack walking since we've seen a Java frame.
1287 return false;
1288 }
1289 return true;
1290 }
1291
1292 char shorty;
1293};
1294
Alex Lightb7edcda2017-04-27 13:20:31 -07001295TwoWordReturn Instrumentation::PopInstrumentationStackFrame(Thread* self,
1296 uintptr_t* return_pc,
1297 uint64_t* gpr_result,
1298 uint64_t* fpr_result) {
1299 DCHECK(gpr_result != nullptr);
1300 DCHECK(fpr_result != nullptr);
Ian Rogers62d6c772013-02-27 08:32:07 -08001301 // Do the pop.
1302 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
1303 CHECK_GT(stack->size(), 0U);
1304 InstrumentationStackFrame instrumentation_frame = stack->front();
1305 stack->pop_front();
1306
1307 // Set return PC and check the sanity of the stack.
1308 *return_pc = instrumentation_frame.return_pc_;
1309 CheckStackDepth(self, instrumentation_frame, 0);
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001310 self->VerifyStack();
Ian Rogers62d6c772013-02-27 08:32:07 -08001311
Mathieu Chartiere401d142015-04-22 13:56:20 -07001312 ArtMethod* method = instrumentation_frame.method_;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001313 uint32_t length;
Andreas Gampe542451c2016-07-26 09:02:02 -07001314 const PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Mingyao Yang2ee17902017-08-30 11:37:08 -07001315 char return_shorty;
1316
1317 // Runtime method does not call into MethodExitEvent() so there should not be
1318 // suspension point below.
1319 ScopedAssertNoThreadSuspension ants(__FUNCTION__, method->IsRuntimeMethod());
1320 if (method->IsRuntimeMethod()) {
1321 if (method != Runtime::Current()->GetCalleeSaveMethod(
1322 CalleeSaveType::kSaveEverythingForClinit)) {
1323 // If the caller is at an invocation point and the runtime method is not
1324 // for clinit, we need to pass return results to the caller.
1325 // We need the correct shorty to decide whether we need to pass the return
1326 // result for deoptimization below.
1327 RuntimeMethodShortyVisitor visitor(self);
1328 visitor.WalkStack();
1329 return_shorty = visitor.shorty;
1330 } else {
1331 // Some runtime methods such as allocations, unresolved field getters, etc.
1332 // have return value. We don't need to set return_value since MethodExitEvent()
1333 // below isn't called for runtime methods. Deoptimization doesn't need the
1334 // value either since the dex instruction will be re-executed by the
1335 // interpreter, except these two cases:
1336 // (1) For an invoke, which is handled above to get the correct shorty.
1337 // (2) For MONITOR_ENTER/EXIT, which cannot be re-executed since it's not
1338 // idempotent. However there is no return value for it anyway.
1339 return_shorty = 'V';
1340 }
1341 } else {
1342 return_shorty = method->GetInterfaceMethodIfProxy(pointer_size)->GetShorty(&length)[0];
1343 }
1344
Alex Lightb7edcda2017-04-27 13:20:31 -07001345 bool is_ref = return_shorty == '[' || return_shorty == 'L';
1346 StackHandleScope<1> hs(self);
1347 MutableHandle<mirror::Object> res(hs.NewHandle<mirror::Object>(nullptr));
Ian Rogers62d6c772013-02-27 08:32:07 -08001348 JValue return_value;
1349 if (return_shorty == 'V') {
1350 return_value.SetJ(0);
1351 } else if (return_shorty == 'F' || return_shorty == 'D') {
Alex Lightb7edcda2017-04-27 13:20:31 -07001352 return_value.SetJ(*fpr_result);
Ian Rogers62d6c772013-02-27 08:32:07 -08001353 } else {
Alex Lightb7edcda2017-04-27 13:20:31 -07001354 return_value.SetJ(*gpr_result);
1355 }
1356 if (is_ref) {
1357 // Take a handle to the return value so we won't lose it if we suspend.
1358 res.Assign(return_value.GetL());
Ian Rogers62d6c772013-02-27 08:32:07 -08001359 }
1360 // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to
1361 // return_pc.
Andreas Gampee2abbc62017-09-15 11:59:26 -07001362 uint32_t dex_pc = dex::kDexNoIndex;
Ian Rogers62d6c772013-02-27 08:32:07 -08001363 mirror::Object* this_object = instrumentation_frame.this_object_;
Mingyao Yang2ee17902017-08-30 11:37:08 -07001364 if (!method->IsRuntimeMethod() && !instrumentation_frame.interpreter_entry_) {
Sebastien Hertz320deb22014-06-11 19:45:05 +02001365 MethodExitEvent(self, this_object, instrumentation_frame.method_, dex_pc, return_value);
1366 }
jeffhao725a9572012-11-13 18:20:12 -08001367
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01001368 // Deoptimize if the caller needs to continue execution in the interpreter. Do nothing if we get
1369 // back to an upcall.
1370 NthCallerVisitor visitor(self, 1, true);
1371 visitor.WalkStack(true);
Sebastien Hertz270a0e12015-01-16 19:49:09 +01001372 bool deoptimize = (visitor.caller != nullptr) &&
Daniel Mihalyieb076692014-08-22 17:33:31 +02001373 (interpreter_stubs_installed_ || IsDeoptimized(visitor.caller) ||
1374 Dbg::IsForcedInterpreterNeededForUpcall(self, visitor.caller));
Alex Lightb7edcda2017-04-27 13:20:31 -07001375 if (is_ref) {
1376 // Restore the return value if it's a reference since it might have moved.
1377 *reinterpret_cast<mirror::Object**>(gpr_result) = res.Get();
1378 }
Nicolas Geoffraya0619e22016-12-20 13:57:43 +00001379 if (deoptimize && Runtime::Current()->IsAsyncDeoptimizeable(*return_pc)) {
Ian Rogers62d6c772013-02-27 08:32:07 -08001380 if (kVerboseInstrumentation) {
Andreas Gampe46ee31b2016-12-14 10:11:49 -08001381 LOG(INFO) << "Deoptimizing "
1382 << visitor.caller->PrettyMethod()
1383 << " by returning from "
1384 << method->PrettyMethod()
1385 << " with result "
1386 << std::hex << return_value.GetJ() << std::dec
1387 << " in "
1388 << *self;
Ian Rogers62d6c772013-02-27 08:32:07 -08001389 }
Mingyao Yang2ee17902017-08-30 11:37:08 -07001390 DeoptimizationMethodType deopt_method_type = GetDeoptimizationMethodType(method);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01001391 self->PushDeoptimizationContext(return_value,
Mingyao Yang2ee17902017-08-30 11:37:08 -07001392 return_shorty == 'L' || return_shorty == '[',
1393 nullptr /* no pending exception */,
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01001394 false /* from_code */,
Mingyao Yang2ee17902017-08-30 11:37:08 -07001395 deopt_method_type);
Andreas Gamped58342c2014-06-05 14:18:08 -07001396 return GetTwoWordSuccessValue(*return_pc,
1397 reinterpret_cast<uintptr_t>(GetQuickDeoptimizationEntryPoint()));
Ian Rogers62d6c772013-02-27 08:32:07 -08001398 } else {
Nicolas Geoffraya0619e22016-12-20 13:57:43 +00001399 if (deoptimize && !Runtime::Current()->IsAsyncDeoptimizeable(*return_pc)) {
Alex Lightd8eb6732018-01-29 15:16:02 -08001400 VLOG(deopt) << "Got a deoptimization request on un-deoptimizable " << method->PrettyMethod()
1401 << " at PC " << reinterpret_cast<void*>(*return_pc);
Nicolas Geoffraya0619e22016-12-20 13:57:43 +00001402 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001403 if (kVerboseInstrumentation) {
David Sehr709b0702016-10-13 09:12:37 -07001404 LOG(INFO) << "Returning from " << method->PrettyMethod()
Brian Carlstrom2d888622013-07-18 17:02:00 -07001405 << " to PC " << reinterpret_cast<void*>(*return_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -08001406 }
Andreas Gamped58342c2014-06-05 14:18:08 -07001407 return GetTwoWordSuccessValue(0, *return_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -08001408 }
jeffhao725a9572012-11-13 18:20:12 -08001409}
1410
Mingyao Yangf711f2c2016-05-23 12:29:39 -07001411uintptr_t Instrumentation::PopMethodForUnwind(Thread* self, bool is_deoptimization) const {
Ian Rogers62d6c772013-02-27 08:32:07 -08001412 // Do the pop.
1413 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
1414 CHECK_GT(stack->size(), 0U);
Alex Lightb7edcda2017-04-27 13:20:31 -07001415 size_t idx = stack->size();
Ian Rogers62d6c772013-02-27 08:32:07 -08001416 InstrumentationStackFrame instrumentation_frame = stack->front();
Ian Rogers62d6c772013-02-27 08:32:07 -08001417
Mathieu Chartiere401d142015-04-22 13:56:20 -07001418 ArtMethod* method = instrumentation_frame.method_;
Ian Rogers62d6c772013-02-27 08:32:07 -08001419 if (is_deoptimization) {
1420 if (kVerboseInstrumentation) {
David Sehr709b0702016-10-13 09:12:37 -07001421 LOG(INFO) << "Popping for deoptimization " << ArtMethod::PrettyMethod(method);
Ian Rogers62d6c772013-02-27 08:32:07 -08001422 }
1423 } else {
1424 if (kVerboseInstrumentation) {
David Sehr709b0702016-10-13 09:12:37 -07001425 LOG(INFO) << "Popping for unwind " << ArtMethod::PrettyMethod(method);
Ian Rogers62d6c772013-02-27 08:32:07 -08001426 }
1427
1428 // Notify listeners of method unwind.
1429 // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to
1430 // return_pc.
Andreas Gampee2abbc62017-09-15 11:59:26 -07001431 uint32_t dex_pc = dex::kDexNoIndex;
Mingyao Yang2ee17902017-08-30 11:37:08 -07001432 if (!method->IsRuntimeMethod()) {
1433 MethodUnwindEvent(self, instrumentation_frame.this_object_, method, dex_pc);
1434 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001435 }
Alex Lightb7edcda2017-04-27 13:20:31 -07001436 // TODO: bring back CheckStackDepth(self, instrumentation_frame, 2);
1437 CHECK_EQ(stack->size(), idx);
1438 DCHECK(instrumentation_frame.method_ == stack->front().method_);
1439 stack->pop_front();
Mingyao Yangf711f2c2016-05-23 12:29:39 -07001440 return instrumentation_frame.return_pc_;
Ian Rogers62d6c772013-02-27 08:32:07 -08001441}
1442
1443std::string InstrumentationStackFrame::Dump() const {
1444 std::ostringstream os;
David Sehr709b0702016-10-13 09:12:37 -07001445 os << "Frame " << frame_id_ << " " << ArtMethod::PrettyMethod(method_) << ":"
Ian Rogers62d6c772013-02-27 08:32:07 -08001446 << reinterpret_cast<void*>(return_pc_) << " this=" << reinterpret_cast<void*>(this_object_);
1447 return os.str();
1448}
1449
1450} // namespace instrumentation
jeffhao725a9572012-11-13 18:20:12 -08001451} // namespace art