blob: 3005c2a2f13481f073e1c2fa63f0e66617b20c39 [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"
Mathieu Chartiere401d142015-04-22 13:56:20 -070022#include "art_method-inl.h"
Ian Rogersef7d42f2014-01-06 12:55:46 -080023#include "atomic.h"
jeffhao725a9572012-11-13 18:20:12 -080024#include "class_linker.h"
25#include "debugger.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080026#include "dex_file-inl.h"
Ian Rogersc7dd2952014-10-21 23:31:19 -070027#include "entrypoints/quick/quick_entrypoints.h"
Mathieu Chartierd8891782014-03-02 13:28:37 -080028#include "entrypoints/quick/quick_alloc_entrypoints.h"
Ian Rogers6f3dbba2014-10-14 17:41:57 -070029#include "entrypoints/runtime_asm_entrypoints.h"
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070030#include "gc_root-inl.h"
Sebastien Hertz138dbfc2013-12-04 18:15:25 +010031#include "interpreter/interpreter.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080032#include "jit/jit.h"
33#include "jit/jit_code_cache.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080034#include "mirror/class-inl.h"
35#include "mirror/dex_cache.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080036#include "mirror/object_array-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070037#include "mirror/object-inl.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080038#include "nth_caller_visitor.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010039#include "oat_quick_method_header.h"
jeffhao725a9572012-11-13 18:20:12 -080040#include "thread.h"
41#include "thread_list.h"
jeffhao725a9572012-11-13 18:20:12 -080042
43namespace art {
Ian Rogers62d6c772013-02-27 08:32:07 -080044namespace instrumentation {
jeffhao725a9572012-11-13 18:20:12 -080045
Sebastien Hertz0462c4c2015-04-01 16:34:17 +020046constexpr bool kVerboseInstrumentation = false;
Sebastien Hertz5bfd5c92013-11-15 11:36:07 +010047
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +010048// Instrumentation works on non-inlined frames by updating returned PCs
49// of compiled frames.
50static constexpr StackVisitor::StackWalkKind kInstrumentationStackWalk =
51 StackVisitor::StackWalkKind::kSkipInlinedFrames;
52
Mathieu Chartiere0671ce2015-07-28 17:23:28 -070053class InstallStubsClassVisitor : public ClassVisitor {
54 public:
55 explicit InstallStubsClassVisitor(Instrumentation* instrumentation)
56 : instrumentation_(instrumentation) {}
57
Mathieu Chartier28357fa2016-10-18 16:27:40 -070058 bool operator()(ObjPtr<mirror::Class> klass) OVERRIDE REQUIRES(Locks::mutator_lock_) {
59 instrumentation_->InstallStubsForClass(klass.Ptr());
Mathieu Chartiere0671ce2015-07-28 17:23:28 -070060 return true; // we visit all classes.
61 }
62
63 private:
64 Instrumentation* const instrumentation_;
65};
66
Ian Rogers62d6c772013-02-27 08:32:07 -080067
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -070068Instrumentation::Instrumentation()
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +000069 : instrumentation_stubs_installed_(false),
70 entry_exit_stubs_installed_(false),
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -070071 interpreter_stubs_installed_(false),
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +000072 interpret_only_(false),
73 forced_interpret_only_(false),
74 have_method_entry_listeners_(false),
75 have_method_exit_listeners_(false),
76 have_method_unwind_listeners_(false),
77 have_dex_pc_listeners_(false),
78 have_field_read_listeners_(false),
79 have_field_write_listeners_(false),
80 have_exception_caught_listeners_(false),
Nicolas Geoffray81f0f952016-01-20 16:25:19 +000081 have_branch_listeners_(false),
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +000082 have_invoke_virtual_or_interface_listeners_(false),
Mathieu Chartierb8aa1e42016-04-05 14:36:57 -070083 deoptimized_methods_lock_("deoptimized methods lock", kDeoptimizedMethodsLock),
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -070084 deoptimization_enabled_(false),
85 interpreter_handler_table_(kMainHandlerTable),
Mathieu Chartier50e93312016-03-16 11:25:29 -070086 quick_alloc_entry_points_instrumentation_counter_(0),
87 alloc_entrypoints_instrumented_(false) {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -070088}
89
Sebastien Hertza10aa372015-01-21 17:30:58 +010090void Instrumentation::InstallStubsForClass(mirror::Class* klass) {
Vladimir Marko72ab6842017-01-20 19:32:50 +000091 if (!klass->IsResolved()) {
Sebastien Hertza8a697f2015-01-15 12:28:47 +010092 // We need the class to be resolved to install/uninstall stubs. Otherwise its methods
93 // could not be initialized or linked with regards to class inheritance.
Vladimir Marko72ab6842017-01-20 19:32:50 +000094 } else if (klass->IsErroneousResolved()) {
95 // We can't execute code in a erroneous class: do nothing.
Sebastien Hertza8a697f2015-01-15 12:28:47 +010096 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -070097 for (ArtMethod& method : klass->GetMethods(kRuntimePointerSize)) {
Alex Light51a64d52015-12-17 13:55:59 -080098 InstallStubsForMethod(&method);
Sebastien Hertza8a697f2015-01-15 12:28:47 +010099 }
jeffhao725a9572012-11-13 18:20:12 -0800100 }
jeffhao725a9572012-11-13 18:20:12 -0800101}
102
Mathieu Chartiere401d142015-04-22 13:56:20 -0700103static void UpdateEntrypoints(ArtMethod* method, const void* quick_code)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700104 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800105 method->SetEntryPointFromQuickCompiledCode(quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100106}
107
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000108bool Instrumentation::NeedDebugVersionFor(ArtMethod* method) const REQUIRES_SHARED(Locks::mutator_lock_) {
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800109 return Dbg::IsDebuggerActive() &&
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000110 Runtime::Current()->IsJavaDebuggable() &&
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800111 !method->IsNative() &&
112 !method->IsProxyMethod();
113}
114
Mathieu Chartiere401d142015-04-22 13:56:20 -0700115void Instrumentation::InstallStubsForMethod(ArtMethod* method) {
Alex Light9139e002015-10-09 15:59:48 -0700116 if (!method->IsInvokable() || method->IsProxyMethod()) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100117 // Do not change stubs for these methods.
118 return;
119 }
Jeff Hao56802772014-08-19 10:17:36 -0700120 // Don't stub Proxy.<init>. Note that the Proxy class itself is not a proxy class.
121 if (method->IsConstructor() &&
122 method->GetDeclaringClass()->DescriptorEquals("Ljava/lang/reflect/Proxy;")) {
Jeff Haodb8a6642014-08-14 17:18:52 -0700123 return;
124 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800125 const void* new_quick_code;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100126 bool uninstall = !entry_exit_stubs_installed_ && !interpreter_stubs_installed_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800127 Runtime* const runtime = Runtime::Current();
128 ClassLinker* const class_linker = runtime->GetClassLinker();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100129 bool is_class_initialized = method->GetDeclaringClass()->IsInitialized();
130 if (uninstall) {
131 if ((forced_interpret_only_ || IsDeoptimized(method)) && !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800132 new_quick_code = GetQuickToInterpreterBridge();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100133 } else if (is_class_initialized || !method->IsStatic() || method->IsConstructor()) {
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000134 if (NeedDebugVersionFor(method)) {
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800135 new_quick_code = GetQuickToInterpreterBridge();
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000136 } else {
137 new_quick_code = class_linker->GetQuickOatCodeFor(method);
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800138 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100139 } else {
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700140 new_quick_code = GetQuickResolutionStub();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100141 }
142 } else { // !uninstall
Sebastien Hertzbae182c2013-12-17 10:42:03 +0100143 if ((interpreter_stubs_installed_ || forced_interpret_only_ || IsDeoptimized(method)) &&
144 !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800145 new_quick_code = GetQuickToInterpreterBridge();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100146 } else {
147 // Do not overwrite resolution trampoline. When the trampoline initializes the method's
148 // class, all its static methods code will be set to the instrumentation entry point.
149 // For more details, see ClassLinker::FixupStaticTrampolines.
150 if (is_class_initialized || !method->IsStatic() || method->IsConstructor()) {
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000151 if (NeedDebugVersionFor(method)) {
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800152 // Oat code should not be used. Don't install instrumentation stub and
153 // use interpreter for instrumentation.
154 new_quick_code = GetQuickToInterpreterBridge();
155 } else if (entry_exit_stubs_installed_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800156 new_quick_code = GetQuickInstrumentationEntryPoint();
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000157 } else {
158 new_quick_code = class_linker->GetQuickOatCodeFor(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100159 }
160 } else {
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700161 new_quick_code = GetQuickResolutionStub();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100162 }
163 }
164 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800165 UpdateEntrypoints(method, new_quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100166}
167
Ian Rogers62d6c772013-02-27 08:32:07 -0800168// Places the instrumentation exit pc as the return PC for every quick frame. This also allows
169// deoptimization of quick frames to interpreter frames.
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100170// Since we may already have done this previously, we need to push new instrumentation frame before
171// existing instrumentation frames.
Ian Rogers62d6c772013-02-27 08:32:07 -0800172static void InstrumentationInstallStack(Thread* thread, void* arg)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700173 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200174 struct InstallStackVisitor FINAL : public StackVisitor {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800175 InstallStackVisitor(Thread* thread_in, Context* context, uintptr_t instrumentation_exit_pc)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100176 : StackVisitor(thread_in, context, kInstrumentationStackWalk),
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800177 instrumentation_stack_(thread_in->GetInstrumentationStack()),
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100178 instrumentation_exit_pc_(instrumentation_exit_pc),
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100179 reached_existing_instrumentation_frames_(false), instrumentation_stack_depth_(0),
180 last_return_pc_(0) {
181 }
jeffhao725a9572012-11-13 18:20:12 -0800182
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700183 bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700184 ArtMethod* m = GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700185 if (m == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800186 if (kVerboseInstrumentation) {
187 LOG(INFO) << " Skipping upcall. Frame " << GetFrameId();
188 }
189 last_return_pc_ = 0;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700190 return true; // Ignore upcalls.
Ian Rogers306057f2012-11-26 12:45:53 -0800191 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700192 if (GetCurrentQuickFrame() == nullptr) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800193 bool interpreter_frame = true;
Sebastien Hertz320deb22014-06-11 19:45:05 +0200194 InstrumentationStackFrame instrumentation_frame(GetThisObject(), m, 0, GetFrameId(),
195 interpreter_frame);
Jeff Haoa15a81b2014-05-27 18:25:47 -0700196 if (kVerboseInstrumentation) {
197 LOG(INFO) << "Pushing shadow frame " << instrumentation_frame.Dump();
198 }
199 shadow_stack_.push_back(instrumentation_frame);
200 return true; // Continue.
201 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800202 uintptr_t return_pc = GetReturnPc();
Sebastien Hertz320deb22014-06-11 19:45:05 +0200203 if (m->IsRuntimeMethod()) {
204 if (return_pc == instrumentation_exit_pc_) {
205 if (kVerboseInstrumentation) {
206 LOG(INFO) << " Handling quick to interpreter transition. Frame " << GetFrameId();
207 }
208 CHECK_LT(instrumentation_stack_depth_, instrumentation_stack_->size());
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200209 const InstrumentationStackFrame& frame =
210 instrumentation_stack_->at(instrumentation_stack_depth_);
Sebastien Hertz320deb22014-06-11 19:45:05 +0200211 CHECK(frame.interpreter_entry_);
212 // This is an interpreter frame so method enter event must have been reported. However we
213 // need to push a DEX pc into the dex_pcs_ list to match size of instrumentation stack.
214 // Since we won't report method entry here, we can safely push any DEX pc.
215 dex_pcs_.push_back(0);
216 last_return_pc_ = frame.return_pc_;
217 ++instrumentation_stack_depth_;
218 return true;
219 } else {
220 if (kVerboseInstrumentation) {
221 LOG(INFO) << " Skipping runtime method. Frame " << GetFrameId();
222 }
223 last_return_pc_ = GetReturnPc();
224 return true; // Ignore unresolved methods since they will be instrumented after resolution.
225 }
226 }
227 if (kVerboseInstrumentation) {
228 LOG(INFO) << " Installing exit stub in " << DescribeLocation();
229 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100230 if (return_pc == instrumentation_exit_pc_) {
231 // We've reached a frame which has already been installed with instrumentation exit stub.
232 // We should have already installed instrumentation on previous frames.
233 reached_existing_instrumentation_frames_ = true;
234
235 CHECK_LT(instrumentation_stack_depth_, instrumentation_stack_->size());
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200236 const InstrumentationStackFrame& frame =
237 instrumentation_stack_->at(instrumentation_stack_depth_);
David Sehr709b0702016-10-13 09:12:37 -0700238 CHECK_EQ(m, frame.method_) << "Expected " << ArtMethod::PrettyMethod(m)
239 << ", Found " << ArtMethod::PrettyMethod(frame.method_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100240 return_pc = frame.return_pc_;
241 if (kVerboseInstrumentation) {
242 LOG(INFO) << "Ignoring already instrumented " << frame.Dump();
243 }
244 } else {
245 CHECK_NE(return_pc, 0U);
246 CHECK(!reached_existing_instrumentation_frames_);
247 InstrumentationStackFrame instrumentation_frame(GetThisObject(), m, return_pc, GetFrameId(),
248 false);
249 if (kVerboseInstrumentation) {
250 LOG(INFO) << "Pushing frame " << instrumentation_frame.Dump();
251 }
252
Sebastien Hertz320deb22014-06-11 19:45:05 +0200253 // Insert frame at the right position so we do not corrupt the instrumentation stack.
254 // Instrumentation stack frames are in descending frame id order.
255 auto it = instrumentation_stack_->begin();
256 for (auto end = instrumentation_stack_->end(); it != end; ++it) {
257 const InstrumentationStackFrame& current = *it;
258 if (instrumentation_frame.frame_id_ >= current.frame_id_) {
259 break;
260 }
261 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100262 instrumentation_stack_->insert(it, instrumentation_frame);
263 SetReturnPc(instrumentation_exit_pc_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800264 }
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100265 dex_pcs_.push_back((GetCurrentOatQuickMethodHeader() == nullptr)
266 ? DexFile::kDexNoIndex
267 : GetCurrentOatQuickMethodHeader()->ToDexPc(m, last_return_pc_));
Ian Rogers62d6c772013-02-27 08:32:07 -0800268 last_return_pc_ = return_pc;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100269 ++instrumentation_stack_depth_;
Ian Rogers306057f2012-11-26 12:45:53 -0800270 return true; // Continue.
271 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800272 std::deque<InstrumentationStackFrame>* const instrumentation_stack_;
Jeff Haoa15a81b2014-05-27 18:25:47 -0700273 std::vector<InstrumentationStackFrame> shadow_stack_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800274 std::vector<uint32_t> dex_pcs_;
Ian Rogers306057f2012-11-26 12:45:53 -0800275 const uintptr_t instrumentation_exit_pc_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100276 bool reached_existing_instrumentation_frames_;
277 size_t instrumentation_stack_depth_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800278 uintptr_t last_return_pc_;
Ian Rogers306057f2012-11-26 12:45:53 -0800279 };
Ian Rogers62d6c772013-02-27 08:32:07 -0800280 if (kVerboseInstrumentation) {
281 std::string thread_name;
282 thread->GetThreadName(thread_name);
283 LOG(INFO) << "Installing exit stubs in " << thread_name;
Ian Rogers306057f2012-11-26 12:45:53 -0800284 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100285
286 Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg);
Ian Rogers700a4022014-05-19 16:49:03 -0700287 std::unique_ptr<Context> context(Context::Create());
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700288 uintptr_t instrumentation_exit_pc = reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc());
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100289 InstallStackVisitor visitor(thread, context.get(), instrumentation_exit_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800290 visitor.WalkStack(true);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100291 CHECK_EQ(visitor.dex_pcs_.size(), thread->GetInstrumentationStack()->size());
Ian Rogers62d6c772013-02-27 08:32:07 -0800292
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100293 if (instrumentation->ShouldNotifyMethodEnterExitEvents()) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100294 // Create method enter events for all methods currently on the thread's stack. We only do this
295 // if no debugger is attached to prevent from posting events twice.
Jeff Haoa15a81b2014-05-27 18:25:47 -0700296 auto ssi = visitor.shadow_stack_.rbegin();
297 for (auto isi = thread->GetInstrumentationStack()->rbegin(),
298 end = thread->GetInstrumentationStack()->rend(); isi != end; ++isi) {
299 while (ssi != visitor.shadow_stack_.rend() && (*ssi).frame_id_ < (*isi).frame_id_) {
300 instrumentation->MethodEnterEvent(thread, (*ssi).this_object_, (*ssi).method_, 0);
301 ++ssi;
302 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100303 uint32_t dex_pc = visitor.dex_pcs_.back();
304 visitor.dex_pcs_.pop_back();
Sebastien Hertz320deb22014-06-11 19:45:05 +0200305 if (!isi->interpreter_entry_) {
306 instrumentation->MethodEnterEvent(thread, (*isi).this_object_, (*isi).method_, dex_pc);
307 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100308 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800309 }
310 thread->VerifyStack();
Ian Rogers306057f2012-11-26 12:45:53 -0800311}
312
Mingyao Yang99170c62015-07-06 11:10:37 -0700313void Instrumentation::InstrumentThreadStack(Thread* thread) {
314 instrumentation_stubs_installed_ = true;
315 InstrumentationInstallStack(thread, this);
316}
317
Ian Rogers62d6c772013-02-27 08:32:07 -0800318// Removes the instrumentation exit pc as the return PC for every quick frame.
319static void InstrumentationRestoreStack(Thread* thread, void* arg)
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +0000320 REQUIRES(Locks::mutator_lock_) {
321 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
322
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200323 struct RestoreStackVisitor FINAL : public StackVisitor {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800324 RestoreStackVisitor(Thread* thread_in, uintptr_t instrumentation_exit_pc,
Ian Rogers62d6c772013-02-27 08:32:07 -0800325 Instrumentation* instrumentation)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100326 : StackVisitor(thread_in, nullptr, kInstrumentationStackWalk),
327 thread_(thread_in),
Ian Rogers62d6c772013-02-27 08:32:07 -0800328 instrumentation_exit_pc_(instrumentation_exit_pc),
329 instrumentation_(instrumentation),
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800330 instrumentation_stack_(thread_in->GetInstrumentationStack()),
Ian Rogers62d6c772013-02-27 08:32:07 -0800331 frames_removed_(0) {}
Ian Rogers306057f2012-11-26 12:45:53 -0800332
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700333 bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800334 if (instrumentation_stack_->size() == 0) {
jeffhao725a9572012-11-13 18:20:12 -0800335 return false; // Stop.
336 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700337 ArtMethod* m = GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700338 if (GetCurrentQuickFrame() == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800339 if (kVerboseInstrumentation) {
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200340 LOG(INFO) << " Ignoring a shadow frame. Frame " << GetFrameId()
David Sehr709b0702016-10-13 09:12:37 -0700341 << " Method=" << ArtMethod::PrettyMethod(m);
Ian Rogers62d6c772013-02-27 08:32:07 -0800342 }
343 return true; // Ignore shadow frames.
344 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700345 if (m == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800346 if (kVerboseInstrumentation) {
347 LOG(INFO) << " Skipping upcall. Frame " << GetFrameId();
348 }
Ian Rogers306057f2012-11-26 12:45:53 -0800349 return true; // Ignore upcalls.
350 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800351 bool removed_stub = false;
352 // TODO: make this search more efficient?
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100353 const size_t frameId = GetFrameId();
354 for (const InstrumentationStackFrame& instrumentation_frame : *instrumentation_stack_) {
355 if (instrumentation_frame.frame_id_ == frameId) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800356 if (kVerboseInstrumentation) {
357 LOG(INFO) << " Removing exit stub in " << DescribeLocation();
358 }
Jeff Hao9a916d32013-06-27 18:45:37 -0700359 if (instrumentation_frame.interpreter_entry_) {
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100360 CHECK(m == Runtime::Current()->GetCalleeSaveMethod(Runtime::kSaveRefsAndArgs));
Jeff Hao9a916d32013-06-27 18:45:37 -0700361 } else {
David Sehr709b0702016-10-13 09:12:37 -0700362 CHECK(m == instrumentation_frame.method_) << ArtMethod::PrettyMethod(m);
Jeff Hao9a916d32013-06-27 18:45:37 -0700363 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800364 SetReturnPc(instrumentation_frame.return_pc_);
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100365 if (instrumentation_->ShouldNotifyMethodEnterExitEvents()) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100366 // Create the method exit events. As the methods didn't really exit the result is 0.
367 // We only do this if no debugger is attached to prevent from posting events twice.
368 instrumentation_->MethodExitEvent(thread_, instrumentation_frame.this_object_, m,
369 GetDexPc(), JValue());
370 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800371 frames_removed_++;
372 removed_stub = true;
373 break;
374 }
375 }
376 if (!removed_stub) {
377 if (kVerboseInstrumentation) {
378 LOG(INFO) << " No exit stub in " << DescribeLocation();
Ian Rogers306057f2012-11-26 12:45:53 -0800379 }
jeffhao725a9572012-11-13 18:20:12 -0800380 }
381 return true; // Continue.
382 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800383 Thread* const thread_;
Ian Rogers306057f2012-11-26 12:45:53 -0800384 const uintptr_t instrumentation_exit_pc_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800385 Instrumentation* const instrumentation_;
386 std::deque<instrumentation::InstrumentationStackFrame>* const instrumentation_stack_;
387 size_t frames_removed_;
jeffhao725a9572012-11-13 18:20:12 -0800388 };
Ian Rogers62d6c772013-02-27 08:32:07 -0800389 if (kVerboseInstrumentation) {
390 std::string thread_name;
391 thread->GetThreadName(thread_name);
392 LOG(INFO) << "Removing exit stubs in " << thread_name;
393 }
394 std::deque<instrumentation::InstrumentationStackFrame>* stack = thread->GetInstrumentationStack();
395 if (stack->size() > 0) {
396 Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700397 uintptr_t instrumentation_exit_pc =
398 reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc());
Ian Rogers62d6c772013-02-27 08:32:07 -0800399 RestoreStackVisitor visitor(thread, instrumentation_exit_pc, instrumentation);
400 visitor.WalkStack(true);
401 CHECK_EQ(visitor.frames_removed_, stack->size());
402 while (stack->size() > 0) {
403 stack->pop_front();
404 }
jeffhao725a9572012-11-13 18:20:12 -0800405 }
406}
407
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200408static bool HasEvent(Instrumentation::InstrumentationEvent expected, uint32_t events) {
409 return (events & expected) != 0;
410}
411
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000412static void PotentiallyAddListenerTo(Instrumentation::InstrumentationEvent event,
413 uint32_t events,
414 std::list<InstrumentationListener*>& list,
415 InstrumentationListener* listener,
416 bool* has_listener)
417 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_) {
418 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
419 if (!HasEvent(event, events)) {
420 return;
421 }
422 // If there is a free slot in the list, we insert the listener in that slot.
423 // Otherwise we add it to the end of the list.
424 auto it = std::find(list.begin(), list.end(), nullptr);
425 if (it != list.end()) {
426 *it = listener;
427 } else {
428 list.push_back(listener);
429 }
430 *has_listener = true;
431}
432
Ian Rogers62d6c772013-02-27 08:32:07 -0800433void Instrumentation::AddListener(InstrumentationListener* listener, uint32_t events) {
434 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000435 PotentiallyAddListenerTo(kMethodEntered,
436 events,
437 method_entry_listeners_,
438 listener,
439 &have_method_entry_listeners_);
440 PotentiallyAddListenerTo(kMethodExited,
441 events,
442 method_exit_listeners_,
443 listener,
444 &have_method_exit_listeners_);
445 PotentiallyAddListenerTo(kMethodUnwind,
446 events,
447 method_unwind_listeners_,
448 listener,
449 &have_method_unwind_listeners_);
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000450 PotentiallyAddListenerTo(kBranch,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000451 events,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000452 branch_listeners_,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000453 listener,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000454 &have_branch_listeners_);
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000455 PotentiallyAddListenerTo(kInvokeVirtualOrInterface,
456 events,
457 invoke_virtual_or_interface_listeners_,
458 listener,
459 &have_invoke_virtual_or_interface_listeners_);
460 PotentiallyAddListenerTo(kDexPcMoved,
461 events,
462 dex_pc_listeners_,
463 listener,
464 &have_dex_pc_listeners_);
465 PotentiallyAddListenerTo(kFieldRead,
466 events,
467 field_read_listeners_,
468 listener,
469 &have_field_read_listeners_);
470 PotentiallyAddListenerTo(kFieldWritten,
471 events,
472 field_write_listeners_,
473 listener,
474 &have_field_write_listeners_);
475 PotentiallyAddListenerTo(kExceptionCaught,
476 events,
477 exception_caught_listeners_,
478 listener,
479 &have_exception_caught_listeners_);
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200480 UpdateInterpreterHandlerTable();
jeffhao725a9572012-11-13 18:20:12 -0800481}
482
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000483static void PotentiallyRemoveListenerFrom(Instrumentation::InstrumentationEvent event,
484 uint32_t events,
485 std::list<InstrumentationListener*>& list,
486 InstrumentationListener* listener,
487 bool* has_listener)
488 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_) {
489 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
490 if (!HasEvent(event, events)) {
491 return;
492 }
493 auto it = std::find(list.begin(), list.end(), listener);
494 if (it != list.end()) {
495 // Just update the entry, do not remove from the list. Removing entries in the list
496 // is unsafe when mutators are iterating over it.
497 *it = nullptr;
498 }
499
500 // Check if the list contains any non-null listener, and update 'has_listener'.
501 for (InstrumentationListener* l : list) {
502 if (l != nullptr) {
503 *has_listener = true;
504 return;
505 }
506 }
507 *has_listener = false;
508}
509
Ian Rogers62d6c772013-02-27 08:32:07 -0800510void Instrumentation::RemoveListener(InstrumentationListener* listener, uint32_t events) {
511 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000512 PotentiallyRemoveListenerFrom(kMethodEntered,
513 events,
514 method_entry_listeners_,
515 listener,
516 &have_method_entry_listeners_);
517 PotentiallyRemoveListenerFrom(kMethodExited,
518 events,
519 method_exit_listeners_,
520 listener,
521 &have_method_exit_listeners_);
522 PotentiallyRemoveListenerFrom(kMethodUnwind,
523 events,
524 method_unwind_listeners_,
525 listener,
526 &have_method_unwind_listeners_);
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000527 PotentiallyRemoveListenerFrom(kBranch,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000528 events,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000529 branch_listeners_,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000530 listener,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000531 &have_branch_listeners_);
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000532 PotentiallyRemoveListenerFrom(kInvokeVirtualOrInterface,
533 events,
534 invoke_virtual_or_interface_listeners_,
535 listener,
536 &have_invoke_virtual_or_interface_listeners_);
537 PotentiallyRemoveListenerFrom(kDexPcMoved,
538 events,
539 dex_pc_listeners_,
540 listener,
541 &have_dex_pc_listeners_);
542 PotentiallyRemoveListenerFrom(kFieldRead,
543 events,
544 field_read_listeners_,
545 listener,
546 &have_field_read_listeners_);
547 PotentiallyRemoveListenerFrom(kFieldWritten,
548 events,
549 field_write_listeners_,
550 listener,
551 &have_field_write_listeners_);
552 PotentiallyRemoveListenerFrom(kExceptionCaught,
553 events,
554 exception_caught_listeners_,
555 listener,
556 &have_exception_caught_listeners_);
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200557 UpdateInterpreterHandlerTable();
jeffhao725a9572012-11-13 18:20:12 -0800558}
559
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200560Instrumentation::InstrumentationLevel Instrumentation::GetCurrentInstrumentationLevel() const {
Alex Lightdba61482016-12-21 08:20:29 -0800561 if (interpreter_stubs_installed_ && interpret_only_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200562 return InstrumentationLevel::kInstrumentWithInterpreter;
Alex Lightdba61482016-12-21 08:20:29 -0800563 } else if (interpreter_stubs_installed_) {
564 return InstrumentationLevel::kInstrumentWithInterpreterAndJit;
Ian Rogers62d6c772013-02-27 08:32:07 -0800565 } else if (entry_exit_stubs_installed_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200566 return InstrumentationLevel::kInstrumentWithInstrumentationStubs;
Ian Rogers62d6c772013-02-27 08:32:07 -0800567 } else {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200568 return InstrumentationLevel::kInstrumentNothing;
Ian Rogers62d6c772013-02-27 08:32:07 -0800569 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200570}
571
Alex Lightdba61482016-12-21 08:20:29 -0800572bool Instrumentation::RequiresInstrumentationInstallation(InstrumentationLevel new_level) const {
573 // We need to reinstall instrumentation if we go to a different level or if the current level is
574 // kInstrumentWithInterpreterAndJit since that level does not force all code to always use the
575 // interpreter and so we might have started running optimized code again.
576 return new_level == InstrumentationLevel::kInstrumentWithInterpreterAndJit ||
577 GetCurrentInstrumentationLevel() != new_level;
578}
579
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200580void Instrumentation::ConfigureStubs(const char* key, InstrumentationLevel desired_level) {
581 // Store the instrumentation level for this key or remove it.
582 if (desired_level == InstrumentationLevel::kInstrumentNothing) {
583 // The client no longer needs instrumentation.
584 requested_instrumentation_levels_.erase(key);
585 } else {
586 // The client needs instrumentation.
587 requested_instrumentation_levels_.Overwrite(key, desired_level);
588 }
589
590 // Look for the highest required instrumentation level.
591 InstrumentationLevel requested_level = InstrumentationLevel::kInstrumentNothing;
592 for (const auto& v : requested_instrumentation_levels_) {
593 requested_level = std::max(requested_level, v.second);
594 }
595
596 interpret_only_ = (requested_level == InstrumentationLevel::kInstrumentWithInterpreter) ||
597 forced_interpret_only_;
598
Alex Lightdba61482016-12-21 08:20:29 -0800599 if (!RequiresInstrumentationInstallation(requested_level)) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800600 // We're already set.
601 return;
602 }
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100603 Thread* const self = Thread::Current();
Ian Rogers62d6c772013-02-27 08:32:07 -0800604 Runtime* runtime = Runtime::Current();
Sebastien Hertza8a697f2015-01-15 12:28:47 +0100605 Locks::mutator_lock_->AssertExclusiveHeld(self);
Ian Rogers62d6c772013-02-27 08:32:07 -0800606 Locks::thread_list_lock_->AssertNotHeld(self);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200607 if (requested_level > InstrumentationLevel::kInstrumentNothing) {
Alex Lightdba61482016-12-21 08:20:29 -0800608 if (requested_level >= InstrumentationLevel::kInstrumentWithInterpreterAndJit) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800609 interpreter_stubs_installed_ = true;
Ian Rogers62d6c772013-02-27 08:32:07 -0800610 entry_exit_stubs_installed_ = true;
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200611 } else {
612 CHECK_EQ(requested_level, InstrumentationLevel::kInstrumentWithInstrumentationStubs);
613 entry_exit_stubs_installed_ = true;
614 interpreter_stubs_installed_ = false;
Ian Rogers62d6c772013-02-27 08:32:07 -0800615 }
Mathieu Chartiere0671ce2015-07-28 17:23:28 -0700616 InstallStubsClassVisitor visitor(this);
617 runtime->GetClassLinker()->VisitClasses(&visitor);
Ian Rogers62d6c772013-02-27 08:32:07 -0800618 instrumentation_stubs_installed_ = true;
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100619 MutexLock mu(self, *Locks::thread_list_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800620 runtime->GetThreadList()->ForEach(InstrumentationInstallStack, this);
621 } else {
622 interpreter_stubs_installed_ = false;
623 entry_exit_stubs_installed_ = false;
Mathieu Chartiere0671ce2015-07-28 17:23:28 -0700624 InstallStubsClassVisitor visitor(this);
625 runtime->GetClassLinker()->VisitClasses(&visitor);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100626 // Restore stack only if there is no method currently deoptimized.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700627 bool empty;
628 {
629 ReaderMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700630 empty = IsDeoptimizedMethodsEmpty(); // Avoid lock violation.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700631 }
632 if (empty) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100633 MutexLock mu(self, *Locks::thread_list_lock_);
634 Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this);
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +0000635 // Only do this after restoring, as walking the stack when restoring will see
636 // the instrumentation exit pc.
637 instrumentation_stubs_installed_ = false;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100638 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800639 }
jeffhao725a9572012-11-13 18:20:12 -0800640}
641
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200642static void ResetQuickAllocEntryPointsForThread(Thread* thread, void* arg ATTRIBUTE_UNUSED) {
Mathieu Chartier5ace2012016-11-30 10:15:41 -0800643 thread->ResetQuickAllocEntryPointsForThread(kUseReadBarrier && thread->GetIsGcMarking());
Ian Rogersfa824272013-11-05 16:12:57 -0800644}
645
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700646void Instrumentation::SetEntrypointsInstrumented(bool instrumented) {
647 Thread* self = Thread::Current();
Mathieu Chartier661974a2014-01-09 11:23:53 -0800648 Runtime* runtime = Runtime::Current();
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700649 Locks::mutator_lock_->AssertNotHeld(self);
650 Locks::instrument_entrypoints_lock_->AssertHeld(self);
651 if (runtime->IsStarted()) {
Mathieu Chartier4f55e222015-09-04 13:26:21 -0700652 ScopedSuspendAll ssa(__FUNCTION__);
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700653 MutexLock mu(self, *Locks::runtime_shutdown_lock_);
Mathieu Chartier661974a2014-01-09 11:23:53 -0800654 SetQuickAllocEntryPointsInstrumented(instrumented);
655 ResetQuickAllocEntryPoints();
Mathieu Chartier50e93312016-03-16 11:25:29 -0700656 alloc_entrypoints_instrumented_ = instrumented;
Mathieu Chartier4f55e222015-09-04 13:26:21 -0700657 } else {
658 MutexLock mu(self, *Locks::runtime_shutdown_lock_);
659 SetQuickAllocEntryPointsInstrumented(instrumented);
Andreas Gampe157c77e2016-10-17 17:44:41 -0700660
661 // Note: ResetQuickAllocEntryPoints only works when the runtime is started. Manually run the
662 // update for just this thread.
Andreas Gampe162ae502016-10-18 10:03:42 -0700663 // Note: self may be null. One of those paths is setting instrumentation in the Heap
664 // constructor for gcstress mode.
665 if (self != nullptr) {
666 ResetQuickAllocEntryPointsForThread(self, nullptr);
667 }
Andreas Gampe157c77e2016-10-17 17:44:41 -0700668
Mathieu Chartier50e93312016-03-16 11:25:29 -0700669 alloc_entrypoints_instrumented_ = instrumented;
Mathieu Chartier661974a2014-01-09 11:23:53 -0800670 }
671}
672
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700673void Instrumentation::InstrumentQuickAllocEntryPoints() {
674 MutexLock mu(Thread::Current(), *Locks::instrument_entrypoints_lock_);
675 InstrumentQuickAllocEntryPointsLocked();
Ian Rogersfa824272013-11-05 16:12:57 -0800676}
677
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700678void Instrumentation::UninstrumentQuickAllocEntryPoints() {
679 MutexLock mu(Thread::Current(), *Locks::instrument_entrypoints_lock_);
680 UninstrumentQuickAllocEntryPointsLocked();
681}
682
683void Instrumentation::InstrumentQuickAllocEntryPointsLocked() {
684 Locks::instrument_entrypoints_lock_->AssertHeld(Thread::Current());
685 if (quick_alloc_entry_points_instrumentation_counter_ == 0) {
686 SetEntrypointsInstrumented(true);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800687 }
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700688 ++quick_alloc_entry_points_instrumentation_counter_;
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700689}
690
691void Instrumentation::UninstrumentQuickAllocEntryPointsLocked() {
692 Locks::instrument_entrypoints_lock_->AssertHeld(Thread::Current());
693 CHECK_GT(quick_alloc_entry_points_instrumentation_counter_, 0U);
694 --quick_alloc_entry_points_instrumentation_counter_;
695 if (quick_alloc_entry_points_instrumentation_counter_ == 0) {
696 SetEntrypointsInstrumented(false);
697 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800698}
699
700void Instrumentation::ResetQuickAllocEntryPoints() {
701 Runtime* runtime = Runtime::Current();
702 if (runtime->IsStarted()) {
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800703 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700704 runtime->GetThreadList()->ForEach(ResetQuickAllocEntryPointsForThread, nullptr);
Ian Rogersfa824272013-11-05 16:12:57 -0800705 }
706}
707
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700708void Instrumentation::UpdateMethodsCodeImpl(ArtMethod* method, const void* quick_code) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800709 const void* new_quick_code;
Ian Rogers62d6c772013-02-27 08:32:07 -0800710 if (LIKELY(!instrumentation_stubs_installed_)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800711 new_quick_code = quick_code;
Jeff Hao65d15d92013-07-16 16:39:33 -0700712 } else {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100713 if ((interpreter_stubs_installed_ || IsDeoptimized(method)) && !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800714 new_quick_code = GetQuickToInterpreterBridge();
Jeff Hao65d15d92013-07-16 16:39:33 -0700715 } else {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700716 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700717 if (class_linker->IsQuickResolutionStub(quick_code) ||
718 class_linker->IsQuickToInterpreterBridge(quick_code)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700719 new_quick_code = quick_code;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700720 } else if (entry_exit_stubs_installed_) {
721 new_quick_code = GetQuickInstrumentationEntryPoint();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700722 } else {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700723 new_quick_code = quick_code;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700724 }
Jeff Hao65d15d92013-07-16 16:39:33 -0700725 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800726 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800727 UpdateEntrypoints(method, new_quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100728}
729
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700730void Instrumentation::UpdateMethodsCode(ArtMethod* method, const void* quick_code) {
731 DCHECK(method->GetDeclaringClass()->IsResolved());
732 UpdateMethodsCodeImpl(method, quick_code);
733}
734
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000735void Instrumentation::UpdateMethodsCodeForJavaDebuggable(ArtMethod* method,
736 const void* quick_code) {
737 // When the runtime is set to Java debuggable, we may update the entry points of
738 // all methods of a class to the interpreter bridge. A method's declaring class
739 // might not be in resolved state yet in that case, so we bypass the DCHECK in
740 // UpdateMethodsCode.
Mingyao Yang3fd448a2016-05-10 14:30:41 -0700741 UpdateMethodsCodeImpl(method, quick_code);
742}
743
Mathieu Chartiere401d142015-04-22 13:56:20 -0700744bool Instrumentation::AddDeoptimizedMethod(ArtMethod* method) {
745 if (IsDeoptimizedMethod(method)) {
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700746 // Already in the map. Return.
747 return false;
748 }
749 // Not found. Add it.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700750 deoptimized_methods_.insert(method);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700751 return true;
752}
753
Mathieu Chartiere401d142015-04-22 13:56:20 -0700754bool Instrumentation::IsDeoptimizedMethod(ArtMethod* method) {
755 return deoptimized_methods_.find(method) != deoptimized_methods_.end();
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700756}
757
Mathieu Chartiere401d142015-04-22 13:56:20 -0700758ArtMethod* Instrumentation::BeginDeoptimizedMethod() {
759 if (deoptimized_methods_.empty()) {
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700760 // Empty.
761 return nullptr;
762 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700763 return *deoptimized_methods_.begin();
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700764}
765
Mathieu Chartiere401d142015-04-22 13:56:20 -0700766bool Instrumentation::RemoveDeoptimizedMethod(ArtMethod* method) {
767 auto it = deoptimized_methods_.find(method);
768 if (it == deoptimized_methods_.end()) {
769 return false;
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700770 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700771 deoptimized_methods_.erase(it);
772 return true;
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700773}
774
775bool Instrumentation::IsDeoptimizedMethodsEmpty() const {
776 return deoptimized_methods_.empty();
777}
778
Mathieu Chartiere401d142015-04-22 13:56:20 -0700779void Instrumentation::Deoptimize(ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100780 CHECK(!method->IsNative());
781 CHECK(!method->IsProxyMethod());
Alex Light9139e002015-10-09 15:59:48 -0700782 CHECK(method->IsInvokable());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100783
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700784 Thread* self = Thread::Current();
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700785 {
786 WriterMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700787 bool has_not_been_deoptimized = AddDeoptimizedMethod(method);
David Sehr709b0702016-10-13 09:12:37 -0700788 CHECK(has_not_been_deoptimized) << "Method " << ArtMethod::PrettyMethod(method)
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200789 << " is already deoptimized";
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700790 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100791 if (!interpreter_stubs_installed_) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800792 UpdateEntrypoints(method, GetQuickInstrumentationEntryPoint());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100793
794 // Install instrumentation exit stub and instrumentation frames. We may already have installed
795 // these previously so it will only cover the newly created frames.
796 instrumentation_stubs_installed_ = true;
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700797 MutexLock mu(self, *Locks::thread_list_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100798 Runtime::Current()->GetThreadList()->ForEach(InstrumentationInstallStack, this);
799 }
800}
801
Mathieu Chartiere401d142015-04-22 13:56:20 -0700802void Instrumentation::Undeoptimize(ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100803 CHECK(!method->IsNative());
804 CHECK(!method->IsProxyMethod());
Alex Light9139e002015-10-09 15:59:48 -0700805 CHECK(method->IsInvokable());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100806
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700807 Thread* self = Thread::Current();
808 bool empty;
809 {
810 WriterMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700811 bool found_and_erased = RemoveDeoptimizedMethod(method);
David Sehr709b0702016-10-13 09:12:37 -0700812 CHECK(found_and_erased) << "Method " << ArtMethod::PrettyMethod(method)
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700813 << " is not deoptimized";
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700814 empty = IsDeoptimizedMethodsEmpty();
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700815 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100816
817 // Restore code and possibly stack only if we did not deoptimize everything.
818 if (!interpreter_stubs_installed_) {
819 // Restore its code or resolution trampoline.
820 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800821 if (method->IsStatic() && !method->IsConstructor() &&
822 !method->GetDeclaringClass()->IsInitialized()) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800823 UpdateEntrypoints(method, GetQuickResolutionStub());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100824 } else {
Nicolas Geoffraya0619e22016-12-20 13:57:43 +0000825 const void* quick_code = NeedDebugVersionFor(method)
826 ? GetQuickToInterpreterBridge()
827 : class_linker->GetQuickOatCodeFor(method);
Elliott Hughes956af0f2014-12-11 14:34:28 -0800828 UpdateEntrypoints(method, quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100829 }
830
831 // If there is no deoptimized method left, we can restore the stack of each thread.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700832 if (empty) {
833 MutexLock mu(self, *Locks::thread_list_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100834 Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this);
835 instrumentation_stubs_installed_ = false;
836 }
837 }
838}
839
Mathieu Chartiere401d142015-04-22 13:56:20 -0700840bool Instrumentation::IsDeoptimized(ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100841 DCHECK(method != nullptr);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700842 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700843 return IsDeoptimizedMethod(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100844}
845
846void Instrumentation::EnableDeoptimization() {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700847 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700848 CHECK(IsDeoptimizedMethodsEmpty());
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100849 CHECK_EQ(deoptimization_enabled_, false);
850 deoptimization_enabled_ = true;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100851}
852
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200853void Instrumentation::DisableDeoptimization(const char* key) {
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100854 CHECK_EQ(deoptimization_enabled_, true);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100855 // If we deoptimized everything, undo it.
Alex Lightdba61482016-12-21 08:20:29 -0800856 InstrumentationLevel level = GetCurrentInstrumentationLevel();
857 if (level == InstrumentationLevel::kInstrumentWithInterpreter) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200858 UndeoptimizeEverything(key);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100859 }
860 // Undeoptimized selected methods.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700861 while (true) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700862 ArtMethod* method;
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700863 {
864 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700865 if (IsDeoptimizedMethodsEmpty()) {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700866 break;
867 }
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700868 method = BeginDeoptimizedMethod();
869 CHECK(method != nullptr);
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700870 }
871 Undeoptimize(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100872 }
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100873 deoptimization_enabled_ = false;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100874}
875
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100876// Indicates if instrumentation should notify method enter/exit events to the listeners.
877bool Instrumentation::ShouldNotifyMethodEnterExitEvents() const {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200878 if (!HasMethodEntryListeners() && !HasMethodExitListeners()) {
879 return false;
880 }
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100881 return !deoptimization_enabled_ && !interpreter_stubs_installed_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100882}
883
Alex Lightdba61482016-12-21 08:20:29 -0800884// TODO we don't check deoptimization_enabled_ because currently there isn't really any support for
885// multiple users of instrumentation. Since this is just a temporary state anyway pending work to
886// ensure that the current_method doesn't get kept across suspend points this should be okay.
887// TODO Remove once b/33630159 is resolved.
888void Instrumentation::ReJitEverything(const char* key) {
889 ConfigureStubs(key, InstrumentationLevel::kInstrumentWithInterpreterAndJit);
890}
891
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200892void Instrumentation::DeoptimizeEverything(const char* key) {
893 CHECK(deoptimization_enabled_);
894 ConfigureStubs(key, InstrumentationLevel::kInstrumentWithInterpreter);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100895}
896
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200897void Instrumentation::UndeoptimizeEverything(const char* key) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100898 CHECK(interpreter_stubs_installed_);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200899 CHECK(deoptimization_enabled_);
900 ConfigureStubs(key, InstrumentationLevel::kInstrumentNothing);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100901}
902
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200903void Instrumentation::EnableMethodTracing(const char* key, bool needs_interpreter) {
904 InstrumentationLevel level;
905 if (needs_interpreter) {
906 level = InstrumentationLevel::kInstrumentWithInterpreter;
907 } else {
908 level = InstrumentationLevel::kInstrumentWithInstrumentationStubs;
909 }
910 ConfigureStubs(key, level);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100911}
912
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200913void Instrumentation::DisableMethodTracing(const char* key) {
914 ConfigureStubs(key, InstrumentationLevel::kInstrumentNothing);
jeffhao725a9572012-11-13 18:20:12 -0800915}
916
Andreas Gampe542451c2016-07-26 09:02:02 -0700917const void* Instrumentation::GetQuickCodeFor(ArtMethod* method, PointerSize pointer_size) const {
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100918 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers62d6c772013-02-27 08:32:07 -0800919 if (LIKELY(!instrumentation_stubs_installed_)) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800920 const void* code = method->GetEntryPointFromQuickCompiledCodePtrSize(pointer_size);
Vladimir Marko8a630572014-04-09 18:45:35 +0100921 DCHECK(code != nullptr);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700922 if (LIKELY(!class_linker->IsQuickResolutionStub(code) &&
923 !class_linker->IsQuickToInterpreterBridge(code)) &&
924 !class_linker->IsQuickResolutionStub(code) &&
925 !class_linker->IsQuickToInterpreterBridge(code)) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800926 return code;
927 }
928 }
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100929 return class_linker->GetQuickOatCodeFor(method);
jeffhao725a9572012-11-13 18:20:12 -0800930}
931
Ian Rogers62d6c772013-02-27 08:32:07 -0800932void Instrumentation::MethodEnterEventImpl(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700933 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800934 uint32_t dex_pc) const {
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000935 if (HasMethodEntryListeners()) {
936 for (InstrumentationListener* listener : method_entry_listeners_) {
937 if (listener != nullptr) {
938 listener->MethodEntered(thread, this_object, method, dex_pc);
939 }
940 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800941 }
942}
943
944void Instrumentation::MethodExitEventImpl(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700945 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800946 uint32_t dex_pc, const JValue& return_value) const {
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000947 if (HasMethodExitListeners()) {
948 for (InstrumentationListener* listener : method_exit_listeners_) {
949 if (listener != nullptr) {
950 listener->MethodExited(thread, this_object, method, dex_pc, return_value);
951 }
952 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800953 }
954}
955
956void Instrumentation::MethodUnwindEvent(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700957 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800958 uint32_t dex_pc) const {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200959 if (HasMethodUnwindListeners()) {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700960 for (InstrumentationListener* listener : method_unwind_listeners_) {
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000961 if (listener != nullptr) {
962 listener->MethodUnwind(thread, this_object, method, dex_pc);
963 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800964 }
965 }
966}
967
968void Instrumentation::DexPcMovedEventImpl(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700969 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800970 uint32_t dex_pc) const {
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000971 for (InstrumentationListener* listener : dex_pc_listeners_) {
972 if (listener != nullptr) {
973 listener->DexPcMoved(thread, this_object, method, dex_pc);
974 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800975 }
976}
977
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000978void Instrumentation::BranchImpl(Thread* thread,
979 ArtMethod* method,
980 uint32_t dex_pc,
981 int32_t offset) const {
982 for (InstrumentationListener* listener : branch_listeners_) {
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000983 if (listener != nullptr) {
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000984 listener->Branch(thread, method, dex_pc, offset);
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000985 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800986 }
987}
988
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100989void Instrumentation::InvokeVirtualOrInterfaceImpl(Thread* thread,
990 mirror::Object* this_object,
991 ArtMethod* caller,
992 uint32_t dex_pc,
993 ArtMethod* callee) const {
Roland Levillain91d65e02016-01-19 15:59:16 +0000994 // We cannot have thread suspension since that would cause the this_object parameter to
Mathieu Chartier3fdb3fe2016-01-14 10:24:28 -0800995 // potentially become a dangling pointer. An alternative could be to put it in a handle instead.
Mathieu Chartier268764d2016-09-13 12:09:38 -0700996 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100997 for (InstrumentationListener* listener : invoke_virtual_or_interface_listeners_) {
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000998 if (listener != nullptr) {
999 listener->InvokeVirtualOrInterface(thread, this_object, caller, dex_pc, callee);
1000 }
Nicolas Geoffray5550ca82015-08-21 18:38:30 +01001001 }
1002}
1003
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +02001004void Instrumentation::FieldReadEventImpl(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001005 ArtMethod* method, uint32_t dex_pc,
Mathieu Chartierc7853442015-03-27 14:35:38 -07001006 ArtField* field) const {
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001007 for (InstrumentationListener* listener : field_read_listeners_) {
1008 if (listener != nullptr) {
1009 listener->FieldRead(thread, this_object, method, dex_pc, field);
1010 }
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +02001011 }
1012}
1013
1014void Instrumentation::FieldWriteEventImpl(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001015 ArtMethod* method, uint32_t dex_pc,
Mathieu Chartierc7853442015-03-27 14:35:38 -07001016 ArtField* field, const JValue& field_value) const {
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001017 for (InstrumentationListener* listener : field_write_listeners_) {
1018 if (listener != nullptr) {
1019 listener->FieldWritten(thread, this_object, method, dex_pc, field, field_value);
1020 }
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +02001021 }
1022}
1023
Nicolas Geoffray14691c52015-03-05 10:40:17 +00001024void Instrumentation::ExceptionCaughtEvent(Thread* thread,
Sebastien Hertz947ff082013-09-17 14:10:13 +02001025 mirror::Throwable* exception_object) const {
Sebastien Hertz9f102032014-05-23 08:59:42 +02001026 if (HasExceptionCaughtListeners()) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +00001027 DCHECK_EQ(thread->GetException(), exception_object);
Jeff Haoc0bd4da2013-04-11 15:52:28 -07001028 thread->ClearException();
Nicolas Geoffray514a6162015-11-03 11:44:24 +00001029 for (InstrumentationListener* listener : exception_caught_listeners_) {
1030 if (listener != nullptr) {
1031 listener->ExceptionCaught(thread, exception_object);
1032 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001033 }
Nicolas Geoffray14691c52015-03-05 10:40:17 +00001034 thread->SetException(exception_object);
Ian Rogers62d6c772013-02-27 08:32:07 -08001035 }
1036}
1037
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +00001038// Computes a frame ID by ignoring inlined frames.
1039size_t Instrumentation::ComputeFrameId(Thread* self,
1040 size_t frame_depth,
1041 size_t inlined_frames_before_frame) {
1042 CHECK_GE(frame_depth, inlined_frames_before_frame);
1043 size_t no_inline_depth = frame_depth - inlined_frames_before_frame;
1044 return StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk) - no_inline_depth;
1045}
1046
Ian Rogers62d6c772013-02-27 08:32:07 -08001047static void CheckStackDepth(Thread* self, const InstrumentationStackFrame& instrumentation_frame,
1048 int delta)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001049 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +01001050 size_t frame_id = StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk) + delta;
Ian Rogers62d6c772013-02-27 08:32:07 -08001051 if (frame_id != instrumentation_frame.frame_id_) {
1052 LOG(ERROR) << "Expected frame_id=" << frame_id << " but found "
1053 << instrumentation_frame.frame_id_;
1054 StackVisitor::DescribeStack(self);
1055 CHECK_EQ(frame_id, instrumentation_frame.frame_id_);
1056 }
1057}
1058
1059void Instrumentation::PushInstrumentationStackFrame(Thread* self, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001060 ArtMethod* method,
Jeff Hao9a916d32013-06-27 18:45:37 -07001061 uintptr_t lr, bool interpreter_entry) {
Ian Rogers62d6c772013-02-27 08:32:07 -08001062 // We have a callee-save frame meaning this value is guaranteed to never be 0.
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +01001063 size_t frame_id = StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk);
Ian Rogers62d6c772013-02-27 08:32:07 -08001064 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
1065 if (kVerboseInstrumentation) {
David Sehr709b0702016-10-13 09:12:37 -07001066 LOG(INFO) << "Entering " << ArtMethod::PrettyMethod(method) << " from PC "
1067 << reinterpret_cast<void*>(lr);
Ian Rogers62d6c772013-02-27 08:32:07 -08001068 }
1069 instrumentation::InstrumentationStackFrame instrumentation_frame(this_object, method, lr,
Jeff Hao9a916d32013-06-27 18:45:37 -07001070 frame_id, interpreter_entry);
Ian Rogers62d6c772013-02-27 08:32:07 -08001071 stack->push_front(instrumentation_frame);
1072
Sebastien Hertz320deb22014-06-11 19:45:05 +02001073 if (!interpreter_entry) {
1074 MethodEnterEvent(self, this_object, method, 0);
1075 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001076}
1077
Andreas Gamped58342c2014-06-05 14:18:08 -07001078TwoWordReturn Instrumentation::PopInstrumentationStackFrame(Thread* self, uintptr_t* return_pc,
1079 uint64_t gpr_result,
1080 uint64_t fpr_result) {
Ian Rogers62d6c772013-02-27 08:32:07 -08001081 // Do the pop.
1082 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
1083 CHECK_GT(stack->size(), 0U);
1084 InstrumentationStackFrame instrumentation_frame = stack->front();
1085 stack->pop_front();
1086
1087 // Set return PC and check the sanity of the stack.
1088 *return_pc = instrumentation_frame.return_pc_;
1089 CheckStackDepth(self, instrumentation_frame, 0);
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001090 self->VerifyStack();
Ian Rogers62d6c772013-02-27 08:32:07 -08001091
Mathieu Chartiere401d142015-04-22 13:56:20 -07001092 ArtMethod* method = instrumentation_frame.method_;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001093 uint32_t length;
Andreas Gampe542451c2016-07-26 09:02:02 -07001094 const PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Mingyao Yang99170c62015-07-06 11:10:37 -07001095 char return_shorty = method->GetInterfaceMethodIfProxy(pointer_size)->GetShorty(&length)[0];
Ian Rogers62d6c772013-02-27 08:32:07 -08001096 JValue return_value;
1097 if (return_shorty == 'V') {
1098 return_value.SetJ(0);
1099 } else if (return_shorty == 'F' || return_shorty == 'D') {
1100 return_value.SetJ(fpr_result);
1101 } else {
1102 return_value.SetJ(gpr_result);
1103 }
1104 // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to
1105 // return_pc.
1106 uint32_t dex_pc = DexFile::kDexNoIndex;
1107 mirror::Object* this_object = instrumentation_frame.this_object_;
Sebastien Hertz320deb22014-06-11 19:45:05 +02001108 if (!instrumentation_frame.interpreter_entry_) {
1109 MethodExitEvent(self, this_object, instrumentation_frame.method_, dex_pc, return_value);
1110 }
jeffhao725a9572012-11-13 18:20:12 -08001111
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01001112 // Deoptimize if the caller needs to continue execution in the interpreter. Do nothing if we get
1113 // back to an upcall.
1114 NthCallerVisitor visitor(self, 1, true);
1115 visitor.WalkStack(true);
Sebastien Hertz270a0e12015-01-16 19:49:09 +01001116 bool deoptimize = (visitor.caller != nullptr) &&
Daniel Mihalyieb076692014-08-22 17:33:31 +02001117 (interpreter_stubs_installed_ || IsDeoptimized(visitor.caller) ||
1118 Dbg::IsForcedInterpreterNeededForUpcall(self, visitor.caller));
Nicolas Geoffraya0619e22016-12-20 13:57:43 +00001119 if (deoptimize && Runtime::Current()->IsAsyncDeoptimizeable(*return_pc)) {
Ian Rogers62d6c772013-02-27 08:32:07 -08001120 if (kVerboseInstrumentation) {
Andreas Gampe46ee31b2016-12-14 10:11:49 -08001121 LOG(INFO) << "Deoptimizing "
1122 << visitor.caller->PrettyMethod()
1123 << " by returning from "
1124 << method->PrettyMethod()
1125 << " with result "
1126 << std::hex << return_value.GetJ() << std::dec
1127 << " in "
1128 << *self;
Ian Rogers62d6c772013-02-27 08:32:07 -08001129 }
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01001130 self->PushDeoptimizationContext(return_value,
1131 return_shorty == 'L',
1132 false /* from_code */,
Sebastien Hertz07474662015-08-25 15:12:33 +00001133 nullptr /* no pending exception */);
Andreas Gamped58342c2014-06-05 14:18:08 -07001134 return GetTwoWordSuccessValue(*return_pc,
1135 reinterpret_cast<uintptr_t>(GetQuickDeoptimizationEntryPoint()));
Ian Rogers62d6c772013-02-27 08:32:07 -08001136 } else {
Nicolas Geoffraya0619e22016-12-20 13:57:43 +00001137 if (deoptimize && !Runtime::Current()->IsAsyncDeoptimizeable(*return_pc)) {
1138 LOG(WARNING) << "Got a deoptimization request on un-deoptimizable " << method->PrettyMethod()
1139 << " at PC " << reinterpret_cast<void*>(*return_pc);
1140 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001141 if (kVerboseInstrumentation) {
David Sehr709b0702016-10-13 09:12:37 -07001142 LOG(INFO) << "Returning from " << method->PrettyMethod()
Brian Carlstrom2d888622013-07-18 17:02:00 -07001143 << " to PC " << reinterpret_cast<void*>(*return_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -08001144 }
Andreas Gamped58342c2014-06-05 14:18:08 -07001145 return GetTwoWordSuccessValue(0, *return_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -08001146 }
jeffhao725a9572012-11-13 18:20:12 -08001147}
1148
Mingyao Yangf711f2c2016-05-23 12:29:39 -07001149uintptr_t Instrumentation::PopMethodForUnwind(Thread* self, bool is_deoptimization) const {
Ian Rogers62d6c772013-02-27 08:32:07 -08001150 // Do the pop.
1151 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
1152 CHECK_GT(stack->size(), 0U);
1153 InstrumentationStackFrame instrumentation_frame = stack->front();
1154 // TODO: bring back CheckStackDepth(self, instrumentation_frame, 2);
1155 stack->pop_front();
1156
Mathieu Chartiere401d142015-04-22 13:56:20 -07001157 ArtMethod* method = instrumentation_frame.method_;
Ian Rogers62d6c772013-02-27 08:32:07 -08001158 if (is_deoptimization) {
1159 if (kVerboseInstrumentation) {
David Sehr709b0702016-10-13 09:12:37 -07001160 LOG(INFO) << "Popping for deoptimization " << ArtMethod::PrettyMethod(method);
Ian Rogers62d6c772013-02-27 08:32:07 -08001161 }
1162 } else {
1163 if (kVerboseInstrumentation) {
David Sehr709b0702016-10-13 09:12:37 -07001164 LOG(INFO) << "Popping for unwind " << ArtMethod::PrettyMethod(method);
Ian Rogers62d6c772013-02-27 08:32:07 -08001165 }
1166
1167 // Notify listeners of method unwind.
1168 // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to
1169 // return_pc.
1170 uint32_t dex_pc = DexFile::kDexNoIndex;
1171 MethodUnwindEvent(self, instrumentation_frame.this_object_, method, dex_pc);
1172 }
Mingyao Yangf711f2c2016-05-23 12:29:39 -07001173 return instrumentation_frame.return_pc_;
Ian Rogers62d6c772013-02-27 08:32:07 -08001174}
1175
1176std::string InstrumentationStackFrame::Dump() const {
1177 std::ostringstream os;
David Sehr709b0702016-10-13 09:12:37 -07001178 os << "Frame " << frame_id_ << " " << ArtMethod::PrettyMethod(method_) << ":"
Ian Rogers62d6c772013-02-27 08:32:07 -08001179 << reinterpret_cast<void*>(return_pc_) << " this=" << reinterpret_cast<void*>(this_object_);
1180 return os.str();
1181}
1182
1183} // namespace instrumentation
jeffhao725a9572012-11-13 18:20:12 -08001184} // namespace art