blob: 34e271144abb13deff193e8e8c36dccdedc23837 [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_) {
Alex Lightf2858632018-04-02 11:28:50 -0700142 art::Runtime* runtime = Runtime::Current();
143 // If anything says we need the debug version or we are debuggable we will need the debug version
144 // of the method.
145 return (runtime->GetRuntimeCallbacks()->MethodNeedsDebugVersion(method) ||
146 runtime->IsJavaDebuggable()) &&
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800147 !method->IsNative() &&
Alex Lightf2858632018-04-02 11:28:50 -0700148 !method->IsProxyMethod();
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800149}
150
Mathieu Chartiere401d142015-04-22 13:56:20 -0700151void Instrumentation::InstallStubsForMethod(ArtMethod* method) {
Alex Light9139e002015-10-09 15:59:48 -0700152 if (!method->IsInvokable() || method->IsProxyMethod()) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100153 // Do not change stubs for these methods.
154 return;
155 }
Jeff Hao56802772014-08-19 10:17:36 -0700156 // Don't stub Proxy.<init>. Note that the Proxy class itself is not a proxy class.
Alex Light6cae5ea2018-06-07 17:07:02 -0700157 // TODO We should remove the need for this since it means we cannot always correctly detect calls
158 // to Proxy.<init>
159 // Annoyingly this can be called before we have actually initialized WellKnownClasses so therefore
160 // we also need to check this based on the declaring-class descriptor. The check is valid because
161 // Proxy only has a single constructor.
162 ArtMethod* well_known_proxy_init = jni::DecodeArtMethod(
163 WellKnownClasses::java_lang_reflect_Proxy_init);
164 if ((LIKELY(well_known_proxy_init != nullptr) && UNLIKELY(method == well_known_proxy_init)) ||
165 UNLIKELY(method->IsConstructor() &&
166 method->GetDeclaringClass()->DescriptorEquals("Ljava/lang/reflect/Proxy;"))) {
Jeff Haodb8a6642014-08-14 17:18:52 -0700167 return;
168 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800169 const void* new_quick_code;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100170 bool uninstall = !entry_exit_stubs_installed_ && !interpreter_stubs_installed_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800171 Runtime* const runtime = Runtime::Current();
172 ClassLinker* const class_linker = runtime->GetClassLinker();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100173 bool is_class_initialized = method->GetDeclaringClass()->IsInitialized();
174 if (uninstall) {
175 if ((forced_interpret_only_ || IsDeoptimized(method)) && !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800176 new_quick_code = GetQuickToInterpreterBridge();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100177 } else if (is_class_initialized || !method->IsStatic() || method->IsConstructor()) {
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000178 if (NeedDebugVersionFor(method)) {
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800179 new_quick_code = GetQuickToInterpreterBridge();
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000180 } else {
Alex Lightfc49fec2018-01-16 22:28:36 +0000181 new_quick_code = class_linker->GetQuickOatCodeFor(method);
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800182 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100183 } else {
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700184 new_quick_code = GetQuickResolutionStub();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100185 }
186 } else { // !uninstall
Sebastien Hertzbae182c2013-12-17 10:42:03 +0100187 if ((interpreter_stubs_installed_ || forced_interpret_only_ || IsDeoptimized(method)) &&
188 !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800189 new_quick_code = GetQuickToInterpreterBridge();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100190 } else {
191 // Do not overwrite resolution trampoline. When the trampoline initializes the method's
192 // class, all its static methods code will be set to the instrumentation entry point.
193 // For more details, see ClassLinker::FixupStaticTrampolines.
194 if (is_class_initialized || !method->IsStatic() || method->IsConstructor()) {
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000195 if (NeedDebugVersionFor(method)) {
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800196 // Oat code should not be used. Don't install instrumentation stub and
197 // use interpreter for instrumentation.
198 new_quick_code = GetQuickToInterpreterBridge();
199 } else if (entry_exit_stubs_installed_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800200 new_quick_code = GetQuickInstrumentationEntryPoint();
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000201 } else {
Alex Lightfc49fec2018-01-16 22:28:36 +0000202 new_quick_code = class_linker->GetQuickOatCodeFor(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100203 }
204 } else {
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700205 new_quick_code = GetQuickResolutionStub();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100206 }
207 }
208 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800209 UpdateEntrypoints(method, new_quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100210}
211
Ian Rogers62d6c772013-02-27 08:32:07 -0800212// Places the instrumentation exit pc as the return PC for every quick frame. This also allows
213// deoptimization of quick frames to interpreter frames.
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100214// Since we may already have done this previously, we need to push new instrumentation frame before
215// existing instrumentation frames.
Ian Rogers62d6c772013-02-27 08:32:07 -0800216static void InstrumentationInstallStack(Thread* thread, void* arg)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700217 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200218 struct InstallStackVisitor FINAL : public StackVisitor {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800219 InstallStackVisitor(Thread* thread_in, Context* context, uintptr_t instrumentation_exit_pc)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100220 : StackVisitor(thread_in, context, kInstrumentationStackWalk),
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800221 instrumentation_stack_(thread_in->GetInstrumentationStack()),
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100222 instrumentation_exit_pc_(instrumentation_exit_pc),
Alex Lighte9278662018-03-08 16:55:58 -0800223 reached_existing_instrumentation_frames_(false), instrumentation_stack_depth_(0),
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100224 last_return_pc_(0) {
225 }
jeffhao725a9572012-11-13 18:20:12 -0800226
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700227 bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700228 ArtMethod* m = GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700229 if (m == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800230 if (kVerboseInstrumentation) {
231 LOG(INFO) << " Skipping upcall. Frame " << GetFrameId();
232 }
233 last_return_pc_ = 0;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700234 return true; // Ignore upcalls.
Ian Rogers306057f2012-11-26 12:45:53 -0800235 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700236 if (GetCurrentQuickFrame() == nullptr) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800237 bool interpreter_frame = true;
Sebastien Hertz320deb22014-06-11 19:45:05 +0200238 InstrumentationStackFrame instrumentation_frame(GetThisObject(), m, 0, GetFrameId(),
239 interpreter_frame);
Jeff Haoa15a81b2014-05-27 18:25:47 -0700240 if (kVerboseInstrumentation) {
241 LOG(INFO) << "Pushing shadow frame " << instrumentation_frame.Dump();
242 }
243 shadow_stack_.push_back(instrumentation_frame);
244 return true; // Continue.
245 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800246 uintptr_t return_pc = GetReturnPc();
Sebastien Hertz320deb22014-06-11 19:45:05 +0200247 if (kVerboseInstrumentation) {
248 LOG(INFO) << " Installing exit stub in " << DescribeLocation();
249 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100250 if (return_pc == instrumentation_exit_pc_) {
Mingyao Yang2ee17902017-08-30 11:37:08 -0700251 CHECK_LT(instrumentation_stack_depth_, instrumentation_stack_->size());
252
253 if (m->IsRuntimeMethod()) {
254 const InstrumentationStackFrame& frame =
255 instrumentation_stack_->at(instrumentation_stack_depth_);
256 if (frame.interpreter_entry_) {
257 // This instrumentation frame is for an interpreter bridge and is
258 // pushed when executing the instrumented interpreter bridge. So method
259 // enter event must have been reported. However we need to push a DEX pc
260 // into the dex_pcs_ list to match size of instrumentation stack.
Andreas Gampee2abbc62017-09-15 11:59:26 -0700261 uint32_t dex_pc = dex::kDexNoIndex;
Mingyao Yang2ee17902017-08-30 11:37:08 -0700262 dex_pcs_.push_back(dex_pc);
263 last_return_pc_ = frame.return_pc_;
264 ++instrumentation_stack_depth_;
265 return true;
266 }
267 }
268
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100269 // We've reached a frame which has already been installed with instrumentation exit stub.
Alex Light74c91c92018-03-08 14:01:44 -0800270 // We should have already installed instrumentation or be interpreter on previous frames.
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100271 reached_existing_instrumentation_frames_ = true;
272
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200273 const InstrumentationStackFrame& frame =
274 instrumentation_stack_->at(instrumentation_stack_depth_);
David Sehr709b0702016-10-13 09:12:37 -0700275 CHECK_EQ(m, frame.method_) << "Expected " << ArtMethod::PrettyMethod(m)
276 << ", Found " << ArtMethod::PrettyMethod(frame.method_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100277 return_pc = frame.return_pc_;
278 if (kVerboseInstrumentation) {
279 LOG(INFO) << "Ignoring already instrumented " << frame.Dump();
280 }
281 } else {
282 CHECK_NE(return_pc, 0U);
Alex Light74c91c92018-03-08 14:01:44 -0800283 if (UNLIKELY(reached_existing_instrumentation_frames_ && !m->IsRuntimeMethod())) {
284 // We already saw an existing instrumentation frame so this should be a runtime-method
285 // inserted by the interpreter or runtime.
Alex Lighte9278662018-03-08 16:55:58 -0800286 std::string thread_name;
287 GetThread()->GetThreadName(thread_name);
288 uint32_t dex_pc = dex::kDexNoIndex;
289 if (last_return_pc_ != 0 &&
290 GetCurrentOatQuickMethodHeader() != nullptr) {
291 dex_pc = GetCurrentOatQuickMethodHeader()->ToDexPc(m, last_return_pc_);
292 }
Alex Light74c91c92018-03-08 14:01:44 -0800293 LOG(FATAL) << "While walking " << thread_name << " found unexpected non-runtime method"
294 << " without instrumentation exit return or interpreter frame."
Alex Lighte9278662018-03-08 16:55:58 -0800295 << " method is " << GetMethod()->PrettyMethod()
296 << " return_pc is " << std::hex << return_pc
297 << " dex pc: " << dex_pc;
298 UNREACHABLE();
299 }
Mingyao Yang2ee17902017-08-30 11:37:08 -0700300 InstrumentationStackFrame instrumentation_frame(
301 m->IsRuntimeMethod() ? nullptr : GetThisObject(),
302 m,
303 return_pc,
304 GetFrameId(), // A runtime method still gets a frame id.
305 false);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100306 if (kVerboseInstrumentation) {
307 LOG(INFO) << "Pushing frame " << instrumentation_frame.Dump();
308 }
309
Sebastien Hertz320deb22014-06-11 19:45:05 +0200310 // Insert frame at the right position so we do not corrupt the instrumentation stack.
311 // Instrumentation stack frames are in descending frame id order.
312 auto it = instrumentation_stack_->begin();
313 for (auto end = instrumentation_stack_->end(); it != end; ++it) {
314 const InstrumentationStackFrame& current = *it;
315 if (instrumentation_frame.frame_id_ >= current.frame_id_) {
316 break;
317 }
318 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100319 instrumentation_stack_->insert(it, instrumentation_frame);
320 SetReturnPc(instrumentation_exit_pc_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800321 }
Andreas Gampee2abbc62017-09-15 11:59:26 -0700322 uint32_t dex_pc = dex::kDexNoIndex;
Mingyao Yang2ee17902017-08-30 11:37:08 -0700323 if (last_return_pc_ != 0 &&
324 GetCurrentOatQuickMethodHeader() != nullptr) {
325 dex_pc = GetCurrentOatQuickMethodHeader()->ToDexPc(m, last_return_pc_);
326 }
327 dex_pcs_.push_back(dex_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800328 last_return_pc_ = return_pc;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100329 ++instrumentation_stack_depth_;
Ian Rogers306057f2012-11-26 12:45:53 -0800330 return true; // Continue.
331 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800332 std::deque<InstrumentationStackFrame>* const instrumentation_stack_;
Jeff Haoa15a81b2014-05-27 18:25:47 -0700333 std::vector<InstrumentationStackFrame> shadow_stack_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800334 std::vector<uint32_t> dex_pcs_;
Ian Rogers306057f2012-11-26 12:45:53 -0800335 const uintptr_t instrumentation_exit_pc_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100336 bool reached_existing_instrumentation_frames_;
337 size_t instrumentation_stack_depth_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800338 uintptr_t last_return_pc_;
Ian Rogers306057f2012-11-26 12:45:53 -0800339 };
Ian Rogers62d6c772013-02-27 08:32:07 -0800340 if (kVerboseInstrumentation) {
341 std::string thread_name;
342 thread->GetThreadName(thread_name);
343 LOG(INFO) << "Installing exit stubs in " << thread_name;
Ian Rogers306057f2012-11-26 12:45:53 -0800344 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100345
346 Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg);
Ian Rogers700a4022014-05-19 16:49:03 -0700347 std::unique_ptr<Context> context(Context::Create());
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700348 uintptr_t instrumentation_exit_pc = reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc());
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100349 InstallStackVisitor visitor(thread, context.get(), instrumentation_exit_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800350 visitor.WalkStack(true);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100351 CHECK_EQ(visitor.dex_pcs_.size(), thread->GetInstrumentationStack()->size());
Ian Rogers62d6c772013-02-27 08:32:07 -0800352
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100353 if (instrumentation->ShouldNotifyMethodEnterExitEvents()) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100354 // Create method enter events for all methods currently on the thread's stack. We only do this
355 // if no debugger is attached to prevent from posting events twice.
Jeff Haoa15a81b2014-05-27 18:25:47 -0700356 auto ssi = visitor.shadow_stack_.rbegin();
357 for (auto isi = thread->GetInstrumentationStack()->rbegin(),
358 end = thread->GetInstrumentationStack()->rend(); isi != end; ++isi) {
359 while (ssi != visitor.shadow_stack_.rend() && (*ssi).frame_id_ < (*isi).frame_id_) {
360 instrumentation->MethodEnterEvent(thread, (*ssi).this_object_, (*ssi).method_, 0);
361 ++ssi;
362 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100363 uint32_t dex_pc = visitor.dex_pcs_.back();
364 visitor.dex_pcs_.pop_back();
Alex Lightdc5423f2018-06-08 10:43:38 -0700365 if (!isi->interpreter_entry_ && !isi->method_->IsRuntimeMethod()) {
Sebastien Hertz320deb22014-06-11 19:45:05 +0200366 instrumentation->MethodEnterEvent(thread, (*isi).this_object_, (*isi).method_, dex_pc);
367 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100368 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800369 }
370 thread->VerifyStack();
Ian Rogers306057f2012-11-26 12:45:53 -0800371}
372
Mingyao Yang99170c62015-07-06 11:10:37 -0700373void Instrumentation::InstrumentThreadStack(Thread* thread) {
374 instrumentation_stubs_installed_ = true;
375 InstrumentationInstallStack(thread, this);
376}
377
Ian Rogers62d6c772013-02-27 08:32:07 -0800378// Removes the instrumentation exit pc as the return PC for every quick frame.
379static void InstrumentationRestoreStack(Thread* thread, void* arg)
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +0000380 REQUIRES(Locks::mutator_lock_) {
381 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
382
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200383 struct RestoreStackVisitor FINAL : public StackVisitor {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800384 RestoreStackVisitor(Thread* thread_in, uintptr_t instrumentation_exit_pc,
Ian Rogers62d6c772013-02-27 08:32:07 -0800385 Instrumentation* instrumentation)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100386 : StackVisitor(thread_in, nullptr, kInstrumentationStackWalk),
387 thread_(thread_in),
Ian Rogers62d6c772013-02-27 08:32:07 -0800388 instrumentation_exit_pc_(instrumentation_exit_pc),
389 instrumentation_(instrumentation),
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800390 instrumentation_stack_(thread_in->GetInstrumentationStack()),
Ian Rogers62d6c772013-02-27 08:32:07 -0800391 frames_removed_(0) {}
Ian Rogers306057f2012-11-26 12:45:53 -0800392
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700393 bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800394 if (instrumentation_stack_->size() == 0) {
jeffhao725a9572012-11-13 18:20:12 -0800395 return false; // Stop.
396 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700397 ArtMethod* m = GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700398 if (GetCurrentQuickFrame() == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800399 if (kVerboseInstrumentation) {
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200400 LOG(INFO) << " Ignoring a shadow frame. Frame " << GetFrameId()
David Sehr709b0702016-10-13 09:12:37 -0700401 << " Method=" << ArtMethod::PrettyMethod(m);
Ian Rogers62d6c772013-02-27 08:32:07 -0800402 }
403 return true; // Ignore shadow frames.
404 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700405 if (m == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800406 if (kVerboseInstrumentation) {
407 LOG(INFO) << " Skipping upcall. Frame " << GetFrameId();
408 }
Ian Rogers306057f2012-11-26 12:45:53 -0800409 return true; // Ignore upcalls.
410 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800411 bool removed_stub = false;
412 // TODO: make this search more efficient?
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100413 const size_t frameId = GetFrameId();
414 for (const InstrumentationStackFrame& instrumentation_frame : *instrumentation_stack_) {
415 if (instrumentation_frame.frame_id_ == frameId) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800416 if (kVerboseInstrumentation) {
417 LOG(INFO) << " Removing exit stub in " << DescribeLocation();
418 }
Jeff Hao9a916d32013-06-27 18:45:37 -0700419 if (instrumentation_frame.interpreter_entry_) {
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700420 CHECK(m == Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs));
Jeff Hao9a916d32013-06-27 18:45:37 -0700421 } else {
David Sehr709b0702016-10-13 09:12:37 -0700422 CHECK(m == instrumentation_frame.method_) << ArtMethod::PrettyMethod(m);
Jeff Hao9a916d32013-06-27 18:45:37 -0700423 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800424 SetReturnPc(instrumentation_frame.return_pc_);
Mingyao Yang2ee17902017-08-30 11:37:08 -0700425 if (instrumentation_->ShouldNotifyMethodEnterExitEvents() &&
426 !m->IsRuntimeMethod()) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100427 // Create the method exit events. As the methods didn't really exit the result is 0.
428 // We only do this if no debugger is attached to prevent from posting events twice.
429 instrumentation_->MethodExitEvent(thread_, instrumentation_frame.this_object_, m,
430 GetDexPc(), JValue());
431 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800432 frames_removed_++;
433 removed_stub = true;
434 break;
435 }
436 }
437 if (!removed_stub) {
438 if (kVerboseInstrumentation) {
439 LOG(INFO) << " No exit stub in " << DescribeLocation();
Ian Rogers306057f2012-11-26 12:45:53 -0800440 }
jeffhao725a9572012-11-13 18:20:12 -0800441 }
442 return true; // Continue.
443 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800444 Thread* const thread_;
Ian Rogers306057f2012-11-26 12:45:53 -0800445 const uintptr_t instrumentation_exit_pc_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800446 Instrumentation* const instrumentation_;
447 std::deque<instrumentation::InstrumentationStackFrame>* const instrumentation_stack_;
448 size_t frames_removed_;
jeffhao725a9572012-11-13 18:20:12 -0800449 };
Ian Rogers62d6c772013-02-27 08:32:07 -0800450 if (kVerboseInstrumentation) {
451 std::string thread_name;
452 thread->GetThreadName(thread_name);
453 LOG(INFO) << "Removing exit stubs in " << thread_name;
454 }
455 std::deque<instrumentation::InstrumentationStackFrame>* stack = thread->GetInstrumentationStack();
456 if (stack->size() > 0) {
457 Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700458 uintptr_t instrumentation_exit_pc =
459 reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc());
Ian Rogers62d6c772013-02-27 08:32:07 -0800460 RestoreStackVisitor visitor(thread, instrumentation_exit_pc, instrumentation);
461 visitor.WalkStack(true);
462 CHECK_EQ(visitor.frames_removed_, stack->size());
463 while (stack->size() > 0) {
464 stack->pop_front();
465 }
jeffhao725a9572012-11-13 18:20:12 -0800466 }
467}
468
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200469static bool HasEvent(Instrumentation::InstrumentationEvent expected, uint32_t events) {
470 return (events & expected) != 0;
471}
472
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000473static void PotentiallyAddListenerTo(Instrumentation::InstrumentationEvent event,
474 uint32_t events,
475 std::list<InstrumentationListener*>& list,
476 InstrumentationListener* listener,
477 bool* has_listener)
478 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_) {
479 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
480 if (!HasEvent(event, events)) {
481 return;
482 }
483 // If there is a free slot in the list, we insert the listener in that slot.
484 // Otherwise we add it to the end of the list.
485 auto it = std::find(list.begin(), list.end(), nullptr);
486 if (it != list.end()) {
487 *it = listener;
488 } else {
489 list.push_back(listener);
490 }
491 *has_listener = true;
492}
493
Ian Rogers62d6c772013-02-27 08:32:07 -0800494void Instrumentation::AddListener(InstrumentationListener* listener, uint32_t events) {
495 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000496 PotentiallyAddListenerTo(kMethodEntered,
497 events,
498 method_entry_listeners_,
499 listener,
500 &have_method_entry_listeners_);
501 PotentiallyAddListenerTo(kMethodExited,
502 events,
503 method_exit_listeners_,
504 listener,
505 &have_method_exit_listeners_);
506 PotentiallyAddListenerTo(kMethodUnwind,
507 events,
508 method_unwind_listeners_,
509 listener,
510 &have_method_unwind_listeners_);
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000511 PotentiallyAddListenerTo(kBranch,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000512 events,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000513 branch_listeners_,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000514 listener,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000515 &have_branch_listeners_);
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000516 PotentiallyAddListenerTo(kInvokeVirtualOrInterface,
517 events,
518 invoke_virtual_or_interface_listeners_,
519 listener,
520 &have_invoke_virtual_or_interface_listeners_);
521 PotentiallyAddListenerTo(kDexPcMoved,
522 events,
523 dex_pc_listeners_,
524 listener,
525 &have_dex_pc_listeners_);
526 PotentiallyAddListenerTo(kFieldRead,
527 events,
528 field_read_listeners_,
529 listener,
530 &have_field_read_listeners_);
531 PotentiallyAddListenerTo(kFieldWritten,
532 events,
533 field_write_listeners_,
534 listener,
535 &have_field_write_listeners_);
Alex Light6e1607e2017-08-23 10:06:18 -0700536 PotentiallyAddListenerTo(kExceptionThrown,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000537 events,
Alex Light6e1607e2017-08-23 10:06:18 -0700538 exception_thrown_listeners_,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000539 listener,
Alex Light6e1607e2017-08-23 10:06:18 -0700540 &have_exception_thrown_listeners_);
Alex Lighte814f9d2017-07-31 16:14:39 -0700541 PotentiallyAddListenerTo(kWatchedFramePop,
542 events,
543 watched_frame_pop_listeners_,
544 listener,
545 &have_watched_frame_pop_listeners_);
Alex Light9fb1ab12017-09-05 09:32:49 -0700546 PotentiallyAddListenerTo(kExceptionHandled,
547 events,
548 exception_handled_listeners_,
549 listener,
550 &have_exception_handled_listeners_);
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200551 UpdateInterpreterHandlerTable();
jeffhao725a9572012-11-13 18:20:12 -0800552}
553
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000554static void PotentiallyRemoveListenerFrom(Instrumentation::InstrumentationEvent event,
555 uint32_t events,
556 std::list<InstrumentationListener*>& list,
557 InstrumentationListener* listener,
558 bool* has_listener)
559 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_) {
560 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
561 if (!HasEvent(event, events)) {
562 return;
563 }
564 auto it = std::find(list.begin(), list.end(), listener);
565 if (it != list.end()) {
566 // Just update the entry, do not remove from the list. Removing entries in the list
567 // is unsafe when mutators are iterating over it.
568 *it = nullptr;
569 }
570
571 // Check if the list contains any non-null listener, and update 'has_listener'.
572 for (InstrumentationListener* l : list) {
573 if (l != nullptr) {
574 *has_listener = true;
575 return;
576 }
577 }
578 *has_listener = false;
579}
580
Ian Rogers62d6c772013-02-27 08:32:07 -0800581void Instrumentation::RemoveListener(InstrumentationListener* listener, uint32_t events) {
582 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000583 PotentiallyRemoveListenerFrom(kMethodEntered,
584 events,
585 method_entry_listeners_,
586 listener,
587 &have_method_entry_listeners_);
588 PotentiallyRemoveListenerFrom(kMethodExited,
589 events,
590 method_exit_listeners_,
591 listener,
592 &have_method_exit_listeners_);
593 PotentiallyRemoveListenerFrom(kMethodUnwind,
594 events,
595 method_unwind_listeners_,
596 listener,
597 &have_method_unwind_listeners_);
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000598 PotentiallyRemoveListenerFrom(kBranch,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000599 events,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000600 branch_listeners_,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000601 listener,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000602 &have_branch_listeners_);
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000603 PotentiallyRemoveListenerFrom(kInvokeVirtualOrInterface,
604 events,
605 invoke_virtual_or_interface_listeners_,
606 listener,
607 &have_invoke_virtual_or_interface_listeners_);
608 PotentiallyRemoveListenerFrom(kDexPcMoved,
609 events,
610 dex_pc_listeners_,
611 listener,
612 &have_dex_pc_listeners_);
613 PotentiallyRemoveListenerFrom(kFieldRead,
614 events,
615 field_read_listeners_,
616 listener,
617 &have_field_read_listeners_);
618 PotentiallyRemoveListenerFrom(kFieldWritten,
619 events,
620 field_write_listeners_,
621 listener,
622 &have_field_write_listeners_);
Alex Light6e1607e2017-08-23 10:06:18 -0700623 PotentiallyRemoveListenerFrom(kExceptionThrown,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000624 events,
Alex Light6e1607e2017-08-23 10:06:18 -0700625 exception_thrown_listeners_,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000626 listener,
Alex Light6e1607e2017-08-23 10:06:18 -0700627 &have_exception_thrown_listeners_);
Alex Lighte814f9d2017-07-31 16:14:39 -0700628 PotentiallyRemoveListenerFrom(kWatchedFramePop,
629 events,
630 watched_frame_pop_listeners_,
631 listener,
632 &have_watched_frame_pop_listeners_);
Alex Light9fb1ab12017-09-05 09:32:49 -0700633 PotentiallyRemoveListenerFrom(kExceptionHandled,
634 events,
635 exception_handled_listeners_,
636 listener,
637 &have_exception_handled_listeners_);
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200638 UpdateInterpreterHandlerTable();
jeffhao725a9572012-11-13 18:20:12 -0800639}
640
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200641Instrumentation::InstrumentationLevel Instrumentation::GetCurrentInstrumentationLevel() const {
Alex Light4ba388a2017-01-27 10:26:49 -0800642 if (interpreter_stubs_installed_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200643 return InstrumentationLevel::kInstrumentWithInterpreter;
Ian Rogers62d6c772013-02-27 08:32:07 -0800644 } else if (entry_exit_stubs_installed_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200645 return InstrumentationLevel::kInstrumentWithInstrumentationStubs;
Ian Rogers62d6c772013-02-27 08:32:07 -0800646 } else {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200647 return InstrumentationLevel::kInstrumentNothing;
Ian Rogers62d6c772013-02-27 08:32:07 -0800648 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200649}
650
Alex Lightdba61482016-12-21 08:20:29 -0800651bool Instrumentation::RequiresInstrumentationInstallation(InstrumentationLevel new_level) const {
Alex Light4ba388a2017-01-27 10:26:49 -0800652 // We need to reinstall instrumentation if we go to a different level.
653 return GetCurrentInstrumentationLevel() != new_level;
Alex Lightdba61482016-12-21 08:20:29 -0800654}
655
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200656void Instrumentation::ConfigureStubs(const char* key, InstrumentationLevel desired_level) {
657 // Store the instrumentation level for this key or remove it.
658 if (desired_level == InstrumentationLevel::kInstrumentNothing) {
659 // The client no longer needs instrumentation.
660 requested_instrumentation_levels_.erase(key);
661 } else {
662 // The client needs instrumentation.
663 requested_instrumentation_levels_.Overwrite(key, desired_level);
664 }
665
666 // Look for the highest required instrumentation level.
667 InstrumentationLevel requested_level = InstrumentationLevel::kInstrumentNothing;
668 for (const auto& v : requested_instrumentation_levels_) {
669 requested_level = std::max(requested_level, v.second);
670 }
671
672 interpret_only_ = (requested_level == InstrumentationLevel::kInstrumentWithInterpreter) ||
673 forced_interpret_only_;
674
Alex Lightdba61482016-12-21 08:20:29 -0800675 if (!RequiresInstrumentationInstallation(requested_level)) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800676 // We're already set.
677 return;
678 }
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100679 Thread* const self = Thread::Current();
Ian Rogers62d6c772013-02-27 08:32:07 -0800680 Runtime* runtime = Runtime::Current();
Sebastien Hertza8a697f2015-01-15 12:28:47 +0100681 Locks::mutator_lock_->AssertExclusiveHeld(self);
Ian Rogers62d6c772013-02-27 08:32:07 -0800682 Locks::thread_list_lock_->AssertNotHeld(self);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200683 if (requested_level > InstrumentationLevel::kInstrumentNothing) {
Alex Light4ba388a2017-01-27 10:26:49 -0800684 if (requested_level == InstrumentationLevel::kInstrumentWithInterpreter) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800685 interpreter_stubs_installed_ = true;
Ian Rogers62d6c772013-02-27 08:32:07 -0800686 entry_exit_stubs_installed_ = true;
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200687 } else {
688 CHECK_EQ(requested_level, InstrumentationLevel::kInstrumentWithInstrumentationStubs);
689 entry_exit_stubs_installed_ = true;
690 interpreter_stubs_installed_ = false;
Ian Rogers62d6c772013-02-27 08:32:07 -0800691 }
Mathieu Chartiere0671ce2015-07-28 17:23:28 -0700692 InstallStubsClassVisitor visitor(this);
693 runtime->GetClassLinker()->VisitClasses(&visitor);
Ian Rogers62d6c772013-02-27 08:32:07 -0800694 instrumentation_stubs_installed_ = true;
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100695 MutexLock mu(self, *Locks::thread_list_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800696 runtime->GetThreadList()->ForEach(InstrumentationInstallStack, this);
697 } else {
698 interpreter_stubs_installed_ = false;
699 entry_exit_stubs_installed_ = false;
Mathieu Chartiere0671ce2015-07-28 17:23:28 -0700700 InstallStubsClassVisitor visitor(this);
701 runtime->GetClassLinker()->VisitClasses(&visitor);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100702 // Restore stack only if there is no method currently deoptimized.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700703 bool empty;
704 {
705 ReaderMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700706 empty = IsDeoptimizedMethodsEmpty(); // Avoid lock violation.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700707 }
708 if (empty) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100709 MutexLock mu(self, *Locks::thread_list_lock_);
710 Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this);
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +0000711 // Only do this after restoring, as walking the stack when restoring will see
712 // the instrumentation exit pc.
713 instrumentation_stubs_installed_ = false;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100714 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800715 }
jeffhao725a9572012-11-13 18:20:12 -0800716}
717
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200718static void ResetQuickAllocEntryPointsForThread(Thread* thread, void* arg ATTRIBUTE_UNUSED) {
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800719 thread->ResetQuickAllocEntryPointsForThread(kUseReadBarrier && thread->GetIsGcMarking());
Ian Rogersfa824272013-11-05 16:12:57 -0800720}
721
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700722void Instrumentation::SetEntrypointsInstrumented(bool instrumented) {
723 Thread* self = Thread::Current();
Mathieu Chartier661974a2014-01-09 11:23:53 -0800724 Runtime* runtime = Runtime::Current();
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700725 Locks::mutator_lock_->AssertNotHeld(self);
726 Locks::instrument_entrypoints_lock_->AssertHeld(self);
727 if (runtime->IsStarted()) {
Mathieu Chartier4f55e222015-09-04 13:26:21 -0700728 ScopedSuspendAll ssa(__FUNCTION__);
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700729 MutexLock mu(self, *Locks::runtime_shutdown_lock_);
Mathieu Chartier661974a2014-01-09 11:23:53 -0800730 SetQuickAllocEntryPointsInstrumented(instrumented);
731 ResetQuickAllocEntryPoints();
Mathieu Chartier50e93312016-03-16 11:25:29 -0700732 alloc_entrypoints_instrumented_ = instrumented;
Mathieu Chartier4f55e222015-09-04 13:26:21 -0700733 } else {
734 MutexLock mu(self, *Locks::runtime_shutdown_lock_);
735 SetQuickAllocEntryPointsInstrumented(instrumented);
Andreas Gampe157c77e2016-10-17 17:44:41 -0700736
737 // Note: ResetQuickAllocEntryPoints only works when the runtime is started. Manually run the
738 // update for just this thread.
Andreas Gampe162ae502016-10-18 10:03:42 -0700739 // Note: self may be null. One of those paths is setting instrumentation in the Heap
740 // constructor for gcstress mode.
741 if (self != nullptr) {
742 ResetQuickAllocEntryPointsForThread(self, nullptr);
743 }
Andreas Gampe157c77e2016-10-17 17:44:41 -0700744
Mathieu Chartier50e93312016-03-16 11:25:29 -0700745 alloc_entrypoints_instrumented_ = instrumented;
Mathieu Chartier661974a2014-01-09 11:23:53 -0800746 }
747}
748
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700749void Instrumentation::InstrumentQuickAllocEntryPoints() {
750 MutexLock mu(Thread::Current(), *Locks::instrument_entrypoints_lock_);
751 InstrumentQuickAllocEntryPointsLocked();
Ian Rogersfa824272013-11-05 16:12:57 -0800752}
753
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700754void Instrumentation::UninstrumentQuickAllocEntryPoints() {
755 MutexLock mu(Thread::Current(), *Locks::instrument_entrypoints_lock_);
756 UninstrumentQuickAllocEntryPointsLocked();
757}
758
759void Instrumentation::InstrumentQuickAllocEntryPointsLocked() {
760 Locks::instrument_entrypoints_lock_->AssertHeld(Thread::Current());
761 if (quick_alloc_entry_points_instrumentation_counter_ == 0) {
762 SetEntrypointsInstrumented(true);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800763 }
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700764 ++quick_alloc_entry_points_instrumentation_counter_;
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700765}
766
767void Instrumentation::UninstrumentQuickAllocEntryPointsLocked() {
768 Locks::instrument_entrypoints_lock_->AssertHeld(Thread::Current());
769 CHECK_GT(quick_alloc_entry_points_instrumentation_counter_, 0U);
770 --quick_alloc_entry_points_instrumentation_counter_;
771 if (quick_alloc_entry_points_instrumentation_counter_ == 0) {
772 SetEntrypointsInstrumented(false);
773 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800774}
775
776void Instrumentation::ResetQuickAllocEntryPoints() {
777 Runtime* runtime = Runtime::Current();
778 if (runtime->IsStarted()) {
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800779 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700780 runtime->GetThreadList()->ForEach(ResetQuickAllocEntryPointsForThread, nullptr);
Ian Rogersfa824272013-11-05 16:12:57 -0800781 }
782}
783
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700784void Instrumentation::UpdateMethodsCodeImpl(ArtMethod* method, const void* quick_code) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800785 const void* new_quick_code;
Ian Rogers62d6c772013-02-27 08:32:07 -0800786 if (LIKELY(!instrumentation_stubs_installed_)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800787 new_quick_code = quick_code;
Jeff Hao65d15d92013-07-16 16:39:33 -0700788 } else {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100789 if ((interpreter_stubs_installed_ || IsDeoptimized(method)) && !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800790 new_quick_code = GetQuickToInterpreterBridge();
Jeff Hao65d15d92013-07-16 16:39:33 -0700791 } else {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700792 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700793 if (class_linker->IsQuickResolutionStub(quick_code) ||
794 class_linker->IsQuickToInterpreterBridge(quick_code)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700795 new_quick_code = quick_code;
Alex Light6cae5ea2018-06-07 17:07:02 -0700796 } else if (entry_exit_stubs_installed_ &&
797 // We need to make sure not to replace anything that InstallStubsForMethod
798 // wouldn't. Specifically we cannot stub out Proxy.<init> since subtypes copy the
799 // implementation directly and this will confuse the instrumentation trampolines.
800 // TODO We should remove the need for this since it makes it impossible to profile
801 // Proxy.<init> correctly in all cases.
802 method != jni::DecodeArtMethod(WellKnownClasses::java_lang_reflect_Proxy_init)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700803 new_quick_code = GetQuickInstrumentationEntryPoint();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700804 } else {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700805 new_quick_code = quick_code;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700806 }
Jeff Hao65d15d92013-07-16 16:39:33 -0700807 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800808 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800809 UpdateEntrypoints(method, new_quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100810}
811
Nicolas Geoffraya6e0e7d2018-01-26 13:16:50 +0000812void Instrumentation::UpdateNativeMethodsCodeToJitCode(ArtMethod* method, const void* quick_code) {
813 // We don't do any read barrier on `method`'s declaring class in this code, as the JIT might
814 // enter here on a soon-to-be deleted ArtMethod. Updating the entrypoint is OK though, as
815 // the ArtMethod is still in memory.
816 const void* new_quick_code = quick_code;
817 if (UNLIKELY(instrumentation_stubs_installed_) && entry_exit_stubs_installed_) {
818 new_quick_code = GetQuickInstrumentationEntryPoint();
819 }
820 UpdateEntrypoints(method, new_quick_code);
821}
822
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700823void Instrumentation::UpdateMethodsCode(ArtMethod* method, const void* quick_code) {
824 DCHECK(method->GetDeclaringClass()->IsResolved());
825 UpdateMethodsCodeImpl(method, quick_code);
826}
827
Alex Light0a5ec3d2017-07-25 16:50:26 -0700828void Instrumentation::UpdateMethodsCodeToInterpreterEntryPoint(ArtMethod* method) {
829 UpdateMethodsCodeImpl(method, GetQuickToInterpreterBridge());
830}
831
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000832void Instrumentation::UpdateMethodsCodeForJavaDebuggable(ArtMethod* method,
833 const void* quick_code) {
834 // When the runtime is set to Java debuggable, we may update the entry points of
835 // all methods of a class to the interpreter bridge. A method's declaring class
836 // might not be in resolved state yet in that case, so we bypass the DCHECK in
837 // UpdateMethodsCode.
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700838 UpdateMethodsCodeImpl(method, quick_code);
839}
840
Mathieu Chartiere401d142015-04-22 13:56:20 -0700841bool Instrumentation::AddDeoptimizedMethod(ArtMethod* method) {
842 if (IsDeoptimizedMethod(method)) {
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700843 // Already in the map. Return.
844 return false;
845 }
846 // Not found. Add it.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700847 deoptimized_methods_.insert(method);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700848 return true;
849}
850
Mathieu Chartiere401d142015-04-22 13:56:20 -0700851bool Instrumentation::IsDeoptimizedMethod(ArtMethod* method) {
852 return deoptimized_methods_.find(method) != deoptimized_methods_.end();
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700853}
854
Mathieu Chartiere401d142015-04-22 13:56:20 -0700855ArtMethod* Instrumentation::BeginDeoptimizedMethod() {
856 if (deoptimized_methods_.empty()) {
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700857 // Empty.
858 return nullptr;
859 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700860 return *deoptimized_methods_.begin();
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700861}
862
Mathieu Chartiere401d142015-04-22 13:56:20 -0700863bool Instrumentation::RemoveDeoptimizedMethod(ArtMethod* method) {
864 auto it = deoptimized_methods_.find(method);
865 if (it == deoptimized_methods_.end()) {
866 return false;
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700867 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700868 deoptimized_methods_.erase(it);
869 return true;
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700870}
871
872bool Instrumentation::IsDeoptimizedMethodsEmpty() const {
873 return deoptimized_methods_.empty();
874}
875
Mathieu Chartiere401d142015-04-22 13:56:20 -0700876void Instrumentation::Deoptimize(ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100877 CHECK(!method->IsNative());
878 CHECK(!method->IsProxyMethod());
Alex Light9139e002015-10-09 15:59:48 -0700879 CHECK(method->IsInvokable());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100880
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700881 Thread* self = Thread::Current();
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700882 {
883 WriterMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700884 bool has_not_been_deoptimized = AddDeoptimizedMethod(method);
David Sehr709b0702016-10-13 09:12:37 -0700885 CHECK(has_not_been_deoptimized) << "Method " << ArtMethod::PrettyMethod(method)
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200886 << " is already deoptimized";
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700887 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100888 if (!interpreter_stubs_installed_) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800889 UpdateEntrypoints(method, GetQuickInstrumentationEntryPoint());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100890
891 // Install instrumentation exit stub and instrumentation frames. We may already have installed
892 // these previously so it will only cover the newly created frames.
893 instrumentation_stubs_installed_ = true;
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700894 MutexLock mu(self, *Locks::thread_list_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100895 Runtime::Current()->GetThreadList()->ForEach(InstrumentationInstallStack, this);
896 }
897}
898
Mathieu Chartiere401d142015-04-22 13:56:20 -0700899void Instrumentation::Undeoptimize(ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100900 CHECK(!method->IsNative());
901 CHECK(!method->IsProxyMethod());
Alex Light9139e002015-10-09 15:59:48 -0700902 CHECK(method->IsInvokable());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100903
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700904 Thread* self = Thread::Current();
905 bool empty;
906 {
907 WriterMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700908 bool found_and_erased = RemoveDeoptimizedMethod(method);
David Sehr709b0702016-10-13 09:12:37 -0700909 CHECK(found_and_erased) << "Method " << ArtMethod::PrettyMethod(method)
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700910 << " is not deoptimized";
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700911 empty = IsDeoptimizedMethodsEmpty();
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700912 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100913
914 // Restore code and possibly stack only if we did not deoptimize everything.
915 if (!interpreter_stubs_installed_) {
916 // Restore its code or resolution trampoline.
917 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800918 if (method->IsStatic() && !method->IsConstructor() &&
919 !method->GetDeclaringClass()->IsInitialized()) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800920 UpdateEntrypoints(method, GetQuickResolutionStub());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100921 } else {
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000922 const void* quick_code = NeedDebugVersionFor(method)
923 ? GetQuickToInterpreterBridge()
Alex Lightfc49fec2018-01-16 22:28:36 +0000924 : class_linker->GetQuickOatCodeFor(method);
Elliott Hughes956af0f2014-12-11 14:34:28 -0800925 UpdateEntrypoints(method, quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100926 }
927
928 // If there is no deoptimized method left, we can restore the stack of each thread.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700929 if (empty) {
930 MutexLock mu(self, *Locks::thread_list_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100931 Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this);
932 instrumentation_stubs_installed_ = false;
933 }
934 }
935}
936
Mathieu Chartiere401d142015-04-22 13:56:20 -0700937bool Instrumentation::IsDeoptimized(ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100938 DCHECK(method != nullptr);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700939 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700940 return IsDeoptimizedMethod(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100941}
942
943void Instrumentation::EnableDeoptimization() {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700944 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700945 CHECK(IsDeoptimizedMethodsEmpty());
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100946 CHECK_EQ(deoptimization_enabled_, false);
947 deoptimization_enabled_ = true;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100948}
949
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200950void Instrumentation::DisableDeoptimization(const char* key) {
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100951 CHECK_EQ(deoptimization_enabled_, true);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100952 // If we deoptimized everything, undo it.
Alex Lightdba61482016-12-21 08:20:29 -0800953 InstrumentationLevel level = GetCurrentInstrumentationLevel();
954 if (level == InstrumentationLevel::kInstrumentWithInterpreter) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200955 UndeoptimizeEverything(key);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100956 }
957 // Undeoptimized selected methods.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700958 while (true) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700959 ArtMethod* method;
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700960 {
961 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700962 if (IsDeoptimizedMethodsEmpty()) {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700963 break;
964 }
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700965 method = BeginDeoptimizedMethod();
966 CHECK(method != nullptr);
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700967 }
968 Undeoptimize(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100969 }
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100970 deoptimization_enabled_ = false;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100971}
972
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100973// Indicates if instrumentation should notify method enter/exit events to the listeners.
974bool Instrumentation::ShouldNotifyMethodEnterExitEvents() const {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200975 if (!HasMethodEntryListeners() && !HasMethodExitListeners()) {
976 return false;
977 }
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100978 return !deoptimization_enabled_ && !interpreter_stubs_installed_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100979}
980
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200981void Instrumentation::DeoptimizeEverything(const char* key) {
982 CHECK(deoptimization_enabled_);
983 ConfigureStubs(key, InstrumentationLevel::kInstrumentWithInterpreter);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100984}
985
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200986void Instrumentation::UndeoptimizeEverything(const char* key) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100987 CHECK(interpreter_stubs_installed_);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200988 CHECK(deoptimization_enabled_);
989 ConfigureStubs(key, InstrumentationLevel::kInstrumentNothing);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100990}
991
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200992void Instrumentation::EnableMethodTracing(const char* key, bool needs_interpreter) {
993 InstrumentationLevel level;
994 if (needs_interpreter) {
995 level = InstrumentationLevel::kInstrumentWithInterpreter;
996 } else {
997 level = InstrumentationLevel::kInstrumentWithInstrumentationStubs;
998 }
999 ConfigureStubs(key, level);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01001000}
1001
Sebastien Hertz0462c4c2015-04-01 16:34:17 +02001002void Instrumentation::DisableMethodTracing(const char* key) {
1003 ConfigureStubs(key, InstrumentationLevel::kInstrumentNothing);
jeffhao725a9572012-11-13 18:20:12 -08001004}
1005
Andreas Gampe542451c2016-07-26 09:02:02 -07001006const void* Instrumentation::GetQuickCodeFor(ArtMethod* method, PointerSize pointer_size) const {
Vladimir Marko97d7e1c2016-10-04 14:44:28 +01001007 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers62d6c772013-02-27 08:32:07 -08001008 if (LIKELY(!instrumentation_stubs_installed_)) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001009 const void* code = method->GetEntryPointFromQuickCompiledCodePtrSize(pointer_size);
Vladimir Marko8a630572014-04-09 18:45:35 +01001010 DCHECK(code != nullptr);
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001011 if (LIKELY(!class_linker->IsQuickResolutionStub(code) &&
1012 !class_linker->IsQuickToInterpreterBridge(code)) &&
1013 !class_linker->IsQuickResolutionStub(code) &&
1014 !class_linker->IsQuickToInterpreterBridge(code)) {
Ian Rogers62d6c772013-02-27 08:32:07 -08001015 return code;
1016 }
1017 }
Alex Lightfc49fec2018-01-16 22:28:36 +00001018 return class_linker->GetQuickOatCodeFor(method);
jeffhao725a9572012-11-13 18:20:12 -08001019}
1020
Alex Lightd7661582017-05-01 13:48:16 -07001021void Instrumentation::MethodEnterEventImpl(Thread* thread,
1022 ObjPtr<mirror::Object> this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001023 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -08001024 uint32_t dex_pc) const {
Mingyao Yang2ee17902017-08-30 11:37:08 -07001025 DCHECK(!method->IsRuntimeMethod());
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001026 if (HasMethodEntryListeners()) {
Alex Lightd7661582017-05-01 13:48:16 -07001027 Thread* self = Thread::Current();
1028 StackHandleScope<1> hs(self);
1029 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001030 for (InstrumentationListener* listener : method_entry_listeners_) {
1031 if (listener != nullptr) {
Alex Lightd7661582017-05-01 13:48:16 -07001032 listener->MethodEntered(thread, thiz, method, dex_pc);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001033 }
1034 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001035 }
1036}
1037
Alex Lightd7661582017-05-01 13:48:16 -07001038void Instrumentation::MethodExitEventImpl(Thread* thread,
1039 ObjPtr<mirror::Object> this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001040 ArtMethod* method,
Alex Lightd7661582017-05-01 13:48:16 -07001041 uint32_t dex_pc,
1042 const JValue& return_value) const {
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001043 if (HasMethodExitListeners()) {
Alex Lightd7661582017-05-01 13:48:16 -07001044 Thread* self = Thread::Current();
1045 StackHandleScope<2> hs(self);
1046 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
1047 if (method->GetInterfaceMethodIfProxy(kRuntimePointerSize)
1048 ->GetReturnTypePrimitive() != Primitive::kPrimNot) {
1049 for (InstrumentationListener* listener : method_exit_listeners_) {
1050 if (listener != nullptr) {
1051 listener->MethodExited(thread, thiz, method, dex_pc, return_value);
1052 }
1053 }
1054 } else {
1055 Handle<mirror::Object> ret(hs.NewHandle(return_value.GetL()));
1056 for (InstrumentationListener* listener : method_exit_listeners_) {
1057 if (listener != nullptr) {
1058 listener->MethodExited(thread, thiz, method, dex_pc, ret);
1059 }
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001060 }
1061 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001062 }
1063}
1064
Alex Lightd7661582017-05-01 13:48:16 -07001065void Instrumentation::MethodUnwindEvent(Thread* thread,
1066 mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001067 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -08001068 uint32_t dex_pc) const {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +02001069 if (HasMethodUnwindListeners()) {
Alex Lightd7661582017-05-01 13:48:16 -07001070 Thread* self = Thread::Current();
1071 StackHandleScope<1> hs(self);
1072 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
Mathieu Chartier02e25112013-08-14 16:14:24 -07001073 for (InstrumentationListener* listener : method_unwind_listeners_) {
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001074 if (listener != nullptr) {
Alex Lightd7661582017-05-01 13:48:16 -07001075 listener->MethodUnwind(thread, thiz, method, dex_pc);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001076 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001077 }
1078 }
1079}
1080
Alex Lightd7661582017-05-01 13:48:16 -07001081void Instrumentation::DexPcMovedEventImpl(Thread* thread,
1082 ObjPtr<mirror::Object> this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001083 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -08001084 uint32_t dex_pc) const {
Alex Lightd7661582017-05-01 13:48:16 -07001085 Thread* self = Thread::Current();
1086 StackHandleScope<1> hs(self);
1087 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001088 for (InstrumentationListener* listener : dex_pc_listeners_) {
1089 if (listener != nullptr) {
Alex Lightd7661582017-05-01 13:48:16 -07001090 listener->DexPcMoved(thread, thiz, method, dex_pc);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001091 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001092 }
1093}
1094
Nicolas Geoffray81f0f952016-01-20 16:25:19 +00001095void Instrumentation::BranchImpl(Thread* thread,
1096 ArtMethod* method,
1097 uint32_t dex_pc,
1098 int32_t offset) const {
1099 for (InstrumentationListener* listener : branch_listeners_) {
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001100 if (listener != nullptr) {
Nicolas Geoffray81f0f952016-01-20 16:25:19 +00001101 listener->Branch(thread, method, dex_pc, offset);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001102 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001103 }
1104}
1105
Nicolas Geoffray5550ca82015-08-21 18:38:30 +01001106void Instrumentation::InvokeVirtualOrInterfaceImpl(Thread* thread,
Alex Lightd7661582017-05-01 13:48:16 -07001107 ObjPtr<mirror::Object> this_object,
Nicolas Geoffray5550ca82015-08-21 18:38:30 +01001108 ArtMethod* caller,
1109 uint32_t dex_pc,
1110 ArtMethod* callee) const {
Alex Lightd7661582017-05-01 13:48:16 -07001111 Thread* self = Thread::Current();
1112 StackHandleScope<1> hs(self);
1113 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
Nicolas Geoffray5550ca82015-08-21 18:38:30 +01001114 for (InstrumentationListener* listener : invoke_virtual_or_interface_listeners_) {
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001115 if (listener != nullptr) {
Alex Lightd7661582017-05-01 13:48:16 -07001116 listener->InvokeVirtualOrInterface(thread, thiz, caller, dex_pc, callee);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001117 }
Nicolas Geoffray5550ca82015-08-21 18:38:30 +01001118 }
1119}
1120
Alex Lighte814f9d2017-07-31 16:14:39 -07001121void Instrumentation::WatchedFramePopImpl(Thread* thread, const ShadowFrame& frame) const {
1122 for (InstrumentationListener* listener : watched_frame_pop_listeners_) {
1123 if (listener != nullptr) {
1124 listener->WatchedFramePop(thread, frame);
1125 }
1126 }
1127}
1128
Alex Lightd7661582017-05-01 13:48:16 -07001129void Instrumentation::FieldReadEventImpl(Thread* thread,
1130 ObjPtr<mirror::Object> this_object,
1131 ArtMethod* method,
1132 uint32_t dex_pc,
Mathieu Chartierc7853442015-03-27 14:35:38 -07001133 ArtField* field) const {
Alex Lightd7661582017-05-01 13:48:16 -07001134 Thread* self = Thread::Current();
1135 StackHandleScope<1> hs(self);
1136 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001137 for (InstrumentationListener* listener : field_read_listeners_) {
1138 if (listener != nullptr) {
Alex Lightd7661582017-05-01 13:48:16 -07001139 listener->FieldRead(thread, thiz, method, dex_pc, field);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001140 }
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +02001141 }
1142}
1143
Alex Lightd7661582017-05-01 13:48:16 -07001144void Instrumentation::FieldWriteEventImpl(Thread* thread,
1145 ObjPtr<mirror::Object> this_object,
1146 ArtMethod* method,
1147 uint32_t dex_pc,
1148 ArtField* field,
1149 const JValue& field_value) const {
1150 Thread* self = Thread::Current();
1151 StackHandleScope<2> hs(self);
1152 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
1153 if (field->IsPrimitiveType()) {
1154 for (InstrumentationListener* listener : field_write_listeners_) {
1155 if (listener != nullptr) {
1156 listener->FieldWritten(thread, thiz, method, dex_pc, field, field_value);
1157 }
1158 }
1159 } else {
1160 Handle<mirror::Object> val(hs.NewHandle(field_value.GetL()));
1161 for (InstrumentationListener* listener : field_write_listeners_) {
1162 if (listener != nullptr) {
1163 listener->FieldWritten(thread, thiz, method, dex_pc, field, val);
1164 }
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001165 }
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +02001166 }
1167}
1168
Alex Light6e1607e2017-08-23 10:06:18 -07001169void Instrumentation::ExceptionThrownEvent(Thread* thread,
Sebastien Hertz947ff082013-09-17 14:10:13 +02001170 mirror::Throwable* exception_object) const {
Hiroshi Yamauchi3481f7a2017-02-10 12:07:36 -08001171 Thread* self = Thread::Current();
1172 StackHandleScope<1> hs(self);
1173 Handle<mirror::Throwable> h_exception(hs.NewHandle(exception_object));
Alex Light6e1607e2017-08-23 10:06:18 -07001174 if (HasExceptionThrownListeners()) {
Hiroshi Yamauchi3481f7a2017-02-10 12:07:36 -08001175 DCHECK_EQ(thread->GetException(), h_exception.Get());
Jeff Haoc0bd4da2013-04-11 15:52:28 -07001176 thread->ClearException();
Alex Light6e1607e2017-08-23 10:06:18 -07001177 for (InstrumentationListener* listener : exception_thrown_listeners_) {
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001178 if (listener != nullptr) {
Alex Light6e1607e2017-08-23 10:06:18 -07001179 listener->ExceptionThrown(thread, h_exception);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001180 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001181 }
Alex Light9fb1ab12017-09-05 09:32:49 -07001182 // See b/65049545 for discussion about this behavior.
1183 thread->AssertNoPendingException();
Hiroshi Yamauchi3481f7a2017-02-10 12:07:36 -08001184 thread->SetException(h_exception.Get());
Ian Rogers62d6c772013-02-27 08:32:07 -08001185 }
1186}
1187
Alex Light9fb1ab12017-09-05 09:32:49 -07001188void Instrumentation::ExceptionHandledEvent(Thread* thread,
1189 mirror::Throwable* exception_object) const {
1190 Thread* self = Thread::Current();
1191 StackHandleScope<1> hs(self);
1192 Handle<mirror::Throwable> h_exception(hs.NewHandle(exception_object));
1193 if (HasExceptionHandledListeners()) {
1194 // We should have cleared the exception so that callers can detect a new one.
1195 DCHECK(thread->GetException() == nullptr);
1196 for (InstrumentationListener* listener : exception_handled_listeners_) {
1197 if (listener != nullptr) {
1198 listener->ExceptionHandled(thread, h_exception);
1199 }
1200 }
1201 }
1202}
1203
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +00001204// Computes a frame ID by ignoring inlined frames.
1205size_t Instrumentation::ComputeFrameId(Thread* self,
1206 size_t frame_depth,
1207 size_t inlined_frames_before_frame) {
1208 CHECK_GE(frame_depth, inlined_frames_before_frame);
1209 size_t no_inline_depth = frame_depth - inlined_frames_before_frame;
1210 return StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk) - no_inline_depth;
1211}
1212
Ian Rogers62d6c772013-02-27 08:32:07 -08001213static void CheckStackDepth(Thread* self, const InstrumentationStackFrame& instrumentation_frame,
1214 int delta)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001215 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +01001216 size_t frame_id = StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk) + delta;
Ian Rogers62d6c772013-02-27 08:32:07 -08001217 if (frame_id != instrumentation_frame.frame_id_) {
1218 LOG(ERROR) << "Expected frame_id=" << frame_id << " but found "
1219 << instrumentation_frame.frame_id_;
1220 StackVisitor::DescribeStack(self);
1221 CHECK_EQ(frame_id, instrumentation_frame.frame_id_);
1222 }
1223}
1224
1225void Instrumentation::PushInstrumentationStackFrame(Thread* self, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001226 ArtMethod* method,
Jeff Hao9a916d32013-06-27 18:45:37 -07001227 uintptr_t lr, bool interpreter_entry) {
Alex Lightb7edcda2017-04-27 13:20:31 -07001228 DCHECK(!self->IsExceptionPending());
Ian Rogers62d6c772013-02-27 08:32:07 -08001229 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
1230 if (kVerboseInstrumentation) {
David Sehr709b0702016-10-13 09:12:37 -07001231 LOG(INFO) << "Entering " << ArtMethod::PrettyMethod(method) << " from PC "
1232 << reinterpret_cast<void*>(lr);
Ian Rogers62d6c772013-02-27 08:32:07 -08001233 }
Alex Lightb7edcda2017-04-27 13:20:31 -07001234
1235 // We send the enter event before pushing the instrumentation frame to make cleanup easier. If the
1236 // event causes an exception we can simply send the unwind event and return.
1237 StackHandleScope<1> hs(self);
1238 Handle<mirror::Object> h_this(hs.NewHandle(this_object));
1239 if (!interpreter_entry) {
1240 MethodEnterEvent(self, h_this.Get(), method, 0);
1241 if (self->IsExceptionPending()) {
1242 MethodUnwindEvent(self, h_this.Get(), method, 0);
1243 return;
1244 }
1245 }
1246
1247 // We have a callee-save frame meaning this value is guaranteed to never be 0.
1248 DCHECK(!self->IsExceptionPending());
1249 size_t frame_id = StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk);
1250
1251 instrumentation::InstrumentationStackFrame instrumentation_frame(h_this.Get(), method, lr,
Jeff Hao9a916d32013-06-27 18:45:37 -07001252 frame_id, interpreter_entry);
Ian Rogers62d6c772013-02-27 08:32:07 -08001253 stack->push_front(instrumentation_frame);
Ian Rogers62d6c772013-02-27 08:32:07 -08001254}
1255
Mingyao Yang2ee17902017-08-30 11:37:08 -07001256DeoptimizationMethodType Instrumentation::GetDeoptimizationMethodType(ArtMethod* method) {
1257 if (method->IsRuntimeMethod()) {
1258 // Certain methods have strict requirement on whether the dex instruction
1259 // should be re-executed upon deoptimization.
1260 if (method == Runtime::Current()->GetCalleeSaveMethod(
1261 CalleeSaveType::kSaveEverythingForClinit)) {
1262 return DeoptimizationMethodType::kKeepDexPc;
1263 }
1264 if (method == Runtime::Current()->GetCalleeSaveMethod(
1265 CalleeSaveType::kSaveEverythingForSuspendCheck)) {
1266 return DeoptimizationMethodType::kKeepDexPc;
1267 }
1268 }
1269 return DeoptimizationMethodType::kDefault;
1270}
1271
1272// Try to get the shorty of a runtime method if it's an invocation stub.
1273struct RuntimeMethodShortyVisitor : public StackVisitor {
1274 explicit RuntimeMethodShortyVisitor(Thread* thread)
1275 : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames),
1276 shorty('V') {}
1277
1278 bool VisitFrame() REQUIRES_SHARED(Locks::mutator_lock_) {
1279 ArtMethod* m = GetMethod();
1280 if (m != nullptr && !m->IsRuntimeMethod()) {
1281 // The first Java method.
1282 if (m->IsNative()) {
1283 // Use JNI method's shorty for the jni stub.
1284 shorty = m->GetShorty()[0];
1285 return false;
1286 }
1287 if (m->IsProxyMethod()) {
1288 // Proxy method just invokes its proxied method via
1289 // art_quick_proxy_invoke_handler.
1290 shorty = m->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty()[0];
1291 return false;
1292 }
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001293 const Instruction& instr = m->DexInstructions().InstructionAt(GetDexPc());
1294 if (instr.IsInvoke()) {
Mingyao Yang2ee17902017-08-30 11:37:08 -07001295 const DexFile* dex_file = m->GetDexFile();
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001296 if (interpreter::IsStringInit(dex_file, instr.VRegB())) {
Mingyao Yang2ee17902017-08-30 11:37:08 -07001297 // Invoking string init constructor is turned into invoking
1298 // StringFactory.newStringFromChars() which returns a string.
1299 shorty = 'L';
1300 return false;
1301 }
1302 // A regular invoke, use callee's shorty.
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001303 uint32_t method_idx = instr.VRegB();
Mingyao Yang2ee17902017-08-30 11:37:08 -07001304 shorty = dex_file->GetMethodShorty(method_idx)[0];
1305 }
1306 // Stop stack walking since we've seen a Java frame.
1307 return false;
1308 }
1309 return true;
1310 }
1311
1312 char shorty;
1313};
1314
Alex Lightb7edcda2017-04-27 13:20:31 -07001315TwoWordReturn Instrumentation::PopInstrumentationStackFrame(Thread* self,
1316 uintptr_t* return_pc,
1317 uint64_t* gpr_result,
1318 uint64_t* fpr_result) {
1319 DCHECK(gpr_result != nullptr);
1320 DCHECK(fpr_result != nullptr);
Ian Rogers62d6c772013-02-27 08:32:07 -08001321 // Do the pop.
1322 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
1323 CHECK_GT(stack->size(), 0U);
1324 InstrumentationStackFrame instrumentation_frame = stack->front();
1325 stack->pop_front();
1326
1327 // Set return PC and check the sanity of the stack.
1328 *return_pc = instrumentation_frame.return_pc_;
1329 CheckStackDepth(self, instrumentation_frame, 0);
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001330 self->VerifyStack();
Ian Rogers62d6c772013-02-27 08:32:07 -08001331
Mathieu Chartiere401d142015-04-22 13:56:20 -07001332 ArtMethod* method = instrumentation_frame.method_;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001333 uint32_t length;
Andreas Gampe542451c2016-07-26 09:02:02 -07001334 const PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Mingyao Yang2ee17902017-08-30 11:37:08 -07001335 char return_shorty;
1336
1337 // Runtime method does not call into MethodExitEvent() so there should not be
1338 // suspension point below.
1339 ScopedAssertNoThreadSuspension ants(__FUNCTION__, method->IsRuntimeMethod());
1340 if (method->IsRuntimeMethod()) {
1341 if (method != Runtime::Current()->GetCalleeSaveMethod(
1342 CalleeSaveType::kSaveEverythingForClinit)) {
1343 // If the caller is at an invocation point and the runtime method is not
1344 // for clinit, we need to pass return results to the caller.
1345 // We need the correct shorty to decide whether we need to pass the return
1346 // result for deoptimization below.
1347 RuntimeMethodShortyVisitor visitor(self);
1348 visitor.WalkStack();
1349 return_shorty = visitor.shorty;
1350 } else {
1351 // Some runtime methods such as allocations, unresolved field getters, etc.
1352 // have return value. We don't need to set return_value since MethodExitEvent()
1353 // below isn't called for runtime methods. Deoptimization doesn't need the
1354 // value either since the dex instruction will be re-executed by the
1355 // interpreter, except these two cases:
1356 // (1) For an invoke, which is handled above to get the correct shorty.
1357 // (2) For MONITOR_ENTER/EXIT, which cannot be re-executed since it's not
1358 // idempotent. However there is no return value for it anyway.
1359 return_shorty = 'V';
1360 }
1361 } else {
1362 return_shorty = method->GetInterfaceMethodIfProxy(pointer_size)->GetShorty(&length)[0];
1363 }
1364
Alex Lightb7edcda2017-04-27 13:20:31 -07001365 bool is_ref = return_shorty == '[' || return_shorty == 'L';
1366 StackHandleScope<1> hs(self);
1367 MutableHandle<mirror::Object> res(hs.NewHandle<mirror::Object>(nullptr));
Ian Rogers62d6c772013-02-27 08:32:07 -08001368 JValue return_value;
1369 if (return_shorty == 'V') {
1370 return_value.SetJ(0);
1371 } else if (return_shorty == 'F' || return_shorty == 'D') {
Alex Lightb7edcda2017-04-27 13:20:31 -07001372 return_value.SetJ(*fpr_result);
Ian Rogers62d6c772013-02-27 08:32:07 -08001373 } else {
Alex Lightb7edcda2017-04-27 13:20:31 -07001374 return_value.SetJ(*gpr_result);
1375 }
1376 if (is_ref) {
1377 // Take a handle to the return value so we won't lose it if we suspend.
1378 res.Assign(return_value.GetL());
Ian Rogers62d6c772013-02-27 08:32:07 -08001379 }
1380 // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to
1381 // return_pc.
Andreas Gampee2abbc62017-09-15 11:59:26 -07001382 uint32_t dex_pc = dex::kDexNoIndex;
Ian Rogers62d6c772013-02-27 08:32:07 -08001383 mirror::Object* this_object = instrumentation_frame.this_object_;
Mingyao Yang2ee17902017-08-30 11:37:08 -07001384 if (!method->IsRuntimeMethod() && !instrumentation_frame.interpreter_entry_) {
Sebastien Hertz320deb22014-06-11 19:45:05 +02001385 MethodExitEvent(self, this_object, instrumentation_frame.method_, dex_pc, return_value);
1386 }
jeffhao725a9572012-11-13 18:20:12 -08001387
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01001388 // Deoptimize if the caller needs to continue execution in the interpreter. Do nothing if we get
1389 // back to an upcall.
1390 NthCallerVisitor visitor(self, 1, true);
1391 visitor.WalkStack(true);
Sebastien Hertz270a0e12015-01-16 19:49:09 +01001392 bool deoptimize = (visitor.caller != nullptr) &&
Daniel Mihalyieb076692014-08-22 17:33:31 +02001393 (interpreter_stubs_installed_ || IsDeoptimized(visitor.caller) ||
1394 Dbg::IsForcedInterpreterNeededForUpcall(self, visitor.caller));
Alex Lightb7edcda2017-04-27 13:20:31 -07001395 if (is_ref) {
1396 // Restore the return value if it's a reference since it might have moved.
1397 *reinterpret_cast<mirror::Object**>(gpr_result) = res.Get();
1398 }
Nicolas Geoffraya0619e22016-12-20 13:57:43 +00001399 if (deoptimize && Runtime::Current()->IsAsyncDeoptimizeable(*return_pc)) {
Ian Rogers62d6c772013-02-27 08:32:07 -08001400 if (kVerboseInstrumentation) {
Andreas Gampe46ee31b2016-12-14 10:11:49 -08001401 LOG(INFO) << "Deoptimizing "
1402 << visitor.caller->PrettyMethod()
1403 << " by returning from "
1404 << method->PrettyMethod()
1405 << " with result "
1406 << std::hex << return_value.GetJ() << std::dec
1407 << " in "
1408 << *self;
Ian Rogers62d6c772013-02-27 08:32:07 -08001409 }
Mingyao Yang2ee17902017-08-30 11:37:08 -07001410 DeoptimizationMethodType deopt_method_type = GetDeoptimizationMethodType(method);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01001411 self->PushDeoptimizationContext(return_value,
Mingyao Yang2ee17902017-08-30 11:37:08 -07001412 return_shorty == 'L' || return_shorty == '[',
1413 nullptr /* no pending exception */,
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01001414 false /* from_code */,
Mingyao Yang2ee17902017-08-30 11:37:08 -07001415 deopt_method_type);
Andreas Gamped58342c2014-06-05 14:18:08 -07001416 return GetTwoWordSuccessValue(*return_pc,
1417 reinterpret_cast<uintptr_t>(GetQuickDeoptimizationEntryPoint()));
Ian Rogers62d6c772013-02-27 08:32:07 -08001418 } else {
Nicolas Geoffraya0619e22016-12-20 13:57:43 +00001419 if (deoptimize && !Runtime::Current()->IsAsyncDeoptimizeable(*return_pc)) {
Alex Lightd8eb6732018-01-29 15:16:02 -08001420 VLOG(deopt) << "Got a deoptimization request on un-deoptimizable " << method->PrettyMethod()
1421 << " at PC " << reinterpret_cast<void*>(*return_pc);
Nicolas Geoffraya0619e22016-12-20 13:57:43 +00001422 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001423 if (kVerboseInstrumentation) {
David Sehr709b0702016-10-13 09:12:37 -07001424 LOG(INFO) << "Returning from " << method->PrettyMethod()
Brian Carlstrom2d888622013-07-18 17:02:00 -07001425 << " to PC " << reinterpret_cast<void*>(*return_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -08001426 }
Andreas Gamped58342c2014-06-05 14:18:08 -07001427 return GetTwoWordSuccessValue(0, *return_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -08001428 }
jeffhao725a9572012-11-13 18:20:12 -08001429}
1430
Mingyao Yangf711f2c2016-05-23 12:29:39 -07001431uintptr_t Instrumentation::PopMethodForUnwind(Thread* self, bool is_deoptimization) const {
Ian Rogers62d6c772013-02-27 08:32:07 -08001432 // Do the pop.
1433 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
1434 CHECK_GT(stack->size(), 0U);
Alex Lightb7edcda2017-04-27 13:20:31 -07001435 size_t idx = stack->size();
Ian Rogers62d6c772013-02-27 08:32:07 -08001436 InstrumentationStackFrame instrumentation_frame = stack->front();
Ian Rogers62d6c772013-02-27 08:32:07 -08001437
Mathieu Chartiere401d142015-04-22 13:56:20 -07001438 ArtMethod* method = instrumentation_frame.method_;
Ian Rogers62d6c772013-02-27 08:32:07 -08001439 if (is_deoptimization) {
1440 if (kVerboseInstrumentation) {
David Sehr709b0702016-10-13 09:12:37 -07001441 LOG(INFO) << "Popping for deoptimization " << ArtMethod::PrettyMethod(method);
Ian Rogers62d6c772013-02-27 08:32:07 -08001442 }
1443 } else {
1444 if (kVerboseInstrumentation) {
David Sehr709b0702016-10-13 09:12:37 -07001445 LOG(INFO) << "Popping for unwind " << ArtMethod::PrettyMethod(method);
Ian Rogers62d6c772013-02-27 08:32:07 -08001446 }
1447
1448 // Notify listeners of method unwind.
1449 // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to
1450 // return_pc.
Andreas Gampee2abbc62017-09-15 11:59:26 -07001451 uint32_t dex_pc = dex::kDexNoIndex;
Mingyao Yang2ee17902017-08-30 11:37:08 -07001452 if (!method->IsRuntimeMethod()) {
1453 MethodUnwindEvent(self, instrumentation_frame.this_object_, method, dex_pc);
1454 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001455 }
Alex Lightb7edcda2017-04-27 13:20:31 -07001456 // TODO: bring back CheckStackDepth(self, instrumentation_frame, 2);
1457 CHECK_EQ(stack->size(), idx);
1458 DCHECK(instrumentation_frame.method_ == stack->front().method_);
1459 stack->pop_front();
Mingyao Yangf711f2c2016-05-23 12:29:39 -07001460 return instrumentation_frame.return_pc_;
Ian Rogers62d6c772013-02-27 08:32:07 -08001461}
1462
1463std::string InstrumentationStackFrame::Dump() const {
1464 std::ostringstream os;
David Sehr709b0702016-10-13 09:12:37 -07001465 os << "Frame " << frame_id_ << " " << ArtMethod::PrettyMethod(method_) << ":"
Ian Rogers62d6c772013-02-27 08:32:07 -08001466 << reinterpret_cast<void*>(return_pc_) << " this=" << reinterpret_cast<void*>(this_object_);
1467 return os.str();
1468}
1469
1470} // namespace instrumentation
jeffhao725a9572012-11-13 18:20:12 -08001471} // namespace art