blob: b107b72b554c74d9e6db081fab010bdbf892586a [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 Chartier1aa8ec22016-02-01 10:34:47 -080058 bool operator()(mirror::Class* klass) OVERRIDE REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere0671ce2015-07-28 17:23:28 -070059 instrumentation_->InstallStubsForClass(klass);
60 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 Chartier3b05e9b2014-03-25 09:29:43 -070083 deoptimized_methods_lock_("deoptimized methods lock"),
84 deoptimization_enabled_(false),
85 interpreter_handler_table_(kMainHandlerTable),
86 quick_alloc_entry_points_instrumentation_counter_(0) {
87}
88
Sebastien Hertza10aa372015-01-21 17:30:58 +010089void Instrumentation::InstallStubsForClass(mirror::Class* klass) {
Sebastien Hertza8a697f2015-01-15 12:28:47 +010090 if (klass->IsErroneous()) {
91 // We can't execute code in a erroneous class: do nothing.
92 } else if (!klass->IsResolved()) {
93 // We need the class to be resolved to install/uninstall stubs. Otherwise its methods
94 // could not be initialized or linked with regards to class inheritance.
95 } else {
Alex Light51a64d52015-12-17 13:55:59 -080096 for (ArtMethod& method : klass->GetMethods(sizeof(void*))) {
97 InstallStubsForMethod(&method);
Sebastien Hertza8a697f2015-01-15 12:28:47 +010098 }
jeffhao725a9572012-11-13 18:20:12 -080099 }
jeffhao725a9572012-11-13 18:20:12 -0800100}
101
Mathieu Chartiere401d142015-04-22 13:56:20 -0700102static void UpdateEntrypoints(ArtMethod* method, const void* quick_code)
Mathieu Chartier90443472015-07-16 20:32:27 -0700103 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800104 method->SetEntryPointFromQuickCompiledCode(quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100105}
106
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800107bool Instrumentation::NeedDebugVersionForBootImageCode(ArtMethod* method, const void* code) const
108 SHARED_REQUIRES(Locks::mutator_lock_) {
109 return Dbg::IsDebuggerActive() &&
110 Runtime::Current()->GetHeap()->IsInBootImageOatFile(code) &&
111 !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()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800134 new_quick_code = class_linker->GetQuickOatCodeFor(method);
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800135 if (NeedDebugVersionForBootImageCode(method, new_quick_code)) {
136 new_quick_code = GetQuickToInterpreterBridge();
137 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100138 } else {
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700139 new_quick_code = GetQuickResolutionStub();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100140 }
141 } else { // !uninstall
Sebastien Hertzbae182c2013-12-17 10:42:03 +0100142 if ((interpreter_stubs_installed_ || forced_interpret_only_ || IsDeoptimized(method)) &&
143 !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800144 new_quick_code = GetQuickToInterpreterBridge();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100145 } else {
146 // Do not overwrite resolution trampoline. When the trampoline initializes the method's
147 // class, all its static methods code will be set to the instrumentation entry point.
148 // For more details, see ClassLinker::FixupStaticTrampolines.
149 if (is_class_initialized || !method->IsStatic() || method->IsConstructor()) {
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800150 new_quick_code = class_linker->GetQuickOatCodeFor(method);
151 if (NeedDebugVersionForBootImageCode(method, new_quick_code)) {
152 // 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();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100157 }
158 } else {
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700159 new_quick_code = GetQuickResolutionStub();
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100160 }
161 }
162 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800163 UpdateEntrypoints(method, new_quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100164}
165
Ian Rogers62d6c772013-02-27 08:32:07 -0800166// Places the instrumentation exit pc as the return PC for every quick frame. This also allows
167// deoptimization of quick frames to interpreter frames.
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100168// Since we may already have done this previously, we need to push new instrumentation frame before
169// existing instrumentation frames.
Ian Rogers62d6c772013-02-27 08:32:07 -0800170static void InstrumentationInstallStack(Thread* thread, void* arg)
Mathieu Chartier90443472015-07-16 20:32:27 -0700171 SHARED_REQUIRES(Locks::mutator_lock_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200172 struct InstallStackVisitor FINAL : public StackVisitor {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800173 InstallStackVisitor(Thread* thread_in, Context* context, uintptr_t instrumentation_exit_pc)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100174 : StackVisitor(thread_in, context, kInstrumentationStackWalk),
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800175 instrumentation_stack_(thread_in->GetInstrumentationStack()),
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100176 instrumentation_exit_pc_(instrumentation_exit_pc),
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100177 reached_existing_instrumentation_frames_(false), instrumentation_stack_depth_(0),
178 last_return_pc_(0) {
179 }
jeffhao725a9572012-11-13 18:20:12 -0800180
Mathieu Chartier90443472015-07-16 20:32:27 -0700181 bool VisitFrame() OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700182 ArtMethod* m = GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700183 if (m == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800184 if (kVerboseInstrumentation) {
185 LOG(INFO) << " Skipping upcall. Frame " << GetFrameId();
186 }
187 last_return_pc_ = 0;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700188 return true; // Ignore upcalls.
Ian Rogers306057f2012-11-26 12:45:53 -0800189 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700190 if (GetCurrentQuickFrame() == nullptr) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800191 bool interpreter_frame = true;
Sebastien Hertz320deb22014-06-11 19:45:05 +0200192 InstrumentationStackFrame instrumentation_frame(GetThisObject(), m, 0, GetFrameId(),
193 interpreter_frame);
Jeff Haoa15a81b2014-05-27 18:25:47 -0700194 if (kVerboseInstrumentation) {
195 LOG(INFO) << "Pushing shadow frame " << instrumentation_frame.Dump();
196 }
197 shadow_stack_.push_back(instrumentation_frame);
198 return true; // Continue.
199 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800200 uintptr_t return_pc = GetReturnPc();
Sebastien Hertz320deb22014-06-11 19:45:05 +0200201 if (m->IsRuntimeMethod()) {
202 if (return_pc == instrumentation_exit_pc_) {
203 if (kVerboseInstrumentation) {
204 LOG(INFO) << " Handling quick to interpreter transition. Frame " << GetFrameId();
205 }
206 CHECK_LT(instrumentation_stack_depth_, instrumentation_stack_->size());
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200207 const InstrumentationStackFrame& frame =
208 instrumentation_stack_->at(instrumentation_stack_depth_);
Sebastien Hertz320deb22014-06-11 19:45:05 +0200209 CHECK(frame.interpreter_entry_);
210 // This is an interpreter frame so method enter event must have been reported. However we
211 // need to push a DEX pc into the dex_pcs_ list to match size of instrumentation stack.
212 // Since we won't report method entry here, we can safely push any DEX pc.
213 dex_pcs_.push_back(0);
214 last_return_pc_ = frame.return_pc_;
215 ++instrumentation_stack_depth_;
216 return true;
217 } else {
218 if (kVerboseInstrumentation) {
219 LOG(INFO) << " Skipping runtime method. Frame " << GetFrameId();
220 }
221 last_return_pc_ = GetReturnPc();
222 return true; // Ignore unresolved methods since they will be instrumented after resolution.
223 }
224 }
225 if (kVerboseInstrumentation) {
226 LOG(INFO) << " Installing exit stub in " << DescribeLocation();
227 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100228 if (return_pc == instrumentation_exit_pc_) {
229 // We've reached a frame which has already been installed with instrumentation exit stub.
230 // We should have already installed instrumentation on previous frames.
231 reached_existing_instrumentation_frames_ = true;
232
233 CHECK_LT(instrumentation_stack_depth_, instrumentation_stack_->size());
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200234 const InstrumentationStackFrame& frame =
235 instrumentation_stack_->at(instrumentation_stack_depth_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100236 CHECK_EQ(m, frame.method_) << "Expected " << PrettyMethod(m)
237 << ", Found " << PrettyMethod(frame.method_);
238 return_pc = frame.return_pc_;
239 if (kVerboseInstrumentation) {
240 LOG(INFO) << "Ignoring already instrumented " << frame.Dump();
241 }
242 } else {
243 CHECK_NE(return_pc, 0U);
244 CHECK(!reached_existing_instrumentation_frames_);
245 InstrumentationStackFrame instrumentation_frame(GetThisObject(), m, return_pc, GetFrameId(),
246 false);
247 if (kVerboseInstrumentation) {
248 LOG(INFO) << "Pushing frame " << instrumentation_frame.Dump();
249 }
250
Sebastien Hertz320deb22014-06-11 19:45:05 +0200251 // Insert frame at the right position so we do not corrupt the instrumentation stack.
252 // Instrumentation stack frames are in descending frame id order.
253 auto it = instrumentation_stack_->begin();
254 for (auto end = instrumentation_stack_->end(); it != end; ++it) {
255 const InstrumentationStackFrame& current = *it;
256 if (instrumentation_frame.frame_id_ >= current.frame_id_) {
257 break;
258 }
259 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100260 instrumentation_stack_->insert(it, instrumentation_frame);
261 SetReturnPc(instrumentation_exit_pc_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800262 }
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100263 dex_pcs_.push_back((GetCurrentOatQuickMethodHeader() == nullptr)
264 ? DexFile::kDexNoIndex
265 : GetCurrentOatQuickMethodHeader()->ToDexPc(m, last_return_pc_));
Ian Rogers62d6c772013-02-27 08:32:07 -0800266 last_return_pc_ = return_pc;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100267 ++instrumentation_stack_depth_;
Ian Rogers306057f2012-11-26 12:45:53 -0800268 return true; // Continue.
269 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800270 std::deque<InstrumentationStackFrame>* const instrumentation_stack_;
Jeff Haoa15a81b2014-05-27 18:25:47 -0700271 std::vector<InstrumentationStackFrame> shadow_stack_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800272 std::vector<uint32_t> dex_pcs_;
Ian Rogers306057f2012-11-26 12:45:53 -0800273 const uintptr_t instrumentation_exit_pc_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100274 bool reached_existing_instrumentation_frames_;
275 size_t instrumentation_stack_depth_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800276 uintptr_t last_return_pc_;
Ian Rogers306057f2012-11-26 12:45:53 -0800277 };
Ian Rogers62d6c772013-02-27 08:32:07 -0800278 if (kVerboseInstrumentation) {
279 std::string thread_name;
280 thread->GetThreadName(thread_name);
281 LOG(INFO) << "Installing exit stubs in " << thread_name;
Ian Rogers306057f2012-11-26 12:45:53 -0800282 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100283
284 Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg);
Ian Rogers700a4022014-05-19 16:49:03 -0700285 std::unique_ptr<Context> context(Context::Create());
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700286 uintptr_t instrumentation_exit_pc = reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc());
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100287 InstallStackVisitor visitor(thread, context.get(), instrumentation_exit_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -0800288 visitor.WalkStack(true);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100289 CHECK_EQ(visitor.dex_pcs_.size(), thread->GetInstrumentationStack()->size());
Ian Rogers62d6c772013-02-27 08:32:07 -0800290
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100291 if (instrumentation->ShouldNotifyMethodEnterExitEvents()) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100292 // Create method enter events for all methods currently on the thread's stack. We only do this
293 // if no debugger is attached to prevent from posting events twice.
Jeff Haoa15a81b2014-05-27 18:25:47 -0700294 auto ssi = visitor.shadow_stack_.rbegin();
295 for (auto isi = thread->GetInstrumentationStack()->rbegin(),
296 end = thread->GetInstrumentationStack()->rend(); isi != end; ++isi) {
297 while (ssi != visitor.shadow_stack_.rend() && (*ssi).frame_id_ < (*isi).frame_id_) {
298 instrumentation->MethodEnterEvent(thread, (*ssi).this_object_, (*ssi).method_, 0);
299 ++ssi;
300 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100301 uint32_t dex_pc = visitor.dex_pcs_.back();
302 visitor.dex_pcs_.pop_back();
Sebastien Hertz320deb22014-06-11 19:45:05 +0200303 if (!isi->interpreter_entry_) {
304 instrumentation->MethodEnterEvent(thread, (*isi).this_object_, (*isi).method_, dex_pc);
305 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100306 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800307 }
308 thread->VerifyStack();
Ian Rogers306057f2012-11-26 12:45:53 -0800309}
310
Mingyao Yang99170c62015-07-06 11:10:37 -0700311void Instrumentation::InstrumentThreadStack(Thread* thread) {
312 instrumentation_stubs_installed_ = true;
313 InstrumentationInstallStack(thread, this);
314}
315
Ian Rogers62d6c772013-02-27 08:32:07 -0800316// Removes the instrumentation exit pc as the return PC for every quick frame.
317static void InstrumentationRestoreStack(Thread* thread, void* arg)
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +0000318 REQUIRES(Locks::mutator_lock_) {
319 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
320
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200321 struct RestoreStackVisitor FINAL : public StackVisitor {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800322 RestoreStackVisitor(Thread* thread_in, uintptr_t instrumentation_exit_pc,
Ian Rogers62d6c772013-02-27 08:32:07 -0800323 Instrumentation* instrumentation)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100324 : StackVisitor(thread_in, nullptr, kInstrumentationStackWalk),
325 thread_(thread_in),
Ian Rogers62d6c772013-02-27 08:32:07 -0800326 instrumentation_exit_pc_(instrumentation_exit_pc),
327 instrumentation_(instrumentation),
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800328 instrumentation_stack_(thread_in->GetInstrumentationStack()),
Ian Rogers62d6c772013-02-27 08:32:07 -0800329 frames_removed_(0) {}
Ian Rogers306057f2012-11-26 12:45:53 -0800330
Mathieu Chartier90443472015-07-16 20:32:27 -0700331 bool VisitFrame() OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800332 if (instrumentation_stack_->size() == 0) {
jeffhao725a9572012-11-13 18:20:12 -0800333 return false; // Stop.
334 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700335 ArtMethod* m = GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700336 if (GetCurrentQuickFrame() == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800337 if (kVerboseInstrumentation) {
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200338 LOG(INFO) << " Ignoring a shadow frame. Frame " << GetFrameId()
339 << " Method=" << PrettyMethod(m);
Ian Rogers62d6c772013-02-27 08:32:07 -0800340 }
341 return true; // Ignore shadow frames.
342 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700343 if (m == nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800344 if (kVerboseInstrumentation) {
345 LOG(INFO) << " Skipping upcall. Frame " << GetFrameId();
346 }
Ian Rogers306057f2012-11-26 12:45:53 -0800347 return true; // Ignore upcalls.
348 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800349 bool removed_stub = false;
350 // TODO: make this search more efficient?
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100351 const size_t frameId = GetFrameId();
352 for (const InstrumentationStackFrame& instrumentation_frame : *instrumentation_stack_) {
353 if (instrumentation_frame.frame_id_ == frameId) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800354 if (kVerboseInstrumentation) {
355 LOG(INFO) << " Removing exit stub in " << DescribeLocation();
356 }
Jeff Hao9a916d32013-06-27 18:45:37 -0700357 if (instrumentation_frame.interpreter_entry_) {
358 CHECK(m == Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs));
359 } else {
360 CHECK(m == instrumentation_frame.method_) << PrettyMethod(m);
361 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800362 SetReturnPc(instrumentation_frame.return_pc_);
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100363 if (instrumentation_->ShouldNotifyMethodEnterExitEvents()) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100364 // Create the method exit events. As the methods didn't really exit the result is 0.
365 // We only do this if no debugger is attached to prevent from posting events twice.
366 instrumentation_->MethodExitEvent(thread_, instrumentation_frame.this_object_, m,
367 GetDexPc(), JValue());
368 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800369 frames_removed_++;
370 removed_stub = true;
371 break;
372 }
373 }
374 if (!removed_stub) {
375 if (kVerboseInstrumentation) {
376 LOG(INFO) << " No exit stub in " << DescribeLocation();
Ian Rogers306057f2012-11-26 12:45:53 -0800377 }
jeffhao725a9572012-11-13 18:20:12 -0800378 }
379 return true; // Continue.
380 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800381 Thread* const thread_;
Ian Rogers306057f2012-11-26 12:45:53 -0800382 const uintptr_t instrumentation_exit_pc_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800383 Instrumentation* const instrumentation_;
384 std::deque<instrumentation::InstrumentationStackFrame>* const instrumentation_stack_;
385 size_t frames_removed_;
jeffhao725a9572012-11-13 18:20:12 -0800386 };
Ian Rogers62d6c772013-02-27 08:32:07 -0800387 if (kVerboseInstrumentation) {
388 std::string thread_name;
389 thread->GetThreadName(thread_name);
390 LOG(INFO) << "Removing exit stubs in " << thread_name;
391 }
392 std::deque<instrumentation::InstrumentationStackFrame>* stack = thread->GetInstrumentationStack();
393 if (stack->size() > 0) {
394 Instrumentation* instrumentation = reinterpret_cast<Instrumentation*>(arg);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700395 uintptr_t instrumentation_exit_pc =
396 reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc());
Ian Rogers62d6c772013-02-27 08:32:07 -0800397 RestoreStackVisitor visitor(thread, instrumentation_exit_pc, instrumentation);
398 visitor.WalkStack(true);
399 CHECK_EQ(visitor.frames_removed_, stack->size());
400 while (stack->size() > 0) {
401 stack->pop_front();
402 }
jeffhao725a9572012-11-13 18:20:12 -0800403 }
404}
405
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200406static bool HasEvent(Instrumentation::InstrumentationEvent expected, uint32_t events) {
407 return (events & expected) != 0;
408}
409
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000410static void PotentiallyAddListenerTo(Instrumentation::InstrumentationEvent event,
411 uint32_t events,
412 std::list<InstrumentationListener*>& list,
413 InstrumentationListener* listener,
414 bool* has_listener)
415 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_) {
416 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
417 if (!HasEvent(event, events)) {
418 return;
419 }
420 // If there is a free slot in the list, we insert the listener in that slot.
421 // Otherwise we add it to the end of the list.
422 auto it = std::find(list.begin(), list.end(), nullptr);
423 if (it != list.end()) {
424 *it = listener;
425 } else {
426 list.push_back(listener);
427 }
428 *has_listener = true;
429}
430
Ian Rogers62d6c772013-02-27 08:32:07 -0800431void Instrumentation::AddListener(InstrumentationListener* listener, uint32_t events) {
432 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000433 PotentiallyAddListenerTo(kMethodEntered,
434 events,
435 method_entry_listeners_,
436 listener,
437 &have_method_entry_listeners_);
438 PotentiallyAddListenerTo(kMethodExited,
439 events,
440 method_exit_listeners_,
441 listener,
442 &have_method_exit_listeners_);
443 PotentiallyAddListenerTo(kMethodUnwind,
444 events,
445 method_unwind_listeners_,
446 listener,
447 &have_method_unwind_listeners_);
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000448 PotentiallyAddListenerTo(kBranch,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000449 events,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000450 branch_listeners_,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000451 listener,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000452 &have_branch_listeners_);
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000453 PotentiallyAddListenerTo(kInvokeVirtualOrInterface,
454 events,
455 invoke_virtual_or_interface_listeners_,
456 listener,
457 &have_invoke_virtual_or_interface_listeners_);
458 PotentiallyAddListenerTo(kDexPcMoved,
459 events,
460 dex_pc_listeners_,
461 listener,
462 &have_dex_pc_listeners_);
463 PotentiallyAddListenerTo(kFieldRead,
464 events,
465 field_read_listeners_,
466 listener,
467 &have_field_read_listeners_);
468 PotentiallyAddListenerTo(kFieldWritten,
469 events,
470 field_write_listeners_,
471 listener,
472 &have_field_write_listeners_);
473 PotentiallyAddListenerTo(kExceptionCaught,
474 events,
475 exception_caught_listeners_,
476 listener,
477 &have_exception_caught_listeners_);
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200478 UpdateInterpreterHandlerTable();
jeffhao725a9572012-11-13 18:20:12 -0800479}
480
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000481static void PotentiallyRemoveListenerFrom(Instrumentation::InstrumentationEvent event,
482 uint32_t events,
483 std::list<InstrumentationListener*>& list,
484 InstrumentationListener* listener,
485 bool* has_listener)
486 REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::classlinker_classes_lock_) {
487 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
488 if (!HasEvent(event, events)) {
489 return;
490 }
491 auto it = std::find(list.begin(), list.end(), listener);
492 if (it != list.end()) {
493 // Just update the entry, do not remove from the list. Removing entries in the list
494 // is unsafe when mutators are iterating over it.
495 *it = nullptr;
496 }
497
498 // Check if the list contains any non-null listener, and update 'has_listener'.
499 for (InstrumentationListener* l : list) {
500 if (l != nullptr) {
501 *has_listener = true;
502 return;
503 }
504 }
505 *has_listener = false;
506}
507
Ian Rogers62d6c772013-02-27 08:32:07 -0800508void Instrumentation::RemoveListener(InstrumentationListener* listener, uint32_t events) {
509 Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current());
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000510 PotentiallyRemoveListenerFrom(kMethodEntered,
511 events,
512 method_entry_listeners_,
513 listener,
514 &have_method_entry_listeners_);
515 PotentiallyRemoveListenerFrom(kMethodExited,
516 events,
517 method_exit_listeners_,
518 listener,
519 &have_method_exit_listeners_);
520 PotentiallyRemoveListenerFrom(kMethodUnwind,
521 events,
522 method_unwind_listeners_,
523 listener,
524 &have_method_unwind_listeners_);
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000525 PotentiallyRemoveListenerFrom(kBranch,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000526 events,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000527 branch_listeners_,
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000528 listener,
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000529 &have_branch_listeners_);
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000530 PotentiallyRemoveListenerFrom(kInvokeVirtualOrInterface,
531 events,
532 invoke_virtual_or_interface_listeners_,
533 listener,
534 &have_invoke_virtual_or_interface_listeners_);
535 PotentiallyRemoveListenerFrom(kDexPcMoved,
536 events,
537 dex_pc_listeners_,
538 listener,
539 &have_dex_pc_listeners_);
540 PotentiallyRemoveListenerFrom(kFieldRead,
541 events,
542 field_read_listeners_,
543 listener,
544 &have_field_read_listeners_);
545 PotentiallyRemoveListenerFrom(kFieldWritten,
546 events,
547 field_write_listeners_,
548 listener,
549 &have_field_write_listeners_);
550 PotentiallyRemoveListenerFrom(kExceptionCaught,
551 events,
552 exception_caught_listeners_,
553 listener,
554 &have_exception_caught_listeners_);
Sebastien Hertzee1997a2013-09-19 14:47:09 +0200555 UpdateInterpreterHandlerTable();
jeffhao725a9572012-11-13 18:20:12 -0800556}
557
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200558Instrumentation::InstrumentationLevel Instrumentation::GetCurrentInstrumentationLevel() const {
Ian Rogers62d6c772013-02-27 08:32:07 -0800559 if (interpreter_stubs_installed_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200560 return InstrumentationLevel::kInstrumentWithInterpreter;
Ian Rogers62d6c772013-02-27 08:32:07 -0800561 } else if (entry_exit_stubs_installed_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200562 return InstrumentationLevel::kInstrumentWithInstrumentationStubs;
Ian Rogers62d6c772013-02-27 08:32:07 -0800563 } else {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200564 return InstrumentationLevel::kInstrumentNothing;
Ian Rogers62d6c772013-02-27 08:32:07 -0800565 }
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200566}
567
568void Instrumentation::ConfigureStubs(const char* key, InstrumentationLevel desired_level) {
569 // Store the instrumentation level for this key or remove it.
570 if (desired_level == InstrumentationLevel::kInstrumentNothing) {
571 // The client no longer needs instrumentation.
572 requested_instrumentation_levels_.erase(key);
573 } else {
574 // The client needs instrumentation.
575 requested_instrumentation_levels_.Overwrite(key, desired_level);
576 }
577
578 // Look for the highest required instrumentation level.
579 InstrumentationLevel requested_level = InstrumentationLevel::kInstrumentNothing;
580 for (const auto& v : requested_instrumentation_levels_) {
581 requested_level = std::max(requested_level, v.second);
582 }
583
584 interpret_only_ = (requested_level == InstrumentationLevel::kInstrumentWithInterpreter) ||
585 forced_interpret_only_;
586
587 InstrumentationLevel current_level = GetCurrentInstrumentationLevel();
588 if (requested_level == current_level) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800589 // We're already set.
590 return;
591 }
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100592 Thread* const self = Thread::Current();
Ian Rogers62d6c772013-02-27 08:32:07 -0800593 Runtime* runtime = Runtime::Current();
Sebastien Hertza8a697f2015-01-15 12:28:47 +0100594 Locks::mutator_lock_->AssertExclusiveHeld(self);
Ian Rogers62d6c772013-02-27 08:32:07 -0800595 Locks::thread_list_lock_->AssertNotHeld(self);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200596 if (requested_level > InstrumentationLevel::kInstrumentNothing) {
597 if (requested_level == InstrumentationLevel::kInstrumentWithInterpreter) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800598 interpreter_stubs_installed_ = true;
Ian Rogers62d6c772013-02-27 08:32:07 -0800599 entry_exit_stubs_installed_ = true;
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200600 } else {
601 CHECK_EQ(requested_level, InstrumentationLevel::kInstrumentWithInstrumentationStubs);
602 entry_exit_stubs_installed_ = true;
603 interpreter_stubs_installed_ = false;
Ian Rogers62d6c772013-02-27 08:32:07 -0800604 }
Mathieu Chartiere0671ce2015-07-28 17:23:28 -0700605 InstallStubsClassVisitor visitor(this);
606 runtime->GetClassLinker()->VisitClasses(&visitor);
Ian Rogers62d6c772013-02-27 08:32:07 -0800607 instrumentation_stubs_installed_ = true;
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100608 MutexLock mu(self, *Locks::thread_list_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800609 runtime->GetThreadList()->ForEach(InstrumentationInstallStack, this);
610 } else {
611 interpreter_stubs_installed_ = false;
612 entry_exit_stubs_installed_ = false;
Mathieu Chartiere0671ce2015-07-28 17:23:28 -0700613 InstallStubsClassVisitor visitor(this);
614 runtime->GetClassLinker()->VisitClasses(&visitor);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100615 // Restore stack only if there is no method currently deoptimized.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700616 bool empty;
617 {
618 ReaderMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700619 empty = IsDeoptimizedMethodsEmpty(); // Avoid lock violation.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700620 }
621 if (empty) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100622 MutexLock mu(self, *Locks::thread_list_lock_);
623 Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this);
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +0000624 // Only do this after restoring, as walking the stack when restoring will see
625 // the instrumentation exit pc.
626 instrumentation_stubs_installed_ = false;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100627 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800628 }
jeffhao725a9572012-11-13 18:20:12 -0800629}
630
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200631static void ResetQuickAllocEntryPointsForThread(Thread* thread, void* arg ATTRIBUTE_UNUSED) {
Ian Rogersfa824272013-11-05 16:12:57 -0800632 thread->ResetQuickAllocEntryPointsForThread();
633}
634
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700635void Instrumentation::SetEntrypointsInstrumented(bool instrumented) {
636 Thread* self = Thread::Current();
Mathieu Chartier661974a2014-01-09 11:23:53 -0800637 Runtime* runtime = Runtime::Current();
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700638 Locks::mutator_lock_->AssertNotHeld(self);
639 Locks::instrument_entrypoints_lock_->AssertHeld(self);
640 if (runtime->IsStarted()) {
Mathieu Chartier4f55e222015-09-04 13:26:21 -0700641 ScopedSuspendAll ssa(__FUNCTION__);
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700642 MutexLock mu(self, *Locks::runtime_shutdown_lock_);
Mathieu Chartier661974a2014-01-09 11:23:53 -0800643 SetQuickAllocEntryPointsInstrumented(instrumented);
644 ResetQuickAllocEntryPoints();
Mathieu Chartier4f55e222015-09-04 13:26:21 -0700645 } else {
646 MutexLock mu(self, *Locks::runtime_shutdown_lock_);
647 SetQuickAllocEntryPointsInstrumented(instrumented);
648 ResetQuickAllocEntryPoints();
Mathieu Chartier661974a2014-01-09 11:23:53 -0800649 }
650}
651
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700652void Instrumentation::InstrumentQuickAllocEntryPoints() {
653 MutexLock mu(Thread::Current(), *Locks::instrument_entrypoints_lock_);
654 InstrumentQuickAllocEntryPointsLocked();
Ian Rogersfa824272013-11-05 16:12:57 -0800655}
656
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700657void Instrumentation::UninstrumentQuickAllocEntryPoints() {
658 MutexLock mu(Thread::Current(), *Locks::instrument_entrypoints_lock_);
659 UninstrumentQuickAllocEntryPointsLocked();
660}
661
662void Instrumentation::InstrumentQuickAllocEntryPointsLocked() {
663 Locks::instrument_entrypoints_lock_->AssertHeld(Thread::Current());
664 if (quick_alloc_entry_points_instrumentation_counter_ == 0) {
665 SetEntrypointsInstrumented(true);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800666 }
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700667 ++quick_alloc_entry_points_instrumentation_counter_;
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700668}
669
670void Instrumentation::UninstrumentQuickAllocEntryPointsLocked() {
671 Locks::instrument_entrypoints_lock_->AssertHeld(Thread::Current());
672 CHECK_GT(quick_alloc_entry_points_instrumentation_counter_, 0U);
673 --quick_alloc_entry_points_instrumentation_counter_;
674 if (quick_alloc_entry_points_instrumentation_counter_ == 0) {
675 SetEntrypointsInstrumented(false);
676 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800677}
678
679void Instrumentation::ResetQuickAllocEntryPoints() {
680 Runtime* runtime = Runtime::Current();
681 if (runtime->IsStarted()) {
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800682 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700683 runtime->GetThreadList()->ForEach(ResetQuickAllocEntryPointsForThread, nullptr);
Ian Rogersfa824272013-11-05 16:12:57 -0800684 }
685}
686
Mathieu Chartiere401d142015-04-22 13:56:20 -0700687void Instrumentation::UpdateMethodsCode(ArtMethod* method, const void* quick_code) {
Sebastien Hertza8a697f2015-01-15 12:28:47 +0100688 DCHECK(method->GetDeclaringClass()->IsResolved());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800689 const void* new_quick_code;
Ian Rogers62d6c772013-02-27 08:32:07 -0800690 if (LIKELY(!instrumentation_stubs_installed_)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800691 new_quick_code = quick_code;
Jeff Hao65d15d92013-07-16 16:39:33 -0700692 } else {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100693 if ((interpreter_stubs_installed_ || IsDeoptimized(method)) && !method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800694 new_quick_code = GetQuickToInterpreterBridge();
Jeff Hao65d15d92013-07-16 16:39:33 -0700695 } else {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700696 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700697 if (class_linker->IsQuickResolutionStub(quick_code) ||
698 class_linker->IsQuickToInterpreterBridge(quick_code)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700699 new_quick_code = quick_code;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700700 } else if (entry_exit_stubs_installed_) {
701 new_quick_code = GetQuickInstrumentationEntryPoint();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700702 } else {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700703 new_quick_code = quick_code;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700704 }
Jeff Hao65d15d92013-07-16 16:39:33 -0700705 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800706 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800707 UpdateEntrypoints(method, new_quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100708}
709
Mathieu Chartiere401d142015-04-22 13:56:20 -0700710bool Instrumentation::AddDeoptimizedMethod(ArtMethod* method) {
711 if (IsDeoptimizedMethod(method)) {
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700712 // Already in the map. Return.
713 return false;
714 }
715 // Not found. Add it.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700716 deoptimized_methods_.insert(method);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700717 return true;
718}
719
Mathieu Chartiere401d142015-04-22 13:56:20 -0700720bool Instrumentation::IsDeoptimizedMethod(ArtMethod* method) {
721 return deoptimized_methods_.find(method) != deoptimized_methods_.end();
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700722}
723
Mathieu Chartiere401d142015-04-22 13:56:20 -0700724ArtMethod* Instrumentation::BeginDeoptimizedMethod() {
725 if (deoptimized_methods_.empty()) {
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700726 // Empty.
727 return nullptr;
728 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700729 return *deoptimized_methods_.begin();
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700730}
731
Mathieu Chartiere401d142015-04-22 13:56:20 -0700732bool Instrumentation::RemoveDeoptimizedMethod(ArtMethod* method) {
733 auto it = deoptimized_methods_.find(method);
734 if (it == deoptimized_methods_.end()) {
735 return false;
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700736 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700737 deoptimized_methods_.erase(it);
738 return true;
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700739}
740
741bool Instrumentation::IsDeoptimizedMethodsEmpty() const {
742 return deoptimized_methods_.empty();
743}
744
Mathieu Chartiere401d142015-04-22 13:56:20 -0700745void Instrumentation::Deoptimize(ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100746 CHECK(!method->IsNative());
747 CHECK(!method->IsProxyMethod());
Alex Light9139e002015-10-09 15:59:48 -0700748 CHECK(method->IsInvokable());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100749
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700750 Thread* self = Thread::Current();
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700751 {
752 WriterMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700753 bool has_not_been_deoptimized = AddDeoptimizedMethod(method);
Daniel Mihalyica1d06c2014-08-18 18:45:31 +0200754 CHECK(has_not_been_deoptimized) << "Method " << PrettyMethod(method)
755 << " is already deoptimized";
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700756 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100757 if (!interpreter_stubs_installed_) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800758 UpdateEntrypoints(method, GetQuickInstrumentationEntryPoint());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100759
760 // Install instrumentation exit stub and instrumentation frames. We may already have installed
761 // these previously so it will only cover the newly created frames.
762 instrumentation_stubs_installed_ = true;
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700763 MutexLock mu(self, *Locks::thread_list_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100764 Runtime::Current()->GetThreadList()->ForEach(InstrumentationInstallStack, this);
765 }
766}
767
Mathieu Chartiere401d142015-04-22 13:56:20 -0700768void Instrumentation::Undeoptimize(ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100769 CHECK(!method->IsNative());
770 CHECK(!method->IsProxyMethod());
Alex Light9139e002015-10-09 15:59:48 -0700771 CHECK(method->IsInvokable());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100772
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700773 Thread* self = Thread::Current();
774 bool empty;
775 {
776 WriterMutexLock mu(self, deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700777 bool found_and_erased = RemoveDeoptimizedMethod(method);
778 CHECK(found_and_erased) << "Method " << PrettyMethod(method)
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700779 << " is not deoptimized";
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700780 empty = IsDeoptimizedMethodsEmpty();
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700781 }
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100782
783 // Restore code and possibly stack only if we did not deoptimize everything.
784 if (!interpreter_stubs_installed_) {
785 // Restore its code or resolution trampoline.
786 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800787 if (method->IsStatic() && !method->IsConstructor() &&
788 !method->GetDeclaringClass()->IsInitialized()) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800789 UpdateEntrypoints(method, GetQuickResolutionStub());
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100790 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800791 const void* quick_code = class_linker->GetQuickOatCodeFor(method);
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -0800792 if (NeedDebugVersionForBootImageCode(method, quick_code)) {
793 quick_code = GetQuickToInterpreterBridge();
794 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800795 UpdateEntrypoints(method, quick_code);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100796 }
797
798 // If there is no deoptimized method left, we can restore the stack of each thread.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700799 if (empty) {
800 MutexLock mu(self, *Locks::thread_list_lock_);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100801 Runtime::Current()->GetThreadList()->ForEach(InstrumentationRestoreStack, this);
802 instrumentation_stubs_installed_ = false;
803 }
804 }
805}
806
Mathieu Chartiere401d142015-04-22 13:56:20 -0700807bool Instrumentation::IsDeoptimized(ArtMethod* method) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100808 DCHECK(method != nullptr);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700809 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700810 return IsDeoptimizedMethod(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100811}
812
813void Instrumentation::EnableDeoptimization() {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700814 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700815 CHECK(IsDeoptimizedMethodsEmpty());
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100816 CHECK_EQ(deoptimization_enabled_, false);
817 deoptimization_enabled_ = true;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100818}
819
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200820void Instrumentation::DisableDeoptimization(const char* key) {
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100821 CHECK_EQ(deoptimization_enabled_, true);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100822 // If we deoptimized everything, undo it.
823 if (interpreter_stubs_installed_) {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200824 UndeoptimizeEverything(key);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100825 }
826 // Undeoptimized selected methods.
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700827 while (true) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700828 ArtMethod* method;
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700829 {
830 ReaderMutexLock mu(Thread::Current(), deoptimized_methods_lock_);
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700831 if (IsDeoptimizedMethodsEmpty()) {
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700832 break;
833 }
Hiroshi Yamauchi799eb3a2014-07-18 15:38:17 -0700834 method = BeginDeoptimizedMethod();
835 CHECK(method != nullptr);
Mathieu Chartier3b05e9b2014-03-25 09:29:43 -0700836 }
837 Undeoptimize(method);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100838 }
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100839 deoptimization_enabled_ = false;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100840}
841
Sebastien Hertz11d40c22014-02-19 18:00:17 +0100842// Indicates if instrumentation should notify method enter/exit events to the listeners.
843bool Instrumentation::ShouldNotifyMethodEnterExitEvents() const {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200844 if (!HasMethodEntryListeners() && !HasMethodExitListeners()) {
845 return false;
846 }
Sebastien Hertz7ec2f1c2014-03-27 20:06:47 +0100847 return !deoptimization_enabled_ && !interpreter_stubs_installed_;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100848}
849
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200850void Instrumentation::DeoptimizeEverything(const char* key) {
851 CHECK(deoptimization_enabled_);
852 ConfigureStubs(key, InstrumentationLevel::kInstrumentWithInterpreter);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100853}
854
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200855void Instrumentation::UndeoptimizeEverything(const char* key) {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100856 CHECK(interpreter_stubs_installed_);
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200857 CHECK(deoptimization_enabled_);
858 ConfigureStubs(key, InstrumentationLevel::kInstrumentNothing);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100859}
860
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200861void Instrumentation::EnableMethodTracing(const char* key, bool needs_interpreter) {
862 InstrumentationLevel level;
863 if (needs_interpreter) {
864 level = InstrumentationLevel::kInstrumentWithInterpreter;
865 } else {
866 level = InstrumentationLevel::kInstrumentWithInstrumentationStubs;
867 }
868 ConfigureStubs(key, level);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100869}
870
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200871void Instrumentation::DisableMethodTracing(const char* key) {
872 ConfigureStubs(key, InstrumentationLevel::kInstrumentNothing);
jeffhao725a9572012-11-13 18:20:12 -0800873}
874
Mathieu Chartiere401d142015-04-22 13:56:20 -0700875const void* Instrumentation::GetQuickCodeFor(ArtMethod* method, size_t pointer_size) const {
Ian Rogers62d6c772013-02-27 08:32:07 -0800876 Runtime* runtime = Runtime::Current();
877 if (LIKELY(!instrumentation_stubs_installed_)) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800878 const void* code = method->GetEntryPointFromQuickCompiledCodePtrSize(pointer_size);
Vladimir Marko8a630572014-04-09 18:45:35 +0100879 DCHECK(code != nullptr);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700880 ClassLinker* class_linker = runtime->GetClassLinker();
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700881 if (LIKELY(!class_linker->IsQuickResolutionStub(code) &&
882 !class_linker->IsQuickToInterpreterBridge(code)) &&
883 !class_linker->IsQuickResolutionStub(code) &&
884 !class_linker->IsQuickToInterpreterBridge(code)) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800885 return code;
886 }
887 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800888 return runtime->GetClassLinker()->GetQuickOatCodeFor(method);
jeffhao725a9572012-11-13 18:20:12 -0800889}
890
Ian Rogers62d6c772013-02-27 08:32:07 -0800891void Instrumentation::MethodEnterEventImpl(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700892 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800893 uint32_t dex_pc) const {
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000894 if (HasMethodEntryListeners()) {
895 for (InstrumentationListener* listener : method_entry_listeners_) {
896 if (listener != nullptr) {
897 listener->MethodEntered(thread, this_object, method, dex_pc);
898 }
899 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800900 }
901}
902
903void Instrumentation::MethodExitEventImpl(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700904 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800905 uint32_t dex_pc, const JValue& return_value) const {
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000906 if (HasMethodExitListeners()) {
907 for (InstrumentationListener* listener : method_exit_listeners_) {
908 if (listener != nullptr) {
909 listener->MethodExited(thread, this_object, method, dex_pc, return_value);
910 }
911 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800912 }
913}
914
915void Instrumentation::MethodUnwindEvent(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700916 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800917 uint32_t dex_pc) const {
Sebastien Hertz0462c4c2015-04-01 16:34:17 +0200918 if (HasMethodUnwindListeners()) {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700919 for (InstrumentationListener* listener : method_unwind_listeners_) {
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000920 if (listener != nullptr) {
921 listener->MethodUnwind(thread, this_object, method, dex_pc);
922 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800923 }
924 }
925}
926
927void Instrumentation::DexPcMovedEventImpl(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700928 ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800929 uint32_t dex_pc) const {
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000930 for (InstrumentationListener* listener : dex_pc_listeners_) {
931 if (listener != nullptr) {
932 listener->DexPcMoved(thread, this_object, method, dex_pc);
933 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800934 }
935}
936
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000937void Instrumentation::BranchImpl(Thread* thread,
938 ArtMethod* method,
939 uint32_t dex_pc,
940 int32_t offset) const {
941 for (InstrumentationListener* listener : branch_listeners_) {
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000942 if (listener != nullptr) {
Nicolas Geoffray81f0f952016-01-20 16:25:19 +0000943 listener->Branch(thread, method, dex_pc, offset);
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000944 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800945 }
946}
947
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100948void Instrumentation::InvokeVirtualOrInterfaceImpl(Thread* thread,
949 mirror::Object* this_object,
950 ArtMethod* caller,
951 uint32_t dex_pc,
952 ArtMethod* callee) const {
Roland Levillain91d65e02016-01-19 15:59:16 +0000953 // We cannot have thread suspension since that would cause the this_object parameter to
Mathieu Chartier3fdb3fe2016-01-14 10:24:28 -0800954 // potentially become a dangling pointer. An alternative could be to put it in a handle instead.
955 ScopedAssertNoThreadSuspension ants(thread, __FUNCTION__);
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100956 for (InstrumentationListener* listener : invoke_virtual_or_interface_listeners_) {
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000957 if (listener != nullptr) {
958 listener->InvokeVirtualOrInterface(thread, this_object, caller, dex_pc, callee);
959 }
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100960 }
961}
962
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200963void Instrumentation::FieldReadEventImpl(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700964 ArtMethod* method, uint32_t dex_pc,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700965 ArtField* field) const {
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000966 for (InstrumentationListener* listener : field_read_listeners_) {
967 if (listener != nullptr) {
968 listener->FieldRead(thread, this_object, method, dex_pc, field);
969 }
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200970 }
971}
972
973void Instrumentation::FieldWriteEventImpl(Thread* thread, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700974 ArtMethod* method, uint32_t dex_pc,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700975 ArtField* field, const JValue& field_value) const {
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000976 for (InstrumentationListener* listener : field_write_listeners_) {
977 if (listener != nullptr) {
978 listener->FieldWritten(thread, this_object, method, dex_pc, field, field_value);
979 }
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200980 }
981}
982
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000983void Instrumentation::ExceptionCaughtEvent(Thread* thread,
Sebastien Hertz947ff082013-09-17 14:10:13 +0200984 mirror::Throwable* exception_object) const {
Sebastien Hertz9f102032014-05-23 08:59:42 +0200985 if (HasExceptionCaughtListeners()) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000986 DCHECK_EQ(thread->GetException(), exception_object);
Jeff Haoc0bd4da2013-04-11 15:52:28 -0700987 thread->ClearException();
Nicolas Geoffray514a6162015-11-03 11:44:24 +0000988 for (InstrumentationListener* listener : exception_caught_listeners_) {
989 if (listener != nullptr) {
990 listener->ExceptionCaught(thread, exception_object);
991 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800992 }
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000993 thread->SetException(exception_object);
Ian Rogers62d6c772013-02-27 08:32:07 -0800994 }
995}
996
Sebastien Hertzb2feaaf2015-10-12 13:40:10 +0000997// Computes a frame ID by ignoring inlined frames.
998size_t Instrumentation::ComputeFrameId(Thread* self,
999 size_t frame_depth,
1000 size_t inlined_frames_before_frame) {
1001 CHECK_GE(frame_depth, inlined_frames_before_frame);
1002 size_t no_inline_depth = frame_depth - inlined_frames_before_frame;
1003 return StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk) - no_inline_depth;
1004}
1005
Ian Rogers62d6c772013-02-27 08:32:07 -08001006static void CheckStackDepth(Thread* self, const InstrumentationStackFrame& instrumentation_frame,
1007 int delta)
Mathieu Chartier90443472015-07-16 20:32:27 -07001008 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +01001009 size_t frame_id = StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk) + delta;
Ian Rogers62d6c772013-02-27 08:32:07 -08001010 if (frame_id != instrumentation_frame.frame_id_) {
1011 LOG(ERROR) << "Expected frame_id=" << frame_id << " but found "
1012 << instrumentation_frame.frame_id_;
1013 StackVisitor::DescribeStack(self);
1014 CHECK_EQ(frame_id, instrumentation_frame.frame_id_);
1015 }
1016}
1017
1018void Instrumentation::PushInstrumentationStackFrame(Thread* self, mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001019 ArtMethod* method,
Jeff Hao9a916d32013-06-27 18:45:37 -07001020 uintptr_t lr, bool interpreter_entry) {
Ian Rogers62d6c772013-02-27 08:32:07 -08001021 // We have a callee-save frame meaning this value is guaranteed to never be 0.
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +01001022 size_t frame_id = StackVisitor::ComputeNumFrames(self, kInstrumentationStackWalk);
Ian Rogers62d6c772013-02-27 08:32:07 -08001023 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
1024 if (kVerboseInstrumentation) {
Brian Carlstrom2d888622013-07-18 17:02:00 -07001025 LOG(INFO) << "Entering " << PrettyMethod(method) << " from PC " << reinterpret_cast<void*>(lr);
Ian Rogers62d6c772013-02-27 08:32:07 -08001026 }
1027 instrumentation::InstrumentationStackFrame instrumentation_frame(this_object, method, lr,
Jeff Hao9a916d32013-06-27 18:45:37 -07001028 frame_id, interpreter_entry);
Ian Rogers62d6c772013-02-27 08:32:07 -08001029 stack->push_front(instrumentation_frame);
1030
Sebastien Hertz320deb22014-06-11 19:45:05 +02001031 if (!interpreter_entry) {
1032 MethodEnterEvent(self, this_object, method, 0);
1033 }
Ian Rogers62d6c772013-02-27 08:32:07 -08001034}
1035
Andreas Gamped58342c2014-06-05 14:18:08 -07001036TwoWordReturn Instrumentation::PopInstrumentationStackFrame(Thread* self, uintptr_t* return_pc,
1037 uint64_t gpr_result,
1038 uint64_t fpr_result) {
Ian Rogers62d6c772013-02-27 08:32:07 -08001039 // Do the pop.
1040 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
1041 CHECK_GT(stack->size(), 0U);
1042 InstrumentationStackFrame instrumentation_frame = stack->front();
1043 stack->pop_front();
1044
1045 // Set return PC and check the sanity of the stack.
1046 *return_pc = instrumentation_frame.return_pc_;
1047 CheckStackDepth(self, instrumentation_frame, 0);
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001048 self->VerifyStack();
Ian Rogers62d6c772013-02-27 08:32:07 -08001049
Mathieu Chartiere401d142015-04-22 13:56:20 -07001050 ArtMethod* method = instrumentation_frame.method_;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001051 uint32_t length;
Mingyao Yang99170c62015-07-06 11:10:37 -07001052 const size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
1053 char return_shorty = method->GetInterfaceMethodIfProxy(pointer_size)->GetShorty(&length)[0];
Ian Rogers62d6c772013-02-27 08:32:07 -08001054 JValue return_value;
1055 if (return_shorty == 'V') {
1056 return_value.SetJ(0);
1057 } else if (return_shorty == 'F' || return_shorty == 'D') {
1058 return_value.SetJ(fpr_result);
1059 } else {
1060 return_value.SetJ(gpr_result);
1061 }
1062 // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to
1063 // return_pc.
1064 uint32_t dex_pc = DexFile::kDexNoIndex;
1065 mirror::Object* this_object = instrumentation_frame.this_object_;
Sebastien Hertz320deb22014-06-11 19:45:05 +02001066 if (!instrumentation_frame.interpreter_entry_) {
1067 MethodExitEvent(self, this_object, instrumentation_frame.method_, dex_pc, return_value);
1068 }
jeffhao725a9572012-11-13 18:20:12 -08001069
Sebastien Hertz138dbfc2013-12-04 18:15:25 +01001070 // Deoptimize if the caller needs to continue execution in the interpreter. Do nothing if we get
1071 // back to an upcall.
1072 NthCallerVisitor visitor(self, 1, true);
1073 visitor.WalkStack(true);
Sebastien Hertz270a0e12015-01-16 19:49:09 +01001074 bool deoptimize = (visitor.caller != nullptr) &&
Daniel Mihalyieb076692014-08-22 17:33:31 +02001075 (interpreter_stubs_installed_ || IsDeoptimized(visitor.caller) ||
1076 Dbg::IsForcedInterpreterNeededForUpcall(self, visitor.caller));
Ian Rogers62d6c772013-02-27 08:32:07 -08001077 if (deoptimize) {
1078 if (kVerboseInstrumentation) {
Sebastien Hertz270a0e12015-01-16 19:49:09 +01001079 LOG(INFO) << StringPrintf("Deoptimizing %s by returning from %s with result %#" PRIx64 " in ",
1080 PrettyMethod(visitor.caller).c_str(),
1081 PrettyMethod(method).c_str(),
1082 return_value.GetJ()) << *self;
Ian Rogers62d6c772013-02-27 08:32:07 -08001083 }
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01001084 self->PushDeoptimizationContext(return_value,
1085 return_shorty == 'L',
1086 false /* from_code */,
Sebastien Hertz07474662015-08-25 15:12:33 +00001087 nullptr /* no pending exception */);
Andreas Gamped58342c2014-06-05 14:18:08 -07001088 return GetTwoWordSuccessValue(*return_pc,
1089 reinterpret_cast<uintptr_t>(GetQuickDeoptimizationEntryPoint()));
Ian Rogers62d6c772013-02-27 08:32:07 -08001090 } else {
1091 if (kVerboseInstrumentation) {
Brian Carlstrom2d888622013-07-18 17:02:00 -07001092 LOG(INFO) << "Returning from " << PrettyMethod(method)
1093 << " to PC " << reinterpret_cast<void*>(*return_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -08001094 }
Andreas Gamped58342c2014-06-05 14:18:08 -07001095 return GetTwoWordSuccessValue(0, *return_pc);
Ian Rogers62d6c772013-02-27 08:32:07 -08001096 }
jeffhao725a9572012-11-13 18:20:12 -08001097}
1098
Ian Rogers62d6c772013-02-27 08:32:07 -08001099void Instrumentation::PopMethodForUnwind(Thread* self, bool is_deoptimization) const {
1100 // Do the pop.
1101 std::deque<instrumentation::InstrumentationStackFrame>* stack = self->GetInstrumentationStack();
1102 CHECK_GT(stack->size(), 0U);
1103 InstrumentationStackFrame instrumentation_frame = stack->front();
1104 // TODO: bring back CheckStackDepth(self, instrumentation_frame, 2);
1105 stack->pop_front();
1106
Mathieu Chartiere401d142015-04-22 13:56:20 -07001107 ArtMethod* method = instrumentation_frame.method_;
Ian Rogers62d6c772013-02-27 08:32:07 -08001108 if (is_deoptimization) {
1109 if (kVerboseInstrumentation) {
1110 LOG(INFO) << "Popping for deoptimization " << PrettyMethod(method);
1111 }
1112 } else {
1113 if (kVerboseInstrumentation) {
1114 LOG(INFO) << "Popping for unwind " << PrettyMethod(method);
1115 }
1116
1117 // Notify listeners of method unwind.
1118 // TODO: improve the dex pc information here, requires knowledge of current PC as opposed to
1119 // return_pc.
1120 uint32_t dex_pc = DexFile::kDexNoIndex;
1121 MethodUnwindEvent(self, instrumentation_frame.this_object_, method, dex_pc);
1122 }
1123}
1124
1125std::string InstrumentationStackFrame::Dump() const {
1126 std::ostringstream os;
1127 os << "Frame " << frame_id_ << " " << PrettyMethod(method_) << ":"
1128 << reinterpret_cast<void*>(return_pc_) << " this=" << reinterpret_cast<void*>(this_object_);
1129 return os.str();
1130}
1131
1132} // namespace instrumentation
jeffhao725a9572012-11-13 18:20:12 -08001133} // namespace art