blob: d7f33d5e43796896c652967d6ca84e63e063e496 [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 Sehr8f4b0562018-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 Lightd698ef52018-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 Lightd698ef52018-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.
157 if (method->IsConstructor() &&
158 method->GetDeclaringClass()->DescriptorEquals("Ljava/lang/reflect/Proxy;")) {
Jeff Haodb8a6642014-08-14 17:18:52 -0700159 return;
160 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800161 const void* new_quick_code;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100162 bool uninstall = !entry_exit_stubs_installed_ && !interpreter_stubs_installed_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800163 Runtime* const runtime = Runtime::Current();
164 ClassLinker* const class_linker = runtime->GetClassLinker();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100165 bool is_class_initialized = method->GetDeclaringClass()->IsInitialized();
166 if (uninstall) {
167 if ((forced_interpret_only_ || IsDeoptimized(method)) && !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800168 new_quick_code = GetQuickToInterpreterBridge();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100169 } else if (is_class_initialized || !method->IsStatic() || method->IsConstructor()) {
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000170 if (NeedDebugVersionFor(method)) {
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800171 new_quick_code = GetQuickToInterpreterBridge();
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000172 } else {
Alex Lightfc49fec2018-01-16 22:28:36 +0000173 new_quick_code = class_linker->GetQuickOatCodeFor(method);
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800174 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100175 } else {
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700176 new_quick_code = GetQuickResolutionStub();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100177 }
178 } else { // !uninstall
Sebastien Hertzbae182c2013-12-17 10:42:03 +0100179 if ((interpreter_stubs_installed_ || forced_interpret_only_ || IsDeoptimized(method)) &&
180 !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800181 new_quick_code = GetQuickToInterpreterBridge();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100182 } else {
183 // Do not overwrite resolution trampoline. When the trampoline initializes the method's
184 // class, all its static methods code will be set to the instrumentation entry point.
185 // For more details, see ClassLinker::FixupStaticTrampolines.
186 if (is_class_initialized || !method->IsStatic() || method->IsConstructor()) {
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000187 if (NeedDebugVersionFor(method)) {
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800188 // Oat code should not be used. Don't install instrumentation stub and
189 // use interpreter for instrumentation.
190 new_quick_code = GetQuickToInterpreterBridge();
191 } else if (entry_exit_stubs_installed_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800192 new_quick_code = GetQuickInstrumentationEntryPoint();
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000193 } else {
Alex Lightfc49fec2018-01-16 22:28:36 +0000194 new_quick_code = class_linker->GetQuickOatCodeFor(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100195 }
196 } else {
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700197 new_quick_code = GetQuickResolutionStub();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100198 }
199 }
200 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800201 UpdateEntrypoints(method, new_quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100202}
203
Ian Rogers62d6c772013-02-27 08:32:07 -0800204// Places the instrumentation exit pc as the return PC for every quick frame. This also allows
205// deoptimization of quick frames to interpreter frames.
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100206// Since we may already have done this previously, we need to push new instrumentation frame before
207// existing instrumentation frames.
Ian Rogers62d6c772013-02-27 08:32:07 -0800208static void InstrumentationInstallStack(Thread* thread, void* arg)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700209 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200210 struct InstallStackVisitor FINAL : public StackVisitor {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800211 InstallStackVisitor(Thread* thread_in, Context* context, uintptr_t instrumentation_exit_pc)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100212 : StackVisitor(thread_in, context, kInstrumentationStackWalk),
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800213 instrumentation_stack_(thread_in->GetInstrumentationStack()),
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100214 instrumentation_exit_pc_(instrumentation_exit_pc),
Alex Light667df0e2018-03-08 16:55:58 -0800215 reached_existing_instrumentation_frames_(false), instrumentation_stack_depth_(0),
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100216 last_return_pc_(0) {
217 }
jeffhao725a9572012-11-13 18:20:12 -0800218
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700219 bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700220 ArtMethod* m = GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700221 if (m == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800222 if (kVerboseInstrumentation) {
223 LOG(INFO) << " Skipping upcall. Frame " << GetFrameId();
224 }
225 last_return_pc_ = 0;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700226 return true; // Ignore upcalls.
Ian Rogers306057f2012-11-26 12:45:53 -0800227 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700228 if (GetCurrentQuickFrame() == nullptr) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800229 bool interpreter_frame = true;
Sebastien Hertz320deb22014-06-11 19:45:05 +0200230 InstrumentationStackFrame instrumentation_frame(GetThisObject(), m, 0, GetFrameId(),
231 interpreter_frame);
Jeff Haoa15a81b2014-05-27 18:25:47 -0700232 if (kVerboseInstrumentation) {
233 LOG(INFO) << "Pushing shadow frame " << instrumentation_frame.Dump();
234 }
235 shadow_stack_.push_back(instrumentation_frame);
236 return true; // Continue.
237 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800238 uintptr_t return_pc = GetReturnPc();
Sebastien Hertz320deb22014-06-11 19:45:05 +0200239 if (kVerboseInstrumentation) {
240 LOG(INFO) << " Installing exit stub in " << DescribeLocation();
241 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100242 if (return_pc == instrumentation_exit_pc_) {
Mingyao Yang2ee17902017-08-30 11:37:08 -0700243 CHECK_LT(instrumentation_stack_depth_, instrumentation_stack_->size());
244
245 if (m->IsRuntimeMethod()) {
246 const InstrumentationStackFrame& frame =
247 instrumentation_stack_->at(instrumentation_stack_depth_);
248 if (frame.interpreter_entry_) {
249 // This instrumentation frame is for an interpreter bridge and is
250 // pushed when executing the instrumented interpreter bridge. So method
251 // enter event must have been reported. However we need to push a DEX pc
252 // into the dex_pcs_ list to match size of instrumentation stack.
Andreas Gampee2abbc62017-09-15 11:59:26 -0700253 uint32_t dex_pc = dex::kDexNoIndex;
Mingyao Yang2ee17902017-08-30 11:37:08 -0700254 dex_pcs_.push_back(dex_pc);
255 last_return_pc_ = frame.return_pc_;
256 ++instrumentation_stack_depth_;
257 return true;
258 }
259 }
260
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100261 // We've reached a frame which has already been installed with instrumentation exit stub.
Alex Lightdc80a712018-03-08 14:01:44 -0800262 // We should have already installed instrumentation or be interpreter on previous frames.
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100263 reached_existing_instrumentation_frames_ = true;
264
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200265 const InstrumentationStackFrame& frame =
266 instrumentation_stack_->at(instrumentation_stack_depth_);
David Sehr709b0702016-10-13 09:12:37 -0700267 CHECK_EQ(m, frame.method_) << "Expected " << ArtMethod::PrettyMethod(m)
268 << ", Found " << ArtMethod::PrettyMethod(frame.method_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100269 return_pc = frame.return_pc_;
270 if (kVerboseInstrumentation) {
271 LOG(INFO) << "Ignoring already instrumented " << frame.Dump();
272 }
273 } else {
274 CHECK_NE(return_pc, 0U);
Alex Lightdc80a712018-03-08 14:01:44 -0800275 if (UNLIKELY(reached_existing_instrumentation_frames_ && !m->IsRuntimeMethod())) {
276 // We already saw an existing instrumentation frame so this should be a runtime-method
277 // inserted by the interpreter or runtime.
Alex Light667df0e2018-03-08 16:55:58 -0800278 std::string thread_name;
279 GetThread()->GetThreadName(thread_name);
280 uint32_t dex_pc = dex::kDexNoIndex;
281 if (last_return_pc_ != 0 &&
282 GetCurrentOatQuickMethodHeader() != nullptr) {
283 dex_pc = GetCurrentOatQuickMethodHeader()->ToDexPc(m, last_return_pc_);
284 }
Alex Lightdc80a712018-03-08 14:01:44 -0800285 LOG(FATAL) << "While walking " << thread_name << " found unexpected non-runtime method"
286 << " without instrumentation exit return or interpreter frame."
Alex Light667df0e2018-03-08 16:55:58 -0800287 << " method is " << GetMethod()->PrettyMethod()
288 << " return_pc is " << std::hex << return_pc
289 << " dex pc: " << dex_pc;
290 UNREACHABLE();
291 }
Mingyao Yang2ee17902017-08-30 11:37:08 -0700292 InstrumentationStackFrame instrumentation_frame(
293 m->IsRuntimeMethod() ? nullptr : GetThisObject(),
294 m,
295 return_pc,
296 GetFrameId(), // A runtime method still gets a frame id.
297 false);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100298 if (kVerboseInstrumentation) {
299 LOG(INFO) << "Pushing frame " << instrumentation_frame.Dump();
300 }
301
Sebastien Hertz320deb22014-06-11 19:45:05 +0200302 // Insert frame at the right position so we do not corrupt the instrumentation stack.
303 // Instrumentation stack frames are in descending frame id order.
304 auto it = instrumentation_stack_->begin();
305 for (auto end = instrumentation_stack_->end(); it != end; ++it) {
306 const InstrumentationStackFrame& current = *it;
307 if (instrumentation_frame.frame_id_ >= current.frame_id_) {
308 break;
309 }
310 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100311 instrumentation_stack_->insert(it, instrumentation_frame);
312 SetReturnPc(instrumentation_exit_pc_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800313 }
Andreas Gampee2abbc62017-09-15 11:59:26 -0700314 uint32_t dex_pc = dex::kDexNoIndex;
Mingyao Yang2ee17902017-08-30 11:37:08 -0700315 if (last_return_pc_ != 0 &&
316 GetCurrentOatQuickMethodHeader() != nullptr) {
317 dex_pc = GetCurrentOatQuickMethodHeader()->ToDexPc(m, last_return_pc_);
318 }
319 dex_pcs_.push_back(dex_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800320 last_return_pc_ = return_pc;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100321 ++instrumentation_stack_depth_;
Ian Rogers306057f2012-11-26 12:45:53 -0800322 return true; // Continue.
323 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800324 std::deque<InstrumentationStackFrame>* const instrumentation_stack_;
Jeff Haoa15a81b2014-05-27 18:25:47 -0700325 std::vector<InstrumentationStackFrame> shadow_stack_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800326 std::vector<uint32_t> dex_pcs_;
Ian Rogers306057f2012-11-26 12:45:53 -0800327 const uintptr_t instrumentation_exit_pc_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100328 bool reached_existing_instrumentation_frames_;
329 size_t instrumentation_stack_depth_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800330 uintptr_t last_return_pc_;
Ian Rogers306057f2012-11-26 12:45:53 -0800331 };
Ian Rogers62d6c772013-02-27 08:32:07 -0800332 if (kVerboseInstrumentation) {
333 std::string thread_name;
334 thread->GetThreadName(thread_name);
335 LOG(INFO) << "Installing exit stubs in " << thread_name;
Ian Rogers306057f2012-11-26 12:45:53 -0800336 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100337
338 Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg);
Ian Rogers700a4022014-05-19 16:49:03 -0700339 std::unique_ptr<Context> context(Context::Create());
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700340 uintptr_t instrumentation_exit_pc = reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc());
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100341 InstallStackVisitor visitor(thread, context.get(), instrumentation_exit_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800342 visitor.WalkStack(true);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100343 CHECK_EQ(visitor.dex_pcs_.size(), thread->GetInstrumentationStack()->size());
Ian Rogers62d6c772013-02-27 08:32:07 -0800344
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100345 if (instrumentation->ShouldNotifyMethodEnterExitEvents()) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100346 // Create method enter events for all methods currently on the thread's stack. We only do this
347 // if no debugger is attached to prevent from posting events twice.
Jeff Haoa15a81b2014-05-27 18:25:47 -0700348 auto ssi = visitor.shadow_stack_.rbegin();
349 for (auto isi = thread->GetInstrumentationStack()->rbegin(),
350 end = thread->GetInstrumentationStack()->rend(); isi != end; ++isi) {
351 while (ssi != visitor.shadow_stack_.rend() && (*ssi).frame_id_ < (*isi).frame_id_) {
352 instrumentation->MethodEnterEvent(thread, (*ssi).this_object_, (*ssi).method_, 0);
353 ++ssi;
354 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100355 uint32_t dex_pc = visitor.dex_pcs_.back();
356 visitor.dex_pcs_.pop_back();
Sebastien Hertz320deb22014-06-11 19:45:05 +0200357 if (!isi->interpreter_entry_) {
358 instrumentation->MethodEnterEvent(thread, (*isi).this_object_, (*isi).method_, dex_pc);
359 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100360 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800361 }
362 thread->VerifyStack();
Ian Rogers306057f2012-11-26 12:45:53 -0800363}
364
Mingyao Yang99170c62015-07-06 11:10:37 -0700365void Instrumentation::InstrumentThreadStack(Thread* thread) {
366 instrumentation_stubs_installed_ = true;
367 InstrumentationInstallStack(thread, this);
368}
369
Ian Rogers62d6c772013-02-27 08:32:07 -0800370// Removes the instrumentation exit pc as the return PC for every quick frame.
371static void InstrumentationRestoreStack(Thread* thread, void* arg)
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +0000372 REQUIRES(Locks::mutator_lock_) {
373 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
374
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200375 struct RestoreStackVisitor FINAL : public StackVisitor {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800376 RestoreStackVisitor(Thread* thread_in, uintptr_t instrumentation_exit_pc,
Ian Rogers62d6c772013-02-27 08:32:07 -0800377 Instrumentation* instrumentation)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100378 : StackVisitor(thread_in, nullptr, kInstrumentationStackWalk),
379 thread_(thread_in),
Ian Rogers62d6c772013-02-27 08:32:07 -0800380 instrumentation_exit_pc_(instrumentation_exit_pc),
381 instrumentation_(instrumentation),
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800382 instrumentation_stack_(thread_in->GetInstrumentationStack()),
Ian Rogers62d6c772013-02-27 08:32:07 -0800383 frames_removed_(0) {}
Ian Rogers306057f2012-11-26 12:45:53 -0800384
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700385 bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800386 if (instrumentation_stack_->size() == 0) {
jeffhao725a9572012-11-13 18:20:12 -0800387 return false; // Stop.
388 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700389 ArtMethod* m = GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700390 if (GetCurrentQuickFrame() == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800391 if (kVerboseInstrumentation) {
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200392 LOG(INFO) << " Ignoring a shadow frame. Frame " << GetFrameId()
David Sehr709b0702016-10-13 09:12:37 -0700393 << " Method=" << ArtMethod::PrettyMethod(m);
Ian Rogers62d6c772013-02-27 08:32:07 -0800394 }
395 return true; // Ignore shadow frames.
396 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700397 if (m == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800398 if (kVerboseInstrumentation) {
399 LOG(INFO) << " Skipping upcall. Frame " << GetFrameId();
400 }
Ian Rogers306057f2012-11-26 12:45:53 -0800401 return true; // Ignore upcalls.
402 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800403 bool removed_stub = false;
404 // TODO: make this search more efficient?
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100405 const size_t frameId = GetFrameId();
406 for (const InstrumentationStackFrame& instrumentation_frame : *instrumentation_stack_) {
407 if (instrumentation_frame.frame_id_ == frameId) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800408 if (kVerboseInstrumentation) {
409 LOG(INFO) << " Removing exit stub in " << DescribeLocation();
410 }
Jeff Hao9a916d32013-06-27 18:45:37 -0700411 if (instrumentation_frame.interpreter_entry_) {
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700412 CHECK(m == Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs));
Jeff Hao9a916d32013-06-27 18:45:37 -0700413 } else {
David Sehr709b0702016-10-13 09:12:37 -0700414 CHECK(m == instrumentation_frame.method_) << ArtMethod::PrettyMethod(m);
Jeff Hao9a916d32013-06-27 18:45:37 -0700415 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800416 SetReturnPc(instrumentation_frame.return_pc_);
Mingyao Yang2ee17902017-08-30 11:37:08 -0700417 if (instrumentation_->ShouldNotifyMethodEnterExitEvents() &&
418 !m->IsRuntimeMethod()) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100419 // Create the method exit events. As the methods didn't really exit the result is 0.
420 // We only do this if no debugger is attached to prevent from posting events twice.
421 instrumentation_->MethodExitEvent(thread_, instrumentation_frame.this_object_, m,
422 GetDexPc(), JValue());
423 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800424 frames_removed_++;
425 removed_stub = true;
426 break;
427 }
428 }
429 if (!removed_stub) {
430 if (kVerboseInstrumentation) {
431 LOG(INFO) << " No exit stub in " << DescribeLocation();
Ian Rogers306057f2012-11-26 12:45:53 -0800432 }
jeffhao725a9572012-11-13 18:20:12 -0800433 }
434 return true; // Continue.
435 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800436 Thread* const thread_;
Ian Rogers306057f2012-11-26 12:45:53 -0800437 const uintptr_t instrumentation_exit_pc_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800438 Instrumentation* const instrumentation_;
439 std::deque<instrumentation::InstrumentationStackFrame>* const instrumentation_stack_;
440 size_t frames_removed_;
jeffhao725a9572012-11-13 18:20:12 -0800441 };
Ian Rogers62d6c772013-02-27 08:32:07 -0800442 if (kVerboseInstrumentation) {
443 std::string thread_name;
444 thread->GetThreadName(thread_name);
445 LOG(INFO) << "Removing exit stubs in " << thread_name;
446 }
447 std::deque<instrumentation::InstrumentationStackFrame>* stack = thread->GetInstrumentationStack();
448 if (stack->size() > 0) {
449 Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700450 uintptr_t instrumentation_exit_pc =
451 reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc());
Ian Rogers62d6c772013-02-27 08:32:07 -0800452 RestoreStackVisitor visitor(thread, instrumentation_exit_pc, instrumentation);
453 visitor.WalkStack(true);
454 CHECK_EQ(visitor.frames_removed_, stack->size());
455 while (stack->size() > 0) {
456 stack->pop_front();
457 }
jeffhao725a9572012-11-13 18:20:12 -0800458 }
459}
460
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200461static bool HasEvent(Instrumentation::InstrumentationEvent expected, uint32_t events) {
462 return (events & expected) != 0;
463}
464
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000465static void PotentiallyAddListenerTo(Instrumentation::InstrumentationEvent event,
466 uint32_t events,
467 std::list<InstrumentationListener*>& list,
468 InstrumentationListener* listener,
469 bool* has_listener)
470 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_) {
471 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
472 if (!HasEvent(event, events)) {
473 return;
474 }
475 // If there is a free slot in the list, we insert the listener in that slot.
476 // Otherwise we add it to the end of the list.
477 auto it = std::find(list.begin(), list.end(), nullptr);
478 if (it != list.end()) {
479 *it = listener;
480 } else {
481 list.push_back(listener);
482 }
483 *has_listener = true;
484}
485
Ian Rogers62d6c772013-02-27 08:32:07 -0800486void Instrumentation::AddListener(InstrumentationListener* listener, uint32_t events) {
487 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000488 PotentiallyAddListenerTo(kMethodEntered,
489 events,
490 method_entry_listeners_,
491 listener,
492 &have_method_entry_listeners_);
493 PotentiallyAddListenerTo(kMethodExited,
494 events,
495 method_exit_listeners_,
496 listener,
497 &have_method_exit_listeners_);
498 PotentiallyAddListenerTo(kMethodUnwind,
499 events,
500 method_unwind_listeners_,
501 listener,
502 &have_method_unwind_listeners_);
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000503 PotentiallyAddListenerTo(kBranch,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000504 events,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000505 branch_listeners_,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000506 listener,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000507 &have_branch_listeners_);
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000508 PotentiallyAddListenerTo(kInvokeVirtualOrInterface,
509 events,
510 invoke_virtual_or_interface_listeners_,
511 listener,
512 &have_invoke_virtual_or_interface_listeners_);
513 PotentiallyAddListenerTo(kDexPcMoved,
514 events,
515 dex_pc_listeners_,
516 listener,
517 &have_dex_pc_listeners_);
518 PotentiallyAddListenerTo(kFieldRead,
519 events,
520 field_read_listeners_,
521 listener,
522 &have_field_read_listeners_);
523 PotentiallyAddListenerTo(kFieldWritten,
524 events,
525 field_write_listeners_,
526 listener,
527 &have_field_write_listeners_);
Alex Light6e1607e2017-08-23 10:06:18 -0700528 PotentiallyAddListenerTo(kExceptionThrown,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000529 events,
Alex Light6e1607e2017-08-23 10:06:18 -0700530 exception_thrown_listeners_,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000531 listener,
Alex Light6e1607e2017-08-23 10:06:18 -0700532 &have_exception_thrown_listeners_);
Alex Lighte814f9d2017-07-31 16:14:39 -0700533 PotentiallyAddListenerTo(kWatchedFramePop,
534 events,
535 watched_frame_pop_listeners_,
536 listener,
537 &have_watched_frame_pop_listeners_);
Alex Light9fb1ab12017-09-05 09:32:49 -0700538 PotentiallyAddListenerTo(kExceptionHandled,
539 events,
540 exception_handled_listeners_,
541 listener,
542 &have_exception_handled_listeners_);
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200543 UpdateInterpreterHandlerTable();
jeffhao725a9572012-11-13 18:20:12 -0800544}
545
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000546static void PotentiallyRemoveListenerFrom(Instrumentation::InstrumentationEvent event,
547 uint32_t events,
548 std::list<InstrumentationListener*>& list,
549 InstrumentationListener* listener,
550 bool* has_listener)
551 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_) {
552 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
553 if (!HasEvent(event, events)) {
554 return;
555 }
556 auto it = std::find(list.begin(), list.end(), listener);
557 if (it != list.end()) {
558 // Just update the entry, do not remove from the list. Removing entries in the list
559 // is unsafe when mutators are iterating over it.
560 *it = nullptr;
561 }
562
563 // Check if the list contains any non-null listener, and update 'has_listener'.
564 for (InstrumentationListener* l : list) {
565 if (l != nullptr) {
566 *has_listener = true;
567 return;
568 }
569 }
570 *has_listener = false;
571}
572
Ian Rogers62d6c772013-02-27 08:32:07 -0800573void Instrumentation::RemoveListener(InstrumentationListener* listener, uint32_t events) {
574 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000575 PotentiallyRemoveListenerFrom(kMethodEntered,
576 events,
577 method_entry_listeners_,
578 listener,
579 &have_method_entry_listeners_);
580 PotentiallyRemoveListenerFrom(kMethodExited,
581 events,
582 method_exit_listeners_,
583 listener,
584 &have_method_exit_listeners_);
585 PotentiallyRemoveListenerFrom(kMethodUnwind,
586 events,
587 method_unwind_listeners_,
588 listener,
589 &have_method_unwind_listeners_);
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000590 PotentiallyRemoveListenerFrom(kBranch,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000591 events,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000592 branch_listeners_,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000593 listener,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000594 &have_branch_listeners_);
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000595 PotentiallyRemoveListenerFrom(kInvokeVirtualOrInterface,
596 events,
597 invoke_virtual_or_interface_listeners_,
598 listener,
599 &have_invoke_virtual_or_interface_listeners_);
600 PotentiallyRemoveListenerFrom(kDexPcMoved,
601 events,
602 dex_pc_listeners_,
603 listener,
604 &have_dex_pc_listeners_);
605 PotentiallyRemoveListenerFrom(kFieldRead,
606 events,
607 field_read_listeners_,
608 listener,
609 &have_field_read_listeners_);
610 PotentiallyRemoveListenerFrom(kFieldWritten,
611 events,
612 field_write_listeners_,
613 listener,
614 &have_field_write_listeners_);
Alex Light6e1607e2017-08-23 10:06:18 -0700615 PotentiallyRemoveListenerFrom(kExceptionThrown,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000616 events,
Alex Light6e1607e2017-08-23 10:06:18 -0700617 exception_thrown_listeners_,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000618 listener,
Alex Light6e1607e2017-08-23 10:06:18 -0700619 &have_exception_thrown_listeners_);
Alex Lighte814f9d2017-07-31 16:14:39 -0700620 PotentiallyRemoveListenerFrom(kWatchedFramePop,
621 events,
622 watched_frame_pop_listeners_,
623 listener,
624 &have_watched_frame_pop_listeners_);
Alex Light9fb1ab12017-09-05 09:32:49 -0700625 PotentiallyRemoveListenerFrom(kExceptionHandled,
626 events,
627 exception_handled_listeners_,
628 listener,
629 &have_exception_handled_listeners_);
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200630 UpdateInterpreterHandlerTable();
jeffhao725a9572012-11-13 18:20:12 -0800631}
632
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200633Instrumentation::InstrumentationLevel Instrumentation::GetCurrentInstrumentationLevel() const {
Alex Light4ba388a2017-01-27 10:26:49 -0800634 if (interpreter_stubs_installed_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200635 return InstrumentationLevel::kInstrumentWithInterpreter;
Ian Rogers62d6c772013-02-27 08:32:07 -0800636 } else if (entry_exit_stubs_installed_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200637 return InstrumentationLevel::kInstrumentWithInstrumentationStubs;
Ian Rogers62d6c772013-02-27 08:32:07 -0800638 } else {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200639 return InstrumentationLevel::kInstrumentNothing;
Ian Rogers62d6c772013-02-27 08:32:07 -0800640 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200641}
642
Alex Lightdba61482016-12-21 08:20:29 -0800643bool Instrumentation::RequiresInstrumentationInstallation(InstrumentationLevel new_level) const {
Alex Light4ba388a2017-01-27 10:26:49 -0800644 // We need to reinstall instrumentation if we go to a different level.
645 return GetCurrentInstrumentationLevel() != new_level;
Alex Lightdba61482016-12-21 08:20:29 -0800646}
647
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200648void Instrumentation::ConfigureStubs(const char* key, InstrumentationLevel desired_level) {
649 // Store the instrumentation level for this key or remove it.
650 if (desired_level == InstrumentationLevel::kInstrumentNothing) {
651 // The client no longer needs instrumentation.
652 requested_instrumentation_levels_.erase(key);
653 } else {
654 // The client needs instrumentation.
655 requested_instrumentation_levels_.Overwrite(key, desired_level);
656 }
657
658 // Look for the highest required instrumentation level.
659 InstrumentationLevel requested_level = InstrumentationLevel::kInstrumentNothing;
660 for (const auto& v : requested_instrumentation_levels_) {
661 requested_level = std::max(requested_level, v.second);
662 }
663
664 interpret_only_ = (requested_level == InstrumentationLevel::kInstrumentWithInterpreter) ||
665 forced_interpret_only_;
666
Alex Lightdba61482016-12-21 08:20:29 -0800667 if (!RequiresInstrumentationInstallation(requested_level)) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800668 // We're already set.
669 return;
670 }
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100671 Thread* const self = Thread::Current();
Ian Rogers62d6c772013-02-27 08:32:07 -0800672 Runtime* runtime = Runtime::Current();
Sebastien Hertza8a697f2015-01-15 12:28:47 +0100673 Locks::mutator_lock_->AssertExclusiveHeld(self);
Ian Rogers62d6c772013-02-27 08:32:07 -0800674 Locks::thread_list_lock_->AssertNotHeld(self);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200675 if (requested_level > InstrumentationLevel::kInstrumentNothing) {
Alex Light4ba388a2017-01-27 10:26:49 -0800676 if (requested_level == InstrumentationLevel::kInstrumentWithInterpreter) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800677 interpreter_stubs_installed_ = true;
Ian Rogers62d6c772013-02-27 08:32:07 -0800678 entry_exit_stubs_installed_ = true;
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200679 } else {
680 CHECK_EQ(requested_level, InstrumentationLevel::kInstrumentWithInstrumentationStubs);
681 entry_exit_stubs_installed_ = true;
682 interpreter_stubs_installed_ = false;
Ian Rogers62d6c772013-02-27 08:32:07 -0800683 }
Mathieu Chartiere0671ce2015-07-28 17:23:28 -0700684 InstallStubsClassVisitor visitor(this);
685 runtime->GetClassLinker()->VisitClasses(&visitor);
Ian Rogers62d6c772013-02-27 08:32:07 -0800686 instrumentation_stubs_installed_ = true;
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100687 MutexLock mu(self, *Locks::thread_list_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800688 runtime->GetThreadList()->ForEach(InstrumentationInstallStack, this);
689 } else {
690 interpreter_stubs_installed_ = false;
691 entry_exit_stubs_installed_ = false;
Mathieu Chartiere0671ce2015-07-28 17:23:28 -0700692 InstallStubsClassVisitor visitor(this);
693 runtime->GetClassLinker()->VisitClasses(&visitor);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100694 // Restore stack only if there is no method currently deoptimized.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700695 bool empty;
696 {
697 ReaderMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700698 empty = IsDeoptimizedMethodsEmpty(); // Avoid lock violation.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700699 }
700 if (empty) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100701 MutexLock mu(self, *Locks::thread_list_lock_);
702 Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this);
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +0000703 // Only do this after restoring, as walking the stack when restoring will see
704 // the instrumentation exit pc.
705 instrumentation_stubs_installed_ = false;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100706 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800707 }
jeffhao725a9572012-11-13 18:20:12 -0800708}
709
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200710static void ResetQuickAllocEntryPointsForThread(Thread* thread, void* arg ATTRIBUTE_UNUSED) {
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800711 thread->ResetQuickAllocEntryPointsForThread(kUseReadBarrier && thread->GetIsGcMarking());
Ian Rogersfa824272013-11-05 16:12:57 -0800712}
713
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700714void Instrumentation::SetEntrypointsInstrumented(bool instrumented) {
715 Thread* self = Thread::Current();
Mathieu Chartier661974a2014-01-09 11:23:53 -0800716 Runtime* runtime = Runtime::Current();
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700717 Locks::mutator_lock_->AssertNotHeld(self);
718 Locks::instrument_entrypoints_lock_->AssertHeld(self);
719 if (runtime->IsStarted()) {
Mathieu Chartier4f55e222015-09-04 13:26:21 -0700720 ScopedSuspendAll ssa(__FUNCTION__);
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700721 MutexLock mu(self, *Locks::runtime_shutdown_lock_);
Mathieu Chartier661974a2014-01-09 11:23:53 -0800722 SetQuickAllocEntryPointsInstrumented(instrumented);
723 ResetQuickAllocEntryPoints();
Mathieu Chartier50e93312016-03-16 11:25:29 -0700724 alloc_entrypoints_instrumented_ = instrumented;
Mathieu Chartier4f55e222015-09-04 13:26:21 -0700725 } else {
726 MutexLock mu(self, *Locks::runtime_shutdown_lock_);
727 SetQuickAllocEntryPointsInstrumented(instrumented);
Andreas Gampe157c77e2016-10-17 17:44:41 -0700728
729 // Note: ResetQuickAllocEntryPoints only works when the runtime is started. Manually run the
730 // update for just this thread.
Andreas Gampe162ae502016-10-18 10:03:42 -0700731 // Note: self may be null. One of those paths is setting instrumentation in the Heap
732 // constructor for gcstress mode.
733 if (self != nullptr) {
734 ResetQuickAllocEntryPointsForThread(self, nullptr);
735 }
Andreas Gampe157c77e2016-10-17 17:44:41 -0700736
Mathieu Chartier50e93312016-03-16 11:25:29 -0700737 alloc_entrypoints_instrumented_ = instrumented;
Mathieu Chartier661974a2014-01-09 11:23:53 -0800738 }
739}
740
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700741void Instrumentation::InstrumentQuickAllocEntryPoints() {
742 MutexLock mu(Thread::Current(), *Locks::instrument_entrypoints_lock_);
743 InstrumentQuickAllocEntryPointsLocked();
Ian Rogersfa824272013-11-05 16:12:57 -0800744}
745
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700746void Instrumentation::UninstrumentQuickAllocEntryPoints() {
747 MutexLock mu(Thread::Current(), *Locks::instrument_entrypoints_lock_);
748 UninstrumentQuickAllocEntryPointsLocked();
749}
750
751void Instrumentation::InstrumentQuickAllocEntryPointsLocked() {
752 Locks::instrument_entrypoints_lock_->AssertHeld(Thread::Current());
753 if (quick_alloc_entry_points_instrumentation_counter_ == 0) {
754 SetEntrypointsInstrumented(true);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800755 }
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700756 ++quick_alloc_entry_points_instrumentation_counter_;
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700757}
758
759void Instrumentation::UninstrumentQuickAllocEntryPointsLocked() {
760 Locks::instrument_entrypoints_lock_->AssertHeld(Thread::Current());
761 CHECK_GT(quick_alloc_entry_points_instrumentation_counter_, 0U);
762 --quick_alloc_entry_points_instrumentation_counter_;
763 if (quick_alloc_entry_points_instrumentation_counter_ == 0) {
764 SetEntrypointsInstrumented(false);
765 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800766}
767
768void Instrumentation::ResetQuickAllocEntryPoints() {
769 Runtime* runtime = Runtime::Current();
770 if (runtime->IsStarted()) {
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800771 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700772 runtime->GetThreadList()->ForEach(ResetQuickAllocEntryPointsForThread, nullptr);
Ian Rogersfa824272013-11-05 16:12:57 -0800773 }
774}
775
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700776void Instrumentation::UpdateMethodsCodeImpl(ArtMethod* method, const void* quick_code) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800777 const void* new_quick_code;
Ian Rogers62d6c772013-02-27 08:32:07 -0800778 if (LIKELY(!instrumentation_stubs_installed_)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800779 new_quick_code = quick_code;
Jeff Hao65d15d92013-07-16 16:39:33 -0700780 } else {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100781 if ((interpreter_stubs_installed_ || IsDeoptimized(method)) && !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800782 new_quick_code = GetQuickToInterpreterBridge();
Jeff Hao65d15d92013-07-16 16:39:33 -0700783 } else {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700784 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700785 if (class_linker->IsQuickResolutionStub(quick_code) ||
786 class_linker->IsQuickToInterpreterBridge(quick_code)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700787 new_quick_code = quick_code;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700788 } else if (entry_exit_stubs_installed_) {
789 new_quick_code = GetQuickInstrumentationEntryPoint();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700790 } else {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700791 new_quick_code = quick_code;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700792 }
Jeff Hao65d15d92013-07-16 16:39:33 -0700793 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800794 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800795 UpdateEntrypoints(method, new_quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100796}
797
Nicolas Geoffraya6e0e7d2018-01-26 13:16:50 +0000798void Instrumentation::UpdateNativeMethodsCodeToJitCode(ArtMethod* method, const void* quick_code) {
799 // We don't do any read barrier on `method`'s declaring class in this code, as the JIT might
800 // enter here on a soon-to-be deleted ArtMethod. Updating the entrypoint is OK though, as
801 // the ArtMethod is still in memory.
802 const void* new_quick_code = quick_code;
803 if (UNLIKELY(instrumentation_stubs_installed_) && entry_exit_stubs_installed_) {
804 new_quick_code = GetQuickInstrumentationEntryPoint();
805 }
806 UpdateEntrypoints(method, new_quick_code);
807}
808
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700809void Instrumentation::UpdateMethodsCode(ArtMethod* method, const void* quick_code) {
810 DCHECK(method->GetDeclaringClass()->IsResolved());
811 UpdateMethodsCodeImpl(method, quick_code);
812}
813
Alex Light0a5ec3d2017-07-25 16:50:26 -0700814void Instrumentation::UpdateMethodsCodeToInterpreterEntryPoint(ArtMethod* method) {
815 UpdateMethodsCodeImpl(method, GetQuickToInterpreterBridge());
816}
817
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000818void Instrumentation::UpdateMethodsCodeForJavaDebuggable(ArtMethod* method,
819 const void* quick_code) {
820 // When the runtime is set to Java debuggable, we may update the entry points of
821 // all methods of a class to the interpreter bridge. A method's declaring class
822 // might not be in resolved state yet in that case, so we bypass the DCHECK in
823 // UpdateMethodsCode.
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700824 UpdateMethodsCodeImpl(method, quick_code);
825}
826
Mathieu Chartiere401d142015-04-22 13:56:20 -0700827bool Instrumentation::AddDeoptimizedMethod(ArtMethod* method) {
828 if (IsDeoptimizedMethod(method)) {
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700829 // Already in the map. Return.
830 return false;
831 }
832 // Not found. Add it.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700833 deoptimized_methods_.insert(method);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700834 return true;
835}
836
Mathieu Chartiere401d142015-04-22 13:56:20 -0700837bool Instrumentation::IsDeoptimizedMethod(ArtMethod* method) {
838 return deoptimized_methods_.find(method) != deoptimized_methods_.end();
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700839}
840
Mathieu Chartiere401d142015-04-22 13:56:20 -0700841ArtMethod* Instrumentation::BeginDeoptimizedMethod() {
842 if (deoptimized_methods_.empty()) {
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700843 // Empty.
844 return nullptr;
845 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700846 return *deoptimized_methods_.begin();
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700847}
848
Mathieu Chartiere401d142015-04-22 13:56:20 -0700849bool Instrumentation::RemoveDeoptimizedMethod(ArtMethod* method) {
850 auto it = deoptimized_methods_.find(method);
851 if (it == deoptimized_methods_.end()) {
852 return false;
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700853 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700854 deoptimized_methods_.erase(it);
855 return true;
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700856}
857
858bool Instrumentation::IsDeoptimizedMethodsEmpty() const {
859 return deoptimized_methods_.empty();
860}
861
Mathieu Chartiere401d142015-04-22 13:56:20 -0700862void Instrumentation::Deoptimize(ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100863 CHECK(!method->IsNative());
864 CHECK(!method->IsProxyMethod());
Alex Light9139e002015-10-09 15:59:48 -0700865 CHECK(method->IsInvokable());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100866
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700867 Thread* self = Thread::Current();
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700868 {
869 WriterMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700870 bool has_not_been_deoptimized = AddDeoptimizedMethod(method);
David Sehr709b0702016-10-13 09:12:37 -0700871 CHECK(has_not_been_deoptimized) << "Method " << ArtMethod::PrettyMethod(method)
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200872 << " is already deoptimized";
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700873 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100874 if (!interpreter_stubs_installed_) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800875 UpdateEntrypoints(method, GetQuickInstrumentationEntryPoint());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100876
877 // Install instrumentation exit stub and instrumentation frames. We may already have installed
878 // these previously so it will only cover the newly created frames.
879 instrumentation_stubs_installed_ = true;
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700880 MutexLock mu(self, *Locks::thread_list_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100881 Runtime::Current()->GetThreadList()->ForEach(InstrumentationInstallStack, this);
882 }
883}
884
Mathieu Chartiere401d142015-04-22 13:56:20 -0700885void Instrumentation::Undeoptimize(ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100886 CHECK(!method->IsNative());
887 CHECK(!method->IsProxyMethod());
Alex Light9139e002015-10-09 15:59:48 -0700888 CHECK(method->IsInvokable());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100889
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700890 Thread* self = Thread::Current();
891 bool empty;
892 {
893 WriterMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700894 bool found_and_erased = RemoveDeoptimizedMethod(method);
David Sehr709b0702016-10-13 09:12:37 -0700895 CHECK(found_and_erased) << "Method " << ArtMethod::PrettyMethod(method)
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700896 << " is not deoptimized";
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700897 empty = IsDeoptimizedMethodsEmpty();
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700898 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100899
900 // Restore code and possibly stack only if we did not deoptimize everything.
901 if (!interpreter_stubs_installed_) {
902 // Restore its code or resolution trampoline.
903 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800904 if (method->IsStatic() && !method->IsConstructor() &&
905 !method->GetDeclaringClass()->IsInitialized()) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800906 UpdateEntrypoints(method, GetQuickResolutionStub());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100907 } else {
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000908 const void* quick_code = NeedDebugVersionFor(method)
909 ? GetQuickToInterpreterBridge()
Alex Lightfc49fec2018-01-16 22:28:36 +0000910 : class_linker->GetQuickOatCodeFor(method);
Elliott Hughes956af0f2014-12-11 14:34:28 -0800911 UpdateEntrypoints(method, quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100912 }
913
914 // If there is no deoptimized method left, we can restore the stack of each thread.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700915 if (empty) {
916 MutexLock mu(self, *Locks::thread_list_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100917 Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this);
918 instrumentation_stubs_installed_ = false;
919 }
920 }
921}
922
Mathieu Chartiere401d142015-04-22 13:56:20 -0700923bool Instrumentation::IsDeoptimized(ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100924 DCHECK(method != nullptr);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700925 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700926 return IsDeoptimizedMethod(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100927}
928
929void Instrumentation::EnableDeoptimization() {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700930 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700931 CHECK(IsDeoptimizedMethodsEmpty());
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100932 CHECK_EQ(deoptimization_enabled_, false);
933 deoptimization_enabled_ = true;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100934}
935
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200936void Instrumentation::DisableDeoptimization(const char* key) {
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100937 CHECK_EQ(deoptimization_enabled_, true);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100938 // If we deoptimized everything, undo it.
Alex Lightdba61482016-12-21 08:20:29 -0800939 InstrumentationLevel level = GetCurrentInstrumentationLevel();
940 if (level == InstrumentationLevel::kInstrumentWithInterpreter) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200941 UndeoptimizeEverything(key);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100942 }
943 // Undeoptimized selected methods.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700944 while (true) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700945 ArtMethod* method;
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700946 {
947 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700948 if (IsDeoptimizedMethodsEmpty()) {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700949 break;
950 }
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700951 method = BeginDeoptimizedMethod();
952 CHECK(method != nullptr);
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700953 }
954 Undeoptimize(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100955 }
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100956 deoptimization_enabled_ = false;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100957}
958
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100959// Indicates if instrumentation should notify method enter/exit events to the listeners.
960bool Instrumentation::ShouldNotifyMethodEnterExitEvents() const {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200961 if (!HasMethodEntryListeners() && !HasMethodExitListeners()) {
962 return false;
963 }
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100964 return !deoptimization_enabled_ && !interpreter_stubs_installed_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100965}
966
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200967void Instrumentation::DeoptimizeEverything(const char* key) {
968 CHECK(deoptimization_enabled_);
969 ConfigureStubs(key, InstrumentationLevel::kInstrumentWithInterpreter);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100970}
971
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200972void Instrumentation::UndeoptimizeEverything(const char* key) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100973 CHECK(interpreter_stubs_installed_);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200974 CHECK(deoptimization_enabled_);
975 ConfigureStubs(key, InstrumentationLevel::kInstrumentNothing);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100976}
977
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200978void Instrumentation::EnableMethodTracing(const char* key, bool needs_interpreter) {
979 InstrumentationLevel level;
980 if (needs_interpreter) {
981 level = InstrumentationLevel::kInstrumentWithInterpreter;
982 } else {
983 level = InstrumentationLevel::kInstrumentWithInstrumentationStubs;
984 }
985 ConfigureStubs(key, level);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100986}
987
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200988void Instrumentation::DisableMethodTracing(const char* key) {
989 ConfigureStubs(key, InstrumentationLevel::kInstrumentNothing);
jeffhao725a9572012-11-13 18:20:12 -0800990}
991
Andreas Gampe542451c2016-07-26 09:02:02 -0700992const void* Instrumentation::GetQuickCodeFor(ArtMethod* method, PointerSize pointer_size) const {
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100993 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers62d6c772013-02-27 08:32:07 -0800994 if (LIKELY(!instrumentation_stubs_installed_)) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800995 const void* code = method->GetEntryPointFromQuickCompiledCodePtrSize(pointer_size);
Vladimir Marko8a630572014-04-09 18:45:35 +0100996 DCHECK(code != nullptr);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700997 if (LIKELY(!class_linker->IsQuickResolutionStub(code) &&
998 !class_linker->IsQuickToInterpreterBridge(code)) &&
999 !class_linker->IsQuickResolutionStub(code) &&
1000 !class_linker->IsQuickToInterpreterBridge(code)) {
Ian Rogers62d6c772013-02-27 08:32:07 -08001001 return code;
1002 }
1003 }
Alex Lightfc49fec2018-01-16 22:28:36 +00001004 return class_linker->GetQuickOatCodeFor(method);
jeffhao725a9572012-11-13 18:20:12 -08001005}
1006
Alex Lightd7661582017-05-01 13:48:16 -07001007void Instrumentation::MethodEnterEventImpl(Thread* thread,
1008 ObjPtr<mirror::Object> this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001009 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -08001010 uint32_t dex_pc) const {
Mingyao Yang2ee17902017-08-30 11:37:08 -07001011 DCHECK(!method->IsRuntimeMethod());
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001012 if (HasMethodEntryListeners()) {
Alex Lightd7661582017-05-01 13:48:16 -07001013 Thread* self = Thread::Current();
1014 StackHandleScope<1> hs(self);
1015 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001016 for (InstrumentationListener* listener : method_entry_listeners_) {
1017 if (listener != nullptr) {
Alex Lightd7661582017-05-01 13:48:16 -07001018 listener->MethodEntered(thread, thiz, method, dex_pc);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001019 }
1020 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001021 }
1022}
1023
Alex Lightd7661582017-05-01 13:48:16 -07001024void Instrumentation::MethodExitEventImpl(Thread* thread,
1025 ObjPtr<mirror::Object> this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001026 ArtMethod* method,
Alex Lightd7661582017-05-01 13:48:16 -07001027 uint32_t dex_pc,
1028 const JValue& return_value) const {
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001029 if (HasMethodExitListeners()) {
Alex Lightd7661582017-05-01 13:48:16 -07001030 Thread* self = Thread::Current();
1031 StackHandleScope<2> hs(self);
1032 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
1033 if (method->GetInterfaceMethodIfProxy(kRuntimePointerSize)
1034 ->GetReturnTypePrimitive() != Primitive::kPrimNot) {
1035 for (InstrumentationListener* listener : method_exit_listeners_) {
1036 if (listener != nullptr) {
1037 listener->MethodExited(thread, thiz, method, dex_pc, return_value);
1038 }
1039 }
1040 } else {
1041 Handle<mirror::Object> ret(hs.NewHandle(return_value.GetL()));
1042 for (InstrumentationListener* listener : method_exit_listeners_) {
1043 if (listener != nullptr) {
1044 listener->MethodExited(thread, thiz, method, dex_pc, ret);
1045 }
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001046 }
1047 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001048 }
1049}
1050
Alex Lightd7661582017-05-01 13:48:16 -07001051void Instrumentation::MethodUnwindEvent(Thread* thread,
1052 mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001053 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -08001054 uint32_t dex_pc) const {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +02001055 if (HasMethodUnwindListeners()) {
Alex Lightd7661582017-05-01 13:48:16 -07001056 Thread* self = Thread::Current();
1057 StackHandleScope<1> hs(self);
1058 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
Mathieu Chartier02e25112013-08-14 16:14:24 -07001059 for (InstrumentationListener* listener : method_unwind_listeners_) {
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001060 if (listener != nullptr) {
Alex Lightd7661582017-05-01 13:48:16 -07001061 listener->MethodUnwind(thread, thiz, method, dex_pc);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001062 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001063 }
1064 }
1065}
1066
Alex Lightd7661582017-05-01 13:48:16 -07001067void Instrumentation::DexPcMovedEventImpl(Thread* thread,
1068 ObjPtr<mirror::Object> this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001069 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -08001070 uint32_t dex_pc) const {
Alex Lightd7661582017-05-01 13:48:16 -07001071 Thread* self = Thread::Current();
1072 StackHandleScope<1> hs(self);
1073 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001074 for (InstrumentationListener* listener : dex_pc_listeners_) {
1075 if (listener != nullptr) {
Alex Lightd7661582017-05-01 13:48:16 -07001076 listener->DexPcMoved(thread, thiz, method, dex_pc);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001077 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001078 }
1079}
1080
Nicolas Geoffray81f0f952016-01-20 16:25:19 +00001081void Instrumentation::BranchImpl(Thread* thread,
1082 ArtMethod* method,
1083 uint32_t dex_pc,
1084 int32_t offset) const {
1085 for (InstrumentationListener* listener : branch_listeners_) {
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001086 if (listener != nullptr) {
Nicolas Geoffray81f0f952016-01-20 16:25:19 +00001087 listener->Branch(thread, method, dex_pc, offset);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001088 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001089 }
1090}
1091
Nicolas Geoffray5550ca82015-08-21 18:38:30 +01001092void Instrumentation::InvokeVirtualOrInterfaceImpl(Thread* thread,
Alex Lightd7661582017-05-01 13:48:16 -07001093 ObjPtr<mirror::Object> this_object,
Nicolas Geoffray5550ca82015-08-21 18:38:30 +01001094 ArtMethod* caller,
1095 uint32_t dex_pc,
1096 ArtMethod* callee) const {
Alex Lightd7661582017-05-01 13:48:16 -07001097 Thread* self = Thread::Current();
1098 StackHandleScope<1> hs(self);
1099 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
Nicolas Geoffray5550ca82015-08-21 18:38:30 +01001100 for (InstrumentationListener* listener : invoke_virtual_or_interface_listeners_) {
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001101 if (listener != nullptr) {
Alex Lightd7661582017-05-01 13:48:16 -07001102 listener->InvokeVirtualOrInterface(thread, thiz, caller, dex_pc, callee);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001103 }
Nicolas Geoffray5550ca82015-08-21 18:38:30 +01001104 }
1105}
1106
Alex Lighte814f9d2017-07-31 16:14:39 -07001107void Instrumentation::WatchedFramePopImpl(Thread* thread, const ShadowFrame& frame) const {
1108 for (InstrumentationListener* listener : watched_frame_pop_listeners_) {
1109 if (listener != nullptr) {
1110 listener->WatchedFramePop(thread, frame);
1111 }
1112 }
1113}
1114
Alex Lightd7661582017-05-01 13:48:16 -07001115void Instrumentation::FieldReadEventImpl(Thread* thread,
1116 ObjPtr<mirror::Object> this_object,
1117 ArtMethod* method,
1118 uint32_t dex_pc,
Mathieu Chartierc7853442015-03-27 14:35:38 -07001119 ArtField* field) const {
Alex Lightd7661582017-05-01 13:48:16 -07001120 Thread* self = Thread::Current();
1121 StackHandleScope<1> hs(self);
1122 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001123 for (InstrumentationListener* listener : field_read_listeners_) {
1124 if (listener != nullptr) {
Alex Lightd7661582017-05-01 13:48:16 -07001125 listener->FieldRead(thread, thiz, method, dex_pc, field);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001126 }
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +02001127 }
1128}
1129
Alex Lightd7661582017-05-01 13:48:16 -07001130void Instrumentation::FieldWriteEventImpl(Thread* thread,
1131 ObjPtr<mirror::Object> this_object,
1132 ArtMethod* method,
1133 uint32_t dex_pc,
1134 ArtField* field,
1135 const JValue& field_value) const {
1136 Thread* self = Thread::Current();
1137 StackHandleScope<2> hs(self);
1138 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
1139 if (field->IsPrimitiveType()) {
1140 for (InstrumentationListener* listener : field_write_listeners_) {
1141 if (listener != nullptr) {
1142 listener->FieldWritten(thread, thiz, method, dex_pc, field, field_value);
1143 }
1144 }
1145 } else {
1146 Handle<mirror::Object> val(hs.NewHandle(field_value.GetL()));
1147 for (InstrumentationListener* listener : field_write_listeners_) {
1148 if (listener != nullptr) {
1149 listener->FieldWritten(thread, thiz, method, dex_pc, field, val);
1150 }
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001151 }
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +02001152 }
1153}
1154
Alex Light6e1607e2017-08-23 10:06:18 -07001155void Instrumentation::ExceptionThrownEvent(Thread* thread,
Sebastien Hertz947ff082013-09-17 14:10:13 +02001156 mirror::Throwable* exception_object) const {
Hiroshi Yamauchi3481f7a2017-02-10 12:07:36 -08001157 Thread* self = Thread::Current();
1158 StackHandleScope<1> hs(self);
1159 Handle<mirror::Throwable> h_exception(hs.NewHandle(exception_object));
Alex Light6e1607e2017-08-23 10:06:18 -07001160 if (HasExceptionThrownListeners()) {
Hiroshi Yamauchi3481f7a2017-02-10 12:07:36 -08001161 DCHECK_EQ(thread->GetException(), h_exception.Get());
Jeff Haoc0bd4da2013-04-11 15:52:28 -07001162 thread->ClearException();
Alex Light6e1607e2017-08-23 10:06:18 -07001163 for (InstrumentationListener* listener : exception_thrown_listeners_) {
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001164 if (listener != nullptr) {
Alex Light6e1607e2017-08-23 10:06:18 -07001165 listener->ExceptionThrown(thread, h_exception);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001166 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001167 }
Alex Light9fb1ab12017-09-05 09:32:49 -07001168 // See b/65049545 for discussion about this behavior.
1169 thread->AssertNoPendingException();
Hiroshi Yamauchi3481f7a2017-02-10 12:07:36 -08001170 thread->SetException(h_exception.Get());
Ian Rogers62d6c772013-02-27 08:32:07 -08001171 }
1172}
1173
Alex Light9fb1ab12017-09-05 09:32:49 -07001174void Instrumentation::ExceptionHandledEvent(Thread* thread,
1175 mirror::Throwable* exception_object) const {
1176 Thread* self = Thread::Current();
1177 StackHandleScope<1> hs(self);
1178 Handle<mirror::Throwable> h_exception(hs.NewHandle(exception_object));
1179 if (HasExceptionHandledListeners()) {
1180 // We should have cleared the exception so that callers can detect a new one.
1181 DCHECK(thread->GetException() == nullptr);
1182 for (InstrumentationListener* listener : exception_handled_listeners_) {
1183 if (listener != nullptr) {
1184 listener->ExceptionHandled(thread, h_exception);
1185 }
1186 }
1187 }
1188}
1189
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +00001190// Computes a frame ID by ignoring inlined frames.
1191size_t Instrumentation::ComputeFrameId(Thread* self,
1192 size_t frame_depth,
1193 size_t inlined_frames_before_frame) {
1194 CHECK_GE(frame_depth, inlined_frames_before_frame);
1195 size_t no_inline_depth = frame_depth - inlined_frames_before_frame;
1196 return StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk) - no_inline_depth;
1197}
1198
Ian Rogers62d6c772013-02-27 08:32:07 -08001199static void CheckStackDepth(Thread* self, const InstrumentationStackFrame& instrumentation_frame,
1200 int delta)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001201 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +01001202 size_t frame_id = StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk) + delta;
Ian Rogers62d6c772013-02-27 08:32:07 -08001203 if (frame_id != instrumentation_frame.frame_id_) {
1204 LOG(ERROR) << "Expected frame_id=" << frame_id << " but found "
1205 << instrumentation_frame.frame_id_;
1206 StackVisitor::DescribeStack(self);
1207 CHECK_EQ(frame_id, instrumentation_frame.frame_id_);
1208 }
1209}
1210
1211void Instrumentation::PushInstrumentationStackFrame(Thread* self, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001212 ArtMethod* method,
Jeff Hao9a916d32013-06-27 18:45:37 -07001213 uintptr_t lr, bool interpreter_entry) {
Alex Lightb7edcda2017-04-27 13:20:31 -07001214 DCHECK(!self->IsExceptionPending());
Ian Rogers62d6c772013-02-27 08:32:07 -08001215 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
1216 if (kVerboseInstrumentation) {
David Sehr709b0702016-10-13 09:12:37 -07001217 LOG(INFO) << "Entering " << ArtMethod::PrettyMethod(method) << " from PC "
1218 << reinterpret_cast<void*>(lr);
Ian Rogers62d6c772013-02-27 08:32:07 -08001219 }
Alex Lightb7edcda2017-04-27 13:20:31 -07001220
1221 // We send the enter event before pushing the instrumentation frame to make cleanup easier. If the
1222 // event causes an exception we can simply send the unwind event and return.
1223 StackHandleScope<1> hs(self);
1224 Handle<mirror::Object> h_this(hs.NewHandle(this_object));
1225 if (!interpreter_entry) {
1226 MethodEnterEvent(self, h_this.Get(), method, 0);
1227 if (self->IsExceptionPending()) {
1228 MethodUnwindEvent(self, h_this.Get(), method, 0);
1229 return;
1230 }
1231 }
1232
1233 // We have a callee-save frame meaning this value is guaranteed to never be 0.
1234 DCHECK(!self->IsExceptionPending());
1235 size_t frame_id = StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk);
1236
1237 instrumentation::InstrumentationStackFrame instrumentation_frame(h_this.Get(), method, lr,
Jeff Hao9a916d32013-06-27 18:45:37 -07001238 frame_id, interpreter_entry);
Ian Rogers62d6c772013-02-27 08:32:07 -08001239 stack->push_front(instrumentation_frame);
Ian Rogers62d6c772013-02-27 08:32:07 -08001240}
1241
Mingyao Yang2ee17902017-08-30 11:37:08 -07001242DeoptimizationMethodType Instrumentation::GetDeoptimizationMethodType(ArtMethod* method) {
1243 if (method->IsRuntimeMethod()) {
1244 // Certain methods have strict requirement on whether the dex instruction
1245 // should be re-executed upon deoptimization.
1246 if (method == Runtime::Current()->GetCalleeSaveMethod(
1247 CalleeSaveType::kSaveEverythingForClinit)) {
1248 return DeoptimizationMethodType::kKeepDexPc;
1249 }
1250 if (method == Runtime::Current()->GetCalleeSaveMethod(
1251 CalleeSaveType::kSaveEverythingForSuspendCheck)) {
1252 return DeoptimizationMethodType::kKeepDexPc;
1253 }
1254 }
1255 return DeoptimizationMethodType::kDefault;
1256}
1257
1258// Try to get the shorty of a runtime method if it's an invocation stub.
1259struct RuntimeMethodShortyVisitor : public StackVisitor {
1260 explicit RuntimeMethodShortyVisitor(Thread* thread)
1261 : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames),
1262 shorty('V') {}
1263
1264 bool VisitFrame() REQUIRES_SHARED(Locks::mutator_lock_) {
1265 ArtMethod* m = GetMethod();
1266 if (m != nullptr && !m->IsRuntimeMethod()) {
1267 // The first Java method.
1268 if (m->IsNative()) {
1269 // Use JNI method's shorty for the jni stub.
1270 shorty = m->GetShorty()[0];
1271 return false;
1272 }
1273 if (m->IsProxyMethod()) {
1274 // Proxy method just invokes its proxied method via
1275 // art_quick_proxy_invoke_handler.
1276 shorty = m->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty()[0];
1277 return false;
1278 }
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001279 const Instruction& instr = m->DexInstructions().InstructionAt(GetDexPc());
1280 if (instr.IsInvoke()) {
Mingyao Yang2ee17902017-08-30 11:37:08 -07001281 const DexFile* dex_file = m->GetDexFile();
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001282 if (interpreter::IsStringInit(dex_file, instr.VRegB())) {
Mingyao Yang2ee17902017-08-30 11:37:08 -07001283 // Invoking string init constructor is turned into invoking
1284 // StringFactory.newStringFromChars() which returns a string.
1285 shorty = 'L';
1286 return false;
1287 }
1288 // A regular invoke, use callee's shorty.
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001289 uint32_t method_idx = instr.VRegB();
Mingyao Yang2ee17902017-08-30 11:37:08 -07001290 shorty = dex_file->GetMethodShorty(method_idx)[0];
1291 }
1292 // Stop stack walking since we've seen a Java frame.
1293 return false;
1294 }
1295 return true;
1296 }
1297
1298 char shorty;
1299};
1300
Alex Lightb7edcda2017-04-27 13:20:31 -07001301TwoWordReturn Instrumentation::PopInstrumentationStackFrame(Thread* self,
1302 uintptr_t* return_pc,
1303 uint64_t* gpr_result,
1304 uint64_t* fpr_result) {
1305 DCHECK(gpr_result != nullptr);
1306 DCHECK(fpr_result != nullptr);
Ian Rogers62d6c772013-02-27 08:32:07 -08001307 // Do the pop.
1308 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
1309 CHECK_GT(stack->size(), 0U);
1310 InstrumentationStackFrame instrumentation_frame = stack->front();
1311 stack->pop_front();
1312
1313 // Set return PC and check the sanity of the stack.
1314 *return_pc = instrumentation_frame.return_pc_;
1315 CheckStackDepth(self, instrumentation_frame, 0);
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001316 self->VerifyStack();
Ian Rogers62d6c772013-02-27 08:32:07 -08001317
Mathieu Chartiere401d142015-04-22 13:56:20 -07001318 ArtMethod* method = instrumentation_frame.method_;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001319 uint32_t length;
Andreas Gampe542451c2016-07-26 09:02:02 -07001320 const PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Mingyao Yang2ee17902017-08-30 11:37:08 -07001321 char return_shorty;
1322
1323 // Runtime method does not call into MethodExitEvent() so there should not be
1324 // suspension point below.
1325 ScopedAssertNoThreadSuspension ants(__FUNCTION__, method->IsRuntimeMethod());
1326 if (method->IsRuntimeMethod()) {
1327 if (method != Runtime::Current()->GetCalleeSaveMethod(
1328 CalleeSaveType::kSaveEverythingForClinit)) {
1329 // If the caller is at an invocation point and the runtime method is not
1330 // for clinit, we need to pass return results to the caller.
1331 // We need the correct shorty to decide whether we need to pass the return
1332 // result for deoptimization below.
1333 RuntimeMethodShortyVisitor visitor(self);
1334 visitor.WalkStack();
1335 return_shorty = visitor.shorty;
1336 } else {
1337 // Some runtime methods such as allocations, unresolved field getters, etc.
1338 // have return value. We don't need to set return_value since MethodExitEvent()
1339 // below isn't called for runtime methods. Deoptimization doesn't need the
1340 // value either since the dex instruction will be re-executed by the
1341 // interpreter, except these two cases:
1342 // (1) For an invoke, which is handled above to get the correct shorty.
1343 // (2) For MONITOR_ENTER/EXIT, which cannot be re-executed since it's not
1344 // idempotent. However there is no return value for it anyway.
1345 return_shorty = 'V';
1346 }
1347 } else {
1348 return_shorty = method->GetInterfaceMethodIfProxy(pointer_size)->GetShorty(&length)[0];
1349 }
1350
Alex Lightb7edcda2017-04-27 13:20:31 -07001351 bool is_ref = return_shorty == '[' || return_shorty == 'L';
1352 StackHandleScope<1> hs(self);
1353 MutableHandle<mirror::Object> res(hs.NewHandle<mirror::Object>(nullptr));
Ian Rogers62d6c772013-02-27 08:32:07 -08001354 JValue return_value;
1355 if (return_shorty == 'V') {
1356 return_value.SetJ(0);
1357 } else if (return_shorty == 'F' || return_shorty == 'D') {
Alex Lightb7edcda2017-04-27 13:20:31 -07001358 return_value.SetJ(*fpr_result);
Ian Rogers62d6c772013-02-27 08:32:07 -08001359 } else {
Alex Lightb7edcda2017-04-27 13:20:31 -07001360 return_value.SetJ(*gpr_result);
1361 }
1362 if (is_ref) {
1363 // Take a handle to the return value so we won't lose it if we suspend.
1364 res.Assign(return_value.GetL());
Ian Rogers62d6c772013-02-27 08:32:07 -08001365 }
1366 // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to
1367 // return_pc.
Andreas Gampee2abbc62017-09-15 11:59:26 -07001368 uint32_t dex_pc = dex::kDexNoIndex;
Ian Rogers62d6c772013-02-27 08:32:07 -08001369 mirror::Object* this_object = instrumentation_frame.this_object_;
Mingyao Yang2ee17902017-08-30 11:37:08 -07001370 if (!method->IsRuntimeMethod() && !instrumentation_frame.interpreter_entry_) {
Sebastien Hertz320deb22014-06-11 19:45:05 +02001371 MethodExitEvent(self, this_object, instrumentation_frame.method_, dex_pc, return_value);
1372 }
jeffhao725a9572012-11-13 18:20:12 -08001373
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01001374 // Deoptimize if the caller needs to continue execution in the interpreter. Do nothing if we get
1375 // back to an upcall.
1376 NthCallerVisitor visitor(self, 1, true);
1377 visitor.WalkStack(true);
Sebastien Hertz270a0e12015-01-16 19:49:09 +01001378 bool deoptimize = (visitor.caller != nullptr) &&
Daniel Mihalyieb076692014-08-22 17:33:31 +02001379 (interpreter_stubs_installed_ || IsDeoptimized(visitor.caller) ||
1380 Dbg::IsForcedInterpreterNeededForUpcall(self, visitor.caller));
Alex Lightb7edcda2017-04-27 13:20:31 -07001381 if (is_ref) {
1382 // Restore the return value if it's a reference since it might have moved.
1383 *reinterpret_cast<mirror::Object**>(gpr_result) = res.Get();
1384 }
Nicolas Geoffraya0619e22016-12-20 13:57:43 +00001385 if (deoptimize && Runtime::Current()->IsAsyncDeoptimizeable(*return_pc)) {
Ian Rogers62d6c772013-02-27 08:32:07 -08001386 if (kVerboseInstrumentation) {
Andreas Gampe46ee31b2016-12-14 10:11:49 -08001387 LOG(INFO) << "Deoptimizing "
1388 << visitor.caller->PrettyMethod()
1389 << " by returning from "
1390 << method->PrettyMethod()
1391 << " with result "
1392 << std::hex << return_value.GetJ() << std::dec
1393 << " in "
1394 << *self;
Ian Rogers62d6c772013-02-27 08:32:07 -08001395 }
Mingyao Yang2ee17902017-08-30 11:37:08 -07001396 DeoptimizationMethodType deopt_method_type = GetDeoptimizationMethodType(method);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01001397 self->PushDeoptimizationContext(return_value,
Mingyao Yang2ee17902017-08-30 11:37:08 -07001398 return_shorty == 'L' || return_shorty == '[',
1399 nullptr /* no pending exception */,
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01001400 false /* from_code */,
Mingyao Yang2ee17902017-08-30 11:37:08 -07001401 deopt_method_type);
Andreas Gamped58342c2014-06-05 14:18:08 -07001402 return GetTwoWordSuccessValue(*return_pc,
1403 reinterpret_cast<uintptr_t>(GetQuickDeoptimizationEntryPoint()));
Ian Rogers62d6c772013-02-27 08:32:07 -08001404 } else {
Nicolas Geoffraya0619e22016-12-20 13:57:43 +00001405 if (deoptimize && !Runtime::Current()->IsAsyncDeoptimizeable(*return_pc)) {
Alex Lightd8eb6732018-01-29 15:16:02 -08001406 VLOG(deopt) << "Got a deoptimization request on un-deoptimizable " << method->PrettyMethod()
1407 << " at PC " << reinterpret_cast<void*>(*return_pc);
Nicolas Geoffraya0619e22016-12-20 13:57:43 +00001408 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001409 if (kVerboseInstrumentation) {
David Sehr709b0702016-10-13 09:12:37 -07001410 LOG(INFO) << "Returning from " << method->PrettyMethod()
Brian Carlstrom2d888622013-07-18 17:02:00 -07001411 << " to PC " << reinterpret_cast<void*>(*return_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -08001412 }
Andreas Gamped58342c2014-06-05 14:18:08 -07001413 return GetTwoWordSuccessValue(0, *return_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -08001414 }
jeffhao725a9572012-11-13 18:20:12 -08001415}
1416
Mingyao Yangf711f2c2016-05-23 12:29:39 -07001417uintptr_t Instrumentation::PopMethodForUnwind(Thread* self, bool is_deoptimization) const {
Ian Rogers62d6c772013-02-27 08:32:07 -08001418 // Do the pop.
1419 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
1420 CHECK_GT(stack->size(), 0U);
Alex Lightb7edcda2017-04-27 13:20:31 -07001421 size_t idx = stack->size();
Ian Rogers62d6c772013-02-27 08:32:07 -08001422 InstrumentationStackFrame instrumentation_frame = stack->front();
Ian Rogers62d6c772013-02-27 08:32:07 -08001423
Mathieu Chartiere401d142015-04-22 13:56:20 -07001424 ArtMethod* method = instrumentation_frame.method_;
Ian Rogers62d6c772013-02-27 08:32:07 -08001425 if (is_deoptimization) {
1426 if (kVerboseInstrumentation) {
David Sehr709b0702016-10-13 09:12:37 -07001427 LOG(INFO) << "Popping for deoptimization " << ArtMethod::PrettyMethod(method);
Ian Rogers62d6c772013-02-27 08:32:07 -08001428 }
1429 } else {
1430 if (kVerboseInstrumentation) {
David Sehr709b0702016-10-13 09:12:37 -07001431 LOG(INFO) << "Popping for unwind " << ArtMethod::PrettyMethod(method);
Ian Rogers62d6c772013-02-27 08:32:07 -08001432 }
1433
1434 // Notify listeners of method unwind.
1435 // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to
1436 // return_pc.
Andreas Gampee2abbc62017-09-15 11:59:26 -07001437 uint32_t dex_pc = dex::kDexNoIndex;
Mingyao Yang2ee17902017-08-30 11:37:08 -07001438 if (!method->IsRuntimeMethod()) {
1439 MethodUnwindEvent(self, instrumentation_frame.this_object_, method, dex_pc);
1440 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001441 }
Alex Lightb7edcda2017-04-27 13:20:31 -07001442 // TODO: bring back CheckStackDepth(self, instrumentation_frame, 2);
1443 CHECK_EQ(stack->size(), idx);
1444 DCHECK(instrumentation_frame.method_ == stack->front().method_);
1445 stack->pop_front();
Mingyao Yangf711f2c2016-05-23 12:29:39 -07001446 return instrumentation_frame.return_pc_;
Ian Rogers62d6c772013-02-27 08:32:07 -08001447}
1448
1449std::string InstrumentationStackFrame::Dump() const {
1450 std::ostringstream os;
David Sehr709b0702016-10-13 09:12:37 -07001451 os << "Frame " << frame_id_ << " " << ArtMethod::PrettyMethod(method_) << ":"
Ian Rogers62d6c772013-02-27 08:32:07 -08001452 << reinterpret_cast<void*>(return_pc_) << " this=" << reinterpret_cast<void*>(this_object_);
1453 return os.str();
1454}
1455
1456} // namespace instrumentation
jeffhao725a9572012-11-13 18:20:12 -08001457} // namespace art