blob: 4d8c6872e8fd6c3b07b2b5fee4b9d628c75b2361 [file] [log] [blame]
jeffhao725a9572012-11-13 18:20:12 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "instrumentation.h"
18
Ian Rogersc7dd2952014-10-21 23:31:19 -070019#include <sstream>
20
Ian Rogerse63db272014-07-15 15:36:11 -070021#include "arch/context.h"
Alex Lightd7661582017-05-01 13:48:16 -070022#include "art_field-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070023#include "art_method-inl.h"
Ian Rogersef7d42f2014-01-06 12:55:46 -080024#include "atomic.h"
Andreas Gampe8228cdf2017-05-30 15:03:54 -070025#include "base/callee_save_type.h"
jeffhao725a9572012-11-13 18:20:12 -080026#include "class_linker.h"
27#include "debugger.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080028#include "dex_file-inl.h"
Mingyao Yang2ee17902017-08-30 11:37:08 -070029#include "dex_instruction-inl.h"
Mathieu Chartierd8891782014-03-02 13:28:37 -080030#include "entrypoints/quick/quick_alloc_entrypoints.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070031#include "entrypoints/quick/quick_entrypoints.h"
Ian Rogers6f3dbba2014-10-14 17:41:57 -070032#include "entrypoints/runtime_asm_entrypoints.h"
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070033#include "gc_root-inl.h"
Sebastien Hertz138dbfc2013-12-04 18:15:25 +010034#include "interpreter/interpreter.h"
Mingyao Yang2ee17902017-08-30 11:37:08 -070035#include "interpreter/interpreter_common.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080036#include "jit/jit.h"
37#include "jit/jit_code_cache.h"
Alex Lightd7661582017-05-01 13:48:16 -070038#include "jvalue-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080039#include "mirror/class-inl.h"
40#include "mirror/dex_cache.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070041#include "mirror/object-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070042#include "mirror/object_array-inl.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080043#include "nth_caller_visitor.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010044#include "oat_quick_method_header.h"
jeffhao725a9572012-11-13 18:20:12 -080045#include "thread.h"
46#include "thread_list.h"
jeffhao725a9572012-11-13 18:20:12 -080047
48namespace art {
Ian Rogers62d6c772013-02-27 08:32:07 -080049namespace instrumentation {
jeffhao725a9572012-11-13 18:20:12 -080050
Sebastien Hertz0462c4c2015-04-01 16:34:17 +020051constexpr bool kVerboseInstrumentation = false;
Sebastien Hertz5bfd5c92013-11-15 11:36:07 +010052
Alex Lightd7661582017-05-01 13:48:16 -070053void InstrumentationListener::MethodExited(Thread* thread,
54 Handle<mirror::Object> this_object,
55 ArtMethod* method,
56 uint32_t dex_pc,
57 Handle<mirror::Object> return_value) {
58 DCHECK_EQ(method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetReturnTypePrimitive(),
59 Primitive::kPrimNot);
60 JValue v;
61 v.SetL(return_value.Get());
62 MethodExited(thread, this_object, method, dex_pc, v);
63}
64
65void InstrumentationListener::FieldWritten(Thread* thread,
66 Handle<mirror::Object> this_object,
67 ArtMethod* method,
68 uint32_t dex_pc,
69 ArtField* field,
70 Handle<mirror::Object> field_value) {
71 DCHECK(!field->IsPrimitiveType());
72 JValue v;
73 v.SetL(field_value.Get());
74 FieldWritten(thread, this_object, method, dex_pc, field, v);
75}
76
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +010077// Instrumentation works on non-inlined frames by updating returned PCs
78// of compiled frames.
79static constexpr StackVisitor::StackWalkKind kInstrumentationStackWalk =
80 StackVisitor::StackWalkKind::kSkipInlinedFrames;
81
Mathieu Chartiere0671ce2015-07-28 17:23:28 -070082class InstallStubsClassVisitor : public ClassVisitor {
83 public:
84 explicit InstallStubsClassVisitor(Instrumentation* instrumentation)
85 : instrumentation_(instrumentation) {}
86
Mathieu Chartier28357fa2016-10-18 16:27:40 -070087 bool operator()(ObjPtr<mirror::Class> klass) OVERRIDE REQUIRES(Locks::mutator_lock_) {
88 instrumentation_->InstallStubsForClass(klass.Ptr());
Mathieu Chartiere0671ce2015-07-28 17:23:28 -070089 return true; // we visit all classes.
90 }
91
92 private:
93 Instrumentation* const instrumentation_;
94};
95
Ian Rogers62d6c772013-02-27 08:32:07 -080096
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -070097Instrumentation::Instrumentation()
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +000098 : instrumentation_stubs_installed_(false),
99 entry_exit_stubs_installed_(false),
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700100 interpreter_stubs_installed_(false),
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +0000101 interpret_only_(false),
102 forced_interpret_only_(false),
103 have_method_entry_listeners_(false),
104 have_method_exit_listeners_(false),
105 have_method_unwind_listeners_(false),
106 have_dex_pc_listeners_(false),
107 have_field_read_listeners_(false),
108 have_field_write_listeners_(false),
Alex Light6e1607e2017-08-23 10:06:18 -0700109 have_exception_thrown_listeners_(false),
Alex Lighte814f9d2017-07-31 16:14:39 -0700110 have_watched_frame_pop_listeners_(false),
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000111 have_branch_listeners_(false),
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +0000112 have_invoke_virtual_or_interface_listeners_(false),
Alex Light9fb1ab12017-09-05 09:32:49 -0700113 have_exception_handled_listeners_(false),
Mathieu Chartierb8aa1e42016-04-05 14:36:57 -0700114 deoptimized_methods_lock_("deoptimized methods lock", kDeoptimizedMethodsLock),
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700115 deoptimization_enabled_(false),
116 interpreter_handler_table_(kMainHandlerTable),
Mathieu Chartier50e93312016-03-16 11:25:29 -0700117 quick_alloc_entry_points_instrumentation_counter_(0),
118 alloc_entrypoints_instrumented_(false) {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700119}
120
Sebastien Hertza10aa372015-01-21 17:30:58 +0100121void Instrumentation::InstallStubsForClass(mirror::Class* klass) {
Vladimir Marko72ab6842017-01-20 19:32:50 +0000122 if (!klass->IsResolved()) {
Sebastien Hertza8a697f2015-01-15 12:28:47 +0100123 // We need the class to be resolved to install/uninstall stubs. Otherwise its methods
124 // could not be initialized or linked with regards to class inheritance.
Vladimir Marko72ab6842017-01-20 19:32:50 +0000125 } else if (klass->IsErroneousResolved()) {
126 // We can't execute code in a erroneous class: do nothing.
Sebastien Hertza8a697f2015-01-15 12:28:47 +0100127 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700128 for (ArtMethod& method : klass->GetMethods(kRuntimePointerSize)) {
Alex Light51a64d52015-12-17 13:55:59 -0800129 InstallStubsForMethod(&method);
Sebastien Hertza8a697f2015-01-15 12:28:47 +0100130 }
jeffhao725a9572012-11-13 18:20:12 -0800131 }
jeffhao725a9572012-11-13 18:20:12 -0800132}
133
Mathieu Chartiere401d142015-04-22 13:56:20 -0700134static void UpdateEntrypoints(ArtMethod* method, const void* quick_code)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700135 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800136 method->SetEntryPointFromQuickCompiledCode(quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100137}
138
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000139bool Instrumentation::NeedDebugVersionFor(ArtMethod* method) const REQUIRES_SHARED(Locks::mutator_lock_) {
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800140 return Dbg::IsDebuggerActive() &&
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000141 Runtime::Current()->IsJavaDebuggable() &&
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800142 !method->IsNative() &&
143 !method->IsProxyMethod();
144}
145
Mathieu Chartiere401d142015-04-22 13:56:20 -0700146void Instrumentation::InstallStubsForMethod(ArtMethod* method) {
Alex Light9139e002015-10-09 15:59:48 -0700147 if (!method->IsInvokable() || method->IsProxyMethod()) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100148 // Do not change stubs for these methods.
149 return;
150 }
Jeff Hao56802772014-08-19 10:17:36 -0700151 // Don't stub Proxy.<init>. Note that the Proxy class itself is not a proxy class.
152 if (method->IsConstructor() &&
153 method->GetDeclaringClass()->DescriptorEquals("Ljava/lang/reflect/Proxy;")) {
Jeff Haodb8a6642014-08-14 17:18:52 -0700154 return;
155 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800156 const void* new_quick_code;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100157 bool uninstall = !entry_exit_stubs_installed_ && !interpreter_stubs_installed_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800158 Runtime* const runtime = Runtime::Current();
159 ClassLinker* const class_linker = runtime->GetClassLinker();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100160 bool is_class_initialized = method->GetDeclaringClass()->IsInitialized();
161 if (uninstall) {
162 if ((forced_interpret_only_ || IsDeoptimized(method)) && !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800163 new_quick_code = GetQuickToInterpreterBridge();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100164 } else if (is_class_initialized || !method->IsStatic() || method->IsConstructor()) {
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000165 if (NeedDebugVersionFor(method)) {
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800166 new_quick_code = GetQuickToInterpreterBridge();
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000167 } else {
168 new_quick_code = class_linker->GetQuickOatCodeFor(method);
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800169 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100170 } else {
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700171 new_quick_code = GetQuickResolutionStub();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100172 }
173 } else { // !uninstall
Sebastien Hertzbae182c2013-12-17 10:42:03 +0100174 if ((interpreter_stubs_installed_ || forced_interpret_only_ || IsDeoptimized(method)) &&
175 !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800176 new_quick_code = GetQuickToInterpreterBridge();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100177 } else {
178 // Do not overwrite resolution trampoline. When the trampoline initializes the method's
179 // class, all its static methods code will be set to the instrumentation entry point.
180 // For more details, see ClassLinker::FixupStaticTrampolines.
181 if (is_class_initialized || !method->IsStatic() || method->IsConstructor()) {
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000182 if (NeedDebugVersionFor(method)) {
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800183 // Oat code should not be used. Don't install instrumentation stub and
184 // use interpreter for instrumentation.
185 new_quick_code = GetQuickToInterpreterBridge();
186 } else if (entry_exit_stubs_installed_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800187 new_quick_code = GetQuickInstrumentationEntryPoint();
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000188 } else {
189 new_quick_code = class_linker->GetQuickOatCodeFor(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100190 }
191 } else {
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700192 new_quick_code = GetQuickResolutionStub();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100193 }
194 }
195 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800196 UpdateEntrypoints(method, new_quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100197}
198
Ian Rogers62d6c772013-02-27 08:32:07 -0800199// Places the instrumentation exit pc as the return PC for every quick frame. This also allows
200// deoptimization of quick frames to interpreter frames.
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100201// Since we may already have done this previously, we need to push new instrumentation frame before
202// existing instrumentation frames.
Ian Rogers62d6c772013-02-27 08:32:07 -0800203static void InstrumentationInstallStack(Thread* thread, void* arg)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700204 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200205 struct InstallStackVisitor FINAL : public StackVisitor {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800206 InstallStackVisitor(Thread* thread_in, Context* context, uintptr_t instrumentation_exit_pc)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100207 : StackVisitor(thread_in, context, kInstrumentationStackWalk),
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800208 instrumentation_stack_(thread_in->GetInstrumentationStack()),
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100209 instrumentation_exit_pc_(instrumentation_exit_pc),
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100210 reached_existing_instrumentation_frames_(false), instrumentation_stack_depth_(0),
211 last_return_pc_(0) {
212 }
jeffhao725a9572012-11-13 18:20:12 -0800213
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700214 bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700215 ArtMethod* m = GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700216 if (m == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800217 if (kVerboseInstrumentation) {
218 LOG(INFO) << " Skipping upcall. Frame " << GetFrameId();
219 }
220 last_return_pc_ = 0;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700221 return true; // Ignore upcalls.
Ian Rogers306057f2012-11-26 12:45:53 -0800222 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700223 if (GetCurrentQuickFrame() == nullptr) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800224 bool interpreter_frame = true;
Sebastien Hertz320deb22014-06-11 19:45:05 +0200225 InstrumentationStackFrame instrumentation_frame(GetThisObject(), m, 0, GetFrameId(),
226 interpreter_frame);
Jeff Haoa15a81b2014-05-27 18:25:47 -0700227 if (kVerboseInstrumentation) {
228 LOG(INFO) << "Pushing shadow frame " << instrumentation_frame.Dump();
229 }
230 shadow_stack_.push_back(instrumentation_frame);
231 return true; // Continue.
232 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800233 uintptr_t return_pc = GetReturnPc();
Sebastien Hertz320deb22014-06-11 19:45:05 +0200234 if (kVerboseInstrumentation) {
235 LOG(INFO) << " Installing exit stub in " << DescribeLocation();
236 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100237 if (return_pc == instrumentation_exit_pc_) {
Mingyao Yang2ee17902017-08-30 11:37:08 -0700238 CHECK_LT(instrumentation_stack_depth_, instrumentation_stack_->size());
239
240 if (m->IsRuntimeMethod()) {
241 const InstrumentationStackFrame& frame =
242 instrumentation_stack_->at(instrumentation_stack_depth_);
243 if (frame.interpreter_entry_) {
244 // This instrumentation frame is for an interpreter bridge and is
245 // pushed when executing the instrumented interpreter bridge. So method
246 // enter event must have been reported. However we need to push a DEX pc
247 // into the dex_pcs_ list to match size of instrumentation stack.
248 uint32_t dex_pc = DexFile::kDexNoIndex;
249 dex_pcs_.push_back(dex_pc);
250 last_return_pc_ = frame.return_pc_;
251 ++instrumentation_stack_depth_;
252 return true;
253 }
254 }
255
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100256 // We've reached a frame which has already been installed with instrumentation exit stub.
257 // We should have already installed instrumentation on previous frames.
258 reached_existing_instrumentation_frames_ = true;
259
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200260 const InstrumentationStackFrame& frame =
261 instrumentation_stack_->at(instrumentation_stack_depth_);
David Sehr709b0702016-10-13 09:12:37 -0700262 CHECK_EQ(m, frame.method_) << "Expected " << ArtMethod::PrettyMethod(m)
263 << ", Found " << ArtMethod::PrettyMethod(frame.method_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100264 return_pc = frame.return_pc_;
265 if (kVerboseInstrumentation) {
266 LOG(INFO) << "Ignoring already instrumented " << frame.Dump();
267 }
268 } else {
269 CHECK_NE(return_pc, 0U);
270 CHECK(!reached_existing_instrumentation_frames_);
Mingyao Yang2ee17902017-08-30 11:37:08 -0700271 InstrumentationStackFrame instrumentation_frame(
272 m->IsRuntimeMethod() ? nullptr : GetThisObject(),
273 m,
274 return_pc,
275 GetFrameId(), // A runtime method still gets a frame id.
276 false);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100277 if (kVerboseInstrumentation) {
278 LOG(INFO) << "Pushing frame " << instrumentation_frame.Dump();
279 }
280
Sebastien Hertz320deb22014-06-11 19:45:05 +0200281 // Insert frame at the right position so we do not corrupt the instrumentation stack.
282 // Instrumentation stack frames are in descending frame id order.
283 auto it = instrumentation_stack_->begin();
284 for (auto end = instrumentation_stack_->end(); it != end; ++it) {
285 const InstrumentationStackFrame& current = *it;
286 if (instrumentation_frame.frame_id_ >= current.frame_id_) {
287 break;
288 }
289 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100290 instrumentation_stack_->insert(it, instrumentation_frame);
291 SetReturnPc(instrumentation_exit_pc_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800292 }
Mingyao Yang2ee17902017-08-30 11:37:08 -0700293 uint32_t dex_pc = DexFile::kDexNoIndex;
294 if (last_return_pc_ != 0 &&
295 GetCurrentOatQuickMethodHeader() != nullptr) {
296 dex_pc = GetCurrentOatQuickMethodHeader()->ToDexPc(m, last_return_pc_);
297 }
298 dex_pcs_.push_back(dex_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800299 last_return_pc_ = return_pc;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100300 ++instrumentation_stack_depth_;
Ian Rogers306057f2012-11-26 12:45:53 -0800301 return true; // Continue.
302 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800303 std::deque<InstrumentationStackFrame>* const instrumentation_stack_;
Jeff Haoa15a81b2014-05-27 18:25:47 -0700304 std::vector<InstrumentationStackFrame> shadow_stack_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800305 std::vector<uint32_t> dex_pcs_;
Ian Rogers306057f2012-11-26 12:45:53 -0800306 const uintptr_t instrumentation_exit_pc_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100307 bool reached_existing_instrumentation_frames_;
308 size_t instrumentation_stack_depth_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800309 uintptr_t last_return_pc_;
Ian Rogers306057f2012-11-26 12:45:53 -0800310 };
Ian Rogers62d6c772013-02-27 08:32:07 -0800311 if (kVerboseInstrumentation) {
312 std::string thread_name;
313 thread->GetThreadName(thread_name);
314 LOG(INFO) << "Installing exit stubs in " << thread_name;
Ian Rogers306057f2012-11-26 12:45:53 -0800315 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100316
317 Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg);
Ian Rogers700a4022014-05-19 16:49:03 -0700318 std::unique_ptr<Context> context(Context::Create());
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700319 uintptr_t instrumentation_exit_pc = reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc());
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100320 InstallStackVisitor visitor(thread, context.get(), instrumentation_exit_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800321 visitor.WalkStack(true);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100322 CHECK_EQ(visitor.dex_pcs_.size(), thread->GetInstrumentationStack()->size());
Ian Rogers62d6c772013-02-27 08:32:07 -0800323
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100324 if (instrumentation->ShouldNotifyMethodEnterExitEvents()) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100325 // Create method enter events for all methods currently on the thread's stack. We only do this
326 // if no debugger is attached to prevent from posting events twice.
Jeff Haoa15a81b2014-05-27 18:25:47 -0700327 auto ssi = visitor.shadow_stack_.rbegin();
328 for (auto isi = thread->GetInstrumentationStack()->rbegin(),
329 end = thread->GetInstrumentationStack()->rend(); isi != end; ++isi) {
330 while (ssi != visitor.shadow_stack_.rend() && (*ssi).frame_id_ < (*isi).frame_id_) {
331 instrumentation->MethodEnterEvent(thread, (*ssi).this_object_, (*ssi).method_, 0);
332 ++ssi;
333 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100334 uint32_t dex_pc = visitor.dex_pcs_.back();
335 visitor.dex_pcs_.pop_back();
Sebastien Hertz320deb22014-06-11 19:45:05 +0200336 if (!isi->interpreter_entry_) {
337 instrumentation->MethodEnterEvent(thread, (*isi).this_object_, (*isi).method_, dex_pc);
338 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100339 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800340 }
341 thread->VerifyStack();
Ian Rogers306057f2012-11-26 12:45:53 -0800342}
343
Mingyao Yang99170c62015-07-06 11:10:37 -0700344void Instrumentation::InstrumentThreadStack(Thread* thread) {
345 instrumentation_stubs_installed_ = true;
346 InstrumentationInstallStack(thread, this);
347}
348
Ian Rogers62d6c772013-02-27 08:32:07 -0800349// Removes the instrumentation exit pc as the return PC for every quick frame.
350static void InstrumentationRestoreStack(Thread* thread, void* arg)
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +0000351 REQUIRES(Locks::mutator_lock_) {
352 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
353
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200354 struct RestoreStackVisitor FINAL : public StackVisitor {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800355 RestoreStackVisitor(Thread* thread_in, uintptr_t instrumentation_exit_pc,
Ian Rogers62d6c772013-02-27 08:32:07 -0800356 Instrumentation* instrumentation)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100357 : StackVisitor(thread_in, nullptr, kInstrumentationStackWalk),
358 thread_(thread_in),
Ian Rogers62d6c772013-02-27 08:32:07 -0800359 instrumentation_exit_pc_(instrumentation_exit_pc),
360 instrumentation_(instrumentation),
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800361 instrumentation_stack_(thread_in->GetInstrumentationStack()),
Ian Rogers62d6c772013-02-27 08:32:07 -0800362 frames_removed_(0) {}
Ian Rogers306057f2012-11-26 12:45:53 -0800363
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700364 bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800365 if (instrumentation_stack_->size() == 0) {
jeffhao725a9572012-11-13 18:20:12 -0800366 return false; // Stop.
367 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700368 ArtMethod* m = GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700369 if (GetCurrentQuickFrame() == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800370 if (kVerboseInstrumentation) {
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200371 LOG(INFO) << " Ignoring a shadow frame. Frame " << GetFrameId()
David Sehr709b0702016-10-13 09:12:37 -0700372 << " Method=" << ArtMethod::PrettyMethod(m);
Ian Rogers62d6c772013-02-27 08:32:07 -0800373 }
374 return true; // Ignore shadow frames.
375 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700376 if (m == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800377 if (kVerboseInstrumentation) {
378 LOG(INFO) << " Skipping upcall. Frame " << GetFrameId();
379 }
Ian Rogers306057f2012-11-26 12:45:53 -0800380 return true; // Ignore upcalls.
381 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800382 bool removed_stub = false;
383 // TODO: make this search more efficient?
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100384 const size_t frameId = GetFrameId();
385 for (const InstrumentationStackFrame& instrumentation_frame : *instrumentation_stack_) {
386 if (instrumentation_frame.frame_id_ == frameId) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800387 if (kVerboseInstrumentation) {
388 LOG(INFO) << " Removing exit stub in " << DescribeLocation();
389 }
Jeff Hao9a916d32013-06-27 18:45:37 -0700390 if (instrumentation_frame.interpreter_entry_) {
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700391 CHECK(m == Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs));
Jeff Hao9a916d32013-06-27 18:45:37 -0700392 } else {
David Sehr709b0702016-10-13 09:12:37 -0700393 CHECK(m == instrumentation_frame.method_) << ArtMethod::PrettyMethod(m);
Jeff Hao9a916d32013-06-27 18:45:37 -0700394 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800395 SetReturnPc(instrumentation_frame.return_pc_);
Mingyao Yang2ee17902017-08-30 11:37:08 -0700396 if (instrumentation_->ShouldNotifyMethodEnterExitEvents() &&
397 !m->IsRuntimeMethod()) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100398 // Create the method exit events. As the methods didn't really exit the result is 0.
399 // We only do this if no debugger is attached to prevent from posting events twice.
400 instrumentation_->MethodExitEvent(thread_, instrumentation_frame.this_object_, m,
401 GetDexPc(), JValue());
402 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800403 frames_removed_++;
404 removed_stub = true;
405 break;
406 }
407 }
408 if (!removed_stub) {
409 if (kVerboseInstrumentation) {
410 LOG(INFO) << " No exit stub in " << DescribeLocation();
Ian Rogers306057f2012-11-26 12:45:53 -0800411 }
jeffhao725a9572012-11-13 18:20:12 -0800412 }
413 return true; // Continue.
414 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800415 Thread* const thread_;
Ian Rogers306057f2012-11-26 12:45:53 -0800416 const uintptr_t instrumentation_exit_pc_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800417 Instrumentation* const instrumentation_;
418 std::deque<instrumentation::InstrumentationStackFrame>* const instrumentation_stack_;
419 size_t frames_removed_;
jeffhao725a9572012-11-13 18:20:12 -0800420 };
Ian Rogers62d6c772013-02-27 08:32:07 -0800421 if (kVerboseInstrumentation) {
422 std::string thread_name;
423 thread->GetThreadName(thread_name);
424 LOG(INFO) << "Removing exit stubs in " << thread_name;
425 }
426 std::deque<instrumentation::InstrumentationStackFrame>* stack = thread->GetInstrumentationStack();
427 if (stack->size() > 0) {
428 Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700429 uintptr_t instrumentation_exit_pc =
430 reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc());
Ian Rogers62d6c772013-02-27 08:32:07 -0800431 RestoreStackVisitor visitor(thread, instrumentation_exit_pc, instrumentation);
432 visitor.WalkStack(true);
433 CHECK_EQ(visitor.frames_removed_, stack->size());
434 while (stack->size() > 0) {
435 stack->pop_front();
436 }
jeffhao725a9572012-11-13 18:20:12 -0800437 }
438}
439
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200440static bool HasEvent(Instrumentation::InstrumentationEvent expected, uint32_t events) {
441 return (events & expected) != 0;
442}
443
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000444static void PotentiallyAddListenerTo(Instrumentation::InstrumentationEvent event,
445 uint32_t events,
446 std::list<InstrumentationListener*>& list,
447 InstrumentationListener* listener,
448 bool* has_listener)
449 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_) {
450 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
451 if (!HasEvent(event, events)) {
452 return;
453 }
454 // If there is a free slot in the list, we insert the listener in that slot.
455 // Otherwise we add it to the end of the list.
456 auto it = std::find(list.begin(), list.end(), nullptr);
457 if (it != list.end()) {
458 *it = listener;
459 } else {
460 list.push_back(listener);
461 }
462 *has_listener = true;
463}
464
Ian Rogers62d6c772013-02-27 08:32:07 -0800465void Instrumentation::AddListener(InstrumentationListener* listener, uint32_t events) {
466 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000467 PotentiallyAddListenerTo(kMethodEntered,
468 events,
469 method_entry_listeners_,
470 listener,
471 &have_method_entry_listeners_);
472 PotentiallyAddListenerTo(kMethodExited,
473 events,
474 method_exit_listeners_,
475 listener,
476 &have_method_exit_listeners_);
477 PotentiallyAddListenerTo(kMethodUnwind,
478 events,
479 method_unwind_listeners_,
480 listener,
481 &have_method_unwind_listeners_);
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000482 PotentiallyAddListenerTo(kBranch,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000483 events,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000484 branch_listeners_,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000485 listener,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000486 &have_branch_listeners_);
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000487 PotentiallyAddListenerTo(kInvokeVirtualOrInterface,
488 events,
489 invoke_virtual_or_interface_listeners_,
490 listener,
491 &have_invoke_virtual_or_interface_listeners_);
492 PotentiallyAddListenerTo(kDexPcMoved,
493 events,
494 dex_pc_listeners_,
495 listener,
496 &have_dex_pc_listeners_);
497 PotentiallyAddListenerTo(kFieldRead,
498 events,
499 field_read_listeners_,
500 listener,
501 &have_field_read_listeners_);
502 PotentiallyAddListenerTo(kFieldWritten,
503 events,
504 field_write_listeners_,
505 listener,
506 &have_field_write_listeners_);
Alex Light6e1607e2017-08-23 10:06:18 -0700507 PotentiallyAddListenerTo(kExceptionThrown,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000508 events,
Alex Light6e1607e2017-08-23 10:06:18 -0700509 exception_thrown_listeners_,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000510 listener,
Alex Light6e1607e2017-08-23 10:06:18 -0700511 &have_exception_thrown_listeners_);
Alex Lighte814f9d2017-07-31 16:14:39 -0700512 PotentiallyAddListenerTo(kWatchedFramePop,
513 events,
514 watched_frame_pop_listeners_,
515 listener,
516 &have_watched_frame_pop_listeners_);
Alex Light9fb1ab12017-09-05 09:32:49 -0700517 PotentiallyAddListenerTo(kExceptionHandled,
518 events,
519 exception_handled_listeners_,
520 listener,
521 &have_exception_handled_listeners_);
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200522 UpdateInterpreterHandlerTable();
jeffhao725a9572012-11-13 18:20:12 -0800523}
524
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000525static void PotentiallyRemoveListenerFrom(Instrumentation::InstrumentationEvent event,
526 uint32_t events,
527 std::list<InstrumentationListener*>& list,
528 InstrumentationListener* listener,
529 bool* has_listener)
530 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_) {
531 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
532 if (!HasEvent(event, events)) {
533 return;
534 }
535 auto it = std::find(list.begin(), list.end(), listener);
536 if (it != list.end()) {
537 // Just update the entry, do not remove from the list. Removing entries in the list
538 // is unsafe when mutators are iterating over it.
539 *it = nullptr;
540 }
541
542 // Check if the list contains any non-null listener, and update 'has_listener'.
543 for (InstrumentationListener* l : list) {
544 if (l != nullptr) {
545 *has_listener = true;
546 return;
547 }
548 }
549 *has_listener = false;
550}
551
Ian Rogers62d6c772013-02-27 08:32:07 -0800552void Instrumentation::RemoveListener(InstrumentationListener* listener, uint32_t events) {
553 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000554 PotentiallyRemoveListenerFrom(kMethodEntered,
555 events,
556 method_entry_listeners_,
557 listener,
558 &have_method_entry_listeners_);
559 PotentiallyRemoveListenerFrom(kMethodExited,
560 events,
561 method_exit_listeners_,
562 listener,
563 &have_method_exit_listeners_);
564 PotentiallyRemoveListenerFrom(kMethodUnwind,
565 events,
566 method_unwind_listeners_,
567 listener,
568 &have_method_unwind_listeners_);
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000569 PotentiallyRemoveListenerFrom(kBranch,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000570 events,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000571 branch_listeners_,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000572 listener,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000573 &have_branch_listeners_);
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000574 PotentiallyRemoveListenerFrom(kInvokeVirtualOrInterface,
575 events,
576 invoke_virtual_or_interface_listeners_,
577 listener,
578 &have_invoke_virtual_or_interface_listeners_);
579 PotentiallyRemoveListenerFrom(kDexPcMoved,
580 events,
581 dex_pc_listeners_,
582 listener,
583 &have_dex_pc_listeners_);
584 PotentiallyRemoveListenerFrom(kFieldRead,
585 events,
586 field_read_listeners_,
587 listener,
588 &have_field_read_listeners_);
589 PotentiallyRemoveListenerFrom(kFieldWritten,
590 events,
591 field_write_listeners_,
592 listener,
593 &have_field_write_listeners_);
Alex Light6e1607e2017-08-23 10:06:18 -0700594 PotentiallyRemoveListenerFrom(kExceptionThrown,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000595 events,
Alex Light6e1607e2017-08-23 10:06:18 -0700596 exception_thrown_listeners_,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000597 listener,
Alex Light6e1607e2017-08-23 10:06:18 -0700598 &have_exception_thrown_listeners_);
Alex Lighte814f9d2017-07-31 16:14:39 -0700599 PotentiallyRemoveListenerFrom(kWatchedFramePop,
600 events,
601 watched_frame_pop_listeners_,
602 listener,
603 &have_watched_frame_pop_listeners_);
Alex Light9fb1ab12017-09-05 09:32:49 -0700604 PotentiallyRemoveListenerFrom(kExceptionHandled,
605 events,
606 exception_handled_listeners_,
607 listener,
608 &have_exception_handled_listeners_);
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200609 UpdateInterpreterHandlerTable();
jeffhao725a9572012-11-13 18:20:12 -0800610}
611
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200612Instrumentation::InstrumentationLevel Instrumentation::GetCurrentInstrumentationLevel() const {
Alex Light4ba388a2017-01-27 10:26:49 -0800613 if (interpreter_stubs_installed_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200614 return InstrumentationLevel::kInstrumentWithInterpreter;
Ian Rogers62d6c772013-02-27 08:32:07 -0800615 } else if (entry_exit_stubs_installed_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200616 return InstrumentationLevel::kInstrumentWithInstrumentationStubs;
Ian Rogers62d6c772013-02-27 08:32:07 -0800617 } else {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200618 return InstrumentationLevel::kInstrumentNothing;
Ian Rogers62d6c772013-02-27 08:32:07 -0800619 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200620}
621
Alex Lightdba61482016-12-21 08:20:29 -0800622bool Instrumentation::RequiresInstrumentationInstallation(InstrumentationLevel new_level) const {
Alex Light4ba388a2017-01-27 10:26:49 -0800623 // We need to reinstall instrumentation if we go to a different level.
624 return GetCurrentInstrumentationLevel() != new_level;
Alex Lightdba61482016-12-21 08:20:29 -0800625}
626
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200627void Instrumentation::ConfigureStubs(const char* key, InstrumentationLevel desired_level) {
628 // Store the instrumentation level for this key or remove it.
629 if (desired_level == InstrumentationLevel::kInstrumentNothing) {
630 // The client no longer needs instrumentation.
631 requested_instrumentation_levels_.erase(key);
632 } else {
633 // The client needs instrumentation.
634 requested_instrumentation_levels_.Overwrite(key, desired_level);
635 }
636
637 // Look for the highest required instrumentation level.
638 InstrumentationLevel requested_level = InstrumentationLevel::kInstrumentNothing;
639 for (const auto& v : requested_instrumentation_levels_) {
640 requested_level = std::max(requested_level, v.second);
641 }
642
643 interpret_only_ = (requested_level == InstrumentationLevel::kInstrumentWithInterpreter) ||
644 forced_interpret_only_;
645
Alex Lightdba61482016-12-21 08:20:29 -0800646 if (!RequiresInstrumentationInstallation(requested_level)) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800647 // We're already set.
648 return;
649 }
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100650 Thread* const self = Thread::Current();
Ian Rogers62d6c772013-02-27 08:32:07 -0800651 Runtime* runtime = Runtime::Current();
Sebastien Hertza8a697f2015-01-15 12:28:47 +0100652 Locks::mutator_lock_->AssertExclusiveHeld(self);
Ian Rogers62d6c772013-02-27 08:32:07 -0800653 Locks::thread_list_lock_->AssertNotHeld(self);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200654 if (requested_level > InstrumentationLevel::kInstrumentNothing) {
Alex Light4ba388a2017-01-27 10:26:49 -0800655 if (requested_level == InstrumentationLevel::kInstrumentWithInterpreter) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800656 interpreter_stubs_installed_ = true;
Ian Rogers62d6c772013-02-27 08:32:07 -0800657 entry_exit_stubs_installed_ = true;
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200658 } else {
659 CHECK_EQ(requested_level, InstrumentationLevel::kInstrumentWithInstrumentationStubs);
660 entry_exit_stubs_installed_ = true;
661 interpreter_stubs_installed_ = false;
Ian Rogers62d6c772013-02-27 08:32:07 -0800662 }
Mathieu Chartiere0671ce2015-07-28 17:23:28 -0700663 InstallStubsClassVisitor visitor(this);
664 runtime->GetClassLinker()->VisitClasses(&visitor);
Ian Rogers62d6c772013-02-27 08:32:07 -0800665 instrumentation_stubs_installed_ = true;
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100666 MutexLock mu(self, *Locks::thread_list_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800667 runtime->GetThreadList()->ForEach(InstrumentationInstallStack, this);
668 } else {
669 interpreter_stubs_installed_ = false;
670 entry_exit_stubs_installed_ = false;
Mathieu Chartiere0671ce2015-07-28 17:23:28 -0700671 InstallStubsClassVisitor visitor(this);
672 runtime->GetClassLinker()->VisitClasses(&visitor);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100673 // Restore stack only if there is no method currently deoptimized.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700674 bool empty;
675 {
676 ReaderMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700677 empty = IsDeoptimizedMethodsEmpty(); // Avoid lock violation.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700678 }
679 if (empty) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100680 MutexLock mu(self, *Locks::thread_list_lock_);
681 Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this);
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +0000682 // Only do this after restoring, as walking the stack when restoring will see
683 // the instrumentation exit pc.
684 instrumentation_stubs_installed_ = false;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100685 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800686 }
jeffhao725a9572012-11-13 18:20:12 -0800687}
688
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200689static void ResetQuickAllocEntryPointsForThread(Thread* thread, void* arg ATTRIBUTE_UNUSED) {
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800690 thread->ResetQuickAllocEntryPointsForThread(kUseReadBarrier && thread->GetIsGcMarking());
Ian Rogersfa824272013-11-05 16:12:57 -0800691}
692
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700693void Instrumentation::SetEntrypointsInstrumented(bool instrumented) {
694 Thread* self = Thread::Current();
Mathieu Chartier661974a2014-01-09 11:23:53 -0800695 Runtime* runtime = Runtime::Current();
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700696 Locks::mutator_lock_->AssertNotHeld(self);
697 Locks::instrument_entrypoints_lock_->AssertHeld(self);
698 if (runtime->IsStarted()) {
Mathieu Chartier4f55e222015-09-04 13:26:21 -0700699 ScopedSuspendAll ssa(__FUNCTION__);
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700700 MutexLock mu(self, *Locks::runtime_shutdown_lock_);
Mathieu Chartier661974a2014-01-09 11:23:53 -0800701 SetQuickAllocEntryPointsInstrumented(instrumented);
702 ResetQuickAllocEntryPoints();
Mathieu Chartier50e93312016-03-16 11:25:29 -0700703 alloc_entrypoints_instrumented_ = instrumented;
Mathieu Chartier4f55e222015-09-04 13:26:21 -0700704 } else {
705 MutexLock mu(self, *Locks::runtime_shutdown_lock_);
706 SetQuickAllocEntryPointsInstrumented(instrumented);
Andreas Gampe157c77e2016-10-17 17:44:41 -0700707
708 // Note: ResetQuickAllocEntryPoints only works when the runtime is started. Manually run the
709 // update for just this thread.
Andreas Gampe162ae502016-10-18 10:03:42 -0700710 // Note: self may be null. One of those paths is setting instrumentation in the Heap
711 // constructor for gcstress mode.
712 if (self != nullptr) {
713 ResetQuickAllocEntryPointsForThread(self, nullptr);
714 }
Andreas Gampe157c77e2016-10-17 17:44:41 -0700715
Mathieu Chartier50e93312016-03-16 11:25:29 -0700716 alloc_entrypoints_instrumented_ = instrumented;
Mathieu Chartier661974a2014-01-09 11:23:53 -0800717 }
718}
719
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700720void Instrumentation::InstrumentQuickAllocEntryPoints() {
721 MutexLock mu(Thread::Current(), *Locks::instrument_entrypoints_lock_);
722 InstrumentQuickAllocEntryPointsLocked();
Ian Rogersfa824272013-11-05 16:12:57 -0800723}
724
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700725void Instrumentation::UninstrumentQuickAllocEntryPoints() {
726 MutexLock mu(Thread::Current(), *Locks::instrument_entrypoints_lock_);
727 UninstrumentQuickAllocEntryPointsLocked();
728}
729
730void Instrumentation::InstrumentQuickAllocEntryPointsLocked() {
731 Locks::instrument_entrypoints_lock_->AssertHeld(Thread::Current());
732 if (quick_alloc_entry_points_instrumentation_counter_ == 0) {
733 SetEntrypointsInstrumented(true);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800734 }
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700735 ++quick_alloc_entry_points_instrumentation_counter_;
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700736}
737
738void Instrumentation::UninstrumentQuickAllocEntryPointsLocked() {
739 Locks::instrument_entrypoints_lock_->AssertHeld(Thread::Current());
740 CHECK_GT(quick_alloc_entry_points_instrumentation_counter_, 0U);
741 --quick_alloc_entry_points_instrumentation_counter_;
742 if (quick_alloc_entry_points_instrumentation_counter_ == 0) {
743 SetEntrypointsInstrumented(false);
744 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800745}
746
747void Instrumentation::ResetQuickAllocEntryPoints() {
748 Runtime* runtime = Runtime::Current();
749 if (runtime->IsStarted()) {
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800750 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700751 runtime->GetThreadList()->ForEach(ResetQuickAllocEntryPointsForThread, nullptr);
Ian Rogersfa824272013-11-05 16:12:57 -0800752 }
753}
754
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700755void Instrumentation::UpdateMethodsCodeImpl(ArtMethod* method, const void* quick_code) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800756 const void* new_quick_code;
Ian Rogers62d6c772013-02-27 08:32:07 -0800757 if (LIKELY(!instrumentation_stubs_installed_)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800758 new_quick_code = quick_code;
Jeff Hao65d15d92013-07-16 16:39:33 -0700759 } else {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100760 if ((interpreter_stubs_installed_ || IsDeoptimized(method)) && !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800761 new_quick_code = GetQuickToInterpreterBridge();
Jeff Hao65d15d92013-07-16 16:39:33 -0700762 } else {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700763 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700764 if (class_linker->IsQuickResolutionStub(quick_code) ||
765 class_linker->IsQuickToInterpreterBridge(quick_code)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700766 new_quick_code = quick_code;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700767 } else if (entry_exit_stubs_installed_) {
768 new_quick_code = GetQuickInstrumentationEntryPoint();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700769 } else {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700770 new_quick_code = quick_code;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700771 }
Jeff Hao65d15d92013-07-16 16:39:33 -0700772 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800773 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800774 UpdateEntrypoints(method, new_quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100775}
776
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700777void Instrumentation::UpdateMethodsCode(ArtMethod* method, const void* quick_code) {
778 DCHECK(method->GetDeclaringClass()->IsResolved());
779 UpdateMethodsCodeImpl(method, quick_code);
780}
781
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000782void Instrumentation::UpdateMethodsCodeForJavaDebuggable(ArtMethod* method,
783 const void* quick_code) {
784 // When the runtime is set to Java debuggable, we may update the entry points of
785 // all methods of a class to the interpreter bridge. A method's declaring class
786 // might not be in resolved state yet in that case, so we bypass the DCHECK in
787 // UpdateMethodsCode.
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700788 UpdateMethodsCodeImpl(method, quick_code);
789}
790
Mathieu Chartiere401d142015-04-22 13:56:20 -0700791bool Instrumentation::AddDeoptimizedMethod(ArtMethod* method) {
792 if (IsDeoptimizedMethod(method)) {
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700793 // Already in the map. Return.
794 return false;
795 }
796 // Not found. Add it.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700797 deoptimized_methods_.insert(method);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700798 return true;
799}
800
Mathieu Chartiere401d142015-04-22 13:56:20 -0700801bool Instrumentation::IsDeoptimizedMethod(ArtMethod* method) {
802 return deoptimized_methods_.find(method) != deoptimized_methods_.end();
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700803}
804
Mathieu Chartiere401d142015-04-22 13:56:20 -0700805ArtMethod* Instrumentation::BeginDeoptimizedMethod() {
806 if (deoptimized_methods_.empty()) {
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700807 // Empty.
808 return nullptr;
809 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700810 return *deoptimized_methods_.begin();
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700811}
812
Mathieu Chartiere401d142015-04-22 13:56:20 -0700813bool Instrumentation::RemoveDeoptimizedMethod(ArtMethod* method) {
814 auto it = deoptimized_methods_.find(method);
815 if (it == deoptimized_methods_.end()) {
816 return false;
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700817 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700818 deoptimized_methods_.erase(it);
819 return true;
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700820}
821
822bool Instrumentation::IsDeoptimizedMethodsEmpty() const {
823 return deoptimized_methods_.empty();
824}
825
Mathieu Chartiere401d142015-04-22 13:56:20 -0700826void Instrumentation::Deoptimize(ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100827 CHECK(!method->IsNative());
828 CHECK(!method->IsProxyMethod());
Alex Light9139e002015-10-09 15:59:48 -0700829 CHECK(method->IsInvokable());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100830
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700831 Thread* self = Thread::Current();
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700832 {
833 WriterMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700834 bool has_not_been_deoptimized = AddDeoptimizedMethod(method);
David Sehr709b0702016-10-13 09:12:37 -0700835 CHECK(has_not_been_deoptimized) << "Method " << ArtMethod::PrettyMethod(method)
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200836 << " is already deoptimized";
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700837 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100838 if (!interpreter_stubs_installed_) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800839 UpdateEntrypoints(method, GetQuickInstrumentationEntryPoint());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100840
841 // Install instrumentation exit stub and instrumentation frames. We may already have installed
842 // these previously so it will only cover the newly created frames.
843 instrumentation_stubs_installed_ = true;
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700844 MutexLock mu(self, *Locks::thread_list_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100845 Runtime::Current()->GetThreadList()->ForEach(InstrumentationInstallStack, this);
846 }
847}
848
Mathieu Chartiere401d142015-04-22 13:56:20 -0700849void Instrumentation::Undeoptimize(ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100850 CHECK(!method->IsNative());
851 CHECK(!method->IsProxyMethod());
Alex Light9139e002015-10-09 15:59:48 -0700852 CHECK(method->IsInvokable());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100853
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700854 Thread* self = Thread::Current();
855 bool empty;
856 {
857 WriterMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700858 bool found_and_erased = RemoveDeoptimizedMethod(method);
David Sehr709b0702016-10-13 09:12:37 -0700859 CHECK(found_and_erased) << "Method " << ArtMethod::PrettyMethod(method)
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700860 << " is not deoptimized";
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700861 empty = IsDeoptimizedMethodsEmpty();
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700862 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100863
864 // Restore code and possibly stack only if we did not deoptimize everything.
865 if (!interpreter_stubs_installed_) {
866 // Restore its code or resolution trampoline.
867 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800868 if (method->IsStatic() && !method->IsConstructor() &&
869 !method->GetDeclaringClass()->IsInitialized()) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800870 UpdateEntrypoints(method, GetQuickResolutionStub());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100871 } else {
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000872 const void* quick_code = NeedDebugVersionFor(method)
873 ? GetQuickToInterpreterBridge()
874 : class_linker->GetQuickOatCodeFor(method);
Elliott Hughes956af0f2014-12-11 14:34:28 -0800875 UpdateEntrypoints(method, quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100876 }
877
878 // If there is no deoptimized method left, we can restore the stack of each thread.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700879 if (empty) {
880 MutexLock mu(self, *Locks::thread_list_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100881 Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this);
882 instrumentation_stubs_installed_ = false;
883 }
884 }
885}
886
Mathieu Chartiere401d142015-04-22 13:56:20 -0700887bool Instrumentation::IsDeoptimized(ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100888 DCHECK(method != nullptr);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700889 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700890 return IsDeoptimizedMethod(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100891}
892
893void Instrumentation::EnableDeoptimization() {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700894 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700895 CHECK(IsDeoptimizedMethodsEmpty());
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100896 CHECK_EQ(deoptimization_enabled_, false);
897 deoptimization_enabled_ = true;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100898}
899
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200900void Instrumentation::DisableDeoptimization(const char* key) {
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100901 CHECK_EQ(deoptimization_enabled_, true);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100902 // If we deoptimized everything, undo it.
Alex Lightdba61482016-12-21 08:20:29 -0800903 InstrumentationLevel level = GetCurrentInstrumentationLevel();
904 if (level == InstrumentationLevel::kInstrumentWithInterpreter) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200905 UndeoptimizeEverything(key);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100906 }
907 // Undeoptimized selected methods.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700908 while (true) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700909 ArtMethod* method;
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700910 {
911 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700912 if (IsDeoptimizedMethodsEmpty()) {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700913 break;
914 }
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700915 method = BeginDeoptimizedMethod();
916 CHECK(method != nullptr);
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700917 }
918 Undeoptimize(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100919 }
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100920 deoptimization_enabled_ = false;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100921}
922
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100923// Indicates if instrumentation should notify method enter/exit events to the listeners.
924bool Instrumentation::ShouldNotifyMethodEnterExitEvents() const {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200925 if (!HasMethodEntryListeners() && !HasMethodExitListeners()) {
926 return false;
927 }
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100928 return !deoptimization_enabled_ && !interpreter_stubs_installed_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100929}
930
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200931void Instrumentation::DeoptimizeEverything(const char* key) {
932 CHECK(deoptimization_enabled_);
933 ConfigureStubs(key, InstrumentationLevel::kInstrumentWithInterpreter);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100934}
935
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200936void Instrumentation::UndeoptimizeEverything(const char* key) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100937 CHECK(interpreter_stubs_installed_);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200938 CHECK(deoptimization_enabled_);
939 ConfigureStubs(key, InstrumentationLevel::kInstrumentNothing);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100940}
941
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200942void Instrumentation::EnableMethodTracing(const char* key, bool needs_interpreter) {
943 InstrumentationLevel level;
944 if (needs_interpreter) {
945 level = InstrumentationLevel::kInstrumentWithInterpreter;
946 } else {
947 level = InstrumentationLevel::kInstrumentWithInstrumentationStubs;
948 }
949 ConfigureStubs(key, level);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100950}
951
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200952void Instrumentation::DisableMethodTracing(const char* key) {
953 ConfigureStubs(key, InstrumentationLevel::kInstrumentNothing);
jeffhao725a9572012-11-13 18:20:12 -0800954}
955
Andreas Gampe542451c2016-07-26 09:02:02 -0700956const void* Instrumentation::GetQuickCodeFor(ArtMethod* method, PointerSize pointer_size) const {
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100957 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers62d6c772013-02-27 08:32:07 -0800958 if (LIKELY(!instrumentation_stubs_installed_)) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800959 const void* code = method->GetEntryPointFromQuickCompiledCodePtrSize(pointer_size);
Vladimir Marko8a630572014-04-09 18:45:35 +0100960 DCHECK(code != nullptr);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700961 if (LIKELY(!class_linker->IsQuickResolutionStub(code) &&
962 !class_linker->IsQuickToInterpreterBridge(code)) &&
963 !class_linker->IsQuickResolutionStub(code) &&
964 !class_linker->IsQuickToInterpreterBridge(code)) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800965 return code;
966 }
967 }
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100968 return class_linker->GetQuickOatCodeFor(method);
jeffhao725a9572012-11-13 18:20:12 -0800969}
970
Alex Lightd7661582017-05-01 13:48:16 -0700971void Instrumentation::MethodEnterEventImpl(Thread* thread,
972 ObjPtr<mirror::Object> this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700973 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800974 uint32_t dex_pc) const {
Mingyao Yang2ee17902017-08-30 11:37:08 -0700975 DCHECK(!method->IsRuntimeMethod());
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000976 if (HasMethodEntryListeners()) {
Alex Lightd7661582017-05-01 13:48:16 -0700977 Thread* self = Thread::Current();
978 StackHandleScope<1> hs(self);
979 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000980 for (InstrumentationListener* listener : method_entry_listeners_) {
981 if (listener != nullptr) {
Alex Lightd7661582017-05-01 13:48:16 -0700982 listener->MethodEntered(thread, thiz, method, dex_pc);
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000983 }
984 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800985 }
986}
987
Alex Lightd7661582017-05-01 13:48:16 -0700988void Instrumentation::MethodExitEventImpl(Thread* thread,
989 ObjPtr<mirror::Object> this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700990 ArtMethod* method,
Alex Lightd7661582017-05-01 13:48:16 -0700991 uint32_t dex_pc,
992 const JValue& return_value) const {
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000993 if (HasMethodExitListeners()) {
Alex Lightd7661582017-05-01 13:48:16 -0700994 Thread* self = Thread::Current();
995 StackHandleScope<2> hs(self);
996 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
997 if (method->GetInterfaceMethodIfProxy(kRuntimePointerSize)
998 ->GetReturnTypePrimitive() != Primitive::kPrimNot) {
999 for (InstrumentationListener* listener : method_exit_listeners_) {
1000 if (listener != nullptr) {
1001 listener->MethodExited(thread, thiz, method, dex_pc, return_value);
1002 }
1003 }
1004 } else {
1005 Handle<mirror::Object> ret(hs.NewHandle(return_value.GetL()));
1006 for (InstrumentationListener* listener : method_exit_listeners_) {
1007 if (listener != nullptr) {
1008 listener->MethodExited(thread, thiz, method, dex_pc, ret);
1009 }
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001010 }
1011 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001012 }
1013}
1014
Alex Lightd7661582017-05-01 13:48:16 -07001015void Instrumentation::MethodUnwindEvent(Thread* thread,
1016 mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001017 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -08001018 uint32_t dex_pc) const {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +02001019 if (HasMethodUnwindListeners()) {
Alex Lightd7661582017-05-01 13:48:16 -07001020 Thread* self = Thread::Current();
1021 StackHandleScope<1> hs(self);
1022 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
Mathieu Chartier02e25112013-08-14 16:14:24 -07001023 for (InstrumentationListener* listener : method_unwind_listeners_) {
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001024 if (listener != nullptr) {
Alex Lightd7661582017-05-01 13:48:16 -07001025 listener->MethodUnwind(thread, thiz, method, dex_pc);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001026 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001027 }
1028 }
1029}
1030
Alex Lightd7661582017-05-01 13:48:16 -07001031void Instrumentation::DexPcMovedEventImpl(Thread* thread,
1032 ObjPtr<mirror::Object> this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001033 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -08001034 uint32_t dex_pc) const {
Alex Lightd7661582017-05-01 13:48:16 -07001035 Thread* self = Thread::Current();
1036 StackHandleScope<1> hs(self);
1037 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001038 for (InstrumentationListener* listener : dex_pc_listeners_) {
1039 if (listener != nullptr) {
Alex Lightd7661582017-05-01 13:48:16 -07001040 listener->DexPcMoved(thread, thiz, method, dex_pc);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001041 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001042 }
1043}
1044
Nicolas Geoffray81f0f952016-01-20 16:25:19 +00001045void Instrumentation::BranchImpl(Thread* thread,
1046 ArtMethod* method,
1047 uint32_t dex_pc,
1048 int32_t offset) const {
1049 for (InstrumentationListener* listener : branch_listeners_) {
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001050 if (listener != nullptr) {
Nicolas Geoffray81f0f952016-01-20 16:25:19 +00001051 listener->Branch(thread, method, dex_pc, offset);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001052 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001053 }
1054}
1055
Nicolas Geoffray5550ca82015-08-21 18:38:30 +01001056void Instrumentation::InvokeVirtualOrInterfaceImpl(Thread* thread,
Alex Lightd7661582017-05-01 13:48:16 -07001057 ObjPtr<mirror::Object> this_object,
Nicolas Geoffray5550ca82015-08-21 18:38:30 +01001058 ArtMethod* caller,
1059 uint32_t dex_pc,
1060 ArtMethod* callee) const {
Alex Lightd7661582017-05-01 13:48:16 -07001061 Thread* self = Thread::Current();
1062 StackHandleScope<1> hs(self);
1063 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
Nicolas Geoffray5550ca82015-08-21 18:38:30 +01001064 for (InstrumentationListener* listener : invoke_virtual_or_interface_listeners_) {
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001065 if (listener != nullptr) {
Alex Lightd7661582017-05-01 13:48:16 -07001066 listener->InvokeVirtualOrInterface(thread, thiz, caller, dex_pc, callee);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001067 }
Nicolas Geoffray5550ca82015-08-21 18:38:30 +01001068 }
1069}
1070
Alex Lighte814f9d2017-07-31 16:14:39 -07001071void Instrumentation::WatchedFramePopImpl(Thread* thread, const ShadowFrame& frame) const {
1072 for (InstrumentationListener* listener : watched_frame_pop_listeners_) {
1073 if (listener != nullptr) {
1074 listener->WatchedFramePop(thread, frame);
1075 }
1076 }
1077}
1078
Alex Lightd7661582017-05-01 13:48:16 -07001079void Instrumentation::FieldReadEventImpl(Thread* thread,
1080 ObjPtr<mirror::Object> this_object,
1081 ArtMethod* method,
1082 uint32_t dex_pc,
Mathieu Chartierc7853442015-03-27 14:35:38 -07001083 ArtField* field) const {
Alex Lightd7661582017-05-01 13:48:16 -07001084 Thread* self = Thread::Current();
1085 StackHandleScope<1> hs(self);
1086 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001087 for (InstrumentationListener* listener : field_read_listeners_) {
1088 if (listener != nullptr) {
Alex Lightd7661582017-05-01 13:48:16 -07001089 listener->FieldRead(thread, thiz, method, dex_pc, field);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001090 }
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +02001091 }
1092}
1093
Alex Lightd7661582017-05-01 13:48:16 -07001094void Instrumentation::FieldWriteEventImpl(Thread* thread,
1095 ObjPtr<mirror::Object> this_object,
1096 ArtMethod* method,
1097 uint32_t dex_pc,
1098 ArtField* field,
1099 const JValue& field_value) const {
1100 Thread* self = Thread::Current();
1101 StackHandleScope<2> hs(self);
1102 Handle<mirror::Object> thiz(hs.NewHandle(this_object));
1103 if (field->IsPrimitiveType()) {
1104 for (InstrumentationListener* listener : field_write_listeners_) {
1105 if (listener != nullptr) {
1106 listener->FieldWritten(thread, thiz, method, dex_pc, field, field_value);
1107 }
1108 }
1109 } else {
1110 Handle<mirror::Object> val(hs.NewHandle(field_value.GetL()));
1111 for (InstrumentationListener* listener : field_write_listeners_) {
1112 if (listener != nullptr) {
1113 listener->FieldWritten(thread, thiz, method, dex_pc, field, val);
1114 }
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001115 }
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +02001116 }
1117}
1118
Alex Light6e1607e2017-08-23 10:06:18 -07001119void Instrumentation::ExceptionThrownEvent(Thread* thread,
Sebastien Hertz947ff082013-09-17 14:10:13 +02001120 mirror::Throwable* exception_object) const {
Hiroshi Yamauchi3481f7a2017-02-10 12:07:36 -08001121 Thread* self = Thread::Current();
1122 StackHandleScope<1> hs(self);
1123 Handle<mirror::Throwable> h_exception(hs.NewHandle(exception_object));
Alex Light6e1607e2017-08-23 10:06:18 -07001124 if (HasExceptionThrownListeners()) {
Hiroshi Yamauchi3481f7a2017-02-10 12:07:36 -08001125 DCHECK_EQ(thread->GetException(), h_exception.Get());
Jeff Haoc0bd4da2013-04-11 15:52:28 -07001126 thread->ClearException();
Alex Light6e1607e2017-08-23 10:06:18 -07001127 for (InstrumentationListener* listener : exception_thrown_listeners_) {
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001128 if (listener != nullptr) {
Alex Light6e1607e2017-08-23 10:06:18 -07001129 listener->ExceptionThrown(thread, h_exception);
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001130 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001131 }
Alex Light9fb1ab12017-09-05 09:32:49 -07001132 // See b/65049545 for discussion about this behavior.
1133 thread->AssertNoPendingException();
Hiroshi Yamauchi3481f7a2017-02-10 12:07:36 -08001134 thread->SetException(h_exception.Get());
Ian Rogers62d6c772013-02-27 08:32:07 -08001135 }
1136}
1137
Alex Light9fb1ab12017-09-05 09:32:49 -07001138void Instrumentation::ExceptionHandledEvent(Thread* thread,
1139 mirror::Throwable* exception_object) const {
1140 Thread* self = Thread::Current();
1141 StackHandleScope<1> hs(self);
1142 Handle<mirror::Throwable> h_exception(hs.NewHandle(exception_object));
1143 if (HasExceptionHandledListeners()) {
1144 // We should have cleared the exception so that callers can detect a new one.
1145 DCHECK(thread->GetException() == nullptr);
1146 for (InstrumentationListener* listener : exception_handled_listeners_) {
1147 if (listener != nullptr) {
1148 listener->ExceptionHandled(thread, h_exception);
1149 }
1150 }
1151 }
1152}
1153
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +00001154// Computes a frame ID by ignoring inlined frames.
1155size_t Instrumentation::ComputeFrameId(Thread* self,
1156 size_t frame_depth,
1157 size_t inlined_frames_before_frame) {
1158 CHECK_GE(frame_depth, inlined_frames_before_frame);
1159 size_t no_inline_depth = frame_depth - inlined_frames_before_frame;
1160 return StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk) - no_inline_depth;
1161}
1162
Ian Rogers62d6c772013-02-27 08:32:07 -08001163static void CheckStackDepth(Thread* self, const InstrumentationStackFrame& instrumentation_frame,
1164 int delta)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001165 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +01001166 size_t frame_id = StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk) + delta;
Ian Rogers62d6c772013-02-27 08:32:07 -08001167 if (frame_id != instrumentation_frame.frame_id_) {
1168 LOG(ERROR) << "Expected frame_id=" << frame_id << " but found "
1169 << instrumentation_frame.frame_id_;
1170 StackVisitor::DescribeStack(self);
1171 CHECK_EQ(frame_id, instrumentation_frame.frame_id_);
1172 }
1173}
1174
1175void Instrumentation::PushInstrumentationStackFrame(Thread* self, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001176 ArtMethod* method,
Jeff Hao9a916d32013-06-27 18:45:37 -07001177 uintptr_t lr, bool interpreter_entry) {
Alex Lightb7edcda2017-04-27 13:20:31 -07001178 DCHECK(!self->IsExceptionPending());
Ian Rogers62d6c772013-02-27 08:32:07 -08001179 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
1180 if (kVerboseInstrumentation) {
David Sehr709b0702016-10-13 09:12:37 -07001181 LOG(INFO) << "Entering " << ArtMethod::PrettyMethod(method) << " from PC "
1182 << reinterpret_cast<void*>(lr);
Ian Rogers62d6c772013-02-27 08:32:07 -08001183 }
Alex Lightb7edcda2017-04-27 13:20:31 -07001184
1185 // We send the enter event before pushing the instrumentation frame to make cleanup easier. If the
1186 // event causes an exception we can simply send the unwind event and return.
1187 StackHandleScope<1> hs(self);
1188 Handle<mirror::Object> h_this(hs.NewHandle(this_object));
1189 if (!interpreter_entry) {
1190 MethodEnterEvent(self, h_this.Get(), method, 0);
1191 if (self->IsExceptionPending()) {
1192 MethodUnwindEvent(self, h_this.Get(), method, 0);
1193 return;
1194 }
1195 }
1196
1197 // We have a callee-save frame meaning this value is guaranteed to never be 0.
1198 DCHECK(!self->IsExceptionPending());
1199 size_t frame_id = StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk);
1200
1201 instrumentation::InstrumentationStackFrame instrumentation_frame(h_this.Get(), method, lr,
Jeff Hao9a916d32013-06-27 18:45:37 -07001202 frame_id, interpreter_entry);
Ian Rogers62d6c772013-02-27 08:32:07 -08001203 stack->push_front(instrumentation_frame);
Ian Rogers62d6c772013-02-27 08:32:07 -08001204}
1205
Mingyao Yang2ee17902017-08-30 11:37:08 -07001206DeoptimizationMethodType Instrumentation::GetDeoptimizationMethodType(ArtMethod* method) {
1207 if (method->IsRuntimeMethod()) {
1208 // Certain methods have strict requirement on whether the dex instruction
1209 // should be re-executed upon deoptimization.
1210 if (method == Runtime::Current()->GetCalleeSaveMethod(
1211 CalleeSaveType::kSaveEverythingForClinit)) {
1212 return DeoptimizationMethodType::kKeepDexPc;
1213 }
1214 if (method == Runtime::Current()->GetCalleeSaveMethod(
1215 CalleeSaveType::kSaveEverythingForSuspendCheck)) {
1216 return DeoptimizationMethodType::kKeepDexPc;
1217 }
1218 }
1219 return DeoptimizationMethodType::kDefault;
1220}
1221
1222// Try to get the shorty of a runtime method if it's an invocation stub.
1223struct RuntimeMethodShortyVisitor : public StackVisitor {
1224 explicit RuntimeMethodShortyVisitor(Thread* thread)
1225 : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames),
1226 shorty('V') {}
1227
1228 bool VisitFrame() REQUIRES_SHARED(Locks::mutator_lock_) {
1229 ArtMethod* m = GetMethod();
1230 if (m != nullptr && !m->IsRuntimeMethod()) {
1231 // The first Java method.
1232 if (m->IsNative()) {
1233 // Use JNI method's shorty for the jni stub.
1234 shorty = m->GetShorty()[0];
1235 return false;
1236 }
1237 if (m->IsProxyMethod()) {
1238 // Proxy method just invokes its proxied method via
1239 // art_quick_proxy_invoke_handler.
1240 shorty = m->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty()[0];
1241 return false;
1242 }
1243 const DexFile::CodeItem* code_item = m->GetCodeItem();
1244 const Instruction* instr = Instruction::At(&code_item->insns_[GetDexPc()]);
1245 if (instr->IsInvoke()) {
1246 const DexFile* dex_file = m->GetDexFile();
1247 if (interpreter::IsStringInit(dex_file, instr->VRegB())) {
1248 // Invoking string init constructor is turned into invoking
1249 // StringFactory.newStringFromChars() which returns a string.
1250 shorty = 'L';
1251 return false;
1252 }
1253 // A regular invoke, use callee's shorty.
1254 uint32_t method_idx = instr->VRegB();
1255 shorty = dex_file->GetMethodShorty(method_idx)[0];
1256 }
1257 // Stop stack walking since we've seen a Java frame.
1258 return false;
1259 }
1260 return true;
1261 }
1262
1263 char shorty;
1264};
1265
Alex Lightb7edcda2017-04-27 13:20:31 -07001266TwoWordReturn Instrumentation::PopInstrumentationStackFrame(Thread* self,
1267 uintptr_t* return_pc,
1268 uint64_t* gpr_result,
1269 uint64_t* fpr_result) {
1270 DCHECK(gpr_result != nullptr);
1271 DCHECK(fpr_result != nullptr);
Ian Rogers62d6c772013-02-27 08:32:07 -08001272 // Do the pop.
1273 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
1274 CHECK_GT(stack->size(), 0U);
1275 InstrumentationStackFrame instrumentation_frame = stack->front();
1276 stack->pop_front();
1277
1278 // Set return PC and check the sanity of the stack.
1279 *return_pc = instrumentation_frame.return_pc_;
1280 CheckStackDepth(self, instrumentation_frame, 0);
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001281 self->VerifyStack();
Ian Rogers62d6c772013-02-27 08:32:07 -08001282
Mathieu Chartiere401d142015-04-22 13:56:20 -07001283 ArtMethod* method = instrumentation_frame.method_;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001284 uint32_t length;
Andreas Gampe542451c2016-07-26 09:02:02 -07001285 const PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Mingyao Yang2ee17902017-08-30 11:37:08 -07001286 char return_shorty;
1287
1288 // Runtime method does not call into MethodExitEvent() so there should not be
1289 // suspension point below.
1290 ScopedAssertNoThreadSuspension ants(__FUNCTION__, method->IsRuntimeMethod());
1291 if (method->IsRuntimeMethod()) {
1292 if (method != Runtime::Current()->GetCalleeSaveMethod(
1293 CalleeSaveType::kSaveEverythingForClinit)) {
1294 // If the caller is at an invocation point and the runtime method is not
1295 // for clinit, we need to pass return results to the caller.
1296 // We need the correct shorty to decide whether we need to pass the return
1297 // result for deoptimization below.
1298 RuntimeMethodShortyVisitor visitor(self);
1299 visitor.WalkStack();
1300 return_shorty = visitor.shorty;
1301 } else {
1302 // Some runtime methods such as allocations, unresolved field getters, etc.
1303 // have return value. We don't need to set return_value since MethodExitEvent()
1304 // below isn't called for runtime methods. Deoptimization doesn't need the
1305 // value either since the dex instruction will be re-executed by the
1306 // interpreter, except these two cases:
1307 // (1) For an invoke, which is handled above to get the correct shorty.
1308 // (2) For MONITOR_ENTER/EXIT, which cannot be re-executed since it's not
1309 // idempotent. However there is no return value for it anyway.
1310 return_shorty = 'V';
1311 }
1312 } else {
1313 return_shorty = method->GetInterfaceMethodIfProxy(pointer_size)->GetShorty(&length)[0];
1314 }
1315
Alex Lightb7edcda2017-04-27 13:20:31 -07001316 bool is_ref = return_shorty == '[' || return_shorty == 'L';
1317 StackHandleScope<1> hs(self);
1318 MutableHandle<mirror::Object> res(hs.NewHandle<mirror::Object>(nullptr));
Ian Rogers62d6c772013-02-27 08:32:07 -08001319 JValue return_value;
1320 if (return_shorty == 'V') {
1321 return_value.SetJ(0);
1322 } else if (return_shorty == 'F' || return_shorty == 'D') {
Alex Lightb7edcda2017-04-27 13:20:31 -07001323 return_value.SetJ(*fpr_result);
Ian Rogers62d6c772013-02-27 08:32:07 -08001324 } else {
Alex Lightb7edcda2017-04-27 13:20:31 -07001325 return_value.SetJ(*gpr_result);
1326 }
1327 if (is_ref) {
1328 // Take a handle to the return value so we won't lose it if we suspend.
1329 res.Assign(return_value.GetL());
Ian Rogers62d6c772013-02-27 08:32:07 -08001330 }
1331 // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to
1332 // return_pc.
1333 uint32_t dex_pc = DexFile::kDexNoIndex;
1334 mirror::Object* this_object = instrumentation_frame.this_object_;
Mingyao Yang2ee17902017-08-30 11:37:08 -07001335 if (!method->IsRuntimeMethod() && !instrumentation_frame.interpreter_entry_) {
Sebastien Hertz320deb22014-06-11 19:45:05 +02001336 MethodExitEvent(self, this_object, instrumentation_frame.method_, dex_pc, return_value);
1337 }
jeffhao725a9572012-11-13 18:20:12 -08001338
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01001339 // Deoptimize if the caller needs to continue execution in the interpreter. Do nothing if we get
1340 // back to an upcall.
1341 NthCallerVisitor visitor(self, 1, true);
1342 visitor.WalkStack(true);
Sebastien Hertz270a0e12015-01-16 19:49:09 +01001343 bool deoptimize = (visitor.caller != nullptr) &&
Daniel Mihalyieb076692014-08-22 17:33:31 +02001344 (interpreter_stubs_installed_ || IsDeoptimized(visitor.caller) ||
1345 Dbg::IsForcedInterpreterNeededForUpcall(self, visitor.caller));
Alex Lightb7edcda2017-04-27 13:20:31 -07001346 if (is_ref) {
1347 // Restore the return value if it's a reference since it might have moved.
1348 *reinterpret_cast<mirror::Object**>(gpr_result) = res.Get();
1349 }
Nicolas Geoffraya0619e22016-12-20 13:57:43 +00001350 if (deoptimize && Runtime::Current()->IsAsyncDeoptimizeable(*return_pc)) {
Ian Rogers62d6c772013-02-27 08:32:07 -08001351 if (kVerboseInstrumentation) {
Andreas Gampe46ee31b2016-12-14 10:11:49 -08001352 LOG(INFO) << "Deoptimizing "
1353 << visitor.caller->PrettyMethod()
1354 << " by returning from "
1355 << method->PrettyMethod()
1356 << " with result "
1357 << std::hex << return_value.GetJ() << std::dec
1358 << " in "
1359 << *self;
Ian Rogers62d6c772013-02-27 08:32:07 -08001360 }
Mingyao Yang2ee17902017-08-30 11:37:08 -07001361 DeoptimizationMethodType deopt_method_type = GetDeoptimizationMethodType(method);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01001362 self->PushDeoptimizationContext(return_value,
Mingyao Yang2ee17902017-08-30 11:37:08 -07001363 return_shorty == 'L' || return_shorty == '[',
1364 nullptr /* no pending exception */,
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01001365 false /* from_code */,
Mingyao Yang2ee17902017-08-30 11:37:08 -07001366 deopt_method_type);
Andreas Gamped58342c2014-06-05 14:18:08 -07001367 return GetTwoWordSuccessValue(*return_pc,
1368 reinterpret_cast<uintptr_t>(GetQuickDeoptimizationEntryPoint()));
Ian Rogers62d6c772013-02-27 08:32:07 -08001369 } else {
Nicolas Geoffraya0619e22016-12-20 13:57:43 +00001370 if (deoptimize && !Runtime::Current()->IsAsyncDeoptimizeable(*return_pc)) {
1371 LOG(WARNING) << "Got a deoptimization request on un-deoptimizable " << method->PrettyMethod()
1372 << " at PC " << reinterpret_cast<void*>(*return_pc);
1373 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001374 if (kVerboseInstrumentation) {
David Sehr709b0702016-10-13 09:12:37 -07001375 LOG(INFO) << "Returning from " << method->PrettyMethod()
Brian Carlstrom2d888622013-07-18 17:02:00 -07001376 << " to PC " << reinterpret_cast<void*>(*return_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -08001377 }
Andreas Gamped58342c2014-06-05 14:18:08 -07001378 return GetTwoWordSuccessValue(0, *return_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -08001379 }
jeffhao725a9572012-11-13 18:20:12 -08001380}
1381
Mingyao Yangf711f2c2016-05-23 12:29:39 -07001382uintptr_t Instrumentation::PopMethodForUnwind(Thread* self, bool is_deoptimization) const {
Ian Rogers62d6c772013-02-27 08:32:07 -08001383 // Do the pop.
1384 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
1385 CHECK_GT(stack->size(), 0U);
Alex Lightb7edcda2017-04-27 13:20:31 -07001386 size_t idx = stack->size();
Ian Rogers62d6c772013-02-27 08:32:07 -08001387 InstrumentationStackFrame instrumentation_frame = stack->front();
Ian Rogers62d6c772013-02-27 08:32:07 -08001388
Mathieu Chartiere401d142015-04-22 13:56:20 -07001389 ArtMethod* method = instrumentation_frame.method_;
Ian Rogers62d6c772013-02-27 08:32:07 -08001390 if (is_deoptimization) {
1391 if (kVerboseInstrumentation) {
David Sehr709b0702016-10-13 09:12:37 -07001392 LOG(INFO) << "Popping for deoptimization " << ArtMethod::PrettyMethod(method);
Ian Rogers62d6c772013-02-27 08:32:07 -08001393 }
1394 } else {
1395 if (kVerboseInstrumentation) {
David Sehr709b0702016-10-13 09:12:37 -07001396 LOG(INFO) << "Popping for unwind " << ArtMethod::PrettyMethod(method);
Ian Rogers62d6c772013-02-27 08:32:07 -08001397 }
1398
1399 // Notify listeners of method unwind.
1400 // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to
1401 // return_pc.
1402 uint32_t dex_pc = DexFile::kDexNoIndex;
Mingyao Yang2ee17902017-08-30 11:37:08 -07001403 if (!method->IsRuntimeMethod()) {
1404 MethodUnwindEvent(self, instrumentation_frame.this_object_, method, dex_pc);
1405 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001406 }
Alex Lightb7edcda2017-04-27 13:20:31 -07001407 // TODO: bring back CheckStackDepth(self, instrumentation_frame, 2);
1408 CHECK_EQ(stack->size(), idx);
1409 DCHECK(instrumentation_frame.method_ == stack->front().method_);
1410 stack->pop_front();
Mingyao Yangf711f2c2016-05-23 12:29:39 -07001411 return instrumentation_frame.return_pc_;
Ian Rogers62d6c772013-02-27 08:32:07 -08001412}
1413
1414std::string InstrumentationStackFrame::Dump() const {
1415 std::ostringstream os;
David Sehr709b0702016-10-13 09:12:37 -07001416 os << "Frame " << frame_id_ << " " << ArtMethod::PrettyMethod(method_) << ":"
Ian Rogers62d6c772013-02-27 08:32:07 -08001417 << reinterpret_cast<void*>(return_pc_) << " this=" << reinterpret_cast<void*>(this_object_);
1418 return os.str();
1419}
1420
1421} // namespace instrumentation
jeffhao725a9572012-11-13 18:20:12 -08001422} // namespace art