blob: 7ddd17329c3866726eeae9523d82a3b0701704c9 [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"
David Sehrc431b9d2018-03-02 12:01:51 -080024#include "base/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),
Alex Light055407c2018-02-26 13:44:04 -0800212 reached_existing_instrumentation_frames_(false),
213 should_be_at_top_(false),
214 instrumentation_stack_depth_(0),
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100215 last_return_pc_(0) {
216 }
jeffhao725a9572012-11-13 18:20:12 -0800217
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700218 bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700219 ArtMethod* m = GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700220 if (m == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800221 if (kVerboseInstrumentation) {
222 LOG(INFO) << " Skipping upcall. Frame " << GetFrameId();
223 }
224 last_return_pc_ = 0;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700225 return true; // Ignore upcalls.
Ian Rogers306057f2012-11-26 12:45:53 -0800226 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700227 if (GetCurrentQuickFrame() == nullptr) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800228 bool interpreter_frame = true;
Sebastien Hertz320deb22014-06-11 19:45:05 +0200229 InstrumentationStackFrame instrumentation_frame(GetThisObject(), m, 0, GetFrameId(),
230 interpreter_frame);
Jeff Haoa15a81b2014-05-27 18:25:47 -0700231 if (kVerboseInstrumentation) {
232 LOG(INFO) << "Pushing shadow frame " << instrumentation_frame.Dump();
233 }
234 shadow_stack_.push_back(instrumentation_frame);
235 return true; // Continue.
236 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800237 uintptr_t return_pc = GetReturnPc();
Alex Light055407c2018-02-26 13:44:04 -0800238 if (UNLIKELY(should_be_at_top_)) {
239 std::string thread_name;
240 GetThread()->GetThreadName(thread_name);
241 uint32_t dex_pc = dex::kDexNoIndex;
242 if (last_return_pc_ != 0 &&
243 GetCurrentOatQuickMethodHeader() != nullptr) {
244 dex_pc = GetCurrentOatQuickMethodHeader()->ToDexPc(m, last_return_pc_);
245 }
246 LOG(FATAL) << "While walking " << thread_name << " Reached unexpected frame above what "
247 << "should have been top. Method is " << GetMethod()->PrettyMethod()
248 << " return_pc: " << std::hex << return_pc
249 << " dex pc: " << dex_pc;
250 UNREACHABLE();
251 }
Sebastien Hertz320deb22014-06-11 19:45:05 +0200252 if (kVerboseInstrumentation) {
253 LOG(INFO) << " Installing exit stub in " << DescribeLocation();
254 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100255 if (return_pc == instrumentation_exit_pc_) {
Mingyao Yang2ee17902017-08-30 11:37:08 -0700256 CHECK_LT(instrumentation_stack_depth_, instrumentation_stack_->size());
257
258 if (m->IsRuntimeMethod()) {
259 const InstrumentationStackFrame& frame =
260 instrumentation_stack_->at(instrumentation_stack_depth_);
261 if (frame.interpreter_entry_) {
262 // This instrumentation frame is for an interpreter bridge and is
263 // pushed when executing the instrumented interpreter bridge. So method
264 // enter event must have been reported. However we need to push a DEX pc
265 // into the dex_pcs_ list to match size of instrumentation stack.
Andreas Gampee2abbc62017-09-15 11:59:26 -0700266 uint32_t dex_pc = dex::kDexNoIndex;
Mingyao Yang2ee17902017-08-30 11:37:08 -0700267 dex_pcs_.push_back(dex_pc);
268 last_return_pc_ = frame.return_pc_;
269 ++instrumentation_stack_depth_;
270 return true;
271 }
272 }
273
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100274 // We've reached a frame which has already been installed with instrumentation exit stub.
275 // We should have already installed instrumentation on previous frames.
276 reached_existing_instrumentation_frames_ = true;
277
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200278 const InstrumentationStackFrame& frame =
279 instrumentation_stack_->at(instrumentation_stack_depth_);
David Sehr709b0702016-10-13 09:12:37 -0700280 CHECK_EQ(m, frame.method_) << "Expected " << ArtMethod::PrettyMethod(m)
281 << ", Found " << ArtMethod::PrettyMethod(frame.method_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100282 return_pc = frame.return_pc_;
283 if (kVerboseInstrumentation) {
284 LOG(INFO) << "Ignoring already instrumented " << frame.Dump();
285 }
Alex Light055407c2018-02-26 13:44:04 -0800286 } else if (UNLIKELY(reached_existing_instrumentation_frames_)) {
287 // If tracing was enabled we might have had all methods have the instrumentation frame
288 // except the runtime transition method at the very top of the stack. This isn't really a
289 // problem since the transition method just goes back into the runtime and never leaves it
290 // so it can be ignored.
291 should_be_at_top_ = true;
292 DCHECK(m->IsRuntimeMethod()) << "Expected method to be runtime method at start of thread "
293 << "but was " << m->PrettyMethod();
294 if (kVerboseInstrumentation) {
295 LOG(INFO) << "reached expected top frame " << m->PrettyMethod();
296 }
297 // Don't bother continuing on the upcalls on non-debug builds.
298 return kIsDebugBuild ? true : false;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100299 } else {
300 CHECK_NE(return_pc, 0U);
Mingyao Yang2ee17902017-08-30 11:37:08 -0700301 InstrumentationStackFrame instrumentation_frame(
302 m->IsRuntimeMethod() ? nullptr : GetThisObject(),
303 m,
304 return_pc,
305 GetFrameId(), // A runtime method still gets a frame id.
306 false);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100307 if (kVerboseInstrumentation) {
308 LOG(INFO) << "Pushing frame " << instrumentation_frame.Dump();
309 }
310
Sebastien Hertz320deb22014-06-11 19:45:05 +0200311 // Insert frame at the right position so we do not corrupt the instrumentation stack.
312 // Instrumentation stack frames are in descending frame id order.
313 auto it = instrumentation_stack_->begin();
314 for (auto end = instrumentation_stack_->end(); it != end; ++it) {
315 const InstrumentationStackFrame& current = *it;
316 if (instrumentation_frame.frame_id_ >= current.frame_id_) {
317 break;
318 }
319 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100320 instrumentation_stack_->insert(it, instrumentation_frame);
321 SetReturnPc(instrumentation_exit_pc_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800322 }
Andreas Gampee2abbc62017-09-15 11:59:26 -0700323 uint32_t dex_pc = dex::kDexNoIndex;
Mingyao Yang2ee17902017-08-30 11:37:08 -0700324 if (last_return_pc_ != 0 &&
325 GetCurrentOatQuickMethodHeader() != nullptr) {
326 dex_pc = GetCurrentOatQuickMethodHeader()->ToDexPc(m, last_return_pc_);
327 }
328 dex_pcs_.push_back(dex_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800329 last_return_pc_ = return_pc;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100330 ++instrumentation_stack_depth_;
Ian Rogers306057f2012-11-26 12:45:53 -0800331 return true; // Continue.
332 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800333 std::deque<InstrumentationStackFrame>* const instrumentation_stack_;
Jeff Haoa15a81b2014-05-27 18:25:47 -0700334 std::vector<InstrumentationStackFrame> shadow_stack_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800335 std::vector<uint32_t> dex_pcs_;
Ian Rogers306057f2012-11-26 12:45:53 -0800336 const uintptr_t instrumentation_exit_pc_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100337 bool reached_existing_instrumentation_frames_;
Alex Light055407c2018-02-26 13:44:04 -0800338 bool should_be_at_top_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100339 size_t instrumentation_stack_depth_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800340 uintptr_t last_return_pc_;
Ian Rogers306057f2012-11-26 12:45:53 -0800341 };
Ian Rogers62d6c772013-02-27 08:32:07 -0800342 if (kVerboseInstrumentation) {
343 std::string thread_name;
344 thread->GetThreadName(thread_name);
345 LOG(INFO) << "Installing exit stubs in " << thread_name;
Ian Rogers306057f2012-11-26 12:45:53 -0800346 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100347
348 Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg);
Ian Rogers700a4022014-05-19 16:49:03 -0700349 std::unique_ptr<Context> context(Context::Create());
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700350 uintptr_t instrumentation_exit_pc = reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc());
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100351 InstallStackVisitor visitor(thread, context.get(), instrumentation_exit_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800352 visitor.WalkStack(true);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100353 CHECK_EQ(visitor.dex_pcs_.size(), thread->GetInstrumentationStack()->size());
Ian Rogers62d6c772013-02-27 08:32:07 -0800354
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100355 if (instrumentation->ShouldNotifyMethodEnterExitEvents()) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100356 // Create method enter events for all methods currently on the thread's stack. We only do this
357 // if no debugger is attached to prevent from posting events twice.
Jeff Haoa15a81b2014-05-27 18:25:47 -0700358 auto ssi = visitor.shadow_stack_.rbegin();
359 for (auto isi = thread->GetInstrumentationStack()->rbegin(),
360 end = thread->GetInstrumentationStack()->rend(); isi != end; ++isi) {
361 while (ssi != visitor.shadow_stack_.rend() && (*ssi).frame_id_ < (*isi).frame_id_) {
362 instrumentation->MethodEnterEvent(thread, (*ssi).this_object_, (*ssi).method_, 0);
363 ++ssi;
364 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100365 uint32_t dex_pc = visitor.dex_pcs_.back();
366 visitor.dex_pcs_.pop_back();
Sebastien Hertz320deb22014-06-11 19:45:05 +0200367 if (!isi->interpreter_entry_) {
368 instrumentation->MethodEnterEvent(thread, (*isi).this_object_, (*isi).method_, dex_pc);
369 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100370 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800371 }
372 thread->VerifyStack();
Ian Rogers306057f2012-11-26 12:45:53 -0800373}
374
Mingyao Yang99170c62015-07-06 11:10:37 -0700375void Instrumentation::InstrumentThreadStack(Thread* thread) {
376 instrumentation_stubs_installed_ = true;
377 InstrumentationInstallStack(thread, this);
378}
379
Ian Rogers62d6c772013-02-27 08:32:07 -0800380// Removes the instrumentation exit pc as the return PC for every quick frame.
381static void InstrumentationRestoreStack(Thread* thread, void* arg)
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +0000382 REQUIRES(Locks::mutator_lock_) {
383 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
384
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200385 struct RestoreStackVisitor FINAL : public StackVisitor {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800386 RestoreStackVisitor(Thread* thread_in, uintptr_t instrumentation_exit_pc,
Ian Rogers62d6c772013-02-27 08:32:07 -0800387 Instrumentation* instrumentation)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100388 : StackVisitor(thread_in, nullptr, kInstrumentationStackWalk),
389 thread_(thread_in),
Ian Rogers62d6c772013-02-27 08:32:07 -0800390 instrumentation_exit_pc_(instrumentation_exit_pc),
391 instrumentation_(instrumentation),
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800392 instrumentation_stack_(thread_in->GetInstrumentationStack()),
Ian Rogers62d6c772013-02-27 08:32:07 -0800393 frames_removed_(0) {}
Ian Rogers306057f2012-11-26 12:45:53 -0800394
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700395 bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800396 if (instrumentation_stack_->size() == 0) {
jeffhao725a9572012-11-13 18:20:12 -0800397 return false; // Stop.
398 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700399 ArtMethod* m = GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700400 if (GetCurrentQuickFrame() == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800401 if (kVerboseInstrumentation) {
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200402 LOG(INFO) << " Ignoring a shadow frame. Frame " << GetFrameId()
David Sehr709b0702016-10-13 09:12:37 -0700403 << " Method=" << ArtMethod::PrettyMethod(m);
Ian Rogers62d6c772013-02-27 08:32:07 -0800404 }
405 return true; // Ignore shadow frames.
406 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700407 if (m == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800408 if (kVerboseInstrumentation) {
409 LOG(INFO) << " Skipping upcall. Frame " << GetFrameId();
410 }
Ian Rogers306057f2012-11-26 12:45:53 -0800411 return true; // Ignore upcalls.
412 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800413 bool removed_stub = false;
414 // TODO: make this search more efficient?
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100415 const size_t frameId = GetFrameId();
416 for (const InstrumentationStackFrame& instrumentation_frame : *instrumentation_stack_) {
417 if (instrumentation_frame.frame_id_ == frameId) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800418 if (kVerboseInstrumentation) {
419 LOG(INFO) << " Removing exit stub in " << DescribeLocation();
420 }
Jeff Hao9a916d32013-06-27 18:45:37 -0700421 if (instrumentation_frame.interpreter_entry_) {
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700422 CHECK(m == Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs));
Jeff Hao9a916d32013-06-27 18:45:37 -0700423 } else {
David Sehr709b0702016-10-13 09:12:37 -0700424 CHECK(m == instrumentation_frame.method_) << ArtMethod::PrettyMethod(m);
Jeff Hao9a916d32013-06-27 18:45:37 -0700425 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800426 SetReturnPc(instrumentation_frame.return_pc_);
Mingyao Yang2ee17902017-08-30 11:37:08 -0700427 if (instrumentation_->ShouldNotifyMethodEnterExitEvents() &&
428 !m->IsRuntimeMethod()) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100429 // Create the method exit events. As the methods didn't really exit the result is 0.
430 // We only do this if no debugger is attached to prevent from posting events twice.
431 instrumentation_->MethodExitEvent(thread_, instrumentation_frame.this_object_, m,
432 GetDexPc(), JValue());
433 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800434 frames_removed_++;
435 removed_stub = true;
436 break;
437 }
438 }
439 if (!removed_stub) {
440 if (kVerboseInstrumentation) {
441 LOG(INFO) << " No exit stub in " << DescribeLocation();
Ian Rogers306057f2012-11-26 12:45:53 -0800442 }
jeffhao725a9572012-11-13 18:20:12 -0800443 }
444 return true; // Continue.
445 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800446 Thread* const thread_;
Ian Rogers306057f2012-11-26 12:45:53 -0800447 const uintptr_t instrumentation_exit_pc_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800448 Instrumentation* const instrumentation_;
449 std::deque<instrumentation::InstrumentationStackFrame>* const instrumentation_stack_;
450 size_t frames_removed_;
jeffhao725a9572012-11-13 18:20:12 -0800451 };
Ian Rogers62d6c772013-02-27 08:32:07 -0800452 if (kVerboseInstrumentation) {
453 std::string thread_name;
454 thread->GetThreadName(thread_name);
455 LOG(INFO) << "Removing exit stubs in " << thread_name;
456 }
457 std::deque<instrumentation::InstrumentationStackFrame>* stack = thread->GetInstrumentationStack();
458 if (stack->size() > 0) {
459 Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700460 uintptr_t instrumentation_exit_pc =
461 reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc());
Ian Rogers62d6c772013-02-27 08:32:07 -0800462 RestoreStackVisitor visitor(thread, instrumentation_exit_pc, instrumentation);
463 visitor.WalkStack(true);
464 CHECK_EQ(visitor.frames_removed_, stack->size());
465 while (stack->size() > 0) {
466 stack->pop_front();
467 }
jeffhao725a9572012-11-13 18:20:12 -0800468 }
469}
470
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200471static bool HasEvent(Instrumentation::InstrumentationEvent expected, uint32_t events) {
472 return (events & expected) != 0;
473}
474
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000475static void PotentiallyAddListenerTo(Instrumentation::InstrumentationEvent event,
476 uint32_t events,
477 std::list<InstrumentationListener*>& list,
478 InstrumentationListener* listener,
479 bool* has_listener)
480 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_) {
481 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
482 if (!HasEvent(event, events)) {
483 return;
484 }
485 // If there is a free slot in the list, we insert the listener in that slot.
486 // Otherwise we add it to the end of the list.
487 auto it = std::find(list.begin(), list.end(), nullptr);
488 if (it != list.end()) {
489 *it = listener;
490 } else {
491 list.push_back(listener);
492 }
493 *has_listener = true;
494}
495
Ian Rogers62d6c772013-02-27 08:32:07 -0800496void Instrumentation::AddListener(InstrumentationListener* listener, uint32_t events) {
497 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000498 PotentiallyAddListenerTo(kMethodEntered,
499 events,
500 method_entry_listeners_,
501 listener,
502 &have_method_entry_listeners_);
503 PotentiallyAddListenerTo(kMethodExited,
504 events,
505 method_exit_listeners_,
506 listener,
507 &have_method_exit_listeners_);
508 PotentiallyAddListenerTo(kMethodUnwind,
509 events,
510 method_unwind_listeners_,
511 listener,
512 &have_method_unwind_listeners_);
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000513 PotentiallyAddListenerTo(kBranch,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000514 events,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000515 branch_listeners_,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000516 listener,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000517 &have_branch_listeners_);
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000518 PotentiallyAddListenerTo(kInvokeVirtualOrInterface,
519 events,
520 invoke_virtual_or_interface_listeners_,
521 listener,
522 &have_invoke_virtual_or_interface_listeners_);
523 PotentiallyAddListenerTo(kDexPcMoved,
524 events,
525 dex_pc_listeners_,
526 listener,
527 &have_dex_pc_listeners_);
528 PotentiallyAddListenerTo(kFieldRead,
529 events,
530 field_read_listeners_,
531 listener,
532 &have_field_read_listeners_);
533 PotentiallyAddListenerTo(kFieldWritten,
534 events,
535 field_write_listeners_,
536 listener,
537 &have_field_write_listeners_);
Alex Light6e1607e2017-08-23 10:06:18 -0700538 PotentiallyAddListenerTo(kExceptionThrown,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000539 events,
Alex Light6e1607e2017-08-23 10:06:18 -0700540 exception_thrown_listeners_,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000541 listener,
Alex Light6e1607e2017-08-23 10:06:18 -0700542 &have_exception_thrown_listeners_);
Alex Lighte814f9d2017-07-31 16:14:39 -0700543 PotentiallyAddListenerTo(kWatchedFramePop,
544 events,
545 watched_frame_pop_listeners_,
546 listener,
547 &have_watched_frame_pop_listeners_);
Alex Light9fb1ab12017-09-05 09:32:49 -0700548 PotentiallyAddListenerTo(kExceptionHandled,
549 events,
550 exception_handled_listeners_,
551 listener,
552 &have_exception_handled_listeners_);
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200553 UpdateInterpreterHandlerTable();
jeffhao725a9572012-11-13 18:20:12 -0800554}
555
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000556static void PotentiallyRemoveListenerFrom(Instrumentation::InstrumentationEvent event,
557 uint32_t events,
558 std::list<InstrumentationListener*>& list,
559 InstrumentationListener* listener,
560 bool* has_listener)
561 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_) {
562 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
563 if (!HasEvent(event, events)) {
564 return;
565 }
566 auto it = std::find(list.begin(), list.end(), listener);
567 if (it != list.end()) {
568 // Just update the entry, do not remove from the list. Removing entries in the list
569 // is unsafe when mutators are iterating over it.
570 *it = nullptr;
571 }
572
573 // Check if the list contains any non-null listener, and update 'has_listener'.
574 for (InstrumentationListener* l : list) {
575 if (l != nullptr) {
576 *has_listener = true;
577 return;
578 }
579 }
580 *has_listener = false;
581}
582
Ian Rogers62d6c772013-02-27 08:32:07 -0800583void Instrumentation::RemoveListener(InstrumentationListener* listener, uint32_t events) {
584 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000585 PotentiallyRemoveListenerFrom(kMethodEntered,
586 events,
587 method_entry_listeners_,
588 listener,
589 &have_method_entry_listeners_);
590 PotentiallyRemoveListenerFrom(kMethodExited,
591 events,
592 method_exit_listeners_,
593 listener,
594 &have_method_exit_listeners_);
595 PotentiallyRemoveListenerFrom(kMethodUnwind,
596 events,
597 method_unwind_listeners_,
598 listener,
599 &have_method_unwind_listeners_);
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000600 PotentiallyRemoveListenerFrom(kBranch,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000601 events,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000602 branch_listeners_,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000603 listener,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000604 &have_branch_listeners_);
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000605 PotentiallyRemoveListenerFrom(kInvokeVirtualOrInterface,
606 events,
607 invoke_virtual_or_interface_listeners_,
608 listener,
609 &have_invoke_virtual_or_interface_listeners_);
610 PotentiallyRemoveListenerFrom(kDexPcMoved,
611 events,
612 dex_pc_listeners_,
613 listener,
614 &have_dex_pc_listeners_);
615 PotentiallyRemoveListenerFrom(kFieldRead,
616 events,
617 field_read_listeners_,
618 listener,
619 &have_field_read_listeners_);
620 PotentiallyRemoveListenerFrom(kFieldWritten,
621 events,
622 field_write_listeners_,
623 listener,
624 &have_field_write_listeners_);
Alex Light6e1607e2017-08-23 10:06:18 -0700625 PotentiallyRemoveListenerFrom(kExceptionThrown,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000626 events,
Alex Light6e1607e2017-08-23 10:06:18 -0700627 exception_thrown_listeners_,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000628 listener,
Alex Light6e1607e2017-08-23 10:06:18 -0700629 &have_exception_thrown_listeners_);
Alex Lighte814f9d2017-07-31 16:14:39 -0700630 PotentiallyRemoveListenerFrom(kWatchedFramePop,
631 events,
632 watched_frame_pop_listeners_,
633 listener,
634 &have_watched_frame_pop_listeners_);
Alex Light9fb1ab12017-09-05 09:32:49 -0700635 PotentiallyRemoveListenerFrom(kExceptionHandled,
636 events,
637 exception_handled_listeners_,
638 listener,
639 &have_exception_handled_listeners_);
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200640 UpdateInterpreterHandlerTable();
jeffhao725a9572012-11-13 18:20:12 -0800641}
642
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200643Instrumentation::InstrumentationLevel Instrumentation::GetCurrentInstrumentationLevel() const {
Alex Light4ba388a2017-01-27 10:26:49 -0800644 if (interpreter_stubs_installed_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200645 return InstrumentationLevel::kInstrumentWithInterpreter;
Ian Rogers62d6c772013-02-27 08:32:07 -0800646 } else if (entry_exit_stubs_installed_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200647 return InstrumentationLevel::kInstrumentWithInstrumentationStubs;
Ian Rogers62d6c772013-02-27 08:32:07 -0800648 } else {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200649 return InstrumentationLevel::kInstrumentNothing;
Ian Rogers62d6c772013-02-27 08:32:07 -0800650 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200651}
652
Alex Lightdba61482016-12-21 08:20:29 -0800653bool Instrumentation::RequiresInstrumentationInstallation(InstrumentationLevel new_level) const {
Alex Light4ba388a2017-01-27 10:26:49 -0800654 // We need to reinstall instrumentation if we go to a different level.
655 return GetCurrentInstrumentationLevel() != new_level;
Alex Lightdba61482016-12-21 08:20:29 -0800656}
657
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200658void Instrumentation::ConfigureStubs(const char* key, InstrumentationLevel desired_level) {
659 // Store the instrumentation level for this key or remove it.
660 if (desired_level == InstrumentationLevel::kInstrumentNothing) {
661 // The client no longer needs instrumentation.
662 requested_instrumentation_levels_.erase(key);
663 } else {
664 // The client needs instrumentation.
665 requested_instrumentation_levels_.Overwrite(key, desired_level);
666 }
667
668 // Look for the highest required instrumentation level.
669 InstrumentationLevel requested_level = InstrumentationLevel::kInstrumentNothing;
670 for (const auto& v : requested_instrumentation_levels_) {
671 requested_level = std::max(requested_level, v.second);
672 }
673
674 interpret_only_ = (requested_level == InstrumentationLevel::kInstrumentWithInterpreter) ||
675 forced_interpret_only_;
676
Alex Lightdba61482016-12-21 08:20:29 -0800677 if (!RequiresInstrumentationInstallation(requested_level)) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800678 // We're already set.
679 return;
680 }
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100681 Thread* const self = Thread::Current();
Ian Rogers62d6c772013-02-27 08:32:07 -0800682 Runtime* runtime = Runtime::Current();
Sebastien Hertza8a697f2015-01-15 12:28:47 +0100683 Locks::mutator_lock_->AssertExclusiveHeld(self);
Ian Rogers62d6c772013-02-27 08:32:07 -0800684 Locks::thread_list_lock_->AssertNotHeld(self);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200685 if (requested_level > InstrumentationLevel::kInstrumentNothing) {
Alex Light4ba388a2017-01-27 10:26:49 -0800686 if (requested_level == InstrumentationLevel::kInstrumentWithInterpreter) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800687 interpreter_stubs_installed_ = true;
Ian Rogers62d6c772013-02-27 08:32:07 -0800688 entry_exit_stubs_installed_ = true;
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200689 } else {
690 CHECK_EQ(requested_level, InstrumentationLevel::kInstrumentWithInstrumentationStubs);
691 entry_exit_stubs_installed_ = true;
692 interpreter_stubs_installed_ = false;
Ian Rogers62d6c772013-02-27 08:32:07 -0800693 }
Mathieu Chartiere0671ce2015-07-28 17:23:28 -0700694 InstallStubsClassVisitor visitor(this);
695 runtime->GetClassLinker()->VisitClasses(&visitor);
Ian Rogers62d6c772013-02-27 08:32:07 -0800696 instrumentation_stubs_installed_ = true;
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100697 MutexLock mu(self, *Locks::thread_list_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800698 runtime->GetThreadList()->ForEach(InstrumentationInstallStack, this);
699 } else {
700 interpreter_stubs_installed_ = false;
701 entry_exit_stubs_installed_ = false;
Mathieu Chartiere0671ce2015-07-28 17:23:28 -0700702 InstallStubsClassVisitor visitor(this);
703 runtime->GetClassLinker()->VisitClasses(&visitor);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100704 // Restore stack only if there is no method currently deoptimized.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700705 bool empty;
706 {
707 ReaderMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700708 empty = IsDeoptimizedMethodsEmpty(); // Avoid lock violation.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700709 }
710 if (empty) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100711 MutexLock mu(self, *Locks::thread_list_lock_);
712 Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this);
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +0000713 // Only do this after restoring, as walking the stack when restoring will see
714 // the instrumentation exit pc.
715 instrumentation_stubs_installed_ = false;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100716 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800717 }
jeffhao725a9572012-11-13 18:20:12 -0800718}
719
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200720static void ResetQuickAllocEntryPointsForThread(Thread* thread, void* arg ATTRIBUTE_UNUSED) {
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800721 thread->ResetQuickAllocEntryPointsForThread(kUseReadBarrier && thread->GetIsGcMarking());
Ian Rogersfa824272013-11-05 16:12:57 -0800722}
723
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700724void Instrumentation::SetEntrypointsInstrumented(bool instrumented) {
725 Thread* self = Thread::Current();
Mathieu Chartier661974a2014-01-09 11:23:53 -0800726 Runtime* runtime = Runtime::Current();
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700727 Locks::mutator_lock_->AssertNotHeld(self);
728 Locks::instrument_entrypoints_lock_->AssertHeld(self);
729 if (runtime->IsStarted()) {
Mathieu Chartier4f55e222015-09-04 13:26:21 -0700730 ScopedSuspendAll ssa(__FUNCTION__);
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700731 MutexLock mu(self, *Locks::runtime_shutdown_lock_);
Mathieu Chartier661974a2014-01-09 11:23:53 -0800732 SetQuickAllocEntryPointsInstrumented(instrumented);
733 ResetQuickAllocEntryPoints();
Mathieu Chartier50e93312016-03-16 11:25:29 -0700734 alloc_entrypoints_instrumented_ = instrumented;
Mathieu Chartier4f55e222015-09-04 13:26:21 -0700735 } else {
736 MutexLock mu(self, *Locks::runtime_shutdown_lock_);
737 SetQuickAllocEntryPointsInstrumented(instrumented);
Andreas Gampe157c77e2016-10-17 17:44:41 -0700738
739 // Note: ResetQuickAllocEntryPoints only works when the runtime is started. Manually run the
740 // update for just this thread.
Andreas Gampe162ae502016-10-18 10:03:42 -0700741 // Note: self may be null. One of those paths is setting instrumentation in the Heap
742 // constructor for gcstress mode.
743 if (self != nullptr) {
744 ResetQuickAllocEntryPointsForThread(self, nullptr);
745 }
Andreas Gampe157c77e2016-10-17 17:44:41 -0700746
Mathieu Chartier50e93312016-03-16 11:25:29 -0700747 alloc_entrypoints_instrumented_ = instrumented;
Mathieu Chartier661974a2014-01-09 11:23:53 -0800748 }
749}
750
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700751void Instrumentation::InstrumentQuickAllocEntryPoints() {
752 MutexLock mu(Thread::Current(), *Locks::instrument_entrypoints_lock_);
753 InstrumentQuickAllocEntryPointsLocked();
Ian Rogersfa824272013-11-05 16:12:57 -0800754}
755
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700756void Instrumentation::UninstrumentQuickAllocEntryPoints() {
757 MutexLock mu(Thread::Current(), *Locks::instrument_entrypoints_lock_);
758 UninstrumentQuickAllocEntryPointsLocked();
759}
760
761void Instrumentation::InstrumentQuickAllocEntryPointsLocked() {
762 Locks::instrument_entrypoints_lock_->AssertHeld(Thread::Current());
763 if (quick_alloc_entry_points_instrumentation_counter_ == 0) {
764 SetEntrypointsInstrumented(true);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800765 }
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700766 ++quick_alloc_entry_points_instrumentation_counter_;
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700767}
768
769void Instrumentation::UninstrumentQuickAllocEntryPointsLocked() {
770 Locks::instrument_entrypoints_lock_->AssertHeld(Thread::Current());
771 CHECK_GT(quick_alloc_entry_points_instrumentation_counter_, 0U);
772 --quick_alloc_entry_points_instrumentation_counter_;
773 if (quick_alloc_entry_points_instrumentation_counter_ == 0) {
774 SetEntrypointsInstrumented(false);
775 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800776}
777
778void Instrumentation::ResetQuickAllocEntryPoints() {
779 Runtime* runtime = Runtime::Current();
780 if (runtime->IsStarted()) {
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800781 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700782 runtime->GetThreadList()->ForEach(ResetQuickAllocEntryPointsForThread, nullptr);
Ian Rogersfa824272013-11-05 16:12:57 -0800783 }
784}
785
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700786void Instrumentation::UpdateMethodsCodeImpl(ArtMethod* method, const void* quick_code) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800787 const void* new_quick_code;
Ian Rogers62d6c772013-02-27 08:32:07 -0800788 if (LIKELY(!instrumentation_stubs_installed_)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800789 new_quick_code = quick_code;
Jeff Hao65d15d92013-07-16 16:39:33 -0700790 } else {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100791 if ((interpreter_stubs_installed_ || IsDeoptimized(method)) && !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800792 new_quick_code = GetQuickToInterpreterBridge();
Jeff Hao65d15d92013-07-16 16:39:33 -0700793 } else {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700794 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700795 if (class_linker->IsQuickResolutionStub(quick_code) ||
796 class_linker->IsQuickToInterpreterBridge(quick_code)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700797 new_quick_code = quick_code;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700798 } else if (entry_exit_stubs_installed_) {
799 new_quick_code = GetQuickInstrumentationEntryPoint();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700800 } else {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700801 new_quick_code = quick_code;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700802 }
Jeff Hao65d15d92013-07-16 16:39:33 -0700803 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800804 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800805 UpdateEntrypoints(method, new_quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100806}
807
Nicolas Geoffraya6e0e7d2018-01-26 13:16:50 +0000808void Instrumentation::UpdateNativeMethodsCodeToJitCode(ArtMethod* method, const void* quick_code) {
809 // We don't do any read barrier on `method`'s declaring class in this code, as the JIT might
810 // enter here on a soon-to-be deleted ArtMethod. Updating the entrypoint is OK though, as
811 // the ArtMethod is still in memory.
812 const void* new_quick_code = quick_code;
813 if (UNLIKELY(instrumentation_stubs_installed_) && entry_exit_stubs_installed_) {
814 new_quick_code = GetQuickInstrumentationEntryPoint();
815 }
816 UpdateEntrypoints(method, new_quick_code);
817}
818
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700819void Instrumentation::UpdateMethodsCode(ArtMethod* method, const void* quick_code) {
820 DCHECK(method->GetDeclaringClass()->IsResolved());
821 UpdateMethodsCodeImpl(method, quick_code);
822}
823
Alex Light0a5ec3d2017-07-25 16:50:26 -0700824void Instrumentation::UpdateMethodsCodeToInterpreterEntryPoint(ArtMethod* method) {
825 UpdateMethodsCodeImpl(method, GetQuickToInterpreterBridge());
826}
827
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000828void Instrumentation::UpdateMethodsCodeForJavaDebuggable(ArtMethod* method,
829 const void* quick_code) {
830 // When the runtime is set to Java debuggable, we may update the entry points of
831 // all methods of a class to the interpreter bridge. A method's declaring class
832 // might not be in resolved state yet in that case, so we bypass the DCHECK in
833 // UpdateMethodsCode.
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700834 UpdateMethodsCodeImpl(method, quick_code);
835}
836
Mathieu Chartiere401d142015-04-22 13:56:20 -0700837bool Instrumentation::AddDeoptimizedMethod(ArtMethod* method) {
838 if (IsDeoptimizedMethod(method)) {
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700839 // Already in the map. Return.
840 return false;
841 }
842 // Not found. Add it.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700843 deoptimized_methods_.insert(method);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700844 return true;
845}
846
Mathieu Chartiere401d142015-04-22 13:56:20 -0700847bool Instrumentation::IsDeoptimizedMethod(ArtMethod* method) {
848 return deoptimized_methods_.find(method) != deoptimized_methods_.end();
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700849}
850
Mathieu Chartiere401d142015-04-22 13:56:20 -0700851ArtMethod* Instrumentation::BeginDeoptimizedMethod() {
852 if (deoptimized_methods_.empty()) {
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700853 // Empty.
854 return nullptr;
855 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700856 return *deoptimized_methods_.begin();
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700857}
858
Mathieu Chartiere401d142015-04-22 13:56:20 -0700859bool Instrumentation::RemoveDeoptimizedMethod(ArtMethod* method) {
860 auto it = deoptimized_methods_.find(method);
861 if (it == deoptimized_methods_.end()) {
862 return false;
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700863 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700864 deoptimized_methods_.erase(it);
865 return true;
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700866}
867
868bool Instrumentation::IsDeoptimizedMethodsEmpty() const {
869 return deoptimized_methods_.empty();
870}
871
Mathieu Chartiere401d142015-04-22 13:56:20 -0700872void Instrumentation::Deoptimize(ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100873 CHECK(!method->IsNative());
874 CHECK(!method->IsProxyMethod());
Alex Light9139e002015-10-09 15:59:48 -0700875 CHECK(method->IsInvokable());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100876
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700877 Thread* self = Thread::Current();
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700878 {
879 WriterMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700880 bool has_not_been_deoptimized = AddDeoptimizedMethod(method);
David Sehr709b0702016-10-13 09:12:37 -0700881 CHECK(has_not_been_deoptimized) << "Method " << ArtMethod::PrettyMethod(method)
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200882 << " is already deoptimized";
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700883 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100884 if (!interpreter_stubs_installed_) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800885 UpdateEntrypoints(method, GetQuickInstrumentationEntryPoint());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100886
887 // Install instrumentation exit stub and instrumentation frames. We may already have installed
888 // these previously so it will only cover the newly created frames.
889 instrumentation_stubs_installed_ = true;
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700890 MutexLock mu(self, *Locks::thread_list_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100891 Runtime::Current()->GetThreadList()->ForEach(InstrumentationInstallStack, this);
892 }
893}
894
Mathieu Chartiere401d142015-04-22 13:56:20 -0700895void Instrumentation::Undeoptimize(ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100896 CHECK(!method->IsNative());
897 CHECK(!method->IsProxyMethod());
Alex Light9139e002015-10-09 15:59:48 -0700898 CHECK(method->IsInvokable());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100899
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700900 Thread* self = Thread::Current();
901 bool empty;
902 {
903 WriterMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700904 bool found_and_erased = RemoveDeoptimizedMethod(method);
David Sehr709b0702016-10-13 09:12:37 -0700905 CHECK(found_and_erased) << "Method " << ArtMethod::PrettyMethod(method)
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700906 << " is not deoptimized";
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700907 empty = IsDeoptimizedMethodsEmpty();
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700908 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100909
910 // Restore code and possibly stack only if we did not deoptimize everything.
911 if (!interpreter_stubs_installed_) {
912 // Restore its code or resolution trampoline.
913 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800914 if (method->IsStatic() && !method->IsConstructor() &&
915 !method->GetDeclaringClass()->IsInitialized()) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800916 UpdateEntrypoints(method, GetQuickResolutionStub());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100917 } else {
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000918 const void* quick_code = NeedDebugVersionFor(method)
919 ? GetQuickToInterpreterBridge()
Alex Lightfc49fec2018-01-16 22:28:36 +0000920 : class_linker->GetQuickOatCodeFor(method);
Elliott Hughes956af0f2014-12-11 14:34:28 -0800921 UpdateEntrypoints(method, quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100922 }
923
924 // If there is no deoptimized method left, we can restore the stack of each thread.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700925 if (empty) {
926 MutexLock mu(self, *Locks::thread_list_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100927 Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this);
928 instrumentation_stubs_installed_ = false;
929 }
930 }
931}
932
Mathieu Chartiere401d142015-04-22 13:56:20 -0700933bool Instrumentation::IsDeoptimized(ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100934 DCHECK(method != nullptr);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700935 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700936 return IsDeoptimizedMethod(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100937}
938
939void Instrumentation::EnableDeoptimization() {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700940 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700941 CHECK(IsDeoptimizedMethodsEmpty());
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100942 CHECK_EQ(deoptimization_enabled_, false);
943 deoptimization_enabled_ = true;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100944}
945
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200946void Instrumentation::DisableDeoptimization(const char* key) {
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100947 CHECK_EQ(deoptimization_enabled_, true);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100948 // If we deoptimized everything, undo it.
Alex Lightdba61482016-12-21 08:20:29 -0800949 InstrumentationLevel level = GetCurrentInstrumentationLevel();
950 if (level == InstrumentationLevel::kInstrumentWithInterpreter) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200951 UndeoptimizeEverything(key);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100952 }
953 // Undeoptimized selected methods.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700954 while (true) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700955 ArtMethod* method;
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700956 {
957 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700958 if (IsDeoptimizedMethodsEmpty()) {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700959 break;
960 }
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700961 method = BeginDeoptimizedMethod();
962 CHECK(method != nullptr);
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700963 }
964 Undeoptimize(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100965 }
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100966 deoptimization_enabled_ = false;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100967}
968
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100969// Indicates if instrumentation should notify method enter/exit events to the listeners.
970bool Instrumentation::ShouldNotifyMethodEnterExitEvents() const {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200971 if (!HasMethodEntryListeners() && !HasMethodExitListeners()) {
972 return false;
973 }
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100974 return !deoptimization_enabled_ && !interpreter_stubs_installed_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100975}
976
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200977void Instrumentation::DeoptimizeEverything(const char* key) {
978 CHECK(deoptimization_enabled_);
979 ConfigureStubs(key, InstrumentationLevel::kInstrumentWithInterpreter);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100980}
981
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200982void Instrumentation::UndeoptimizeEverything(const char* key) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100983 CHECK(interpreter_stubs_installed_);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200984 CHECK(deoptimization_enabled_);
985 ConfigureStubs(key, InstrumentationLevel::kInstrumentNothing);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100986}
987
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200988void Instrumentation::EnableMethodTracing(const char* key, bool needs_interpreter) {
989 InstrumentationLevel level;
990 if (needs_interpreter) {
991 level = InstrumentationLevel::kInstrumentWithInterpreter;
992 } else {
993 level = InstrumentationLevel::kInstrumentWithInstrumentationStubs;
994 }
995 ConfigureStubs(key, level);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100996}
997
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200998void Instrumentation::DisableMethodTracing(const char* key) {
999 ConfigureStubs(key, InstrumentationLevel::kInstrumentNothing);
jeffhao725a9572012-11-13 18:20:12 -08001000}
1001
Andreas Gampe542451c2016-07-26 09:02:02 -07001002const void* Instrumentation::GetQuickCodeFor(ArtMethod* method, PointerSize pointer_size) const {
Vladimir Marko97d7e1c2016-10-04 14:44:28 +01001003 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers62d6c772013-02-27 08:32:07 -08001004 if (LIKELY(!instrumentation_stubs_installed_)) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001005 const void* code = method->GetEntryPointFromQuickCompiledCodePtrSize(pointer_size);
Vladimir Marko8a630572014-04-09 18:45:35 +01001006 DCHECK(code != nullptr);
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001007 if (LIKELY(!class_linker->IsQuickResolutionStub(code) &&
1008 !class_linker->IsQuickToInterpreterBridge(code)) &&
1009 !class_linker->IsQuickResolutionStub(code) &&
1010 !class_linker->IsQuickToInterpreterBridge(code)) {
Ian Rogers62d6c772013-02-27 08:32:07 -08001011 return code;
1012 }
1013 }
Alex Lightfc49fec2018-01-16 22:28:36 +00001014 return class_linker->GetQuickOatCodeFor(method);
jeffhao725a9572012-11-13 18:20:12 -08001015}
1016
Alex Lightd7661582017-05-01 13:48:16 -07001017void Instrumentation::MethodEnterEventImpl(Thread* thread,
1018 ObjPtr<mirror::Object> this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001019 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -08001020 uint32_t dex_pc) const {
Mingyao Yang2ee17902017-08-30 11:37:08 -07001021 DCHECK(!method->IsRuntimeMethod());
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001022 if (HasMethodEntryListeners()) {
Alex Lightd7661582017-05-01 13:48:16 -07001023 Thread* self = Thread::Current();
1024 StackHandleScope<1> hs(self);
1025 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001026 for (InstrumentationListener* listener : method_entry_listeners_) {
1027 if (listener != nullptr) {
Alex Lightd7661582017-05-01 13:48:16 -07001028 listener->MethodEntered(thread, thiz, method, dex_pc);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001029 }
1030 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001031 }
1032}
1033
Alex Lightd7661582017-05-01 13:48:16 -07001034void Instrumentation::MethodExitEventImpl(Thread* thread,
1035 ObjPtr<mirror::Object> this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001036 ArtMethod* method,
Alex Lightd7661582017-05-01 13:48:16 -07001037 uint32_t dex_pc,
1038 const JValue& return_value) const {
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001039 if (HasMethodExitListeners()) {
Alex Lightd7661582017-05-01 13:48:16 -07001040 Thread* self = Thread::Current();
1041 StackHandleScope<2> hs(self);
1042 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
1043 if (method->GetInterfaceMethodIfProxy(kRuntimePointerSize)
1044 ->GetReturnTypePrimitive() != Primitive::kPrimNot) {
1045 for (InstrumentationListener* listener : method_exit_listeners_) {
1046 if (listener != nullptr) {
1047 listener->MethodExited(thread, thiz, method, dex_pc, return_value);
1048 }
1049 }
1050 } else {
1051 Handle<mirror::Object> ret(hs.NewHandle(return_value.GetL()));
1052 for (InstrumentationListener* listener : method_exit_listeners_) {
1053 if (listener != nullptr) {
1054 listener->MethodExited(thread, thiz, method, dex_pc, ret);
1055 }
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001056 }
1057 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001058 }
1059}
1060
Alex Lightd7661582017-05-01 13:48:16 -07001061void Instrumentation::MethodUnwindEvent(Thread* thread,
1062 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 {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +02001065 if (HasMethodUnwindListeners()) {
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));
Mathieu Chartier02e25112013-08-14 16:14:24 -07001069 for (InstrumentationListener* listener : method_unwind_listeners_) {
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001070 if (listener != nullptr) {
Alex Lightd7661582017-05-01 13:48:16 -07001071 listener->MethodUnwind(thread, thiz, method, dex_pc);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001072 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001073 }
1074 }
1075}
1076
Alex Lightd7661582017-05-01 13:48:16 -07001077void Instrumentation::DexPcMovedEventImpl(Thread* thread,
1078 ObjPtr<mirror::Object> this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001079 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -08001080 uint32_t dex_pc) const {
Alex Lightd7661582017-05-01 13:48:16 -07001081 Thread* self = Thread::Current();
1082 StackHandleScope<1> hs(self);
1083 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001084 for (InstrumentationListener* listener : dex_pc_listeners_) {
1085 if (listener != nullptr) {
Alex Lightd7661582017-05-01 13:48:16 -07001086 listener->DexPcMoved(thread, thiz, method, dex_pc);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001087 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001088 }
1089}
1090
Nicolas Geoffray81f0f952016-01-20 16:25:19 +00001091void Instrumentation::BranchImpl(Thread* thread,
1092 ArtMethod* method,
1093 uint32_t dex_pc,
1094 int32_t offset) const {
1095 for (InstrumentationListener* listener : branch_listeners_) {
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001096 if (listener != nullptr) {
Nicolas Geoffray81f0f952016-01-20 16:25:19 +00001097 listener->Branch(thread, method, dex_pc, offset);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001098 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001099 }
1100}
1101
Nicolas Geoffray5550ca82015-08-21 18:38:30 +01001102void Instrumentation::InvokeVirtualOrInterfaceImpl(Thread* thread,
Alex Lightd7661582017-05-01 13:48:16 -07001103 ObjPtr<mirror::Object> this_object,
Nicolas Geoffray5550ca82015-08-21 18:38:30 +01001104 ArtMethod* caller,
1105 uint32_t dex_pc,
1106 ArtMethod* callee) const {
Alex Lightd7661582017-05-01 13:48:16 -07001107 Thread* self = Thread::Current();
1108 StackHandleScope<1> hs(self);
1109 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
Nicolas Geoffray5550ca82015-08-21 18:38:30 +01001110 for (InstrumentationListener* listener : invoke_virtual_or_interface_listeners_) {
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001111 if (listener != nullptr) {
Alex Lightd7661582017-05-01 13:48:16 -07001112 listener->InvokeVirtualOrInterface(thread, thiz, caller, dex_pc, callee);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001113 }
Nicolas Geoffray5550ca82015-08-21 18:38:30 +01001114 }
1115}
1116
Alex Lighte814f9d2017-07-31 16:14:39 -07001117void Instrumentation::WatchedFramePopImpl(Thread* thread, const ShadowFrame& frame) const {
1118 for (InstrumentationListener* listener : watched_frame_pop_listeners_) {
1119 if (listener != nullptr) {
1120 listener->WatchedFramePop(thread, frame);
1121 }
1122 }
1123}
1124
Alex Lightd7661582017-05-01 13:48:16 -07001125void Instrumentation::FieldReadEventImpl(Thread* thread,
1126 ObjPtr<mirror::Object> this_object,
1127 ArtMethod* method,
1128 uint32_t dex_pc,
Mathieu Chartierc7853442015-03-27 14:35:38 -07001129 ArtField* field) const {
Alex Lightd7661582017-05-01 13:48:16 -07001130 Thread* self = Thread::Current();
1131 StackHandleScope<1> hs(self);
1132 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001133 for (InstrumentationListener* listener : field_read_listeners_) {
1134 if (listener != nullptr) {
Alex Lightd7661582017-05-01 13:48:16 -07001135 listener->FieldRead(thread, thiz, method, dex_pc, field);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001136 }
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +02001137 }
1138}
1139
Alex Lightd7661582017-05-01 13:48:16 -07001140void Instrumentation::FieldWriteEventImpl(Thread* thread,
1141 ObjPtr<mirror::Object> this_object,
1142 ArtMethod* method,
1143 uint32_t dex_pc,
1144 ArtField* field,
1145 const JValue& field_value) const {
1146 Thread* self = Thread::Current();
1147 StackHandleScope<2> hs(self);
1148 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
1149 if (field->IsPrimitiveType()) {
1150 for (InstrumentationListener* listener : field_write_listeners_) {
1151 if (listener != nullptr) {
1152 listener->FieldWritten(thread, thiz, method, dex_pc, field, field_value);
1153 }
1154 }
1155 } else {
1156 Handle<mirror::Object> val(hs.NewHandle(field_value.GetL()));
1157 for (InstrumentationListener* listener : field_write_listeners_) {
1158 if (listener != nullptr) {
1159 listener->FieldWritten(thread, thiz, method, dex_pc, field, val);
1160 }
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001161 }
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +02001162 }
1163}
1164
Alex Light6e1607e2017-08-23 10:06:18 -07001165void Instrumentation::ExceptionThrownEvent(Thread* thread,
Sebastien Hertz947ff082013-09-17 14:10:13 +02001166 mirror::Throwable* exception_object) const {
Hiroshi Yamauchi3481f7a2017-02-10 12:07:36 -08001167 Thread* self = Thread::Current();
1168 StackHandleScope<1> hs(self);
1169 Handle<mirror::Throwable> h_exception(hs.NewHandle(exception_object));
Alex Light6e1607e2017-08-23 10:06:18 -07001170 if (HasExceptionThrownListeners()) {
Hiroshi Yamauchi3481f7a2017-02-10 12:07:36 -08001171 DCHECK_EQ(thread->GetException(), h_exception.Get());
Jeff Haoc0bd4da2013-04-11 15:52:28 -07001172 thread->ClearException();
Alex Light6e1607e2017-08-23 10:06:18 -07001173 for (InstrumentationListener* listener : exception_thrown_listeners_) {
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001174 if (listener != nullptr) {
Alex Light6e1607e2017-08-23 10:06:18 -07001175 listener->ExceptionThrown(thread, h_exception);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001176 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001177 }
Alex Light9fb1ab12017-09-05 09:32:49 -07001178 // See b/65049545 for discussion about this behavior.
1179 thread->AssertNoPendingException();
Hiroshi Yamauchi3481f7a2017-02-10 12:07:36 -08001180 thread->SetException(h_exception.Get());
Ian Rogers62d6c772013-02-27 08:32:07 -08001181 }
1182}
1183
Alex Light9fb1ab12017-09-05 09:32:49 -07001184void Instrumentation::ExceptionHandledEvent(Thread* thread,
1185 mirror::Throwable* exception_object) const {
1186 Thread* self = Thread::Current();
1187 StackHandleScope<1> hs(self);
1188 Handle<mirror::Throwable> h_exception(hs.NewHandle(exception_object));
1189 if (HasExceptionHandledListeners()) {
1190 // We should have cleared the exception so that callers can detect a new one.
1191 DCHECK(thread->GetException() == nullptr);
1192 for (InstrumentationListener* listener : exception_handled_listeners_) {
1193 if (listener != nullptr) {
1194 listener->ExceptionHandled(thread, h_exception);
1195 }
1196 }
1197 }
1198}
1199
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +00001200// Computes a frame ID by ignoring inlined frames.
1201size_t Instrumentation::ComputeFrameId(Thread* self,
1202 size_t frame_depth,
1203 size_t inlined_frames_before_frame) {
1204 CHECK_GE(frame_depth, inlined_frames_before_frame);
1205 size_t no_inline_depth = frame_depth - inlined_frames_before_frame;
1206 return StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk) - no_inline_depth;
1207}
1208
Ian Rogers62d6c772013-02-27 08:32:07 -08001209static void CheckStackDepth(Thread* self, const InstrumentationStackFrame& instrumentation_frame,
1210 int delta)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001211 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +01001212 size_t frame_id = StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk) + delta;
Ian Rogers62d6c772013-02-27 08:32:07 -08001213 if (frame_id != instrumentation_frame.frame_id_) {
1214 LOG(ERROR) << "Expected frame_id=" << frame_id << " but found "
1215 << instrumentation_frame.frame_id_;
1216 StackVisitor::DescribeStack(self);
1217 CHECK_EQ(frame_id, instrumentation_frame.frame_id_);
1218 }
1219}
1220
1221void Instrumentation::PushInstrumentationStackFrame(Thread* self, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001222 ArtMethod* method,
Jeff Hao9a916d32013-06-27 18:45:37 -07001223 uintptr_t lr, bool interpreter_entry) {
Alex Lightb7edcda2017-04-27 13:20:31 -07001224 DCHECK(!self->IsExceptionPending());
Ian Rogers62d6c772013-02-27 08:32:07 -08001225 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
1226 if (kVerboseInstrumentation) {
David Sehr709b0702016-10-13 09:12:37 -07001227 LOG(INFO) << "Entering " << ArtMethod::PrettyMethod(method) << " from PC "
1228 << reinterpret_cast<void*>(lr);
Ian Rogers62d6c772013-02-27 08:32:07 -08001229 }
Alex Lightb7edcda2017-04-27 13:20:31 -07001230
1231 // We send the enter event before pushing the instrumentation frame to make cleanup easier. If the
1232 // event causes an exception we can simply send the unwind event and return.
1233 StackHandleScope<1> hs(self);
1234 Handle<mirror::Object> h_this(hs.NewHandle(this_object));
1235 if (!interpreter_entry) {
1236 MethodEnterEvent(self, h_this.Get(), method, 0);
1237 if (self->IsExceptionPending()) {
1238 MethodUnwindEvent(self, h_this.Get(), method, 0);
1239 return;
1240 }
1241 }
1242
1243 // We have a callee-save frame meaning this value is guaranteed to never be 0.
1244 DCHECK(!self->IsExceptionPending());
1245 size_t frame_id = StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk);
1246
1247 instrumentation::InstrumentationStackFrame instrumentation_frame(h_this.Get(), method, lr,
Jeff Hao9a916d32013-06-27 18:45:37 -07001248 frame_id, interpreter_entry);
Ian Rogers62d6c772013-02-27 08:32:07 -08001249 stack->push_front(instrumentation_frame);
Ian Rogers62d6c772013-02-27 08:32:07 -08001250}
1251
Mingyao Yang2ee17902017-08-30 11:37:08 -07001252DeoptimizationMethodType Instrumentation::GetDeoptimizationMethodType(ArtMethod* method) {
1253 if (method->IsRuntimeMethod()) {
1254 // Certain methods have strict requirement on whether the dex instruction
1255 // should be re-executed upon deoptimization.
1256 if (method == Runtime::Current()->GetCalleeSaveMethod(
1257 CalleeSaveType::kSaveEverythingForClinit)) {
1258 return DeoptimizationMethodType::kKeepDexPc;
1259 }
1260 if (method == Runtime::Current()->GetCalleeSaveMethod(
1261 CalleeSaveType::kSaveEverythingForSuspendCheck)) {
1262 return DeoptimizationMethodType::kKeepDexPc;
1263 }
1264 }
1265 return DeoptimizationMethodType::kDefault;
1266}
1267
1268// Try to get the shorty of a runtime method if it's an invocation stub.
1269struct RuntimeMethodShortyVisitor : public StackVisitor {
1270 explicit RuntimeMethodShortyVisitor(Thread* thread)
1271 : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames),
1272 shorty('V') {}
1273
1274 bool VisitFrame() REQUIRES_SHARED(Locks::mutator_lock_) {
1275 ArtMethod* m = GetMethod();
1276 if (m != nullptr && !m->IsRuntimeMethod()) {
1277 // The first Java method.
1278 if (m->IsNative()) {
1279 // Use JNI method's shorty for the jni stub.
1280 shorty = m->GetShorty()[0];
1281 return false;
1282 }
1283 if (m->IsProxyMethod()) {
1284 // Proxy method just invokes its proxied method via
1285 // art_quick_proxy_invoke_handler.
1286 shorty = m->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty()[0];
1287 return false;
1288 }
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001289 const Instruction& instr = m->DexInstructions().InstructionAt(GetDexPc());
1290 if (instr.IsInvoke()) {
Mingyao Yang2ee17902017-08-30 11:37:08 -07001291 const DexFile* dex_file = m->GetDexFile();
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001292 if (interpreter::IsStringInit(dex_file, instr.VRegB())) {
Mingyao Yang2ee17902017-08-30 11:37:08 -07001293 // Invoking string init constructor is turned into invoking
1294 // StringFactory.newStringFromChars() which returns a string.
1295 shorty = 'L';
1296 return false;
1297 }
1298 // A regular invoke, use callee's shorty.
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001299 uint32_t method_idx = instr.VRegB();
Mingyao Yang2ee17902017-08-30 11:37:08 -07001300 shorty = dex_file->GetMethodShorty(method_idx)[0];
1301 }
1302 // Stop stack walking since we've seen a Java frame.
1303 return false;
1304 }
1305 return true;
1306 }
1307
1308 char shorty;
1309};
1310
Alex Lightb7edcda2017-04-27 13:20:31 -07001311TwoWordReturn Instrumentation::PopInstrumentationStackFrame(Thread* self,
1312 uintptr_t* return_pc,
1313 uint64_t* gpr_result,
1314 uint64_t* fpr_result) {
1315 DCHECK(gpr_result != nullptr);
1316 DCHECK(fpr_result != nullptr);
Ian Rogers62d6c772013-02-27 08:32:07 -08001317 // Do the pop.
1318 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
1319 CHECK_GT(stack->size(), 0U);
1320 InstrumentationStackFrame instrumentation_frame = stack->front();
1321 stack->pop_front();
1322
1323 // Set return PC and check the sanity of the stack.
1324 *return_pc = instrumentation_frame.return_pc_;
1325 CheckStackDepth(self, instrumentation_frame, 0);
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001326 self->VerifyStack();
Ian Rogers62d6c772013-02-27 08:32:07 -08001327
Mathieu Chartiere401d142015-04-22 13:56:20 -07001328 ArtMethod* method = instrumentation_frame.method_;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001329 uint32_t length;
Andreas Gampe542451c2016-07-26 09:02:02 -07001330 const PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Mingyao Yang2ee17902017-08-30 11:37:08 -07001331 char return_shorty;
1332
1333 // Runtime method does not call into MethodExitEvent() so there should not be
1334 // suspension point below.
1335 ScopedAssertNoThreadSuspension ants(__FUNCTION__, method->IsRuntimeMethod());
1336 if (method->IsRuntimeMethod()) {
1337 if (method != Runtime::Current()->GetCalleeSaveMethod(
1338 CalleeSaveType::kSaveEverythingForClinit)) {
1339 // If the caller is at an invocation point and the runtime method is not
1340 // for clinit, we need to pass return results to the caller.
1341 // We need the correct shorty to decide whether we need to pass the return
1342 // result for deoptimization below.
1343 RuntimeMethodShortyVisitor visitor(self);
1344 visitor.WalkStack();
1345 return_shorty = visitor.shorty;
1346 } else {
1347 // Some runtime methods such as allocations, unresolved field getters, etc.
1348 // have return value. We don't need to set return_value since MethodExitEvent()
1349 // below isn't called for runtime methods. Deoptimization doesn't need the
1350 // value either since the dex instruction will be re-executed by the
1351 // interpreter, except these two cases:
1352 // (1) For an invoke, which is handled above to get the correct shorty.
1353 // (2) For MONITOR_ENTER/EXIT, which cannot be re-executed since it's not
1354 // idempotent. However there is no return value for it anyway.
1355 return_shorty = 'V';
1356 }
1357 } else {
1358 return_shorty = method->GetInterfaceMethodIfProxy(pointer_size)->GetShorty(&length)[0];
1359 }
1360
Alex Lightb7edcda2017-04-27 13:20:31 -07001361 bool is_ref = return_shorty == '[' || return_shorty == 'L';
1362 StackHandleScope<1> hs(self);
1363 MutableHandle<mirror::Object> res(hs.NewHandle<mirror::Object>(nullptr));
Ian Rogers62d6c772013-02-27 08:32:07 -08001364 JValue return_value;
1365 if (return_shorty == 'V') {
1366 return_value.SetJ(0);
1367 } else if (return_shorty == 'F' || return_shorty == 'D') {
Alex Lightb7edcda2017-04-27 13:20:31 -07001368 return_value.SetJ(*fpr_result);
Ian Rogers62d6c772013-02-27 08:32:07 -08001369 } else {
Alex Lightb7edcda2017-04-27 13:20:31 -07001370 return_value.SetJ(*gpr_result);
1371 }
1372 if (is_ref) {
1373 // Take a handle to the return value so we won't lose it if we suspend.
1374 res.Assign(return_value.GetL());
Ian Rogers62d6c772013-02-27 08:32:07 -08001375 }
1376 // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to
1377 // return_pc.
Andreas Gampee2abbc62017-09-15 11:59:26 -07001378 uint32_t dex_pc = dex::kDexNoIndex;
Ian Rogers62d6c772013-02-27 08:32:07 -08001379 mirror::Object* this_object = instrumentation_frame.this_object_;
Mingyao Yang2ee17902017-08-30 11:37:08 -07001380 if (!method->IsRuntimeMethod() && !instrumentation_frame.interpreter_entry_) {
Sebastien Hertz320deb22014-06-11 19:45:05 +02001381 MethodExitEvent(self, this_object, instrumentation_frame.method_, dex_pc, return_value);
1382 }
jeffhao725a9572012-11-13 18:20:12 -08001383
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01001384 // Deoptimize if the caller needs to continue execution in the interpreter. Do nothing if we get
1385 // back to an upcall.
1386 NthCallerVisitor visitor(self, 1, true);
1387 visitor.WalkStack(true);
Sebastien Hertz270a0e12015-01-16 19:49:09 +01001388 bool deoptimize = (visitor.caller != nullptr) &&
Daniel Mihalyieb076692014-08-22 17:33:31 +02001389 (interpreter_stubs_installed_ || IsDeoptimized(visitor.caller) ||
1390 Dbg::IsForcedInterpreterNeededForUpcall(self, visitor.caller));
Alex Lightb7edcda2017-04-27 13:20:31 -07001391 if (is_ref) {
1392 // Restore the return value if it's a reference since it might have moved.
1393 *reinterpret_cast<mirror::Object**>(gpr_result) = res.Get();
1394 }
Nicolas Geoffraya0619e22016-12-20 13:57:43 +00001395 if (deoptimize && Runtime::Current()->IsAsyncDeoptimizeable(*return_pc)) {
Ian Rogers62d6c772013-02-27 08:32:07 -08001396 if (kVerboseInstrumentation) {
Andreas Gampe46ee31b2016-12-14 10:11:49 -08001397 LOG(INFO) << "Deoptimizing "
1398 << visitor.caller->PrettyMethod()
1399 << " by returning from "
1400 << method->PrettyMethod()
1401 << " with result "
1402 << std::hex << return_value.GetJ() << std::dec
1403 << " in "
1404 << *self;
Ian Rogers62d6c772013-02-27 08:32:07 -08001405 }
Mingyao Yang2ee17902017-08-30 11:37:08 -07001406 DeoptimizationMethodType deopt_method_type = GetDeoptimizationMethodType(method);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01001407 self->PushDeoptimizationContext(return_value,
Mingyao Yang2ee17902017-08-30 11:37:08 -07001408 return_shorty == 'L' || return_shorty == '[',
1409 nullptr /* no pending exception */,
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01001410 false /* from_code */,
Mingyao Yang2ee17902017-08-30 11:37:08 -07001411 deopt_method_type);
Andreas Gamped58342c2014-06-05 14:18:08 -07001412 return GetTwoWordSuccessValue(*return_pc,
1413 reinterpret_cast<uintptr_t>(GetQuickDeoptimizationEntryPoint()));
Ian Rogers62d6c772013-02-27 08:32:07 -08001414 } else {
Nicolas Geoffraya0619e22016-12-20 13:57:43 +00001415 if (deoptimize && !Runtime::Current()->IsAsyncDeoptimizeable(*return_pc)) {
Alex Lightd8eb6732018-01-29 15:16:02 -08001416 VLOG(deopt) << "Got a deoptimization request on un-deoptimizable " << method->PrettyMethod()
1417 << " at PC " << reinterpret_cast<void*>(*return_pc);
Nicolas Geoffraya0619e22016-12-20 13:57:43 +00001418 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001419 if (kVerboseInstrumentation) {
David Sehr709b0702016-10-13 09:12:37 -07001420 LOG(INFO) << "Returning from " << method->PrettyMethod()
Brian Carlstrom2d888622013-07-18 17:02:00 -07001421 << " to PC " << reinterpret_cast<void*>(*return_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -08001422 }
Andreas Gamped58342c2014-06-05 14:18:08 -07001423 return GetTwoWordSuccessValue(0, *return_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -08001424 }
jeffhao725a9572012-11-13 18:20:12 -08001425}
1426
Mingyao Yangf711f2c2016-05-23 12:29:39 -07001427uintptr_t Instrumentation::PopMethodForUnwind(Thread* self, bool is_deoptimization) const {
Ian Rogers62d6c772013-02-27 08:32:07 -08001428 // Do the pop.
1429 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
1430 CHECK_GT(stack->size(), 0U);
Alex Lightb7edcda2017-04-27 13:20:31 -07001431 size_t idx = stack->size();
Ian Rogers62d6c772013-02-27 08:32:07 -08001432 InstrumentationStackFrame instrumentation_frame = stack->front();
Ian Rogers62d6c772013-02-27 08:32:07 -08001433
Mathieu Chartiere401d142015-04-22 13:56:20 -07001434 ArtMethod* method = instrumentation_frame.method_;
Ian Rogers62d6c772013-02-27 08:32:07 -08001435 if (is_deoptimization) {
1436 if (kVerboseInstrumentation) {
David Sehr709b0702016-10-13 09:12:37 -07001437 LOG(INFO) << "Popping for deoptimization " << ArtMethod::PrettyMethod(method);
Ian Rogers62d6c772013-02-27 08:32:07 -08001438 }
1439 } else {
1440 if (kVerboseInstrumentation) {
David Sehr709b0702016-10-13 09:12:37 -07001441 LOG(INFO) << "Popping for unwind " << ArtMethod::PrettyMethod(method);
Ian Rogers62d6c772013-02-27 08:32:07 -08001442 }
1443
1444 // Notify listeners of method unwind.
1445 // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to
1446 // return_pc.
Andreas Gampee2abbc62017-09-15 11:59:26 -07001447 uint32_t dex_pc = dex::kDexNoIndex;
Mingyao Yang2ee17902017-08-30 11:37:08 -07001448 if (!method->IsRuntimeMethod()) {
1449 MethodUnwindEvent(self, instrumentation_frame.this_object_, method, dex_pc);
1450 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001451 }
Alex Lightb7edcda2017-04-27 13:20:31 -07001452 // TODO: bring back CheckStackDepth(self, instrumentation_frame, 2);
1453 CHECK_EQ(stack->size(), idx);
1454 DCHECK(instrumentation_frame.method_ == stack->front().method_);
1455 stack->pop_front();
Mingyao Yangf711f2c2016-05-23 12:29:39 -07001456 return instrumentation_frame.return_pc_;
Ian Rogers62d6c772013-02-27 08:32:07 -08001457}
1458
1459std::string InstrumentationStackFrame::Dump() const {
1460 std::ostringstream os;
David Sehr709b0702016-10-13 09:12:37 -07001461 os << "Frame " << frame_id_ << " " << ArtMethod::PrettyMethod(method_) << ":"
Ian Rogers62d6c772013-02-27 08:32:07 -08001462 << reinterpret_cast<void*>(return_pc_) << " this=" << reinterpret_cast<void*>(this_object_);
1463 return os.str();
1464}
1465
1466} // namespace instrumentation
jeffhao725a9572012-11-13 18:20:12 -08001467} // namespace art